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 "asn1/asn1c/asn_internal.h"
9 #include "asn1/asn1c/OCTET_STRING.h"
10 #include <errno.h>
11
12 asn_dec_rval_t
OCTET_STRING_decode_oer(const asn_codec_ctx_t * opt_codec_ctx,const asn_TYPE_descriptor_t * td,const asn_oer_constraints_t * constraints,void ** sptr,const void * ptr,size_t size)13 OCTET_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 const asn_OCTET_STRING_specifics_t *specs =
18 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
19 : &asn_SPC_OCTET_STRING_specs;
20 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
21 const asn_oer_constraints_t *cts =
22 constraints ? constraints : td->encoding_constraints.oer_constraints;
23 ssize_t ct_size = cts ? cts->size : -1;
24 asn_dec_rval_t rval = {RC_OK, 0};
25 size_t expected_length = 0;
26
27 size_t unit_bytes;
28 switch(specs->subvariant) {
29 default:
30 case ASN_OSUBV_BIT:
31 ASN_DEBUG("Invalid use of OCTET STRING to decode BIT STRING");
32 ASN__DECODE_FAILED;
33 case ASN_OSUBV_ANY:
34 /* Fall through */
35 case ASN_OSUBV_STR:
36 unit_bytes = 1;
37 break;
38 case ASN_OSUBV_U16:
39 unit_bytes = 2;
40 break;
41 case ASN_OSUBV_U32:
42 unit_bytes = 4;
43 break;
44 }
45
46 (void)opt_codec_ctx;
47
48 if(!st) {
49 st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
50 if(!st) ASN__DECODE_FAILED;
51 }
52
53 if(ct_size >= 0) {
54 expected_length = unit_bytes * ct_size;
55 } else {
56 /*
57 * X.696 (08/2015) #27.2
58 * Encode length determinant as _number of octets_, but only
59 * if upper bound is not equal to lower bound.
60 */
61 ssize_t len_len = oer_fetch_length(ptr, size, &expected_length);
62 if(len_len > 0) {
63 rval.consumed = len_len;
64 ptr = (const char *)ptr + len_len;
65 size -= len_len;
66 } else if(len_len == 0) {
67 ASN__DECODE_STARVED;
68 } else if(len_len < 0) {
69 ASN__DECODE_FAILED;
70 }
71
72 if(expected_length % unit_bytes != 0) {
73 ASN_DEBUG(
74 "Data size %" ASN_PRI_SIZE " bytes is not consistent with multiplier %" ASN_PRI_SIZE "",
75 expected_length, unit_bytes);
76 ASN__DECODE_FAILED;
77 }
78 }
79
80 if(size < expected_length) {
81 ASN__DECODE_STARVED;
82 } else {
83 uint8_t *buf = MALLOC(expected_length + 1);
84 if(buf == NULL) {
85 ASN__DECODE_FAILED;
86 } else {
87 memcpy(buf, ptr, expected_length);
88 buf[expected_length] = '\0';
89 }
90 FREEMEM(st->buf);
91 st->buf = buf;
92 st->size = expected_length;
93
94 rval.consumed += expected_length;
95 return rval;
96 }
97 }
98
99 /*
100 * Encode as Canonical OER.
101 */
102 asn_enc_rval_t
OCTET_STRING_encode_oer(const asn_TYPE_descriptor_t * td,const asn_oer_constraints_t * constraints,const void * sptr,asn_app_consume_bytes_f * cb,void * app_key)103 OCTET_STRING_encode_oer(const asn_TYPE_descriptor_t *td,
104 const asn_oer_constraints_t *constraints,
105 const void *sptr, asn_app_consume_bytes_f *cb,
106 void *app_key) {
107 const asn_OCTET_STRING_specifics_t *specs =
108 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
109 : &asn_SPC_OCTET_STRING_specs;
110 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
111 const asn_oer_constraints_t *cts =
112 constraints ? constraints : td->encoding_constraints.oer_constraints;
113 ssize_t ct_size = cts ? cts->size : -1;
114 asn_enc_rval_t er = {0, 0, 0};
115
116 if(!st) ASN__ENCODE_FAILED;
117
118 ASN_DEBUG("Encoding %s %" ASN_PRI_SIZE " as OCTET STRING", td ? td->name : "", st->size);
119
120 if(ct_size >= 0) {
121 /*
122 * Check that available data matches the constraint
123 */
124 size_t unit_bytes;
125 switch(specs->subvariant) {
126 default:
127 case ASN_OSUBV_BIT:
128 ASN_DEBUG("Invalid use of OCTET STRING to encode BIT STRING");
129 ASN__ENCODE_FAILED;
130 case ASN_OSUBV_ANY:
131 /* Fall through */
132 case ASN_OSUBV_STR:
133 unit_bytes = 1;
134 break;
135 case ASN_OSUBV_U16:
136 unit_bytes = 2;
137 break;
138 case ASN_OSUBV_U32:
139 unit_bytes = 4;
140 break;
141 }
142
143 if(st->size != unit_bytes * (size_t)ct_size) {
144 ASN_DEBUG(
145 "Trying to encode %s (%" ASN_PRI_SIZE " bytes) which doesn't fit SIZE "
146 "constraint (%" ASN_PRI_SIZE ")",
147 td->name, st->size, ct_size);
148 ASN__ENCODE_FAILED;
149 }
150 } else {
151 /*
152 * X.696 (08/2015) #27.2
153 * Encode length determinant as _number of octets_, but only
154 * if upper bound is not equal to lower bound.
155 */
156 ssize_t ret = oer_serialize_length(st->size, cb, app_key);
157 if(ret < 0) {
158 ASN__ENCODE_FAILED;
159 }
160 er.encoded += ret;
161 }
162
163 er.encoded += st->size;
164 if(cb(st->buf, st->size, app_key) < 0) {
165 ASN__ENCODE_FAILED;
166 } else {
167 ASN__ENCODED_OK(er);
168 }
169 }
170
171 #endif /* ASN_DISABLE_OER_SUPPORT */
172