Line | Count | Source |
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 | 0 | { |
48 | 0 | static char const zb32asc[32] = {'y','b','n','d','r','f','g','8', |
49 | 0 | 'e','j','k','m','c','p','q','x', |
50 | 0 | 'o','t','1','u','w','i','s','z', |
51 | 0 | 'a','3','4','5','h','7','6','9' }; |
52 | 0 | const unsigned char *s; |
53 | 0 | char *output, *d; |
54 | 0 | size_t datalen; |
55 | |
|
56 | 0 | datalen = (databits + 7) / 8; |
57 | 0 | if (datalen > (1 << 16)) |
58 | 0 | { |
59 | 0 | errno = EINVAL; |
60 | 0 | return NULL; |
61 | 0 | } |
62 | | |
63 | 0 | d = output = xtrymalloc (8 * (datalen / 5) |
64 | 0 | + 2 * (datalen % 5) |
65 | 0 | - ((datalen%5)>2) |
66 | 0 | + 1); |
67 | 0 | 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 | 0 | for (s = data; datalen >= 5; s += 5, datalen -= 5) |
73 | 0 | { |
74 | 0 | *d++ = zb32asc[((s[0] ) >> 3) ]; |
75 | 0 | *d++ = zb32asc[((s[0] & 7) << 2) | (s[1] >> 6) ]; |
76 | 0 | *d++ = zb32asc[((s[1] & 63) >> 1) ]; |
77 | 0 | *d++ = zb32asc[((s[1] & 1) << 4) | (s[2] >> 4) ]; |
78 | 0 | *d++ = zb32asc[((s[2] & 15) << 1) | (s[3] >> 7) ]; |
79 | 0 | *d++ = zb32asc[((s[3] & 127) >> 2) ]; |
80 | 0 | *d++ = zb32asc[((s[3] & 3) << 3) | (s[4] >> 5) ]; |
81 | 0 | *d++ = zb32asc[((s[4] & 31) ) ]; |
82 | 0 | } |
83 | |
|
84 | 0 | switch (datalen) |
85 | 0 | { |
86 | 0 | case 4: |
87 | 0 | *d++ = zb32asc[((s[0] ) >> 3) ]; |
88 | 0 | *d++ = zb32asc[((s[0] & 7) << 2) | (s[1] >> 6) ]; |
89 | 0 | *d++ = zb32asc[((s[1] & 63) >> 1) ]; |
90 | 0 | *d++ = zb32asc[((s[1] & 1) << 4) | (s[2] >> 4) ]; |
91 | 0 | *d++ = zb32asc[((s[2] & 15) << 1) | (s[3] >> 7) ]; |
92 | 0 | *d++ = zb32asc[((s[3] & 127) >> 2) ]; |
93 | 0 | *d++ = zb32asc[((s[3] & 3) << 3) ]; |
94 | 0 | break; |
95 | 0 | case 3: |
96 | 0 | *d++ = zb32asc[((s[0] ) >> 3) ]; |
97 | 0 | *d++ = zb32asc[((s[0] & 7) << 2) | (s[1] >> 6) ]; |
98 | 0 | *d++ = zb32asc[((s[1] & 63) >> 1) ]; |
99 | 0 | *d++ = zb32asc[((s[1] & 1) << 4) | (s[2] >> 4) ]; |
100 | 0 | *d++ = zb32asc[((s[2] & 15) << 1) ]; |
101 | 0 | break; |
102 | 0 | case 2: |
103 | 0 | *d++ = zb32asc[((s[0] ) >> 3) ]; |
104 | 0 | *d++ = zb32asc[((s[0] & 7) << 2) | (s[1] >> 6) ]; |
105 | 0 | *d++ = zb32asc[((s[1] & 63) >> 1) ]; |
106 | 0 | *d++ = zb32asc[((s[1] & 1) << 4) ]; |
107 | 0 | break; |
108 | 0 | case 1: |
109 | 0 | *d++ = zb32asc[((s[0] ) >> 3) ]; |
110 | 0 | *d++ = zb32asc[((s[0] & 7) << 2) ]; |
111 | 0 | break; |
112 | 0 | default: |
113 | 0 | break; |
114 | 0 | } |
115 | 0 | *d = 0; |
116 | | |
117 | | /* Need to strip some bytes if not a multiple of 40. */ |
118 | 0 | output[(databits + 5 - 1) / 5] = 0; |
119 | 0 | return output; |
120 | 0 | } |