RedfishCrtLib.h 10 KB

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