CommonLib.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  1. /** @file
  2. Common basic Library Functions
  3. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <ctype.h>
  10. #ifdef __GNUC__
  11. #include <unistd.h>
  12. #else
  13. #include <direct.h>
  14. #endif
  15. #include "CommonLib.h"
  16. #include "EfiUtilityMsgs.h"
  17. #define SAFE_STRING_CONSTRAINT_CHECK(Expression, Status) \
  18. do { \
  19. ASSERT (Expression); \
  20. if (!(Expression)) { \
  21. return Status; \
  22. } \
  23. } while (FALSE)
  24. VOID
  25. PeiZeroMem (
  26. IN VOID *Buffer,
  27. IN UINTN Size
  28. )
  29. /*++
  30. Routine Description:
  31. Set Buffer to zero for Size bytes.
  32. Arguments:
  33. Buffer - Memory to set.
  34. Size - Number of bytes to set
  35. Returns:
  36. None
  37. --*/
  38. {
  39. INT8 *Ptr;
  40. Ptr = Buffer;
  41. while (Size--) {
  42. *(Ptr++) = 0;
  43. }
  44. }
  45. VOID
  46. PeiCopyMem (
  47. IN VOID *Destination,
  48. IN VOID *Source,
  49. IN UINTN Length
  50. )
  51. /*++
  52. Routine Description:
  53. Copy Length bytes from Source to Destination.
  54. Arguments:
  55. Destination - Target of copy
  56. Source - Place to copy from
  57. Length - Number of bytes to copy
  58. Returns:
  59. None
  60. --*/
  61. {
  62. CHAR8 *Destination8;
  63. CHAR8 *Source8;
  64. Destination8 = Destination;
  65. Source8 = Source;
  66. while (Length--) {
  67. *(Destination8++) = *(Source8++);
  68. }
  69. }
  70. VOID
  71. ZeroMem (
  72. IN VOID *Buffer,
  73. IN UINTN Size
  74. )
  75. {
  76. PeiZeroMem (Buffer, Size);
  77. }
  78. VOID
  79. CopyMem (
  80. IN VOID *Destination,
  81. IN VOID *Source,
  82. IN UINTN Length
  83. )
  84. {
  85. PeiCopyMem (Destination, Source, Length);
  86. }
  87. INTN
  88. CompareGuid (
  89. IN EFI_GUID *Guid1,
  90. IN EFI_GUID *Guid2
  91. )
  92. /*++
  93. Routine Description:
  94. Compares to GUIDs
  95. Arguments:
  96. Guid1 - guid to compare
  97. Guid2 - guid to compare
  98. Returns:
  99. = 0 if Guid1 == Guid2
  100. != 0 if Guid1 != Guid2
  101. --*/
  102. {
  103. INT32 *g1;
  104. INT32 *g2;
  105. INT32 r;
  106. //
  107. // Compare 32 bits at a time
  108. //
  109. g1 = (INT32 *) Guid1;
  110. g2 = (INT32 *) Guid2;
  111. r = g1[0] - g2[0];
  112. r |= g1[1] - g2[1];
  113. r |= g1[2] - g2[2];
  114. r |= g1[3] - g2[3];
  115. return r;
  116. }
  117. EFI_STATUS
  118. GetFileImage (
  119. IN CHAR8 *InputFileName,
  120. OUT CHAR8 **InputFileImage,
  121. OUT UINT32 *BytesRead
  122. )
  123. /*++
  124. Routine Description:
  125. This function opens a file and reads it into a memory buffer. The function
  126. will allocate the memory buffer and returns the size of the buffer.
  127. Arguments:
  128. InputFileName The name of the file to read.
  129. InputFileImage A pointer to the memory buffer.
  130. BytesRead The size of the memory buffer.
  131. Returns:
  132. EFI_SUCCESS The function completed successfully.
  133. EFI_INVALID_PARAMETER One of the input parameters was invalid.
  134. EFI_ABORTED An error occurred.
  135. EFI_OUT_OF_RESOURCES No resource to complete operations.
  136. --*/
  137. {
  138. FILE *InputFile;
  139. UINT32 FileSize;
  140. //
  141. // Verify input parameters.
  142. //
  143. if (InputFileName == NULL || strlen (InputFileName) == 0 || InputFileImage == NULL) {
  144. return EFI_INVALID_PARAMETER;
  145. }
  146. //
  147. // Open the file and copy contents into a memory buffer.
  148. //
  149. //
  150. // Open the file
  151. //
  152. InputFile = fopen (LongFilePath (InputFileName), "rb");
  153. if (InputFile == NULL) {
  154. Error (NULL, 0, 0001, "Error opening the input file", InputFileName);
  155. return EFI_ABORTED;
  156. }
  157. //
  158. // Go to the end so that we can determine the file size
  159. //
  160. if (fseek (InputFile, 0, SEEK_END)) {
  161. Error (NULL, 0, 0004, "Error reading the input file", InputFileName);
  162. fclose (InputFile);
  163. return EFI_ABORTED;
  164. }
  165. //
  166. // Get the file size
  167. //
  168. FileSize = ftell (InputFile);
  169. if (FileSize == -1) {
  170. Error (NULL, 0, 0003, "Error parsing the input file", InputFileName);
  171. fclose (InputFile);
  172. return EFI_ABORTED;
  173. }
  174. //
  175. // Allocate a buffer
  176. //
  177. *InputFileImage = malloc (FileSize);
  178. if (*InputFileImage == NULL) {
  179. fclose (InputFile);
  180. return EFI_OUT_OF_RESOURCES;
  181. }
  182. //
  183. // Reset to the beginning of the file
  184. //
  185. if (fseek (InputFile, 0, SEEK_SET)) {
  186. Error (NULL, 0, 0004, "Error reading the input file", InputFileName);
  187. fclose (InputFile);
  188. free (*InputFileImage);
  189. *InputFileImage = NULL;
  190. return EFI_ABORTED;
  191. }
  192. //
  193. // Read all of the file contents.
  194. //
  195. *BytesRead = fread (*InputFileImage, sizeof (UINT8), FileSize, InputFile);
  196. if (*BytesRead != sizeof (UINT8) * FileSize) {
  197. Error (NULL, 0, 0004, "Error reading the input file", InputFileName);
  198. fclose (InputFile);
  199. free (*InputFileImage);
  200. *InputFileImage = NULL;
  201. return EFI_ABORTED;
  202. }
  203. //
  204. // Close the file
  205. //
  206. fclose (InputFile);
  207. return EFI_SUCCESS;
  208. }
  209. EFI_STATUS
  210. PutFileImage (
  211. IN CHAR8 *OutputFileName,
  212. IN CHAR8 *OutputFileImage,
  213. IN UINT32 BytesToWrite
  214. )
  215. /*++
  216. Routine Description:
  217. This function opens a file and writes OutputFileImage into the file.
  218. Arguments:
  219. OutputFileName The name of the file to write.
  220. OutputFileImage A pointer to the memory buffer.
  221. BytesToWrite The size of the memory buffer.
  222. Returns:
  223. EFI_SUCCESS The function completed successfully.
  224. EFI_INVALID_PARAMETER One of the input parameters was invalid.
  225. EFI_ABORTED An error occurred.
  226. EFI_OUT_OF_RESOURCES No resource to complete operations.
  227. --*/
  228. {
  229. FILE *OutputFile;
  230. UINT32 BytesWrote;
  231. //
  232. // Verify input parameters.
  233. //
  234. if (OutputFileName == NULL || strlen (OutputFileName) == 0 || OutputFileImage == NULL) {
  235. return EFI_INVALID_PARAMETER;
  236. }
  237. //
  238. // Open the file and copy contents into a memory buffer.
  239. //
  240. //
  241. // Open the file
  242. //
  243. OutputFile = fopen (LongFilePath (OutputFileName), "wb");
  244. if (OutputFile == NULL) {
  245. Error (NULL, 0, 0001, "Error opening the output file", OutputFileName);
  246. return EFI_ABORTED;
  247. }
  248. //
  249. // Write all of the file contents.
  250. //
  251. BytesWrote = fwrite (OutputFileImage, sizeof (UINT8), BytesToWrite, OutputFile);
  252. if (BytesWrote != sizeof (UINT8) * BytesToWrite) {
  253. Error (NULL, 0, 0002, "Error writing the output file", OutputFileName);
  254. fclose (OutputFile);
  255. return EFI_ABORTED;
  256. }
  257. //
  258. // Close the file
  259. //
  260. fclose (OutputFile);
  261. return EFI_SUCCESS;
  262. }
  263. UINT8
  264. CalculateChecksum8 (
  265. IN UINT8 *Buffer,
  266. IN UINTN Size
  267. )
  268. /*++
  269. Routine Description:
  270. This function calculates the value needed for a valid UINT8 checksum
  271. Arguments:
  272. Buffer Pointer to buffer containing byte data of component.
  273. Size Size of the buffer
  274. Returns:
  275. The 8 bit checksum value needed.
  276. --*/
  277. {
  278. return (UINT8) (0x100 - CalculateSum8 (Buffer, Size));
  279. }
  280. UINT8
  281. CalculateSum8 (
  282. IN UINT8 *Buffer,
  283. IN UINTN Size
  284. )
  285. /*++
  286. Routine Description::
  287. This function calculates the UINT8 sum for the requested region.
  288. Arguments:
  289. Buffer Pointer to buffer containing byte data of component.
  290. Size Size of the buffer
  291. Returns:
  292. The 8 bit checksum value needed.
  293. --*/
  294. {
  295. UINTN Index;
  296. UINT8 Sum;
  297. Sum = 0;
  298. //
  299. // Perform the byte sum for buffer
  300. //
  301. for (Index = 0; Index < Size; Index++) {
  302. Sum = (UINT8) (Sum + Buffer[Index]);
  303. }
  304. return Sum;
  305. }
  306. UINT16
  307. CalculateChecksum16 (
  308. IN UINT16 *Buffer,
  309. IN UINTN Size
  310. )
  311. /*++
  312. Routine Description::
  313. This function calculates the value needed for a valid UINT16 checksum
  314. Arguments:
  315. Buffer Pointer to buffer containing byte data of component.
  316. Size Size of the buffer
  317. Returns:
  318. The 16 bit checksum value needed.
  319. --*/
  320. {
  321. return (UINT16) (0x10000 - CalculateSum16 (Buffer, Size));
  322. }
  323. UINT16
  324. CalculateSum16 (
  325. IN UINT16 *Buffer,
  326. IN UINTN Size
  327. )
  328. /*++
  329. Routine Description:
  330. This function calculates the UINT16 sum for the requested region.
  331. Arguments:
  332. Buffer Pointer to buffer containing byte data of component.
  333. Size Size of the buffer
  334. Returns:
  335. The 16 bit checksum
  336. --*/
  337. {
  338. UINTN Index;
  339. UINT16 Sum;
  340. Sum = 0;
  341. //
  342. // Perform the word sum for buffer
  343. //
  344. for (Index = 0; Index < Size; Index++) {
  345. Sum = (UINT16) (Sum + Buffer[Index]);
  346. }
  347. return (UINT16) Sum;
  348. }
  349. EFI_STATUS
  350. PrintGuid (
  351. IN EFI_GUID *Guid
  352. )
  353. /*++
  354. Routine Description:
  355. This function prints a GUID to STDOUT.
  356. Arguments:
  357. Guid Pointer to a GUID to print.
  358. Returns:
  359. EFI_SUCCESS The GUID was printed.
  360. EFI_INVALID_PARAMETER The input was NULL.
  361. --*/
  362. {
  363. if (Guid == NULL) {
  364. Error (NULL, 0, 2000, "Invalid parameter", "PrintGuidToBuffer() called with a NULL value");
  365. return EFI_INVALID_PARAMETER;
  366. }
  367. printf (
  368. "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
  369. (unsigned) Guid->Data1,
  370. Guid->Data2,
  371. Guid->Data3,
  372. Guid->Data4[0],
  373. Guid->Data4[1],
  374. Guid->Data4[2],
  375. Guid->Data4[3],
  376. Guid->Data4[4],
  377. Guid->Data4[5],
  378. Guid->Data4[6],
  379. Guid->Data4[7]
  380. );
  381. return EFI_SUCCESS;
  382. }
  383. EFI_STATUS
  384. PrintGuidToBuffer (
  385. IN EFI_GUID *Guid,
  386. IN OUT UINT8 *Buffer,
  387. IN UINT32 BufferLen,
  388. IN BOOLEAN Uppercase
  389. )
  390. /*++
  391. Routine Description:
  392. This function prints a GUID to a buffer
  393. Arguments:
  394. Guid - Pointer to a GUID to print.
  395. Buffer - Pointer to a user-provided buffer to print to
  396. BufferLen - Size of the Buffer
  397. Uppercase - If use upper case.
  398. Returns:
  399. EFI_SUCCESS The GUID was printed.
  400. EFI_INVALID_PARAMETER The input was NULL.
  401. EFI_BUFFER_TOO_SMALL The input buffer was not big enough
  402. --*/
  403. {
  404. if (Guid == NULL) {
  405. Error (NULL, 0, 2000, "Invalid parameter", "PrintGuidToBuffer() called with a NULL value");
  406. return EFI_INVALID_PARAMETER;
  407. }
  408. if (BufferLen < PRINTED_GUID_BUFFER_SIZE) {
  409. Error (NULL, 0, 2000, "Invalid parameter", "PrintGuidToBuffer() called with invalid buffer size");
  410. return EFI_BUFFER_TOO_SMALL;
  411. }
  412. if (Uppercase) {
  413. sprintf (
  414. (CHAR8 *)Buffer,
  415. "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
  416. (unsigned) Guid->Data1,
  417. Guid->Data2,
  418. Guid->Data3,
  419. Guid->Data4[0],
  420. Guid->Data4[1],
  421. Guid->Data4[2],
  422. Guid->Data4[3],
  423. Guid->Data4[4],
  424. Guid->Data4[5],
  425. Guid->Data4[6],
  426. Guid->Data4[7]
  427. );
  428. } else {
  429. sprintf (
  430. (CHAR8 *)Buffer,
  431. "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  432. (unsigned) Guid->Data1,
  433. Guid->Data2,
  434. Guid->Data3,
  435. Guid->Data4[0],
  436. Guid->Data4[1],
  437. Guid->Data4[2],
  438. Guid->Data4[3],
  439. Guid->Data4[4],
  440. Guid->Data4[5],
  441. Guid->Data4[6],
  442. Guid->Data4[7]
  443. );
  444. }
  445. return EFI_SUCCESS;
  446. }
  447. #ifdef __GNUC__
  448. size_t _filelength(int fd)
  449. {
  450. struct stat stat_buf;
  451. fstat(fd, &stat_buf);
  452. return stat_buf.st_size;
  453. }
  454. #ifndef __CYGWIN__
  455. char *strlwr(char *s)
  456. {
  457. char *p = s;
  458. for(;*s;s++) {
  459. *s = tolower(*s);
  460. }
  461. return p;
  462. }
  463. #endif
  464. #endif
  465. #define WINDOWS_EXTENSION_PATH "\\\\?\\"
  466. #define WINDOWS_UNC_EXTENSION_PATH "\\\\?\\UNC"
  467. //
  468. // Global data to store full file path. It is not required to be free.
  469. //
  470. CHAR8 mCommonLibFullPath[MAX_LONG_FILE_PATH];
  471. CHAR8 *
  472. LongFilePath (
  473. IN CHAR8 *FileName
  474. )
  475. /*++
  476. Routine Description:
  477. Convert FileName to the long file path, which can support larger than 260 length.
  478. Arguments:
  479. FileName - FileName.
  480. Returns:
  481. LongFilePath A pointer to the converted long file path.
  482. --*/
  483. {
  484. #ifdef __GNUC__
  485. //
  486. // __GNUC__ may not be good way to differentiate unix and windows. Need more investigation here.
  487. // unix has no limitation on file path. Just return FileName.
  488. //
  489. return FileName;
  490. #else
  491. CHAR8 *RootPath;
  492. CHAR8 *PathPointer;
  493. CHAR8 *NextPointer;
  494. PathPointer = (CHAR8 *) FileName;
  495. if (FileName != NULL) {
  496. //
  497. // Add the extension string first to support long file path.
  498. //
  499. mCommonLibFullPath[0] = 0;
  500. strcpy (mCommonLibFullPath, WINDOWS_EXTENSION_PATH);
  501. if (strlen (FileName) > 1 && FileName[0] == '\\' && FileName[1] == '\\') {
  502. //
  503. // network path like \\server\share to \\?\UNC\server\share
  504. //
  505. strcpy (mCommonLibFullPath, WINDOWS_UNC_EXTENSION_PATH);
  506. FileName ++;
  507. } else if (strlen (FileName) < 3 || FileName[1] != ':' || (FileName[2] != '\\' && FileName[2] != '/')) {
  508. //
  509. // Relative file path. Convert it to absolute path.
  510. //
  511. RootPath = getcwd (NULL, 0);
  512. if (RootPath != NULL) {
  513. if (strlen (mCommonLibFullPath) + strlen (RootPath) > MAX_LONG_FILE_PATH - 1) {
  514. Error (NULL, 0, 2000, "Invalid parameter", "RootPath is too long!");
  515. free (RootPath);
  516. return NULL;
  517. }
  518. strncat (mCommonLibFullPath, RootPath, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);
  519. if (FileName[0] != '\\' && FileName[0] != '/') {
  520. if (strlen (mCommonLibFullPath) + 1 > MAX_LONG_FILE_PATH - 1) {
  521. Error (NULL, 0, 2000, "Invalid parameter", "RootPath is too long!");
  522. free (RootPath);
  523. return NULL;
  524. }
  525. //
  526. // Attach directory separator
  527. //
  528. strncat (mCommonLibFullPath, "\\", MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);
  529. }
  530. free (RootPath);
  531. }
  532. }
  533. //
  534. // Construct the full file path
  535. //
  536. if (strlen (mCommonLibFullPath) + strlen (FileName) > MAX_LONG_FILE_PATH - 1) {
  537. Error (NULL, 0, 2000, "Invalid parameter", "FileName %s is too long!", FileName);
  538. return NULL;
  539. }
  540. strncat (mCommonLibFullPath, FileName, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);
  541. //
  542. // Convert directory separator '/' to '\\'
  543. //
  544. PathPointer = (CHAR8 *) mCommonLibFullPath;
  545. do {
  546. if (*PathPointer == '/') {
  547. *PathPointer = '\\';
  548. }
  549. } while (*PathPointer ++ != '\0');
  550. //
  551. // Convert ":\\\\" to ":\\", because it doesn't work with WINDOWS_EXTENSION_PATH.
  552. //
  553. if ((PathPointer = strstr (mCommonLibFullPath, ":\\\\")) != NULL) {
  554. *(PathPointer + 2) = '\0';
  555. strncat (mCommonLibFullPath, PathPointer + 3, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);
  556. }
  557. //
  558. // Convert ".\\" to "", because it doesn't work with WINDOWS_EXTENSION_PATH.
  559. //
  560. while ((PathPointer = strstr (mCommonLibFullPath, ".\\")) != NULL) {
  561. *PathPointer = '\0';
  562. strncat (mCommonLibFullPath, PathPointer + 2, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);
  563. }
  564. //
  565. // Convert "\\.\\" to "\\", because it doesn't work with WINDOWS_EXTENSION_PATH.
  566. //
  567. while ((PathPointer = strstr (mCommonLibFullPath, "\\.\\")) != NULL) {
  568. *PathPointer = '\0';
  569. strncat (mCommonLibFullPath, PathPointer + 2, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);
  570. }
  571. //
  572. // Convert "\\..\\" to last directory, because it doesn't work with WINDOWS_EXTENSION_PATH.
  573. //
  574. while ((PathPointer = strstr (mCommonLibFullPath, "\\..\\")) != NULL) {
  575. NextPointer = PathPointer + 3;
  576. do {
  577. PathPointer --;
  578. } while (PathPointer > mCommonLibFullPath && *PathPointer != ':' && *PathPointer != '\\');
  579. if (*PathPointer == '\\') {
  580. //
  581. // Skip one directory
  582. //
  583. *PathPointer = '\0';
  584. strncat (mCommonLibFullPath, NextPointer, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);
  585. } else {
  586. //
  587. // No directory is found. Just break.
  588. //
  589. break;
  590. }
  591. }
  592. PathPointer = mCommonLibFullPath;
  593. }
  594. return PathPointer;
  595. #endif
  596. }
  597. CHAR16
  598. InternalCharToUpper (
  599. CHAR16 Char
  600. )
  601. {
  602. if (Char >= L'a' && Char <= L'z') {
  603. return (CHAR16) (Char - (L'a' - L'A'));
  604. }
  605. return Char;
  606. }
  607. UINTN
  608. StrnLenS (
  609. CONST CHAR16 *String,
  610. UINTN MaxSize
  611. )
  612. {
  613. UINTN Length;
  614. ASSERT (((UINTN) String & BIT0) == 0);
  615. //
  616. // If String is a null pointer or MaxSize is 0, then the StrnLenS function returns zero.
  617. //
  618. if ((String == NULL) || (MaxSize == 0)) {
  619. return 0;
  620. }
  621. Length = 0;
  622. while (String[Length] != 0) {
  623. if (Length >= MaxSize - 1) {
  624. return MaxSize;
  625. }
  626. Length++;
  627. }
  628. return Length;
  629. }
  630. VOID *
  631. InternalAllocatePool (
  632. UINTN AllocationSize
  633. )
  634. {
  635. VOID * Memory;
  636. Memory = malloc(AllocationSize);
  637. ASSERT(Memory != NULL);
  638. return Memory;
  639. }
  640. VOID *
  641. InternalReallocatePool (
  642. UINTN OldSize,
  643. UINTN NewSize,
  644. VOID *OldBuffer OPTIONAL
  645. )
  646. {
  647. VOID *NewBuffer;
  648. NewBuffer = AllocateZeroPool (NewSize);
  649. if (NewBuffer != NULL && OldBuffer != NULL) {
  650. memcpy (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
  651. free(OldBuffer);
  652. }
  653. return NewBuffer;
  654. }
  655. VOID *
  656. ReallocatePool (
  657. UINTN OldSize,
  658. UINTN NewSize,
  659. VOID *OldBuffer OPTIONAL
  660. )
  661. {
  662. return InternalReallocatePool (OldSize, NewSize, OldBuffer);
  663. }
  664. /**
  665. Returns the length of a Null-terminated Unicode string.
  666. This function returns the number of Unicode characters in the Null-terminated
  667. Unicode string specified by String.
  668. If String is NULL, then ASSERT().
  669. If String is not aligned on a 16-bit boundary, then ASSERT().
  670. If PcdMaximumUnicodeStringLength is not zero, and String contains more than
  671. PcdMaximumUnicodeStringLength Unicode characters, not including the
  672. Null-terminator, then ASSERT().
  673. @param String A pointer to a Null-terminated Unicode string.
  674. @return The length of String.
  675. **/
  676. UINTN
  677. StrLen (
  678. CONST CHAR16 *String
  679. )
  680. {
  681. UINTN Length;
  682. ASSERT (String != NULL);
  683. ASSERT (((UINTN) String & BIT0) == 0);
  684. for (Length = 0; *String != L'\0'; String++, Length++) {
  685. //
  686. // If PcdMaximumUnicodeStringLength is not zero,
  687. // length should not more than PcdMaximumUnicodeStringLength
  688. //
  689. }
  690. return Length;
  691. }
  692. BOOLEAN
  693. InternalSafeStringIsOverlap (
  694. IN VOID *Base1,
  695. IN UINTN Size1,
  696. IN VOID *Base2,
  697. IN UINTN Size2
  698. )
  699. {
  700. if ((((UINTN)Base1 >= (UINTN)Base2) && ((UINTN)Base1 < (UINTN)Base2 + Size2)) ||
  701. (((UINTN)Base2 >= (UINTN)Base1) && ((UINTN)Base2 < (UINTN)Base1 + Size1))) {
  702. return TRUE;
  703. }
  704. return FALSE;
  705. }
  706. BOOLEAN
  707. InternalSafeStringNoStrOverlap (
  708. IN CHAR16 *Str1,
  709. IN UINTN Size1,
  710. IN CHAR16 *Str2,
  711. IN UINTN Size2
  712. )
  713. {
  714. return !InternalSafeStringIsOverlap (Str1, Size1 * sizeof(CHAR16), Str2, Size2 * sizeof(CHAR16));
  715. }
  716. /**
  717. Convert a Null-terminated Unicode decimal string to a value of type UINT64.
  718. This function outputs a value of type UINT64 by interpreting the contents of
  719. the Unicode string specified by String as a decimal number. The format of the
  720. input Unicode string String is:
  721. [spaces] [decimal digits].
  722. The valid decimal digit character is in the range [0-9]. The function will
  723. ignore the pad space, which includes spaces or tab characters, before
  724. [decimal digits]. The running zero in the beginning of [decimal digits] will
  725. be ignored. Then, the function stops at the first character that is a not a
  726. valid decimal character or a Null-terminator, whichever one comes first.
  727. If String is NULL, then ASSERT().
  728. If Data is NULL, then ASSERT().
  729. If String is not aligned in a 16-bit boundary, then ASSERT().
  730. If PcdMaximumUnicodeStringLength is not zero, and String contains more than
  731. PcdMaximumUnicodeStringLength Unicode characters, not including the
  732. Null-terminator, then ASSERT().
  733. If String has no valid decimal digits in the above format, then 0 is stored
  734. at the location pointed to by Data.
  735. If the number represented by String exceeds the range defined by UINT64, then
  736. MAX_UINT64 is stored at the location pointed to by Data.
  737. If EndPointer is not NULL, a pointer to the character that stopped the scan
  738. is stored at the location pointed to by EndPointer. If String has no valid
  739. decimal digits right after the optional pad spaces, the value of String is
  740. stored at the location pointed to by EndPointer.
  741. @param String Pointer to a Null-terminated Unicode string.
  742. @param EndPointer Pointer to character that stops scan.
  743. @param Data Pointer to the converted value.
  744. @retval RETURN_SUCCESS Value is translated from String.
  745. @retval RETURN_INVALID_PARAMETER If String is NULL.
  746. If Data is NULL.
  747. If PcdMaximumUnicodeStringLength is not
  748. zero, and String contains more than
  749. PcdMaximumUnicodeStringLength Unicode
  750. characters, not including the
  751. Null-terminator.
  752. @retval RETURN_UNSUPPORTED If the number represented by String exceeds
  753. the range defined by UINT64.
  754. **/
  755. RETURN_STATUS
  756. StrDecimalToUint64S (
  757. CONST CHAR16 *String,
  758. CHAR16 **EndPointer, OPTIONAL
  759. UINT64 *Data
  760. )
  761. {
  762. ASSERT (((UINTN) String & BIT0) == 0);
  763. //
  764. // 1. Neither String nor Data shall be a null pointer.
  765. //
  766. SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
  767. SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);
  768. //
  769. // 2. The length of String shall not be greater than RSIZE_MAX.
  770. //
  771. if (RSIZE_MAX != 0) {
  772. SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String, RSIZE_MAX + 1) <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
  773. }
  774. if (EndPointer != NULL) {
  775. *EndPointer = (CHAR16 *) String;
  776. }
  777. //
  778. // Ignore the pad spaces (space or tab)
  779. //
  780. while ((*String == L' ') || (*String == L'\t')) {
  781. String++;
  782. }
  783. //
  784. // Ignore leading Zeros after the spaces
  785. //
  786. while (*String == L'0') {
  787. String++;
  788. }
  789. *Data = 0;
  790. while (InternalIsDecimalDigitCharacter (*String)) {
  791. //
  792. // If the number represented by String overflows according to the range
  793. // defined by UINT64, then MAX_UINT64 is stored in *Data and
  794. // RETURN_UNSUPPORTED is returned.
  795. //
  796. if (*Data > ((MAX_UINT64 - (*String - L'0'))/10)) {
  797. *Data = MAX_UINT64;
  798. if (EndPointer != NULL) {
  799. *EndPointer = (CHAR16 *) String;
  800. }
  801. return RETURN_UNSUPPORTED;
  802. }
  803. *Data = (*Data) * 10 + (*String - L'0');
  804. String++;
  805. }
  806. if (EndPointer != NULL) {
  807. *EndPointer = (CHAR16 *) String;
  808. }
  809. return RETURN_SUCCESS;
  810. }
  811. /**
  812. Convert a Null-terminated Unicode hexadecimal string to a value of type
  813. UINT64.
  814. This function outputs a value of type UINT64 by interpreting the contents of
  815. the Unicode string specified by String as a hexadecimal number. The format of
  816. the input Unicode string String is:
  817. [spaces][zeros][x][hexadecimal digits].
  818. The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
  819. The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
  820. If "x" appears in the input string, it must be prefixed with at least one 0.
  821. The function will ignore the pad space, which includes spaces or tab
  822. characters, before [zeros], [x] or [hexadecimal digit]. The running zero
  823. before [x] or [hexadecimal digit] will be ignored. Then, the decoding starts
  824. after [x] or the first valid hexadecimal digit. Then, the function stops at
  825. the first character that is a not a valid hexadecimal character or NULL,
  826. whichever one comes first.
  827. If String is NULL, then ASSERT().
  828. If Data is NULL, then ASSERT().
  829. If String is not aligned in a 16-bit boundary, then ASSERT().
  830. If PcdMaximumUnicodeStringLength is not zero, and String contains more than
  831. PcdMaximumUnicodeStringLength Unicode characters, not including the
  832. Null-terminator, then ASSERT().
  833. If String has no valid hexadecimal digits in the above format, then 0 is
  834. stored at the location pointed to by Data.
  835. If the number represented by String exceeds the range defined by UINT64, then
  836. MAX_UINT64 is stored at the location pointed to by Data.
  837. If EndPointer is not NULL, a pointer to the character that stopped the scan
  838. is stored at the location pointed to by EndPointer. If String has no valid
  839. hexadecimal digits right after the optional pad spaces, the value of String
  840. is stored at the location pointed to by EndPointer.
  841. @param String Pointer to a Null-terminated Unicode string.
  842. @param EndPointer Pointer to character that stops scan.
  843. @param Data Pointer to the converted value.
  844. @retval RETURN_SUCCESS Value is translated from String.
  845. @retval RETURN_INVALID_PARAMETER If String is NULL.
  846. If Data is NULL.
  847. If PcdMaximumUnicodeStringLength is not
  848. zero, and String contains more than
  849. PcdMaximumUnicodeStringLength Unicode
  850. characters, not including the
  851. Null-terminator.
  852. @retval RETURN_UNSUPPORTED If the number represented by String exceeds
  853. the range defined by UINT64.
  854. **/
  855. RETURN_STATUS
  856. StrHexToUint64S (
  857. CONST CHAR16 *String,
  858. CHAR16 **EndPointer, OPTIONAL
  859. UINT64 *Data
  860. )
  861. {
  862. ASSERT (((UINTN) String & BIT0) == 0);
  863. //
  864. // 1. Neither String nor Data shall be a null pointer.
  865. //
  866. SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
  867. SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);
  868. //
  869. // 2. The length of String shall not be greater than RSIZE_MAX.
  870. //
  871. if (RSIZE_MAX != 0) {
  872. SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String, RSIZE_MAX + 1) <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
  873. }
  874. if (EndPointer != NULL) {
  875. *EndPointer = (CHAR16 *) String;
  876. }
  877. //
  878. // Ignore the pad spaces (space or tab)
  879. //
  880. while ((*String == L' ') || (*String == L'\t')) {
  881. String++;
  882. }
  883. //
  884. // Ignore leading Zeros after the spaces
  885. //
  886. while (*String == L'0') {
  887. String++;
  888. }
  889. if (InternalCharToUpper (*String) == L'X') {
  890. if (*(String - 1) != L'0') {
  891. *Data = 0;
  892. return RETURN_SUCCESS;
  893. }
  894. //
  895. // Skip the 'X'
  896. //
  897. String++;
  898. }
  899. *Data = 0;
  900. while (InternalIsHexaDecimalDigitCharacter (*String)) {
  901. //
  902. // If the number represented by String overflows according to the range
  903. // defined by UINT64, then MAX_UINT64 is stored in *Data and
  904. // RETURN_UNSUPPORTED is returned.
  905. //
  906. if (*Data > ((MAX_UINT64 - InternalHexCharToUintn (*String))>>4)) {
  907. *Data = MAX_UINT64;
  908. if (EndPointer != NULL) {
  909. *EndPointer = (CHAR16 *) String;
  910. }
  911. return RETURN_UNSUPPORTED;
  912. }
  913. *Data = ((*Data) << 4) + InternalHexCharToUintn (*String);
  914. String++;
  915. }
  916. if (EndPointer != NULL) {
  917. *EndPointer = (CHAR16 *) String;
  918. }
  919. return RETURN_SUCCESS;
  920. }
  921. UINT64
  922. StrDecimalToUint64 (
  923. CONST CHAR16 *String
  924. )
  925. {
  926. UINT64 Result;
  927. StrDecimalToUint64S (String, (CHAR16 **) NULL, &Result);
  928. return Result;
  929. }
  930. UINT64
  931. StrHexToUint64 (
  932. CONST CHAR16 *String
  933. )
  934. {
  935. UINT64 Result;
  936. StrHexToUint64S (String, (CHAR16 **) NULL, &Result);
  937. return Result;
  938. }
  939. UINTN
  940. StrSize (
  941. CONST CHAR16 *String
  942. )
  943. {
  944. return (StrLen (String) + 1) * sizeof (*String);
  945. }
  946. UINT64
  947. ReadUnaligned64 (
  948. CONST UINT64 *Buffer
  949. )
  950. {
  951. ASSERT (Buffer != NULL);
  952. return *Buffer;
  953. }
  954. UINT64
  955. WriteUnaligned64 (
  956. UINT64 *Buffer,
  957. UINT64 Value
  958. )
  959. {
  960. ASSERT (Buffer != NULL);
  961. return *Buffer = Value;
  962. }
  963. EFI_GUID *
  964. CopyGuid (
  965. EFI_GUID *DestinationGuid,
  966. CONST EFI_GUID *SourceGuid
  967. )
  968. {
  969. WriteUnaligned64 (
  970. (UINT64*)DestinationGuid,
  971. ReadUnaligned64 ((CONST UINT64*)SourceGuid)
  972. );
  973. WriteUnaligned64 (
  974. (UINT64*)DestinationGuid + 1,
  975. ReadUnaligned64 ((CONST UINT64*)SourceGuid + 1)
  976. );
  977. return DestinationGuid;
  978. }
  979. UINT16
  980. SwapBytes16 (
  981. UINT16 Value
  982. )
  983. {
  984. return (UINT16) ((Value<< 8) | (Value>> 8));
  985. }
  986. UINT32
  987. SwapBytes32 (
  988. UINT32 Value
  989. )
  990. {
  991. UINT32 LowerBytes;
  992. UINT32 HigherBytes;
  993. LowerBytes = (UINT32) SwapBytes16 ((UINT16) Value);
  994. HigherBytes = (UINT32) SwapBytes16 ((UINT16) (Value >> 16));
  995. return (LowerBytes << 16 | HigherBytes);
  996. }
  997. BOOLEAN
  998. InternalIsDecimalDigitCharacter (
  999. CHAR16 Char
  1000. )
  1001. {
  1002. return (BOOLEAN) (Char >= L'0' && Char <= L'9');
  1003. }
  1004. VOID *
  1005. InternalAllocateCopyPool (
  1006. UINTN AllocationSize,
  1007. CONST VOID *Buffer
  1008. )
  1009. {
  1010. VOID *Memory;
  1011. ASSERT (Buffer != NULL);
  1012. Memory = malloc (AllocationSize);
  1013. if (Memory != NULL) {
  1014. Memory = memcpy (Memory, Buffer, AllocationSize);
  1015. }
  1016. return Memory;
  1017. }
  1018. BOOLEAN
  1019. InternalIsHexaDecimalDigitCharacter (
  1020. CHAR16 Char
  1021. )
  1022. {
  1023. return (BOOLEAN) (InternalIsDecimalDigitCharacter (Char) ||
  1024. (Char >= L'A' && Char <= L'F') ||
  1025. (Char >= L'a' && Char <= L'f'));
  1026. }
  1027. UINTN
  1028. InternalHexCharToUintn (
  1029. CHAR16 Char
  1030. )
  1031. {
  1032. if (InternalIsDecimalDigitCharacter (Char)) {
  1033. return Char - L'0';
  1034. }
  1035. return (10 + InternalCharToUpper (Char) - L'A');
  1036. }
  1037. /**
  1038. Convert a Null-terminated Unicode hexadecimal string to a byte array.
  1039. This function outputs a byte array by interpreting the contents of
  1040. the Unicode string specified by String in hexadecimal format. The format of
  1041. the input Unicode string String is:
  1042. [XX]*
  1043. X is a hexadecimal digit character in the range [0-9], [a-f] and [A-F].
  1044. The function decodes every two hexadecimal digit characters as one byte. The
  1045. decoding stops after Length of characters and outputs Buffer containing
  1046. (Length / 2) bytes.
  1047. If String is not aligned in a 16-bit boundary, then ASSERT().
  1048. If String is NULL, then ASSERT().
  1049. If Buffer is NULL, then ASSERT().
  1050. If Length is not multiple of 2, then ASSERT().
  1051. If PcdMaximumUnicodeStringLength is not zero and Length is greater than
  1052. PcdMaximumUnicodeStringLength, then ASSERT().
  1053. If MaxBufferSize is less than (Length / 2), then ASSERT().
  1054. @param String Pointer to a Null-terminated Unicode string.
  1055. @param Length The number of Unicode characters to decode.
  1056. @param Buffer Pointer to the converted bytes array.
  1057. @param MaxBufferSize The maximum size of Buffer.
  1058. @retval RETURN_SUCCESS Buffer is translated from String.
  1059. @retval RETURN_INVALID_PARAMETER If String is NULL.
  1060. If Data is NULL.
  1061. If Length is not multiple of 2.
  1062. If PcdMaximumUnicodeStringLength is not zero,
  1063. and Length is greater than
  1064. PcdMaximumUnicodeStringLength.
  1065. @retval RETURN_UNSUPPORTED If Length of characters from String contain
  1066. a character that is not valid hexadecimal
  1067. digit characters, or a Null-terminator.
  1068. @retval RETURN_BUFFER_TOO_SMALL If MaxBufferSize is less than (Length / 2).
  1069. **/
  1070. RETURN_STATUS
  1071. StrHexToBytes (
  1072. CONST CHAR16 *String,
  1073. UINTN Length,
  1074. UINT8 *Buffer,
  1075. UINTN MaxBufferSize
  1076. )
  1077. {
  1078. UINTN Index;
  1079. ASSERT (((UINTN) String & BIT0) == 0);
  1080. //
  1081. // 1. None of String or Buffer shall be a null pointer.
  1082. //
  1083. SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
  1084. SAFE_STRING_CONSTRAINT_CHECK ((Buffer != NULL), RETURN_INVALID_PARAMETER);
  1085. //
  1086. // 2. Length shall not be greater than RSIZE_MAX.
  1087. //
  1088. if (RSIZE_MAX != 0) {
  1089. SAFE_STRING_CONSTRAINT_CHECK ((Length <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
  1090. }
  1091. //
  1092. // 3. Length shall not be odd.
  1093. //
  1094. SAFE_STRING_CONSTRAINT_CHECK (((Length & BIT0) == 0), RETURN_INVALID_PARAMETER);
  1095. //
  1096. // 4. MaxBufferSize shall equal to or greater than Length / 2.
  1097. //
  1098. SAFE_STRING_CONSTRAINT_CHECK ((MaxBufferSize >= Length / 2), RETURN_BUFFER_TOO_SMALL);
  1099. //
  1100. // 5. String shall not contains invalid hexadecimal digits.
  1101. //
  1102. for (Index = 0; Index < Length; Index++) {
  1103. if (!InternalIsHexaDecimalDigitCharacter (String[Index])) {
  1104. break;
  1105. }
  1106. }
  1107. if (Index != Length) {
  1108. return RETURN_UNSUPPORTED;
  1109. }
  1110. //
  1111. // Convert the hex string to bytes.
  1112. //
  1113. for(Index = 0; Index < Length; Index++) {
  1114. //
  1115. // For even characters, write the upper nibble for each buffer byte,
  1116. // and for even characters, the lower nibble.
  1117. //
  1118. if ((Index & BIT0) == 0) {
  1119. Buffer[Index / 2] = (UINT8) InternalHexCharToUintn (String[Index]) << 4;
  1120. } else {
  1121. Buffer[Index / 2] |= (UINT8) InternalHexCharToUintn (String[Index]);
  1122. }
  1123. }
  1124. return RETURN_SUCCESS;
  1125. }
  1126. /**
  1127. Convert a Null-terminated Unicode GUID string to a value of type
  1128. EFI_GUID.
  1129. This function outputs a GUID value by interpreting the contents of
  1130. the Unicode string specified by String. The format of the input
  1131. Unicode string String consists of 36 characters, as follows:
  1132. aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
  1133. The pairs aa - pp are two characters in the range [0-9], [a-f] and
  1134. [A-F], with each pair representing a single byte hexadecimal value.
  1135. The mapping between String and the EFI_GUID structure is as follows:
  1136. aa Data1[24:31]
  1137. bb Data1[16:23]
  1138. cc Data1[8:15]
  1139. dd Data1[0:7]
  1140. ee Data2[8:15]
  1141. ff Data2[0:7]
  1142. gg Data3[8:15]
  1143. hh Data3[0:7]
  1144. ii Data4[0:7]
  1145. jj Data4[8:15]
  1146. kk Data4[16:23]
  1147. ll Data4[24:31]
  1148. mm Data4[32:39]
  1149. nn Data4[40:47]
  1150. oo Data4[48:55]
  1151. pp Data4[56:63]
  1152. If String is NULL, then ASSERT().
  1153. If Guid is NULL, then ASSERT().
  1154. If String is not aligned in a 16-bit boundary, then ASSERT().
  1155. @param String Pointer to a Null-terminated Unicode string.
  1156. @param Guid Pointer to the converted GUID.
  1157. @retval RETURN_SUCCESS Guid is translated from String.
  1158. @retval RETURN_INVALID_PARAMETER If String is NULL.
  1159. If Data is NULL.
  1160. @retval RETURN_UNSUPPORTED If String is not as the above format.
  1161. **/
  1162. RETURN_STATUS
  1163. StrToGuid (
  1164. CONST CHAR16 *String,
  1165. EFI_GUID *Guid
  1166. )
  1167. {
  1168. RETURN_STATUS Status;
  1169. EFI_GUID LocalGuid;
  1170. ASSERT (((UINTN) String & BIT0) == 0);
  1171. //
  1172. // 1. None of String or Guid shall be a null pointer.
  1173. //
  1174. SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
  1175. SAFE_STRING_CONSTRAINT_CHECK ((Guid != NULL), RETURN_INVALID_PARAMETER);
  1176. //
  1177. // Get aabbccdd in big-endian.
  1178. //
  1179. Status = StrHexToBytes (String, 2 * sizeof (LocalGuid.Data1), (UINT8 *) &LocalGuid.Data1, sizeof (LocalGuid.Data1));
  1180. if (RETURN_ERROR (Status) || String[2 * sizeof (LocalGuid.Data1)] != L'-') {
  1181. return RETURN_UNSUPPORTED;
  1182. }
  1183. //
  1184. // Convert big-endian to little-endian.
  1185. //
  1186. LocalGuid.Data1 = SwapBytes32 (LocalGuid.Data1);
  1187. String += 2 * sizeof (LocalGuid.Data1) + 1;
  1188. //
  1189. // Get eeff in big-endian.
  1190. //
  1191. Status = StrHexToBytes (String, 2 * sizeof (LocalGuid.Data2), (UINT8 *) &LocalGuid.Data2, sizeof (LocalGuid.Data2));
  1192. if (RETURN_ERROR (Status) || String[2 * sizeof (LocalGuid.Data2)] != L'-') {
  1193. return RETURN_UNSUPPORTED;
  1194. }
  1195. //
  1196. // Convert big-endian to little-endian.
  1197. //
  1198. LocalGuid.Data2 = SwapBytes16 (LocalGuid.Data2);
  1199. String += 2 * sizeof (LocalGuid.Data2) + 1;
  1200. //
  1201. // Get gghh in big-endian.
  1202. //
  1203. Status = StrHexToBytes (String, 2 * sizeof (LocalGuid.Data3), (UINT8 *) &LocalGuid.Data3, sizeof (LocalGuid.Data3));
  1204. if (RETURN_ERROR (Status) || String[2 * sizeof (LocalGuid.Data3)] != L'-') {
  1205. return RETURN_UNSUPPORTED;
  1206. }
  1207. //
  1208. // Convert big-endian to little-endian.
  1209. //
  1210. LocalGuid.Data3 = SwapBytes16 (LocalGuid.Data3);
  1211. String += 2 * sizeof (LocalGuid.Data3) + 1;
  1212. //
  1213. // Get iijj.
  1214. //
  1215. Status = StrHexToBytes (String, 2 * 2, &LocalGuid.Data4[0], 2);
  1216. if (RETURN_ERROR (Status) || String[2 * 2] != L'-') {
  1217. return RETURN_UNSUPPORTED;
  1218. }
  1219. String += 2 * 2 + 1;
  1220. //
  1221. // Get kkllmmnnoopp.
  1222. //
  1223. Status = StrHexToBytes (String, 2 * 6, &LocalGuid.Data4[2], 6);
  1224. if (RETURN_ERROR (Status)) {
  1225. return RETURN_UNSUPPORTED;
  1226. }
  1227. CopyGuid (Guid, &LocalGuid);
  1228. return RETURN_SUCCESS;
  1229. }
  1230. /**
  1231. Compares up to a specified length the contents of two Null-terminated Unicode strings,
  1232. and returns the difference between the first mismatched Unicode characters.
  1233. This function compares the Null-terminated Unicode string FirstString to the
  1234. Null-terminated Unicode string SecondString. At most, Length Unicode
  1235. characters will be compared. If Length is 0, then 0 is returned. If
  1236. FirstString is identical to SecondString, then 0 is returned. Otherwise, the
  1237. value returned is the first mismatched Unicode character in SecondString
  1238. subtracted from the first mismatched Unicode character in FirstString.
  1239. If Length > 0 and FirstString is NULL, then ASSERT().
  1240. If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().
  1241. If Length > 0 and SecondString is NULL, then ASSERT().
  1242. If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().
  1243. If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
  1244. PcdMaximumUnicodeStringLength, then ASSERT().
  1245. If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more than
  1246. PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
  1247. then ASSERT().
  1248. If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more than
  1249. PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
  1250. then ASSERT().
  1251. @param FirstString A pointer to a Null-terminated Unicode string.
  1252. @param SecondString A pointer to a Null-terminated Unicode string.
  1253. @param Length The maximum number of Unicode characters to compare.
  1254. @retval 0 FirstString is identical to SecondString.
  1255. @return others FirstString is not identical to SecondString.
  1256. **/
  1257. INTN
  1258. StrnCmp (
  1259. CONST CHAR16 *FirstString,
  1260. CONST CHAR16 *SecondString,
  1261. UINTN Length
  1262. )
  1263. {
  1264. if (Length == 0) {
  1265. return 0;
  1266. }
  1267. //
  1268. // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
  1269. // Length tests are performed inside StrLen().
  1270. //
  1271. ASSERT (StrSize (FirstString) != 0);
  1272. ASSERT (StrSize (SecondString) != 0);
  1273. while ((*FirstString != L'\0') &&
  1274. (*SecondString != L'\0') &&
  1275. (*FirstString == *SecondString) &&
  1276. (Length > 1)) {
  1277. FirstString++;
  1278. SecondString++;
  1279. Length--;
  1280. }
  1281. return *FirstString - *SecondString;
  1282. }
  1283. VOID *
  1284. AllocateCopyPool (
  1285. UINTN AllocationSize,
  1286. CONST VOID *Buffer
  1287. )
  1288. {
  1289. return InternalAllocateCopyPool (AllocationSize, Buffer);
  1290. }
  1291. INTN
  1292. StrCmp (
  1293. CONST CHAR16 *FirstString,
  1294. CONST CHAR16 *SecondString
  1295. )
  1296. {
  1297. //
  1298. // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
  1299. //
  1300. ASSERT (StrSize (FirstString) != 0);
  1301. ASSERT (StrSize (SecondString) != 0);
  1302. while ((*FirstString != L'\0') && (*FirstString == *SecondString)) {
  1303. FirstString++;
  1304. SecondString++;
  1305. }
  1306. return *FirstString - *SecondString;
  1307. }
  1308. UINT64
  1309. SwapBytes64 (
  1310. UINT64 Value
  1311. )
  1312. {
  1313. return InternalMathSwapBytes64 (Value);
  1314. }
  1315. UINT64
  1316. InternalMathSwapBytes64 (
  1317. UINT64 Operand
  1318. )
  1319. {
  1320. UINT64 LowerBytes;
  1321. UINT64 HigherBytes;
  1322. LowerBytes = (UINT64) SwapBytes32 ((UINT32) Operand);
  1323. HigherBytes = (UINT64) SwapBytes32 ((UINT32) (Operand >> 32));
  1324. return (LowerBytes << 32 | HigherBytes);
  1325. }
  1326. RETURN_STATUS
  1327. StrToIpv4Address (
  1328. CONST CHAR16 *String,
  1329. CHAR16 **EndPointer,
  1330. EFI_IPv4_ADDRESS *Address,
  1331. UINT8 *PrefixLength
  1332. )
  1333. {
  1334. RETURN_STATUS Status;
  1335. UINTN AddressIndex;
  1336. UINT64 Uint64;
  1337. EFI_IPv4_ADDRESS LocalAddress;
  1338. UINT8 LocalPrefixLength;
  1339. CHAR16 *Pointer;
  1340. LocalPrefixLength = MAX_UINT8;
  1341. LocalAddress.Addr[0] = 0;
  1342. ASSERT (((UINTN) String & BIT0) == 0);
  1343. //
  1344. // 1. None of String or Guid shall be a null pointer.
  1345. //
  1346. SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
  1347. SAFE_STRING_CONSTRAINT_CHECK ((Address != NULL), RETURN_INVALID_PARAMETER);
  1348. for (Pointer = (CHAR16 *) String, AddressIndex = 0; AddressIndex < ARRAY_SIZE (Address->Addr) + 1;) {
  1349. if (!InternalIsDecimalDigitCharacter (*Pointer)) {
  1350. //
  1351. // D or P contains invalid characters.
  1352. //
  1353. break;
  1354. }
  1355. //
  1356. // Get D or P.
  1357. //
  1358. Status = StrDecimalToUint64S ((CONST CHAR16 *) Pointer, &Pointer, &Uint64);
  1359. if (RETURN_ERROR (Status)) {
  1360. return RETURN_UNSUPPORTED;
  1361. }
  1362. if (AddressIndex == ARRAY_SIZE (Address->Addr)) {
  1363. //
  1364. // It's P.
  1365. //
  1366. if (Uint64 > 32) {
  1367. return RETURN_UNSUPPORTED;
  1368. }
  1369. LocalPrefixLength = (UINT8) Uint64;
  1370. } else {
  1371. //
  1372. // It's D.
  1373. //
  1374. if (Uint64 > MAX_UINT8) {
  1375. return RETURN_UNSUPPORTED;
  1376. }
  1377. LocalAddress.Addr[AddressIndex] = (UINT8) Uint64;
  1378. AddressIndex++;
  1379. }
  1380. //
  1381. // Check the '.' or '/', depending on the AddressIndex.
  1382. //
  1383. if (AddressIndex == ARRAY_SIZE (Address->Addr)) {
  1384. if (*Pointer == L'/') {
  1385. //
  1386. // '/P' is in the String.
  1387. // Skip "/" and get P in next loop.
  1388. //
  1389. Pointer++;
  1390. } else {
  1391. //
  1392. // '/P' is not in the String.
  1393. //
  1394. break;
  1395. }
  1396. } else if (AddressIndex < ARRAY_SIZE (Address->Addr)) {
  1397. if (*Pointer == L'.') {
  1398. //
  1399. // D should be followed by '.'
  1400. //
  1401. Pointer++;
  1402. } else {
  1403. return RETURN_UNSUPPORTED;
  1404. }
  1405. }
  1406. }
  1407. if (AddressIndex < ARRAY_SIZE (Address->Addr)) {
  1408. return RETURN_UNSUPPORTED;
  1409. }
  1410. memcpy (Address, &LocalAddress, sizeof (*Address));
  1411. if (PrefixLength != NULL) {
  1412. *PrefixLength = LocalPrefixLength;
  1413. }
  1414. if (EndPointer != NULL) {
  1415. *EndPointer = Pointer;
  1416. }
  1417. return RETURN_SUCCESS;
  1418. }
  1419. RETURN_STATUS
  1420. StrToIpv6Address (
  1421. CONST CHAR16 *String,
  1422. CHAR16 **EndPointer,
  1423. EFI_IPv6_ADDRESS *Address,
  1424. UINT8 *PrefixLength
  1425. )
  1426. {
  1427. RETURN_STATUS Status;
  1428. UINTN AddressIndex;
  1429. UINT64 Uint64;
  1430. EFI_IPv6_ADDRESS LocalAddress;
  1431. UINT8 LocalPrefixLength;
  1432. CONST CHAR16 *Pointer;
  1433. CHAR16 *End;
  1434. UINTN CompressStart;
  1435. BOOLEAN ExpectPrefix;
  1436. LocalPrefixLength = MAX_UINT8;
  1437. CompressStart = ARRAY_SIZE (Address->Addr);
  1438. ExpectPrefix = FALSE;
  1439. ASSERT (((UINTN) String & BIT0) == 0);
  1440. //
  1441. // 1. None of String or Guid shall be a null pointer.
  1442. //
  1443. SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
  1444. SAFE_STRING_CONSTRAINT_CHECK ((Address != NULL), RETURN_INVALID_PARAMETER);
  1445. for (Pointer = String, AddressIndex = 0; AddressIndex < ARRAY_SIZE (Address->Addr) + 1;) {
  1446. if (!InternalIsHexaDecimalDigitCharacter (*Pointer)) {
  1447. if (*Pointer != L':') {
  1448. //
  1449. // ":" or "/" should be followed by digit characters.
  1450. //
  1451. return RETURN_UNSUPPORTED;
  1452. }
  1453. //
  1454. // Meet second ":" after previous ":" or "/"
  1455. // or meet first ":" in the beginning of String.
  1456. //
  1457. if (ExpectPrefix) {
  1458. //
  1459. // ":" shall not be after "/"
  1460. //
  1461. return RETURN_UNSUPPORTED;
  1462. }
  1463. if (CompressStart != ARRAY_SIZE (Address->Addr) || AddressIndex == ARRAY_SIZE (Address->Addr)) {
  1464. //
  1465. // "::" can only appear once.
  1466. // "::" can only appear when address is not full length.
  1467. //
  1468. return RETURN_UNSUPPORTED;
  1469. } else {
  1470. //
  1471. // Remember the start of zero compressing.
  1472. //
  1473. CompressStart = AddressIndex;
  1474. Pointer++;
  1475. if (CompressStart == 0) {
  1476. if (*Pointer != L':') {
  1477. //
  1478. // Single ":" shall not be in the beginning of String.
  1479. //
  1480. return RETURN_UNSUPPORTED;
  1481. }
  1482. Pointer++;
  1483. }
  1484. }
  1485. }
  1486. if (!InternalIsHexaDecimalDigitCharacter (*Pointer)) {
  1487. if (*Pointer == L'/') {
  1488. //
  1489. // Might be optional "/P" after "::".
  1490. //
  1491. if (CompressStart != AddressIndex) {
  1492. return RETURN_UNSUPPORTED;
  1493. }
  1494. } else {
  1495. break;
  1496. }
  1497. } else {
  1498. if (!ExpectPrefix) {
  1499. //
  1500. // Get X.
  1501. //
  1502. Status = StrHexToUint64S (Pointer, &End, &Uint64);
  1503. if (RETURN_ERROR (Status) || End - Pointer > 4) {
  1504. //
  1505. // Number of hexadecimal digit characters is no more than 4.
  1506. //
  1507. return RETURN_UNSUPPORTED;
  1508. }
  1509. Pointer = End;
  1510. //
  1511. // Uint64 won't exceed MAX_UINT16 if number of hexadecimal digit characters is no more than 4.
  1512. //
  1513. ASSERT (AddressIndex + 1 < ARRAY_SIZE (Address->Addr));
  1514. LocalAddress.Addr[AddressIndex] = (UINT8) ((UINT16) Uint64 >> 8);
  1515. LocalAddress.Addr[AddressIndex + 1] = (UINT8) Uint64;
  1516. AddressIndex += 2;
  1517. } else {
  1518. //
  1519. // Get P, then exit the loop.
  1520. //
  1521. Status = StrDecimalToUint64S (Pointer, &End, &Uint64);
  1522. if (RETURN_ERROR (Status) || End == Pointer || Uint64 > 128) {
  1523. //
  1524. // Prefix length should not exceed 128.
  1525. //
  1526. return RETURN_UNSUPPORTED;
  1527. }
  1528. LocalPrefixLength = (UINT8) Uint64;
  1529. Pointer = End;
  1530. break;
  1531. }
  1532. }
  1533. //
  1534. // Skip ':' or "/"
  1535. //
  1536. if (*Pointer == L'/') {
  1537. ExpectPrefix = TRUE;
  1538. } else if (*Pointer == L':') {
  1539. if (AddressIndex == ARRAY_SIZE (Address->Addr)) {
  1540. //
  1541. // Meet additional ":" after all 8 16-bit address
  1542. //
  1543. break;
  1544. }
  1545. } else {
  1546. //
  1547. // Meet other character that is not "/" or ":" after all 8 16-bit address
  1548. //
  1549. break;
  1550. }
  1551. Pointer++;
  1552. }
  1553. if ((AddressIndex == ARRAY_SIZE (Address->Addr) && CompressStart != ARRAY_SIZE (Address->Addr)) ||
  1554. (AddressIndex != ARRAY_SIZE (Address->Addr) && CompressStart == ARRAY_SIZE (Address->Addr))
  1555. ) {
  1556. //
  1557. // Full length of address shall not have compressing zeros.
  1558. // Non-full length of address shall have compressing zeros.
  1559. //
  1560. return RETURN_UNSUPPORTED;
  1561. }
  1562. memcpy (&Address->Addr[0], &LocalAddress.Addr[0], CompressStart);
  1563. if (AddressIndex > CompressStart) {
  1564. memset (&Address->Addr[CompressStart], 0, ARRAY_SIZE (Address->Addr) - AddressIndex);
  1565. memcpy (
  1566. &Address->Addr[CompressStart + ARRAY_SIZE (Address->Addr) - AddressIndex],
  1567. &LocalAddress.Addr[CompressStart],
  1568. AddressIndex - CompressStart
  1569. );
  1570. }
  1571. if (PrefixLength != NULL) {
  1572. *PrefixLength = LocalPrefixLength;
  1573. }
  1574. if (EndPointer != NULL) {
  1575. *EndPointer = (CHAR16 *) Pointer;
  1576. }
  1577. return RETURN_SUCCESS;
  1578. }
  1579. RETURN_STATUS
  1580. UnicodeStrToAsciiStrS (
  1581. CONST CHAR16 *Source,
  1582. CHAR8 *Destination,
  1583. UINTN DestMax
  1584. )
  1585. {
  1586. UINTN SourceLen;
  1587. ASSERT (((UINTN) Source & BIT0) == 0);
  1588. //
  1589. // 1. Neither Destination nor Source shall be a null pointer.
  1590. //
  1591. SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
  1592. SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
  1593. //
  1594. // 2. DestMax shall not be greater than ASCII_RSIZE_MAX or RSIZE_MAX.
  1595. //
  1596. if (ASCII_RSIZE_MAX != 0) {
  1597. SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
  1598. }
  1599. if (RSIZE_MAX != 0) {
  1600. SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
  1601. }
  1602. //
  1603. // 3. DestMax shall not equal zero.
  1604. //
  1605. SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
  1606. //
  1607. // 4. DestMax shall be greater than StrnLenS (Source, DestMax).
  1608. //
  1609. SourceLen = StrnLenS (Source, DestMax);
  1610. SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);
  1611. //
  1612. // 5. Copying shall not take place between objects that overlap.
  1613. //
  1614. SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax, (VOID *)Source, (SourceLen + 1) * sizeof(CHAR16)), RETURN_ACCESS_DENIED);
  1615. //
  1616. // convert string
  1617. //
  1618. while (*Source != '\0') {
  1619. //
  1620. // If any Unicode characters in Source contain
  1621. // non-zero value in the upper 8 bits, then ASSERT().
  1622. //
  1623. ASSERT (*Source < 0x100);
  1624. *(Destination++) = (CHAR8) *(Source++);
  1625. }
  1626. *Destination = '\0';
  1627. return RETURN_SUCCESS;
  1628. }
  1629. RETURN_STATUS
  1630. StrCpyS (
  1631. CHAR16 *Destination,
  1632. UINTN DestMax,
  1633. CONST CHAR16 *Source
  1634. )
  1635. {
  1636. UINTN SourceLen;
  1637. ASSERT (((UINTN) Destination & BIT0) == 0);
  1638. ASSERT (((UINTN) Source & BIT0) == 0);
  1639. //
  1640. // 1. Neither Destination nor Source shall be a null pointer.
  1641. //
  1642. SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
  1643. SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
  1644. //
  1645. // 2. DestMax shall not be greater than RSIZE_MAX.
  1646. //
  1647. if (RSIZE_MAX != 0) {
  1648. SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
  1649. }
  1650. //
  1651. // 3. DestMax shall not equal zero.
  1652. //
  1653. SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
  1654. //
  1655. // 4. DestMax shall be greater than StrnLenS(Source, DestMax).
  1656. //
  1657. SourceLen = StrnLenS (Source, DestMax);
  1658. SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);
  1659. //
  1660. // 5. Copying shall not take place between objects that overlap.
  1661. //
  1662. SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination, DestMax, (CHAR16 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
  1663. //
  1664. // The StrCpyS function copies the string pointed to by Source (including the terminating
  1665. // null character) into the array pointed to by Destination.
  1666. //
  1667. while (*Source != 0) {
  1668. *(Destination++) = *(Source++);
  1669. }
  1670. *Destination = 0;
  1671. return RETURN_SUCCESS;
  1672. }
  1673. VOID *
  1674. AllocateZeroPool (
  1675. UINTN AllocationSize
  1676. )
  1677. {
  1678. VOID * Memory;
  1679. Memory = malloc(AllocationSize);
  1680. ASSERT (Memory != NULL);
  1681. if (Memory == NULL) {
  1682. fprintf(stderr, "Not memory for malloc\n");
  1683. }
  1684. memset(Memory, 0, AllocationSize);
  1685. return Memory;
  1686. }
  1687. VOID *
  1688. AllocatePool (
  1689. UINTN AllocationSize
  1690. )
  1691. {
  1692. return InternalAllocatePool (AllocationSize);
  1693. }
  1694. UINT16
  1695. WriteUnaligned16 (
  1696. UINT16 *Buffer,
  1697. UINT16 Value
  1698. )
  1699. {
  1700. ASSERT (Buffer != NULL);
  1701. return *Buffer = Value;
  1702. }
  1703. UINT16
  1704. ReadUnaligned16 (
  1705. CONST UINT16 *Buffer
  1706. )
  1707. {
  1708. ASSERT (Buffer != NULL);
  1709. return *Buffer;
  1710. }
  1711. /**
  1712. Return whether the integer string is a hex string.
  1713. @param Str The integer string
  1714. @retval TRUE Hex string
  1715. @retval FALSE Decimal string
  1716. **/
  1717. BOOLEAN
  1718. IsHexStr (
  1719. CHAR16 *Str
  1720. )
  1721. {
  1722. //
  1723. // skip preceding white space
  1724. //
  1725. while ((*Str != 0) && *Str == L' ') {
  1726. Str ++;
  1727. }
  1728. //
  1729. // skip preceding zeros
  1730. //
  1731. while ((*Str != 0) && *Str == L'0') {
  1732. Str ++;
  1733. }
  1734. return (BOOLEAN) (*Str == L'x' || *Str == L'X');
  1735. }
  1736. /**
  1737. Convert integer string to uint.
  1738. @param Str The integer string. If leading with "0x" or "0X", it's hexadecimal.
  1739. @return A UINTN value represented by Str
  1740. **/
  1741. UINTN
  1742. Strtoi (
  1743. CHAR16 *Str
  1744. )
  1745. {
  1746. if (IsHexStr (Str)) {
  1747. return (UINTN)StrHexToUint64 (Str);
  1748. } else {
  1749. return (UINTN)StrDecimalToUint64 (Str);
  1750. }
  1751. }
  1752. /**
  1753. Convert integer string to 64 bit data.
  1754. @param Str The integer string. If leading with "0x" or "0X", it's hexadecimal.
  1755. @param Data A pointer to the UINT64 value represented by Str
  1756. **/
  1757. VOID
  1758. Strtoi64 (
  1759. CHAR16 *Str,
  1760. UINT64 *Data
  1761. )
  1762. {
  1763. if (IsHexStr (Str)) {
  1764. *Data = StrHexToUint64 (Str);
  1765. } else {
  1766. *Data = StrDecimalToUint64 (Str);
  1767. }
  1768. }
  1769. /**
  1770. Converts a Unicode string to ASCII string.
  1771. @param Str The equivalent Unicode string
  1772. @param AsciiStr On input, it points to destination ASCII string buffer; on output, it points
  1773. to the next ASCII string next to it
  1774. **/
  1775. VOID
  1776. StrToAscii (
  1777. CHAR16 *Str,
  1778. CHAR8 **AsciiStr
  1779. )
  1780. {
  1781. CHAR8 *Dest;
  1782. Dest = *AsciiStr;
  1783. while (!IS_NULL (*Str)) {
  1784. *(Dest++) = (CHAR8) *(Str++);
  1785. }
  1786. *Dest = 0;
  1787. //
  1788. // Return the string next to it
  1789. //
  1790. *AsciiStr = Dest + 1;
  1791. }
  1792. /**
  1793. Gets current sub-string from a string list, before return
  1794. the list header is moved to next sub-string. The sub-string is separated
  1795. by the specified character. For example, the separator is ',', the string
  1796. list is "2,0,3", it returns "2", the remain list move to "0,3"
  1797. @param List A string list separated by the specified separator
  1798. @param Separator The separator character
  1799. @return A pointer to the current sub-string
  1800. **/
  1801. CHAR16 *
  1802. SplitStr (
  1803. CHAR16 **List,
  1804. CHAR16 Separator
  1805. )
  1806. {
  1807. CHAR16 *Str;
  1808. CHAR16 *ReturnStr;
  1809. Str = *List;
  1810. ReturnStr = Str;
  1811. if (IS_NULL (*Str)) {
  1812. return ReturnStr;
  1813. }
  1814. //
  1815. // Find first occurrence of the separator
  1816. //
  1817. while (!IS_NULL (*Str)) {
  1818. if (*Str == Separator) {
  1819. break;
  1820. }
  1821. Str++;
  1822. }
  1823. if (*Str == Separator) {
  1824. //
  1825. // Find a sub-string, terminate it
  1826. //
  1827. *Str = L'\0';
  1828. Str++;
  1829. }
  1830. //
  1831. // Move to next sub-string
  1832. //
  1833. *List = Str;
  1834. return ReturnStr;
  1835. }