binascii.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file binascii.h
  8. *
  9. * \brief Header for binascii.c
  10. **/
  11. #ifndef TOR_BINASCII_H
  12. #define TOR_BINASCII_H
  13. #include "orconfig.h"
  14. #include <stddef.h>
  15. #include "lib/testsupport/testsupport.h"
  16. #include "lib/cc/torint.h"
  17. const char *hex_str(const char *from, size_t fromlen);
  18. /** @{ */
  19. /** These macros don't check for overflow. Use them only for constant inputs
  20. * (like array declarations). The *_LEN macros are the raw encoding lengths
  21. * (without terminating NUL), while the *_BUFSIZE macros count the terminating
  22. * NUL. */
  23. #define BASE64_LEN(n) (CEIL_DIV((n), 3) * 4)
  24. #define BASE32_LEN(n) (CEIL_DIV((n), 5) * 8)
  25. #define BASE16_LEN(n) ((n) * 2)
  26. #define BASE64_BUFSIZE(n) (BASE64_LEN(n) + 1)
  27. #define BASE32_BUFSIZE(n) (BASE32_LEN(n) + 1)
  28. #define BASE16_BUFSIZE(n) (BASE16_LEN(n) + 1)
  29. #define BASE64_NOPAD_LEN(n) (CEIL_DIV((n) * 4, 3))
  30. #define BASE32_NOPAD_LEN(n) (CEIL_DIV((n) * 8, 5))
  31. #define BASE64_NOPAD_BUFSIZE(n) (BASE64_NOPAD_LEN(n) + 1)
  32. #define BASE32_NOPAD_BUFSIZE(n) (BASE32_NOPAD_LEN(n) + 1)
  33. /** @} */
  34. #define BASE64_ENCODE_MULTILINE 1
  35. size_t base64_encode_size(size_t srclen, int flags);
  36. int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen,
  37. int flags);
  38. int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  39. int base64_encode_nopad(char *dest, size_t destlen,
  40. const uint8_t *src, size_t srclen);
  41. /** Characters that can appear (case-insensitively) in a base32 encoding. */
  42. #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
  43. void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  44. int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  45. size_t base32_encoded_size(size_t srclen);
  46. void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen);
  47. int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen);
  48. #endif /* !defined(TOR_UTIL_FORMAT_H) */