2 C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based
\r
3 Cryptographic Library.
\r
5 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
\r
6 This program and the accompanying materials
\r
7 are licensed and made available under the terms and conditions of the BSD License
\r
8 which accompanies this distribution. The full text of the license may be found at
\r
9 http://opensource.org/licenses/bsd-license.php
\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
\r
16 #include <OpenSslSupport.h>
\r
20 FILE *stderr = NULL;
\r
22 FILE *stdout = NULL;
\r
32 // Duplicated from EDKII BaseSortLib for qsort() wrapper
\r
37 IN OUT VOID *BufferToSort,
\r
38 IN CONST UINTN Count,
\r
39 IN CONST UINTN ElementSize,
\r
40 IN SORT_COMPARE CompareFunction,
\r
46 UINTN NextSwapLocation;
\r
48 ASSERT(BufferToSort != NULL);
\r
49 ASSERT(CompareFunction != NULL);
\r
50 ASSERT(Buffer != NULL);
\r
52 if (Count < 2 || ElementSize < 1) {
\r
56 NextSwapLocation = 0;
\r
59 // Pick a pivot (we choose last element)
\r
61 Pivot = ((UINT8 *)BufferToSort + ((Count - 1) * ElementSize));
\r
64 // Now get the pivot such that all on "left" are below it
\r
65 // and everything "right" are above it
\r
67 for (LoopCount = 0; LoopCount < Count - 1; LoopCount++)
\r
70 // If the element is less than the pivot
\r
72 if (CompareFunction ((VOID *)((UINT8 *)BufferToSort + ((LoopCount) * ElementSize)), Pivot) <= 0) {
\r
76 CopyMem (Buffer, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);
\r
77 CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), (UINT8 *)BufferToSort + ((LoopCount) * ElementSize), ElementSize);
\r
78 CopyMem ((UINT8 *)BufferToSort + ((LoopCount) * ElementSize), Buffer, ElementSize);
\r
81 // Increment NextSwapLocation
\r
87 // Swap pivot to it's final position (NextSwapLocaiton)
\r
89 CopyMem (Buffer, Pivot, ElementSize);
\r
90 CopyMem (Pivot, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);
\r
91 CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), Buffer, ElementSize);
\r
94 // Now recurse on 2 paritial lists. Neither of these will have the 'pivot' element.
\r
95 // IE list is sorted left half, pivot element, sorted right half...
\r
106 (UINT8 *)BufferToSort + (NextSwapLocation + 1) * ElementSize,
\r
107 Count - NextSwapLocation - 1,
\r
116 //---------------------------------------------------------
\r
117 // Standard C Run-time Library Interface Wrapper
\r
118 //---------------------------------------------------------
\r
121 // -- String Manipulation Routines --
\r
124 /* Scan a string for the last occurrence of a character */
\r
125 char *strrchr (const char *str, int c)
\r
129 for (save = NULL; ; ++str) {
\r
131 save = (char *)str;
\r
139 /* Read formatted data from a string */
\r
140 int sscanf (const char *buffer, const char *format, ...)
\r
143 // Null sscanf() function implementation to satisfy the linker, since
\r
144 // no direct functionality logic dependency in present UEFI cases.
\r
150 // -- Character Classification Routines --
\r
153 /* Determines if a particular character is a decimal-digit character */
\r
154 int isdigit (int c)
\r
157 // <digit> ::= [0-9]
\r
159 return (('0' <= (c)) && ((c) <= '9'));
\r
162 /* Determine if an integer represents character that is a hex digit */
\r
163 int isxdigit (int c)
\r
166 // <hexdigit> ::= [0-9] | [a-f] | [A-F]
\r
168 return ((('0' <= (c)) && ((c) <= '9')) ||
\r
169 (('a' <= (c)) && ((c) <= 'f')) ||
\r
170 (('A' <= (c)) && ((c) <= 'F')));
\r
173 /* Determines if a particular character represents a space character */
\r
174 int isspace (int c)
\r
179 return ((c) == ' ');
\r
182 /* Determine if a particular character is an alphanumeric character */
\r
183 int isalnum (int c)
\r
186 // <alnum> ::= [0-9] | [a-z] | [A-Z]
\r
188 return ((('0' <= (c)) && ((c) <= '9')) ||
\r
189 (('a' <= (c)) && ((c) <= 'z')) ||
\r
190 (('A' <= (c)) && ((c) <= 'Z')));
\r
193 /* Determines if a particular character is in upper case */
\r
194 int isupper (int c)
\r
197 // <uppercase letter> := [A-Z]
\r
199 return (('A' <= (c)) && ((c) <= 'Z'));
\r
203 // -- Data Conversion Routines --
\r
206 /* Convert strings to a long-integer value */
\r
207 long strtol (const char *nptr, char **endptr, int base)
\r
210 // Null strtol() function implementation to satisfy the linker, since there is
\r
211 // no direct functionality logic dependency in present UEFI cases.
\r
216 /* Convert strings to an unsigned long-integer value */
\r
217 unsigned long strtoul (const char *nptr, char **endptr, int base)
\r
220 // Null strtoul() function implementation to satisfy the linker, since there is
\r
221 // no direct functionality logic dependency in present UEFI cases.
\r
226 /* Convert character to lowercase */
\r
227 int tolower (int c)
\r
229 if (('A' <= (c)) && ((c) <= 'Z')) {
\r
230 return (c - ('A' - 'a'));
\r
236 // -- Searching and Sorting Routines --
\r
239 /* Performs a quick sort */
\r
240 void qsort (void *base, size_t num, size_t width, int (*compare)(const void *, const void *))
\r
244 ASSERT (base != NULL);
\r
245 ASSERT (compare != NULL);
\r
248 // Use CRT-style malloc to cover BS and RT memory allocation.
\r
250 Buffer = malloc (width);
\r
251 ASSERT (Buffer != NULL);
\r
254 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
\r
256 QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);
\r
263 // -- Process and Environment Control Routines --
\r
266 /* Get a value from the current environment */
\r
267 char *getenv (const char *varname)
\r
270 // Null getenv() function implementation to satisfy the linker, since there is
\r
271 // no direct functionality logic dependency in present UEFI cases.
\r
277 // -- Stream I/O Routines --
\r
280 /* Write formatted output using a pointer to a list of arguments */
\r
281 int vfprintf (FILE *stream, const char *format, VA_LIST arg)
\r
286 /* Write data to a stream */
\r
287 size_t fwrite (const void *buffer, size_t size, size_t count, FILE *stream)
\r
293 // -- Dummy OpenSSL Support Routines --
\r
296 int BIO_printf (void *bio, const char *format, ...)
\r
301 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
\r
306 void *UI_OpenSSL(void)
\r
311 int X509_load_cert_file (VOID *ctx, const char *file, int type)
\r
316 int X509_load_crl_file (VOID *ctx, const char *file, int type)
\r
321 int chmod (const char *c, mode_t m)
\r
331 void closelog (void)
\r
340 (EFIAPI *NoReturnFuncPtr)(
\r
342 ) __attribute__((__noreturn__));
\r
357 NoReturnFuncPtr NoReturnFunc;
\r
359 NoReturnFunc = (NoReturnFuncPtr) NopFunction;
\r
372 int fclose (FILE *f)
\r
377 FILE *fopen (const char *c, const char *m)
\r
382 size_t fread (void *b, size_t c, size_t i, FILE *f)
\r
387 int fprintf (FILE *f, const char *s, ...)
\r
392 uid_t getuid (void)
\r
397 uid_t geteuid (void)
\r
402 gid_t getgid (void)
\r
407 gid_t getegid (void)
\r
412 off_t lseek (int a, off_t o, int d)
\r
417 void openlog (const char *c, int a, int b)
\r
422 ssize_t read (int f, void *b, size_t c)
\r
427 int stat (const char *c, struct stat *s)
\r
432 int strcasecmp (const char *c, const char *s)
\r
437 int strncasecmp (const char *c, const char *s, size_t l)
\r
442 void syslog (int a, const char *c, ...)
\r
447 ssize_t write (int f, const void *b, size_t l)
\r