CrtWrapper.c 9.4 KB

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