-
Notifications
You must be signed in to change notification settings - Fork 51
/
ff_dir.c
3725 lines (3192 loc) · 132 KB
/
ff_dir.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FreeRTOS+FAT V2.3.3
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*
*/
/**
* @file ff_dir.c
* @ingroup DIR
*
* @defgroup DIR Handles Directory Traversal
* @brief Handles DIR access and traversal.
*
* Provides FindFirst() and FindNext() Interfaces
**/
#include "ff_headers.h"
#include <stdio.h>
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
#include <wchar.h>
#endif
#if defined( WIN32 )
#define wcsicmp _wcsicmp
#else
#define wcsicmp wcscasecmp
#include <ctype.h>
#endif
/* Calculate a simple LFN checksum. */
static uint8_t FF_CreateChkSum( const uint8_t * pa_pShortName );
static BaseType_t FF_ShortNameExists( FF_IOManager_t * pxIOManager,
uint32_t ulDirCluster,
char * pcShortName,
FF_Error_t * pxError );
#if ( ffconfigSHORTNAME_CASE != 0 )
/* For short-name entries, NT/XP etc store case information in byte 0x0c
* Use this to show proper case of "README.txt" or "source.H".
*/
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
static void FF_CaseShortName( FF_T_WCHAR * pcName,
uint8_t attrib );
#else
static void FF_CaseShortName( char * pcName,
uint8_t attrib );
#endif
#endif /* ffconfigSHORTNAME_CASE */
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
/* For unicode, the short name can be expanded to wchar
* by inserting zero's.
*/
static void FF_ShortNameExpand( FF_T_WCHAR * );
#endif
/*
* Transform a name as stored on disk "README__TXT"
* to a nul-terminated string: "README.TXT", "FILE001".
* A dot is only added if an extension is present.
*/
static void FF_ProcessShortName( char * pcName );
#if ( ffconfigTIME_SUPPORT != 0 )
static void FF_PlaceTime( uint8_t * pucEntryBuffer,
uint32_t Offset,
FF_SystemTime_t * pxTime );
static void FF_PlaceDate( uint8_t * pucEntryBuffer,
uint32_t Offset,
FF_SystemTime_t * pxTime );
static void FF_GetTime( FF_SystemTime_t * pxTime,
const uint8_t * pucEntryBuffer,
uint32_t ulOffset );
static void FF_GetDate( FF_SystemTime_t * pxTime,
const uint8_t * pucEntryBuffer,
uint32_t ulOffset );
#endif /* ffconfigTIME_SUPPORT */
static FF_Error_t FF_Traverse( FF_IOManager_t * pxIOManager,
uint32_t ulEntry,
FF_FetchContext_t * pxContext );
static int32_t FF_FindFreeDirent( FF_IOManager_t * pxIOManager,
FF_FindParams_t * pxFindParams,
uint16_t usSequential );
#if ( ffconfigLFN_SUPPORT != 0 )
static int8_t FF_CreateLFNEntry( uint8_t * pucEntryBuffer,
uint8_t * pcName,
UBaseType_t uxNameLength,
UBaseType_t uxLFN,
uint8_t ucCheckSum );
#endif /* ffconfigLFN_SUPPORT */
static BaseType_t FF_ValidShortChar( char cChar );
#if ( ffconfigLFN_SUPPORT != 0 )
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
static FF_Error_t FF_CreateLFNs( FF_IOManager_t * pxIOManager,
uint32_t ulDirCluster,
FF_T_WCHAR * pcName,
uint8_t ucCheckSum,
uint16_t usEntry );
#else
static FF_Error_t FF_CreateLFNs( FF_IOManager_t * pxIOManager,
uint32_t ulDirCluster,
char * pcName,
uint8_t ucCheckSum,
uint16_t usEntry );
#endif /* if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) */
#endif /* if ( ffconfigLFN_SUPPORT != 0 ) */
#if ( FF_NOSTRCASECMP == 0 )
static portINLINE unsigned char prvToLower( unsigned char c )
{
unsigned char cReturnChar;
if( ( c >= 'A' ) && ( c <= 'Z' ) )
{
cReturnChar = c + 0x20;
}
else
{
cReturnChar = c;
}
return cReturnChar;
}
int strcasecmp( const char * pcString1,
const char * pcString2 )
{
unsigned char c1, c2;
do
{
c1 = ( unsigned char ) *pcString1++;
c2 = ( unsigned char ) *pcString2++;
c1 = prvToLower( c1 );
c2 = prvToLower( c2 );
}
while( ( c1 == c2 ) && ( c1 != '\0' ) );
return ( int ) c1 - c2;
} /* strcasecmp() */
#endif /* if ( FF_NOSTRCASECMP == 0 ) */
/*-----------------------------------------------------------*/
static uint8_t FF_CreateChkSum( const uint8_t * pa_pShortName )
{
uint8_t cNameLen;
uint8_t ChkSum = 0;
for( cNameLen = 11; cNameLen != 0; cNameLen-- )
{
ChkSum = ( uint8_t )
( ( ( ChkSum & 1 ) ? 0x80 : 0 ) + ( ChkSum >> 1 ) + *( pa_pShortName++ ) );
}
return ChkSum;
} /* FF_CreateChkSum() */
/*-----------------------------------------------------------*/
/* _HT_ Does not need a wchar version because a short name is treated a normal string of bytes */
static BaseType_t FF_ShortNameExists( FF_IOManager_t * pxIOManager,
uint32_t ulDirCluster,
char * pcShortName,
FF_Error_t * pxError )
{
BaseType_t xIndex;
const uint8_t * pucEntryBuffer = NULL; /* initialisation not necessary, just for the compiler */
uint8_t ucAttrib;
FF_FetchContext_t xFetchContext;
char pcMyShortName[ FF_SIZEOF_DIRECTORY_ENTRY ];
BaseType_t xResult = -1;
#if ( ffconfigHASH_CACHE != 0 )
uint32_t ulHash;
#endif
*pxError = FF_ERR_NONE;
#if ( ffconfigHASH_CACHE != 0 )
{
if( !FF_DirHashed( pxIOManager, ulDirCluster ) )
{
/* Hash the directory. */
FF_HashDir( pxIOManager, ulDirCluster );
}
#if ffconfigHASH_FUNCTION == CRC16
{
ulHash = ( uint32_t ) FF_GetCRC16( ( uint8_t * ) pcShortName, ( uint32_t ) strlen( pcShortName ) );
}
#else /* ffconfigHASH_FUNCTION == CRC8 */
{
ulHash = ( uint32_t ) FF_GetCRC8( ( uint8_t * ) pcShortName, ( uint32_t ) strlen( pcShortName ) );
}
#endif
{
/* FF_CheckDirentHash result: 0 not found, 1 found, -1 directory not hashed */
xResult = FF_CheckDirentHash( pxIOManager, ulDirCluster, ulHash );
}
}
#endif /* if ( ffconfigHASH_CACHE != 0 ) */
if( xResult < 0 )
{
xResult = pdFALSE;
*pxError = FF_InitEntryFetch( pxIOManager, ulDirCluster, &xFetchContext );
if( FF_isERR( *pxError ) == pdFALSE )
{
for( xIndex = 0; xIndex < FF_MAX_ENTRIES_PER_DIRECTORY; xIndex++ )
{
/* Call FF_FetchEntryWithContext only once for every block (usually 512 bytes) */
if( ( xIndex == 0 ) ||
( pucEntryBuffer >= xFetchContext.pxBuffer->pucBuffer + ( pxIOManager->usSectorSize - FF_SIZEOF_DIRECTORY_ENTRY ) ) )
{
*pxError = FF_FetchEntryWithContext( pxIOManager, ( uint32_t ) xIndex, &xFetchContext, NULL );
if( FF_isERR( *pxError ) )
{
break;
}
pucEntryBuffer = xFetchContext.pxBuffer->pucBuffer;
}
else
{
/* Advance 32 bytes to get the next directory entry. */
pucEntryBuffer += FF_SIZEOF_DIRECTORY_ENTRY;
}
if( FF_isEndOfDir( pucEntryBuffer ) )
{
break;
}
if( FF_isDeleted( pucEntryBuffer ) == pdFALSE )
{
ucAttrib = FF_getChar( pucEntryBuffer, FF_FAT_DIRENT_ATTRIB );
if( ( ucAttrib & FF_FAT_ATTR_LFN ) != FF_FAT_ATTR_LFN )
{
memcpy( pcMyShortName, pucEntryBuffer, sizeof( pcMyShortName ) );
FF_ProcessShortName( pcMyShortName );
if( strcmp( ( const char * ) pcShortName, ( const char * ) pcMyShortName ) == 0 )
{
xResult = pdTRUE;
break;
}
}
}
} /* for ( xIndex = 0; xIndex < FF_MAX_ENTRIES_PER_DIRECTORY; xIndex++ ) */
}
*pxError = FF_CleanupEntryFetch( pxIOManager, &xFetchContext );
}
return xResult;
} /* FF_ShortNameExists() */
/*-----------------------------------------------------------*/
/* _HT_ When adding many files to a single directory, FF_FindEntryInDir was sometimes */
/* _HT_ called 3 times before inserting a single file. With these changes it is called one time only */
/* _HT_ pxFindParams caches some information: */
/* _HT_ 1: the first free entry 2: whether the short-name version already exists */
/* *INDENT-OFF* */
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
uint32_t FF_FindEntryInDir( FF_IOManager_t * pxIOManager,
FF_FindParams_t * pxFindParams,
const FF_T_WCHAR * pcName,
uint8_t pa_Attrib,
FF_DirEnt_t * pxDirEntry,
FF_Error_t * pxError )
#else
uint32_t FF_FindEntryInDir( FF_IOManager_t * pxIOManager,
FF_FindParams_t * pxFindParams,
const char * pcName,
uint8_t pa_Attrib,
FF_DirEnt_t * pxDirEntry,
FF_Error_t * pxError )
#endif /* if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) */
/* *INDENT-ON* */
{
FF_FetchContext_t xFetchContext;
/* const pointer to read from pBuffer */
const uint8_t * src = NULL;
/* As we're walking through a directory, we might as well
* find the first free entry to help FF_FindFreeDirent( )
* The result will be stored in 'pxFindParams->lFreeEntry' */
BaseType_t entriesNeeded;
BaseType_t freeCount = 0;
FF_Error_t xError;
/* If the file name fits into a short file name
* then the existence of that short file name will be checked as well. */
BaseType_t testShortname;
uint32_t xResult = 0ul;
#if ( ffconfigUNICODE_UTF8_SUPPORT == 1 )
int32_t utf8Error;
#endif
#if ( ffconfigLFN_SUPPORT != 0 )
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
FF_T_WCHAR * pcCurPtr; /* Pointer to store a LFN. */
FF_T_WCHAR * pcLastPtr = pxDirEntry->pcFileName + sizeof( pxDirEntry->pcFileName );
#else
char * pcCurPtr; /* Pointer to store a LFN. */
char * pcLastPtr = pxDirEntry->pcFileName + sizeof( pxDirEntry->pcFileName );
#endif /* ffconfigUNICODE_UTF16_SUPPORT */
uint16_t lfnItem = 0;
uint8_t ucCheckSum = 0;
BaseType_t xLFNCount = 0;
BaseType_t xLFNTotal = 0;
uint8_t lastAttrib;
BaseType_t xIndex;
#endif /* ffconfigLFN_SUPPORT */
#if ( ffconfigLFN_SUPPORT != 0 )
{
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
BaseType_t NameLen = ( BaseType_t ) wcslen( ( const char * ) pcName );
#else
BaseType_t NameLen = ( BaseType_t ) strlen( ( const char * ) pcName );
#endif
/* Find enough places for the LFNs and the ShortName. */
entriesNeeded = ( uint8_t ) ( ( NameLen + 12 ) / 13 ) + 1;
}
#else
{
entriesNeeded = 1;
}
#endif /* ffconfigLFN_SUPPORT */
if( ( pxFindParams->ulFlags & FIND_FLAG_FITS_SHORT_OK ) == FIND_FLAG_FITS_SHORT_OK )
{
testShortname = pdTRUE;
}
else
{
testShortname = pdFALSE;
}
pxDirEntry->ucAttrib = 0;
if( ( pxFindParams->ulFlags & FIND_FLAG_CREATE_FLAG ) != 0 )
{
/* A file is to be created: keep track of the first free entry big enough
* to hold this file name. */
pxFindParams->lFreeEntry = -1;
}
else
{
pxFindParams->lFreeEntry = 0;
}
xError = FF_InitEntryFetch( pxIOManager, pxFindParams->ulDirCluster, &xFetchContext );
if( FF_isERR( xError ) == pdFALSE )
{
for( pxDirEntry->usCurrentItem = 0; pxDirEntry->usCurrentItem < FF_MAX_ENTRIES_PER_DIRECTORY; pxDirEntry->usCurrentItem++ )
{
if( ( src == NULL ) ||
( src >= xFetchContext.pxBuffer->pucBuffer + ( pxIOManager->usSectorSize - FF_SIZEOF_DIRECTORY_ENTRY ) ) )
{
xError = FF_FetchEntryWithContext( pxIOManager, pxDirEntry->usCurrentItem, &xFetchContext, NULL );
if( FF_isERR( xError ) != pdFALSE )
{
break;
}
src = xFetchContext.pxBuffer->pucBuffer;
}
else
{
/* Advance 32 bytes. */
src += FF_SIZEOF_DIRECTORY_ENTRY;
}
if( FF_isEndOfDir( src ) )
{
/* 0x00 end-of-dir. */
break;
}
if( FF_isDeleted( src ) )
{
/* Entry not used or deleted. */
pxDirEntry->ucAttrib = 0;
if( ( pxFindParams->lFreeEntry < 0 ) && ( ++freeCount == entriesNeeded ) )
{
/* Remember the beginning entry in the sequential sequence. */
pxFindParams->lFreeEntry = ( pxDirEntry->usCurrentItem - ( int32_t ) ( entriesNeeded - 1 ) );
}
continue;
}
/* The current entry is in use, so reset the free-entry-counter */
freeCount = 0;
#if ( ffconfigLFN_SUPPORT != 0 )
{
lastAttrib = pxDirEntry->ucAttrib;
}
#endif
pxDirEntry->ucAttrib = FF_getChar( src, FF_FAT_DIRENT_ATTRIB );
if( ( pxDirEntry->ucAttrib & FF_FAT_ATTR_LFN ) == FF_FAT_ATTR_LFN )
{
/* LFN Processing. */
#if ( ffconfigLFN_SUPPORT != 0 )
{
if( ( xLFNCount == 0 ) || ( ( lastAttrib & FF_FAT_ATTR_LFN ) != FF_FAT_ATTR_LFN ) )
{
xLFNTotal = xLFNCount = ( BaseType_t ) ( src[ 0 ] & ~0x40 );
lfnItem = pxDirEntry->usCurrentItem;
ucCheckSum = FF_getChar( src, FF_FAT_LFN_CHECKSUM );
pcLastPtr[ -1 ] = '\0';
}
if( xLFNCount != 0 )
{
xLFNCount--;
pcCurPtr = pxDirEntry->pcFileName + ( xLFNCount * 13 );
/*
* This section needs to extract the name and do the comparison
* dependent on UNICODE settings in the FreeRTOSFATConfig.h file.
*/
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
{
/* Add UTF-16 Routine here. */
/* Copy first 5 UTF-16 chars ( 10 bytes ). */
memcpy( pcCurPtr, &src[ FF_FAT_LFN_NAME_1 ], 10 );
/* Increment Filename pointer 5 utf16 chars. */
pcCurPtr += 5;
/* Copy next 6 chars ( 12 bytes ). */
memcpy( pcCurPtr, &src[ FF_FAT_LFN_NAME_2 ], 12 );
pcCurPtr += 6;
/* You're getting the idea by now! */
memcpy( pcCurPtr, &src[ FF_FAT_LFN_NAME_3 ], 4 );
pcCurPtr += 2;
} /* ffconfigUNICODE_UTF16_SUPPORT */
#elif ( ffconfigUNICODE_UTF8_SUPPORT != 0 )
{
/* UTF-8 Routine here. */
for( xIndex = 0; ( xIndex < 5 ) && ( pcCurPtr < pcLastPtr ); xIndex++ )
{
/* Was there a surrogate sequence? -- Add handling here. */
utf8Error = FF_Utf16ctoUtf8c( ( uint8_t * ) pcCurPtr, ( uint16_t * ) &src[ FF_FAT_LFN_NAME_1 + ( 2 * xIndex ) ], pcLastPtr - pcCurPtr );
if( utf8Error > 0 )
{
pcCurPtr += utf8Error;
}
else
{
if( FF_GETERROR( utf8Error ) == FF_ERR_UNICODE_INVALID_SEQUENCE )
{
/* Handle potential surrogate sequence across entries. */
}
}
}
for( xIndex = 0; xIndex < 6 && pcCurPtr < pcLastPtr; xIndex++ )
{
/* Was there a surrogate sequence? -- To add handling here. */
utf8Error = FF_Utf16ctoUtf8c( ( uint8_t * ) pcCurPtr, ( uint16_t * ) &src[ FF_FAT_LFN_NAME_2 + ( 2 * xIndex ) ], pcLastPtr - pcCurPtr );
if( utf8Error > 0 )
{
pcCurPtr += utf8Error;
}
else
{
if( FF_GETERROR( utf8Error ) == FF_ERR_UNICODE_INVALID_SEQUENCE )
{
/* Handle potential surrogate sequence across entries. */
}
}
}
for( xIndex = 0; xIndex < 2 && pcCurPtr < pcLastPtr; xIndex++ )
{
/* Was there a surrogate sequence? -- To add handling here. */
utf8Error = FF_Utf16ctoUtf8c( ( uint8_t * ) pcCurPtr, ( uint16_t * ) &src[ FF_FAT_LFN_NAME_3 + ( 2 * xIndex ) ], pcLastPtr - pcCurPtr );
if( utf8Error > 0 )
{
pcCurPtr += utf8Error;
}
else
{
if( FF_GETERROR( utf8Error ) == FF_ERR_UNICODE_INVALID_SEQUENCE )
{
/* Handle potential surrogate sequence across entries. */
}
}
}
} /* ffconfigUNICODE_UTF8_SUPPORT */
#else /* if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) */
{ /* use ASCII notation. */
for( xIndex = 0; ( xIndex < 10 ) && ( pcCurPtr < pcLastPtr ); xIndex += 2 )
{
*( pcCurPtr++ ) = ( char ) src[ FF_FAT_LFN_NAME_1 + xIndex ];
}
for( xIndex = 0; ( xIndex < 12 ) && ( pcCurPtr < pcLastPtr ); xIndex += 2 )
{
*( pcCurPtr++ ) = ( char ) src[ FF_FAT_LFN_NAME_2 + xIndex ];
}
for( xIndex = 0; ( xIndex < 4 ) && ( pcCurPtr < pcLastPtr ); xIndex += 2 )
{
*( pcCurPtr++ ) = ( char ) src[ FF_FAT_LFN_NAME_3 + xIndex ];
}
}
#endif /* ( ffconfigUNICODE_UTF16_SUPPORT == 0 ) && !( ffconfigUNICODE_UTF8_SUPPORT == 0 ) */
if( ( xLFNCount == xLFNTotal - 1 ) && ( pcCurPtr < pcLastPtr ) )
{
*pcCurPtr = '\0'; /* Important when name len is multiple of 13. */
}
} /* if( xLFNCount ) */
}
#endif /* ffconfigLFN_SUPPORT */
continue;
}
if( ( pxDirEntry->ucAttrib & FF_FAT_ATTR_VOLID ) == FF_FAT_ATTR_VOLID )
{
#if ( ffconfigLFN_SUPPORT != 0 )
{
xLFNTotal = 0;
}
#endif /* ffconfigLFN_SUPPORT */
continue;
}
#if ( ffconfigLFN_SUPPORT != 0 )
if( ( xLFNTotal == 0 ) || ( ucCheckSum != FF_CreateChkSum( src ) ) )
#endif /* ffconfigLFN_SUPPORT */
{
/* This entry has only a short name, or the checksum isn't correct
* Use the short name for comparison */
memcpy( pxDirEntry->pcFileName, src, 11 );
FF_ProcessShortName( ( char * ) pxDirEntry->pcFileName );
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
{
/* FileName now contains a 8-bit short name
* Expand it to a FF_T_WCHAR string. */
FF_ShortNameExpand( pxDirEntry->pcFileName );
}
#endif /* ffconfigUNICODE_UTF16_SUPPORT */
#if ( ffconfigLFN_SUPPORT != 0 )
{
xLFNTotal = 0;
}
#endif /* ffconfigLFN_SUPPORT */
}
/* This function FF_FindEntryInDir( ) is either called with
* pa_Attrib==0 or with pa_Attrib==FF_FAT_ATTR_DIR
* In the last case the caller is looking for a directory */
if( ( pxDirEntry->ucAttrib & pa_Attrib ) == pa_Attrib )
{
if( testShortname )
{
/* Both strings are stored in the directory format
* e.g. "README TXT", without a dot */
if( memcmp( src, pxFindParams->pcEntryBuffer, 11 ) == 0 )
{
pxFindParams->ulFlags |= FIND_FLAG_SHORTNAME_CHECKED | FIND_FLAG_SHORTNAME_FOUND;
}
}
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
if( wcsicmp( ( const char * ) pcName, ( const char * ) pxDirEntry->pcFileName ) == 0 )
#else
if( FF_stricmp( ( const char * ) pcName, ( const char * ) pxDirEntry->pcFileName ) == 0 )
#endif /* ffconfigUNICODE_UTF16_SUPPORT */
{
/* Finally get the complete information. */
#if ( ffconfigLFN_SUPPORT != 0 )
if( xLFNTotal )
{
xError = FF_PopulateLongDirent( pxIOManager, pxDirEntry, ( uint16_t ) lfnItem, &xFetchContext );
if( FF_isERR( xError ) )
{
break;
}
}
else
#endif /* ffconfigLFN_SUPPORT */
{
FF_PopulateShortDirent( pxIOManager, pxDirEntry, src );
/* HT: usCurrentItem wasn't increased here. */
pxDirEntry->usCurrentItem++;
}
/* Object found, the cluster number will be returned. */
xResult = pxDirEntry->ulObjectCluster;
break;
}
}
#if ( ffconfigLFN_SUPPORT != 0 )
{
xLFNTotal = 0;
}
#endif
} /* for( ; pxDirEntry->usCurrentItem < FF_MAX_ENTRIES_PER_DIRECTORY; pxDirEntry->usCurrentItem++ ) */
{
FF_Error_t xTempError;
xTempError = FF_CleanupEntryFetch( pxIOManager, &xFetchContext );
if( FF_isERR( xError ) == pdFALSE )
{
xError = xTempError;
}
}
} /* if( FF_isERR( xError ) == pdFALSE ) */
if( FF_isERR( xError ) == pdFALSE )
{
/* If a free entry wasn't found yet, put it to the current (last) item */
if( pxFindParams->lFreeEntry < 0 )
{
pxFindParams->lFreeEntry = pxDirEntry->usCurrentItem;
}
/* If we were checking the existence of the short-name
* set the Checked flag now */
if( testShortname )
{
pxFindParams->ulFlags |= FIND_FLAG_SHORTNAME_CHECKED;
}
}
if( pxError != NULL )
{
*pxError = xError;
}
return xResult;
} /* FF_FindEntryInDir() */
/*-----------------------------------------------------------*/
/**
* @private
**/
/* *INDENT-OFF* */
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
uint32_t FF_FindDir( FF_IOManager_t * pxIOManager,
const FF_T_WCHAR * pcPath,
uint16_t pathLen,
FF_Error_t * pxError )
#else
uint32_t FF_FindDir( FF_IOManager_t * pxIOManager,
const char * pcPath,
uint16_t pathLen,
FF_Error_t * pxError )
#endif
/* *INDENT-ON* */
{
uint16_t it = 0; /* Re-entrancy Variables for FF_strtok( ). */
BaseType_t last = pdFALSE;
FF_DirEnt_t xMyDirectory;
FF_FindParams_t xFindParams;
FF_Error_t xError;
BaseType_t xFound;
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
FF_T_WCHAR mytoken[ ffconfigMAX_FILENAME ];
FF_T_WCHAR * pcToken;
#else
char mytoken[ ffconfigMAX_FILENAME ];
char * pcToken;
#endif
#if ( ffconfigPATH_CACHE != 0 )
BaseType_t xIndex;
#endif
memset( &xFindParams, '\0', sizeof( xFindParams ) );
xFindParams.ulDirCluster = pxIOManager->xPartition.ulRootDirCluster;
xError = FF_ERR_NONE;
if( pathLen <= 1 )
{
/* Must be the root directory.
* 'xFindParams.ulDirCluster' will be returned containing 'pxIOManager->xPartition.ulRootDirCluster'. */
xFound = pdTRUE;
}
else
{
/* Only the root directory '/' shall have a trailing slash in its name. */
if( ( pcPath[ pathLen - 1 ] == '\\' ) || ( pcPath[ pathLen - 1 ] == '/' ) )
{
pathLen--;
}
xFound = pdFALSE;
#if ( ffconfigPATH_CACHE != 0 ) /* Is the requested pcPath in the PATH CACHE? */
{
FF_PendSemaphore( pxIOManager->pvSemaphore ); /* Thread safety on shared object! */
{
for( xIndex = 0; xIndex < ffconfigPATH_CACHE_DEPTH; xIndex++ )
{
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
if( wcslen( pxIOManager->xPartition.pxPathCache[ xIndex ].pcPath ) == ( size_t ) pathLen )
#else
if( strlen( pxIOManager->xPartition.pxPathCache[ xIndex ].pcPath ) == ( size_t ) pathLen )
#endif
{
if( FF_strmatch( pxIOManager->xPartition.pxPathCache[ xIndex ].pcPath, pcPath, pathLen ) )
{
xFindParams.ulDirCluster = pxIOManager->xPartition.pxPathCache[ xIndex ].ulDirCluster;
xFound = pdTRUE;
break;
}
}
}
}
FF_ReleaseSemaphore( pxIOManager->pvSemaphore );
}
#endif /* ffconfigPATH_CACHE */
}
if( xFound == pdFALSE )
{
pcToken = FF_strtok( pcPath, mytoken, &it, &last, pathLen );
do
{
xMyDirectory.usCurrentItem = 0;
xFindParams.ulDirCluster = FF_FindEntryInDir( pxIOManager, &xFindParams, pcToken, ( uint8_t ) FF_FAT_ATTR_DIR, &xMyDirectory, &xError );
if( xFindParams.ulDirCluster == 0ul )
{
break;
}
pcToken = FF_strtok( pcPath, mytoken, &it, &last, pathLen );
} while( pcToken != NULL );
if( ( pcToken != NULL ) &&
( ( FF_isERR( xError ) == pdFALSE ) || ( FF_GETERROR( xError ) == FF_ERR_DIR_END_OF_DIR ) ) )
{
xError = FF_createERR( FF_FINDDIR, FF_ERR_FILE_INVALID_PATH );
}
#if ( ffconfigPATH_CACHE != 0 ) /* Update the PATH CACHE with a new PATH. */
{
/* Only cache if the dir was actually found! */
if( ( FF_isERR( xError ) == pdFALSE ) && ( xFindParams.ulDirCluster != 0ul ) )
{
FF_PendSemaphore( pxIOManager->pvSemaphore );
{
if( pathLen < ffconfigMAX_FILENAME ) /* Ensure the PATH won't cause a buffer overrun. */
{
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
{
memcpy( pxIOManager->xPartition.pxPathCache[ pxIOManager->xPartition.ulPCIndex ].pcPath, pcPath, pathLen * sizeof( FF_T_WCHAR ) );
}
#else
{
memcpy( pxIOManager->xPartition.pxPathCache[ pxIOManager->xPartition.ulPCIndex ].pcPath, pcPath, pathLen );
}
#endif
pxIOManager->xPartition.pxPathCache[ pxIOManager->xPartition.ulPCIndex ].pcPath[ pathLen ] = '\0';
pxIOManager->xPartition.pxPathCache[ pxIOManager->xPartition.ulPCIndex ].ulDirCluster = xFindParams.ulDirCluster;
pxIOManager->xPartition.ulPCIndex += 1;
if( pxIOManager->xPartition.ulPCIndex >= ffconfigPATH_CACHE_DEPTH )
{
pxIOManager->xPartition.ulPCIndex = 0;
}
}
}
FF_ReleaseSemaphore( pxIOManager->pvSemaphore );
}
}
#endif /* ffconfigPATH_CACHE */
} /* if( pathLen > 1 ) */
if( pxError != NULL )
{
*pxError = xError;
}
return xFindParams.ulDirCluster;
} /* FF_FindDir() */
/*-----------------------------------------------------------*/
#if ( ffconfigSHORTNAME_CASE != 0 )
/**
* * For short-name entries, NT/XP etc store case information in byte 0x0c
* Use this to show proper case of "README.txt" or "source.H"
**/
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
static void FF_CaseShortName( FF_T_WCHAR * pcName,
uint8_t attrib )
#else
static void FF_CaseShortName( char * pcName,
uint8_t attrib )
#endif
{
uint8_t testAttrib = FF_FAT_CASE_ATTR_BASE;
for( ; *pcName != '\0'; pcName++ )
{
if( *pcName == '.' )
{
testAttrib = FF_FAT_CASE_ATTR_EXT;
}
else if( ( attrib & testAttrib ) != 0 )
{
if( ( *pcName >= 'A' ) && ( *pcName <= 'Z' ) )
{
*pcName += 0x20;
}
}
else if( ( *pcName >= 'a' ) && ( *pcName <= 'z' ) )
{
*pcName -= 0x20;
}
}
}
#endif /* ffconfigSHORTNAME_CASE */
/*-----------------------------------------------------------*/
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
/**
* **/
static void FF_ShortNameExpand( FF_T_WCHAR * pFileName )
{
int8_t * pSource = ( ( int8_t * ) pFileName ) + 13;
int8_t * pTarget = ( ( int8_t * ) pFileName ) + 26;
/* "aaaaaaaa.eee": 12 characters plus a zero makes 13
* Copy from the right to the left */
while( pTarget > ( int8_t * ) pFileName )
{
pTarget[ -1 ] = 0;
pTarget[ -2 ] = *( --pSource );
pTarget -= 2;
}
}
#endif /* ffconfigUNICODE_UTF16_SUPPORT */
/*-----------------------------------------------------------*/
static void FF_ProcessShortName( char * pcName )
{
char pcShortName[ 13 ];
char * pcTarget = pcName;
int iSource;
memcpy( pcShortName, pcName, 11 );
for( iSource = 0; iSource < 11; iSource++ )
{
if( pcShortName[ iSource ] == 0x20 )
{
if( iSource >= 8 )
{
break;
}
iSource = 7;
}
else
{
if( iSource == 8 )
{
*( pcTarget++ ) = '.';
}
*( pcTarget++ ) = pcShortName[ iSource ];
}
}
*pcTarget = '\0';
}
/*-----------------------------------------------------------*/
#if ( ffconfigTIME_SUPPORT != 0 )
static void FF_PlaceTime( uint8_t * pucEntryBuffer,
uint32_t Offset,
FF_SystemTime_t * pxTime )
{
uint16_t myShort;
/* HT time changes:
* E.g. Unzip needs to use original time rather than
* the result of FF_GetSystemTime */
myShort = 0;
myShort |= ( ( pxTime->Hour << 11 ) & 0xF800 );
myShort |= ( ( pxTime->Minute << 5 ) & 0x07E0 );
myShort |= ( ( pxTime->Second / 2 ) & 0x001F );
FF_putShort( pucEntryBuffer, ( uint16_t ) Offset, myShort );
}
#endif /* ffconfigTIME_SUPPORT */
/*-----------------------------------------------------------*/
#if ( ffconfigTIME_SUPPORT != 0 )
static void FF_PlaceDate( uint8_t * pucEntryBuffer,
uint32_t Offset,
FF_SystemTime_t * pxTime )
{
uint16_t myShort;
/* HT time changes:
* Unzip needs to use original date rather than
* the current date, so make it a parameter. */
myShort = 0;
myShort |= ( ( ( pxTime->Year - 1980 ) << 9 ) & 0xFE00 );
myShort |= ( ( pxTime->Month << 5 ) & 0x01E0 );
myShort |= ( pxTime->Day & 0x001F );
FF_putShort( pucEntryBuffer, ( uint16_t ) Offset, myShort );
}
#endif /* ffconfigTIME_SUPPORT */
/*-----------------------------------------------------------*/
#if ( ffconfigTIME_SUPPORT != 0 )
static void FF_GetTime( FF_SystemTime_t * pxTime,
const uint8_t * pucEntryBuffer,
uint32_t Offset )
{
uint16_t myShort;
myShort = FF_getShort( pucEntryBuffer, ( uint16_t ) Offset );
pxTime->Hour = ( ( ( myShort & 0xF800 ) >> 11 ) & 0x001F );
pxTime->Minute = ( ( ( myShort & 0x07E0 ) >> 5 ) & 0x003F );
pxTime->Second = 2 * ( myShort & 0x01F );
}
#endif /* ffconfigTIME_SUPPORT */
/*-----------------------------------------------------------*/
#if ( ffconfigTIME_SUPPORT != 0 )
static void FF_GetDate( FF_SystemTime_t * pxTime,
const uint8_t * pucEntryBuffer,
uint32_t Offset )
{
uint16_t myShort;
myShort = FF_getShort( pucEntryBuffer, ( uint16_t ) Offset );
pxTime->Year = 1980 + ( ( ( myShort & 0xFE00 ) >> 9 ) & 0x07F );