chiark / gitweb /
gnupg2 (2.1.17-3) unstable; urgency=medium
[gnupg2.git] / common / zb32.c
1 /* zb32.c - z-base-32 functions
2  * Copyright (C) 2014  Werner Koch
3  *
4  * This file is part of GnuPG.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of either
8  *
9  *   - the GNU Lesser General Public License as published by the Free
10  *     Software Foundation; either version 3 of the License, or (at
11  *     your option) any later version.
12  *
13  * or
14  *
15  *   - the GNU General Public License as published by the Free
16  *     Software Foundation; either version 2 of the License, or (at
17  *     your option) any later version.
18  *
19  * or both in parallel, as here.
20  *
21  * This file is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, see <https://www.gnu.org/licenses/>.
28  */
29
30 #include <config.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <assert.h>
36
37 #include "util.h"
38 #include "zb32.h"
39
40 /* Zooko's base32 variant. See RFC-6189 and
41    http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt
42    Caller must xfree the returned string.  Returns NULL and sets ERRNO
43    on error.  To avoid integer overflow DATALEN is limited to 2^16
44    bytes.  Note, that DATABITS is measured in bits!.  */
45 char *
46 zb32_encode (const void *data, unsigned int databits)
47 {
48   static char const zb32asc[32] = {'y','b','n','d','r','f','g','8',
49                                    'e','j','k','m','c','p','q','x',
50                                    'o','t','1','u','w','i','s','z',
51                                    'a','3','4','5','h','7','6','9' };
52   const unsigned char *s;
53   char *output, *d;
54   size_t datalen;
55
56   datalen = (databits + 7) / 8;
57   if (datalen > (1 << 16))
58     {
59       errno = EINVAL;
60       return NULL;
61     }
62
63   d = output = xtrymalloc (8 * (datalen / 5)
64                            + 2 * (datalen % 5)
65                            - ((datalen%5)>2)
66                            + 1);
67   if (!output)
68     return NULL;
69
70   /* I use straightforward code.  The compiler should be able to do a
71      better job on optimization than me and it is easier to read.  */
72   for (s = data; datalen >= 5; s += 5, datalen -= 5)
73     {
74       *d++ = zb32asc[((s[0]      ) >> 3)               ];
75       *d++ = zb32asc[((s[0] &   7) << 2) | (s[1] >> 6) ];
76       *d++ = zb32asc[((s[1] &  63) >> 1)               ];
77       *d++ = zb32asc[((s[1] &   1) << 4) | (s[2] >> 4) ];
78       *d++ = zb32asc[((s[2] &  15) << 1) | (s[3] >> 7) ];
79       *d++ = zb32asc[((s[3] & 127) >> 2)               ];
80       *d++ = zb32asc[((s[3] &   3) << 3) | (s[4] >> 5) ];
81       *d++ = zb32asc[((s[4] &  31)     )               ];
82     }
83
84   switch (datalen)
85     {
86     case 4:
87       *d++ = zb32asc[((s[0]      ) >> 3)               ];
88       *d++ = zb32asc[((s[0] &   7) << 2) | (s[1] >> 6) ];
89       *d++ = zb32asc[((s[1] &  63) >> 1)               ];
90       *d++ = zb32asc[((s[1] &   1) << 4) | (s[2] >> 4) ];
91       *d++ = zb32asc[((s[2] &  15) << 1) | (s[3] >> 7) ];
92       *d++ = zb32asc[((s[3] & 127) >> 2)               ];
93       *d++ = zb32asc[((s[3] &   3) << 3)               ];
94       break;
95     case 3:
96       *d++ = zb32asc[((s[0]      ) >> 3)               ];
97       *d++ = zb32asc[((s[0] &   7) << 2) | (s[1] >> 6) ];
98       *d++ = zb32asc[((s[1] &  63) >> 1)               ];
99       *d++ = zb32asc[((s[1] &   1) << 4) | (s[2] >> 4) ];
100       *d++ = zb32asc[((s[2] &  15) << 1)               ];
101       break;
102     case 2:
103       *d++ = zb32asc[((s[0]      ) >> 3)               ];
104       *d++ = zb32asc[((s[0] &   7) << 2) | (s[1] >> 6) ];
105       *d++ = zb32asc[((s[1] &  63) >> 1)               ];
106       *d++ = zb32asc[((s[1] &   1) << 4)               ];
107       break;
108     case 1:
109       *d++ = zb32asc[((s[0]      ) >> 3)               ];
110       *d++ = zb32asc[((s[0] &   7) << 2)               ];
111       break;
112     default:
113       break;
114     }
115   *d = 0;
116
117   /* Need to strip some bytes if not a multiple of 40.  */
118   output[(databits + 5 - 1) / 5] = 0;
119   return output;
120 }