2 * Copyright (c) 1995-2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * 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.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 static const char base64_chars
[] =
49 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
55 for (p
= base64_chars
; *p
; p
++)
57 return p
- base64_chars
;
61 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
62 rk_base64_encode(const void *data
, int size
, char **str
)
67 const unsigned char *q
;
69 if (size
> INT_MAX
/4 || size
< 0) {
75 p
= s
= (char *) malloc(size
* 4 / 3 + 4);
80 q
= (const unsigned char *) data
;
82 for (i
= 0; i
< size
;) {
92 p
[0] = base64_chars
[(c
& 0x00fc0000) >> 18];
93 p
[1] = base64_chars
[(c
& 0x0003f000) >> 12];
94 p
[2] = base64_chars
[(c
& 0x00000fc0) >> 6];
95 p
[3] = base64_chars
[(c
& 0x0000003f) >> 0];
104 return (int) strlen(s
);
107 #define DECODE_ERROR 0xffffffff
110 token_decode(const char *token
)
113 unsigned int val
= 0;
115 if (strlen(token
) < 4)
117 for (i
= 0; i
< 4; i
++) {
124 val
+= pos(token
[i
]);
128 return (marker
<< 24) | val
;
131 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
132 rk_base64_decode(const char *str
, void *data
)
138 for (p
= str
; *p
&& (*p
== '=' || strchr(base64_chars
, *p
)); p
+= 4) {
139 unsigned int val
= token_decode(p
);
140 unsigned int marker
= (val
>> 24) & 0xff;
141 if (val
== DECODE_ERROR
) {
145 *q
++ = (val
>> 16) & 0xff;
147 *q
++ = (val
>> 8) & 0xff;
151 if (q
- (unsigned char *) data
> INT_MAX
) {
155 return q
- (unsigned char *) data
;
159 static int decode_flag
;
160 static int help_flag
;
163 * The short options are compatible with a subset of the FreeBSD contrib
164 * vis(1). Heimdal additions have long option names only.
166 static struct getargs args
[] = {
167 { "decode", 'd', arg_flag
, &decode_flag
, "Decode", NULL
},
168 { "help", 'h', arg_flag
, &help_flag
, "Print help message", NULL
},
170 static size_t num_args
= sizeof(args
)/sizeof(args
[0]);
173 main(int argc
, char **argv
)
175 unsigned char *buf
= NULL
;
181 setprogname("rkbase64");
182 if (getarg(args
, num_args
, argc
, argv
, &goptind
) || help_flag
) {
183 arg_printusage(args
, num_args
, NULL
, "FILE | -");
184 return help_flag
? 0 : 1;
191 return arg_printusage(args
, num_args
, NULL
, "FILE | -- -"), 0;
193 return arg_printusage(args
, num_args
, NULL
, "FILE | -- -"), 1;
195 if (strcmp(argv
[0], "-") == 0) {
197 unsigned char d
[4096];
200 while (!feof(stdin
) && !ferror(stdin
)) {
201 bytes
= fread(d
, 1, sizeof(d
), stdin
);
204 if (buflen
+ bytes
> bufsz
) {
205 if ((tmp
= realloc(buf
, bufsz
+ (bufsz
>> 2) + sizeof(d
))) == NULL
)
206 err(1, "Could not read stdin");
208 bufsz
= bufsz
+ (bufsz
>> 2) + sizeof(d
);
210 memcpy(buf
+ buflen
, d
, bytes
);
214 err(1, "Could not read stdin");
217 if ((errno
= rk_undumpdata(argv
[0], &d
, &bufsz
)))
218 err(1, "Could not read %s", argv
[0]);
226 if (buflen
== bufsz
) {
229 if ((tmp
= realloc(buf
, bufsz
+ 1)) == NULL
)
230 err(1, "Could not decode data");
236 if ((d
= malloc(buflen
* 3 / 4 + 4)) == NULL
)
237 err(1, "Could not decode data");
239 if ((ret
= rk_base64_decode((const char *)buf
, d
)) < 0)
240 err(1, "Could not decode data");
241 if (fwrite(d
, ret
, 1, stdout
) != 1)
242 err(1, "Could not write decoded data");
247 if ((ret
= rk_base64_encode(buf
, buflen
, &e
)) < 0)
248 err(1, "Could not encode data");
249 if (fwrite(e
, ret
, 1, stdout
) != 1)
250 err(1, "Could not write decoded data");
252 if (fwrite("\n", 1, 1, stdout
) != 1)
253 err(1, "Could not write decoded data");