github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/cpvmm/vmm/bootstrap/bootstrap_string.c (about)

     1  /*
     2   * File: bootstrap_string.c
     3   * Description: string support for bootstrap
     4   * Author: John Manferdelli 
     5   * Copyright (c) 2013 Intel Corporation
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  
    19  // this is all 32 bit code
    20  #include "bootstrap_types.h"
    21  #include "bootstrap_string.h"
    22  
    23  extern void bprint(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
    24  
    25  char* vmm_strncpy(char *dest, const char *src, int n)
    26  {
    27      char* out= dest;
    28  
    29      while(n>0 && *src!='\0') {
    30          *(dest++)= *(src++);
    31          n--;
    32      }
    33      *dest= 0;
    34      return out;
    35  }
    36  
    37  
    38  #ifndef __x86_64__
    39  char* vmm_strcpy(char *dest, const char *src)
    40  {
    41      char* out= dest;
    42  
    43      while(*src!='\0') {
    44          *(dest++)= *(src++);
    45      }
    46      *dest= 0;
    47      return out;
    48  }
    49  #endif
    50  
    51  
    52  char* vmm_strchr (const char * str, int character)
    53  {
    54      const char* p= str;
    55  
    56      while(1) {
    57          if(*p==character)
    58              return (char*) p;
    59          if(*p=='\0')
    60              break;
    61          p++;
    62      }
    63      return NULL;
    64  }
    65  
    66  
    67  #ifndef __x86_64__
    68  int vmm_strcmp (const char * str1, const char * str2)
    69  {
    70      while(*str1!='\0' && *str2!='\0') {
    71          if(*str1>*str2)
    72              return 1;
    73          if(*str1<*str2)
    74              return -1;
    75          str1++; str2++;
    76      }
    77      return 0;
    78  }
    79  #endif
    80  
    81  
    82  int vmm_strncmp (const char * str1, const char * str2, int n)
    83  {
    84  
    85      while(n>=0 && *str1!='\0' && *str2!='\0') {
    86          if(*str1>*str2)
    87              return 1;
    88          if(*str1<*str2)
    89              return -1;
    90          str1++; str2++;
    91          n--;
    92      }
    93      return 0;
    94  }
    95  
    96  
    97  #ifndef __x86_64__
    98  void *vmm_memset(void *dest, int val, uint32_t count)
    99  {
   100      uint8_t* p= (uint8_t*) dest;
   101      while(count-->0)
   102  	*(p++)= (uint8_t) val;
   103      return dest;
   104  }
   105  
   106  
   107  void *vmm_memcpy(void *dest, const void* src, uint32_t count)
   108  {
   109      uint8_t* p= (uint8_t*) dest;
   110      uint8_t* q= (uint8_t*) src;
   111      while(count-->0)
   112  	*(p++)= *(q++);
   113      return dest;
   114  }
   115  
   116  
   117  uint32_t vmm_strlen(const char* p)
   118  {
   119      uint32_t count= 0;
   120  
   121      if(p==NULL)
   122          return 0;
   123      while(*(p++)!=0) {
   124          count++;
   125      }
   126      return count;
   127  }
   128  #endif
   129  
   130  
   131  uint64_t vmm_strtoul (const char* str, char** endptr, int base)
   132  {
   133    (void)str;
   134    (void)endptr;
   135    (void)base;
   136    return 0;
   137  }
   138  
   139  
   140  void HexDump(uint8_t* start, uint8_t* end)
   141  {
   142      uint8_t* p= start;
   143      int      i;
   144  
   145      while(p<=end) {
   146        bprint("%p: ", p);
   147          i= 0;
   148          while(p<=end) {
   149              bprint("%08x ", *(uint32_t*)p);
   150              p+= 4;
   151              i++;
   152              if(i>3)
   153                  break;
   154          } 
   155          bprint("\n");
   156      }
   157      bprint("\n");
   158  }
   159  
   160  
   161  
   162  #define _XA     0x00    /* extra alphabetic - not supported */
   163  #define _XS     0x40    /* extra space */
   164  #define _BB     0x00    /* BEL, BS, etc. - not supported */
   165  #define _CN     0x20    /* CR, FF, HT, NL, VT */
   166  #define _DI     0x04    /* ''-'9' */
   167  #define _LO     0x02    /* 'a'-'z' */
   168  #define _PU     0x10    /* punctuation */
   169  #define _SP     0x08    /* space */
   170  #define _UP     0x01    /* 'A'-'Z' */
   171  #define _XD     0x80    /* ''-'9', 'A'-'F', 'a'-'f' */
   172  
   173  
   174  const uint8_t _ctype[257] = {
   175      _CN,            /* 0x0      0.     */
   176      _CN,            /* 0x1      1.     */
   177      _CN,            /* 0x2      2.     */
   178      _CN,            /* 0x3      3.     */
   179      _CN,            /* 0x4      4.     */
   180      _CN,            /* 0x5      5.     */
   181      _CN,            /* 0x6      6.     */
   182      _CN,            /* 0x7      7.     */
   183      _CN,            /* 0x8      8.     */
   184      _CN|_SP,        /* 0x9      9.     */
   185      _CN|_SP,        /* 0xA     10.     */
   186      _CN|_SP,        /* 0xB     11.     */
   187      _CN|_SP,        /* 0xC     12.     */
   188      _CN|_SP,        /* 0xD     13.     */
   189      _CN,            /* 0xE     14.     */
   190      _CN,            /* 0xF     15.     */
   191      _CN,            /* 0x10    16.     */
   192      _CN,            /* 0x11    17.     */
   193      _CN,            /* 0x12    18.     */
   194      _CN,            /* 0x13    19.     */
   195      _CN,            /* 0x14    20.     */
   196      _CN,            /* 0x15    21.     */
   197      _CN,            /* 0x16    22.     */
   198      _CN,            /* 0x17    23.     */
   199      _CN,            /* 0x18    24.     */
   200      _CN,            /* 0x19    25.     */
   201      _CN,            /* 0x1A    26.     */
   202      _CN,            /* 0x1B    27.     */
   203      _CN,            /* 0x1C    28.     */
   204      _CN,            /* 0x1D    29.     */
   205      _CN,            /* 0x1E    30.     */
   206      _CN,            /* 0x1F    31.     */
   207      _XS|_SP,        /* 0x20    32. ' ' */
   208      _PU,            /* 0x21    33. '!' */
   209      _PU,            /* 0x22    34. '"' */
   210      _PU,            /* 0x23    35. '#' */
   211      _PU,            /* 0x24    36. '$' */
   212      _PU,            /* 0x25    37. '%' */
   213      _PU,            /* 0x26    38. '&' */
   214      _PU,            /* 0x27    39. ''' */
   215      _PU,            /* 0x28    40. '(' */
   216      _PU,            /* 0x29    41. ')' */
   217      _PU,            /* 0x2A    42. '*' */
   218      _PU,            /* 0x2B    43. '+' */
   219      _PU,            /* 0x2C    44. ',' */
   220      _PU,            /* 0x2D    45. '-' */
   221      _PU,            /* 0x2E    46. '.' */
   222      _PU,            /* 0x2F    47. '/' */
   223      _XD|_DI,        /* 0x30    48. '' */
   224      _XD|_DI,        /* 0x31    49. '1' */
   225      _XD|_DI,        /* 0x32    50. '2' */
   226      _XD|_DI,        /* 0x33    51. '3' */
   227      _XD|_DI,        /* 0x34    52. '4' */
   228      _XD|_DI,        /* 0x35    53. '5' */
   229      _XD|_DI,        /* 0x36    54. '6' */
   230      _XD|_DI,        /* 0x37    55. '7' */
   231      _XD|_DI,        /* 0x38    56. '8' */
   232      _XD|_DI,        /* 0x39    57. '9' */
   233      _PU,            /* 0x3A    58. ':' */
   234      _PU,            /* 0x3B    59. ';' */
   235      _PU,            /* 0x3C    60. '<' */
   236      _PU,            /* 0x3D    61. '=' */
   237      _PU,            /* 0x3E    62. '>' */
   238      _PU,            /* 0x3F    63. '?' */
   239      _PU,            /* 0x40    64. '@' */
   240      _XD|_UP,        /* 0x41    65. 'A' */
   241      _XD|_UP,        /* 0x42    66. 'B' */
   242      _XD|_UP,        /* 0x43    67. 'C' */
   243      _XD|_UP,        /* 0x44    68. 'D' */
   244      _XD|_UP,        /* 0x45    69. 'E' */
   245      _XD|_UP,        /* 0x46    70. 'F' */
   246      _UP,            /* 0x47    71. 'G' */
   247      _UP,            /* 0x48    72. 'H' */
   248      _UP,            /* 0x49    73. 'I' */
   249      _UP,            /* 0x4A    74. 'J' */
   250      _UP,            /* 0x4B    75. 'K' */
   251      _UP,            /* 0x4C    76. 'L' */
   252      _UP,            /* 0x4D    77. 'M' */
   253      _UP,            /* 0x4E    78. 'N' */
   254      _UP,            /* 0x4F    79. 'O' */
   255      _UP,            /* 0x50    80. 'P' */
   256      _UP,            /* 0x51    81. 'Q' */
   257      _UP,            /* 0x52    82. 'R' */
   258      _UP,            /* 0x53    83. 'S' */
   259      _UP,            /* 0x54    84. 'T' */
   260      _UP,            /* 0x55    85. 'U' */
   261      _UP,            /* 0x56    86. 'V' */
   262      _UP,            /* 0x57    87. 'W' */
   263      _UP,            /* 0x58    88. 'X' */
   264      _UP,            /* 0x59    89. 'Y' */
   265      _UP,            /* 0x5A    90. 'Z' */
   266      _PU,            /* 0x5B    91. '[' */
   267      _PU,            /* 0x5C    92. '\' */
   268      _PU,            /* 0x5D    93. ']' */
   269      _PU,            /* 0x5E    94. '^' */
   270      _PU,            /* 0x5F    95. '_' */
   271      _PU,            /* 0x60    96. '`' */
   272      _XD|_LO,        /* 0x61    97. 'a' */
   273      _XD|_LO,        /* 0x62    98. 'b' */
   274      _XD|_LO,        /* 0x63    99. 'c' */
   275      _XD|_LO,        /* 0x64   100. 'd' */
   276      _XD|_LO,        /* 0x65   101. 'e' */
   277      _XD|_LO,        /* 0x66   102. 'f' */
   278      _LO,            /* 0x67   103. 'g' */
   279      _LO,            /* 0x68   104. 'h' */
   280      _LO,            /* 0x69   105. 'i' */
   281      _LO,            /* 0x6A   106. 'j' */
   282      _LO,            /* 0x6B   107. 'k' */
   283      _LO,            /* 0x6C   108. 'l' */
   284      _LO,            /* 0x6D   109. 'm' */
   285      _LO,            /* 0x6E   110. 'n' */
   286      _LO,            /* 0x6F   111. 'o' */
   287      _LO,            /* 0x70   112. 'p' */
   288      _LO,            /* 0x71   113. 'q' */
   289      _LO,            /* 0x72   114. 'r' */
   290      _LO,            /* 0x73   115. 's' */
   291      _LO,            /* 0x74   116. 't' */
   292      _LO,            /* 0x75   117. 'u' */
   293      _LO,            /* 0x76   118. 'v' */
   294      _LO,            /* 0x77   119. 'w' */
   295      _LO,            /* 0x78   120. 'x' */
   296      _LO,            /* 0x79   121. 'y' */
   297      _LO,            /* 0x7A   122. 'z' */
   298      _PU,            /* 0x7B   123. '{' */
   299      _PU,            /* 0x7C   124. '|' */
   300      _PU,            /* 0x7D   125. '}' */
   301      _PU,            /* 0x7E   126. '~' */
   302      _CN,            /* 0x7F   127.     */
   303      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  // 0x80 to 0x8F
   304      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  // 0x90 to 0x9F
   305      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  // 0xA0 to 0xAF
   306      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  // 0xB0 to 0xBF
   307      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  // 0xC0 to 0xCF
   308      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  // 0xD0 to 0xDF
   309      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  // 0xE0 to 0xEF
   310      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 // 0xF0 to 0x100
   311  };
   312  
   313  bool isdigit(int c)
   314  {
   315      return (_ctype[(unsigned char)(c)] & (_DI));
   316  }
   317  
   318  
   319  bool isspace(int c)
   320  {
   321      return (_ctype[(unsigned char)(c)] & (_SP));
   322  }
   323  
   324  
   325  bool isxdigit(int c)
   326  {
   327      return (_ctype[(unsigned char)(c)] & (_XD));
   328  }
   329  
   330  
   331  bool isupper(int c)
   332  {
   333      return (_ctype[(unsigned char)(c)] & (_UP));
   334  }
   335  
   336  
   337  bool islower(int c)
   338  {
   339      return (_ctype[(unsigned char)(c)] & (_LO));
   340  }
   341  
   342  
   343  bool isprint(int c)
   344  {
   345      return (_ctype[(unsigned char)(c)] & (_LO | _UP | _DI |
   346                                            _SP | _PU));
   347  }
   348  
   349  
   350  bool isalpha(int c)
   351  {
   352      return (_ctype[(unsigned char)(c)] & (_LO | _UP));
   353  }
   354  
   355  
   356  const char* get_option_val(const cmdline_option_t *options,
   357                                char vals[][MAX_VALUE_LEN], const char *opt_name)
   358  {
   359      int i;
   360      for (i = 0; options[i].name != NULL; i++ ) {
   361          if ( vmm_strcmp(options[i].name, opt_name) == 0 )
   362              return vals[i];
   363      }
   364      bprint("requested unknown option: %s\n", opt_name);
   365      return NULL;
   366  }
   367  
   368  
   369  void cmdline_parse(const char *cmdline, const cmdline_option_t *options,
   370                            char vals[][MAX_VALUE_LEN])
   371  {
   372      const char *p = cmdline;
   373      int i;
   374  
   375      /* copy default values to vals[] */
   376      for ( i = 0; options[i].name != NULL; i++ ) {
   377          vmm_strncpy(vals[i], options[i].def_val, MAX_VALUE_LEN-1);
   378          vals[i][MAX_VALUE_LEN-1] = '\0';
   379      }
   380  
   381      if ( p == NULL )
   382          return;
   383  
   384      /* parse options */
   385      while ( 1 ) {
   386          /* skip whitespace */
   387          while ( isspace(*p) )
   388              p++;
   389          if ( *p == '\0' )
   390              break;
   391  
   392          /* find end of current option */
   393          const char *opt_start = p;
   394          const char *opt_end = (const char*)vmm_strchr(opt_start, ' ');
   395          if ( opt_end == NULL )
   396              opt_end = opt_start + vmm_strlen(opt_start);
   397          p = opt_end;
   398  
   399          /* find value part; if no value found, use default and continue */
   400          const char *val_start = vmm_strchr(opt_start, '=');
   401          if ( val_start == NULL || val_start > opt_end )
   402              continue;
   403          val_start++;
   404  
   405          unsigned int opt_name_size = val_start - opt_start - 1;
   406          unsigned int copy_size = opt_end - val_start;
   407          if ( copy_size > MAX_VALUE_LEN - 1 )
   408              copy_size = MAX_VALUE_LEN - 1;
   409          if ( opt_name_size == 0 || copy_size == 0 )
   410              continue;
   411  
   412          /* value found, so copy it */
   413          for ( i = 0; options[i].name != NULL; i++ ) {
   414              if ( vmm_strncmp(options[i].name, opt_start, opt_name_size ) == 0 ) {
   415                  vmm_strncpy(vals[i], val_start, copy_size);
   416                  vals[i][copy_size] = '\0'; /* add '\0' to the end of string */
   417                  break;
   418              }
   419          }
   420      }
   421  }
   422  
   423  
   424  const char *skip_filename(const char *cmdline)
   425  {
   426      if ( cmdline == NULL || *cmdline == '\0' )
   427          return cmdline;
   428  
   429      /* strip leading spaces, file name, then any spaces until the next
   430       non-space char (e.g. "  /foo/bar   baz" -> "baz"; "/foo/bar" -> "")*/
   431      while ( *cmdline != '\0' && isspace(*cmdline) )
   432          cmdline++;
   433      while ( *cmdline != '\0' && !isspace(*cmdline) )
   434          cmdline++;
   435      while ( *cmdline != '\0' && isspace(*cmdline) )
   436          cmdline++;
   437      return cmdline;
   438  }
   439  
   440