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

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

Explicitly treat the str argument to fat_lfn_str_nlength() as unaligned.

fat_lfn_str_nlength() was defined to take uint16_t * and as such had
reasons to assume natural alignment of the pointed object. This was
however not the case when the function was called from fat_lfn_size()
because the argument there is an address from inside a packed structure.

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