2 C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based
5 Copyright (c) 2009 - 2011, 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>
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 it's final position (NextSwapLocaiton)
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 paritial 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 /* Read formatted data from a string */
140 int sscanf (const char *buffer
, const char *format
, ...)
143 // Null sscanf() function implementation to satisfy the linker, since
144 // no direct functionality logic dependency in present UEFI cases.
150 // -- Character Classification Routines --
153 /* Determines if a particular character is a decimal-digit character */
159 return (('0' <= (c
)) && ((c
) <= '9'));
162 /* Determine if an integer represents character that is a hex digit */
166 // <hexdigit> ::= [0-9] | [a-f] | [A-F]
168 return ((('0' <= (c
)) && ((c
) <= '9')) ||
169 (('a' <= (c
)) && ((c
) <= 'f')) ||
170 (('A' <= (c
)) && ((c
) <= 'F')));
173 /* Determines if a particular character represents a space character */
182 /* Determine if a particular character is an alphanumeric character */
186 // <alnum> ::= [0-9] | [a-z] | [A-Z]
188 return ((('0' <= (c
)) && ((c
) <= '9')) ||
189 (('a' <= (c
)) && ((c
) <= 'z')) ||
190 (('A' <= (c
)) && ((c
) <= 'Z')));
193 /* Determines if a particular character is in upper case */
197 // <uppercase letter> := [A-Z]
199 return (('A' <= (c
)) && ((c
) <= 'Z'));
203 // -- Data Conversion Routines --
206 /* Convert strings to a long-integer value */
207 long strtol (const char *nptr
, char **endptr
, int base
)
210 // Null strtol() function implementation to satisfy the linker, since there is
211 // no direct functionality logic dependency in present UEFI cases.
216 /* Convert strings to an unsigned long-integer value */
217 unsigned long strtoul (const char *nptr
, char **endptr
, int base
)
220 // Null strtoul() function implementation to satisfy the linker, since there is
221 // no direct functionality logic dependency in present UEFI cases.
226 /* Convert character to lowercase */
229 if (('A' <= (c
)) && ((c
) <= 'Z')) {
230 return (c
- ('A' - 'a'));
236 // -- Searching and Sorting Routines --
239 /* Performs a quick sort */
240 void qsort (void *base
, size_t num
, size_t width
, int (*compare
)(const void *, const void *))
244 ASSERT (base
!= NULL
);
245 ASSERT (compare
!= NULL
);
248 // Use CRT-style malloc to cover BS and RT memory allocation.
250 Buffer
= malloc (width
);
251 ASSERT (Buffer
!= NULL
);
254 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
256 QuickSortWorker (base
, (UINTN
)num
, (UINTN
)width
, (SORT_COMPARE
)compare
, Buffer
);
263 // -- Process and Environment Control Routines --
266 /* Get a value from the current environment */
267 char *getenv (const char *varname
)
270 // Null getenv() function implementation to satisfy the linker, since there is
271 // no direct functionality logic dependency in present UEFI cases.
277 // -- Stream I/O Routines --
280 /* Write formatted output using a pointer to a list of arguments */
281 int vfprintf (FILE *stream
, const char *format
, VA_LIST arg
)
286 /* Write data to a stream */
287 size_t fwrite (const void *buffer
, size_t size
, size_t count
, FILE *stream
)
293 // -- Dummy OpenSSL Support Routines --
296 int BIO_printf (void *bio
, const char *format
, ...)
301 int BIO_snprintf(char *buf
, size_t n
, const char *format
, ...)
306 void *UI_OpenSSL(void)
311 int X509_load_cert_file (VOID
*ctx
, const char *file
, int type
)
316 int X509_load_crl_file (VOID
*ctx
, const char *file
, int type
)
321 int chmod (const char *c
, mode_t m
)
340 (EFIAPI
*NoReturnFuncPtr
)(
342 ) __attribute__((__noreturn__
));
357 NoReturnFuncPtr NoReturnFunc
;
359 NoReturnFunc
= (NoReturnFuncPtr
) NopFunction
;
377 FILE *fopen (const char *c
, const char *m
)
382 size_t fread (void *b
, size_t c
, size_t i
, FILE *f
)
387 int fprintf (FILE *f
, const char *s
, ...)
412 off_t
lseek (int a
, off_t o
, int d
)
417 void openlog (const char *c
, int a
, int b
)
422 ssize_t
read (int f
, void *b
, size_t c
)
427 int stat (const char *c
, struct stat
*s
)
432 int strcasecmp (const char *c
, const char *s
)
437 int strncasecmp (const char *c
, const char *s
, size_t l
)
442 void syslog (int a
, const char *c
, ...)
447 ssize_t
write (int f
, const void *b
, size_t l
)