1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/* Self tests for base32.
Copyright (C) 2004, 2008-2018 Free Software Foundation, Inc.
Based on the tests for base64 written by Simon Josefsson.
Adapted for base32 by Gijs van Tulder.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include "base32.h"
#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "macros.h"
int
main (void)
{
const char *in = "abcdefghijklmnop";
const char *b32in = "MFRGGZDFMZTWQ2LKNNWG23TPOA======";
char out[255];
size_t len;
bool ok;
char *p;
memset (out, 0x42, sizeof (out));
base32_encode (in, 0, out, 0);
ASSERT (out[0] == '\x42');
memset (out, 0x42, sizeof (out));
base32_encode (in, 1, out, 10);
ASSERT (memcmp (out, "ME======", 1) == 0);
memset (out, 0x42, sizeof (out));
base32_encode (in, 1, out, 2);
ASSERT (memcmp (out, "ME======", 2) == 0);
memset (out, 0x42, sizeof (out));
base32_encode (in, 1, out, 3);
ASSERT (memcmp (out, "ME======", 3) == 0);
memset (out, 0x42, sizeof (out));
base32_encode (in, 1, out, 4);
ASSERT (memcmp (out, "ME======", 4) == 0);
memset (out, 0x42, sizeof (out));
base32_encode (in, 1, out, 8);
ASSERT (memcmp (out, "ME======", 8) == 0);
memset (out, 0x42, sizeof (out));
base32_encode (in, 2, out, 8);
ASSERT (memcmp (out, "MFRA====", 8) == 0);
memset (out, 0x42, sizeof (out));
base32_encode (in, 3, out, 8);
ASSERT (memcmp (out, "MFRGG===", 8) == 0);
memset (out, 0x42, sizeof (out));
base32_encode (in, 4, out, 8);
ASSERT (memcmp (out, "MFRGGZA=", 8) == 0);
memset (out, 0x42, sizeof (out));
base32_encode (in, 5, out, 8);
ASSERT (memcmp (out, "MFRGGZDF", 8) == 0);
memset (out, 0x42, sizeof (out));
base32_encode (in, 6, out, 16);
ASSERT (memcmp (out, "MFRGGZDFMY======", 16) == 0);
memset (out, 0x42, sizeof (out));
base32_encode (in, 6, out, 100);
ASSERT (memcmp (out, "MFRGGZDFMY======", 16) == 0);
/* Decode. */
memset (out, 0x42, sizeof (out));
len = 0;
ok = base32_decode (b32in, 8, out, &len);
ASSERT (ok);
ASSERT (len == 0);
memset (out, 0x42, sizeof (out));
len = 1;
ok = base32_decode (b32in, 8, out, &len);
ASSERT (ok);
ASSERT (len == 1);
ASSERT (memcmp (out, "abcdefghijklmnop", 1) == 0);
memset (out, 0x42, sizeof (out));
len = 2;
ok = base32_decode (b32in, 8, out, &len);
ASSERT (ok);
ASSERT (len == 2);
ASSERT (memcmp (out, "abcdefghijklmnop", 2) == 0);
memset (out, 0x42, sizeof (out));
len = 3;
ok = base32_decode (b32in, 8, out, &len);
ASSERT (ok);
ASSERT (len == 3);
ASSERT (memcmp (out, "abcdefghijklmnop", 3) == 0);
memset (out, 0x42, sizeof (out));
len = 4;
ok = base32_decode (b32in, 8, out, &len);
ASSERT (ok);
ASSERT (len == 4);
ASSERT (memcmp (out, "abcdefghijklmnop", 4) == 0);
memset (out, 0x42, sizeof (out));
len = 5;
ok = base32_decode (b32in, 8, out, &len);
ASSERT (ok);
ASSERT (len == 5);
ASSERT (memcmp (out, "abcdefghijklmnop", 5) == 0);
memset (out, 0x42, sizeof (out));
len = 6;
ok = base32_decode (b32in, 8, out, &len);
ASSERT (ok);
ASSERT (len == 5);
ASSERT (memcmp (out, "abcdefghijklmnop", 5) == 0);
memset (out, 0x42, sizeof (out));
len = 100;
ok = base32_decode (b32in, strlen (b32in), out, &len);
ASSERT (ok);
ASSERT (len == 16);
ASSERT (memcmp (out, "abcdefghijklmnop", 16) == 0);
/* Allocating encode */
len = base32_encode_alloc (in, strlen (in), &p);
ASSERT (len == 32);
ASSERT (strcmp (p, "MFRGGZDFMZTWQ2LKNNWG23TPOA======") == 0);
free (p);
len = base32_encode_alloc (in, SIZE_MAX - 5, &p);
ASSERT (len == 0);
/* Decode context function */
{
struct base32_decode_context ctx;
base32_decode_ctx_init (&ctx);
len = sizeof (out);
ok = base32_decode_ctx (&ctx, b32in, strlen (b32in), out, &len);
ASSERT (ok);
ASSERT (len == 16);
ASSERT (memcmp (out, "abcdefghijklmnop", len) == 0);
}
/* Allocating decode context function */
ok = base32_decode_alloc_ctx (NULL, b32in, strlen (b32in), &p, &len);
ASSERT (ok);
ASSERT (len == 16);
ASSERT (memcmp (out, "abcdefghijklmnop", len) == 0);
free (p);
{
struct base32_decode_context ctx;
const char *newlineb32 = "MFRG\nGZDFMZTWQ2LKNNW\nG23TPOA======";
base32_decode_ctx_init (&ctx);
ok = base32_decode_alloc_ctx (&ctx, newlineb32, strlen (newlineb32), &p, &len);
ASSERT (ok);
ASSERT (len == strlen (in));
ASSERT (memcmp (p, in, len) == 0);
free (p);
}
{
struct base32_decode_context ctx;
base32_decode_ctx_init (&ctx);
ok = base32_decode_alloc_ctx (&ctx, "MFRGGZDFM\nZTWQ2LK", 17, &p, &len);
ASSERT (ok);
ASSERT (len == 10);
ASSERT (memcmp (p, "abcdefghij", len) == 0);
free (p);
base32_decode_ctx_init (&ctx);
ok = base32_decode_alloc_ctx (&ctx, "MF\n", 3, &p, &len);
ASSERT (ok);
ASSERT (len == 0);
free (p);
ok = base32_decode_alloc_ctx (&ctx, "RGGZDFMZ", 8, &p, &len);
ASSERT (ok);
ASSERT (len == 5);
ASSERT (memcmp (p, "abcde", len) == 0);
free (p);
ok = base32_decode_alloc_ctx (&ctx, "TWQ2LK", 6, &p, &len);
ASSERT (ok);
ASSERT (len == 5);
ASSERT (memcmp (p, "fghij", len) == 0);
free (p);
ok = base32_decode_alloc_ctx (&ctx, "", 0, &p, &len);
ASSERT (ok);
free (p);
}
{
struct base32_decode_context ctx;
const char *newlineb32 = "\n\n\n\n\n";
base32_decode_ctx_init (&ctx);
ok = base32_decode_alloc_ctx (&ctx, newlineb32, strlen (newlineb32), &p, &len);
ASSERT (ok);
ASSERT (len == 0);
free (p);
}
ok = base32_decode_alloc_ctx (NULL, " ! ", 3, &p, &len);
ASSERT (!ok);
ok = base32_decode_alloc_ctx (NULL, "ABC\nDEF", 7, &p, &len);
ASSERT (!ok);
ok = base32_decode_alloc_ctx (NULL, "AA", 2, &p, &len);
ASSERT (!ok);
ok = base32_decode_alloc_ctx (NULL, "AA=", 3, &p, &len);
ASSERT (!ok);
ok = base32_decode_alloc_ctx (NULL, "AABBAAxx", 8, &p, &len);
ASSERT (!ok);
ok = base32_decode_alloc_ctx (NULL, "AABBAA=X", 8, &p, &len);
ASSERT (!ok);
ok = base32_decode_alloc_ctx (NULL, "AABBAA=X", 8, &p, &len);
ASSERT (!ok);
ok = base32_decode_alloc_ctx (NULL, "AABBAA=A", 8, &p, &len);
ASSERT (!ok);
return 0;
}
|