3 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>.
5 * Redistribution and modifications are permitted subject to BSD license.
7 #include <asn_system.h>
8 #include <asn_internal.h>
9 #include <asn_bit_data.h>
12 * Create a contiguous non-refillable bit data structure.
13 * Can be freed by FREEMEM().
16 asn_bit_data_new_contiguous(const void *data, size_t size_bits) {
17 size_t size_bytes = (size_bits + 7) / 8;
21 /* Get the extensions map */
22 pd = CALLOC(1, sizeof(*pd) + size_bytes + 1);
26 bytes = (void *)(((char *)pd) + sizeof(*pd));
27 memcpy(bytes, data, size_bytes);
28 bytes[size_bytes] = 0;
31 pd->nbits = size_bits;
38 asn_bit_data_string(asn_bit_data_t *pd) {
39 static char buf[2][32];
42 snprintf(buf[n], sizeof(buf[n]),
43 "{m=%" ASN_PRI_SIZE " span %" ASN_PRI_SIZE "[%" ASN_PRI_SIZE
44 "..%" ASN_PRI_SIZE "] (%" ASN_PRI_SIZE ")}",
45 pd->moved, ((uintptr_t)(pd->buffer) & 0xf), pd->nboff, pd->nbits,
46 pd->nbits - pd->nboff);
51 asn_get_undo(asn_bit_data_t *pd, int nbits) {
52 if((ssize_t)pd->nboff < nbits) {
53 assert((ssize_t)pd->nboff < nbits);
61 * Extract a small number of bits (<= 31) from the specified PER data pointer.
64 asn_get_few_bits(asn_bit_data_t *pd, int nbits) {
65 size_t off; /* Next after last bit offset */
66 ssize_t nleft; /* Number of bits left in this stream */
73 nleft = pd->nbits - pd->nboff;
76 if(!pd->refill || nbits > 31) return -1;
77 /* Accumulate unused bytes before refill */
78 ASN_DEBUG("Obtain the rest %d bits (want %d)",
79 (int)nleft, (int)nbits);
80 tailv = asn_get_few_bits(pd, nleft);
81 if(tailv < 0) return -1;
82 /* Refill (replace pd contents with new data) */
86 vhead = asn_get_few_bits(pd, nbits);
87 /* Combine the rest of previous pd with the head of new one */
88 tailv = (tailv << nbits) | vhead; /* Could == -1 */
93 * Normalize position indicator.
96 pd->buffer += (pd->nboff >> 3);
97 pd->nbits -= (pd->nboff & ~0x07);
106 * Extract specified number of bits.
109 accum = nbits ? (buf[0]) >> (8 - off) : 0;
111 accum = ((buf[0] << 8) + buf[1]) >> (16 - off);
113 accum = ((buf[0] << 16) + (buf[1] << 8) + buf[2]) >> (24 - off);
115 accum = (((uint32_t)buf[0] << 24) + (buf[1] << 16)
116 + (buf[2] << 8) + (buf[3])) >> (32 - off);
117 else if(nbits <= 31) {
118 asn_bit_data_t tpd = *pd;
119 /* Here are we with our 31-bits limit plus 1..7 bits offset. */
120 asn_get_undo(&tpd, nbits);
121 /* The number of available bits in the stream allow
122 * for the following operations to take place without
123 * invoking the ->refill() function */
124 accum = asn_get_few_bits(&tpd, nbits - 24) << 24;
125 accum |= asn_get_few_bits(&tpd, 24);
127 asn_get_undo(pd, nbits);
131 accum &= (((uint32_t)1 << nbits) - 1);
133 ASN_DEBUG(" [PER got %2d<=%2d bits => span %d %+ld[%d..%d]:%02x (%d) => 0x%x]",
134 (int)nbits, (int)nleft,
136 (((long)pd->buffer) & 0xf),
137 (int)pd->nboff, (int)pd->nbits,
138 ((pd->buffer != NULL)?pd->buffer[0]:0),
139 (int)(pd->nbits - pd->nboff),
146 * Extract a large number of bits from the specified PER data pointer.
149 asn_get_many_bits(asn_bit_data_t *pd, uint8_t *dst, int alright, int nbits) {
152 if(alright && (nbits & 7)) {
153 /* Perform right alignment of a first few bits */
154 value = asn_get_few_bits(pd, nbits & 0x07);
155 if(value < 0) return -1;
156 *dst++ = value; /* value is already right-aligned */
162 value = asn_get_few_bits(pd, 24);
163 if(value < 0) return -1;
164 *(dst++) = value >> 16;
165 *(dst++) = value >> 8;
169 value = asn_get_few_bits(pd, nbits);
170 if(value < 0) return -1;
171 if(nbits & 7) { /* implies left alignment */
172 value <<= 8 - (nbits & 7),
173 nbits += 8 - (nbits & 7);
175 *dst++ = value >> 24;
178 *dst++ = value >> 16;
190 * Put a small number of bits (<= 31).
193 asn_put_few_bits(asn_bit_outp_t *po, uint32_t bits, int obits) {
194 size_t off; /* Next after last bit offset */
195 size_t omsk; /* Existing last byte meaningful bits mask */
198 if(obits <= 0 || obits >= 32) return obits ? -1 : 0;
200 ASN_DEBUG("[PER put %d bits %x to %p+%d bits]",
201 obits, (int)bits, (void *)po->buffer, (int)po->nboff);
204 * Normalize position indicator.
207 po->buffer += (po->nboff >> 3);
208 po->nbits -= (po->nboff & ~0x07);
213 * Flush whole-bytes output, if necessary.
215 if(po->nboff + obits > po->nbits) {
216 size_t complete_bytes;
217 if(!po->buffer) po->buffer = po->tmpspace;
218 complete_bytes = (po->buffer - po->tmpspace);
219 ASN_DEBUG("[PER output %ld complete + %ld]",
220 (long)complete_bytes, (long)po->flushed_bytes);
221 if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0)
224 po->tmpspace[0] = po->buffer[0];
225 po->buffer = po->tmpspace;
226 po->nbits = 8 * sizeof(po->tmpspace);
227 po->flushed_bytes += complete_bytes;
231 * Now, due to sizeof(tmpspace), we are guaranteed large enough space.
234 omsk = ~((1 << (8 - po->nboff)) - 1);
235 off = (po->nboff + obits);
237 /* Clear data of debris before meaningful bits */
238 bits &= (((uint32_t)1 << obits) - 1);
240 ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits,
241 (int)bits, (int)bits,
242 (int)po->nboff, (int)off,
243 buf[0], (int)(omsk&0xff),
244 (int)(buf[0] & omsk));
246 if(off <= 8) /* Completely within 1 byte */
249 buf[0] = (buf[0] & omsk) | bits;
253 buf[0] = (buf[0] & omsk) | (bits >> 8),
258 buf[0] = (buf[0] & omsk) | (bits >> 16),
264 buf[0] = (buf[0] & omsk) | (bits >> 24),
269 if(asn_put_few_bits(po, bits >> (obits - 24), 24)) return -1;
270 if(asn_put_few_bits(po, bits, obits - 24)) return -1;
273 ASN_DEBUG("[PER out %u/%x => %02x buf+%ld]",
274 (int)bits, (int)bits, buf[0],
275 (long)(po->buffer - po->tmpspace));
282 * Output a large number of bits.
285 asn_put_many_bits(asn_bit_outp_t *po, const uint8_t *src, int nbits) {
291 value = (src[0] << 16) | (src[1] << 8) | src[2];
294 if(asn_put_few_bits(po, value, 24))
299 value = (value << 8) | src[1];
301 value = (value << 8) | src[2];
303 value >>= (8 - (nbits & 0x07));
304 if(asn_put_few_bits(po, value, nbits))
315 asn_put_aligned_flush(asn_bit_outp_t *po) {
316 uint32_t unused_bits = (0x7 & (8 - (po->nboff & 0x07)));
317 size_t complete_bytes =
318 (po->buffer ? po->buffer - po->tmpspace : 0) + ((po->nboff + 7) >> 3);
321 po->buffer[po->nboff >> 3] &= ~0u << unused_bits;
324 if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0) {
327 po->buffer = po->tmpspace;
329 po->nbits = 8 * sizeof(po->tmpspace);
330 po->flushed_bytes += complete_bytes;