加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
constr_CHOICE.c 28.05 KB
一键复制 编辑 原始数据 按行查看 历史
eomer 提交于 2019-11-12 10:34 . init repo
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
/*
* Copyright (c) 2003, 2004, 2005, 2006, 2007 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <constr_CHOICE.h>
#include <per_opentype.h>
/*
* Number of bytes left for this structure.
* (ctx->left) indicates the number of bytes _transferred_ for the structure.
* (size) contains the number of bytes in the buffer passed.
*/
#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
/*
* If the subprocessor function returns with an indication that it wants
* more data, it may well be a fatal decoding problem, because the
* size is constrained by the <TLV>'s L, even if the buffer size allows
* reading more data.
* For example, consider the buffer containing the following TLVs:
* <T:5><L:1><V> <T:6>...
* The TLV length clearly indicates that one byte is expected in V, but
* if the V processor returns with "want more data" even if the buffer
* contains way more data than the V processor have seen.
*/
#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
/*
* This macro "eats" the part of the buffer which is definitely "consumed",
* i.e. was correctly converted into local representation or rightfully skipped.
*/
#undef ADVANCE
#define ADVANCE(num_bytes) do { \
size_t num = num_bytes; \
ptr = ((const char *)ptr) + num;\
size -= num; \
if(ctx->left >= 0) \
ctx->left -= num; \
consumed_myself += num; \
} while(0)
/*
* Switch to the next phase of parsing.
*/
#undef NEXT_PHASE
#define NEXT_PHASE(ctx) do { \
ctx->phase++; \
ctx->step = 0; \
} while(0)
/*
* Return a standardized complex structure.
*/
#undef RETURN
#define RETURN(_code) do { \
rval.code = _code; \
rval.consumed = consumed_myself;\
return rval; \
} while(0)
/*
* See the definitions.
*/
static int _fetch_present_idx(const void *struct_ptr, int off, int size);
static void _set_present_idx(void *sptr, int offset, int size, int pres);
/*
* Tags are canonically sorted in the tag to member table.
*/
static int
_search4tag(const void *ap, const void *bp) {
const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
int a_class = BER_TAG_CLASS(a->el_tag);
int b_class = BER_TAG_CLASS(b->el_tag);
if(a_class == b_class) {
ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
if(a_value == b_value)
return 0;
else if(a_value < b_value)
return -1;
else
return 1;
} else if(a_class < b_class) {
return -1;
} else {
return 1;
}
}
/*
* The decoder of the CHOICE type.
*/
asn_dec_rval_t
CHOICE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
/*
* Bring closer parts of structure description.
*/
asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
asn_TYPE_member_t *elements = td->elements;
/*
* Parts of the structure being constructed.
*/
void *st = *struct_ptr; /* Target structure. */
asn_struct_ctx_t *ctx; /* Decoder context */
ber_tlv_tag_t tlv_tag; /* T from TLV */
ssize_t tag_len; /* Length of TLV's T */
asn_dec_rval_t rval; /* Return code from subparsers */
ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
ASN_DEBUG("Decoding %s as CHOICE", td->name);
/*
* Create the target structure if it is not present already.
*/
if(st == 0) {
st = *struct_ptr = CALLOC(1, specs->struct_size);
if(st == 0) {
RETURN(RC_FAIL);
}
}
/*
* Restore parsing context.
*/
ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
/*
* Start to parse where left previously
*/
switch(ctx->phase) {
case 0:
/*
* PHASE 0.
* Check that the set of tags associated with given structure
* perfectly fits our expectations.
*/
if(tag_mode || td->tags_count) {
rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
tag_mode, -1, &ctx->left, 0);
if(rval.code != RC_OK) {
ASN_DEBUG("%s tagging check failed: %d",
td->name, rval.code);
return rval;
}
if(ctx->left >= 0) {
/* ?Substracted below! */
ctx->left += rval.consumed;
}
ADVANCE(rval.consumed);
} else {
ctx->left = -1;
}
NEXT_PHASE(ctx);
ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
(long)ctx->left, (long)size);
/* Fall through */
case 1:
/*
* Fetch the T from TLV.
*/
tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
switch(tag_len) {
case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
/* Fall through */
case -1: RETURN(RC_FAIL);
}
do {
const asn_TYPE_tag2member_t *t2m;
asn_TYPE_tag2member_t key;
key.el_tag = tlv_tag;
t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
specs->tag2el, specs->tag2el_count,
sizeof(specs->tag2el[0]), _search4tag);
if(t2m) {
/*
* Found the element corresponding to the tag.
*/
NEXT_PHASE(ctx);
ctx->step = t2m->el_no;
break;
} else if(specs->ext_start == -1) {
ASN_DEBUG("Unexpected tag %s "
"in non-extensible CHOICE %s",
ber_tlv_tag_string(tlv_tag), td->name);
RETURN(RC_FAIL);
} else {
/* Skip this tag */
ssize_t skip;
ASN_DEBUG("Skipping unknown tag %s",
ber_tlv_tag_string(tlv_tag));
skip = ber_skip_length(opt_codec_ctx,
BER_TLV_CONSTRUCTED(ptr),
(const char *)ptr + tag_len,
LEFT - tag_len);
switch(skip) {
case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
/* Fall through */
case -1: RETURN(RC_FAIL);
}
ADVANCE(skip + tag_len);
RETURN(RC_OK);
}
} while(0);
case 2:
/*
* PHASE 2.
* Read in the element.
*/
do {
asn_TYPE_member_t *elm;/* CHOICE's element */
void *memb_ptr; /* Pointer to the member */
void **memb_ptr2; /* Pointer to that pointer */
elm = &elements[ctx->step];
/*
* Compute the position of the member inside a structure,
* and also a type of containment (it may be contained
* as pointer or using inline inclusion).
*/
if(elm->flags & ATF_POINTER) {
/* Member is a pointer to another structure */
memb_ptr2 = (void **)((char *)st + elm->memb_offset);
} else {
/*
* A pointer to a pointer
* holding the start of the structure
*/
memb_ptr = (char *)st + elm->memb_offset;
memb_ptr2 = &memb_ptr;
}
/* Set presence to be able to free it properly at any time */
_set_present_idx(st, specs->pres_offset,
specs->pres_size, ctx->step + 1);
/*
* Invoke the member fetch routine according to member's type
*/
rval = elm->type->ber_decoder(opt_codec_ctx, elm->type,
memb_ptr2, ptr, LEFT, elm->tag_mode);
switch(rval.code) {
case RC_OK:
break;
case RC_WMORE: /* More data expected */
if(!SIZE_VIOLATION) {
ADVANCE(rval.consumed);
RETURN(RC_WMORE);
}
RETURN(RC_FAIL);
case RC_FAIL: /* Fatal error */
RETURN(rval.code);
} /* switch(rval) */
ADVANCE(rval.consumed);
} while(0);
NEXT_PHASE(ctx);
/* Fall through */
case 3:
ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
td->name, (long)ctx->left, (long)size,
tag_mode, td->tags_count);
if(ctx->left > 0) {
/*
* The type must be fully decoded
* by the CHOICE member-specific decoder.
*/
RETURN(RC_FAIL);
}
if(ctx->left == -1
&& !(tag_mode || td->tags_count)) {
/*
* This is an untagged CHOICE.
* It doesn't contain nothing
* except for the member itself, including all its tags.
* The decoding is completed.
*/
NEXT_PHASE(ctx);
break;
}
/*
* Read in the "end of data chunks"'s.
*/
while(ctx->left < 0) {
ssize_t tl;
tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
switch(tl) {
case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
/* Fall through */
case -1: RETURN(RC_FAIL);
}
/*
* Expected <0><0>...
*/
if(((const uint8_t *)ptr)[0] == 0) {
if(LEFT < 2) {
if(SIZE_VIOLATION)
RETURN(RC_FAIL);
else
RETURN(RC_WMORE);
} else if(((const uint8_t *)ptr)[1] == 0) {
/*
* Correctly finished with <0><0>.
*/
ADVANCE(2);
ctx->left++;
continue;
}
} else {
ASN_DEBUG("Unexpected continuation in %s",
td->name);
RETURN(RC_FAIL);
}
/* UNREACHABLE */
}
NEXT_PHASE(ctx);
case 4:
/* No meaningful work here */
break;
}
RETURN(RC_OK);
}
asn_enc_rval_t
CHOICE_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
int tag_mode, ber_tlv_tag_t tag,
asn_app_consume_bytes_f *cb, void *app_key) {
asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
asn_TYPE_member_t *elm; /* CHOICE element */
asn_enc_rval_t erval;
void *memb_ptr;
size_t computed_size = 0;
int present;
if(!sptr) ASN__ENCODE_FAILED;
ASN_DEBUG("%s %s as CHOICE",
cb?"Encoding":"Estimating", td->name);
present = _fetch_present_idx(sptr,
specs->pres_offset, specs->pres_size);
/*
* If the structure was not initialized, it cannot be encoded:
* can't deduce what to encode in the choice type.
*/
if(present <= 0 || present > td->elements_count) {
if(present == 0 && td->elements_count == 0) {
/* The CHOICE is empty?! */
erval.encoded = 0;
ASN__ENCODED_OK(erval);
}
ASN__ENCODE_FAILED;
}
/*
* Seek over the present member of the structure.
*/
elm = &td->elements[present-1];
if(elm->flags & ATF_POINTER) {
memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
if(memb_ptr == 0) {
if(elm->optional) {
erval.encoded = 0;
ASN__ENCODED_OK(erval);
}
/* Mandatory element absent */
ASN__ENCODE_FAILED;
}
} else {
memb_ptr = (void *)((char *)sptr + elm->memb_offset);
}
/*
* If the CHOICE itself is tagged EXPLICIT:
* T ::= [2] EXPLICIT CHOICE { ... }
* Then emit the appropriate tags.
*/
if(tag_mode == 1 || td->tags_count) {
/*
* For this, we need to pre-compute the member.
*/
ssize_t ret;
/* Encode member with its tag */
erval = elm->type->der_encoder(elm->type, memb_ptr,
elm->tag_mode, elm->tag, 0, 0);
if(erval.encoded == -1)
return erval;
/* Encode CHOICE with parent or my own tag */
ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
cb, app_key);
if(ret == -1)
ASN__ENCODE_FAILED;
computed_size += ret;
}
/*
* Encode the single underlying member.
*/
erval = elm->type->der_encoder(elm->type, memb_ptr,
elm->tag_mode, elm->tag, cb, app_key);
if(erval.encoded == -1)
return erval;
ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
(long)erval.encoded, (long)computed_size);
erval.encoded += computed_size;
return erval;
}
ber_tlv_tag_t
CHOICE_outmost_tag(const asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
int present;
assert(tag_mode == 0); (void)tag_mode;
assert(tag == 0); (void)tag;
/*
* Figure out which CHOICE element is encoded.
*/
present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
if(present > 0 || present <= td->elements_count) {
const asn_TYPE_member_t *elm = &td->elements[present-1];
const void *memb_ptr;
if(elm->flags & ATF_POINTER) {
memb_ptr = *(const void * const *)
((const char *)ptr + elm->memb_offset);
} else {
memb_ptr = (const void *)
((const char *)ptr + elm->memb_offset);
}
return asn_TYPE_outmost_tag(elm->type, memb_ptr,
elm->tag_mode, elm->tag);
} else {
return (ber_tlv_tag_t)-1;
}
}
int
CHOICE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
int present;
if(!sptr) {
ASN__CTFAIL(app_key, td, sptr,
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
/*
* Figure out which CHOICE element is encoded.
*/
present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
if(present > 0 && present <= td->elements_count) {
asn_TYPE_member_t *elm = &td->elements[present-1];
const void *memb_ptr;
if(elm->flags & ATF_POINTER) {
memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
if(!memb_ptr) {
if(elm->optional)
return 0;
ASN__CTFAIL(app_key, td, sptr,
"%s: mandatory CHOICE element %s absent (%s:%d)",
td->name, elm->name, __FILE__, __LINE__);
return -1;
}
} else {
memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
}
if(elm->memb_constraints) {
return elm->memb_constraints(elm->type, memb_ptr,
ctfailcb, app_key);
} else {
int ret = elm->type->check_constraints(elm->type,
memb_ptr, ctfailcb, app_key);
/*
* Cannot inherit it eralier:
* need to make sure we get the updated version.
*/
elm->memb_constraints = elm->type->check_constraints;
return ret;
}
} else {
ASN__CTFAIL(app_key, td, sptr,
"%s: no CHOICE element given (%s:%d)",
td->name, __FILE__, __LINE__);
return -1;
}
}
#undef XER_ADVANCE
#define XER_ADVANCE(num_bytes) do { \
size_t num = num_bytes; \
buf_ptr = (const void *)(((const char *)buf_ptr) + num); \
size -= num; \
consumed_myself += num; \
} while(0)
/*
* Decode the XER (XML) data.
*/
asn_dec_rval_t
CHOICE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
void **struct_ptr, const char *opt_mname,
const void *buf_ptr, size_t size) {
/*
* Bring closer parts of structure description.
*/
asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
/*
* Parts of the structure being constructed.
*/
void *st = *struct_ptr; /* Target structure. */
asn_struct_ctx_t *ctx; /* Decoder context */
asn_dec_rval_t rval; /* Return value of a decoder */
ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
int edx; /* Element index */
/*
* Create the target structure if it is not present already.
*/
if(st == 0) {
st = *struct_ptr = CALLOC(1, specs->struct_size);
if(st == 0) RETURN(RC_FAIL);
}
/*
* Restore parsing context.
*/
ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
if(ctx->phase == 0 && !*xml_tag)
ctx->phase = 1; /* Skip the outer tag checking phase */
/*
* Phases of XER/XML processing:
* Phase 0: Check that the opening tag matches our expectations.
* Phase 1: Processing body and reacting on closing tag.
* Phase 2: Processing inner type.
* Phase 3: Only waiting for closing tag.
* Phase 4: Skipping unknown extensions.
* Phase 5: PHASED OUT
*/
for(edx = ctx->step; ctx->phase <= 4;) {
pxer_chunk_type_e ch_type; /* XER chunk type */
ssize_t ch_size; /* Chunk size */
xer_check_tag_e tcv; /* Tag check value */
asn_TYPE_member_t *elm;
/*
* Go inside the member.
*/
if(ctx->phase == 2) {
asn_dec_rval_t tmprval;
void *memb_ptr; /* Pointer to the member */
void **memb_ptr2; /* Pointer to that pointer */
elm = &td->elements[edx];
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;
}
/* Start/Continue decoding the inner member */
tmprval = elm->type->xer_decoder(opt_codec_ctx,
elm->type, memb_ptr2, elm->name,
buf_ptr, size);
XER_ADVANCE(tmprval.consumed);
ASN_DEBUG("XER/CHOICE: itdf: [%s] code=%d",
elm->type->name, tmprval.code);
if(tmprval.code != RC_OK)
RETURN(tmprval.code);
assert(_fetch_present_idx(st,
specs->pres_offset, specs->pres_size) == 0);
/* Record what we've got */
_set_present_idx(st,
specs->pres_offset, specs->pres_size, edx + 1);
ctx->phase = 3;
/* Fall through */
}
/* No need to wait for closing tag; special mode. */
if(ctx->phase == 3 && !*xml_tag) {
ctx->phase = 5; /* Phase out */
RETURN(RC_OK);
}
/*
* Get the next part of the XML stream.
*/
ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
if(ch_size == -1) {
RETURN(RC_FAIL);
} else {
switch(ch_type) {
case PXER_WMORE:
RETURN(RC_WMORE);
case PXER_COMMENT: /* Got XML comment */
case PXER_TEXT: /* Ignore free-standing text */
XER_ADVANCE(ch_size); /* Skip silently */
continue;
case PXER_TAG:
break; /* Check the rest down there */
}
}
tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
xml_tag, tcv);
/* Skip the extensions section */
if(ctx->phase == 4) {
ASN_DEBUG("skip_unknown(%d, %ld)",
tcv, (long)ctx->left);
switch(xer_skip_unknown(tcv, &ctx->left)) {
case -1:
ctx->phase = 5;
RETURN(RC_FAIL);
continue;
case 1:
ctx->phase = 3;
/* Fall through */
case 0:
XER_ADVANCE(ch_size);
continue;
case 2:
ctx->phase = 3;
break;
}
}
switch(tcv) {
case XCT_BOTH:
break; /* No CHOICE? */
case XCT_CLOSING:
if(ctx->phase != 3)
break;
XER_ADVANCE(ch_size);
ctx->phase = 5; /* Phase out */
RETURN(RC_OK);
case XCT_OPENING:
if(ctx->phase == 0) {
XER_ADVANCE(ch_size);
ctx->phase = 1; /* Processing body phase */
continue;
}
/* Fall through */
case XCT_UNKNOWN_OP:
case XCT_UNKNOWN_BO:
if(ctx->phase != 1)
break; /* Really unexpected */
/*
* Search which inner member corresponds to this tag.
*/
for(edx = 0; edx < td->elements_count; edx++) {
elm = &td->elements[edx];
tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
switch(tcv) {
case XCT_BOTH:
case XCT_OPENING:
/*
* Process this member.
*/
ctx->step = edx;
ctx->phase = 2;
break;
case XCT_UNKNOWN_OP:
case XCT_UNKNOWN_BO:
continue;
default:
edx = td->elements_count;
break; /* Phase out */
}
break;
}
if(edx != td->elements_count)
continue;
/* It is expected extension */
if(specs->ext_start != -1) {
ASN_DEBUG("Got anticipated extension");
/*
* Check for (XCT_BOTH or XCT_UNKNOWN_BO)
* By using a mask. Only record a pure
* <opening> tags.
*/
if(tcv & XCT_CLOSING) {
/* Found </extension> without body */
ctx->phase = 3; /* Terminating */
} else {
ctx->left = 1;
ctx->phase = 4; /* Skip ...'s */
}
XER_ADVANCE(ch_size);
continue;
}
/* Fall through */
default:
break;
}
ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]"
" (ph=%d, tag=%s)",
ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
td->name, ctx->phase, xml_tag);
break;
}
ctx->phase = 5; /* Phase out, just in case */
RETURN(RC_FAIL);
}
asn_enc_rval_t
CHOICE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
int ilevel, enum xer_encoder_flags_e flags,
asn_app_consume_bytes_f *cb, void *app_key) {
asn_CHOICE_specifics_t *specs=(asn_CHOICE_specifics_t *)td->specifics;
asn_enc_rval_t er;
int present;
if(!sptr)
ASN__ENCODE_FAILED;
/*
* Figure out which CHOICE element is encoded.
*/
present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
if(present <= 0 || present > td->elements_count) {
ASN__ENCODE_FAILED;
} else {
asn_enc_rval_t tmper;
asn_TYPE_member_t *elm = &td->elements[present-1];
void *memb_ptr;
const char *mname = elm->name;
unsigned int mlen = strlen(mname);
if(elm->flags & ATF_POINTER) {
memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
if(!memb_ptr) ASN__ENCODE_FAILED;
} else {
memb_ptr = (void *)((char *)sptr + elm->memb_offset);
}
er.encoded = 0;
if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel);
ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
tmper = elm->type->xer_encoder(elm->type, memb_ptr,
ilevel + 1, flags, cb, app_key);
if(tmper.encoded == -1) return tmper;
ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
er.encoded += 5 + (2 * mlen) + tmper.encoded;
}
if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel - 1);
ASN__ENCODED_OK(er);
cb_failed:
ASN__ENCODE_FAILED;
}
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;
}
asn_enc_rval_t
CHOICE_encode_uper(asn_TYPE_descriptor_t *td,
asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
asn_TYPE_member_t *elm; /* CHOICE's element */
asn_per_constraint_t *ct;
void *memb_ptr;
int present;
int present_enc;
if(!sptr) ASN__ENCODE_FAILED;
ASN_DEBUG("Encoding %s as CHOICE", td->name);
if(constraints) ct = &constraints->value;
else if(td->per_constraints) ct = &td->per_constraints->value;
else ct = 0;
present = _fetch_present_idx(sptr,
specs->pres_offset, specs->pres_size);
/*
* If the structure was not initialized properly, it cannot be encoded:
* can't deduce what to encode in the choice type.
*/
if(present <= 0 || present > td->elements_count)
ASN__ENCODE_FAILED;
else
present--;
ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
/* Adjust if canonical order is different from natural order */
if(specs->canonical_order)
present_enc = specs->canonical_order[present];
else
present_enc = present;
if(ct && ct->range_bits >= 0) {
if(present_enc < ct->lower_bound
|| present_enc > ct->upper_bound) {
if(ct->flags & APC_EXTENSIBLE) {
if(per_put_few_bits(po, 1, 1))
ASN__ENCODE_FAILED;
} else {
ASN__ENCODE_FAILED;
}
ct = 0;
}
}
if(ct && ct->flags & APC_EXTENSIBLE)
if(per_put_few_bits(po, 0, 1))
ASN__ENCODE_FAILED;
elm = &td->elements[present];
if(elm->flags & ATF_POINTER) {
/* Member is a pointer to another structure */
memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
if(!memb_ptr) ASN__ENCODE_FAILED;
} else {
memb_ptr = (char *)sptr + elm->memb_offset;
}
if(ct && ct->range_bits >= 0) {
if(per_put_few_bits(po, present_enc, ct->range_bits))
ASN__ENCODE_FAILED;
return elm->type->uper_encoder(elm->type, elm->per_constraints,
memb_ptr, po);
} else {
asn_enc_rval_t rval;
if(specs->ext_start == -1)
ASN__ENCODE_FAILED;
if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
ASN__ENCODE_FAILED;
if(uper_open_type_put(elm->type, elm->per_constraints,
memb_ptr, po))
ASN__ENCODE_FAILED;
rval.encoded = 0;
ASN__ENCODED_OK(rval);
}
}
int
CHOICE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
asn_app_consume_bytes_f *cb, void *app_key) {
asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
int present;
if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
/*
* Figure out which CHOICE element is encoded.
*/
present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
/*
* Print that element.
*/
if(present > 0 && present <= td->elements_count) {
asn_TYPE_member_t *elm = &td->elements[present-1];
const void *memb_ptr;
if(elm->flags & ATF_POINTER) {
memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
} else {
memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
}
/* Print member's name and stuff */
if(0) {
if(cb(elm->name, strlen(elm->name), app_key) < 0
|| cb(": ", 2, app_key) < 0)
return -1;
}
return elm->type->print_struct(elm->type, memb_ptr, ilevel,
cb, app_key);
} else {
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
}
}
void
CHOICE_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
asn_CHOICE_specifics_t *specs = (asn_CHOICE_specifics_t *)td->specifics;
int present;
if(!td || !ptr)
return;
ASN_DEBUG("Freeing %s as CHOICE", td->name);
/*
* Figure out which CHOICE element is encoded.
*/
present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
/*
* Free that element.
*/
if(present > 0 && present <= td->elements_count) {
asn_TYPE_member_t *elm = &td->elements[present-1];
void *memb_ptr;
if(elm->flags & ATF_POINTER) {
memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
if(memb_ptr)
ASN_STRUCT_FREE(*elm->type, memb_ptr);
} else {
memb_ptr = (void *)((char *)ptr + elm->memb_offset);
ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
}
}
if(!contents_only) {
FREEMEM(ptr);
}
}
/*
* The following functions functions offer protection against -fshort-enums,
* compatible with little- and big-endian machines.
* If assertion is triggered, either disable -fshort-enums, or add an entry
* here with the ->pres_size of your target stracture.
* Unless the target structure is packed, the ".present" member
* is guaranteed to be aligned properly. ASN.1 compiler itself does not
* produce packed code.
*/
static int
_fetch_present_idx(const void *struct_ptr, int pres_offset, int pres_size) {
const void *present_ptr;
int present;
present_ptr = ((const char *)struct_ptr) + pres_offset;
switch(pres_size) {
case sizeof(int): present = *(const int *)present_ptr; break;
case sizeof(short): present = *(const short *)present_ptr; break;
case sizeof(char): present = *(const char *)present_ptr; break;
default:
/* ANSI C mandates enum to be equivalent to integer */
assert(pres_size != sizeof(int));
return 0; /* If not aborted, pass back safe value */
}
return present;
}
static void
_set_present_idx(void *struct_ptr, int pres_offset, int pres_size, int present) {
void *present_ptr;
present_ptr = ((char *)struct_ptr) + pres_offset;
switch(pres_size) {
case sizeof(int): *(int *)present_ptr = present; break;
case sizeof(short): *(short *)present_ptr = present; break;
case sizeof(char): *(char *)present_ptr = present; break;
default:
/* ANSI C mandates enum to be equivalent to integer */
assert(pres_size != sizeof(int));
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化