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