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 |
|
---|
29 | /** @addtogroup exfat
|
---|
30 | * @{
|
---|
31 | */
|
---|
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 |
|
---|
45 | exfat_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;
|
---|
67 | else
|
---|
68 | return EXFAT_DENTRY_FREE;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | uint16_t exfat_name_hash(const uint16_t *name, const uint16_t *uctable, size_t chars)
|
---|
73 | {
|
---|
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++;
|
---|
82 |
|
---|
83 | hash = ((hash << 15) | (hash >> 1)) + (ch & 0xff);
|
---|
84 | hash = ((hash << 15) | (hash >> 1)) + (ch >> 8);
|
---|
85 | }
|
---|
86 |
|
---|
87 | return hash;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void exfat_dentry_get_name(const exfat_name_dentry_t *name, size_t size, uint16_t *dst, size_t *offset)
|
---|
91 | {
|
---|
92 | size_t i = 0;
|
---|
93 | while (i < EXFAT_NAME_PART_LEN && *offset < size) {
|
---|
94 | dst[*offset] = uint16_t_le2host(name->name[i]);
|
---|
95 | i++;
|
---|
96 | (*offset)++;
|
---|
97 | }
|
---|
98 | dst[*offset] = '\0';
|
---|
99 | }
|
---|
100 |
|
---|
101 | void 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 |
|
---|
112 | bool exfat_valid_char(char32_t ch)
|
---|
113 | {
|
---|
114 | if (ch >= 0x01 && ch <= 0x1F)
|
---|
115 | return false;
|
---|
116 |
|
---|
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 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | bool exfat_valid_name(const char *name)
|
---|
134 | {
|
---|
135 | size_t off = 0;
|
---|
136 | char32_t ch;
|
---|
137 |
|
---|
138 | while ((ch = str_decode(name, &off, STR_NO_LIMIT)) != 0) {
|
---|
139 | if (!exfat_valid_char(ch))
|
---|
140 | return false;
|
---|
141 | }
|
---|
142 | return true;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * @}
|
---|
147 | */
|
---|