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);
298 aper_get_align(asn_per_data_t *pd) {
300 if(pd->nboff & 0x7) {
301 ASN_DEBUG("Aligning %ld bits", 8 - ((unsigned long)pd->nboff & 0x7));
302 return per_get_few_bits(pd, 8 - (pd->nboff & 0x7));
308 aper_get_length(asn_per_data_t *pd, int range, int ebits, int *repeat) {
313 // First check effective_bits parameter.
315 if (range <= 65536 && range >= 0)
316 return aper_get_nsnnwn(pd, range);
319 if (aper_get_align(pd) < 0)
322 if(ebits >= 0) return per_get_few_bits(pd, ebits);
324 value = per_get_few_bits(pd, 8);
325 if(value < 0) return -1;
326 if((value & 128) == 0) /* #10.9.3.6 */
327 return (value & 0x7F);
328 if((value & 64) == 0) { /* #10.9.3.7 */
329 value = ((value & 63) << 8) | per_get_few_bits(pd, 8);
330 if(value < 0) return -1;
333 value &= 63; /* this is "m" from X.691, #10.9.3.8 */
334 if(value < 1 || value > 4)
337 return (16384 * value);
341 aper_get_nslength(asn_per_data_t *pd) {
344 ASN_DEBUG("Getting normally small length");
346 if(per_get_few_bits(pd, 1) == 0) {
347 length = per_get_few_bits(pd, 6) + 1;
348 if(length <= 0) return -1;
349 ASN_DEBUG("l=%ld", length);
353 length = aper_get_length(pd, -1, -1, &repeat);
354 if(length >= 0 && !repeat) return length;
355 return -1; /* Error, or do not support >16K extensions */
360 aper_get_nsnnwn(asn_per_data_t *pd, int range) {
364 ASN_DEBUG("getting nsnnwn with range %d", range);
369 if (range < 0) return -1;
371 for (i = 1; i <= 8; i++) {
376 value = per_get_few_bits(pd, i);
378 } else if (range == 256){
381 } else if (range <= 65536) {
387 if (aper_get_align(pd) < 0)
389 value = per_get_few_bits(pd, 8 * bytes);
393 int aper_put_align(asn_per_outp_t *po) {
395 if(po->nboff & 0x7) {
396 ASN_DEBUG("Aligning %ld bits", 8 - ((unsigned long)po->nboff & 0x7));
397 if(per_put_few_bits(po, 0x00, (8 - (po->nboff & 0x7))))
404 aper_put_length(asn_per_outp_t *po, int range, size_t length) {
406 ASN_DEBUG("APER put length %zu with range %d", length, range);
408 /* 10.9 X.691 Note 2 */
409 if (range <= 65536 && range >= 0)
410 return aper_put_nsnnwn(po, range, length);
412 if (aper_put_align(po) < 0)
415 if(length <= 127) /* #10.9.3.6 */{
416 return per_put_few_bits(po, length, 8)
417 ? -1 : (ssize_t)length;
419 else if(length < 16384) /* #10.9.3.7 */
420 return per_put_few_bits(po, length|0x8000, 16)
421 ? -1 : (ssize_t)length;
424 if(length > 4) length = 4;
426 return per_put_few_bits(po, 0xC0 | length, 8)
427 ? -1 : (ssize_t)(length << 14);
432 aper_put_nslength(asn_per_outp_t *po, size_t length) {
436 if(length == 0) return -1;
437 return per_put_few_bits(po, length-1, 7) ? -1 : 0;
439 if(aper_put_length(po, -1, length) != (ssize_t)length) {
440 /* This might happen in case of >16K extensions */
449 aper_put_nsnnwn(asn_per_outp_t *po, int range, int number) {
452 ASN_DEBUG("aper put nsnnwn %d with range %d", number, range);
457 int bits = 1 << (8 * i);
466 for (i = 1; i <= 8; i++) {
471 return per_put_few_bits(po, number, i);
472 } else if(range == 256) {
474 } else if(range <= 65536) {
476 } else { /* Ranges > 64K */
479 int bits = 1 << (8 * i);
486 if(aper_put_align(po) < 0) /* Aligning on octet */
488 /* if(per_put_few_bits(po, bytes, 8))
491 return per_put_few_bits(po, number, 8 * bytes);