| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
|---|
| 3 | * Copyright (c) 2011 Oleg Romanenko
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup fs
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * @file fat_dentry.c
|
|---|
| 36 | * @brief Functions that work with FAT directory entries.
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include "fat_dentry.h"
|
|---|
| 40 | #include <ctype.h>
|
|---|
| 41 | #include <str.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <byteorder.h>
|
|---|
| 44 | #include <assert.h>
|
|---|
| 45 | #include <unistd.h>
|
|---|
| 46 | #include <sys/types.h>
|
|---|
| 47 |
|
|---|
| 48 | /** Compare path component with the name read from the dentry.
|
|---|
| 49 | *
|
|---|
| 50 | * This function compares the path component with the name read from the dentry.
|
|---|
| 51 | * The comparison is case insensitive and tolerates a mismatch on the trailing
|
|---|
| 52 | * dot character at the end of the name (i.e. when there is a dot, but no
|
|---|
| 53 | * extension).
|
|---|
| 54 | *
|
|---|
| 55 | * @param name Node name read from the dentry.
|
|---|
| 56 | * @param component Path component.
|
|---|
| 57 | *
|
|---|
| 58 | * @return Zero on match, non-zero otherwise.
|
|---|
| 59 | */
|
|---|
| 60 | int fat_dentry_namecmp(char *name, const char *component)
|
|---|
| 61 | {
|
|---|
| 62 | int rc;
|
|---|
| 63 | size_t size;
|
|---|
| 64 |
|
|---|
| 65 | if (!(rc = stricmp(name, component)))
|
|---|
| 66 | return rc;
|
|---|
| 67 | if (!str_chr(name, '.')) {
|
|---|
| 68 | /*
|
|---|
| 69 | * There is no '.' in the name, so we know that there is enough
|
|---|
| 70 | * space for appending an extra '.' to name.
|
|---|
| 71 | */
|
|---|
| 72 | size = str_size(name);
|
|---|
| 73 | name[size] = '.';
|
|---|
| 74 | name[size + 1] = '\0';
|
|---|
| 75 | rc = stricmp(name, component);
|
|---|
| 76 | }
|
|---|
| 77 | return rc;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | void fat_dentry_name_get(const fat_dentry_t *d, char *buf)
|
|---|
| 81 | {
|
|---|
| 82 | unsigned int i;
|
|---|
| 83 |
|
|---|
| 84 | for (i = 0; i < FAT_NAME_LEN; i++) {
|
|---|
| 85 | if (d->name[i] == FAT_PAD)
|
|---|
| 86 | break;
|
|---|
| 87 |
|
|---|
| 88 | if (d->name[i] == FAT_DENTRY_E5_ESC)
|
|---|
| 89 | *buf++ = 0xe5;
|
|---|
| 90 | else {
|
|---|
| 91 | if (d->lcase & FAT_LCASE_LOWER_NAME)
|
|---|
| 92 | *buf++ = tolower(d->name[i]);
|
|---|
| 93 | else
|
|---|
| 94 | *buf++ = d->name[i];
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if (d->ext[0] != FAT_PAD)
|
|---|
| 99 | *buf++ = '.';
|
|---|
| 100 |
|
|---|
| 101 | for (i = 0; i < FAT_EXT_LEN; i++) {
|
|---|
| 102 | if (d->ext[i] == FAT_PAD) {
|
|---|
| 103 | *buf = '\0';
|
|---|
| 104 | return;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if (d->ext[i] == FAT_DENTRY_E5_ESC)
|
|---|
| 108 | *buf++ = 0xe5;
|
|---|
| 109 | else {
|
|---|
| 110 | if (d->lcase & FAT_LCASE_LOWER_EXT)
|
|---|
| 111 | *buf++ = tolower(d->ext[i]);
|
|---|
| 112 | else
|
|---|
| 113 | *buf++ = d->ext[i];
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | *buf = '\0';
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | void fat_dentry_name_set(fat_dentry_t *d, const char *name)
|
|---|
| 121 | {
|
|---|
| 122 | unsigned int i;
|
|---|
| 123 | const char fake_ext[] = " ";
|
|---|
| 124 | bool lower_name = true;
|
|---|
| 125 | bool lower_ext = true;
|
|---|
| 126 |
|
|---|
| 127 | for (i = 0; i < FAT_NAME_LEN; i++) {
|
|---|
| 128 | switch ((uint8_t) *name) {
|
|---|
| 129 | case 0xe5:
|
|---|
| 130 | d->name[i] = FAT_DENTRY_E5_ESC;
|
|---|
| 131 | name++;
|
|---|
| 132 | break;
|
|---|
| 133 | case '\0':
|
|---|
| 134 | case '.':
|
|---|
| 135 | d->name[i] = FAT_PAD;
|
|---|
| 136 | break;
|
|---|
| 137 | default:
|
|---|
| 138 | if (isalpha(*name)) {
|
|---|
| 139 | if (!islower(*name))
|
|---|
| 140 | lower_name = false;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | d->name[i] = toupper(*name++);
|
|---|
| 144 | break;
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | if (*name++ != '.')
|
|---|
| 149 | name = fake_ext;
|
|---|
| 150 |
|
|---|
| 151 | for (i = 0; i < FAT_EXT_LEN; i++) {
|
|---|
| 152 | switch ((uint8_t) *name) {
|
|---|
| 153 | case 0xe5:
|
|---|
| 154 | d->ext[i] = FAT_DENTRY_E5_ESC;
|
|---|
| 155 | name++;
|
|---|
| 156 | break;
|
|---|
| 157 | case '\0':
|
|---|
| 158 | d->ext[i] = FAT_PAD;
|
|---|
| 159 | break;
|
|---|
| 160 | default:
|
|---|
| 161 | if (isalpha(*name)) {
|
|---|
| 162 | if (!islower(*name))
|
|---|
| 163 | lower_ext = false;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | d->ext[i] = toupper(*name++);
|
|---|
| 167 | break;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | if (lower_name)
|
|---|
| 172 | d->lcase |= FAT_LCASE_LOWER_NAME;
|
|---|
| 173 | else
|
|---|
| 174 | d->lcase &= ~FAT_LCASE_LOWER_NAME;
|
|---|
| 175 |
|
|---|
| 176 | if (lower_ext)
|
|---|
| 177 | d->lcase |= FAT_LCASE_LOWER_EXT;
|
|---|
| 178 | else
|
|---|
| 179 | d->lcase &= ~FAT_LCASE_LOWER_EXT;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d)
|
|---|
| 183 | {
|
|---|
| 184 | if (d->attr == FAT_ATTR_LFN) {
|
|---|
| 185 | /* long name entry */
|
|---|
| 186 | if (FAT_LFN_ORDER(d) & FAT_LFN_ERASED)
|
|---|
| 187 | return FAT_DENTRY_FREE;
|
|---|
| 188 | else
|
|---|
| 189 | return FAT_DENTRY_LFN;
|
|---|
| 190 | }
|
|---|
| 191 | if (d->attr & FAT_ATTR_VOLLABEL) {
|
|---|
| 192 | /* volume label entry */
|
|---|
| 193 | return FAT_DENTRY_SKIP;
|
|---|
| 194 | }
|
|---|
| 195 | if (d->name[0] == FAT_DENTRY_ERASED) {
|
|---|
| 196 | /* not-currently-used entry */
|
|---|
| 197 | return FAT_DENTRY_FREE;
|
|---|
| 198 | }
|
|---|
| 199 | if (d->name[0] == FAT_DENTRY_UNUSED) {
|
|---|
| 200 | /* never used entry */
|
|---|
| 201 | return FAT_DENTRY_LAST;
|
|---|
| 202 | }
|
|---|
| 203 | if (d->name[0] == FAT_DENTRY_DOT) {
|
|---|
| 204 | /*
|
|---|
| 205 | * Most likely '.' or '..'.
|
|---|
| 206 | * It cannot occur in a regular file name.
|
|---|
| 207 | */
|
|---|
| 208 | return FAT_DENTRY_SKIP;
|
|---|
| 209 | }
|
|---|
| 210 | return FAT_DENTRY_VALID;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /** Compute checksum of Node name.
|
|---|
| 214 | *
|
|---|
| 215 | * Returns an unsigned byte checksum computed on an unsigned byte
|
|---|
| 216 | * array. The array must be 11 bytes long and is assumed to contain
|
|---|
| 217 | * a name stored in the format of a MS-DOS directory entry.
|
|---|
| 218 | *
|
|---|
| 219 | * @param name Node name read from the dentry.
|
|---|
| 220 | *
|
|---|
| 221 | * @return An 8-bit unsigned checksum of the name.
|
|---|
| 222 | */
|
|---|
| 223 | uint8_t fat_dentry_chksum(uint8_t *name)
|
|---|
| 224 | {
|
|---|
| 225 | uint8_t i, sum = 0;
|
|---|
| 226 |
|
|---|
| 227 | for (i = 0; i < (FAT_NAME_LEN + FAT_EXT_LEN); i++)
|
|---|
| 228 | sum = ((sum & 1) ? 0x80 : 0) + (sum >> 1) + name[i];
|
|---|
| 229 |
|
|---|
| 230 | return sum;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /** Get number of bytes in a string with size limit.
|
|---|
| 234 | *
|
|---|
| 235 | * @param str NULL-terminated (or not) string. The pointer comes from a packed
|
|---|
| 236 | * structure and as such is expected to be unaligned.
|
|---|
| 237 | * @param size Maximum number of bytes to consider.
|
|---|
| 238 | *
|
|---|
| 239 | * @return Number of bytes in string (without 0 and ff).
|
|---|
| 240 | *
|
|---|
| 241 | */
|
|---|
| 242 | size_t fat_lfn_str_nlength(const unaligned_uint16_t *str, size_t size)
|
|---|
| 243 | {
|
|---|
| 244 | size_t offset = 0;
|
|---|
| 245 |
|
|---|
| 246 | while (offset < size) {
|
|---|
| 247 | if (str[offset] == 0 || str[offset] == FAT_LFN_PAD)
|
|---|
| 248 | break;
|
|---|
| 249 | offset++;
|
|---|
| 250 | }
|
|---|
| 251 | return offset;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /** Get number of bytes in a FAT long entry occuped by characters.
|
|---|
| 255 | *
|
|---|
| 256 | * @param d FAT long entry.
|
|---|
| 257 | *
|
|---|
| 258 | * @return Number of bytes.
|
|---|
| 259 | *
|
|---|
| 260 | */
|
|---|
| 261 | size_t fat_lfn_size(const fat_dentry_t *d)
|
|---|
| 262 | {
|
|---|
| 263 | size_t size = 0;
|
|---|
| 264 |
|
|---|
| 265 | size += fat_lfn_str_nlength(FAT_LFN_PART1(d), FAT_LFN_PART1_SIZE);
|
|---|
| 266 | size += fat_lfn_str_nlength(FAT_LFN_PART2(d), FAT_LFN_PART2_SIZE);
|
|---|
| 267 | size += fat_lfn_str_nlength(FAT_LFN_PART3(d), FAT_LFN_PART3_SIZE);
|
|---|
| 268 |
|
|---|
| 269 | return size;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | size_t fat_lfn_get_entry(const fat_dentry_t *d, uint16_t *dst, size_t *offset)
|
|---|
| 273 | {
|
|---|
| 274 | int i;
|
|---|
| 275 | for (i = FAT_LFN_PART3_SIZE - 1; i >= 0 && *offset > 0; i--) {
|
|---|
| 276 | if (d->lfn.part3[i] == 0 || d->lfn.part3[i] == FAT_LFN_PAD)
|
|---|
| 277 | continue;
|
|---|
| 278 | (*offset)--;
|
|---|
| 279 | dst[(*offset)] = uint16_t_le2host(d->lfn.part3[i]);
|
|---|
| 280 | }
|
|---|
| 281 | for (i = FAT_LFN_PART2_SIZE - 1; i >= 0 && *offset > 0; i--) {
|
|---|
| 282 | if (d->lfn.part2[i] == 0 || d->lfn.part2[i] == FAT_LFN_PAD)
|
|---|
| 283 | continue;
|
|---|
| 284 | (*offset)--;
|
|---|
| 285 | dst[(*offset)] = uint16_t_le2host(d->lfn.part2[i]);
|
|---|
| 286 | }
|
|---|
| 287 | for (i = FAT_LFN_PART1_SIZE - 1; i >= 0 && *offset > 0; i--) {
|
|---|
| 288 | if (d->lfn.part1[i] == 0 || d->lfn.part1[i] == FAT_LFN_PAD)
|
|---|
| 289 | continue;
|
|---|
| 290 | (*offset)--;
|
|---|
| 291 | dst[(*offset)] = uint16_t_le2host(d->lfn.part1[i]);
|
|---|
| 292 | }
|
|---|
| 293 | return *offset;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | size_t fat_lfn_set_entry(const uint16_t *src, size_t *offset, size_t size,
|
|---|
| 297 | fat_dentry_t *d)
|
|---|
| 298 | {
|
|---|
| 299 | size_t idx;
|
|---|
| 300 | for (idx = 0; idx < FAT_LFN_PART1_SIZE; idx++) {
|
|---|
| 301 | if (*offset < size) {
|
|---|
| 302 | d->lfn.part1[idx] = host2uint16_t_le(src[*offset]);
|
|---|
| 303 | (*offset)++;
|
|---|
| 304 | } else
|
|---|
| 305 | d->lfn.part1[idx] = FAT_LFN_PAD;
|
|---|
| 306 | }
|
|---|
| 307 | for (idx = 0; idx < FAT_LFN_PART2_SIZE; idx++) {
|
|---|
| 308 | if (*offset < size) {
|
|---|
| 309 | d->lfn.part2[idx] = host2uint16_t_le(src[*offset]);
|
|---|
| 310 | (*offset)++;
|
|---|
| 311 | } else
|
|---|
| 312 | d->lfn.part2[idx] = FAT_LFN_PAD;
|
|---|
| 313 | }
|
|---|
| 314 | for (idx = 0; idx < FAT_LFN_PART3_SIZE; idx++) {
|
|---|
| 315 | if (*offset < size) {
|
|---|
| 316 | d->lfn.part3[idx] = host2uint16_t_le(src[*offset]);
|
|---|
| 317 | (*offset)++;
|
|---|
| 318 | } else
|
|---|
| 319 | d->lfn.part3[idx] = FAT_LFN_PAD;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | if (src[*offset] == 0)
|
|---|
| 323 | offset++;
|
|---|
| 324 | FAT_LFN_ATTR(d) = FAT_ATTR_LFN;
|
|---|
| 325 | d->lfn.type = 0;
|
|---|
| 326 | d->lfn.firstc_lo = 0;
|
|---|
| 327 |
|
|---|
| 328 | return *offset;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | void str_to_ascii(char *dst, const char *src, size_t count, uint8_t pad)
|
|---|
| 332 | {
|
|---|
| 333 | wchar_t ch;
|
|---|
| 334 | size_t off = 0;
|
|---|
| 335 | size_t i = 0;
|
|---|
| 336 |
|
|---|
| 337 | while (i < count) {
|
|---|
| 338 | if ((ch = str_decode(src, &off, STR_NO_LIMIT)) != 0) {
|
|---|
| 339 | if (ascii_check(ch) && IS_D_CHAR(ch))
|
|---|
| 340 | *dst = toupper(ch);
|
|---|
| 341 | else
|
|---|
| 342 | *dst = pad;
|
|---|
| 343 | } else
|
|---|
| 344 | break;
|
|---|
| 345 |
|
|---|
| 346 | dst++;
|
|---|
| 347 | i++;
|
|---|
| 348 | }
|
|---|
| 349 | *dst = '\0';
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | bool fat_valid_name(const char *name)
|
|---|
| 353 | {
|
|---|
| 354 | wchar_t ch;
|
|---|
| 355 | size_t offset=0;
|
|---|
| 356 | bool result = true;
|
|---|
| 357 |
|
|---|
| 358 | while ((ch = str_decode(name, &offset, STR_NO_LIMIT)) != 0) {
|
|---|
| 359 | if (str_chr(FAT_STOP_CHARS, ch) != NULL) {
|
|---|
| 360 | result = false;
|
|---|
| 361 | break;
|
|---|
| 362 | }
|
|---|
| 363 | }
|
|---|
| 364 | return result;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | bool fat_valid_short_name(const char *name)
|
|---|
| 368 | {
|
|---|
| 369 | unsigned int i;
|
|---|
| 370 | unsigned int dot = 0;
|
|---|
| 371 | bool dot_found = false;
|
|---|
| 372 |
|
|---|
| 373 | for (i = 0; name[i]; i++) {
|
|---|
| 374 | if (name[i] == '.') {
|
|---|
| 375 | if (dot_found) {
|
|---|
| 376 | return false;
|
|---|
| 377 | } else {
|
|---|
| 378 | dot_found = true;
|
|---|
| 379 | dot = i;
|
|---|
| 380 | }
|
|---|
| 381 | } else {
|
|---|
| 382 | if (!IS_D_CHAR(name[i]))
|
|---|
| 383 | return false;
|
|---|
| 384 | }
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | if (dot_found) {
|
|---|
| 388 | if (dot > FAT_NAME_LEN)
|
|---|
| 389 | return false;
|
|---|
| 390 | if (i - dot > FAT_EXT_LEN + 1)
|
|---|
| 391 | return false;
|
|---|
| 392 | } else {
|
|---|
| 393 | if (i > FAT_NAME_LEN)
|
|---|
| 394 | return false;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | return true;
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | size_t utf16_length(const uint16_t *wstr)
|
|---|
| 401 | {
|
|---|
| 402 | size_t len = 0;
|
|---|
| 403 |
|
|---|
| 404 | while (*wstr++ != 0)
|
|---|
| 405 | len++;
|
|---|
| 406 |
|
|---|
| 407 | return len;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | /**
|
|---|
| 411 | * @}
|
|---|
| 412 | */
|
|---|