Add CryptoPkg (from UDK2010.UP3)
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / SysCall / CrtWrapper.c
CommitLineData
97f98500
HT
1/** @file\r
2 C Run-Time Libraries (CRT) Wrapper Implementation for OpenSSL-based\r
3 Cryptographic Library.\r
4\r
5Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <OpenSslSupport.h>\r
17\r
18typedef\r
19INTN\r
20(*SORT_COMPARE)(\r
21 IN VOID *Buffer1,\r
22 IN VOID *Buffer2\r
23 );\r
24\r
25//\r
26// Duplicated from EDKII BaseSortLib for qsort() wrapper\r
27//\r
28STATIC\r
29VOID\r
30QuickSortWorker (\r
31 IN OUT VOID *BufferToSort,\r
32 IN CONST UINTN Count,\r
33 IN CONST UINTN ElementSize,\r
34 IN SORT_COMPARE CompareFunction,\r
35 IN VOID *Buffer\r
36 )\r
37{\r
38 VOID *Pivot;\r
39 UINTN LoopCount;\r
40 UINTN NextSwapLocation;\r
41\r
42 ASSERT(BufferToSort != NULL);\r
43 ASSERT(CompareFunction != NULL);\r
44 ASSERT(Buffer != NULL);\r
45\r
46 if (Count < 2 || ElementSize < 1) {\r
47 return;\r
48 }\r
49\r
50 NextSwapLocation = 0;\r
51\r
52 //\r
53 // Pick a pivot (we choose last element)\r
54 //\r
55 Pivot = ((UINT8 *)BufferToSort + ((Count - 1) * ElementSize));\r
56\r
57 //\r
58 // Now get the pivot such that all on "left" are below it\r
59 // and everything "right" are above it\r
60 //\r
61 for (LoopCount = 0; LoopCount < Count - 1; LoopCount++)\r
62 {\r
63 //\r
64 // If the element is less than the pivot\r
65 //\r
66 if (CompareFunction ((VOID *)((UINT8 *)BufferToSort + ((LoopCount) * ElementSize)), Pivot) <= 0) {\r
67 //\r
68 // Swap\r
69 //\r
70 CopyMem (Buffer, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);\r
71 CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), (UINT8 *)BufferToSort + ((LoopCount) * ElementSize), ElementSize);\r
72 CopyMem ((UINT8 *)BufferToSort + ((LoopCount) * ElementSize), Buffer, ElementSize);\r
73\r
74 //\r
75 // Increment NextSwapLocation\r
76 //\r
77 NextSwapLocation++;\r
78 }\r
79 }\r
80 //\r
81 // Swap pivot to it's final position (NextSwapLocaiton)\r
82 //\r
83 CopyMem (Buffer, Pivot, ElementSize);\r
84 CopyMem (Pivot, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), ElementSize);\r
85 CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), Buffer, ElementSize);\r
86\r
87 //\r
88 // Now recurse on 2 paritial lists. Neither of these will have the 'pivot' element.\r
89 // IE list is sorted left half, pivot element, sorted right half...\r
90 //\r
91 QuickSortWorker (\r
92 BufferToSort,\r
93 NextSwapLocation,\r
94 ElementSize,\r
95 CompareFunction,\r
96 Buffer\r
97 );\r
98\r
99 QuickSortWorker (\r
100 (UINT8 *)BufferToSort + (NextSwapLocation + 1) * ElementSize,\r
101 Count - NextSwapLocation - 1,\r
102 ElementSize,\r
103 CompareFunction,\r
104 Buffer\r
105 );\r
106\r
107 return;\r
108}\r
109\r
110//---------------------------------------------------------\r
111// Standard C Run-time Library Interface Wrapper\r
112//---------------------------------------------------------\r
113\r
114//\r
115// -- String Manipulation Routines --\r
116//\r
117\r
118/* Scan a string for the last occurrence of a character */\r
119char *strrchr (const char *str, int c)\r
120{\r
121 char * save;\r
122\r
123 for (save = NULL; ; ++str) {\r
124 if (*str == c) {\r
125 save = (char *)str;\r
126 }\r
127 if (*str == 0) {\r
128 return (save);\r
129 }\r
130 }\r
131}\r
132\r
133/* Read formatted data from a string */\r
134int sscanf (const char *buffer, const char *format, ...)\r
135{\r
136 //\r
137 // Null sscanf() function implementation to satisfy the linker, since\r
138 // no direct functionality logic dependency in present UEFI cases.\r
139 //\r
140 return 0;\r
141}\r
142\r
143//\r
144// -- Character Classification Routines --\r
145//\r
146\r
147/* Determines if a particular character is a decimal-digit character */\r
148int isdigit (int c)\r
149{\r
150 //\r
151 // <digit> ::= [0-9]\r
152 //\r
153 return (('0' <= (c)) && ((c) <= '9'));\r
154}\r
155\r
156/* Determine if an integer represents character that is a hex digit */\r
157int isxdigit (int c)\r
158{\r
159 //\r
160 // <hexdigit> ::= [0-9] | [a-f] | [A-F]\r
161 //\r
162 return ((('0' <= (c)) && ((c) <= '9')) ||\r
163 (('a' <= (c)) && ((c) <= 'f')) ||\r
164 (('A' <= (c)) && ((c) <= 'F')));\r
165}\r
166\r
167/* Determines if a particular character represents a space character */\r
168int isspace (int c)\r
169{\r
170 //\r
171 // <space> ::= [ ]\r
172 //\r
173 return ((c) == ' ');\r
174}\r
175\r
176/* Determine if a particular character is an alphanumeric character */\r
177int isalnum (int c)\r
178{\r
179 //\r
180 // <alnum> ::= [0-9] | [a-z] | [A-Z]\r
181 //\r
182 return ((('0' <= (c)) && ((c) <= '9')) ||\r
183 (('a' <= (c)) && ((c) <= 'z')) ||\r
184 (('A' <= (c)) && ((c) <= 'Z')));\r
185}\r
186\r
187/* Determines if a particular character is in upper case */\r
188int isupper (int c)\r
189{\r
190 //\r
191 // <uppercase letter> := [A-Z]\r
192 //\r
193 return (('A' <= (c)) && ((c) <= 'Z'));\r
194}\r
195\r
196//\r
197// -- Data Conversion Routines --\r
198//\r
199\r
200/* Convert strings to a long-integer value */\r
201long strtol (const char *nptr, char **endptr, int base)\r
202{\r
203 //\r
204 // Null strtol() function implementation to satisfy the linker, since there is\r
205 // no direct functionality logic dependency in present UEFI cases.\r
206 //\r
207 return 0;\r
208}\r
209\r
210/* Convert strings to an unsigned long-integer value */\r
211unsigned long strtoul (const char *nptr, char **endptr, int base)\r
212{\r
213 //\r
214 // Null strtoul() function implementation to satisfy the linker, since there is\r
215 // no direct functionality logic dependency in present UEFI cases.\r
216 //\r
217 return 0;\r
218}\r
219\r
220/* Convert character to lowercase */\r
221int tolower (int c)\r
222{\r
223 if (('A' <= (c)) && ((c) <= 'Z')) {\r
224 return (c - ('A' - 'a'));\r
225 }\r
226 return (c);\r
227}\r
228\r
229//\r
230// -- Searching and Sorting Routines --\r
231//\r
232\r
233/* Performs a quick sort */\r
234void qsort (void *base, size_t num, size_t width, int (*compare)(const void *, const void *))\r
235{\r
236 VOID *Buffer;\r
237\r
238 ASSERT (base != NULL);\r
239 ASSERT (compare != NULL);\r
240\r
241 Buffer = AllocatePool (width);\r
242 ASSERT (Buffer != NULL);\r
243\r
244 //\r
245 // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.\r
246 //\r
247 QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);\r
248\r
249 FreePool (Buffer);\r
250 return;\r
251}\r
252\r
253//\r
254// -- Process and Environment Control Routines --\r
255//\r
256\r
257/* Get a value from the current environment */\r
258char *getenv (const char *varname)\r
259{\r
260 //\r
261 // Null getenv() function implementation to satisfy the linker, since there is\r
262 // no direct functionality logic dependency in present UEFI cases.\r
263 //\r
264 return NULL;\r
265}\r
266\r
267//\r
268// -- Stream I/O Routines --\r
269//\r
270\r
271/* Write formatted output using a pointer to a list of arguments */\r
272int vfprintf (FILE *stream, const char *format, VA_LIST arg)\r
273{\r
274 return 0;\r
275}\r
276\r
277/* Write data to a stream */\r
278size_t fwrite (const void *buffer, size_t size, size_t count, FILE *stream)\r
279{\r
280 return 0;\r
281}\r