constr_SET_OF_aper.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 
11  const asn_per_constraints_t *constraints, const void *sptr,
12  asn_per_outp_t *po) {
13  const asn_anonymous_set_ *list;
14  const asn_per_constraint_t *ct;
15  const asn_TYPE_member_t *elm = td->elements;
16  struct _el_buffer *encoded_els;
17  asn_enc_rval_t er = {0,0,0};
18  int seq;
19 
20  if(!sptr) ASN__ENCODE_FAILED;
21 
22  list = _A_CSET_FROM_VOID(sptr);
23 
24  er.encoded = 0;
25 
26  ASN_DEBUG("Encoding %s as SET OF (%d)", td->name, list->count);
27 
28  if(constraints) ct = &constraints->size;
31  else ct = 0;
32 
33  /* If extensible constraint, check if size is in root */
34  if(ct) {
35  int not_in_root =
36  (list->count < ct->lower_bound || list->count > ct->upper_bound);
37  ASN_DEBUG("lb %lld ub %lld %s",
38  (long long int)ct->lower_bound,
39  (long long int)ct->upper_bound,
40  ct->flags & APC_EXTENSIBLE ? "ext" : "fix");
41  if(ct->flags & APC_EXTENSIBLE) {
42  /* Declare whether size is in extension root */
43  if(per_put_few_bits(po, not_in_root, 1)) ASN__ENCODE_FAILED;
44  if(not_in_root) ct = 0;
45  } else if(not_in_root && ct->effective_bits >= 0) {
47  }
48 
49  }
50 
51  if(ct && ct->effective_bits >= 0) {
52  /* X.691, #19.5: No length determinant */
53  /*if(per_put_few_bits(po, list->count - ct->lower_bound,
54  ct->effective_bits))
55  ASN__ENCODE_FAILED;*/
56 
57  if (aper_put_length(po, ct->lower_bound, ct->upper_bound, list->count - ct->lower_bound, 0) < 0) {
59  }
60  }
61 
62  /*
63  * Canonical PER #22.1 mandates dynamic sorting of the SET OF elements
64  * according to their encodings. Build an array of the encoded elements.
65  */
66  encoded_els = SET_OF__encode_sorted(elm, list, SOES_CAPER);
67 
68  for(seq = 0; seq < list->count;) {
69  ssize_t may_encode;
70  int need_eom = 0;
71  if(ct && ct->effective_bits >= 0) {
72  may_encode = list->count;
73  } else {
74  may_encode =
75  aper_put_length(po, -1, -1, list->count - seq, &need_eom);
76  if(may_encode < 0) ASN__ENCODE_FAILED;
77  }
78 
79  while(may_encode--) {
80  const struct _el_buffer *el = &encoded_els[seq++];
81  if(asn_put_many_bits(po, el->buf,
82  (8 * el->length) - el->bits_unused) < 0) {
83  break;
84  }
85  }
86  if(need_eom && (aper_put_length(po, -1, -1, 0, NULL) < 0))
87  ASN__ENCODE_FAILED; /* End of Message length */
88  }
89 
90  /* If element's count is zero, we still need to output size 0 */
91  if (!list->count)
92  if(aper_put_length(po, -1, -1, 0, NULL) < 0)
94 
95  SET_OF__encode_sorted_free(encoded_els, list->count);
96 
97  ASN__ENCODED_OK(er);
98 }
99 
101 SET_OF_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
102  const asn_TYPE_descriptor_t *td,
103  const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
104  asn_dec_rval_t rv = {RC_OK, 0};
105  const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
106  const asn_TYPE_member_t *elm = td->elements; /* Single one */
107  void *st = *sptr;
108  asn_anonymous_set_ *list;
109  const asn_per_constraint_t *ct;
110  int repeat = 0;
111  ssize_t nelems;
112 
113  if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
115 
116  /*
117  * Create the target structure if it is not present already.
118  */
119  if(!st) {
120  st = *sptr = CALLOC(1, specs->struct_size);
121  if(!st) ASN__DECODE_FAILED;
122  }
123  list = _A_SET_FROM_VOID(st);
124 
125  /* Figure out which constraints to use */
126  if(constraints) ct = &constraints->size;
129  else ct = 0;
130 
131  if(ct && ct->flags & APC_EXTENSIBLE) {
132  int value = per_get_few_bits(pd, 1);
133  if(value < 0) ASN__DECODE_STARVED;
134  if(value) ct = 0; /* Not restricted! */
135  }
136 
137  if(ct && ct->upper_bound >= 1 && ct->upper_bound <= 65535
138  && ct->upper_bound == ct->lower_bound) {
139  /* X.691, #19.5: No length determinant */
140  nelems = ct->upper_bound;
141  ASN_DEBUG("Preparing to fetch %ld elements from %s",
142  (long)nelems, td->name);
143  } else {
144  nelems = -1;
145  }
146 
147  do {
148  int i;
149  if(nelems < 0) {
150  if (ct)
151  nelems = aper_get_length(pd, ct->lower_bound ? ct->lower_bound : 0,
152  ct->upper_bound ? ct->upper_bound : -1,
153  ct->effective_bits, &repeat);
154  else
155  nelems = aper_get_length(pd, 0, -1, -1, &repeat);
156  ASN_DEBUG("Got to decode %d elements (eff %d)",
157  (int)nelems, (int)(ct ? ct->effective_bits : -1));
158  if(nelems < 0) ASN__DECODE_STARVED;
159  }
160 
161  for(i = 0; i < nelems; i++) {
162  void *ptr = 0;
163  ASN_DEBUG("SET OF %s decoding", elm->type->name);
164  rv = elm->type->op->aper_decoder(opt_codec_ctx, elm->type,
165  elm->encoding_constraints.per_constraints, &ptr, pd);
166  ASN_DEBUG("%s SET OF %s decoded %d, %p",
167  td->name, elm->type->name, rv.code, ptr);
168  if(rv.code == RC_OK) {
169  if(ASN_SET_ADD(list, ptr) == 0)
170  continue;
171  ASN_DEBUG("Failed to add element into %s",
172  td->name);
173  /* Fall through */
174  rv.code = RC_FAIL;
175  } else {
176  ASN_DEBUG("Failed decoding %s of %s (SET OF)",
177  elm->type->name, td->name);
178  }
179  if(ptr) ASN_STRUCT_FREE(*elm->type, ptr);
180  return rv;
181  }
182 
183  nelems = -1; /* Allow uper_get_length() */
184  } while(repeat);
185 
186  ASN_DEBUG("Decoded %s as SET OF", td->name);
187 
188  rv.code = RC_OK;
189  rv.consumed = 0;
190  return rv;
191 }
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_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__ENCODED_OK
#define ASN__ENCODED_OK(rval)
Definition: asn_codecs.h:67
SET_OF_decode_aper
asn_dec_rval_t SET_OF_decode_aper(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_aper.c:101
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
aper_put_length
ssize_t aper_put_length(asn_per_outp_t *po, ssize_t lb, ssize_t ub, size_t n, int *opt_need_eom)
Definition: aper_support.c:196
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
SET_OF_encode_aper
asn_enc_rval_t SET_OF_encode_aper(const asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints, const void *sptr, asn_per_outp_t *po)
Definition: constr_SET_OF_aper.c:10
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
SOES_CAPER
@ SOES_CAPER
Definition: constr_SET_OF.h:88
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
aper_get_length
ssize_t aper_get_length(asn_per_data_t *pd, ssize_t lb, ssize_t ub, int effective_bound_bits, int *repeat)
Definition: aper_support.c:20
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_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
asn_enc_rval_s::encoded
ssize_t encoded
Definition: asn_codecs.h:47
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_denm_coding
Author(s): Jean-Pierre Busch , Guido Küppers , Lennart Reiher
autogenerated on Sun May 18 2025 02:23:47