Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / ASN1c / BIT_STRING_oer.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 #ifndef ASN_DISABLE_OER_SUPPORT
7
8 #include <asn_internal.h>
9 #include <BIT_STRING.h>
10 #include <errno.h>
11
12 asn_dec_rval_t
13 BIT_STRING_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
14                       const asn_TYPE_descriptor_t *td,
15                       const asn_oer_constraints_t *constraints, void **sptr,
16                       const void *ptr, size_t size) {
17     BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
18     const asn_oer_constraints_t *cts =
19         constraints ? constraints : td->encoding_constraints.oer_constraints;
20     ssize_t ct_size = cts ? cts->size : -1;
21     asn_dec_rval_t rval = {RC_OK, 0};
22     size_t expected_length = 0;
23
24     (void)opt_codec_ctx;
25
26     if(!st) {
27         st = (BIT_STRING_t *)(*sptr = CALLOC(1, sizeof(*st)));
28         if(!st) ASN__DECODE_FAILED;
29     }
30
31     if(ct_size >= 0) {
32         expected_length = (ct_size + 7) >> 3;
33         st->bits_unused = (8 - (ct_size & 7)) & 7;
34     } else {
35         /*
36          * X.696 (08/2015) #13.3.1
37          * Encode length determinant as _number of octets_, but only
38          * if upper bound is not equal to lower bound.
39          */
40         ssize_t len_len = oer_fetch_length(ptr, size, &expected_length);
41         if(len_len > 0) {
42             ptr = (const char *)ptr + len_len;
43             size -= len_len;
44         } else if(len_len == 0) {
45             ASN__DECODE_STARVED;
46         } else if(len_len < 0) {
47             ASN__DECODE_FAILED;
48         }
49
50         if(expected_length < 1) {
51             ASN__DECODE_FAILED;
52         } else if(expected_length > size) {
53             ASN__DECODE_STARVED;
54         }
55
56         st->bits_unused = ((const uint8_t *)ptr)[0];
57         if(st->bits_unused & ~7) {
58             ASN_DEBUG("%s: unused bits outside of 0..7 range", td->name);
59             ASN__DECODE_FAILED;
60         }
61         ptr = (const char *)ptr + 1;
62         size--;
63         expected_length--;
64         rval.consumed = len_len + 1;
65     }
66
67     if(size < expected_length) {
68         ASN__DECODE_STARVED;
69     } else {
70         uint8_t *buf = MALLOC(expected_length + 1);
71         if(buf == NULL) {
72             ASN__DECODE_FAILED;
73         } else {
74             memcpy(buf, ptr, expected_length);
75             buf[expected_length] = '\0';
76         }
77         FREEMEM(st->buf);
78         st->buf = buf;
79         st->size = expected_length;
80         if(expected_length > 0) {
81             buf[expected_length - 1] &= (0xff << st->bits_unused);
82         }
83
84         rval.consumed += expected_length;
85         return rval;
86     }
87 }
88
89 /*
90  * Encode as Canonical OER.
91  */
92 asn_enc_rval_t
93 BIT_STRING_encode_oer(const asn_TYPE_descriptor_t *td,
94                       const asn_oer_constraints_t *constraints,
95                       const void *sptr, asn_app_consume_bytes_f *cb,
96                       void *app_key) {
97     const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
98     asn_enc_rval_t erval = {0, 0, 0};
99     const asn_oer_constraints_t *cts =
100         constraints ? constraints : td->encoding_constraints.oer_constraints;
101     ssize_t ct_size = cts ? cts->size : -1;
102     size_t trailing_zeros = 0;
103     int fix_last_byte = 0;
104
105     if(!st) ASN__ENCODE_FAILED;
106
107     if(st->bits_unused & ~7) {
108         ASN_DEBUG("BIT STRING unused bits %d out of 0..7 range",
109                   st->bits_unused);
110         ASN__ENCODE_FAILED;
111     }
112     if(st->bits_unused && !(st->size && st->buf)) {
113         ASN_DEBUG("BIT STRING %s size 0 can't support unused bits %d", td->name,
114                   st->bits_unused);
115         ASN__ENCODE_FAILED;
116     }
117
118     if(ct_size >= 0) {
119         size_t ct_bytes = (ct_size + 7) >> 3;
120         if(st->size > ct_bytes) {
121             ASN_DEBUG("More bits in BIT STRING %s (%" ASN_PRI_SSIZE ") than constrained %" ASN_PRI_SSIZE "",
122                       td->name, 8 * st->size - st->bits_unused, ct_size);
123             ASN__ENCODE_FAILED;
124         }
125         trailing_zeros = ct_bytes - st->size;   /* Allow larger constraint */
126     } else {
127         uint8_t ub = st->bits_unused & 7;
128         ssize_t len_len = oer_serialize_length(1 + st->size, cb, app_key);
129         if(len_len < 0) ASN__ENCODE_FAILED;
130         if(cb(&ub, 1, app_key) < 0) {
131             ASN__ENCODE_FAILED;
132         }
133         erval.encoded += len_len + 1;
134     }
135
136     if(st->bits_unused) {
137         if(st->buf[st->size - 1] & (0xff << st->bits_unused)) {
138             fix_last_byte = 1;
139         }
140     }
141
142     if(cb(st->buf, st->size - fix_last_byte, app_key) < 0) {
143         ASN__ENCODE_FAILED;
144     }
145
146     if(fix_last_byte) {
147         uint8_t b = st->buf[st->size - 1] & (0xff << st->bits_unused);
148         if(cb(&b, 1, app_key) < 0) {
149             ASN__ENCODE_FAILED;
150         }
151     }
152
153     erval.encoded += st->size;
154
155     if(trailing_zeros) {
156         static uint8_t zeros[16];
157         while(trailing_zeros > 0) {
158             int ret;
159             if(trailing_zeros < sizeof(zeros)) {
160                 ret = cb(zeros, trailing_zeros, app_key);
161                 erval.encoded += trailing_zeros;
162             } else {
163                 ret = cb(zeros, sizeof(zeros), app_key);
164                 erval.encoded += sizeof(zeros);
165             }
166             if(ret < 0) ASN__ENCODE_FAILED;
167         }
168     }
169
170     return erval;
171 }
172
173
174 #endif  /* ASN_DISABLE_OER_SUPPORT */