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