vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #include <asn_internal.h> |
| 6 | #include <asn_codecs_prim.h> |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | |
| 9 | /* |
| 10 | * Decode an always-primitive type. |
| 11 | */ |
| 12 | asn_dec_rval_t |
| 13 | ber_decode_primitive(asn_codec_ctx_t *opt_codec_ctx, |
| 14 | asn_TYPE_descriptor_t *td, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 15 | void **sptr, const void *buf_ptr, size_t size, int tag_mode) { |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 16 | ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr; |
| 17 | asn_dec_rval_t rval; |
| 18 | ber_tlv_len_t length; |
| 19 | |
| 20 | /* |
| 21 | * If the structure is not there, allocate it. |
| 22 | */ |
| 23 | if(st == NULL) { |
vlm | 82e317b | 2004-12-14 13:31:01 +0000 | [diff] [blame] | 24 | st = (ASN__PRIMITIVE_TYPE_t *)CALLOC(1, sizeof(*st)); |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 25 | if(st == NULL) { |
| 26 | rval.code = RC_FAIL; |
| 27 | rval.consumed = 0; |
| 28 | return rval; |
| 29 | } |
vlm | 82e317b | 2004-12-14 13:31:01 +0000 | [diff] [blame] | 30 | *sptr = (void *)st; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | ASN_DEBUG("Decoding %s as plain primitive (tm=%d)", |
| 34 | td->name, tag_mode); |
| 35 | |
| 36 | /* |
| 37 | * Check tags and extract value length. |
| 38 | */ |
| 39 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, |
| 40 | tag_mode, 0, &length, 0); |
| 41 | if(rval.code != RC_OK) |
| 42 | return rval; |
| 43 | |
| 44 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 45 | |
| 46 | /* |
| 47 | * Make sure we have this length. |
| 48 | */ |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 49 | buf_ptr = ((const char *)buf_ptr) + rval.consumed; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 50 | size -= rval.consumed; |
| 51 | if(length > (ber_tlv_len_t)size) { |
| 52 | rval.code = RC_WMORE; |
| 53 | rval.consumed = 0; |
| 54 | return rval; |
| 55 | } |
| 56 | |
vlm | 8a09e0f | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 57 | st->size = (int)length; |
| 58 | /* The following better be optimized away. */ |
| 59 | if(sizeof(st->size) != sizeof(length) |
| 60 | && (ber_tlv_len_t)st->size != length) { |
| 61 | st->size = 0; |
| 62 | rval.code = RC_FAIL; |
| 63 | rval.consumed = 0; |
| 64 | return rval; |
| 65 | } |
| 66 | |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 67 | st->buf = (uint8_t *)MALLOC(length + 1); |
vlm | 8a09e0f | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 68 | if(!st->buf) { |
| 69 | st->size = 0; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 70 | rval.code = RC_FAIL; |
| 71 | rval.consumed = 0; |
| 72 | return rval; |
| 73 | } |
| 74 | |
| 75 | memcpy(st->buf, buf_ptr, length); |
| 76 | st->buf[length] = '\0'; /* Just in case */ |
| 77 | |
| 78 | rval.code = RC_OK; |
| 79 | rval.consumed += length; |
| 80 | |
| 81 | ASN_DEBUG("Took %ld/%ld bytes to encode %s", |
| 82 | (long)rval.consumed, |
| 83 | (long)length, td->name); |
| 84 | |
| 85 | return rval; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Encode an always-primitive type using DER. |
| 90 | */ |
| 91 | asn_enc_rval_t |
| 92 | der_encode_primitive(asn_TYPE_descriptor_t *td, void *sptr, |
| 93 | int tag_mode, ber_tlv_tag_t tag, |
| 94 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 95 | asn_enc_rval_t erval; |
| 96 | ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr; |
| 97 | |
| 98 | ASN_DEBUG("%s %s as a primitive type (tm=%d)", |
| 99 | cb?"Encoding":"Estimating", td->name, tag_mode); |
| 100 | |
| 101 | erval.encoded = der_write_tags(td, st->size, tag_mode, 0, tag, |
| 102 | cb, app_key); |
| 103 | ASN_DEBUG("%s wrote tags %d", td->name, (int)erval.encoded); |
| 104 | if(erval.encoded == -1) { |
| 105 | erval.failed_type = td; |
| 106 | erval.structure_ptr = sptr; |
| 107 | return erval; |
| 108 | } |
| 109 | |
| 110 | if(cb && st->buf) { |
| 111 | if(cb(st->buf, st->size, app_key) < 0) { |
| 112 | erval.encoded = -1; |
| 113 | erval.failed_type = td; |
| 114 | erval.structure_ptr = sptr; |
| 115 | return erval; |
| 116 | } |
| 117 | } else { |
| 118 | assert(st->buf || st->size == 0); |
| 119 | } |
| 120 | |
| 121 | erval.encoded += st->size; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 122 | _ASN_ENCODED_OK(erval); |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
| 126 | ASN__PRIMITIVE_TYPE_free(asn_TYPE_descriptor_t *td, void *sptr, |
| 127 | int contents_only) { |
| 128 | ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr; |
| 129 | |
| 130 | if(!td || !sptr) |
| 131 | return; |
| 132 | |
| 133 | ASN_DEBUG("Freeing %s as a primitive type", td->name); |
| 134 | |
| 135 | if(st->buf) |
| 136 | FREEMEM(st->buf); |
| 137 | |
| 138 | if(!contents_only) |
| 139 | FREEMEM(st); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /* |
| 144 | * Local internal type passed around as an argument. |
| 145 | */ |
| 146 | struct xdp_arg_s { |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 147 | asn_TYPE_descriptor_t *type_descriptor; |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 148 | void *struct_key; |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 149 | xer_primitive_body_decoder_f *prim_body_decoder; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 150 | int decoded_something; |
| 151 | int want_more; |
| 152 | }; |
| 153 | |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 154 | |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 155 | static int |
| 156 | xer_decode__unexpected_tag(void *key, const void *chunk_buf, size_t chunk_size) { |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 157 | struct xdp_arg_s *arg = (struct xdp_arg_s *)key; |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 158 | enum xer_pbd_rval bret; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 159 | |
| 160 | if(arg->decoded_something) { |
vlm | 2a4245f | 2004-10-22 08:17:16 +0000 | [diff] [blame] | 161 | if(xer_is_whitespace(chunk_buf, chunk_size)) |
vlm | 8a09e0f | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 162 | return 0; /* Skip it. */ |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 163 | /* |
| 164 | * Decoding was done once already. Prohibit doing it again. |
| 165 | */ |
| 166 | return -1; |
| 167 | } |
| 168 | |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 169 | bret = arg->prim_body_decoder(arg->type_descriptor, |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 170 | arg->struct_key, chunk_buf, chunk_size); |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 171 | switch(bret) { |
| 172 | case XPBD_SYSTEM_FAILURE: |
| 173 | case XPBD_DECODER_LIMIT: |
| 174 | case XPBD_BROKEN_ENCODING: |
| 175 | break; |
| 176 | case XPBD_BODY_CONSUMED: |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 177 | /* Tag decoded successfully */ |
| 178 | arg->decoded_something = 1; |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 179 | /* Fall through */ |
| 180 | case XPBD_NOT_BODY_IGNORE: /* Safe to proceed further */ |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 181 | return 0; |
| 182 | } |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 183 | |
| 184 | return -1; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static ssize_t |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 188 | xer_decode__body(void *key, const void *chunk_buf, size_t chunk_size, int have_more) { |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 189 | struct xdp_arg_s *arg = (struct xdp_arg_s *)key; |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 190 | enum xer_pbd_rval bret; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 191 | |
| 192 | if(arg->decoded_something) { |
vlm | 2a4245f | 2004-10-22 08:17:16 +0000 | [diff] [blame] | 193 | if(xer_is_whitespace(chunk_buf, chunk_size)) |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 194 | return chunk_size; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 195 | /* |
| 196 | * Decoding was done once already. Prohibit doing it again. |
| 197 | */ |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | if(!have_more) { |
| 202 | /* |
| 203 | * If we've received something like "1", we can't really |
| 204 | * tell whether it is really `1` or `123`, until we know |
| 205 | * that there is no more data coming. |
| 206 | * The have_more argument will be set to 1 once something |
| 207 | * like this is available to the caller of this callback: |
| 208 | * "1<tag_start..." |
| 209 | */ |
| 210 | arg->want_more = 1; |
| 211 | return -1; |
| 212 | } |
| 213 | |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 214 | bret = arg->prim_body_decoder(arg->type_descriptor, |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 215 | arg->struct_key, chunk_buf, chunk_size); |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 216 | switch(bret) { |
| 217 | case XPBD_SYSTEM_FAILURE: |
| 218 | case XPBD_DECODER_LIMIT: |
| 219 | case XPBD_BROKEN_ENCODING: |
| 220 | break; |
| 221 | case XPBD_BODY_CONSUMED: |
| 222 | /* Tag decoded successfully */ |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 223 | arg->decoded_something = 1; |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 224 | /* Fall through */ |
| 225 | case XPBD_NOT_BODY_IGNORE: /* Safe to proceed further */ |
| 226 | return chunk_size; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 227 | } |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 228 | |
| 229 | return -1; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | |
| 233 | asn_dec_rval_t |
| 234 | xer_decode_primitive(asn_codec_ctx_t *opt_codec_ctx, |
| 235 | asn_TYPE_descriptor_t *td, |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 236 | void **sptr, |
| 237 | size_t struct_size, |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 238 | const char *opt_mname, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 239 | const void *buf_ptr, size_t size, |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 240 | xer_primitive_body_decoder_f *prim_body_decoder |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 241 | ) { |
| 242 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 243 | asn_struct_ctx_t s_ctx; |
| 244 | struct xdp_arg_s s_arg; |
| 245 | asn_dec_rval_t rc; |
| 246 | |
| 247 | /* |
| 248 | * Create the structure if does not exist. |
| 249 | */ |
| 250 | if(!*sptr) { |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 251 | *sptr = CALLOC(1, struct_size); |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 252 | if(!*sptr) { |
| 253 | asn_dec_rval_t rval; |
| 254 | rval.code = RC_FAIL; |
| 255 | rval.consumed = 0; |
| 256 | return rval; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | memset(&s_ctx, 0, sizeof(s_ctx)); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 261 | s_arg.type_descriptor = td; |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 262 | s_arg.struct_key = *sptr; |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 263 | s_arg.prim_body_decoder = prim_body_decoder; |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 264 | s_arg.decoded_something = 0; |
| 265 | s_arg.want_more = 0; |
| 266 | |
| 267 | rc = xer_decode_general(opt_codec_ctx, &s_ctx, &s_arg, |
| 268 | xml_tag, buf_ptr, size, |
| 269 | xer_decode__unexpected_tag, xer_decode__body); |
| 270 | switch(rc.code) { |
| 271 | case RC_OK: |
| 272 | if(!s_arg.decoded_something) { |
vlm | 64f1ba2 | 2004-10-23 11:19:43 +0000 | [diff] [blame] | 273 | char ch; |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 274 | ASN_DEBUG("Primitive body is not recognized, " |
| 275 | "supplying empty one"); |
| 276 | /* |
| 277 | * Decoding opportunity has come and gone. |
| 278 | * Where's the result? |
| 279 | * Try to feed with empty body, see if it eats it. |
| 280 | */ |
| 281 | if(prim_body_decoder(s_arg.type_descriptor, |
| 282 | s_arg.struct_key, &ch, 0) |
| 283 | != XPBD_BODY_CONSUMED) { |
vlm | f7de4de | 2004-10-23 10:17:34 +0000 | [diff] [blame] | 284 | /* |
| 285 | * This decoder does not like empty stuff. |
| 286 | */ |
| 287 | rc.code = RC_FAIL; |
| 288 | rc.consumed = 0; |
| 289 | } |
vlm | c4436cf | 2004-10-21 11:20:50 +0000 | [diff] [blame] | 290 | } |
| 291 | break; |
| 292 | case RC_WMORE: |
| 293 | /* |
| 294 | * Redo the whole thing later. |
| 295 | * We don't have a context to save intermediate parsing state. |
| 296 | */ |
| 297 | rc.consumed = 0; |
| 298 | break; |
| 299 | case RC_FAIL: |
| 300 | rc.consumed = 0; |
| 301 | if(s_arg.want_more) |
| 302 | rc.code = RC_WMORE; |
| 303 | break; |
| 304 | } |
| 305 | return rc; |
| 306 | } |
| 307 | |