3 * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_application.h>
7 #include <asn_internal.h>
8 #include <xer_support.h> /* XER/XML parsing support */
12 * Decode the XER encoding of a given type.
15 xer_decode(const asn_codec_ctx_t *opt_codec_ctx,
16 const asn_TYPE_descriptor_t *td, void **struct_ptr,
17 const void *buffer, size_t size) {
18 asn_codec_ctx_t s_codec_ctx;
21 * Stack checker requires that the codec context
22 * must be allocated on the stack.
25 if(opt_codec_ctx->max_stack_size) {
26 s_codec_ctx = *opt_codec_ctx;
27 opt_codec_ctx = &s_codec_ctx;
30 /* If context is not given, be security-conscious anyway */
31 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
32 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
33 opt_codec_ctx = &s_codec_ctx;
37 * Invoke type-specific decoder.
39 return td->op->xer_decoder(opt_codec_ctx, td, struct_ptr, 0, buffer, size);
45 pxml_chunk_type_e chunk_type;
47 const void *chunk_buf;
48 int callback_not_invoked;
52 xer__token_cb(pxml_chunk_type_e type, const void *_chunk_data, size_t _chunk_size, void *key) {
53 struct xer__cb_arg *arg = (struct xer__cb_arg *)key;
54 arg->chunk_type = type;
55 arg->chunk_size = _chunk_size;
56 arg->chunk_buf = _chunk_data;
57 arg->callback_not_invoked = 0;
58 return -1; /* Terminate the XML parsing */
62 * Fetch the next token from the XER/XML stream.
65 xer_next_token(int *stateContext, const void *buffer, size_t size, pxer_chunk_type_e *ch_type) {
66 struct xer__cb_arg arg;
67 int new_stateContext = *stateContext;
70 arg.callback_not_invoked = 1;
71 ret = pxml_parse(&new_stateContext, buffer, size, xer__token_cb, &arg);
72 if(ret < 0) return -1;
73 if(arg.callback_not_invoked) {
74 assert(ret == 0); /* No data was consumed */
75 *ch_type = PXER_WMORE;
76 return 0; /* Try again with more data */
78 assert(arg.chunk_size);
79 assert(arg.chunk_buf == buffer);
83 * Translate the XML chunk types into more convenient ones.
85 switch(arg.chunk_type) {
90 *ch_type = PXER_WMORE;
91 return 0; /* Want more */
96 case PXML_COMMENT_END:
97 *ch_type = PXER_COMMENT;
101 *stateContext = new_stateContext;
102 return arg.chunk_size;
105 #define CSLASH 0x2f /* '/' */
106 #define LANGLE 0x3c /* '<' */
107 #define RANGLE 0x3e /* '>' */
110 xer_check_tag(const void *buf_ptr, int size, const char *need_tag) {
111 const char *buf = (const char *)buf_ptr;
113 xer_check_tag_e ct = XCT_OPENING;
115 if(size < 2 || buf[0] != LANGLE || buf[size-1] != RANGLE) {
117 ASN_DEBUG("Broken XML tag: \"%c...%c\"",
118 buf[0], buf[size - 1]);
123 * Determine the tag class.
125 if(buf[1] == CSLASH) {
126 buf += 2; /* advance past "</" */
127 size -= 3; /* strip "</" and ">" */
129 if(size > 0 && buf[size-1] == CSLASH)
130 return XCT_BROKEN; /* </abc/> */
132 buf++; /* advance past "<" */
133 size -= 2; /* strip "<" and ">" */
134 if(size > 0 && buf[size-1] == CSLASH) {
136 size--; /* One more, for "/" */
140 /* Sometimes we don't care about the tag */
141 if(!need_tag || !*need_tag)
142 return (xer_check_tag_e)(XCT__UNK__MASK | ct);
145 * Determine the tag name.
147 for(end = buf + size; buf < end; buf++, need_tag++) {
148 int b = *buf, n = *need_tag;
152 case 0x09: case 0x0a: case 0x0c: case 0x0d:
154 /* "<abc def/>": whitespace is normal */
158 return (xer_check_tag_e)(XCT__UNK__MASK | ct);
161 return XCT_BROKEN; /* Embedded 0 in buf?! */
164 return (xer_check_tag_e)(XCT__UNK__MASK | ct);
171 #define ADVANCE(num_bytes) do { \
172 size_t num = (num_bytes); \
173 buf_ptr = ((const char *)buf_ptr) + num; \
175 consumed_myself += num; \
179 #define RETURN(_code) do { \
181 rval.consumed = consumed_myself; \
182 if(rval.code != RC_OK) \
183 ASN_DEBUG("Failed with %d", rval.code); \
187 #define XER_GOT_BODY(chunk_buf, chunk_size, size) do { \
188 ssize_t converted_size = body_receiver \
189 (struct_key, chunk_buf, chunk_size, \
190 (size_t)chunk_size < size); \
191 if(converted_size == -1) RETURN(RC_FAIL); \
192 if(converted_size == 0 \
193 && size == (size_t)chunk_size) \
195 chunk_size = converted_size; \
197 #define XER_GOT_EMPTY() do { \
198 if(body_receiver(struct_key, 0, 0, size > 0) == -1) \
203 * Generalized function for decoding the primitive values.
206 xer_decode_general(const asn_codec_ctx_t *opt_codec_ctx,
207 asn_struct_ctx_t *ctx, /* Type decoder context */
209 const char *xml_tag, /* Expected XML tag */
210 const void *buf_ptr, size_t size,
211 int (*opt_unexpected_tag_decoder)
212 (void *struct_key, const void *chunk_buf, size_t chunk_size),
213 ssize_t (*body_receiver)
214 (void *struct_key, const void *chunk_buf, size_t chunk_size,
219 ssize_t consumed_myself = 0;
224 * Phases of XER/XML processing:
225 * Phase 0: Check that the opening tag matches our expectations.
226 * Phase 1: Processing body and reacting on closing tag.
228 if(ctx->phase > 1) RETURN(RC_FAIL);
230 pxer_chunk_type_e ch_type; /* XER chunk type */
231 ssize_t ch_size; /* Chunk size */
232 xer_check_tag_e tcv; /* Tag check value */
235 * Get the next part of the XML stream.
237 ch_size = xer_next_token(&ctx->context, buf_ptr, size,
245 case PXER_COMMENT: /* Got XML comment */
246 ADVANCE(ch_size); /* Skip silently */
249 if(ctx->phase == 0) {
251 * We have to ignore whitespace here,
252 * but in order to be forward compatible
253 * with EXTENDED-XER (EMBED-VALUES, #25)
254 * any text is just ignored here.
257 XER_GOT_BODY(buf_ptr, ch_size, size);
262 break; /* Check the rest down there */
266 assert(ch_type == PXER_TAG && size);
268 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
271 * Expecting the opening tag
272 * for the type being processed.
274 * Waiting for the closing XML tag.
278 if(ctx->phase) break;
279 /* Finished decoding of an empty element */
282 ctx->phase = 2; /* Phase out */
285 if(ctx->phase) break;
287 ctx->phase = 1; /* Processing body phase */
290 if(!ctx->phase) break;
292 ctx->phase = 2; /* Phase out */
296 * Certain tags in the body may be expected.
298 if(opt_unexpected_tag_decoder
299 && opt_unexpected_tag_decoder(struct_key,
300 buf_ptr, ch_size) >= 0) {
301 /* Tag's processed fine */
304 /* We are not expecting
305 * the closing tag anymore. */
306 ctx->phase = 2; /* Phase out */
313 break; /* Unexpected tag */
316 ASN_DEBUG("Unexpected XML tag (expected \"%s\")", xml_tag);
317 break; /* Dark and mysterious things have just happened */
325 xer_whitespace_span(const void *chunk_buf, size_t chunk_size) {
326 const char *p = (const char *)chunk_buf;
327 const char *pend = p + chunk_size;
329 for(; p < pend; p++) {
334 * CARRIAGE RETURN (13)
337 case 0x09: case 0x0a: case 0x0d: case 0x20:
344 return (p - (const char *)chunk_buf);
348 * This is a vastly simplified, non-validating XML tree skipper.
351 xer_skip_unknown(xer_check_tag_e tcv, ber_tlv_len_t *depth) {
356 /* These negate each other. */
365 return (tcv == XCT_CLOSING) ? 2 : 1;