blob: e48a5ed0eaf320c2f9f2163e5e279a56d3e075c1 [file] [log] [blame]
vlm5ec2fe12005-03-29 17:21:14 +00001/*
2 * Copyright (c) 2004, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
vlm9de248e2004-10-20 15:50:55 +00005#include <asn_application.h>
6#include <asn_internal.h>
7#include <xer_support.h> /* XER/XML parsing support */
vlm9de248e2004-10-20 15:50:55 +00008
9
10/*
11 * Decode the XER encoding of a given type.
12 */
13asn_dec_rval_t
14xer_decode(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
vlmb02dcc62005-03-10 18:52:02 +000015 void **struct_ptr, const void *buffer, size_t size) {
vlm9de248e2004-10-20 15:50:55 +000016 asn_codec_ctx_t s_codec_ctx;
17
18 /*
19 * Satisfy the requirement that the codec context
20 * must be allocated on the stack.
21 */
22 if(opt_codec_ctx && opt_codec_ctx->max_stack_size) {
23 s_codec_ctx = *opt_codec_ctx;
24 opt_codec_ctx = &s_codec_ctx;
25 }
26
27 /*
28 * Invoke type-specific decoder.
29 */
30 return td->xer_decoder(opt_codec_ctx, td, struct_ptr, 0, buffer, size);
31}
32
33
34
35struct xer__cb_arg {
36 pxml_chunk_type_e chunk_type;
37 size_t chunk_size;
vlm4df9cc12005-03-09 22:19:25 +000038 const void *chunk_buf;
vlm9de248e2004-10-20 15:50:55 +000039 int callback_not_invoked;
40};
41
42static int
vlm4df9cc12005-03-09 22:19:25 +000043xer__token_cb(pxml_chunk_type_e type, const void *_chunk_data, size_t _chunk_size, void *key) {
vlm9de248e2004-10-20 15:50:55 +000044 struct xer__cb_arg *arg = (struct xer__cb_arg *)key;
45 arg->chunk_type = type;
46 arg->chunk_size = _chunk_size;
47 arg->chunk_buf = _chunk_data;
48 arg->callback_not_invoked = 0;
49 return -1; /* Terminate the XML parsing */
50}
51
52/*
53 * Fetch the next token from the XER/XML stream.
54 */
55ssize_t
vlm4df9cc12005-03-09 22:19:25 +000056xer_next_token(int *stateContext, const void *buffer, size_t size, pxer_chunk_type_e *ch_type) {
vlm9de248e2004-10-20 15:50:55 +000057 struct xer__cb_arg arg;
vlmc70f8f32005-02-18 18:06:36 +000058 int new_stateContext = *stateContext;
vlm9de248e2004-10-20 15:50:55 +000059 ssize_t ret;
60
61 arg.callback_not_invoked = 1;
vlmc70f8f32005-02-18 18:06:36 +000062 ret = pxml_parse(&new_stateContext, buffer, size, xer__token_cb, &arg);
vlm9de248e2004-10-20 15:50:55 +000063 if(ret < 0) return -1;
64 if(arg.callback_not_invoked) {
65 assert(ret == 0); /* No data was consumed */
66 return 0; /* Try again with more data */
67 } else {
68 assert(arg.chunk_size);
69 assert(arg.chunk_buf == buffer);
70 }
71
72 /*
73 * Translate the XML chunk types into more convenient ones.
74 */
75 switch(arg.chunk_type) {
76 case PXML_TEXT:
77 *ch_type = PXER_TEXT;
78 break;
79 case PXML_TAG: return 0; /* Want more */
80 case PXML_TAG_END:
81 *ch_type = PXER_TAG;
82 break;
83 case PXML_COMMENT:
84 case PXML_COMMENT_END:
85 *ch_type = PXER_COMMENT;
86 break;
87 }
88
vlmc70f8f32005-02-18 18:06:36 +000089 *stateContext = new_stateContext;
vlm9de248e2004-10-20 15:50:55 +000090 return arg.chunk_size;
91}
92
93#define CSLASH 0x2f /* '/' */
94#define LANGLE 0x3c /* '<' */
95#define RANGLE 0x3e /* '>' */
96
97xer_check_tag_e
98xer_check_tag(const void *buf_ptr, int size, const char *need_tag) {
99 const char *buf = (const char *)buf_ptr;
100 const char *end;
101 xer_check_tag_e ct = XCT_OPENING;
102
103 if(size < 2 || buf[0] != LANGLE || buf[size-1] != RANGLE) {
vlm390a9022005-02-14 17:21:22 +0000104 if(size >= 2)
105 ASN_DEBUG("Broken XML tag: \"%c...%c\"", buf[0], buf[size - 1]);
vlm9de248e2004-10-20 15:50:55 +0000106 return XCT_BROKEN;
107 }
108
109 /*
110 * Determine the tag class.
111 */
112 if(buf[1] == CSLASH) {
113 buf += 2; /* advance past "</" */
114 size -= 3; /* strip "</" and ">" */
115 ct = XCT_CLOSING;
116 if(size > 0 && buf[size-1] == CSLASH)
117 return XCT_BROKEN; /* </abc/> */
118 } else {
119 buf++; /* advance past "<" */
120 size -= 2; /* strip "<" and ">" */
121 if(size > 0 && buf[size-1] == CSLASH) {
122 ct = XCT_BOTH;
123 size--; /* One more, for "/" */
124 }
125 }
126
vlm6d44a542005-11-08 03:06:16 +0000127 /* Sometimes we don't care about the tag */
128 if(!need_tag || !*need_tag)
129 return (xer_check_tag_e)(XCT__UNK__MASK | ct);
130
vlm9de248e2004-10-20 15:50:55 +0000131 /*
132 * Determine the tag name.
133 */
134 for(end = buf + size; buf < end; buf++, need_tag++) {
135 int b = *buf, n = *need_tag;
136 if(b != n) {
137 if(n == 0) {
138 switch(b) {
139 case 0x09: case 0x0a: case 0x0c: case 0x0d:
140 case 0x20:
141 /* "<abc def/>": whitespace is normal */
142 return ct;
143 }
144 }
vlmc70f8f32005-02-18 18:06:36 +0000145 return (xer_check_tag_e)(XCT__UNK__MASK | ct);
vlm9de248e2004-10-20 15:50:55 +0000146 }
147 if(b == 0)
148 return XCT_BROKEN; /* Embedded 0 in buf?! */
149 }
vlm957da5b2005-02-18 14:23:48 +0000150 if(*need_tag)
vlmc70f8f32005-02-18 18:06:36 +0000151 return (xer_check_tag_e)(XCT__UNK__MASK | ct);
vlm9de248e2004-10-20 15:50:55 +0000152
153 return ct;
154}
155
156
157#undef ADVANCE
158#define ADVANCE(num_bytes) do { \
159 size_t num = (num_bytes); \
vlm4df9cc12005-03-09 22:19:25 +0000160 buf_ptr = ((const char *)buf_ptr) + num; \
vlm9de248e2004-10-20 15:50:55 +0000161 size -= num; \
162 consumed_myself += num; \
163 } while(0)
164
165#undef RETURN
166#define RETURN(_code) do { \
167 rval.code = _code; \
168 rval.consumed = consumed_myself; \
169 return rval; \
170 } while(0)
171
vlm88c50282005-02-18 12:26:20 +0000172#define XER_GOT_BODY(chunk_buf, chunk_size, size) do { \
vlm9de248e2004-10-20 15:50:55 +0000173 ssize_t converted_size = body_receiver \
vlm3b6e1122004-10-21 11:22:12 +0000174 (struct_key, chunk_buf, chunk_size, \
vlm9de248e2004-10-20 15:50:55 +0000175 (size_t)chunk_size < size); \
176 if(converted_size == -1) RETURN(RC_FAIL); \
vlmc70f8f32005-02-18 18:06:36 +0000177 if(converted_size == 0 \
178 && size == (size_t)chunk_size) \
vlm88c50282005-02-18 12:26:20 +0000179 RETURN(RC_WMORE); \
vlm9de248e2004-10-20 15:50:55 +0000180 chunk_size = converted_size; \
181 } while(0)
182#define XER_GOT_EMPTY() do { \
vlm88c50282005-02-18 12:26:20 +0000183 if(body_receiver(struct_key, 0, 0, size > 0) == -1) \
184 RETURN(RC_FAIL); \
vlm9de248e2004-10-20 15:50:55 +0000185 } while(0)
186
187/*
188 * Generalized function for decoding the primitive values.
189 */
190asn_dec_rval_t
191xer_decode_general(asn_codec_ctx_t *opt_codec_ctx,
192 asn_struct_ctx_t *ctx, /* Type decoder context */
vlm3b6e1122004-10-21 11:22:12 +0000193 void *struct_key,
vlm9de248e2004-10-20 15:50:55 +0000194 const char *xml_tag, /* Expected XML tag */
vlm4df9cc12005-03-09 22:19:25 +0000195 const void *buf_ptr, size_t size,
vlm9de248e2004-10-20 15:50:55 +0000196 int (*opt_unexpected_tag_decoder)
vlm4df9cc12005-03-09 22:19:25 +0000197 (void *struct_key, const void *chunk_buf, size_t chunk_size),
vlm9de248e2004-10-20 15:50:55 +0000198 ssize_t (*body_receiver)
vlm4df9cc12005-03-09 22:19:25 +0000199 (void *struct_key, const void *chunk_buf, size_t chunk_size,
vlm9de248e2004-10-20 15:50:55 +0000200 int have_more)
201 ) {
202
203 asn_dec_rval_t rval;
204 ssize_t consumed_myself = 0;
vlm9de248e2004-10-20 15:50:55 +0000205
206 (void)opt_codec_ctx;
207
208 /*
209 * Phases of XER/XML processing:
210 * Phase 0: Check that the opening tag matches our expectations.
211 * Phase 1: Processing body and reacting on closing tag.
212 */
213 if(ctx->phase > 1) RETURN(RC_FAIL);
vlmcb7222f2005-02-18 16:10:40 +0000214 for(;;) {
vlmb8404852004-10-23 13:27:03 +0000215 pxer_chunk_type_e ch_type; /* XER chunk type */
216 ssize_t ch_size; /* Chunk size */
217 xer_check_tag_e tcv; /* Tag check value */
vlm9de248e2004-10-20 15:50:55 +0000218
219 /*
220 * Get the next part of the XML stream.
221 */
vlmc70f8f32005-02-18 18:06:36 +0000222 ch_size = xer_next_token(&ctx->context, buf_ptr, size,
223 &ch_type);
vlm9de248e2004-10-20 15:50:55 +0000224 switch(ch_size) {
225 case -1: RETURN(RC_FAIL);
226 case 0:
vlm9de248e2004-10-20 15:50:55 +0000227 RETURN(RC_WMORE);
228 default:
229 switch(ch_type) {
230 case PXER_COMMENT: /* Got XML comment */
231 ADVANCE(ch_size); /* Skip silently */
232 continue;
233 case PXER_TEXT:
234 if(ctx->phase == 0) {
vlm7958f112004-10-21 05:44:11 +0000235 /*
236 * We have to ignore whitespace here,
237 * but in order to be forward compatible
238 * with EXTENDED-XER (EMBED-VALUES, #25)
239 * any text is just ignored here.
240 */
241 } else {
vlm88c50282005-02-18 12:26:20 +0000242 XER_GOT_BODY(buf_ptr, ch_size, size);
vlm9de248e2004-10-20 15:50:55 +0000243 }
vlm9de248e2004-10-20 15:50:55 +0000244 ADVANCE(ch_size);
245 continue;
246 case PXER_TAG:
247 break; /* Check the rest down there */
248 }
249 }
250
251 assert(ch_type == PXER_TAG && size);
252
253 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
vlmb8404852004-10-23 13:27:03 +0000254 /*
255 * Phase 0:
256 * Expecting the opening tag
257 * for the type being processed.
258 * Phase 1:
259 * Waiting for the closing XML tag.
260 */
261 switch(tcv) {
262 case XCT_BOTH:
263 if(ctx->phase) break;
264 /* Finished decoding of an empty element */
265 XER_GOT_EMPTY();
266 ADVANCE(ch_size);
267 ctx->phase = 2; /* Phase out */
268 RETURN(RC_OK);
269 case XCT_OPENING:
270 if(ctx->phase) break;
271 ADVANCE(ch_size);
272 ctx->phase = 1; /* Processing body phase */
273 continue;
274 case XCT_CLOSING:
275 if(!ctx->phase) break;
276 ADVANCE(ch_size);
277 ctx->phase = 2; /* Phase out */
278 RETURN(RC_OK);
vlm957da5b2005-02-18 14:23:48 +0000279 case XCT_UNKNOWN_BO:
vlm9de248e2004-10-20 15:50:55 +0000280 /*
vlmb8404852004-10-23 13:27:03 +0000281 * Certain tags in the body may be expected.
vlm9de248e2004-10-20 15:50:55 +0000282 */
vlmb8404852004-10-23 13:27:03 +0000283 if(opt_unexpected_tag_decoder
284 && opt_unexpected_tag_decoder(struct_key,
vlm8a09e0f2005-02-25 14:20:30 +0000285 buf_ptr, ch_size) >= 0) {
vlmb8404852004-10-23 13:27:03 +0000286 /* Tag's processed fine */
vlm9de248e2004-10-20 15:50:55 +0000287 ADVANCE(ch_size);
vlm80a48592005-02-25 12:10:27 +0000288 if(!ctx->phase) {
289 /* We are not expecting
290 * the closing tag anymore. */
291 ctx->phase = 2; /* Phase out */
292 RETURN(RC_OK);
293 }
vlm9de248e2004-10-20 15:50:55 +0000294 continue;
vlm9de248e2004-10-20 15:50:55 +0000295 }
vlmb8404852004-10-23 13:27:03 +0000296 /* Fall through */
297 default:
298 break; /* Unexpected tag */
vlm9de248e2004-10-20 15:50:55 +0000299 }
vlmb8404852004-10-23 13:27:03 +0000300
vlmaa930cb2005-02-24 22:37:07 +0000301 ASN_DEBUG("Unexpected XML tag (expected \"%s\")", xml_tag);
vlm9de248e2004-10-20 15:50:55 +0000302 break; /* Dark and mysterious things have just happened */
303 }
304
305 RETURN(RC_FAIL);
306}
307
vlm2a4245f2004-10-22 08:17:16 +0000308
309int
vlm4df9cc12005-03-09 22:19:25 +0000310xer_is_whitespace(const void *chunk_buf, size_t chunk_size) {
311 const char *p = (const char *)chunk_buf;
312 const char *pend = p + chunk_size;
vlm2a4245f2004-10-22 08:17:16 +0000313
314 for(; p < pend; p++) {
315 switch(*p) {
316 /* X.693, #8.1.4
317 * HORISONTAL TAB (9)
318 * LINE FEED (10)
319 * CARRIAGE RETURN (13)
320 * SPACE (32)
321 */
322 case 0x09: case 0x0a: case 0x0d: case 0x20:
323 break;
324 default:
325 return 0;
326 }
327 }
328 return 1; /* All whitespace */
329}
330
vlmcb7222f2005-02-18 16:10:40 +0000331/*
332 * This is a vastly simplified, non-validating XML tree skipper.
333 */
334int
335xer_skip_unknown(xer_check_tag_e tcv, ber_tlv_len_t *depth) {
336 assert(*depth > 0);
337 switch(tcv) {
338 case XCT_BOTH:
339 case XCT_UNKNOWN_BO:
340 /* These negate each other. */
341 return 0;
342 case XCT_OPENING:
343 case XCT_UNKNOWN_OP:
344 ++(*depth);
345 return 0;
346 case XCT_CLOSING:
347 case XCT_UNKNOWN_CL:
348 if(--(*depth) == 0)
349 return (tcv == XCT_CLOSING) ? 2 : 1;
350 return 0;
351 default:
352 return -1;
353 }
354}