blob: aff5075cdd7f7a325efb0b915d1f33b746eb5141 [file] [log] [blame]
Lev Walkincfc16d32017-08-30 19:15:08 -07001/*
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
12asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070013BIT_STRING_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
14 const asn_TYPE_descriptor_t *td,
Lev Walkincfc16d32017-08-30 19:15:08 -070015 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 =
Lev Walkina5972be2017-09-29 23:15:58 -070019 constraints ? constraints : td->encoding_constraints.oer_constraints;
Lev Walkincfc16d32017-08-30 19:15:08 -070020 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) {
Lev Walkina5b02882017-10-01 22:48:44 -070051 ASN__DECODE_FAILED;
52 } else if(expected_length > size) {
Lev Walkincfc16d32017-08-30 19:15:08 -070053 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 */
92asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070093BIT_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;
Lev Walkinfc89b9d2017-08-31 00:54:38 -070098 asn_enc_rval_t erval = {0, 0, 0};
99 const asn_oer_constraints_t *cts =
Lev Walkina5972be2017-09-29 23:15:58 -0700100 constraints ? constraints : td->encoding_constraints.oer_constraints;
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700101 ssize_t ct_size = cts ? cts->size : -1;
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700102 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) {
Lev Walkin9f470d62017-10-21 16:27:08 -0700121 ASN_DEBUG("More bits in BIT STRING %s (%" ASN_PRI_SSIZE ") than constrained %" ASN_PRI_SSIZE "",
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700122 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;
Lev Walkin71191ba2017-08-31 01:00:00 -0700128 ssize_t len_len = oer_serialize_length(1 + st->size, cb, app_key);
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700129 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;
Lev Walkincfc16d32017-08-30 19:15:08 -0700171}
172
173
174#endif /* ASN_DISABLE_OER_SUPPORT */