lfn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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]);
  80. 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. for (up = uni; (up - uni) / 2 < maxlen && (up[0] || up[1]); up += 2) {
  88. if (UNICODE_CONVERTABLE(up[0], up[1]))
  89. *cp++ = up[0];
  90. else {
  91. /* here the same escape notation is used as in the Linux kernel */
  92. *cp++ = ':';
  93. val = (up[1] << 8) + up[0];
  94. cp[2] = fat_uni2esc[val & 0x3f];
  95. val >>= 6;
  96. cp[1] = fat_uni2esc[val & 0x3f];
  97. val >>= 6;
  98. cp[0] = fat_uni2esc[val & 0x3f];
  99. cp += 3;
  100. }
  101. }
  102. *cp = 0;
  103. return (out);
  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. static void clear_lfn_slots(int start, int end)
  112. {
  113. int i;
  114. LFN_ENT empty;
  115. /* New dir entry is zeroed except first byte, which is set to 0xe5.
  116. * This is to avoid that some FAT-reading OSes (not Linux! ;) stop reading
  117. * a directory at the first zero entry...
  118. */
  119. memset(&empty, 0, sizeof(empty));
  120. empty.id = DELETED_FLAG;
  121. for (i = start; i <= end; ++i) {
  122. fs_write(lfn_offsets[i], sizeof(LFN_ENT), &empty);
  123. }
  124. }
  125. void lfn_fix_checksum(loff_t from, loff_t to, const char *short_name)
  126. {
  127. int i;
  128. __u8 sum;
  129. for (sum = 0, i = 0; i < 11; i++)
  130. sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + short_name[i];
  131. for (; from < to; from += sizeof(LFN_ENT)) {
  132. fs_write(from + offsetof(LFN_ENT, alias_checksum), sizeof(sum), &sum);
  133. }
  134. }
  135. void lfn_reset(void)
  136. {
  137. if (lfn_unicode)
  138. free(lfn_unicode);
  139. lfn_unicode = NULL;
  140. if (lfn_offsets)
  141. free(lfn_offsets);
  142. lfn_offsets = NULL;
  143. lfn_slot = -1;
  144. }
  145. /* This function is only called with de->attr == VFAT_LN_ATTR. It stores part
  146. * of the long name. */
  147. void lfn_add_slot(DIR_ENT * de, loff_t dir_offset)
  148. {
  149. LFN_ENT *lfn = (LFN_ENT *) de;
  150. int slot = lfn->id & LFN_ID_SLOTMASK;
  151. unsigned offset;
  152. if (lfn_slot == 0)
  153. lfn_check_orphaned();
  154. if (de->attr != VFAT_LN_ATTR)
  155. die("lfn_add_slot called with non-LFN directory entry");
  156. if (lfn->id & LFN_ID_START && slot != 0) {
  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 (slot == lfn_slot && lfn->alias_checksum == lfn_checksum) {
  169. char *part1 = CNV_THIS_PART(lfn);
  170. char *part2 = CNV_PARTS_SO_FAR();
  171. printf(" It could be that the LFN start bit is wrong here\n"
  172. " if \"%s\" seems to match \"%s\".\n", part1, part2);
  173. free(part1);
  174. free(part2);
  175. can_clear = 1;
  176. }
  177. if (interactive) {
  178. printf("1: Delete previous LFN\n2: Leave it as it is.\n");
  179. if (can_clear)
  180. printf("3: Clear start bit and concatenate LFNs\n");
  181. } else
  182. printf(" Not auto-correcting this.\n");
  183. if (interactive) {
  184. switch (get_key(can_clear ? "123" : "12", "?")) {
  185. case '1':
  186. clear_lfn_slots(0, lfn_parts - 1);
  187. lfn_reset();
  188. break;
  189. case '2':
  190. break;
  191. case '3':
  192. lfn->id &= ~LFN_ID_START;
  193. fs_write(dir_offset + offsetof(LFN_ENT, id),
  194. sizeof(lfn->id), &lfn->id);
  195. break;
  196. }
  197. }
  198. }
  199. lfn_slot = slot;
  200. lfn_checksum = lfn->alias_checksum;
  201. lfn_unicode = alloc((lfn_slot * CHARS_PER_LFN + 1) * 2);
  202. lfn_offsets = alloc(lfn_slot * sizeof(loff_t));
  203. lfn_parts = 0;
  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. } else
  217. 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. } else if (slot != lfn_slot) {
  241. /* wrong sequence number */
  242. /* Causes: 1) seq-no destroyed */
  243. /* Fixes: 1) delete LFN, 2) fix number (maybe only if following parts
  244. * are ok?, maybe only if checksum is ok?) (Attention: space
  245. * for name was allocated before!) */
  246. int can_fix = 0;
  247. printf("Unexpected long filename sequence number "
  248. "(%d vs. expected %d).\n", slot, lfn_slot);
  249. if (lfn->alias_checksum == lfn_checksum && lfn_slot > 0) {
  250. char *part1 = CNV_THIS_PART(lfn);
  251. char *part2 = CNV_PARTS_SO_FAR();
  252. printf(" It could be that just the number is wrong\n"
  253. " if \"%s\" seems to match \"%s\".\n", part1, part2);
  254. free(part1);
  255. free(part2);
  256. can_fix = 1;
  257. }
  258. if (interactive) {
  259. printf
  260. ("1: Delete LFN\n2: Leave it as it is (and ignore LFN so far)\n");
  261. if (can_fix)
  262. printf("3: Correct sequence number\n");
  263. } else
  264. printf(" Not auto-correcting this.\n");
  265. switch (interactive ? get_key(can_fix ? "123" : "12", "?") : '2') {
  266. case '1':
  267. if (!lfn_offsets) {
  268. lfn_offsets = alloc(sizeof(loff_t));
  269. lfn_parts = 0;
  270. }
  271. lfn_offsets[lfn_parts++] = dir_offset;
  272. clear_lfn_slots(0, lfn_parts - 1);
  273. lfn_reset();
  274. return;
  275. case '2':
  276. lfn_reset();
  277. return;
  278. case '3':
  279. lfn->id = (lfn->id & ~LFN_ID_SLOTMASK) | lfn_slot;
  280. fs_write(dir_offset + offsetof(LFN_ENT, id),
  281. sizeof(lfn->id), &lfn->id);
  282. break;
  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. printf("Checksum in long filename part wrong "
  290. "(%02x vs. expected %02x).\n",
  291. lfn->alias_checksum, lfn_checksum);
  292. if (interactive) {
  293. printf("1: Delete LFN\n2: Leave it as it is.\n"
  294. "3: Correct checksum\n");
  295. } else
  296. printf(" Not auto-correcting this.\n");
  297. if (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. if (lfn_slot != -1) {
  315. lfn_slot--;
  316. offset = lfn_slot * CHARS_PER_LFN * 2;
  317. copy_lfn_part(lfn_unicode + offset, lfn);
  318. if (lfn->id & LFN_ID_START)
  319. lfn_unicode[offset + 26] = lfn_unicode[offset + 27] = 0;
  320. lfn_offsets[lfn_parts++] = dir_offset;
  321. }
  322. if (lfn->reserved != 0) {
  323. printf("Reserved field in VFAT long filename slot is not 0 "
  324. "(but 0x%02x).\n", lfn->reserved);
  325. if (interactive)
  326. printf("1: Fix.\n2: Leave it.\n");
  327. else
  328. 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
  341. printf("Auto-setting to 0.\n");
  342. if (!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. /* This function is always called when de->attr != VFAT_LN_ATTR is found, to
  350. * retrieve the previously constructed LFN. */
  351. char *lfn_get(DIR_ENT * de, loff_t * lfn_offset)
  352. {
  353. char *lfn;
  354. __u8 sum;
  355. int i;
  356. *lfn_offset = 0;
  357. if (de->attr == VFAT_LN_ATTR)
  358. die("lfn_get called with LFN directory entry");
  359. #if 0
  360. if (de->lcase)
  361. printf("lcase=%02x\n", de->lcase);
  362. #endif
  363. if (lfn_slot == -1)
  364. /* no long name for this file */
  365. return NULL;
  366. if (lfn_slot != 0) {
  367. /* The long name isn't finished yet. */
  368. /* Causes: 1) LFN slot overwritten by non-VFAT aware tool */
  369. /* Fixes: 1) delete LFN 2) move overwriting entry to somewhere else
  370. * and let user enter missing part of LFN (hard to do :-()
  371. * 3) renumber entries and truncate name */
  372. char *long_name = CNV_PARTS_SO_FAR();
  373. char *short_name = file_name(de->name);
  374. printf("Unfinished long file name \"%s\".\n"
  375. " (Start may have been overwritten by %s)\n",
  376. long_name, short_name);
  377. free(long_name);
  378. if (interactive) {
  379. printf("1: Delete LFN\n2: Leave it as it is.\n"
  380. "3: Fix numbering (truncates long name and attaches "
  381. "it to short name %s)\n", short_name);
  382. } else
  383. printf(" Not auto-correcting this.\n");
  384. switch (interactive ? get_key("123", "?") : '2') {
  385. case '1':
  386. clear_lfn_slots(0, lfn_parts - 1);
  387. lfn_reset();
  388. return NULL;
  389. case '2':
  390. lfn_reset();
  391. return NULL;
  392. case '3':
  393. for (i = 0; i < lfn_parts; ++i) {
  394. __u8 id = (lfn_parts - i) | (i == 0 ? LFN_ID_START : 0);
  395. fs_write(lfn_offsets[i] + offsetof(LFN_ENT, id),
  396. sizeof(id), &id);
  397. }
  398. memmove(lfn_unicode, lfn_unicode + lfn_slot * CHARS_PER_LFN * 2,
  399. lfn_parts * CHARS_PER_LFN * 2);
  400. break;
  401. }
  402. }
  403. for (sum = 0, i = 0; i < 11; i++)
  404. sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + de->name[i];
  405. if (sum != lfn_checksum) {
  406. /* checksum doesn't match, long name doesn't apply to this alias */
  407. /* Causes: 1) alias renamed */
  408. /* Fixes: 1) Fix checksum in LFN entries */
  409. char *long_name = CNV_PARTS_SO_FAR();
  410. char *short_name = file_name(de->name);
  411. printf("Wrong checksum for long file name \"%s\".\n"
  412. " (Short name %s may have changed without updating the long name)\n",
  413. long_name, short_name);
  414. free(long_name);
  415. if (interactive) {
  416. printf("1: Delete LFN\n2: Leave it as it is.\n"
  417. "3: Fix checksum (attaches to short name %s)\n", short_name);
  418. } else
  419. 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
  453. printf(" Auto-deleting.\n");
  454. if (!interactive || get_key("12", "?") == '1') {
  455. clear_lfn_slots(0, lfn_parts - 1);
  456. }
  457. lfn_reset();
  458. }