blob: 3462f7cffc357548eb779cdf4fca7fb1b3b8b305 [file] [log] [blame]
Lev Walkined3a4ae2017-07-07 10:09:51 -07001/*
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
12asn_dec_rval_t
13INTEGER_decode_oer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -070014 const asn_oer_constraints_t *constraints, void **sptr,
Lev Walkined3a4ae2017-07-07 10:09:51 -070015 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 Walkin4118ccf2017-08-02 10:37:31 -070019 struct asn_oer_constraint_number_s ct = {0, 0};
20 size_t req_bytes;
Lev Walkined3a4ae2017-07-07 10:09:51 -070021
22 (void)opt_codec_ctx;
Lev Walkine4d8c922017-07-10 20:29:33 -070023 (void)specs;
Lev Walkined3a4ae2017-07-07 10:09:51 -070024
25 if(!st) {
26 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
27 if(!st) ASN__DECODE_FAILED;
28 }
29
Lev Walkined3a4ae2017-07-07 10:09:51 -070030 FREEMEM(st->buf);
31 st->buf = 0;
32 st->size = 0;
Lev Walkine4d8c922017-07-10 20:29:33 -070033
34 if(!constraints) constraints = td->oer_constraints;
Lev Walkin4118ccf2017-08-02 10:37:31 -070035 if(constraints) ct = constraints->value;
Lev Walkine4d8c922017-07-10 20:29:33 -070036
Lev Walkin4118ccf2017-08-02 10:37:31 -070037 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 Walkine4d8c922017-07-10 20:29:33 -070058 /* X.969 08/2015 10.2(a) */
59 unsigned msb; /* Most significant bit */
60 size_t useful_size;
61
Lev Walkine4d8c922017-07-10 20:29:33 -070062 /* 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 Walkin4118ccf2017-08-02 10:37:31 -070081 } else {
82 /* X.969 08/2015 10.2(b) */
83 st->buf = (uint8_t *)MALLOC(req_bytes + 1);
84 if(!st->buf) {
Lev Walkine4d8c922017-07-10 20:29:33 -070085 ASN__DECODE_FAILED;
86 }
Lev Walkin4118ccf2017-08-02 10:37:31 -070087
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 Walkine4d8c922017-07-10 20:29:33 -070094 }
Lev Walkined3a4ae2017-07-07 10:09:51 -070095}
96
Lev Walkinfcfe19b2017-07-10 21:57:14 -070097/*
98 * Encode as Canonical OER.
99 */
100asn_enc_rval_t
101INTEGER_encode_oer(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700102 const asn_oer_constraints_t *constraints, void *sptr,
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700103 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkincc9b3ae2017-07-14 08:57:02 +0400104 const INTEGER_t *st = sptr;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700105 asn_enc_rval_t er;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700106 struct asn_oer_constraint_number_s ct = {0, 0};
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700107 const uint8_t *buf;
108 const uint8_t *end;
109 size_t useful_bytes;
110 size_t req_bytes = 0;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700111 int sign = 0;
112
113 if(!st || st->size == 0) ASN__ENCODE_FAILED;
114
115 if(!constraints) constraints = td->oer_constraints;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700116 if(constraints) ct = constraints->value;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700117
118 er.encoded = 0;
119
120 buf = st->buf;
121 end = buf + st->size;
122
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700123 sign = (buf && buf < end) ? buf[0] & 0x80 : 0;
124
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700125 /* Ignore 9 leading zeroes or ones */
Lev Walkin4118ccf2017-08-02 10:37:31 -0700126 if(ct.positive) {
Lev Walkincc9b3ae2017-07-14 08:57:02 +0400127 if(sign) {
128 /* The value given is a signed value. Can't proceed. */
129 ASN__ENCODE_FAILED;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700130 }
Lev Walkincc9b3ae2017-07-14 08:57:02 +0400131 /* 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 Walkinfcfe19b2017-07-10 21:57:14 -0700144 }
145
146 useful_bytes = end - buf;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700147 if(ct.width) {
148 req_bytes = ct.width;
149 } else {
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700150 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 Walkin4118ccf2017-08-02 10:37:31 -0700156 }
157
158 if(req_bytes < useful_bytes) {
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700159 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 Walkined3a4ae2017-07-07 10:09:51 -0700177#endif /* ASN_DISABLE_OER_SUPPORT */