util_format.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #ifndef TOR_UTIL_FORMAT_H
  7. #define TOR_UTIL_FORMAT_H
  8. #include "testsupport.h"
  9. #include "torint.h"
  10. /** @{ */
  11. /** These macros don't check for overflow. Use them only for constant inputs
  12. * (like array declarations). The *_LEN macros are the raw encoding lengths
  13. * (without terminating NUL), while the *_BUFSIZE macros count the terminating
  14. * NUL. */
  15. #define BASE64_LEN(n) (CEIL_DIV((n), 3) * 4)
  16. #define BASE32_LEN(n) (CEIL_DIV((n), 5) * 8)
  17. #define BASE16_LEN(n) ((n) * 2)
  18. #define BASE64_BUFSIZE(n) (BASE64_LEN(n) + 1)
  19. #define BASE32_BUFSIZE(n) (BASE32_LEN(n) + 1)
  20. #define BASE16_BUFSIZE(n) (BASE16_LEN(n) + 1)
  21. #define BASE64_NOPAD_LEN(n) (CEIL_DIV((n) * 4, 3))
  22. #define BASE32_NOPAD_LEN(n) (CEIL_DIV((n) * 8, 5))
  23. #define BASE64_NOPAD_BUFSIZE(n) (BASE64_NOPAD_LEN(n) + 1)
  24. #define BASE32_NOPAD_BUFSIZE(n) (BASE32_NOPAD_LEN(n) + 1)
  25. /** @} */
  26. #define BASE64_ENCODE_MULTILINE 1
  27. size_t base64_encode_size(size_t srclen, int flags);
  28. int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen,
  29. int flags);
  30. int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  31. int base64_encode_nopad(char *dest, size_t destlen,
  32. const uint8_t *src, size_t srclen);
  33. /** Characters that can appear (case-insensitively) in a base32 encoding. */
  34. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
  35. void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  36. int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  37. size_t base32_encoded_size(size_t srclen);
  38. int hex_decode_digit(char c);
  39. void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  40. int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  41. #endif