5 * Base32 encoding and decoding.
7 * (c) 1997 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
39 /*----- Important tables --------------------------------------------------*/
41 static const char encodemap
[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" };
43 static const signed char decodemap
[] = {
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 2x */
47 -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, /* 3x */
48 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 4x */
49 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 5x */
50 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 6x */
51 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 7x */
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @base32_encode@ --- *
58 * Arguments: @base32_ctx *ctx@ = pointer to a context block
59 * @const void *p@ = pointer to a source buffer
60 * @size_t sz@ = size of the source buffer
61 * @dstr *d@ = pointer to destination string
65 * Use: Encodes a binary string in base32. To flush out the final
66 * few characters (if necessary), pass a null source pointer.
69 void base32_encode(base32_ctx
*ctx
,
70 const void *p
, size_t sz
,
74 unsigned long accl
= ctx
->accl
, acch
= ctx
->acch
;
75 unsigned qsz
= ctx
->qsz
;
76 const unsigned char *src
= p
;
79 acch
= (acch
<< 8) | ((accl
>> 24) & 0xff);
80 accl
= (accl
<< 8) | *src
++;
84 DPUTC(d
, encodemap
[(acch
>> 3) & 0x1f]);
85 DPUTC(d
, encodemap
[((acch
<< 2) & 0x1c) | ((accl
>> 30) & 0x03)]);
86 DPUTC(d
, encodemap
[(accl
>> 25) & 0x1f]);
87 DPUTC(d
, encodemap
[(accl
>> 20) & 0x1f]);
88 DPUTC(d
, encodemap
[(accl
>> 15) & 0x1f]);
89 DPUTC(d
, encodemap
[(accl
>> 10) & 0x1f]);
90 DPUTC(d
, encodemap
[(accl
>> 5) & 0x1f]);
91 DPUTC(d
, encodemap
[(accl
>> 0) & 0x1f]);
93 if (ctx
->maxline
&& ctx
->lnlen
>= ctx
->maxline
) {
94 dstr_puts(d
, ctx
->indent
);
102 ctx
->acch
= acch
; ctx
->accl
= accl
;
105 unsigned long accl
= ctx
->accl
, acch
= ctx
->acch
;
106 unsigned qsz
= ctx
->qsz
;
109 unsigned pad
= 5 - qsz
;
111 acch
= accl
<< (pad
* 8 - 32);
114 acch
= (acch
<< (8 * pad
)) | ((accl
& 0xffffffff) >> (32 - 8 * pad
));
115 accl
= accl
<< (8 * pad
);
120 DPUTC(d
, encodemap
[(acch
>> 3) & 0x1f]);
121 acch
= (acch
<< 5) | ((accl
>> 27) & 0x1f);
132 ctx
->acch
= ctx
->accl
= 0;
136 /* --- @base32_decode@ --- *
138 * Arguments: @base32_ctx *ctx@ = pointer to a context block
139 * @const void *p@ = pointer to a source buffer
140 * @size_t sz@ = size of the source buffer
141 * @dstr *d@ = pointer to destination string
145 * Use: Decodes a binary string in base32. To flush out the final
146 * few characters (if necessary), pass a null source pointer.
149 void base32_decode(base32_ctx
*ctx
,
150 const void *p
, size_t sz
,
154 unsigned long accl
= ctx
->accl
, acch
= ctx
->acch
;
155 unsigned qsz
= ctx
->qsz
;
161 /* --- Get the next character and convert it --- */
164 if (ch
>= 128 || ch
< 0)
172 /* --- Bung it in the accumulator --- */
174 acch
= (acch
<< 5) | ((accl
>> 27) & 0x1f);
175 accl
= (accl
<< 5) | ch
;
178 /* --- Maybe write out a completed triplet --- */
181 DPUTC(d
, (acch
>> 0) & 0xff);
182 DPUTC(d
, (accl
>> 24) & 0xff);
183 DPUTC(d
, (accl
>> 16) & 0xff);
184 DPUTC(d
, (accl
>> 8) & 0xff);
185 DPUTC(d
, (accl
>> 0) & 0xff);
191 ctx
->acch
= acch
; ctx
->accl
= accl
;
195 /* --- Notes about the tail-end bits --- *
197 * I'll use the queue size to work out how many tail-end bytes I ought to
198 * write. This isn't strictly right, but it's easier.
201 unsigned long acch
= ctx
->acch
, accl
= ctx
->accl
;
202 unsigned qsz
= ctx
->qsz
;
204 /* --- Now fiddle with everything else --- *
206 * There's a bodge here for invalid encodings which have a funny number
207 * of quintets in the final group. I'm not sure this is really worth
208 * having, but it might save some unexpected behaviour. (Not that you
209 * won't still get unexpected behaviour if the stream is completely
214 unsigned pad
= 8 - qsz
;
216 acch
= accl
<< (5 * pad
- 32);
219 acch
= (acch
<< (5 * pad
)) | ((accl
& 0xffffffff) >> (32 - 5 * pad
));
220 accl
= accl
<< (5 * pad
);
225 DPUTC(d
, acch
& 0xff);
232 /* --- That seems to be good enough --- */
235 ctx
->acch
= ctx
->accl
= 0;
239 /* --- @base32_init@ --- *
241 * Arguments: @base32_ctx *ctx@ = pointer to context block to initialize
245 * Use: Initializes a base32 context properly.
248 void base32_init(base32_ctx
*ctx
)
250 ctx
->accl
= ctx
->acch
= 0;
257 /*----- Test driver code --------------------------------------------------*/
261 int main(int argc
, char *argv
[])
263 unsigned char buf
[BUFSIZ
];
266 void (*proc
)(base32_ctx
*, const void *, size_t, dstr
*);
271 if (argc
> 1 && strcmp(argv
[1], "-d") == 0)
272 proc
= base32_decode
;
274 proc
= base32_encode
;
281 sz
= fread(buf
, 1, sizeof(buf
), stdin
);
283 proc(&ctx
, buf
, sz
, &d
);
284 dstr_write(&d
, stdout
);
287 } while (sz
== sizeof(buf
));
289 proc(&ctx
, 0, 0, &d
);
290 dstr_write(&d
, stdout
);
292 if (proc
== base32_encode
)
300 /*----- That's all, folks -------------------------------------------------*/