1 /* $KAME: base64.c,v 1.1 2004/06/08 07:26:56 jinmei Exp $ */
4 * Copyright (C) 2004 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
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 */
65 char *dst
; /* Head of the available space for resulting
67 char *dstend
; /* End of the buffer */
68 } base64_decode_ctx_t
;
71 mem_tobuffer(base64_decode_ctx_t
*ctx
, void *base
, unsigned int length
)
73 if (ctx
->dst
+ length
>= ctx
->dstend
)
75 memcpy(ctx
->dst
, base
, length
);
81 base64_decode_init(base64_decode_ctx_t
*ctx
, int length
,
82 char *result
, size_t resultlen
)
85 ctx
->seen_end
= FALSE
;
88 ctx
->dstend
= result
+ resultlen
;
92 base64_decode_char(base64_decode_ctx_t
*ctx
, int c
)
96 if (ctx
->seen_end
== TRUE
)
98 if ((s
= strchr(base64
, c
)) == NULL
)
100 ctx
->val
[ctx
->digits
++] = s
- base64
;
101 if (ctx
->digits
== 4) {
103 unsigned char buf
[3];
104 if (ctx
->val
[0] == 64 || ctx
->val
[1] == 64)
106 if (ctx
->val
[2] == 64 && ctx
->val
[3] != 64)
109 * Check that bits that should be zero are.
111 if (ctx
->val
[2] == 64 && (ctx
->val
[1] & 0xf) != 0)
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)
119 n
= (ctx
->val
[2] == 64) ? 1 :
120 (ctx
->val
[3] == 64) ? 2 : 3;
122 ctx
->seen_end
= TRUE
;
123 if (ctx
->val
[2] == 64)
125 if (ctx
->val
[3] == 64)
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
))
133 if (ctx
->length
>= 0) {
145 base64_decode_finish(base64_decode_ctx_t
*ctx
)
149 if (ctx
->digits
!= 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
);
164 if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
166 if (base64_decode_char(&ctx
, c
))
169 if (base64_decode_finish(&ctx
))
171 return (ctx
.dst
- result
);