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