ANY_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 #include <errno.h>
9 
10 #undef RETURN
11 #define RETURN(_code) \
12  do { \
13  asn_dec_rval_t tmprval; \
14  tmprval.code = _code; \
15  tmprval.consumed = consumed_myself; \
16  return tmprval; \
17  } while(0)
18 
19 int
21  uint8_t *buffer = NULL;
22  ssize_t erval;
23 
24  if(!st || !td) {
25  errno = EINVAL;
26  return -1;
27  }
28 
29  if(!sptr) {
30  if(st->buf) FREEMEM(st->buf);
31  st->size = 0;
32  return 0;
33  }
34 
35  erval = aper_encode_to_new_buffer(td, td->encoding_constraints.per_constraints, sptr, (void**)&buffer);
36 
37  if(erval == -1) {
38  if(buffer) FREEMEM(buffer);
39  return -1;
40  }
41  assert((size_t)erval > 0);
42 
43  if(st->buf) FREEMEM(st->buf);
44  st->buf = buffer;
45  st->size = erval;
46 
47  return 0;
48 }
49 
50 ANY_t *
52  ANY_t tmp;
53  ANY_t *st;
54 
55  if(!td || !sptr) {
56  errno = EINVAL;
57  return 0;
58  }
59 
60  memset(&tmp, 0, sizeof(tmp));
61 
62  if(ANY_fromType_aper(&tmp, td, sptr)) return 0;
63 
64  st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
65  if(st) {
66  *st = tmp;
67  return st;
68  } else {
69  FREEMEM(tmp.buf);
70  return 0;
71  }
72 }
73 
74 int
75 ANY_to_type_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
76  asn_dec_rval_t rval;
77  void *newst = 0;
78 
79  if(!st || !td || !struct_ptr) {
80  errno = EINVAL;
81  return -1;
82  }
83 
84  if(st->buf == 0) {
85  /* Nothing to convert, make it empty. */
86  *struct_ptr = (void *)0;
87  return 0;
88  }
89 
90  rval = aper_decode(0, td, (void **)&newst, st->buf, st->size, 0, 0);
91  if(rval.code == RC_OK) {
92  *struct_ptr = newst;
93  return 0;
94  } else {
95  /* Remove possibly partially decoded data. */
96  ASN_STRUCT_FREE(*td, newst);
97  return -1;
98  }
99 }
100 
101 int
103  return ANY_to_type_aper(st, td, struct_ptr) == 0 && *struct_ptr != 0 ? 0 : -1;
104 }
105 
107 ANY_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
108  const asn_TYPE_descriptor_t *td,
109  const asn_per_constraints_t *constraints, void **sptr,
110  asn_per_data_t *pd) {
111  const asn_OCTET_STRING_specifics_t *specs =
114  size_t consumed_myself = 0;
115  int repeat;
116  ANY_t *st = (ANY_t *)*sptr;
117 
118  (void)opt_codec_ctx;
119  (void)constraints;
120 
121  /*
122  * Allocate the structure.
123  */
124  if(!st) {
125  st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
126  if(!st) RETURN(RC_FAIL);
127  }
128 
129  ASN_DEBUG("APER Decoding ANY type");
130 
131  st->size = 0;
132  do {
133  ssize_t raw_len;
134  ssize_t len_bytes;
135  ssize_t len_bits;
136  void *p;
137  int ret;
138 
139  /* Get the PER length */
140  raw_len = aper_get_length(pd, -1, -1, 0, &repeat);
141  if(raw_len < 0) RETURN(RC_WMORE);
142  if(raw_len == 0 && st->buf) break;
143 
144  ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
145  repeat ? "repeat" : "once", td->name);
146  len_bytes = raw_len;
147  len_bits = len_bytes * 8;
148 
149  p = REALLOC(st->buf, st->size + len_bytes + 1);
150  if(!p) RETURN(RC_FAIL);
151  st->buf = (uint8_t *)p;
152 
153  ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
154  if(ret < 0) RETURN(RC_WMORE);
155  consumed_myself += len_bits;
156  st->size += len_bytes;
157  } while(repeat);
158  st->buf[st->size] = 0; /* nul-terminate */
159 
160  RETURN(RC_OK);
161 }
162 
165  const asn_per_constraints_t *constraints, const void *sptr,
166  asn_per_outp_t *po) {
167  const ANY_t *st = (const ANY_t *)sptr;
168  asn_enc_rval_t er = {0, 0, 0};
169  const uint8_t *buf;
170  size_t size;
171  int ret;
172 
173  (void)constraints;
174 
175  if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
176 
177  buf = st->buf;
178  size = st->size;
179  do {
180  int need_eom = 0;
181  ssize_t may_save = aper_put_length(po, -1, -1, size, &need_eom);
182  if(may_save < 0) ASN__ENCODE_FAILED;
183 
184  ret = per_put_many_bits(po, buf, may_save * 8);
185  if(ret) ASN__ENCODE_FAILED;
186 
187  buf += may_save;
188  size -= may_save;
189  assert(!(may_save & 0x07) || !size);
190  if(need_eom && aper_put_length(po, -1, -1, 0, NULL))
191  ASN__ENCODE_FAILED; /* End of Message length */
192  } while(size);
193 
194  ASN__ENCODED_OK(er);
195 }
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
RETURN
#define RETURN(_code)
Definition: ANY_aper.c:11
ASN__ENCODED_OK
#define ASN__ENCODED_OK(rval)
Definition: asn_codecs.h:67
ANY::buf
uint8_t * buf
Definition: ANY.h:15
ANY.h
asn_TYPE_descriptor_s::name
const char * name
Definition: constr_TYPE.h:225
asn_enc_rval_s
Definition: asn_codecs.h:41
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
RC_WMORE
@ RC_WMORE
Definition: asn_codecs.h:83
CALLOC
#define CALLOC(nmemb, size)
Definition: asn_internal.h:37
asn_OCTET_STRING_specifics_s::struct_size
unsigned struct_size
Definition: OCTET_STRING.h:131
ANY_new_fromType_aper
ANY_t * ANY_new_fromType_aper(asn_TYPE_descriptor_t *td, void *sptr)
Definition: ANY_aper.c:51
asn_OCTET_STRING_specifics_s
Definition: OCTET_STRING.h:125
REALLOC
#define REALLOC(oldptr, size)
Definition: asn_internal.h:39
FREEMEM
#define FREEMEM(ptr)
Definition: asn_internal.h:40
asn_bit_data_s
Definition: asn_bit_data.h:17
ANY_to_type_aper
int ANY_to_type_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr)
Definition: ANY_aper.c:75
ASN__ENCODE_FAILED
#define ASN__ENCODE_FAILED
Definition: asn_codecs.h:59
asn_TYPE_descriptor_s
Definition: constr_TYPE.h:224
ANY_encode_aper
asn_enc_rval_t ANY_encode_aper(const asn_TYPE_descriptor_t *td, const asn_per_constraints_t *constraints, const void *sptr, asn_per_outp_t *po)
Definition: ANY_aper.c:164
ANY_fromType_aper
int ANY_fromType_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr)
Definition: ANY_aper.c:20
asn_TYPE_descriptor_s::specifics
const void * specifics
Definition: constr_TYPE.h:259
ANY_decode_aper
asn_dec_rval_t ANY_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: ANY_aper.c:107
RC_OK
@ RC_OK
Definition: asn_codecs.h:82
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_PRI_SIZE
#define ASN_PRI_SIZE
Definition: asn_system.h:172
ANY::size
int size
Definition: ANY.h:16
per_put_many_bits
#define per_put_many_bits(out, src, nbits)
Definition: per_support.h:47
ANY_to_type_aper_checked
int ANY_to_type_aper_checked(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr)
Definition: ANY_aper.c:102
per_get_many_bits
#define per_get_many_bits(data, dst, align, bits)
Definition: per_support.h:41
asn_encoding_constraints_s::per_constraints
const struct asn_per_constraints_s * per_constraints
Definition: constr_TYPE.h:213
asn_internal.h
asn_codec_ctx_s
Definition: asn_codecs.h:23
asn_dec_rval_s
Definition: asn_codecs.h:86
asn_per_constraints_s
Definition: per_support.h:30
asn_SPC_ANY_specs
asn_OCTET_STRING_specifics_t asn_SPC_ANY_specs
Definition: ANY.c:8
asn_TYPE_descriptor_s::encoding_constraints
asn_encoding_constraints_t encoding_constraints
Definition: constr_TYPE.h:247
ANY
Definition: ANY.h:14
aper_encode_to_new_buffer
ssize_t aper_encode_to_new_buffer(const struct asn_TYPE_descriptor_s *td, const asn_per_constraints_t *constraints, const void *sptr, void **buffer_r)
aper_decode
asn_dec_rval_t aper_decode(const struct asn_codec_ctx_s *opt_codec_ctx, const struct asn_TYPE_descriptor_s *type_descriptor, void **struct_ptr, const void *buffer, size_t size, int skip_bits, int unused_bits)
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_cpm_ts_coding
Author(s): Jean-Pierre Busch , Guido Küppers , Lennart Reiher
autogenerated on Sun May 18 2025 02:22:37