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