]>
Commit | Line | Data |
---|---|---|
97f98500 HT |
1 | /** @file\r |
2 | C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based\r | |
3 | Cryptographic Library.\r | |
4 | \r | |
fc9fa685 | 5 | Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r |
2009f6b4 | 6 | SPDX-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 | 12 | int errno = 0;\r |
13 | \r | |
c61fb9c8 | 14 | FILE *stderr = NULL;\r |
b7d320f8 | 15 | FILE *stdin = NULL;\r |
c61fb9c8 | 16 | FILE *stdout = NULL;\r |
17 | \r | |
97f98500 | 18 | typedef\r |
03805fc3 | 19 | int\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 | |
28 | STATIC\r | |
29 | VOID\r | |
30 | QuickSortWorker (\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 | |
eb520d94 LE |
118 | char *strchr(const char *str, int ch)\r |
119 | {\r | |
120 | return ScanMem8 (str, AsciiStrSize (str), (UINT8)ch);\r | |
121 | }\r | |
122 | \r | |
97f98500 HT |
123 | /* Scan a string for the last occurrence of a character */\r |
124 | char *strrchr (const char *str, int c)\r | |
125 | {\r | |
126 | char * save;\r | |
127 | \r | |
128 | for (save = NULL; ; ++str) {\r | |
129 | if (*str == c) {\r | |
130 | save = (char *)str;\r | |
131 | }\r | |
132 | if (*str == 0) {\r | |
133 | return (save);\r | |
134 | }\r | |
135 | }\r | |
136 | }\r | |
137 | \r | |
fc9fa685 QL |
138 | /* Compare first n bytes of string s1 with string s2, ignoring case */\r |
139 | int strncasecmp (const char *s1, const char *s2, size_t n)\r | |
140 | {\r | |
141 | int Val;\r | |
142 | \r | |
143 | ASSERT(s1 != NULL);\r | |
144 | ASSERT(s2 != NULL);\r | |
145 | \r | |
146 | if (n != 0) {\r | |
147 | do {\r | |
148 | Val = tolower(*s1) - tolower(*s2);\r | |
149 | if (Val != 0) {\r | |
150 | return Val;\r | |
151 | }\r | |
152 | ++s1;\r | |
153 | ++s2;\r | |
154 | if (*s1 == '\0') {\r | |
155 | break;\r | |
156 | }\r | |
157 | } while (--n != 0);\r | |
158 | }\r | |
159 | return 0;\r | |
160 | }\r | |
161 | \r | |
97f98500 HT |
162 | /* Read formatted data from a string */\r |
163 | int sscanf (const char *buffer, const char *format, ...)\r | |
164 | {\r | |
165 | //\r | |
166 | // Null sscanf() function implementation to satisfy the linker, since\r | |
167 | // no direct functionality logic dependency in present UEFI cases.\r | |
168 | //\r | |
169 | return 0;\r | |
170 | }\r | |
171 | \r | |
fc9fa685 QL |
172 | /* Maps errnum to an error-message string */\r |
173 | char * strerror (int errnum)\r | |
174 | {\r | |
175 | return NULL;\r | |
176 | }\r | |
177 | \r | |
178 | /* Computes the length of the maximum initial segment of the string pointed to by s1\r | |
179 | which consists entirely of characters from the string pointed to by s2. */\r | |
180 | size_t strspn (const char *s1 , const char *s2)\r | |
181 | {\r | |
182 | UINT8 Map[32];\r | |
183 | UINT32 Index;\r | |
184 | size_t Count;\r | |
185 | \r | |
186 | for (Index = 0; Index < 32; Index++) {\r | |
187 | Map[Index] = 0;\r | |
188 | }\r | |
189 | \r | |
190 | while (*s2) {\r | |
191 | Map[*s2 >> 3] |= (1 << (*s2 & 7));\r | |
192 | s2++;\r | |
193 | }\r | |
194 | \r | |
195 | if (*s1) {\r | |
196 | Count = 0;\r | |
197 | while (Map[*s1 >> 3] & (1 << (*s1 & 7))) {\r | |
198 | Count++;\r | |
199 | s1++;\r | |
200 | }\r | |
201 | \r | |
202 | return Count;\r | |
203 | }\r | |
204 | \r | |
205 | return 0;\r | |
206 | }\r | |
207 | \r | |
208 | /* Computes the length of the maximum initial segment of the string pointed to by s1\r | |
209 | which consists entirely of characters not from the string pointed to by s2. */\r | |
210 | size_t strcspn (const char *s1, const char *s2)\r | |
211 | {\r | |
212 | UINT8 Map[32];\r | |
213 | UINT32 Index;\r | |
214 | size_t Count;\r | |
215 | \r | |
216 | for (Index = 0; Index < 32; Index++) {\r | |
217 | Map[Index] = 0;\r | |
218 | }\r | |
219 | \r | |
220 | while (*s2) {\r | |
221 | Map[*s2 >> 3] |= (1 << (*s2 & 7));\r | |
222 | s2++;\r | |
223 | }\r | |
224 | \r | |
225 | Map[0] |= 1;\r | |
226 | \r | |
227 | Count = 0;\r | |
228 | while (!(Map[*s1 >> 3] & (1 << (*s1 & 7)))) {\r | |
229 | Count ++;\r | |
230 | s1++;\r | |
231 | }\r | |
232 | \r | |
233 | return Count;\r | |
234 | }\r | |
235 | \r | |
97f98500 HT |
236 | //\r |
237 | // -- Character Classification Routines --\r | |
238 | //\r | |
239 | \r | |
240 | /* Determines if a particular character is a decimal-digit character */\r | |
241 | int isdigit (int c)\r | |
242 | {\r | |
243 | //\r | |
244 | // <digit> ::= [0-9]\r | |
245 | //\r | |
246 | return (('0' <= (c)) && ((c) <= '9'));\r | |
247 | }\r | |
248 | \r | |
249 | /* Determine if an integer represents character that is a hex digit */\r | |
250 | int isxdigit (int c)\r | |
251 | {\r | |
252 | //\r | |
253 | // <hexdigit> ::= [0-9] | [a-f] | [A-F]\r | |
254 | //\r | |
255 | return ((('0' <= (c)) && ((c) <= '9')) ||\r | |
256 | (('a' <= (c)) && ((c) <= 'f')) ||\r | |
257 | (('A' <= (c)) && ((c) <= 'F')));\r | |
258 | }\r | |
259 | \r | |
260 | /* Determines if a particular character represents a space character */\r | |
261 | int isspace (int c)\r | |
262 | {\r | |
263 | //\r | |
264 | // <space> ::= [ ]\r | |
265 | //\r | |
266 | return ((c) == ' ');\r | |
267 | }\r | |
268 | \r | |
269 | /* Determine if a particular character is an alphanumeric character */\r | |
270 | int isalnum (int c)\r | |
271 | {\r | |
272 | //\r | |
273 | // <alnum> ::= [0-9] | [a-z] | [A-Z]\r | |
274 | //\r | |
275 | return ((('0' <= (c)) && ((c) <= '9')) ||\r | |
276 | (('a' <= (c)) && ((c) <= 'z')) ||\r | |
277 | (('A' <= (c)) && ((c) <= 'Z')));\r | |
278 | }\r | |
279 | \r | |
280 | /* Determines if a particular character is in upper case */\r | |
281 | int isupper (int c)\r | |
282 | {\r | |
283 | //\r | |
284 | // <uppercase letter> := [A-Z]\r | |
285 | //\r | |
286 | return (('A' <= (c)) && ((c) <= 'Z'));\r | |
287 | }\r | |
288 | \r | |
289 | //\r | |
290 | // -- Data Conversion Routines --\r | |
291 | //\r | |
292 | \r | |
293 | /* Convert strings to a long-integer value */\r | |
294 | long strtol (const char *nptr, char **endptr, int base)\r | |
295 | {\r | |
296 | //\r | |
297 | // Null strtol() function implementation to satisfy the linker, since there is\r | |
298 | // no direct functionality logic dependency in present UEFI cases.\r | |
299 | //\r | |
300 | return 0;\r | |
301 | }\r | |
302 | \r | |
303 | /* Convert strings to an unsigned long-integer value */\r | |
304 | unsigned long strtoul (const char *nptr, char **endptr, int base)\r | |
305 | {\r | |
306 | //\r | |
307 | // Null strtoul() function implementation to satisfy the linker, since there is\r | |
308 | // no direct functionality logic dependency in present UEFI cases.\r | |
309 | //\r | |
310 | return 0;\r | |
311 | }\r | |
312 | \r | |
313 | /* Convert character to lowercase */\r | |
314 | int tolower (int c)\r | |
315 | {\r | |
316 | if (('A' <= (c)) && ((c) <= 'Z')) {\r | |
317 | return (c - ('A' - 'a'));\r | |
318 | }\r | |
319 | return (c);\r | |
320 | }\r | |
321 | \r | |
322 | //\r | |
323 | // -- Searching and Sorting Routines --\r | |
324 | //\r | |
325 | \r | |
326 | /* Performs a quick sort */\r | |
327 | void qsort (void *base, size_t num, size_t width, int (*compare)(const void *, const void *))\r | |
328 | {\r | |
329 | VOID *Buffer;\r | |
330 | \r | |
331 | ASSERT (base != NULL);\r | |
332 | ASSERT (compare != NULL);\r | |
333 | \r | |
b7d320f8 | 334 | //\r |
335 | // Use CRT-style malloc to cover BS and RT memory allocation.\r | |
336 | //\r | |
337 | Buffer = malloc (width);\r | |
97f98500 HT |
338 | ASSERT (Buffer != NULL);\r |
339 | \r | |
340 | //\r | |
341 | // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.\r | |
342 | //\r | |
343 | QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);\r | |
344 | \r | |
b7d320f8 | 345 | free (Buffer);\r |
97f98500 HT |
346 | return;\r |
347 | }\r | |
348 | \r | |
349 | //\r | |
350 | // -- Process and Environment Control Routines --\r | |
351 | //\r | |
352 | \r | |
353 | /* Get a value from the current environment */\r | |
354 | char *getenv (const char *varname)\r | |
355 | {\r | |
356 | //\r | |
357 | // Null getenv() function implementation to satisfy the linker, since there is\r | |
358 | // no direct functionality logic dependency in present UEFI cases.\r | |
359 | //\r | |
360 | return NULL;\r | |
361 | }\r | |
362 | \r | |
ee3198e6 LE |
363 | /* Get a value from the current environment */\r |
364 | char *secure_getenv (const char *varname)\r | |
365 | {\r | |
366 | //\r | |
367 | // Null secure_getenv() function implementation to satisfy the linker, since\r | |
368 | // there is no direct functionality logic dependency in present UEFI cases.\r | |
369 | //\r | |
370 | // From the secure_getenv() manual: 'just like getenv() except that it\r | |
371 | // returns NULL in cases where "secure execution" is required'.\r | |
372 | //\r | |
373 | return NULL;\r | |
374 | }\r | |
375 | \r | |
97f98500 HT |
376 | //\r |
377 | // -- Stream I/O Routines --\r | |
378 | //\r | |
379 | \r | |
97f98500 HT |
380 | /* Write data to a stream */\r |
381 | size_t fwrite (const void *buffer, size_t size, size_t count, FILE *stream)\r | |
382 | {\r | |
383 | return 0;\r | |
384 | }\r | |
f754e613 | 385 | \r |
386 | //\r | |
387 | // -- Dummy OpenSSL Support Routines --\r | |
388 | //\r | |
389 | \r | |
390 | int BIO_printf (void *bio, const char *format, ...)\r | |
391 | {\r | |
392 | return 0;\r | |
393 | }\r | |
394 | \r | |
395 | int BIO_snprintf(char *buf, size_t n, const char *format, ...)\r | |
396 | {\r | |
397 | return 0;\r | |
398 | }\r | |
4a567c96 | 399 | \r |
f114914b | 400 | #ifdef __GNUC__\r |
401 | \r | |
402 | typedef\r | |
403 | VOID\r | |
404 | (EFIAPI *NoReturnFuncPtr)(\r | |
405 | VOID\r | |
406 | ) __attribute__((__noreturn__));\r | |
407 | \r | |
f114914b | 408 | STATIC\r |
409 | VOID\r | |
410 | EFIAPI\r | |
411 | NopFunction (\r | |
412 | VOID\r | |
413 | )\r | |
414 | {\r | |
415 | }\r | |
416 | \r | |
fc9fa685 | 417 | void abort (void)\r |
8e01b449 | 418 | {\r |
f114914b | 419 | NoReturnFuncPtr NoReturnFunc;\r |
420 | \r | |
421 | NoReturnFunc = (NoReturnFuncPtr) NopFunction;\r | |
8e01b449 | 422 | \r |
f114914b | 423 | NoReturnFunc ();\r |
8e01b449 | 424 | }\r |
425 | \r | |
f114914b | 426 | #else\r |
427 | \r | |
fc9fa685 | 428 | void abort (void)\r |
f114914b | 429 | {\r |
fc9fa685 | 430 | // Do nothing\r |
f114914b | 431 | }\r |
432 | \r | |
433 | #endif\r | |
434 | \r | |
8e01b449 | 435 | int fclose (FILE *f)\r |
436 | {\r | |
437 | return 0;\r | |
438 | }\r | |
439 | \r | |
440 | FILE *fopen (const char *c, const char *m)\r | |
441 | {\r | |
442 | return NULL;\r | |
443 | }\r | |
444 | \r | |
445 | size_t fread (void *b, size_t c, size_t i, FILE *f)\r | |
446 | {\r | |
447 | return 0;\r | |
448 | }\r | |
449 | \r | |
8e01b449 | 450 | uid_t getuid (void)\r |
451 | {\r | |
452 | return 0;\r | |
453 | }\r | |
454 | \r | |
455 | uid_t geteuid (void)\r | |
456 | {\r | |
457 | return 0;\r | |
458 | }\r | |
459 | \r | |
460 | gid_t getgid (void)\r | |
461 | {\r | |
462 | return 0;\r | |
463 | }\r | |
464 | \r | |
465 | gid_t getegid (void)\r | |
466 | {\r | |
467 | return 0;\r | |
468 | }\r | |
469 | \r | |
32387e00 JW |
470 | int printf (char const *fmt, ...)\r |
471 | {\r | |
472 | return 0;\r | |
473 | }\r |