NativeEnumerated_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_TYPE_descriptor_t *td,
12  const asn_per_constraints_t *constraints,
13  void **sptr, asn_per_data_t *pd) {
14  const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics;
15  asn_dec_rval_t rval = { RC_OK, 0 };
16  long *native = (long *)*sptr;
17  const asn_per_constraint_t *ct;
18  long value;
19 
20  (void)opt_codec_ctx;
21 
22  if(constraints) ct = &constraints->value;
23  else if(td->encoding_constraints.per_constraints)
24  ct = &td->encoding_constraints.per_constraints->value;
25  else ASN__DECODE_FAILED; /* Mandatory! */
26  if(!specs) ASN__DECODE_FAILED;
27 
28  if(!native) {
29  native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
30  if(!native) ASN__DECODE_FAILED;
31  }
32 
33  ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
34 
35  if(ct->flags & APC_EXTENSIBLE) {
36  int inext = per_get_few_bits(pd, 1);
37  if(inext < 0) ASN__DECODE_STARVED;
38  if(inext) ct = 0;
39  }
40 
41  /* Deal with APER padding */
42  if(ct && ct->upper_bound >= 255) {
43  int padding = 0;
44  padding = (8 - (pd->moved % 8)) % 8;
45  ASN_DEBUG("For NativeEnumerated %s,offset = %zu Padding bits = %d",
46  td->name, pd->moved, padding);
47  ASN_DEBUG("For NativeEnumerated %s, upper bound = %llu",
48  td->name, (unsigned long long)ct->upper_bound);
49  if(padding > 0)
50  per_get_few_bits(pd, padding);
51  }
52 
53  if(ct && ct->range_bits >= 0) {
54  value = per_get_few_bits(pd, ct->range_bits);
55  if(value < 0) ASN__DECODE_STARVED;
56  if(value >= (specs->extension
57  ? specs->extension - 1 : specs->map_count))
59  } else {
60  if(!specs->extension)
62  /*
63  * X.691, #10.6: normally small non-negative whole number;
64  */
65 
66  /* XXX handle indefinite index length > 64k */
67  value = aper_get_nsnnwn(pd);
68  if(value < 0) ASN__DECODE_STARVED;
69  value += specs->extension - 1;
70  //if(value >= specs->map_count)
71  // ASN__DECODE_FAILED;
72  if(value >= specs->map_count) {
73  ASN_DEBUG("Decoded unknown index value %s = %ld", td->name, value);
74  /* unknown index. Workaround => set the first enumeration value */
75  *native = specs->value2enum[0].nat_value;
76  return rval;
77  }
78  }
79 
80  *native = specs->value2enum[value].nat_value;
81  ASN_DEBUG("Decoded %s = %ld", td->name, *native);
82 
83  return rval;
84 }
85 
88  const asn_per_constraints_t *constraints,
89  const void *sptr, asn_per_outp_t *po) {
90  const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics;
91  asn_enc_rval_t er = {0,0,0};
92  long native, value;
93  const asn_per_constraint_t *ct;
94  int inext = 0, range_bits = 1;
97 
98  if(!sptr) ASN__ENCODE_FAILED;
99  if(!specs) ASN__ENCODE_FAILED;
100 
101  if(constraints) ct = &constraints->value;
102  else if(td->encoding_constraints.per_constraints)
103  ct = &td->encoding_constraints.per_constraints->value;
104  else ASN__ENCODE_FAILED; /* Mandatory! */
105 
106  ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
107 
108  er.encoded = 0;
109 
110  native = *(const long *)sptr;
111  if(native < 0) ASN__ENCODE_FAILED;
112 
113  key.nat_value = native;
114  kf = bsearch(&key, specs->value2enum, specs->map_count,
116  if(!kf) {
117  ASN_DEBUG("No element corresponds to %ld", native);
119  }
120  value = kf - specs->value2enum;
121 
122  if(ct->range_bits >= 0) {
123  int cmpWith = specs->extension
124  ? specs->extension - 1 : specs->map_count;
125  if(value >= cmpWith)
126  inext = 1;
127  }
128  if(ct->flags & APC_EXTENSIBLE) {
129  if(per_put_few_bits(po, inext, 1))
131  if(inext) range_bits = 0;
132  } else if(inext) {
134  }
135 
136  if(range_bits && ct && ct->range_bits >= 0) {
137  if(per_put_few_bits(po, value, ct->range_bits))
139  ASN__ENCODED_OK(er);
140  }
141 
142  if(!specs->extension)
144 
145  /*
146  * X.691, #10.6: normally small non-negative whole number;
147  */
148  ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
149  value, specs->extension, inext,
150  value - (inext ? (specs->extension - 1) : 0));
151  if(aper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0)))
153 
154  ASN__ENCODED_OK(er);
155 }
asn_bit_outp_s
Definition: asn_bit_data.h:56
NativeEnumerated_encode_aper
asn_enc_rval_t NativeEnumerated_encode_aper(const asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints, const void *sptr, asn_per_outp_t *po)
Definition: NativeEnumerated_aper.c:87
ASN__ENCODED_OK
#define ASN__ENCODED_OK(rval)
Definition: asn_codecs.h:67
asn_enc_rval_s
Definition: asn_codecs.h:41
NativeEnumerated.h
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_constraint_s
Definition: per_support.h:18
NativeEnumerated__compar_value2enum
int NativeEnumerated__compar_value2enum(const void *ap, const void *bp)
Definition: NativeEnumerated.c:105
asn_bit_data_s
Definition: asn_bit_data.h:17
ASN__ENCODE_FAILED
#define ASN__ENCODE_FAILED
Definition: asn_codecs.h:59
asn_TYPE_descriptor_s
Definition: constr_TYPE.h:224
asn_TYPE_descriptor_s::specifics
const void * specifics
Definition: constr_TYPE.h:259
NativeEnumerated_decode_aper
asn_dec_rval_t NativeEnumerated_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: NativeEnumerated_aper.c:10
asn_INTEGER_specifics_s
Definition: INTEGER.h:28
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
ASN__DECODE_STARVED
#define ASN__DECODE_STARVED
Definition: asn_codecs.h:97
key
static void ssize_t void * key
Definition: asn_internal.h:96
asn_internal.h
asn_codec_ctx_s
Definition: asn_codecs.h:23
asn_dec_rval_s
Definition: asn_codecs.h:86
ASN__DECODE_FAILED
#define ASN__DECODE_FAILED
Definition: asn_codecs.h:90
asn_per_constraint_s::range_bits
int range_bits
Definition: per_support.h:25
asn_per_constraints_s
Definition: per_support.h:30
asn_per_constraint_s::upper_bound
intmax_t upper_bound
Definition: per_support.h:28
asn_per_constraint_s::flags
enum asn_per_constraint_s::asn_per_constraint_flags flags
per_put_few_bits
#define per_put_few_bits(out, bits, obits)
Definition: per_support.h:46
asn_INTEGER_enum_map_s
Definition: INTEGER.h:21


etsi_its_denm_coding
Author(s): Jean-Pierre Busch , Guido Küppers , Lennart Reiher
autogenerated on Sun May 18 2025 02:23:48