Changeset 7194a60 in mainline
- Timestamp:
- 2011-06-27T16:40:55Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5dfb1948
- Parents:
- 2e839dda
- Location:
- uspace/srv/fs/fat
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/fs/fat/fat_dentry.c
r2e839dda r7194a60 42 42 #include <byteorder.h> 43 43 #include <assert.h> 44 45 static bool is_d_char(const char ch)46 {47 if (isalnum(ch) || ch == '_')48 return true;49 else50 return false;51 }52 44 53 45 /** Compare path component with the name read from the dentry. … … 83 75 } 84 76 85 bool fat_dentry_name_verify(const char *name)86 {87 unsigned int i;88 unsigned int dot = 0;89 bool dot_found = false;90 91 92 for (i = 0; name[i]; i++) {93 if (name[i] == '.') {94 if (dot_found) {95 return false;96 } else {97 dot_found = true;98 dot = i;99 }100 } else {101 if (!is_d_char(name[i]))102 return false;103 }104 }105 106 if (dot_found) {107 if (dot > FAT_NAME_LEN)108 return false;109 if (i - dot > FAT_EXT_LEN + 1)110 return false;111 } else {112 if (i > FAT_NAME_LEN)113 return false;114 }115 116 return true;117 }118 119 77 void fat_dentry_name_get(const fat_dentry_t *d, char *buf) 120 78 { … … 282 240 283 241 while (offset < size) { 284 if (str[offset] == 0 || str[offset] == 0xffff)242 if (str[offset] == 0 || str[offset] == FAT_LFN_PAD) 285 243 break; 286 244 offset++; … … 307 265 } 308 266 309 size_t fat_lfn_ copy_part(const uint16_t *src, size_t src_size, uint16_t *dst, size_t *offset)267 size_t fat_lfn_get_part(const uint16_t *src, size_t src_size, wchar_t *dst, size_t *offset) 310 268 { 311 269 while (src_size!=0 && (*offset)!=0) { 312 270 src_size--; 313 if (src[src_size] == 0 || src[src_size] == 0xffff)271 if (src[src_size] == 0 || src[src_size] == FAT_LFN_PAD) 314 272 continue; 315 273 … … 320 278 } 321 279 322 size_t fat_lfn_ copy_entry(const fat_dentry_t *d, uint16_t *dst, size_t *offset)323 { 324 fat_lfn_ copy_part(FAT_LFN_PART3(d), FAT_LFN_PART3_SIZE, dst, offset);325 fat_lfn_ copy_part(FAT_LFN_PART2(d), FAT_LFN_PART2_SIZE, dst, offset);326 fat_lfn_ copy_part(FAT_LFN_PART1(d), FAT_LFN_PART1_SIZE, dst, offset);280 size_t fat_lfn_get_entry(const fat_dentry_t *d, wchar_t *dst, size_t *offset) 281 { 282 fat_lfn_get_part(FAT_LFN_PART3(d), FAT_LFN_PART3_SIZE, dst, offset); 283 fat_lfn_get_part(FAT_LFN_PART2(d), FAT_LFN_PART2_SIZE, dst, offset); 284 fat_lfn_get_part(FAT_LFN_PART1(d), FAT_LFN_PART1_SIZE, dst, offset); 327 285 328 286 return *offset; 329 287 } 330 288 331 /** Convert utf16 string to string. 332 * 333 * Convert wide string @a src to string. The output is written to the buffer 334 * specified by @a dest and @a size. @a size must be non-zero and the string 335 * written will always be well-formed. 336 * 337 * @param dest Destination buffer. 338 * @param size Size of the destination buffer. 339 * @param src Source wide string. 340 */ 341 int utf16_to_str(char *dest, size_t size, const uint16_t *src) 342 { 343 int rc; 344 uint16_t ch; 345 size_t src_idx, dest_off; 346 347 /* There must be space for a null terminator in the buffer. */ 348 assert(size > 0); 349 350 src_idx = 0; 351 dest_off = 0; 352 353 while ((ch = src[src_idx++]) != 0) { 354 rc = chr_encode(ch, dest, &dest_off, size - 1); 355 if (rc != EOK) 356 return rc; 357 } 358 359 dest[dest_off] = '\0'; 360 return EOK; 361 } 362 363 /** Convert string to utf16 string. 364 * 365 * Convert string @a src to wide string. The output is written to the 366 * buffer specified by @a dest and @a dlen. @a dlen must be non-zero 367 * and the wide string written will always be null-terminated. 368 * 369 * @param dest Destination buffer. 370 * @param dlen Length of destination buffer (number of wchars). 371 * @param src Source string. 372 */ 373 int str_to_utf16(uint16_t *dest, size_t dlen, const char *src) 374 { 375 size_t offset; 376 size_t di; 377 uint16_t c; 378 379 assert(dlen > 0); 380 381 offset = 0; 382 di = 0; 383 384 do { 385 if (di >= dlen - 1) 386 return EOVERFLOW; 387 388 c = str_decode(src, &offset, STR_NO_LIMIT); 389 dest[di++] = c; 390 } while (c != '\0'); 391 392 dest[dlen - 1] = '\0'; 393 return EOK; 394 } 395 396 bool fat_lfn_valid_char(uint16_t c) 289 size_t fat_lfn_set_part(const wchar_t *src, size_t *offset, size_t src_size, uint16_t *dst, size_t dst_size) 290 { 291 size_t idx; 292 for (idx=0; idx < dst_size; idx++) { 293 if (*offset < src_size) { 294 dst[idx] = uint16_t_le2host(src[*offset]); 295 (*offset)++; 296 } 297 else 298 dst[idx] = FAT_LFN_PAD; 299 } 300 return *offset; 301 } 302 303 size_t fat_lfn_set_entry(const wchar_t *src, size_t *offset, size_t size, fat_dentry_t *d) 304 { 305 fat_lfn_set_part(src, offset, size, FAT_LFN_PART1(d), FAT_LFN_PART1_SIZE); 306 fat_lfn_set_part(src, offset, size, FAT_LFN_PART2(d), FAT_LFN_PART2_SIZE); 307 fat_lfn_set_part(src, offset, size, FAT_LFN_PART3(d), FAT_LFN_PART3_SIZE); 308 if (src[*offset] == 0) 309 offset++; 310 FAT_LFN_ATTR(d) = FAT_ATTR_LFN; 311 d->lfn.type = 0; 312 d->lfn.firstc_lo = 0; 313 314 return *offset; 315 } 316 317 bool fat_sfn_valid_char(wchar_t c) 397 318 { 398 319 char valid[] = {"_.$%\'-@~!(){}^#&"}; 399 320 size_t idx=0; 400 321 401 if (c > 0xff) return false; 402 if (isdigit(c) || (isalpha(c) && isupper(c))) 322 if (!ascii_check(c)) 323 return false; 324 if (isdigit(c) || isupper(c)) 403 325 return true; 404 326 while(valid[idx]!=0) … … 409 331 } 410 332 411 bool fat_ lfn_valid_str(const uint16_t *str)412 { 413 uint16_t c;333 bool fat_sfn_valid(const wchar_t *wstr) 334 { 335 wchar_t c; 414 336 size_t idx=0; 415 if ( str[idx] == 0 ||str[idx] == '.')337 if (wstr[idx] == 0 || wstr[idx] == '.') 416 338 return false; 417 while ((c= str[idx++]) != 0) {418 if (!fat_ lfn_valid_char(c))339 while ((c=wstr[idx++]) != 0) { 340 if (!fat_sfn_valid_char(c)) 419 341 return false; 420 342 } … … 422 344 } 423 345 424 /** Get number of characters in a wide string. 425 * 426 * @param str NULL-terminated wide string. 427 * 428 * @return Number of characters in @a str. 429 * 430 */ 431 size_t utf16_length(const uint16_t *wstr) 432 { 433 size_t len = 0; 434 435 while (*wstr++ != 0) 436 len++; 437 return len; 438 } 439 440 bool fat_dentry_is_sfn(const uint16_t *str) 346 /* TODO: add more checks to long name */ 347 bool fat_lfn_valid(const wchar_t *wstr) 348 { 349 wchar_t c; 350 size_t idx=0; 351 if (wstr[idx] == 0 || wstr[idx] == '.') 352 return false; 353 while ((c=wstr[idx++]) != 0) { 354 if (ascii_check(c)) { 355 if (c == '?' || c == '*' || c == '\\' || c == '/') 356 return false; 357 } 358 } 359 return true; 360 } 361 362 void wstr_to_ascii(char *dst, const wchar_t *src, size_t count, uint8_t pad) 363 { 364 size_t i = 0; 365 while (i < count) { 366 if (src && *src) { 367 if (ascii_check(*src)) { 368 *dst = toupper(*src); 369 src++; 370 } 371 else 372 *dst = pad; 373 } 374 else 375 break; 376 377 dst++; 378 i++; 379 } 380 *dst = '\0'; 381 } 382 383 bool fat_dentry_is_sfn(const wchar_t *wstr) 441 384 { 442 385 /* 1. Length <= 11 characters */ 443 if ( utf16_length(str) > (FAT_NAME_LEN + FAT_EXT_LEN))386 if (wstr_length(wstr) > (FAT_NAME_LEN + FAT_EXT_LEN)) 444 387 return false; 445 388 /* … … 448 391 * 4. String should not contain invalid characters 449 392 */ 450 if (!fat_ lfn_valid_str(str))393 if (!fat_sfn_valid(wstr)) 451 394 return false; 452 395 -
uspace/srv/fs/fat/fat_dentry.h
r2e839dda r7194a60 56 56 #define FAT_LCASE_LOWER_EXT 0x10 57 57 58 #define FAT_PAD ' ' 58 #define FAT_PAD ' ' 59 #define FAT_LFN_PAD 0xffff 60 #define FAT_SFN_CHAR '_' 59 61 60 62 #define FAT_DENTRY_UNUSED 0x00 … … 76 78 #define FAT_LFN_CHKSUM(d) (d->lfn.check_sum) 77 79 78 #define FAT_LFN_NAME_SIZE 2 5580 #define FAT_LFN_NAME_SIZE 260 79 81 #define FAT_LFN_MAX_COUNT 20 80 82 #define FAT_LFN_PART1_SIZE 5 … … 130 132 131 133 extern int fat_dentry_namecmp(char *, const char *); 132 extern bool fat_dentry_name_verify(const char *);133 134 extern void fat_dentry_name_get(const fat_dentry_t *, char *); 134 135 extern void fat_dentry_name_set(fat_dentry_t *, const char *); … … 138 139 extern size_t fat_lfn_str_nlength(const uint16_t *, size_t); 139 140 extern size_t fat_lfn_size(const fat_dentry_t *); 140 extern size_t fat_lfn_copy_part(const uint16_t *, size_t, uint16_t *, size_t *); 141 extern size_t fat_lfn_copy_entry(const fat_dentry_t *, uint16_t *, size_t *); 141 extern size_t fat_lfn_get_part(const uint16_t *, size_t, wchar_t *, size_t *); 142 extern size_t fat_lfn_get_entry(const fat_dentry_t *, wchar_t *, size_t *); 143 extern size_t fat_lfn_set_part(const wchar_t *, size_t *, size_t, uint16_t *, size_t); 144 extern size_t fat_lfn_set_entry(const wchar_t *, size_t *, size_t, fat_dentry_t *); 142 145 143 extern int utf16_to_str(char *, size_t, const uint16_t *);144 extern int str_to_utf16(uint16_t *, size_t, const char *); 145 extern bool fat_ lfn_valid_char(uint16_t);146 extern bool fat_ lfn_valid_str(const uint16_t *);147 extern size_t utf16_length(const uint16_t *);148 extern bool fat_dentry_is_sfn(const uint16_t *);146 extern void wstr_to_ascii(char *dst, const wchar_t *src, size_t count, uint8_t pad); 147 148 extern bool fat_sfn_valid_char(wchar_t); 149 extern bool fat_sfn_valid(const wchar_t *); 150 extern bool fat_lfn_valid(const wchar_t *wstr); 151 extern bool fat_dentry_is_sfn(const wchar_t *); 149 152 150 153
Note:
See TracChangeset
for help on using the changeset viewer.