lfn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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_fix_checksum(loff_t from, loff_t to, const char *short_name)
  125. {
  126. int i;
  127. __u8 sum;
  128. for (sum = 0, i = 0; i < 11; i++)
  129. sum = (((sum&1) << 7) | ((sum&0xfe) >> 1)) + short_name[i];
  130. for( ; from < to; from += sizeof(LFN_ENT) ) {
  131. fs_write( from + offsetof(LFN_ENT,alias_checksum), sizeof(sum), &sum );
  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. /* This function is only called with de->attr == VFAT_LN_ATTR. It stores part
  145. * of the long name. */
  146. void lfn_add_slot( DIR_ENT *de, loff_t dir_offset )
  147. {
  148. LFN_ENT *lfn = (LFN_ENT *)de;
  149. int slot = lfn->id & LFN_ID_SLOTMASK;
  150. unsigned offset;
  151. if (lfn_slot == 0) lfn_check_orphaned();
  152. if (de->attr != VFAT_LN_ATTR)
  153. die("lfn_add_slot called with non-LFN directory entry");
  154. if (lfn->id & LFN_ID_START && slot != 0) {
  155. if (lfn_slot != -1) {
  156. int can_clear = 0;
  157. /* There is already a LFN "in progess", so it is an error that a
  158. * new start entry is here. */
  159. /* Causes: 1) if slot# == expected: start bit set mysteriously, 2)
  160. * old LFN overwritten by new one */
  161. /* Fixes: 1) delete previous LFN 2) if slot# == expected and
  162. * checksum ok: clear start bit */
  163. /* XXX: Should delay that until next LFN known (then can better
  164. * display the name) */
  165. printf( "A new long file name starts within an old one.\n" );
  166. if (slot == lfn_slot &&
  167. lfn->alias_checksum == lfn_checksum) {
  168. char *part1 = CNV_THIS_PART(lfn);
  169. char *part2 = CNV_PARTS_SO_FAR();
  170. printf( " It could be that the LFN start bit is wrong here\n"
  171. " if \"%s\" seems to match \"%s\".\n", part1, part2 );
  172. free( part1 );
  173. free( part2 );
  174. can_clear = 1;
  175. }
  176. if (interactive) {
  177. printf( "1: Delete previous LFN\n2: Leave it as it is.\n" );
  178. if (can_clear)
  179. printf( "3: Clear start bit and concatenate LFNs\n" );
  180. }
  181. else printf( " Not auto-correcting this.\n" );
  182. if (interactive) {
  183. switch( get_key( can_clear ? "123" : "12", "?" )) {
  184. case '1':
  185. clear_lfn_slots( 0, lfn_parts-1 );
  186. lfn_reset();
  187. break;
  188. case '2':
  189. break;
  190. case '3':
  191. lfn->id &= ~LFN_ID_START;
  192. fs_write( dir_offset+offsetof(LFN_ENT,id),
  193. sizeof(lfn->id), &lfn->id );
  194. break;
  195. }
  196. }
  197. }
  198. lfn_slot = slot;
  199. lfn_checksum = lfn->alias_checksum;
  200. lfn_unicode = alloc( (lfn_slot*CHARS_PER_LFN+1)*2 );
  201. lfn_offsets = alloc( lfn_slot*sizeof(loff_t) );
  202. lfn_parts = 0;
  203. }
  204. else if (lfn_slot == -1 && slot != 0) {
  205. /* No LFN in progress, but slot found; start bit missing */
  206. /* Causes: 1) start bit got lost, 2) Previous slot with start bit got
  207. * lost */
  208. /* Fixes: 1) delete LFN, 2) set start bit */
  209. char *part = CNV_THIS_PART(lfn);
  210. printf( "Long filename fragment \"%s\" found outside a LFN "
  211. "sequence.\n (Maybe the start bit is missing on the "
  212. "last fragment)\n", part );
  213. if (interactive) {
  214. printf( "1: Delete fragment\n2: Leave it as it is.\n"
  215. "3: Set start bit\n" );
  216. }
  217. else printf( " Not auto-correcting this.\n" );
  218. switch( interactive ? get_key( "123", "?" ) : '2') {
  219. case '1':
  220. if (!lfn_offsets)
  221. lfn_offsets = alloc( 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 = slot;
  234. lfn_checksum = lfn->alias_checksum;
  235. lfn_unicode = alloc( (lfn_slot*CHARS_PER_LFN+1)*2 );
  236. lfn_offsets = alloc( lfn_slot*sizeof(loff_t) );
  237. lfn_parts = 0;
  238. break;
  239. }
  240. }
  241. else if (slot != 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. printf( "Unexpected long filename sequence number "
  249. "(%d vs. expected %d).\n",
  250. slot, lfn_slot );
  251. if (lfn->alias_checksum == lfn_checksum && lfn_slot > 0) {
  252. char *part1 = CNV_THIS_PART(lfn);
  253. char *part2 = CNV_PARTS_SO_FAR();
  254. printf( " It could be that just the number is wrong\n"
  255. " if \"%s\" seems to match \"%s\".\n", part1, part2 );
  256. free( part1 );
  257. free( part2 );
  258. can_fix = 1;
  259. }
  260. if (interactive) {
  261. printf( "1: Delete LFN\n2: Leave it as it is (and ignore LFN so far)\n" );
  262. if (can_fix)
  263. printf( "3: Correct sequence number\n" );
  264. }
  265. else printf( " Not auto-correcting this.\n" );
  266. switch( interactive ? get_key( can_fix ? "123" : "12", "?" ) : '2') {
  267. case '1':
  268. if (!lfn_offsets) {
  269. lfn_offsets = alloc( sizeof(loff_t) );
  270. lfn_parts = 0;
  271. }
  272. lfn_offsets[lfn_parts++] = dir_offset;
  273. clear_lfn_slots( 0, lfn_parts-1 );
  274. lfn_reset();
  275. return;
  276. case '2':
  277. lfn_reset();
  278. return;
  279. case '3':
  280. lfn->id = (lfn->id & ~LFN_ID_SLOTMASK) | lfn_slot;
  281. fs_write( dir_offset+offsetof(LFN_ENT,id),
  282. sizeof(lfn->id), &lfn->id );
  283. break;
  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. printf( "Checksum in long filename part wrong "
  291. "(%02x vs. expected %02x).\n",
  292. lfn->alias_checksum, lfn_checksum );
  293. if (interactive) {
  294. printf( "1: Delete LFN\n2: Leave it as it is.\n"
  295. "3: Correct checksum\n" );
  296. }
  297. else printf( " Not auto-correcting this.\n" );
  298. if (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. if (lfn_slot != -1) {
  316. lfn_slot--;
  317. offset = lfn_slot * CHARS_PER_LFN*2;
  318. copy_lfn_part( 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. if (lfn->reserved != 0) {
  324. printf( "Reserved field in VFAT long filename slot is not 0 "
  325. "(but 0x%02x).\n", lfn->reserved );
  326. if (interactive)
  327. printf( "1: Fix.\n2: Leave it.\n" );
  328. else printf( "Auto-setting to 0.\n" );
  329. if (!interactive || get_key("12","?") == '1') {
  330. lfn->reserved = 0;
  331. fs_write( dir_offset+offsetof(LFN_ENT,reserved),
  332. sizeof(lfn->reserved), &lfn->reserved );
  333. }
  334. }
  335. if (lfn->start != CT_LE_W(0)) {
  336. printf( "Start cluster field in VFAT long filename slot is not 0 "
  337. "(but 0x%04x).\n", lfn->start );
  338. if (interactive)
  339. printf( "1: Fix.\n2: Leave it.\n" );
  340. else printf( "Auto-setting to 0.\n" );
  341. if (!interactive || get_key("12","?") == '1') {
  342. lfn->start = CT_LE_W(0);
  343. fs_write( dir_offset+offsetof(LFN_ENT,start),
  344. sizeof(lfn->start),&lfn->start );
  345. }
  346. }
  347. }
  348. /* This function is always called when de->attr != VFAT_LN_ATTR is found, to
  349. * retrieve the previously constructed LFN. */
  350. char *lfn_get( DIR_ENT *de, loff_t *lfn_offset )
  351. {
  352. char *lfn;
  353. __u8 sum;
  354. int i;
  355. *lfn_offset = 0;
  356. if (de->attr == VFAT_LN_ATTR)
  357. die("lfn_get called with LFN directory entry");
  358. #if 0
  359. if (de->lcase)
  360. printf( "lcase=%02x\n",de->lcase );
  361. #endif
  362. if (lfn_slot == -1)
  363. /* no long name for this file */
  364. return NULL;
  365. if (lfn_slot != 0) {
  366. /* The long name isn't finished yet. */
  367. /* Causes: 1) LFN slot overwritten by non-VFAT aware tool */
  368. /* Fixes: 1) delete LFN 2) move overwriting entry to somewhere else
  369. * and let user enter missing part of LFN (hard to do :-()
  370. * 3) renumber entries and truncate name */
  371. char *long_name = CNV_PARTS_SO_FAR();
  372. char *short_name = file_name(de->name);
  373. printf( "Unfinished long file name \"%s\".\n"
  374. " (Start may have been overwritten by %s)\n",
  375. long_name, short_name );
  376. free( long_name );
  377. if (interactive) {
  378. printf( "1: Delete LFN\n2: Leave it as it is.\n"
  379. "3: Fix numbering (truncates long name and attaches "
  380. "it to short name %s)\n", short_name );
  381. }
  382. else printf( " Not auto-correcting this.\n" );
  383. switch( interactive ? get_key( "123", "?" ) : '2') {
  384. case '1':
  385. clear_lfn_slots( 0, lfn_parts-1 );
  386. lfn_reset();
  387. return NULL;
  388. case '2':
  389. lfn_reset();
  390. return NULL;
  391. case '3':
  392. for( i = 0; i < lfn_parts; ++i ) {
  393. __u8 id = (lfn_parts-i) | (i==0 ? LFN_ID_START : 0);
  394. fs_write( lfn_offsets[i]+offsetof(LFN_ENT,id),
  395. sizeof(id), &id );
  396. }
  397. memmove( lfn_unicode, lfn_unicode+lfn_slot*CHARS_PER_LFN*2,
  398. lfn_parts*CHARS_PER_LFN*2 );
  399. break;
  400. }
  401. }
  402. for (sum = 0, i = 0; i < 11; i++)
  403. sum = (((sum&1) << 7) | ((sum&0xfe) >> 1)) + de->name[i];
  404. if (sum != lfn_checksum) {
  405. /* checksum doesn't match, long name doesn't apply to this alias */
  406. /* Causes: 1) alias renamed */
  407. /* Fixes: 1) Fix checksum in LFN entries */
  408. char *long_name = CNV_PARTS_SO_FAR();
  409. char *short_name = file_name(de->name);
  410. printf( "Wrong checksum for long file name \"%s\".\n"
  411. " (Short name %s may have changed without updating the long name)\n",
  412. long_name, short_name );
  413. free( long_name );
  414. if (interactive) {
  415. printf( "1: Delete LFN\n2: Leave it as it is.\n"
  416. "3: Fix checksum (attaches to short name %s)\n",
  417. short_name );
  418. }
  419. else printf( " Not auto-correcting this.\n" );
  420. if (interactive) {
  421. switch( get_key( "123", "?" )) {
  422. case '1':
  423. clear_lfn_slots( 0, lfn_parts-1 );
  424. lfn_reset();
  425. return NULL;
  426. case '2':
  427. lfn_reset();
  428. return NULL;
  429. case '3':
  430. for( i = 0; i < lfn_parts; ++i ) {
  431. fs_write( lfn_offsets[i]+offsetof(LFN_ENT,alias_checksum),
  432. sizeof(sum), &sum );
  433. }
  434. break;
  435. }
  436. }
  437. }
  438. *lfn_offset = lfn_offsets[0];
  439. lfn = cnv_unicode( lfn_unicode, UNTIL_0, 1 );
  440. lfn_reset();
  441. return( lfn );
  442. }
  443. void lfn_check_orphaned(void)
  444. {
  445. char *long_name;
  446. if (lfn_slot == -1)
  447. return;
  448. long_name = CNV_PARTS_SO_FAR();
  449. printf("Orphaned long file name part \"%s\"\n", long_name);
  450. if (interactive)
  451. printf( "1: Delete.\n2: Leave it.\n" );
  452. else printf( " Auto-deleting.\n" );
  453. if (!interactive || get_key("12","?") == '1') {
  454. clear_lfn_slots(0, lfn_parts - 1);
  455. }
  456. lfn_reset();
  457. }