Home | History | Annotate | Download | only in SysCall
      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 /* Scan a string for the last occurrence of a character */
    125 char *strrchr (const char *str, int c)
    126 {
    127   char * save;
    128 
    129   for (save = NULL; ; ++str) {
    130     if (*str == c) {
    131       save = (char *)str;
    132     }
    133     if (*str == 0) {
    134       return (save);
    135     }
    136   }
    137 }
    138 
    139 /* Read formatted data from a string */
    140 int sscanf (const char *buffer, const char *format, ...)
    141 {
    142   //
    143   // Null sscanf() function implementation to satisfy the linker, since
    144   // no direct functionality logic dependency in present UEFI cases.
    145   //
    146   return 0;
    147 }
    148 
    149 //
    150 // -- Character Classification Routines --
    151 //
    152 
    153 /* Determines if a particular character is a decimal-digit character */
    154 int isdigit (int c)
    155 {
    156   //
    157   // <digit> ::= [0-9]
    158   //
    159   return (('0' <= (c)) && ((c) <= '9'));
    160 }
    161 
    162 /* Determine if an integer represents character that is a hex digit */
    163 int isxdigit (int c)
    164 {
    165   //
    166   // <hexdigit> ::= [0-9] | [a-f] | [A-F]
    167   //
    168   return ((('0' <= (c)) && ((c) <= '9')) ||
    169           (('a' <= (c)) && ((c) <= 'f')) ||
    170           (('A' <= (c)) && ((c) <= 'F')));
    171 }
    172 
    173 /* Determines if a particular character represents a space character */
    174 int isspace (int c)
    175 {
    176   //
    177   // <space> ::= [ ]
    178   //
    179   return ((c) == ' ');
    180 }
    181 
    182 /* Determine if a particular character is an alphanumeric character */
    183 int isalnum (int c)
    184 {
    185   //
    186   // <alnum> ::= [0-9] | [a-z] | [A-Z]
    187   //
    188   return ((('0' <= (c)) && ((c) <= '9')) ||
    189           (('a' <= (c)) && ((c) <= 'z')) ||
    190           (('A' <= (c)) && ((c) <= 'Z')));
    191 }
    192 
    193 /* Determines if a particular character is in upper case */
    194 int isupper (int c)
    195 {
    196   //
    197   // <uppercase letter> := [A-Z]
    198   //
    199   return (('A' <= (c)) && ((c) <= 'Z'));
    200 }
    201 
    202 //
    203 // -- Data Conversion Routines --
    204 //
    205 
    206 /* Convert strings to a long-integer value */
    207 long strtol (const char *nptr, char **endptr, int base)
    208 {
    209   //
    210   // Null strtol() function implementation to satisfy the linker, since there is
    211   // no direct functionality logic dependency in present UEFI cases.
    212   //
    213   return 0;
    214 }
    215 
    216 /* Convert strings to an unsigned long-integer value */
    217 unsigned long strtoul (const char *nptr, char **endptr, int base)
    218 {
    219   //
    220   // Null strtoul() function implementation to satisfy the linker, since there is
    221   // no direct functionality logic dependency in present UEFI cases.
    222   //
    223   return 0;
    224 }
    225 
    226 /* Convert character to lowercase */
    227 int tolower (int c)
    228 {
    229   if (('A' <= (c)) && ((c) <= 'Z')) {
    230     return (c - ('A' - 'a'));
    231   }
    232   return (c);
    233 }
    234 
    235 //
    236 // -- Searching and Sorting Routines --
    237 //
    238 
    239 /* Performs a quick sort */
    240 void qsort (void *base, size_t num, size_t width, int (*compare)(const void *, const void *))
    241 {
    242   VOID  *Buffer;
    243 
    244   ASSERT (base    != NULL);
    245   ASSERT (compare != NULL);
    246 
    247   //
    248   // Use CRT-style malloc to cover BS and RT memory allocation.
    249   //
    250   Buffer = malloc (width);
    251   ASSERT (Buffer != NULL);
    252 
    253   //
    254   // Re-use PerformQuickSort() function Implementation in EDKII BaseSortLib.
    255   //
    256   QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare, Buffer);
    257 
    258   free (Buffer);
    259   return;
    260 }
    261 
    262 //
    263 // -- Process and Environment Control Routines --
    264 //
    265 
    266 /* Get a value from the current environment */
    267 char *getenv (const char *varname)
    268 {
    269   //
    270   // Null getenv() function implementation to satisfy the linker, since there is
    271   // no direct functionality logic dependency in present UEFI cases.
    272   //
    273   return NULL;
    274 }
    275 
    276 //
    277 // -- Stream I/O Routines --
    278 //
    279 
    280 /* Write formatted output using a pointer to a list of arguments */
    281 int vfprintf (FILE *stream, const char *format, VA_LIST arg)
    282 {
    283   return 0;
    284 }
    285 
    286 /* Write data to a stream */
    287 size_t fwrite (const void *buffer, size_t size, size_t count, FILE *stream)
    288 {
    289   return 0;
    290 }
    291 
    292 //
    293 //  -- Dummy OpenSSL Support Routines --
    294 //
    295 
    296 int BIO_printf (void *bio, const char *format, ...)
    297 {
    298   return 0;
    299 }
    300 
    301 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
    302 {
    303   return 0;
    304 }
    305 
    306 void *UI_OpenSSL(void)
    307 {
    308   return NULL;
    309 }
    310 
    311 int X509_load_cert_file (VOID *ctx, const char *file, int type)
    312 {
    313   return 0;
    314 }
    315 
    316 int X509_load_crl_file (VOID *ctx, const char *file, int type)
    317 {
    318   return 0;
    319 }
    320 
    321 int chmod (const char *c, mode_t m)
    322 {
    323   return -1;
    324 }
    325 
    326 int close (int f)
    327 {
    328   return -1;
    329 }
    330 
    331 void closelog (void)
    332 {
    333 
    334 }
    335 
    336 #ifdef __GNUC__
    337 
    338 typedef
    339 VOID
    340 (EFIAPI *NoReturnFuncPtr)(
    341   VOID
    342   ) __attribute__((__noreturn__));
    343 
    344 
    345 STATIC
    346 VOID
    347 EFIAPI
    348 NopFunction (
    349   VOID
    350   )
    351 {
    352 }
    353 
    354 
    355 void exit (int e)
    356 {
    357   NoReturnFuncPtr NoReturnFunc;
    358 
    359   NoReturnFunc = (NoReturnFuncPtr) NopFunction;
    360 
    361   NoReturnFunc ();
    362 }
    363 
    364 #else
    365 
    366 void exit (int e)
    367 {
    368 }
    369 
    370 #endif
    371 
    372 int fclose (FILE *f)
    373 {
    374   return 0;
    375 }
    376 
    377 FILE *fopen (const char *c, const char *m)
    378 {
    379   return NULL;
    380 }
    381 
    382 size_t fread (void *b, size_t c, size_t i, FILE *f)
    383 {
    384   return 0;
    385 }
    386 
    387 int fputs (const char *s, FILE *f)
    388 {
    389   return 0;
    390 }
    391 
    392 int fprintf (FILE *f, const char *s, ...)
    393 {
    394   return 0;
    395 }
    396 
    397 uid_t getuid (void)
    398 {
    399   return 0;
    400 }
    401 
    402 uid_t geteuid (void)
    403 {
    404   return 0;
    405 }
    406 
    407 gid_t getgid (void)
    408 {
    409   return 0;
    410 }
    411 
    412 gid_t getegid (void)
    413 {
    414   return 0;
    415 }
    416 
    417 off_t lseek (int a, off_t o, int d)
    418 {
    419   return 0;
    420 }
    421 
    422 void openlog (const char *c, int a, int b)
    423 {
    424 
    425 }
    426 
    427 ssize_t read (int f, void *b, size_t c)
    428 {
    429   return 0;
    430 }
    431 
    432 int stat (const char *c, struct stat *s)
    433 {
    434   return -1;
    435 }
    436 
    437 int strcasecmp (const char *c, const char *s)
    438 {
    439   return 0;
    440 }
    441 
    442 int strncasecmp (const char *c, const char *s, size_t l)
    443 {
    444   return 0;
    445 }
    446 
    447 void syslog (int a, const char *c, ...)
    448 {
    449 
    450 }
    451 
    452 ssize_t write (int f, const void *b, size_t l)
    453 {
    454   return 0;
    455 }
    456 
    457 int printf (char const *fmt, ...)
    458 {
    459   return 0;
    460 }
    461