[VFATLIB] Upgrade fsck.fat to 4.1
[reactos.git] / sdk / lib / fslib / vfatlib / check / lfn.c
1 /* lfn.c - Functions for handling VFAT long filenames
2
3 Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
4 Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
5 Copyright (C) 2015 Andreas Bombe <aeb@debian.org>
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 The complete text of the GNU General Public License
21 can be found in /usr/share/common-licenses/GPL-3 file.
22 */
23
24 #include "vfatlib.h"
25
26 #define NDEBUG
27 #include <debug.h>
28
29 typedef struct {
30 uint8_t id; /* sequence number for slot */
31 uint8_t name0_4[10]; /* first 5 characters in name */
32 uint8_t attr; /* attribute byte */
33 uint8_t reserved; /* always 0 */
34 uint8_t alias_checksum; /* checksum for 8.3 alias */
35 uint8_t name5_10[12]; /* 6 more characters in name */
36 uint16_t start; /* starting cluster number, 0 in long slots */
37 uint8_t name11_12[4]; /* last 2 characters in name */
38 } LFN_ENT;
39
40 #define LFN_ID_START 0x40
41 #define LFN_ID_SLOTMASK 0x1f
42
43 #define CHARS_PER_LFN 13
44
45 /* These modul-global vars represent the state of the LFN parser */
46 unsigned char *lfn_unicode = NULL;
47 unsigned char lfn_checksum;
48 int lfn_slot = -1;
49 off_t *lfn_offsets = NULL;
50 int lfn_parts = 0;
51
52 static unsigned char fat_uni2esc[64] = {
53 '0', '1', '2', '3', '4', '5', '6', '7',
54 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
55 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
56 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
57 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
58 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
59 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
60 'u', 'v', 'w', 'x', 'y', 'z', '+', '-'
61 };
62
63 /* This defines which unicode chars are directly convertable to ISO-8859-1 */
64 #define UNICODE_CONVERTABLE(cl,ch) (ch == 0 && (cl < 0x80 || cl >= 0xa0))
65
66 /* for maxlen param */
67 #define UNTIL_0 INT_MAX
68
69 /* Convert name part in 'lfn' from unicode to ASCII */
70 #define CNV_THIS_PART(lfn) \
71 ({ \
72 unsigned char __part_uni[CHARS_PER_LFN*2]; \
73 copy_lfn_part( __part_uni, lfn ); \
74 cnv_unicode( __part_uni, CHARS_PER_LFN, 0 ); \
75 })
76
77 /* Convert name parts collected so far (from previous slots) from unicode to
78 * ASCII */
79 #define CNV_PARTS_SO_FAR() \
80 (cnv_unicode( lfn_unicode+(lfn_slot*CHARS_PER_LFN*2), \
81 lfn_parts*CHARS_PER_LFN, 0 ))
82
83 #define BYTES_TO_WCHAR(cl,ch) ((wchar_t)((unsigned)(cl) + ((unsigned)(ch) << 8)))
84 static size_t mbslen(wchar_t x)
85 {
86 wchar_t wstr[] = { x, 0 };
87 return wcstombs(NULL, wstr, 0);
88 }
89
90 static size_t wctombs(char *dest, wchar_t x)
91 {
92 wchar_t wstr[] = { x, 0 };
93 size_t size = wcstombs(NULL, wstr, 0);
94 if (size != (size_t) - 1)
95 size = wcstombs(dest, wstr, size + 1);
96 return size;
97 }
98
99 /* This function converts an unicode string to a normal ASCII string, assuming
100 * ISO-8859-1 charset. Characters not in 8859-1 are converted to the same
101 * escape notation as used by the kernel, i.e. the uuencode-like ":xxx" */
102 static char *cnv_unicode(const unsigned char *uni, int maxlen, int use_q)
103 {
104 const unsigned char *up;
105 unsigned char *out, *cp;
106 int len, val;
107 size_t x;
108
109 for (len = 0, up = uni; (up - uni) / 2 < maxlen && (up[0] || up[1]);
110 up += 2) {
111 if ((x = mbslen(BYTES_TO_WCHAR(up[0], up[1]))) != (size_t) - 1)
112 len += x;
113 else if (UNICODE_CONVERTABLE(up[0], up[1]))
114 ++len;
115 else
116 len += 4;
117 }
118 cp = out = use_q ? qalloc(&mem_queue, len + 1) : alloc(len + 1);
119
120 for (up = uni; (up - uni) / 2 < maxlen && (up[0] || up[1]); up += 2) {
121 if ((x =
122 wctombs((char *)cp, BYTES_TO_WCHAR(up[0], up[1]))) != (size_t) - 1)
123 cp += x;
124 else if (UNICODE_CONVERTABLE(up[0], up[1]))
125 *cp++ = up[0];
126 else {
127 /* here the same escape notation is used as in the Linux kernel */
128 *cp++ = ':';
129 val = (up[1] << 8) + up[0];
130 cp[2] = fat_uni2esc[val & 0x3f];
131 val >>= 6;
132 cp[1] = fat_uni2esc[val & 0x3f];
133 val >>= 6;
134 cp[0] = fat_uni2esc[val & 0x3f];
135 cp += 3;
136 }
137 }
138 *cp = 0;
139
140 return (char *)out;
141 }
142
143 static void copy_lfn_part(unsigned char *dst, LFN_ENT * lfn)
144 {
145 memcpy(dst, lfn->name0_4, 10);
146 memcpy(dst + 10, lfn->name5_10, 12);
147 memcpy(dst + 22, lfn->name11_12, 4);
148 }
149
150 static void clear_lfn_slots(int start, int end)
151 {
152 int i;
153 LFN_ENT empty;
154
155 /* New dir entry is zeroed except first byte, which is set to 0xe5.
156 * This is to avoid that some FAT-reading OSes (not Linux! ;) stop reading
157 * a directory at the first zero entry...
158 */
159 memset(&empty, 0, sizeof(empty));
160 empty.id = DELETED_FLAG;
161
162 for (i = start; i <= end; ++i) {
163 fs_write(lfn_offsets[i], sizeof(LFN_ENT), &empty);
164 }
165 }
166
167 void lfn_fix_checksum(off_t from, off_t to, const char *short_name)
168 {
169 int i;
170 uint8_t sum;
171 for (sum = 0, i = 0; i < 11; i++)
172 sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + short_name[i];
173
174 for (; from < to; from += sizeof(LFN_ENT)) {
175 fs_write(from + offsetof(LFN_ENT, alias_checksum), sizeof(sum), &sum);
176 }
177 }
178
179 void lfn_reset(void)
180 {
181 if (lfn_unicode)
182 free(lfn_unicode);
183 lfn_unicode = NULL;
184 if (lfn_offsets)
185 free(lfn_offsets);
186 lfn_offsets = NULL;
187 lfn_slot = -1;
188 }
189
190 /* This function is only called with de->attr == VFAT_LN_ATTR. It stores part
191 * of the long name. */
192 void lfn_add_slot(DIR_ENT * de, off_t dir_offset)
193 {
194 LFN_ENT *lfn = (LFN_ENT *) de;
195 int slot = lfn->id & LFN_ID_SLOTMASK;
196 unsigned offset;
197
198 if (lfn_slot == 0)
199 lfn_check_orphaned();
200
201 if (de->attr != VFAT_LN_ATTR)
202 die("lfn_add_slot called with non-LFN directory entry");
203
204 if (lfn->id & LFN_ID_START && slot != 0) {
205 if (lfn_slot != -1) {
206 int can_clear = 0;
207 /* There is already a LFN "in progess", so it is an error that a
208 * new start entry is here. */
209 /* Causes: 1) if slot# == expected: start bit set mysteriously, 2)
210 * old LFN overwritten by new one */
211 /* Fixes: 1) delete previous LFN 2) if slot# == expected and
212 * checksum ok: clear start bit */
213 /* XXX: Should delay that until next LFN known (then can better
214 * display the name) */
215 printf("A new long file name starts within an old one.\n");
216 if (slot == lfn_slot && lfn->alias_checksum == lfn_checksum) {
217 char *part1 = CNV_THIS_PART(lfn);
218 char *part2 = CNV_PARTS_SO_FAR();
219 printf(" It could be that the LFN start bit is wrong here\n"
220 " if \"%s\" seems to match \"%s\".\n", part1, part2);
221 free(part1);
222 free(part2);
223 can_clear = 1;
224 }
225 if (interactive) {
226 printf("1: Delete previous LFN\n2: Leave it as it is.\n");
227 if (can_clear)
228 printf("3: Clear start bit and concatenate LFNs\n");
229 } else
230 printf(" Not auto-correcting this.\n");
231 if (interactive) {
232 switch (get_key(can_clear ? "123" : "12", "?")) {
233 case '1':
234 clear_lfn_slots(0, lfn_parts - 1);
235 lfn_reset();
236 break;
237 case '2':
238 break;
239 case '3':
240 lfn->id &= ~LFN_ID_START;
241 fs_write(dir_offset + offsetof(LFN_ENT, id),
242 sizeof(lfn->id), &lfn->id);
243 break;
244 }
245 }
246 }
247 lfn_slot = slot;
248 lfn_checksum = lfn->alias_checksum;
249 lfn_unicode = alloc((lfn_slot * CHARS_PER_LFN + 1) * 2);
250 lfn_offsets = alloc(lfn_slot * sizeof(off_t));
251 lfn_parts = 0;
252 } else if (lfn_slot == -1 && slot != 0) {
253 /* No LFN in progress, but slot found; start bit missing */
254 /* Causes: 1) start bit got lost, 2) Previous slot with start bit got
255 * lost */
256 /* Fixes: 1) delete LFN, 2) set start bit */
257 char *part = CNV_THIS_PART(lfn);
258 printf("Long filename fragment \"%s\" found outside a LFN "
259 "sequence.\n (Maybe the start bit is missing on the "
260 "last fragment)\n", part);
261 free(part);
262 if (interactive) {
263 printf("1: Delete fragment\n2: Leave it as it is.\n"
264 "3: Set start bit\n");
265 } else
266 printf(" Not auto-correcting this.\n");
267 switch (interactive ? get_key("123", "?") : '2') {
268 case '1':
269 if (!lfn_offsets)
270 lfn_offsets = alloc(sizeof(off_t));
271 lfn_offsets[0] = dir_offset;
272 clear_lfn_slots(0, 0);
273 lfn_reset();
274 return;
275 case '2':
276 lfn_reset();
277 return;
278 case '3':
279 lfn->id |= LFN_ID_START;
280 fs_write(dir_offset + offsetof(LFN_ENT, id),
281 sizeof(lfn->id), &lfn->id);
282 lfn_slot = slot;
283 lfn_checksum = lfn->alias_checksum;
284 lfn_unicode = alloc((lfn_slot * CHARS_PER_LFN + 1) * 2);
285 lfn_offsets = alloc(lfn_slot * sizeof(off_t));
286 lfn_parts = 0;
287 break;
288 }
289 } else if (slot != lfn_slot) {
290 /* wrong sequence number */
291 /* Causes: 1) seq-no destroyed */
292 /* Fixes: 1) delete LFN, 2) fix number (maybe only if following parts
293 * are ok?, maybe only if checksum is ok?) (Attention: space
294 * for name was allocated before!) */
295 int can_fix = 0;
296 printf("Unexpected long filename sequence number "
297 "(%d vs. expected %d).\n", slot, lfn_slot);
298 if (lfn->alias_checksum == lfn_checksum && lfn_slot > 0) {
299 char *part1 = CNV_THIS_PART(lfn);
300 char *part2 = CNV_PARTS_SO_FAR();
301 printf(" It could be that just the number is wrong\n"
302 " if \"%s\" seems to match \"%s\".\n", part1, part2);
303 free(part1);
304 free(part2);
305 can_fix = 1;
306 }
307 if (interactive) {
308 printf
309 ("1: Delete LFN\n2: Leave it as it is (and ignore LFN so far)\n");
310 if (can_fix)
311 printf("3: Correct sequence number\n");
312 } else
313 printf(" Not auto-correcting this.\n");
314 switch (interactive ? get_key(can_fix ? "123" : "12", "?") : '2') {
315 case '1':
316 if (!lfn_offsets) {
317 lfn_offsets = alloc(sizeof(off_t));
318 lfn_parts = 0;
319 }
320 lfn_offsets[lfn_parts++] = dir_offset;
321 clear_lfn_slots(0, lfn_parts - 1);
322 lfn_reset();
323 return;
324 case '2':
325 lfn_reset();
326 return;
327 case '3':
328 lfn->id = (lfn->id & ~LFN_ID_SLOTMASK) | lfn_slot;
329 fs_write(dir_offset + offsetof(LFN_ENT, id),
330 sizeof(lfn->id), &lfn->id);
331 break;
332 }
333 }
334
335 if (lfn->alias_checksum != lfn_checksum) {
336 /* checksum mismatch */
337 /* Causes: 1) checksum field here destroyed */
338 /* Fixes: 1) delete LFN, 2) fix checksum */
339 printf("Checksum in long filename part wrong "
340 "(%02x vs. expected %02x).\n",
341 lfn->alias_checksum, lfn_checksum);
342 if (interactive) {
343 printf("1: Delete LFN\n2: Leave it as it is.\n"
344 "3: Correct checksum\n");
345 } else
346 printf(" Not auto-correcting this.\n");
347 if (interactive) {
348 switch (get_key("123", "?")) {
349 case '1':
350 lfn_offsets[lfn_parts++] = dir_offset;
351 clear_lfn_slots(0, lfn_parts - 1);
352 lfn_reset();
353 return;
354 case '2':
355 break;
356 case '3':
357 lfn->alias_checksum = lfn_checksum;
358 fs_write(dir_offset + offsetof(LFN_ENT, alias_checksum),
359 sizeof(lfn->alias_checksum), &lfn->alias_checksum);
360 break;
361 }
362 }
363 }
364
365 if (lfn_slot != -1) {
366 lfn_slot--;
367 offset = lfn_slot * CHARS_PER_LFN * 2;
368 copy_lfn_part(lfn_unicode + offset, lfn);
369 if (lfn->id & LFN_ID_START)
370 lfn_unicode[offset + 26] = lfn_unicode[offset + 27] = 0;
371 lfn_offsets[lfn_parts++] = dir_offset;
372 }
373
374 if (lfn->reserved != 0) {
375 printf("Reserved field in VFAT long filename slot is not 0 "
376 "(but 0x%02x).\n", lfn->reserved);
377 if (interactive)
378 printf("1: Fix.\n2: Leave it.\n");
379 else
380 printf("Auto-setting to 0.\n");
381 if (!interactive || get_key("12", "?") == '1') {
382 lfn->reserved = 0;
383 fs_write(dir_offset + offsetof(LFN_ENT, reserved),
384 sizeof(lfn->reserved), &lfn->reserved);
385 }
386 }
387 if (lfn->start != htole16(0)) {
388 printf("Start cluster field in VFAT long filename slot is not 0 "
389 "(but 0x%04x).\n", lfn->start);
390 if (interactive)
391 printf("1: Fix.\n2: Leave it.\n");
392 else
393 printf("Auto-setting to 0.\n");
394 if (!interactive || get_key("12", "?") == '1') {
395 lfn->start = htole16(0);
396 fs_write(dir_offset + offsetof(LFN_ENT, start),
397 sizeof(lfn->start), &lfn->start);
398 }
399 }
400 }
401
402 /* This function is always called when de->attr != VFAT_LN_ATTR is found, to
403 * retrieve the previously constructed LFN. */
404 char *lfn_get(DIR_ENT * de, off_t * lfn_offset)
405 {
406 char *lfn;
407 uint8_t sum;
408 int i;
409
410 *lfn_offset = 0;
411 if (de->attr == VFAT_LN_ATTR)
412 die("lfn_get called with LFN directory entry");
413
414 #if 0
415 if (de->lcase)
416 printf("lcase=%02x\n", de->lcase);
417 #endif
418
419 if (lfn_slot == -1)
420 /* no long name for this file */
421 return NULL;
422
423 if (lfn_slot != 0) {
424 /* The long name isn't finished yet. */
425 /* Causes: 1) LFN slot overwritten by non-VFAT aware tool */
426 /* Fixes: 1) delete LFN 2) move overwriting entry to somewhere else
427 * and let user enter missing part of LFN (hard to do :-()
428 * 3) renumber entries and truncate name */
429 char *long_name = CNV_PARTS_SO_FAR();
430 char *short_name = file_name(de->name);
431 printf("Unfinished long file name \"%s\".\n"
432 " (Start may have been overwritten by %s)\n",
433 long_name, short_name);
434 free(long_name);
435 if (interactive) {
436 printf("1: Delete LFN\n2: Leave it as it is.\n"
437 "3: Fix numbering (truncates long name and attaches "
438 "it to short name %s)\n", short_name);
439 } else
440 printf(" Not auto-correcting this.\n");
441 switch (interactive ? get_key("123", "?") : '2') {
442 case '1':
443 clear_lfn_slots(0, lfn_parts - 1);
444 lfn_reset();
445 return NULL;
446 case '2':
447 lfn_reset();
448 return NULL;
449 case '3':
450 for (i = 0; i < lfn_parts; ++i) {
451 uint8_t id = (lfn_parts - i) | (i == 0 ? LFN_ID_START : 0);
452 fs_write(lfn_offsets[i] + offsetof(LFN_ENT, id),
453 sizeof(id), &id);
454 }
455 memmove(lfn_unicode, lfn_unicode + lfn_slot * CHARS_PER_LFN * 2,
456 lfn_parts * CHARS_PER_LFN * 2);
457 break;
458 }
459 }
460
461 for (sum = 0, i = 0; i < MSDOS_NAME; i++)
462 sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + de->name[i];
463 if (sum != lfn_checksum) {
464 /* checksum doesn't match, long name doesn't apply to this alias */
465 /* Causes: 1) alias renamed */
466 /* Fixes: 1) Fix checksum in LFN entries */
467 char *long_name = CNV_PARTS_SO_FAR();
468 char *short_name = file_name(de->name);
469 printf("Wrong checksum for long file name \"%s\".\n"
470 " (Short name %s may have changed without updating the long name)\n",
471 long_name, short_name);
472 free(long_name);
473 if (interactive) {
474 printf("1: Delete LFN\n2: Leave it as it is.\n"
475 "3: Fix checksum (attaches to short name %s)\n", short_name);
476 } else
477 printf(" Not auto-correcting this.\n");
478 if (interactive) {
479 switch (get_key("123", "?")) {
480 case '1':
481 clear_lfn_slots(0, lfn_parts - 1);
482 lfn_reset();
483 return NULL;
484 case '2':
485 lfn_reset();
486 return NULL;
487 case '3':
488 for (i = 0; i < lfn_parts; ++i) {
489 fs_write(lfn_offsets[i] + offsetof(LFN_ENT, alias_checksum),
490 sizeof(sum), &sum);
491 }
492 break;
493 }
494 }
495 }
496
497 *lfn_offset = lfn_offsets[0];
498 lfn = cnv_unicode(lfn_unicode, UNTIL_0, 1);
499 lfn_reset();
500 return (lfn);
501 }
502
503 void lfn_check_orphaned(void)
504 {
505 char *long_name;
506
507 if (lfn_slot == -1)
508 return;
509
510 long_name = CNV_PARTS_SO_FAR();
511 printf("Orphaned long file name part \"%s\"\n", long_name);
512 free(long_name);
513 if (interactive)
514 printf("1: Delete.\n2: Leave it.\n");
515 else
516 printf(" Auto-deleting.\n");
517 if (!interactive || get_key("12", "?") == '1') {
518 clear_lfn_slots(0, lfn_parts - 1);
519 }
520 lfn_reset();
521 }