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 | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 17 | asn_OCTET_STRING_specifics_t *specs = |
| 18 | td->specifics |
| 19 | ? (asn_OCTET_STRING_specifics_t *)td->specifics |
Lev Walkin | 2535bd5 | 2017-08-27 23:45:56 -0700 | [diff] [blame] | 20 | : (asn_OCTET_STRING_specifics_t *)&asn_SPC_OCTET_STRING_specs; |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 21 | OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 22 | const asn_oer_constraints_t *cts = |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 23 | constraints ? constraints : td->encoding_constraints.oer_constraints; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 24 | ssize_t ct_size = cts ? cts->size : -1; |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 25 | 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 Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 54 | if(ct_size >= 0) { |
| 55 | expected_length = unit_bytes * ct_size; |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 56 | } 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 | */ |
| 103 | asn_enc_rval_t |
| 104 | OCTET_STRING_encode_oer(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 105 | const asn_oer_constraints_t *constraints, void *sptr, |
| 106 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 107 | asn_OCTET_STRING_specifics_t *specs = |
| 108 | td->specifics |
| 109 | ? (asn_OCTET_STRING_specifics_t *)td->specifics |
Lev Walkin | 2535bd5 | 2017-08-27 23:45:56 -0700 | [diff] [blame] | 110 | : (asn_OCTET_STRING_specifics_t *)&asn_SPC_OCTET_STRING_specs; |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 111 | OCTET_STRING_t *st = (OCTET_STRING_t *)sptr; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 112 | const asn_oer_constraints_t *cts = |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 113 | constraints ? constraints : td->encoding_constraints.oer_constraints; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 114 | ssize_t ct_size = cts ? cts->size : -1; |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 115 | asn_enc_rval_t er = {0, 0, 0}; |
| 116 | |
| 117 | if(!st) ASN__ENCODE_FAILED; |
| 118 | |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 119 | 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] | 120 | |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 121 | if(ct_size >= 0) { |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 122 | /* |
| 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 Walkin | 6021ec0 | 2017-08-07 19:00:46 -0700 | [diff] [blame] | 144 | if(st->size != unit_bytes * (size_t)ct_size) { |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 145 | ASN_DEBUG( |
| 146 | "Trying to encode %s (%zu bytes) which doesn't fit SIZE " |
Lev Walkin | 2481002 | 2017-08-25 12:16:11 -0700 | [diff] [blame] | 147 | "constraint (%zu)", |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 148 | td->name, st->size, ct_size); |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 149 | 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 */ |