wojcieszek

lab_fat12

Nov 29th, 2021 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct fat_BS
  6. {
  7. unsigned char bootjmp[3];
  8. unsigned char oem_name[8];
  9. unsigned short bytes_per_sector;
  10. unsigned char sectors_per_cluster;
  11. unsigned short reserved_sector_count;
  12. unsigned char table_count;
  13. unsigned short root_entry_count;
  14. unsigned short total_sectors_16;
  15. unsigned char media_type;
  16. unsigned short table_size_16;
  17. unsigned short sectors_per_track;
  18. unsigned short head_side_count;
  19. unsigned int hidden_sector_count;
  20. unsigned int total_sectors_32;
  21.  
  22. }__attribute__((packed)) fat_BS_t;
  23.  
  24. typedef struct boot_sec{
  25. unsigned char bios_int;
  26. unsigned char not_used;
  27. unsigned char extended_boot;
  28. unsigned int volume_number;
  29. unsigned char volume_label[11];
  30. unsigned char file_system[8];
  31. unsigned char not_used2[448];
  32. unsigned char signature_value[2];
  33. }__attribute__((packed)) boot_sec_struct;
  34.  
  35. typedef struct root_dir{
  36. unsigned char filename_first_char;
  37. unsigned char filename[10];
  38. unsigned char file_attributes;
  39. unsigned char reserved;
  40. unsigned char file_creation_time;
  41. unsigned short creation_time;
  42. unsigned short creation_date;
  43. unsigned short access_date;
  44. unsigned short high_order_bytes;
  45. unsigned short modified_time;
  46. unsigned short modified_date;
  47. unsigned short low_order_bytes;
  48. unsigned int file_size;
  49. }__attribute__((packed)) root_dir_struct;
  50.  
  51. int main() {
  52. struct fat_BS *test = malloc(sizeof(fat_BS_t));
  53. struct boot_sec *test2 = malloc(sizeof(boot_sec_struct));
  54.  
  55. FILE *testing = fopen("fat12_c1.bin", "rb");
  56.  
  57. fread(test, sizeof(fat_BS_t), 1, testing);
  58. fread(test2, sizeof(boot_sec_struct), 1, testing);
  59.  
  60. int fat_size = test->table_size_16 * test->bytes_per_sector;
  61.  
  62. unsigned char *test3 = malloc(fat_size);
  63. unsigned char *test4 = malloc(fat_size);
  64.  
  65. int reserved_size = test->reserved_sector_count * test->bytes_per_sector;
  66.  
  67. fseek(testing, reserved_size, SEEK_SET);
  68. fread(test3, fat_size, 1, testing);
  69. fread(test4, fat_size, 1, testing);
  70.  
  71. struct root_dir *array_of_root = malloc(sizeof(root_dir_struct)*test->root_entry_count);
  72. fseek(testing, reserved_size + fat_size * (unsigned int) (test->table_count), SEEK_SET);
  73. for(int x = 0; x < test->root_entry_count; x++){
  74. fread((array_of_root+x), sizeof(root_dir_struct), 1, testing);
  75. }
  76.  
  77. int curr_pos = ftell(testing);
  78.  
  79. unsigned char *file_from_bin;
  80.  
  81. for(int x = 0; x < test->root_entry_count; x++){
  82. fseek(testing, curr_pos, SEEK_SET);
  83. fseek(testing, (array_of_root->low_order_bytes -2 ) * test->bytes_per_sector, SEEK_CUR);
  84. file_from_bin = malloc(array_of_root->file_size+10);
  85. fread(file_from_bin, array_of_root->file_size, 1, testing);
  86. printf("%d", x);
  87. }
  88.  
  89. fclose(testing);
  90. free(test);
  91. free(test2);
  92. free(test3);
  93. free(test4);
  94. free(file_from_bin);
  95. return 0;
  96. }
  97.  
Add Comment
Please, Sign In to add comment