source: mainline/uspace/srv/fs/fat/fat_dentry.c@ 5d95f02

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5d95f02 was 5d95f02, checked in by Jakub Jermar <jakub@…>, 14 years ago

Cleanup.

  • Property mode set to 100644
File size: 9.0 KB
RevLine 
[6ebaff9]1/*
2 * Copyright (c) 2008 Jakub Jermar
[c4bbca8]3 * Copyright (c) 2011 Oleg Romanenko
[6ebaff9]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
[033ef7d3]39#include "fat_dentry.h"
[0fdd6bb]40#include <ctype.h>
[19f857a]41#include <str.h>
[34fdb75]42#include <errno.h>
[e65e406]43#include <byteorder.h>
[4372b49]44#include <assert.h>
[033ef7d3]45
[14c331a]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;
[92fd52d7]61 size_t size;
62
[14c331a]63 if (!(rc = stricmp(name, component)))
64 return rc;
[7afb4a5]65 if (!str_chr(name, '.')) {
[14c331a]66 /*
67 * There is no '.' in the name, so we know that there is enough
68 * space for appending an extra '.' to name.
69 */
[92fd52d7]70 size = str_size(name);
71 name[size] = '.';
72 name[size + 1] = '\0';
[14c331a]73 rc = stricmp(name, component);
74 }
75 return rc;
76}
77
[0fdd6bb]78void fat_dentry_name_get(const fat_dentry_t *d, char *buf)
[033ef7d3]79{
[a248234]80 unsigned int i;
81
[033ef7d3]82 for (i = 0; i < FAT_NAME_LEN; i++) {
83 if (d->name[i] == FAT_PAD)
84 break;
[a248234]85
[033ef7d3]86 if (d->name[i] == FAT_DENTRY_E5_ESC)
87 *buf++ = 0xe5;
[a248234]88 else {
89 if (d->lcase & FAT_LCASE_LOWER_NAME)
90 *buf++ = tolower(d->name[i]);
91 else
92 *buf++ = d->name[i];
93 }
[033ef7d3]94 }
[a248234]95
[033ef7d3]96 if (d->ext[0] != FAT_PAD)
97 *buf++ = '.';
[a248234]98
[033ef7d3]99 for (i = 0; i < FAT_EXT_LEN; i++) {
100 if (d->ext[i] == FAT_PAD) {
101 *buf = '\0';
102 return;
103 }
[a248234]104
[033ef7d3]105 if (d->ext[i] == FAT_DENTRY_E5_ESC)
106 *buf++ = 0xe5;
[a248234]107 else {
108 if (d->lcase & FAT_LCASE_LOWER_EXT)
109 *buf++ = tolower(d->ext[i]);
110 else
111 *buf++ = d->ext[i];
112 }
[033ef7d3]113 }
[a248234]114
[033ef7d3]115 *buf = '\0';
116}
117
[0fdd6bb]118void fat_dentry_name_set(fat_dentry_t *d, const char *name)
119{
[a248234]120 unsigned int i;
[0fdd6bb]121 const char fake_ext[] = " ";
[a248234]122 bool lower_name = true;
123 bool lower_ext = true;
124
[0fdd6bb]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:
[a248234]136 if (isalpha(*name)) {
137 if (!islower(*name))
138 lower_name = false;
139 }
140
[0fdd6bb]141 d->name[i] = toupper(*name++);
142 break;
143 }
144 }
[a248234]145
[0fdd6bb]146 if (*name++ != '.')
147 name = fake_ext;
[a248234]148
[0fdd6bb]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:
[a248234]159 if (isalpha(*name)) {
160 if (!islower(*name))
161 lower_ext = false;
162 }
163
[0fdd6bb]164 d->ext[i] = toupper(*name++);
165 break;
166 }
167 }
[a248234]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;
[0fdd6bb]178}
179
180fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d)
[033ef7d3]181{
[65ccd23]182 if (d->attr == FAT_ATTR_LFN) {
[34fdb75]183 /* long name entry */
[c6aca755]184 if (FAT_LFN_ORDER(d) & FAT_LFN_ERASED)
[34fdb75]185 return FAT_DENTRY_FREE;
186 else
187 return FAT_DENTRY_LFN;
[65ccd23]188 }
[033ef7d3]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 */
[0fdd6bb]195 return FAT_DENTRY_FREE;
[033ef7d3]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}
[6ebaff9]210
[ed19497]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{
[5d95f02]223 uint8_t i, sum = 0;
224
225 for (i = 0; i < (FAT_NAME_LEN + FAT_EXT_LEN); i++)
[ed19497]226 sum = ((sum & 1) ? 0x80 : 0) + (sum >> 1) + name[i];
[5d95f02]227
[ed19497]228 return sum;
229}
230
[34fdb75]231/** Get number of bytes in a string with size limit.
232 *
233 * @param str NULL-terminated (or not) string.
234 * @param size Maximum number of bytes to consider.
235 *
236 * @return Number of bytes in string (without 0 and ff).
237 *
238 */
[4372b49]239size_t fat_lfn_str_nlength(const uint16_t *str, size_t size)
[34fdb75]240{
241 size_t offset = 0;
242
243 while (offset < size) {
[7194a60]244 if (str[offset] == 0 || str[offset] == FAT_LFN_PAD)
[34fdb75]245 break;
[4372b49]246 offset++;
[34fdb75]247 }
248 return offset;
249}
250
251/** Get number of bytes in a FAT long entry occuped by characters.
252 *
253 * @param d FAT long entry.
254 *
255 * @return Number of bytes.
256 *
257 */
258size_t fat_lfn_size(const fat_dentry_t *d)
259{
260 size_t size = 0;
261
262 size += fat_lfn_str_nlength(FAT_LFN_PART1(d), FAT_LFN_PART1_SIZE);
263 size += fat_lfn_str_nlength(FAT_LFN_PART2(d), FAT_LFN_PART2_SIZE);
264 size += fat_lfn_str_nlength(FAT_LFN_PART3(d), FAT_LFN_PART3_SIZE);
265
266 return size;
267}
268
[7234617]269size_t fat_lfn_get_entry(const fat_dentry_t *d, uint16_t *dst, size_t *offset)
[34fdb75]270{
[7234617]271 int i;
[5d95f02]272 for (i = FAT_LFN_PART3_SIZE - 1; i >= 0 && *offset > 0; i--) {
[7234617]273 if (d->lfn.part3[i] == 0 || d->lfn.part3[i] == FAT_LFN_PAD)
[34fdb75]274 continue;
[4372b49]275 (*offset)--;
[7234617]276 dst[(*offset)] = uint16_t_le2host(d->lfn.part3[i]);
277 }
[5d95f02]278 for (i = FAT_LFN_PART2_SIZE - 1; i >= 0 && *offset > 0; i--) {
[7234617]279 if (d->lfn.part2[i] == 0 || d->lfn.part2[i] == FAT_LFN_PAD)
280 continue;
281 (*offset)--;
282 dst[(*offset)] = uint16_t_le2host(d->lfn.part2[i]);
283 }
[5d95f02]284 for (i = FAT_LFN_PART1_SIZE - 1; i >= 0 && *offset > 0; i--) {
[7234617]285 if (d->lfn.part1[i] == 0 || d->lfn.part1[i] == FAT_LFN_PAD)
286 continue;
287 (*offset)--;
288 dst[(*offset)] = uint16_t_le2host(d->lfn.part1[i]);
[34fdb75]289 }
[4372b49]290 return *offset;
[34fdb75]291}
292
[5d95f02]293size_t fat_lfn_set_entry(const uint16_t *src, size_t *offset, size_t size,
294 fat_dentry_t *d)
[34fdb75]295{
[7194a60]296 size_t idx;
[5d95f02]297 for (idx = 0; idx < FAT_LFN_PART1_SIZE; idx++) {
[7234617]298 if (*offset < size) {
299 d->lfn.part1[idx] = host2uint16_t_le(src[*offset]);
[7194a60]300 (*offset)++;
[5d95f02]301 } else
[7234617]302 d->lfn.part1[idx] = FAT_LFN_PAD;
303 }
[5d95f02]304 for (idx = 0; idx < FAT_LFN_PART2_SIZE; idx++) {
[7234617]305 if (*offset < size) {
306 d->lfn.part2[idx] = host2uint16_t_le(src[*offset]);
307 (*offset)++;
[5d95f02]308 } else
[7234617]309 d->lfn.part2[idx] = FAT_LFN_PAD;
310 }
[5d95f02]311 for (idx = 0; idx < FAT_LFN_PART3_SIZE; idx++) {
[7234617]312 if (*offset < size) {
313 d->lfn.part3[idx] = host2uint16_t_le(src[*offset]);
314 (*offset)++;
[5d95f02]315 } else
[7234617]316 d->lfn.part3[idx] = FAT_LFN_PAD;
[34fdb75]317 }
318
[7194a60]319 if (src[*offset] == 0)
320 offset++;
321 FAT_LFN_ATTR(d) = FAT_ATTR_LFN;
322 d->lfn.type = 0;
323 d->lfn.firstc_lo = 0;
324
325 return *offset;
[9553d7d]326}
327
[411e9ca]328void str_to_ascii(char *dst, const char *src, size_t count, uint8_t pad)
[7194a60]329{
[411e9ca]330 wchar_t ch;
331 size_t off = 0;
[7194a60]332 size_t i = 0;
[411e9ca]333
[7194a60]334 while (i < count) {
[411e9ca]335 if ((ch = str_decode(src, &off, STR_NO_LIMIT)) != 0) {
336 if (ascii_check(ch) & IS_D_CHAR(ch))
337 *dst = toupper(ch);
[7194a60]338 else
339 *dst = pad;
[5d95f02]340 } else
[7194a60]341 break;
342
343 dst++;
344 i++;
345 }
346 *dst = '\0';
[2d0d637]347}
348
[411e9ca]349bool fat_valid_name(const char *name)
[2d0d637]350{
[411e9ca]351 wchar_t ch;
352 size_t offset=0;
353 bool result = true;
[2d0d637]354
[411e9ca]355 while ((ch = str_decode(name, &offset, STR_NO_LIMIT)) != 0) {
[980311e]356 if (str_chr(FAT_STOP_CHARS, ch) != NULL) {
[411e9ca]357 result = false;
358 break;
359 }
360 }
361 return result;
362}
363
364bool fat_valid_short_name(const char *name)
365{
366 unsigned int i;
367 unsigned int dot = 0;
368 bool dot_found = false;
369
370 for (i = 0; name[i]; i++) {
371 if (name[i] == '.') {
372 if (dot_found) {
373 return false;
374 } else {
375 dot_found = true;
376 dot = i;
377 }
378 } else {
379 if (!IS_D_CHAR(name[i]))
380 return false;
381 }
382 }
383
384 if (dot_found) {
385 if (dot > FAT_NAME_LEN)
386 return false;
387 if (i - dot > FAT_EXT_LEN + 1)
388 return false;
389 } else {
390 if (i > FAT_NAME_LEN)
391 return false;
392 }
393
[2d0d637]394 return true;
395}
396
[411e9ca]397size_t utf16_length(const uint16_t *wstr)
398{
399 size_t len = 0;
400
401 while (*wstr++ != 0)
402 len++;
403
404 return len;
405}
[ed19497]406
[6ebaff9]407/**
408 * @}
409 */
Note: See TracBrowser for help on using the repository browser.