-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_ascii85.c
486 lines (394 loc) · 18.3 KB
/
test_ascii85.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/** @file test_ascii85.c
*
* @brief Ascii85 encoder and decoder tester
*
* @par
* @copyright Copyright © 2017 Doug Currie, Londonderry, NH, USA. All rights reserved.
*
* @par
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/
#include "ascii85.h"
#include "lcut/lcut.h"
#include "lcut/xorshift_e.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <time.h>
// Some tests adapted from https://github.com/judsonx/base85 -- thanks Judson Weissert!
// Wikipedia Example for Ascii85
// A quote from Thomas Hobbes's Leviathan:
//
// Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.
//
// If this is initially encoded using US-ASCII, it can be reencoded in Ascii85 as follows:
//
// <~9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,
// O<DJ+*.@<*K0@<6L(Df-\0Ec5e;DffZ(EZee.Bl.9pF"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY
// i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa
// l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G
// >uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c~>
typedef struct tv_pair_s
{
const uint8_t *in;
const uint8_t *out;
} tv_pair_t;
tv_pair_t tp0 =
{
.in = (const uint8_t *)
"Man is distinguished, not only by his reason, but by this singular passion from other animals, "
"which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable "
"generation of knowledge, exceeds the short vehemence of any carnal pleasure.",
.out = (const uint8_t *)
"9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,"
"O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY"
"i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa"
"l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G"
">uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c"
};
static void tc_encode_decode (lcut_tc_t *tc, void *data)
{
tv_pair_t *tv = (tv_pair_t *)data;
int32_t in_len = (int32_t )strlen((const char *)tv->in);
int32_t max_out_len = ascii85_get_max_encoded_length(in_len);
LCUT_TRUE(tc, max_out_len > 0);
uint8_t obuf[max_out_len + 1];
int32_t olen = encode_ascii85(tv->in, in_len, obuf, max_out_len);
LCUT_TRUE(tc, olen >= 0);
LCUT_TRUE(tc, olen <= max_out_len);
obuf[olen] = 0u;
LCUT_TRUE(tc, (0 == strcmp((char *)obuf, (const char *)tv->out)));
int32_t out_len = (int32_t )strlen((const char *)tv->out);
int32_t max_in_len = ascii85_get_max_decoded_length(out_len);
LCUT_TRUE(tc, max_in_len > 0);
uint8_t ibuf[max_in_len + 1];
olen = decode_ascii85(tv->out, out_len, ibuf, max_in_len);
LCUT_TRUE(tc, olen >= 0);
LCUT_TRUE(tc, olen <= max_in_len);
ibuf[olen] = 0u;
LCUT_TRUE(tc, (0 == strcmp((char *)ibuf, (const char *)tv->in)));
}
typedef struct tv_epair_s
{
const uint8_t *in;
int32_t isz;
const uint8_t *out;
int32_t osz;
} tv_epair_t;
#define helloworld ((const uint8_t *)"hello world!")
static tv_epair_t tps0 = { .in = helloworld, .isz = 0, .out = (const uint8_t *)"", .osz = 0 };
static tv_epair_t tps1 = { .in = helloworld, .isz = 1, .out = (const uint8_t *)"BE", .osz = 2 };
static tv_epair_t tps2 = { .in = helloworld, .isz = 2, .out = (const uint8_t *)"BOq", .osz = 3 };
static tv_epair_t tps3 = { .in = helloworld, .isz = 3, .out = (const uint8_t *)"BOtu", .osz = 4 };
static tv_epair_t tps4 = { .in = helloworld, .isz = 4, .out = (const uint8_t *)"BOu!r", .osz = 5 };
static tv_epair_t tps5 = { .in = helloworld, .isz = 5, .out = (const uint8_t *)"BOu!rDZ", .osz = 7 };
static tv_epair_t tps6 = { .in = helloworld, .isz = 6, .out = (const uint8_t *)"BOu!rD]f", .osz = 8 };
static tv_epair_t tps7 = { .in = helloworld, .isz = 7, .out = (const uint8_t *)"BOu!rD]j6", .osz = 9 };
static tv_epair_t tps8 = { .in = helloworld, .isz = 8, .out = (const uint8_t *)"BOu!rD]j7B", .osz = 10 };
static tv_epair_t tps9 = { .in = helloworld, .isz = 9, .out = (const uint8_t *)"BOu!rD]j7BEW", .osz = 12 };
static tv_epair_t tps10 = { .in = helloworld, .isz = 10, .out = (const uint8_t *)"BOu!rD]j7BEbk", .osz = 13 };
static tv_epair_t tps11 = { .in = helloworld, .isz = 11, .out = (const uint8_t *)"BOu!rD]j7BEbo7", .osz = 14 };
static tv_epair_t tps12 = { .in = helloworld, .isz = 12, .out = (const uint8_t *)"BOu!rD]j7BEbo80", .osz = 15 };
static uint8_t zeroes[32];
static tv_epair_t tpz0 = { .in = zeroes, .isz = 0, .out = (const uint8_t *)"", .osz = 0 };
static tv_epair_t tpz4 = { .in = zeroes, .isz = 4, .out = (const uint8_t *)"z", .osz = 1 };
static tv_epair_t tpz8 = { .in = zeroes, .isz = 8, .out = (const uint8_t *)"zz", .osz = 2 };
static tv_epair_t tpz12 = { .in = zeroes, .isz = 12, .out = (const uint8_t *)"zzz", .osz = 3 };
static tv_epair_t tpz16 = { .in = zeroes, .isz = 16, .out = (const uint8_t *)"zzzz", .osz = 4 };
static tv_epair_t tpz20 = { .in = zeroes, .isz = 20, .out = (const uint8_t *)"zzzzz", .osz = 5 };
static tv_epair_t tpz24 = { .in = zeroes, .isz = 24, .out = (const uint8_t *)"zzzzzz", .osz = 6 };
static tv_epair_t tpz28 = { .in = zeroes, .isz = 28, .out = (const uint8_t *)"zzzzzzz", .osz = 7 };
static tv_epair_t tpz32 = { .in = zeroes, .isz = 32, .out = (const uint8_t *)"zzzzzzzz", .osz = 8 };
static tv_epair_t tpz1 = { .in = zeroes, .isz = 1, .out = (const uint8_t *)"!!", .osz = 2 };
static tv_epair_t tpz5 = { .in = zeroes, .isz = 5, .out = (const uint8_t *)"z!!", .osz = 3 };
static tv_epair_t tpz9 = { .in = zeroes, .isz = 9, .out = (const uint8_t *)"zz!!", .osz = 4 };
static tv_epair_t tpz2 = { .in = zeroes, .isz = 2, .out = (const uint8_t *)"!!!", .osz = 3 };
static tv_epair_t tpz6 = { .in = zeroes, .isz = 6, .out = (const uint8_t *)"z!!!", .osz = 4 };
static tv_epair_t tpz10 = { .in = zeroes, .isz = 10, .out = (const uint8_t *)"zz!!!", .osz = 5 };
static tv_epair_t tpz3 = { .in = zeroes, .isz = 3, .out = (const uint8_t *)"!!!!", .osz = 4 };
static tv_epair_t tpz7 = { .in = zeroes, .isz = 7, .out = (const uint8_t *)"z!!!!", .osz = 5 };
static tv_epair_t tpz11 = { .in = zeroes, .isz = 11, .out = (const uint8_t *)"zz!!!!", .osz = 6 };
static const uint8_t binary1[] = { 0xff, 0xd8, 0xff, 0xe0 };
static const uint8_t binary2[] = { 0xff, 0xff, 0xff, 0xff };
static tv_epair_t tpb0 = { .in = binary1, .isz = 4, .out = (const uint8_t *)"s4IA0", .osz = 5};
static tv_epair_t tpb1 = { .in = binary2, .isz = 4, .out = (const uint8_t *)"s8W-!", .osz = 5};
static void tc_e_encode_decode (lcut_tc_t *tc, void *data)
{
tv_epair_t *tv = (tv_epair_t *)data;
uint8_t buf[1024];
int32_t olen = encode_ascii85(tv->in, tv->isz, buf, 1023u);
LCUT_TRUE(tc, olen >= 0);
LCUT_TRUE(tc, olen < 1024);
LCUT_TRUE(tc, olen == tv->osz);
LCUT_TRUE(tc, (0 == memcmp(buf, tv->out, olen)));
olen = decode_ascii85(tv->out, tv->osz, buf, 1023u);
LCUT_TRUE(tc, olen >= 0);
LCUT_TRUE(tc, olen < 1024);
LCUT_TRUE(tc, olen == tv->isz);
LCUT_TRUE(tc, (0 == memcmp(buf, tv->in, olen)));
}
static tv_epair_t tpe1 = { .in = (const uint8_t *)"abcx", .isz = 4, .out = (const uint8_t *)"", .osz = ascii85_err_bad_decode_char };
static tv_epair_t tpe2 = { .in = (const uint8_t *)"~>", .isz = 2, .out = (const uint8_t *)"", .osz = ascii85_err_bad_decode_char };
static tv_epair_t tpe3 = { .in = (const uint8_t *)"s8W-\"", .isz = 5, .out = (const uint8_t *)"", .osz = ascii85_err_decode_overflow };
static void tc_expect_error (lcut_tc_t *tc, void *data)
{
tv_epair_t *tv = (tv_epair_t *)data;
uint8_t buf[1024];
int32_t olen = decode_ascii85(tv->in, tv->isz, buf, 1023u);
if (olen != tv->osz) printf("Got: %d sb %d\n", olen, tv->osz);
LCUT_TRUE(tc, olen < 0);
LCUT_TRUE(tc, olen == tv->osz);
}
#define MAX_A85_SIZE (4095u)
static uint32_t random_size (void)
{
uint32_t x = (xorshift128plus_next() % 8u) + 1u;
uint32_t y = (xorshift128plus_next() % 8u) + 1u;
uint32_t size = xorshift128plus_next() & ((1u << ((x / y) + 4)) - 1u);
if (size < 1u)
{
//fprintf(stderr, "size increased from %u to 1\n", size);
size = 1u;
}
else if (size > MAX_A85_SIZE)
{
//fprintf(stderr, "size decreased from %u to %u\n", size, MAX_A85_SIZE);
size = MAX_A85_SIZE;
}
else
{
// ok
}
return (size);
}
static uint64_t rand_seed;
static void tc_a85_random (lcut_tc_t *tc, void *data)
{
uint8_t ibuf[MAX_A85_SIZE];
uint8_t obuf[MAX_A85_SIZE + (MAX_A85_SIZE / 2u)];
uint8_t dbuf[MAX_A85_SIZE * 6u];
int count = 100000;
uint64_t rand = 0u;
(void )data;
if (rand_seed == 0)
{
// Spread out the changing bits since only a few are likely to change between runs
rand_seed = murmurhash3_avalanche(time(NULL));
rand_seed ^= murmurhash3_avalanche((uint64_t )&rand_seed);
}
xorshift128plus_seed(rand_seed);
while (count--)
{
uint32_t isz = random_size();
LCUT_TRUE(tc, ascii85_get_max_encoded_length(isz) <= (int32_t )sizeof(obuf));
for (uint32_t i = 0u; i < isz; )
{
if ((i % 8u) == 0u)
{
rand = xorshift128plus_next();
}
else
{
rand >>= 8u;
}
ibuf[i++] = rand & 0xffu;
}
int32_t olen = encode_ascii85(ibuf, isz, obuf, sizeof(obuf));
LCUT_TRUE(tc, olen >= 0);
LCUT_TRUE(tc, olen <= ascii85_get_max_encoded_length(isz));
int32_t max_decoded = ascii85_get_max_decoded_length(olen);
LCUT_TRUE(tc, max_decoded > 0);
LCUT_TRUE(tc, max_decoded <= (int32_t )sizeof(dbuf));
olen = decode_ascii85(obuf, olen, dbuf, sizeof(dbuf));
if (olen < 0) printf("Err: %d isz: %d seed: %" PRIu64 "\n", olen, isz, rand_seed); else {}
LCUT_TRUE(tc, olen >= 0);
LCUT_TRUE(tc, olen <= max_decoded);
LCUT_TRUE(tc, (uint32_t )olen == isz);
LCUT_TRUE(tc, (0 == memcmp(dbuf, ibuf, olen)));
}
}
static int do_unit_test (void)
{
lcut_t test;
lcut_ts_t *suite = NULL;
LCUT_TEST_BEGIN(&test, "Ascii85 Tests", NULL, NULL);
LCUT_TS_INIT(&test, suite, "Ascii85 Tests", NULL, NULL);
LCUT_TC_ADD(&test, suite, "Leviathan", tc_encode_decode, (void *)&tp0, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 0", tc_e_encode_decode, (void *)&tps0, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 1", tc_e_encode_decode, (void *)&tps1, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 2", tc_e_encode_decode, (void *)&tps2, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 3", tc_e_encode_decode, (void *)&tps3, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 4", tc_e_encode_decode, (void *)&tps4, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 5", tc_e_encode_decode, (void *)&tps5, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 6", tc_e_encode_decode, (void *)&tps6, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 7", tc_e_encode_decode, (void *)&tps7, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 8", tc_e_encode_decode, (void *)&tps8, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 9", tc_e_encode_decode, (void *)&tps9, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 10", tc_e_encode_decode, (void *)&tps10, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 11", tc_e_encode_decode, (void *)&tps11, NULL, NULL);
LCUT_TC_ADD(&test, suite, "HelloWorld 12", tc_e_encode_decode, (void *)&tps12, NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 0", tc_e_encode_decode, (void *)&tpz0 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 1", tc_e_encode_decode, (void *)&tpz1 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 2", tc_e_encode_decode, (void *)&tpz2 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 3", tc_e_encode_decode, (void *)&tpz3 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 4", tc_e_encode_decode, (void *)&tpz4 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 5", tc_e_encode_decode, (void *)&tpz5 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 6", tc_e_encode_decode, (void *)&tpz6 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 7", tc_e_encode_decode, (void *)&tpz7 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 8", tc_e_encode_decode, (void *)&tpz8 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 9", tc_e_encode_decode, (void *)&tpz9 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 10", tc_e_encode_decode, (void *)&tpz10, NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 11", tc_e_encode_decode, (void *)&tpz11, NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 12", tc_e_encode_decode, (void *)&tpz12, NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 16", tc_e_encode_decode, (void *)&tpz16, NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 20", tc_e_encode_decode, (void *)&tpz20, NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 24", tc_e_encode_decode, (void *)&tpz24, NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 28", tc_e_encode_decode, (void *)&tpz28, NULL, NULL);
LCUT_TC_ADD(&test, suite, "Zeroes 32", tc_e_encode_decode, (void *)&tpz32, NULL, NULL);
LCUT_TC_ADD(&test, suite, "Binary 0", tc_e_encode_decode, (void *)&tpb0 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Binary 1", tc_e_encode_decode, (void *)&tpb1 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Error bad char x", tc_expect_error, (void *)&tpe1 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Error bad char ~", tc_expect_error, (void *)&tpe2 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Error overflow", tc_expect_error, (void *)&tpe3 , NULL, NULL);
LCUT_TC_ADD(&test, suite, "Random data", tc_a85_random, (void *)NULL , NULL, NULL);
// LCUT_TC_ADD(&test, suite, "test random sorts", tc_random_sorts, NULL, tc_random_setup, NULL);
LCUT_TS_ADD(&test, suite);
LCUT_TEST_RUN(&test);
LCUT_TEST_REPORT(&test);
LCUT_TEST_END(&test);
LCUT_TEST_RESULT(&test); // exits
return 0; // not reached
}
static void usage (void)
{
printf("usage: test [-u] [-i <string>] [-o <string>] [-s <seed>]\n");
exit(EXIT_FAILURE);
}
// Examples:
// e$ ./test -i hello
// <~BOu!rDZ~>
// e$ ./test -i hello -o 'BOu!rDZ'
// ~Encode OK
// e$ ./test -o 'BOu!rDZ'
// ~Decoded: 68656c6c6f
int main (int argc, char **argv)
{
int i = 1;
bool unit_test = true;
char *istr = NULL;
char *ostr = NULL;
while (i < argc)
{
if (strcmp(argv[i], "-i") == 0)
{
unit_test = false;
i += 1;
if (i < argc) istr = argv[i];
else usage();
}
else if (strcmp(argv[i], "-o") == 0)
{
unit_test = false;
i += 1;
if (i < argc) ostr = argv[i];
else usage();
}
else if (strcmp(argv[i], "-s") == 0)
{
i += 1;
if (i < argc) rand_seed = atoll(argv[i]);
else usage();
}
else
{
//printf("arg %d: %s\n", i, argv[i]);
break;
}
i += 1;
}
if (unit_test)
{
return do_unit_test();
}
else if (NULL != istr)
{
size_t isz = strlen(istr);
int32_t olen = ascii85_get_max_encoded_length(isz);
if (olen < 0)
{
printf("~Encode size error %d\n", olen);
}
else
{
uint8_t obuf[olen];
olen = encode_ascii85((uint8_t *)istr, isz, obuf, olen);
if (NULL != ostr)
{
size_t osz = strlen(ostr);
if ((size_t )olen != osz)
{
printf("~Encode size mismatch, expected %zu got %d\n", osz, olen);
}
else if (0 != memcmp(obuf, ostr, olen))
{
printf("~Encode mismatch\n");
}
else
{
printf("~Encode OK\n");
}
}
else
{
printf("<~");
for (i = 0; i < olen; i++) printf("%c", (char )obuf[i]);
printf("~>\n");
}
}
}
else if (NULL != ostr)
{
// Not very useful perhaps since ostr is binary so hard to type at console
size_t osz = strlen(ostr);
int32_t olen = ascii85_get_max_decoded_length(osz);
if (olen < 0)
{
printf("~Decode size error %d\n", olen);
}
else
{
uint8_t dbuf[olen];
int32_t ilen = decode_ascii85((uint8_t *)ostr, osz, dbuf, olen);
if (ilen < 0)
{
printf("~Decode error %d\n", ilen);
}
else
{
printf("~Decoded: ");
for (i = 0; i < ilen; i++) printf("%02x", dbuf[i]);
printf("\n");
}
}
}
else
{
usage();
}
}