]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c
CryptoPkg: Apply uncrustify changes
[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 //
269 // -- Character Classification Routines --
270 //
271
272 /* Determines if a particular character is a decimal-digit character */
273 int
274 isdigit (
275 int c
276 )
277 {
278 //
279 // <digit> ::= [0-9]
280 //
281 return (('0' <= (c)) && ((c) <= '9'));
282 }
283
284 /* Determine if an integer represents character that is a hex digit */
285 int
286 isxdigit (
287 int c
288 )
289 {
290 //
291 // <hexdigit> ::= [0-9] | [a-f] | [A-F]
292 //
293 return ((('0' <= (c)) && ((c) <= '9')) ||
294 (('a' <= (c)) && ((c) <= 'f')) ||
295 (('A' <= (c)) && ((c) <= 'F')));
296 }
297
298 /* Determines if a particular character represents a space character */
299 int
300 isspace (
301 int c
302 )
303 {
304 //
305 // <space> ::= [ ]
306 //
307 return ((c) == ' ');
308 }
309
310 /* Determine if a particular character is an alphanumeric character */
311 int
312 isalnum (
313 int c
314 )
315 {
316 //
317 // <alnum> ::= [0-9] | [a-z] | [A-Z]
318 //
319 return ((('0' <= (c)) && ((c) <= '9')) ||
320 (('a' <= (c)) && ((c) <= 'z')) ||
321 (('A' <= (c)) && ((c) <= 'Z')));
322 }
323
324 /* Determines if a particular character is in upper case */
325 int
326 isupper (
327 int c
328 )
329 {
330 //
331 // <uppercase letter> := [A-Z]
332 //
333 return (('A' <= (c)) && ((c) <= 'Z'));
334 }
335
336 //
337 // -- Data Conversion Routines --
338 //
339
340 /* Convert strings to a long-integer value */
341 long
342 strtol (
343 const char *nptr,
344 char **endptr,
345 int base
346 )
347 {
348 //
349 // Null strtol() function implementation to satisfy the linker, since there is
350 // no direct functionality logic dependency in present UEFI cases.
351 //
352 return 0;
353 }
354
355 /* Convert strings to an unsigned long-integer value */
356 unsigned long
357 strtoul (
358 const char *nptr,
359 char **endptr,
360 int base
361 )
362 {
363 //
364 // Null strtoul() function implementation to satisfy the linker, since there is
365 // no direct functionality logic dependency in present UEFI cases.
366 //
367 return 0;
368 }
369
370 /* Convert character to lowercase */
371 int
372 tolower (
373 int c
374 )
375 {
376 if (('A' <= (c)) && ((c) <= 'Z')) {
377 return (c - ('A' - 'a'));
378 }
379
380 return (c);
381 }
382
383 //
384 // -- Searching and Sorting Routines --
385 //
386
387 /* Performs a quick sort */
388 void
389 qsort (
390 void *base,
391 size_t num,
392 size_t width,
393 int ( *compare )(const void *, const void *)
394 )
395 {
396 VOID *Buffer;
397
398 ASSERT (base != NULL);
399 ASSERT (compare != NULL);
400
401 //
402 // Use CRT-style malloc to cover BS and RT memory allocation.
403 //
404 Buffer = malloc (width);
405 ASSERT (Buffer != NULL);
406
407 //
408 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
409 //
410 QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);
411
412 free (Buffer);
413 return;
414 }
415
416 //
417 // -- Process and Environment Control Routines --
418 //
419
420 /* Get a value from the current environment */
421 char *
422 getenv (
423 const char *varname
424 )
425 {
426 //
427 // Null getenv() function implementation to satisfy the linker, since there is
428 // no direct functionality logic dependency in present UEFI cases.
429 //
430 return NULL;
431 }
432
433 /* Get a value from the current environment */
434 char *
435 secure_getenv (
436 const char *varname
437 )
438 {
439 //
440 // Null secure_getenv() function implementation to satisfy the linker, since
441 // there is no direct functionality logic dependency in present UEFI cases.
442 //
443 // From the secure_getenv() manual: 'just like getenv() except that it
444 // returns NULL in cases where "secure execution" is required'.
445 //
446 return NULL;
447 }
448
449 //
450 // -- Stream I/O Routines --
451 //
452
453 /* Write data to a stream */
454 size_t
455 fwrite (
456 const void *buffer,
457 size_t size,
458 size_t count,
459 FILE *stream
460 )
461 {
462 return 0;
463 }
464
465 //
466 // -- Dummy OpenSSL Support Routines --
467 //
468
469 int
470 BIO_printf (
471 void *bio,
472 const char *format,
473 ...
474 )
475 {
476 return 0;
477 }
478
479 int
480 BIO_snprintf (
481 char *buf,
482 size_t n,
483 const char *format,
484 ...
485 )
486 {
487 return 0;
488 }
489
490 #ifdef __GNUC__
491
492 typedef
493 VOID
494 (EFIAPI *NoReturnFuncPtr)(
495 VOID
496 ) __attribute__ ((__noreturn__));
497
498 STATIC
499 VOID
500 EFIAPI
501 NopFunction (
502 VOID
503 )
504 {
505 }
506
507 void
508 abort (
509 void
510 )
511 {
512 NoReturnFuncPtr NoReturnFunc;
513
514 NoReturnFunc = (NoReturnFuncPtr)NopFunction;
515
516 NoReturnFunc ();
517 }
518
519 #else
520
521 void
522 abort (
523 void
524 )
525 {
526 // Do nothing
527 }
528
529 #endif
530
531 int
532 fclose (
533 FILE *f
534 )
535 {
536 return 0;
537 }
538
539 FILE *
540 fopen (
541 const char *c,
542 const char *m
543 )
544 {
545 return NULL;
546 }
547
548 size_t
549 fread (
550 void *b,
551 size_t c,
552 size_t i,
553 FILE *f
554 )
555 {
556 return 0;
557 }
558
559 uid_t
560 getuid (
561 void
562 )
563 {
564 return 0;
565 }
566
567 uid_t
568 geteuid (
569 void
570 )
571 {
572 return 0;
573 }
574
575 gid_t
576 getgid (
577 void
578 )
579 {
580 return 0;
581 }
582
583 gid_t
584 getegid (
585 void
586 )
587 {
588 return 0;
589 }
590
591 int
592 printf (
593 char const *fmt,
594 ...
595 )
596 {
597 return 0;
598 }