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