]>
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 |
7c342378 | 12 | int errno = 0;\r |
8e01b449 | 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 | |
7c342378 MK |
38 | VOID *Pivot;\r |
39 | UINTN LoopCount;\r | |
40 | UINTN NextSwapLocation;\r | |
97f98500 | 41 | \r |
7c342378 MK |
42 | ASSERT (BufferToSort != NULL);\r |
43 | ASSERT (CompareFunction != NULL);\r | |
44 | ASSERT (Buffer != NULL);\r | |
97f98500 | 45 | \r |
7c342378 | 46 | if ((Count < 2) || (ElementSize < 1)) {\r |
97f98500 HT |
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 | |
7c342378 | 61 | for (LoopCount = 0; LoopCount < Count - 1; LoopCount++) {\r |
97f98500 HT |
62 | //\r |
63 | // If the element is less than the pivot\r | |
64 | //\r | |
65 | if (CompareFunction ((VOID *)((UINT8 *)BufferToSort + ((LoopCount) * ElementSize)), Pivot) <= 0) {\r | |
66 | //\r | |
67 | // Swap\r | |
68 | //\r | |
69 | CopyMem (Buffer, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);\r | |
70 | CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), (UINT8 *)BufferToSort + ((LoopCount) * ElementSize), ElementSize);\r | |
71 | CopyMem ((UINT8 *)BufferToSort + ((LoopCount) * ElementSize), Buffer, ElementSize);\r | |
72 | \r | |
73 | //\r | |
74 | // Increment NextSwapLocation\r | |
75 | //\r | |
76 | NextSwapLocation++;\r | |
77 | }\r | |
78 | }\r | |
7c342378 | 79 | \r |
97f98500 | 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 | |
7c342378 | 110 | // ---------------------------------------------------------\r |
97f98500 | 111 | // Standard C Run-time Library Interface Wrapper\r |
7c342378 | 112 | // ---------------------------------------------------------\r |
97f98500 HT |
113 | \r |
114 | //\r | |
115 | // -- String Manipulation Routines --\r | |
116 | //\r | |
117 | \r | |
7c342378 MK |
118 | char *\r |
119 | strchr (\r | |
120 | const char *str,\r | |
121 | int ch\r | |
122 | )\r | |
eb520d94 LE |
123 | {\r |
124 | return ScanMem8 (str, AsciiStrSize (str), (UINT8)ch);\r | |
125 | }\r | |
126 | \r | |
97f98500 | 127 | /* Scan a string for the last occurrence of a character */\r |
7c342378 MK |
128 | char *\r |
129 | strrchr (\r | |
130 | const char *str,\r | |
131 | int c\r | |
132 | )\r | |
97f98500 | 133 | {\r |
7c342378 | 134 | char *save;\r |
97f98500 HT |
135 | \r |
136 | for (save = NULL; ; ++str) {\r | |
137 | if (*str == c) {\r | |
138 | save = (char *)str;\r | |
139 | }\r | |
7c342378 | 140 | \r |
97f98500 HT |
141 | if (*str == 0) {\r |
142 | return (save);\r | |
143 | }\r | |
144 | }\r | |
145 | }\r | |
146 | \r | |
fc9fa685 | 147 | /* Compare first n bytes of string s1 with string s2, ignoring case */\r |
7c342378 MK |
148 | int\r |
149 | strncasecmp (\r | |
150 | const char *s1,\r | |
151 | const char *s2,\r | |
152 | size_t n\r | |
153 | )\r | |
fc9fa685 | 154 | {\r |
7c342378 | 155 | int Val;\r |
fc9fa685 | 156 | \r |
7c342378 MK |
157 | ASSERT (s1 != NULL);\r |
158 | ASSERT (s2 != NULL);\r | |
fc9fa685 QL |
159 | \r |
160 | if (n != 0) {\r | |
161 | do {\r | |
7c342378 | 162 | Val = tolower (*s1) - tolower (*s2);\r |
fc9fa685 QL |
163 | if (Val != 0) {\r |
164 | return Val;\r | |
165 | }\r | |
7c342378 | 166 | \r |
fc9fa685 QL |
167 | ++s1;\r |
168 | ++s2;\r | |
169 | if (*s1 == '\0') {\r | |
170 | break;\r | |
171 | }\r | |
172 | } while (--n != 0);\r | |
173 | }\r | |
7c342378 | 174 | \r |
fc9fa685 QL |
175 | return 0;\r |
176 | }\r | |
177 | \r | |
97f98500 | 178 | /* Read formatted data from a string */\r |
7c342378 MK |
179 | int\r |
180 | sscanf (\r | |
181 | const char *buffer,\r | |
182 | const char *format,\r | |
183 | ...\r | |
184 | )\r | |
97f98500 HT |
185 | {\r |
186 | //\r | |
187 | // Null sscanf() function implementation to satisfy the linker, since\r | |
188 | // no direct functionality logic dependency in present UEFI cases.\r | |
189 | //\r | |
190 | return 0;\r | |
191 | }\r | |
192 | \r | |
fc9fa685 | 193 | /* Maps errnum to an error-message string */\r |
7c342378 MK |
194 | char *\r |
195 | strerror (\r | |
196 | int errnum\r | |
197 | )\r | |
fc9fa685 QL |
198 | {\r |
199 | return NULL;\r | |
200 | }\r | |
201 | \r | |
202 | /* Computes the length of the maximum initial segment of the string pointed to by s1\r | |
203 | which consists entirely of characters from the string pointed to by s2. */\r | |
7c342378 MK |
204 | size_t\r |
205 | strspn (\r | |
206 | const char *s1,\r | |
207 | const char *s2\r | |
208 | )\r | |
fc9fa685 QL |
209 | {\r |
210 | UINT8 Map[32];\r | |
211 | UINT32 Index;\r | |
212 | size_t Count;\r | |
213 | \r | |
214 | for (Index = 0; Index < 32; Index++) {\r | |
215 | Map[Index] = 0;\r | |
216 | }\r | |
217 | \r | |
218 | while (*s2) {\r | |
219 | Map[*s2 >> 3] |= (1 << (*s2 & 7));\r | |
220 | s2++;\r | |
221 | }\r | |
222 | \r | |
223 | if (*s1) {\r | |
224 | Count = 0;\r | |
225 | while (Map[*s1 >> 3] & (1 << (*s1 & 7))) {\r | |
226 | Count++;\r | |
227 | s1++;\r | |
228 | }\r | |
229 | \r | |
230 | return Count;\r | |
231 | }\r | |
232 | \r | |
233 | return 0;\r | |
234 | }\r | |
235 | \r | |
236 | /* Computes the length of the maximum initial segment of the string pointed to by s1\r | |
237 | which consists entirely of characters not from the string pointed to by s2. */\r | |
7c342378 MK |
238 | size_t\r |
239 | strcspn (\r | |
240 | const char *s1,\r | |
241 | const char *s2\r | |
242 | )\r | |
fc9fa685 | 243 | {\r |
7c342378 MK |
244 | UINT8 Map[32];\r |
245 | UINT32 Index;\r | |
246 | size_t Count;\r | |
fc9fa685 QL |
247 | \r |
248 | for (Index = 0; Index < 32; Index++) {\r | |
249 | Map[Index] = 0;\r | |
250 | }\r | |
251 | \r | |
252 | while (*s2) {\r | |
253 | Map[*s2 >> 3] |= (1 << (*s2 & 7));\r | |
254 | s2++;\r | |
255 | }\r | |
256 | \r | |
257 | Map[0] |= 1;\r | |
258 | \r | |
7c342378 | 259 | Count = 0;\r |
fc9fa685 | 260 | while (!(Map[*s1 >> 3] & (1 << (*s1 & 7)))) {\r |
7c342378 | 261 | Count++;\r |
fc9fa685 QL |
262 | s1++;\r |
263 | }\r | |
264 | \r | |
265 | return Count;\r | |
266 | }\r | |
267 | \r | |
97f98500 HT |
268 | //\r |
269 | // -- Character Classification Routines --\r | |
270 | //\r | |
271 | \r | |
272 | /* Determines if a particular character is a decimal-digit character */\r | |
7c342378 MK |
273 | int\r |
274 | isdigit (\r | |
275 | int c\r | |
276 | )\r | |
97f98500 HT |
277 | {\r |
278 | //\r | |
279 | // <digit> ::= [0-9]\r | |
280 | //\r | |
281 | return (('0' <= (c)) && ((c) <= '9'));\r | |
282 | }\r | |
283 | \r | |
284 | /* Determine if an integer represents character that is a hex digit */\r | |
7c342378 MK |
285 | int\r |
286 | isxdigit (\r | |
287 | int c\r | |
288 | )\r | |
97f98500 HT |
289 | {\r |
290 | //\r | |
291 | // <hexdigit> ::= [0-9] | [a-f] | [A-F]\r | |
292 | //\r | |
293 | return ((('0' <= (c)) && ((c) <= '9')) ||\r | |
294 | (('a' <= (c)) && ((c) <= 'f')) ||\r | |
295 | (('A' <= (c)) && ((c) <= 'F')));\r | |
296 | }\r | |
297 | \r | |
298 | /* Determines if a particular character represents a space character */\r | |
7c342378 MK |
299 | int\r |
300 | isspace (\r | |
301 | int c\r | |
302 | )\r | |
97f98500 HT |
303 | {\r |
304 | //\r | |
305 | // <space> ::= [ ]\r | |
306 | //\r | |
307 | return ((c) == ' ');\r | |
308 | }\r | |
309 | \r | |
310 | /* Determine if a particular character is an alphanumeric character */\r | |
7c342378 MK |
311 | int\r |
312 | isalnum (\r | |
313 | int c\r | |
314 | )\r | |
97f98500 HT |
315 | {\r |
316 | //\r | |
317 | // <alnum> ::= [0-9] | [a-z] | [A-Z]\r | |
318 | //\r | |
319 | return ((('0' <= (c)) && ((c) <= '9')) ||\r | |
320 | (('a' <= (c)) && ((c) <= 'z')) ||\r | |
321 | (('A' <= (c)) && ((c) <= 'Z')));\r | |
322 | }\r | |
323 | \r | |
324 | /* Determines if a particular character is in upper case */\r | |
7c342378 MK |
325 | int\r |
326 | isupper (\r | |
327 | int c\r | |
328 | )\r | |
97f98500 HT |
329 | {\r |
330 | //\r | |
331 | // <uppercase letter> := [A-Z]\r | |
332 | //\r | |
333 | return (('A' <= (c)) && ((c) <= 'Z'));\r | |
334 | }\r | |
335 | \r | |
336 | //\r | |
337 | // -- Data Conversion Routines --\r | |
338 | //\r | |
339 | \r | |
340 | /* Convert strings to a long-integer value */\r | |
7c342378 MK |
341 | long\r |
342 | strtol (\r | |
343 | const char *nptr,\r | |
344 | char **endptr,\r | |
345 | int base\r | |
346 | )\r | |
97f98500 HT |
347 | {\r |
348 | //\r | |
349 | // Null strtol() function implementation to satisfy the linker, since there is\r | |
350 | // no direct functionality logic dependency in present UEFI cases.\r | |
351 | //\r | |
352 | return 0;\r | |
353 | }\r | |
354 | \r | |
355 | /* Convert strings to an unsigned long-integer value */\r | |
7c342378 MK |
356 | unsigned long\r |
357 | strtoul (\r | |
358 | const char *nptr,\r | |
359 | char **endptr,\r | |
360 | int base\r | |
361 | )\r | |
97f98500 HT |
362 | {\r |
363 | //\r | |
364 | // Null strtoul() function implementation to satisfy the linker, since there is\r | |
365 | // no direct functionality logic dependency in present UEFI cases.\r | |
366 | //\r | |
367 | return 0;\r | |
368 | }\r | |
369 | \r | |
370 | /* Convert character to lowercase */\r | |
7c342378 MK |
371 | int\r |
372 | tolower (\r | |
373 | int c\r | |
374 | )\r | |
97f98500 HT |
375 | {\r |
376 | if (('A' <= (c)) && ((c) <= 'Z')) {\r | |
377 | return (c - ('A' - 'a'));\r | |
378 | }\r | |
7c342378 | 379 | \r |
97f98500 HT |
380 | return (c);\r |
381 | }\r | |
382 | \r | |
383 | //\r | |
384 | // -- Searching and Sorting Routines --\r | |
385 | //\r | |
386 | \r | |
387 | /* Performs a quick sort */\r | |
7c342378 MK |
388 | void\r |
389 | qsort (\r | |
390 | void *base,\r | |
391 | size_t num,\r | |
392 | size_t width,\r | |
393 | int ( *compare )(const void *, const void *)\r | |
394 | )\r | |
97f98500 HT |
395 | {\r |
396 | VOID *Buffer;\r | |
397 | \r | |
398 | ASSERT (base != NULL);\r | |
399 | ASSERT (compare != NULL);\r | |
400 | \r | |
b7d320f8 | 401 | //\r |
402 | // Use CRT-style malloc to cover BS and RT memory allocation.\r | |
403 | //\r | |
404 | Buffer = malloc (width);\r | |
97f98500 HT |
405 | ASSERT (Buffer != NULL);\r |
406 | \r | |
407 | //\r | |
408 | // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.\r | |
409 | //\r | |
410 | QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);\r | |
411 | \r | |
b7d320f8 | 412 | free (Buffer);\r |
97f98500 HT |
413 | return;\r |
414 | }\r | |
415 | \r | |
416 | //\r | |
417 | // -- Process and Environment Control Routines --\r | |
418 | //\r | |
419 | \r | |
420 | /* Get a value from the current environment */\r | |
7c342378 MK |
421 | char *\r |
422 | getenv (\r | |
423 | const char *varname\r | |
424 | )\r | |
97f98500 HT |
425 | {\r |
426 | //\r | |
427 | // Null getenv() function implementation to satisfy the linker, since there is\r | |
428 | // no direct functionality logic dependency in present UEFI cases.\r | |
429 | //\r | |
430 | return NULL;\r | |
431 | }\r | |
432 | \r | |
ee3198e6 | 433 | /* Get a value from the current environment */\r |
7c342378 MK |
434 | char *\r |
435 | secure_getenv (\r | |
436 | const char *varname\r | |
437 | )\r | |
ee3198e6 LE |
438 | {\r |
439 | //\r | |
440 | // Null secure_getenv() function implementation to satisfy the linker, since\r | |
441 | // there is no direct functionality logic dependency in present UEFI cases.\r | |
442 | //\r | |
443 | // From the secure_getenv() manual: 'just like getenv() except that it\r | |
444 | // returns NULL in cases where "secure execution" is required'.\r | |
445 | //\r | |
446 | return NULL;\r | |
447 | }\r | |
448 | \r | |
97f98500 HT |
449 | //\r |
450 | // -- Stream I/O Routines --\r | |
451 | //\r | |
452 | \r | |
97f98500 | 453 | /* Write data to a stream */\r |
7c342378 MK |
454 | size_t\r |
455 | fwrite (\r | |
456 | const void *buffer,\r | |
457 | size_t size,\r | |
458 | size_t count,\r | |
459 | FILE *stream\r | |
460 | )\r | |
97f98500 HT |
461 | {\r |
462 | return 0;\r | |
463 | }\r | |
f754e613 | 464 | \r |
465 | //\r | |
466 | // -- Dummy OpenSSL Support Routines --\r | |
467 | //\r | |
468 | \r | |
7c342378 MK |
469 | int\r |
470 | BIO_printf (\r | |
471 | void *bio,\r | |
472 | const char *format,\r | |
473 | ...\r | |
474 | )\r | |
f754e613 | 475 | {\r |
476 | return 0;\r | |
477 | }\r | |
478 | \r | |
7c342378 MK |
479 | int\r |
480 | BIO_snprintf (\r | |
481 | char *buf,\r | |
482 | size_t n,\r | |
483 | const char *format,\r | |
484 | ...\r | |
485 | )\r | |
f754e613 | 486 | {\r |
487 | return 0;\r | |
488 | }\r | |
4a567c96 | 489 | \r |
f114914b | 490 | #ifdef __GNUC__\r |
491 | \r | |
492 | typedef\r | |
493 | VOID\r | |
494 | (EFIAPI *NoReturnFuncPtr)(\r | |
495 | VOID\r | |
7c342378 | 496 | ) __attribute__ ((__noreturn__));\r |
f114914b | 497 | \r |
f114914b | 498 | STATIC\r |
499 | VOID\r | |
500 | EFIAPI\r | |
501 | NopFunction (\r | |
502 | VOID\r | |
503 | )\r | |
504 | {\r | |
505 | }\r | |
506 | \r | |
7c342378 MK |
507 | void\r |
508 | abort (\r | |
509 | void\r | |
510 | )\r | |
8e01b449 | 511 | {\r |
7c342378 | 512 | NoReturnFuncPtr NoReturnFunc;\r |
f114914b | 513 | \r |
7c342378 | 514 | NoReturnFunc = (NoReturnFuncPtr)NopFunction;\r |
8e01b449 | 515 | \r |
f114914b | 516 | NoReturnFunc ();\r |
8e01b449 | 517 | }\r |
518 | \r | |
f114914b | 519 | #else\r |
520 | \r | |
7c342378 MK |
521 | void\r |
522 | abort (\r | |
523 | void\r | |
524 | )\r | |
f114914b | 525 | {\r |
fc9fa685 | 526 | // Do nothing\r |
f114914b | 527 | }\r |
528 | \r | |
529 | #endif\r | |
530 | \r | |
7c342378 MK |
531 | int\r |
532 | fclose (\r | |
533 | FILE *f\r | |
534 | )\r | |
8e01b449 | 535 | {\r |
536 | return 0;\r | |
537 | }\r | |
538 | \r | |
7c342378 MK |
539 | FILE *\r |
540 | fopen (\r | |
541 | const char *c,\r | |
542 | const char *m\r | |
543 | )\r | |
8e01b449 | 544 | {\r |
545 | return NULL;\r | |
546 | }\r | |
547 | \r | |
7c342378 MK |
548 | size_t\r |
549 | fread (\r | |
550 | void *b,\r | |
551 | size_t c,\r | |
552 | size_t i,\r | |
553 | FILE *f\r | |
554 | )\r | |
8e01b449 | 555 | {\r |
556 | return 0;\r | |
557 | }\r | |
558 | \r | |
7c342378 MK |
559 | uid_t\r |
560 | getuid (\r | |
561 | void\r | |
562 | )\r | |
8e01b449 | 563 | {\r |
564 | return 0;\r | |
565 | }\r | |
566 | \r | |
7c342378 MK |
567 | uid_t\r |
568 | geteuid (\r | |
569 | void\r | |
570 | )\r | |
8e01b449 | 571 | {\r |
572 | return 0;\r | |
573 | }\r | |
574 | \r | |
7c342378 MK |
575 | gid_t\r |
576 | getgid (\r | |
577 | void\r | |
578 | )\r | |
8e01b449 | 579 | {\r |
580 | return 0;\r | |
581 | }\r | |
582 | \r | |
7c342378 MK |
583 | gid_t\r |
584 | getegid (\r | |
585 | void\r | |
586 | )\r | |
8e01b449 | 587 | {\r |
588 | return 0;\r | |
589 | }\r | |
590 | \r | |
7c342378 MK |
591 | int\r |
592 | printf (\r | |
593 | char const *fmt,\r | |
594 | ...\r | |
595 | )\r | |
32387e00 JW |
596 | {\r |
597 | return 0;\r | |
598 | }\r |