Rev 3505 | Rev 3634 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3505 | Rev 3628 | ||
---|---|---|---|
Line 34... | Line 34... | ||
34 | * @file fat_dentry.c |
34 | * @file fat_dentry.c |
35 | * @brief Functions that work with FAT directory entries. |
35 | * @brief Functions that work with FAT directory entries. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "fat_dentry.h" |
38 | #include "fat_dentry.h" |
- | 39 | #include <ctype.h> |
|
39 | 40 | ||
40 | #define FAT_PAD ' ' |
41 | #define FAT_PAD ' ' |
41 | 42 | ||
42 | #define FAT_DENTRY_UNUSED 0x00 |
43 | #define FAT_DENTRY_UNUSED 0x00 |
43 | #define FAT_DENTRY_E5_ESC 0x05 |
44 | #define FAT_DENTRY_E5_ESC 0x05 |
44 | #define FAT_DENTRY_DOT 0x2e |
45 | #define FAT_DENTRY_DOT 0x2e |
45 | #define FAT_DENTRY_ERASED 0xe5 |
46 | #define FAT_DENTRY_ERASED 0xe5 |
46 | 47 | ||
- | 48 | static bool is_d_char(const char ch) |
|
- | 49 | { |
|
- | 50 | if (isalnum(ch) || ch == '_') |
|
- | 51 | return true; |
|
- | 52 | else |
|
- | 53 | return false; |
|
- | 54 | } |
|
- | 55 | ||
- | 56 | bool fat_dentry_name_verify(const char *name) |
|
- | 57 | { |
|
- | 58 | unsigned i, dot; |
|
- | 59 | bool dot_found = false; |
|
- | 60 | ||
- | 61 | ||
- | 62 | for (i = 0; name[i]; i++) { |
|
- | 63 | if (name[i] == '.') { |
|
- | 64 | if (dot_found) { |
|
- | 65 | return false; |
|
- | 66 | } else { |
|
- | 67 | dot_found = true; |
|
- | 68 | dot = i; |
|
- | 69 | } |
|
- | 70 | } else { |
|
- | 71 | if (!is_d_char(name[i])) |
|
- | 72 | return false; |
|
- | 73 | } |
|
- | 74 | } |
|
- | 75 | ||
- | 76 | if (dot_found) { |
|
- | 77 | if (dot > FAT_NAME_LEN) |
|
- | 78 | return false; |
|
- | 79 | if (i - dot > FAT_EXT_LEN + 1) |
|
- | 80 | return false; |
|
- | 81 | } else { |
|
- | 82 | if (i > FAT_NAME_LEN) |
|
- | 83 | return false; |
|
- | 84 | } |
|
- | 85 | ||
- | 86 | return true; |
|
- | 87 | } |
|
- | 88 | ||
47 | void dentry_name_canonify(fat_dentry_t *d, char *buf) |
89 | void fat_dentry_name_get(const fat_dentry_t *d, char *buf) |
48 | { |
90 | { |
49 | int i; |
91 | int i; |
50 | 92 | ||
51 | for (i = 0; i < FAT_NAME_LEN; i++) { |
93 | for (i = 0; i < FAT_NAME_LEN; i++) { |
52 | if (d->name[i] == FAT_PAD) |
94 | if (d->name[i] == FAT_PAD) |
Line 69... | Line 111... | ||
69 | *buf++ = d->ext[i]; |
111 | *buf++ = d->ext[i]; |
70 | } |
112 | } |
71 | *buf = '\0'; |
113 | *buf = '\0'; |
72 | } |
114 | } |
73 | 115 | ||
- | 116 | void fat_dentry_name_set(fat_dentry_t *d, const char *name) |
|
- | 117 | { |
|
- | 118 | int i; |
|
- | 119 | const char fake_ext[] = " "; |
|
- | 120 | ||
- | 121 | ||
- | 122 | for (i = 0; i < FAT_NAME_LEN; i++) { |
|
- | 123 | switch ((uint8_t) *name) { |
|
- | 124 | case 0xe5: |
|
- | 125 | d->name[i] = FAT_DENTRY_E5_ESC; |
|
- | 126 | name++; |
|
- | 127 | break; |
|
- | 128 | case '\0': |
|
- | 129 | case '.': |
|
- | 130 | d->name[i] = FAT_PAD; |
|
- | 131 | break; |
|
- | 132 | default: |
|
- | 133 | d->name[i] = toupper(*name++); |
|
- | 134 | break; |
|
- | 135 | } |
|
- | 136 | } |
|
- | 137 | if (*name++ != '.') |
|
- | 138 | name = fake_ext; |
|
- | 139 | for (i = 0; i < FAT_EXT_LEN; i++) { |
|
- | 140 | switch ((uint8_t) *name) { |
|
- | 141 | case 0xe5: |
|
- | 142 | d->ext[i] = FAT_DENTRY_E5_ESC; |
|
- | 143 | name++; |
|
- | 144 | break; |
|
- | 145 | case '\0': |
|
- | 146 | d->ext[i] = FAT_PAD; |
|
- | 147 | break; |
|
- | 148 | default: |
|
- | 149 | d->ext[i] = toupper(*name++); |
|
- | 150 | break; |
|
- | 151 | } |
|
- | 152 | } |
|
- | 153 | } |
|
- | 154 | ||
74 | fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d) |
155 | fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d) |
75 | { |
156 | { |
76 | if (d->attr & FAT_ATTR_VOLLABEL) { |
157 | if (d->attr & FAT_ATTR_VOLLABEL) { |
77 | /* volume label entry */ |
158 | /* volume label entry */ |
78 | return FAT_DENTRY_SKIP; |
159 | return FAT_DENTRY_SKIP; |
79 | } |
160 | } |
80 | if (d->name[0] == FAT_DENTRY_ERASED) { |
161 | if (d->name[0] == FAT_DENTRY_ERASED) { |
81 | /* not-currently-used entry */ |
162 | /* not-currently-used entry */ |
82 | return FAT_DENTRY_SKIP; |
163 | return FAT_DENTRY_FREE; |
83 | } |
164 | } |
84 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
165 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
85 | /* never used entry */ |
166 | /* never used entry */ |
86 | return FAT_DENTRY_LAST; |
167 | return FAT_DENTRY_LAST; |
87 | } |
168 | } |