lfn.c 14 KB

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