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 |
| 54 | xer_next_token(int *stateContext, void *buffer, size_t size, |
| 55 | pxer_chunk_type_e *ch_type) { |
| 56 | struct xer__cb_arg arg; |
| 57 | ssize_t ret; |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame^] | 58 | int new_stateContext = *stateContext; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 59 | |
| 60 | arg.callback_not_invoked = 1; |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame^] | 61 | ret = pxml_parse(&new_stateContext, buffer, size, xer__token_cb, &arg); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 62 | if(ret < 0) return -1; |
| 63 | if(arg.callback_not_invoked) { |
| 64 | assert(ret == 0); /* No data was consumed */ |
| 65 | return 0; /* Try again with more data */ |
| 66 | } else { |
| 67 | assert(arg.chunk_size); |
| 68 | assert(arg.chunk_buf == buffer); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Translate the XML chunk types into more convenient ones. |
| 73 | */ |
| 74 | switch(arg.chunk_type) { |
| 75 | case PXML_TEXT: |
| 76 | *ch_type = PXER_TEXT; |
| 77 | break; |
| 78 | case PXML_TAG: return 0; /* Want more */ |
| 79 | case PXML_TAG_END: |
| 80 | *ch_type = PXER_TAG; |
| 81 | break; |
| 82 | case PXML_COMMENT: |
| 83 | case PXML_COMMENT_END: |
| 84 | *ch_type = PXER_COMMENT; |
| 85 | break; |
| 86 | } |
| 87 | |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame^] | 88 | *stateContext = new_stateContext; /* Update the context */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 89 | return arg.chunk_size; |
| 90 | } |
| 91 | |
| 92 | #define CSLASH 0x2f /* '/' */ |
| 93 | #define LANGLE 0x3c /* '<' */ |
| 94 | #define RANGLE 0x3e /* '>' */ |
| 95 | |
| 96 | xer_check_tag_e |
| 97 | xer_check_tag(const void *buf_ptr, int size, const char *need_tag) { |
| 98 | const char *buf = (const char *)buf_ptr; |
| 99 | const char *end; |
| 100 | xer_check_tag_e ct = XCT_OPENING; |
| 101 | |
| 102 | if(size < 2 || buf[0] != LANGLE || buf[size-1] != RANGLE) { |
vlm | 390a902 | 2005-02-14 17:21:22 +0000 | [diff] [blame^] | 103 | if(size >= 2) |
| 104 | ASN_DEBUG("Broken XML tag: \"%c...%c\"", buf[0], buf[size - 1]); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 105 | return XCT_BROKEN; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Determine the tag class. |
| 110 | */ |
| 111 | if(buf[1] == CSLASH) { |
| 112 | buf += 2; /* advance past "</" */ |
| 113 | size -= 3; /* strip "</" and ">" */ |
| 114 | ct = XCT_CLOSING; |
| 115 | if(size > 0 && buf[size-1] == CSLASH) |
| 116 | return XCT_BROKEN; /* </abc/> */ |
| 117 | } else { |
| 118 | buf++; /* advance past "<" */ |
| 119 | size -= 2; /* strip "<" and ">" */ |
| 120 | if(size > 0 && buf[size-1] == CSLASH) { |
| 121 | ct = XCT_BOTH; |
| 122 | size--; /* One more, for "/" */ |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Determine the tag name. |
| 128 | */ |
| 129 | for(end = buf + size; buf < end; buf++, need_tag++) { |
| 130 | int b = *buf, n = *need_tag; |
| 131 | if(b != n) { |
| 132 | if(n == 0) { |
| 133 | switch(b) { |
| 134 | case 0x09: case 0x0a: case 0x0c: case 0x0d: |
| 135 | case 0x20: |
| 136 | /* "<abc def/>": whitespace is normal */ |
| 137 | return ct; |
| 138 | } |
| 139 | } |
| 140 | return XCT_UNEXPECTED; |
| 141 | } |
| 142 | if(b == 0) |
| 143 | return XCT_BROKEN; /* Embedded 0 in buf?! */ |
| 144 | } |
| 145 | if(*need_tag) return XCT_UNEXPECTED; |
| 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 | |
| 166 | #define XER_GOT_BODY(chunk_buf, chunk_size) do { \ |
| 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); \ |
| 171 | chunk_size = converted_size; \ |
| 172 | } while(0) |
| 173 | #define XER_GOT_EMPTY() do { \ |
| 174 | ssize_t chunk_size = 0; \ |
| 175 | XER_GOT_BODY(0, chunk_size); \ |
| 176 | } while(0) |
| 177 | |
| 178 | /* |
| 179 | * Generalized function for decoding the primitive values. |
| 180 | */ |
| 181 | asn_dec_rval_t |
| 182 | xer_decode_general(asn_codec_ctx_t *opt_codec_ctx, |
| 183 | asn_struct_ctx_t *ctx, /* Type decoder context */ |
vlm | 3b6e112 | 2004-10-21 11:22:12 +0000 | [diff] [blame] | 184 | void *struct_key, |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 185 | const char *xml_tag, /* Expected XML tag */ |
| 186 | void *buf_ptr, size_t size, |
| 187 | int (*opt_unexpected_tag_decoder) |
vlm | 3b6e112 | 2004-10-21 11:22:12 +0000 | [diff] [blame] | 188 | (void *struct_key, void *chunk_buf, size_t chunk_size), |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 189 | ssize_t (*body_receiver) |
vlm | 3b6e112 | 2004-10-21 11:22:12 +0000 | [diff] [blame] | 190 | (void *struct_key, void *chunk_buf, size_t chunk_size, |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 191 | int have_more) |
| 192 | ) { |
| 193 | |
| 194 | asn_dec_rval_t rval; |
| 195 | ssize_t consumed_myself = 0; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 196 | int xer_state; /* XER low level parsing context */ |
| 197 | |
| 198 | (void)opt_codec_ctx; |
| 199 | |
| 200 | /* |
| 201 | * Phases of XER/XML processing: |
| 202 | * Phase 0: Check that the opening tag matches our expectations. |
| 203 | * Phase 1: Processing body and reacting on closing tag. |
| 204 | */ |
| 205 | if(ctx->phase > 1) RETURN(RC_FAIL); |
| 206 | for(xer_state = ctx->step;;) { |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 207 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 208 | ssize_t ch_size; /* Chunk size */ |
| 209 | xer_check_tag_e tcv; /* Tag check value */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * Get the next part of the XML stream. |
| 213 | */ |
| 214 | ch_size = xer_next_token(&xer_state, buf_ptr, size, &ch_type); |
| 215 | switch(ch_size) { |
| 216 | case -1: RETURN(RC_FAIL); |
| 217 | case 0: |
| 218 | ctx->step = xer_state; |
| 219 | RETURN(RC_WMORE); |
| 220 | default: |
| 221 | switch(ch_type) { |
| 222 | case PXER_COMMENT: /* Got XML comment */ |
| 223 | ADVANCE(ch_size); /* Skip silently */ |
| 224 | continue; |
| 225 | case PXER_TEXT: |
| 226 | if(ctx->phase == 0) { |
vlm | 7958f11 | 2004-10-21 05:44:11 +0000 | [diff] [blame] | 227 | /* |
| 228 | * We have to ignore whitespace here, |
| 229 | * but in order to be forward compatible |
| 230 | * with EXTENDED-XER (EMBED-VALUES, #25) |
| 231 | * any text is just ignored here. |
| 232 | */ |
| 233 | } else { |
| 234 | XER_GOT_BODY(buf_ptr, ch_size); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 235 | } |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 236 | ADVANCE(ch_size); |
| 237 | continue; |
| 238 | case PXER_TAG: |
| 239 | break; /* Check the rest down there */ |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | assert(ch_type == PXER_TAG && size); |
| 244 | |
| 245 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 246 | /* |
| 247 | * Phase 0: |
| 248 | * Expecting the opening tag |
| 249 | * for the type being processed. |
| 250 | * Phase 1: |
| 251 | * Waiting for the closing XML tag. |
| 252 | */ |
| 253 | switch(tcv) { |
| 254 | case XCT_BOTH: |
| 255 | if(ctx->phase) break; |
| 256 | /* Finished decoding of an empty element */ |
| 257 | XER_GOT_EMPTY(); |
| 258 | ADVANCE(ch_size); |
| 259 | ctx->phase = 2; /* Phase out */ |
| 260 | RETURN(RC_OK); |
| 261 | case XCT_OPENING: |
| 262 | if(ctx->phase) break; |
| 263 | ADVANCE(ch_size); |
| 264 | ctx->phase = 1; /* Processing body phase */ |
| 265 | continue; |
| 266 | case XCT_CLOSING: |
| 267 | if(!ctx->phase) break; |
| 268 | ADVANCE(ch_size); |
| 269 | ctx->phase = 2; /* Phase out */ |
| 270 | RETURN(RC_OK); |
| 271 | case XCT_UNEXPECTED: |
| 272 | if(!ctx->phase) break; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 273 | /* |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 274 | * Certain tags in the body may be expected. |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 275 | */ |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 276 | if(opt_unexpected_tag_decoder |
| 277 | && opt_unexpected_tag_decoder(struct_key, |
| 278 | buf_ptr, ch_size) == 0) { |
| 279 | /* Tag's processed fine */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 280 | ADVANCE(ch_size); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 281 | continue; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 282 | } |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 283 | /* Fall through */ |
| 284 | default: |
| 285 | break; /* Unexpected tag */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 286 | } |
vlm | b840485 | 2004-10-23 13:27:03 +0000 | [diff] [blame] | 287 | |
| 288 | ASN_DEBUG("Unexpected XML tag"); |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 289 | break; /* Dark and mysterious things have just happened */ |
| 290 | } |
| 291 | |
| 292 | RETURN(RC_FAIL); |
| 293 | } |
| 294 | |
vlm | 2a4245f | 2004-10-22 08:17:16 +0000 | [diff] [blame] | 295 | |
| 296 | int |
| 297 | xer_is_whitespace(void *chunk_buf, size_t chunk_size) { |
| 298 | char *p = (char *)chunk_buf; |
| 299 | char *pend = p + chunk_size; |
| 300 | |
| 301 | for(; p < pend; p++) { |
| 302 | switch(*p) { |
| 303 | /* X.693, #8.1.4 |
| 304 | * HORISONTAL TAB (9) |
| 305 | * LINE FEED (10) |
| 306 | * CARRIAGE RETURN (13) |
| 307 | * SPACE (32) |
| 308 | */ |
| 309 | case 0x09: case 0x0a: case 0x0d: case 0x20: |
| 310 | break; |
| 311 | default: |
| 312 | return 0; |
| 313 | } |
| 314 | } |
| 315 | return 1; /* All whitespace */ |
| 316 | } |
| 317 | |