Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 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 { \ |
Lev Walkin | 96f9921 | 2017-08-29 23:38:31 -0700 | [diff] [blame] | 18 | asn_dec_rval_t rval; \ |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 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) |
Lev Walkin | 96f9921 | 2017-08-29 23:38:31 -0700 | [diff] [blame] | 42 | #undef SET_PHASE |
| 43 | #define SET_PHASE(ctx, value) \ |
| 44 | do { \ |
| 45 | ctx->phase = value; \ |
| 46 | ctx->step = 0; \ |
| 47 | } while(0) |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 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; |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 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 | |
| 125 | return 0; /* Want more */ |
| 126 | } |
| 127 | |
| 128 | |
| 129 | asn_dec_rval_t |
| 130 | CHOICE_decode_oer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 131 | const asn_oer_constraints_t *constraints, void **struct_ptr, |
| 132 | const void *ptr, size_t size) { |
| 133 | /* |
| 134 | * Bring closer parts of structure description. |
| 135 | */ |
| 136 | asn_CHOICE_specifics_t *specs = (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 | |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 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 = {tlv_tag, 0, 0, 0}; |
| 184 | |
| 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 Walkin | 96f9921 | 2017-08-29 23:38:31 -0700 | [diff] [blame] | 203 | ASN_DEBUG( |
| 204 | "Not implemented skipping open type extension for tag %s", |
| 205 | ber_tlv_tag_string(tlv_tag)); |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 206 | RETURN(RC_FAIL); |
| 207 | } |
| 208 | } while(0); |
| 209 | |
| 210 | |
| 211 | ADVANCE(tag_len); |
| 212 | } |
Lev Walkin | 96f9921 | 2017-08-29 23:38:31 -0700 | [diff] [blame] | 213 | /* Fall through */ |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 214 | 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 Walkin | 96f9921 | 2017-08-29 23:38:31 -0700 | [diff] [blame] | 218 | asn_dec_rval_t rval; |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 219 | |
| 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 Walkin | 0c54f3d | 2017-08-28 00:21:15 -0700 | [diff] [blame] | 240 | if(specs->ext_start >= 0 && specs->ext_start <= ctx->step) { |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 241 | /* We're in the extensions group. #20.2 requires Open Type */ |
Lev Walkin | 0c54f3d | 2017-08-28 00:21:15 -0700 | [diff] [blame] | 242 | ASN_DEBUG("Not implemented %s es=%d, edx=%u at %s:%d", td->name, |
| 243 | specs->ext_start, ctx->step, __FILE__, __LINE__); |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 244 | RETURN(RC_FAIL); |
| 245 | } |
| 246 | |
| 247 | rval = elm->type->op->oer_decoder(opt_codec_ctx, elm->type, |
| 248 | elm->oer_constraints, memb_ptr2, ptr, |
| 249 | size); |
| 250 | rval.consumed += consumed_myself; |
Lev Walkin | 96f9921 | 2017-08-29 23:38:31 -0700 | [diff] [blame] | 251 | 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 Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 259 | return rval; |
| 260 | } |
Lev Walkin | 96f9921 | 2017-08-29 23:38:31 -0700 | [diff] [blame] | 261 | case 2: |
| 262 | /* Already decoded everything */ |
| 263 | RETURN(RC_OK); |
| 264 | case 3: |
| 265 | /* Failed to decode, after all */ |
| 266 | RETURN(RC_FAIL); |
Lev Walkin | 9a1736d | 2017-08-27 23:46:34 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | RETURN(RC_FAIL); |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Encode as Canonical OER. |
| 274 | */ |
| 275 | asn_enc_rval_t |
| 276 | CHOICE_encode_oer(asn_TYPE_descriptor_t *td, |
| 277 | const asn_oer_constraints_t *constraints, void *sptr, |
| 278 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 279 | |
| 280 | (void)constraints; |
| 281 | (void)cb; |
| 282 | (void)app_key; |
| 283 | |
| 284 | ASN__ENCODE_FAILED; |
| 285 | } |
| 286 | |
| 287 | #endif /* ASN_DISABLE_OER_SUPPORT */ |