blob: 5b40c43c0c929be2d8da33be80c6e3cff0523937 [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) {
Lev Walkind62a6622017-08-22 02:31:02 -070016 const asn_INTEGER_specifics_t *specs =
17 (const asn_INTEGER_specifics_t *)td->specifics;
Lev Walkined3a4ae2017-07-07 10:09:51 -070018 asn_dec_rval_t rval = {RC_OK, 0};
19 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin4118ccf2017-08-02 10:37:31 -070020 struct asn_oer_constraint_number_s ct = {0, 0};
21 size_t req_bytes;
Lev Walkined3a4ae2017-07-07 10:09:51 -070022
23 (void)opt_codec_ctx;
Lev Walkine4d8c922017-07-10 20:29:33 -070024 (void)specs;
Lev Walkined3a4ae2017-07-07 10:09:51 -070025
26 if(!st) {
27 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
28 if(!st) ASN__DECODE_FAILED;
29 }
30
Lev Walkined3a4ae2017-07-07 10:09:51 -070031 FREEMEM(st->buf);
32 st->buf = 0;
33 st->size = 0;
Lev Walkine4d8c922017-07-10 20:29:33 -070034
35 if(!constraints) constraints = td->oer_constraints;
Lev Walkin4118ccf2017-08-02 10:37:31 -070036 if(constraints) ct = constraints->value;
Lev Walkine4d8c922017-07-10 20:29:33 -070037
Lev Walkin4118ccf2017-08-02 10:37:31 -070038 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 Walkine4d8c922017-07-10 20:29:33 -070059 /* X.969 08/2015 10.2(a) */
60 unsigned msb; /* Most significant bit */
61 size_t useful_size;
62
Lev Walkine4d8c922017-07-10 20:29:33 -070063 /* 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 Walkin4118ccf2017-08-02 10:37:31 -070082 } else {
83 /* X.969 08/2015 10.2(b) */
84 st->buf = (uint8_t *)MALLOC(req_bytes + 1);
85 if(!st->buf) {
Lev Walkine4d8c922017-07-10 20:29:33 -070086 ASN__DECODE_FAILED;
87 }
Lev Walkin4118ccf2017-08-02 10:37:31 -070088
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 Walkine4d8c922017-07-10 20:29:33 -070095 }
Lev Walkined3a4ae2017-07-07 10:09:51 -070096}
97
Lev Walkinfcfe19b2017-07-10 21:57:14 -070098/*
99 * Encode as Canonical OER.
100 */
101asn_enc_rval_t
102INTEGER_encode_oer(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700103 const asn_oer_constraints_t *constraints, void *sptr,
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700104 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkincc9b3ae2017-07-14 08:57:02 +0400105 const INTEGER_t *st = sptr;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700106 asn_enc_rval_t er;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700107 struct asn_oer_constraint_number_s ct = {0, 0};
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700108 const uint8_t *buf;
109 const uint8_t *end;
110 size_t useful_bytes;
111 size_t req_bytes = 0;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700112 int sign = 0;
113
114 if(!st || st->size == 0) ASN__ENCODE_FAILED;
115
116 if(!constraints) constraints = td->oer_constraints;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700117 if(constraints) ct = constraints->value;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700118
119 er.encoded = 0;
120
121 buf = st->buf;
122 end = buf + st->size;
123
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700124 sign = (buf && buf < end) ? buf[0] & 0x80 : 0;
125
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700126 /* Ignore 9 leading zeroes or ones */
Lev Walkin4118ccf2017-08-02 10:37:31 -0700127 if(ct.positive) {
Lev Walkincc9b3ae2017-07-14 08:57:02 +0400128 if(sign) {
129 /* The value given is a signed value. Can't proceed. */
130 ASN__ENCODE_FAILED;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700131 }
Lev Walkincc9b3ae2017-07-14 08:57:02 +0400132 /* 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 Walkinfcfe19b2017-07-10 21:57:14 -0700145 }
146
147 useful_bytes = end - buf;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700148 if(ct.width) {
149 req_bytes = ct.width;
150 } else {
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700151 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 Walkin4118ccf2017-08-02 10:37:31 -0700157 }
158
159 if(req_bytes < useful_bytes) {
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700160 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 Walkined3a4ae2017-07-07 10:09:51 -0700178#endif /* ASN_DISABLE_OER_SUPPORT */