Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_unicode.h"
#include "fx_utility.h"
_fx_unicode_temp_long_file_name
_fx_unicode_search_name
...
...
_fx_unicode_directory_search(FX_MEDIA *, FX_DIR_ENTRY *, UCHAR *, ULONG, UCHAR *, ULONG *, ULONG)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/src/fx_unicode_directory_search.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** FileX Component */ /** */ /** Unicode */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE /* Include necessary system files. */ #include "fx_api.h" #include "fx_unicode.h" #include "fx_utility.h" #ifndef FX_NO_LOCAL_PATH FX_LOCAL_PATH_SETUP #endif /* Define several Unicode working arrays... This keeps the data structures off the local stack. *//* ... */ UCHAR _fx_unicode_temp_long_file_name[FX_MAX_LONG_NAME_LEN]; UCHAR _fx_unicode_search_name[FX_MAX_LONG_NAME_LEN * 2]; ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_unicode_directory_search PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function searches for the specified unicode or short name. */ /* */ /* Note: The buffer of short_name and unicode_name must be valid to */ /* fill search result. When short_name is a zero length string, search */ /* is based on unicode string (terminated with NULL). If it's found */ /* the short name is written back to buffer of short_name. In this case*/ /* unicode_name_buffer_length is ignored and short_name_buffer_length */ /* must not be zero to specify the buffer length. If buffer is too */ /* smallfor the result, overflow characters and NULL-terminator are cut*/ /* off. When short_name is a valid string, search is based on */ /* short_name (terminated with NULL). If it's found the unicode name is*/ /* written back to buffer of unicode_name. In this case */ /* short_name_buffer_length is ignored and unicode_name_buffer_length */ /* must not be zero to specify the unicode buffer length. If buffer is */ /* too small for the result, overflow characters are cut off but */ /* NULL-terminator is kept. */ /* */ /* INPUT */ /* */ /* media_ptr Pointer to media */ /* short_name Pointer to short name */ /* short_name_buffer_length Buffer length for short name */ /* unicode_name Pointer to Unicode name */ /* unicode_name_length Unicode name length */ /* unicode_name_buffer_length Buffer length for Unicode name*/ /* */ /* OUTPUT */ /* */ /* Completion Status */ /* */ /* CALLS */ /* */ /* _fx_unicode_directory_entry_read Read a full unicode directory */ /* entry */ /* _fx_utility_FAT_entry_read Read a FAT entry */ /* */ /* CALLED BY */ /* */ /* Unicode Utilities */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 William E. Lamie Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _fx_unicode_directory_search(FX_MEDIA *media_ptr, FX_DIR_ENTRY *entry_ptr, UCHAR *short_name, ULONG short_name_buffer_length, UCHAR *unicode_name, ULONG *unicode_name_length, ULONG unicode_name_buffer_length) { ULONG i, j; UINT status, found; ULONG cluster, next_cluster = 0; ULONG directory_size; FX_DIR_ENTRY search_dir; FX_DIR_ENTRY *search_dir_ptr; ULONG unicode_search_length; ULONG local_unicode_name_length; CHAR unicode_to_short_name[13]; CHAR *short_name_ptr; /* Setup temp unicode name length. */ local_unicode_name_length = *unicode_name_length; #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of directory search requests. */ media_ptr -> fx_media_directory_searches++;/* ... */ #endif /* Set the initial search directory to the current working directory - if there is one. *//* ... */ /* First check for a local path pointer stored in the thread control block. This is only available in ThreadX Version 4 and above. *//* ... */ #ifndef FX_NO_LOCAL_PATH if (_tx_thread_current_ptr -> tx_thread_filex_ptr) { /* Determine if the local directory is not the root directory. */ if (((FX_PATH *)_tx_thread_current_ptr -> tx_thread_filex_ptr) -> fx_path_directory.fx_dir_entry_name[0]) { /* Start at the current working directory of the media. */ search_dir = ((FX_PATH *)_tx_thread_current_ptr -> tx_thread_filex_ptr) -> fx_path_directory; /* Set the internal pointer to the search directory as well. */ search_dir_ptr = &search_dir; }if (((FX_PATH *)_tx_thread_current_ptr -> tx_thread_filex_ptr) -> fx_path_directory.fx_dir_entry_name[0]) { ... } else { /* We are searching in the root directory. */ search_dir_ptr = FX_NULL; }else { ... } }if (_tx_thread_current_ptr -> tx_thread_filex_ptr) { ... } else/* ... */ #endif if (media_ptr -> fx_media_default_path.fx_path_directory.fx_dir_entry_name[0]) { /* Start at the current working directory of the media. */ search_dir = media_ptr -> fx_media_default_path.fx_path_directory; /* Set the internal pointer to the search directory as well. */ search_dir_ptr = &search_dir; }else if (media_ptr -> fx_media_default_path.fx_path_directory.fx_dir_entry_name[0]) { ... } else { /* The current default directory is the root so just set the search directory pointer to NULL. *//* ... */ search_dir_ptr = FX_NULL; }else { ... } /* Calculate the directory size. */ if (search_dir_ptr) { /* Ensure that the search directory's last search cluster is cleared. */ search_dir_ptr -> fx_dir_entry_last_search_cluster = 0; /* Calculate the directory size by counting the allocated clusters for it. *//* ... */ i = 0; cluster = search_dir_ptr -> fx_dir_entry_cluster; while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { /* Increment the cluster count. */ i++; /* Read the next FAT entry. */ status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster); /* Check the return status. */ if (status != FX_SUCCESS) { /* Return the bad status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Check for error situation. */ if ((cluster == next_cluster) || (i > media_ptr -> fx_media_total_clusters)) { /* Return the bad status. */ return(FX_FAT_READ_ERROR); }if ((cluster == next_cluster) || (i > media_ptr -> fx_media_total_clusters)) { ... } cluster = next_cluster; }while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { ... } /* Now we can calculate the directory size. */ directory_size = (((ULONG)media_ptr -> fx_media_bytes_per_sector) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster) * i) / (ULONG)FX_DIR_ENTRY_SIZE; /* Also save this in the directory entry so we don't have to calculate it later. *//* ... */ search_dir_ptr -> fx_dir_entry_file_size = directory_size; }if (search_dir_ptr) { ... } else { /* Directory size is the number of entries in the root directory. */ directory_size = (ULONG)media_ptr -> fx_media_root_directory_entries; }else { ... } /* Determine if we are searching for a short file name or a unicode file name. */ if (short_name[0] == 0) { /* If the unicode name fit into short name length, covert the Unicode to ASCII if possible. */ if (local_unicode_name_length <= 13) { for (j = 0; j < local_unicode_name_length; j++) { if ((unicode_name[j * 2] <= 0x7F) && (unicode_name[j * 2 + 1] == 0)) { unicode_to_short_name[j] = (CHAR)unicode_name[j * 2]; if ((unicode_to_short_name[j] >= 'a') && (unicode_to_short_name[j] <= 'z')) { /* Lower case, convert to upper case! */ unicode_to_short_name[j] = (CHAR)((INT)unicode_to_short_name[j] - 0x20); }if ((unicode_to_short_name[j] >= 'a') && (unicode_to_short_name[j] <= 'z')) { ... } }if ((unicode_name[j * 2] <= 0x7F) && (unicode_name[j * 2 + 1] == 0)) { ... } else { unicode_to_short_name[0] = 0; break; }else { ... } }for (j = 0; j < local_unicode_name_length; j++) { ... } }if (local_unicode_name_length <= 13) { ... } else { unicode_to_short_name[0] = 0; }else { ... } }if (short_name[0] == 0) { ... } else { unicode_to_short_name[0] = 0; }else { ... } /* Loop through entries in the directory. Yes, this is a linear search! *//* ... */ i = 0; do { /* Read an entry from the directory. */ status = _fx_unicode_directory_entry_read(media_ptr, search_dir_ptr, &i, entry_ptr, &_fx_unicode_search_name[0], &unicode_search_length); i++; /* Check for error status. */ if (status != FX_SUCCESS) { return(status); }if (status != FX_SUCCESS) { ... } /* Determine if this is an empty entry. */ if (((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0)) { continue; }if (((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_FREE) && (entry_ptr -> fx_dir_entry_short_name[0] == 0)) { ... } /* Determine if this is the last directory entry. */ if ((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_DONE) { break; }if ((UCHAR)entry_ptr -> fx_dir_entry_name[0] == (UCHAR)FX_DIR_ENTRY_DONE) { ... } /* Determine if there is a short name to match. */ if (unicode_to_short_name[0]) { /* Get the short name pointer. */ if (entry_ptr -> fx_dir_entry_short_name[0]) { short_name_ptr = entry_ptr -> fx_dir_entry_short_name; }if (entry_ptr -> fx_dir_entry_short_name[0]) { ... } else { short_name_ptr = entry_ptr -> fx_dir_entry_name; }else { ... } for (j = 0; j < local_unicode_name_length; j++) { /* Compare characters. */ if (short_name_ptr[j] != unicode_to_short_name[j]) { break; }if (short_name_ptr[j] != unicode_to_short_name[j]) { ... } }for (j = 0; j < local_unicode_name_length; j++) { ... } if (j == local_unicode_name_length) { /* Only the 1st 13 bytes or the buffer length is copied, whichever is smaller. */ if (short_name_buffer_length > 13) { short_name_buffer_length = 13; }if (short_name_buffer_length > 13) { ... } /* The names match, copy the short name into the destination. */ for (j = 0; j < short_name_buffer_length; j++) { short_name[j] = (UCHAR)short_name_ptr[j]; }for (j = 0; j < short_name_buffer_length; j++) { ... } /* Return success to caller. */ return(FX_SUCCESS); }if (j == local_unicode_name_length) { ... } }if (unicode_to_short_name[0]) { ... } /* Determine if this is not a unicode name. */ if (unicode_search_length == 0) { continue; }if (unicode_search_length == 0) { ... } /* Determine if we are searching for a short file name or a unicode file name. */ if (short_name[0]) { /* We have a short name and need a unicode name. Compare the short name against the short name in the directory entry for a match. *//* ... */ found = FX_TRUE; for (j = 0; j < 12; j++) { /* Compare characters... */ if (entry_ptr -> fx_dir_entry_short_name[0]) { /* Yes, the return name is in the short name field... compare against it! */ if (short_name[j] != (UCHAR)entry_ptr -> fx_dir_entry_short_name[j]) { found = FX_FALSE; break; }if (short_name[j] != (UCHAR)entry_ptr -> fx_dir_entry_short_name[j]) { ... } }if (entry_ptr -> fx_dir_entry_short_name[0]) { ... } else { /* No, the return name is in the name field... compare against it! */ if (short_name[j] != (UCHAR)entry_ptr -> fx_dir_entry_name[j]) { found = FX_FALSE; break; }if (short_name[j] != (UCHAR)entry_ptr -> fx_dir_entry_name[j]) { ... } }else { ... } /* Are we done? */ if (short_name[j] == (UCHAR)FX_NULL) { break; }if (short_name[j] == (UCHAR)FX_NULL) { ... } }for (j = 0; j < 12; j++) { ... } /* One final compare to see if we have a match. */ if ((found == FX_FALSE) || ((j == 12) && (short_name[12] != 0))) { continue; }if ((found == FX_FALSE) || ((j == 12) && (short_name[12] != 0))) { ... } /* A match was found so copy the unicode name and length and return. */ /* Copy the length. */ *unicode_name_length = unicode_search_length; /* Check if the name fit in the buffer. */ if (unicode_name_buffer_length < (unicode_search_length + 1) * 2) { unicode_search_length = (unicode_name_buffer_length - 2) / 2; }if (unicode_name_buffer_length < (unicode_search_length + 1) * 2) { ... } /* Copy the unicode name. */ for (j = 0; j < unicode_search_length * 2; j++) { /* Copy one unicode character to the destination... */ unicode_name[j] = _fx_unicode_search_name[j]; }for (j = 0; j < unicode_search_length * 2; j++) { ... } /* Make sure there is a NULL in the destination. */ unicode_name[j] = (UCHAR)0; unicode_name[j + 1] = (UCHAR)0; /* Return successful completion. */ return(FX_SUCCESS); }if (short_name[0]) { ... } else { /* Determine if this is the correct unicode name. */ if (unicode_search_length != local_unicode_name_length) { continue; }if (unicode_search_length != local_unicode_name_length) { ... } /* Compare the unicode search name with the requested unicode name. */ for (j = 0; j < (local_unicode_name_length * 2); j = j + 2) { /* Compare bytes of each unicode name. */ if (unicode_name[j] != _fx_unicode_search_name[j]) { /* Not match, Check if the character is in ASCII range. */ if ((_fx_unicode_search_name[j + 1] == 0) && (unicode_name[j + 1] == 0)) { /* Check if it is case mismatch. */ if ((unicode_name[j]) >= 'a' && (unicode_name[j] <= 'z')) { if ((unicode_name[j] - 0x20) == _fx_unicode_search_name[j]) { continue; }if ((unicode_name[j] - 0x20) == _fx_unicode_search_name[j]) { ... } }if ((unicode_name[j]) >= 'a' && (unicode_name[j] <= 'z')) { ... } if ((_fx_unicode_search_name[j]) >= 'a' && (_fx_unicode_search_name[j] <= 'z')) { if ((_fx_unicode_search_name[j] - 0x20) == unicode_name[j]) { continue; }if ((_fx_unicode_search_name[j] - 0x20) == unicode_name[j]) { ... } }if ((_fx_unicode_search_name[j]) >= 'a' && (_fx_unicode_search_name[j] <= 'z')) { ... } }if ((_fx_unicode_search_name[j + 1] == 0) && (unicode_name[j + 1] == 0)) { ... } break; }if (unicode_name[j] != _fx_unicode_search_name[j]) { ... } /* Compare the next byte. */ if (unicode_name[j + 1] != _fx_unicode_search_name[j + 1]) { break; }if (unicode_name[j + 1] != _fx_unicode_search_name[j + 1]) { ... } }for (j = 0; j < (local_unicode_name_length * 2); j = j + 2) { ... } /* Determine if the names do not match. */ if (j != (local_unicode_name_length * 2)) { continue; }if (j != (local_unicode_name_length * 2)) { ... } /* Otherwise, the names match, copy the short name into the destination. */ /* Only the 1st 13 bytes or the buffer length is copied, whichever is smaller. */ if (short_name_buffer_length > 13) { short_name_buffer_length = 13; }if (short_name_buffer_length > 13) { ... } for (j = 0; j < short_name_buffer_length; j++) { /* Copy a character. */ if (entry_ptr -> fx_dir_entry_short_name[0]) { short_name[j] = (UCHAR)entry_ptr -> fx_dir_entry_short_name[j]; }if (entry_ptr -> fx_dir_entry_short_name[0]) { ... } else { short_name[j] = (UCHAR)entry_ptr -> fx_dir_entry_name[j]; }else { ... } }for (j = 0; j < short_name_buffer_length; j++) { ... } /* Return success to caller. */ return(FX_SUCCESS); }else { ... } ...} while (i < directory_size); /* Return not found. */ return(FX_NOT_FOUND); }{ ... }