Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -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 <INTEGER.h> |
| 10 | #include <errno.h> |
| 11 | |
| 12 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 13 | INTEGER_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 14 | const asn_oer_constraints_t *constraints, void **sptr, |
Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 15 | const void *ptr, size_t size) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 16 | const asn_INTEGER_specifics_t *specs = |
| 17 | (const asn_INTEGER_specifics_t *)td->specifics; |
Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 18 | asn_dec_rval_t rval = {RC_OK, 0}; |
| 19 | INTEGER_t *st = (INTEGER_t *)*sptr; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 20 | struct asn_oer_constraint_number_s ct = {0, 0}; |
| 21 | size_t req_bytes; |
Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 22 | |
| 23 | (void)opt_codec_ctx; |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame] | 24 | (void)specs; |
Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 25 | |
| 26 | if(!st) { |
| 27 | st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st))); |
| 28 | if(!st) ASN__DECODE_FAILED; |
| 29 | } |
| 30 | |
Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 31 | FREEMEM(st->buf); |
| 32 | st->buf = 0; |
| 33 | st->size = 0; |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame] | 34 | |
| 35 | if(!constraints) constraints = td->oer_constraints; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 36 | if(constraints) ct = constraints->value; |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame] | 37 | |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 38 | if(ct.width) { |
| 39 | req_bytes = ct.width; |
| 40 | } else { |
| 41 | /* No lower bound and no upper bound, effectively */ |
| 42 | |
| 43 | ssize_t consumed = oer_fetch_length(ptr, size, &req_bytes); |
| 44 | if(consumed == 0) { |
| 45 | ASN__DECODE_STARVED; |
| 46 | } else if(consumed == -1) { |
| 47 | ASN__DECODE_FAILED; |
| 48 | } |
| 49 | rval.consumed += consumed; |
| 50 | ptr = (const char *)ptr + consumed; |
| 51 | size -= consumed; |
| 52 | } |
| 53 | |
| 54 | if(req_bytes > size) { |
| 55 | ASN__DECODE_STARVED; |
| 56 | } |
| 57 | |
| 58 | if(ct.positive) { |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame] | 59 | /* X.969 08/2015 10.2(a) */ |
| 60 | unsigned msb; /* Most significant bit */ |
| 61 | size_t useful_size; |
| 62 | |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame] | 63 | /* Check most significant bit */ |
| 64 | msb = *(const uint8_t *)ptr >> 7; /* yields 0 or 1 */ |
| 65 | useful_size = msb + req_bytes; |
| 66 | st->buf = (uint8_t *)MALLOC(useful_size + 1); |
| 67 | if(!st->buf) { |
| 68 | ASN__DECODE_FAILED; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Record a large unsigned in a way not to confuse it |
| 73 | * with signed value. |
| 74 | */ |
| 75 | st->buf[0] = '\0'; |
| 76 | memcpy(st->buf + msb, ptr, req_bytes); |
| 77 | st->buf[useful_size] = '\0'; /* Just in case, 0-terminate */ |
| 78 | st->size = useful_size; |
| 79 | |
| 80 | rval.consumed += req_bytes; |
| 81 | return rval; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 82 | } else { |
| 83 | /* X.969 08/2015 10.2(b) */ |
| 84 | st->buf = (uint8_t *)MALLOC(req_bytes + 1); |
| 85 | if(!st->buf) { |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame] | 86 | ASN__DECODE_FAILED; |
| 87 | } |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 88 | |
| 89 | memcpy(st->buf, ptr, req_bytes); |
| 90 | st->buf[req_bytes] = '\0'; /* Just in case, 0-terminate */ |
| 91 | st->size = req_bytes; |
| 92 | |
| 93 | rval.consumed += req_bytes; |
| 94 | return rval; |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame] | 95 | } |
Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 98 | /* |
| 99 | * Encode as Canonical OER. |
| 100 | */ |
| 101 | asn_enc_rval_t |
| 102 | INTEGER_encode_oer(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 103 | const asn_oer_constraints_t *constraints, void *sptr, |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 104 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | cc9b3ae | 2017-07-14 08:57:02 +0400 | [diff] [blame] | 105 | const INTEGER_t *st = sptr; |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 106 | asn_enc_rval_t er; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 107 | struct asn_oer_constraint_number_s ct = {0, 0}; |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 108 | const uint8_t *buf; |
| 109 | const uint8_t *end; |
| 110 | size_t useful_bytes; |
| 111 | size_t req_bytes = 0; |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 112 | int sign = 0; |
| 113 | |
| 114 | if(!st || st->size == 0) ASN__ENCODE_FAILED; |
| 115 | |
| 116 | if(!constraints) constraints = td->oer_constraints; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 117 | if(constraints) ct = constraints->value; |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 118 | |
| 119 | er.encoded = 0; |
| 120 | |
| 121 | buf = st->buf; |
| 122 | end = buf + st->size; |
| 123 | |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 124 | sign = (buf && buf < end) ? buf[0] & 0x80 : 0; |
| 125 | |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 126 | /* Ignore 9 leading zeroes or ones */ |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 127 | if(ct.positive) { |
Lev Walkin | cc9b3ae | 2017-07-14 08:57:02 +0400 | [diff] [blame] | 128 | if(sign) { |
| 129 | /* The value given is a signed value. Can't proceed. */ |
| 130 | ASN__ENCODE_FAILED; |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 131 | } |
Lev Walkin | cc9b3ae | 2017-07-14 08:57:02 +0400 | [diff] [blame] | 132 | /* Remove leading zeros. */ |
| 133 | for(; buf + 1 < end; buf++) { |
| 134 | if(buf[0] != 0x0) break; |
| 135 | } |
| 136 | } else { |
| 137 | for(; buf + 1 < end; buf++) { |
| 138 | if(buf[0] == 0x0 && (buf[1] & 0x80) == 0) { |
| 139 | continue; |
| 140 | } else if(buf[0] == 0xff && (buf[1] & 0x80) != 0) { |
| 141 | continue; |
| 142 | } |
| 143 | break; |
| 144 | } |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | useful_bytes = end - buf; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 148 | if(ct.width) { |
| 149 | req_bytes = ct.width; |
| 150 | } else { |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 151 | ssize_t r = oer_serialize_length(useful_bytes, cb, app_key); |
| 152 | if(r < 0) { |
| 153 | ASN__ENCODE_FAILED; |
| 154 | } |
| 155 | er.encoded += r; |
| 156 | req_bytes = useful_bytes; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | if(req_bytes < useful_bytes) { |
Lev Walkin | fcfe19b | 2017-07-10 21:57:14 -0700 | [diff] [blame] | 160 | ASN__ENCODE_FAILED; |
| 161 | } |
| 162 | |
| 163 | er.encoded += req_bytes; |
| 164 | |
| 165 | for(; req_bytes > useful_bytes; req_bytes--) { |
| 166 | if(cb(sign?"\xff":"\0", 1, app_key) < 0) { |
| 167 | ASN__ENCODE_FAILED; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if(cb(buf, useful_bytes, app_key) < 0) { |
| 172 | ASN__ENCODE_FAILED; |
| 173 | } |
| 174 | |
| 175 | ASN__ENCODED_OK(er); |
| 176 | } |
| 177 | |
Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 178 | #endif /* ASN_DISABLE_OER_SUPPORT */ |