1
2
3
4
5
6
7
8#include <common.h>
9#include <charset.h>
10#include <cp1250.h>
11#include <cp437.h>
12#include <efi_loader.h>
13
14
15static const char illegal[] = "+,<=>:;\"/\\|?*[]\x7f";
16
17
18
19
20
21#if CONFIG_FAT_DEFAULT_CODEPAGE == 1250
22
23static const u16 codepage[] = CP1250;
24#else
25
26static const u16 *codepage = codepage_437;
27#endif
28
29
30const efi_guid_t efi_guid_unicode_collation_protocol2 =
31 EFI_UNICODE_COLLATION_PROTOCOL2_GUID;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48static efi_intn_t EFIAPI efi_stri_coll(
49 struct efi_unicode_collation_protocol *this, u16 *s1, u16 *s2)
50{
51 s32 c1, c2;
52 efi_intn_t ret = 0;
53
54 EFI_ENTRY("%p, %ls, %ls", this, s1, s2);
55 for (; *s1 | *s2; ++s1, ++s2) {
56 c1 = utf_to_upper(*s1);
57 c2 = utf_to_upper(*s2);
58 if (c1 < c2) {
59 ret = -1;
60 goto out;
61 } else if (c1 > c2) {
62 ret = 1;
63 goto out;
64 }
65 }
66out:
67 EFI_EXIT(EFI_SUCCESS);
68 return ret;
69}
70
71
72
73
74
75
76
77static s32 next_lower(const u16 **string)
78{
79 return utf_to_lower(utf16_get(string));
80}
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102static bool metai_match(const u16 *string, const u16 *pattern)
103{
104 s32 first, s, p;
105
106 for (; *string && *pattern;) {
107 const u16 *string_old = string;
108
109 s = next_lower(&string);
110 p = next_lower(&pattern);
111
112 switch (p) {
113 case '*':
114
115 for (;; s = next_lower(&string)) {
116 if (metai_match(string_old, pattern))
117 return true;
118 if (!s)
119 return false;
120 string_old = string;
121 }
122 case '?':
123
124 break;
125 case '[':
126
127 p = next_lower(&pattern);
128 first = p;
129 if (first == ']')
130
131 return false;
132 p = next_lower(&pattern);
133 if (p == '-') {
134
135 p = next_lower(&pattern);
136 if (s < first || s > p)
137 return false;
138 p = next_lower(&pattern);
139 if (p != ']')
140 return false;
141 } else {
142
143 bool hit = false;
144
145 if (s == first)
146 hit = true;
147 for (; p && p != ']';
148 p = next_lower(&pattern)) {
149 if (p == s)
150 hit = true;
151 }
152 if (!hit || p != ']')
153 return false;
154 }
155 break;
156 default:
157
158 if (p != s)
159 return false;
160 }
161 }
162 if (!*pattern && !*string)
163 return true;
164 return false;
165}
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186static bool EFIAPI efi_metai_match(struct efi_unicode_collation_protocol *this,
187 const u16 *string, const u16 *pattern)
188{
189 bool ret;
190
191 EFI_ENTRY("%p, %ls, %ls", this, string, pattern);
192 ret = metai_match(string, pattern);
193 EFI_EXIT(EFI_SUCCESS);
194 return ret;
195}
196
197
198
199
200
201
202
203
204
205
206
207
208
209static void EFIAPI efi_str_lwr(struct efi_unicode_collation_protocol *this,
210 u16 *string)
211{
212 EFI_ENTRY("%p, %ls", this, string);
213 for (; *string; ++string)
214 *string = utf_to_lower(*string);
215 EFI_EXIT(EFI_SUCCESS);
216}
217
218
219
220
221
222
223
224
225
226
227
228
229
230static void EFIAPI efi_str_upr(struct efi_unicode_collation_protocol *this,
231 u16 *string)
232{
233 EFI_ENTRY("%p, %ls", this, string);
234 for (; *string; ++string)
235 *string = utf_to_upper(*string);
236 EFI_EXIT(EFI_SUCCESS);
237}
238
239
240
241
242
243
244
245
246
247
248
249
250static void EFIAPI efi_fat_to_str(struct efi_unicode_collation_protocol *this,
251 efi_uintn_t fat_size, char *fat, u16 *string)
252{
253 efi_uintn_t i;
254 u16 c;
255
256 EFI_ENTRY("%p, %zu, %s, %p", this, fat_size, fat, string);
257 for (i = 0; i < fat_size; ++i) {
258 c = (unsigned char)fat[i];
259 if (c > 0x80)
260 c = codepage[i - 0x80];
261 string[i] = c;
262 if (!c)
263 break;
264 }
265 string[i] = 0;
266 EFI_EXIT(EFI_SUCCESS);
267}
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283static bool EFIAPI efi_str_to_fat(struct efi_unicode_collation_protocol *this,
284 const u16 *string, efi_uintn_t fat_size,
285 char *fat)
286{
287 efi_uintn_t i;
288 s32 c;
289 bool ret = false;
290
291 EFI_ENTRY("%p, %ls, %zu, %p", this, string, fat_size, fat);
292 for (i = 0; i < fat_size;) {
293 c = utf16_get(&string);
294 switch (c) {
295
296 case '.':
297 case ' ':
298 continue;
299 case 0:
300 break;
301 }
302 c = utf_to_upper(c);
303 if (utf_to_cp(&c, codepage) ||
304 (c && (c < 0x20 || strchr(illegal, c)))) {
305 ret = true;
306 c = '_';
307 }
308
309 fat[i] = c;
310 if (!c)
311 break;
312 ++i;
313 }
314 EFI_EXIT(EFI_SUCCESS);
315 return ret;
316}
317
318const struct efi_unicode_collation_protocol efi_unicode_collation_protocol2 = {
319 .stri_coll = efi_stri_coll,
320 .metai_match = efi_metai_match,
321 .str_lwr = efi_str_lwr,
322 .str_upr = efi_str_upr,
323 .fat_to_str = efi_fat_to_str,
324 .str_to_fat = efi_str_to_fat,
325 .supported_languages = "en",
326};
327