compat_ctype.h 2.0 KB

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