blob: 3a72166cf37ba4b0744093961ca698a8475e3810 [file] [log] [blame]
Lev Walkin1d76f3c2017-07-25 07:58:05 -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 <OCTET_STRING.h>
10#include <errno.h>
11
Lev Walkin1d76f3c2017-07-25 07:58:05 -070012asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -070013OCTET_STRING_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -070014 asn_TYPE_descriptor_t *td,
15 const asn_oer_constraints_t *constraints, void **sptr,
16 const void *ptr, size_t size) {
Lev Walkind84f6032017-10-03 16:33:59 -070017 const asn_OCTET_STRING_specifics_t *specs =
18 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
19 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin1d76f3c2017-07-25 07:58:05 -070020 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -070021 const asn_oer_constraints_t *cts =
Lev Walkina5972be2017-09-29 23:15:58 -070022 constraints ? constraints : td->encoding_constraints.oer_constraints;
Lev Walkin4118ccf2017-08-02 10:37:31 -070023 ssize_t ct_size = cts ? cts->size : -1;
Lev Walkin1d76f3c2017-07-25 07:58:05 -070024 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
Lev Walkin4118ccf2017-08-02 10:37:31 -070053 if(ct_size >= 0) {
54 expected_length = unit_bytes * ct_size;
Lev Walkin1d76f3c2017-07-25 07:58:05 -070055 } 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 %zu bytes is not consistent with multiplier %zu",
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 */
102asn_enc_rval_t
103OCTET_STRING_encode_oer(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700104 const asn_oer_constraints_t *constraints, void *sptr,
105 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind84f6032017-10-03 16:33:59 -0700106 const asn_OCTET_STRING_specifics_t *specs =
107 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
108 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin1d76f3c2017-07-25 07:58:05 -0700109 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin494fb702017-08-07 20:07:00 -0700110 const asn_oer_constraints_t *cts =
Lev Walkina5972be2017-09-29 23:15:58 -0700111 constraints ? constraints : td->encoding_constraints.oer_constraints;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700112 ssize_t ct_size = cts ? cts->size : -1;
Lev Walkin1d76f3c2017-07-25 07:58:05 -0700113 asn_enc_rval_t er = {0, 0, 0};
114
115 if(!st) ASN__ENCODE_FAILED;
116
Lev Walkin24810022017-08-25 12:16:11 -0700117 ASN_DEBUG("Encoding %s %zu as OCTET STRING", td ? td->name : "", st->size);
Lev Walkin1d76f3c2017-07-25 07:58:05 -0700118
Lev Walkin4118ccf2017-08-02 10:37:31 -0700119 if(ct_size >= 0) {
Lev Walkin1d76f3c2017-07-25 07:58:05 -0700120 /*
121 * Check that available data matches the constraint
122 */
123 size_t unit_bytes;
124 switch(specs->subvariant) {
125 default:
126 case ASN_OSUBV_BIT:
127 ASN_DEBUG("Invalid use of OCTET STRING to encode BIT STRING");
128 ASN__ENCODE_FAILED;
129 case ASN_OSUBV_ANY:
130 /* Fall through */
131 case ASN_OSUBV_STR:
132 unit_bytes = 1;
133 break;
134 case ASN_OSUBV_U16:
135 unit_bytes = 2;
136 break;
137 case ASN_OSUBV_U32:
138 unit_bytes = 4;
139 break;
140 }
141
Lev Walkin6021ec02017-08-07 19:00:46 -0700142 if(st->size != unit_bytes * (size_t)ct_size) {
Lev Walkin1d76f3c2017-07-25 07:58:05 -0700143 ASN_DEBUG(
144 "Trying to encode %s (%zu bytes) which doesn't fit SIZE "
Lev Walkin24810022017-08-25 12:16:11 -0700145 "constraint (%zu)",
Lev Walkin4118ccf2017-08-02 10:37:31 -0700146 td->name, st->size, ct_size);
Lev Walkin1d76f3c2017-07-25 07:58:05 -0700147 ASN__ENCODE_FAILED;
148 }
149 } else {
150 /*
151 * X.696 (08/2015) #27.2
152 * Encode length determinant as _number of octets_, but only
153 * if upper bound is not equal to lower bound.
154 */
155 ssize_t ret = oer_serialize_length(st->size, cb, app_key);
156 if(ret < 0) {
157 ASN__ENCODE_FAILED;
158 }
159 er.encoded += ret;
160 }
161
162 er.encoded += st->size;
163 if(cb(st->buf, st->size, app_key) < 0) {
164 ASN__ENCODE_FAILED;
165 } else {
166 ASN__ENCODED_OK(er);
167 }
168}
169
170#endif /* ASN_DISABLE_OER_SUPPORT */