lfn.c 14 KB

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