blob: ca5674df5761ae58a933c8efca1b06a8f0baa452 [file] [log] [blame]
Lev Walkin9a1736d2017-08-27 23:46:34 -07001/*
2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
5 */
6#ifndef ASN_DISABLE_OER_SUPPORT
7
8#include <asn_internal.h>
9#include <constr_CHOICE.h>
10#include <errno.h>
11
12/*
13 * Return a standardized complex structure.
14 */
15#undef RETURN
16#define RETURN(_code) \
17 do { \
Lev Walkin96f99212017-08-29 23:38:31 -070018 asn_dec_rval_t rval; \
Lev Walkin9a1736d2017-08-27 23:46:34 -070019 rval.code = _code; \
20 rval.consumed = consumed_myself; \
21 return rval; \
22 } while(0)
23
24#undef ADVANCE
25#define ADVANCE(num_bytes) \
26 do { \
27 size_t num = num_bytes; \
28 ptr = ((const char *)ptr) + num; \
29 size -= num; \
30 consumed_myself += num; \
31 } while(0)
32
33/*
34 * Switch to the next phase of parsing.
35 */
36#undef NEXT_PHASE
37#define NEXT_PHASE(ctx) \
38 do { \
39 ctx->phase++; \
40 ctx->step = 0; \
41 } while(0)
Lev Walkin96f99212017-08-29 23:38:31 -070042#undef SET_PHASE
43#define SET_PHASE(ctx, value) \
44 do { \
45 ctx->phase = value; \
46 ctx->step = 0; \
47 } while(0)
Lev Walkin9a1736d2017-08-27 23:46:34 -070048
49/*
50 * Tags are canonically sorted in the tag to member table.
51 */
52static int
53_search4tag(const void *ap, const void *bp) {
54 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
55 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
56
57 int a_class = BER_TAG_CLASS(a->el_tag);
58 int b_class = BER_TAG_CLASS(b->el_tag);
59
60 if(a_class == b_class) {
61 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
62 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
63
64 if(a_value == b_value)
65 return 0;
66 else if(a_value < b_value)
67 return -1;
68 else
69 return 1;
70 } else if(a_class < b_class) {
71 return -1;
72 } else {
73 return 1;
74 }
75}
76
77/*
78 * X.696 (08/2015) #8.7 Encoding of tags
79 */
80static ssize_t
81oer_fetch_tag(const void *ptr, size_t size, ber_tlv_tag_t *tag_r) {
82 ber_tlv_tag_t val;
83 ber_tlv_tag_t tclass;
84 size_t skipped;
85
86 if(size == 0)
87 return 0;
88
89 val = *(const uint8_t *)ptr;
90 tclass = (val >> 6);
91 if((val & 0x3F) != 0x3F) {
92 /* #8.7.1 */
93 *tag_r = ((val & 0x3F) << 2) | tclass;
Lev Walkin9a1736d2017-08-27 23:46:34 -070094 return 1;
95 }
96
97 /*
98 * Each octet contains 7 bits of useful information.
99 * The MSB is 0 if it is the last octet of the tag.
100 */
101 for(val = 0, ptr = ((const char *)ptr) + 1, skipped = 2; skipped <= size;
102 ptr = ((const char *)ptr) + 1, skipped++) {
103 unsigned int oct = *(const uint8_t *)ptr;
104 if(oct & 0x80) {
105 val = (val << 7) | (oct & 0x7F);
106 /*
107 * Make sure there are at least 9 bits spare
108 * at the MS side of a value.
109 */
110 if(val >> ((8 * sizeof(val)) - 9)) {
111 /*
112 * We would not be able to accomodate
113 * any more tag bits.
114 */
115 return -1;
116 }
117 } else {
118 val = (val << 7) | oct;
119 *tag_r = (val << 2) | tclass;
120 return skipped;
121 }
122 }
123
Lev Walkin9a1736d2017-08-27 23:46:34 -0700124 return 0; /* Want more */
125}
126
Lev Walkin9a1736d2017-08-27 23:46:34 -0700127asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700128CHOICE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin9a1736d2017-08-27 23:46:34 -0700129 const asn_oer_constraints_t *constraints, void **struct_ptr,
130 const void *ptr, size_t size) {
131 /*
132 * Bring closer parts of structure description.
133 */
Lev Walkind84f6032017-10-03 16:33:59 -0700134 const asn_CHOICE_specifics_t *specs =
135 (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkin9a1736d2017-08-27 23:46:34 -0700136 asn_TYPE_member_t *elements = td->elements;
137
138 /*
139 * Parts of the structure being constructed.
140 */
141 void *st = *struct_ptr; /* Target structure. */
142 asn_struct_ctx_t *ctx; /* Decoder context */
143
Lev Walkin9a1736d2017-08-27 23:46:34 -0700144 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
145
146 (void)constraints;
147
148 ASN_DEBUG("Decoding %s as CHOICE", td->name);
149
150 /*
151 * Create the target structure if it is not present already.
152 */
153 if(st == 0) {
154 st = *struct_ptr = CALLOC(1, specs->struct_size);
155 if(st == 0) {
156 RETURN(RC_FAIL);
157 }
158 }
159
160 /*
161 * Restore parsing context.
162 */
163 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
164 switch(ctx->phase) {
165 case 0: {
166 /*
167 * Discover the tag.
168 */
169 ber_tlv_tag_t tlv_tag; /* T from TLV */
170 ssize_t tag_len; /* Length of TLV's T */
171
172 tag_len = oer_fetch_tag(ptr, size, &tlv_tag);
173 switch(tag_len) {
174 case 0:
175 ASN__DECODE_STARVED;
176 case -1:
177 ASN__DECODE_FAILED;
178 }
179
180 do {
181 const asn_TYPE_tag2member_t *t2m;
Lev Walkineff98a52017-09-13 22:24:35 +0000182 asn_TYPE_tag2member_t key = {0, 0, 0, 0};
183 key.el_tag = tlv_tag;
Lev Walkin9a1736d2017-08-27 23:46:34 -0700184
185 t2m = (const asn_TYPE_tag2member_t *)bsearch(
186 &key, specs->tag2el, specs->tag2el_count,
187 sizeof(specs->tag2el[0]), _search4tag);
188 if(t2m) {
189 /*
190 * Found the element corresponding to the tag.
191 */
192 NEXT_PHASE(ctx);
193 ctx->step = t2m->el_no;
194 break;
195 } else if(specs->ext_start == -1) {
196 ASN_DEBUG(
197 "Unexpected tag %s "
198 "in non-extensible CHOICE %s",
199 ber_tlv_tag_string(tlv_tag), td->name);
200 RETURN(RC_FAIL);
201 } else {
202 /* Skip open type extension */
Lev Walkin96f99212017-08-29 23:38:31 -0700203 ASN_DEBUG(
204 "Not implemented skipping open type extension for tag %s",
205 ber_tlv_tag_string(tlv_tag));
Lev Walkin9a1736d2017-08-27 23:46:34 -0700206 RETURN(RC_FAIL);
207 }
208 } while(0);
209
210
211 ADVANCE(tag_len);
212 }
Lev Walkin96f99212017-08-29 23:38:31 -0700213 /* Fall through */
Lev Walkin9a1736d2017-08-27 23:46:34 -0700214 case 1: {
215 asn_TYPE_member_t *elm = &elements[ctx->step]; /* CHOICE's element */
216 void *memb_ptr; /* Pointer to the member */
217 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkin96f99212017-08-29 23:38:31 -0700218 asn_dec_rval_t rval;
Lev Walkin9a1736d2017-08-27 23:46:34 -0700219
220 /*
221 * Compute the position of the member inside a structure,
222 * and also a type of containment (it may be contained
223 * as pointer or using inline inclusion).
224 */
225 if(elm->flags & ATF_POINTER) {
226 /* Member is a pointer to another structure */
227 memb_ptr2 = (void **)((char *)st + elm->memb_offset);
228 } else {
229 /*
230 * A pointer to a pointer
231 * holding the start of the structure
232 */
233 memb_ptr = (char *)st + elm->memb_offset;
234 memb_ptr2 = &memb_ptr;
235 }
236
237 /* Set presence to be able to free it properly at any time */
238 (void)CHOICE_variant_set_presence(td, st, ctx->step + 1);
239
Lev Walkin0c54f3d2017-08-28 00:21:15 -0700240 if(specs->ext_start >= 0 && specs->ext_start <= ctx->step) {
Lev Walkin9a1736d2017-08-27 23:46:34 -0700241 /* We're in the extensions group. #20.2 requires Open Type */
Lev Walkin0c54f3d2017-08-28 00:21:15 -0700242 ASN_DEBUG("Not implemented %s es=%d, edx=%u at %s:%d", td->name,
243 specs->ext_start, ctx->step, __FILE__, __LINE__);
Lev Walkin9a1736d2017-08-27 23:46:34 -0700244 RETURN(RC_FAIL);
245 }
246
247 rval = elm->type->op->oer_decoder(opt_codec_ctx, elm->type,
Lev Walkina5972be2017-09-29 23:15:58 -0700248 elm->encoding_constraints.oer_constraints, memb_ptr2, ptr,
Lev Walkin9a1736d2017-08-27 23:46:34 -0700249 size);
250 rval.consumed += consumed_myself;
Lev Walkin96f99212017-08-29 23:38:31 -0700251 switch(rval.code) {
252 case RC_OK:
253 NEXT_PHASE(ctx);
254 case RC_WMORE:
255 break;
256 case RC_FAIL:
257 SET_PHASE(ctx, 3); /* => 3 */
258 }
Lev Walkin9a1736d2017-08-27 23:46:34 -0700259 return rval;
260 }
Lev Walkin96f99212017-08-29 23:38:31 -0700261 case 2:
262 /* Already decoded everything */
263 RETURN(RC_OK);
264 case 3:
265 /* Failed to decode, after all */
266 RETURN(RC_FAIL);
Lev Walkin9a1736d2017-08-27 23:46:34 -0700267 }
268
269 RETURN(RC_FAIL);
270}
271
272/*
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700273 * X.696 (08/2015) #8.7 Encoding of tags
274 */
275static ssize_t
276oer_put_tag(ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
277 uint8_t tclass = BER_TAG_CLASS(tag);
278 ber_tlv_tag_t tval = BER_TAG_VALUE(tag);
279
280 if(tval < 0x3F) {
281 uint8_t b = (uint8_t)((tclass << 6) | tval);
282 if(cb(&b, 1, app_key) < 0) {
283 return -1;
284 }
285 return 1;
286 } else {
287 uint8_t buf[1 + 2 * sizeof(tval)];
288 uint8_t *b = &buf[sizeof(buf)-1]; /* Last addressable */
289 size_t encoded;
290 for(; ; tval >>= 7) {
291 if(tval >> 7) {
292 *b-- = 0x80 | (tval & 0x7f);
293 } else {
294 *b-- = tval & 0x7f;
295 break;
296 }
297 }
298 *b = (uint8_t)((tclass << 6) | 0x3F);
299 encoded = sizeof(buf) - (b - buf);
300 if(cb(b, encoded, app_key) < 0) {
301 return -1;
302 }
303 return encoded;
304 }
305
306}
307
308/*
Lev Walkin9a1736d2017-08-27 23:46:34 -0700309 * Encode as Canonical OER.
310 */
311asn_enc_rval_t
312CHOICE_encode_oer(asn_TYPE_descriptor_t *td,
313 const asn_oer_constraints_t *constraints, void *sptr,
314 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind84f6032017-10-03 16:33:59 -0700315 const asn_CHOICE_specifics_t *specs =
316 (const asn_CHOICE_specifics_t *)td->specifics;
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700317 asn_TYPE_member_t *elm; /* CHOICE element */
318 unsigned present;
319 void *memb_ptr;
320 ber_tlv_tag_t tag;
321 ssize_t tag_len;
322 asn_enc_rval_t er;
Lev Walkin9a1736d2017-08-27 23:46:34 -0700323
324 (void)constraints;
Lev Walkin71191ba2017-08-31 01:00:00 -0700325 (void)specs;
Lev Walkin9a1736d2017-08-27 23:46:34 -0700326
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700327 if(!sptr) ASN__ENCODE_FAILED;
328
329 ASN_DEBUG("OER %s encoding as CHOICE", td->name);
330
331 present = CHOICE_variant_get_presence(td, sptr);
332 if(present == 0 || present > td->elements_count) {
333 ASN_DEBUG("CHOICE %s member is not selected", td->name);
334 ASN__ENCODE_FAILED;
335 }
336
337 elm = &td->elements[present-1];
338 if(elm->flags & ATF_POINTER) {
339 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
340 if(memb_ptr == 0) {
341 /* Mandatory element absent */
342 ASN__ENCODE_FAILED;
343 }
344 } else {
345 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
346 }
347
348 tag = asn_TYPE_outmost_tag(elm->type, memb_ptr, elm->tag_mode, elm->tag);
349 if(tag == 0) {
350 ASN__ENCODE_FAILED;
351 }
352
353 tag_len = oer_put_tag(tag, cb, app_key);
354 if(tag_len < 0) {
355 ASN__ENCODE_FAILED;
356 }
357
Lev Walkina5972be2017-09-29 23:15:58 -0700358 er = elm->type->op->oer_encoder(elm->type, elm->encoding_constraints.oer_constraints, memb_ptr,
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700359 cb, app_key);
Lev Walkin7193cf02017-10-17 14:10:35 -0700360 if(er.encoded >= 0)
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700361 er.encoded += tag_len;
362
363 return er;
Lev Walkin9a1736d2017-08-27 23:46:34 -0700364}
365
366#endif /* ASN_DISABLE_OER_SUPPORT */