Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define DATA_OFFSET_POS 12
- int main (int argc, char *argv[]) {
- FILE *fp_in;
- FILE *fp_out;
- if (argc != 3) {
- perror("Usage: <input file> <output file>");
- return 1;
- }
- if ((fp_in = fopen(argv[1], "rb")) == 0) {
- perror("Cannot find input file");
- return 2;
- }
- if ((fp_out = fopen(argv[2], "wb")) == 0) {
- perror("Cannot find output file");
- return 3;
- }
- fseek(fp_in, 0L, SEEK_END);
- int filelen = ftell(fp_in);
- rewind(fp_in);
- int fp_count = 0;
- unsigned int IHL;
- unsigned int packet_size;
- unsigned int data_offset;
- unsigned int header_size;
- unsigned int data_length;
- while (fp_count < filelen) {
- fseek(fp_in, fp_count, SEEK_SET);
- printf("Offset : %u\n", fp_count);
- // determine IHL for this packet
- unsigned char bytes[4];
- printf("Offset before read : %i\n", (int) ftell(fp_in));
- fread(bytes, 1, 4, fp_in);
- printf("Offset after read : %i\n", (int) ftell(fp_in));
- IHL = bytes[0] & 0x0F;
- // determine packet_size for this packet
- packet_size = (((int)bytes[2])<<8) | ((int)bytes[3]);
- printf("Therefore, offset of packet_size : %i\n", (int) ftell(fp_in)-2);
- printf("Packet Size : %u\n", packet_size);
- // determine data_offset for this packet
- fseek(fp_in, fp_count + DATA_OFFSET_POS + (IHL*4), SEEK_SET);
- unsigned char bytes2[1];
- fread(bytes2, 1, 1, fp_in);
- data_offset = (bytes2[0] & (15<<4))>>4;
- // determine where to read data from
- header_size = (IHL+data_offset)*4;
- printf("Total Header Size : %u\n", header_size);
- // seek to where we should read from
- fseek(fp_in, fp_count+header_size, SEEK_SET);
- // determine amount of data to read
- data_length = packet_size - header_size;
- // read in the data
- unsigned char data[data_length];
- fread(data, 1, data_length, fp_in);
- // write out the data
- fwrite(data, 1, data_length, fp_out);
- // update file pointer count
- fp_count += packet_size;
- printf("Data Length : %u\n", data_length);
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement