blob: 39bbe3cebf26c6617d7ccb4e92c3a2ba9ee6e374 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#undef NDEBUG
2#include <stdio.h>
3#include <stdlib.h>
4#include <sys/types.h>
5#include <string.h>
6#include <assert.h>
7
8#include <T.h>
9
Lev Walkinf15320b2004-06-03 03:38:44 +000010
11uint8_t buf1[] = {
12 32 | (2 << 6), /* [0], constructed */
Lev Walkin2a744a72013-03-27 01:56:23 -070013 25, /* L */
Lev Walkinf15320b2004-06-03 03:38:44 +000014
15 /* string [0] IMPLICIT UTF8String, */
16 (2 << 6), /* [0] */
Lev Walkin2a744a72013-03-27 01:56:23 -070017 16, /* L */
Lev Walkinf15320b2004-06-03 03:38:44 +000018 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z',
19
20 /* beta [2] IMPLICIT INTEGER OPTIONAL */
21 (2 << 6) + 2, /* [2] */
Lev Walkin2a744a72013-03-27 01:56:23 -070022 5, /* L */
23 0,
24 75,
25 0x4b,
26 75,
Lev Walkinf15320b2004-06-03 03:38:44 +000027 75,
28};
29
Lev Walkin2a744a72013-03-27 01:56:23 -070030uint8_t buf1_reconstr[] = {
31 32 | (2 << 6), /* [0], constructed */
32 24, /* L */
Lev Walkinf15320b2004-06-03 03:38:44 +000033
Lev Walkin2a744a72013-03-27 01:56:23 -070034 /* string [0] IMPLICIT UTF8String, */
35 (2 << 6), /* [0] */
36 16, /* L */
Lev Walkinf15320b2004-06-03 03:38:44 +000037 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z',
38
Lev Walkin2a744a72013-03-27 01:56:23 -070039 /* beta [2] IMPLICIT INTEGER OPTIONAL */
40 (2 << 6) + 2, /* [2] */
41 4, /* L */
Lev Walkinf15320b2004-06-03 03:38:44 +000042 75,
Lev Walkin2a744a72013-03-27 01:56:23 -070043 75,
44 75,
45 0x4b,
Lev Walkinf15320b2004-06-03 03:38:44 +000046};
47
48
Lev Walkinf15320b2004-06-03 03:38:44 +000049static void
Lev Walkin1715b322013-03-28 04:02:13 -070050check(T_t *tp, uint8_t *buf, size_t size, size_t consumed) {
Lev Walkin90bf7ae2004-10-20 15:46:56 +000051 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +000052
53 tp = memset(tp, 0, sizeof(*tp));
54
Lev Walkin1715b322013-03-28 04:02:13 -070055 fprintf(stderr, "Buf %p (%zd)\n", buf, size);
Lev Walkinc7400c52004-09-29 13:14:36 +000056 rval = ber_decode(0, &asn_DEF_T, (void **)&tp, buf, size);
Lev Walkin1715b322013-03-28 04:02:13 -070057 fprintf(stderr, "Returned code %d, consumed %zd\n",
58 (int)rval.code, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +000059
60 assert(rval.code == RC_OK);
61 assert(rval.consumed == consumed);
62
Lev Walkin2a744a72013-03-27 01:56:23 -070063 assert(tp->choice.seq.string.size == 16);
Lev Walkin5b63acf2014-01-14 01:48:37 -080064 assert(strcmp((char *)tp->choice.seq.string.buf, "zzzzzzzzzzzzzzzz") == 0);
Lev Walkin2a744a72013-03-27 01:56:23 -070065 assert(tp->choice.seq.alpha == NULL);
66 assert(tp->choice.seq.beta);
67 assert(*tp->choice.seq.beta == 0x4b4b4b4b);
Lev Walkinf15320b2004-06-03 03:38:44 +000068}
69
Lev Walkin67c2aec2004-06-05 08:47:18 +000070size_t buf_pos;
71size_t buf_size;
Lev Walkin2a744a72013-03-27 01:56:23 -070072uint8_t *buf;
Lev Walkinf15320b2004-06-03 03:38:44 +000073
74static int
Lev Walkin2a744a72013-03-27 01:56:23 -070075buf_fill(const void *buffer, size_t size, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +000076
Lev Walkin67c2aec2004-06-05 08:47:18 +000077 (void)app_key; /* Unused argument */
78
Lev Walkinf15320b2004-06-03 03:38:44 +000079 if(buf_pos + size > buf_size) {
Lev Walkin1715b322013-03-28 04:02:13 -070080 fprintf(stderr, "%zd + %zd > %zd\n",
81 buf_pos, size, buf_size);
Lev Walkinf15320b2004-06-03 03:38:44 +000082 return -1;
83 }
84
Lev Walkin2a744a72013-03-27 01:56:23 -070085 memcpy(buf + buf_pos, buffer, size);
Lev Walkinf15320b2004-06-03 03:38:44 +000086 buf_pos += size;
Lev Walkin1715b322013-03-28 04:02:13 -070087 fprintf(stderr, " written %zd (%zd)\n", size, buf_pos);
Lev Walkinf15320b2004-06-03 03:38:44 +000088
89 return 0;
90}
91
Lev Walkin6fc56602004-08-20 13:16:42 +000092static void
Lev Walkinbc09dd42017-10-19 02:16:35 -070093compare(T_t *tp, uint8_t *cmp_buf, size_t cmp_buf_size) {
Lev Walkina9cc46e2004-09-22 16:06:28 +000094 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +000095
96 buf_size = cmp_buf_size + 100;
Lev Walkina4f8e942017-10-08 19:28:20 -070097 uint8_t scratch[buf_size];
98 buf = scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +000099 buf_pos = 0;
100
101 /*
102 * Try to re-create using DER encoding.
103 */
Lev Walkinc7400c52004-09-29 13:14:36 +0000104 erval = der_encode(&asn_DEF_T, tp, buf_fill, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 assert(erval.encoded != -1);
Lev Walkinbc09dd42017-10-19 02:16:35 -0700106 if((size_t)erval.encoded != cmp_buf_size) {
Lev Walkin1715b322013-03-28 04:02:13 -0700107 printf("%zd != %zd\n", erval.encoded, cmp_buf_size);
Lev Walkinbc09dd42017-10-19 02:16:35 -0700108 assert((size_t)erval.encoded == cmp_buf_size);
109 }
110 for(size_t i = 0; i < cmp_buf_size; i++) {
Lev Walkin2a744a72013-03-27 01:56:23 -0700111 if(buf[i] != cmp_buf[i]) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000112 fprintf(stderr, "Recreated buffer content mismatch:\n");
Lev Walkinbc09dd42017-10-19 02:16:35 -0700113 fprintf(stderr, "Byte %zd, %x != %x (%d != %d)\n",
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 i,
Lev Walkin2a744a72013-03-27 01:56:23 -0700115 buf[i], cmp_buf[i],
116 buf[i], cmp_buf[i]
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 );
118 }
Lev Walkin2a744a72013-03-27 01:56:23 -0700119 assert(buf[i] == cmp_buf[i]);
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 }
Lev Walkina4f8e942017-10-08 19:28:20 -0700121
122 buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000123}
124
125static void
Lev Walkin2a744a72013-03-27 01:56:23 -0700126partial_read(uint8_t *buf_0, size_t size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000127 T_t t, *tp;
Lev Walkin90bf7ae2004-10-20 15:46:56 +0000128 asn_dec_rval_t rval;
Lev Walkina4f8e942017-10-08 19:28:20 -0700129 uint8_t buf_1[size];
130 uint8_t buf_2[size];
131 uint8_t buf_3[size];
Lev Walkinf15320b2004-06-03 03:38:44 +0000132
133 fprintf(stderr, "\nPartial read sequence...\n");
134
135 /*
136 * Divide the space (size) into three blocks in various combinations:
137 * |<----->i1<----->i2<----->|
Lev Walkin2a744a72013-03-27 01:56:23 -0700138 * ^ buf_0 ^ buf_0+size
Lev Walkinf15320b2004-06-03 03:38:44 +0000139 * Try to read block by block.
140 */
Lev Walkina4f8e942017-10-08 19:28:20 -0700141 for(size_t i1 = 0; i1 < size; i1++) {
142 for(size_t i2 = i1; i2 < size; i2++) {
Lev Walkin2a744a72013-03-27 01:56:23 -0700143 uint8_t *chunk1 = buf_0;
Lev Walkin67c2aec2004-06-05 08:47:18 +0000144 size_t size1 = i1;
Lev Walkin2a744a72013-03-27 01:56:23 -0700145 uint8_t *chunk2 = buf_0 + size1;
Lev Walkin67c2aec2004-06-05 08:47:18 +0000146 size_t size2 = i2 - i1;
Lev Walkin2a744a72013-03-27 01:56:23 -0700147 uint8_t *chunk3 = buf_0 + size1 + size2;
Lev Walkin67c2aec2004-06-05 08:47:18 +0000148 size_t size3 = size - size1 - size2;
Lev Walkinf15320b2004-06-03 03:38:44 +0000149
Lev Walkin1715b322013-03-28 04:02:13 -0700150 fprintf(stderr, "\n%zd:{%zd, %zd, %zd}...\n",
151 size, size1, size2, size3);
Lev Walkinf15320b2004-06-03 03:38:44 +0000152
Lev Walkin2a744a72013-03-27 01:56:23 -0700153 memset(buf_1, 0, size);
154 memset(buf_2, 0, size);
155 memset(buf_3, 0, size);
156 memcpy(buf_1, chunk1, size1);
157 memcpy(buf_2, chunk2, size2);
158 memcpy(buf_3, chunk3, size3);
Lev Walkinf15320b2004-06-03 03:38:44 +0000159
160 tp = memset(&t, 0, sizeof(t));
161
Lev Walkin1715b322013-03-28 04:02:13 -0700162 fprintf(stderr, "=> Chunk 1 (%zd):\n", size1);
Lev Walkinc7400c52004-09-29 13:14:36 +0000163 rval = ber_decode(0, &asn_DEF_T, (void **)&tp,
Lev Walkin2a744a72013-03-27 01:56:23 -0700164 buf_1, size1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 assert(rval.code == RC_WMORE);
166 assert(rval.consumed <= size1);
167 if(rval.consumed < size1) {
168 int leftover = size1 - rval.consumed;
Lev Walkin2a744a72013-03-27 01:56:23 -0700169 memcpy(buf_2, buf_1 + rval.consumed, leftover);
170 memcpy(buf_2 + leftover, chunk2, size2);
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 size2 += leftover;
172 }
173
Lev Walkin1715b322013-03-28 04:02:13 -0700174 fprintf(stderr, "=> Chunk 2 (%zd):\n", size2);
Lev Walkinc7400c52004-09-29 13:14:36 +0000175 rval = ber_decode(0, &asn_DEF_T, (void **)&tp,
Lev Walkin2a744a72013-03-27 01:56:23 -0700176 buf_2, size2);
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 assert(rval.code == RC_WMORE);
178 assert(rval.consumed <= size2);
179 if(rval.consumed < size2) {
180 int leftover = size2 - rval.consumed;
Lev Walkin2a744a72013-03-27 01:56:23 -0700181 memcpy(buf_3, buf_2 + rval.consumed, leftover);
182 memcpy(buf_3 + leftover, chunk3, size3);
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 size3 += leftover;
184 }
185
Lev Walkin1715b322013-03-28 04:02:13 -0700186 fprintf(stderr, "=> Chunk 3 (%zd):\n", size3);
Lev Walkinc7400c52004-09-29 13:14:36 +0000187 rval = ber_decode(0, &asn_DEF_T, (void **)&tp,
Lev Walkin2a744a72013-03-27 01:56:23 -0700188 buf_3, size3);
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 assert(rval.code == RC_OK);
190 assert(rval.consumed == size3);
191
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700192 ASN_STRUCT_RESET(asn_DEF_T, &t);
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 }
194 }
195}
196
197int
Lev Walkin2a744a72013-03-27 01:56:23 -0700198main(int ac, char **av) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000199 T_t t;
200
Lev Walkin2a744a72013-03-27 01:56:23 -0700201 (void)ac; /* Unused argument */
202 (void)av; /* Unused argument */
Lev Walkinf15320b2004-06-03 03:38:44 +0000203
204 /* Check exact buf1 */
205 check(&t, buf1, sizeof(buf1), sizeof(buf1));
206 compare(&t, buf1_reconstr, sizeof(buf1_reconstr));
Lev Walkin2a744a72013-03-27 01:56:23 -0700207 asn_fprint(stderr, &asn_DEF_T, &t);
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700208 ASN_STRUCT_RESET(asn_DEF_T, &t);
Lev Walkinf15320b2004-06-03 03:38:44 +0000209
210 /* Check slightly more than buf1 */
211 check(&t, buf1, sizeof(buf1) + 10, sizeof(buf1));
212 compare(&t, buf1_reconstr, sizeof(buf1_reconstr));
Lev Walkin2a744a72013-03-27 01:56:23 -0700213 asn_fprint(stderr, &asn_DEF_T, &t);
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700214 ASN_STRUCT_RESET(asn_DEF_T, &t);
Lev Walkinf15320b2004-06-03 03:38:44 +0000215
216 /* Split the buffer in parts and check decoder restartability */
Lev Walkin2a744a72013-03-27 01:56:23 -0700217 partial_read(buf1, sizeof(buf1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000218
219 return 0;
220}