2 C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based
5 Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <CrtLibSupport.h>
32 // Duplicated from EDKII BaseSortLib for qsort() wrapper
37 IN OUT VOID
*BufferToSort
,
39 IN CONST UINTN ElementSize
,
40 IN SORT_COMPARE CompareFunction
,
46 UINTN NextSwapLocation
;
48 ASSERT(BufferToSort
!= NULL
);
49 ASSERT(CompareFunction
!= NULL
);
50 ASSERT(Buffer
!= NULL
);
52 if (Count
< 2 || ElementSize
< 1) {
59 // Pick a pivot (we choose last element)
61 Pivot
= ((UINT8
*)BufferToSort
+ ((Count
- 1) * ElementSize
));
64 // Now get the pivot such that all on "left" are below it
65 // and everything "right" are above it
67 for (LoopCount
= 0; LoopCount
< Count
- 1; LoopCount
++)
70 // If the element is less than the pivot
72 if (CompareFunction ((VOID
*)((UINT8
*)BufferToSort
+ ((LoopCount
) * ElementSize
)), Pivot
) <= 0) {
76 CopyMem (Buffer
, (UINT8
*)BufferToSort
+ (NextSwapLocation
* ElementSize
), ElementSize
);
77 CopyMem ((UINT8
*)BufferToSort
+ (NextSwapLocation
* ElementSize
), (UINT8
*)BufferToSort
+ ((LoopCount
) * ElementSize
), ElementSize
);
78 CopyMem ((UINT8
*)BufferToSort
+ ((LoopCount
) * ElementSize
), Buffer
, ElementSize
);
81 // Increment NextSwapLocation
87 // Swap pivot to its final position (NextSwapLocation)
89 CopyMem (Buffer
, Pivot
, ElementSize
);
90 CopyMem (Pivot
, (UINT8
*)BufferToSort
+ (NextSwapLocation
* ElementSize
), ElementSize
);
91 CopyMem ((UINT8
*)BufferToSort
+ (NextSwapLocation
* ElementSize
), Buffer
, ElementSize
);
94 // Now recurse on 2 partial lists. Neither of these will have the 'pivot' element.
95 // IE list is sorted left half, pivot element, sorted right half...
106 (UINT8
*)BufferToSort
+ (NextSwapLocation
+ 1) * ElementSize
,
107 Count
- NextSwapLocation
- 1,
116 //---------------------------------------------------------
117 // Standard C Run-time Library Interface Wrapper
118 //---------------------------------------------------------
121 // -- String Manipulation Routines --
124 /* Scan a string for the last occurrence of a character */
125 char *strrchr (const char *str
, int c
)
129 for (save
= NULL
; ; ++str
) {
139 /* Compare first n bytes of string s1 with string s2, ignoring case */
140 int strncasecmp (const char *s1
, const char *s2
, size_t n
)
149 Val
= tolower(*s1
) - tolower(*s2
);
163 /* Read formatted data from a string */
164 int sscanf (const char *buffer
, const char *format
, ...)
167 // Null sscanf() function implementation to satisfy the linker, since
168 // no direct functionality logic dependency in present UEFI cases.
173 /* Maps errnum to an error-message string */
174 char * strerror (int errnum
)
179 /* Computes the length of the maximum initial segment of the string pointed to by s1
180 which consists entirely of characters from the string pointed to by s2. */
181 size_t strspn (const char *s1
, const char *s2
)
187 for (Index
= 0; Index
< 32; Index
++) {
192 Map
[*s2
>> 3] |= (1 << (*s2
& 7));
198 while (Map
[*s1
>> 3] & (1 << (*s1
& 7))) {
209 /* Computes the length of the maximum initial segment of the string pointed to by s1
210 which consists entirely of characters not from the string pointed to by s2. */
211 size_t strcspn (const char *s1
, const char *s2
)
217 for (Index
= 0; Index
< 32; Index
++) {
222 Map
[*s2
>> 3] |= (1 << (*s2
& 7));
229 while (!(Map
[*s1
>> 3] & (1 << (*s1
& 7)))) {
238 // -- Character Classification Routines --
241 /* Determines if a particular character is a decimal-digit character */
247 return (('0' <= (c
)) && ((c
) <= '9'));
250 /* Determine if an integer represents character that is a hex digit */
254 // <hexdigit> ::= [0-9] | [a-f] | [A-F]
256 return ((('0' <= (c
)) && ((c
) <= '9')) ||
257 (('a' <= (c
)) && ((c
) <= 'f')) ||
258 (('A' <= (c
)) && ((c
) <= 'F')));
261 /* Determines if a particular character represents a space character */
270 /* Determine if a particular character is an alphanumeric character */
274 // <alnum> ::= [0-9] | [a-z] | [A-Z]
276 return ((('0' <= (c
)) && ((c
) <= '9')) ||
277 (('a' <= (c
)) && ((c
) <= 'z')) ||
278 (('A' <= (c
)) && ((c
) <= 'Z')));
281 /* Determines if a particular character is in upper case */
285 // <uppercase letter> := [A-Z]
287 return (('A' <= (c
)) && ((c
) <= 'Z'));
291 // -- Data Conversion Routines --
294 /* Convert strings to a long-integer value */
295 long strtol (const char *nptr
, char **endptr
, int base
)
298 // Null strtol() function implementation to satisfy the linker, since there is
299 // no direct functionality logic dependency in present UEFI cases.
304 /* Convert strings to an unsigned long-integer value */
305 unsigned long strtoul (const char *nptr
, char **endptr
, int base
)
308 // Null strtoul() function implementation to satisfy the linker, since there is
309 // no direct functionality logic dependency in present UEFI cases.
314 /* Convert character to lowercase */
317 if (('A' <= (c
)) && ((c
) <= 'Z')) {
318 return (c
- ('A' - 'a'));
324 // -- Searching and Sorting Routines --
327 /* Performs a quick sort */
328 void qsort (void *base
, size_t num
, size_t width
, int (*compare
)(const void *, const void *))
332 ASSERT (base
!= NULL
);
333 ASSERT (compare
!= NULL
);
336 // Use CRT-style malloc to cover BS and RT memory allocation.
338 Buffer
= malloc (width
);
339 ASSERT (Buffer
!= NULL
);
342 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
344 QuickSortWorker (base
, (UINTN
)num
, (UINTN
)width
, (SORT_COMPARE
)compare
, Buffer
);
351 // -- Process and Environment Control Routines --
354 /* Get a value from the current environment */
355 char *getenv (const char *varname
)
358 // Null getenv() function implementation to satisfy the linker, since there is
359 // no direct functionality logic dependency in present UEFI cases.
364 /* Get a value from the current environment */
365 char *secure_getenv (const char *varname
)
368 // Null secure_getenv() function implementation to satisfy the linker, since
369 // there is no direct functionality logic dependency in present UEFI cases.
371 // From the secure_getenv() manual: 'just like getenv() except that it
372 // returns NULL in cases where "secure execution" is required'.
378 // -- Stream I/O Routines --
381 /* Write data to a stream */
382 size_t fwrite (const void *buffer
, size_t size
, size_t count
, FILE *stream
)
388 // -- Dummy OpenSSL Support Routines --
391 int BIO_printf (void *bio
, const char *format
, ...)
396 int BIO_snprintf(char *buf
, size_t n
, const char *format
, ...)
405 (EFIAPI
*NoReturnFuncPtr
)(
407 ) __attribute__((__noreturn__
));
420 NoReturnFuncPtr NoReturnFunc
;
422 NoReturnFunc
= (NoReturnFuncPtr
) NopFunction
;
441 FILE *fopen (const char *c
, const char *m
)
446 size_t fread (void *b
, size_t c
, size_t i
, FILE *f
)
471 int printf (char const *fmt
, ...)