Lev Walkin | c6bd359 | 2017-08-30 17:36:23 -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_SET_OF.h> |
| 10 | #include <asn_SET_OF.h> |
| 11 | #include <errno.h> |
| 12 | |
| 13 | /* |
| 14 | * This macro "eats" the part of the buffer which is definitely "consumed", |
| 15 | * i.e. was correctly converted into local representation or rightfully skipped. |
| 16 | */ |
| 17 | #undef ADVANCE |
| 18 | #define ADVANCE(num_bytes) \ |
| 19 | do { \ |
| 20 | size_t num = num_bytes; \ |
| 21 | ptr = ((const char *)ptr) + num; \ |
| 22 | size -= num; \ |
| 23 | consumed_myself += num; \ |
| 24 | } while(0) |
| 25 | |
| 26 | /* |
| 27 | * Switch to the next phase of parsing. |
| 28 | */ |
| 29 | #undef NEXT_PHASE |
| 30 | #define NEXT_PHASE(ctx) \ |
| 31 | do { \ |
| 32 | ctx->phase++; \ |
| 33 | ctx->step = 0; \ |
| 34 | } while(0) |
| 35 | #undef SET_PHASE |
| 36 | #define SET_PHASE(ctx, value) \ |
| 37 | do { \ |
| 38 | ctx->phase = value; \ |
| 39 | ctx->step = 0; \ |
| 40 | } while(0) |
| 41 | |
| 42 | /* |
| 43 | * Return a standardized complex structure. |
| 44 | */ |
| 45 | #undef RETURN |
| 46 | #define RETURN(_code) \ |
| 47 | do { \ |
| 48 | asn_dec_rval_t rval; \ |
| 49 | rval.code = _code; \ |
| 50 | rval.consumed = consumed_myself; \ |
| 51 | return rval; \ |
| 52 | } while(0) |
| 53 | |
| 54 | /* |
| 55 | * The SEQUENCE OF and SET OF values utilize a "quantity field". |
| 56 | * It is is a pointless combination of #8.6 (length determinant, capable |
| 57 | * of encoding tiny and huge numbers in the shortest possible number of octets) |
| 58 | * and the variable sized integer. What could have been encoded by #8.6 alone |
| 59 | * is required to be encoded by #8.6 followed by that number of unsigned octets. |
| 60 | * This doesn't make too much sense. It seems that the original version of OER |
| 61 | * standard have been using the unconstrained unsigned integer as a quantity |
| 62 | * field, and this legacy have gone through ISO/ITU-T standardization process. |
| 63 | */ |
| 64 | static ssize_t |
| 65 | oer_fetch_quantity(const void *ptr, size_t size, size_t *qty_r) { |
| 66 | size_t len = 0; |
| 67 | size_t qty; |
| 68 | |
| 69 | ssize_t len_len = oer_fetch_length(ptr, size, &len); |
| 70 | if(len_len <= 0) { |
| 71 | *qty_r = 0; |
| 72 | return len_len; |
| 73 | } |
| 74 | |
| 75 | if((len_len + len) > size) { |
| 76 | *qty_r = 0; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | const uint8_t *b = (const uint8_t *)ptr + len_len; |
| 81 | const uint8_t *bend = b + len_len; |
| 82 | |
| 83 | |
| 84 | /* Skip the leading 0-bytes */ |
| 85 | for(; b < bend && *b == 0; b++) { |
| 86 | } |
| 87 | |
| 88 | |
| 89 | if((bend - b) > (ssize_t)sizeof(size_t)) { |
| 90 | /* Length is not representable by the native size_t type */ |
| 91 | *qty_r = 0; |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | for(qty = 0; b < bend; b++) { |
| 96 | qty = (qty << 8) + *b; |
| 97 | } |
| 98 | |
| 99 | if(qty > RSIZE_MAX) { /* A bit of C11 validation */ |
| 100 | *qty_r = 0; |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | *qty_r = qty; |
| 105 | assert((size_t)len_len + len == (size_t)(bend - (const uint8_t *)ptr)); |
| 106 | return len_len + len; |
| 107 | } |
| 108 | |
| 109 | asn_dec_rval_t |
| 110 | SET_OF_decode_oer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 111 | const asn_oer_constraints_t *constraints, void **struct_ptr, |
| 112 | const void *ptr, size_t size) { |
| 113 | asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics; |
| 114 | asn_dec_rval_t rval = {RC_OK, 0}; |
| 115 | void *st = *struct_ptr; /* Target structure */ |
| 116 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 117 | size_t consumed_myself = 0; /* Consumed bytes from ptr. */ |
| 118 | |
| 119 | (void)opt_codec_ctx; |
| 120 | (void)constraints; |
| 121 | |
| 122 | /* |
| 123 | * Create the target structure if it is not present already. |
| 124 | */ |
| 125 | if(st == 0) { |
| 126 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 127 | if(st == 0) { |
| 128 | RETURN(RC_FAIL); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Restore parsing context. |
| 134 | */ |
| 135 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
| 136 | |
| 137 | /* |
| 138 | * Start to parse where left previously. |
| 139 | */ |
| 140 | switch(ctx->phase) { |
| 141 | case 0: { |
| 142 | /* |
| 143 | * Fetch number of elements to decode. |
| 144 | */ |
| 145 | size_t length = 0; |
| 146 | size_t len_size = oer_fetch_quantity(ptr, size, &length); |
| 147 | switch(len_size) { |
| 148 | case 0: |
| 149 | RETURN(RC_WMORE); |
| 150 | case -1: |
| 151 | RETURN(RC_FAIL); |
| 152 | default: |
| 153 | ASN_DEBUG("ptr[] = %02x, advancing %zu, length=%zu", *(const uint8_t *)ptr, len_size, length); |
| 154 | ADVANCE(len_size); |
| 155 | ctx->left = length; |
| 156 | } |
| 157 | } |
| 158 | NEXT_PHASE(ctx); |
| 159 | /* FALL THROUGH */ |
| 160 | case 1: { |
| 161 | /* Decode components of the extension root */ |
| 162 | asn_TYPE_member_t *elm = td->elements; |
| 163 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(st); |
| 164 | |
| 165 | assert(td->elements_count == 1); |
| 166 | |
| 167 | ASN_DEBUG("OER SET OF %s Decoding PHASE 1", td->name); |
| 168 | |
| 169 | for(; ctx->left > 0; ctx->left--) { |
| 170 | asn_dec_rval_t rv = elm->type->op->oer_decoder( |
| 171 | opt_codec_ctx, elm->type, elm->oer_constraints, &ctx->ptr, ptr, |
| 172 | size); |
| 173 | ADVANCE(rv.consumed); |
| 174 | switch(rv.code) { |
| 175 | case RC_OK: |
| 176 | if(ASN_SET_ADD(list, ctx->ptr) != 0) { |
| 177 | RETURN(RC_FAIL); |
| 178 | } else { |
| 179 | ctx->ptr = 0; |
| 180 | break; |
| 181 | } |
| 182 | case RC_WMORE: |
| 183 | RETURN(RC_WMORE); |
| 184 | case RC_FAIL: |
| 185 | ASN_STRUCT_FREE(*elm->type, ctx->ptr); |
| 186 | ctx->ptr = 0; |
| 187 | SET_PHASE(ctx, 3); |
| 188 | RETURN(RC_FAIL); |
| 189 | } |
| 190 | } |
| 191 | /* Decoded decently. */ |
| 192 | NEXT_PHASE(ctx); |
| 193 | } |
| 194 | /* Fall through */ |
| 195 | case 2: |
| 196 | /* Ignore fully decoded */ |
| 197 | assert(ctx->left == 0); |
| 198 | RETURN(RC_OK); |
| 199 | case 3: |
| 200 | /* Failed to decode. */ |
| 201 | RETURN(RC_FAIL); |
| 202 | } |
| 203 | |
| 204 | return rval; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Encode as Canonical OER. |
| 209 | */ |
| 210 | asn_enc_rval_t |
| 211 | SET_OF_encode_oer(asn_TYPE_descriptor_t *td, |
| 212 | const asn_oer_constraints_t *constraints, void *sptr, |
| 213 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 214 | size_t computed_size = 0; |
| 215 | |
| 216 | (void)constraints; |
| 217 | (void)cb; |
| 218 | (void)app_key; |
| 219 | |
| 220 | { |
| 221 | asn_enc_rval_t er = {computed_size, td, sptr}; |
| 222 | ASN__ENCODED_OK(er); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | #endif /* ASN_DISABLE_OER_SUPPORT */ |