blob: 299a7c1eed8ae2bb9056ce3091612e072a6fe069 [file] [log] [blame]
Lev Walkinfc776432005-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 */
Lev Walkindc06f6b2004-10-20 15:50:55 +00005#include <asn_application.h>
6#include <asn_internal.h>
7#include <xer_support.h> /* XER/XML parsing support */
Lev Walkindc06f6b2004-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,
Lev Walkin8c3b8542005-03-10 18:52:02 +000015 void **struct_ptr, const void *buffer, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +000016 asn_codec_ctx_t s_codec_ctx;
17
18 /*
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000019 * Stack checker requires that the codec context
Lev Walkindc06f6b2004-10-20 15:50:55 +000020 * must be allocated on the stack.
21 */
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000022 if(opt_codec_ctx) {
23 if(opt_codec_ctx->max_stack_size) {
24 s_codec_ctx = *opt_codec_ctx;
25 opt_codec_ctx = &s_codec_ctx;
26 }
27 } else {
28 /* If context is not given, be security-conscious anyway */
29 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
Lev Walkin7c1dc052016-03-14 03:08:15 -070030 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
Lev Walkindc06f6b2004-10-20 15:50:55 +000031 opt_codec_ctx = &s_codec_ctx;
32 }
33
34 /*
35 * Invoke type-specific decoder.
36 */
37 return td->xer_decoder(opt_codec_ctx, td, struct_ptr, 0, buffer, size);
38}
39
40
41
42struct xer__cb_arg {
43 pxml_chunk_type_e chunk_type;
44 size_t chunk_size;
Lev Walkin0fab1a62005-03-09 22:19:25 +000045 const void *chunk_buf;
Lev Walkindc06f6b2004-10-20 15:50:55 +000046 int callback_not_invoked;
47};
48
49static int
Lev Walkin0fab1a62005-03-09 22:19:25 +000050xer__token_cb(pxml_chunk_type_e type, const void *_chunk_data, size_t _chunk_size, void *key) {
Lev Walkindc06f6b2004-10-20 15:50:55 +000051 struct xer__cb_arg *arg = (struct xer__cb_arg *)key;
52 arg->chunk_type = type;
53 arg->chunk_size = _chunk_size;
54 arg->chunk_buf = _chunk_data;
55 arg->callback_not_invoked = 0;
56 return -1; /* Terminate the XML parsing */
57}
58
59/*
60 * Fetch the next token from the XER/XML stream.
61 */
62ssize_t
Lev Walkin0fab1a62005-03-09 22:19:25 +000063xer_next_token(int *stateContext, const void *buffer, size_t size, pxer_chunk_type_e *ch_type) {
Lev Walkindc06f6b2004-10-20 15:50:55 +000064 struct xer__cb_arg arg;
Lev Walkin1e443962005-02-18 18:06:36 +000065 int new_stateContext = *stateContext;
Lev Walkindc06f6b2004-10-20 15:50:55 +000066 ssize_t ret;
67
68 arg.callback_not_invoked = 1;
Lev Walkin1e443962005-02-18 18:06:36 +000069 ret = pxml_parse(&new_stateContext, buffer, size, xer__token_cb, &arg);
Lev Walkindc06f6b2004-10-20 15:50:55 +000070 if(ret < 0) return -1;
71 if(arg.callback_not_invoked) {
72 assert(ret == 0); /* No data was consumed */
Lev Walkin97363482016-01-24 19:23:02 -080073 *ch_type = PXER_WMORE;
Lev Walkindc06f6b2004-10-20 15:50:55 +000074 return 0; /* Try again with more data */
75 } else {
76 assert(arg.chunk_size);
77 assert(arg.chunk_buf == buffer);
78 }
79
80 /*
81 * Translate the XML chunk types into more convenient ones.
82 */
83 switch(arg.chunk_type) {
84 case PXML_TEXT:
85 *ch_type = PXER_TEXT;
86 break;
Lev Walkin97363482016-01-24 19:23:02 -080087 case PXML_TAG:
88 *ch_type = PXER_WMORE;
89 return 0; /* Want more */
Lev Walkindc06f6b2004-10-20 15:50:55 +000090 case PXML_TAG_END:
91 *ch_type = PXER_TAG;
92 break;
93 case PXML_COMMENT:
94 case PXML_COMMENT_END:
95 *ch_type = PXER_COMMENT;
96 break;
97 }
98
Lev Walkin1e443962005-02-18 18:06:36 +000099 *stateContext = new_stateContext;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000100 return arg.chunk_size;
101}
102
103#define CSLASH 0x2f /* '/' */
104#define LANGLE 0x3c /* '<' */
105#define RANGLE 0x3e /* '>' */
106
107xer_check_tag_e
108xer_check_tag(const void *buf_ptr, int size, const char *need_tag) {
109 const char *buf = (const char *)buf_ptr;
110 const char *end;
111 xer_check_tag_e ct = XCT_OPENING;
112
113 if(size < 2 || buf[0] != LANGLE || buf[size-1] != RANGLE) {
Lev Walkinc61f3862005-02-14 17:21:22 +0000114 if(size >= 2)
Lev Walkinc9c6f4a2012-09-04 13:14:03 -0700115 ASN_DEBUG("Broken XML tag: \"%c...%c\"",
116 buf[0], buf[size - 1]);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000117 return XCT_BROKEN;
118 }
119
120 /*
121 * Determine the tag class.
122 */
123 if(buf[1] == CSLASH) {
124 buf += 2; /* advance past "</" */
125 size -= 3; /* strip "</" and ">" */
126 ct = XCT_CLOSING;
127 if(size > 0 && buf[size-1] == CSLASH)
128 return XCT_BROKEN; /* </abc/> */
129 } else {
130 buf++; /* advance past "<" */
131 size -= 2; /* strip "<" and ">" */
132 if(size > 0 && buf[size-1] == CSLASH) {
133 ct = XCT_BOTH;
134 size--; /* One more, for "/" */
135 }
136 }
137
Lev Walkind1bfea62005-11-08 03:06:16 +0000138 /* Sometimes we don't care about the tag */
139 if(!need_tag || !*need_tag)
140 return (xer_check_tag_e)(XCT__UNK__MASK | ct);
141
Lev Walkindc06f6b2004-10-20 15:50:55 +0000142 /*
143 * Determine the tag name.
144 */
145 for(end = buf + size; buf < end; buf++, need_tag++) {
146 int b = *buf, n = *need_tag;
147 if(b != n) {
148 if(n == 0) {
149 switch(b) {
150 case 0x09: case 0x0a: case 0x0c: case 0x0d:
151 case 0x20:
152 /* "<abc def/>": whitespace is normal */
153 return ct;
154 }
155 }
Lev Walkin1e443962005-02-18 18:06:36 +0000156 return (xer_check_tag_e)(XCT__UNK__MASK | ct);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000157 }
158 if(b == 0)
159 return XCT_BROKEN; /* Embedded 0 in buf?! */
160 }
Lev Walkin904e65b2005-02-18 14:23:48 +0000161 if(*need_tag)
Lev Walkin1e443962005-02-18 18:06:36 +0000162 return (xer_check_tag_e)(XCT__UNK__MASK | ct);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000163
164 return ct;
165}
166
167
168#undef ADVANCE
169#define ADVANCE(num_bytes) do { \
170 size_t num = (num_bytes); \
Lev Walkin0fab1a62005-03-09 22:19:25 +0000171 buf_ptr = ((const char *)buf_ptr) + num; \
Lev Walkindc06f6b2004-10-20 15:50:55 +0000172 size -= num; \
173 consumed_myself += num; \
174 } while(0)
175
176#undef RETURN
177#define RETURN(_code) do { \
178 rval.code = _code; \
179 rval.consumed = consumed_myself; \
Lev Walkin00918812006-09-18 21:19:32 +0000180 if(rval.code != RC_OK) \
181 ASN_DEBUG("Failed with %d", rval.code); \
Lev Walkindc06f6b2004-10-20 15:50:55 +0000182 return rval; \
183 } while(0)
184
Lev Walkin9357ec12005-02-18 12:26:20 +0000185#define XER_GOT_BODY(chunk_buf, chunk_size, size) do { \
Lev Walkindc06f6b2004-10-20 15:50:55 +0000186 ssize_t converted_size = body_receiver \
Lev Walkin3256d6f2004-10-21 11:22:12 +0000187 (struct_key, chunk_buf, chunk_size, \
Lev Walkindc06f6b2004-10-20 15:50:55 +0000188 (size_t)chunk_size < size); \
189 if(converted_size == -1) RETURN(RC_FAIL); \
Lev Walkin1e443962005-02-18 18:06:36 +0000190 if(converted_size == 0 \
191 && size == (size_t)chunk_size) \
Lev Walkin9357ec12005-02-18 12:26:20 +0000192 RETURN(RC_WMORE); \
Lev Walkindc06f6b2004-10-20 15:50:55 +0000193 chunk_size = converted_size; \
194 } while(0)
195#define XER_GOT_EMPTY() do { \
Lev Walkin9357ec12005-02-18 12:26:20 +0000196 if(body_receiver(struct_key, 0, 0, size > 0) == -1) \
197 RETURN(RC_FAIL); \
Lev Walkindc06f6b2004-10-20 15:50:55 +0000198 } while(0)
199
200/*
201 * Generalized function for decoding the primitive values.
202 */
203asn_dec_rval_t
204xer_decode_general(asn_codec_ctx_t *opt_codec_ctx,
205 asn_struct_ctx_t *ctx, /* Type decoder context */
Lev Walkin3256d6f2004-10-21 11:22:12 +0000206 void *struct_key,
Lev Walkindc06f6b2004-10-20 15:50:55 +0000207 const char *xml_tag, /* Expected XML tag */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000208 const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +0000209 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000210 (void *struct_key, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +0000211 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000212 (void *struct_key, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +0000213 int have_more)
214 ) {
215
216 asn_dec_rval_t rval;
217 ssize_t consumed_myself = 0;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000218
219 (void)opt_codec_ctx;
220
221 /*
222 * Phases of XER/XML processing:
223 * Phase 0: Check that the opening tag matches our expectations.
224 * Phase 1: Processing body and reacting on closing tag.
225 */
226 if(ctx->phase > 1) RETURN(RC_FAIL);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000227 for(;;) {
Lev Walkind2de48a2004-10-23 13:27:03 +0000228 pxer_chunk_type_e ch_type; /* XER chunk type */
229 ssize_t ch_size; /* Chunk size */
230 xer_check_tag_e tcv; /* Tag check value */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000231
232 /*
233 * Get the next part of the XML stream.
234 */
Lev Walkin1e443962005-02-18 18:06:36 +0000235 ch_size = xer_next_token(&ctx->context, buf_ptr, size,
236 &ch_type);
Lev Walkin97363482016-01-24 19:23:02 -0800237 if(ch_size == -1) {
238 RETURN(RC_FAIL);
239 } else {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000240 switch(ch_type) {
Lev Walkin97363482016-01-24 19:23:02 -0800241 case PXER_WMORE:
242 RETURN(RC_WMORE);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000243 case PXER_COMMENT: /* Got XML comment */
244 ADVANCE(ch_size); /* Skip silently */
245 continue;
246 case PXER_TEXT:
247 if(ctx->phase == 0) {
Lev Walkincfeecfb2004-10-21 05:44:11 +0000248 /*
249 * We have to ignore whitespace here,
250 * but in order to be forward compatible
251 * with EXTENDED-XER (EMBED-VALUES, #25)
252 * any text is just ignored here.
253 */
254 } else {
Lev Walkin9357ec12005-02-18 12:26:20 +0000255 XER_GOT_BODY(buf_ptr, ch_size, size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000256 }
Lev Walkindc06f6b2004-10-20 15:50:55 +0000257 ADVANCE(ch_size);
258 continue;
259 case PXER_TAG:
260 break; /* Check the rest down there */
261 }
262 }
263
264 assert(ch_type == PXER_TAG && size);
265
266 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
Lev Walkind2de48a2004-10-23 13:27:03 +0000267 /*
268 * Phase 0:
269 * Expecting the opening tag
270 * for the type being processed.
271 * Phase 1:
272 * Waiting for the closing XML tag.
273 */
274 switch(tcv) {
275 case XCT_BOTH:
276 if(ctx->phase) break;
277 /* Finished decoding of an empty element */
278 XER_GOT_EMPTY();
279 ADVANCE(ch_size);
280 ctx->phase = 2; /* Phase out */
281 RETURN(RC_OK);
282 case XCT_OPENING:
283 if(ctx->phase) break;
284 ADVANCE(ch_size);
285 ctx->phase = 1; /* Processing body phase */
286 continue;
287 case XCT_CLOSING:
288 if(!ctx->phase) break;
289 ADVANCE(ch_size);
290 ctx->phase = 2; /* Phase out */
291 RETURN(RC_OK);
Lev Walkin904e65b2005-02-18 14:23:48 +0000292 case XCT_UNKNOWN_BO:
Lev Walkindc06f6b2004-10-20 15:50:55 +0000293 /*
Lev Walkind2de48a2004-10-23 13:27:03 +0000294 * Certain tags in the body may be expected.
Lev Walkindc06f6b2004-10-20 15:50:55 +0000295 */
Lev Walkind2de48a2004-10-23 13:27:03 +0000296 if(opt_unexpected_tag_decoder
297 && opt_unexpected_tag_decoder(struct_key,
Lev Walkin4efbfb72005-02-25 14:20:30 +0000298 buf_ptr, ch_size) >= 0) {
Lev Walkind2de48a2004-10-23 13:27:03 +0000299 /* Tag's processed fine */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000300 ADVANCE(ch_size);
Lev Walkine0b56e02005-02-25 12:10:27 +0000301 if(!ctx->phase) {
302 /* We are not expecting
303 * the closing tag anymore. */
304 ctx->phase = 2; /* Phase out */
305 RETURN(RC_OK);
306 }
Lev Walkindc06f6b2004-10-20 15:50:55 +0000307 continue;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000308 }
Lev Walkind2de48a2004-10-23 13:27:03 +0000309 /* Fall through */
310 default:
311 break; /* Unexpected tag */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000312 }
Lev Walkind2de48a2004-10-23 13:27:03 +0000313
Lev Walkin642962a2005-02-24 22:37:07 +0000314 ASN_DEBUG("Unexpected XML tag (expected \"%s\")", xml_tag);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000315 break; /* Dark and mysterious things have just happened */
316 }
317
318 RETURN(RC_FAIL);
319}
320
Lev Walkindde25b32004-10-22 08:17:16 +0000321
Lev Walkinf7982282013-03-16 07:01:42 -0700322size_t
323xer_whitespace_span(const void *chunk_buf, size_t chunk_size) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000324 const char *p = (const char *)chunk_buf;
325 const char *pend = p + chunk_size;
Lev Walkindde25b32004-10-22 08:17:16 +0000326
327 for(; p < pend; p++) {
328 switch(*p) {
329 /* X.693, #8.1.4
330 * HORISONTAL TAB (9)
331 * LINE FEED (10)
332 * CARRIAGE RETURN (13)
333 * SPACE (32)
334 */
335 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinf7982282013-03-16 07:01:42 -0700336 continue;
Lev Walkindde25b32004-10-22 08:17:16 +0000337 default:
Lev Walkinf7982282013-03-16 07:01:42 -0700338 break;
Lev Walkindde25b32004-10-22 08:17:16 +0000339 }
Lev Walkinf7982282013-03-16 07:01:42 -0700340 break;
Lev Walkindde25b32004-10-22 08:17:16 +0000341 }
Lev Walkinf7982282013-03-16 07:01:42 -0700342 return (p - (const char *)chunk_buf);
Lev Walkindde25b32004-10-22 08:17:16 +0000343}
344
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000345/*
346 * This is a vastly simplified, non-validating XML tree skipper.
347 */
348int
349xer_skip_unknown(xer_check_tag_e tcv, ber_tlv_len_t *depth) {
350 assert(*depth > 0);
351 switch(tcv) {
352 case XCT_BOTH:
353 case XCT_UNKNOWN_BO:
354 /* These negate each other. */
355 return 0;
356 case XCT_OPENING:
357 case XCT_UNKNOWN_OP:
358 ++(*depth);
359 return 0;
360 case XCT_CLOSING:
361 case XCT_UNKNOWN_CL:
362 if(--(*depth) == 0)
363 return (tcv == XCT_CLOSING) ? 2 : 1;
364 return 0;
365 default:
366 return -1;
367 }
368}