2 C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based
5 Copyright (c) 2009 - 2010, 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 <OpenSslSupport.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 it's final position (NextSwapLocaiton)
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 paritial 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 /* Read formatted data from a string */
134 int sscanf (const char *buffer
, const char *format
, ...)
137 // Null sscanf() function implementation to satisfy the linker, since
138 // no direct functionality logic dependency in present UEFI cases.
144 // -- Character Classification Routines --
147 /* Determines if a particular character is a decimal-digit character */
153 return (('0' <= (c
)) && ((c
) <= '9'));
156 /* Determine if an integer represents character that is a hex digit */
160 // <hexdigit> ::= [0-9] | [a-f] | [A-F]
162 return ((('0' <= (c
)) && ((c
) <= '9')) ||
163 (('a' <= (c
)) && ((c
) <= 'f')) ||
164 (('A' <= (c
)) && ((c
) <= 'F')));
167 /* Determines if a particular character represents a space character */
176 /* Determine if a particular character is an alphanumeric character */
180 // <alnum> ::= [0-9] | [a-z] | [A-Z]
182 return ((('0' <= (c
)) && ((c
) <= '9')) ||
183 (('a' <= (c
)) && ((c
) <= 'z')) ||
184 (('A' <= (c
)) && ((c
) <= 'Z')));
187 /* Determines if a particular character is in upper case */
191 // <uppercase letter> := [A-Z]
193 return (('A' <= (c
)) && ((c
) <= 'Z'));
197 // -- Data Conversion Routines --
200 /* Convert strings to a long-integer value */
201 long strtol (const char *nptr
, char **endptr
, int base
)
204 // Null strtol() function implementation to satisfy the linker, since there is
205 // no direct functionality logic dependency in present UEFI cases.
210 /* Convert strings to an unsigned long-integer value */
211 unsigned long strtoul (const char *nptr
, char **endptr
, int base
)
214 // Null strtoul() function implementation to satisfy the linker, since there is
215 // no direct functionality logic dependency in present UEFI cases.
220 /* Convert character to lowercase */
223 if (('A' <= (c
)) && ((c
) <= 'Z')) {
224 return (c
- ('A' - 'a'));
230 // -- Searching and Sorting Routines --
233 /* Performs a quick sort */
234 void qsort (void *base
, size_t num
, size_t width
, int (*compare
)(const void *, const void *))
238 ASSERT (base
!= NULL
);
239 ASSERT (compare
!= NULL
);
241 Buffer
= AllocatePool (width
);
242 ASSERT (Buffer
!= NULL
);
245 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
247 QuickSortWorker (base
, (UINTN
)num
, (UINTN
)width
, (SORT_COMPARE
)compare
, Buffer
);
254 // -- Process and Environment Control Routines --
257 /* Get a value from the current environment */
258 char *getenv (const char *varname
)
261 // Null getenv() function implementation to satisfy the linker, since there is
262 // no direct functionality logic dependency in present UEFI cases.
268 // -- Stream I/O Routines --
271 /* Write formatted output using a pointer to a list of arguments */
272 int vfprintf (FILE *stream
, const char *format
, VA_LIST arg
)
277 /* Write data to a stream */
278 size_t fwrite (const void *buffer
, size_t size
, size_t count
, FILE *stream
)