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-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
10 * \brief Header for binascii.c
13 #ifndef TOR_BINASCII_H
14 #define TOR_BINASCII_H
18 #include "lib/testsupport/testsupport.h"
19 #include "lib/cc/torint.h"
21 const char *hex_str(const char *from
, size_t fromlen
);
24 /** These macros don't check for overflow. Use them only for constant inputs
25 * (like array declarations). The *_LEN macros are the raw encoding lengths
26 * (without terminating NUL), while the *_BUFSIZE macros count the terminating
28 #define BASE64_LEN(n) (CEIL_DIV((n), 3) * 4)
29 #define BASE32_LEN(n) (CEIL_DIV((n), 5) * 8)
30 #define BASE16_LEN(n) ((n) * 2)
32 #define BASE64_BUFSIZE(n) (BASE64_LEN(n) + 1)
33 #define BASE32_BUFSIZE(n) (BASE32_LEN(n) + 1)
34 #define BASE16_BUFSIZE(n) (BASE16_LEN(n) + 1)
36 #define BASE64_NOPAD_LEN(n) (CEIL_DIV((n) * 4, 3))
37 #define BASE32_NOPAD_LEN(n) (CEIL_DIV((n) * 8, 5))
39 #define BASE64_NOPAD_BUFSIZE(n) (BASE64_NOPAD_LEN(n) + 1)
40 #define BASE32_NOPAD_BUFSIZE(n) (BASE32_NOPAD_LEN(n) + 1)
43 #define BASE64_ENCODE_MULTILINE 1
44 size_t base64_encode_size(size_t srclen
, int flags
);
45 size_t base64_decode_maxsize(size_t srclen
);
46 int base64_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
,
48 int base64_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
49 int base64_encode_nopad(char *dest
, size_t destlen
,
50 const uint8_t *src
, size_t srclen
);
52 /** Characters that can appear (case-insensitively) in a base32 encoding. */
53 #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567"
54 void base32_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
55 int base32_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
56 size_t base32_encoded_size(size_t srclen
);
58 void base16_encode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
59 int base16_decode(char *dest
, size_t destlen
, const char *src
, size_t srclen
);
61 #endif /* !defined(TOR_BINASCII_H) */