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