vlm | fa67ddc | 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 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <constr_SET_OF.h> |
| 7 | #include <asn_SET_OF.h> |
vlm | a329666 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 8 | #include <assert.h> |
vlm | fa67ddc | 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 | */ |
vlm | c519061 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 15 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
vlm | fa67ddc | 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 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 28 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
vlm | fa67ddc | 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 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 34 | #undef ADVANCE |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 35 | #define ADVANCE(num_bytes) do { \ |
| 36 | size_t num = num_bytes; \ |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 37 | ptr = ((char *)ptr) + num; \ |
vlm | fa67ddc | 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 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 47 | #undef NEXT_PHASE |
| 48 | #undef PHASE_OUT |
vlm | fa67ddc | 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 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 58 | #undef RETURN |
vlm | fa67ddc | 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 | */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 68 | asn_dec_rval_t |
vlm | ef6355b | 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, |
vlm | fa67ddc | 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 | */ |
vlm | ef6355b | 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 */ |
vlm | fa67ddc | 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. */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 81 | asn_struct_ctx_t *ctx; /* Decoder context */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | |
| 83 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 84 | asn_dec_rval_t rval; /* Return code from subparsers */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 85 | |
| 86 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
| 87 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 88 | ASN_DEBUG("Decoding %s as SET OF", td->name); |
vlm | fa67ddc | 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 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 103 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
vlm | fa67ddc | 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 | |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 116 | rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size, |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 117 | tag_mode, 1, &ctx->left, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 118 | if(rval.code != RC_OK) { |
| 119 | ASN_DEBUG("%s tagging check failed: %d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 120 | td->name, rval.code); |
vlm | 2ebb08c | 2004-10-05 06:36:44 +0000 | [diff] [blame] | 121 | return rval; |
vlm | fa67ddc | 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) { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 150 | ASN_DEBUG("End of SET OF %s", td->name); |
vlm | fa67ddc | 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 */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 185 | if(element->tag != (ber_tlv_tag_t)-1) { |
vlm | fa67ddc | 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", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 192 | ber_tlv_tag_string(tlv_tag), td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 193 | ASN_DEBUG("%s SET OF has tag %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 194 | td->name, ber_tlv_tag_string(element->tag)); |
vlm | fa67ddc | 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 | */ |
vlm | ef6355b | 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); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | ASN_DEBUG("In %s SET OF %s code %d consumed %d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 211 | td->name, element->type->name, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 212 | rval.code, (int)rval.consumed); |
| 213 | switch(rval.code) { |
| 214 | case RC_OK: |
| 215 | { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 216 | A_SET_OF(void) *list; |
vlm | b2d5829 | 2005-01-27 17:55:42 +0000 | [diff] [blame] | 217 | (void *)list = (void *)st; |
vlm | fa67ddc | 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) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 276 | struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr; |
vlm | fa67ddc | 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) { |
vlm | da67468 | 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; |
vlm | fa67ddc | 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 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 311 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 312 | SET_OF_encode_der(asn_TYPE_descriptor_t *td, void *ptr, |
vlm | fa67ddc | 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) { |
vlm | ef6355b | 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; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 317 | der_type_encoder_f *der_encoder = elm_type->der_encoder; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 318 | A_SET_OF(void) *list; |
vlm | fa67ddc | 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; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 323 | asn_enc_rval_t erval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 324 | int ret; |
| 325 | int edx; |
| 326 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 327 | ASN_DEBUG("Estimating size for SET OF %s", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 328 | |
| 329 | /* |
| 330 | * Gather the length of the underlying members sequence. |
| 331 | */ |
vlm | b2d5829 | 2005-01-27 17:55:42 +0000 | [diff] [blame] | 332 | (void *)list = ptr; |
vlm | fa67ddc | 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 */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 341 | if(max_encoded_len < (size_t)erval.encoded) |
vlm | fa67ddc | 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 | */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 348 | encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 349 | cb, app_key); |
| 350 | if(encoding_size == -1) { |
| 351 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 352 | erval.failed_type = td; |
vlm | fa67ddc | 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 | */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 368 | encoded_els = (struct _el_buffer *)MALLOC( |
| 369 | list->count * sizeof(encoded_els[0])); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 370 | if(encoded_els == NULL) { |
| 371 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 372 | erval.failed_type = td; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 373 | erval.structure_ptr = ptr; |
| 374 | return erval; |
| 375 | } |
| 376 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 377 | ASN_DEBUG("Encoding members of %s SET OF", td->name); |
vlm | fa67ddc | 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 | */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 389 | encoded_el->buf = (uint8_t *)MALLOC(max_encoded_len); |
vlm | fa67ddc | 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; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 398 | erval.failed_type = td; |
vlm | fa67ddc | 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 |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 431 | && cb(encoded_el->buf, encoded_el->length, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 432 | ret = -1; |
| 433 | FREEMEM(encoded_el->buf); |
| 434 | } |
| 435 | FREEMEM(encoded_els); |
| 436 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 437 | if(ret || computed_size != (size_t)encoding_size) { |
vlm | fa67ddc | 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; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 443 | erval.failed_type = td; |
vlm | fa67ddc | 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 | |
vlm | 5f4f4a8 | 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; |
| 472 | const char *elm_tag = specs->as_XMLValueList |
| 473 | ? 0 : ((*element->name) |
| 474 | ? element->name : element->type->xml_tag); |
| 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 */ |
vlm | 5f4f4a8 | 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 | |
| 494 | /* |
| 495 | * Restore parsing context. |
| 496 | */ |
| 497 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
| 498 | |
| 499 | /* |
| 500 | * Phases of XER/XML processing: |
| 501 | * Phase 0: Check that the opening tag matches our expectations. |
| 502 | * Phase 1: Processing body and reacting on closing tag. |
| 503 | * Phase 2: Processing inner type. |
| 504 | */ |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 505 | for(; ctx->phase <= 2;) { |
vlm | 5f4f4a8 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 506 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 507 | ssize_t ch_size; /* Chunk size */ |
| 508 | xer_check_tag_e tcv; /* Tag check value */ |
| 509 | |
| 510 | /* |
| 511 | * Go inside the inner member of a set. |
| 512 | */ |
| 513 | if(ctx->phase == 2) { |
| 514 | asn_dec_rval_t tmprval; |
| 515 | |
| 516 | /* Invoke the inner type decoder, m.b. multiple times */ |
| 517 | tmprval = element->type->xer_decoder(opt_codec_ctx, |
| 518 | element->type, &ctx->ptr, elm_tag, |
| 519 | buf_ptr, size); |
| 520 | if(tmprval.code == RC_OK) { |
| 521 | A_SET_OF(void) *list; |
| 522 | (void *)list = (void *)st; |
| 523 | if(ASN_SET_ADD(list, ctx->ptr) != 0) |
| 524 | RETURN(RC_FAIL); |
| 525 | ctx->ptr = 0; |
| 526 | XER_ADVANCE(tmprval.consumed); |
| 527 | } else { |
| 528 | XER_ADVANCE(tmprval.consumed); |
| 529 | RETURN(tmprval.code); |
| 530 | } |
| 531 | ctx->phase = 1; /* Back to body processing */ |
vlm | 5f4f4a8 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 532 | ASN_DEBUG("XER/SET OF phase => %d", ctx->phase); |
| 533 | /* Fall through */ |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * Get the next part of the XML stream. |
| 538 | */ |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 539 | ch_size = xer_next_token(&ctx->context, |
| 540 | buf_ptr, size, &ch_type); |
vlm | 5f4f4a8 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 541 | switch(ch_size) { |
| 542 | case -1: RETURN(RC_FAIL); |
vlm | cb7222f | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 543 | case 0: RETURN(RC_WMORE); |
vlm | 5f4f4a8 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 544 | default: |
| 545 | switch(ch_type) { |
| 546 | case PXER_COMMENT: /* Got XML comment */ |
| 547 | case PXER_TEXT: /* Ignore free-standing text */ |
| 548 | XER_ADVANCE(ch_size); /* Skip silently */ |
| 549 | continue; |
| 550 | case PXER_TAG: |
| 551 | break; /* Check the rest down there */ |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | tcv = xer_check_tag(buf_ptr, ch_size, xml_tag); |
| 556 | ASN_DEBUG("XER/SET OF: tcv = %d, ph=%d", tcv, ctx->phase); |
| 557 | switch(tcv) { |
| 558 | case XCT_CLOSING: |
| 559 | if(ctx->phase == 0) break; |
| 560 | ctx->phase = 0; |
| 561 | /* Fall through */ |
| 562 | case XCT_BOTH: |
| 563 | if(ctx->phase == 0) { |
| 564 | /* No more things to decode */ |
| 565 | XER_ADVANCE(ch_size); |
| 566 | ctx->phase = 3; /* Phase out */ |
| 567 | RETURN(RC_OK); |
| 568 | } |
| 569 | /* Fall through */ |
| 570 | case XCT_OPENING: |
| 571 | if(ctx->phase == 0) { |
| 572 | XER_ADVANCE(ch_size); |
| 573 | ctx->phase = 1; /* Processing body phase */ |
| 574 | continue; |
| 575 | } |
| 576 | /* Fall through */ |
vlm | 957da5b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 577 | case XCT_UNKNOWN_OP: |
| 578 | case XCT_UNKNOWN_BO: |
vlm | 5f4f4a8 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 579 | |
| 580 | ASN_DEBUG("XER/SET OF: tcv=%d, ph=%d", tcv, ctx->phase); |
| 581 | if(ctx->phase != 1) |
| 582 | break; /* Really unexpected */ |
| 583 | |
| 584 | /* |
| 585 | * Search which member corresponds to this tag. |
| 586 | */ |
| 587 | tcv = xer_check_tag(buf_ptr, ch_size, elm_tag); |
| 588 | switch(tcv) { |
| 589 | case XCT_BOTH: |
| 590 | case XCT_OPENING: |
| 591 | /* |
| 592 | * Process this member. |
| 593 | */ |
| 594 | ctx->phase = 2; |
| 595 | continue; |
vlm | 5f4f4a8 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 596 | default: |
| 597 | break; /* Phase out */ |
| 598 | } |
| 599 | /* Fall through */ |
| 600 | default: |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | ASN_DEBUG("Unexpected XML tag in SET OF"); |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | ctx->phase = 3; /* "Phase out" on hard failure */ |
| 609 | RETURN(RC_FAIL); |
| 610 | } |
| 611 | |
| 612 | |
| 613 | |
vlm | a329666 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 614 | typedef struct xer_tmp_enc_s { |
| 615 | void *buffer; |
| 616 | size_t offset; |
| 617 | size_t size; |
| 618 | } xer_tmp_enc_t; |
| 619 | static int |
| 620 | SET_OF_encode_xer_callback(const void *buffer, size_t size, void *key) { |
| 621 | xer_tmp_enc_t *t = (xer_tmp_enc_t *)key; |
| 622 | if(t->offset + size >= t->size) { |
| 623 | size_t newsize = (t->size << 2) + size; |
| 624 | void *p = REALLOC(t->buffer, newsize); |
| 625 | if(!p) return -1; |
| 626 | t->buffer = p; |
| 627 | t->size = newsize; |
| 628 | } |
| 629 | memcpy((char *)t->buffer + t->offset, buffer, size); |
| 630 | t->offset += size; |
| 631 | return 0; |
| 632 | } |
| 633 | static int |
| 634 | SET_OF_xer_order(const void *aptr, const void *bptr) { |
| 635 | const xer_tmp_enc_t *a = (const xer_tmp_enc_t *)aptr; |
| 636 | const xer_tmp_enc_t *b = (const xer_tmp_enc_t *)bptr; |
| 637 | size_t minlen = a->offset; |
| 638 | int ret; |
| 639 | if(b->offset < minlen) minlen = b->offset; |
| 640 | /* Well-formed UTF-8 has this nice lexicographical property... */ |
| 641 | ret = memcmp(a->buffer, b->buffer, minlen); |
| 642 | if(ret != 0) return ret; |
| 643 | if(a->offset == b->offset) |
| 644 | return 0; |
| 645 | if(a->offset == minlen) |
| 646 | return -1; |
| 647 | return 1; |
| 648 | } |
| 649 | |
| 650 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 651 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 652 | SET_OF_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 653 | int ilevel, enum xer_encoder_flags_e flags, |
| 654 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 655 | asn_enc_rval_t er; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 656 | asn_SET_OF_specifics_t *specs=(asn_SET_OF_specifics_t *)td->specifics; |
| 657 | asn_TYPE_member_t *element = td->elements; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 658 | A_SET_OF(void) *list; |
| 659 | const char *mname = specs->as_XMLValueList |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 660 | ? 0 : ((*element->name) |
| 661 | ? element->name : element->type->xml_tag); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 662 | size_t mlen = mname ? strlen(mname) : 0; |
| 663 | int xcan = (flags & XER_F_CANONICAL); |
vlm | a329666 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 664 | xer_tmp_enc_t *encs = 0; |
| 665 | size_t encs_count = 0; |
| 666 | void *original_app_key = app_key; |
| 667 | asn_app_consume_bytes_f *original_cb = cb; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 668 | int i; |
| 669 | |
| 670 | if(!sptr) _ASN_ENCODE_FAILED; |
| 671 | |
vlm | b2d5829 | 2005-01-27 17:55:42 +0000 | [diff] [blame] | 672 | (void *)list = sptr; |
vlm | a329666 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 673 | |
| 674 | if(xcan) { |
| 675 | encs = (xer_tmp_enc_t *)MALLOC(list->count * sizeof(encs[0])); |
| 676 | if(!encs) _ASN_ENCODE_FAILED; |
| 677 | cb = SET_OF_encode_xer_callback; |
| 678 | } |
| 679 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 680 | er.encoded = 0; |
| 681 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 682 | for(i = 0; i < list->count; i++) { |
| 683 | asn_enc_rval_t tmper; |
| 684 | |
| 685 | void *memb_ptr = list->array[i]; |
| 686 | if(!memb_ptr) continue; |
| 687 | |
vlm | a329666 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 688 | if(encs) { |
| 689 | memset(&encs[encs_count], 0, sizeof(encs[0])); |
| 690 | app_key = &encs[encs_count]; |
| 691 | encs_count++; |
| 692 | } |
| 693 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 694 | if(mname) { |
| 695 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel); |
| 696 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 697 | } |
| 698 | |
| 699 | tmper = element->type->xer_encoder(element->type, memb_ptr, |
| 700 | ilevel + 1, flags, cb, app_key); |
vlm | a329666 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 701 | if(tmper.encoded == -1) { |
| 702 | td = tmper.failed_type; |
| 703 | sptr = tmper.structure_ptr; |
| 704 | goto cb_failed; |
| 705 | } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 706 | |
| 707 | if(mname) { |
| 708 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 709 | er.encoded += 5; |
| 710 | } |
| 711 | |
| 712 | er.encoded += (2 * mlen) + tmper.encoded; |
| 713 | } |
| 714 | |
| 715 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 716 | |
vlm | a329666 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 717 | if(encs) { |
| 718 | xer_tmp_enc_t *enc = encs; |
| 719 | xer_tmp_enc_t *end = encs + encs_count; |
| 720 | ssize_t control_size = 0; |
| 721 | |
| 722 | cb = original_cb; |
| 723 | app_key = original_app_key; |
| 724 | qsort(encs, encs_count, sizeof(encs[0]), SET_OF_xer_order); |
| 725 | |
| 726 | for(; enc < end; enc++) { |
| 727 | _ASN_CALLBACK(enc->buffer, enc->offset); |
| 728 | FREEMEM(enc->buffer); |
| 729 | enc->buffer = 0; |
| 730 | control_size += enc->offset; |
| 731 | } |
| 732 | assert(control_size == er.encoded); |
| 733 | } |
| 734 | |
| 735 | goto cleanup; |
| 736 | cb_failed: |
| 737 | er.encoded = -1; |
| 738 | er.failed_type = td; |
| 739 | er.structure_ptr = sptr; |
| 740 | cleanup: |
| 741 | if(encs) { |
| 742 | while(encs_count-- > 0) { |
| 743 | if(encs[encs_count].buffer) |
| 744 | FREEMEM(encs[encs_count].buffer); |
| 745 | } |
| 746 | free(encs); |
| 747 | } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 748 | return er; |
| 749 | } |
| 750 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 751 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 752 | SET_OF_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 753 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 754 | asn_TYPE_member_t *element = td->elements; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 755 | const A_SET_OF(void) *list; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 756 | int ret; |
| 757 | int i; |
| 758 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 759 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 760 | |
| 761 | /* Dump preamble */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 762 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 763 | || cb(" ::= {", 6, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 764 | return -1; |
| 765 | |
vlm | b2d5829 | 2005-01-27 17:55:42 +0000 | [diff] [blame] | 766 | (const void *)list = sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 767 | for(i = 0; i < list->count; i++) { |
| 768 | const void *memb_ptr = list->array[i]; |
| 769 | if(!memb_ptr) continue; |
| 770 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 771 | _i_INDENT(1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 772 | |
| 773 | ret = element->type->print_struct(element->type, memb_ptr, |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 774 | ilevel + 1, cb, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 775 | if(ret) return ret; |
| 776 | } |
| 777 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 778 | ilevel--; |
| 779 | _i_INDENT(1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 780 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 781 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | void |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 785 | SET_OF_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 786 | if(td && ptr) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 787 | asn_TYPE_member_t *element = td->elements; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 788 | A_SET_OF(void) *list; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 789 | int i; |
| 790 | |
| 791 | /* |
| 792 | * Could not use set_of_empty() because of (*free) |
| 793 | * incompatibility. |
| 794 | */ |
vlm | b2d5829 | 2005-01-27 17:55:42 +0000 | [diff] [blame] | 795 | (void *)list = ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 796 | for(i = 0; i < list->count; i++) { |
| 797 | void *memb_ptr = list->array[i]; |
| 798 | if(memb_ptr) |
| 799 | element->type->free_struct(element->type, memb_ptr, 0); |
| 800 | } |
vlm | 7ebd67c | 2004-07-15 10:51:26 +0000 | [diff] [blame] | 801 | list->count = 0; /* No meaningful elements left */ |
| 802 | |
| 803 | asn_set_empty(list); /* Remove (list->array) */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 804 | |
| 805 | if(!contents_only) { |
| 806 | FREEMEM(ptr); |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 812 | SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 813 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 814 | asn_TYPE_member_t *element = td->elements; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 815 | asn_constr_check_f *constr; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 816 | const A_SET_OF(void) *list; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 817 | int i; |
| 818 | |
| 819 | if(!sptr) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 820 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 821 | "%s: value not given (%s:%d)", |
| 822 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 823 | return -1; |
| 824 | } |
| 825 | |
vlm | b2d5829 | 2005-01-27 17:55:42 +0000 | [diff] [blame] | 826 | (const void *)list = sptr; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 827 | |
| 828 | constr = element->memb_constraints; |
| 829 | if(!constr) constr = element->type->check_constraints; |
| 830 | |
| 831 | /* |
| 832 | * Iterate over the members of an array. |
| 833 | * Validate each in turn, until one fails. |
| 834 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 835 | for(i = 0; i < list->count; i++) { |
| 836 | const void *memb_ptr = list->array[i]; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 837 | int ret; |
| 838 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 839 | if(!memb_ptr) continue; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 840 | |
| 841 | ret = constr(element->type, memb_ptr, app_errlog, app_key); |
| 842 | if(ret) return ret; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 843 | } |
| 844 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 845 | /* |
| 846 | * Cannot inherit it eralier: |
| 847 | * need to make sure we get the updated version. |
| 848 | */ |
| 849 | if(!element->memb_constraints) |
| 850 | element->memb_constraints = element->type->check_constraints; |
| 851 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 852 | return 0; |
| 853 | } |