constr_CHOICE_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  */
9 
11 CHOICE_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
12  const asn_TYPE_descriptor_t *td,
13  const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
14  const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
15  asn_dec_rval_t rv;
16  const asn_per_constraint_t *ct = NULL;
17  asn_TYPE_member_t *elm = NULL; /* CHOICE's element */
18  void *memb_ptr = NULL;
19  void **memb_ptr2 = NULL;
20  void *st = *sptr;
21  int value = 0;
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 
34  if(constraints) ct = &constraints->value;
37  else ct = 0;
38 
39  if(ct && ct->flags & APC_EXTENSIBLE) {
40  value = per_get_few_bits(pd, 1);
41  if(value < 0) ASN__DECODE_STARVED;
42  if(value) ct = 0; /* Not restricted */
43  if((unsigned)value >= td->elements_count)
45  }
46 
47  if(ct && ct->range_bits >= 0) {
48  value = per_get_few_bits(pd, ct->range_bits);
49  if(value < 0) ASN__DECODE_STARVED;
50  ASN_DEBUG("CHOICE %s got index %d in range %d",
51  td->name, value, ct->range_bits);
52  if(value > ct->upper_bound)
54  } else {
55  if(specs->ext_start == -1)
57 
58  if(specs && specs->tag2el_count > (unsigned)specs->ext_start) {
59  value = aper_get_nsnnwn(pd); /* extension elements range */
60  if(value < 0) ASN__DECODE_STARVED;
61  value += specs->ext_start;
62  if((unsigned)value >= td->elements_count)
64  }
65  }
66 
67  /* Adjust if canonical order is different from natural order */
68  if(specs->from_canonical_order) {
69  ASN_DEBUG("CHOICE presence from wire %d", value);
70  value = specs->from_canonical_order[value];
71  ASN_DEBUG("CHOICE presence index effective %d", value);
72  }
73 
74  /* Set presence to be able to free it later */
75  _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
76 
77  elm = &td->elements[value];
78  if(elm->flags & ATF_POINTER) {
79  /* Member is a pointer to another structure */
80  memb_ptr2 = (void **)((char *)st + elm->memb_offset);
81  } else {
82  memb_ptr = (char *)st + elm->memb_offset;
83  memb_ptr2 = &memb_ptr;
84  }
85  ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
86 
87  if(ct && ct->range_bits >= 0) {
88  rv = elm->type->op->aper_decoder(opt_codec_ctx, elm->type,
89  elm->encoding_constraints.per_constraints, memb_ptr2, pd);
90  } else {
91  rv = aper_open_type_get(opt_codec_ctx, elm->type,
92  elm->encoding_constraints.per_constraints, memb_ptr2, pd);
93  }
94 
95  if(rv.code != RC_OK)
96  ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
97  elm->name, td->name, rv.code);
98  return rv;
99 }
100 
103  const asn_per_constraints_t *constraints,
104  const void *sptr, asn_per_outp_t *po) {
105  const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
106  const asn_TYPE_member_t *elm; /* CHOICE's element */
107  const asn_per_constraint_t *ct;
108  const void *memb_ptr;
109  int present;
110  int present_enc;
111 
112  if(!sptr) ASN__ENCODE_FAILED;
113 
114  ASN_DEBUG("Encoding %s as CHOICE using ALIGNED PER", td->name);
115 
116  if(constraints) ct = &constraints->value;
119  else ct = 0;
120 
121  present = _fetch_present_idx(sptr,
122  specs->pres_offset, specs->pres_size);
123 
124  /*
125  * If the structure was not initialized properly, it cannot be encoded:
126  * can't deduce what to encode in the choice type.
127  */
128  if(present <= 0 || (unsigned)present > td->elements_count)
130  else
131  present--;
132 
133  /* Adjust if canonical order is different from natural order */
134  if(specs->to_canonical_order)
135  present_enc = specs->to_canonical_order[present];
136  else
137  present_enc = present;
138 
139  ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
140 
141  if(ct && ct->range_bits >= 0) {
142  if(present_enc < ct->lower_bound
143  || present_enc > ct->upper_bound) {
144  if(ct->flags & APC_EXTENSIBLE) {
145  ASN_DEBUG(
146  "CHOICE member %d (enc %d) is an extension (%"ASN_PRIdMAX"..%"ASN_PRIdMAX")",
147  present, present_enc, ct->lower_bound, ct->upper_bound);
148  if(per_put_few_bits(po, 1, 1))
150  } else {
152  }
153  ct = 0;
154  }
155  }
156  if(ct && ct->flags & APC_EXTENSIBLE) {
157  ASN_DEBUG("CHOICE member %d (enc %d) is not an extension (%"ASN_PRIdMAX"..%"ASN_PRIdMAX")",
158  present, present_enc, ct->lower_bound, ct->upper_bound);
159  if(per_put_few_bits(po, 0, 1))
161  }
162 
163  elm = &td->elements[present];
164  ASN_DEBUG("CHOICE member \"%s\" %d (as %d)", elm->name, present,
165  present_enc);
166  if(elm->flags & ATF_POINTER) {
167  /* Member is a pointer to another structure */
168  memb_ptr = *(const void *const *)((const char *)sptr + elm->memb_offset);
169  if(!memb_ptr) ASN__ENCODE_FAILED;
170  } else {
171  memb_ptr = (const char *)sptr + elm->memb_offset;
172  }
173 
174  if(ct && ct->range_bits >= 0) {
175  if(per_put_few_bits(po, present_enc, ct->range_bits))
177 
178  return elm->type->op->aper_encoder(elm->type, elm->encoding_constraints.per_constraints,
179  memb_ptr, po);
180  } else {
181  asn_enc_rval_t rval = {0,0,0};
182  if(specs->ext_start == -1)
184  if(aper_put_nsnnwn(po, present_enc - specs->ext_start))
186  if(aper_open_type_put(elm->type, elm->encoding_constraints.per_constraints,
187  memb_ptr, po))
189  rval.encoded = 0;
190  ASN__ENCODED_OK(rval);
191  }
192 }
asn_bit_outp_s
Definition: asn_bit_data.h:56
asn_TYPE_member_s::memb_offset
unsigned memb_offset
Definition: constr_TYPE.h:275
CHOICE_decode_aper
asn_dec_rval_t CHOICE_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_CHOICE_aper.c:11
aper_open_type_get
asn_dec_rval_t aper_open_type_get(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: aper_opentype.c:125
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::elements_count
unsigned elements_count
Definition: constr_TYPE.h:253
asn_TYPE_descriptor_s::name
const char * name
Definition: constr_TYPE.h:225
asn_enc_rval_s
Definition: asn_codecs.h:41
CALLOC
#define CALLOC(nmemb, size)
Definition: asn_internal.h:37
aper_put_nsnnwn
int aper_put_nsnnwn(asn_per_outp_t *po, int number)
Definition: aper_support.c:251
per_get_few_bits
#define per_get_few_bits(data, bits)
Definition: per_support.h:39
asn_per_constraints_s::value
asn_per_constraint_t value
Definition: per_support.h:31
asn_CHOICE_specifics_s::struct_size
unsigned struct_size
Definition: constr_CHOICE.h:18
asn_per_constraint_s
Definition: per_support.h:18
asn_CHOICE_specifics_s
Definition: constr_CHOICE.h:14
asn_CHOICE_specifics_s::tag2el_count
unsigned tag2el_count
Definition: constr_CHOICE.h:27
asn_bit_data_s
Definition: asn_bit_data.h:17
ATF_POINTER
@ ATF_POINTER
Definition: constr_TYPE.h:268
ASN__ENCODE_FAILED
#define ASN__ENCODE_FAILED
Definition: asn_codecs.h:59
_set_present_idx
void _set_present_idx(void *sptr, unsigned offset, unsigned size, unsigned present)
Definition: constr_CHOICE.c:236
asn_TYPE_descriptor_s
Definition: constr_TYPE.h:224
asn_TYPE_descriptor_s::specifics
const void * specifics
Definition: constr_TYPE.h:259
asn_CHOICE_specifics_s::pres_offset
unsigned pres_offset
Definition: constr_CHOICE.h:20
asn_CHOICE_specifics_s::ext_start
signed ext_start
Definition: constr_CHOICE.h:36
RC_OK
@ RC_OK
Definition: asn_codecs.h:82
aper_get_nsnnwn
ssize_t aper_get_nsnnwn(asn_per_data_t *pd)
Definition: aper_support.c:72
aper_open_type_put
int aper_open_type_put(const asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints, const void *sptr, asn_per_outp_t *po)
Definition: aper_opentype.c:89
ASN__STACK_OVERFLOW_CHECK
static int CC_NOTUSED ASN__STACK_OVERFLOW_CHECK(const asn_codec_ctx_t *ctx)
Definition: asn_internal.h:165
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_internal.h
asn_CHOICE_specifics_s::to_canonical_order
const unsigned * to_canonical_order
Definition: constr_CHOICE.h:30
asn_CHOICE_specifics_s::from_canonical_order
const unsigned * from_canonical_order
Definition: constr_CHOICE.h:31
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
constr_CHOICE.h
asn_dec_rval_s
Definition: asn_codecs.h:86
ASN__DECODE_FAILED
#define ASN__DECODE_FAILED
Definition: asn_codecs.h:90
aper_opentype.h
CHOICE_encode_aper
asn_enc_rval_t CHOICE_encode_aper(const asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints, const void *sptr, asn_per_outp_t *po)
Definition: constr_CHOICE_aper.c:102
_fetch_present_idx
unsigned _fetch_present_idx(const void *struct_ptr, unsigned off, unsigned size)
Definition: constr_CHOICE.c:215
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
per_put_few_bits
#define per_put_few_bits(out, bits, obits)
Definition: per_support.h:46
asn_CHOICE_specifics_s::pres_size
unsigned pres_size
Definition: constr_CHOICE.h:21


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