PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/edk2/CryptoPkg/Library/BaseCryptLib/SysCall/CrtWrapper.c

https://gitlab.com/envieidoc/Clover
C | 450 lines | 256 code | 79 blank | 115 comment | 26 complexity | a53b22196c95e4b62f27173695549dd4 MD5 | raw file
  1. /** @file
  2. C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based
  3. Cryptographic Library.
  4. Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
  5. This program and the accompanying materials
  6. are licensed and made available under the terms and conditions of the BSD License
  7. which accompanies this distribution. The full text of the license may be found at
  8. http://opensource.org/licenses/bsd-license.php
  9. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  11. **/
  12. #include <OpenSslSupport.h>
  13. int errno = 0;
  14. FILE *stderr = NULL;
  15. FILE *stdin = NULL;
  16. FILE *stdout = NULL;
  17. typedef
  18. int
  19. (*SORT_COMPARE)(
  20. IN VOID *Buffer1,
  21. IN VOID *Buffer2
  22. );
  23. //
  24. // Duplicated from EDKII BaseSortLib for qsort() wrapper
  25. //
  26. STATIC
  27. VOID
  28. QuickSortWorker (
  29. IN OUT VOID *BufferToSort,
  30. IN CONST UINTN Count,
  31. IN CONST UINTN ElementSize,
  32. IN SORT_COMPARE CompareFunction,
  33. IN VOID *Buffer
  34. )
  35. {
  36. VOID *Pivot;
  37. UINTN LoopCount;
  38. UINTN NextSwapLocation;
  39. ASSERT(BufferToSort != NULL);
  40. ASSERT(CompareFunction != NULL);
  41. ASSERT(Buffer != NULL);
  42. if (Count < 2 || ElementSize < 1) {
  43. return;
  44. }
  45. NextSwapLocation = 0;
  46. //
  47. // Pick a pivot (we choose last element)
  48. //
  49. Pivot = ((UINT8 *)BufferToSort + ((Count - 1) * ElementSize));
  50. //
  51. // Now get the pivot such that all on "left" are below it
  52. // and everything "right" are above it
  53. //
  54. for (LoopCount = 0; LoopCount < Count - 1; LoopCount++)
  55. {
  56. //
  57. // If the element is less than the pivot
  58. //
  59. if (CompareFunction ((VOID *)((UINT8 *)BufferToSort + ((LoopCount) * ElementSize)), Pivot) <= 0) {
  60. //
  61. // Swap
  62. //
  63. CopyMem (Buffer, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);
  64. CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), (UINT8 *)BufferToSort + ((LoopCount) * ElementSize), ElementSize);
  65. CopyMem ((UINT8 *)BufferToSort + ((LoopCount) * ElementSize), Buffer, ElementSize);
  66. //
  67. // Increment NextSwapLocation
  68. //
  69. NextSwapLocation++;
  70. }
  71. }
  72. //
  73. // Swap pivot to it's final position (NextSwapLocaiton)
  74. //
  75. CopyMem (Buffer, Pivot, ElementSize);
  76. CopyMem (Pivot, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);
  77. CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), Buffer, ElementSize);
  78. //
  79. // Now recurse on 2 paritial lists. Neither of these will have the 'pivot' element.
  80. // IE list is sorted left half, pivot element, sorted right half...
  81. //
  82. QuickSortWorker (
  83. BufferToSort,
  84. NextSwapLocation,
  85. ElementSize,
  86. CompareFunction,
  87. Buffer
  88. );
  89. QuickSortWorker (
  90. (UINT8 *)BufferToSort + (NextSwapLocation + 1) * ElementSize,
  91. Count - NextSwapLocation - 1,
  92. ElementSize,
  93. CompareFunction,
  94. Buffer
  95. );
  96. return;
  97. }
  98. //---------------------------------------------------------
  99. // Standard C Run-time Library Interface Wrapper
  100. //---------------------------------------------------------
  101. //
  102. // -- String Manipulation Routines --
  103. //
  104. /* Scan a string for the last occurrence of a character */
  105. char *strrchr (const char *str, int c)
  106. {
  107. char * save;
  108. for (save = NULL; ; ++str) {
  109. if (*str == c) {
  110. save = (char *)str;
  111. }
  112. if (*str == 0) {
  113. return (save);
  114. }
  115. }
  116. }
  117. /* Read formatted data from a string */
  118. int sscanf (const char *buffer, const char *format, ...)
  119. {
  120. //
  121. // Null sscanf() function implementation to satisfy the linker, since
  122. // no direct functionality logic dependency in present UEFI cases.
  123. //
  124. return 0;
  125. }
  126. //
  127. // -- Character Classification Routines --
  128. //
  129. /* Determines if a particular character is a decimal-digit character */
  130. int isdigit (int c)
  131. {
  132. //
  133. // <digit> ::= [0-9]
  134. //
  135. return (('0' <= (c)) && ((c) <= '9'));
  136. }
  137. /* Determine if an integer represents character that is a hex digit */
  138. int isxdigit (int c)
  139. {
  140. //
  141. // <hexdigit> ::= [0-9] | [a-f] | [A-F]
  142. //
  143. return ((('0' <= (c)) && ((c) <= '9')) ||
  144. (('a' <= (c)) && ((c) <= 'f')) ||
  145. (('A' <= (c)) && ((c) <= 'F')));
  146. }
  147. /* Determines if a particular character represents a space character */
  148. int isspace (int c)
  149. {
  150. //
  151. // <space> ::= [ ]
  152. //
  153. return ((c) == ' ');
  154. }
  155. /* Determine if a particular character is an alphanumeric character */
  156. int isalnum (int c)
  157. {
  158. //
  159. // <alnum> ::= [0-9] | [a-z] | [A-Z]
  160. //
  161. return ((('0' <= (c)) && ((c) <= '9')) ||
  162. (('a' <= (c)) && ((c) <= 'z')) ||
  163. (('A' <= (c)) && ((c) <= 'Z')));
  164. }
  165. /* Determines if a particular character is in upper case */
  166. int isupper (int c)
  167. {
  168. //
  169. // <uppercase letter> := [A-Z]
  170. //
  171. return (('A' <= (c)) && ((c) <= 'Z'));
  172. }
  173. //
  174. // -- Data Conversion Routines --
  175. //
  176. /* Convert strings to a long-integer value */
  177. long strtol (const char *nptr, char **endptr, int base)
  178. {
  179. //
  180. // Null strtol() function implementation to satisfy the linker, since there is
  181. // no direct functionality logic dependency in present UEFI cases.
  182. //
  183. return 0;
  184. }
  185. /* Convert strings to an unsigned long-integer value */
  186. unsigned long strtoul (const char *nptr, char **endptr, int base)
  187. {
  188. //
  189. // Null strtoul() function implementation to satisfy the linker, since there is
  190. // no direct functionality logic dependency in present UEFI cases.
  191. //
  192. return 0;
  193. }
  194. /* Convert character to lowercase */
  195. int tolower (int c)
  196. {
  197. if (('A' <= (c)) && ((c) <= 'Z')) {
  198. return (c - ('A' - 'a'));
  199. }
  200. return (c);
  201. }
  202. //
  203. // -- Searching and Sorting Routines --
  204. //
  205. /* Performs a quick sort */
  206. void qsort (void *base, size_t num, size_t width, int (*compare)(const void *, const void *))
  207. {
  208. VOID *Buffer;
  209. ASSERT (base != NULL);
  210. ASSERT (compare != NULL);
  211. //
  212. // Use CRT-style malloc to cover BS and RT memory allocation.
  213. //
  214. Buffer = malloc (width);
  215. ASSERT (Buffer != NULL);
  216. //
  217. // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
  218. //
  219. QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);
  220. free (Buffer);
  221. return;
  222. }
  223. //
  224. // -- Process and Environment Control Routines --
  225. //
  226. /* Get a value from the current environment */
  227. char *getenv (const char *varname)
  228. {
  229. //
  230. // Null getenv() function implementation to satisfy the linker, since there is
  231. // no direct functionality logic dependency in present UEFI cases.
  232. //
  233. return NULL;
  234. }
  235. //
  236. // -- Stream I/O Routines --
  237. //
  238. /* Write formatted output using a pointer to a list of arguments */
  239. int vfprintf (FILE *stream, const char *format, VA_LIST arg)
  240. {
  241. return 0;
  242. }
  243. /* Write data to a stream */
  244. size_t fwrite (const void *buffer, size_t size, size_t count, FILE *stream)
  245. {
  246. return 0;
  247. }
  248. //
  249. // -- Dummy OpenSSL Support Routines --
  250. //
  251. int BIO_printf (void *bio, const char *format, ...)
  252. {
  253. return 0;
  254. }
  255. int BIO_snprintf(char *buf, size_t n, const char *format, ...)
  256. {
  257. return 0;
  258. }
  259. void *UI_OpenSSL(void)
  260. {
  261. return NULL;
  262. }
  263. int X509_load_cert_file (VOID *ctx, const char *file, int type)
  264. {
  265. return 0;
  266. }
  267. int X509_load_crl_file (VOID *ctx, const char *file, int type)
  268. {
  269. return 0;
  270. }
  271. int chmod (const char *c, mode_t m)
  272. {
  273. return -1;
  274. }
  275. int close (int f)
  276. {
  277. return -1;
  278. }
  279. void closelog (void)
  280. {
  281. }
  282. #ifdef __GNUC__
  283. typedef
  284. VOID
  285. (EFIAPI *NoReturnFuncPtr)(
  286. VOID
  287. ) __attribute__((__noreturn__));
  288. STATIC
  289. VOID
  290. EFIAPI
  291. NopFunction (
  292. VOID
  293. )
  294. {
  295. }
  296. void exit (int e)
  297. {
  298. NoReturnFuncPtr NoReturnFunc;
  299. NoReturnFunc = (NoReturnFuncPtr) NopFunction;
  300. NoReturnFunc ();
  301. }
  302. #else
  303. void exit (int e)
  304. {
  305. }
  306. #endif
  307. int fclose (FILE *f)
  308. {
  309. return 0;
  310. }
  311. FILE *fopen (const char *c, const char *m)
  312. {
  313. return NULL;
  314. }
  315. size_t fread (void *b, size_t c, size_t i, FILE *f)
  316. {
  317. return 0;
  318. }
  319. int fprintf (FILE *f, const char *s, ...)
  320. {
  321. return 0;
  322. }
  323. uid_t getuid (void)
  324. {
  325. return 0;
  326. }
  327. uid_t geteuid (void)
  328. {
  329. return 0;
  330. }
  331. gid_t getgid (void)
  332. {
  333. return 0;
  334. }
  335. gid_t getegid (void)
  336. {
  337. return 0;
  338. }
  339. off_t lseek (int a, off_t o, int d)
  340. {
  341. return 0;
  342. }
  343. void openlog (const char *c, int a, int b)
  344. {
  345. }
  346. ssize_t read (int f, void *b, size_t c)
  347. {
  348. return 0;
  349. }
  350. int stat (const char *c, struct stat *s)
  351. {
  352. return -1;
  353. }
  354. int strcasecmp (const char *c, const char *s)
  355. {
  356. return 0;
  357. }
  358. int strncasecmp (const char *c, const char *s, size_t l)
  359. {
  360. return 0;
  361. }
  362. void syslog (int a, const char *c, ...)
  363. {
  364. }
  365. ssize_t write (int f, const void *b, size_t l)
  366. {
  367. return 0;
  368. }