Commit 8bb4a952 authored by Lev Walkin's avatar Lev Walkin

SET OF XER support

parent d5125645
/*-
* Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Copyright (c) 2003, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#ifndef _CONSTR_SEQUENCE_OF_H_
......@@ -10,12 +10,13 @@
/*
* A set specialized functions dealing with the SEQUENCE OF type.
* Implemented using SET OF.
* Generally implemented using SET OF.
*/
#define SEQUENCE_OF_free SET_OF_free
#define SEQUENCE_OF_print SET_OF_print
#define SEQUENCE_OF_constraint SET_OF_constraint
#define SEQUENCE_OF_decode_ber SET_OF_decode_ber
#define SEQUENCE_OF_decode_xer SET_OF_decode_xer
der_type_encoder_f SEQUENCE_OF_encode_der;
xer_type_encoder_f SEQUENCE_OF_encode_xer;
......
......@@ -643,7 +643,7 @@ SET_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
RETURN(RC_FAIL);
}
elm = &td->elements[edx];
elm = &elements[edx];
if(elm->flags & ATF_POINTER) {
/* Member is a pointer to another structure */
......@@ -724,7 +724,7 @@ SET_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
* Search which member corresponds to this tag.
*/
for(edx = 0; edx < td->elements_count; edx++) {
elm = &td->elements[edx];
elm = &elements[edx];
tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
switch(tcv) {
case XCT_BOTH:
......
......@@ -47,6 +47,7 @@ asn_constr_check_f SET_constraint;
ber_type_decoder_f SET_decode_ber;
der_type_encoder_f SET_encode_der;
xer_type_decoder_f SET_decode_xer;
xer_type_decoder_f SET_decode_xer;
xer_type_encoder_f SET_encode_xer;
/***********************
......
......@@ -449,6 +449,172 @@ SET_OF_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
return erval;
}
#undef XER_ADVANCE
#define XER_ADVANCE(num_bytes) do { \
size_t num = num_bytes; \
buf_ptr = ((char *)buf_ptr) + num; \
size -= num; \
consumed_myself += num; \
} while(0)
/*
* Decode the XER (XML) data.
*/
asn_dec_rval_t
SET_OF_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
void **struct_ptr, const char *opt_mname,
void *buf_ptr, size_t size) {
/*
* Bring closer parts of structure description.
*/
asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics;
asn_TYPE_member_t *element = td->elements;
const char *elm_tag = specs->as_XMLValueList
? 0 : ((*element->name)
? element->name : element->type->xml_tag);
const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
/*
* ... and 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 from a decoder */
ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
int xer_state; /* XER low level parsing context */
/*
* 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);
/*
* 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.
*/
for(xer_state = ctx->left; ctx->phase <= 2;) {
pxer_chunk_type_e ch_type; /* XER chunk type */
ssize_t ch_size; /* Chunk size */
xer_check_tag_e tcv; /* Tag check value */
/*
* Go inside the inner member of a set.
*/
if(ctx->phase == 2) {
asn_dec_rval_t tmprval;
/* Invoke the inner type decoder, m.b. multiple times */
tmprval = element->type->xer_decoder(opt_codec_ctx,
element->type, &ctx->ptr, elm_tag,
buf_ptr, size);
if(tmprval.code == RC_OK) {
A_SET_OF(void) *list;
(void *)list = (void *)st;
if(ASN_SET_ADD(list, ctx->ptr) != 0)
RETURN(RC_FAIL);
ctx->ptr = 0;
XER_ADVANCE(tmprval.consumed);
} else {
XER_ADVANCE(tmprval.consumed);
RETURN(tmprval.code);
}
ctx->phase = 1; /* Back to body processing */
ctx->left = xer_state = 0; /* New, clean state */
ASN_DEBUG("XER/SET OF phase => %d", ctx->phase);
/* Fall through */
}
/*
* Get the next part of the XML stream.
*/
ch_size = xer_next_token(&xer_state, buf_ptr, size, &ch_type);
switch(ch_size) {
case -1: RETURN(RC_FAIL);
case 0:
ctx->left = xer_state;
RETURN(RC_WMORE);
default:
switch(ch_type) {
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/SET OF: tcv = %d, ph=%d", tcv, ctx->phase);
switch(tcv) {
case XCT_CLOSING:
if(ctx->phase == 0) break;
ctx->phase = 0;
/* Fall through */
case XCT_BOTH:
if(ctx->phase == 0) {
/* No more things to decode */
XER_ADVANCE(ch_size);
ctx->phase = 3; /* Phase out */
RETURN(RC_OK);
}
/* Fall through */
case XCT_OPENING:
if(ctx->phase == 0) {
XER_ADVANCE(ch_size);
ctx->phase = 1; /* Processing body phase */
continue;
}
/* Fall through */
case XCT_UNEXPECTED:
ASN_DEBUG("XER/SET OF: tcv=%d, ph=%d", tcv, ctx->phase);
if(ctx->phase != 1)
break; /* Really unexpected */
/*
* Search which member corresponds to this tag.
*/
tcv = xer_check_tag(buf_ptr, ch_size, elm_tag);
switch(tcv) {
case XCT_BOTH:
case XCT_OPENING:
/*
* Process this member.
*/
ctx->phase = 2;
continue;
case XCT_UNEXPECTED:
case XCT_CLOSING:
default:
break; /* Phase out */
}
/* Fall through */
default:
break;
}
ASN_DEBUG("Unexpected XML tag in SET OF");
break;
}
ctx->phase = 3; /* "Phase out" on hard failure */
RETURN(RC_FAIL);
}
typedef struct xer_tmp_enc_s {
void *buffer;
size_t offset;
......
......@@ -26,6 +26,7 @@ asn_struct_print_f SET_OF_print;
asn_constr_check_f SET_OF_constraint;
ber_type_decoder_f SET_OF_decode_ber;
der_type_encoder_f SET_OF_encode_der;
xer_type_decoder_f SET_OF_decode_xer;
xer_type_encoder_f SET_OF_encode_xer;
#endif /* _CONSTR_SET_OF_H_ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment