]> git.stg.codes - stg.git/blob - libs/smux/constr_CHOICE_oer.c
Regen SMUX support library with more recent ASN1 compiler.
[stg.git] / libs / smux / constr_CHOICE_oer.c
1 /*
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 {                                 \
18         asn_dec_rval_t rval;             \
19         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)
42 #undef  SET_PHASE
43 #define SET_PHASE(ctx, value) \
44     do {                      \
45         ctx->phase = value;   \
46         ctx->step = 0;        \
47     } while(0)
48
49 /*
50  * Tags are canonically sorted in the tag to member table.
51  */
52 static 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  */
80 static ssize_t
81 oer_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;
94         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
124     return 0; /* Want more */
125 }
126
127 asn_dec_rval_t
128 CHOICE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
129                   const asn_TYPE_descriptor_t *td,
130                   const asn_oer_constraints_t *constraints, void **struct_ptr,
131                   const void *ptr, size_t size) {
132     /*
133      * Bring closer parts of structure description.
134      */
135     const asn_CHOICE_specifics_t *specs =
136         (const asn_CHOICE_specifics_t *)td->specifics;
137     asn_TYPE_member_t *elements = td->elements;
138
139     /*
140      * Parts of the structure being constructed.
141      */
142     void *st = *struct_ptr; /* Target structure. */
143     asn_struct_ctx_t *ctx;  /* Decoder context */
144
145     ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
146
147     (void)constraints;
148
149     ASN_DEBUG("Decoding %s as CHOICE", td->name);
150
151     /*
152      * Create the target structure if it is not present already.
153      */
154     if(st == 0) {
155         st = *struct_ptr = CALLOC(1, specs->struct_size);
156         if(st == 0) {
157             RETURN(RC_FAIL);
158         }
159     }
160
161     /*
162      * Restore parsing context.
163      */
164     ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
165     switch(ctx->phase) {
166     case 0: {
167         /*
168          * Discover the tag.
169          */
170         ber_tlv_tag_t tlv_tag; /* T from TLV */
171         ssize_t tag_len;       /* Length of TLV's T */
172
173         tag_len = oer_fetch_tag(ptr, size, &tlv_tag);
174         switch(tag_len) {
175         case 0:
176             ASN__DECODE_STARVED;
177         case -1:
178             ASN__DECODE_FAILED;
179         }
180
181         do {
182             const asn_TYPE_tag2member_t *t2m;
183             asn_TYPE_tag2member_t key = {0, 0, 0, 0};
184             key.el_tag = tlv_tag;
185
186             t2m = (const asn_TYPE_tag2member_t *)bsearch(
187                 &key, specs->tag2el, specs->tag2el_count,
188                 sizeof(specs->tag2el[0]), _search4tag);
189             if(t2m) {
190                 /*
191                  * Found the element corresponding to the tag.
192                  */
193                 NEXT_PHASE(ctx);
194                 ctx->step = t2m->el_no;
195                 break;
196             } else if(specs->ext_start == -1) {
197                 ASN_DEBUG(
198                     "Unexpected tag %s "
199                     "in non-extensible CHOICE %s",
200                     ber_tlv_tag_string(tlv_tag), td->name);
201                 RETURN(RC_FAIL);
202             } else {
203                 /* Skip open type extension */
204                 ASN_DEBUG(
205                     "Not implemented skipping open type extension for tag %s",
206                     ber_tlv_tag_string(tlv_tag));
207                 RETURN(RC_FAIL);
208             }
209         } while(0);
210
211
212         ADVANCE(tag_len);
213     }
214         /* Fall through */
215     case 1: {
216         asn_TYPE_member_t *elm = &elements[ctx->step]; /* CHOICE's element */
217         void *memb_ptr;         /* Pointer to the member */
218         void **memb_ptr2;       /* Pointer to that pointer */
219         asn_dec_rval_t rval;
220
221         /*
222          * Compute the position of the member inside a structure,
223          * and also a type of containment (it may be contained
224          * as pointer or using inline inclusion).
225          */
226         if(elm->flags & ATF_POINTER) {
227             /* Member is a pointer to another structure */
228             memb_ptr2 = (void **)((char *)st + elm->memb_offset);
229         } else {
230             /*
231              * A pointer to a pointer
232              * holding the start of the structure
233              */
234             memb_ptr = (char *)st + elm->memb_offset;
235             memb_ptr2 = &memb_ptr;
236         }
237
238         /* Set presence to be able to free it properly at any time */
239         (void)CHOICE_variant_set_presence(td, st, ctx->step + 1);
240
241         if(specs->ext_start >= 0 && specs->ext_start <= ctx->step) {
242             ssize_t got =
243                 oer_open_type_get(opt_codec_ctx, elm->type,
244                                   elm->encoding_constraints.oer_constraints,
245                                   memb_ptr2, ptr, size);
246             if(got < 0) ASN__DECODE_FAILED;
247             if(got == 0) ASN__DECODE_STARVED;
248             rval.code = RC_OK;
249             rval.consumed = got;
250         } else {
251             rval = elm->type->op->oer_decoder(
252                 opt_codec_ctx, elm->type,
253                 elm->encoding_constraints.oer_constraints, memb_ptr2, ptr,
254                 size);
255         }
256         rval.consumed += consumed_myself;
257         switch(rval.code) {
258         case RC_OK:
259             NEXT_PHASE(ctx);
260         case RC_WMORE:
261             break;
262         case RC_FAIL:
263             SET_PHASE(ctx, 3);  /* => 3 */
264         }
265         return rval;
266     }
267     case 2:
268         /* Already decoded everything */
269         RETURN(RC_OK);
270     case 3:
271         /* Failed to decode, after all */
272         RETURN(RC_FAIL);
273     }
274
275     RETURN(RC_FAIL);
276 }
277
278 /*
279  * X.696 (08/2015) #8.7 Encoding of tags
280  */
281 static ssize_t
282 oer_put_tag(ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
283     uint8_t tclass = BER_TAG_CLASS(tag);
284     ber_tlv_tag_t tval = BER_TAG_VALUE(tag);
285
286     if(tval < 0x3F) {
287         uint8_t b = (uint8_t)((tclass << 6) | tval);
288         if(cb(&b, 1, app_key) < 0) {
289             return -1;
290         }
291         return 1;
292     } else {
293         uint8_t buf[1 + 2 * sizeof(tval)];
294         uint8_t *b = &buf[sizeof(buf)-1]; /* Last addressable */
295         size_t encoded;
296         for(; ; tval >>= 7) {
297             if(tval >> 7) {
298                 *b-- = 0x80 | (tval & 0x7f);
299             } else {
300                 *b-- = tval & 0x7f;
301                 break;
302             }
303         }
304         *b = (uint8_t)((tclass << 6) | 0x3F);
305         encoded = sizeof(buf) - (b - buf);
306         if(cb(b, encoded, app_key) < 0) {
307             return -1;
308         }
309         return encoded;
310     }
311
312 }
313
314 /*
315  * Encode as Canonical OER.
316  */
317 asn_enc_rval_t
318 CHOICE_encode_oer(const asn_TYPE_descriptor_t *td,
319                   const asn_oer_constraints_t *constraints, const void *sptr,
320                   asn_app_consume_bytes_f *cb, void *app_key) {
321     const asn_CHOICE_specifics_t *specs =
322         (const asn_CHOICE_specifics_t *)td->specifics;
323     asn_TYPE_member_t *elm; /* CHOICE element */
324     unsigned present;
325     const void *memb_ptr;
326     ber_tlv_tag_t tag;
327     ssize_t tag_len;
328     asn_enc_rval_t er = {0, 0, 0};
329
330     (void)constraints;
331
332     if(!sptr) ASN__ENCODE_FAILED;
333
334     ASN_DEBUG("OER %s encoding as CHOICE", td->name);
335
336     present = CHOICE_variant_get_presence(td, sptr);
337     if(present == 0 || present > td->elements_count) {
338         ASN_DEBUG("CHOICE %s member is not selected", td->name);
339         ASN__ENCODE_FAILED;
340     }
341
342     elm = &td->elements[present-1];
343     if(elm->flags & ATF_POINTER) {
344         memb_ptr =
345             *(const void *const *)((const char *)sptr + elm->memb_offset);
346         if(memb_ptr == 0) {
347             /* Mandatory element absent */
348             ASN__ENCODE_FAILED;
349         }
350     } else {
351         memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
352     }
353
354     tag = asn_TYPE_outmost_tag(elm->type, memb_ptr, elm->tag_mode, elm->tag);
355     if(tag == 0) {
356         ASN__ENCODE_FAILED;
357     }
358
359     tag_len = oer_put_tag(tag, cb, app_key);
360     if(tag_len < 0) {
361         ASN__ENCODE_FAILED;
362     }
363
364     if(specs->ext_start >= 0 && (unsigned)specs->ext_start <= (present-1)) {
365         ssize_t encoded = oer_open_type_put(elm->type,
366                                elm->encoding_constraints.oer_constraints,
367                                memb_ptr, cb, app_key);
368         if(encoded < 0) ASN__ENCODE_FAILED;
369         er.encoded = tag_len + encoded;
370     } else {
371         er = elm->type->op->oer_encoder(
372             elm->type, elm->encoding_constraints.oer_constraints, memb_ptr, cb,
373             app_key);
374         if(er.encoded >= 0) er.encoded += tag_len;
375     }
376
377     return er;
378 }
379
380 #endif  /* ASN_DISABLE_OER_SUPPORT */