1 /** @file
2   C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based
3   Cryptographic Library.
4 
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
10 
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.
13 
14 **/
15 
16 #include <OpenSslSupport.h>
17 
18 int errno = 0;
19 
20 FILE  *stderr = NULL;
21 FILE  *stdin  = NULL;
22 FILE  *stdout = NULL;
23 
24 typedef
25 INTN
26 (*SORT_COMPARE)(
27   IN  VOID  *Buffer1,
28   IN  VOID  *Buffer2
29   );
30 
31 //
32 // Duplicated from EDKII BaseSortLib for qsort() wrapper
33 //
34 STATIC
35 VOID
QuickSortWorker(IN OUT VOID * BufferToSort,IN CONST UINTN Count,IN CONST UINTN ElementSize,IN SORT_COMPARE CompareFunction,IN VOID * Buffer)36 QuickSortWorker (
37   IN OUT    VOID          *BufferToSort,
38   IN CONST  UINTN         Count,
39   IN CONST  UINTN         ElementSize,
40   IN        SORT_COMPARE  CompareFunction,
41   IN        VOID          *Buffer
42   )
43 {
44   VOID        *Pivot;
45   UINTN       LoopCount;
46   UINTN       NextSwapLocation;
47 
48   ASSERT(BufferToSort    != NULL);
49   ASSERT(CompareFunction != NULL);
50   ASSERT(Buffer          != NULL);
51 
52   if (Count < 2 || ElementSize  < 1) {
53     return;
54   }
55 
56   NextSwapLocation = 0;
57 
58   //
59   // Pick a pivot (we choose last element)
60   //
61   Pivot = ((UINT8 *)BufferToSort + ((Count - 1) * ElementSize));
62 
63   //
64   // Now get the pivot such that all on "left" are below it
65   // and everything "right" are above it
66   //
67   for (LoopCount = 0; LoopCount < Count - 1;  LoopCount++)
68   {
69     //
70     // If the element is less than the pivot
71     //
72     if (CompareFunction ((VOID *)((UINT8 *)BufferToSort + ((LoopCount) * ElementSize)), Pivot) <= 0) {
73       //
74       // Swap
75       //
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);
79 
80       //
81       // Increment NextSwapLocation
82       //
83       NextSwapLocation++;
84     }
85   }
86   //
87   // Swap pivot to it's final position (NextSwapLocaiton)
88   //
89   CopyMem (Buffer, Pivot, ElementSize);
90   CopyMem (Pivot, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);
91   CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), Buffer, ElementSize);
92 
93   //
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...
96   //
97   QuickSortWorker (
98     BufferToSort,
99     NextSwapLocation,
100     ElementSize,
101     CompareFunction,
102     Buffer
103     );
104 
105   QuickSortWorker (
106     (UINT8 *)BufferToSort + (NextSwapLocation + 1) * ElementSize,
107     Count - NextSwapLocation - 1,
108     ElementSize,
109     CompareFunction,
110     Buffer
111     );
112 
113   return;
114 }
115 
116 //---------------------------------------------------------
117 // Standard C Run-time Library Interface Wrapper
118 //---------------------------------------------------------
119 
120 //
121 // -- String Manipulation Routines --
122 //
123 
124 /* Scan a string for the last occurrence of a character */
strrchr(const char * str,int c)125 char *strrchr (const char *str, int c)
126 {
127   char * save;
128 
129   for (save = NULL; ; ++str) {
130     if (*str == c) {
131       save = (char *)str;
132     }
133     if (*str == 0) {
134       return (save);
135     }
136   }
137 }
138 
139 /* Read formatted data from a string */
sscanf(const char * buffer,const char * format,...)140 int sscanf (const char *buffer, const char *format, ...)
141 {
142   //
143   // Null sscanf() function implementation to satisfy the linker, since
144   // no direct functionality logic dependency in present UEFI cases.
145   //
146   return 0;
147 }
148 
149 //
150 // -- Character Classification Routines --
151 //
152 
153 /* Determines if a particular character is a decimal-digit character */
isdigit(int c)154 int isdigit (int c)
155 {
156   //
157   // <digit> ::= [0-9]
158   //
159   return (('0' <= (c)) && ((c) <= '9'));
160 }
161 
162 /* Determine if an integer represents character that is a hex digit */
isxdigit(int c)163 int isxdigit (int c)
164 {
165   //
166   // <hexdigit> ::= [0-9] | [a-f] | [A-F]
167   //
168   return ((('0' <= (c)) && ((c) <= '9')) ||
169           (('a' <= (c)) && ((c) <= 'f')) ||
170           (('A' <= (c)) && ((c) <= 'F')));
171 }
172 
173 /* Determines if a particular character represents a space character */
isspace(int c)174 int isspace (int c)
175 {
176   //
177   // <space> ::= [ ]
178   //
179   return ((c) == ' ');
180 }
181 
182 /* Determine if a particular character is an alphanumeric character */
isalnum(int c)183 int isalnum (int c)
184 {
185   //
186   // <alnum> ::= [0-9] | [a-z] | [A-Z]
187   //
188   return ((('0' <= (c)) && ((c) <= '9')) ||
189           (('a' <= (c)) && ((c) <= 'z')) ||
190           (('A' <= (c)) && ((c) <= 'Z')));
191 }
192 
193 /* Determines if a particular character is in upper case */
isupper(int c)194 int isupper (int c)
195 {
196   //
197   // <uppercase letter> := [A-Z]
198   //
199   return (('A' <= (c)) && ((c) <= 'Z'));
200 }
201 
202 //
203 // -- Data Conversion Routines --
204 //
205 
206 /* Convert strings to a long-integer value */
strtol(const char * nptr,char ** endptr,int base)207 long strtol (const char *nptr, char **endptr, int base)
208 {
209   //
210   // Null strtol() function implementation to satisfy the linker, since there is
211   // no direct functionality logic dependency in present UEFI cases.
212   //
213   return 0;
214 }
215 
216 /* Convert strings to an unsigned long-integer value */
strtoul(const char * nptr,char ** endptr,int base)217 unsigned long strtoul (const char *nptr, char **endptr, int base)
218 {
219   //
220   // Null strtoul() function implementation to satisfy the linker, since there is
221   // no direct functionality logic dependency in present UEFI cases.
222   //
223   return 0;
224 }
225 
226 /* Convert character to lowercase */
tolower(int c)227 int tolower (int c)
228 {
229   if (('A' <= (c)) && ((c) <= 'Z')) {
230     return (c - ('A' - 'a'));
231   }
232   return (c);
233 }
234 
235 //
236 // -- Searching and Sorting Routines --
237 //
238 
239 /* Performs a quick sort */
qsort(void * base,size_t num,size_t width,int (* compare)(const void *,const void *))240 void qsort (void *base, size_t num, size_t width, int (*compare)(const void *, const void *))
241 {
242   VOID  *Buffer;
243 
244   ASSERT (base    != NULL);
245   ASSERT (compare != NULL);
246 
247   //
248   // Use CRT-style malloc to cover BS and RT memory allocation.
249   //
250   Buffer = malloc (width);
251   ASSERT (Buffer != NULL);
252 
253   //
254   // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
255   //
256   QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);
257 
258   free (Buffer);
259   return;
260 }
261 
262 //
263 // -- Process and Environment Control Routines --
264 //
265 
266 /* Get a value from the current environment */
getenv(const char * varname)267 char *getenv (const char *varname)
268 {
269   //
270   // Null getenv() function implementation to satisfy the linker, since there is
271   // no direct functionality logic dependency in present UEFI cases.
272   //
273   return NULL;
274 }
275 
276 //
277 // -- Stream I/O Routines --
278 //
279 
280 /* Write formatted output using a pointer to a list of arguments */
vfprintf(FILE * stream,const char * format,VA_LIST arg)281 int vfprintf (FILE *stream, const char *format, VA_LIST arg)
282 {
283   return 0;
284 }
285 
286 /* Write data to a stream */
fwrite(const void * buffer,size_t size,size_t count,FILE * stream)287 size_t fwrite (const void *buffer, size_t size, size_t count, FILE *stream)
288 {
289   return 0;
290 }
291 
292 //
293 //  -- Dummy OpenSSL Support Routines --
294 //
295 
BIO_printf(void * bio,const char * format,...)296 int BIO_printf (void *bio, const char *format, ...)
297 {
298   return 0;
299 }
300 
BIO_snprintf(char * buf,size_t n,const char * format,...)301 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
302 {
303   return 0;
304 }
305 
UI_OpenSSL(void)306 void *UI_OpenSSL(void)
307 {
308   return NULL;
309 }
310 
X509_load_cert_file(VOID * ctx,const char * file,int type)311 int X509_load_cert_file (VOID *ctx, const char *file, int type)
312 {
313   return 0;
314 }
315 
X509_load_crl_file(VOID * ctx,const char * file,int type)316 int X509_load_crl_file (VOID *ctx, const char *file, int type)
317 {
318   return 0;
319 }
320 
chmod(const char * c,mode_t m)321 int chmod (const char *c, mode_t m)
322 {
323   return -1;
324 }
325 
close(int f)326 int close (int f)
327 {
328   return -1;
329 }
330 
closelog(void)331 void closelog (void)
332 {
333 
334 }
335 
336 #ifdef __GNUC__
337 
338 typedef
339 VOID
340 (EFIAPI *NoReturnFuncPtr)(
341   VOID
342   ) __attribute__((__noreturn__));
343 
344 
345 STATIC
346 VOID
347 EFIAPI
NopFunction(VOID)348 NopFunction (
349   VOID
350   )
351 {
352 }
353 
354 
exit(int e)355 void exit (int e)
356 {
357   NoReturnFuncPtr NoReturnFunc;
358 
359   NoReturnFunc = (NoReturnFuncPtr) NopFunction;
360 
361   NoReturnFunc ();
362 }
363 
364 #else
365 
exit(int e)366 void exit (int e)
367 {
368 }
369 
370 #endif
371 
fclose(FILE * f)372 int fclose (FILE *f)
373 {
374   return 0;
375 }
376 
fopen(const char * c,const char * m)377 FILE *fopen (const char *c, const char *m)
378 {
379   return NULL;
380 }
381 
fread(void * b,size_t c,size_t i,FILE * f)382 size_t fread (void *b, size_t c, size_t i, FILE *f)
383 {
384   return 0;
385 }
386 
fprintf(FILE * f,const char * s,...)387 int fprintf (FILE *f, const char *s, ...)
388 {
389   return 0;
390 }
391 
getuid(void)392 uid_t getuid (void)
393 {
394   return 0;
395 }
396 
geteuid(void)397 uid_t geteuid (void)
398 {
399   return 0;
400 }
401 
getgid(void)402 gid_t getgid (void)
403 {
404   return 0;
405 }
406 
getegid(void)407 gid_t getegid (void)
408 {
409   return 0;
410 }
411 
lseek(int a,off_t o,int d)412 off_t lseek (int a, off_t o, int d)
413 {
414   return 0;
415 }
416 
openlog(const char * c,int a,int b)417 void openlog (const char *c, int a, int b)
418 {
419 
420 }
421 
read(int f,void * b,size_t c)422 ssize_t read (int f, void *b, size_t c)
423 {
424   return 0;
425 }
426 
stat(const char * c,struct stat * s)427 int stat (const char *c, struct stat *s)
428 {
429   return -1;
430 }
431 
strcasecmp(const char * c,const char * s)432 int strcasecmp (const char *c, const char *s)
433 {
434   return 0;
435 }
436 
strncasecmp(const char * c,const char * s,size_t l)437 int strncasecmp (const char *c, const char *s, size_t l)
438 {
439   return 0;
440 }
441 
syslog(int a,const char * c,...)442 void syslog (int a, const char *c, ...)
443 {
444 
445 }
446 
write(int f,const void * b,size_t l)447 ssize_t write (int f, const void *b, size_t l)
448 {
449   return 0;
450 }
451