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 /* Scan a string for the last occurrence of a character */
119 char *strrchr (const char *str
, int c
)
123 for (save
= NULL
; ; ++str
) {
133 /* Compare first n bytes of string s1 with string s2, ignoring case */
134 int strncasecmp (const char *s1
, const char *s2
, size_t n
)
143 Val
= tolower(*s1
) - tolower(*s2
);
157 /* Read formatted data from a string */
158 int sscanf (const char *buffer
, const char *format
, ...)
161 // Null sscanf() function implementation to satisfy the linker, since
162 // no direct functionality logic dependency in present UEFI cases.
167 /* Maps errnum to an error-message string */
168 char * strerror (int errnum
)
173 /* Computes the length of the maximum initial segment of the string pointed to by s1
174 which consists entirely of characters from the string pointed to by s2. */
175 size_t strspn (const char *s1
, const char *s2
)
181 for (Index
= 0; Index
< 32; Index
++) {
186 Map
[*s2
>> 3] |= (1 << (*s2
& 7));
192 while (Map
[*s1
>> 3] & (1 << (*s1
& 7))) {
203 /* Computes the length of the maximum initial segment of the string pointed to by s1
204 which consists entirely of characters not from the string pointed to by s2. */
205 size_t strcspn (const char *s1
, const char *s2
)
211 for (Index
= 0; Index
< 32; Index
++) {
216 Map
[*s2
>> 3] |= (1 << (*s2
& 7));
223 while (!(Map
[*s1
>> 3] & (1 << (*s1
& 7)))) {
232 // -- Character Classification Routines --
235 /* Determines if a particular character is a decimal-digit character */
241 return (('0' <= (c
)) && ((c
) <= '9'));
244 /* Determine if an integer represents character that is a hex digit */
248 // <hexdigit> ::= [0-9] | [a-f] | [A-F]
250 return ((('0' <= (c
)) && ((c
) <= '9')) ||
251 (('a' <= (c
)) && ((c
) <= 'f')) ||
252 (('A' <= (c
)) && ((c
) <= 'F')));
255 /* Determines if a particular character represents a space character */
264 /* Determine if a particular character is an alphanumeric character */
268 // <alnum> ::= [0-9] | [a-z] | [A-Z]
270 return ((('0' <= (c
)) && ((c
) <= '9')) ||
271 (('a' <= (c
)) && ((c
) <= 'z')) ||
272 (('A' <= (c
)) && ((c
) <= 'Z')));
275 /* Determines if a particular character is in upper case */
279 // <uppercase letter> := [A-Z]
281 return (('A' <= (c
)) && ((c
) <= 'Z'));
285 // -- Data Conversion Routines --
288 /* Convert strings to a long-integer value */
289 long strtol (const char *nptr
, char **endptr
, int base
)
292 // Null strtol() function implementation to satisfy the linker, since there is
293 // no direct functionality logic dependency in present UEFI cases.
298 /* Convert strings to an unsigned long-integer value */
299 unsigned long strtoul (const char *nptr
, char **endptr
, int base
)
302 // Null strtoul() function implementation to satisfy the linker, since there is
303 // no direct functionality logic dependency in present UEFI cases.
308 /* Convert character to lowercase */
311 if (('A' <= (c
)) && ((c
) <= 'Z')) {
312 return (c
- ('A' - 'a'));
318 // -- Searching and Sorting Routines --
321 /* Performs a quick sort */
322 void qsort (void *base
, size_t num
, size_t width
, int (*compare
)(const void *, const void *))
326 ASSERT (base
!= NULL
);
327 ASSERT (compare
!= NULL
);
330 // Use CRT-style malloc to cover BS and RT memory allocation.
332 Buffer
= malloc (width
);
333 ASSERT (Buffer
!= NULL
);
336 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
338 QuickSortWorker (base
, (UINTN
)num
, (UINTN
)width
, (SORT_COMPARE
)compare
, Buffer
);
345 // -- Process and Environment Control Routines --
348 /* Get a value from the current environment */
349 char *getenv (const char *varname
)
352 // Null getenv() function implementation to satisfy the linker, since there is
353 // no direct functionality logic dependency in present UEFI cases.
358 /* Get a value from the current environment */
359 char *secure_getenv (const char *varname
)
362 // Null secure_getenv() function implementation to satisfy the linker, since
363 // there is no direct functionality logic dependency in present UEFI cases.
365 // From the secure_getenv() manual: 'just like getenv() except that it
366 // returns NULL in cases where "secure execution" is required'.
372 // -- Stream I/O Routines --
375 /* Write data to a stream */
376 size_t fwrite (const void *buffer
, size_t size
, size_t count
, FILE *stream
)
382 // -- Dummy OpenSSL Support Routines --
385 int BIO_printf (void *bio
, const char *format
, ...)
390 int BIO_snprintf(char *buf
, size_t n
, const char *format
, ...)
399 (EFIAPI
*NoReturnFuncPtr
)(
401 ) __attribute__((__noreturn__
));
414 NoReturnFuncPtr NoReturnFunc
;
416 NoReturnFunc
= (NoReturnFuncPtr
) NopFunction
;
435 FILE *fopen (const char *c
, const char *m
)
440 size_t fread (void *b
, size_t c
, size_t i
, FILE *f
)
465 int printf (char const *fmt
, ...)