]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c
CryptoPkg/CrtLibSupport: add secure_getenv() stub function
[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 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 <CrtLibSupport.h>
17
18 int errno = 0;
19
20 FILE *stderr = NULL;
21 FILE *stdin = NULL;
22 FILE *stdout = NULL;
23
24 typedef
25 int
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 /* Compare first n bytes of string s1 with string s2, ignoring case */
140 int strncasecmp (const char *s1, const char *s2, size_t n)
141 {
142 int Val;
143
144 ASSERT(s1 != NULL);
145 ASSERT(s2 != NULL);
146
147 if (n != 0) {
148 do {
149 Val = tolower(*s1) - tolower(*s2);
150 if (Val != 0) {
151 return Val;
152 }
153 ++s1;
154 ++s2;
155 if (*s1 == '\0') {
156 break;
157 }
158 } while (--n != 0);
159 }
160 return 0;
161 }
162
163 /* Read formatted data from a string */
164 int sscanf (const char *buffer, const char *format, ...)
165 {
166 //
167 // Null sscanf() function implementation to satisfy the linker, since
168 // no direct functionality logic dependency in present UEFI cases.
169 //
170 return 0;
171 }
172
173 /* Maps errnum to an error-message string */
174 char * strerror (int errnum)
175 {
176 return NULL;
177 }
178
179 /* Computes the length of the maximum initial segment of the string pointed to by s1
180 which consists entirely of characters from the string pointed to by s2. */
181 size_t strspn (const char *s1 , const char *s2)
182 {
183 UINT8 Map[32];
184 UINT32 Index;
185 size_t Count;
186
187 for (Index = 0; Index < 32; Index++) {
188 Map[Index] = 0;
189 }
190
191 while (*s2) {
192 Map[*s2 >> 3] |= (1 << (*s2 & 7));
193 s2++;
194 }
195
196 if (*s1) {
197 Count = 0;
198 while (Map[*s1 >> 3] & (1 << (*s1 & 7))) {
199 Count++;
200 s1++;
201 }
202
203 return Count;
204 }
205
206 return 0;
207 }
208
209 /* Computes the length of the maximum initial segment of the string pointed to by s1
210 which consists entirely of characters not from the string pointed to by s2. */
211 size_t strcspn (const char *s1, const char *s2)
212 {
213 UINT8 Map[32];
214 UINT32 Index;
215 size_t Count;
216
217 for (Index = 0; Index < 32; Index++) {
218 Map[Index] = 0;
219 }
220
221 while (*s2) {
222 Map[*s2 >> 3] |= (1 << (*s2 & 7));
223 s2++;
224 }
225
226 Map[0] |= 1;
227
228 Count = 0;
229 while (!(Map[*s1 >> 3] & (1 << (*s1 & 7)))) {
230 Count ++;
231 s1++;
232 }
233
234 return Count;
235 }
236
237 //
238 // -- Character Classification Routines --
239 //
240
241 /* Determines if a particular character is a decimal-digit character */
242 int isdigit (int c)
243 {
244 //
245 // <digit> ::= [0-9]
246 //
247 return (('0' <= (c)) && ((c) <= '9'));
248 }
249
250 /* Determine if an integer represents character that is a hex digit */
251 int isxdigit (int c)
252 {
253 //
254 // <hexdigit> ::= [0-9] | [a-f] | [A-F]
255 //
256 return ((('0' <= (c)) && ((c) <= '9')) ||
257 (('a' <= (c)) && ((c) <= 'f')) ||
258 (('A' <= (c)) && ((c) <= 'F')));
259 }
260
261 /* Determines if a particular character represents a space character */
262 int isspace (int c)
263 {
264 //
265 // <space> ::= [ ]
266 //
267 return ((c) == ' ');
268 }
269
270 /* Determine if a particular character is an alphanumeric character */
271 int isalnum (int c)
272 {
273 //
274 // <alnum> ::= [0-9] | [a-z] | [A-Z]
275 //
276 return ((('0' <= (c)) && ((c) <= '9')) ||
277 (('a' <= (c)) && ((c) <= 'z')) ||
278 (('A' <= (c)) && ((c) <= 'Z')));
279 }
280
281 /* Determines if a particular character is in upper case */
282 int isupper (int c)
283 {
284 //
285 // <uppercase letter> := [A-Z]
286 //
287 return (('A' <= (c)) && ((c) <= 'Z'));
288 }
289
290 //
291 // -- Data Conversion Routines --
292 //
293
294 /* Convert strings to a long-integer value */
295 long strtol (const char *nptr, char **endptr, int base)
296 {
297 //
298 // Null strtol() function implementation to satisfy the linker, since there is
299 // no direct functionality logic dependency in present UEFI cases.
300 //
301 return 0;
302 }
303
304 /* Convert strings to an unsigned long-integer value */
305 unsigned long strtoul (const char *nptr, char **endptr, int base)
306 {
307 //
308 // Null strtoul() function implementation to satisfy the linker, since there is
309 // no direct functionality logic dependency in present UEFI cases.
310 //
311 return 0;
312 }
313
314 /* Convert character to lowercase */
315 int tolower (int c)
316 {
317 if (('A' <= (c)) && ((c) <= 'Z')) {
318 return (c - ('A' - 'a'));
319 }
320 return (c);
321 }
322
323 //
324 // -- Searching and Sorting Routines --
325 //
326
327 /* Performs a quick sort */
328 void qsort (void *base, size_t num, size_t width, int (*compare)(const void *, const void *))
329 {
330 VOID *Buffer;
331
332 ASSERT (base != NULL);
333 ASSERT (compare != NULL);
334
335 //
336 // Use CRT-style malloc to cover BS and RT memory allocation.
337 //
338 Buffer = malloc (width);
339 ASSERT (Buffer != NULL);
340
341 //
342 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
343 //
344 QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);
345
346 free (Buffer);
347 return;
348 }
349
350 //
351 // -- Process and Environment Control Routines --
352 //
353
354 /* Get a value from the current environment */
355 char *getenv (const char *varname)
356 {
357 //
358 // Null getenv() function implementation to satisfy the linker, since there is
359 // no direct functionality logic dependency in present UEFI cases.
360 //
361 return NULL;
362 }
363
364 /* Get a value from the current environment */
365 char *secure_getenv (const char *varname)
366 {
367 //
368 // Null secure_getenv() function implementation to satisfy the linker, since
369 // there is no direct functionality logic dependency in present UEFI cases.
370 //
371 // From the secure_getenv() manual: 'just like getenv() except that it
372 // returns NULL in cases where "secure execution" is required'.
373 //
374 return NULL;
375 }
376
377 //
378 // -- Stream I/O Routines --
379 //
380
381 /* Write data to a stream */
382 size_t fwrite (const void *buffer, size_t size, size_t count, FILE *stream)
383 {
384 return 0;
385 }
386
387 //
388 // -- Dummy OpenSSL Support Routines --
389 //
390
391 int BIO_printf (void *bio, const char *format, ...)
392 {
393 return 0;
394 }
395
396 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
397 {
398 return 0;
399 }
400
401 #ifdef __GNUC__
402
403 typedef
404 VOID
405 (EFIAPI *NoReturnFuncPtr)(
406 VOID
407 ) __attribute__((__noreturn__));
408
409 STATIC
410 VOID
411 EFIAPI
412 NopFunction (
413 VOID
414 )
415 {
416 }
417
418 void abort (void)
419 {
420 NoReturnFuncPtr NoReturnFunc;
421
422 NoReturnFunc = (NoReturnFuncPtr) NopFunction;
423
424 NoReturnFunc ();
425 }
426
427 #else
428
429 void abort (void)
430 {
431 // Do nothing
432 }
433
434 #endif
435
436 int fclose (FILE *f)
437 {
438 return 0;
439 }
440
441 FILE *fopen (const char *c, const char *m)
442 {
443 return NULL;
444 }
445
446 size_t fread (void *b, size_t c, size_t i, FILE *f)
447 {
448 return 0;
449 }
450
451 uid_t getuid (void)
452 {
453 return 0;
454 }
455
456 uid_t geteuid (void)
457 {
458 return 0;
459 }
460
461 gid_t getgid (void)
462 {
463 return 0;
464 }
465
466 gid_t getegid (void)
467 {
468 return 0;
469 }
470
471 int printf (char const *fmt, ...)
472 {
473 return 0;
474 }