Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / lib / isc / base64.c
blobaad7d8522af40f880941caf34a305d1b6020863b
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: base64.c,v 1.32 2007/06/19 23:47:17 tbox Exp */
22 /*! \file */
24 #include <config.h>
26 #include <isc/base64.h>
27 #include <isc/buffer.h>
28 #include <isc/lex.h>
29 #include <isc/string.h>
30 #include <isc/util.h>
32 #define RETERR(x) do { \
33 isc_result_t _r = (x); \
34 if (_r != ISC_R_SUCCESS) \
35 return (_r); \
36 } while (0)
39 /*@{*/
40 /*!
41 * These static functions are also present in lib/dns/rdata.c. I'm not
42 * sure where they should go. -- bwelling
44 static isc_result_t
45 str_totext(const char *source, isc_buffer_t *target);
47 static isc_result_t
48 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
50 static const char base64[] =
51 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
52 /*@}*/
54 isc_result_t
55 isc_base64_totext(isc_region_t *source, int wordlength,
56 const char *wordbreak, isc_buffer_t *target)
58 char buf[5];
59 unsigned int loops = 0;
61 if (wordlength < 4)
62 wordlength = 4;
64 memset(buf, 0, sizeof(buf));
65 while (source->length > 2) {
66 buf[0] = base64[(source->base[0]>>2)&0x3f];
67 buf[1] = base64[((source->base[0]<<4)&0x30)|
68 ((source->base[1]>>4)&0x0f)];
69 buf[2] = base64[((source->base[1]<<2)&0x3c)|
70 ((source->base[2]>>6)&0x03)];
71 buf[3] = base64[source->base[2]&0x3f];
72 RETERR(str_totext(buf, target));
73 isc_region_consume(source, 3);
75 loops++;
76 if (source->length != 0 &&
77 (int)((loops + 1) * 4) >= wordlength)
79 loops = 0;
80 RETERR(str_totext(wordbreak, target));
83 if (source->length == 2) {
84 buf[0] = base64[(source->base[0]>>2)&0x3f];
85 buf[1] = base64[((source->base[0]<<4)&0x30)|
86 ((source->base[1]>>4)&0x0f)];
87 buf[2] = base64[((source->base[1]<<2)&0x3c)];
88 buf[3] = '=';
89 RETERR(str_totext(buf, target));
90 } else if (source->length == 1) {
91 buf[0] = base64[(source->base[0]>>2)&0x3f];
92 buf[1] = base64[((source->base[0]<<4)&0x30)];
93 buf[2] = buf[3] = '=';
94 RETERR(str_totext(buf, target));
96 return (ISC_R_SUCCESS);
99 /*%
100 * State of a base64 decoding process in progress.
102 typedef struct {
103 int length; /*%< Desired length of binary data or -1 */
104 isc_buffer_t *target; /*%< Buffer for resulting binary data */
105 int digits; /*%< Number of buffered base64 digits */
106 isc_boolean_t seen_end; /*%< True if "=" end marker seen */
107 int val[4];
108 } base64_decode_ctx_t;
110 static inline void
111 base64_decode_init(base64_decode_ctx_t *ctx, int length, isc_buffer_t *target)
113 ctx->digits = 0;
114 ctx->seen_end = ISC_FALSE;
115 ctx->length = length;
116 ctx->target = target;
119 static inline isc_result_t
120 base64_decode_char(base64_decode_ctx_t *ctx, int c) {
121 char *s;
123 if (ctx->seen_end)
124 return (ISC_R_BADBASE64);
125 if ((s = strchr(base64, c)) == NULL)
126 return (ISC_R_BADBASE64);
127 ctx->val[ctx->digits++] = s - base64;
128 if (ctx->digits == 4) {
129 int n;
130 unsigned char buf[3];
131 if (ctx->val[0] == 64 || ctx->val[1] == 64)
132 return (ISC_R_BADBASE64);
133 if (ctx->val[2] == 64 && ctx->val[3] != 64)
134 return (ISC_R_BADBASE64);
136 * Check that bits that should be zero are.
138 if (ctx->val[2] == 64 && (ctx->val[1] & 0xf) != 0)
139 return (ISC_R_BADBASE64);
141 * We don't need to test for ctx->val[2] != 64 as
142 * the bottom two bits of 64 are zero.
144 if (ctx->val[3] == 64 && (ctx->val[2] & 0x3) != 0)
145 return (ISC_R_BADBASE64);
146 n = (ctx->val[2] == 64) ? 1 :
147 (ctx->val[3] == 64) ? 2 : 3;
148 if (n != 3) {
149 ctx->seen_end = ISC_TRUE;
150 if (ctx->val[2] == 64)
151 ctx->val[2] = 0;
152 if (ctx->val[3] == 64)
153 ctx->val[3] = 0;
155 buf[0] = (ctx->val[0]<<2)|(ctx->val[1]>>4);
156 buf[1] = (ctx->val[1]<<4)|(ctx->val[2]>>2);
157 buf[2] = (ctx->val[2]<<6)|(ctx->val[3]);
158 RETERR(mem_tobuffer(ctx->target, buf, n));
159 if (ctx->length >= 0) {
160 if (n > ctx->length)
161 return (ISC_R_BADBASE64);
162 else
163 ctx->length -= n;
165 ctx->digits = 0;
167 return (ISC_R_SUCCESS);
170 static inline isc_result_t
171 base64_decode_finish(base64_decode_ctx_t *ctx) {
172 if (ctx->length > 0)
173 return (ISC_R_UNEXPECTEDEND);
174 if (ctx->digits != 0)
175 return (ISC_R_BADBASE64);
176 return (ISC_R_SUCCESS);
179 isc_result_t
180 isc_base64_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
181 base64_decode_ctx_t ctx;
182 isc_textregion_t *tr;
183 isc_token_t token;
184 isc_boolean_t eol;
186 base64_decode_init(&ctx, length, target);
188 while (!ctx.seen_end && (ctx.length != 0)) {
189 unsigned int i;
191 if (length > 0)
192 eol = ISC_FALSE;
193 else
194 eol = ISC_TRUE;
195 RETERR(isc_lex_getmastertoken(lexer, &token,
196 isc_tokentype_string, eol));
197 if (token.type != isc_tokentype_string)
198 break;
199 tr = &token.value.as_textregion;
200 for (i = 0; i < tr->length; i++)
201 RETERR(base64_decode_char(&ctx, tr->base[i]));
203 if (ctx.length < 0 && !ctx.seen_end)
204 isc_lex_ungettoken(lexer, &token);
205 RETERR(base64_decode_finish(&ctx));
206 return (ISC_R_SUCCESS);
209 isc_result_t
210 isc_base64_decodestring(const char *cstr, isc_buffer_t *target) {
211 base64_decode_ctx_t ctx;
213 base64_decode_init(&ctx, -1, target);
214 for (;;) {
215 int c = *cstr++;
216 if (c == '\0')
217 break;
218 if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
219 continue;
220 RETERR(base64_decode_char(&ctx, c));
222 RETERR(base64_decode_finish(&ctx));
223 return (ISC_R_SUCCESS);
226 static isc_result_t
227 str_totext(const char *source, isc_buffer_t *target) {
228 unsigned int l;
229 isc_region_t region;
231 isc_buffer_availableregion(target, &region);
232 l = strlen(source);
234 if (l > region.length)
235 return (ISC_R_NOSPACE);
237 memcpy(region.base, source, l);
238 isc_buffer_add(target, l);
239 return (ISC_R_SUCCESS);
242 static isc_result_t
243 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
244 isc_region_t tr;
246 isc_buffer_availableregion(target, &tr);
247 if (length > tr.length)
248 return (ISC_R_NOSPACE);
249 memcpy(tr.base, base, length);
250 isc_buffer_add(target, length);
251 return (ISC_R_SUCCESS);