]> Pileus Git - aweather/blob - opt/level2.c
cleaning up files
[aweather] / opt / level2.c
1 /* Prototype stuff for parsing Level-II data */
2 /*
3  * TODO: ARGG, the packet sizses are all wrong..
4  *       Check sizes of decompressed bzip files
5  *       Split things back up to seperate files
6  * The second bzip contains different size packets?
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <error.h>
13 #include <glib.h>
14 #include <bzlib.h>
15 #include "level2.h"
16
17 /* Not used in favor of RSL */
18 level2_packet_t **level2_split_packets(char *data, int *num_packets, int data_size)
19 {
20         int packet_i = 0;
21         level2_packet_t  *packet = (level2_packet_t *)data;
22         int max_packets = 128;
23         level2_packet_t **packets = malloc(sizeof(level2_packet_t *)*max_packets);
24         while ((void *)packet < (void *)(data+data_size)) {
25                 /* Increase packets size if necessasairy */
26                 if (packet_i >= max_packets) {
27                         max_packets *= 2;
28                         packets = realloc(packets, sizeof(level2_packet_t *)*max_packets);
29                 }
30
31                 /* Fix byte order for packet */
32                 packet->size = g_ntohs(packet->size);
33                 packet->seq  = g_ntohs(packet->seq);
34                 // TODO: Convert the rest of the bytes
35
36                 /* Save packet location */
37                 packets[packet_i] = packet;
38
39                 /* Increment packet and packet_i */
40                 // new =                      old              + CTM + 2*size         + fcs
41                 //packet = (level2_packet_t *)( ((char *)packet) + 12  + 2*packet->size + 4 );
42                 packet++;
43                 packet_i++;
44         }
45         packets = realloc(packets, sizeof(level2_packet_t *)*packet_i);
46         *num_packets = packet_i;
47         return packets;
48 }
49
50 /* Not used in favor of RSL */
51 level2_packet_t *level2_decompress(char *raw_data, int *num_packets)
52 {
53         /* Read header */
54         FILE *fd = fopen(raw_data, "r");
55         if (fd == NULL)
56                 error(1, errno, "Error opening files `%s'", raw_data);
57         level2_header_t header;
58         if (1 != fread(&header, sizeof(level2_header_t), 1, fd))
59                 error(1, errno, "Error reading header");
60
61         /* Decompress the bzips
62          *   store the entire sequence starting at data
63          *   cur_data is for each individual bzip and is the last chunk of data */
64         char *data = NULL;
65         int data_size = 0; // size of previously decmpressed data
66         char *bz2 = NULL;  // temp buf for bzipped data
67         unsigned int _bz2_size = 0;
68         while ((int)_bz2_size >= 0) {
69                 if (1 != fread(&_bz2_size, 4, 1, fd))
70                         break; //error(1, errno, "Error reading _bz2_size, pos=%x", (unsigned int)ftell(fd));
71                 _bz2_size = g_ntohl(_bz2_size);
72                 int bz2_size = abs(_bz2_size);
73
74                 /* Read data */
75                 if (NULL == (bz2 = (char *)realloc(bz2, bz2_size)))
76                         error(1, errno, "cannot allocate `%d' bytes for buffer", bz2_size);
77                 if (bz2_size != fread(bz2, 1, bz2_size, fd))
78                         error(1, errno, "error reading from input file");
79
80                 /* Decompress on individual bzip to the end of the sequence */
81                 unsigned int cur_data_size = 1<<17;
82                 int status = BZ_OUTBUFF_FULL;
83                 while (status == BZ_OUTBUFF_FULL) {
84                         cur_data_size *= 2;
85                         data = realloc(data, data_size + cur_data_size);
86                         status = BZ2_bzBuffToBuffDecompress(data + data_size, &cur_data_size, bz2, bz2_size, 0, 0);
87                 }
88                 if (status != BZ_OK)
89                         error(1, 1, "Error decompressing data");
90                 data_size += cur_data_size; // Add current chunk to decompressed data
91
92                 /* Debug */
93                 printf("data_size = %d, cur_data_size = %d\n", data_size, cur_data_size);
94         }
95         data = realloc(data, data_size); // free unused space at the end
96
97         return (level2_packet_t *)data;
98         //return level2_split_packets(data, num_packets, data_size);
99 }
100
101
102
103 int main(int argc, char **argv)
104 {
105         if (argc < 2) {
106                 printf("usage: %s <level2-data>\n", argv[0]);
107                 return 0;
108         }
109
110         int num_packets;
111         level2_packet_t *packets = level2_decompress(argv[1], &num_packets);
112         printf("read %d packets\n", num_packets);
113         
114         //FILE *output = fopen("output.dat", "w+");
115         //fwrite(packets[i], 1, 18470816, output);
116         //fclose(output);
117
118         FILE *output = fopen("output.dat", "w+");
119         int i;
120         for (i = 0; i < 10000; i++) {
121                 printf("packet: size=%x, seq=%d\n", g_ntohs(packets[i].size), g_ntohs(packets[i].seq));
122                 fwrite("################", 1, 16, output);
123                 fwrite(packets+i, 1, sizeof(level2_packet_t), output);
124         }
125         fclose(output);
126
127         return 0;
128 }