blob: 95d7101779ff379f63a0bc2b6b51fda2a7b546e9 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin99414dc2004-09-29 13:21:23 +00002 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <assert.h>
7
Lev Walkincc6a9102004-09-23 22:06:26 +00008#undef ADVANCE
Lev Walkin5ccf1eb2004-09-24 20:58:47 +00009#define ADVANCE(num_bytes) do { \
10 size_t num = num_bytes; \
11 ptr = ((char *)ptr) + num; \
12 size -= num; \
13 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000014 } while(0)
Lev Walkincc6a9102004-09-23 22:06:26 +000015#undef RETURN
Lev Walkin5ccf1eb2004-09-24 20:58:47 +000016#define RETURN(_code) do { \
17 ber_dec_rval_t rval; \
18 rval.code = _code; \
19 if(opt_ctx) opt_ctx->step = step; /* Save context */ \
20 if(_code == RC_OK || opt_ctx) \
21 rval.consumed = consumed_myself; \
22 else \
23 rval.consumed = 0; /* Context-free */ \
24 return rval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000025 } while(0)
26
27/*
28 * The BER decoder of any type.
29 */
30ber_dec_rval_t
Lev Walkin99414dc2004-09-29 13:21:23 +000031ber_decode(asn_codec_ctx_t *opt_codec_ctx,
32 asn_TYPE_descriptor_t *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000033 void **struct_ptr, void *ptr, size_t size) {
Lev Walkin99414dc2004-09-29 13:21:23 +000034 asn_codec_ctx_t s_codec_ctx;
35
36 /*
37 * Satisfy the requirement that the codec context
38 * must be allocated on the stack.
39 */
40 if(opt_codec_ctx && opt_codec_ctx->max_stack_size) {
41 s_codec_ctx = *opt_codec_ctx;
42 opt_codec_ctx = &s_codec_ctx;
43 }
Lev Walkinf15320b2004-06-03 03:38:44 +000044
45 /*
46 * Invoke type-specific decoder.
47 */
Lev Walkin99414dc2004-09-29 13:21:23 +000048 return type_descriptor->ber_decoder(opt_codec_ctx, type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000049 struct_ptr, /* Pointer to the destination structure */
50 ptr, size, /* Buffer and its size */
51 0 /* Default tag mode is 0 */
52 );
53}
54
55/*
56 * Check the set of <TL<TL<TL...>>> tags matches the definition.
57 */
58ber_dec_rval_t
Lev Walkin99414dc2004-09-29 13:21:23 +000059ber_check_tags(asn_codec_ctx_t *opt_codec_ctx,
60 asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +000061 void *ptr, size_t size, int tag_mode, int last_tag_form,
Lev Walkinf15320b2004-06-03 03:38:44 +000062 ber_tlv_len_t *last_length, int *opt_tlv_form) {
63 ssize_t consumed_myself = 0;
64 ssize_t tag_len;
65 ssize_t len_len;
66 ber_tlv_tag_t tlv_tag;
67 ber_tlv_len_t tlv_len;
68 ber_tlv_len_t limit_len = -1;
69 int expect_00_terminators = 0;
70 int tlv_constr = -1; /* If CHOICE, opt_tlv_form is not given */
Lev Walkin5ccf1eb2004-09-24 20:58:47 +000071 int step = opt_ctx ? opt_ctx->step : 0; /* Where we left previously */
Lev Walkinf15320b2004-06-03 03:38:44 +000072 int tagno;
73
Lev Walkin99414dc2004-09-29 13:21:23 +000074 if(opt_codec_ctx && opt_codec_ctx->max_stack_size) {
75 ptrdiff_t usedstack = ((char *)opt_codec_ctx - (char *)&size);
76 /* do not change the semantics:
77 * double negative is required to avoid int wrap-around */
78 if(usedstack > 0) usedstack = -usedstack;
79 ASN_DEBUG("Current stack size %ld", -(long)usedstack);
80 if(usedstack < -(ptrdiff_t)opt_codec_ctx->max_stack_size) {
81 ASN_DEBUG("Stack limit %ld reached",
82 (long)opt_codec_ctx->max_stack_size);
83 RETURN(RC_FAIL);
84 }
85 }
86
Lev Walkinf15320b2004-06-03 03:38:44 +000087 /*
Lev Walkin906654e2004-09-10 15:49:15 +000088 * So what does all this implicit skip stuff mean?
Lev Walkin26e22222004-06-06 07:59:35 +000089 * Imagine two types,
Lev Walkinf15320b2004-06-03 03:38:44 +000090 * A ::= [5] IMPLICIT T
91 * B ::= [2] EXPLICIT T
92 * Where T is defined as
93 * T ::= [4] IMPLICIT SEQUENCE { ... }
94 *
95 * Let's say, we are starting to decode type A, given the
96 * following TLV stream: <5> <0>. What does this mean?
97 * It means that the type A contains type T which is,
98 * in turn, empty.
99 * Remember though, that we are still in A. We cannot
100 * just pass control to the type T decoder. Why? Because
101 * the type T decoder expects <4> <0>, not <5> <0>.
102 * So, we must make sure we are going to receive <5> while
103 * still in A, then pass control to the T decoder, indicating
104 * that the tag <4> was implicitly skipped. The decoder of T
105 * hence will be prepared to treat <4> as valid tag, and decode
106 * it appropriately.
107 */
108
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000109 tagno = step /* Continuing where left previously */
Lev Walkinf15320b2004-06-03 03:38:44 +0000110 + (tag_mode==1?-1:0)
111 ;
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000112 ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000113 td->name, (long)size, tag_mode, step, tagno);
Lev Walkin99414dc2004-09-29 13:21:23 +0000114 /* assert(td->tags_count >= 1) May not be the case for CHOICE or ANY */
Lev Walkin7210fdb2004-09-04 04:44:30 +0000115
Lev Walkin906654e2004-09-10 15:49:15 +0000116 if(tag_mode == 0 && tagno == td->tags_count) {
Lev Walkin7210fdb2004-09-04 04:44:30 +0000117 /*
118 * This must be the _untagged_ ANY type,
119 * which outermost tag isn't known in advance.
120 * Fetch the tag and length separately.
121 */
122 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
123 switch(tag_len) {
124 case -1: RETURN(RC_FAIL);
125 case 0: RETURN(RC_WMORE);
126 }
127 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
128 len_len = ber_fetch_length(tlv_constr,
129 (char *)ptr + tag_len, size - tag_len, &tlv_len);
130 switch(len_len) {
131 case -1: RETURN(RC_FAIL);
132 case 0: RETURN(RC_WMORE);
133 }
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000134 ASN_DEBUG("Advancing %ld in ANY case",
135 (long)(tag_len + len_len));
Lev Walkincc6a9102004-09-23 22:06:26 +0000136 ADVANCE(tag_len + len_len);
Lev Walkin7210fdb2004-09-04 04:44:30 +0000137 } else {
138 assert(tagno < td->tags_count); /* At least one loop */
139 }
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000140 for((void)tagno; tagno < td->tags_count; tagno++, step++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000141
142 /*
143 * Fetch and process T from TLV.
144 */
145 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000146 ASN_DEBUG("Fetching tag from {%p,%ld} %02X..%02X: "
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000147 "len %ld, step %d, tag %s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 ptr, (long)size,
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000149 size?*(uint8_t *)ptr:0,
Lev Walkin9e12f2e2004-07-21 04:03:14 +0000150 ((size_t)tag_len<size&&tag_len>0)
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000151 ?*((uint8_t *)ptr + tag_len):0,
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000152 (long)tag_len, step,
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 ber_tlv_tag_string(tlv_tag));
154 switch(tag_len) {
155 case -1: RETURN(RC_FAIL);
156 case 0: RETURN(RC_WMORE);
157 }
158
159 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
160
161 /*
162 * If {I}, don't check anything.
163 * If {I,B,C}, check B and C unless we're at I.
164 */
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000165 if(tag_mode != 0 && step == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 /*
167 * We don't expect tag to match here.
168 * It's just because we don't know how the tag
169 * is supposed to look like.
170 */
171 } else {
172 assert(tagno >= 0); /* Guaranteed by the code above */
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000173 if(tlv_tag != td->tags[tagno]) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 /*
175 * Unexpected tag. Too bad.
176 */
177 ASN_DEBUG("Expected: %s, expectation failed",
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000178 ber_tlv_tag_string(td->tags[tagno]));
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 RETURN(RC_FAIL);
180 }
181 }
182
183 /*
184 * Attention: if there are more tags expected,
185 * ensure that the current tag is presented
186 * in constructed form (it contains other tags!).
187 * If this one is the last one, check that the tag form
188 * matches the one given in descriptor.
189 */
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000190 if(tagno < (td->tags_count - 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 if(tlv_constr == 0) {
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000192 ASN_DEBUG("tlv_constr = %d, expfail",
193 tlv_constr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 RETURN(RC_FAIL);
195 }
196 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000197 if(last_tag_form != tlv_constr
198 && last_tag_form != -1) {
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000199 ASN_DEBUG("last_tag_form %d != %d",
Lev Walkin8e8078a2004-09-26 13:10:40 +0000200 last_tag_form, tlv_constr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000201 RETURN(RC_FAIL);
202 }
203 }
204
205 /*
206 * Fetch and process L from TLV.
207 */
208 len_len = ber_fetch_length(tlv_constr,
Lev Walkin4d9528c2004-08-11 08:10:13 +0000209 (char *)ptr + tag_len, size - tag_len, &tlv_len);
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000210 ASN_DEBUG("Fetchinig len = %ld", (long)len_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000211 switch(len_len) {
212 case -1: RETURN(RC_FAIL);
213 case 0: RETURN(RC_WMORE);
214 }
215
216 /*
217 * FIXME
218 * As of today, the chain of tags
219 * must either contain several indefinite length TLVs,
220 * or several definite length ones.
221 * No mixing is allowed.
222 */
223 if(tlv_len == -1) {
224 /*
225 * Indefinite length.
226 */
227 if(limit_len == -1) {
228 expect_00_terminators++;
229 } else {
230 ASN_DEBUG("Unexpected indefinite length "
231 "in a chain of definite lengths");
232 RETURN(RC_FAIL);
233 }
234 ADVANCE(tag_len + len_len);
235 continue;
236 } else {
237 if(expect_00_terminators) {
238 ASN_DEBUG("Unexpected definite length "
239 "in a chain of indefinite lengths");
240 RETURN(RC_FAIL);
241 }
242 }
243
244 /*
245 * Check that multiple TLVs specify ever decreasing length,
246 * which is consistent.
247 */
248 if(limit_len == -1) {
249 limit_len = tlv_len + tag_len + len_len;
Lev Walkin99414dc2004-09-29 13:21:23 +0000250 if(limit_len < 0) {
251 /* Too great tlv_len value? */
252 RETURN(RC_FAIL);
253 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000254 } else if(limit_len != tlv_len + tag_len + len_len) {
255 /*
256 * Inner TLV specifies length which is inconsistent
257 * with the outer TLV's length value.
258 */
259 ASN_DEBUG("Outer TLV is %d and inner is %d",
260 limit_len, tlv_len);
261 RETURN(RC_FAIL);
262 }
263
264 ADVANCE(tag_len + len_len);
265
266 limit_len -= (tag_len + len_len);
Lev Walkind9bd7752004-06-05 08:17:50 +0000267 if((ssize_t)size > limit_len) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000268 /*
269 * Make sure that we won't consume more bytes
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000270 * from the parent frame than the inferred limit.
Lev Walkinf15320b2004-06-03 03:38:44 +0000271 */
272 size = limit_len;
273 }
274 }
275
276 if(opt_tlv_form)
277 *opt_tlv_form = tlv_constr;
278 if(expect_00_terminators)
279 *last_length = -expect_00_terminators;
280 else
281 *last_length = tlv_len;
282
283 RETURN(RC_OK);
284}