lfn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /* lfn.c - Functions for handling VFAT long filenames
  2. Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  3. Copyright (C) 2008-2013 Daniel Baumann <mail@daniel-baumann.ch>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. On Debian systems, the complete text of the GNU General Public License
  15. can be found in /usr/share/common-licenses/GPL-3 file.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. #include <time.h>
  22. #include "common.h"
  23. #include "io.h"
  24. #include "dosfsck.h"
  25. #include "lfn.h"
  26. #include "file.h"
  27. typedef struct {
  28. __u8 id; /* sequence number for slot */
  29. __u8 name0_4[10]; /* first 5 characters in name */
  30. __u8 attr; /* attribute byte */
  31. __u8 reserved; /* always 0 */
  32. __u8 alias_checksum; /* checksum for 8.3 alias */
  33. __u8 name5_10[12]; /* 6 more characters in name */
  34. __u16 start; /* starting cluster number, 0 in long slots */
  35. __u8 name11_12[4]; /* last 2 characters in name */
  36. } LFN_ENT;
  37. #define LFN_ID_START 0x40
  38. #define LFN_ID_SLOTMASK 0x1f
  39. #define CHARS_PER_LFN 13
  40. /* These modul-global vars represent the state of the LFN parser */
  41. unsigned char *lfn_unicode = NULL;
  42. unsigned char lfn_checksum;
  43. int lfn_slot = -1;
  44. loff_t *lfn_offsets = NULL;
  45. int lfn_parts = 0;
  46. static unsigned char fat_uni2esc[64] = {
  47. '0', '1', '2', '3', '4', '5', '6', '7',
  48. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
  49. 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  50. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  51. 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  52. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
  53. 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  54. 'u', 'v', 'w', 'x', 'y', 'z', '+', '-'
  55. };
  56. /* This defines which unicode chars are directly convertable to ISO-8859-1 */
  57. #define UNICODE_CONVERTABLE(cl,ch) (ch == 0 && (cl < 0x80 || cl >= 0xa0))
  58. /* for maxlen param */
  59. #define UNTIL_0 INT_MAX
  60. /* Convert name part in 'lfn' from unicode to ASCII */
  61. #define CNV_THIS_PART(lfn) \
  62. ({ \
  63. unsigned char __part_uni[CHARS_PER_LFN*2]; \
  64. copy_lfn_part( __part_uni, lfn ); \
  65. cnv_unicode( __part_uni, CHARS_PER_LFN, 0 ); \
  66. })
  67. /* Convert name parts collected so far (from previous slots) from unicode to
  68. * ASCII */
  69. #define CNV_PARTS_SO_FAR() \
  70. (cnv_unicode( lfn_unicode+(lfn_slot*CHARS_PER_LFN*2), \
  71. lfn_parts*CHARS_PER_LFN, 0 ))
  72. /* This function converts an unicode string to a normal ASCII string, assuming
  73. * ISO-8859-1 charset. Characters not in 8859-1 are converted to the same
  74. * escape notation as used by the kernel, i.e. the uuencode-like ":xxx" */
  75. static char *cnv_unicode(const unsigned char *uni, int maxlen, int use_q)
  76. {
  77. const unsigned char *up;
  78. unsigned char *out, *cp;
  79. int len, val;
  80. for (len = 0, up = uni; (up - uni) / 2 < maxlen && (up[0] || up[1]);
  81. up += 2) {
  82. if (UNICODE_CONVERTABLE(up[0], up[1]))
  83. ++len;
  84. else
  85. len += 4;
  86. }
  87. cp = out = use_q ? qalloc(&mem_queue, len + 1) : alloc(len + 1);
  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. return (char *)out;
  105. }
  106. static void copy_lfn_part(unsigned char *dst, LFN_ENT * lfn)
  107. {
  108. memcpy(dst, lfn->name0_4, 10);
  109. memcpy(dst + 10, lfn->name5_10, 12);
  110. memcpy(dst + 22, lfn->name11_12, 4);
  111. }
  112. static void clear_lfn_slots(int start, int end)
  113. {
  114. int i;
  115. LFN_ENT empty;
  116. /* New dir entry is zeroed except first byte, which is set to 0xe5.
  117. * This is to avoid that some FAT-reading OSes (not Linux! ;) stop reading
  118. * a directory at the first zero entry...
  119. */
  120. memset(&empty, 0, sizeof(empty));
  121. empty.id = DELETED_FLAG;
  122. for (i = start; i <= end; ++i) {
  123. fs_write(lfn_offsets[i], sizeof(LFN_ENT), &empty);
  124. }
  125. }
  126. void lfn_fix_checksum(loff_t from, loff_t to, const char *short_name)
  127. {
  128. int i;
  129. __u8 sum;
  130. for (sum = 0, i = 0; i < 11; i++)
  131. sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + short_name[i];
  132. for (; from < to; from += sizeof(LFN_ENT)) {
  133. fs_write(from + offsetof(LFN_ENT, alias_checksum), sizeof(sum), &sum);
  134. }
  135. }
  136. void lfn_reset(void)
  137. {
  138. if (lfn_unicode)
  139. free(lfn_unicode);
  140. lfn_unicode = NULL;
  141. if (lfn_offsets)
  142. free(lfn_offsets);
  143. lfn_offsets = NULL;
  144. lfn_slot = -1;
  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. int slot = lfn->id & LFN_ID_SLOTMASK;
  152. unsigned offset;
  153. if (lfn_slot == 0)
  154. lfn_check_orphaned();
  155. if (de->attr != VFAT_LN_ATTR)
  156. die("lfn_add_slot called with non-LFN directory entry");
  157. if (lfn->id & LFN_ID_START && slot != 0) {
  158. if (lfn_slot != -1) {
  159. int can_clear = 0;
  160. /* There is already a LFN "in progess", so it is an error that a
  161. * new start entry is here. */
  162. /* Causes: 1) if slot# == expected: start bit set mysteriously, 2)
  163. * old LFN overwritten by new one */
  164. /* Fixes: 1) delete previous LFN 2) if slot# == expected and
  165. * checksum ok: clear start bit */
  166. /* XXX: Should delay that until next LFN known (then can better
  167. * display the name) */
  168. printf("A new long file name starts within an old one.\n");
  169. if (slot == lfn_slot && 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. } else
  183. 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 = slot;
  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. } else if (lfn_slot == -1 && slot != 0) {
  206. /* No LFN in progress, but slot found; start bit missing */
  207. /* Causes: 1) start bit got lost, 2) Previous slot with start bit got
  208. * lost */
  209. /* Fixes: 1) delete LFN, 2) set start bit */
  210. char *part = CNV_THIS_PART(lfn);
  211. printf("Long filename fragment \"%s\" found outside a LFN "
  212. "sequence.\n (Maybe the start bit is missing on the "
  213. "last fragment)\n", part);
  214. if (interactive) {
  215. printf("1: Delete fragment\n2: Leave it as it is.\n"
  216. "3: Set start bit\n");
  217. } else
  218. printf(" Not auto-correcting this.\n");
  219. switch (interactive ? get_key("123", "?") : '2') {
  220. case '1':
  221. if (!lfn_offsets)
  222. lfn_offsets = alloc(sizeof(loff_t));
  223. lfn_offsets[0] = dir_offset;
  224. clear_lfn_slots(0, 0);
  225. lfn_reset();
  226. return;
  227. case '2':
  228. lfn_reset();
  229. return;
  230. case '3':
  231. lfn->id |= LFN_ID_START;
  232. fs_write(dir_offset + offsetof(LFN_ENT, id),
  233. sizeof(lfn->id), &lfn->id);
  234. lfn_slot = slot;
  235. lfn_checksum = lfn->alias_checksum;
  236. lfn_unicode = alloc((lfn_slot * CHARS_PER_LFN + 1) * 2);
  237. lfn_offsets = alloc(lfn_slot * sizeof(loff_t));
  238. lfn_parts = 0;
  239. break;
  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", slot, lfn_slot);
  250. if (lfn->alias_checksum == lfn_checksum && lfn_slot > 0) {
  251. char *part1 = CNV_THIS_PART(lfn);
  252. char *part2 = CNV_PARTS_SO_FAR();
  253. printf(" It could be that just the number is wrong\n"
  254. " if \"%s\" seems to match \"%s\".\n", part1, part2);
  255. free(part1);
  256. free(part2);
  257. can_fix = 1;
  258. }
  259. if (interactive) {
  260. printf
  261. ("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. } else
  265. 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. } else
  297. 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
  329. printf("Auto-setting to 0.\n");
  330. if (!interactive || get_key("12", "?") == '1') {
  331. lfn->reserved = 0;
  332. fs_write(dir_offset + offsetof(LFN_ENT, reserved),
  333. sizeof(lfn->reserved), &lfn->reserved);
  334. }
  335. }
  336. if (lfn->start != CT_LE_W(0)) {
  337. printf("Start cluster field in VFAT long filename slot is not 0 "
  338. "(but 0x%04x).\n", lfn->start);
  339. if (interactive)
  340. printf("1: Fix.\n2: Leave it.\n");
  341. else
  342. printf("Auto-setting to 0.\n");
  343. if (!interactive || get_key("12", "?") == '1') {
  344. lfn->start = CT_LE_W(0);
  345. fs_write(dir_offset + offsetof(LFN_ENT, start),
  346. sizeof(lfn->start), &lfn->start);
  347. }
  348. }
  349. }
  350. /* This function is always called when de->attr != VFAT_LN_ATTR is found, to
  351. * retrieve the previously constructed LFN. */
  352. char *lfn_get(DIR_ENT * de, loff_t * lfn_offset)
  353. {
  354. char *lfn;
  355. __u8 sum;
  356. int i;
  357. *lfn_offset = 0;
  358. if (de->attr == VFAT_LN_ATTR)
  359. die("lfn_get called with LFN directory entry");
  360. #if 0
  361. if (de->lcase)
  362. printf("lcase=%02x\n", de->lcase);
  363. #endif
  364. if (lfn_slot == -1)
  365. /* no long name for this file */
  366. return NULL;
  367. if (lfn_slot != 0) {
  368. /* The long name isn't finished yet. */
  369. /* Causes: 1) LFN slot overwritten by non-VFAT aware tool */
  370. /* Fixes: 1) delete LFN 2) move overwriting entry to somewhere else
  371. * and let user enter missing part of LFN (hard to do :-()
  372. * 3) renumber entries and truncate name */
  373. char *long_name = CNV_PARTS_SO_FAR();
  374. char *short_name = file_name(de->name);
  375. printf("Unfinished long file name \"%s\".\n"
  376. " (Start may have been overwritten by %s)\n",
  377. long_name, short_name);
  378. free(long_name);
  379. if (interactive) {
  380. printf("1: Delete LFN\n2: Leave it as it is.\n"
  381. "3: Fix numbering (truncates long name and attaches "
  382. "it to short name %s)\n", short_name);
  383. } else
  384. printf(" Not auto-correcting this.\n");
  385. switch (interactive ? get_key("123", "?") : '2') {
  386. case '1':
  387. clear_lfn_slots(0, lfn_parts - 1);
  388. lfn_reset();
  389. return NULL;
  390. case '2':
  391. lfn_reset();
  392. return NULL;
  393. case '3':
  394. for (i = 0; i < lfn_parts; ++i) {
  395. __u8 id = (lfn_parts - i) | (i == 0 ? LFN_ID_START : 0);
  396. fs_write(lfn_offsets[i] + offsetof(LFN_ENT, id),
  397. sizeof(id), &id);
  398. }
  399. memmove(lfn_unicode, lfn_unicode + lfn_slot * CHARS_PER_LFN * 2,
  400. lfn_parts * CHARS_PER_LFN * 2);
  401. break;
  402. }
  403. }
  404. for (sum = 0, i = 0; i < 11; i++)
  405. sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + de->name[i];
  406. if (sum != lfn_checksum) {
  407. /* checksum doesn't match, long name doesn't apply to this alias */
  408. /* Causes: 1) alias renamed */
  409. /* Fixes: 1) Fix checksum in LFN entries */
  410. char *long_name = CNV_PARTS_SO_FAR();
  411. char *short_name = file_name(de->name);
  412. printf("Wrong checksum for long file name \"%s\".\n"
  413. " (Short name %s may have changed without updating the long name)\n",
  414. long_name, short_name);
  415. free(long_name);
  416. if (interactive) {
  417. printf("1: Delete LFN\n2: Leave it as it is.\n"
  418. "3: Fix checksum (attaches to short name %s)\n", short_name);
  419. } else
  420. printf(" Not auto-correcting this.\n");
  421. if (interactive) {
  422. switch (get_key("123", "?")) {
  423. case '1':
  424. clear_lfn_slots(0, lfn_parts - 1);
  425. lfn_reset();
  426. return NULL;
  427. case '2':
  428. lfn_reset();
  429. return NULL;
  430. case '3':
  431. for (i = 0; i < lfn_parts; ++i) {
  432. fs_write(lfn_offsets[i] + offsetof(LFN_ENT, alias_checksum),
  433. sizeof(sum), &sum);
  434. }
  435. break;
  436. }
  437. }
  438. }
  439. *lfn_offset = lfn_offsets[0];
  440. lfn = cnv_unicode(lfn_unicode, UNTIL_0, 1);
  441. lfn_reset();
  442. return (lfn);
  443. }
  444. void lfn_check_orphaned(void)
  445. {
  446. char *long_name;
  447. if (lfn_slot == -1)
  448. return;
  449. long_name = CNV_PARTS_SO_FAR();
  450. printf("Orphaned long file name part \"%s\"\n", long_name);
  451. if (interactive)
  452. printf("1: Delete.\n2: Leave it.\n");
  453. else
  454. printf(" Auto-deleting.\n");
  455. if (!interactive || get_key("12", "?") == '1') {
  456. clear_lfn_slots(0, lfn_parts - 1);
  457. }
  458. lfn_reset();
  459. }