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