/src/nettle/base64-encode.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* base64-encode.c |
2 | | |
3 | | Copyright (C) 2002 Niels Möller |
4 | | |
5 | | This file is part of GNU Nettle. |
6 | | |
7 | | GNU Nettle is free software: you can redistribute it and/or |
8 | | modify it under the terms of either: |
9 | | |
10 | | * the GNU Lesser General Public License as published by the Free |
11 | | Software Foundation; either version 3 of the License, or (at your |
12 | | option) any later version. |
13 | | |
14 | | or |
15 | | |
16 | | * the GNU General Public License as published by the Free |
17 | | Software Foundation; either version 2 of the License, or (at your |
18 | | option) any later version. |
19 | | |
20 | | or both in parallel, as here. |
21 | | |
22 | | GNU Nettle is distributed in the hope that it will be useful, |
23 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
25 | | General Public License for more details. |
26 | | |
27 | | You should have received copies of the GNU General Public License and |
28 | | the GNU Lesser General Public License along with this program. If |
29 | | not, see http://www.gnu.org/licenses/. |
30 | | */ |
31 | | |
32 | | #if HAVE_CONFIG_H |
33 | | # include "config.h" |
34 | | #endif |
35 | | |
36 | | #include <assert.h> |
37 | | #include <stdlib.h> |
38 | | |
39 | | #include "base64.h" |
40 | | |
41 | 0 | #define ENCODE(alphabet,x) ((alphabet)[0x3F & (x)]) |
42 | | |
43 | | static void |
44 | | encode_raw(const char *alphabet, |
45 | | char *dst, size_t length, const uint8_t *src) |
46 | 0 | { |
47 | 0 | const uint8_t *in = src + length; |
48 | 0 | char *out = dst + BASE64_ENCODE_RAW_LENGTH(length); |
49 | |
|
50 | 0 | unsigned left_over = length % 3; |
51 | |
|
52 | 0 | if (left_over) |
53 | 0 | { |
54 | 0 | in -= left_over; |
55 | 0 | *--out = '='; |
56 | 0 | switch(left_over) |
57 | 0 | { |
58 | 0 | case 1: |
59 | 0 | *--out = '='; |
60 | 0 | *--out = ENCODE(alphabet, (in[0] << 4)); |
61 | 0 | break; |
62 | | |
63 | 0 | case 2: |
64 | 0 | *--out = ENCODE(alphabet, (in[1] << 2)); |
65 | 0 | *--out = ENCODE(alphabet, ((in[0] << 4) | (in[1] >> 4))); |
66 | 0 | break; |
67 | | |
68 | 0 | default: |
69 | 0 | abort(); |
70 | 0 | } |
71 | 0 | *--out = ENCODE(alphabet, (in[0] >> 2)); |
72 | 0 | } |
73 | | |
74 | 0 | while (in > src) |
75 | 0 | { |
76 | 0 | in -= 3; |
77 | 0 | *--out = ENCODE(alphabet, (in[2])); |
78 | 0 | *--out = ENCODE(alphabet, ((in[1] << 2) | (in[2] >> 6))); |
79 | 0 | *--out = ENCODE(alphabet, ((in[0] << 4) | (in[1] >> 4))); |
80 | 0 | *--out = ENCODE(alphabet, (in[0] >> 2)); |
81 | 0 | } |
82 | 0 | assert(in == src); |
83 | 0 | assert(out == dst); |
84 | 0 | } |
85 | | |
86 | | static const char base64_encode_table[64] = |
87 | | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
88 | | "abcdefghijklmnopqrstuvwxyz" |
89 | | "0123456789+/"; |
90 | | |
91 | | void |
92 | | base64_encode_raw(char *dst, size_t length, const uint8_t *src) |
93 | 0 | { |
94 | 0 | encode_raw(base64_encode_table, dst, length, src); |
95 | 0 | } |
96 | | |
97 | | void |
98 | | base64_encode_group(char *dst, uint32_t group) |
99 | 0 | { |
100 | 0 | *dst++ = ENCODE(base64_encode_table, (group >> 18)); |
101 | 0 | *dst++ = ENCODE(base64_encode_table, (group >> 12)); |
102 | 0 | *dst++ = ENCODE(base64_encode_table, (group >> 6)); |
103 | 0 | *dst++ = ENCODE(base64_encode_table, group); |
104 | 0 | } |
105 | | |
106 | | void |
107 | | base64_encode_init(struct base64_encode_ctx *ctx) |
108 | 0 | { |
109 | 0 | ctx->word = ctx->bits = 0; |
110 | 0 | ctx->alphabet = base64_encode_table; |
111 | 0 | } |
112 | | |
113 | | /* Encodes a single byte. */ |
114 | | size_t |
115 | | base64_encode_single(struct base64_encode_ctx *ctx, |
116 | | char *dst, |
117 | | uint8_t src) |
118 | 0 | { |
119 | 0 | unsigned done = 0; |
120 | 0 | unsigned word = ctx->word << 8 | src; |
121 | 0 | unsigned bits = ctx->bits + 8; |
122 | | |
123 | 0 | while (bits >= 6) |
124 | 0 | { |
125 | 0 | bits -= 6; |
126 | 0 | dst[done++] = ENCODE(ctx->alphabet, (word >> bits)); |
127 | 0 | } |
128 | |
|
129 | 0 | ctx->bits = bits; |
130 | 0 | ctx->word = word; |
131 | |
|
132 | 0 | assert(done <= 2); |
133 | | |
134 | 0 | return done; |
135 | 0 | } |
136 | | |
137 | | /* Returns the number of output characters. DST should point to an |
138 | | * area of size at least BASE64_ENCODE_LENGTH(length). */ |
139 | | size_t |
140 | | base64_encode_update(struct base64_encode_ctx *ctx, |
141 | | char *dst, |
142 | | size_t length, |
143 | | const uint8_t *src) |
144 | 0 | { |
145 | 0 | size_t done = 0; |
146 | 0 | size_t left = length; |
147 | 0 | unsigned left_over; |
148 | 0 | size_t bulk; |
149 | | |
150 | 0 | while (ctx->bits && left) |
151 | 0 | { |
152 | 0 | left--; |
153 | 0 | done += base64_encode_single(ctx, dst + done, *src++); |
154 | 0 | } |
155 | | |
156 | 0 | left_over = left % 3; |
157 | 0 | bulk = left - left_over; |
158 | | |
159 | 0 | if (bulk) |
160 | 0 | { |
161 | 0 | assert(!(bulk % 3)); |
162 | | |
163 | 0 | encode_raw(ctx->alphabet, dst + done, bulk, src); |
164 | 0 | done += BASE64_ENCODE_RAW_LENGTH(bulk); |
165 | 0 | src += bulk; |
166 | 0 | left = left_over; |
167 | 0 | } |
168 | | |
169 | 0 | while (left) |
170 | 0 | { |
171 | 0 | left--; |
172 | 0 | done += base64_encode_single(ctx, dst + done, *src++); |
173 | 0 | } |
174 | |
|
175 | 0 | assert(done <= BASE64_ENCODE_LENGTH(length)); |
176 | | |
177 | 0 | return done; |
178 | 0 | } |
179 | | |
180 | | /* DST should point to an area of size at least |
181 | | * BASE64_ENCODE_FINAL_SIZE */ |
182 | | size_t |
183 | | base64_encode_final(struct base64_encode_ctx *ctx, |
184 | | char *dst) |
185 | 0 | { |
186 | 0 | unsigned done = 0; |
187 | 0 | unsigned bits = ctx->bits; |
188 | | |
189 | 0 | if (bits) |
190 | 0 | { |
191 | 0 | dst[done++] = ENCODE(ctx->alphabet, (ctx->word << (6 - ctx->bits))); |
192 | 0 | for (; bits < 6; bits += 2) |
193 | 0 | dst[done++] = '='; |
194 | |
|
195 | 0 | ctx->bits = 0; |
196 | 0 | } |
197 | |
|
198 | 0 | assert(done <= BASE64_ENCODE_FINAL_LENGTH); |
199 | 0 | return done; |
200 | 0 | } |