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

     1  /*
     2   * Copyright (c) 2013 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *     http://www.apache.org/licenses/LICENSE-2.0
     8   * Unless required by applicable law or agreed to in writing, software
     9   * distributed under the License is distributed on an "AS IS" BASIS,
    10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11   * See the License for the specific language governing permissions and
    12   * limitations under the License.
    13   */
    14  
    15  #include "common_libc.h"
    16  #include "hw_utils.h"
    17  #include "cli_libc.h"
    18  
    19  typedef INT32 (*ASCII_TO_DIGIT)(char);
    20  
    21  
    22  int CLI_strcmp( char *string1, char *string2)
    23  {
    24      while ((*string1 == *string2) && (0 != *string1)) {
    25          string1++;
    26          string2++;
    27      }
    28      return (*string1 - *string2);
    29  }
    30  
    31  int CLI_strncmp( char *string1, char *string2, size_t n)
    32  {
    33      size_t i;
    34  
    35      for (i = 0; i < n; ++i) {
    36          if (string1[i] != string2[i])
    37              return 1;
    38      }
    39      return 0;
    40  }
    41  
    42  int CLI_is_substr( char *bigstring, char *smallstring)
    43  {
    44      while ((*bigstring == *smallstring) && (0 != *smallstring)) {
    45          bigstring++;
    46          smallstring++;
    47      }
    48      return (0 == *smallstring);
    49  }
    50  
    51  INT32 asciiToDecDigit( char ch)
    52  {
    53      if (ch >= '0' && ch <= '9')
    54          return ch - '0';
    55      return -1;
    56  }
    57  
    58  INT32 asciiToHexDigit(
    59                  char ch)
    60  {
    61      if (ch >= '0' && ch <= '9')
    62          return ch - '0';
    63      if (ch >= 'a' && ch <= 'f')
    64          return ch - 'a' + 10;
    65      if (ch >= 'A' && ch <= 'F')
    66          return ch - 'A' + 10;
    67      return -1;
    68  }
    69  
    70  UINT64 CLI_atol64(
    71           char *string, unsigned base, int *perror)
    72  {
    73      UINT64 value = 0;
    74      INT32  lastDigit;
    75      ASCII_TO_DIGIT ascii_to_digit;
    76      int error = 0;
    77  
    78      do {
    79  
    80          if (10 == base) {
    81              ascii_to_digit = asciiToDecDigit;
    82          }
    83          else if (16 == base) {
    84              ascii_to_digit = asciiToHexDigit;
    85          }
    86          else {
    87              error = -1;    // bad base
    88              value = 0;
    89              break;
    90          }
    91  
    92          while (*string != 0) {
    93              lastDigit = (*ascii_to_digit)(*string);
    94              if (-1 == lastDigit) {
    95                  error = -1;    // bad input
    96                  value = 0;
    97                  break;
    98              }
    99              value = value * base + lastDigit;
   100              string++;
   101          }
   102  
   103      } while (0);
   104  
   105      if (NULL != perror) {
   106          *perror = error;
   107      }
   108      return value;
   109  }
   110  
   111  
   112  UINT32 CLI_atol32( char *string, unsigned base, int *error)
   113  {
   114      return (UINT32) CLI_atol64(string, base, error);
   115  }
   116