Changeset 7194a60 in mainline


Ignore:
Timestamp:
2011-06-27T16:40:55Z (13 years ago)
Author:
Oleg Romanenko <romanenko.oleg@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5dfb1948
Parents:
2e839dda
Message:

Modifications in fat_dentry.h and fat_dentry.c:

  1. Using wchar_t for wide string
  2. Using standart string functions when it possible.
  3. Add function for convesion unicode character to ascii:

wstr_to_ascii

  1. Renaming few functions.
Location:
uspace/srv/fs/fat
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/fs/fat/fat_dentry.c

    r2e839dda r7194a60  
    4242#include <byteorder.h>
    4343#include <assert.h>
    44 
    45 static bool is_d_char(const char ch)
    46 {
    47         if (isalnum(ch) || ch == '_')
    48                 return true;
    49         else
    50                 return false;
    51 }
    5244
    5345/** Compare path component with the name read from the dentry.
     
    8375}
    8476
    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 
    11977void fat_dentry_name_get(const fat_dentry_t *d, char *buf)
    12078{
     
    282240
    283241        while (offset < size) {
    284                 if (str[offset] == 0 || str[offset] == 0xffff)
     242                if (str[offset] == 0 || str[offset] == FAT_LFN_PAD)
    285243                        break;
    286244                offset++;
     
    307265}
    308266
    309 size_t fat_lfn_copy_part(const uint16_t *src, size_t src_size, uint16_t *dst, size_t *offset)
     267size_t fat_lfn_get_part(const uint16_t *src, size_t src_size, wchar_t *dst, size_t *offset)
    310268{
    311269        while (src_size!=0 && (*offset)!=0) {
    312270                src_size--;
    313                 if (src[src_size] == 0 || src[src_size] == 0xffff)
     271                if (src[src_size] == 0 || src[src_size] == FAT_LFN_PAD)
    314272                        continue;
    315273
     
    320278}
    321279
    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);
     280size_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);
    327285
    328286        return *offset;
    329287}
    330288
    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)
     289size_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
     303size_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
     317bool fat_sfn_valid_char(wchar_t c)
    397318{
    398319        char valid[] = {"_.$%\'-@~!(){}^#&"};
    399320        size_t idx=0;
    400321
    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))
    403325                return true;   
    404326        while(valid[idx]!=0)
     
    409331}
    410332
    411 bool fat_lfn_valid_str(const uint16_t *str)
    412 {
    413         uint16_t c;
     333bool fat_sfn_valid(const wchar_t *wstr)
     334{
     335        wchar_t c;
    414336        size_t idx=0;
    415         if (str[idx] == 0 || str[idx] == '.')
     337        if (wstr[idx] == 0 || wstr[idx] == '.')
    416338                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))
    419341                        return false;
    420342        }
     
    422344}
    423345
    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 */
     347bool 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
     362void 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
     383bool fat_dentry_is_sfn(const wchar_t *wstr)
    441384{
    442385        /* 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))
    444387                return false;
    445388        /*
     
    448391         * 4. String should not contain invalid characters
    449392         */
    450         if (!fat_lfn_valid_str(str))
     393        if (!fat_sfn_valid(wstr))
    451394                return false;
    452395       
  • uspace/srv/fs/fat/fat_dentry.h

    r2e839dda r7194a60  
    5656#define FAT_LCASE_LOWER_EXT     0x10
    5757
    58 #define FAT_PAD                 ' '
     58#define FAT_PAD                 ' '
     59#define FAT_LFN_PAD     0xffff
     60#define FAT_SFN_CHAR '_'
    5961
    6062#define FAT_DENTRY_UNUSED       0x00
     
    7678#define FAT_LFN_CHKSUM(d) (d->lfn.check_sum)
    7779
    78 #define FAT_LFN_NAME_SIZE   255
     80#define FAT_LFN_NAME_SIZE   260
    7981#define FAT_LFN_MAX_COUNT   20
    8082#define FAT_LFN_PART1_SIZE  5
     
    130132
    131133extern int fat_dentry_namecmp(char *, const char *);
    132 extern bool fat_dentry_name_verify(const char *);
    133134extern void fat_dentry_name_get(const fat_dentry_t *, char *);
    134135extern void fat_dentry_name_set(fat_dentry_t *, const char *);
     
    138139extern size_t fat_lfn_str_nlength(const uint16_t *, size_t);
    139140extern 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 *);
     141extern size_t fat_lfn_get_part(const uint16_t *, size_t, wchar_t *, size_t *);
     142extern size_t fat_lfn_get_entry(const fat_dentry_t *, wchar_t *, size_t *);
     143extern size_t fat_lfn_set_part(const wchar_t *, size_t *, size_t, uint16_t *, size_t);
     144extern size_t fat_lfn_set_entry(const wchar_t *, size_t *, size_t, fat_dentry_t *);
    142145
    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 *);
     146extern void wstr_to_ascii(char *dst, const wchar_t *src, size_t count, uint8_t pad);
     147
     148extern bool fat_sfn_valid_char(wchar_t);
     149extern bool fat_sfn_valid(const wchar_t *);
     150extern bool fat_lfn_valid(const wchar_t *wstr);
     151extern bool fat_dentry_is_sfn(const wchar_t *);
    149152
    150153
Note: See TracChangeset for help on using the changeset viewer.