Line data Source code
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 <http://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 :
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 19 : 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 19 : datalen = (databits + 7) / 8;
57 19 : if (datalen > (1 << 16))
58 : {
59 0 : errno = EINVAL;
60 0 : return NULL;
61 : }
62 :
63 19 : d = output = xtrymalloc (8 * (datalen / 5)
64 : + 2 * (datalen % 5)
65 : - ((datalen%5)>2)
66 : + 1);
67 19 : if (!output)
68 0 : 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 27 : for (s = data; datalen >= 5; s += 5, datalen -= 5)
73 : {
74 8 : *d++ = zb32asc[((s[0] ) >> 3) ];
75 8 : *d++ = zb32asc[((s[0] & 7) << 2) | (s[1] >> 6) ];
76 8 : *d++ = zb32asc[((s[1] & 63) >> 1) ];
77 8 : *d++ = zb32asc[((s[1] & 1) << 4) | (s[2] >> 4) ];
78 8 : *d++ = zb32asc[((s[2] & 15) << 1) | (s[3] >> 7) ];
79 8 : *d++ = zb32asc[((s[3] & 127) >> 2) ];
80 8 : *d++ = zb32asc[((s[3] & 3) << 3) | (s[4] >> 5) ];
81 8 : *d++ = zb32asc[((s[4] & 31) ) ];
82 : }
83 :
84 19 : switch (datalen)
85 : {
86 : case 4:
87 1 : *d++ = zb32asc[((s[0] ) >> 3) ];
88 1 : *d++ = zb32asc[((s[0] & 7) << 2) | (s[1] >> 6) ];
89 1 : *d++ = zb32asc[((s[1] & 63) >> 1) ];
90 1 : *d++ = zb32asc[((s[1] & 1) << 4) | (s[2] >> 4) ];
91 1 : *d++ = zb32asc[((s[2] & 15) << 1) | (s[3] >> 7) ];
92 1 : *d++ = zb32asc[((s[3] & 127) >> 2) ];
93 1 : *d++ = zb32asc[((s[3] & 3) << 3) ];
94 1 : break;
95 : case 3:
96 5 : *d++ = zb32asc[((s[0] ) >> 3) ];
97 5 : *d++ = zb32asc[((s[0] & 7) << 2) | (s[1] >> 6) ];
98 5 : *d++ = zb32asc[((s[1] & 63) >> 1) ];
99 5 : *d++ = zb32asc[((s[1] & 1) << 4) | (s[2] >> 4) ];
100 5 : *d++ = zb32asc[((s[2] & 15) << 1) ];
101 5 : break;
102 : case 2:
103 5 : *d++ = zb32asc[((s[0] ) >> 3) ];
104 5 : *d++ = zb32asc[((s[0] & 7) << 2) | (s[1] >> 6) ];
105 5 : *d++ = zb32asc[((s[1] & 63) >> 1) ];
106 5 : *d++ = zb32asc[((s[1] & 1) << 4) ];
107 5 : break;
108 : case 1:
109 4 : *d++ = zb32asc[((s[0] ) >> 3) ];
110 4 : *d++ = zb32asc[((s[0] & 7) << 2) ];
111 4 : break;
112 : default:
113 4 : break;
114 : }
115 19 : *d = 0;
116 :
117 : /* Need to strip some bytes if not a multiple of 40. */
118 19 : output[(databits + 5 - 1) / 5] = 0;
119 19 : return output;
120 : }
|