constr_SET_OF_uper.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
8 
10 SET_OF_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
11  const asn_TYPE_descriptor_t *td,
12  const asn_per_constraints_t *constraints, void **sptr,
13  asn_per_data_t *pd) {
14  asn_dec_rval_t rv = {RC_OK, 0};
15  const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
16  const asn_TYPE_member_t *elm = td->elements; /* Single one */
17  void *st = *sptr;
18  asn_anonymous_set_ *list;
19  const asn_per_constraint_t *ct;
20  int repeat = 0;
21  ssize_t nelems;
22 
23  if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
25 
26  /*
27  * Create the target structure if it is not present already.
28  */
29  if(!st) {
30  st = *sptr = CALLOC(1, specs->struct_size);
31  if(!st) ASN__DECODE_FAILED;
32  }
33  list = _A_SET_FROM_VOID(st);
34 
35  /* Figure out which constraints to use */
36  if(constraints) ct = &constraints->size;
39  else ct = 0;
40 
41  if(ct && ct->flags & APC_EXTENSIBLE) {
42  int value = per_get_few_bits(pd, 1);
43  if(value < 0) ASN__DECODE_STARVED;
44  if(value) ct = 0; /* Not restricted! */
45  }
46 
47  if(ct && ct->effective_bits >= 0) {
48  /* X.691, #19.5: No length determinant */
49  nelems = per_get_few_bits(pd, ct->effective_bits);
50  ASN_DEBUG("Preparing to fetch %ld+%"ASN_PRIdMAX" elements from %s",
51  (long)nelems, ct->lower_bound, td->name);
52  if(nelems < 0) ASN__DECODE_STARVED;
53  nelems += ct->lower_bound;
54  /* check if nelem is in root, when it is not extensible */
55  if (nelems > ct->upper_bound && !(ct->flags & APC_EXTENSIBLE)) {
57  }
58  } else {
59  nelems = -1;
60  }
61 
62  do {
63  int i;
64  if(nelems < 0) {
65  nelems = uper_get_length(pd, -1, 0, &repeat);
66  ASN_DEBUG("Got to decode %" ASN_PRI_SSIZE " elements (eff %d)",
67  nelems, (int)(ct ? ct->effective_bits : -1));
68  if(nelems < 0) ASN__DECODE_STARVED;
69  }
70 
71  for(i = 0; i < nelems; i++) {
72  void *ptr = 0;
73  ASN_DEBUG("SET OF %s decoding", elm->type->name);
74  rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
75  elm->encoding_constraints.per_constraints,
76  &ptr, pd);
77  ASN_DEBUG("%s SET OF %s decoded %d, %p",
78  td->name, elm->type->name, rv.code, ptr);
79  if(rv.code == RC_OK) {
80  if(ASN_SET_ADD(list, ptr) == 0) {
81  if(rv.consumed == 0 && nelems > 200) {
82  /* Protect from SET OF NULL compression bombs. */
84  }
85  continue;
86  }
87  ASN_DEBUG("Failed to add element into %s",
88  td->name);
89  /* Fall through */
90  rv.code = RC_FAIL;
91  } else {
92  ASN_DEBUG("Failed decoding %s of %s (SET OF)",
93  elm->type->name, td->name);
94  }
95  if(ptr) ASN_STRUCT_FREE(*elm->type, ptr);
96  return rv;
97  }
98 
99  nelems = -1; /* Allow uper_get_length() */
100  } while(repeat);
101 
102  ASN_DEBUG("Decoded %s as SET OF", td->name);
103 
104  rv.code = RC_OK;
105  rv.consumed = 0;
106  return rv;
107 }
108 
111  const asn_per_constraints_t *constraints, const void *sptr,
112  asn_per_outp_t *po) {
113  const asn_anonymous_set_ *list;
114  const asn_per_constraint_t *ct;
115  const asn_TYPE_member_t *elm = td->elements;
116  struct _el_buffer *encoded_els;
117  asn_enc_rval_t er = {0,0,0};
118  size_t encoded_edx;
119 
120  if(!sptr) ASN__ENCODE_FAILED;
121 
122  list = _A_CSET_FROM_VOID(sptr);
123 
124  er.encoded = 0;
125 
126  ASN_DEBUG("Encoding %s as SEQUENCE OF (%d)", td->name, list->count);
127 
128  if(constraints) ct = &constraints->size;
131  else ct = 0;
132 
133  /* If extensible constraint, check if size is in root */
134  if(ct) {
135  int not_in_root =
136  (list->count < ct->lower_bound || list->count > ct->upper_bound);
137  ASN_DEBUG("lb %"ASN_PRIdMAX" ub %"ASN_PRIdMAX" %s", ct->lower_bound, ct->upper_bound,
138  ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
139  if(ct->flags & APC_EXTENSIBLE) {
140  /* Declare whether size is in extension root */
141  if(per_put_few_bits(po, not_in_root, 1)) ASN__ENCODE_FAILED;
142  if(not_in_root) ct = 0;
143  } else if(not_in_root && ct->effective_bits >= 0) {
145  }
146 
147  }
148 
149  if(ct && ct->effective_bits >= 0) {
150  /* X.691, #19.5: No length determinant */
151  if(per_put_few_bits(po, list->count - ct->lower_bound,
152  ct->effective_bits))
154  } else if(list->count == 0) {
155  /* When the list is empty add only the length determinant
156  * X.691, #20.6 and #11.9.4.1
157  */
158  if (uper_put_length(po, 0, 0)) {
160  }
161  ASN__ENCODED_OK(er);
162  }
163 
164 
165  /*
166  * Canonical UPER #22.1 mandates dynamic sorting of the SET OF elements
167  * according to their encodings. Build an array of the encoded elements.
168  */
169  encoded_els = SET_OF__encode_sorted(elm, list, SOES_CUPER);
170 
171  for(encoded_edx = 0; (ssize_t)encoded_edx < list->count;) {
172  ssize_t may_encode;
173  size_t edx;
174  int need_eom = 0;
175 
176  if(ct && ct->effective_bits >= 0) {
177  may_encode = list->count;
178  } else {
179  may_encode =
180  uper_put_length(po, list->count - encoded_edx, &need_eom);
181  if(may_encode < 0) ASN__ENCODE_FAILED;
182  }
183 
184  for(edx = encoded_edx; edx < encoded_edx + may_encode; edx++) {
185  const struct _el_buffer *el = &encoded_els[edx];
186  if(asn_put_many_bits(po, el->buf,
187  (8 * el->length) - el->bits_unused) < 0) {
188  break;
189  }
190  }
191 
192  if(need_eom && uper_put_length(po, 0, 0))
193  ASN__ENCODE_FAILED; /* End of Message length */
194 
195  encoded_edx += may_encode;
196  }
197 
198  SET_OF__encode_sorted_free(encoded_els, list->count);
199 
200  if((ssize_t)encoded_edx == list->count) {
201  ASN__ENCODED_OK(er);
202  } else {
204  }
205 }
ASN_STRUCT_FREE
#define ASN_STRUCT_FREE(asn_DEF, ptr)
Definition: constr_TYPE.h:102
asn_bit_outp_s
Definition: asn_bit_data.h:56
SET_OF_encode_uper
asn_enc_rval_t SET_OF_encode_uper(const asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints, const void *sptr, asn_per_outp_t *po)
Definition: constr_SET_OF_uper.c:110
SET_OF__encode_sorted
struct _el_buffer * SET_OF__encode_sorted(const asn_TYPE_member_t *elm, const asn_anonymous_set_ *list, enum SET_OF__encode_method method)
Definition: constr_SET_OF.c:142
_el_buffer::bits_unused
unsigned bits_unused
Definition: constr_SET_OF.h:82
ASN_PRIdMAX
#define ASN_PRIdMAX
Definition: asn_system.h:181
ASN__ENCODED_OK
#define ASN__ENCODED_OK(rval)
Definition: asn_codecs.h:67
asn_TYPE_descriptor_s::name
const char * name
Definition: constr_TYPE.h:225
asn_enc_rval_s
Definition: asn_codecs.h:41
asn_per_constraint_s::lower_bound
intmax_t lower_bound
Definition: per_support.h:27
uper_put_length
ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length, int *opt_need_eom)
Definition: uper_support.c:168
CALLOC
#define CALLOC(nmemb, size)
Definition: asn_internal.h:37
per_get_few_bits
#define per_get_few_bits(data, bits)
Definition: per_support.h:39
asn_per_constraint_s
Definition: per_support.h:18
SOES_CUPER
@ SOES_CUPER
Definition: constr_SET_OF.h:87
asn_bit_data_s
Definition: asn_bit_data.h:17
ASN__ENCODE_FAILED
#define ASN__ENCODE_FAILED
Definition: asn_codecs.h:59
_el_buffer::length
size_t length
Definition: constr_SET_OF.h:80
asn_TYPE_descriptor_s
Definition: constr_TYPE.h:224
ASN_SET_ADD
#define ASN_SET_ADD(headptr, ptr)
Definition: asn_SET_OF.h:30
_el_buffer
Definition: constr_SET_OF.h:78
asn_TYPE_descriptor_s::specifics
const void * specifics
Definition: constr_TYPE.h:259
asn_SET_OF_specifics_s::struct_size
unsigned struct_size
Definition: constr_SET_OF.h:19
_A_SET_FROM_VOID
#define _A_SET_FROM_VOID(ptr)
Definition: asn_SET_OF.h:65
RC_OK
@ RC_OK
Definition: asn_codecs.h:82
asn_dec_rval_s::consumed
size_t consumed
Definition: asn_codecs.h:88
asn_per_constraint_s::effective_bits
int effective_bits
Definition: per_support.h:26
asn_put_many_bits
int asn_put_many_bits(asn_bit_outp_t *, const uint8_t *src, int put_nbits)
Definition: asn_bit_data.c:284
constr_SET_OF.h
ASN__STACK_OVERFLOW_CHECK
static int CC_NOTUSED ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx)
Definition: asn_internal.h:165
_A_CSET_FROM_VOID
#define _A_CSET_FROM_VOID(ptr)
Definition: asn_SET_OF.h:66
ASN__DECODE_STARVED
#define ASN__DECODE_STARVED
Definition: asn_codecs.h:97
asn_encoding_constraints_s::per_constraints
const struct asn_per_constraints_s * per_constraints
Definition: constr_TYPE.h:213
ASN_PRI_SSIZE
#define ASN_PRI_SSIZE
Definition: asn_system.h:173
asn_per_constraints_s::size
asn_per_constraint_t size
Definition: per_support.h:32
asn_internal.h
asn_codec_ctx_s
Definition: asn_codecs.h:23
asn_TYPE_descriptor_s::elements
struct asn_TYPE_member_s * elements
Definition: constr_TYPE.h:252
asn_dec_rval_s
Definition: asn_codecs.h:86
ASN__DECODE_FAILED
#define ASN__DECODE_FAILED
Definition: asn_codecs.h:90
SET_OF_decode_uper
asn_dec_rval_t SET_OF_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, const asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd)
Definition: constr_SET_OF_uper.c:10
asn_enc_rval_s::encoded
ssize_t encoded
Definition: asn_codecs.h:47
uper_get_length
ssize_t uper_get_length(asn_per_data_t *pd, int effective_bound_bits, size_t lower_bound, int *repeat)
Definition: uper_support.c:14
asn_per_constraints_s
Definition: per_support.h:30
asn_TYPE_member_s
Definition: constr_TYPE.h:272
asn_TYPE_descriptor_s::encoding_constraints
asn_encoding_constraints_t encoding_constraints
Definition: constr_TYPE.h:247
asn_per_constraint_s::upper_bound
intmax_t upper_bound
Definition: per_support.h:28
SET_OF__encode_sorted_free
void SET_OF__encode_sorted_free(struct _el_buffer *el_buf, size_t count)
Definition: constr_SET_OF.c:131
asn_per_constraint_s::flags
enum asn_per_constraint_s::asn_per_constraint_flags flags
asn_SET_OF_specifics_s
Definition: constr_SET_OF.h:15
_el_buffer::buf
uint8_t * buf
Definition: constr_SET_OF.h:79
per_put_few_bits
#define per_put_few_bits(out, bits, obits)
Definition: per_support.h:46
RC_FAIL
@ RC_FAIL
Definition: asn_codecs.h:84
asn_dec_rval_s::code
enum asn_dec_rval_code_e code
Definition: asn_codecs.h:87


etsi_its_vam_ts_coding
Author(s): Jean-Pierre Busch , Guido Küppers , Lennart Reiher
autogenerated on Sun May 18 2025 02:30:55