2 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_system.h>
7 #include <asn_internal.h>
8 #include <asn_bit_data.h>
11 * Create a contiguous non-refillable bit data structure.
12 * Can be freed by FREEMEM().
15 asn_bit_data_new_contiguous(const void *data, size_t size_bits) {
16 size_t size_bytes = (size_bits + 7) / 8;
20 /* Get the extensions map */
21 pd = CALLOC(1, sizeof(*pd) + size_bytes + 1);
25 bytes = (void *)(((char *)pd) + sizeof(*pd));
26 memcpy(bytes, data, size_bytes);
27 bytes[size_bytes] = 0;
30 pd->nbits = size_bits;
37 asn_bit_data_string(asn_bit_data_t *pd) {
38 static char buf[2][32];
41 snprintf(buf[n], sizeof(buf[n]),
42 "{m=%" ASN_PRI_SIZE " span %" ASN_PRI_SIZE "[%" ASN_PRI_SIZE
43 "..%" ASN_PRI_SIZE "] (%" ASN_PRI_SIZE ")}",
44 pd->moved, ((uintptr_t)(pd->buffer) & 0xf), pd->nboff, pd->nbits,
45 pd->nbits - pd->nboff);
50 asn_get_undo(asn_bit_data_t *pd, int nbits) {
51 if((ssize_t)pd->nboff < nbits) {
52 assert((ssize_t)pd->nboff < nbits);
60 * Extract a small number of bits (<= 31) from the specified PER data pointer.
63 asn_get_few_bits(asn_bit_data_t *pd, int nbits) {
64 size_t off; /* Next after last bit offset */
65 ssize_t nleft; /* Number of bits left in this stream */
72 nleft = pd->nbits - pd->nboff;
75 if(!pd->refill || nbits > 31) return -1;
76 /* Accumulate unused bytes before refill */
77 ASN_DEBUG("Obtain the rest %d bits (want %d)",
78 (int)nleft, (int)nbits);
79 tailv = asn_get_few_bits(pd, nleft);
80 if(tailv < 0) return -1;
81 /* Refill (replace pd contents with new data) */
85 vhead = asn_get_few_bits(pd, nbits);
86 /* Combine the rest of previous pd with the head of new one */
87 tailv = (tailv << nbits) | vhead; /* Could == -1 */
92 * Normalize position indicator.
95 pd->buffer += (pd->nboff >> 3);
96 pd->nbits -= (pd->nboff & ~0x07);
105 * Extract specified number of bits.
108 accum = nbits ? (buf[0]) >> (8 - off) : 0;
110 accum = ((buf[0] << 8) + buf[1]) >> (16 - off);
112 accum = ((buf[0] << 16) + (buf[1] << 8) + buf[2]) >> (24 - off);
114 accum = (((uint32_t)buf[0] << 24) + (buf[1] << 16)
115 + (buf[2] << 8) + (buf[3])) >> (32 - off);
116 else if(nbits <= 31) {
117 asn_bit_data_t tpd = *pd;
118 /* Here are we with our 31-bits limit plus 1..7 bits offset. */
119 asn_get_undo(&tpd, nbits);
120 /* The number of available bits in the stream allow
121 * for the following operations to take place without
122 * invoking the ->refill() function */
123 accum = asn_get_few_bits(&tpd, nbits - 24) << 24;
124 accum |= asn_get_few_bits(&tpd, 24);
126 asn_get_undo(pd, nbits);
130 accum &= (((uint32_t)1 << nbits) - 1);
132 ASN_DEBUG(" [PER got %2d<=%2d bits => span %d %+ld[%d..%d]:%02x (%d) => 0x%x]",
133 (int)nbits, (int)nleft,
135 (((long)pd->buffer) & 0xf),
136 (int)pd->nboff, (int)pd->nbits,
137 ((pd->buffer != NULL)?pd->buffer[0]:0),
138 (int)(pd->nbits - pd->nboff),
145 * Extract a large number of bits from the specified PER data pointer.
148 asn_get_many_bits(asn_bit_data_t *pd, uint8_t *dst, int alright, int nbits) {
151 if(alright && (nbits & 7)) {
152 /* Perform right alignment of a first few bits */
153 value = asn_get_few_bits(pd, nbits & 0x07);
154 if(value < 0) return -1;
155 *dst++ = value; /* value is already right-aligned */
161 value = asn_get_few_bits(pd, 24);
162 if(value < 0) return -1;
163 *(dst++) = value >> 16;
164 *(dst++) = value >> 8;
168 value = asn_get_few_bits(pd, nbits);
169 if(value < 0) return -1;
170 if(nbits & 7) { /* implies left alignment */
171 value <<= 8 - (nbits & 7),
172 nbits += 8 - (nbits & 7);
174 *dst++ = value >> 24;
177 *dst++ = value >> 16;
189 * Put a small number of bits (<= 31).
192 asn_put_few_bits(asn_bit_outp_t *po, uint32_t bits, int obits) {
193 size_t off; /* Next after last bit offset */
194 size_t omsk; /* Existing last byte meaningful bits mask */
197 if(obits <= 0 || obits >= 32) return obits ? -1 : 0;
199 ASN_DEBUG("[PER put %d bits %x to %p+%d bits]",
200 obits, (int)bits, (void *)po->buffer, (int)po->nboff);
203 * Normalize position indicator.
206 po->buffer += (po->nboff >> 3);
207 po->nbits -= (po->nboff & ~0x07);
212 * Flush whole-bytes output, if necessary.
214 if(po->nboff + obits > po->nbits) {
215 size_t complete_bytes;
216 if(!po->buffer) po->buffer = po->tmpspace;
217 complete_bytes = (po->buffer - po->tmpspace);
218 ASN_DEBUG("[PER output %ld complete + %ld]",
219 (long)complete_bytes, (long)po->flushed_bytes);
220 if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0)
223 po->tmpspace[0] = po->buffer[0];
224 po->buffer = po->tmpspace;
225 po->nbits = 8 * sizeof(po->tmpspace);
226 po->flushed_bytes += complete_bytes;
230 * Now, due to sizeof(tmpspace), we are guaranteed large enough space.
233 omsk = ~((1 << (8 - po->nboff)) - 1);
234 off = (po->nboff + obits);
236 /* Clear data of debris before meaningful bits */
237 bits &= (((uint32_t)1 << obits) - 1);
239 ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits,
240 (int)bits, (int)bits,
241 (int)po->nboff, (int)off,
242 buf[0], (int)(omsk&0xff),
243 (int)(buf[0] & omsk));
245 if(off <= 8) /* Completely within 1 byte */
248 buf[0] = (buf[0] & omsk) | bits;
252 buf[0] = (buf[0] & omsk) | (bits >> 8),
257 buf[0] = (buf[0] & omsk) | (bits >> 16),
263 buf[0] = (buf[0] & omsk) | (bits >> 24),
268 if(asn_put_few_bits(po, bits >> (obits - 24), 24)) return -1;
269 if(asn_put_few_bits(po, bits, obits - 24)) return -1;
272 ASN_DEBUG("[PER out %u/%x => %02x buf+%ld]",
273 (int)bits, (int)bits, buf[0],
274 (long)(po->buffer - po->tmpspace));
281 * Output a large number of bits.
284 asn_put_many_bits(asn_bit_outp_t *po, const uint8_t *src, int nbits) {
290 value = (src[0] << 16) | (src[1] << 8) | src[2];
293 if(asn_put_few_bits(po, value, 24))
298 value = (value << 8) | src[1];
300 value = (value << 8) | src[2];
302 value >>= (8 - (nbits & 0x07));
303 if(asn_put_few_bits(po, value, nbits))
314 asn_put_aligned_flush(asn_bit_outp_t *po) {
315 uint32_t unused_bits = (0x7 & (8 - (po->nboff & 0x07)));
316 size_t complete_bytes =
317 (po->buffer ? po->buffer - po->tmpspace : 0) + ((po->nboff + 7) >> 3);
320 po->buffer[po->nboff >> 3] &= ~0u << unused_bits;
323 if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0) {
326 po->buffer = po->tmpspace;
328 po->nbits = 8 * sizeof(po->tmpspace);
329 po->flushed_bytes += complete_bytes;