2 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_system.h>
6 #include <asn_internal.h>
7 #include <per_support.h>
10 * X.691-201508 #10.9 General rules for encoding a length determinant.
11 * Get the optionally constrained length "n" from the stream.
14 uper_get_length(asn_per_data_t *pd, int ebits, size_t lower_bound,
20 /* #11.9.4.1 Encoding if constrained (according to effective bits) */
21 if(ebits >= 0 && ebits <= 16) {
22 value = per_get_few_bits(pd, ebits);
23 if(value >= 0) value += lower_bound;
27 value = per_get_few_bits(pd, 8);
28 if((value & 0x80) == 0) { /* #11.9.3.6 */
29 return (value & 0x7F);
30 } else if((value & 0x40) == 0) { /* #11.9.3.7 */
31 /* bit 8 ... set to 1 and bit 7 ... set to zero */
32 value = ((value & 0x3f) << 8) | per_get_few_bits(pd, 8);
33 return value; /* potential -1 from per_get_few_bits passes through. */
34 } else if(value < 0) {
35 ASN_DEBUG("END of stream reached for PER");
38 value &= 0x3f; /* this is "m" from X.691, #11.9.3.8 */
39 if(value < 1 || value > 4) {
40 return -1; /* Prohibited by #11.9.3.8 */
43 return (16384 * value);
47 * Get the normally small length "n".
48 * This procedure used to decode length of extensions bit-maps
49 * for SET and SEQUENCE types.
52 uper_get_nslength(asn_per_data_t *pd) {
55 ASN_DEBUG("Getting normally small length");
57 if(per_get_few_bits(pd, 1) == 0) {
58 length = per_get_few_bits(pd, 6) + 1;
59 if(length <= 0) return -1;
60 ASN_DEBUG("l=%d", (int)length);
64 length = uper_get_length(pd, -1, 0, &repeat);
65 if(length >= 0 && !repeat) return length;
66 return -1; /* Error, or do not support >16K extensions */
71 * Get the normally small non-negative whole number.
75 uper_get_nsnnwn(asn_per_data_t *pd) {
78 value = per_get_few_bits(pd, 7);
79 if(value & 64) { /* implicit (value < 0) */
82 value |= per_get_few_bits(pd, 2);
83 if(value & 128) /* implicit (value < 0) */
89 value = per_get_few_bits(pd, 8 * value);
97 * X.691-11/2008, #11.6
98 * Encoding of a normally small non-negative whole number
101 uper_put_nsnnwn(asn_per_outp_t *po, int n) {
106 return per_put_few_bits(po, n, 7);
112 else if(n < 256 * 65536)
115 return -1; /* This is not a "normally small" value */
116 if(per_put_few_bits(po, bytes, 8))
119 return per_put_few_bits(po, n, 8 * bytes);
123 /* X.691-2008/11, #11.5.6 -> #11.3 */
124 int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *out_value, int nbits) {
125 unsigned long lhalf; /* Lower half of the number*/
129 half = per_get_few_bits(pd, nbits);
130 if(half < 0) return -1;
135 if((size_t)nbits > 8 * sizeof(*out_value))
136 return -1; /* RANGE */
138 half = per_get_few_bits(pd, 31);
139 if(half < 0) return -1;
141 if(uper_get_constrained_whole_number(pd, &lhalf, nbits - 31))
144 *out_value = ((unsigned long)half << (nbits - 31)) | lhalf;
149 /* X.691-2008/11, #11.5.6 -> #11.3 */
151 uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v,
154 return per_put_few_bits(po, v, nbits);
156 /* Put higher portion first, followed by lower 31-bit */
157 if(uper_put_constrained_whole_number_u(po, v >> 31, nbits - 31))
159 return per_put_few_bits(po, v, 31);
164 * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
165 * Put the length "n" (or part of it) into the stream.
168 uper_put_length(asn_per_outp_t *po, size_t length, int *need_eom) {
170 if(!need_eom) need_eom = &dummy;
172 if(length <= 127) { /* #11.9.3.6 */
174 return per_put_few_bits(po, length, 8)
175 ? -1 : (ssize_t)length;
176 } else if(length < 16384) { /* #10.9.3.7 */
178 return per_put_few_bits(po, length|0x8000, 16)
179 ? -1 : (ssize_t)length;
182 *need_eom = 0 == (length & 16383);
189 return per_put_few_bits(po, 0xC0 | length, 8)
190 ? -1 : (ssize_t)(length << 14);
196 * Put the normally small length "n" into the stream.
197 * This procedure used to encode length of extensions bit-maps
198 * for SET and SEQUENCE types.
201 uper_put_nslength(asn_per_outp_t *po, size_t length) {
204 if(length == 0) return -1;
205 return per_put_few_bits(po, length - 1, 7) ? -1 : 0;
208 if(uper_put_length(po, length, &need_eom) != (ssize_t)length
210 /* This might happen in case of >16K extensions */
219 per__long_range(long lb, long ub, unsigned long *range_r) {
220 unsigned long bounds_range;
221 if((ub < 0) == (lb < 0)) {
222 bounds_range = ub - lb;
225 bounds_range = 1 + ((unsigned long)ub + (unsigned long)-(lb + 1));
227 assert(!"Unreachable");
230 *range_r = bounds_range;
235 per_long_range_rebase(long v, long lb, long ub, unsigned long *output) {
240 if(v < lb || v > ub || per__long_range(lb, ub, &range) < 0) {
246 * Fundamentally what we're doing is returning (v-lb).
247 * However, this triggers undefined behavior when the word width
248 * of signed (v) is the same as the size of unsigned (*output).
249 * In practice, it triggers the UndefinedSanitizer. Therefore we shall
250 * compute the ranges accurately to avoid C's undefined behavior.
252 if((v < 0) == (lb < 0)) {
256 unsigned long rebased = 1 + (unsigned long)-(v+1) + (unsigned long)lb;
257 assert(rebased <= range); /* By construction */
261 unsigned long rebased = 1 + (unsigned long)-(lb+1) + (unsigned long)v;
262 assert(rebased <= range); /* By construction */
266 assert(!"Unreachable");
272 per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp) {
275 if(per__long_range(lb, ub, &range) != 0) {
281 * We can encode something in the given number of bits that technically
282 * exceeds the range. This is an avenue for security errors,
283 * so we don't allow that.
288 if(inp <= LONG_MAX) {
289 *outp = (long)inp + lb;
291 *outp = (lb + LONG_MAX + 1) + (long)((inp - LONG_MAX) - 1);