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