2 * Copyright (c) 2005, 2006 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 * Extract a small number of bits (<= 31) from the specified PER data pointer.
13 per_get_few_bits(asn_per_data_t *pd, int nbits) {
14 size_t off; /* Next after last bit offset */
18 if(nbits < 0 || pd->nboff + nbits > pd->nbits)
21 ASN_DEBUG("[PER get %d bits from %p+%d bits]",
22 nbits, pd->buffer, pd->nboff);
25 * Normalize position indicator.
28 pd->buffer += (pd->nboff >> 3);
29 pd->nbits -= (pd->nboff & ~0x07);
32 off = (pd->nboff += nbits);
36 * Extract specified number of bits.
39 accum = nbits ? (buf[0]) >> (8 - off) : 0;
41 accum = ((buf[0] << 8) + buf[1]) >> (16 - off);
43 accum = ((buf[0] << 16) + (buf[1] << 8) + buf[2]) >> (24 - off);
45 accum = ((buf[0] << 24) + (buf[1] << 16)
46 + (buf[2] << 8) + (buf[3])) >> (32 - off);
47 else if(nbits <= 31) {
48 asn_per_data_t tpd = *pd;
49 /* Here are we with our 31-bits limit plus 1..7 bits offset. */
51 accum = per_get_few_bits(&tpd, nbits - 24) << 24;
52 accum |= per_get_few_bits(&tpd, 24);
54 pd->nboff -= nbits; /* Oops, revert back */
58 return (accum & (((uint32_t)1 << nbits) - 1));
62 * Extract a large number of bits from the specified PER data pointer.
65 per_get_many_bits(asn_per_data_t *pd, uint8_t *dst, int alright, int nbits) {
68 if(alright && (nbits & 7)) {
69 /* Perform right alignment of a first few bits */
70 value = per_get_few_bits(pd, nbits & 0x07);
71 if(value < 0) return -1;
72 *dst++ = value; /* value is already right-aligned */
78 value = per_get_few_bits(pd, 24);
79 if(value < 0) return -1;
80 *(dst++) = value >> 16;
81 *(dst++) = value >> 8;
85 value = per_get_few_bits(pd, nbits);
86 if(value < 0) return -1;
87 if(nbits & 7) { /* implies left alignment */
88 value <<= 8 - (nbits & 7),
89 nbits += 8 - (nbits & 7);
106 * Get the length "n" from the stream.
109 uper_get_length(asn_per_data_t *pd, int ebits, int *repeat) {
114 if(ebits >= 0) return per_get_few_bits(pd, ebits);
116 value = per_get_few_bits(pd, 8);
117 if(value < 0) return -1;
118 if((value & 128) == 0) /* #10.9.3.6 */
119 return (value & 0x7F);
120 if((value & 64) == 0) { /* #10.9.3.7 */
121 value = ((value & 63) << 8) | per_get_few_bits(pd, 8);
122 if(value < 0) return -1;
125 value &= 63; /* this is "m" from X.691, #10.9.3.8 */
126 if(value < 1 || value > 4)
129 return (16384 * value);
133 * Get the normally small non-negative whole number.
137 uper_get_nsnnwn(asn_per_data_t *pd) {
140 value = per_get_few_bits(pd, 7);
141 if(value & 64) { /* implicit (value < 0) */
144 value |= per_get_few_bits(pd, 2);
145 if(value & 128) /* implicit (value < 0) */
151 value = per_get_few_bits(pd, 8 * value);
159 * Put the normally small non-negative whole number.
163 uper_put_nsnnwn(asn_per_outp_t *po, int n) {
168 return per_put_few_bits(po, n, 7);
174 else if(n < 256 * 65536)
177 return -1; /* This is not a "normally small" value */
178 if(per_put_few_bits(po, bytes, 8))
181 return per_put_few_bits(po, n, 8 * bytes);
186 * Put a small number of bits (<= 31).
189 per_put_few_bits(asn_per_outp_t *po, uint32_t bits, int obits) {
190 size_t off; /* Next after last bit offset */
191 size_t omsk; /* Existing last byte meaningful bits mask */
194 if(obits <= 0 || obits >= 32) return obits ? -1 : 0;
196 ASN_DEBUG("[PER put %d bits to %p+%d bits]",
197 obits, po->buffer, po->nboff);
200 * Normalize position indicator.
203 po->buffer += (po->nboff >> 3);
204 po->nbits -= (po->nboff & ~0x07);
209 * Flush whole-bytes output, if necessary.
211 if(po->nboff + obits > po->nbits) {
212 int complete_bytes = (po->buffer - po->tmpspace);
213 if(po->outper(po->buffer, complete_bytes, po->op_key) < 0)
216 po->tmpspace[0] = po->buffer[0];
217 po->buffer = po->tmpspace;
218 po->nbits = 8 * sizeof(po->tmpspace);
219 po->flushed_bytes += complete_bytes;
223 * Now, due to sizeof(tmpspace), we are guaranteed large enough space.
226 omsk = ~((1 << (8 - po->nboff)) - 1);
227 off = (po->nboff += obits);
229 /* Clear data of debris before meaningful bits */
230 bits &= (((uint32_t)1 << obits) - 1);
232 ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits, bits, bits,
233 po->nboff - obits, off, buf[0], omsk&0xff, buf[0] & omsk);
235 if(off <= 8) /* Completely within 1 byte */
237 buf[0] = (buf[0] & omsk) | bits;
240 buf[0] = (buf[0] & omsk) | (bits >> 8),
244 buf[0] = (buf[0] & omsk) | (bits >> 16),
249 buf[0] = (buf[0] & omsk) | (bits >> 24),
254 ASN_DEBUG("->[PER out split %d]", obits);
255 per_put_few_bits(po, bits >> 8, 24);
256 per_put_few_bits(po, bits, obits - 24);
257 ASN_DEBUG("<-[PER out split %d]", obits);
260 ASN_DEBUG("[PER out %u/%x => %02x buf+%d]",
261 bits, bits, buf[0], po->buffer - po->tmpspace);
268 * Output a large number of bits.
271 per_put_many_bits(asn_per_outp_t *po, const uint8_t *src, int nbits) {
277 value = (src[0] << 16) | (src[1] << 8) | src[2];
280 if(per_put_few_bits(po, value, 24))
285 value = (value << 8) | src[1];
287 value = (value << 8) | src[2];
289 value >>= (8 - (nbits & 0x07));
290 if(per_put_few_bits(po, value, nbits))
300 * Put the length "n" (or part of it) into the stream.
303 uper_put_length(asn_per_outp_t *po, size_t length) {
305 if(length <= 127) /* #10.9.3.6 */
306 return per_put_few_bits(po, length, 8)
307 ? -1 : (ssize_t)length;
308 else if(length < 16384) /* #10.9.3.7 */
309 return per_put_few_bits(po, length|0x8000, 16)
310 ? -1 : (ssize_t)length;
313 if(length > 4) length = 4;
315 return per_put_few_bits(po, 0xC0 | length, 8)
316 ? -1 : (ssize_t)(length << 14);