source: mainline/uspace/srv/fs/exfat/exfat_dentry.c

Last change on this file was 28a5ebd, checked in by Martin Decky <martin@…>, 5 years ago

Use char32_t instead of wchat_t to represent UTF-32 strings

The intention of the native HelenOS string API has been always to
support Unicode in the UTF-8 and UTF-32 encodings as the sole character
representations and ignore the obsolete mess of older single-byte and
multibyte character encodings. Before C11, the wchar_t type has been
slightly misused for the purpose of the UTF-32 strings. The newer
char32_t type is obviously a much more suitable option. The standard
defines char32_t as uint_least32_t, thus we can take the liberty to fix
it to uint32_t.

To maintain compatilibity with the C Standard, the putwchar(wchar_t)
functions has been replaced by our custom putuchar(char32_t) functions
where appropriate.

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[2fb88ea]1/*
2 * Copyright (c) 2011 Oleg Romanenko
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[b1834a01]29/** @addtogroup exfat
[2fb88ea]30 * @{
[1b20da0]31 */
[2fb88ea]32
33/**
34 * @file exfat_dentry.c
35 * @brief Functions that work with exFAT directory entries.
36 */
37
38#include "exfat_dentry.h"
39#include <ctype.h>
40#include <str.h>
41#include <errno.h>
42#include <byteorder.h>
43#include <assert.h>
44
45exfat_dentry_clsf_t exfat_classify_dentry(const exfat_dentry_t *d)
46{
47 switch (d->type) {
48 case EXFAT_TYPE_VOLLABEL:
49 return EXFAT_DENTRY_VOLLABEL;
50 case EXFAT_TYPE_BITMAP:
51 return EXFAT_DENTRY_BITMAP;
52 case EXFAT_TYPE_UCTABLE:
53 return EXFAT_DENTRY_UCTABLE;
54 case EXFAT_TYPE_GUID:
55 return EXFAT_DENTRY_GUID;
56 case EXFAT_TYPE_FILE:
57 return EXFAT_DENTRY_FILE;
58 case EXFAT_TYPE_STREAM:
59 return EXFAT_DENTRY_STREAM;
60 case EXFAT_TYPE_NAME:
61 return EXFAT_DENTRY_NAME;
62 case EXFAT_TYPE_UNUSED:
63 return EXFAT_DENTRY_LAST;
64 default:
65 if (d->type & EXFAT_TYPE_USED)
66 return EXFAT_DENTRY_SKIP;
[042a725]67 else
68 return EXFAT_DENTRY_FREE;
[2fb88ea]69 }
70}
71
[b4af128]72uint16_t exfat_name_hash(const uint16_t *name, const uint16_t *uctable, size_t chars)
[2fb88ea]73{
[b4af128]74 uint16_t hash = 0;
75 uint16_t ch;
76 while (*name) {
77 if (*name < chars)
78 ch = uint16_t_le2host(uctable[*name]);
79 else
80 ch = *name;
81 name++;
[2fb88ea]82
[b4af128]83 hash = ((hash << 15) | (hash >> 1)) + (ch & 0xff);
84 hash = ((hash << 15) | (hash >> 1)) + (ch >> 8);
85 }
86
87 return hash;
[2fb88ea]88}
89
[1f78546]90void exfat_dentry_get_name(const exfat_name_dentry_t *name, size_t size, uint16_t *dst, size_t *offset)
[2fb88ea]91{
[f3504c1]92 size_t i = 0;
[ff0c270]93 while (i < EXFAT_NAME_PART_LEN && *offset < size) {
[1f78546]94 dst[*offset] = uint16_t_le2host(name->name[i]);
95 i++;
96 (*offset)++;
97 }
98 dst[*offset] = '\0';
[2fb88ea]99}
100
[f3504c1]101void exfat_dentry_get_vollabel(const exfat_vollabel_dentry_t *vollabel,
102 size_t size, uint16_t *dst)
103{
104 size_t i = 0;
105 while (i < EXFAT_VOLLABEL_LEN && i < vollabel->size && i < size) {
106 dst[i] = uint16_t_le2host(vollabel->label[i]);
107 i++;
108 }
109 dst[i] = '\0';
110}
111
[28a5ebd]112bool exfat_valid_char(char32_t ch)
[2fb88ea]113{
[3e242d2]114 if (ch >= 0x01 && ch <= 0x1F)
115 return false;
116
[7a6690d8]117 switch (ch) {
118 case '/':
119 case '\\':
120 case '?':
121 case '|':
122 case '>':
123 case '<':
124 case '"':
125 case '*':
126 case ':':
127 return false;
128 default:
129 return true;
130 }
[2fb88ea]131}
132
133bool exfat_valid_name(const char *name)
134{
[7a6690d8]135 size_t off = 0;
[28a5ebd]136 char32_t ch;
[7a6690d8]137
138 while ((ch = str_decode(name, &off, STR_NO_LIMIT)) != 0) {
139 if (!exfat_valid_char(ch))
140 return false;
141 }
[04a153f]142 return true;
[2fb88ea]143}
144
145/**
146 * @}
[1b20da0]147 */
Note: See TracBrowser for help on using the repository browser.