Advertisement
Guest User

awdad

a guest
Jan 21st, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define DATA_OFFSET_POS 12
  4.  
  5. int main (int argc, char *argv[]) {
  6.  
  7. FILE *fp_in;
  8. FILE *fp_out;
  9.  
  10. if (argc != 3) {
  11. perror("Usage: <input file> <output file>");
  12. return 1;
  13. }
  14.  
  15. if ((fp_in = fopen(argv[1], "rb")) == 0) {
  16. perror("Cannot find input file");
  17. return 2;
  18. }
  19. if ((fp_out = fopen(argv[2], "wb")) == 0) {
  20. perror("Cannot find output file");
  21. return 3;
  22. }
  23.  
  24. fseek(fp_in, 0L, SEEK_END);
  25. int filelen = ftell(fp_in);
  26. rewind(fp_in);
  27.  
  28. int fp_count = 0;
  29. unsigned int IHL;
  30. unsigned int packet_size;
  31. unsigned int data_offset;
  32. unsigned int header_size;
  33. unsigned int data_length;
  34. while (fp_count < filelen) {
  35.  
  36. fseek(fp_in, fp_count, SEEK_SET);
  37. printf("Offset : %u\n", fp_count);
  38.  
  39. // determine IHL for this packet
  40. unsigned char bytes[4];
  41. printf("Offset before read : %i\n", (int) ftell(fp_in));
  42. fread(bytes, 1, 4, fp_in);
  43. printf("Offset after read : %i\n", (int) ftell(fp_in));
  44. IHL = bytes[0] & 0x0F;
  45.  
  46. // determine packet_size for this packet
  47. packet_size = (((int)bytes[2])<<8) | ((int)bytes[3]);
  48. printf("Therefore, offset of packet_size : %i\n", (int) ftell(fp_in)-2);
  49. printf("Packet Size : %u\n", packet_size);
  50.  
  51. // determine data_offset for this packet
  52. fseek(fp_in, fp_count + DATA_OFFSET_POS + (IHL*4), SEEK_SET);
  53. unsigned char bytes2[1];
  54. fread(bytes2, 1, 1, fp_in);
  55. data_offset = (bytes2[0] & (15<<4))>>4;
  56.  
  57. // determine where to read data from
  58. header_size = (IHL+data_offset)*4;
  59. printf("Total Header Size : %u\n", header_size);
  60.  
  61. // seek to where we should read from
  62. fseek(fp_in, fp_count+header_size, SEEK_SET);
  63.  
  64. // determine amount of data to read
  65. data_length = packet_size - header_size;
  66.  
  67. // read in the data
  68. unsigned char data[data_length];
  69. fread(data, 1, data_length, fp_in);
  70.  
  71. // write out the data
  72. fwrite(data, 1, data_length, fp_out);
  73.  
  74. // update file pointer count
  75. fp_count += packet_size;
  76. printf("Data Length : %u\n", data_length);
  77. printf("\n");
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement