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