5 /* nettle, low-level cryptographics library
7 * Copyright (C) 2002 Niels Möller
9 * The nettle library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or (at your
12 * option) any later version.
14 * The nettle library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the nettle library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 static const uint8_t encode_table[64] =
35 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
36 "abcdefghijklmnopqrstuvwxyz"
39 #define ENCODE(x) (encode_table[0x3F & (x)])
42 base64_encode_raw(uint8_t *dst, unsigned length, const uint8_t *src)
44 const uint8_t *in = src + length;
45 uint8_t *out = dst + BASE64_ENCODE_RAW_LENGTH(length);
47 unsigned left_over = length % 3;
57 *--out = ENCODE(in[0] << 4);
61 *--out = ENCODE( in[1] << 2);
62 *--out = ENCODE((in[0] << 4) | (in[1] >> 4));
68 *--out = ENCODE(in[0] >> 2);
74 *--out = ENCODE( in[2]);
75 *--out = ENCODE((in[1] << 2) | (in[2] >> 6));
76 *--out = ENCODE((in[0] << 4) | (in[1] >> 4));
77 *--out = ENCODE( in[0] >> 2);
85 base64_encode(uint8_t *dst,
89 unsigned dst_length = BASE64_ENCODE_RAW_LENGTH(src_length);
90 unsigned n = src_length / 3;
91 unsigned left_over = src_length % 3;
96 const uint8_t *in = src + n * 3;
97 uint8_t *out = dst + dst_length;
103 *--out = ENCODE(in[0] << 4);
107 *--out = ENCODE( in[1] << 2);
108 *--out = ENCODE((in[0] << 4) | (in[1] >> 4));
114 *--out = ENCODE(in[0] >> 2);
118 base64_encode_raw(n, dst, src);
121 assert(done == dst_length);
128 base64_encode_group(uint8_t *dst, uint32_t group)
130 *dst++ = ENCODE(group >> 18);
131 *dst++ = ENCODE(group >> 12);
132 *dst++ = ENCODE(group >> 6);
133 *dst++ = ENCODE(group);
137 base64_encode_init(struct base64_encode_ctx *ctx)
139 ctx->word = ctx->bits = 0;
142 /* Encodes a single byte. */
144 base64_encode_single(struct base64_encode_ctx *ctx,
149 unsigned word = ctx->word << 8 | src;
150 unsigned bits = ctx->bits + 8;
155 dst[done++] = ENCODE(word >> bits);
166 /* Returns the number of output characters. DST should point to an
167 * area of size at least BASE64_ENCODE_LENGTH(length). */
169 base64_encode_update(struct base64_encode_ctx *ctx,
175 unsigned left = length;
179 while (ctx->bits && left)
182 done += base64_encode_single(ctx, dst + done, *src++);
185 left_over = left % 3;
186 bulk = left - left_over;
192 base64_encode_raw(dst + done, bulk, src);
193 done += BASE64_ENCODE_RAW_LENGTH(bulk);
201 done += base64_encode_single(ctx, dst + done, *src++);
204 assert(done <= BASE64_ENCODE_LENGTH(length));
209 /* DST should point to an area of size at least
210 * BASE64_ENCODE_FINAL_SIZE */
212 base64_encode_final(struct base64_encode_ctx *ctx,
216 unsigned bits = ctx->bits;
220 dst[done++] = ENCODE(ctx->word << (6 - ctx->bits));
221 for (; bits < 6; bits += 2)
227 assert(done <= BASE64_ENCODE_FINAL_LENGTH);