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> |
| 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 | */ |
vlm | c519061 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 14 | #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left) |
vlm | fa67ddc | 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 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 27 | #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size) |
vlm | fa67ddc | 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 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 33 | #undef ADVANCE |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | #define ADVANCE(num_bytes) do { \ |
| 35 | size_t num = num_bytes; \ |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 36 | ptr = ((char *)ptr) + num; \ |
vlm | fa67ddc | 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 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 46 | #undef NEXT_PHASE |
| 47 | #undef PHASE_OUT |
vlm | fa67ddc | 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 | */ |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 57 | #undef RETURN |
vlm | fa67ddc | 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 | */ |
| 67 | ber_dec_rval_t |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 68 | SET_OF_decode_ber(asn1_TYPE_descriptor_t *td, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 69 | void **struct_ptr, void *ptr, size_t size, int tag_mode) { |
| 70 | /* |
| 71 | * Bring closer parts of structure description. |
| 72 | */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 73 | asn1_SET_OF_specifics_t *specs = (asn1_SET_OF_specifics_t *)td->specifics; |
| 74 | asn1_TYPE_member_t *element = td->elements; /* Single one */ |
vlm | fa67ddc | 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. */ |
| 80 | ber_dec_ctx_t *ctx; /* Decoder context */ |
| 81 | |
| 82 | ber_tlv_tag_t tlv_tag; /* T from TLV */ |
| 83 | //ber_tlv_len_t tlv_len; /* L from TLV */ |
| 84 | ber_dec_rval_t rval; /* Return code from subparsers */ |
| 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 | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 103 | ctx = (ber_dec_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 | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 116 | rval = ber_check_tags(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 | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 121 | consumed_myself += rval.consumed; |
| 122 | RETURN(rval.code); |
| 123 | } |
| 124 | |
| 125 | if(ctx->left >= 0) |
| 126 | ctx->left += rval.consumed; /* ?Substracted below! */ |
| 127 | ADVANCE(rval.consumed); |
| 128 | |
| 129 | ASN_DEBUG("Structure consumes %ld bytes, " |
| 130 | "buffer %ld", (long)ctx->left, (long)size); |
| 131 | |
| 132 | NEXT_PHASE(ctx); |
| 133 | /* Fall through */ |
| 134 | case 1: |
| 135 | /* |
| 136 | * PHASE 1. |
| 137 | * From the place where we've left it previously, |
| 138 | * try to decode the next item. |
| 139 | */ |
| 140 | for(;; ctx->step = 0) { |
| 141 | ssize_t tag_len; /* Length of TLV's T */ |
| 142 | |
| 143 | if(ctx->step & 1) |
| 144 | goto microphase2; |
| 145 | |
| 146 | /* |
| 147 | * MICROPHASE 1: Synchronize decoding. |
| 148 | */ |
| 149 | |
| 150 | if(ctx->left == 0) { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 151 | ASN_DEBUG("End of SET OF %s", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 152 | /* |
| 153 | * No more things to decode. |
| 154 | * Exit out of here. |
| 155 | */ |
| 156 | PHASE_OUT(ctx); |
| 157 | RETURN(RC_OK); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Fetch the T from TLV. |
| 162 | */ |
| 163 | tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag); |
| 164 | switch(tag_len) { |
| 165 | case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE); |
| 166 | /* Fall through */ |
| 167 | case -1: RETURN(RC_FAIL); |
| 168 | } |
| 169 | |
| 170 | if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) { |
| 171 | if(LEFT < 2) { |
| 172 | if(SIZE_VIOLATION) |
| 173 | RETURN(RC_FAIL); |
| 174 | else |
| 175 | RETURN(RC_WMORE); |
| 176 | } else if(((uint8_t *)ptr)[1] == 0) { |
| 177 | /* |
| 178 | * Found the terminator of the |
| 179 | * indefinite length structure. |
| 180 | */ |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* Outmost tag may be unknown and cannot be fetched/compared */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 186 | if(element->tag != (ber_tlv_tag_t)-1) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 187 | if(BER_TAGS_EQUAL(tlv_tag, element->tag)) { |
| 188 | /* |
| 189 | * The new list member of expected type has arrived. |
| 190 | */ |
| 191 | } else { |
| 192 | ASN_DEBUG("Unexpected tag %s fixed SET OF %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 193 | ber_tlv_tag_string(tlv_tag), td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 194 | ASN_DEBUG("%s SET OF has tag %s", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 195 | td->name, ber_tlv_tag_string(element->tag)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 196 | RETURN(RC_FAIL); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * MICROPHASE 2: Invoke the member-specific decoder. |
| 202 | */ |
| 203 | ctx->step |= 1; /* Confirm entering next microphase */ |
| 204 | microphase2: |
| 205 | |
| 206 | /* |
| 207 | * Invoke the member fetch routine according to member's type |
| 208 | */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 209 | rval = element->type->ber_decoder(element->type, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | &ctx->ptr, ptr, LEFT, 0); |
| 211 | ASN_DEBUG("In %s SET OF %s code %d consumed %d", |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 212 | td->name, element->type->name, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 213 | rval.code, (int)rval.consumed); |
| 214 | switch(rval.code) { |
| 215 | case RC_OK: |
| 216 | { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 217 | A_SET_OF(void) *list; |
| 218 | (void *)list = st; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 219 | if(ASN_SET_ADD(list, ctx->ptr) != 0) |
| 220 | RETURN(RC_FAIL); |
| 221 | else |
| 222 | ctx->ptr = 0; |
| 223 | } |
| 224 | break; |
| 225 | case RC_WMORE: /* More data expected */ |
| 226 | if(!SIZE_VIOLATION) { |
| 227 | ADVANCE(rval.consumed); |
| 228 | RETURN(RC_WMORE); |
| 229 | } |
| 230 | /* Fall through */ |
| 231 | case RC_FAIL: /* Fatal error */ |
| 232 | RETURN(RC_FAIL); |
| 233 | } /* switch(rval) */ |
| 234 | |
| 235 | ADVANCE(rval.consumed); |
| 236 | } /* for(all list members) */ |
| 237 | |
| 238 | NEXT_PHASE(ctx); |
| 239 | case 2: |
| 240 | /* |
| 241 | * Read in all "end of content" TLVs. |
| 242 | */ |
| 243 | while(ctx->left < 0) { |
| 244 | if(LEFT < 2) { |
| 245 | if(LEFT > 0 && ((char *)ptr)[0] != 0) { |
| 246 | /* Unexpected tag */ |
| 247 | RETURN(RC_FAIL); |
| 248 | } else { |
| 249 | RETURN(RC_WMORE); |
| 250 | } |
| 251 | } |
| 252 | if(((char *)ptr)[0] == 0 |
| 253 | && ((char *)ptr)[1] == 0) { |
| 254 | ADVANCE(2); |
| 255 | ctx->left++; |
| 256 | } else { |
| 257 | RETURN(RC_FAIL); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | PHASE_OUT(ctx); |
| 262 | } |
| 263 | |
| 264 | RETURN(RC_OK); |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Internally visible buffer holding a single encoded element. |
| 269 | */ |
| 270 | struct _el_buffer { |
| 271 | uint8_t *buf; |
| 272 | size_t length; |
| 273 | size_t size; |
| 274 | }; |
| 275 | /* Append bytes to the above structure */ |
| 276 | 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] | 277 | struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 278 | |
| 279 | if(el_buf->length + size > el_buf->size) |
| 280 | return -1; |
| 281 | |
| 282 | memcpy(el_buf->buf + el_buf->length, buffer, size); |
| 283 | |
| 284 | el_buf->length += size; |
| 285 | return 0; |
| 286 | } |
| 287 | static int _el_buf_cmp(const void *ap, const void *bp) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 288 | const struct _el_buffer *a = (const struct _el_buffer *)ap; |
| 289 | const struct _el_buffer *b = (const struct _el_buffer *)bp; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 290 | int ret; |
| 291 | size_t common_len; |
| 292 | |
| 293 | if(a->length < b->length) |
| 294 | common_len = a->length; |
| 295 | else |
| 296 | common_len = b->length; |
| 297 | |
| 298 | ret = memcmp(a->buf, b->buf, common_len); |
| 299 | if(ret == 0) { |
| 300 | if(a->length < b->length) |
| 301 | ret = -1; |
| 302 | else if(a->length > b->length) |
| 303 | ret = 1; |
| 304 | } |
| 305 | |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * The DER encoder of the SET OF type. |
| 311 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 312 | asn_enc_rval_t |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 313 | SET_OF_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 314 | int tag_mode, ber_tlv_tag_t tag, |
| 315 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 316 | asn1_TYPE_member_t *elm = td->elements; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 317 | asn1_TYPE_descriptor_t *elm_type = elm->type; |
| 318 | der_type_encoder_f *der_encoder = elm_type->der_encoder; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 319 | A_SET_OF(void) *list; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 320 | size_t computed_size = 0; |
| 321 | ssize_t encoding_size = 0; |
| 322 | struct _el_buffer *encoded_els; |
| 323 | size_t max_encoded_len = 1; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 324 | asn_enc_rval_t erval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 325 | int ret; |
| 326 | int edx; |
| 327 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 328 | ASN_DEBUG("Estimating size for SET OF %s", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * Gather the length of the underlying members sequence. |
| 332 | */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 333 | (void *)list = ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 334 | for(edx = 0; edx < list->count; edx++) { |
| 335 | void *memb_ptr = list->array[edx]; |
| 336 | erval = der_encoder(elm_type, memb_ptr, 0, elm->tag, 0, 0); |
| 337 | if(erval.encoded == -1) |
| 338 | return erval; |
| 339 | computed_size += erval.encoded; |
| 340 | |
| 341 | /* Compute maximum encoding's size */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 342 | if(max_encoded_len < (size_t)erval.encoded) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 343 | max_encoded_len = erval.encoded; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Encode the TLV for the sequence itself. |
| 348 | */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 349 | encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 350 | cb, app_key); |
| 351 | if(encoding_size == -1) { |
| 352 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 353 | erval.failed_type = td; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 354 | erval.structure_ptr = ptr; |
| 355 | return erval; |
| 356 | } |
| 357 | computed_size += encoding_size; |
| 358 | |
| 359 | if(!cb) { |
| 360 | erval.encoded = computed_size; |
| 361 | return erval; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * DER mandates dynamic sorting of the SET OF elements |
| 366 | * according to their encodings. Build an array of the |
| 367 | * encoded elements. |
| 368 | */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 369 | encoded_els = (struct _el_buffer *)MALLOC( |
| 370 | list->count * sizeof(encoded_els[0])); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 371 | if(encoded_els == NULL) { |
| 372 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 373 | erval.failed_type = td; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 374 | erval.structure_ptr = ptr; |
| 375 | return erval; |
| 376 | } |
| 377 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 378 | ASN_DEBUG("Encoding members of %s SET OF", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | * Encode all members. |
| 382 | */ |
| 383 | for(edx = 0; edx < list->count; edx++) { |
| 384 | void *memb_ptr = list->array[edx]; |
| 385 | struct _el_buffer *encoded_el = &encoded_els[edx]; |
| 386 | |
| 387 | /* |
| 388 | * Prepare space for encoding. |
| 389 | */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 390 | encoded_el->buf = (uint8_t *)MALLOC(max_encoded_len); |
vlm | fa67ddc | 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; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 399 | erval.failed_type = td; |
vlm | fa67ddc | 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; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Sort the encoded elements according to their encoding. |
| 420 | */ |
| 421 | qsort(encoded_els, list->count, sizeof(encoded_els[0]), _el_buf_cmp); |
| 422 | |
| 423 | /* |
| 424 | * Report encoded elements to the application. |
| 425 | * Dispose of temporary sorted members table. |
| 426 | */ |
| 427 | ret = 0; |
| 428 | for(edx = 0; edx < list->count; edx++) { |
| 429 | struct _el_buffer *encoded_el = &encoded_els[edx]; |
| 430 | /* Report encoded chunks to the application */ |
| 431 | if(ret == 0 |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 432 | && cb(encoded_el->buf, encoded_el->length, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 433 | ret = -1; |
| 434 | FREEMEM(encoded_el->buf); |
| 435 | } |
| 436 | FREEMEM(encoded_els); |
| 437 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 438 | if(ret || computed_size != (size_t)encoding_size) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 439 | /* |
| 440 | * Standard callback failed, or |
| 441 | * encoded size is not equal to the computed size. |
| 442 | */ |
| 443 | erval.encoded = -1; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 444 | erval.failed_type = td; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 445 | erval.structure_ptr = ptr; |
| 446 | } else { |
| 447 | erval.encoded = computed_size; |
| 448 | } |
| 449 | |
| 450 | return erval; |
| 451 | } |
| 452 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 453 | asn_enc_rval_t |
| 454 | SET_OF_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr, |
| 455 | int ilevel, enum xer_encoder_flags_e flags, |
| 456 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 457 | asn_enc_rval_t er; |
| 458 | asn1_SET_OF_specifics_t *specs=(asn1_SET_OF_specifics_t *)td->specifics; |
| 459 | asn1_TYPE_member_t *element = td->elements; |
| 460 | A_SET_OF(void) *list; |
| 461 | const char *mname = specs->as_XMLValueList |
| 462 | ? 0 : ((*element->name) ? element->name : element->type->name); |
| 463 | size_t mlen = mname ? strlen(mname) : 0; |
| 464 | int xcan = (flags & XER_F_CANONICAL); |
| 465 | int i; |
| 466 | |
| 467 | if(!sptr) _ASN_ENCODE_FAILED; |
| 468 | |
| 469 | er.encoded = 0; |
| 470 | |
| 471 | (void *)list = sptr; |
| 472 | for(i = 0; i < list->count; i++) { |
| 473 | asn_enc_rval_t tmper; |
| 474 | |
| 475 | void *memb_ptr = list->array[i]; |
| 476 | if(!memb_ptr) continue; |
| 477 | |
| 478 | if(mname) { |
| 479 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel); |
| 480 | _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 481 | } |
| 482 | |
| 483 | tmper = element->type->xer_encoder(element->type, memb_ptr, |
| 484 | ilevel + 1, flags, cb, app_key); |
| 485 | if(tmper.encoded == -1) return tmper; |
| 486 | |
| 487 | if(mname) { |
| 488 | _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1); |
| 489 | er.encoded += 5; |
| 490 | } |
| 491 | |
| 492 | er.encoded += (2 * mlen) + tmper.encoded; |
| 493 | } |
| 494 | |
| 495 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
| 496 | |
| 497 | return er; |
| 498 | } |
| 499 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 500 | int |
| 501 | SET_OF_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 502 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 503 | asn1_TYPE_member_t *element = td->elements; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 504 | const A_SET_OF(void) *list; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 505 | int ret; |
| 506 | int i; |
| 507 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 508 | if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 509 | |
| 510 | /* Dump preamble */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 511 | if(cb(td->name, strlen(td->name), app_key) < 0 |
| 512 | || cb(" ::= {", 6, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 513 | return -1; |
| 514 | |
vlm | e517408 | 2004-08-11 09:34:42 +0000 | [diff] [blame] | 515 | (const void *)list = sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 516 | for(i = 0; i < list->count; i++) { |
| 517 | const void *memb_ptr = list->array[i]; |
| 518 | if(!memb_ptr) continue; |
| 519 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 520 | _i_INDENT(1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 521 | |
| 522 | ret = element->type->print_struct(element->type, memb_ptr, |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 523 | ilevel + 1, cb, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 524 | if(ret) return ret; |
| 525 | } |
| 526 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 527 | ilevel--; |
| 528 | _i_INDENT(1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 529 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 530 | return (cb("}", 1, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void |
| 534 | SET_OF_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
| 535 | if(td && ptr) { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 536 | asn1_TYPE_member_t *element = td->elements; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 537 | A_SET_OF(void) *list; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 538 | int i; |
| 539 | |
| 540 | /* |
| 541 | * Could not use set_of_empty() because of (*free) |
| 542 | * incompatibility. |
| 543 | */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 544 | (void *)list = ptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 545 | for(i = 0; i < list->count; i++) { |
| 546 | void *memb_ptr = list->array[i]; |
| 547 | if(memb_ptr) |
| 548 | element->type->free_struct(element->type, memb_ptr, 0); |
| 549 | } |
vlm | 7ebd67c | 2004-07-15 10:51:26 +0000 | [diff] [blame] | 550 | list->count = 0; /* No meaningful elements left */ |
| 551 | |
| 552 | asn_set_empty(list); /* Remove (list->array) */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 553 | |
| 554 | if(!contents_only) { |
| 555 | FREEMEM(ptr); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | int |
| 561 | SET_OF_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 562 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 563 | asn1_TYPE_member_t *element = td->elements; |
| 564 | asn_constr_check_f *constr; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 565 | const A_SET_OF(void) *list; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 566 | int i; |
| 567 | |
| 568 | if(!sptr) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 569 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 570 | "%s: value not given (%s:%d)", |
| 571 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 572 | return -1; |
| 573 | } |
| 574 | |
vlm | e517408 | 2004-08-11 09:34:42 +0000 | [diff] [blame] | 575 | (const void *)list = sptr; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 576 | |
| 577 | constr = element->memb_constraints; |
| 578 | if(!constr) constr = element->type->check_constraints; |
| 579 | |
| 580 | /* |
| 581 | * Iterate over the members of an array. |
| 582 | * Validate each in turn, until one fails. |
| 583 | */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 584 | for(i = 0; i < list->count; i++) { |
| 585 | const void *memb_ptr = list->array[i]; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 586 | int ret; |
| 587 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 588 | if(!memb_ptr) continue; |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 589 | |
| 590 | ret = constr(element->type, memb_ptr, app_errlog, app_key); |
| 591 | if(ret) return ret; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 592 | } |
| 593 | |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 594 | /* |
| 595 | * Cannot inherit it eralier: |
| 596 | * need to make sure we get the updated version. |
| 597 | */ |
| 598 | if(!element->memb_constraints) |
| 599 | element->memb_constraints = element->type->check_constraints; |
| 600 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 601 | return 0; |
| 602 | } |