]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c
7b3dda70e0941242e5b53af89d9bb878aaeca6df
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / SysCall / CrtWrapper.c
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
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 Buffer = AllocatePool (width);
248 ASSERT (Buffer != NULL);
249
250 //
251 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
252 //
253 QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);
254
255 FreePool (Buffer);
256 return;
257 }
258
259 //
260 // -- Process and Environment Control Routines --
261 //
262
263 /* Get a value from the current environment */
264 char *getenv (const char *varname)
265 {
266 //
267 // Null getenv() function implementation to satisfy the linker, since there is
268 // no direct functionality logic dependency in present UEFI cases.
269 //
270 return NULL;
271 }
272
273 //
274 // -- Stream I/O Routines --
275 //
276
277 /* Write formatted output using a pointer to a list of arguments */
278 int vfprintf (FILE *stream, const char *format, VA_LIST arg)
279 {
280 return 0;
281 }
282
283 /* Write data to a stream */
284 size_t fwrite (const void *buffer, size_t size, size_t count, FILE *stream)
285 {
286 return 0;
287 }
288
289 //
290 // -- Dummy OpenSSL Support Routines --
291 //
292
293 int BIO_printf (void *bio, const char *format, ...)
294 {
295 return 0;
296 }
297
298 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
299 {
300 return 0;
301 }
302
303 void *UI_OpenSSL(void)
304 {
305 return NULL;
306 }
307
308 int X509_load_cert_file (VOID *ctx, const char *file, int type)
309 {
310 return 0;
311 }
312
313 int X509_load_crl_file (VOID *ctx, const char *file, int type)
314 {
315 return 0;
316 }
317
318 int chmod (const char *c, mode_t m)
319 {
320 return -1;
321 }
322
323 int close (int f)
324 {
325 return -1;
326 }
327
328 void closelog (void)
329 {
330
331 }
332
333 #ifdef __GNUC__
334
335 typedef
336 VOID
337 (EFIAPI *NoReturnFuncPtr)(
338 VOID
339 ) __attribute__((__noreturn__));
340
341
342 STATIC
343 VOID
344 EFIAPI
345 NopFunction (
346 VOID
347 )
348 {
349 }
350
351
352 void exit (int e)
353 {
354 NoReturnFuncPtr NoReturnFunc;
355
356 NoReturnFunc = (NoReturnFuncPtr) NopFunction;
357
358 NoReturnFunc ();
359 }
360
361 #else
362
363 void exit (int e)
364 {
365 }
366
367 #endif
368
369 int fclose (FILE *f)
370 {
371 return 0;
372 }
373
374 FILE *fopen (const char *c, const char *m)
375 {
376 return NULL;
377 }
378
379 size_t fread (void *b, size_t c, size_t i, FILE *f)
380 {
381 return 0;
382 }
383
384 int fprintf (FILE *f, const char *s, ...)
385 {
386 return 0;
387 }
388
389 uid_t getuid (void)
390 {
391 return 0;
392 }
393
394 uid_t geteuid (void)
395 {
396 return 0;
397 }
398
399 gid_t getgid (void)
400 {
401 return 0;
402 }
403
404 gid_t getegid (void)
405 {
406 return 0;
407 }
408
409 off_t lseek (int a, off_t o, int d)
410 {
411 return 0;
412 }
413
414 void openlog (const char *c, int a, int b)
415 {
416
417 }
418
419 ssize_t read (int f, void *b, size_t c)
420 {
421 return 0;
422 }
423
424 int stat (const char *c, struct stat *s)
425 {
426 return -1;
427 }
428
429 int strcasecmp (const char *c, const char *s)
430 {
431 return 0;
432 }
433
434 int strncasecmp (const char *c, const char *s, size_t l)
435 {
436 return 0;
437 }
438
439 void syslog (int a, const char *c, ...)
440 {
441
442 }
443
444 ssize_t write (int f, const void *b, size_t l)
445 {
446 return 0;
447 }