Lev Walkin | f15320b | 2004-06-03 03:38:44 +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 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <constr_SET_OF.h> |
| 7 | #include <asn_SET_OF.h> |
| 8 | |
| 9 | /* |
| 10 | * Number of bytes left for this structure. |
| 11 | * (ctx->left) indicates the number of bytes _transferred_ for the structure. |
| 12 | * (size) contains the number of bytes in the buffer passed. |
| 13 | */ |
Lev Walkin | ec1ffd4 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 14 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * If the subprocessor function returns with an indication that it wants |
| 18 | * more data, it may well be a fatal decoding problem, because the |
| 19 | * size is constrained by the <TLV>'s L, even if the buffer size allows |
| 20 | * reading more data. |
| 21 | * For example, consider the buffer containing the following TLVs: |
| 22 | * <T:5><L:1><V> <T:6>... |
| 23 | * The TLV length clearly indicates that one byte is expected in V, but |
| 24 | * if the V processor returns with "want more data" even if the buffer |
| 25 | * contains way more data than the V processor have seen. |
| 26 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 27 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * This macro "eats" the part of the buffer which is definitely "consumed", |
| 31 | * i.e. was correctly converted into local representation or rightfully skipped. |
| 32 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 33 | #undef ADVANCE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | #define ADVANCE(num_bytes) do { \ |
| 35 | size_t num = num_bytes; \ |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 36 | ptr = ((const char *)ptr) + num;\ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | size -= num; \ |
| 38 | if(ctx->left >= 0) \ |
| 39 | ctx->left -= num; \ |
| 40 | consumed_myself += num; \ |
| 41 | } while(0) |
| 42 | |
| 43 | /* |
| 44 | * Switch to the next phase of parsing. |
| 45 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 46 | #undef NEXT_PHASE |
| 47 | #undef PHASE_OUT |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | #define NEXT_PHASE(ctx) do { \ |
| 49 | ctx->phase++; \ |
| 50 | ctx->step = 0; \ |
| 51 | } while(0) |
| 52 | #define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0) |
| 53 | |
| 54 | /* |
| 55 | * Return a standardized complex structure. |
| 56 | */ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 57 | #undef RETURN |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 58 | #define RETURN(_code) do { \ |
| 59 | rval.code = _code; \ |
| 60 | rval.consumed = consumed_myself;\ |
| 61 | return rval; \ |
| 62 | } while(0) |
| 63 | |
| 64 | /* |
| 65 | * The decoder of the SET OF type. |
| 66 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 67 | asn_dec_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 68 | SET_OF_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 69 | void **struct_ptr, const void *ptr, size_t size, int tag_mode) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | /* |
| 71 | * Bring closer parts of structure description. |
| 72 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 73 | asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 74 | asn_TYPE_member_t *elm = td->elements; /* Single one */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * Parts of the structure being constructed. |
| 78 | */ |
| 79 | void *st = *struct_ptr; /* Target structure. */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 80 | asn_struct_ctx_t *ctx; /* Decoder context */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | |
| 82 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 83 | asn_dec_rval_t rval; /* Return code from subparsers */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | |
| 85 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
| 86 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 87 | ASN_DEBUG("Decoding %s as SET OF", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * Create the target structure if it is not present already. |
| 91 | */ |
| 92 | if(st == 0) { |
| 93 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 94 | if(st == 0) { |
| 95 | RETURN(RC_FAIL); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Restore parsing context. |
| 101 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 102 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Start to parse where left previously |
| 106 | */ |
| 107 | switch(ctx->phase) { |
| 108 | case 0: |
| 109 | /* |
| 110 | * PHASE 0. |
| 111 | * Check that the set of tags associated with given structure |
| 112 | * perfectly fits our expectations. |
| 113 | */ |
| 114 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 115 | rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 116 | tag_mode, 1, &ctx->left, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | if(rval.code != RC_OK) { |
| 118 | ASN_DEBUG("%s tagging check failed: %d", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 119 | td->name, rval.code); |
Lev Walkin | 4ab42cd | 2004-10-05 06:36:44 +0000 | [diff] [blame] | 120 | return rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if(ctx->left >= 0) |
| 124 | ctx->left += rval.consumed; /* ?Substracted below! */ |
| 125 | ADVANCE(rval.consumed); |
| 126 | |
| 127 | ASN_DEBUG("Structure consumes %ld bytes, " |
| 128 | "buffer %ld", (long)ctx->left, (long)size); |
| 129 | |
| 130 | NEXT_PHASE(ctx); |
| 131 | /* Fall through */ |
| 132 | case 1: |
| 133 | /* |
| 134 | * PHASE 1. |
| 135 | * From the place where we've left it previously, |
| 136 | * try to decode the next item. |
| 137 | */ |
| 138 | for(;; ctx->step = 0) { |
| 139 | ssize_t tag_len; /* Length of TLV's T */ |
| 140 | |
| 141 | if(ctx->step & 1) |
| 142 | goto microphase2; |
| 143 | |
| 144 | /* |
| 145 | * MICROPHASE 1: Synchronize decoding. |
| 146 | */ |
| 147 | |
| 148 | if(ctx->left == 0) { |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 149 | ASN_DEBUG("End of SET OF %s", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 150 | /* |
| 151 | * No more things to decode. |
| 152 | * Exit out of here. |
| 153 | */ |
| 154 | PHASE_OUT(ctx); |
| 155 | RETURN(RC_OK); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Fetch the T from TLV. |
| 160 | */ |
| 161 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 162 | switch(tag_len) { |
| 163 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 164 | /* Fall through */ |
| 165 | case -1: RETURN(RC_FAIL); |
| 166 | } |
| 167 | |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 168 | if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | if(LEFT < 2) { |
| 170 | if(SIZE_VIOLATION) |
| 171 | RETURN(RC_FAIL); |
| 172 | else |
| 173 | RETURN(RC_WMORE); |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 174 | } else if(((const uint8_t *)ptr)[1] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 175 | /* |
| 176 | * Found the terminator of the |
| 177 | * indefinite length structure. |
| 178 | */ |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* Outmost tag may be unknown and cannot be fetched/compared */ |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 184 | if(elm->tag != (ber_tlv_tag_t)-1) { |
| 185 | if(BER_TAGS_EQUAL(tlv_tag, elm->tag)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 186 | /* |
| 187 | * The new list member of expected type has arrived. |
| 188 | */ |
| 189 | } else { |
| 190 | ASN_DEBUG("Unexpected tag %s fixed SET OF %s", |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 191 | ber_tlv_tag_string(tlv_tag), td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | ASN_DEBUG("%s SET OF has tag %s", |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 193 | td->name, ber_tlv_tag_string(elm->tag)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 194 | RETURN(RC_FAIL); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * MICROPHASE 2: Invoke the member-specific decoder. |
| 200 | */ |
| 201 | ctx->step |= 1; /* Confirm entering next microphase */ |
| 202 | microphase2: |
| 203 | |
| 204 | /* |
| 205 | * Invoke the member fetch routine according to member's type |
| 206 | */ |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 207 | rval = elm->type->ber_decoder(opt_codec_ctx, |
| 208 | elm->type, &ctx->ptr, ptr, LEFT, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 209 | ASN_DEBUG("In %s SET OF %s code %d consumed %d", |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 210 | td->name, elm->type->name, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 211 | rval.code, (int)rval.consumed); |
| 212 | switch(rval.code) { |
| 213 | case RC_OK: |
| 214 | { |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 215 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(st); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 216 | if(ASN_SET_ADD(list, ctx->ptr) != 0) |
| 217 | RETURN(RC_FAIL); |
| 218 | else |
| 219 | ctx->ptr = 0; |
| 220 | } |
| 221 | break; |
| 222 | case RC_WMORE: /* More data expected */ |
| 223 | if(!SIZE_VIOLATION) { |
| 224 | ADVANCE(rval.consumed); |
| 225 | RETURN(RC_WMORE); |
| 226 | } |
| 227 | /* Fall through */ |
| 228 | case RC_FAIL: /* Fatal error */ |
| 229 | RETURN(RC_FAIL); |
| 230 | } /* switch(rval) */ |
| 231 | |
| 232 | ADVANCE(rval.consumed); |
| 233 | } /* for(all list members) */ |
| 234 | |
| 235 | NEXT_PHASE(ctx); |
| 236 | case 2: |
| 237 | /* |
| 238 | * Read in all "end of content" TLVs. |
| 239 | */ |
| 240 | while(ctx->left < 0) { |
| 241 | if(LEFT < 2) { |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 242 | if(LEFT > 0 && ((const char *)ptr)[0] != 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 243 | /* Unexpected tag */ |
| 244 | RETURN(RC_FAIL); |
| 245 | } else { |
| 246 | RETURN(RC_WMORE); |
| 247 | } |
| 248 | } |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 249 | if(((const char *)ptr)[0] == 0 |
| 250 | && ((const char *)ptr)[1] == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 251 | ADVANCE(2); |
| 252 | ctx->left++; |
| 253 | } else { |
| 254 | RETURN(RC_FAIL); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | PHASE_OUT(ctx); |
| 259 | } |
| 260 | |
| 261 | RETURN(RC_OK); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Internally visible buffer holding a single encoded element. |
| 266 | */ |
| 267 | struct _el_buffer { |
| 268 | uint8_t *buf; |
| 269 | size_t length; |
| 270 | size_t size; |
| 271 | }; |
| 272 | /* Append bytes to the above structure */ |
| 273 | static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 274 | struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 275 | |
| 276 | if(el_buf->length + size > el_buf->size) |
| 277 | return -1; |
| 278 | |
| 279 | memcpy(el_buf->buf + el_buf->length, buffer, size); |
| 280 | |
| 281 | el_buf->length += size; |
| 282 | return 0; |
| 283 | } |
| 284 | static int _el_buf_cmp(const void *ap, const void *bp) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 285 | const struct _el_buffer *a = (const struct _el_buffer *)ap; |
| 286 | const struct _el_buffer *b = (const struct _el_buffer *)bp; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 287 | int ret; |
| 288 | size_t common_len; |
| 289 | |
| 290 | if(a->length < b->length) |
| 291 | common_len = a->length; |
| 292 | else |
| 293 | common_len = b->length; |
| 294 | |
| 295 | ret = memcmp(a->buf, b->buf, common_len); |
| 296 | if(ret == 0) { |
| 297 | if(a->length < b->length) |
| 298 | ret = -1; |
| 299 | else if(a->length > b->length) |
| 300 | ret = 1; |
| 301 | } |
| 302 | |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * The DER encoder of the SET OF type. |
| 308 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 309 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 310 | SET_OF_encode_der(asn_TYPE_descriptor_t *td, void *ptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 311 | int tag_mode, ber_tlv_tag_t tag, |
| 312 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 313 | asn_TYPE_member_t *elm = td->elements; |
| 314 | asn_TYPE_descriptor_t *elm_type = elm->type; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 315 | der_type_encoder_f *der_encoder = elm_type->der_encoder; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 316 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 317 | size_t computed_size = 0; |
| 318 | ssize_t encoding_size = 0; |
| 319 | struct _el_buffer *encoded_els; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 320 | ssize_t eels_count = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 321 | size_t max_encoded_len = 1; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 322 | asn_enc_rval_t erval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 323 | int ret; |
| 324 | int edx; |
| 325 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 326 | ASN_DEBUG("Estimating size for SET OF %s", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * Gather the length of the underlying members sequence. |
| 330 | */ |
| 331 | for(edx = 0; edx < list->count; edx++) { |
| 332 | void *memb_ptr = list->array[edx]; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 333 | if(!memb_ptr) continue; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 334 | erval = der_encoder(elm_type, memb_ptr, 0, elm->tag, 0, 0); |
| 335 | if(erval.encoded == -1) |
| 336 | return erval; |
| 337 | computed_size += erval.encoded; |
| 338 | |
| 339 | /* Compute maximum encoding's size */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 340 | if(max_encoded_len < (size_t)erval.encoded) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 341 | max_encoded_len = erval.encoded; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Encode the TLV for the sequence itself. |
| 346 | */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 347 | encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 348 | cb, app_key); |
| 349 | if(encoding_size == -1) { |
| 350 | erval.encoded = -1; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 351 | erval.failed_type = td; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 352 | erval.structure_ptr = ptr; |
| 353 | return erval; |
| 354 | } |
| 355 | computed_size += encoding_size; |
| 356 | |
| 357 | if(!cb) { |
| 358 | erval.encoded = computed_size; |
| 359 | return erval; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * DER mandates dynamic sorting of the SET OF elements |
| 364 | * according to their encodings. Build an array of the |
| 365 | * encoded elements. |
| 366 | */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 367 | encoded_els = (struct _el_buffer *)MALLOC( |
| 368 | list->count * sizeof(encoded_els[0])); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 369 | if(encoded_els == NULL) { |
| 370 | erval.encoded = -1; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 371 | erval.failed_type = td; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 372 | erval.structure_ptr = ptr; |
| 373 | return erval; |
| 374 | } |
| 375 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 376 | ASN_DEBUG("Encoding members of %s SET OF", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 377 | |
| 378 | /* |
| 379 | * Encode all members. |
| 380 | */ |
| 381 | for(edx = 0; edx < list->count; edx++) { |
| 382 | void *memb_ptr = list->array[edx]; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 383 | struct _el_buffer *encoded_el = &encoded_els[eels_count]; |
| 384 | |
| 385 | if(!memb_ptr) continue; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 386 | |
| 387 | /* |
| 388 | * Prepare space for encoding. |
| 389 | */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 390 | encoded_el->buf = (uint8_t *)MALLOC(max_encoded_len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 391 | if(encoded_el->buf) { |
| 392 | encoded_el->length = 0; |
| 393 | encoded_el->size = max_encoded_len; |
| 394 | } else { |
| 395 | for(edx--; edx >= 0; edx--) |
| 396 | FREEMEM(encoded_els[edx].buf); |
| 397 | FREEMEM(encoded_els); |
| 398 | erval.encoded = -1; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 399 | erval.failed_type = td; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 400 | erval.structure_ptr = ptr; |
| 401 | return erval; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Encode the member into the prepared space. |
| 406 | */ |
| 407 | erval = der_encoder(elm_type, memb_ptr, 0, elm->tag, |
| 408 | _el_addbytes, encoded_el); |
| 409 | if(erval.encoded == -1) { |
| 410 | for(; edx >= 0; edx--) |
| 411 | FREEMEM(encoded_els[edx].buf); |
| 412 | FREEMEM(encoded_els); |
| 413 | return erval; |
| 414 | } |
| 415 | encoding_size += erval.encoded; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 416 | eels_count++; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Sort the encoded elements according to their encoding. |
| 421 | */ |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 422 | qsort(encoded_els, eels_count, sizeof(encoded_els[0]), _el_buf_cmp); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | * Report encoded elements to the application. |
| 426 | * Dispose of temporary sorted members table. |
| 427 | */ |
| 428 | ret = 0; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 429 | for(edx = 0; edx < eels_count; edx++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 430 | struct _el_buffer *encoded_el = &encoded_els[edx]; |
| 431 | /* Report encoded chunks to the application */ |
| 432 | if(ret == 0 |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 433 | && cb(encoded_el->buf, encoded_el->length, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 434 | ret = -1; |
| 435 | FREEMEM(encoded_el->buf); |
| 436 | } |
| 437 | FREEMEM(encoded_els); |
| 438 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 439 | if(ret || computed_size != (size_t)encoding_size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 440 | /* |
| 441 | * Standard callback failed, or |
| 442 | * encoded size is not equal to the computed size. |
| 443 | */ |
| 444 | erval.encoded = -1; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 445 | erval.failed_type = td; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 446 | erval.structure_ptr = ptr; |
| 447 | } else { |
| 448 | erval.encoded = computed_size; |
| 449 | } |
| 450 | |
| 451 | return erval; |
| 452 | } |
| 453 | |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 454 | #undef XER_ADVANCE |
| 455 | #define XER_ADVANCE(num_bytes) do { \ |
| 456 | size_t num = num_bytes; \ |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 457 | buf_ptr = ((const char *)buf_ptr) + num;\ |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 458 | size -= num; \ |
| 459 | consumed_myself += num; \ |
| 460 | } while(0) |
| 461 | |
| 462 | /* |
| 463 | * Decode the XER (XML) data. |
| 464 | */ |
| 465 | asn_dec_rval_t |
| 466 | SET_OF_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 467 | void **struct_ptr, const char *opt_mname, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 468 | const void *buf_ptr, size_t size) { |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 469 | /* |
| 470 | * Bring closer parts of structure description. |
| 471 | */ |
| 472 | asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics; |
| 473 | asn_TYPE_member_t *element = td->elements; |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 474 | const char *elm_tag; |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 475 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 476 | |
| 477 | /* |
| 478 | * ... and parts of the structure being constructed. |
| 479 | */ |
| 480 | void *st = *struct_ptr; /* Target structure. */ |
| 481 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 482 | |
| 483 | asn_dec_rval_t rval; /* Return value from a decoder */ |
| 484 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 485 | |
| 486 | /* |
| 487 | * Create the target structure if it is not present already. |
| 488 | */ |
| 489 | if(st == 0) { |
| 490 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 491 | if(st == 0) RETURN(RC_FAIL); |
| 492 | } |
| 493 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 494 | /* Which tag is expected for the downstream */ |
| 495 | if(specs->as_XMLValueList) { |
| 496 | elm_tag = (specs->as_XMLValueList == 1) ? 0 : ""; |
| 497 | } else { |
| 498 | elm_tag = (*element->name) |
| 499 | ? element->name : element->type->xml_tag; |
| 500 | } |
| 501 | |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 502 | /* |
| 503 | * Restore parsing context. |
| 504 | */ |
| 505 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
| 506 | |
| 507 | /* |
| 508 | * Phases of XER/XML processing: |
| 509 | * Phase 0: Check that the opening tag matches our expectations. |
| 510 | * Phase 1: Processing body and reacting on closing tag. |
| 511 | * Phase 2: Processing inner type. |
| 512 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 513 | for(; ctx->phase <= 2;) { |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 514 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 515 | ssize_t ch_size; /* Chunk size */ |
| 516 | xer_check_tag_e tcv; /* Tag check value */ |
| 517 | |
| 518 | /* |
| 519 | * Go inside the inner member of a set. |
| 520 | */ |
| 521 | if(ctx->phase == 2) { |
| 522 | asn_dec_rval_t tmprval; |
| 523 | |
| 524 | /* Invoke the inner type decoder, m.b. multiple times */ |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 525 | ASN_DEBUG("XER/SET OF element [%s]", elm_tag); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 526 | tmprval = element->type->xer_decoder(opt_codec_ctx, |
| 527 | element->type, &ctx->ptr, elm_tag, |
| 528 | buf_ptr, size); |
| 529 | if(tmprval.code == RC_OK) { |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 530 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(st); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 531 | if(ASN_SET_ADD(list, ctx->ptr) != 0) |
| 532 | RETURN(RC_FAIL); |
| 533 | ctx->ptr = 0; |
| 534 | XER_ADVANCE(tmprval.consumed); |
| 535 | } else { |
| 536 | XER_ADVANCE(tmprval.consumed); |
| 537 | RETURN(tmprval.code); |
| 538 | } |
| 539 | ctx->phase = 1; /* Back to body processing */ |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 540 | ASN_DEBUG("XER/SET OF phase => %d", ctx->phase); |
| 541 | /* Fall through */ |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * Get the next part of the XML stream. |
| 546 | */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 547 | ch_size = xer_next_token(&ctx->context, |
| 548 | buf_ptr, size, &ch_type); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 549 | switch(ch_size) { |
| 550 | case -1: RETURN(RC_FAIL); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 551 | case 0: RETURN(RC_WMORE); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 552 | default: |
| 553 | switch(ch_type) { |
| 554 | case PXER_COMMENT: /* Got XML comment */ |
| 555 | case PXER_TEXT: /* Ignore free-standing text */ |
| 556 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 557 | continue; |
| 558 | case PXER_TAG: |
| 559 | break; /* Check the rest down there */ |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 564 | ASN_DEBUG("XER/SET OF: tcv = %d, ph=%d t=%s", |
| 565 | tcv, ctx->phase, xml_tag); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 566 | switch(tcv) { |
| 567 | case XCT_CLOSING: |
| 568 | if(ctx->phase == 0) break; |
| 569 | ctx->phase = 0; |
| 570 | /* Fall through */ |
| 571 | case XCT_BOTH: |
| 572 | if(ctx->phase == 0) { |
| 573 | /* No more things to decode */ |
| 574 | XER_ADVANCE(ch_size); |
| 575 | ctx->phase = 3; /* Phase out */ |
| 576 | RETURN(RC_OK); |
| 577 | } |
| 578 | /* Fall through */ |
| 579 | case XCT_OPENING: |
| 580 | if(ctx->phase == 0) { |
| 581 | XER_ADVANCE(ch_size); |
| 582 | ctx->phase = 1; /* Processing body phase */ |
| 583 | continue; |
| 584 | } |
| 585 | /* Fall through */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 586 | case XCT_UNKNOWN_OP: |
| 587 | case XCT_UNKNOWN_BO: |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 588 | |
| 589 | ASN_DEBUG("XER/SET OF: tcv=%d, ph=%d", tcv, ctx->phase); |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 590 | if(ctx->phase == 1) { |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 591 | /* |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 592 | * Process a single possible member. |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 593 | */ |
| 594 | ctx->phase = 2; |
| 595 | continue; |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 596 | } |
| 597 | /* Fall through */ |
| 598 | default: |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | ASN_DEBUG("Unexpected XML tag in SET OF"); |
| 603 | break; |
| 604 | } |
| 605 | |
| 606 | ctx->phase = 3; /* "Phase out" on hard failure */ |
| 607 | RETURN(RC_FAIL); |
| 608 | } |
| 609 | |
| 610 | |
| 611 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 612 | typedef struct xer_tmp_enc_s { |
| 613 | void *buffer; |
| 614 | size_t offset; |
| 615 | size_t size; |
| 616 | } xer_tmp_enc_t; |
| 617 | static int |
| 618 | SET_OF_encode_xer_callback(const void *buffer, size_t size, void *key) { |
| 619 | xer_tmp_enc_t *t = (xer_tmp_enc_t *)key; |
| 620 | if(t->offset + size >= t->size) { |
| 621 | size_t newsize = (t->size << 2) + size; |
| 622 | void *p = REALLOC(t->buffer, newsize); |
| 623 | if(!p) return -1; |
| 624 | t->buffer = p; |
| 625 | t->size = newsize; |
| 626 | } |
| 627 | memcpy((char *)t->buffer + t->offset, buffer, size); |
| 628 | t->offset += size; |
| 629 | return 0; |
| 630 | } |
| 631 | static int |
| 632 | SET_OF_xer_order(const void *aptr, const void *bptr) { |
| 633 | const xer_tmp_enc_t *a = (const xer_tmp_enc_t *)aptr; |
| 634 | const xer_tmp_enc_t *b = (const xer_tmp_enc_t *)bptr; |
| 635 | size_t minlen = a->offset; |
| 636 | int ret; |
| 637 | if(b->offset < minlen) minlen = b->offset; |
| 638 | /* Well-formed UTF-8 has this nice lexicographical property... */ |
| 639 | ret = memcmp(a->buffer, b->buffer, minlen); |
| 640 | if(ret != 0) return ret; |
| 641 | if(a->offset == b->offset) |
| 642 | return 0; |
| 643 | if(a->offset == minlen) |
| 644 | return -1; |
| 645 | return 1; |
| 646 | } |
| 647 | |
| 648 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 649 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 650 | SET_OF_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 651 | int ilevel, enum xer_encoder_flags_e flags, |
| 652 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 653 | asn_enc_rval_t er; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 654 | asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 655 | asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 656 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(sptr); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 657 | const char *mname = specs->as_XMLValueList |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 658 | ? 0 : ((*elm->name) ? elm->name : elm->type->xml_tag); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 659 | size_t mlen = mname ? strlen(mname) : 0; |
| 660 | int xcan = (flags & XER_F_CANONICAL); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 661 | xer_tmp_enc_t *encs = 0; |
| 662 | size_t encs_count = 0; |
| 663 | void *original_app_key = app_key; |
| 664 | asn_app_consume_bytes_f *original_cb = cb; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 665 | int i; |
| 666 | |
| 667 | if(!sptr) _ASN_ENCODE_FAILED; |
| 668 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 669 | if(xcan) { |
| 670 | encs = (xer_tmp_enc_t *)MALLOC(list->count * sizeof(encs[0])); |
| 671 | if(!encs) _ASN_ENCODE_FAILED; |
| 672 | cb = SET_OF_encode_xer_callback; |
| 673 | } |
| 674 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 675 | er.encoded = 0; |
| 676 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 677 | for(i = 0; i < list->count; i++) { |
| 678 | asn_enc_rval_t tmper; |
| 679 | |
| 680 | void *memb_ptr = list->array[i]; |
| 681 | if(!memb_ptr) continue; |
| 682 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 683 | if(encs) { |
| 684 | memset(&encs[encs_count], 0, sizeof(encs[0])); |
| 685 | app_key = &encs[encs_count]; |
| 686 | encs_count++; |
| 687 | } |
| 688 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 689 | if(mname) { |
| 690 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel); |
| 691 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 692 | } |
| 693 | |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 694 | if(!xcan && specs->as_XMLValueList == 1) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 695 | _i_ASN_TEXT_INDENT(1, ilevel + 1); |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 696 | tmper = elm->type->xer_encoder(elm->type, memb_ptr, |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 697 | ilevel + (specs->as_XMLValueList != 2), |
| 698 | flags, cb, app_key); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 699 | if(tmper.encoded == -1) { |
| 700 | td = tmper.failed_type; |
| 701 | sptr = tmper.structure_ptr; |
| 702 | goto cb_failed; |
| 703 | } |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 704 | if(tmper.encoded == 0 && specs->as_XMLValueList) { |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 705 | const char *name = elm->type->xml_tag; |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 706 | size_t len = strlen(name); |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 707 | _ASN_CALLBACK3("<", 1, name, len, "/>", 2); |
| 708 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 709 | |
| 710 | if(mname) { |
| 711 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 712 | er.encoded += 5; |
| 713 | } |
| 714 | |
| 715 | er.encoded += (2 * mlen) + tmper.encoded; |
| 716 | } |
| 717 | |
| 718 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 719 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 720 | if(encs) { |
| 721 | xer_tmp_enc_t *enc = encs; |
| 722 | xer_tmp_enc_t *end = encs + encs_count; |
| 723 | ssize_t control_size = 0; |
| 724 | |
| 725 | cb = original_cb; |
| 726 | app_key = original_app_key; |
| 727 | qsort(encs, encs_count, sizeof(encs[0]), SET_OF_xer_order); |
| 728 | |
| 729 | for(; enc < end; enc++) { |
| 730 | _ASN_CALLBACK(enc->buffer, enc->offset); |
| 731 | FREEMEM(enc->buffer); |
| 732 | enc->buffer = 0; |
| 733 | control_size += enc->offset; |
| 734 | } |
| 735 | assert(control_size == er.encoded); |
| 736 | } |
| 737 | |
| 738 | goto cleanup; |
| 739 | cb_failed: |
| 740 | er.encoded = -1; |
| 741 | er.failed_type = td; |
| 742 | er.structure_ptr = sptr; |
| 743 | cleanup: |
| 744 | if(encs) { |
| 745 | while(encs_count-- > 0) { |
| 746 | if(encs[encs_count].buffer) |
| 747 | FREEMEM(encs[encs_count].buffer); |
| 748 | } |
| 749 | free(encs); |
| 750 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 751 | return er; |
| 752 | } |
| 753 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 754 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 755 | SET_OF_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 756 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 757 | asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 758 | const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 759 | int ret; |
| 760 | int i; |
| 761 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 762 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 763 | |
| 764 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 765 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 766 | || cb(" ::= {", 6, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 767 | return -1; |
| 768 | |
| 769 | for(i = 0; i < list->count; i++) { |
| 770 | const void *memb_ptr = list->array[i]; |
| 771 | if(!memb_ptr) continue; |
| 772 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 773 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 774 | |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 775 | ret = elm->type->print_struct(elm->type, memb_ptr, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 776 | ilevel + 1, cb, app_key); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 777 | if(ret) return ret; |
| 778 | } |
| 779 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 780 | ilevel--; |
| 781 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 782 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 783 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | void |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 787 | SET_OF_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 788 | if(td && ptr) { |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 789 | asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 790 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 791 | int i; |
| 792 | |
| 793 | /* |
| 794 | * Could not use set_of_empty() because of (*free) |
| 795 | * incompatibility. |
| 796 | */ |
| 797 | for(i = 0; i < list->count; i++) { |
| 798 | void *memb_ptr = list->array[i]; |
| 799 | if(memb_ptr) |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 800 | elm->type->free_struct(elm->type, memb_ptr, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 801 | } |
Lev Walkin | 246a2af | 2004-07-15 10:51:26 +0000 | [diff] [blame] | 802 | list->count = 0; /* No meaningful elements left */ |
| 803 | |
| 804 | asn_set_empty(list); /* Remove (list->array) */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 805 | |
| 806 | if(!contents_only) { |
| 807 | FREEMEM(ptr); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 813 | SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 814 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 815 | asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 816 | asn_constr_check_f *constr; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 817 | const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 818 | int i; |
| 819 | |
| 820 | if(!sptr) { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 821 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 822 | "%s: value not given (%s:%d)", |
| 823 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 824 | return -1; |
| 825 | } |
| 826 | |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 827 | constr = elm->memb_constraints; |
| 828 | if(!constr) constr = elm->type->check_constraints; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 829 | |
| 830 | /* |
| 831 | * Iterate over the members of an array. |
| 832 | * Validate each in turn, until one fails. |
| 833 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 834 | for(i = 0; i < list->count; i++) { |
| 835 | const void *memb_ptr = list->array[i]; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 836 | int ret; |
| 837 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 838 | if(!memb_ptr) continue; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 839 | |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 840 | ret = constr(elm->type, memb_ptr, app_errlog, app_key); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 841 | if(ret) return ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 844 | /* |
| 845 | * Cannot inherit it eralier: |
| 846 | * need to make sure we get the updated version. |
| 847 | */ |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 848 | if(!elm->memb_constraints) |
| 849 | elm->memb_constraints = elm->type->check_constraints; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 850 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 851 | return 0; |
| 852 | } |