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