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