blob: 483229e89431c96b37e5cd4e7e07c0ae54f8889f [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 Walkinf15320b2004-06-03 03:38:44 +000093compare(T_t *tp, uint8_t *cmp_buf, int cmp_buf_size) {
Lev Walkina9cc46e2004-09-22 16:06:28 +000094 asn_enc_rval_t erval;
Lev Walkinf15320b2004-06-03 03:38:44 +000095 int i;
96
97 buf_size = cmp_buf_size + 100;
Lev Walkina4f8e942017-10-08 19:28:20 -070098 uint8_t scratch[buf_size];
99 buf = scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000100 buf_pos = 0;
101
102 /*
103 * Try to re-create using DER encoding.
104 */
Lev Walkinc7400c52004-09-29 13:14:36 +0000105 erval = der_encode(&asn_DEF_T, tp, buf_fill, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 assert(erval.encoded != -1);
107 if(erval.encoded != cmp_buf_size) {
Lev Walkin1715b322013-03-28 04:02:13 -0700108 printf("%zd != %zd\n", erval.encoded, cmp_buf_size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 }
110 assert(erval.encoded == cmp_buf_size);
111 for(i = 0; i < cmp_buf_size; i++) {
Lev Walkin2a744a72013-03-27 01:56:23 -0700112 if(buf[i] != cmp_buf[i]) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 fprintf(stderr, "Recreated buffer content mismatch:\n");
114 fprintf(stderr, "Byte %d, %x != %x (%d != %d)\n",
115 i,
Lev Walkin2a744a72013-03-27 01:56:23 -0700116 buf[i], cmp_buf[i],
117 buf[i], cmp_buf[i]
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 );
119 }
Lev Walkin2a744a72013-03-27 01:56:23 -0700120 assert(buf[i] == cmp_buf[i]);
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 }
Lev Walkina4f8e942017-10-08 19:28:20 -0700122
123 buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000124}
125
126static void
Lev Walkin2a744a72013-03-27 01:56:23 -0700127partial_read(uint8_t *buf_0, size_t size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000128 T_t t, *tp;
Lev Walkin90bf7ae2004-10-20 15:46:56 +0000129 asn_dec_rval_t rval;
Lev Walkina4f8e942017-10-08 19:28:20 -0700130 uint8_t buf_1[size];
131 uint8_t buf_2[size];
132 uint8_t buf_3[size];
Lev Walkinf15320b2004-06-03 03:38:44 +0000133
134 fprintf(stderr, "\nPartial read sequence...\n");
135
136 /*
137 * Divide the space (size) into three blocks in various combinations:
138 * |<----->i1<----->i2<----->|
Lev Walkin2a744a72013-03-27 01:56:23 -0700139 * ^ buf_0 ^ buf_0+size
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 * Try to read block by block.
141 */
Lev Walkina4f8e942017-10-08 19:28:20 -0700142 for(size_t i1 = 0; i1 < size; i1++) {
143 for(size_t i2 = i1; i2 < size; i2++) {
Lev Walkin2a744a72013-03-27 01:56:23 -0700144 uint8_t *chunk1 = buf_0;
Lev Walkin67c2aec2004-06-05 08:47:18 +0000145 size_t size1 = i1;
Lev Walkin2a744a72013-03-27 01:56:23 -0700146 uint8_t *chunk2 = buf_0 + size1;
Lev Walkin67c2aec2004-06-05 08:47:18 +0000147 size_t size2 = i2 - i1;
Lev Walkin2a744a72013-03-27 01:56:23 -0700148 uint8_t *chunk3 = buf_0 + size1 + size2;
Lev Walkin67c2aec2004-06-05 08:47:18 +0000149 size_t size3 = size - size1 - size2;
Lev Walkinf15320b2004-06-03 03:38:44 +0000150
Lev Walkin1715b322013-03-28 04:02:13 -0700151 fprintf(stderr, "\n%zd:{%zd, %zd, %zd}...\n",
152 size, size1, size2, size3);
Lev Walkinf15320b2004-06-03 03:38:44 +0000153
Lev Walkin2a744a72013-03-27 01:56:23 -0700154 memset(buf_1, 0, size);
155 memset(buf_2, 0, size);
156 memset(buf_3, 0, size);
157 memcpy(buf_1, chunk1, size1);
158 memcpy(buf_2, chunk2, size2);
159 memcpy(buf_3, chunk3, size3);
Lev Walkinf15320b2004-06-03 03:38:44 +0000160
161 tp = memset(&t, 0, sizeof(t));
162
Lev Walkin1715b322013-03-28 04:02:13 -0700163 fprintf(stderr, "=> Chunk 1 (%zd):\n", size1);
Lev Walkinc7400c52004-09-29 13:14:36 +0000164 rval = ber_decode(0, &asn_DEF_T, (void **)&tp,
Lev Walkin2a744a72013-03-27 01:56:23 -0700165 buf_1, size1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 assert(rval.code == RC_WMORE);
167 assert(rval.consumed <= size1);
168 if(rval.consumed < size1) {
169 int leftover = size1 - rval.consumed;
Lev Walkin2a744a72013-03-27 01:56:23 -0700170 memcpy(buf_2, buf_1 + rval.consumed, leftover);
171 memcpy(buf_2 + leftover, chunk2, size2);
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 size2 += leftover;
173 }
174
Lev Walkin1715b322013-03-28 04:02:13 -0700175 fprintf(stderr, "=> Chunk 2 (%zd):\n", size2);
Lev Walkinc7400c52004-09-29 13:14:36 +0000176 rval = ber_decode(0, &asn_DEF_T, (void **)&tp,
Lev Walkin2a744a72013-03-27 01:56:23 -0700177 buf_2, size2);
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 assert(rval.code == RC_WMORE);
179 assert(rval.consumed <= size2);
180 if(rval.consumed < size2) {
181 int leftover = size2 - rval.consumed;
Lev Walkin2a744a72013-03-27 01:56:23 -0700182 memcpy(buf_3, buf_2 + rval.consumed, leftover);
183 memcpy(buf_3 + leftover, chunk3, size3);
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 size3 += leftover;
185 }
186
Lev Walkin1715b322013-03-28 04:02:13 -0700187 fprintf(stderr, "=> Chunk 3 (%zd):\n", size3);
Lev Walkinc7400c52004-09-29 13:14:36 +0000188 rval = ber_decode(0, &asn_DEF_T, (void **)&tp,
Lev Walkin2a744a72013-03-27 01:56:23 -0700189 buf_3, size3);
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 assert(rval.code == RC_OK);
191 assert(rval.consumed == size3);
192
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700193 ASN_STRUCT_RESET(asn_DEF_T, &t);
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 }
195 }
196}
197
198int
Lev Walkin2a744a72013-03-27 01:56:23 -0700199main(int ac, char **av) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 T_t t;
201
Lev Walkin2a744a72013-03-27 01:56:23 -0700202 (void)ac; /* Unused argument */
203 (void)av; /* Unused argument */
Lev Walkinf15320b2004-06-03 03:38:44 +0000204
205 /* Check exact buf1 */
206 check(&t, buf1, sizeof(buf1), sizeof(buf1));
207 compare(&t, buf1_reconstr, sizeof(buf1_reconstr));
Lev Walkin2a744a72013-03-27 01:56:23 -0700208 asn_fprint(stderr, &asn_DEF_T, &t);
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700209 ASN_STRUCT_RESET(asn_DEF_T, &t);
Lev Walkinf15320b2004-06-03 03:38:44 +0000210
211 /* Check slightly more than buf1 */
212 check(&t, buf1, sizeof(buf1) + 10, sizeof(buf1));
213 compare(&t, buf1_reconstr, sizeof(buf1_reconstr));
Lev Walkin2a744a72013-03-27 01:56:23 -0700214 asn_fprint(stderr, &asn_DEF_T, &t);
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700215 ASN_STRUCT_RESET(asn_DEF_T, &t);
Lev Walkinf15320b2004-06-03 03:38:44 +0000216
217 /* Split the buffer in parts and check decoder restartability */
Lev Walkin2a744a72013-03-27 01:56:23 -0700218 partial_read(buf1, sizeof(buf1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000219
220 return 0;
221}