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