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 | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 474 | const char *elm_tag = ((*element->name) |
| 475 | ? element->name : element->type->xml_tag); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 476 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 477 | |
| 478 | /* |
| 479 | * ... and parts of the structure being constructed. |
| 480 | */ |
| 481 | void *st = *struct_ptr; /* Target structure. */ |
| 482 | asn_struct_ctx_t *ctx; /* Decoder context */ |
| 483 | |
| 484 | asn_dec_rval_t rval; /* Return value from a decoder */ |
| 485 | ssize_t consumed_myself = 0; /* Consumed bytes from ptr */ |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 486 | |
| 487 | /* |
| 488 | * Create the target structure if it is not present already. |
| 489 | */ |
| 490 | if(st == 0) { |
| 491 | st = *struct_ptr = CALLOC(1, specs->struct_size); |
| 492 | if(st == 0) RETURN(RC_FAIL); |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Restore parsing context. |
| 497 | */ |
| 498 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
| 499 | |
| 500 | /* |
| 501 | * Phases of XER/XML processing: |
| 502 | * Phase 0: Check that the opening tag matches our expectations. |
| 503 | * Phase 1: Processing body and reacting on closing tag. |
| 504 | * Phase 2: Processing inner type. |
| 505 | */ |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 506 | for(; ctx->phase <= 2;) { |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 507 | pxer_chunk_type_e ch_type; /* XER chunk type */ |
| 508 | ssize_t ch_size; /* Chunk size */ |
| 509 | xer_check_tag_e tcv; /* Tag check value */ |
| 510 | |
| 511 | /* |
| 512 | * Go inside the inner member of a set. |
| 513 | */ |
| 514 | if(ctx->phase == 2) { |
| 515 | asn_dec_rval_t tmprval; |
| 516 | |
| 517 | /* Invoke the inner type decoder, m.b. multiple times */ |
| 518 | tmprval = element->type->xer_decoder(opt_codec_ctx, |
| 519 | element->type, &ctx->ptr, elm_tag, |
| 520 | buf_ptr, size); |
| 521 | if(tmprval.code == RC_OK) { |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 522 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(st); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 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 */ |
Lev Walkin | 8bb4a95 | 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 | */ |
Lev Walkin | 1e44396 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 539 | ch_size = xer_next_token(&ctx->context, |
| 540 | buf_ptr, size, &ch_type); |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 541 | switch(ch_size) { |
| 542 | case -1: RETURN(RC_FAIL); |
Lev Walkin | 2eeeedc | 2005-02-18 16:10:40 +0000 | [diff] [blame] | 543 | case 0: RETURN(RC_WMORE); |
Lev Walkin | 8bb4a95 | 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 */ |
Lev Walkin | 904e65b | 2005-02-18 14:23:48 +0000 | [diff] [blame] | 577 | case XCT_UNKNOWN_OP: |
| 578 | case XCT_UNKNOWN_BO: |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 579 | |
| 580 | ASN_DEBUG("XER/SET OF: tcv=%d, ph=%d", tcv, ctx->phase); |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 581 | if(ctx->phase == 1) { |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 582 | /* |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 583 | * Process a single possible member. |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 584 | */ |
| 585 | ctx->phase = 2; |
| 586 | continue; |
Lev Walkin | 8bb4a95 | 2005-02-14 20:15:40 +0000 | [diff] [blame] | 587 | } |
| 588 | /* Fall through */ |
| 589 | default: |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | ASN_DEBUG("Unexpected XML tag in SET OF"); |
| 594 | break; |
| 595 | } |
| 596 | |
| 597 | ctx->phase = 3; /* "Phase out" on hard failure */ |
| 598 | RETURN(RC_FAIL); |
| 599 | } |
| 600 | |
| 601 | |
| 602 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 603 | typedef struct xer_tmp_enc_s { |
| 604 | void *buffer; |
| 605 | size_t offset; |
| 606 | size_t size; |
| 607 | } xer_tmp_enc_t; |
| 608 | static int |
| 609 | SET_OF_encode_xer_callback(const void *buffer, size_t size, void *key) { |
| 610 | xer_tmp_enc_t *t = (xer_tmp_enc_t *)key; |
| 611 | if(t->offset + size >= t->size) { |
| 612 | size_t newsize = (t->size << 2) + size; |
| 613 | void *p = REALLOC(t->buffer, newsize); |
| 614 | if(!p) return -1; |
| 615 | t->buffer = p; |
| 616 | t->size = newsize; |
| 617 | } |
| 618 | memcpy((char *)t->buffer + t->offset, buffer, size); |
| 619 | t->offset += size; |
| 620 | return 0; |
| 621 | } |
| 622 | static int |
| 623 | SET_OF_xer_order(const void *aptr, const void *bptr) { |
| 624 | const xer_tmp_enc_t *a = (const xer_tmp_enc_t *)aptr; |
| 625 | const xer_tmp_enc_t *b = (const xer_tmp_enc_t *)bptr; |
| 626 | size_t minlen = a->offset; |
| 627 | int ret; |
| 628 | if(b->offset < minlen) minlen = b->offset; |
| 629 | /* Well-formed UTF-8 has this nice lexicographical property... */ |
| 630 | ret = memcmp(a->buffer, b->buffer, minlen); |
| 631 | if(ret != 0) return ret; |
| 632 | if(a->offset == b->offset) |
| 633 | return 0; |
| 634 | if(a->offset == minlen) |
| 635 | return -1; |
| 636 | return 1; |
| 637 | } |
| 638 | |
| 639 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 640 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 641 | SET_OF_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 642 | int ilevel, enum xer_encoder_flags_e flags, |
| 643 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 644 | asn_enc_rval_t er; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 645 | 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] | 646 | asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 647 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(sptr); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 648 | const char *mname = specs->as_XMLValueList |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 649 | ? 0 : ((*elm->name) ? elm->name : elm->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 | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 660 | if(xcan) { |
| 661 | encs = (xer_tmp_enc_t *)MALLOC(list->count * sizeof(encs[0])); |
| 662 | if(!encs) _ASN_ENCODE_FAILED; |
| 663 | cb = SET_OF_encode_xer_callback; |
| 664 | } |
| 665 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 666 | er.encoded = 0; |
| 667 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 668 | for(i = 0; i < list->count; i++) { |
| 669 | asn_enc_rval_t tmper; |
| 670 | |
| 671 | void *memb_ptr = list->array[i]; |
| 672 | if(!memb_ptr) continue; |
| 673 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 674 | if(encs) { |
| 675 | memset(&encs[encs_count], 0, sizeof(encs[0])); |
| 676 | app_key = &encs[encs_count]; |
| 677 | encs_count++; |
| 678 | } |
| 679 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 680 | if(mname) { |
| 681 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel); |
| 682 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 683 | } |
| 684 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 685 | if(!xcan && specs->as_XMLValueList) |
| 686 | _i_ASN_TEXT_INDENT(1, ilevel + 1); |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 687 | tmper = elm->type->xer_encoder(elm->type, memb_ptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 688 | ilevel + 1, flags, cb, app_key); |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 689 | if(tmper.encoded == -1) { |
| 690 | td = tmper.failed_type; |
| 691 | sptr = tmper.structure_ptr; |
| 692 | goto cb_failed; |
| 693 | } |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 694 | if(tmper.encoded == 0 && specs->as_XMLValueList) { |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 695 | const char *name = (*elm->name) |
| 696 | ? elm->name : elm->type->xml_tag; |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 697 | size_t len = strlen(name); |
Lev Walkin | 642962a | 2005-02-24 22:37:07 +0000 | [diff] [blame] | 698 | _ASN_CALLBACK3("<", 1, name, len, "/>", 2); |
| 699 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 700 | |
| 701 | if(mname) { |
| 702 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 703 | er.encoded += 5; |
| 704 | } |
| 705 | |
| 706 | er.encoded += (2 * mlen) + tmper.encoded; |
| 707 | } |
| 708 | |
| 709 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 710 | |
Lev Walkin | d519380 | 2004-10-03 09:12:07 +0000 | [diff] [blame] | 711 | if(encs) { |
| 712 | xer_tmp_enc_t *enc = encs; |
| 713 | xer_tmp_enc_t *end = encs + encs_count; |
| 714 | ssize_t control_size = 0; |
| 715 | |
| 716 | cb = original_cb; |
| 717 | app_key = original_app_key; |
| 718 | qsort(encs, encs_count, sizeof(encs[0]), SET_OF_xer_order); |
| 719 | |
| 720 | for(; enc < end; enc++) { |
| 721 | _ASN_CALLBACK(enc->buffer, enc->offset); |
| 722 | FREEMEM(enc->buffer); |
| 723 | enc->buffer = 0; |
| 724 | control_size += enc->offset; |
| 725 | } |
| 726 | assert(control_size == er.encoded); |
| 727 | } |
| 728 | |
| 729 | goto cleanup; |
| 730 | cb_failed: |
| 731 | er.encoded = -1; |
| 732 | er.failed_type = td; |
| 733 | er.structure_ptr = sptr; |
| 734 | cleanup: |
| 735 | if(encs) { |
| 736 | while(encs_count-- > 0) { |
| 737 | if(encs[encs_count].buffer) |
| 738 | FREEMEM(encs[encs_count].buffer); |
| 739 | } |
| 740 | free(encs); |
| 741 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 742 | return er; |
| 743 | } |
| 744 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 745 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 746 | 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] | 747 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 748 | asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 749 | const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 750 | int ret; |
| 751 | int i; |
| 752 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 753 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 754 | |
| 755 | /* Dump preamble */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 756 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 757 | || cb(" ::= {", 6, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 758 | return -1; |
| 759 | |
| 760 | for(i = 0; i < list->count; i++) { |
| 761 | const void *memb_ptr = list->array[i]; |
| 762 | if(!memb_ptr) continue; |
| 763 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 764 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 765 | |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 766 | ret = elm->type->print_struct(elm->type, memb_ptr, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 767 | ilevel + 1, cb, app_key); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 768 | if(ret) return ret; |
| 769 | } |
| 770 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 771 | ilevel--; |
| 772 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 773 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 774 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | void |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 778 | 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] | 779 | if(td && ptr) { |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 780 | asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 781 | asn_anonymous_set_ *list = _A_SET_FROM_VOID(ptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 782 | int i; |
| 783 | |
| 784 | /* |
| 785 | * Could not use set_of_empty() because of (*free) |
| 786 | * incompatibility. |
| 787 | */ |
| 788 | for(i = 0; i < list->count; i++) { |
| 789 | void *memb_ptr = list->array[i]; |
| 790 | if(memb_ptr) |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 791 | elm->type->free_struct(elm->type, memb_ptr, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 792 | } |
Lev Walkin | 246a2af | 2004-07-15 10:51:26 +0000 | [diff] [blame] | 793 | list->count = 0; /* No meaningful elements left */ |
| 794 | |
| 795 | asn_set_empty(list); /* Remove (list->array) */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 796 | |
| 797 | if(!contents_only) { |
| 798 | FREEMEM(ptr); |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 804 | SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 805 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 806 | asn_TYPE_member_t *elm = td->elements; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 807 | asn_constr_check_f *constr; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 808 | const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 809 | int i; |
| 810 | |
| 811 | if(!sptr) { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 812 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 813 | "%s: value not given (%s:%d)", |
| 814 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 815 | return -1; |
| 816 | } |
| 817 | |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 818 | constr = elm->memb_constraints; |
| 819 | if(!constr) constr = elm->type->check_constraints; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 820 | |
| 821 | /* |
| 822 | * Iterate over the members of an array. |
| 823 | * Validate each in turn, until one fails. |
| 824 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 825 | for(i = 0; i < list->count; i++) { |
| 826 | const void *memb_ptr = list->array[i]; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 827 | int ret; |
| 828 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 829 | if(!memb_ptr) continue; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 830 | |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 831 | ret = constr(elm->type, memb_ptr, app_errlog, app_key); |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 832 | if(ret) return ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 835 | /* |
| 836 | * Cannot inherit it eralier: |
| 837 | * need to make sure we get the updated version. |
| 838 | */ |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 839 | if(!elm->memb_constraints) |
| 840 | elm->memb_constraints = elm->type->check_constraints; |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 841 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 842 | return 0; |
| 843 | } |