4 #include <asn_bit_data.c>
7 check_asn_bits_decoding() {
8 uint8_t buf[] = { 0xB7, 0x19, 0x2F, 0xEE, 0xAD };
12 memset(&pos, 0, sizeof(pos));
16 pos.nbits = sizeof(buf) * 8;
18 z = asn_get_few_bits(&pos, 32);
20 assert(pos.nbits == sizeof(buf) * 8);
22 z = asn_get_few_bits(&pos, 0);
24 assert(pos.nboff == 0);
25 assert(pos.nbits == sizeof(buf) * 8);
27 z = asn_get_few_bits(&pos, 1);
29 assert(pos.nboff == 1);
30 assert(pos.nbits == sizeof(buf) * 8);
32 z = asn_get_few_bits(&pos, 2);
34 assert(pos.nboff == 3);
35 assert(pos.nbits == sizeof(buf) * 8);
37 z = asn_get_few_bits(&pos, 2);
39 assert(pos.nboff == 5);
40 assert(pos.nbits == sizeof(buf) * 8);
42 z = asn_get_few_bits(&pos, 3);
44 assert(pos.nboff == 8);
45 assert(pos.nbits == sizeof(buf) * 8);
47 z = asn_get_few_bits(&pos, 8);
49 assert(pos.nboff == 8);
50 assert(pos.nbits == (sizeof(buf) - 1) * 8);
52 z = asn_get_few_bits(&pos, 1);
54 assert(pos.nboff == 1);
55 assert(pos.nbits == (sizeof(buf) - 2) * 8);
57 z = asn_get_few_bits(&pos, 3);
59 assert(pos.nboff == 4);
60 assert(pos.nbits == (sizeof(buf) - 2) * 8);
62 z = asn_get_few_bits(&pos, 8);
64 assert(pos.nboff == 12);
68 pos.nbits = sizeof(buf) * 8;
69 z = asn_get_few_bits(&pos, 24);
70 assert(z == 14443711);
72 pos.buffer = (unsigned char *)"\001";
75 z = asn_get_few_bits(&pos, 1);
76 assert(pos.nboff == 7);
77 assert(pos.nbits == 7);
80 pos.buffer = (unsigned char *)"\001";
83 z = asn_get_few_bits(&pos, 1);
84 assert(pos.nboff == 8);
85 assert(pos.nbits == 8);
88 pos.buffer = (unsigned char *)"\000";
91 z = asn_get_few_bits(&pos, 1);
92 assert(pos.nboff == 8);
93 assert(pos.nbits == 8);
95 z = asn_get_few_bits(&pos, 1);
96 assert(pos.nboff == 8);
97 assert(pos.nbits == 8);
100 pos.buffer = (unsigned char *)"\000";
103 z = asn_get_few_bits(&pos, 1);
104 assert(pos.nboff == 8);
105 assert(pos.nbits == 9);
107 z = asn_get_few_bits(&pos, 1);
108 assert(pos.nboff == 1);
109 assert(pos.nbits == 1);
112 pos.buffer = (unsigned char *)"\001";
115 z = asn_get_few_bits(&pos, 1);
116 assert(pos.nboff == 8);
117 assert(pos.nbits == 9);
119 z = asn_get_few_bits(&pos, 1);
120 assert(pos.nboff == 1);
121 assert(pos.nbits == 1);
124 /* Get full 31-bit range */
127 pos.nbits = sizeof(buf) * 8;
128 z = asn_get_few_bits(&pos, 31);
129 assert(z == 1179384747);
131 /* Get a bit shifted range */
134 pos.nbits = sizeof(buf) * 8;
135 z = asn_get_few_bits(&pos, 31);
136 assert(z == 1663434197);
140 pos.nbits = sizeof(buf) * 8;
141 z = asn_get_many_bits(&pos, tmpbuf, 0, sizeof(buf) * 8);
143 assert(buf[0] == tmpbuf[0]);
144 assert(buf[1] == tmpbuf[1]);
145 assert(buf[2] == tmpbuf[2]);
146 assert(buf[3] == tmpbuf[3]);
147 assert(buf[4] == tmpbuf[4]);
151 pos.nbits = sizeof(buf) * 8;
152 z = asn_get_many_bits(&pos, tmpbuf, 0, sizeof(buf) * 8);
157 pos.nbits = sizeof(buf) * 8;
158 z = asn_get_many_bits(&pos, tmpbuf, 0, sizeof(buf) * 8 - 1);
160 assert(tmpbuf[0] == 110);
161 assert(tmpbuf[1] == 50);
162 assert(tmpbuf[2] == 95);
163 assert(tmpbuf[3] == 221);
164 assert(tmpbuf[4] == 90);
168 pos.nbits = sizeof(buf) * 8;
169 z = asn_get_many_bits(&pos, tmpbuf, 1, sizeof(buf) * 8 - 1);
171 assert(tmpbuf[0] == 55);
172 assert(tmpbuf[0] != buf[0]);
173 assert(tmpbuf[1] == buf[1]);
174 assert(tmpbuf[2] == buf[2]);
175 assert(tmpbuf[3] == buf[3]);
176 assert(tmpbuf[4] == buf[4]);
179 static int Ignore(const void *data, size_t size, void *op_key) {
187 check_asn_bits_encoding() {
191 po.buffer = po.tmpspace;
196 po.tmpspace[0] = 0xff;
198 ret = asn_put_few_bits(&po, 0, 0);
200 assert(po.nboff == 0);
201 assert(po.buffer == po.tmpspace);
202 assert(po.tmpspace[0] == 0xff);
204 ret = asn_put_few_bits(&po, 0, 1);
206 assert(po.nboff == 1);
207 assert(po.nbits == 8 * sizeof(po.tmpspace));
208 assert(po.buffer == po.tmpspace);
209 assert(po.tmpspace[0] == 0x00);
211 ret = asn_put_few_bits(&po, 1, 1);
213 assert(po.nboff == 2);
214 assert(po.nbits == 8 * sizeof(po.tmpspace));
215 assert(po.buffer == po.tmpspace);
216 assert(po.tmpspace[0] == 0x40);
218 ret = asn_put_few_bits(&po, 1, 1);
220 assert(po.nboff == 3);
221 assert(po.nbits == 8 * sizeof(po.tmpspace));
222 assert(po.buffer == po.tmpspace);
223 assert(po.tmpspace[0] == 0x60);
225 ret = asn_put_few_bits(&po, 15, 5);
227 assert(po.nboff == 8);
228 assert(po.nbits == 8 * sizeof(po.tmpspace));
229 assert(po.buffer == po.tmpspace);
230 assert(po.tmpspace[0] == 0x6F);
232 ret = asn_put_few_bits(&po, 0xf0ff, 16);
234 assert(po.nboff == 16);
235 assert(po.nbits == 8 * sizeof(po.tmpspace) - 8);
236 assert(po.buffer == po.tmpspace + 1);
237 assert(po.tmpspace[0] == 0x6F);
238 assert(po.tmpspace[1] == 0xf0);
239 assert(po.tmpspace[2] == 0xff);
243 ret = asn_put_few_bits(&po, 2, 1);
245 assert(po.nboff == 8);
246 assert(po.nbits == 8 * sizeof(po.tmpspace) - 16);
247 assert(po.buffer == po.tmpspace + 2);
248 assert(po.tmpspace[0] == 0x6F);
249 assert(po.tmpspace[1] == 0xf0);
250 assert(po.tmpspace[2] == 0xfe);
252 ret = asn_put_few_bits(&po, 2, 32);
255 ret = asn_put_few_bits(&po, 2, -1);
258 ret = asn_put_few_bits(&po, -1, 31);
260 assert(po.nboff == 31);
261 assert(po.nbits == 8 * sizeof(po.tmpspace) - 24);
262 assert(po.buffer == po.tmpspace + 3);
263 assert(po.tmpspace[0] == 0x6F);
264 assert(po.tmpspace[1] == 0xf0);
265 assert(po.tmpspace[2] == 0xfe);
266 assert(po.tmpspace[3] == 0xff);
267 assert(po.tmpspace[4] == 0xff);
268 assert(po.tmpspace[5] == 0xff);
269 assert(po.tmpspace[6] == 0xfe);
274 * Add N bits after P bits. Should result in N+P bits added.
277 check_asn_bits_encoding_auto() {
281 for(prior = 0; prior <= 31; prior++) {
282 for(next = 0; next <= 31; next++) {
284 po.buffer = po.tmpspace;
289 po.tmpspace[0] = 0xff;
291 ret = asn_put_few_bits(&po, -1, prior);
294 ASN_DEBUG(" (out{nboff=%d,nbits=%d,buf_offset=%d})", (int)po.nboff, (int)po.nbits, (int)(po.buffer - po.tmpspace));
296 ret = asn_put_few_bits(&po, -1, next);
299 ASN_DEBUG(" (out{nboff=%d,nbits=%d,buf_offset=%d})", (int)po.nboff, (int)po.nbits, (int)(po.buffer - po.tmpspace));
301 ASN_DEBUG("Putting %d + %d bits (%d/%d), got %d bytes and %d bits",
302 prior, next, (prior + next) / 8, (prior + next) % 8,
303 (int)(po.buffer - po.tmpspace), (int)po.nboff);
304 assert((po.buffer - po.tmpspace) * 8 + po.nboff == (size_t)(prior + next));
305 for(i = 0; i < (po.buffer - po.tmpspace); i++)
306 assert(po.tmpspace[0] == (unsigned char)-1);
312 check_asn_bits_sweep_with(uint8_t buf[], int already_bits, int add_bits) {
321 memset(&pos, 0, sizeof(pos));
324 pos.nbits = buf_size * 8;
326 memset(&out, 0, sizeof(out));
327 out.buffer = out.tmpspace;
328 out.nbits = 8 * sizeof(out.tmpspace);
329 assert(sizeof(out.tmpspace) >= buf_size);
330 memcpy(out.buffer, buf, buf_size);
332 d_already = asn_get_few_bits(&pos, already_bits);
333 d_add = asn_get_few_bits(&pos, add_bits);
335 asn_put_few_bits(&out, d_already, already_bits);
336 asn_put_few_bits(&out, d_add, add_bits);
338 left_bits = 8 - (out.nboff % 8);
339 d_left = asn_get_few_bits(&pos, left_bits);
344 asn_put_few_bits(&out, d_left, left_bits);
345 assert(0 == (out.nboff % 8));
347 if(0 != memcmp(out.tmpspace, buf, buf_size)) {
349 for(size_t i = 0; i < buf_size; i++)
350 printf(" %02x", buf[i]);
352 for(size_t i = 0; i < buf_size; i++)
353 printf(" %02x", out.tmpspace[i]);
354 printf(" (out{nboff=%d,left=%d,%02x})\n", (int)out.nboff, left_bits, (int)d_left);
355 assert(0 == memcmp(out.tmpspace, buf, buf_size));
360 check_asn_bits_sweep() {
361 uint8_t buf[3][8] = {
362 { 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA },
363 { 0xB7, 0x19, 0x2F, 0xEE, 0xAD, 0x11, 0xAA, 0x55 },
364 { 0xEE, 0xAD, 0x11, 0xAA, 0x55, 0xB7, 0x19, 0x2F }
370 for(buf_idx = 0; buf_idx < 3; buf_idx++) {
371 for(already_bits = 0; already_bits < 24; already_bits++) {
372 for(add_bits = 0; add_bits <= 31; add_bits++) {
373 check_asn_bits_sweep_with(buf[buf_idx], already_bits, add_bits);
381 check_asn_bits_decoding();
382 check_asn_bits_encoding();
383 check_asn_bits_encoding_auto();
384 check_asn_bits_sweep();