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