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