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