2 C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based
5 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <CrtLibSupport.h>
26 // Duplicated from EDKII BaseSortLib for qsort() wrapper
31 IN OUT VOID
*BufferToSort
,
33 IN CONST UINTN ElementSize
,
34 IN SORT_COMPARE CompareFunction
,
40 UINTN NextSwapLocation
;
42 ASSERT(BufferToSort
!= NULL
);
43 ASSERT(CompareFunction
!= NULL
);
44 ASSERT(Buffer
!= NULL
);
46 if (Count
< 2 || ElementSize
< 1) {
53 // Pick a pivot (we choose last element)
55 Pivot
= ((UINT8
*)BufferToSort
+ ((Count
- 1) * ElementSize
));
58 // Now get the pivot such that all on "left" are below it
59 // and everything "right" are above it
61 for (LoopCount
= 0; LoopCount
< Count
- 1; LoopCount
++)
64 // If the element is less than the pivot
66 if (CompareFunction ((VOID
*)((UINT8
*)BufferToSort
+ ((LoopCount
) * ElementSize
)), Pivot
) <= 0) {
70 CopyMem (Buffer
, (UINT8
*)BufferToSort
+ (NextSwapLocation
* ElementSize
), ElementSize
);
71 CopyMem ((UINT8
*)BufferToSort
+ (NextSwapLocation
* ElementSize
), (UINT8
*)BufferToSort
+ ((LoopCount
) * ElementSize
), ElementSize
);
72 CopyMem ((UINT8
*)BufferToSort
+ ((LoopCount
) * ElementSize
), Buffer
, ElementSize
);
75 // Increment NextSwapLocation
81 // Swap pivot to its final position (NextSwapLocation)
83 CopyMem (Buffer
, Pivot
, ElementSize
);
84 CopyMem (Pivot
, (UINT8
*)BufferToSort
+ (NextSwapLocation
* ElementSize
), ElementSize
);
85 CopyMem ((UINT8
*)BufferToSort
+ (NextSwapLocation
* ElementSize
), Buffer
, ElementSize
);
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...
100 (UINT8
*)BufferToSort
+ (NextSwapLocation
+ 1) * ElementSize
,
101 Count
- NextSwapLocation
- 1,
110 //---------------------------------------------------------
111 // Standard C Run-time Library Interface Wrapper
112 //---------------------------------------------------------
115 // -- String Manipulation Routines --
118 char *strchr(const char *str
, int ch
)
120 return ScanMem8 (str
, AsciiStrSize (str
), (UINT8
)ch
);
123 /* Scan a string for the last occurrence of a character */
124 char *strrchr (const char *str
, int c
)
128 for (save
= NULL
; ; ++str
) {
138 /* Compare first n bytes of string s1 with string s2, ignoring case */
139 int strncasecmp (const char *s1
, const char *s2
, size_t n
)
148 Val
= tolower(*s1
) - tolower(*s2
);
162 /* Read formatted data from a string */
163 int sscanf (const char *buffer
, const char *format
, ...)
166 // Null sscanf() function implementation to satisfy the linker, since
167 // no direct functionality logic dependency in present UEFI cases.
172 /* Maps errnum to an error-message string */
173 char * strerror (int errnum
)
178 /* Computes the length of the maximum initial segment of the string pointed to by s1
179 which consists entirely of characters from the string pointed to by s2. */
180 size_t strspn (const char *s1
, const char *s2
)
186 for (Index
= 0; Index
< 32; Index
++) {
191 Map
[*s2
>> 3] |= (1 << (*s2
& 7));
197 while (Map
[*s1
>> 3] & (1 << (*s1
& 7))) {
208 /* Computes the length of the maximum initial segment of the string pointed to by s1
209 which consists entirely of characters not from the string pointed to by s2. */
210 size_t strcspn (const char *s1
, const char *s2
)
216 for (Index
= 0; Index
< 32; Index
++) {
221 Map
[*s2
>> 3] |= (1 << (*s2
& 7));
228 while (!(Map
[*s1
>> 3] & (1 << (*s1
& 7)))) {
237 // -- Character Classification Routines --
240 /* Determines if a particular character is a decimal-digit character */
246 return (('0' <= (c
)) && ((c
) <= '9'));
249 /* Determine if an integer represents character that is a hex digit */
253 // <hexdigit> ::= [0-9] | [a-f] | [A-F]
255 return ((('0' <= (c
)) && ((c
) <= '9')) ||
256 (('a' <= (c
)) && ((c
) <= 'f')) ||
257 (('A' <= (c
)) && ((c
) <= 'F')));
260 /* Determines if a particular character represents a space character */
269 /* Determine if a particular character is an alphanumeric character */
273 // <alnum> ::= [0-9] | [a-z] | [A-Z]
275 return ((('0' <= (c
)) && ((c
) <= '9')) ||
276 (('a' <= (c
)) && ((c
) <= 'z')) ||
277 (('A' <= (c
)) && ((c
) <= 'Z')));
280 /* Determines if a particular character is in upper case */
284 // <uppercase letter> := [A-Z]
286 return (('A' <= (c
)) && ((c
) <= 'Z'));
290 // -- Data Conversion Routines --
293 /* Convert strings to a long-integer value */
294 long strtol (const char *nptr
, char **endptr
, int base
)
297 // Null strtol() function implementation to satisfy the linker, since there is
298 // no direct functionality logic dependency in present UEFI cases.
303 /* Convert strings to an unsigned long-integer value */
304 unsigned long strtoul (const char *nptr
, char **endptr
, int base
)
307 // Null strtoul() function implementation to satisfy the linker, since there is
308 // no direct functionality logic dependency in present UEFI cases.
313 /* Convert character to lowercase */
316 if (('A' <= (c
)) && ((c
) <= 'Z')) {
317 return (c
- ('A' - 'a'));
323 // -- Searching and Sorting Routines --
326 /* Performs a quick sort */
327 void qsort (void *base
, size_t num
, size_t width
, int (*compare
)(const void *, const void *))
331 ASSERT (base
!= NULL
);
332 ASSERT (compare
!= NULL
);
335 // Use CRT-style malloc to cover BS and RT memory allocation.
337 Buffer
= malloc (width
);
338 ASSERT (Buffer
!= NULL
);
341 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
343 QuickSortWorker (base
, (UINTN
)num
, (UINTN
)width
, (SORT_COMPARE
)compare
, Buffer
);
350 // -- Process and Environment Control Routines --
353 /* Get a value from the current environment */
354 char *getenv (const char *varname
)
357 // Null getenv() function implementation to satisfy the linker, since there is
358 // no direct functionality logic dependency in present UEFI cases.
363 /* Get a value from the current environment */
364 char *secure_getenv (const char *varname
)
367 // Null secure_getenv() function implementation to satisfy the linker, since
368 // there is no direct functionality logic dependency in present UEFI cases.
370 // From the secure_getenv() manual: 'just like getenv() except that it
371 // returns NULL in cases where "secure execution" is required'.
377 // -- Stream I/O Routines --
380 /* Write data to a stream */
381 size_t fwrite (const void *buffer
, size_t size
, size_t count
, FILE *stream
)
387 // -- Dummy OpenSSL Support Routines --
390 int BIO_printf (void *bio
, const char *format
, ...)
395 int BIO_snprintf(char *buf
, size_t n
, const char *format
, ...)
404 (EFIAPI
*NoReturnFuncPtr
)(
406 ) __attribute__((__noreturn__
));
419 NoReturnFuncPtr NoReturnFunc
;
421 NoReturnFunc
= (NoReturnFuncPtr
) NopFunction
;
440 FILE *fopen (const char *c
, const char *m
)
445 size_t fread (void *b
, size_t c
, size_t i
, FILE *f
)
470 int printf (char const *fmt
, ...)