1 /* zb32.c - z-base-32 functions
2 * Copyright (C) 2014 Werner Koch
4 * This file is part of GnuPG.
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of either
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.
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.
19 * or both in parallel, as here.
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.
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/>.
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!. */
46 zb32_encode (const void *data, unsigned int databits)
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;
56 datalen = (databits + 7) / 8;
57 if (datalen > (1 << 16))
63 d = output = xtrymalloc (8 * (datalen / 5)
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)
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) ) ];
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) ];
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) ];
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) ];
109 *d++ = zb32asc[((s[0] ) >> 3) ];
110 *d++ = zb32asc[((s[0] & 7) << 2) ];
117 /* Need to strip some bytes if not a multiple of 40. */
118 output[(databits + 5 - 1) / 5] = 0;