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 <OCTET_STRING.h>
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;
28 switch(specs->subvariant) {
31 ASN_DEBUG("Invalid use of OCTET STRING to decode BIT STRING");
49 st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
50 if(!st) ASN__DECODE_FAILED;
54 expected_length = unit_bytes * ct_size;
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.
61 ssize_t len_len = oer_fetch_length(ptr, size, &expected_length);
63 rval.consumed = len_len;
64 ptr = (const char *)ptr + len_len;
66 } else if(len_len == 0) {
68 } else if(len_len < 0) {
72 if(expected_length % unit_bytes != 0) {
74 "Data size %" ASN_PRI_SIZE " bytes is not consistent with multiplier %" ASN_PRI_SIZE "",
75 expected_length, unit_bytes);
80 if(size < expected_length) {
83 uint8_t *buf = MALLOC(expected_length + 1);
87 memcpy(buf, ptr, expected_length);
88 buf[expected_length] = '\0';
92 st->size = expected_length;
94 rval.consumed += expected_length;
100 * Encode as Canonical OER.
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,
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};
116 if(!st) ASN__ENCODE_FAILED;
118 ASN_DEBUG("Encoding %s %" ASN_PRI_SIZE " as OCTET STRING", td ? td->name : "", st->size);
122 * Check that available data matches the constraint
125 switch(specs->subvariant) {
128 ASN_DEBUG("Invalid use of OCTET STRING to encode BIT STRING");
143 if(st->size != unit_bytes * (size_t)ct_size) {
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);
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.
156 ssize_t ret = oer_serialize_length(st->size, cb, app_key);
163 er.encoded += st->size;
164 if(cb(st->buf, st->size, app_key) < 0) {
171 #endif /* ASN_DISABLE_OER_SUPPORT */