asn_dec_rval_t SET_OF_decode_aper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { asn_dec_rval_t rv; asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics; asn_TYPE_member_t *elm = td->elements; /* Single one */ void *st = *sptr; asn_anonymous_set_ *list; asn_per_constraint_t *ct; int repeat = 0; ssize_t nelems; if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) { ASN__DECODE_FAILED; } /* * Create the target structure if it is not present already. */ if(!st) { st = *sptr = CALLOC(1, specs->struct_size); if(!st) { ASN__DECODE_FAILED; } } list = _A_SET_FROM_VOID(st); /* Figure out which constraints to use */ if(constraints) { ct = &constraints->size; } else if(td->per_constraints) { ct = &td->per_constraints->size; } else { ct = 0; } if(ct && ct->flags & APC_EXTENSIBLE) { int value = aper_get_few_bits(pd, 1); if(value < 0) { ASN__DECODE_STARVED; } if(value) { ct = 0; /* Not restricted! */ } } if(ct && ct->effective_bits >= 0) { /* X.691, #19.5: No length determinant */ nelems = aper_get_few_bits(pd, ct->effective_bits); ASN_DEBUG("Preparing to fetch %ld+%ld elements from %s", (long)nelems, ct->lower_bound, td->name); if(nelems < 0) { ASN__DECODE_STARVED; } nelems += ct->lower_bound; } else { nelems = -1; } do { int i; if(nelems < 0) { nelems = aper_get_length(pd, ct ? ct->effective_bits : -1, &repeat); ASN_DEBUG("Got to decode %d elements (eff %d)", (int)nelems, (int)(ct ? ct->effective_bits : -1)); if(nelems < 0) { ASN__DECODE_STARVED; } } for(i = 0; i < nelems; i++) { void *ptr = 0; ASN_DEBUG("SET OF %s decoding", elm->type->name); rv = elm->type->aper_decoder(opt_codec_ctx, elm->type, elm->per_constraints, &ptr, pd); ASN_DEBUG("%s SET OF %s decoded %d, %p", td->name, elm->type->name, rv.code, ptr); if(rv.code == RC_OK) { if(ASN_SET_ADD(list, ptr) == 0) { continue; } ASN_DEBUG("Failed to add element into %s", td->name); /* Fall through */ rv.code = RC_FAIL; } else { ASN_DEBUG("Failed decoding %s of %s (SET OF)", elm->type->name, td->name); } if(ptr) { ASN_STRUCT_FREE(*elm->type, ptr); } return rv; } nelems = -1; /* Allow aper_get_length() */ } while(repeat); ASN_DEBUG("Decoded %s as SET OF", td->name); rv.code = RC_OK; rv.consumed = 0; return rv; }
asn_dec_rval_t CHOICE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics; asn_dec_rval_t rv; asn_per_constraint_t *ct; asn_TYPE_member_t *elm; /* CHOICE's element */ void *memb_ptr; void **memb_ptr2; void *st = *sptr; int value; if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) ASN__DECODE_FAILED; /* * Create the target structure if it is not present already. */ if(!st) { st = *sptr = CALLOC(1, specs->struct_size); if(!st) ASN__DECODE_FAILED; } if(constraints) ct = &constraints->value; else if(td->per_constraints) ct = &td->per_constraints->value; else ct = 0; if(ct && ct->flags & APC_EXTENSIBLE) { value = per_get_few_bits(pd, 1); if(value < 0) ASN__DECODE_STARVED; if(value) ct = 0; /* Not restricted */ } if(ct && ct->range_bits >= 0) { value = per_get_few_bits(pd, ct->range_bits); if(value < 0) ASN__DECODE_STARVED; ASN_DEBUG("CHOICE %s got index %d in range %d", td->name, value, ct->range_bits); if(value > ct->upper_bound) ASN__DECODE_FAILED; } else { if(specs->ext_start == -1) ASN__DECODE_FAILED; value = uper_get_nsnnwn(pd); if(value < 0) ASN__DECODE_STARVED; value += specs->ext_start; if(value >= td->elements_count) ASN__DECODE_FAILED; } /* Adjust if canonical order is different from natural order */ if(specs->canonical_order) value = specs->canonical_order[value]; /* Set presence to be able to free it later */ _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1); elm = &td->elements[value]; if(elm->flags & ATF_POINTER) { /* Member is a pointer to another structure */ memb_ptr2 = (void **)((char *)st + elm->memb_offset); } else { memb_ptr = (char *)st + elm->memb_offset; memb_ptr2 = &memb_ptr; } ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name); if(ct && ct->range_bits >= 0) { rv = elm->type->uper_decoder(opt_codec_ctx, elm->type, elm->per_constraints, memb_ptr2, pd); } else { rv = uper_open_type_get(opt_codec_ctx, elm->type, elm->per_constraints, memb_ptr2, pd); } if(rv.code != RC_OK) ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d", elm->name, td->name, rv.code); return rv; }
static asn_dec_rval_t uper_open_type_get_simple(const asn_codec_ctx_t *ctx, const asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { asn_dec_rval_t rv; ssize_t chunk_bytes; int repeat; uint8_t *buf = 0; size_t bufLen = 0; size_t bufSize = 0; asn_per_data_t spd; size_t padding; ASN__STACK_OVERFLOW_CHECK(ctx); ASN_DEBUG("Getting open type %s...", td->name); do { chunk_bytes = uper_get_length(pd, -1, 0, &repeat); if(chunk_bytes < 0) { FREEMEM(buf); ASN__DECODE_STARVED; } if(bufLen + chunk_bytes > bufSize) { void *ptr; bufSize = chunk_bytes + (bufSize << 2); ptr = REALLOC(buf, bufSize); if(!ptr) { FREEMEM(buf); ASN__DECODE_FAILED; } buf = ptr; } if(per_get_many_bits(pd, buf + bufLen, 0, chunk_bytes << 3)) { FREEMEM(buf); ASN__DECODE_STARVED; } bufLen += chunk_bytes; } while(repeat); ASN_DEBUG("Getting open type %s encoded in %ld bytes", td->name, (long)bufLen); memset(&spd, 0, sizeof(spd)); spd.buffer = buf; spd.nbits = bufLen << 3; ASN_DEBUG_INDENT_ADD(+4); rv = td->op->uper_decoder(ctx, td, constraints, sptr, &spd); ASN_DEBUG_INDENT_ADD(-4); if(rv.code == RC_OK) { /* Check padding validity */ padding = spd.nbits - spd.nboff; if ((padding < 8 || /* X.691#10.1.3 */ (spd.nboff == 0 && spd.nbits == 8 && spd.buffer == buf)) && per_get_few_bits(&spd, padding) == 0) { /* Everything is cool */ FREEMEM(buf); return rv; } FREEMEM(buf); if(padding >= 8) { ASN_DEBUG("Too large padding %d in open type", (int)padding); ASN__DECODE_FAILED; } else { ASN_DEBUG("Non-zero padding"); ASN__DECODE_FAILED; } } else { FREEMEM(buf); /* rv.code could be RC_WMORE, nonsense in this context */ rv.code = RC_FAIL; /* Noone would give us more */ } return rv; }
/* * Check the set of <TL<TL<TL...>>> tags matches the definition. */ asn_dec_rval_t ber_check_tags(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx, const void *ptr, size_t size, int tag_mode, int last_tag_form, ber_tlv_len_t *last_length, int *opt_tlv_form) { ssize_t consumed_myself = 0; ssize_t tag_len; ssize_t len_len; ber_tlv_tag_t tlv_tag; ber_tlv_len_t tlv_len; ber_tlv_len_t limit_len = -1; int expect_00_terminators = 0; int tlv_constr = -1; /* If CHOICE, opt_tlv_form is not given */ int step = opt_ctx ? opt_ctx->step : 0; /* Where we left previously */ int tagno; /* * Make sure we didn't exceed the maximum stack size. */ if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) RETURN(RC_FAIL); /* * So what does all this implicit skip stuff mean? * Imagine two types, * A ::= [5] IMPLICIT T * B ::= [2] EXPLICIT T * Where T is defined as * T ::= [4] IMPLICIT SEQUENCE { ... } * * Let's say, we are starting to decode type A, given the * following TLV stream: <5> <0>. What does this mean? * It means that the type A contains type T which is, * in turn, empty. * Remember though, that we are still in A. We cannot * just pass control to the type T decoder. Why? Because * the type T decoder expects <4> <0>, not <5> <0>. * So, we must make sure we are going to receive <5> while * still in A, then pass control to the T decoder, indicating * that the tag <4> was implicitly skipped. The decoder of T * hence will be prepared to treat <4> as valid tag, and decode * it appropriately. */ tagno = step /* Continuing where left previously */ + (tag_mode==1?-1:0) ; ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)", td->name, (long)size, tag_mode, step, tagno); /* assert(td->tags_count >= 1) May not be the case for CHOICE or ANY */ if(tag_mode == 0 && tagno == (int)td->tags_count) { /* * This must be the _untagged_ ANY type, * which outermost tag isn't known in advance. * Fetch the tag and length separately. */ tag_len = ber_fetch_tag(ptr, size, &tlv_tag); switch(tag_len) { case -1: RETURN(RC_FAIL); case 0: RETURN(RC_WMORE); } tlv_constr = BER_TLV_CONSTRUCTED(ptr); len_len = ber_fetch_length(tlv_constr, (const char *)ptr + tag_len, size - tag_len, &tlv_len); switch(len_len) { case -1: RETURN(RC_FAIL); case 0: RETURN(RC_WMORE); } ASN_DEBUG("Advancing %ld in ANY case", (long)(tag_len + len_len)); ADVANCE(tag_len + len_len); } else { assert(tagno < (int)td->tags_count); /* At least one loop */ } for((void)tagno; tagno < (int)td->tags_count; tagno++, step++) { /* * Fetch and process T from TLV. */ tag_len = ber_fetch_tag(ptr, size, &tlv_tag); ASN_DEBUG("Fetching tag from {%p,%ld}: " "len %ld, step %d, tagno %d got %s", ptr, (long)size, (long)tag_len, step, tagno, ber_tlv_tag_string(tlv_tag)); switch(tag_len) { case -1: RETURN(RC_FAIL); case 0: RETURN(RC_WMORE); } tlv_constr = BER_TLV_CONSTRUCTED(ptr); /* * If {I}, don't check anything. * If {I,B,C}, check B and C unless we're at I. */ if(tag_mode != 0 && step == 0) { /* * We don't expect tag to match here. * It's just because we don't know how the tag * is supposed to look like. */ } else { assert(tagno >= 0); /* Guaranteed by the code above */ if(tlv_tag != td->tags[tagno]) { /* * Unexpected tag. Too bad. */ ASN_DEBUG("Expected: %s, " "expectation failed (tn=%d, tm=%d)", ber_tlv_tag_string(td->tags[tagno]), tagno, tag_mode ); RETURN(RC_FAIL); } } /* * Attention: if there are more tags expected, * ensure that the current tag is presented * in constructed form (it contains other tags!). * If this one is the last one, check that the tag form * matches the one given in descriptor. */ if(tagno < ((int)td->tags_count - 1)) { if(tlv_constr == 0) { ASN_DEBUG("tlv_constr = %d, expfail", tlv_constr); RETURN(RC_FAIL); } } else { if(last_tag_form != tlv_constr && last_tag_form != -1) { ASN_DEBUG("last_tag_form %d != %d", last_tag_form, tlv_constr); RETURN(RC_FAIL); } } /* * Fetch and process L from TLV. */ len_len = ber_fetch_length(tlv_constr, (const char *)ptr + tag_len, size - tag_len, &tlv_len); ASN_DEBUG("Fetching len = %ld", (long)len_len); switch(len_len) { case -1: RETURN(RC_FAIL); case 0: RETURN(RC_WMORE); } /* * FIXME * As of today, the chain of tags * must either contain several indefinite length TLVs, * or several definite length ones. * No mixing is allowed. */ if(tlv_len == -1) { /* * Indefinite length. */ if(limit_len == -1) { expect_00_terminators++; } else { ASN_DEBUG("Unexpected indefinite length " "in a chain of definite lengths"); RETURN(RC_FAIL); } ADVANCE(tag_len + len_len); continue; } else { if(expect_00_terminators) { ASN_DEBUG("Unexpected definite length " "in a chain of indefinite lengths"); RETURN(RC_FAIL); } } /* * Check that multiple TLVs specify ever decreasing length, * which is consistent. */ if(limit_len == -1) { limit_len = tlv_len + tag_len + len_len; if(limit_len < 0) { /* Too great tlv_len value? */ RETURN(RC_FAIL); } } else if(limit_len != tlv_len + tag_len + len_len) { /* * Inner TLV specifies length which is inconsistent * with the outer TLV's length value. */ ASN_DEBUG("Outer TLV is %ld and inner is %ld", (long)limit_len, (long)tlv_len); RETURN(RC_FAIL); } ADVANCE(tag_len + len_len); limit_len -= (tag_len + len_len); if((ssize_t)size > limit_len) { /* * Make sure that we won't consume more bytes * from the parent frame than the inferred limit. */ size = limit_len; } } if(opt_tlv_form) *opt_tlv_form = tlv_constr; if(expect_00_terminators) *last_length = -expect_00_terminators; else *last_length = tlv_len; RETURN(RC_OK); }
static asn_dec_rval_t CC_NOTUSED uper_open_type_get_complex(const asn_codec_ctx_t *ctx, const asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { uper_ugot_key arg; asn_dec_rval_t rv; ssize_t padding; ASN__STACK_OVERFLOW_CHECK(ctx); ASN_DEBUG("Getting open type %s from %s", td->name, asn_bit_data_string(pd)); arg.oldpd = *pd; arg.unclaimed = 0; arg.ot_moved = 0; arg.repeat = 1; pd->refill = uper_ugot_refill; pd->refill_key = &arg; pd->nbits = pd->nboff; /* 0 good bits at this point, will refill */ pd->moved = 0; /* This now counts the open type size in bits */ ASN_DEBUG_INDENT_ADD(+4); rv = td->op->uper_decoder(ctx, td, constraints, sptr, pd); ASN_DEBUG_INDENT_ADD(-4); #define UPDRESTOREPD do { \ /* buffer and nboff are valid, preserve them. */ \ pd->nbits = arg.oldpd.nbits - (pd->moved - arg.ot_moved); \ pd->moved = arg.oldpd.moved + (pd->moved - arg.ot_moved); \ pd->refill = arg.oldpd.refill; \ pd->refill_key = arg.oldpd.refill_key; \ } while(0) if(rv.code != RC_OK) { UPDRESTOREPD; return rv; } ASN_DEBUG("OpenType %s pd%s old%s unclaimed=%d, repeat=%d", td->name, asn_bit_data_string(pd), asn_bit_data_string(&arg.oldpd), (int)arg.unclaimed, (int)arg.repeat); padding = pd->moved % 8; if(padding) { int32_t pvalue; if(padding > 7) { ASN_DEBUG("Too large padding %d in open type", (int)padding); rv.code = RC_FAIL; UPDRESTOREPD; return rv; } padding = 8 - padding; ASN_DEBUG("Getting padding of %d bits", (int)padding); pvalue = per_get_few_bits(pd, padding); switch(pvalue) { case -1: ASN_DEBUG("Padding skip failed"); UPDRESTOREPD; ASN__DECODE_STARVED; case 0: break; default: ASN_DEBUG("Non-blank padding (%d bits 0x%02x)", (int)padding, (int)pvalue); UPDRESTOREPD; ASN__DECODE_FAILED; } } if(pd->nboff != pd->nbits) { ASN_DEBUG("Open type %s overhead pd%s old%s", td->name, asn_bit_data_string(pd), asn_bit_data_string(&arg.oldpd)); if(1) { UPDRESTOREPD; ASN__DECODE_FAILED; } else { arg.unclaimed += pd->nbits - pd->nboff; } } /* Adjust pd back so it points to original data */ UPDRESTOREPD; /* Skip data not consumed by the decoder */ if(arg.unclaimed) { ASN_DEBUG("Getting unclaimed %d", (int)arg.unclaimed); switch(per_skip_bits(pd, arg.unclaimed)) { case -1: ASN_DEBUG("Claim of %d failed", (int)arg.unclaimed); ASN__DECODE_STARVED; case 0: ASN_DEBUG("Got claim of %d", (int)arg.unclaimed); break; default: /* Padding must be blank */ ASN_DEBUG("Non-blank unconsumed padding"); ASN__DECODE_FAILED; } arg.unclaimed = 0; } if(arg.repeat) { ASN_DEBUG("Not consumed the whole thing"); rv.code = RC_FAIL; return rv; } return rv; }