asn1c/skeletons/constr_SET_OF.c

602 lines
14 KiB
C
Raw Normal View History

2004-06-03 03:38:44 +00:00
/*-
* Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
2004-09-22 16:06:28 +00:00
#include <asn_internal.h>
2004-06-03 03:38:44 +00:00
#include <constr_SET_OF.h>
#include <asn_SET_OF.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.
*/
2004-08-18 04:53:32 +00:00
#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
2004-06-03 03:38:44 +00:00
/*
* 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.
*/
2004-09-23 22:06:26 +00:00
#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
2004-06-03 03:38:44 +00:00
/*
* This macro "eats" the part of the buffer which is definitely "consumed",
* i.e. was correctly converted into local representation or rightfully skipped.
*/
2004-09-23 22:06:26 +00:00
#undef ADVANCE
2004-06-03 03:38:44 +00:00
#define ADVANCE(num_bytes) do { \
size_t num = num_bytes; \
ptr = ((char *)ptr) + num; \
2004-06-03 03:38:44 +00:00
size -= num; \
if(ctx->left >= 0) \
ctx->left -= num; \
consumed_myself += num; \
} while(0)
/*
* Switch to the next phase of parsing.
*/
2004-09-23 22:06:26 +00:00
#undef NEXT_PHASE
#undef PHASE_OUT
2004-06-03 03:38:44 +00:00
#define NEXT_PHASE(ctx) do { \
ctx->phase++; \
ctx->step = 0; \
} while(0)
#define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
/*
* Return a standardized complex structure.
*/
2004-09-23 22:06:26 +00:00
#undef RETURN
2004-06-03 03:38:44 +00:00
#define RETURN(_code) do { \
rval.code = _code; \
rval.consumed = consumed_myself;\
return rval; \
} while(0)
/*
* The decoder of the SET OF type.
*/
ber_dec_rval_t
2004-09-29 13:26:15 +00:00
SET_OF_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
2004-06-03 03:38:44 +00:00
void **struct_ptr, void *ptr, size_t size, int tag_mode) {
/*
* Bring closer parts of structure description.
*/
2004-09-29 13:26:15 +00:00
asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics;
asn_TYPE_member_t *element = td->elements; /* Single one */
2004-06-03 03:38:44 +00:00
/*
* Parts of the structure being constructed.
*/
void *st = *struct_ptr; /* Target structure. */
2004-09-29 13:26:15 +00:00
asn_struct_ctx_t *ctx; /* Decoder context */
2004-06-03 03:38:44 +00:00
ber_tlv_tag_t tlv_tag; /* T from TLV */
ber_dec_rval_t rval; /* Return code from subparsers */
ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
2004-08-20 13:23:42 +00:00
ASN_DEBUG("Decoding %s as SET OF", td->name);
2004-06-03 03:38:44 +00:00
/*
* 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.
*/
2004-09-29 13:26:15 +00:00
ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
2004-06-03 03:38:44 +00:00
/*
* 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.
*/
2004-09-29 13:26:15 +00:00
rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
tag_mode, 1, &ctx->left, 0);
2004-06-03 03:38:44 +00:00
if(rval.code != RC_OK) {
ASN_DEBUG("%s tagging check failed: %d",
2004-08-20 13:23:42 +00:00
td->name, rval.code);
2004-06-03 03:38:44 +00:00
consumed_myself += rval.consumed;
RETURN(rval.code);
}
if(ctx->left >= 0)
ctx->left += rval.consumed; /* ?Substracted below! */
ADVANCE(rval.consumed);
ASN_DEBUG("Structure consumes %ld bytes, "
"buffer %ld", (long)ctx->left, (long)size);
NEXT_PHASE(ctx);
/* Fall through */
case 1:
/*
* PHASE 1.
* From the place where we've left it previously,
* try to decode the next item.
*/
for(;; ctx->step = 0) {
ssize_t tag_len; /* Length of TLV's T */
if(ctx->step & 1)
goto microphase2;
/*
* MICROPHASE 1: Synchronize decoding.
*/
if(ctx->left == 0) {
2004-08-20 13:23:42 +00:00
ASN_DEBUG("End of SET OF %s", td->name);
2004-06-03 03:38:44 +00:00
/*
* No more things to decode.
* Exit out of here.
*/
PHASE_OUT(ctx);
RETURN(RC_OK);
}
/*
* Fetch the T from TLV.
*/
tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
switch(tag_len) {
case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
/* Fall through */
case -1: RETURN(RC_FAIL);
}
if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
if(LEFT < 2) {
if(SIZE_VIOLATION)
RETURN(RC_FAIL);
else
RETURN(RC_WMORE);
} else if(((uint8_t *)ptr)[1] == 0) {
/*
* Found the terminator of the
* indefinite length structure.
*/
break;
}
}
/* Outmost tag may be unknown and cannot be fetched/compared */
2004-06-05 08:17:50 +00:00
if(element->tag != (ber_tlv_tag_t)-1) {
2004-06-03 03:38:44 +00:00
if(BER_TAGS_EQUAL(tlv_tag, element->tag)) {
/*
* The new list member of expected type has arrived.
*/
} else {
ASN_DEBUG("Unexpected tag %s fixed SET OF %s",
2004-08-20 13:23:42 +00:00
ber_tlv_tag_string(tlv_tag), td->name);
2004-06-03 03:38:44 +00:00
ASN_DEBUG("%s SET OF has tag %s",
2004-08-20 13:23:42 +00:00
td->name, ber_tlv_tag_string(element->tag));
2004-06-03 03:38:44 +00:00
RETURN(RC_FAIL);
}
}
/*
* MICROPHASE 2: Invoke the member-specific decoder.
*/
ctx->step |= 1; /* Confirm entering next microphase */
microphase2:
/*
* Invoke the member fetch routine according to member's type
*/
2004-09-29 13:26:15 +00:00
rval = element->type->ber_decoder(opt_codec_ctx,
element->type, &ctx->ptr, ptr, LEFT, 0);
2004-06-03 03:38:44 +00:00
ASN_DEBUG("In %s SET OF %s code %d consumed %d",
2004-08-20 13:23:42 +00:00
td->name, element->type->name,
2004-06-03 03:38:44 +00:00
rval.code, (int)rval.consumed);
switch(rval.code) {
case RC_OK:
{
2004-08-11 09:07:36 +00:00
A_SET_OF(void) *list;
(void *)list = st;
2004-06-03 03:38:44 +00:00
if(ASN_SET_ADD(list, ctx->ptr) != 0)
RETURN(RC_FAIL);
else
ctx->ptr = 0;
}
break;
case RC_WMORE: /* More data expected */
if(!SIZE_VIOLATION) {
ADVANCE(rval.consumed);
RETURN(RC_WMORE);
}
/* Fall through */
case RC_FAIL: /* Fatal error */
RETURN(RC_FAIL);
} /* switch(rval) */
ADVANCE(rval.consumed);
} /* for(all list members) */
NEXT_PHASE(ctx);
case 2:
/*
* Read in all "end of content" TLVs.
*/
while(ctx->left < 0) {
if(LEFT < 2) {
if(LEFT > 0 && ((char *)ptr)[0] != 0) {
/* Unexpected tag */
RETURN(RC_FAIL);
} else {
RETURN(RC_WMORE);
}
}
if(((char *)ptr)[0] == 0
&& ((char *)ptr)[1] == 0) {
ADVANCE(2);
ctx->left++;
} else {
RETURN(RC_FAIL);
}
}
PHASE_OUT(ctx);
}
RETURN(RC_OK);
}
/*
* Internally visible buffer holding a single encoded element.
*/
struct _el_buffer {
uint8_t *buf;
size_t length;
size_t size;
};
/* Append bytes to the above structure */
static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr) {
2004-08-11 09:07:36 +00:00
struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr;
2004-06-03 03:38:44 +00:00
if(el_buf->length + size > el_buf->size)
return -1;
memcpy(el_buf->buf + el_buf->length, buffer, size);
el_buf->length += size;
return 0;
}
static int _el_buf_cmp(const void *ap, const void *bp) {
2004-08-11 09:07:36 +00:00
const struct _el_buffer *a = (const struct _el_buffer *)ap;
const struct _el_buffer *b = (const struct _el_buffer *)bp;
2004-06-03 03:38:44 +00:00
int ret;
size_t common_len;
if(a->length < b->length)
common_len = a->length;
else
common_len = b->length;
ret = memcmp(a->buf, b->buf, common_len);
if(ret == 0) {
if(a->length < b->length)
ret = -1;
else if(a->length > b->length)
ret = 1;
}
return ret;
}
/*
* The DER encoder of the SET OF type.
*/
2004-09-22 16:06:28 +00:00
asn_enc_rval_t
2004-09-29 13:26:15 +00:00
SET_OF_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
2004-06-03 03:38:44 +00:00
int tag_mode, ber_tlv_tag_t tag,
asn_app_consume_bytes_f *cb, void *app_key) {
2004-09-29 13:26:15 +00:00
asn_TYPE_member_t *elm = td->elements;
asn_TYPE_descriptor_t *elm_type = elm->type;
2004-06-03 03:38:44 +00:00
der_type_encoder_f *der_encoder = elm_type->der_encoder;
2004-08-11 09:07:36 +00:00
A_SET_OF(void) *list;
2004-06-03 03:38:44 +00:00
size_t computed_size = 0;
ssize_t encoding_size = 0;
struct _el_buffer *encoded_els;
size_t max_encoded_len = 1;
2004-09-22 16:06:28 +00:00
asn_enc_rval_t erval;
2004-06-03 03:38:44 +00:00
int ret;
int edx;
2004-08-20 13:23:42 +00:00
ASN_DEBUG("Estimating size for SET OF %s", td->name);
2004-06-03 03:38:44 +00:00
/*
* Gather the length of the underlying members sequence.
*/
2004-08-11 09:07:36 +00:00
(void *)list = ptr;
2004-06-03 03:38:44 +00:00
for(edx = 0; edx < list->count; edx++) {
void *memb_ptr = list->array[edx];
erval = der_encoder(elm_type, memb_ptr, 0, elm->tag, 0, 0);
if(erval.encoded == -1)
return erval;
computed_size += erval.encoded;
/* Compute maximum encoding's size */
2004-06-05 08:17:50 +00:00
if(max_encoded_len < (size_t)erval.encoded)
2004-06-03 03:38:44 +00:00
max_encoded_len = erval.encoded;
}
/*
* Encode the TLV for the sequence itself.
*/
encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag,
2004-06-03 03:38:44 +00:00
cb, app_key);
if(encoding_size == -1) {
erval.encoded = -1;
2004-08-20 13:23:42 +00:00
erval.failed_type = td;
2004-06-03 03:38:44 +00:00
erval.structure_ptr = ptr;
return erval;
}
computed_size += encoding_size;
if(!cb) {
erval.encoded = computed_size;
return erval;
}
/*
* DER mandates dynamic sorting of the SET OF elements
* according to their encodings. Build an array of the
* encoded elements.
*/
encoded_els = (struct _el_buffer *)MALLOC(
list->count * sizeof(encoded_els[0]));
2004-06-03 03:38:44 +00:00
if(encoded_els == NULL) {
erval.encoded = -1;
2004-08-20 13:23:42 +00:00
erval.failed_type = td;
2004-06-03 03:38:44 +00:00
erval.structure_ptr = ptr;
return erval;
}
2004-08-20 13:23:42 +00:00
ASN_DEBUG("Encoding members of %s SET OF", td->name);
2004-06-03 03:38:44 +00:00
/*
* Encode all members.
*/
for(edx = 0; edx < list->count; edx++) {
void *memb_ptr = list->array[edx];
struct _el_buffer *encoded_el = &encoded_els[edx];
/*
* Prepare space for encoding.
*/
2004-08-11 09:07:36 +00:00
encoded_el->buf = (uint8_t *)MALLOC(max_encoded_len);
2004-06-03 03:38:44 +00:00
if(encoded_el->buf) {
encoded_el->length = 0;
encoded_el->size = max_encoded_len;
} else {
for(edx--; edx >= 0; edx--)
FREEMEM(encoded_els[edx].buf);
FREEMEM(encoded_els);
erval.encoded = -1;
2004-08-20 13:23:42 +00:00
erval.failed_type = td;
2004-06-03 03:38:44 +00:00
erval.structure_ptr = ptr;
return erval;
}
/*
* Encode the member into the prepared space.
*/
erval = der_encoder(elm_type, memb_ptr, 0, elm->tag,
_el_addbytes, encoded_el);
if(erval.encoded == -1) {
for(; edx >= 0; edx--)
FREEMEM(encoded_els[edx].buf);
FREEMEM(encoded_els);
return erval;
}
encoding_size += erval.encoded;
}
/*
* Sort the encoded elements according to their encoding.
*/
qsort(encoded_els, list->count, sizeof(encoded_els[0]), _el_buf_cmp);
/*
* Report encoded elements to the application.
* Dispose of temporary sorted members table.
*/
ret = 0;
for(edx = 0; edx < list->count; edx++) {
struct _el_buffer *encoded_el = &encoded_els[edx];
/* Report encoded chunks to the application */
if(ret == 0
&& cb(encoded_el->buf, encoded_el->length, app_key) < 0)
2004-06-03 03:38:44 +00:00
ret = -1;
FREEMEM(encoded_el->buf);
}
FREEMEM(encoded_els);
2004-06-05 08:17:50 +00:00
if(ret || computed_size != (size_t)encoding_size) {
2004-06-03 03:38:44 +00:00
/*
* Standard callback failed, or
* encoded size is not equal to the computed size.
*/
erval.encoded = -1;
2004-08-20 13:23:42 +00:00
erval.failed_type = td;
2004-06-03 03:38:44 +00:00
erval.structure_ptr = ptr;
} else {
erval.encoded = computed_size;
}
return erval;
}
2004-09-22 16:06:28 +00:00
asn_enc_rval_t
2004-09-29 13:26:15 +00:00
SET_OF_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
2004-09-22 16:06:28 +00:00
int ilevel, enum xer_encoder_flags_e flags,
asn_app_consume_bytes_f *cb, void *app_key) {
asn_enc_rval_t er;
2004-09-29 13:26:15 +00:00
asn_SET_OF_specifics_t *specs=(asn_SET_OF_specifics_t *)td->specifics;
asn_TYPE_member_t *element = td->elements;
2004-09-22 16:06:28 +00:00
A_SET_OF(void) *list;
const char *mname = specs->as_XMLValueList
? 0 : ((*element->name) ? element->name : element->type->name);
size_t mlen = mname ? strlen(mname) : 0;
int xcan = (flags & XER_F_CANONICAL);
int i;
if(!sptr) _ASN_ENCODE_FAILED;
er.encoded = 0;
(void *)list = sptr;
for(i = 0; i < list->count; i++) {
asn_enc_rval_t tmper;
void *memb_ptr = list->array[i];
if(!memb_ptr) continue;
if(mname) {
if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
_ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
}
tmper = element->type->xer_encoder(element->type, memb_ptr,
ilevel + 1, flags, cb, app_key);
if(tmper.encoded == -1) return tmper;
if(mname) {
_ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
er.encoded += 5;
}
er.encoded += (2 * mlen) + tmper.encoded;
}
if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
return er;
}
2004-06-03 03:38:44 +00:00
int
2004-09-29 13:26:15 +00:00
SET_OF_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
2004-06-03 03:38:44 +00:00
asn_app_consume_bytes_f *cb, void *app_key) {
2004-09-29 13:26:15 +00:00
asn_TYPE_member_t *element = td->elements;
2004-08-11 09:07:36 +00:00
const A_SET_OF(void) *list;
2004-06-03 03:38:44 +00:00
int ret;
int i;
if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
2004-06-03 03:38:44 +00:00
/* Dump preamble */
if(cb(td->name, strlen(td->name), app_key) < 0
|| cb(" ::= {", 6, app_key) < 0)
2004-06-03 03:38:44 +00:00
return -1;
2004-08-11 09:34:42 +00:00
(const void *)list = sptr;
2004-06-03 03:38:44 +00:00
for(i = 0; i < list->count; i++) {
const void *memb_ptr = list->array[i];
if(!memb_ptr) continue;
_i_INDENT(1);
2004-06-03 03:38:44 +00:00
ret = element->type->print_struct(element->type, memb_ptr,
ilevel + 1, cb, app_key);
2004-06-03 03:38:44 +00:00
if(ret) return ret;
}
ilevel--;
_i_INDENT(1);
2004-06-03 03:38:44 +00:00
return (cb("}", 1, app_key) < 0) ? -1 : 0;
2004-06-03 03:38:44 +00:00
}
void
2004-09-29 13:26:15 +00:00
SET_OF_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
2004-06-03 03:38:44 +00:00
if(td && ptr) {
2004-09-29 13:26:15 +00:00
asn_TYPE_member_t *element = td->elements;
2004-08-11 09:07:36 +00:00
A_SET_OF(void) *list;
2004-06-03 03:38:44 +00:00
int i;
/*
* Could not use set_of_empty() because of (*free)
* incompatibility.
*/
2004-08-11 09:07:36 +00:00
(void *)list = ptr;
2004-06-03 03:38:44 +00:00
for(i = 0; i < list->count; i++) {
void *memb_ptr = list->array[i];
if(memb_ptr)
element->type->free_struct(element->type, memb_ptr, 0);
}
2004-07-15 10:51:26 +00:00
list->count = 0; /* No meaningful elements left */
asn_set_empty(list); /* Remove (list->array) */
2004-06-03 03:38:44 +00:00
if(!contents_only) {
FREEMEM(ptr);
}
}
}
int
2004-09-29 13:26:15 +00:00
SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
2004-06-03 03:38:44 +00:00
asn_app_consume_bytes_f *app_errlog, void *app_key) {
2004-09-29 13:26:15 +00:00
asn_TYPE_member_t *element = td->elements;
2004-08-20 13:23:42 +00:00
asn_constr_check_f *constr;
2004-08-11 09:07:36 +00:00
const A_SET_OF(void) *list;
2004-06-03 03:38:44 +00:00
int i;
if(!sptr) {
2004-08-11 09:44:13 +00:00
_ASN_ERRLOG(app_errlog, app_key,
2004-08-22 13:47:59 +00:00
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
2004-06-03 03:38:44 +00:00
return -1;
}
2004-08-11 09:34:42 +00:00
(const void *)list = sptr;
2004-08-20 13:23:42 +00:00
constr = element->memb_constraints;
if(!constr) constr = element->type->check_constraints;
/*
* Iterate over the members of an array.
* Validate each in turn, until one fails.
*/
2004-06-03 03:38:44 +00:00
for(i = 0; i < list->count; i++) {
const void *memb_ptr = list->array[i];
2004-08-20 13:23:42 +00:00
int ret;
2004-06-03 03:38:44 +00:00
if(!memb_ptr) continue;
2004-08-20 13:23:42 +00:00
ret = constr(element->type, memb_ptr, app_errlog, app_key);
if(ret) return ret;
2004-06-03 03:38:44 +00:00
}
2004-08-20 13:23:42 +00:00
/*
* Cannot inherit it eralier:
* need to make sure we get the updated version.
*/
if(!element->memb_constraints)
element->memb_constraints = element->type->check_constraints;
2004-06-03 03:38:44 +00:00
return 0;
}