1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
5 # Licensed under the Apache License, Version 2.0 (the "License"); *
6 # you may not use this file except in compliance with the License. *
7 # You may obtain a copy of the License at *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
11 # Unless required by applicable law or agreed to in writing, software *
12 # distributed under the License is distributed on an "AS IS" BASIS, *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 # See the License for the specific language governing permissions and *
15 # limitations under the License. *
17 ******************************************************************************/
20 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>.
21 * All rights reserved.
22 * Redistribution and modifications are permitted subject to BSD license.
24 #include <asn_system.h>
25 #include <asn_internal.h>
26 #include <asn_bit_data.h>
29 * Create a contiguous non-refillable bit data structure.
30 * Can be freed by FREEMEM().
33 asn_bit_data_new_contiguous(const void *data, size_t size_bits) {
34 size_t size_bytes = (size_bits + 7) / 8;
38 /* Get the extensions map */
39 pd = CALLOC(1, sizeof(*pd) + size_bytes + 1);
43 bytes = (void *)(((char *)pd) + sizeof(*pd));
44 memcpy(bytes, data, size_bytes);
45 bytes[size_bytes] = 0;
48 pd->nbits = size_bits;
55 asn_bit_data_string(asn_bit_data_t *pd) {
56 static char buf[2][32];
59 snprintf(buf[n], sizeof(buf[n]),
60 "{m=%" ASN_PRI_SIZE " span %" ASN_PRI_SIZE "[%" ASN_PRI_SIZE
61 "..%" ASN_PRI_SIZE "] (%" ASN_PRI_SIZE ")}",
62 pd->moved, ((uintptr_t)(pd->buffer) & 0xf), pd->nboff, pd->nbits,
63 pd->nbits - pd->nboff);
68 asn_get_undo(asn_bit_data_t *pd, int nbits) {
69 if((ssize_t)pd->nboff < nbits) {
70 assert((ssize_t)pd->nboff < nbits);
78 * Extract a small number of bits (<= 31) from the specified PER data pointer.
81 asn_get_few_bits(asn_bit_data_t *pd, int nbits) {
82 size_t off; /* Next after last bit offset */
83 ssize_t nleft; /* Number of bits left in this stream */
90 nleft = pd->nbits - pd->nboff;
93 if(!pd->refill || nbits > 31) return -1;
94 /* Accumulate unused bytes before refill */
95 ASN_DEBUG("Obtain the rest %d bits (want %d)",
96 (int)nleft, (int)nbits);
97 tailv = asn_get_few_bits(pd, nleft);
98 if(tailv < 0) return -1;
99 /* Refill (replace pd contents with new data) */
103 vhead = asn_get_few_bits(pd, nbits);
104 /* Combine the rest of previous pd with the head of new one */
105 tailv = (tailv << nbits) | vhead; /* Could == -1 */
110 * Normalize position indicator.
113 pd->buffer += (pd->nboff >> 3);
114 pd->nbits -= (pd->nboff & ~0x07);
123 * Extract specified number of bits.
126 accum = nbits ? (buf[0]) >> (8 - off) : 0;
128 accum = ((buf[0] << 8) + buf[1]) >> (16 - off);
130 accum = ((buf[0] << 16) + (buf[1] << 8) + buf[2]) >> (24 - off);
132 accum = (((uint32_t)buf[0] << 24) + (buf[1] << 16)
133 + (buf[2] << 8) + (buf[3])) >> (32 - off);
134 else if(nbits <= 31) {
135 asn_bit_data_t tpd = *pd;
136 /* Here are we with our 31-bits limit plus 1..7 bits offset. */
137 asn_get_undo(&tpd, nbits);
138 /* The number of available bits in the stream allow
139 * for the following operations to take place without
140 * invoking the ->refill() function */
141 accum = asn_get_few_bits(&tpd, nbits - 24) << 24;
142 accum |= asn_get_few_bits(&tpd, 24);
144 asn_get_undo(pd, nbits);
148 accum &= (((uint32_t)1 << nbits) - 1);
150 ASN_DEBUG(" [PER got %2d<=%2d bits => span %d %+ld[%d..%d]:%02x (%d) => 0x%x]",
151 (int)nbits, (int)nleft,
153 (((long)pd->buffer) & 0xf),
154 (int)pd->nboff, (int)pd->nbits,
155 ((pd->buffer != NULL)?pd->buffer[0]:0),
156 (int)(pd->nbits - pd->nboff),
163 * Extract a large number of bits from the specified PER data pointer.
166 asn_get_many_bits(asn_bit_data_t *pd, uint8_t *dst, int alright, int nbits) {
169 if(alright && (nbits & 7)) {
170 /* Perform right alignment of a first few bits */
171 value = asn_get_few_bits(pd, nbits & 0x07);
172 if(value < 0) return -1;
173 *dst++ = value; /* value is already right-aligned */
179 value = asn_get_few_bits(pd, 24);
180 if(value < 0) return -1;
181 *(dst++) = value >> 16;
182 *(dst++) = value >> 8;
186 value = asn_get_few_bits(pd, nbits);
187 if(value < 0) return -1;
188 if(nbits & 7) { /* implies left alignment */
189 value <<= 8 - (nbits & 7),
190 nbits += 8 - (nbits & 7);
192 *dst++ = value >> 24;
195 *dst++ = value >> 16;
207 * Put a small number of bits (<= 31).
210 asn_put_few_bits(asn_bit_outp_t *po, uint32_t bits, int obits) {
211 size_t off; /* Next after last bit offset */
212 size_t omsk; /* Existing last byte meaningful bits mask */
215 if(obits <= 0 || obits >= 32) return obits ? -1 : 0;
217 ASN_DEBUG("[PER put %d bits %x to %p+%d bits]",
218 obits, (int)bits, (void *)po->buffer, (int)po->nboff);
221 * Normalize position indicator.
224 po->buffer += (po->nboff >> 3);
225 po->nbits -= (po->nboff & ~0x07);
230 * Flush whole-bytes output, if necessary.
232 if(po->nboff + obits > po->nbits) {
233 size_t complete_bytes;
234 if(!po->buffer) po->buffer = po->tmpspace;
235 complete_bytes = (po->buffer - po->tmpspace);
236 ASN_DEBUG("[PER output %ld complete + %ld]",
237 (long)complete_bytes, (long)po->flushed_bytes);
238 if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0)
241 po->tmpspace[0] = po->buffer[0];
242 po->buffer = po->tmpspace;
243 po->nbits = 8 * sizeof(po->tmpspace);
244 po->flushed_bytes += complete_bytes;
248 * Now, due to sizeof(tmpspace), we are guaranteed large enough space.
251 omsk = ~((1 << (8 - po->nboff)) - 1);
252 off = (po->nboff + obits);
254 /* Clear data of debris before meaningful bits */
255 bits &= (((uint32_t)1 << obits) - 1);
257 ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits,
258 (int)bits, (int)bits,
259 (int)po->nboff, (int)off,
260 buf[0], (int)(omsk&0xff),
261 (int)(buf[0] & omsk));
263 if(off <= 8) /* Completely within 1 byte */
266 buf[0] = (buf[0] & omsk) | bits;
270 buf[0] = (buf[0] & omsk) | (bits >> 8),
275 buf[0] = (buf[0] & omsk) | (bits >> 16),
281 buf[0] = (buf[0] & omsk) | (bits >> 24),
286 if(asn_put_few_bits(po, bits >> (obits - 24), 24)) return -1;
287 if(asn_put_few_bits(po, bits, obits - 24)) return -1;
290 ASN_DEBUG("[PER out %u/%x => %02x buf+%ld]",
291 (int)bits, (int)bits, buf[0],
292 (long)(po->buffer - po->tmpspace));
299 * Output a large number of bits.
302 asn_put_many_bits(asn_bit_outp_t *po, const uint8_t *src, int nbits) {
308 value = (src[0] << 16) | (src[1] << 8) | src[2];
311 if(asn_put_few_bits(po, value, 24))
316 value = (value << 8) | src[1];
318 value = (value << 8) | src[2];
320 value >>= (8 - (nbits & 0x07));
321 if(asn_put_few_bits(po, value, nbits))
332 asn_put_aligned_flush(asn_bit_outp_t *po) {
333 uint32_t unused_bits = (0x7 & (8 - (po->nboff & 0x07)));
334 size_t complete_bytes =
335 (po->buffer ? po->buffer - po->tmpspace : 0) + ((po->nboff + 7) >> 3);
338 po->buffer[po->nboff >> 3] &= ~0u << unused_bits;
341 if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0) {
344 po->buffer = po->tmpspace;
346 po->nbits = 8 * sizeof(po->tmpspace);
347 po->flushed_bytes += complete_bytes;