Changeset 4372b49 in mainline


Ignore:
Timestamp:
2011-06-21T12:02:24Z (13 years ago)
Author:
Oleg Romanenko <romanenko.oleg@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9553d7d
Parents:
563686b
Message:

Fixes for api and better unicode support.

Location:
uspace/srv/fs/fat
Files:
4 edited

Legend:

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

    r563686b r4372b49  
    4141#include <errno.h>
    4242#include <byteorder.h>
     43#include <assert.h>
    4344
    4445static bool is_d_char(const char ch)
     
    276277 *
    277278 */
    278 size_t fat_lfn_str_nlength(const uint8_t *str, size_t size)
     279size_t fat_lfn_str_nlength(const uint16_t *str, size_t size)
    279280{
    280281        size_t offset = 0;
    281282
    282283        while (offset < size) {
    283                 if ((str[offset] == 0x00 && str[offset+1] == 0x00) ||
    284                         (str[offset] == 0xff && str[offset+1] == 0xff))
    285                         break;
    286                
    287                 offset += 2;
     284                if (str[offset] == 0 || str[offset] == 0xffff)
     285                        break;
     286                offset++;
    288287        }
    289288        return offset;
     
    308307}
    309308
    310 size_t fat_lfn_copy_part(const uint8_t *src, size_t src_size, uint8_t *dst, size_t offset)
    311 {
    312         int i;
    313         for (i=src_size-1; i>0 && offset>1; i-=2) {
    314                 if ((src[i] == 0x00 && src[i-1] == 0x00) ||
    315                         (src[i] == 0xff && src[i-1] == 0xff))
     309size_t fat_lfn_copy_part(const uint16_t *src, size_t src_size, uint16_t *dst, size_t *offset)
     310{
     311        while (src_size!=0 && (*offset)!=0) {
     312                src_size--;
     313                if (src[src_size] == 0 || src[src_size] == 0xffff)
    316314                        continue;
    317                 dst[offset-1] = src[i];
    318                 dst[offset-2] = src[i-1];
    319                 offset-=2;
    320         }
    321         return offset;
    322 }
    323 
    324 size_t fat_lfn_copy_entry(const fat_dentry_t *d, uint8_t *dst, size_t offset)
    325 {
    326         offset = fat_lfn_copy_part(FAT_LFN_PART3(d),
    327             FAT_LFN_PART3_SIZE, dst, offset);
    328         offset = fat_lfn_copy_part(FAT_LFN_PART2(d),
    329             FAT_LFN_PART2_SIZE, dst, offset);
    330         offset = fat_lfn_copy_part(FAT_LFN_PART1(d),
    331             FAT_LFN_PART1_SIZE, dst, offset);
    332 
    333         return offset;
    334 }
    335 
    336 int fat_lfn_convert_name(const uint8_t *src, size_t src_size, uint8_t *dst, size_t dst_size)
    337 {
    338         size_t i, offset = 0;
    339         uint16_t c;
     315
     316                (*offset)--;
     317                dst[(*offset)] = uint16_t_le2host(src[src_size]);
     318        }
     319        return (*offset);
     320}
     321
     322size_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);
     327
     328        return *offset;
     329}
     330
     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 */
     341int utf16_to_str(char *dest, size_t size, const uint16_t *src)
     342{
    340343        int rc;
    341         for (i=0; i<src_size; i+=2) {
    342                 if (src[i+1] == 0x00) {
    343                         if (offset+1 < dst_size)
    344                                 dst[offset++] = src[i];
    345                         else
    346                                 return EOVERFLOW;
    347                 } else {
    348                         c = uint16_t_le2host((src[i] << 8) | src[i+1]);
    349                         rc = chr_encode(c, (char*)dst, &offset, dst_size);
    350                         if (rc!=EOK) {
    351                                 return rc;
    352                         }
    353                 }
    354         }
    355         dst[offset] = 0;       
     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';
    356360        return EOK;
    357361}
  • uspace/srv/fs/fat/fat_dentry.h

    r563686b r4372b49  
    7878#define FAT_LFN_NAME_SIZE   255
    7979#define FAT_LFN_MAX_COUNT   20
    80 #define FAT_LFN_PART1_SIZE  10
    81 #define FAT_LFN_PART2_SIZE  12
    82 #define FAT_LFN_PART3_SIZE  4
     80#define FAT_LFN_PART1_SIZE  5
     81#define FAT_LFN_PART2_SIZE  6
     82#define FAT_LFN_PART3_SIZE  2
    8383#define FAT_LFN_ENTRY_SIZE \
    8484    (FAT_LFN_PART1_SIZE + FAT_LFN_PART2_SIZE + FAT_LFN_PART3_SIZE)
     
    117117                struct {
    118118                        uint8_t         order;
    119                         uint8_t         part1[FAT_LFN_PART1_SIZE];
     119                        uint16_t        part1[FAT_LFN_PART1_SIZE];
    120120                        uint8_t         attr;
    121121                        uint8_t         type;
    122122                        uint8_t         check_sum;
    123                         uint8_t         part2[FAT_LFN_PART2_SIZE];
     123                        uint16_t        part2[FAT_LFN_PART2_SIZE];
    124124                        uint16_t        firstc_lo; /* MUST be 0 */
    125                         uint8_t         part3[FAT_LFN_PART3_SIZE];
    126                 } lfn __attribute__ ((packed));
     125                        uint16_t        part3[FAT_LFN_PART3_SIZE];
     126                } __attribute__ ((packed)) lfn;
    127127        };
    128128} __attribute__ ((packed)) fat_dentry_t;
     
    136136extern uint8_t fat_dentry_chksum(uint8_t *);
    137137
    138 extern size_t fat_lfn_str_nlength(const uint8_t *, size_t);
     138extern size_t fat_lfn_str_nlength(const uint16_t *, size_t);
    139139extern size_t fat_lfn_size(const fat_dentry_t *);
    140 extern size_t fat_lfn_copy_part(const uint8_t *, size_t, uint8_t *, size_t);
    141 extern size_t fat_lfn_copy_entry(const fat_dentry_t *, uint8_t *, size_t);
    142 extern int fat_lfn_convert_name(const uint8_t *, size_t, uint8_t *, size_t);
     140extern size_t fat_lfn_copy_part(const uint16_t *, size_t, uint16_t *, size_t *);
     141extern size_t fat_lfn_copy_entry(const fat_dentry_t *, uint16_t *, size_t *);
     142extern int utf16_to_str(char *, size_t, const uint16_t *);
    143143
    144144
  • uspace/srv/fs/fat/fat_directory.c

    r563686b r4372b49  
    4141#include <byteorder.h>
    4242#include <mem.h>
     43#include <str.h>
    4344
    4445int fat_directory_block_load(fat_directory_t *);
     
    5859        di->last = false;
    5960
    60         di->lfn_utf16[0] = '\0';
     61        di->wname[0] = '\0';
    6162        di->lfn_offset = 0;
    6263        di->lfn_size = 0;
     
    174175                                                (di->checksum == FAT_LFN_CHKSUM(d))) {
    175176                                                /* Right order! */
    176                                                 di->lfn_offset = fat_lfn_copy_entry(d, di->lfn_utf16,
    177                                                         di->lfn_offset);
     177                                                fat_lfn_copy_entry(d, di->wname, &di->lfn_offset);
    178178                                        } else {
    179179                                                /* Something wrong with order. Skip this long entries set */
     
    190190                                                                (FAT_LFN_COUNT(d) - 1)) + fat_lfn_size(d);
    191191                                                        di->lfn_offset = di->lfn_size;
    192                                                         di->lfn_offset = fat_lfn_copy_entry(d, di->lfn_utf16,
    193                                                                 di->lfn_offset);
     192                                                        fat_lfn_copy_entry(d, di->wname, &di->lfn_offset);
    194193                                                        di->checksum = FAT_LFN_CHKSUM(d);
    195194                                                }
     
    200199                                if (di->long_entry &&
    201200                                        (di->checksum == fat_dentry_chksum(d->name))) {
    202                                         int rc;
    203                                         rc = fat_lfn_convert_name(di->lfn_utf16, di->lfn_size,
    204                                                 (uint8_t*)name, FAT_LFN_NAME_SIZE);
    205                                         if (rc!=EOK)
     201                                        di->wname[di->lfn_size] = '\0';
     202                                        if (utf16_to_str(name, FAT_LFN_NAME_SIZE, di->wname)!=EOK)
    206203                                                fat_dentry_name_get(d, name);
    207204                                }
  • uspace/srv/fs/fat/fat_directory.h

    r563686b r4372b49  
    4848        bool last;
    4949        /* Long entry data */
    50         uint8_t lfn_utf16[FAT_LFN_MAX_COUNT * FAT_LFN_ENTRY_SIZE];
     50        uint16_t wname[FAT_LFN_MAX_COUNT * FAT_LFN_ENTRY_SIZE];
    5151        size_t lfn_offset;
    5252        size_t lfn_size;
Note: See TracChangeset for help on using the changeset viewer.