FileName.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*++
  2. Copyright (c) 2005 - 2007, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials are licensed and made available
  4. under the terms and conditions of the BSD License which accompanies this
  5. distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. FileName.c
  11. Abstract:
  12. Functions for manipulating file names
  13. Revision History
  14. --*/
  15. #include "Fat.h"
  16. BOOLEAN
  17. FatCheckIs8Dot3Name (
  18. IN CHAR16 *FileName,
  19. OUT CHAR8 *File8Dot3Name
  20. )
  21. /*++
  22. Routine Description:
  23. This function checks whether the input FileName is a valid 8.3 short name.
  24. If the input FileName is a valid 8.3, the output is the 8.3 short name;
  25. otherwise, the output is the base tag of 8.3 short name.
  26. Arguments:
  27. FileName - The input unicode filename.
  28. File8Dot3Name - The output ascii 8.3 short name or base tag of 8.3 short name.
  29. Returns:
  30. TRUE - The input unicode filename is a valid 8.3 short name.
  31. FALSE - The input unicode filename is not a valid 8.3 short name.
  32. --*/
  33. {
  34. BOOLEAN PossibleShortName;
  35. CHAR16 *TempName;
  36. CHAR16 *ExtendName;
  37. CHAR16 *SeparateDot;
  38. UINTN MainNameLen;
  39. UINTN ExtendNameLen;
  40. PossibleShortName = TRUE;
  41. SeparateDot = NULL;
  42. SetMem (File8Dot3Name, FAT_NAME_LEN, ' ');
  43. for (TempName = FileName; *TempName; TempName++) {
  44. if (*TempName == L'.') {
  45. SeparateDot = TempName;
  46. }
  47. }
  48. if (SeparateDot == NULL) {
  49. //
  50. // Extended filename is not detected
  51. //
  52. MainNameLen = TempName - FileName;
  53. ExtendName = TempName;
  54. ExtendNameLen = 0;
  55. } else {
  56. //
  57. // Extended filename is detected
  58. //
  59. MainNameLen = SeparateDot - FileName;
  60. ExtendName = SeparateDot + 1;
  61. ExtendNameLen = TempName - ExtendName;
  62. }
  63. //
  64. // We scan the filename for the second time
  65. // to check if there exists any extra blanks and dots
  66. //
  67. while (--TempName >= FileName) {
  68. if ((*TempName == L'.' || *TempName == L' ') && (TempName != SeparateDot)) {
  69. //
  70. // There exist extra blanks and dots
  71. //
  72. PossibleShortName = FALSE;
  73. }
  74. }
  75. if (MainNameLen == 0) {
  76. PossibleShortName = FALSE;
  77. }
  78. if (MainNameLen > FAT_MAIN_NAME_LEN) {
  79. PossibleShortName = FALSE;
  80. MainNameLen = FAT_MAIN_NAME_LEN;
  81. }
  82. if (ExtendNameLen > FAT_EXTEND_NAME_LEN) {
  83. PossibleShortName = FALSE;
  84. ExtendNameLen = FAT_EXTEND_NAME_LEN;
  85. }
  86. if (FatStrToFat (FileName, MainNameLen, File8Dot3Name)) {
  87. PossibleShortName = FALSE;
  88. }
  89. if (FatStrToFat (ExtendName, ExtendNameLen, File8Dot3Name + FAT_MAIN_NAME_LEN)) {
  90. PossibleShortName = FALSE;
  91. }
  92. return PossibleShortName;
  93. }
  94. STATIC
  95. UINTN
  96. FatTrimAsciiTrailingBlanks (
  97. IN CHAR8 *Name,
  98. IN UINTN Len
  99. )
  100. /*++
  101. Routine Description:
  102. Trim the trailing blanks of fat name.
  103. Arguments:
  104. Name - The Char8 string needs to be trimed.
  105. Len - The length of the fat name.
  106. Returns:
  107. The real length of the fat name after the trailing blanks are trimmed.
  108. --*/
  109. {
  110. while (Len > 0 && Name[Len - 1] == ' ') {
  111. Len--;
  112. }
  113. return Len;
  114. }
  115. VOID
  116. FatNameToStr (
  117. IN CHAR8 *FatName,
  118. IN UINTN Len,
  119. IN UINTN LowerCase,
  120. OUT CHAR16 *Str
  121. )
  122. /*++
  123. Routine Description:
  124. Convert the ascii fat name to the unicode string and strip trailing spaces,
  125. and if necessary, convert the unicode string to lower case.
  126. Arguments:
  127. FatName - The Char8 string needs to be converted.
  128. Len - The length of the fat name.
  129. LowerCase - Indicate whether to convert the string to lower case.
  130. Str - The result of the convertion.
  131. Returns:
  132. None.
  133. --*/
  134. {
  135. //
  136. // First, trim the trailing blanks
  137. //
  138. Len = FatTrimAsciiTrailingBlanks (FatName, Len);
  139. //
  140. // Convert fat string to unicode string
  141. //
  142. FatFatToStr (Len, FatName, Str);
  143. //
  144. // If the name is to be lower cased, do it now
  145. //
  146. if (LowerCase != 0) {
  147. FatStrLwr (Str);
  148. }
  149. }
  150. VOID
  151. FatCreate8Dot3Name (
  152. IN FAT_OFILE *Parent,
  153. IN FAT_DIRENT *DirEnt
  154. )
  155. /*++
  156. Routine Description:
  157. This function generates 8Dot3 name from user specified name for a newly created file.
  158. Arguments:
  159. Parent - The parent directory.
  160. DirEnt - The directory entry whose 8Dot3Name needs to be generated.
  161. Returns:
  162. None.
  163. --*/
  164. {
  165. CHAR8 *ShortName;
  166. CHAR8 *ShortNameChar;
  167. UINTN BaseTagLen;
  168. UINTN Index;
  169. UINTN Retry;
  170. UINT8 Segment;
  171. union {
  172. UINT32 Crc;
  173. struct HEX_DATA {
  174. UINT8 Segment : HASH_VALUE_TAG_LEN;
  175. } Hex[HASH_VALUE_TAG_LEN];
  176. } HashValue;
  177. //
  178. // Make sure the whole directory has been loaded
  179. //
  180. ASSERT (Parent->ODir->EndOfDir);
  181. ShortName = DirEnt->Entry.FileName;
  182. //
  183. // Trim trailing blanks of 8.3 name
  184. //
  185. BaseTagLen = FatTrimAsciiTrailingBlanks (ShortName, FAT_MAIN_NAME_LEN);
  186. if (BaseTagLen > SPEC_BASE_TAG_LEN) {
  187. BaseTagLen = SPEC_BASE_TAG_LEN;
  188. }
  189. //
  190. // We first use the algorithm described by spec.
  191. //
  192. ShortNameChar = ShortName + BaseTagLen;
  193. *ShortNameChar++ = '~';
  194. *ShortNameChar = '1';
  195. Retry = 0;
  196. while (*FatShortNameHashSearch (Parent->ODir, ShortName) != NULL) {
  197. *ShortNameChar = (CHAR8)(*ShortNameChar + 1);
  198. if (++Retry == MAX_SPEC_RETRY) {
  199. //
  200. // We use new algorithm to generate 8.3 name
  201. //
  202. ASSERT (DirEnt->FileString != NULL);
  203. gBS->CalculateCrc32 (DirEnt->FileString, StrSize (DirEnt->FileString), &HashValue.Crc);
  204. if (BaseTagLen > HASH_BASE_TAG_LEN) {
  205. BaseTagLen = HASH_BASE_TAG_LEN;
  206. }
  207. ShortNameChar = ShortName + BaseTagLen;
  208. for (Index = 0; Index < HASH_VALUE_TAG_LEN; Index++) {
  209. Segment = HashValue.Hex[Index].Segment;
  210. if (Segment > 9) {
  211. *ShortNameChar++ = (CHAR8)(Segment - 10 + 'A');
  212. } else {
  213. *ShortNameChar++ = (CHAR8)(Segment + '0');
  214. }
  215. }
  216. *ShortNameChar++ = '~';
  217. *ShortNameChar = '1';
  218. }
  219. }
  220. }
  221. STATIC
  222. UINT8
  223. FatCheckNameCase (
  224. IN CHAR16 *Str,
  225. IN UINT8 InCaseFlag
  226. )
  227. /*++
  228. Routine Description:
  229. Check the string is lower case or upper case
  230. and it is used by fatname to dir entry count
  231. Arguments:
  232. Str - The string which needs to be checked.
  233. InCaseFlag - The input case flag which is returned when the string is lower case.
  234. Returns:
  235. OutCaseFlag - The output case flag.
  236. --*/
  237. {
  238. CHAR16 Buffer[FAT_MAIN_NAME_LEN + 1];
  239. UINT8 OutCaseFlag;
  240. ASSERT (StrSize (Str) <= sizeof (Buffer));
  241. //
  242. // Assume the case of input string is mixed
  243. //
  244. OutCaseFlag = FAT_CASE_MIXED;
  245. //
  246. // Lower case a copy of the string, if it matches the
  247. // original then the string is lower case
  248. //
  249. StrCpy (Buffer, Str);
  250. FatStrLwr (Buffer);
  251. if (StrCmp (Str, Buffer) == 0) {
  252. OutCaseFlag = InCaseFlag;
  253. }
  254. //
  255. // Upper case a copy of the string, if it matches the
  256. // original then the string is upper case
  257. //
  258. StrCpy (Buffer, Str);
  259. FatStrUpr (Buffer);
  260. if (StrCmp (Str, Buffer) == 0) {
  261. OutCaseFlag = 0;
  262. }
  263. return OutCaseFlag;
  264. }
  265. VOID
  266. FatSetCaseFlag (
  267. IN FAT_DIRENT *DirEnt
  268. )
  269. /*++
  270. Routine Description:
  271. Set the caseflag value for the directory entry.
  272. Arguments:
  273. DirEnt - The logical directory entry whose caseflag value is to be set.
  274. Returns:
  275. None.
  276. --*/
  277. {
  278. CHAR16 LfnBuffer[FAT_MAIN_NAME_LEN + 1 + FAT_EXTEND_NAME_LEN + 1];
  279. CHAR16 *TempCharPtr;
  280. CHAR16 *ExtendName;
  281. CHAR16 *FileNameCharPtr;
  282. UINT8 CaseFlag;
  283. ExtendName = NULL;
  284. TempCharPtr = LfnBuffer;
  285. FileNameCharPtr = DirEnt->FileString;
  286. ASSERT (StrSize (DirEnt->FileString) <= sizeof (LfnBuffer));
  287. while ((*TempCharPtr = *FileNameCharPtr) != 0) {
  288. if (*TempCharPtr == L'.') {
  289. ExtendName = TempCharPtr;
  290. }
  291. TempCharPtr++;
  292. FileNameCharPtr++;
  293. }
  294. CaseFlag = 0;
  295. if (ExtendName != NULL) {
  296. *ExtendName = 0;
  297. ExtendName++;
  298. CaseFlag = (UINT8)(CaseFlag | FatCheckNameCase (ExtendName, FAT_CASE_EXT_LOWER));
  299. }
  300. CaseFlag = (UINT8)(CaseFlag | FatCheckNameCase (LfnBuffer, FAT_CASE_NAME_LOWER));
  301. if ((CaseFlag & FAT_CASE_MIXED) == 0) {
  302. //
  303. // We just need one directory entry to store this file name entry
  304. //
  305. DirEnt->Entry.CaseFlag = CaseFlag;
  306. } else {
  307. //
  308. // We need one extra directory entry to store the mixed case entry
  309. //
  310. DirEnt->Entry.CaseFlag = 0;
  311. DirEnt->EntryCount++;
  312. }
  313. }
  314. VOID
  315. FatGetFileNameViaCaseFlag (
  316. IN FAT_DIRENT *DirEnt,
  317. OUT CHAR16 *FileString
  318. )
  319. /*++
  320. Routine Description:
  321. Convert the 8.3 ASCII fat name to cased Unicode string according to case flag.
  322. Arguments:
  323. DirEnt - The corresponding directory entry.
  324. FileString - The output Unicode file name.
  325. Returns:
  326. None.
  327. --*/
  328. {
  329. UINT8 CaseFlag;
  330. CHAR8 *File8Dot3Name;
  331. CHAR16 TempExt[1 + FAT_EXTEND_NAME_LEN + 1];
  332. //
  333. // Store file extension like ".txt"
  334. //
  335. CaseFlag = DirEnt->Entry.CaseFlag;
  336. File8Dot3Name = DirEnt->Entry.FileName;
  337. FatNameToStr (File8Dot3Name, FAT_MAIN_NAME_LEN, CaseFlag & FAT_CASE_NAME_LOWER, FileString);
  338. FatNameToStr (File8Dot3Name + FAT_MAIN_NAME_LEN, FAT_EXTEND_NAME_LEN, CaseFlag & FAT_CASE_EXT_LOWER, &TempExt[1]);
  339. if (TempExt[1] != 0) {
  340. TempExt[0] = L'.';
  341. StrCat (FileString, TempExt);
  342. }
  343. }
  344. UINT8
  345. FatCheckSum (
  346. IN CHAR8 *ShortNameString
  347. )
  348. /*++
  349. Routine Description:
  350. Get the Check sum for a short name.
  351. Arguments:
  352. ShortNameString - The short name for a file.
  353. Returns:
  354. Sum - UINT8 checksum.
  355. --*/
  356. {
  357. UINTN ShortNameLen;
  358. UINT8 Sum;
  359. Sum = 0;
  360. for (ShortNameLen = FAT_NAME_LEN; ShortNameLen != 0; ShortNameLen--) {
  361. Sum = (UINT8)(((Sum & 1) ? 0x80 : 0) + (Sum >> 1) + *ShortNameString++);
  362. }
  363. return Sum;
  364. }
  365. CHAR16 *
  366. FatGetNextNameComponent (
  367. IN CHAR16 *Path,
  368. OUT CHAR16 *Name
  369. )
  370. /*++
  371. Routine Description:
  372. Takes Path as input, returns the next name component
  373. in Name, and returns the position after Name (e.g., the
  374. start of the next name component)
  375. Arguments:
  376. Path - The path of one file.
  377. Name - The next name component in Path.
  378. Returns:
  379. The position after Name in the Path
  380. --*/
  381. {
  382. while (*Path != 0 && *Path != PATH_NAME_SEPARATOR) {
  383. *Name++ = *Path++;
  384. }
  385. *Name = 0;
  386. //
  387. // Get off of trailing path name separator
  388. //
  389. while (*Path == PATH_NAME_SEPARATOR) {
  390. Path++;
  391. }
  392. return Path;
  393. }
  394. BOOLEAN
  395. FatFileNameIsValid (
  396. IN CHAR16 *InputFileName,
  397. OUT CHAR16 *OutputFileName
  398. )
  399. /*++
  400. Routine Description:
  401. Check whether the IFileName is valid long file name. If the IFileName is a valid
  402. long file name, then we trim the possible leading blanks and leading/trailing dots.
  403. the trimmed filename is stored in OutputFileName
  404. Arguments:
  405. InputFileName - The input file name.
  406. OutputFileName - The output file name.
  407. Returns:
  408. TRUE - The InputFileName is a valid long file name.
  409. FALSE - The InputFileName is not a valid long file name.
  410. --*/
  411. {
  412. CHAR16 *TempNamePointer;
  413. CHAR16 TempChar;
  414. //
  415. // Trim Leading blanks
  416. //
  417. while (*InputFileName == L' ') {
  418. InputFileName++;
  419. }
  420. TempNamePointer = OutputFileName;
  421. while (*InputFileName != 0) {
  422. *TempNamePointer++ = *InputFileName++;
  423. }
  424. //
  425. // Trim Trailing blanks and dots
  426. //
  427. while (TempNamePointer > OutputFileName) {
  428. TempChar = *(TempNamePointer - 1);
  429. if (TempChar != L' ' && TempChar != L'.') {
  430. break;
  431. }
  432. TempNamePointer--;
  433. }
  434. *TempNamePointer = 0;
  435. //
  436. // Per FAT Spec the file name should meet the following criteria:
  437. // C1. Length (FileLongName) <= 255
  438. // C2. Length (X:FileFullPath<NUL>) <= 260
  439. // Here we check C1.
  440. //
  441. if (TempNamePointer - OutputFileName > EFI_FILE_STRING_LENGTH) {
  442. return FALSE;
  443. }
  444. //
  445. // See if there is any illegal characters within the name
  446. //
  447. do {
  448. if (*OutputFileName < 0x20 ||
  449. *OutputFileName == '\"' ||
  450. *OutputFileName == '*' ||
  451. *OutputFileName == '/' ||
  452. *OutputFileName == ':' ||
  453. *OutputFileName == '<' ||
  454. *OutputFileName == '>' ||
  455. *OutputFileName == '?' ||
  456. *OutputFileName == '\\' ||
  457. *OutputFileName == '|'
  458. ) {
  459. return FALSE;
  460. }
  461. OutputFileName++;
  462. } while (*OutputFileName != 0);
  463. return TRUE;
  464. }