1 | /** @file
|
---|
2 | Redfish CRT wrapper functions.
|
---|
3 |
|
---|
4 | Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
|
---|
5 | (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
|
---|
6 |
|
---|
7 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
8 |
|
---|
9 | **/
|
---|
10 |
|
---|
11 | #ifndef REDFISH_CRT_LIB_H_
|
---|
12 | #define REDFISH_CRT_LIB_H_
|
---|
13 |
|
---|
14 | #include <Library/BaseLib.h>
|
---|
15 | #include <Library/BaseMemoryLib.h>
|
---|
16 | #include <Library/DebugLib.h>
|
---|
17 | #include <Library/PrintLib.h>
|
---|
18 |
|
---|
19 | #define MAX_STRING_SIZE 0x10000000
|
---|
20 |
|
---|
21 | // Minimum value for an object of type long long int.
|
---|
22 | #define LLONG_MIN MIN_INT64
|
---|
23 |
|
---|
24 | // Maximum value for an object of type long long int.
|
---|
25 | #define LLONG_MAX MAX_INT64
|
---|
26 |
|
---|
27 | // We dont support double on edk2
|
---|
28 | #define HUGE_VAL 0
|
---|
29 |
|
---|
30 | #if defined (MDE_CPU_X64) || defined (MDE_CPU_AARCH64) || defined (MDE_CPU_RISCV64)
|
---|
31 | //
|
---|
32 | // With GCC we would normally use SIXTY_FOUR_BIT_LONG, but MSVC needs
|
---|
33 | // SIXTY_FOUR_BIT, because 'long' is 32-bit and only 'long long' is
|
---|
34 | // 64-bit. Since using 'long long' works fine on GCC too, just do that.
|
---|
35 | //
|
---|
36 | #define SIXTY_FOUR_BIT
|
---|
37 | #elif defined (MDE_CPU_IA32) || defined (MDE_CPU_ARM) || defined (MDE_CPU_EBC)
|
---|
38 | #define THIRTY_TWO_BIT
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | //
|
---|
42 | // Map all va_xxxx elements to VA_xxx defined in MdePkg/Include/Base.h
|
---|
43 | //
|
---|
44 | #if !defined (__CC_ARM) // if va_list is not already defined
|
---|
45 | #define va_list VA_LIST
|
---|
46 | #define va_arg VA_ARG
|
---|
47 | #define va_start VA_START
|
---|
48 | #define va_end VA_END
|
---|
49 | #else // __CC_ARM
|
---|
50 | #define va_start(Marker, Parameter) __va_start(Marker, Parameter)
|
---|
51 | #define va_arg(Marker, TYPE) __va_arg(Marker, TYPE)
|
---|
52 | #define va_end(Marker) ((void)0)
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | //
|
---|
56 | // Definitions for global constants used by CRT library routines
|
---|
57 | //
|
---|
58 | #define INT_MAX MAX_INT32 /* Maximum (signed) int value */
|
---|
59 | #define LONG_MAX 0X7FFFFFFFL /* max value for a long */
|
---|
60 | #define LONG_MIN (-LONG_MAX-1) /* min value for a long */
|
---|
61 | #define ULONG_MAX 0xFFFFFFFF /* Maximum unsigned long value */
|
---|
62 | #define CHAR_BIT 8 /* Number of bits in a char */
|
---|
63 |
|
---|
64 | // Maximum value for an object of type unsigned long long int.
|
---|
65 | #define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL // 2^64 - 1
|
---|
66 | // Maximum value for an object of type unsigned char.
|
---|
67 | #define UCHAR_MAX 255 // 2^8 - 1
|
---|
68 |
|
---|
69 | //
|
---|
70 | // Basic types mapping
|
---|
71 | //
|
---|
72 | typedef UINTN size_t;
|
---|
73 | typedef INTN ssize_t;
|
---|
74 | typedef INT32 time_t;
|
---|
75 | typedef UINT8 __uint8_t;
|
---|
76 | typedef UINT8 sa_family_t;
|
---|
77 | typedef UINT32 uid_t;
|
---|
78 | typedef UINT32 gid_t;
|
---|
79 | typedef INT32 int32_t;
|
---|
80 | typedef UINT32 uint32_t;
|
---|
81 | typedef UINT16 uint16_t;
|
---|
82 | typedef UINT8 uint8_t;
|
---|
83 | typedef enum {
|
---|
84 | false, true
|
---|
85 | } bool;
|
---|
86 |
|
---|
87 | //
|
---|
88 | // File operations are not required for EFI building,
|
---|
89 | // so FILE is mapped to VOID * to pass build
|
---|
90 | //
|
---|
91 | typedef VOID *FILE;
|
---|
92 |
|
---|
93 | /**
|
---|
94 | This is the Redfish version of CRT snprintf function, this function replaces "%s" to
|
---|
95 | "%a" before invoking AsciiSPrint(). That is becasue "%s" is unicode base on edk2
|
---|
96 | environment however "%s" is ascii code base on snprintf().
|
---|
97 | See definitions of AsciiSPrint() for the details.
|
---|
98 |
|
---|
99 | @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
|
---|
100 | ASCII string.
|
---|
101 | @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
|
---|
102 | @param FormatString A Null-terminated ASCII format string.
|
---|
103 | @param ... Variable argument list whose contents are accessed based on the
|
---|
104 | format string specified by FormatString.
|
---|
105 |
|
---|
106 | @return The number of ASCII characters in the produced output buffer not including the
|
---|
107 | Null-terminator. Zero means no string is produced or the error happens.
|
---|
108 |
|
---|
109 | **/
|
---|
110 | UINTN
|
---|
111 | EFIAPI
|
---|
112 | RedfishAsciiSPrint (
|
---|
113 | OUT CHAR8 *StartOfBuffer,
|
---|
114 | IN UINTN BufferSize,
|
---|
115 | IN CONST CHAR8 *FormatString,
|
---|
116 | ...
|
---|
117 | );
|
---|
118 |
|
---|
119 | /**
|
---|
120 | This is the Redfish version of CRT vsnprintf function, this function replaces "%s" to
|
---|
121 | "%a" before invoking AsciiVSPrint(). That is because "%s" is unicode base on edk2
|
---|
122 | environment however "%s" is ascii code base on vsnprintf().
|
---|
123 | See definitions of AsciiVSPrint() for the details.
|
---|
124 |
|
---|
125 | @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
|
---|
126 | ASCII string.
|
---|
127 | @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
|
---|
128 | @param FormatString A Null-terminated ASCII format string.
|
---|
129 | @param Marker VA_LIST marker for the variable argument list.
|
---|
130 |
|
---|
131 | @return The number of ASCII characters in the produced output buffer not including the
|
---|
132 | Null-terminator.
|
---|
133 |
|
---|
134 | **/
|
---|
135 | UINTN
|
---|
136 | EFIAPI
|
---|
137 | RedfishAsciiVSPrint (
|
---|
138 | OUT CHAR8 *StartOfBuffer,
|
---|
139 | IN UINTN BufferSize,
|
---|
140 | IN CONST CHAR8 *FormatString,
|
---|
141 | IN VA_LIST Marker
|
---|
142 | );
|
---|
143 |
|
---|
144 | //
|
---|
145 | // Global variables
|
---|
146 | //
|
---|
147 | extern int errno;
|
---|
148 | extern FILE *stderr;
|
---|
149 |
|
---|
150 | //
|
---|
151 | // Function prototypes of CRT Library routines
|
---|
152 | //
|
---|
153 | void *
|
---|
154 | malloc (
|
---|
155 | size_t
|
---|
156 | );
|
---|
157 |
|
---|
158 | void *
|
---|
159 | realloc (
|
---|
160 | void *,
|
---|
161 | size_t
|
---|
162 | );
|
---|
163 |
|
---|
164 | void *
|
---|
165 | calloc (
|
---|
166 | size_t Num,
|
---|
167 | size_t Size
|
---|
168 | );
|
---|
169 |
|
---|
170 | void
|
---|
171 | free (
|
---|
172 | void *
|
---|
173 | );
|
---|
174 |
|
---|
175 | void *
|
---|
176 | memset (
|
---|
177 | void *,
|
---|
178 | int,
|
---|
179 | size_t
|
---|
180 | );
|
---|
181 |
|
---|
182 | int
|
---|
183 | memcmp (
|
---|
184 | const void *,
|
---|
185 | const void *,
|
---|
186 | size_t
|
---|
187 | );
|
---|
188 |
|
---|
189 | int
|
---|
190 | isdigit (
|
---|
191 | int
|
---|
192 | );
|
---|
193 |
|
---|
194 | int
|
---|
195 | isspace (
|
---|
196 | int
|
---|
197 | );
|
---|
198 |
|
---|
199 | int
|
---|
200 | tolower (
|
---|
201 | int
|
---|
202 | );
|
---|
203 |
|
---|
204 | int
|
---|
205 | isupper (
|
---|
206 | int
|
---|
207 | );
|
---|
208 |
|
---|
209 | int
|
---|
210 | isxdigit (
|
---|
211 | int
|
---|
212 | );
|
---|
213 |
|
---|
214 | int
|
---|
215 | isalnum (
|
---|
216 | int
|
---|
217 | );
|
---|
218 |
|
---|
219 | void *
|
---|
220 | memcpy (
|
---|
221 | void *,
|
---|
222 | const void *,
|
---|
223 | size_t
|
---|
224 | );
|
---|
225 |
|
---|
226 | void *
|
---|
227 | memset (
|
---|
228 | void *,
|
---|
229 | int,
|
---|
230 | size_t
|
---|
231 | );
|
---|
232 |
|
---|
233 | void *
|
---|
234 | memchr (
|
---|
235 | const void *,
|
---|
236 | int,
|
---|
237 | size_t
|
---|
238 | );
|
---|
239 |
|
---|
240 | int
|
---|
241 | memcmp (
|
---|
242 | const void *,
|
---|
243 | const void *,
|
---|
244 | size_t
|
---|
245 | );
|
---|
246 |
|
---|
247 | void *
|
---|
248 | memmove (
|
---|
249 | void *,
|
---|
250 | const void *,
|
---|
251 | size_t
|
---|
252 | );
|
---|
253 |
|
---|
254 | int
|
---|
255 | strcmp (
|
---|
256 | const char *,
|
---|
257 | const char *
|
---|
258 | );
|
---|
259 |
|
---|
260 | int
|
---|
261 | strncmp (
|
---|
262 | const char *,
|
---|
263 | const char *,
|
---|
264 | size_t
|
---|
265 | );
|
---|
266 |
|
---|
267 | char *
|
---|
268 | strcpy (
|
---|
269 | char *,
|
---|
270 | const char *
|
---|
271 | );
|
---|
272 |
|
---|
273 | size_t
|
---|
274 | strlen (
|
---|
275 | const char *
|
---|
276 | );
|
---|
277 |
|
---|
278 | char *
|
---|
279 | strcat (
|
---|
280 | char *,
|
---|
281 | const char *
|
---|
282 | );
|
---|
283 |
|
---|
284 | char *
|
---|
285 | strchr (
|
---|
286 | const char *,
|
---|
287 | int
|
---|
288 | );
|
---|
289 |
|
---|
290 | int
|
---|
291 | strcasecmp (
|
---|
292 | const char *,
|
---|
293 | const char *
|
---|
294 | );
|
---|
295 |
|
---|
296 | int
|
---|
297 | strncasecmp (
|
---|
298 | const char *,
|
---|
299 | const char *,
|
---|
300 | size_t
|
---|
301 | );
|
---|
302 |
|
---|
303 | char *
|
---|
304 | strncpy (
|
---|
305 | char *,
|
---|
306 | size_t,
|
---|
307 | const char *,
|
---|
308 | size_t
|
---|
309 | );
|
---|
310 |
|
---|
311 | int
|
---|
312 | strncmp (
|
---|
313 | const char *,
|
---|
314 | const char *,
|
---|
315 | size_t
|
---|
316 | );
|
---|
317 |
|
---|
318 | char *
|
---|
319 | strrchr (
|
---|
320 | const char *,
|
---|
321 | int
|
---|
322 | );
|
---|
323 |
|
---|
324 | unsigned long
|
---|
325 | strtoul (
|
---|
326 | const char *,
|
---|
327 | char **,
|
---|
328 | int
|
---|
329 | );
|
---|
330 |
|
---|
331 | char *
|
---|
332 | strstr (
|
---|
333 | const char *s1,
|
---|
334 | const char *s2
|
---|
335 | );
|
---|
336 |
|
---|
337 | long
|
---|
338 | strtol (
|
---|
339 | const char *,
|
---|
340 | char **,
|
---|
341 | int
|
---|
342 | );
|
---|
343 |
|
---|
344 | char *
|
---|
345 | strerror (
|
---|
346 | int
|
---|
347 | );
|
---|
348 |
|
---|
349 | size_t
|
---|
350 | strspn (
|
---|
351 | const char *,
|
---|
352 | const char *
|
---|
353 | );
|
---|
354 |
|
---|
355 | char *
|
---|
356 | strdup (
|
---|
357 | const char *str
|
---|
358 | );
|
---|
359 |
|
---|
360 | char *
|
---|
361 | strpbrk (
|
---|
362 | const char *s1,
|
---|
363 | const char *s2
|
---|
364 | );
|
---|
365 |
|
---|
366 | unsigned long long
|
---|
367 | strtoull (
|
---|
368 | const char *nptr,
|
---|
369 | char **endptr,
|
---|
370 | int base
|
---|
371 | );
|
---|
372 |
|
---|
373 | long long
|
---|
374 | strtoll (
|
---|
375 | const char *nptr,
|
---|
376 | char **endptr,
|
---|
377 | int base
|
---|
378 | );
|
---|
379 |
|
---|
380 | long
|
---|
381 | strtol (
|
---|
382 | const char *nptr,
|
---|
383 | char **endptr,
|
---|
384 | int base
|
---|
385 | );
|
---|
386 |
|
---|
387 | double
|
---|
388 | strtod (
|
---|
389 | const char *__restrict nptr,
|
---|
390 | char **__restrict endptr
|
---|
391 | );
|
---|
392 |
|
---|
393 | size_t
|
---|
394 | strcspn (
|
---|
395 | const char *,
|
---|
396 | const char *
|
---|
397 | );
|
---|
398 |
|
---|
399 | int
|
---|
400 | printf (
|
---|
401 | const char *,
|
---|
402 | ...
|
---|
403 | );
|
---|
404 |
|
---|
405 | int
|
---|
406 | sscanf (
|
---|
407 | const char *,
|
---|
408 | const char *,
|
---|
409 | ...
|
---|
410 | );
|
---|
411 |
|
---|
412 | FILE *
|
---|
413 | fopen (
|
---|
414 | const char *,
|
---|
415 | const char *
|
---|
416 | );
|
---|
417 |
|
---|
418 | size_t
|
---|
419 | fread (
|
---|
420 | void *,
|
---|
421 | size_t,
|
---|
422 | size_t,
|
---|
423 | FILE *
|
---|
424 | );
|
---|
425 |
|
---|
426 | size_t
|
---|
427 | fwrite (
|
---|
428 | const void *,
|
---|
429 | size_t,
|
---|
430 | size_t,
|
---|
431 | FILE *
|
---|
432 | );
|
---|
433 |
|
---|
434 | int
|
---|
435 | fclose (
|
---|
436 | FILE *
|
---|
437 | );
|
---|
438 |
|
---|
439 | int
|
---|
440 | fprintf (
|
---|
441 | FILE *,
|
---|
442 | const char *,
|
---|
443 | ...
|
---|
444 | );
|
---|
445 |
|
---|
446 | int
|
---|
447 | fgetc (
|
---|
448 | FILE *_File
|
---|
449 | );
|
---|
450 |
|
---|
451 | uid_t
|
---|
452 | getuid (
|
---|
453 | void
|
---|
454 | );
|
---|
455 |
|
---|
456 | uid_t
|
---|
457 | geteuid (
|
---|
458 | void
|
---|
459 | );
|
---|
460 |
|
---|
461 | gid_t
|
---|
462 | getgid (
|
---|
463 | void
|
---|
464 | );
|
---|
465 |
|
---|
466 | gid_t
|
---|
467 | getegid (
|
---|
468 | void
|
---|
469 | );
|
---|
470 |
|
---|
471 | void
|
---|
472 | qsort (
|
---|
473 | void *,
|
---|
474 | size_t,
|
---|
475 | size_t,
|
---|
476 | int (*)(const void *, const void *)
|
---|
477 | );
|
---|
478 |
|
---|
479 | char *
|
---|
480 | getenv (
|
---|
481 | const char *
|
---|
482 | );
|
---|
483 |
|
---|
484 | #if defined (__GNUC__) && (__GNUC__ >= 2)
|
---|
485 | void
|
---|
486 | abort (
|
---|
487 | void
|
---|
488 | ) __attribute__ ((__noreturn__));
|
---|
489 |
|
---|
490 | #else
|
---|
491 | void
|
---|
492 | abort (
|
---|
493 | void
|
---|
494 | );
|
---|
495 |
|
---|
496 | #endif
|
---|
497 | int
|
---|
498 | toupper (
|
---|
499 | int
|
---|
500 | );
|
---|
501 |
|
---|
502 | int
|
---|
503 | Digit2Val (
|
---|
504 | int
|
---|
505 | );
|
---|
506 |
|
---|
507 | time_t
|
---|
508 | time (
|
---|
509 | time_t *
|
---|
510 | );
|
---|
511 |
|
---|
512 | //
|
---|
513 | // Macros that directly map functions to BaseLib, BaseMemoryLib, and DebugLib functions
|
---|
514 | //
|
---|
515 | #define strcmp AsciiStrCmp
|
---|
516 | #define memcpy(dest, source, count) CopyMem(dest,source,(UINTN)(count))
|
---|
517 | #define memset(dest, ch, count) SetMem(dest,(UINTN)(count),(UINT8)(ch))
|
---|
518 | #define memchr(buf, ch, count) ScanMem8(buf,(UINTN)(count),(UINT8)ch)
|
---|
519 | #define memcmp(buf1, buf2, count) (int)(CompareMem(buf1,buf2,(UINTN)(count)))
|
---|
520 | #define memmove(dest, source, count) CopyMem(dest,source,(UINTN)(count))
|
---|
521 | #define strlen(str) (size_t)(AsciiStrnLenS(str,MAX_STRING_SIZE))
|
---|
522 | #define strcpy(strDest, strSource) AsciiStrCpyS(strDest,(strlen(strSource)+1),strSource)
|
---|
523 | #define strncpy(strDest, strSource, count) AsciiStrnCpyS(strDest,(UINTN)count,strSource,(UINTN)count)
|
---|
524 | #define strncpys(strDest, DestLen, strSource, count) AsciiStrnCpyS(strDest,DestLen,strSource,(UINTN)count)
|
---|
525 | #define strcat(strDest, strSource) AsciiStrCatS(strDest,(strlen(strSource)+strlen(strDest)+1),strSource)
|
---|
526 | #define strchr(str, ch) ScanMem8((VOID *)(str),AsciiStrSize(str),(UINT8)ch)
|
---|
527 | #define strcasecmp(str1, str2) (int)AsciiStriCmp(str1,str2)
|
---|
528 | #define strstr(s1, s2) AsciiStrStr(s1,s2)
|
---|
529 | #define snprintf(buf, len, ...) RedfishAsciiSPrint(buf,len,__VA_ARGS__)
|
---|
530 | #define vsnprintf(buf, len, format, marker) RedfishAsciiVSPrint((buf),(len),(format),(marker))
|
---|
531 | #define assert(expression) ASSERT(expression)
|
---|
532 | #define offsetof(type, member) OFFSET_OF(type,member)
|
---|
533 |
|
---|
534 | #define EOF (-1)
|
---|
535 |
|
---|
536 | extern int errno;
|
---|
537 |
|
---|
538 | #define ERANGE 34 /* 34 Result too large */
|
---|
539 |
|
---|
540 | #endif
|
---|