-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase64_test.c
56 lines (50 loc) · 1.09 KB
/
base64_test.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "weecrypt.h"
int
main(void)
{
unsigned char text[256] = { 0 };
for (unsigned i = 0; i < 256; ++i)
text[i] = i;
for (unsigned i = 1; i <= sizeof(text); ++i) {
char *encode = base64_encode(text, i);
assert(encode != NULL);
unsigned decode_len = 0;
char *decode = base64_decode(encode, &decode_len);
assert(decode != NULL);
int bad = 0;
if (decode_len != i) {
printf("%u: decode_len=%u i=%u\n", i, decode_len, i);
bad = 1;
}
if (memcmp(text, decode, i) != 0) {
printf("%u: text and decode differ!", i);
bad = 1;
}
if (bad) {
printf("Original:");
for (unsigned j = 0; j < i; ++j) {
if (j % 8 == 0) {
printf("\n");
}
printf(" 0x%02X", text[j]);
}
printf("\n");
printf("Encoded: %s\n", encode);
printf("Decoded:");
for (unsigned j = 0; j < decode_len; ++j) {
if (j % 8 == 0) {
printf("\n");
}
printf(" 0x%02X", ((unsigned char *)decode)[j]);
}
printf("\n");
}
free(encode);
free(decode);
}
return 0;
}