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