79957cc89f780ce45a5fb11b4e15750e1f9b1989
[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 void exit (int e)
334 {
335
336 }
337
338 int fclose (FILE *f)
339 {
340 return 0;
341 }
342
343 FILE *fopen (const char *c, const char *m)
344 {
345 return NULL;
346 }
347
348 size_t fread (void *b, size_t c, size_t i, FILE *f)
349 {
350 return 0;
351 }
352
353 int fprintf (FILE *f, const char *s, ...)
354 {
355 return 0;
356 }
357
358 uid_t getuid (void)
359 {
360 return 0;
361 }
362
363 uid_t geteuid (void)
364 {
365 return 0;
366 }
367
368 gid_t getgid (void)
369 {
370 return 0;
371 }
372
373 gid_t getegid (void)
374 {
375 return 0;
376 }
377
378 off_t lseek (int a, off_t o, int d)
379 {
380 return 0;
381 }
382
383 void openlog (const char *c, int a, int b)
384 {
385
386 }
387
388 ssize_t read (int f, void *b, size_t c)
389 {
390 return 0;
391 }
392
393 int stat (const char *c, struct stat *s)
394 {
395 return -1;
396 }
397
398 int strcasecmp (const char *c, const char *s)
399 {
400 return 0;
401 }
402
403 int strncasecmp (const char *c, const char *s, size_t l)
404 {
405 return 0;
406 }
407
408 void syslog (int a, const char *c, ...)
409 {
410
411 }
412
413 ssize_t write (int f, const void *b, size_t l)
414 {
415 return 0;
416 }