comparison miscprog/grokdsn.c @ 192:5d84f63eff72

grokdsn: able to follow the FAT chain for the directory
author Michael Spacefalcon <falcon@ivan.Harhan.ORG>
date Wed, 07 Jan 2015 07:12:51 +0000
parents 0c631396b8ce
children 37f78f986a0a
comparison
equal deleted inserted replaced
191:0c631396b8ce 192:5d84f63eff72
48 }; 48 };
49 49
50 char *dsnfilename; 50 char *dsnfilename;
51 u_char *filemapping; 51 u_char *filemapping;
52 struct cdf_header *cdf_header; 52 struct cdf_header *cdf_header;
53 unsigned total_sectors; 53 unsigned total_sectors, fat_nsectors;
54 54
55 open_and_mmap_file() 55 open_and_mmap_file()
56 { 56 {
57 int fd; 57 int fd;
58 struct stat st; 58 struct stat st;
106 printf("File size threshold: %u\n", 106 printf("File size threshold: %u\n",
107 le32toh(cdf_header->min_large_file)); 107 le32toh(cdf_header->min_large_file));
108 return(0); 108 return(0);
109 } 109 }
110 110
111 u_char *
112 get_sector_ptr(secid)
113 unsigned secid;
114 {
115 if (secid > total_sectors) {
116 fprintf(stderr,
117 "error: request for sector #%u; we only have %u in total\n",
118 secid, total_sectors);
119 exit(1);
120 }
121 return filemapping + (secid + 1) * 512;
122 }
123
124 init_fat_access()
125 {
126 fat_nsectors = le32toh(cdf_header->fat_sectors);
127 if (fat_nsectors > HEADER_MFAT_ENTRIES) {
128 fprintf(stderr, "error: large FAT not supported\n");
129 exit(1);
130 }
131 return(0);
132 }
133
134 u_char *
135 get_fat_sector(fat_sec_no)
136 unsigned fat_sec_no;
137 {
138 unsigned secid;
139
140 if (fat_sec_no > fat_nsectors) {
141 fprintf(stderr,
142 "error: request for FAT sector #%u; we only have %u in total\n",
143 fat_sec_no, fat_nsectors);
144 exit(1);
145 }
146 secid = le32toh(cdf_header->fat_secids[fat_sec_no]);
147 return get_sector_ptr(secid);
148 }
149
150 int32_t
151 get_fat_entry(entry_no)
152 unsigned entry_no;
153 {
154 unsigned fat_sec_no, entry_in_sec;
155 uint32_t *fat_sector;
156
157 fat_sec_no = entry_no / 128;
158 entry_in_sec = entry_no % 128;
159 fat_sector = (uint32_t *) get_fat_sector(fat_sec_no);
160 return le32toh(fat_sector[entry_in_sec]);
161 }
162
163 dump_fat_chain(sarg)
164 char *sarg;
165 {
166 int i, n;
167
168 init_fat_access();
169 i = atoi(sarg);
170 while (i >= 0) {
171 n = get_fat_entry(i);
172 printf("FAT[%d] = %d\n", i, n);
173 i = n;
174 }
175 return(0);
176 }
177
111 main(argc, argv) 178 main(argc, argv)
112 char **argv; 179 char **argv;
113 { 180 {
114 if (sizeof(struct cdf_header) != 512) { 181 if (sizeof(struct cdf_header) != 512) {
115 fprintf(stderr, "error: struct cdf_header is misdefined\n"); 182 fprintf(stderr, "error: struct cdf_header is misdefined\n");
116 exit(1); 183 exit(1);
117 } 184 }
118 if (argc != 3) { 185 if (argc < 3) {
119 fprintf(stderr, "usage: %s binfile.dsn <operation>\n", argv[0]); 186 fprintf(stderr, "usage: %s binfile.dsn <op> [args]\n", argv[0]);
120 exit(1); 187 exit(1);
121 } 188 }
122 dsnfilename = argv[1]; 189 dsnfilename = argv[1];
123 open_and_mmap_file(); 190 open_and_mmap_file();
124 if (!strcmp(argv[2], "hdr")) 191 if (!strcmp(argv[2], "hdr"))
125 return dump_cdf_header(); 192 return dump_cdf_header();
193 if (!strcmp(argv[2], "fatchain"))
194 return dump_fat_chain(argv[3]);
126 fprintf(stderr, "error: \"%s\" is not a recognized command\n", argv[2]); 195 fprintf(stderr, "error: \"%s\" is not a recognized command\n", argv[2]);
127 exit(1); 196 exit(1);
128 } 197 }