• 296894139
    了解作者
  • C/C++
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 10 积分
    下载积分
  • 3
    下载次数
  • 2018-05-14 09:25
    上传日期
通过c和asm结合读取fat12文件内容
fat12文件系统读取.rar
  • fat12文件系统读取
  • asm.txt
    674B
  • C.txt
    9.6KB
内容介绍
#include <stdio.h> #include <stdlib.h> #include <string.h> #define IMGSIZE 1440*1024 struct BPB { int BytsPerSec, SecPerClus, RsvdSecCnt, NumFATs, RootEntCnt, FATSz16; } bpb; struct Entry { char Name[9], Ext[4]; int FstClus, Type; // 0 for file, 1 for folder struct Entry *Next, *Children; } root; struct Count { char* Line; int NumFile, NumDir; }; unsigned char image[IMGSIZE]; void my_print(char* c); void my_print_char(char c); void my_strlwr(char* str) { int i; for (i = 0; str[i] != 0; ++i) { if (str[i] >= 'A' && str[i] <= 'Z') { str[i] += 'a' - 'A'; } } } void my_itoa(int i, char* a){ char const digit[] = "0123456789"; char* p = a; int shifter = i; do { p++; shifter /= 10; } while (shifter); *p = 0; do { *(--p) = digit[i % 10]; i /= 10; } while (i); } int get_int(int offset, int len) { int result = 0, i; for (i = offset + len - 1; i >= offset; --i) { result = result * 256 + image[i]; } return result; } int get_next_clus(int curr_clus) { int clus_id = curr_clus / 2, fat_value = get_int(bpb.RsvdSecCnt * bpb.BytsPerSec + clus_id * 3, 3); if (curr_clus % 2 == 0) { return fat_value & 0x000fff; } else { return (fat_value & 0xfff000) / 0x1000; } } void load_img(const char* filename) { FILE *f; f = fopen(filename, "rb"); fread(image, IMGSIZE, 1, f); fclose(f); } void get_bpb() { bpb.BytsPerSec = get_int(11, 2); bpb.SecPerClus = get_int(13, 1); bpb.RsvdSecCnt = get_int(14, 2); bpb.NumFATs = get_int(16, 1); bpb.RootEntCnt = get_int(17, 2); bpb.FATSz16 = get_int(22, 2); } void get_dir(struct Entry* entry) { int clus = entry->FstClus, is_root = 1, i, offset = (bpb.RsvdSecCnt + bpb.FATSz16 * bpb.NumFATs) * bpb.BytsPerSec; if (clus >= 2) { // in data area is_root = 0; offset += bpb.RootEntCnt * 32 + (clus - 2) * bpb.BytsPerSec * bpb.SecPerClus; } for (i = offset; ; i += 32) { // finished? if (is_root == 1 && i >= offset + bpb.RootEntCnt * 32) { break; } else if (is_root == 0 && i >= offset + bpb.BytsPerSec * bpb.SecPerClus) { if ((clus = get_next_clus(clus)) >= 0xff7) { break; } else { i = offset = (bpb.RsvdSecCnt + bpb.FATSz16 * bpb.NumFATs) * bpb.BytsPerSec + bpb.RootEntCnt * 32 + (clus - 2) * bpb.BytsPerSec * bpb.SecPerClus; } } // ignore invaild entries if (image[i] != '.' && image[i] != 0 && image[i] != 5 && image[i] != 0xE5 && image[i + 0xB] != 0xF && (image[i + 0xB] & 0x2) == 0) { struct Entry* new_entry = malloc(sizeof(struct Entry)); // entry name int j; for (j = i; j < i + 8 && image[j] != 0x20; j++) { new_entry->Name[j-i] = image[j]; } new_entry->Name[j-i] = 0; my_strlwr(new_entry->Name); // file or folder if ((image[i + 0xB] & 0x10) == 0) { // file new_entry->Type = 0; for (j = i + 8; j < i + 0xB && image[j] != 0x20; j++) { new_entry->Ext[j - i - 8] = image[j]; } new_entry->Ext[j - i - 8] = 0; my_strlwr(new_entry->Ext); } else { // folder new_entry->Type = 1; } new_entry->FstClus = get_int(i + 26, 2); new_entry->Next = NULL; new_entry->Children = NULL; // insert it if (entry->Children == NULL) { entry->Children = new_entry; } else { struct Entry* ptr = entry->Children; while (ptr->Next != NULL) { ptr = ptr->Next; } ptr->Next = new_entry; } // recursively get dir if (new_entry->Type == 1) { get_dir(new_entry); } } } } void print_dir(struct Entry* entry, char* fullpath) { struct Entry* ptr = entry->Children; while (ptr != NULL) { char* new_path = malloc(strlen(fullpath) + strlen(ptr->Name) + 2); strcpy(new_path, fullpath); if (*fullpath != 0) { strcat(new_path, "/"); } if (ptr->Type == 1 && ptr->Children != NULL) { strcat(new_path, ptr->Name); print_dir(ptr, new_path); } else { my_print("\x1b[36m"); my_print(new_path); if (ptr->Type == 0) { my_print("\x1b[0m"); my_print(ptr->Name); my_print("."); my_print(ptr->Ext); } else { my_print(ptr->Name); my_print("/\x1b[0m"); } my_print("\n"); } free(new_path); ptr = ptr->Next; } } void print_file(struct Entry* entry) { int clus = entry->FstClus; while (1) { int i, offset = (bpb.RsvdSecCnt + bpb.FATSz16 * bpb.NumFATs) * bpb.BytsPerSec + bpb.RootEntCnt * 32 + (clus - 2) * bpb.BytsPerSec * bpb.SecPerClus; for (i = 0; i < bpb.BytsPerSec * bpb.SecPerClus; ++i) { my_print_char(image[offset + i]); } if ((clus = get_next_clus(clus)) >= 0xff7) { break; } } } struct Entry* find_file(struct Entry* entry, char* path, char* target) { if (strcmp(path, target) == 0) { return entry; } struct Entry* ptr = entry->Children; while (ptr != NULL) { char* new_path = malloc(strlen(path) + strlen(ptr->Name) + strlen(ptr->Ext) + 2); strcpy(new_path, path); strcat(new_path, ptr->Name); if (ptr->Type == 0) { strcat(new_path, "."); strcat(new_path, ptr->Ext); } if (strcmp(new_path, target) == 0) { free(new_path); return ptr; } if (ptr->Type == 1) { strcat(new_path, "/"); struct Entry* result = find_file(ptr, new_path, target); if (result != NULL) { free(new_path); return result; } } free(new_path); ptr = ptr->Next; } return NULL; } struct Count* count_dir(struct Entry* entry, int depth) { struct Count* count = malloc(sizeof(struct Count)); count->Line = malloc(1); count->Line[0] = 0; count->NumFile = 0; count->NumDir = 0; struct Entry* ptr = entry->Children; while (ptr != NULL) { if (ptr->Type == 1) { struct Count* new_count = count_dir(ptr, depth + 1); // count->NumFile += new_count->NumFile; // count->NumDir += new_count->NumDir; count->NumDir++; char* new_line = malloc(strlen(count->Line) + strlen(new_count->Line) + 1); strcpy(new_line, count->Line); strcat(new_line, new_count->Line); free(new_count->Line); free(new_count); free(count->Line); count->Line = new_line; } else { count->NumFile++; } ptr = ptr->Next; } char* new_line = malloc(strlen(count->Line) + 128); new_line[0] = 0; int i; for (i = 0; i < depth; ++i) { strcat(new_line, " "); } if (*entry->Name == 0) { *entry->Name = '/'; } strcat(new_line, entry->Name); strcat(new_line, " : "); my_itoa(count->NumFile, new_line + strlen(new_line)); strcat
评论
    相关推荐
    • Sim.zip
      电机控制系统,02年开始积累,效果好,速度快,PI控制,经典控制,fuzzy PID,自适应控制,速度位置控制等等,欢迎大家参考。
    • mfiles.rar
      精通matlab6.5电子书后附带光盘中计算书中算例的程序源码
    • gonjain-macro-blocked.zip
      同上,学习matlab的好教程,呵呵,大家不要骂我亚 很好的
    • 贪心算法.rar
      贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解。
    • smlrt.rar
      同上,学习matlab的好教程,呵呵,大家不要骂我亚 很好的
    • dunami-ISAM.rar
      Matlab mfiles instance analysis, there are a large number of examples, each M file is one example
    • syagshot.rar
      matlab mfiles实例分析,有大量的实例,每个M文件就是一个例子
    • uGRUB-main.zip
      uGRUB:使用Grub2引导程序创建最终的多重引导USB闪存驱动器
    • NS2example.rar
      在ns2下实现无线传感器网络仿真,多个节点进行网络活动
    • tor-0.2.1.20.tar.gz
      The Onion Router 洋葱路由。具体不用解释了。