Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / ASN1c / NULL.c
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 #                                                                            *
5 # Licensed under the Apache License, Version 2.0 (the "License");            *
6 # you may not use this file except in compliance with the License.           *
7 # You may obtain a copy of the License at                                    *
8 #                                                                            *
9 #      http://www.apache.org/licenses/LICENSE-2.0                            *
10 #                                                                            *
11 # Unless required by applicable law or agreed to in writing, software        *
12 # distributed under the License is distributed on an "AS IS" BASIS,          *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
14 # See the License for the specific language governing permissions and        *
15 # limitations under the License.                                             *
16 #                                                                            *
17 ******************************************************************************/
18
19 /*-
20  * Copyright (c) 2003, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
21  * Redistribution and modifications are permitted subject to BSD license.
22  */
23 #include <asn_internal.h>
24 #include <asn_codecs_prim.h>
25 #include <NULL.h>
26 #include <BOOLEAN.h>    /* Implemented in terms of BOOLEAN type */
27
28 /*
29  * NULL basic type description.
30  */
31 static const ber_tlv_tag_t asn_DEF_NULL_tags[] = {
32         (ASN_TAG_CLASS_UNIVERSAL | (5 << 2))
33 };
34 asn_TYPE_operation_t asn_OP_NULL = {
35         BOOLEAN_free,
36         NULL_print,
37         NULL_compare,
38         BOOLEAN_decode_ber,     /* Implemented in terms of BOOLEAN */
39         NULL_encode_der,        /* Special handling of DER encoding */
40         NULL_decode_xer,
41         NULL_encode_xer,
42 #ifdef  ASN_DISABLE_OER_SUPPORT
43         0,
44         0,
45 #else
46         NULL_decode_oer,
47         NULL_encode_oer,
48 #endif  /* ASN_DISABLE_OER_SUPPORT */
49 #ifdef  ASN_DISABLE_PER_SUPPORT
50         0,
51         0,
52         0,
53         0,
54 #else
55         NULL_decode_uper,       /* Unaligned PER decoder */
56         NULL_encode_uper,       /* Unaligned PER encoder */
57         NULL_decode_aper,       /* Aligned PER decoder */
58         NULL_encode_aper,       /* Aligned PER encoder */
59 #endif  /* ASN_DISABLE_PER_SUPPORT */
60         NULL_random_fill,
61         0       /* Use generic outmost tag fetcher */
62 };
63 asn_TYPE_descriptor_t asn_DEF_NULL = {
64         "NULL",
65         "NULL",
66         &asn_OP_NULL,
67         asn_DEF_NULL_tags,
68         sizeof(asn_DEF_NULL_tags) / sizeof(asn_DEF_NULL_tags[0]),
69         asn_DEF_NULL_tags,      /* Same as above */
70         sizeof(asn_DEF_NULL_tags) / sizeof(asn_DEF_NULL_tags[0]),
71         { 0, 0, asn_generic_no_constraint },
72         0, 0,   /* No members */
73         0       /* No specifics */
74 };
75
76 asn_enc_rval_t
77 NULL_encode_der(const asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode,
78                 ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
79         asn_enc_rval_t erval = {0,0,0};
80
81         erval.encoded = der_write_tags(td, 0, tag_mode, 0, tag, cb, app_key);
82         if(erval.encoded == -1) {
83                 erval.failed_type = td;
84                 erval.structure_ptr = ptr;
85         }
86
87         ASN__ENCODED_OK(erval);
88 }
89
90 asn_enc_rval_t
91 NULL_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
92                 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
93                 void *app_key) {
94         asn_enc_rval_t er = {0,0,0};
95
96         (void)td;
97         (void)sptr;
98         (void)ilevel;
99         (void)flags;
100         (void)cb;
101         (void)app_key;
102
103         /* XMLNullValue is empty */
104         er.encoded = 0;
105         ASN__ENCODED_OK(er);
106 }
107
108
109 static enum xer_pbd_rval
110 NULL__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
111                       const void *chunk_buf, size_t chunk_size) {
112     (void)td;
113         (void)sptr;
114         (void)chunk_buf;    /* Going to be empty according to the rules below. */
115
116         /*
117          * There must be no content in self-terminating <NULL/> tag.
118          */
119         if(chunk_size)
120                 return XPBD_BROKEN_ENCODING;
121         else
122                 return XPBD_BODY_CONSUMED;
123 }
124
125 asn_dec_rval_t
126 NULL_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
127                 const asn_TYPE_descriptor_t *td, void **sptr,
128                 const char *opt_mname, const void *buf_ptr, size_t size) {
129     return xer_decode_primitive(opt_codec_ctx, td,
130                 sptr, sizeof(NULL_t), opt_mname, buf_ptr, size,
131                 NULL__xer_body_decode);
132 }
133
134 int
135 NULL_compare(const asn_TYPE_descriptor_t *td, const void *a, const void *b) {
136     (void)td;
137     (void)a;
138     (void)b;
139     return 0;
140 }
141
142 int
143 NULL_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
144            asn_app_consume_bytes_f *cb, void *app_key) {
145     (void)td;   /* Unused argument */
146         (void)ilevel;   /* Unused argument */
147
148         if(sptr) {
149                 return (cb("<present>", 9, app_key) < 0) ? -1 : 0;
150         } else {
151                 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
152         }
153 }
154
155 #ifndef ASN_DISABLE_OER_SUPPORT
156
157 asn_dec_rval_t
158 NULL_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
159                 const asn_TYPE_descriptor_t *td,
160                 const asn_oer_constraints_t *constraints, void **sptr,
161                 const void *ptr, size_t size) {
162     asn_dec_rval_t rv = {RC_OK, 0};
163     (void)opt_codec_ctx;
164     (void)td;
165     (void)constraints;
166     (void)ptr;
167     (void)size;
168
169     if(!*sptr) {
170         *sptr = MALLOC(sizeof(NULL_t));
171         if(*sptr) {
172             *(NULL_t *)*sptr = 0;
173         } else {
174             ASN__DECODE_FAILED;
175         }
176     }
177
178     return rv;
179 }
180
181 asn_enc_rval_t
182 NULL_encode_oer(const asn_TYPE_descriptor_t *td,
183                 const asn_oer_constraints_t *constraints, const void *sptr,
184                 asn_app_consume_bytes_f *cb, void *app_key) {
185     asn_enc_rval_t er = {0,0,0};
186
187     (void)td;
188     (void)sptr;
189     (void)constraints;
190     (void)cb;
191     (void)app_key;
192
193     er.encoded = 0; /* Encoding in 0 bytes. */
194
195     ASN__ENCODED_OK(er);
196 }
197
198 #endif /* ASN_DISABLE_OER_SUPPORT */
199
200 #ifndef ASN_DISABLE_PER_SUPPORT
201
202 asn_dec_rval_t
203 NULL_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
204                  const asn_TYPE_descriptor_t *td,
205                  const asn_per_constraints_t *constraints, void **sptr,
206                  asn_per_data_t *pd) {
207     asn_dec_rval_t rv;
208
209         (void)opt_codec_ctx;
210         (void)td;
211         (void)constraints;
212         (void)pd;
213
214         if(!*sptr) {
215                 *sptr = MALLOC(sizeof(NULL_t));
216                 if(*sptr) {
217                         *(NULL_t *)*sptr = 0;
218                 } else {
219                         ASN__DECODE_FAILED;
220                 }
221         }
222
223         /*
224          * NULL type does not have content octets.
225          */
226
227         rv.code = RC_OK;
228         rv.consumed = 0;
229         return rv;
230 }
231
232 asn_enc_rval_t
233 NULL_encode_uper(const asn_TYPE_descriptor_t *td,
234                  const asn_per_constraints_t *constraints, const void *sptr,
235                  asn_per_outp_t *po) {
236         asn_enc_rval_t er = {0,0,0};
237
238         (void)td;
239         (void)constraints;
240         (void)sptr;
241         (void)po;
242
243         er.encoded = 0;
244         ASN__ENCODED_OK(er);
245 }
246
247 asn_dec_rval_t
248 NULL_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
249                  const asn_TYPE_descriptor_t *td,
250                  const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
251         asn_dec_rval_t rv = {RC_OK, 0};
252
253         (void)opt_codec_ctx;
254         (void)td;
255         (void)constraints;
256         (void)pd;
257
258         if(!*sptr) {
259                 *sptr = MALLOC(sizeof(NULL_t));
260                 if(*sptr) {
261                         *(NULL_t *)*sptr = 0;
262                 } else {
263                         ASN__DECODE_FAILED;
264                 }
265         }
266
267         /*
268          * NULL type does not have content octets.
269          */
270
271         rv.code = RC_OK;
272         rv.consumed = 0;
273         return rv;
274 }
275
276
277 asn_enc_rval_t
278 NULL_encode_aper(const asn_TYPE_descriptor_t *td,
279                  const asn_per_constraints_t *constraints,
280                  const void *sptr, asn_per_outp_t *po) {
281         asn_enc_rval_t er = {0,0,0};
282
283         (void)td;
284         (void)constraints;
285         (void)sptr;
286         (void)po;
287
288         er.encoded = 0;
289         ASN__ENCODED_OK(er);
290 }
291
292 #endif  /* ASN_DISABLE_PER_SUPPORT */
293
294 asn_random_fill_result_t
295 NULL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
296                     const asn_encoding_constraints_t *constr,
297                     size_t max_length) {
298     asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
299     asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
300     asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
301     NULL_t *st = *sptr;
302
303     (void)td;
304     (void)constr;
305
306     if(max_length == 0) return result_skipped;
307
308     if(st == NULL) {
309         st = (NULL_t *)(*sptr = CALLOC(1, sizeof(*st)));
310         if(st == NULL) {
311             return result_failed;
312         }
313     }
314
315     return result_ok;
316 }
317