This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <config.h>
19
20 #include "base64.h"
21
22 #include <stddef.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdint.h>
27
28 #include "macros.h"
29
30 int
31 main (void)
32 {
33 const char *in = "abcdefghijklmnop";
34 const char *b64in = "YWJjZGVmZw==";
35 char out[255];
36 idx_t len;
37 bool ok;
38 char *p;
39
40 memset (out, 0x42, sizeof (out));
41 base64_encode (in, 0, out, 0);
42 ASSERT (out[0] == '\x42');
43
44 memset (out, 0x42, sizeof (out));
45 base64_encode (in, 1, out, 1);
46 ASSERT (memcmp (out, "YQ==", 1) == 0);
47
48 memset (out, 0x42, sizeof (out));
49 base64_encode (in, 1, out, 2);
50 ASSERT (memcmp (out, "YQ==", 2) == 0);
51
52 memset (out, 0x42, sizeof (out));
53 base64_encode (in, 1, out, 3);
54 ASSERT (memcmp (out, "YQ==", 3) == 0);
55
56 memset (out, 0x42, sizeof (out));
57 base64_encode (in, 1, out, 4);
58 ASSERT (memcmp (out, "YQ==", 4) == 0);
59
60 memset (out, 0x42, sizeof (out));
61 base64_encode (in, 1, out, 8);
62 ASSERT (memcmp (out, "YQ==", 4) == 0);
63
64 memset (out, 0x42, sizeof (out));
65 base64_encode (in, 2, out, 4);
66 ASSERT (memcmp (out, "YWI=", 4) == 0);
67
68 memset (out, 0x42, sizeof (out));
69 base64_encode (in, 3, out, 4);
70 ASSERT (memcmp (out, "YWJj", 4) == 0);
71
72 memset (out, 0x42, sizeof (out));
73 base64_encode (in, 4, out, 5);
74 ASSERT (memcmp (out, "YWJjZA==", 5) == 0);
75
76 memset (out, 0x42, sizeof (out));
77 base64_encode (in, 4, out, 100);
78 ASSERT (memcmp (out, "YWJjZA==", 6) == 0);
79
80
81
82 memset (out, 0x42, sizeof (out));
83 len = 0;
84 ok = base64_decode (b64in, 4, out, &len);
85 ASSERT (ok);
86 ASSERT (len == 0);
87
88 memset (out, 0x42, sizeof (out));
89 len = 1;
90 ok = base64_decode (b64in, 4, out, &len);
91 ASSERT (ok);
92 ASSERT (len == 1);
93 ASSERT (memcmp (out, "abcdefg", 1) == 0);
94
95 memset (out, 0x42, sizeof (out));
96 len = 2;
97 ok = base64_decode (b64in, 4, out, &len);
98 ASSERT (ok);
99 ASSERT (len == 2);
100 ASSERT (memcmp (out, "abcdefg", 2) == 0);
101
102 memset (out, 0x42, sizeof (out));
103 len = 3;
104 ok = base64_decode (b64in, 4, out, &len);
105 ASSERT (ok);
106 ASSERT (len == 3);
107 ASSERT (memcmp (out, "abcdefg", 3) == 0);
108
109 memset (out, 0x42, sizeof (out));
110 len = 4;
111 ok = base64_decode (b64in, 4, out, &len);
112 ASSERT (ok);
113 ASSERT (len == 3);
114 ASSERT (memcmp (out, "abcdefg", 3) == 0);
115
116 memset (out, 0x42, sizeof (out));
117 len = 100;
118 ok = base64_decode (b64in, strlen (b64in), out, &len);
119 ASSERT (ok);
120 ASSERT (len == 7);
121 ASSERT (memcmp (out, "abcdefg", 7) == 0);
122
123
124
125 len = base64_encode_alloc (in, strlen (in), &p);
126 ASSERT (len == 24);
127 ASSERT (strcmp (p, "YWJjZGVmZ2hpamtsbW5vcA==") == 0);
128 free (p);
129
130 len = base64_encode_alloc (in, IDX_MAX - 5, &p);
131 ASSERT (len == 0);
132
133
134 {
135 struct base64_decode_context ctx;
136
137 base64_decode_ctx_init (&ctx);
138
139 len = sizeof (out);
140 ok = base64_decode_ctx (&ctx, b64in, strlen (b64in), out, &len);
141 ASSERT (ok);
142 ASSERT (len == 7);
143 ASSERT (memcmp (out, "abcdefg", len) == 0);
144 }
145
146
147
148 ok = base64_decode_alloc_ctx (NULL, b64in, strlen (b64in), &p, &len);
149 ASSERT (ok);
150 ASSERT (len == 7);
151 ASSERT (memcmp (out, "abcdefg", len) == 0);
152 free (p);
153
154 {
155 struct base64_decode_context ctx;
156 const char *newlineb64 = "YWJjZG\nVmZ2hp\namtsbW5vcA==";
157
158 base64_decode_ctx_init (&ctx);
159
160 ok = base64_decode_alloc_ctx (&ctx, newlineb64, strlen (newlineb64), &p, &len);
161 ASSERT (ok);
162 ASSERT (len == strlen (in));
163 ASSERT (memcmp (p, in, len) == 0);
164 free (p);
165 }
166
167 {
168 struct base64_decode_context ctx;
169 base64_decode_ctx_init (&ctx);
170
171 ok = base64_decode_alloc_ctx (&ctx, "YW\nJjZGVmZ2hp", 13, &p, &len);
172 ASSERT (ok);
173 ASSERT (len == 9);
174 ASSERT (memcmp (p, "abcdefghi", len) == 0);
175 free (p);
176
177 base64_decode_ctx_init (&ctx);
178
179 ok = base64_decode_alloc_ctx (&ctx, "YW\n", 3, &p, &len);
180 ASSERT (ok);
181 ASSERT (len == 0);
182 free (p);
183
184 ok = base64_decode_alloc_ctx (&ctx, "JjZGVmZ2", 8, &p, &len);
185 ASSERT (ok);
186 ASSERT (len == 6);
187 ASSERT (memcmp (p, "abcdef", len) == 0);
188 free (p);
189
190 ok = base64_decode_alloc_ctx (&ctx, "hp", 2, &p, &len);
191 ASSERT (ok);
192 ASSERT (len == 3);
193 ASSERT (memcmp (p, "ghi", len) == 0);
194 free (p);
195
196 ok = base64_decode_alloc_ctx (&ctx, "", 0, &p, &len);
197 ASSERT (ok);
198 free (p);
199 }
200
201 {
202 struct base64_decode_context ctx;
203 const char *newlineb64 = "\n\n\n\n\n";
204
205 base64_decode_ctx_init (&ctx);
206
207 ok = base64_decode_alloc_ctx (&ctx, newlineb64, strlen (newlineb64), &p, &len);
208 ASSERT (ok);
209 ASSERT (len == 0);
210 free (p);
211 }
212
213 ok = base64_decode_alloc_ctx (NULL, " ! ", 3, &p, &len);
214 ASSERT (!ok);
215
216 ok = base64_decode_alloc_ctx (NULL, "abc\ndef", 7, &p, &len);
217 ASSERT (!ok);
218
219 ok = base64_decode_alloc_ctx (NULL, "aa", 2, &p, &len);
220 ASSERT (!ok);
221
222 ok = base64_decode_alloc_ctx (NULL, "aa=", 3, &p, &len);
223 ASSERT (!ok);
224
225 ok = base64_decode_alloc_ctx (NULL, "aax", 3, &p, &len);
226 ASSERT (!ok);
227
228 ok = base64_decode_alloc_ctx (NULL, "aa=X", 4, &p, &len);
229 ASSERT (!ok);
230
231 ok = base64_decode_alloc_ctx (NULL, "aa=X", 4, &p, &len);
232 ASSERT (!ok);
233
234 ok = base64_decode_alloc_ctx (NULL, "aax=X", 5, &p, &len);
235 ASSERT (!ok);
236
237 return 0;
238 }