Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 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 <asn_internal.h> |
| 9 | #include <OCTET_STRING.h> |
| 10 | #include <errno.h> |
| 11 | |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 12 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 13 | OCTET_STRING_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 14 | asn_TYPE_descriptor_t *td, |
| 15 | const asn_oer_constraints_t *constraints, void **sptr, |
| 16 | const void *ptr, size_t size) { |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 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; |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 20 | OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 21 | const asn_oer_constraints_t *cts = |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 22 | constraints ? constraints : td->encoding_constraints.oer_constraints; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 23 | ssize_t ct_size = cts ? cts->size : -1; |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 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 | |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 53 | if(ct_size >= 0) { |
| 54 | expected_length = unit_bytes * ct_size; |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 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 %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 | */ |
| 102 | asn_enc_rval_t |
| 103 | OCTET_STRING_encode_oer(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 104 | const asn_oer_constraints_t *constraints, void *sptr, |
| 105 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | d84f603 | 2017-10-03 16:33:59 -0700 | [diff] [blame] | 106 | 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 Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 109 | OCTET_STRING_t *st = (OCTET_STRING_t *)sptr; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 110 | const asn_oer_constraints_t *cts = |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 111 | constraints ? constraints : td->encoding_constraints.oer_constraints; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 112 | ssize_t ct_size = cts ? cts->size : -1; |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 113 | asn_enc_rval_t er = {0, 0, 0}; |
| 114 | |
| 115 | if(!st) ASN__ENCODE_FAILED; |
| 116 | |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 117 | ASN_DEBUG("Encoding %s %zu as OCTET STRING", td ? td->name : "", st->size); |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 118 | |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 119 | if(ct_size >= 0) { |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 120 | /* |
| 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 Walkin | 6021ec0 | 2017-08-07 19:00:46 -0700 | [diff] [blame] | 142 | if(st->size != unit_bytes * (size_t)ct_size) { |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 143 | ASN_DEBUG( |
| 144 | "Trying to encode %s (%zu bytes) which doesn't fit SIZE " |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 145 | "constraint (%zu)", |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 146 | td->name, st->size, ct_size); |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 147 | 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 */ |