chiark / gitweb /
str: New str_matchx function optionally reports possible prefix.
[mLib] / base32.c
1 /* -*-c-*-
2  *
3  * $Id$
4  *
5  * Base32 encoding and decoding.
6  *
7  * (c) 1997 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "base32.h"
37 #include "dstr.h"
38
39 /*----- Important tables --------------------------------------------------*/
40
41 static const char encodemap[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" };
42
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 */
52 };  
53
54 /*----- Main code ---------------------------------------------------------*/
55
56 /* --- @base32_encode@ --- *
57  *
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
62  *
63  * Returns:     ---
64  *
65  * Use:         Encodes a binary string in base32.  To flush out the final
66  *              few characters (if necessary), pass a null source pointer.
67  */
68
69 void base32_encode(base32_ctx *ctx,
70                    const void *p, size_t sz,
71                    dstr *d)
72 {
73   if (p) {
74     unsigned long accl = ctx->accl, acch = ctx->acch;
75     unsigned qsz = ctx->qsz;
76     const unsigned char *src = p;
77
78     while (sz) {
79       acch = (acch << 8) | ((accl >> 24) & 0xff);
80       accl = (accl << 8) | *src++;
81       qsz++;
82       sz--;
83       if (qsz == 5) {
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]);
92         ctx->lnlen += 8;
93         if (ctx->maxline && ctx->lnlen >= ctx->maxline) {
94           dstr_puts(d, ctx->indent);
95           ctx->lnlen = 0;
96         }
97         qsz = 0;
98         accl = acch = 0;
99       }
100     }
101
102     ctx->acch = acch; ctx->accl = accl;
103     ctx->qsz = qsz;
104   } else {
105     unsigned long accl = ctx->accl, acch = ctx->acch;
106     unsigned qsz = ctx->qsz;
107
108     if (qsz) {
109       unsigned pad = 5 - qsz;
110       if (pad > 3) {
111         acch = accl << (pad * 8 - 32);
112         accl = 0;
113       } else {
114         acch = (acch << (8 * pad)) | ((accl & 0xffffffff) >> (32 - 8 * pad));
115         accl = accl << (8 * pad);
116       }
117       qsz = 40;
118       pad *= 8;
119       while (qsz > pad) {
120         DPUTC(d, encodemap[(acch >> 3) & 0x1f]);
121         acch = (acch << 5) | ((accl >> 27) & 0x1f);
122         accl = (accl << 5);
123         qsz -= 5;
124       }
125       while (qsz) {
126         DPUTC(d, '=');
127         qsz -= 5;
128       }
129       ctx->lnlen += 8;
130     }
131     ctx->qsz = 0;
132     ctx->acch = ctx->accl = 0;
133   }
134 }
135
136 /* --- @base32_decode@ --- *
137  *
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
142  *
143  * Returns:     ---
144  *
145  * Use:         Decodes a binary string in base32.  To flush out the final
146  *              few characters (if necessary), pass a null source pointer.
147  */
148
149 void base32_decode(base32_ctx *ctx,
150                    const void *p, size_t sz,
151                    dstr *d)
152 {
153   if (p) {
154     unsigned long accl = ctx->accl, acch = ctx->acch;
155     unsigned qsz = ctx->qsz;
156     const char *src = p;
157     int ch;
158
159     while (sz) {
160
161       /* --- Get the next character and convert it --- */
162
163       ch = *src++;
164       if (ch >= 128 || ch < 0)
165         ch = -1;
166       else
167         ch = decodemap[ch];
168       sz--;
169       if (ch == -1)
170         continue;
171
172       /* --- Bung it in the accumulator --- */
173
174       acch = (acch << 5) | ((accl >> 27) & 0x1f);
175       accl = (accl << 5) | ch;
176       qsz++;
177
178       /* --- Maybe write out a completed triplet --- */
179
180       if (qsz == 8) {
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);
186         acch = accl = 0;
187         qsz = 0;
188       }
189     }
190
191     ctx->acch = acch; ctx->accl = accl;
192     ctx->qsz = qsz;
193   } else {
194
195     /* --- Notes about the tail-end bits --- *
196      *
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.
199      */
200
201     unsigned long acch = ctx->acch, accl = ctx->accl;
202     unsigned qsz = ctx->qsz;
203
204     /* --- Now fiddle with everything else --- *
205      *
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
210      * empty, of course.)
211      */
212
213     if (qsz) {
214       unsigned pad = 8 - qsz;
215       if (pad > 6) {
216         acch = accl << (5 * pad - 32);
217         accl = 0;
218       } else {
219         acch = (acch << (5 * pad)) | ((accl & 0xffffffff) >> (32 - 5 * pad));
220         accl = accl << (5 * pad);
221       }
222
223       qsz *= 5;
224       while (qsz >= 8) {
225         DPUTC(d, acch & 0xff);
226         acch = accl >> 24;
227         accl <<= 8;
228         qsz -= 8;
229       }
230     }
231
232     /* --- That seems to be good enough --- */
233
234     ctx->qsz = 0;
235     ctx->acch = ctx->accl = 0;
236   }
237 }
238
239 /* --- @base32_init@ --- *
240  *
241  * Arguments:   @base32_ctx *ctx@ = pointer to context block to initialize
242  *
243  * Returns:     ---
244  *
245  * Use:         Initializes a base32 context properly.
246  */
247
248 void base32_init(base32_ctx *ctx)
249 {
250   ctx->accl = ctx->acch = 0;
251   ctx->qsz = 0;
252   ctx->lnlen = 0;
253   ctx->indent = "\n";
254   ctx->maxline = 72;
255 }
256
257 /*----- Test driver code --------------------------------------------------*/
258
259 #ifdef TEST_RIG
260
261 int main(int argc, char *argv[])
262 {
263   unsigned char buf[BUFSIZ];
264   dstr d = DSTR_INIT;
265   base32_ctx ctx;
266   void (*proc)(base32_ctx *, const void *, size_t, dstr *);
267   size_t sz;
268
269   base32_init(&ctx);
270
271   if (argc > 1 && strcmp(argv[1], "-d") == 0)
272     proc = base32_decode;
273   else {
274     proc = base32_encode;
275     putchar('\t');
276     ctx.indent = "\n\t";
277     ctx.maxline = 32;
278   }
279
280   do {
281     sz = fread(buf, 1, sizeof(buf), stdin);
282     if (sz) {
283       proc(&ctx, buf, sz, &d);
284       dstr_write(&d, stdout);
285       dstr_destroy(&d);
286     }
287   } while (sz == sizeof(buf));
288
289   proc(&ctx, 0, 0, &d);
290   dstr_write(&d, stdout);
291
292   if (proc == base32_encode)
293     putchar('\n');
294
295   return (0);
296 }
297
298 #endif
299
300 /*----- That's all, folks -------------------------------------------------*/