blob: 506ca780a6b1e41c3e128950c380ae7539d3b670 [file] [log] [blame]
vlm337167e2005-11-26 11:25:14 +00001/*
2 * Copyright (c) 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <asn_system.h>
6#include <per_support.h>
7
8/*
9 * Extract a small number of bits (<= 24) from the specified PER data pointer.
10 */
11int32_t
12per_get_few_bits(asn_per_data_t *pd, int nbits) {
13 size_t off; /* Next after last bit offset */
14 uint32_t accum;
15 uint8_t *buf;
16
17 if(nbits < 0 || pd->nboff + nbits > pd->nbits)
18 return -1;
19 if(nbits == 0)
20 return 0;
21
22 /*
23 * Normalize position indicator.
24 */
25 if(pd->nboff >= 8) {
26 pd->buffer += (pd->nboff >> 3);
27 pd->nbits -= (pd->nboff & ~0x07);
28 pd->nboff &= 0x07;
29 }
30 off = (pd->nboff += nbits);
31 buf = pd->buffer;
32
33 /*
34 * Extract specified number of bits.
35 */
36 if(off <= 8)
37 accum = (buf[0]) >> (8 - off);
38 else if(off <= 16)
39 accum = ((buf[0] << 8) + buf[1]) >> (16 - off);
40 else if(off <= 24)
41 accum = ((buf[0] << 16) + (buf[1] << 8) + buf[2]) >> (24 - off);
42 else if(off <= 31)
43 accum = ((buf[0] << 24) + (buf[1] << 16)
44 + (buf[2] << 8) + (buf[3])) >> (32 - off);
45 else {
46 pd->nboff -= nbits; /* Oops, revert back */
47 return -1;
48 }
49
50 return (accum & ((1 << nbits) - 1));
51}
52
53/*
54 * Extract a large number of bits from the specified PER data pointer.
55 */
56int
57per_get_many_bits(asn_per_data_t *pd, uint8_t *dst, int alright, int nbits) {
58 int32_t value;
59
60 if(alright && (nbits & 7)) {
61 /* Perform right alignment of a first few bits */
62 value = per_get_few_bits(pd, nbits & 0x07);
63 if(value < 0) return -1;
64 *dst++ = value; /* value is already right-aligned */
65 nbits &= ~7;
66 }
67
68 while(nbits) {
69 if(nbits >= 24) {
70 value = per_get_few_bits(pd, 24);
71 if(value < 0) return -1;
72 *(dst++) = value >> 16;
73 *(dst++) = value >> 8;
74 *(dst++) = value;
75 nbits -= 24;
76 } else {
77 value = per_get_few_bits(pd, nbits);
78 if(value < 0) return -1;
79 if(nbits & 7) { /* implies alright */
80 value <<= 8 - (nbits & 7),
81 nbits += 8 - (nbits & 7);
82 if(nbits > 24)
83 *dst++ = value >> 24;
84 }
85 if(nbits > 16)
86 *dst++ = value >> 16;
87 if(nbits > 8)
88 *dst++ = value >> 8;
89 *dst++ = value;
90 break;
91 }
92 }
93
94 return 0;
95}
96
97/*
98 * Get the length "n" from the stream.
99 */
100ssize_t
101uper_get_length(asn_per_data_t *pd, int ebits, int *repeat) {
102 ssize_t value;
103
104 *repeat = 0;
105
106 if(ebits >= 0) return per_get_few_bits(pd, ebits);
107
108 value = per_get_few_bits(pd, 8);
109 if(value < 0) return -1;
110 if((value & 128) == 0) /* #10.9.3.6 */
111 return (value & 0x7F);
112 if((value & 64) == 0) { /* #10.9.3.7 */
113 value = ((value & 63) << 8) | per_get_few_bits(pd, 8);
114 if(value < 0) return -1;
115 return value;
116 }
117 value &= 63; /* this is "m" from X.691, #10.9.3.8 */
118 if(value < 1 || value > 4)
119 return -1;
120 *repeat = 1;
121 return (16384 * value);
122}
123
124/*
125 * Get the normally small non-negative whole number.
126 * X.691, #10.6
127 */
128ssize_t
129uper_get_nsnnwn(asn_per_data_t *pd) {
130 ssize_t value;
131
132 value = per_get_few_bits(pd, 7);
133 if(value & 64) { /* implicit (value < 0) */
134 value &= 63;
135 value <<= 2;
136 value |= per_get_few_bits(pd, 2);
137 if(value & 128) /* implicit (value < 0) */
138 return -1;
139 if(value == 0)
140 return 0;
141 if(value >= 3)
142 return -1;
143 value = per_get_few_bits(pd, 8 * value);
144 return value;
145 }
146
147 return value;
148}