forked from robertapengelly/dosfstools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmd.c
1344 lines (851 loc) · 35.2 KB
/
mmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* @file mmd.c
*****************************************************************************/
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "mmd.h"
#include "msdos.h"
#include "report.h"
#include "write7x.h"
#ifndef PATH_MAX
# define PATH_MAX 2048
#endif
static FILE *ofp;
struct dir_info {
unsigned int current_cluster;
unsigned char current_sector;
unsigned char current_entry;
unsigned char *scratch;
unsigned char flags;
};
static struct mmd_state *state = 0;
static const char *program_name = 0;
static struct msdos_boot_sector bs;
static int size_fat = 0;
static unsigned int cluster_count = 0;
static unsigned int data_area = 0;
static unsigned int info_sector = 0;
static unsigned int number_of_fats = 0;
static unsigned int reserved_sectors = 0;
static unsigned int root_cluster = 0;
static unsigned int root_dir = 0;
static unsigned int root_entries = 0;
static unsigned int sectors_per_cluster = 0;
static unsigned int sectors_per_fat = 0;
static unsigned int total_sectors = 0;
struct option {
const char *name;
int index, flags;
};
#define OPTION_NO_ARG 0x0001
#define OPTION_HAS_ARG 0x0002
enum options {
OPTION_IGNORED = 1,
OPTION_HELP,
OPTION_INPUT,
OPTION_OFFSET
};
static struct option opts[] = {
{ "i", OPTION_INPUT, OPTION_HAS_ARG },
{ "-help", OPTION_HELP, OPTION_NO_ARG },
{ "-offset", OPTION_OFFSET, OPTION_HAS_ARG },
{ 0, 0, 0 }
};
static int strstart (const char *val, const char **str) {
const char *p = val;
const char *q = *str;
while (*p != '\0') {
if (*p != *q) {
return 0;
}
++p;
++q;
}
*str = q;
return 1;
}
static void print_help (int exitval) {
if (!program_name) {
goto _exit;
}
fprintf (stderr, "Usage: %s [options] dirname\n\n", program_name);
fprintf (stderr, "Options:\n\n");
fprintf (stderr, " Short options:\n\n");
fprintf (stderr, " -i Specify the input target.\n");
fprintf (stderr, "\n");
fprintf (stderr, " Long options:\n\n");
fprintf (stderr, " --help Show this help information then exit.\n");
fprintf (stderr, " --offset SECTOR Write the filesystem starting at SECTOR.\n");
_exit:
exit (exitval);
}
static void *xmalloc (size_t size) {
void *ptr = malloc (size);
if (ptr == NULL && size) {
report_at (program_name, 0, REPORT_ERROR, "memory full (malloc)");
exit (EXIT_FAILURE);
}
memset (ptr, 0, size);
return ptr;
}
static void *xrealloc (void *ptr, size_t size) {
void *new_ptr = realloc (ptr, size);
if (new_ptr == NULL && size) {
report_at (program_name, 0, REPORT_ERROR, "memory full (realloc)");
exit (EXIT_FAILURE);
}
return new_ptr;
}
static char *xstrdup (const char *str) {
char *ptr = xmalloc (strlen (str) + 1);
strcpy (ptr, str);
return ptr;
}
static void dynarray_add (void *ptab, size_t *nb_ptr, void *data) {
int nb, nb_alloc;
void **pp;
nb = *nb_ptr;
pp = *(void ***) ptab;
if ((nb & (nb - 1)) == 0) {
if (!nb) {
nb_alloc = 1;
} else {
nb_alloc = nb * 2;
}
pp = xrealloc (pp, nb_alloc * sizeof (void *));
*(void ***) ptab = pp;
}
pp[nb++] = data;
*nb_ptr = nb;
}
static void parse_args (int *pargc, char ***pargv, int optind) {
char **argv = *pargv;
int argc = *pargc;
struct option *popt;
const char *optarg, *r;
if (argc == optind) {
print_help (EXIT_SUCCESS);
}
while (optind < argc) {
r = argv[optind++];
if (r[0] != '-' || r[1] == '\0') {
dynarray_add (&state->dirs, &state->nb_dirs, xstrdup (r));
continue;
}
for (popt = opts; popt; ++popt) {
const char *p1 = popt->name;
const char *r1 = (r + 1);
if (!p1) {
report_at (program_name, 0, REPORT_ERROR, "invalid option -- '%s'", r);
exit (EXIT_FAILURE);
}
if (!strstart (p1, &r1)) {
continue;
}
optarg = r1;
if (popt->flags & OPTION_HAS_ARG) {
if (*optarg == '\0') {
if (optind >= argc) {
report_at (program_name, 0, REPORT_ERROR, "argument to '%s' is missing", r);
exit (EXIT_FAILURE);
}
optarg = argv[optind++];
}
} else if (*optarg != '\0') {
continue;
}
break;
}
switch (popt->index) {
case OPTION_HELP: {
print_help (EXIT_SUCCESS);
break;
}
case OPTION_INPUT: {
if (state->outfile) {
report_at (program_name, 0, REPORT_ERROR, "multiple output files provided");
exit (EXIT_FAILURE);
}
state->outfile = xstrdup (optarg);
break;
}
case OPTION_OFFSET: {
long conversion;
char *temp;
errno = 0;
conversion = strtol (optarg, &temp, 0);
if (!*optarg || isspace ((int) *optarg) || errno || *temp) {
report_at (program_name, 0, REPORT_ERROR, "bad number for offset (%s)", optarg);
exit (EXIT_FAILURE);
}
if (conversion < 0 || (unsigned long) conversion > UINT_MAX) {
report_at (program_name, 0, REPORT_ERROR, "offset must be between 0 and %u", UINT_MAX);
exit (EXIT_FAILURE);
}
state->offset = (unsigned long) conversion;
break;
}
default: {
report_at (program_name, 0, REPORT_ERROR, "unsupported option '%s'", r);
exit (EXIT_FAILURE);
}
}
}
}
static int seekto (long offset) {
return fseek (ofp, (state->offset * 512) + offset, SEEK_SET);
}
static int create_dir (const char *target, unsigned char *scratch);
static int get_next_entry (struct dir_info *di, struct msdos_dirent *de);
static int get_free_dirent (const char *path, struct dir_info *di, struct msdos_dirent *de);
static int open_dir (const char *target, struct dir_info *di);
static unsigned int get_fat_entry (unsigned char *scratch, unsigned int cluster);
static int set_fat_entry (unsigned char *scratch, unsigned int cluster, unsigned int value);
static unsigned int get_free_fat (unsigned char *scratch);
static int canonical_to_dir (char *dest, const char *src) {
static const char invalid_chars[] = "\"*+,./:;<=>?[\\]|";
int i, j;
int namelen = 0, dots = 0, extlen = 0;
memset (dest, ' ', 11);
if (*src == '\0' || *src == '.') {
return -1;
}
for (j = i = 0; *src != '\0'; i++) {
int c = (unsigned char) *src++;
if (c == '/' || c == '\\') {
break;
}
if (i >= 12) {
return -1;
}
if (i == 0 && c == 0xE5) {
/**
* 0xE5 in the first character of the name is a marker for delected files,
* it needs to be translated to 0x05.
*/
c = 0x05;
} else if (c == '.') {
if (dots++) {
return -1;
}
j = 8;
continue;
}
if (c <= 0x20 || strchr (invalid_chars, c)) {
return -1;
}
if (dots) {
if (++extlen > 3) {
return -1;
}
} else {
if (++namelen > 8) {
return -1;
}
}
if (c >= 'a' && c <= 'z') {
c -= 'a' - 'A';
}
dest[j++] = c;
}
return 0;
}
static int create_dir (const char *target, unsigned char *scratch) {
unsigned char tmppath[PATH_MAX];
unsigned char filename[12];
unsigned char *p;
struct dir_info di;
struct msdos_dirent de;
unsigned short date;
unsigned short time;
unsigned int cluster, i;
unsigned int dir_sector;
unsigned int dir_offset;
/* Get a local copy of the target. If it's larger than PATH_MAX, abort. */
strncpy ((char *) tmppath, (char *) target, PATH_MAX);
tmppath[PATH_MAX - 1] = 0;
if (strcmp ((char *) target, (char *) tmppath)) {
return -1;
}
/* Strip leading seperators. */
while (tmppath[0] == '/' || tmppath[0] == '\\') {
strcpy ((char *) tmppath, (char *) tmppath + 1);
}
/* Parse filename off the end of the suppiled target. */
p = tmppath;
while (*(p++));
p--;
while (p > tmppath && *p != '/' && *p != '\\') {
p--;
}
if (*p == '/' || *p == '\\') {
p++;
}
if (canonical_to_dir ((char *) filename, (char *) p) < 0) {
fprintf (stderr, "Failed to convert to 8:3\n");
return -1;
}
if (p > tmppath) {
p--;
}
if (*p == '/' || *p == '\\' || p == tmppath) {
*p = 0;
}
di.scratch = scratch;
if (open_dir ((char *) tmppath, &di) < 0) {
fprintf (stderr, "Failed to open directory\n");
return -1;
}
while (!get_next_entry (&di, &de)) {
/*if (!de.name[0] || de.name[0] == 0xE5 || (de.attr & ATTR_LONG_NAME) == ATTR_LONG_NAME) {*/
if (!de.name[0] || de.name[0] == 0xE5) {
continue;
}
if (!memcmp (de.name, filename, 11)) {
fprintf (stderr, "%s already exists.\n", target);
return -1;
}
}
if (get_free_dirent ((char *) tmppath, &di, &de) < 0) {
return -1;
}
date = generate_datestamp ();
time = generate_timestamp ();
memset (&de, 0, sizeof (de));
memcpy (de.name, filename, 11);
cluster = get_free_fat (scratch);
if (cluster == 0x0FFFFFF7 || set_fat_entry (scratch, cluster, 0x0FFFFFF8) < 0) {
return -1;
}
write721_to_byte_array (de.startlo, cluster);
write721_to_byte_array (de.starthi, cluster >> 16);
de.attr = ATTR_DIR;
write721_to_byte_array (de.ctime, time);
write721_to_byte_array (de.cdate, date);
write721_to_byte_array (de.adate, date);
write721_to_byte_array (de.time, time);
write721_to_byte_array (de.date, date);
if (di.current_cluster == 0) {
dir_sector = root_dir + di.current_sector;
} else {
dir_sector = data_area + ((di.current_cluster - 2) * sectors_per_cluster) + di.current_sector;
}
/*fi->dir_offset = di.current_entry - 1;*/
dir_offset = di.current_entry;
if (seekto ((unsigned long) dir_sector * 512) || fread (scratch, 512, 1, ofp) != 1) {
return -1;
}
memcpy (&(((struct msdos_dirent *) scratch)[dir_offset]), &de, sizeof (de));
if (seekto ((unsigned long) dir_sector * 512) || fwrite (scratch, 512, 1, ofp) != 1) {
return -1;
}
memset (scratch, 0, 512);
for (i = 0; i < sectors_per_cluster; i++) {
unsigned long offset = (unsigned long) (data_area + ((cluster - 2) * sectors_per_cluster) + i);
if (seekto (offset * 512)) {
return -1;
}
if (fwrite (scratch, 512, 1, ofp) != 1) {
return -1;
}
}
dir_offset = 0;
dir_sector = data_area + ((cluster - 2) * sectors_per_cluster);
memset (de.name, ' ', 11);
de.name[0] = '.';
write721_to_byte_array (de.startlo, cluster);
write721_to_byte_array (de.starthi, cluster >> 16);
memcpy (&(((struct msdos_dirent *) scratch)[dir_offset]), &de, sizeof (de));
dir_offset++;
de.name[1] = '.';
if (di.current_cluster == root_cluster) {
di.current_cluster = 0;
}
write721_to_byte_array (de.startlo, di.current_cluster);
write721_to_byte_array (de.starthi, di.current_cluster >> 16);
memcpy (&(((struct msdos_dirent *) scratch)[dir_offset]), &de, sizeof (de));
if (seekto ((unsigned long) dir_sector * 512) || fwrite (scratch, 512, 1, ofp) != 1) {
return -1;
}
if (size_fat == 32) {
struct fat32_fsinfo *info;
unsigned int free_clusters;
unsigned int next_cluster;
if (seekto ((unsigned long) info_sector * 512) || fread (scratch, 512, 1, ofp) != 1) {
return -1;
}
info = (struct fat32_fsinfo *) (scratch + 0x1e0);
free_clusters = (unsigned int) info->free_clusters[0] | (((unsigned int) info->free_clusters[1]) << 8) | (((unsigned int) info->free_clusters[2]) << 16) | (((unsigned int) info->free_clusters[3]) << 24);
next_cluster = (unsigned int) info->next_cluster[0] | (((unsigned int) info->next_cluster[1]) << 8) | (((unsigned int) info->next_cluster[2]) << 16) | (((unsigned int) info->next_cluster[3]) << 24);
free_clusters--;
next_cluster++;
write741_to_byte_array (info->free_clusters, free_clusters);
write741_to_byte_array (info->next_cluster, next_cluster);
if (seekto ((unsigned long) info_sector * 512) || fwrite (scratch, 512, 1, ofp) != 1) {
return -1;
}
}
return 0;
}
static int get_free_dirent (const char *path, struct dir_info *di, struct msdos_dirent *de) {
int entry;
unsigned int i, tempclust;
if (open_dir (path, di) < 0) {
return -1;
}
entry = 0;
di->flags |= 0x01;
do {
entry = get_next_entry (di, de);
/*if (entry == 0 && (!de->name[0] || de->name[0] == 0xE5 || (de->attr & ATTR_LONG_NAME) == ATTR_LONG_NAME)) {*/
if (entry == 0 && (!de->name[0] || de->name[0] == 0xE5)) {
/*if (de->name[0] == 0xE5 || (de->attr & ATTR_LONG_NAME) == ATTR_LONG_NAME) {*/
if (de->name[0] == 0xE5) {
di->current_entry--;
}
return 0;
} else if (entry == -1) {
return -1;
} else if (entry == 1) {
if ((tempclust = get_free_fat (di->scratch) == 0x0FFFFFF7)) {
return -1;
}
memset (di->scratch, 0, 512);
for (i = 0; i < sectors_per_cluster; i++) {
unsigned long offset = (unsigned long) (data_area + ((tempclust - 2) * sectors_per_cluster) + i);
if (seekto (offset * 512)) {
return -1;
}
if (fwrite (di->scratch, 512, 1, ofp) != 1) {
return -1;
}
}
i = 0;
if (set_fat_entry (di->scratch, di->current_cluster, tempclust) < 0) {
return -1;
}
di->current_cluster = tempclust;
di->current_entry = 0;
di->current_sector = 0;
if (size_fat == 12) {
tempclust = 0x0FF8;
} else if (size_fat == 16) {
tempclust = 0xFFF8;
} else if (size_fat == 32) {
struct fat32_fsinfo *info;
unsigned int free_clusters;
unsigned int next_cluster;
tempclust = 0x0ffffff8;
if (seekto ((unsigned long) info_sector * 512) || fread (di->scratch, 512, 1, ofp) != 1) {
return -1;
}
info = (struct fat32_fsinfo *) (di->scratch + 0x1e0);
free_clusters = (unsigned int) info->free_clusters[0] | (((unsigned int) info->free_clusters[1]) << 8) | (((unsigned int) info->free_clusters[2]) << 16) | (((unsigned int) info->free_clusters[3]) << 24);
next_cluster = (unsigned int) info->next_cluster[0] | (((unsigned int) info->next_cluster[1]) << 8) | (((unsigned int) info->next_cluster[2]) << 16) | (((unsigned int) info->next_cluster[3]) << 24);
free_clusters--;
next_cluster++;
write741_to_byte_array (info->free_clusters, free_clusters);
write741_to_byte_array (info->next_cluster, next_cluster);
if (seekto ((unsigned long) info_sector * 512) || fwrite (di->scratch, 512, 1, ofp) != 1) {
return -1;
}
} else {
return -1;
}
}
} while (!entry);
/* We should't get here! */
return -1;
}
static int get_next_entry (struct dir_info *di, struct msdos_dirent *de) {
unsigned long offset;
if (di->current_entry >= 512 / sizeof (*de)) {
di->current_entry = 0;
di->current_sector++;
if (di->current_cluster == 0) {
unsigned long offset;
if (di->current_sector * (512 / sizeof (*de)) >= root_entries) {
return -1;
}
offset = (unsigned long) root_dir + di->current_sector;
if (seekto (offset * 512) || fread (di->scratch, 512, 1, ofp) != 1) {
return -1;
}
} else {
if (di->current_sector >= sectors_per_cluster) {
di->current_sector = 0;
if ((size_fat == 12 && di->current_cluster >= 0x0FF7) || (size_fat == 16 && di->current_cluster >= 0xFFF7) || (size_fat == 32 && di->current_cluster >= 0x0FFFFFF7)) {
if (!(di->flags & 0x01)) {
return -1;
}
return 1;
}
di->current_cluster = get_fat_entry (di->scratch, di->current_cluster);
}
offset = (unsigned long) data_area;
offset += ((di->current_cluster - 2) * sectors_per_cluster);
offset += di->current_sector;
if (seekto (offset * 512) || fread (di->scratch, 512, 1, ofp) != 1) {
return -1;
}
}
}
memcpy (de, &(((struct msdos_dirent *) di->scratch)[di->current_entry]), sizeof (*de));
if (de->name[0] == 0) {
if (di->flags & 0x01) {
return 0;
}
return -1;
}
if (de->name[0] == 0x05) {
de->name[0] = 0xE5;
return 0;
}
di->current_entry++;
return 0;
}
static int open_dir (const char *target, struct dir_info *di) {
di->flags = 0;
if (!strlen ((char *) target) || (strlen ((char *) target) == 1 && (target[0] == '/' || target[0] == '\\'))) {
unsigned long offset;
if (size_fat == 32) {
di->current_cluster = root_cluster;
di->current_entry = 0;
di->current_sector = 0;
offset = (unsigned long) data_area + ((di->current_cluster - 2) * sectors_per_cluster);
} else {
di->current_cluster = 0;
di->current_entry = 0;
di->current_sector = 0;
offset = (unsigned long) root_dir;
}
if (seekto (offset * 512) || fread (di->scratch, 512, 1, ofp) != 1) {
return -1;
}
} else {
unsigned char tmpfn[12];
unsigned char *ptr;
struct msdos_dirent de;
unsigned int result;
unsigned long offset;
if (size_fat == 32) {
di->current_cluster = root_cluster;
di->current_entry = 0;
di->current_sector = 0;
offset = (unsigned long) data_area + ((di->current_cluster - 2) * sectors_per_cluster);
} else {
di->current_cluster = 0;
di->current_entry = 0;
di->current_sector = 0;
offset = (unsigned long) root_dir;
}
if (seekto (offset * 512) || fread (di->scratch, 512, 1, ofp) != 1) {
return -1;
}
ptr = (unsigned char *) target;
while ((*ptr == '/' || *ptr == '\\') && *ptr) {
ptr++;
}
while (*ptr) {
if (canonical_to_dir ((char *) tmpfn, (char *) ptr) < 0) {
fprintf (stderr, "Failed to convert to 8:3\n");
return -1;
}
de.name[0] = 0;
do {
result = get_next_entry (di, &de);
} while (!result && memcmp (de.name, tmpfn, 11));
if (!memcmp (de.name, tmpfn, 11) && (de.attr & ATTR_DIR) == ATTR_DIR) {
unsigned long offset;
di->current_cluster = (unsigned int) de.startlo[0] | ((unsigned int) de.startlo[1]) << 8 | ((unsigned int) de.starthi[0]) << 16 | ((unsigned int) de.starthi[1]) << 24;
di->current_entry = 0;
di->current_sector = 0;
offset = (unsigned long) (data_area + ((di->current_cluster - 2) * sectors_per_cluster));
if (seekto (offset * 512)) {
return -1;
}
if (fread (di->scratch, 512, 1, ofp) != 1) {
return -1;
}
} else if (!memcmp (de.name, tmpfn, 11) && !(de.attr & ATTR_DIR)) {
return -1;
}
while (*ptr != '/' && *ptr != '\\' && *ptr) {
ptr++;
}
if (*ptr == '/' || *ptr == '\\') {
ptr++;
}
}
if (!di->current_cluster) {
return -1;
}
}
return 0;
}
static int set_fat_entry (unsigned char *scratch, unsigned int cluster, unsigned int value) {
unsigned int i, offset, sector;
if (size_fat == 12) {
offset = cluster + (cluster / 2);
value &= 0x0fff;
} else if (size_fat == 16) {
offset = cluster * 2;
value &= 0xffff;
} else if (size_fat == 32) {
offset = cluster * 4;
value &= 0x0fffffff;
} else {
return -1;
}
/**
* At this point, offset is the BYTE offset of the desired sector from the start
* of the FAT. Calculate the physical sector containing this FAT entry.
*/
sector = (offset / 512) + reserved_sectors;
if (seekto (sector * 512) || fread (scratch, 512, 1, ofp) != 1) {
return -1;
}
/**
* At this point, we "merely" need to extract the relevant entry. This is
* easy for FAT16 and FAT32, but a royal PITA for FAT12 as a single entry
* may span a sector boundary. The normal way around this is always to
* read two FAT sectors, but luxary is (by design intent) unavailable.
*/
offset %= 512;
if (size_fat == 12) {
if (offset == 511) {
if (((cluster * 3) & 0x01) == 0) {
scratch[offset] = (unsigned char) (value & 0xFF);
} else {
scratch[offset] = (unsigned char) ((scratch[offset] & 0x0F) | (value & 0xF0));
}
for (i = 0; i < number_of_fats; i++) {
unsigned long temp = sector + (i * sectors_per_fat);
if (seekto (temp * 512) < 0 || fwrite (scratch, 512, 1, ofp) != 1) {
return -1;
}
}
sector++;
if (seekto (sector) || fread (scratch, 512, 1, ofp) != 1) {
return -1;
}
if (((cluster * 3) & 0x01) == 0) {
scratch[0] = (unsigned char) ((scratch[0] & 0xF0) | (value & 0x0F));
} else {
scratch[0] = (unsigned char) (value & 0xFF00);
}
goto _write_fat;
} else {