2 * Copyright (c) 2006-2009 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 static const char cb32[] =
28 "abcdefghijklmnopqrstuvwxyz012345";
29 static unsigned char rev32[128];
31 static int base32_decode(void *, size_t *, const char *, size_t);
32 static int base32_encode(char *, size_t *, const void *, size_t);
33 static int base32_handles_dots();
34 static int base32_blksize_raw();
35 static int base32_blksize_enc();
37 static struct encoder base32_encoder =
51 return &base32_encoder;
77 static int reverse_init = 0;
80 for (i = 0; i < 32; i++) {
97 base32_reverse_init();
102 base32_encode(char *buf, size_t *buflen, const void *data, size_t size)
110 memset(buf, 0, *buflen);
112 /* how many chars can we encode within the buf */
113 maxsize = BLKSIZE_RAW * (*buflen / BLKSIZE_ENC);
114 /* how big will the encoded data be */
115 newsize = BLKSIZE_ENC * (size / BLKSIZE_RAW);
116 if (size % BLKSIZE_RAW) {
117 newsize += BLKSIZE_ENC;
119 /* if the buffer is too small, eat some of the data */
120 if (*buflen < newsize) {
124 p = (unsigned char *) buf;
125 q = (unsigned char *)data;
127 for(i=0;i<size;i+=BLKSIZE_RAW) {
128 p[0] = cb32[((q[0] & 0xf8) >> 3)];
129 p[1] = cb32[(((q[0] & 0x07) << 2) | ((q[1] & 0xc0) >> 6))];
130 p[2] = (i+1 < size) ? cb32[((q[1] & 0x3e) >> 1)] : '\0';
131 p[3] = (i+1 < size) ? cb32[((q[1] & 0x01) << 4) | ((q[2] & 0xf0) >> 4)] : '\0';
132 p[4] = (i+2 < size) ? cb32[((q[2] & 0x0f) << 1) | ((q[3] & 0x80) >> 7)] : '\0';
133 p[5] = (i+3 < size) ? cb32[((q[3] & 0x7c) >> 2)] : '\0';
134 p[6] = (i+3 < size) ? cb32[((q[3] & 0x03) << 3) | ((q[4] & 0xe0) >> 5)] : '\0';
135 p[7] = (i+4 < size) ? cb32[((q[4] & 0x1f))] : '\0';
142 /* store number of bytes from data that was used */
148 #define DECODE_ERROR 0xffffffff
149 #define REV32(x) rev32[(int) (x)]
152 decode_token(const unsigned char *t, unsigned char *data, size_t len)
157 data[0] = ((REV32(t[0]) & 0x1f) << 3) |
158 ((REV32(t[1]) & 0x1c) >> 2);
163 data[1] = ((REV32(t[1]) & 0x03) << 6) |
164 ((REV32(t[2]) & 0x1f) << 1) |
165 ((REV32(t[3]) & 0x10) >> 4);
170 data[2] = ((REV32(t[3]) & 0x0f) << 4) |
171 ((REV32(t[4]) & 0x1e) >> 1);
176 data[3] = ((REV32(t[4]) & 0x01) << 7) |
177 ((REV32(t[5]) & 0x1f) << 2) |
178 ((REV32(t[6]) & 0x18) >> 3);
183 data[4] = ((REV32(t[6]) & 0x07) << 5) |
184 ((REV32(t[7]) & 0x1f));
190 base32_decode(void *buf, size_t *buflen, const char *str, size_t slen)
198 base32_reverse_init();
200 /* chars needed to decode slen */
201 newsize = BLKSIZE_RAW * (slen / BLKSIZE_ENC + 1) + 1;
202 /* encoded chars that fit in buf */
203 maxsize = BLKSIZE_ENC * (*buflen / BLKSIZE_RAW + 1) + 1;
204 /* if the buffer is too small, eat some of the data */
205 if (*buflen < newsize) {
210 for (p = str; *p && strchr(cb32, *p); p += BLKSIZE_ENC) {
211 len = decode_token((unsigned char *) p, (unsigned char *) q, slen);
215 if (len < BLKSIZE_RAW)
220 return q - (unsigned char *) buf;