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
#define TABLE_INVALID -1
35
#define TABLE_SPACE -2
38
/* FIXME: Make sure that all whitespace characters, SPC, HT, VT, FF,
39
* CR and LF are ignored. */
40
static const signed char
43
-1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
44
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45
-2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
46
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1,
47
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
48
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
49
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
50
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
51
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
57
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62
base64_decode_init(struct base64_decode_ctx *ctx)
64
ctx->word = ctx->bits = ctx->padding = 0;
68
base64_decode_single(struct base64_decode_ctx *ctx,
74
data = decode_table[src];
79
assert(data >= 0 && data < 0x40);
84
ctx->word = ctx->word << 6 | data;
90
dst[0] = ctx->word >> ctx->bits;
102
/* There can be at most two padding characters. */
103
if (!ctx->bits || ctx->padding > 2)
106
if (ctx->word & ( (1<<ctx->bits) - 1))
107
/* We shouldn't have any leftover bits */
117
base64_decode_update(struct base64_decode_ctx *ctx,
118
unsigned *dst_length,
126
assert(*dst_length >= BASE64_DECODE_LENGTH(src_length));
128
for (i = 0, done = 0; i<src_length; i++)
129
switch(base64_decode_single(ctx, dst + done, src[i]))
142
assert(done <= BASE64_DECODE_LENGTH(src_length));
149
base64_decode_final(struct base64_decode_ctx *ctx)
151
return ctx->bits == 0;