]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c
ArmVirtPkg: Fix various typos
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / SysCall / CrtWrapper.c
CommitLineData
97f98500
HT
1/** @file\r
2 C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based\r
3 Cryptographic Library.\r
4\r
fc9fa685 5Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
97f98500
HT
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
fc9fa685 16#include <CrtLibSupport.h>\r
97f98500 17\r
8e01b449 18int errno = 0;\r
19\r
c61fb9c8 20FILE *stderr = NULL;\r
b7d320f8 21FILE *stdin = NULL;\r
c61fb9c8 22FILE *stdout = NULL;\r
23\r
97f98500 24typedef\r
03805fc3 25int\r
97f98500
HT
26(*SORT_COMPARE)(\r
27 IN VOID *Buffer1,\r
28 IN VOID *Buffer2\r
29 );\r
30\r
31//\r
32// Duplicated from EDKII BaseSortLib for qsort() wrapper\r
33//\r
34STATIC\r
35VOID\r
36QuickSortWorker (\r
37 IN OUT VOID *BufferToSort,\r
38 IN CONST UINTN Count,\r
39 IN CONST UINTN ElementSize,\r
40 IN SORT_COMPARE CompareFunction,\r
41 IN VOID *Buffer\r
42 )\r
43{\r
44 VOID *Pivot;\r
45 UINTN LoopCount;\r
46 UINTN NextSwapLocation;\r
47\r
48 ASSERT(BufferToSort != NULL);\r
49 ASSERT(CompareFunction != NULL);\r
50 ASSERT(Buffer != NULL);\r
51\r
52 if (Count < 2 || ElementSize < 1) {\r
53 return;\r
54 }\r
55\r
56 NextSwapLocation = 0;\r
57\r
58 //\r
59 // Pick a pivot (we choose last element)\r
60 //\r
61 Pivot = ((UINT8 *)BufferToSort + ((Count - 1) * ElementSize));\r
62\r
63 //\r
64 // Now get the pivot such that all on "left" are below it\r
65 // and everything "right" are above it\r
66 //\r
67 for (LoopCount = 0; LoopCount < Count - 1; LoopCount++)\r
68 {\r
69 //\r
70 // If the element is less than the pivot\r
71 //\r
72 if (CompareFunction ((VOID *)((UINT8 *)BufferToSort + ((LoopCount) * ElementSize)), Pivot) <= 0) {\r
73 //\r
74 // Swap\r
75 //\r
76 CopyMem (Buffer, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);\r
77 CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), (UINT8 *)BufferToSort + ((LoopCount) * ElementSize), ElementSize);\r
78 CopyMem ((UINT8 *)BufferToSort + ((LoopCount) * ElementSize), Buffer, ElementSize);\r
79\r
80 //\r
81 // Increment NextSwapLocation\r
82 //\r
83 NextSwapLocation++;\r
84 }\r
85 }\r
86 //\r
87 // Swap pivot to it's final position (NextSwapLocaiton)\r
88 //\r
89 CopyMem (Buffer, Pivot, ElementSize);\r
90 CopyMem (Pivot, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);\r
91 CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), Buffer, ElementSize);\r
92\r
93 //\r
94 // Now recurse on 2 paritial lists. Neither of these will have the 'pivot' element.\r
95 // IE list is sorted left half, pivot element, sorted right half...\r
96 //\r
97 QuickSortWorker (\r
98 BufferToSort,\r
99 NextSwapLocation,\r
100 ElementSize,\r
101 CompareFunction,\r
102 Buffer\r
103 );\r
104\r
105 QuickSortWorker (\r
106 (UINT8 *)BufferToSort + (NextSwapLocation + 1) * ElementSize,\r
107 Count - NextSwapLocation - 1,\r
108 ElementSize,\r
109 CompareFunction,\r
110 Buffer\r
111 );\r
112\r
113 return;\r
114}\r
115\r
116//---------------------------------------------------------\r
117// Standard C Run-time Library Interface Wrapper\r
118//---------------------------------------------------------\r
119\r
120//\r
121// -- String Manipulation Routines --\r
122//\r
123\r
124/* Scan a string for the last occurrence of a character */\r
125char *strrchr (const char *str, int c)\r
126{\r
127 char * save;\r
128\r
129 for (save = NULL; ; ++str) {\r
130 if (*str == c) {\r
131 save = (char *)str;\r
132 }\r
133 if (*str == 0) {\r
134 return (save);\r
135 }\r
136 }\r
137}\r
138\r
fc9fa685
QL
139/* Compare first n bytes of string s1 with string s2, ignoring case */\r
140int strncasecmp (const char *s1, const char *s2, size_t n)\r
141{\r
142 int Val;\r
143\r
144 ASSERT(s1 != NULL);\r
145 ASSERT(s2 != NULL);\r
146\r
147 if (n != 0) {\r
148 do {\r
149 Val = tolower(*s1) - tolower(*s2);\r
150 if (Val != 0) {\r
151 return Val;\r
152 }\r
153 ++s1;\r
154 ++s2;\r
155 if (*s1 == '\0') {\r
156 break;\r
157 }\r
158 } while (--n != 0);\r
159 }\r
160 return 0;\r
161}\r
162\r
97f98500
HT
163/* Read formatted data from a string */\r
164int sscanf (const char *buffer, const char *format, ...)\r
165{\r
166 //\r
167 // Null sscanf() function implementation to satisfy the linker, since\r
168 // no direct functionality logic dependency in present UEFI cases.\r
169 //\r
170 return 0;\r
171}\r
172\r
fc9fa685
QL
173/* Maps errnum to an error-message string */\r
174char * strerror (int errnum)\r
175{\r
176 return NULL;\r
177}\r
178\r
179/* Computes the length of the maximum initial segment of the string pointed to by s1\r
180 which consists entirely of characters from the string pointed to by s2. */\r
181size_t strspn (const char *s1 , const char *s2)\r
182{\r
183 UINT8 Map[32];\r
184 UINT32 Index;\r
185 size_t Count;\r
186\r
187 for (Index = 0; Index < 32; Index++) {\r
188 Map[Index] = 0;\r
189 }\r
190\r
191 while (*s2) {\r
192 Map[*s2 >> 3] |= (1 << (*s2 & 7));\r
193 s2++;\r
194 }\r
195\r
196 if (*s1) {\r
197 Count = 0;\r
198 while (Map[*s1 >> 3] & (1 << (*s1 & 7))) {\r
199 Count++;\r
200 s1++;\r
201 }\r
202\r
203 return Count;\r
204 }\r
205\r
206 return 0;\r
207}\r
208\r
209/* Computes the length of the maximum initial segment of the string pointed to by s1\r
210 which consists entirely of characters not from the string pointed to by s2. */\r
211size_t strcspn (const char *s1, const char *s2)\r
212{\r
213 UINT8 Map[32];\r
214 UINT32 Index;\r
215 size_t Count;\r
216\r
217 for (Index = 0; Index < 32; Index++) {\r
218 Map[Index] = 0;\r
219 }\r
220\r
221 while (*s2) {\r
222 Map[*s2 >> 3] |= (1 << (*s2 & 7));\r
223 s2++;\r
224 }\r
225\r
226 Map[0] |= 1;\r
227\r
228 Count = 0;\r
229 while (!(Map[*s1 >> 3] & (1 << (*s1 & 7)))) {\r
230 Count ++;\r
231 s1++;\r
232 }\r
233\r
234 return Count;\r
235}\r
236\r
97f98500
HT
237//\r
238// -- Character Classification Routines --\r
239//\r
240\r
241/* Determines if a particular character is a decimal-digit character */\r
242int isdigit (int c)\r
243{\r
244 //\r
245 // <digit> ::= [0-9]\r
246 //\r
247 return (('0' <= (c)) && ((c) <= '9'));\r
248}\r
249\r
250/* Determine if an integer represents character that is a hex digit */\r
251int isxdigit (int c)\r
252{\r
253 //\r
254 // <hexdigit> ::= [0-9] | [a-f] | [A-F]\r
255 //\r
256 return ((('0' <= (c)) && ((c) <= '9')) ||\r
257 (('a' <= (c)) && ((c) <= 'f')) ||\r
258 (('A' <= (c)) && ((c) <= 'F')));\r
259}\r
260\r
261/* Determines if a particular character represents a space character */\r
262int isspace (int c)\r
263{\r
264 //\r
265 // <space> ::= [ ]\r
266 //\r
267 return ((c) == ' ');\r
268}\r
269\r
270/* Determine if a particular character is an alphanumeric character */\r
271int isalnum (int c)\r
272{\r
273 //\r
274 // <alnum> ::= [0-9] | [a-z] | [A-Z]\r
275 //\r
276 return ((('0' <= (c)) && ((c) <= '9')) ||\r
277 (('a' <= (c)) && ((c) <= 'z')) ||\r
278 (('A' <= (c)) && ((c) <= 'Z')));\r
279}\r
280\r
281/* Determines if a particular character is in upper case */\r
282int isupper (int c)\r
283{\r
284 //\r
285 // <uppercase letter> := [A-Z]\r
286 //\r
287 return (('A' <= (c)) && ((c) <= 'Z'));\r
288}\r
289\r
290//\r
291// -- Data Conversion Routines --\r
292//\r
293\r
294/* Convert strings to a long-integer value */\r
295long strtol (const char *nptr, char **endptr, int base)\r
296{\r
297 //\r
298 // Null strtol() function implementation to satisfy the linker, since there is\r
299 // no direct functionality logic dependency in present UEFI cases.\r
300 //\r
301 return 0;\r
302}\r
303\r
304/* Convert strings to an unsigned long-integer value */\r
305unsigned long strtoul (const char *nptr, char **endptr, int base)\r
306{\r
307 //\r
308 // Null strtoul() function implementation to satisfy the linker, since there is\r
309 // no direct functionality logic dependency in present UEFI cases.\r
310 //\r
311 return 0;\r
312}\r
313\r
314/* Convert character to lowercase */\r
315int tolower (int c)\r
316{\r
317 if (('A' <= (c)) && ((c) <= 'Z')) {\r
318 return (c - ('A' - 'a'));\r
319 }\r
320 return (c);\r
321}\r
322\r
323//\r
324// -- Searching and Sorting Routines --\r
325//\r
326\r
327/* Performs a quick sort */\r
328void qsort (void *base, size_t num, size_t width, int (*compare)(const void *, const void *))\r
329{\r
330 VOID *Buffer;\r
331\r
332 ASSERT (base != NULL);\r
333 ASSERT (compare != NULL);\r
334\r
b7d320f8 335 //\r
336 // Use CRT-style malloc to cover BS and RT memory allocation.\r
337 //\r
338 Buffer = malloc (width);\r
97f98500
HT
339 ASSERT (Buffer != NULL);\r
340\r
341 //\r
342 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.\r
343 //\r
344 QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);\r
345\r
b7d320f8 346 free (Buffer);\r
97f98500
HT
347 return;\r
348}\r
349\r
350//\r
351// -- Process and Environment Control Routines --\r
352//\r
353\r
354/* Get a value from the current environment */\r
355char *getenv (const char *varname)\r
356{\r
357 //\r
358 // Null getenv() function implementation to satisfy the linker, since there is\r
359 // no direct functionality logic dependency in present UEFI cases.\r
360 //\r
361 return NULL;\r
362}\r
363\r
ee3198e6
LE
364/* Get a value from the current environment */\r
365char *secure_getenv (const char *varname)\r
366{\r
367 //\r
368 // Null secure_getenv() function implementation to satisfy the linker, since\r
369 // there is no direct functionality logic dependency in present UEFI cases.\r
370 //\r
371 // From the secure_getenv() manual: 'just like getenv() except that it\r
372 // returns NULL in cases where "secure execution" is required'.\r
373 //\r
374 return NULL;\r
375}\r
376\r
97f98500
HT
377//\r
378// -- Stream I/O Routines --\r
379//\r
380\r
97f98500
HT
381/* Write data to a stream */\r
382size_t fwrite (const void *buffer, size_t size, size_t count, FILE *stream)\r
383{\r
384 return 0;\r
385}\r
f754e613 386\r
387//\r
388// -- Dummy OpenSSL Support Routines --\r
389//\r
390\r
391int BIO_printf (void *bio, const char *format, ...)\r
392{\r
393 return 0;\r
394}\r
395\r
396int BIO_snprintf(char *buf, size_t n, const char *format, ...)\r
397{\r
398 return 0;\r
399}\r
4a567c96 400\r
f114914b 401#ifdef __GNUC__\r
402\r
403typedef\r
404VOID\r
405(EFIAPI *NoReturnFuncPtr)(\r
406 VOID\r
407 ) __attribute__((__noreturn__));\r
408\r
f114914b 409STATIC\r
410VOID\r
411EFIAPI\r
412NopFunction (\r
413 VOID\r
414 )\r
415{\r
416}\r
417\r
fc9fa685 418void abort (void)\r
8e01b449 419{\r
f114914b 420 NoReturnFuncPtr NoReturnFunc;\r
421\r
422 NoReturnFunc = (NoReturnFuncPtr) NopFunction;\r
8e01b449 423\r
f114914b 424 NoReturnFunc ();\r
8e01b449 425}\r
426\r
f114914b 427#else\r
428\r
fc9fa685 429void abort (void)\r
f114914b 430{\r
fc9fa685 431 // Do nothing\r
f114914b 432}\r
433\r
434#endif\r
435\r
8e01b449 436int fclose (FILE *f)\r
437{\r
438 return 0;\r
439}\r
440\r
441FILE *fopen (const char *c, const char *m)\r
442{\r
443 return NULL;\r
444}\r
445\r
446size_t fread (void *b, size_t c, size_t i, FILE *f)\r
447{\r
448 return 0;\r
449}\r
450\r
8e01b449 451uid_t getuid (void)\r
452{\r
453 return 0;\r
454}\r
455\r
456uid_t geteuid (void)\r
457{\r
458 return 0;\r
459}\r
460\r
461gid_t getgid (void)\r
462{\r
463 return 0;\r
464}\r
465\r
466gid_t getegid (void)\r
467{\r
468 return 0;\r
469}\r
470\r
32387e00
JW
471int printf (char const *fmt, ...)\r
472{\r
473 return 0;\r
474}\r