2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #ifndef ASN_DISABLE_OER_SUPPORT
8 #include <asn_internal.h>
9 #include <BIT_STRING.h>
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;
27 st = (BIT_STRING_t *)(*sptr = CALLOC(1, sizeof(*st)));
28 if(!st) ASN__DECODE_FAILED;
32 expected_length = (ct_size + 7) >> 3;
33 st->bits_unused = (8 - (ct_size & 7)) & 7;
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.
40 ssize_t len_len = oer_fetch_length(ptr, size, &expected_length);
42 ptr = (const char *)ptr + len_len;
44 } else if(len_len == 0) {
46 } else if(len_len < 0) {
50 if(expected_length < 1) {
52 } else if(expected_length > size) {
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);
61 ptr = (const char *)ptr + 1;
64 rval.consumed = len_len + 1;
67 if(size < expected_length) {
70 uint8_t *buf = MALLOC(expected_length + 1);
74 memcpy(buf, ptr, expected_length);
75 buf[expected_length] = '\0';
79 st->size = expected_length;
80 if(expected_length > 0) {
81 buf[expected_length - 1] &= (0xff << st->bits_unused);
84 rval.consumed += expected_length;
90 * Encode as Canonical OER.
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,
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;
105 if(!st) ASN__ENCODE_FAILED;
107 if(st->bits_unused & ~7) {
108 ASN_DEBUG("BIT STRING unused bits %d out of 0..7 range",
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,
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);
125 trailing_zeros = ct_bytes - st->size; /* Allow larger constraint */
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) {
133 erval.encoded += len_len + 1;
136 if(st->bits_unused) {
137 if(st->buf[st->size - 1] & (0xff << st->bits_unused)) {
142 if(cb(st->buf, st->size - fix_last_byte, app_key) < 0) {
147 uint8_t b = st->buf[st->size - 1] & (0xff << st->bits_unused);
148 if(cb(&b, 1, app_key) < 0) {
153 erval.encoded += st->size;
156 static uint8_t zeros[16];
157 while(trailing_zeros > 0) {
159 if(trailing_zeros < sizeof(zeros)) {
160 ret = cb(zeros, trailing_zeros, app_key);
161 erval.encoded += trailing_zeros;
163 ret = cb(zeros, sizeof(zeros), app_key);
164 erval.encoded += sizeof(zeros);
166 if(ret < 0) ASN__ENCODE_FAILED;
174 #endif /* ASN_DISABLE_OER_SUPPORT */