16#if !HAVE_NETTLE_BASE64_H || !HAVE_NETTLE34_BASE64
49#define TABLE_INVALID -1
56 static const signed char base64_decode_table[0x100] =
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -1, -1,
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
62 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1,
63 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
64 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
65 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
66 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
67 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
78 ctx->
table = base64_decode_table;
86 int data = ctx->
table[(uint8_t) src];
91 assert(data >= 0 && data < 0x40);
118 if (ctx->
word & ( (1<<ctx->
bits) - 1))
138 for (i = 0, done = 0; i<src_length; i++)
161 return ctx->
bits == 0;
166#define ENCODE(alphabet,x) ((alphabet)[0x3F & (x)])
170 char *dst,
size_t length,
const uint8_t *src)
172 const uint8_t *in = src + length;
175 unsigned left_over = length % 3;
185 *--out =
ENCODE(alphabet, (in[0] << 4));
189 *--out =
ENCODE(alphabet, (in[1] << 2));
190 *--out =
ENCODE(alphabet, ((in[0] << 4) | (in[1] >> 4)));
196 *--out =
ENCODE(alphabet, (in[0] >> 2));
202 *--out =
ENCODE(alphabet, (in[2]));
203 *--out =
ENCODE(alphabet, ((in[1] << 2) | (in[2] >> 6)));
204 *--out =
ENCODE(alphabet, ((in[0] << 4) | (in[1] >> 4)));
205 *--out =
ENCODE(alphabet, (in[0] >> 2));
212 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
213 "abcdefghijklmnopqrstuvwxyz"
245 unsigned word = ctx->
word << 8 | src;
246 unsigned bits = ctx->
bits + 8;
271 size_t left = length;
275 while (ctx->
bits && left)
281 left_over = left % 3;
282 bulk = left - left_over;
312 unsigned bits = ctx->
bits;
317 for (; bits < 6; bits += 2)
void base64_encode_raw(char *dst, size_t length, const uint8_t *src)
void base64_encode_group(char *dst, uint32_t group)
int base64_decode_single(struct base64_decode_ctx *ctx, uint8_t *dst, char src)
void base64_encode_init(struct base64_encode_ctx *ctx)
size_t base64_encode_single(struct base64_encode_ctx *ctx, char *dst, uint8_t src)
size_t base64_encode_update(struct base64_encode_ctx *ctx, char *dst, size_t length, const uint8_t *src)
#define ENCODE(alphabet, x)
static const char base64_encode_table[64]
void base64_decode_init(struct base64_decode_ctx *ctx)
size_t base64_encode_final(struct base64_encode_ctx *ctx, char *dst)
int base64_decode_update(struct base64_decode_ctx *ctx, size_t *dst_length, uint8_t *dst, size_t src_length, const char *src)
int base64_decode_final(struct base64_decode_ctx *ctx)
static void encode_raw(const char *alphabet, char *dst, size_t length, const uint8_t *src)
#define BASE64_ENCODE_FINAL_LENGTH
#define BASE64_ENCODE_RAW_LENGTH(length)
#define BASE64_DECODE_LENGTH(length)
#define BASE64_ENCODE_LENGTH(length)
const signed char * table