SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / BOOLEAN_ber.c
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  */
6 #include <asn_internal.h>
7 #include <BOOLEAN.h>
8
9 /*
10  * Decode BOOLEAN type.
11  */
12 asn_dec_rval_t
13 BOOLEAN_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
14                    const asn_TYPE_descriptor_t *td, void **bool_value,
15                    const void *buf_ptr, size_t size, int tag_mode) {
16     BOOLEAN_t *st = (BOOLEAN_t *)*bool_value;
17     asn_dec_rval_t rval;
18     ber_tlv_len_t length;
19     ber_tlv_len_t lidx;
20
21     if(st == NULL) {
22         st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st)));
23         if(st == NULL) {
24             rval.code = RC_FAIL;
25             rval.consumed = 0;
26             return rval;
27         }
28     }
29
30     ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)",
31             td->name, tag_mode);
32
33     /*
34      * Check tags.
35      */
36     rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
37         tag_mode, 0, &length, 0);
38     if(rval.code != RC_OK)
39         return rval;
40
41     ASN_DEBUG("Boolean length is %d bytes", (int)length);
42
43     buf_ptr = ((const char *)buf_ptr) + rval.consumed;
44     size -= rval.consumed;
45     if(length > (ber_tlv_len_t)size) {
46         rval.code = RC_WMORE;
47         rval.consumed = 0;
48         return rval;
49     }
50
51     /*
52      * Compute boolean value.
53      */
54     for(*st = 0, lidx = 0;
55         (lidx < length) && *st == 0; lidx++) {
56         /*
57          * Very simple approach: read bytes until the end or
58          * value is already TRUE.
59          * BOOLEAN is not supposed to contain meaningful data anyway.
60          */
61         *st |= ((const uint8_t *)buf_ptr)[lidx];
62     }
63
64     rval.code = RC_OK;
65     rval.consumed += length;
66
67     ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d",
68         (long)rval.consumed, (long)length,
69         td->name, *st);
70
71     return rval;
72 }
73
74 asn_enc_rval_t
75 BOOLEAN_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
76                    int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
77                    void *app_key) {
78     asn_enc_rval_t erval = {0,0,0};
79     const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
80
81     erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key);
82     if(erval.encoded == -1) {
83         erval.failed_type = td;
84         erval.structure_ptr = sptr;
85         return erval;
86     }
87
88     if(cb) {
89         uint8_t bool_value;
90
91         bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */
92
93         if(cb(&bool_value, 1, app_key) < 0) {
94             erval.encoded = -1;
95             erval.failed_type = td;
96             erval.structure_ptr = sptr;
97             return erval;
98         }
99     }
100
101     erval.encoded += 1;
102
103     ASN__ENCODED_OK(erval);
104 }