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