37 if (fromlen>(
sizeof(buf)-1)/2)
38 fromlen = (
sizeof(buf)-1)/2;
49 base32_encoded_size(
size_t srclen)
53 enclen = BASE32_NOPAD_BUFSIZE(srclen);
54 tor_assert(enclen < INT_MAX && enclen > srclen);
60 base32_encode(
char *dest,
size_t destlen,
const char *src,
size_t srclen)
63 size_t nbits = srclen * 8;
67 tor_assert(base32_encoded_size(srclen) <= destlen);
71 memset(dest, 0, destlen);
73 for (i=0,bit=0; bit < nbits; ++i, bit+=5) {
76 v = ((uint8_t)src[idx]) << 8;
78 v += (uint8_t)src[idx+1];
80 u = (v >> (11-(bit%8))) & 0x1F;
90 base32_decode(
char *dest,
size_t destlen,
const char *src,
size_t srclen)
97 nbits = ((srclen * 5) / 8) * 8;
104 memset(dest, 0, destlen);
107 tmp = tor_malloc_zero(srclen);
108 for (j = 0; j < srclen; ++j) {
109 if (src[j] > 0x60 && src[j] < 0x7B) tmp[j] = src[j] - 0x61;
110 else if (src[j] > 0x31 && src[j] < 0x38) tmp[j] = src[j] - 0x18;
111 else if (src[j] > 0x40 && src[j] < 0x5B) tmp[j] = src[j] - 0x41;
113 log_warn(
LD_GENERAL,
"illegal character in base32 encoded string");
120 for (i = 0, bit = 0; bit < nbits; ++i, bit += 8) {
123 dest[i] = (((uint8_t)tmp[(bit/5)]) << 3) +
124 (((uint8_t)tmp[(bit/5)+1]) >> 2);
127 dest[i] = (((uint8_t)tmp[(bit/5)]) << 6) +
128 (((uint8_t)tmp[(bit/5)+1]) << 1) +
129 (((uint8_t)tmp[(bit/5)+2]) >> 4);
132 dest[i] = (((uint8_t)tmp[(bit/5)]) << 4) +
133 (((uint8_t)tmp[(bit/5)+1]) >> 1);
136 dest[i] = (((uint8_t)tmp[(bit/5)]) << 7) +
137 (((uint8_t)tmp[(bit/5)+1]) << 2) +
138 (((uint8_t)tmp[(bit/5)+2]) >> 3);
141 dest[i] = (((uint8_t)tmp[(bit/5)]) << 5) +
142 ((uint8_t)tmp[(bit/5)+1]);
147 memset(tmp, 0, srclen);
153 #define BASE64_OPENSSL_LINELEN 64
172 tor_assert(CEIL_DIV(srclen, 3) < INT_MAX / 4);
175 if (flags & BASE64_ENCODE_MULTILINE)
176 enclen += CEIL_DIV(enclen, BASE64_OPENSSL_LINELEN);
178 tor_assert(enclen < INT_MAX && (enclen == 0 || enclen > srclen));
191 return CEIL_DIV(srclen * 3, 4);
196 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
197 'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
198 'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
199 'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
200 'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
201 'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
202 'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
203 '4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'
218 const unsigned char *usrc = (
unsigned char *)src;
219 const unsigned char *eous = usrc + srclen;
231 if (destlen < enclen + 1)
235 if (enclen > INT_MAX)
239 memset(dest, 0, destlen);
245 #define ENCODE_CHAR(ch) \
248 if (flags & BASE64_ENCODE_MULTILINE) { \
249 if (++linelen % BASE64_OPENSSL_LINELEN == 0) { \
256 #define ENCODE_N(idx) \
257 ENCODE_CHAR(base64_encode_table[(n >> ((3 - idx) * 6)) & 0x3f])
259 #define ENCODE_PAD() ENCODE_CHAR('=')
265 for ( ; usrc < eous; ++usrc) {
266 n = (n << 8) | *usrc;
267 if ((++n_idx) == 3) {
314 if (flags & BASE64_ENCODE_MULTILINE && linelen != 0)
330 const uint8_t *src,
size_t srclen)
332 int n =
base64_encode(dest, destlen, (
const char*) src, srclen, 0);
335 tor_assert((
size_t)n < destlen && dest[n] == 0);
339 if (*in ==
'=' || *in ==
'\n') {
349 return (
int)(out - dest);
352 #undef BASE64_OPENSSL_LINELEN
365 X,
X,
X,
X,
X,
X,
X,
X,
X, SP, SP, SP,
X, SP,
X,
X,
366 X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
367 SP,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X, 62,
X,
X,
X, 63,
368 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
X,
X,
X, PAD,
X,
X,
369 X, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
370 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
X,
X,
X,
X,
X,
371 X, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
372 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
X,
X,
X,
X,
X,
373 X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
374 X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
375 X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
376 X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
377 X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
378 X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
379 X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
380 X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
X,
398 const char *eos = src+srclen;
403 if (destlen > INT_MAX)
407 memset(dest, 0, destlen);
413 for ( ; src < eos; ++src) {
414 unsigned char c = (
unsigned char) *src;
429 if ((++n_idx) == 4) {
431 if (destlen < 3 || di > destlen - 3)
433 dest[di++] = (n>>16);
434 dest[di++] = (n>>8) & 0xff;
435 dest[di++] = (n) & 0xff;
453 if (destlen < 1 || di > destlen - 1)
459 if (destlen < 2 || di > destlen - 2)
461 dest[di++] = n >> 10;
484 tor_assert(destlen >= BASE16_BUFSIZE(srclen));
488 memset(dest, 0, destlen);
493 *cp++ =
"0123456789ABCDEF"[ (*(
const uint8_t*)src) >> 4 ];
494 *cp++ =
"0123456789ABCDEF"[ (*(
const uint8_t*)src) & 0xf ];
509 char *dest_orig = dest;
512 if ((srclen % 2) != 0)
514 if (destlen < srclen/2 || destlen > INT_MAX)
518 memset(dest, 0, destlen);
526 *(uint8_t*)dest = (v1<<4)|v2;
531 tor_assert((dest-dest_orig) <= (ptrdiff_t) destlen);
533 return (
int) (dest-dest_orig);
int base32_decode(char *dest, size_t destlen, const char *src, size_t srclen)
const char * hex_str(const char *from, size_t fromlen)
int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen)
int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen, int flags)
int base64_encode_nopad(char *dest, size_t destlen, const uint8_t *src, size_t srclen)
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
static const uint8_t base64_decode_table[256]
size_t base64_decode_maxsize(size_t srclen)
size_t base64_encode_size(size_t srclen, int flags)
static const char base64_encode_table[64]
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Locale-independent character-type inspection (header)
static int hex_decode_digit(char c)
Headers for util_malloc.c.
Integer definitions used throughout Tor.
Macros to manage assertions, fatal and non-fatal.
#define tor_fragile_assert()