source: mainline/uspace/srv/fs/fat/fat_dentry.c@ 944aa24

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 944aa24 was 411e9ca, checked in by Oleg Romanenko <romanenko.oleg@…>, 14 years ago
  1. Following the ECMA-107 rules for short file names. Function

fat_valid_name and fat_valid_short_name

  1. Rewrite fat_lfn_set_part, fat_lfn_set_entry, str_to_ascii functions

to use uint16_t for UTF16 instead of wchar_t, basing on new function
for conversion from utf8 to utf16.

  • Property mode set to 100644
File size: 8.8 KB
Line 
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
46/** Compare path component with the name read from the dentry.
47 *
48 * This function compares the path component with the name read from the dentry.
49 * The comparison is case insensitive and tolerates a mismatch on the trailing
50 * dot character at the end of the name (i.e. when there is a dot, but no
51 * extension).
52 *
53 * @param name Node name read from the dentry.
54 * @param component Path component.
55 *
56 * @return Zero on match, non-zero otherwise.
57 */
58int fat_dentry_namecmp(char *name, const char *component)
59{
60 int rc;
61 size_t size;
62
63 if (!(rc = stricmp(name, component)))
64 return rc;
65 if (!str_chr(name, '.')) {
66 /*
67 * There is no '.' in the name, so we know that there is enough
68 * space for appending an extra '.' to name.
69 */
70 size = str_size(name);
71 name[size] = '.';
72 name[size + 1] = '\0';
73 rc = stricmp(name, component);
74 }
75 return rc;
76}
77
78void fat_dentry_name_get(const fat_dentry_t *d, char *buf)
79{
80 unsigned int i;
81
82 for (i = 0; i < FAT_NAME_LEN; i++) {
83 if (d->name[i] == FAT_PAD)
84 break;
85
86 if (d->name[i] == FAT_DENTRY_E5_ESC)
87 *buf++ = 0xe5;
88 else {
89 if (d->lcase & FAT_LCASE_LOWER_NAME)
90 *buf++ = tolower(d->name[i]);
91 else
92 *buf++ = d->name[i];
93 }
94 }
95
96 if (d->ext[0] != FAT_PAD)
97 *buf++ = '.';
98
99 for (i = 0; i < FAT_EXT_LEN; i++) {
100 if (d->ext[i] == FAT_PAD) {
101 *buf = '\0';
102 return;
103 }
104
105 if (d->ext[i] == FAT_DENTRY_E5_ESC)
106 *buf++ = 0xe5;
107 else {
108 if (d->lcase & FAT_LCASE_LOWER_EXT)
109 *buf++ = tolower(d->ext[i]);
110 else
111 *buf++ = d->ext[i];
112 }
113 }
114
115 *buf = '\0';
116}
117
118void fat_dentry_name_set(fat_dentry_t *d, const char *name)
119{
120 unsigned int i;
121 const char fake_ext[] = " ";
122 bool lower_name = true;
123 bool lower_ext = true;
124
125 for (i = 0; i < FAT_NAME_LEN; i++) {
126 switch ((uint8_t) *name) {
127 case 0xe5:
128 d->name[i] = FAT_DENTRY_E5_ESC;
129 name++;
130 break;
131 case '\0':
132 case '.':
133 d->name[i] = FAT_PAD;
134 break;
135 default:
136 if (isalpha(*name)) {
137 if (!islower(*name))
138 lower_name = false;
139 }
140
141 d->name[i] = toupper(*name++);
142 break;
143 }
144 }
145
146 if (*name++ != '.')
147 name = fake_ext;
148
149 for (i = 0; i < FAT_EXT_LEN; i++) {
150 switch ((uint8_t) *name) {
151 case 0xe5:
152 d->ext[i] = FAT_DENTRY_E5_ESC;
153 name++;
154 break;
155 case '\0':
156 d->ext[i] = FAT_PAD;
157 break;
158 default:
159 if (isalpha(*name)) {
160 if (!islower(*name))
161 lower_ext = false;
162 }
163
164 d->ext[i] = toupper(*name++);
165 break;
166 }
167 }
168
169 if (lower_name)
170 d->lcase |= FAT_LCASE_LOWER_NAME;
171 else
172 d->lcase &= ~FAT_LCASE_LOWER_NAME;
173
174 if (lower_ext)
175 d->lcase |= FAT_LCASE_LOWER_EXT;
176 else
177 d->lcase &= ~FAT_LCASE_LOWER_EXT;
178}
179
180fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d)
181{
182 if (d->attr == FAT_ATTR_LFN) {
183 /* long name entry */
184 if (FAT_LFN_ORDER(d) & FAT_LFN_ERASED)
185 return FAT_DENTRY_FREE;
186 else
187 return FAT_DENTRY_LFN;
188 }
189 if (d->attr & FAT_ATTR_VOLLABEL) {
190 /* volume label entry */
191 return FAT_DENTRY_SKIP;
192 }
193 if (d->name[0] == FAT_DENTRY_ERASED) {
194 /* not-currently-used entry */
195 return FAT_DENTRY_FREE;
196 }
197 if (d->name[0] == FAT_DENTRY_UNUSED) {
198 /* never used entry */
199 return FAT_DENTRY_LAST;
200 }
201 if (d->name[0] == FAT_DENTRY_DOT) {
202 /*
203 * Most likely '.' or '..'.
204 * It cannot occur in a regular file name.
205 */
206 return FAT_DENTRY_SKIP;
207 }
208 return FAT_DENTRY_VALID;
209}
210
211/** Compute checksum of Node name.
212 *
213 * Returns an unsigned byte checksum computed on an unsigned byte
214 * array. The array must be 11 bytes long and is assumed to contain
215 * a name stored in the format of a MS-DOS directory entry.
216 *
217 * @param name Node name read from the dentry.
218 *
219 * @return An 8-bit unsigned checksum of the name.
220 */
221uint8_t fat_dentry_chksum(uint8_t *name)
222{
223 uint8_t i, sum=0;
224 for (i=0; i<(FAT_NAME_LEN+FAT_EXT_LEN); i++) {
225 sum = ((sum & 1) ? 0x80 : 0) + (sum >> 1) + name[i];
226 }
227 return sum;
228}
229
230/** Get number of bytes in a string with size limit.
231 *
232 * @param str NULL-terminated (or not) string.
233 * @param size Maximum number of bytes to consider.
234 *
235 * @return Number of bytes in string (without 0 and ff).
236 *
237 */
238size_t fat_lfn_str_nlength(const uint16_t *str, size_t size)
239{
240 size_t offset = 0;
241
242 while (offset < size) {
243 if (str[offset] == 0 || str[offset] == FAT_LFN_PAD)
244 break;
245 offset++;
246 }
247 return offset;
248}
249
250/** Get number of bytes in a FAT long entry occuped by characters.
251 *
252 * @param d FAT long entry.
253 *
254 * @return Number of bytes.
255 *
256 */
257size_t fat_lfn_size(const fat_dentry_t *d)
258{
259 size_t size = 0;
260
261 size += fat_lfn_str_nlength(FAT_LFN_PART1(d), FAT_LFN_PART1_SIZE);
262 size += fat_lfn_str_nlength(FAT_LFN_PART2(d), FAT_LFN_PART2_SIZE);
263 size += fat_lfn_str_nlength(FAT_LFN_PART3(d), FAT_LFN_PART3_SIZE);
264
265 return size;
266}
267
268size_t fat_lfn_get_part(const uint16_t *src, size_t src_size, uint16_t *dst, size_t *offset)
269{
270 while (src_size!=0 && (*offset)!=0) {
271 src_size--;
272 if (src[src_size] == 0 || src[src_size] == FAT_LFN_PAD)
273 continue;
274
275 (*offset)--;
276 dst[(*offset)] = uint16_t_le2host(src[src_size]);
277 }
278 return (*offset);
279}
280
281size_t fat_lfn_get_entry(const fat_dentry_t *d, uint16_t *dst, size_t *offset)
282{
283 fat_lfn_get_part(FAT_LFN_PART3(d), FAT_LFN_PART3_SIZE, dst, offset);
284 fat_lfn_get_part(FAT_LFN_PART2(d), FAT_LFN_PART2_SIZE, dst, offset);
285 fat_lfn_get_part(FAT_LFN_PART1(d), FAT_LFN_PART1_SIZE, dst, offset);
286
287 return *offset;
288}
289
290size_t fat_lfn_set_part(const uint16_t *src, size_t *offset, size_t src_size, uint16_t *dst, size_t dst_size)
291{
292 size_t idx;
293 for (idx=0; idx < dst_size; idx++) {
294 if (*offset < src_size) {
295 dst[idx] = uint16_t_le2host(src[*offset]);
296 (*offset)++;
297 }
298 else
299 dst[idx] = FAT_LFN_PAD;
300 }
301 return *offset;
302}
303
304size_t fat_lfn_set_entry(const uint16_t *src, size_t *offset, size_t size, fat_dentry_t *d)
305{
306 fat_lfn_set_part(src, offset, size, FAT_LFN_PART1(d), FAT_LFN_PART1_SIZE);
307 fat_lfn_set_part(src, offset, size, FAT_LFN_PART2(d), FAT_LFN_PART2_SIZE);
308 fat_lfn_set_part(src, offset, size, FAT_LFN_PART3(d), FAT_LFN_PART3_SIZE);
309 if (src[*offset] == 0)
310 offset++;
311 FAT_LFN_ATTR(d) = FAT_ATTR_LFN;
312 d->lfn.type = 0;
313 d->lfn.firstc_lo = 0;
314
315 return *offset;
316}
317
318void str_to_ascii(char *dst, const char *src, size_t count, uint8_t pad)
319{
320 wchar_t ch;
321 size_t off = 0;
322 size_t i = 0;
323
324 while (i < count) {
325 if ((ch = str_decode(src, &off, STR_NO_LIMIT)) != 0) {
326 if (ascii_check(ch) & IS_D_CHAR(ch))
327 *dst = toupper(ch);
328 else
329 *dst = pad;
330 }
331 else
332 break;
333
334 dst++;
335 i++;
336 }
337 *dst = '\0';
338}
339
340bool fat_valid_name(const char *name)
341{
342 wchar_t ch;
343 size_t offset=0;
344 bool result = true;
345
346 while ((ch = str_decode(name, &offset, STR_NO_LIMIT)) != 0) {
347 if (wstr_chr(FAT_STOP_CHARS, ch) != NULL) {
348 result = false;
349 break;
350 }
351 }
352 return result;
353}
354
355bool fat_valid_short_name(const char *name)
356{
357 unsigned int i;
358 unsigned int dot = 0;
359 bool dot_found = false;
360
361 for (i = 0; name[i]; i++) {
362 if (name[i] == '.') {
363 if (dot_found) {
364 return false;
365 } else {
366 dot_found = true;
367 dot = i;
368 }
369 } else {
370 if (!IS_D_CHAR(name[i]))
371 return false;
372 }
373 }
374
375 if (dot_found) {
376 if (dot > FAT_NAME_LEN)
377 return false;
378 if (i - dot > FAT_EXT_LEN + 1)
379 return false;
380 } else {
381 if (i > FAT_NAME_LEN)
382 return false;
383 }
384
385 return true;
386}
387
388size_t utf16_length(const uint16_t *wstr)
389{
390 size_t len = 0;
391
392 while (*wstr++ != 0)
393 len++;
394
395 return len;
396}
397
398/**
399 * @}
400 */
Note: See TracBrowser for help on using the repository browser.