Change version after creation of r2 branch
[ric-plt/resource-status-manager.git] / RSM / asn1codec / e2ap_engine / BOOLEAN.c
1
2 /*-
3  * Copyright (c) 2003, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <asn_codecs_prim.h>
8 #include <BOOLEAN.h>
9
10 /*
11  * BOOLEAN basic type description.
12  */
13 static const ber_tlv_tag_t asn_DEF_BOOLEAN_tags[] = {
14         (ASN_TAG_CLASS_UNIVERSAL | (1 << 2))
15 };
16 asn_TYPE_operation_t asn_OP_BOOLEAN = {
17         BOOLEAN_free,
18         BOOLEAN_print,
19         BOOLEAN_compare,
20         BOOLEAN_decode_ber,
21         BOOLEAN_encode_der,
22         BOOLEAN_decode_xer,
23         BOOLEAN_encode_xer,
24 #ifdef  ASN_DISABLE_OER_SUPPORT
25         0,
26         0,
27 #else
28         BOOLEAN_decode_oer,
29         BOOLEAN_encode_oer,
30 #endif  /* ASN_DISABLE_OER_SUPPORT */
31 #ifdef  ASN_DISABLE_PER_SUPPORT
32         0,
33         0,
34         0,
35         0,
36 #else
37         BOOLEAN_decode_uper,    /* Unaligned PER decoder */
38         BOOLEAN_encode_uper,    /* Unaligned PER encoder */
39         BOOLEAN_decode_aper,    /* Aligned PER decoder */
40         BOOLEAN_encode_aper,    /* Aligned PER encoder */
41 #endif  /* ASN_DISABLE_PER_SUPPORT */
42         BOOLEAN_random_fill,
43         0       /* Use generic outmost tag fetcher */
44 };
45 asn_TYPE_descriptor_t asn_DEF_BOOLEAN = {
46         "BOOLEAN",
47         "BOOLEAN",
48         &asn_OP_BOOLEAN,
49         asn_DEF_BOOLEAN_tags,
50         sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
51         asn_DEF_BOOLEAN_tags,   /* Same as above */
52         sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
53         { 0, 0, asn_generic_no_constraint },
54         0, 0,   /* No members */
55         0       /* No specifics */
56 };
57
58 /*
59  * Decode BOOLEAN type.
60  */
61 asn_dec_rval_t
62 BOOLEAN_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
63                    const asn_TYPE_descriptor_t *td, void **bool_value,
64                    const void *buf_ptr, size_t size, int tag_mode) {
65     BOOLEAN_t *st = (BOOLEAN_t *)*bool_value;
66         asn_dec_rval_t rval;
67         ber_tlv_len_t length;
68         ber_tlv_len_t lidx;
69
70         if(st == NULL) {
71                 st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st)));
72                 if(st == NULL) {
73                         rval.code = RC_FAIL;
74                         rval.consumed = 0;
75                         return rval;
76                 }
77         }
78
79         ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)",
80                 td->name, tag_mode);
81
82         /*
83          * Check tags.
84          */
85         rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
86                 tag_mode, 0, &length, 0);
87         if(rval.code != RC_OK)
88                 return rval;
89
90         ASN_DEBUG("Boolean length is %d bytes", (int)length);
91
92         buf_ptr = ((const char *)buf_ptr) + rval.consumed;
93         size -= rval.consumed;
94         if(length > (ber_tlv_len_t)size) {
95                 rval.code = RC_WMORE;
96                 rval.consumed = 0;
97                 return rval;
98         }
99
100         /*
101          * Compute boolean value.
102          */
103         for(*st = 0, lidx = 0;
104                 (lidx < length) && *st == 0; lidx++) {
105                 /*
106                  * Very simple approach: read bytes until the end or
107                  * value is already TRUE.
108                  * BOOLEAN is not supposed to contain meaningful data anyway.
109                  */
110                 *st |= ((const uint8_t *)buf_ptr)[lidx];
111         }
112
113         rval.code = RC_OK;
114         rval.consumed += length;
115
116         ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d",
117                 (long)rval.consumed, (long)length,
118                 td->name, *st);
119         
120         return rval;
121 }
122
123 asn_enc_rval_t
124 BOOLEAN_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
125                    int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
126                    void *app_key) {
127         asn_enc_rval_t erval = {0,0,0};
128         const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
129
130         erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key);
131         if(erval.encoded == -1) {
132                 erval.failed_type = td;
133                 erval.structure_ptr = sptr;
134                 return erval;
135         }
136
137         if(cb) {
138                 uint8_t bool_value;
139
140                 bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */
141
142                 if(cb(&bool_value, 1, app_key) < 0) {
143                         erval.encoded = -1;
144                         erval.failed_type = td;
145                         erval.structure_ptr = sptr;
146                         return erval;
147                 }
148         }
149
150         erval.encoded += 1;
151
152         ASN__ENCODED_OK(erval);
153 }
154
155
156 /*
157  * Decode the chunk of XML text encoding INTEGER.
158  */
159 static enum xer_pbd_rval
160 BOOLEAN__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
161                          const void *chunk_buf, size_t chunk_size) {
162     BOOLEAN_t *st = (BOOLEAN_t *)sptr;
163         const char *p = (const char *)chunk_buf;
164
165         (void)td;
166
167         if(chunk_size && p[0] == 0x3c /* '<' */) {
168                 switch(xer_check_tag(chunk_buf, chunk_size, "false")) {
169                 case XCT_BOTH:
170                         /* "<false/>" */
171                         *st = 0;
172                         break;
173                 case XCT_UNKNOWN_BO:
174                         if(xer_check_tag(chunk_buf, chunk_size, "true")
175                                         != XCT_BOTH)
176                                 return XPBD_BROKEN_ENCODING;
177                         /* "<true/>" */
178                         *st = 1;        /* Or 0xff as in DER?.. */
179                         break;
180                 default:
181                         return XPBD_BROKEN_ENCODING;
182                 }
183                 return XPBD_BODY_CONSUMED;
184         } else {
185                 return XPBD_BROKEN_ENCODING;
186         }
187 }
188
189
190 asn_dec_rval_t
191 BOOLEAN_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
192                    const asn_TYPE_descriptor_t *td, void **sptr,
193                    const char *opt_mname, const void *buf_ptr, size_t size) {
194     return xer_decode_primitive(opt_codec_ctx, td,
195                 sptr, sizeof(BOOLEAN_t), opt_mname, buf_ptr, size,
196                 BOOLEAN__xer_body_decode);
197 }
198
199 asn_enc_rval_t
200 BOOLEAN_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
201         int ilevel, enum xer_encoder_flags_e flags,
202                 asn_app_consume_bytes_f *cb, void *app_key) {
203         const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
204         asn_enc_rval_t er = {0, 0, 0};
205
206         (void)ilevel;
207         (void)flags;
208
209         if(!st) ASN__ENCODE_FAILED;
210
211         if(*st) {
212                 ASN__CALLBACK("<true/>", 7);
213         } else {
214                 ASN__CALLBACK("<false/>", 8);
215         }
216
217         ASN__ENCODED_OK(er);
218 cb_failed:
219         ASN__ENCODE_FAILED;
220 }
221
222 int
223 BOOLEAN_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
224               asn_app_consume_bytes_f *cb, void *app_key) {
225     const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
226         const char *buf;
227         size_t buflen;
228
229         (void)td;       /* Unused argument */
230         (void)ilevel;   /* Unused argument */
231
232         if(st) {
233                 if(*st) {
234                         buf = "TRUE";
235                         buflen = 4;
236                 } else {
237                         buf = "FALSE";
238                         buflen = 5;
239                 }
240         } else {
241                 buf = "<absent>";
242                 buflen = 8;
243         }
244
245         return (cb(buf, buflen, app_key) < 0) ? -1 : 0;
246 }
247
248 void
249 BOOLEAN_free(const asn_TYPE_descriptor_t *td, void *ptr,
250              enum asn_struct_free_method method) {
251     if(td && ptr) {
252         switch(method) {
253         case ASFM_FREE_EVERYTHING:
254             FREEMEM(ptr);
255             break;
256         case ASFM_FREE_UNDERLYING:
257             break;
258         case ASFM_FREE_UNDERLYING_AND_RESET:
259             memset(ptr, 0, sizeof(BOOLEAN_t));
260             break;
261         }
262     }
263 }
264
265 #ifndef ASN_DISABLE_PER_SUPPORT
266
267 asn_dec_rval_t
268 BOOLEAN_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
269                     const asn_TYPE_descriptor_t *td,
270                     const asn_per_constraints_t *constraints, void **sptr,
271                     asn_per_data_t *pd) {
272     asn_dec_rval_t rv;
273         BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
274
275         (void)opt_codec_ctx;
276     (void)td;
277         (void)constraints;
278
279         if(!st) {
280                 st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
281                 if(!st) ASN__DECODE_FAILED;
282         }
283
284         /*
285          * Extract a single bit
286          */
287         switch(per_get_few_bits(pd, 1)) {
288         case 1: *st = 1; break;
289         case 0: *st = 0; break;
290         case -1: default: ASN__DECODE_STARVED;
291         }
292
293         ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
294
295         rv.code = RC_OK;
296         rv.consumed = 1;
297         return rv;
298 }
299
300
301 asn_enc_rval_t
302 BOOLEAN_encode_uper(const asn_TYPE_descriptor_t *td,
303                     const asn_per_constraints_t *constraints, const void *sptr,
304                     asn_per_outp_t *po) {
305     const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
306         asn_enc_rval_t er = { 0, 0, 0 };
307
308         (void)constraints;
309
310         if(!st) ASN__ENCODE_FAILED;
311
312         if(per_put_few_bits(po, *st ? 1 : 0, 1))
313                 ASN__ENCODE_FAILED;
314
315         ASN__ENCODED_OK(er);
316 }
317
318 asn_dec_rval_t
319 BOOLEAN_decode_aper(const asn_codec_ctx_t *opt_codec_ctx, const asn_TYPE_descriptor_t *td,
320                     const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
321         asn_dec_rval_t rv;
322         BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
323
324         (void)opt_codec_ctx;
325         (void)constraints;
326         (void)td;
327
328         if(!st) {
329                 st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
330                 if(!st) ASN__DECODE_FAILED;
331         }
332
333         /*
334          * Extract a single bit
335          */
336         switch(per_get_few_bits(pd, 1)) {
337         case 1:
338                 *st = 1;
339                 break;
340         case 0:
341                 *st = 0;
342                 break;
343         case -1:
344         default:
345                 ASN__DECODE_STARVED;
346         }
347
348         ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
349
350         rv.code = RC_OK;
351         rv.consumed = 1;
352         return rv;
353 }
354
355 asn_enc_rval_t
356 BOOLEAN_encode_aper(const asn_TYPE_descriptor_t *td,
357                     const asn_per_constraints_t *constraints,
358                     const void *sptr, asn_per_outp_t *po) {
359         const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
360         asn_enc_rval_t er = { 0, 0, 0 };
361
362         (void)constraints;
363
364         if(!st) ASN__ENCODE_FAILED;
365
366         if(per_put_few_bits(po, *st ? 1 : 0, 1))
367                 ASN__ENCODE_FAILED;
368
369         ASN__ENCODED_OK(er);
370 }
371
372 #endif /* ASN_DISABLE_PER_SUPPORT */
373
374 #ifndef  ASN_DISABLE_OER_SUPPORT
375
376 /*
377  * Encode as Canonical OER.
378  */
379 asn_enc_rval_t
380 BOOLEAN_encode_oer(const asn_TYPE_descriptor_t *td,
381                    const asn_oer_constraints_t *constraints, const void *sptr,
382                    asn_app_consume_bytes_f *cb, void *app_key) {
383     asn_enc_rval_t er = { 1, 0, 0 };
384     const BOOLEAN_t *st = sptr;
385     uint8_t bool_value = *st ? 0xff : 0; /* 0xff mandated by OER */
386
387     (void)td;
388     (void)constraints;  /* Constraints are unused in OER */
389
390     if(cb(&bool_value, 1, app_key) < 0) {
391         ASN__ENCODE_FAILED;
392     } else {
393         ASN__ENCODED_OK(er);
394     }
395 }
396
397 asn_dec_rval_t
398 BOOLEAN_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
399                    const asn_TYPE_descriptor_t *td,
400                    const asn_oer_constraints_t *constraints, void **sptr,
401                    const void *ptr, size_t size) {
402     asn_dec_rval_t ok = {RC_OK, 1};
403     BOOLEAN_t *st;
404
405     (void)opt_codec_ctx;
406     (void)td;
407     (void)constraints; /* Constraints are unused in OER */
408
409     if(size < 1) {
410         ASN__DECODE_STARVED;
411     }
412
413     if(!(st = *sptr)) {
414         st = (BOOLEAN_t *)(*sptr = CALLOC(1, sizeof(*st)));
415         if(!st) ASN__DECODE_FAILED;
416     }
417
418     *st = *(const uint8_t *)ptr;
419
420     return ok;
421 }
422
423
424
425 #endif
426
427 int
428 BOOLEAN_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
429                 const void *bptr) {
430     const BOOLEAN_t *a = aptr;
431     const BOOLEAN_t *b = bptr;
432
433     (void)td;
434
435     if(a && b) {
436         if(!*a == !*b) {    /* TRUE can be encoded by any non-zero byte. */
437             return 0;
438         } else if(!*a) {
439             return -1;
440         } else {
441             return 1;
442         }
443     } else if(!a) {
444         return -1;
445     } else {
446         return 1;
447     }
448 }
449
450 asn_random_fill_result_t
451 BOOLEAN_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
452                     const asn_encoding_constraints_t *constraints,
453                     size_t max_length) {
454     asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
455     asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
456     asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
457     BOOLEAN_t *st = *sptr;
458
459     if(max_length == 0) return result_skipped;
460
461     if(st == NULL) {
462         st = (BOOLEAN_t *)(*sptr = CALLOC(1, sizeof(*st)));
463         if(st == NULL) {
464             return result_failed;
465         }
466     }
467
468     if(!constraints || !constraints->per_constraints)
469         constraints = &td->encoding_constraints;
470     if(constraints->per_constraints) {
471         const asn_per_constraint_t *pc = &constraints->per_constraints->value;
472         if(pc->flags & APC_CONSTRAINED) {
473             *st = asn_random_between(pc->lower_bound, pc->upper_bound);
474             return result_ok;
475         }
476     }
477
478     /* Simulate booleans that are sloppily set and biased. */
479     switch(asn_random_between(0, 7)) {
480     case 0:
481     case 1:
482     case 2:
483         *st = 0; break;
484     case 3: *st = -1; break;
485     case 4: *st = 1; break;
486     case 5: *st = INT_MIN; break;
487     case 6: *st = INT_MAX; break;
488     default:
489         *st = asn_random_between(INT_MIN, INT_MAX);
490         break;
491     }
492     return result_ok;
493 }