ITS
constr_SET_OF.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <constr_SET_OF.h>
8 #include <asn_SET_OF.h>
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  */
15 #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
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  */
28 #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
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  */
34 #undef ADVANCE
35 #define ADVANCE(num_bytes) do { \
36  size_t num = num_bytes; \
37  ptr = ((const char *)ptr) + num;\
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  */
47 #undef NEXT_PHASE
48 #undef PHASE_OUT
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  */
58 #undef RETURN
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  */
70  void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
71  /*
72  * Bring closer parts of structure description.
73  */
75  asn_TYPE_member_t *elm = td->elements; /* Single one */
76 
77  /*
78  * Parts of the structure being constructed.
79  */
80  void *st = *struct_ptr; /* Target structure. */
81  asn_struct_ctx_t *ctx; /* Decoder context */
82 
83  ber_tlv_tag_t tlv_tag; /* T from TLV */
84  asn_dec_rval_t rval; /* Return code from subparsers */
85 
86  ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
87 
88  ASN_DEBUG("Decoding %s as SET OF", td->name);
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  */
103  ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
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 
116  rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
117  tag_mode, 1, &ctx->left, 0);
118  if(rval.code != RC_OK) {
119  ASN_DEBUG("%s tagging check failed: %d",
120  td->name, rval.code);
121  return rval;
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) {
150  ASN_DEBUG("End of SET OF %s", td->name);
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 && ((const 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(((const 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 */
185  if(elm->tag != (ber_tlv_tag_t)-1) {
186  if(BER_TAGS_EQUAL(tlv_tag, elm->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",
192  ber_tlv_tag_string(tlv_tag), td->name);
193  ASN_DEBUG("%s SET OF has tag %s",
194  td->name, ber_tlv_tag_string(elm->tag));
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  */
208  rval = elm->type->ber_decoder(opt_codec_ctx,
209  elm->type, &ctx->ptr, ptr, LEFT, 0);
210  ASN_DEBUG("In %s SET OF %s code %d consumed %d",
211  td->name, elm->type->name,
212  rval.code, (int)rval.consumed);
213  switch(rval.code) {
214  case RC_OK:
215  {
216  asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
217  if(ASN_SET_ADD(list, ctx->ptr) != 0)
218  RETURN(RC_FAIL);
219  else
220  ctx->ptr = 0;
221  }
222  break;
223  case RC_WMORE: /* More data expected */
224  if(!SIZE_VIOLATION) {
225  ADVANCE(rval.consumed);
226  RETURN(RC_WMORE);
227  }
228  /* Fall through */
229  case RC_FAIL: /* Fatal error */
230  ASN_STRUCT_FREE(*elm->type, ctx->ptr);
231  ctx->ptr = 0;
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 && ((const char *)ptr)[0] != 0) {
246  /* Unexpected tag */
247  RETURN(RC_FAIL);
248  } else {
249  RETURN(RC_WMORE);
250  }
251  }
252  if(((const char *)ptr)[0] == 0
253  && ((const 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) {
277  struct _el_buffer *el_buf = (struct _el_buffer *)el_buf_ptr;
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) {
288  const struct _el_buffer *a = (const struct _el_buffer *)ap;
289  const struct _el_buffer *b = (const struct _el_buffer *)bp;
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  */
314  int tag_mode, ber_tlv_tag_t tag,
315  asn_app_consume_bytes_f *cb, void *app_key) {
316  asn_TYPE_member_t *elm = td->elements;
317  asn_TYPE_descriptor_t *elm_type = elm->type;
318  der_type_encoder_f *der_encoder = elm_type->der_encoder;
319  asn_anonymous_set_ *list = _A_SET_FROM_VOID(ptr);
320  size_t computed_size = 0;
321  ssize_t encoding_size = 0;
322  struct _el_buffer *encoded_els;
323  ssize_t eels_count = 0;
324  size_t max_encoded_len = 1;
325  asn_enc_rval_t erval;
326  int ret;
327  int edx;
328 
329  ASN_DEBUG("Estimating size for SET OF %s", td->name);
330 
331  /*
332  * Gather the length of the underlying members sequence.
333  */
334  for(edx = 0; edx < list->count; edx++) {
335  void *memb_ptr = list->array[edx];
336  if(!memb_ptr) continue;
337  erval = der_encoder(elm_type, memb_ptr, 0, elm->tag, 0, 0);
338  if(erval.encoded == -1)
339  return erval;
340  computed_size += erval.encoded;
341 
342  /* Compute maximum encoding's size */
343  if(max_encoded_len < (size_t)erval.encoded)
344  max_encoded_len = erval.encoded;
345  }
346 
347  /*
348  * Encode the TLV for the sequence itself.
349  */
350  encoding_size = der_write_tags(td, computed_size, tag_mode, 1, tag,
351  cb, app_key);
352  if(encoding_size == -1) {
353  erval.encoded = -1;
354  erval.failed_type = td;
355  erval.structure_ptr = ptr;
356  return erval;
357  }
358  computed_size += encoding_size;
359 
360  if(!cb || list->count == 0) {
361  erval.encoded = computed_size;
362  _ASN_ENCODED_OK(erval);
363  }
364 
365  /*
366  * DER mandates dynamic sorting of the SET OF elements
367  * according to their encodings. Build an array of the
368  * encoded elements.
369  */
370  encoded_els = (struct _el_buffer *)MALLOC(
371  list->count * sizeof(encoded_els[0]));
372  if(encoded_els == NULL) {
373  erval.encoded = -1;
374  erval.failed_type = td;
375  erval.structure_ptr = ptr;
376  return erval;
377  }
378 
379  ASN_DEBUG("Encoding members of %s SET OF", td->name);
380 
381  /*
382  * Encode all members.
383  */
384  for(edx = 0; edx < list->count; edx++) {
385  void *memb_ptr = list->array[edx];
386  struct _el_buffer *encoded_el = &encoded_els[eels_count];
387 
388  if(!memb_ptr) continue;
389 
390  /*
391  * Prepare space for encoding.
392  */
393  encoded_el->buf = (uint8_t *)MALLOC(max_encoded_len);
394  if(encoded_el->buf) {
395  encoded_el->length = 0;
396  encoded_el->size = max_encoded_len;
397  } else {
398  for(edx--; edx >= 0; edx--)
399  FREEMEM(encoded_els[edx].buf);
400  FREEMEM(encoded_els);
401  erval.encoded = -1;
402  erval.failed_type = td;
403  erval.structure_ptr = ptr;
404  return erval;
405  }
406 
407  /*
408  * Encode the member into the prepared space.
409  */
410  erval = der_encoder(elm_type, memb_ptr, 0, elm->tag,
411  _el_addbytes, encoded_el);
412  if(erval.encoded == -1) {
413  for(; edx >= 0; edx--)
414  FREEMEM(encoded_els[edx].buf);
415  FREEMEM(encoded_els);
416  return erval;
417  }
418  encoding_size += erval.encoded;
419  eels_count++;
420  }
421 
422  /*
423  * Sort the encoded elements according to their encoding.
424  */
425  qsort(encoded_els, eels_count, sizeof(encoded_els[0]), _el_buf_cmp);
426 
427  /*
428  * Report encoded elements to the application.
429  * Dispose of temporary sorted members table.
430  */
431  ret = 0;
432  for(edx = 0; edx < eels_count; edx++) {
433  struct _el_buffer *encoded_el = &encoded_els[edx];
434  /* Report encoded chunks to the application */
435  if(ret == 0
436  && cb(encoded_el->buf, encoded_el->length, app_key) < 0)
437  ret = -1;
438  FREEMEM(encoded_el->buf);
439  }
440  FREEMEM(encoded_els);
441 
442  if(ret || computed_size != (size_t)encoding_size) {
443  /*
444  * Standard callback failed, or
445  * encoded size is not equal to the computed size.
446  */
447  erval.encoded = -1;
448  erval.failed_type = td;
449  erval.structure_ptr = ptr;
450  } else {
451  erval.encoded = computed_size;
452  }
453 
454  _ASN_ENCODED_OK(erval);
455 }
456 
457 #undef XER_ADVANCE
458 #define XER_ADVANCE(num_bytes) do { \
459  size_t num = num_bytes; \
460  buf_ptr = ((const char *)buf_ptr) + num;\
461  size -= num; \
462  consumed_myself += num; \
463  } while(0)
464 
465 /*
466  * Decode the XER (XML) data.
467  */
470  void **struct_ptr, const char *opt_mname,
471  const void *buf_ptr, size_t size) {
472  /*
473  * Bring closer parts of structure description.
474  */
476  asn_TYPE_member_t *element = td->elements;
477  const char *elm_tag;
478  const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
479 
480  /*
481  * ... and parts of the structure being constructed.
482  */
483  void *st = *struct_ptr; /* Target structure. */
484  asn_struct_ctx_t *ctx; /* Decoder context */
485 
486  asn_dec_rval_t rval; /* Return value from a decoder */
487  ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
488 
489  /*
490  * Create the target structure if it is not present already.
491  */
492  if(st == 0) {
493  st = *struct_ptr = CALLOC(1, specs->struct_size);
494  if(st == 0) RETURN(RC_FAIL);
495  }
496 
497  /* Which tag is expected for the downstream */
498  if(specs->as_XMLValueList) {
499  elm_tag = (specs->as_XMLValueList == 1) ? 0 : "";
500  } else {
501  elm_tag = (*element->name)
502  ? element->name : element->type->xml_tag;
503  }
504 
505  /*
506  * Restore parsing context.
507  */
508  ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
509 
510  /*
511  * Phases of XER/XML processing:
512  * Phase 0: Check that the opening tag matches our expectations.
513  * Phase 1: Processing body and reacting on closing tag.
514  * Phase 2: Processing inner type.
515  */
516  for(; ctx->phase <= 2;) {
517  pxer_chunk_type_e ch_type; /* XER chunk type */
518  ssize_t ch_size; /* Chunk size */
519  xer_check_tag_e tcv; /* Tag check value */
520 
521  /*
522  * Go inside the inner member of a set.
523  */
524  if(ctx->phase == 2) {
525  asn_dec_rval_t tmprval;
526 
527  /* Invoke the inner type decoder, m.b. multiple times */
528  ASN_DEBUG("XER/SET OF element [%s]", elm_tag);
529  tmprval = element->type->xer_decoder(opt_codec_ctx,
530  element->type, &ctx->ptr, elm_tag,
531  buf_ptr, size);
532  if(tmprval.code == RC_OK) {
533  asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
534  if(ASN_SET_ADD(list, ctx->ptr) != 0)
535  RETURN(RC_FAIL);
536  ctx->ptr = 0;
537  XER_ADVANCE(tmprval.consumed);
538  } else {
539  XER_ADVANCE(tmprval.consumed);
540  RETURN(tmprval.code);
541  }
542  ctx->phase = 1; /* Back to body processing */
543  ASN_DEBUG("XER/SET OF phase => %d", ctx->phase);
544  /* Fall through */
545  }
546 
547  /*
548  * Get the next part of the XML stream.
549  */
550  ch_size = xer_next_token(&ctx->context,
551  buf_ptr, size, &ch_type);
552  switch(ch_size) {
553  case -1: RETURN(RC_FAIL);
554  case 0: RETURN(RC_WMORE);
555  default:
556  switch(ch_type) {
557  case PXER_COMMENT: /* Got XML comment */
558  case PXER_TEXT: /* Ignore free-standing text */
559  XER_ADVANCE(ch_size); /* Skip silently */
560  continue;
561  case PXER_TAG:
562  break; /* Check the rest down there */
563  }
564  }
565 
566  tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
567  ASN_DEBUG("XER/SET OF: tcv = %d, ph=%d t=%s",
568  tcv, ctx->phase, xml_tag);
569  switch(tcv) {
570  case XCT_CLOSING:
571  if(ctx->phase == 0) break;
572  ctx->phase = 0;
573  /* Fall through */
574  case XCT_BOTH:
575  if(ctx->phase == 0) {
576  /* No more things to decode */
577  XER_ADVANCE(ch_size);
578  ctx->phase = 3; /* Phase out */
579  RETURN(RC_OK);
580  }
581  /* Fall through */
582  case XCT_OPENING:
583  if(ctx->phase == 0) {
584  XER_ADVANCE(ch_size);
585  ctx->phase = 1; /* Processing body phase */
586  continue;
587  }
588  /* Fall through */
589  case XCT_UNKNOWN_OP:
590  case XCT_UNKNOWN_BO:
591 
592  ASN_DEBUG("XER/SET OF: tcv=%d, ph=%d", tcv, ctx->phase);
593  if(ctx->phase == 1) {
594  /*
595  * Process a single possible member.
596  */
597  ctx->phase = 2;
598  continue;
599  }
600  /* Fall through */
601  default:
602  break;
603  }
604 
605  ASN_DEBUG("Unexpected XML tag in SET OF");
606  break;
607  }
608 
609  ctx->phase = 3; /* "Phase out" on hard failure */
610  RETURN(RC_FAIL);
611 }
612 
613 
614 
615 typedef struct xer_tmp_enc_s {
616  void *buffer;
617  size_t offset;
618  size_t size;
619 } xer_tmp_enc_t;
620 static int
621 SET_OF_encode_xer_callback(const void *buffer, size_t size, void *key) {
622  xer_tmp_enc_t *t = (xer_tmp_enc_t *)key;
623  if(t->offset + size >= t->size) {
624  size_t newsize = (t->size << 2) + size;
625  void *p = REALLOC(t->buffer, newsize);
626  if(!p) return -1;
627  t->buffer = p;
628  t->size = newsize;
629  }
630  memcpy((char *)t->buffer + t->offset, buffer, size);
631  t->offset += size;
632  return 0;
633 }
634 static int
635 SET_OF_xer_order(const void *aptr, const void *bptr) {
636  const xer_tmp_enc_t *a = (const xer_tmp_enc_t *)aptr;
637  const xer_tmp_enc_t *b = (const xer_tmp_enc_t *)bptr;
638  size_t minlen = a->offset;
639  int ret;
640  if(b->offset < minlen) minlen = b->offset;
641  /* Well-formed UTF-8 has this nice lexicographical property... */
642  ret = memcmp(a->buffer, b->buffer, minlen);
643  if(ret != 0) return ret;
644  if(a->offset == b->offset)
645  return 0;
646  if(a->offset == minlen)
647  return -1;
648  return 1;
649 }
650 
651 
654  int ilevel, enum xer_encoder_flags_e flags,
655  asn_app_consume_bytes_f *cb, void *app_key) {
656  asn_enc_rval_t er;
658  asn_TYPE_member_t *elm = td->elements;
659  asn_anonymous_set_ *list = _A_SET_FROM_VOID(sptr);
660  const char *mname = specs->as_XMLValueList
661  ? 0 : ((*elm->name) ? elm->name : elm->type->xml_tag);
662  size_t mlen = mname ? strlen(mname) : 0;
663  int xcan = (flags & XER_F_CANONICAL);
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;
668  int i;
669 
670  if(!sptr) _ASN_ENCODE_FAILED;
671 
672  if(xcan) {
673  encs = (xer_tmp_enc_t *)MALLOC(list->count * sizeof(encs[0]));
674  if(!encs) _ASN_ENCODE_FAILED;
676  }
677 
678  er.encoded = 0;
679 
680  for(i = 0; i < list->count; i++) {
681  asn_enc_rval_t tmper;
682 
683  void *memb_ptr = list->array[i];
684  if(!memb_ptr) continue;
685 
686  if(encs) {
687  memset(&encs[encs_count], 0, sizeof(encs[0]));
688  app_key = &encs[encs_count];
689  encs_count++;
690  }
691 
692  if(mname) {
693  if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
694  _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
695  }
696 
697  if(!xcan && specs->as_XMLValueList == 1)
698  _i_ASN_TEXT_INDENT(1, ilevel + 1);
699  tmper = elm->type->xer_encoder(elm->type, memb_ptr,
700  ilevel + (specs->as_XMLValueList != 2),
701  flags, cb, app_key);
702  if(tmper.encoded == -1) {
703  td = tmper.failed_type;
704  sptr = tmper.structure_ptr;
705  goto cb_failed;
706  }
707  if(tmper.encoded == 0 && specs->as_XMLValueList) {
708  const char *name = elm->type->xml_tag;
709  size_t len = strlen(name);
710  _ASN_CALLBACK3("<", 1, name, len, "/>", 2);
711  }
712 
713  if(mname) {
714  _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
715  er.encoded += 5;
716  }
717 
718  er.encoded += (2 * mlen) + tmper.encoded;
719  }
720 
721  if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
722 
723  if(encs) {
724  xer_tmp_enc_t *enc = encs;
725  xer_tmp_enc_t *end = encs + encs_count;
726  ssize_t control_size = 0;
727 
728  cb = original_cb;
729  app_key = original_app_key;
730  qsort(encs, encs_count, sizeof(encs[0]), SET_OF_xer_order);
731 
732  for(; enc < end; enc++) {
733  _ASN_CALLBACK(enc->buffer, enc->offset);
734  FREEMEM(enc->buffer);
735  enc->buffer = 0;
736  control_size += enc->offset;
737  }
738  assert(control_size == er.encoded);
739  }
740 
741  goto cleanup;
742 cb_failed:
743  er.encoded = -1;
744  er.failed_type = td;
745  er.structure_ptr = sptr;
746 cleanup:
747  if(encs) {
748  while(encs_count-- > 0) {
749  if(encs[encs_count].buffer)
750  FREEMEM(encs[encs_count].buffer);
751  }
752  FREEMEM(encs);
753  }
754  _ASN_ENCODED_OK(er);
755 }
756 
757 int
758 SET_OF_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
759  asn_app_consume_bytes_f *cb, void *app_key) {
760  asn_TYPE_member_t *elm = td->elements;
761  const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
762  int ret;
763  int i;
764 
765  if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
766 
767  /* Dump preamble */
768  if(cb(td->name, strlen(td->name), app_key) < 0
769  || cb(" ::= {", 6, app_key) < 0)
770  return -1;
771 
772  for(i = 0; i < list->count; i++) {
773  const void *memb_ptr = list->array[i];
774  if(!memb_ptr) continue;
775 
776  _i_INDENT(1);
777 
778  ret = elm->type->print_struct(elm->type, memb_ptr,
779  ilevel + 1, cb, app_key);
780  if(ret) return ret;
781  }
782 
783  ilevel--;
784  _i_INDENT(1);
785 
786  return (cb("}", 1, app_key) < 0) ? -1 : 0;
787 }
788 
789 void
790 SET_OF_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
791  if(td && ptr) {
792  asn_SET_OF_specifics_t *specs;
793  asn_TYPE_member_t *elm = td->elements;
794  asn_anonymous_set_ *list = _A_SET_FROM_VOID(ptr);
795  asn_struct_ctx_t *ctx; /* Decoder context */
796  int i;
797 
798  /*
799  * Could not use set_of_empty() because of (*free)
800  * incompatibility.
801  */
802  for(i = 0; i < list->count; i++) {
803  void *memb_ptr = list->array[i];
804  if(memb_ptr)
805  ASN_STRUCT_FREE(*elm->type, memb_ptr);
806  }
807  list->count = 0; /* No meaningful elements left */
808 
809  asn_set_empty(list); /* Remove (list->array) */
810 
811  specs = (asn_SET_OF_specifics_t *)td->specifics;
812  ctx = (asn_struct_ctx_t *)((char *)ptr + specs->ctx_offset);
813  if(ctx->ptr) {
814  ASN_STRUCT_FREE(*elm->type, ctx->ptr);
815  ctx->ptr = 0;
816  }
817 
818  if(!contents_only) {
819  FREEMEM(ptr);
820  }
821  }
822 }
823 
824 int
826  asn_app_constraint_failed_f *ctfailcb, void *app_key) {
827  asn_TYPE_member_t *elm = td->elements;
828  asn_constr_check_f *constr;
829  const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
830  int i;
831 
832  if(!sptr) {
833  _ASN_CTFAIL(app_key, td, sptr,
834  "%s: value not given (%s:%d)",
835  td->name, __FILE__, __LINE__);
836  return -1;
837  }
838 
839  constr = elm->memb_constraints;
840  if(!constr) constr = elm->type->check_constraints;
841 
842  /*
843  * Iterate over the members of an array.
844  * Validate each in turn, until one fails.
845  */
846  for(i = 0; i < list->count; i++) {
847  const void *memb_ptr = list->array[i];
848  int ret;
849 
850  if(!memb_ptr) continue;
851 
852  ret = constr(elm->type, memb_ptr, ctfailcb, app_key);
853  if(ret) return ret;
854  }
855 
856  /*
857  * Cannot inherit it eralier:
858  * need to make sure we get the updated version.
859  */
860  if(!elm->memb_constraints)
862 
863  return 0;
864 }
865 
868  asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
869  asn_dec_rval_t rv;
871  asn_TYPE_member_t *elm = td->elements; /* Single one */
872  void *st = *sptr;
873  asn_anonymous_set_ *list;
875  int repeat = 0;
876  ssize_t nelems;
877 
878  if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
880 
881  /*
882  * Create the target structure if it is not present already.
883  */
884  if(!st) {
885  st = *sptr = CALLOC(1, specs->struct_size);
886  if(!st) _ASN_DECODE_FAILED;
887  }
888  list = _A_SET_FROM_VOID(st);
889 
890  /* Figure out which constraints to use */
891  if(constraints) ct = &constraints->size;
892  else if(td->per_constraints) ct = &td->per_constraints->size;
893  else ct = 0;
894 
895  if(ct && ct->flags & APC_EXTENSIBLE) {
896  int value = per_get_few_bits(pd, 1);
897  if(value < 0) _ASN_DECODE_STARVED;
898  if(value) ct = 0; /* Not restricted! */
899  }
900 
901  if(ct && ct->effective_bits >= 0) {
902  /* X.691, #19.5: No length determinant */
903  nelems = per_get_few_bits(pd, ct->effective_bits);
904  ASN_DEBUG("Preparing to fetch %ld+%ld elements from %s",
905  (long)nelems, ct->lower_bound, td->name);
906  if(nelems < 0) _ASN_DECODE_STARVED;
907  nelems += ct->lower_bound;
908  } else {
909  nelems = -1;
910  }
911 
912  do {
913  int i;
914  if(nelems < 0) {
915  nelems = uper_get_length(pd,
916  ct ? ct->effective_bits : -1, &repeat);
917  ASN_DEBUG("Got to decode %d elements (eff %d)",
918  (int)nelems, (int)(ct ? ct->effective_bits : -1));
919  if(nelems < 0) _ASN_DECODE_STARVED;
920  }
921 
922  for(i = 0; i < nelems; i++) {
923  void *ptr = 0;
924  ASN_DEBUG("SET OF %s decoding", elm->type->name);
925  rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
926  elm->per_constraints, &ptr, pd);
927  ASN_DEBUG("%s SET OF %s decoded %d, %p",
928  td->name, elm->type->name, rv.code, ptr);
929  if(rv.code == RC_OK) {
930  if(ASN_SET_ADD(list, ptr) == 0)
931  continue;
932  ASN_DEBUG("Failed to add element into %s",
933  td->name);
934  /* Fall through */
935  rv.code = RC_FAIL;
936  } else {
937  ASN_DEBUG("Failed decoding %s of %s (SET OF)",
938  elm->type->name, td->name);
939  }
940  if(ptr) ASN_STRUCT_FREE(*elm->type, ptr);
941  return rv;
942  }
943 
944  nelems = -1; /* Allow uper_get_length() */
945  } while(repeat);
946 
947  ASN_DEBUG("Decoded %s as SET OF", td->name);
948 
949  rv.code = RC_OK;
950  rv.consumed = 0;
951  return rv;
952 }
953 
#define _i_ASN_TEXT_INDENT(nl, level)
Definition: asn_internal.h:82
xer_check_tag
Definition: xer_decoder.h:76
ssize_t der_write_tags(struct asn_TYPE_descriptor_s *type_descriptor, size_t struct_length, int tag_mode, int last_tag_form, ber_tlv_tag_t tag, asn_app_consume_bytes_f *consume_bytes_cb, void *app_key)
Definition: der_encoder.c:77
int SET_OF_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, asn_app_consume_bytes_f *cb, void *app_key)
void * structure_ptr
Definition: asn_codecs.h:58
static int SET_OF_encode_xer_callback(const void *buffer, size_t size, void *key)
struct asn_TYPE_descriptor_s * failed_type
Definition: asn_codecs.h:55
ssize_t ber_fetch_tag(const void *bufptr, size_t size, ber_tlv_tag_t *tag_r)
Definition: ber_tlv_tag.c:10
#define XER_ADVANCE(num_bytes)
static int _el_buf_cmp(const void *ap, const void *bp)
#define NEXT_PHASE(ctx)
Definition: constr_SET_OF.c:49
asn_per_constraint_t size
Definition: per_support.h:32
#define _ASN_DECODE_STARVED
Definition: asn_codecs.h:98
struct xer_tmp_enc_s xer_tmp_enc_t
static int _el_addbytes(const void *buffer, size_t size, void *el_buf_ptr)
#define REALLOC(oldptr, size)
Definition: asn_internal.h:28
#define PHASE_OUT(ctx)
Definition: constr_SET_OF.c:53
void asn_set_empty(void *asn_set_of_x)
Definition: asn_SET_OF.c:71
char * ber_tlv_tag_string(ber_tlv_tag_t tag)
Definition: ber_tlv_tag.c:94
#define ASN_SET_ADD(headptr, ptr)
Definition: asn_SET_OF.h:20
#define RETURN(_code)
Definition: constr_SET_OF.c:59
xer_encoder_flags_e
Definition: xer_encoder.h:17
ssize_t encoded
Definition: asn_codecs.h:48
asn_constr_check_f * memb_constraints
Definition: constr_TYPE.h:147
asn_dec_rval_t SET_OF_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, void **struct_ptr, const char *opt_mname, const void *buf_ptr, size_t size)
#define CALLOC(nmemb, size)
Definition: asn_internal.h:26
static int _ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx)
Definition: asn_internal.h:105
asn_enc_rval_t SET_OF_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, int ilevel, enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb, void *app_key)
#define _ASN_CALLBACK(buf, size)
Definition: asn_internal.h:73
#define _ASN_ENCODED_OK(rval)
Definition: asn_codecs.h:68
int() asn_constr_check_f(struct asn_TYPE_descriptor_s *type_descriptor, const void *struct_ptr, asn_app_constraint_failed_f *optional_callback, void *optional_app_key)
Definition: constraints.h:40
#define _A_CSET_FROM_VOID(ptr)
Definition: asn_SET_OF.h:56
asn_constr_check_f * check_constraints
Definition: constr_TYPE.h:95
asn_enc_rval_t() der_type_encoder_f(struct asn_TYPE_descriptor_s *type_descriptor, void *struct_ptr, int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *consume_bytes_cb, void *app_key)
Definition: der_encoder.h:37
asn_dec_rval_t ber_check_tags(struct asn_codec_ctx_s *opt_codec_ctx, struct asn_TYPE_descriptor_s *type_descriptor, asn_struct_ctx_t *opt_ctx, const void *ptr, size_t size, int tag_mode, int last_tag_form, ber_tlv_len_t *last_length, int *opt_tlv_form)
Definition: ber_decoder.c:65
#define ASN_STRUCT_FREE(asn_DEF, ptr)
Definition: constr_TYPE.h:56
#define FREEMEM(ptr)
Definition: asn_internal.h:29
#define ADVANCE(num_bytes)
Definition: constr_SET_OF.c:35
#define _ASN_ENCODE_FAILED
Definition: asn_codecs.h:60
ssize_t xer_next_token(int *stateContext, const void *buffer, size_t size, pxer_chunk_type_e *_ch_type)
Definition: xer_decoder.c:63
asn_struct_print_f * print_struct
Definition: constr_TYPE.h:94
enum xer_check_tag xer_check_tag_e
struct asn_TYPE_member_s * elements
Definition: constr_TYPE.h:121
#define LEFT
Definition: constr_SET_OF.c:15
void SET_OF_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only)
int SET_OF_constraint(asn_TYPE_descriptor_t *td, const void *sptr, asn_app_constraint_failed_f *ctfailcb, void *app_key)
int() asn_app_consume_bytes_f(const void *buffer, size_t size, void *application_specific_key)
asn_dec_rval_t SET_OF_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd)
#define _ASN_CALLBACK3(buf1, size1, buf2, size2, buf3, size3)
Definition: asn_internal.h:77
int32_t per_get_few_bits(asn_per_data_t *per_data, int get_nbits)
Definition: per_support.c:38
#define SIZE_VIOLATION
Definition: constr_SET_OF.c:28
size_t consumed
Definition: asn_codecs.h:89
der_type_encoder_f * der_encoder
Definition: constr_TYPE.h:97
#define _i_INDENT(nl)
Definition: asn_internal.h:93
void() asn_app_constraint_failed_f(void *application_specific_key, struct asn_TYPE_descriptor_s *type_descriptor_which_failed, const void *structure_which_failed_ptr, const char *error_message_format,...) GCC_PRINTFLIKE(4
ssize_t uper_get_length(asn_per_data_t *pd, int effective_bound_bits, int *repeat)
Definition: per_support.c:167
enum asn_dec_rval_code_e code
Definition: asn_codecs.h:88
asn_TYPE_descriptor_t * type
Definition: constr_TYPE.h:146
size_t length
#define BER_TAGS_EQUAL(tag1, tag2)
Definition: ber_tlv_tag.h:27
uint8_t * buf
static void ASN_DEBUG(const char *fmt,...)
Definition: asn_internal.h:62
asn_enc_rval_t SET_OF_encode_der(asn_TYPE_descriptor_t *td, void *ptr, int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key)
#define _ASN_CTFAIL
Definition: constraints.h:57
#define _A_SET_FROM_VOID(ptr)
Definition: asn_SET_OF.h:55
#define MALLOC(size)
Definition: asn_internal.h:27
asn_dec_rval_t SET_OF_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, void **struct_ptr, const void *ptr, size_t size, int tag_mode)
Definition: constr_SET_OF.c:69
ber_tlv_tag_t tag
Definition: constr_TYPE.h:144
asn_per_constraints_t * per_constraints
Definition: constr_TYPE.h:116
static int SET_OF_xer_order(const void *aptr, const void *bptr)
enum pxer_chunk_type pxer_chunk_type_e
#define _ASN_DECODE_FAILED
Definition: asn_codecs.h:91
unsigned ber_tlv_tag_t
Definition: ber_tlv_tag.h:18