usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / dhcpv6 / base64.c
blob7c9731d683a7874e31f0ce64dbfb8aabcbad658b
1 /* $KAME: base64.c,v 1.1 2004/06/08 07:26:56 jinmei Exp $ */
3 /*
4 * Copyright (C) 2004 WIDE Project.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
33 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
34 * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
36 * Permission to use, copy, modify, and distribute this software for any
37 * purpose with or without fee is hereby granted, provided that the above
38 * copyright notice and this permission notice appear in all copies.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
41 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
42 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
43 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
44 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
45 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
46 * PERFORMANCE OF THIS SOFTWARE.
49 #include <string.h>
51 typedef enum { FALSE = 0, TRUE = 1 } boolean_t;
53 static const char base64[] =
54 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
57 * State of a base64 decoding process in progress.
59 typedef struct {
60 int length; /* Desired length of binary data or -1 */
61 int digits; /* Number of buffered base64 digits */
62 boolean_t seen_end; /* True if "=" end marker seen */
63 int val[4];
65 char *dst; /* Head of the available space for resulting
66 * binary data */
67 char *dstend; /* End of the buffer */
68 } base64_decode_ctx_t;
70 static int
71 mem_tobuffer(base64_decode_ctx_t *ctx, void *base, unsigned int length)
73 if (ctx->dst + length >= ctx->dstend)
74 return (-1);
75 memcpy(ctx->dst, base, length);
76 ctx->dst += length;
77 return (0);
80 static inline void
81 base64_decode_init(base64_decode_ctx_t *ctx, int length,
82 char *result, size_t resultlen)
84 ctx->digits = 0;
85 ctx->seen_end = FALSE;
86 ctx->length = length;
87 ctx->dst = result;
88 ctx->dstend = result + resultlen;
91 static inline int
92 base64_decode_char(base64_decode_ctx_t *ctx, int c)
94 char *s;
96 if (ctx->seen_end == TRUE)
97 return (-1);
98 if ((s = strchr(base64, c)) == NULL)
99 return (-1);
100 ctx->val[ctx->digits++] = s - base64;
101 if (ctx->digits == 4) {
102 int n;
103 unsigned char buf[3];
104 if (ctx->val[0] == 64 || ctx->val[1] == 64)
105 return (-1);
106 if (ctx->val[2] == 64 && ctx->val[3] != 64)
107 return (-1);
109 * Check that bits that should be zero are.
111 if (ctx->val[2] == 64 && (ctx->val[1] & 0xf) != 0)
112 return (-1);
114 * We don't need to test for ctx->val[2] != 64 as
115 * the bottom two bits of 64 are zero.
117 if (ctx->val[3] == 64 && (ctx->val[2] & 0x3) != 0)
118 return (-1);
119 n = (ctx->val[2] == 64) ? 1 :
120 (ctx->val[3] == 64) ? 2 : 3;
121 if (n != 3) {
122 ctx->seen_end = TRUE;
123 if (ctx->val[2] == 64)
124 ctx->val[2] = 0;
125 if (ctx->val[3] == 64)
126 ctx->val[3] = 0;
128 buf[0] = (ctx->val[0]<<2)|(ctx->val[1]>>4);
129 buf[1] = (ctx->val[1]<<4)|(ctx->val[2]>>2);
130 buf[2] = (ctx->val[2]<<6)|(ctx->val[3]);
131 if (mem_tobuffer(ctx, buf, n))
132 return (-1);
133 if (ctx->length >= 0) {
134 if (n > ctx->length)
135 return (-1);
136 else
137 ctx->length -= n;
139 ctx->digits = 0;
141 return (0);
144 static inline int
145 base64_decode_finish(base64_decode_ctx_t *ctx)
147 if (ctx->length > 0)
148 return (-1);
149 if (ctx->digits != 0)
150 return (-1);
151 return (0);
155 base64_decodestring(const char *cstr, char *result, size_t resultlen)
157 base64_decode_ctx_t ctx;
159 base64_decode_init(&ctx, -1, result, resultlen);
160 for (;;) {
161 int c = *cstr++;
162 if (c == '\0')
163 break;
164 if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
165 continue;
166 if (base64_decode_char(&ctx, c))
167 return (-1);
169 if (base64_decode_finish(&ctx))
170 return (-1);
171 return (ctx.dst - result);