tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

compat_ctype.h (2089B)


      1 /* Copyright (c) 2003-2004, Roger Dingledine
      2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
      3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
      4 /* See LICENSE for licensing information */
      5 
      6 /**
      7 * \file compat_ctype.h
      8 * \brief Locale-independent character-type inspection (header)
      9 **/
     10 
     11 #ifndef TOR_COMPAT_CTYPE_H
     12 #define TOR_COMPAT_CTYPE_H
     13 
     14 #include "orconfig.h"
     15 #include "lib/cc/torint.h"
     16 
     17 /* Much of the time when we're checking ctypes, we're doing spec compliance,
     18 * which all assumes we're doing ASCII. */
     19 #define DECLARE_CTYPE_FN(name)                                          \
     20  static int TOR_##name(char c);                                        \
     21  extern const uint32_t TOR_##name##_TABLE[];                           \
     22  static inline int TOR_##name(char c) {                                \
     23    uint8_t u = c;                                                      \
     24    return !!(TOR_##name##_TABLE[(u >> 5) & 7] & (1u << (u & 31)));     \
     25  }
     26 DECLARE_CTYPE_FN(ISALPHA)
     27 DECLARE_CTYPE_FN(ISALNUM)
     28 DECLARE_CTYPE_FN(ISSPACE)
     29 DECLARE_CTYPE_FN(ISDIGIT)
     30 DECLARE_CTYPE_FN(ISXDIGIT)
     31 DECLARE_CTYPE_FN(ISPRINT)
     32 DECLARE_CTYPE_FN(ISLOWER)
     33 DECLARE_CTYPE_FN(ISUPPER)
     34 extern const uint8_t TOR_TOUPPER_TABLE[];
     35 extern const uint8_t TOR_TOLOWER_TABLE[];
     36 #define TOR_TOLOWER(c) (TOR_TOLOWER_TABLE[(uint8_t)c])
     37 #define TOR_TOUPPER(c) (TOR_TOUPPER_TABLE[(uint8_t)c])
     38 
     39 static inline int hex_decode_digit(char c);
     40 
     41 /** Helper: given a hex digit, return its value, or -1 if it isn't hex. */
     42 static inline int
     43 hex_decode_digit(char c)
     44 {
     45  switch (c) {
     46    case '0': return 0;
     47    case '1': return 1;
     48    case '2': return 2;
     49    case '3': return 3;
     50    case '4': return 4;
     51    case '5': return 5;
     52    case '6': return 6;
     53    case '7': return 7;
     54    case '8': return 8;
     55    case '9': return 9;
     56    case 'A': case 'a': return 10;
     57    case 'B': case 'b': return 11;
     58    case 'C': case 'c': return 12;
     59    case 'D': case 'd': return 13;
     60    case 'E': case 'e': return 14;
     61    case 'F': case 'f': return 15;
     62    default:
     63      return -1;
     64  }
     65 }
     66 
     67 #endif /* !defined(TOR_COMPAT_CTYPE_H) */