vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1 | |
| 2 | #include <asn_application.h> |
| 3 | #include <asn_internal.h> |
| 4 | #include <xer_support.h> /* XER/XML parsing support */ |
| 5 | #include <assert.h> |
| 6 | |
| 7 | |
| 8 | /* |
| 9 | * Decode the XER encoding of a given type. |
| 10 | */ |
| 11 | asn_dec_rval_t |
| 12 | xer_decode(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 13 | void **struct_ptr, void *buffer, size_t size) { |
| 14 | asn_codec_ctx_t s_codec_ctx; |
| 15 | |
| 16 | /* |
| 17 | * Satisfy the requirement that the codec context |
| 18 | * must be allocated on the stack. |
| 19 | */ |
| 20 | if(opt_codec_ctx && opt_codec_ctx->max_stack_size) { |
| 21 | s_codec_ctx = *opt_codec_ctx; |
| 22 | opt_codec_ctx = &s_codec_ctx; |
| 23 | } |
| 24 | |
| 25 | /* |
| 26 | * Invoke type-specific decoder. |
| 27 | */ |
| 28 | return td->xer_decoder(opt_codec_ctx, td, struct_ptr, 0, buffer, size); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | |
| 33 | struct xer__cb_arg { |
| 34 | pxml_chunk_type_e chunk_type; |
| 35 | size_t chunk_size; |
| 36 | void *chunk_buf; |
| 37 | int callback_not_invoked; |
| 38 | }; |
| 39 | |
| 40 | static int |
| 41 | xer__token_cb(pxml_chunk_type_e type, void *_chunk_data, size_t _chunk_size, void *key) { |
| 42 | struct xer__cb_arg *arg = (struct xer__cb_arg *)key; |
| 43 | arg->chunk_type = type; |
| 44 | arg->chunk_size = _chunk_size; |
| 45 | arg->chunk_buf = _chunk_data; |
| 46 | arg->callback_not_invoked = 0; |
| 47 | return -1; /* Terminate the XML parsing */ |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Fetch the next token from the XER/XML stream. |
| 52 | */ |
| 53 | ssize_t |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 54 | xer_next_token(int *stateContext, void *buffer, size_t size, pxer_chunk_type_e *ch_type) { |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 55 | struct xer__cb_arg arg; |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 56 | int new_stateContext = *stateContext; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 57 | ssize_t ret; |
| 58 | |
| 59 | arg.callback_not_invoked = 1; |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 60 | ret = pxml_parse(&new_stateContext, buffer, size, xer__token_cb, &arg); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 61 | if(ret < 0) return -1; |
| 62 | if(arg.callback_not_invoked) { |
| 63 | assert(ret == 0); /* No data was consumed */ |
| 64 | return 0; /* Try again with more data */ |
| 65 | } else { |
| 66 | assert(arg.chunk_size); |
| 67 | assert(arg.chunk_buf == buffer); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Translate the XML chunk types into more convenient ones. |
| 72 | */ |
| 73 | switch(arg.chunk_type) { |
| 74 | case PXML_TEXT: |
| 75 | *ch_type = PXER_TEXT; |
| 76 | break; |
| 77 | case PXML_TAG: return 0; /* Want more */ |
| 78 | case PXML_TAG_END: |
| 79 | *ch_type = PXER_TAG; |
| 80 | break; |
| 81 | case PXML_COMMENT: |
| 82 | case PXML_COMMENT_END: |
| 83 | *ch_type = PXER_COMMENT; |
| 84 | break; |
| 85 | } |
| 86 | |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 87 | *stateContext = new_stateContext; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 88 | return arg.chunk_size; |
| 89 | } |
| 90 | |
| 91 | #define CSLASH 0x2f /* '/' */ |
| 92 | #define LANGLE 0x3c /* '<' */ |
| 93 | #define RANGLE 0x3e /* '>' */ |
| 94 | |
| 95 | xer_check_tag_e |
| 96 | xer_check_tag(const void *buf_ptr, int size, const char *need_tag) { |
| 97 | const char *buf = (const char *)buf_ptr; |
| 98 | const char *end; |
| 99 | xer_check_tag_e ct = XCT_OPENING; |
| 100 | |
| 101 | if(size < 2 || buf[0] != LANGLE || buf[size-1] != RANGLE) { |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 102 | if(size >= 2) |
| 103 | ASN_DEBUG("Broken XML tag: \"%c...%c\"", buf[0], buf[size - 1]); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 104 | return XCT_BROKEN; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Determine the tag class. |
| 109 | */ |
| 110 | if(buf[1] == CSLASH) { |
| 111 | buf += 2; /* advance past "</" */ |
| 112 | size -= 3; /* strip "</" and ">" */ |
| 113 | ct = XCT_CLOSING; |
| 114 | if(size > 0 && buf[size-1] == CSLASH) |
| 115 | return XCT_BROKEN; /* </abc/> */ |
| 116 | } else { |
| 117 | buf++; /* advance past "<" */ |
| 118 | size -= 2; /* strip "<" and ">" */ |
| 119 | if(size > 0 && buf[size-1] == CSLASH) { |
| 120 | ct = XCT_BOTH; |
| 121 | size--; /* One more, for "/" */ |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Determine the tag name. |
| 127 | */ |
| 128 | for(end = buf + size; buf < end; buf++, need_tag++) { |
| 129 | int b = *buf, n = *need_tag; |
| 130 | if(b != n) { |
| 131 | if(n == 0) { |
| 132 | switch(b) { |
| 133 | case 0x09: case 0x0a: case 0x0c: case 0x0d: |
| 134 | case 0x20: |
| 135 | /* "<abc def/>": whitespace is normal */ |
| 136 | return ct; |
| 137 | } |
| 138 | } |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 139 | return (xer_check_tag_e)(XCT__UNK__MASK | ct); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 140 | } |
| 141 | if(b == 0) |
| 142 | return XCT_BROKEN; /* Embedded 0 in buf?! */ |
| 143 | } |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 144 | if(*need_tag) |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 145 | return (xer_check_tag_e)(XCT__UNK__MASK | ct); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 146 | |
| 147 | return ct; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | #undef ADVANCE |
| 152 | #define ADVANCE(num_bytes) do { \ |
| 153 | size_t num = (num_bytes); \ |
| 154 | buf_ptr = ((char *)buf_ptr) + num; \ |
| 155 | size -= num; \ |
| 156 | consumed_myself += num; \ |
| 157 | } while(0) |
| 158 | |
| 159 | #undef RETURN |
| 160 | #define RETURN(_code) do { \ |
| 161 | rval.code = _code; \ |
| 162 | rval.consumed = consumed_myself; \ |
| 163 | return rval; \ |
| 164 | } while(0) |
| 165 | |
vlm | 88c5028 | 2005-02-18 12:26:20 +0000 | [diff] [blame] | 166 | #define XER_GOT_BODY(chunk_buf, chunk_size, size) do { \ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 167 | ssize_t converted_size = body_receiver \ |
vlm | 3b6e112 | 2004-10-21 11:22:12 +0000 | [diff] [blame] | 168 | (struct_key, chunk_buf, chunk_size, \ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 169 | (size_t)chunk_size < size); \ |
| 170 | if(converted_size == -1) RETURN(RC_FAIL); \ |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 171 | if(converted_size == 0 \ |
| 172 | && size == (size_t)chunk_size) \ |
vlm | 88c5028 | 2005-02-18 12:26:20 +0000 | [diff] [blame] | 173 | RETURN(RC_WMORE); \ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 174 | chunk_size = converted_size; \ |
| 175 | } while(0) |
| 176 | #define XER_GOT_EMPTY() do { \ |
vlm | 88c5028 | 2005-02-18 12:26:20 +0000 | [diff] [blame] | 177 | if(body_receiver(struct_key, 0, 0, size > 0) == -1) \ |
| 178 | RETURN(RC_FAIL); \ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 179 | } while(0) |
| 180 | |
| 181 | /* |
| 182 | * Generalized function for decoding the primitive values. |
| 183 | */ |
| 184 | asn_dec_rval_t |
| 185 | xer_decode_general(asn_codec_ctx_t *opt_codec_ctx, |
| 186 | asn_struct_ctx_t *ctx, /* Type decoder context */ |
vlm | 3b6e112 | 2004-10-21 11:22:12 +0000 | [diff] [blame] | 187 | void *struct_key, |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 188 | const char *xml_tag, /* Expected XML tag */ |
| 189 | void *buf_ptr, size_t size, |
| 190 | int (*opt_unexpected_tag_decoder) |
vlm | 3b6e112 | 2004-10-21 11:22:12 +0000 | [diff] [blame] | 191 | (void *struct_key, void *chunk_buf, size_t chunk_size), |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 192 | ssize_t (*body_receiver) |
vlm | 3b6e112 | 2004-10-21 11:22:12 +0000 | [diff] [blame] | 193 | (void *struct_key, void *chunk_buf, size_t chunk_size, |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 194 | int have_more) |
| 195 | ) { |
| 196 | |
| 197 | asn_dec_rval_t rval; |
| 198 | ssize_t consumed_myself = 0; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 199 | |
| 200 | (void)opt_codec_ctx; |
| 201 | |
| 202 | /* |
| 203 | * Phases of XER/XML processing: |
| 204 | * Phase 0: Check that the opening tag matches our expectations. |
| 205 | * Phase 1: Processing body and reacting on closing tag. |
| 206 | */ |
| 207 | if(ctx->phase > 1) RETURN(RC_FAIL); |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 208 | for(;;) { |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 209 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 210 | ssize_t ch_size; /* Chunk size */ |
| 211 | xer_check_tag_e tcv; /* Tag check value */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Get the next part of the XML stream. |
| 215 | */ |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 216 | ch_size = xer_next_token(&ctx->context, buf_ptr, size, |
| 217 | &ch_type); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 218 | switch(ch_size) { |
| 219 | case -1: RETURN(RC_FAIL); |
| 220 | case 0: |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 221 | RETURN(RC_WMORE); |
| 222 | default: |
| 223 | switch(ch_type) { |
| 224 | case PXER_COMMENT: /* Got XML comment */ |
| 225 | ADVANCE(ch_size); /* Skip silently */ |
| 226 | continue; |
| 227 | case PXER_TEXT: |
| 228 | if(ctx->phase == 0) { |
vlm | 7958f11 | 2004-10-21 05:44:11 +0000 | [diff] [blame] | 229 | /* |
| 230 | * We have to ignore whitespace here, |
| 231 | * but in order to be forward compatible |
| 232 | * with EXTENDED-XER (EMBED-VALUES, #25) |
| 233 | * any text is just ignored here. |
| 234 | */ |
| 235 | } else { |
vlm | 88c5028 | 2005-02-18 12:26:20 +0000 | [diff] [blame] | 236 | XER_GOT_BODY(buf_ptr, ch_size, size); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 237 | } |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 238 | ADVANCE(ch_size); |
| 239 | continue; |
| 240 | case PXER_TAG: |
| 241 | break; /* Check the rest down there */ |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | assert(ch_type == PXER_TAG && size); |
| 246 | |
| 247 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 248 | /* |
| 249 | * Phase 0: |
| 250 | * Expecting the opening tag |
| 251 | * for the type being processed. |
| 252 | * Phase 1: |
| 253 | * Waiting for the closing XML tag. |
| 254 | */ |
| 255 | switch(tcv) { |
| 256 | case XCT_BOTH: |
| 257 | if(ctx->phase) break; |
| 258 | /* Finished decoding of an empty element */ |
| 259 | XER_GOT_EMPTY(); |
| 260 | ADVANCE(ch_size); |
| 261 | ctx->phase = 2; /* Phase out */ |
| 262 | RETURN(RC_OK); |
| 263 | case XCT_OPENING: |
| 264 | if(ctx->phase) break; |
| 265 | ADVANCE(ch_size); |
| 266 | ctx->phase = 1; /* Processing body phase */ |
| 267 | continue; |
| 268 | case XCT_CLOSING: |
| 269 | if(!ctx->phase) break; |
| 270 | ADVANCE(ch_size); |
| 271 | ctx->phase = 2; /* Phase out */ |
| 272 | RETURN(RC_OK); |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 273 | case XCT_UNKNOWN_BO: |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 274 | if(!ctx->phase) break; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 275 | /* |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 276 | * Certain tags in the body may be expected. |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 277 | */ |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 278 | if(opt_unexpected_tag_decoder |
| 279 | && opt_unexpected_tag_decoder(struct_key, |
| 280 | buf_ptr, ch_size) == 0) { |
| 281 | /* Tag's processed fine */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 282 | ADVANCE(ch_size); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 283 | continue; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 284 | } |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 285 | /* Fall through */ |
| 286 | default: |
| 287 | break; /* Unexpected tag */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 288 | } |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 289 | |
| 290 | ASN_DEBUG("Unexpected XML tag"); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 291 | break; /* Dark and mysterious things have just happened */ |
| 292 | } |
| 293 | |
| 294 | RETURN(RC_FAIL); |
| 295 | } |
| 296 | |
vlm | 2a4245f | 2004-10-22 08:17:16 +0000 | [diff] [blame] | 297 | |
| 298 | int |
| 299 | xer_is_whitespace(void *chunk_buf, size_t chunk_size) { |
| 300 | char *p = (char *)chunk_buf; |
| 301 | char *pend = p + chunk_size; |
| 302 | |
| 303 | for(; p < pend; p++) { |
| 304 | switch(*p) { |
| 305 | /* X.693, #8.1.4 |
| 306 | * HORISONTAL TAB (9) |
| 307 | * LINE FEED (10) |
| 308 | * CARRIAGE RETURN (13) |
| 309 | * SPACE (32) |
| 310 | */ |
| 311 | case 0x09: case 0x0a: case 0x0d: case 0x20: |
| 312 | break; |
| 313 | default: |
| 314 | return 0; |
| 315 | } |
| 316 | } |
| 317 | return 1; /* All whitespace */ |
| 318 | } |
| 319 | |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 320 | /* |
| 321 | * This is a vastly simplified, non-validating XML tree skipper. |
| 322 | */ |
| 323 | int |
| 324 | xer_skip_unknown(xer_check_tag_e tcv, ber_tlv_len_t *depth) { |
| 325 | assert(*depth > 0); |
| 326 | switch(tcv) { |
| 327 | case XCT_BOTH: |
| 328 | case XCT_UNKNOWN_BO: |
| 329 | /* These negate each other. */ |
| 330 | return 0; |
| 331 | case XCT_OPENING: |
| 332 | case XCT_UNKNOWN_OP: |
| 333 | ++(*depth); |
| 334 | return 0; |
| 335 | case XCT_CLOSING: |
| 336 | case XCT_UNKNOWN_CL: |
| 337 | if(--(*depth) == 0) |
| 338 | return (tcv == XCT_CLOSING) ? 2 : 1; |
| 339 | return 0; |
| 340 | default: |
| 341 | return -1; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | |