-per_put_few_bits(asn_per_outp_t *po, uint32_t bits, int obits) {
- size_t off; /* Next after last bit offset */
- size_t omsk; /* Existing last byte meaningful bits mask */
- uint8_t *buf;
-
- if(obits <= 0 || obits >= 32) return obits ? -1 : 0;
-
- ASN_DEBUG("[PER put %d bits %x to %p+%d bits]",
- obits, (int)bits, po->buffer, (int)po->nboff);
-
- /*
- * Normalize position indicator.
- */
- if(po->nboff >= 8) {
- po->buffer += (po->nboff >> 3);
- po->nbits -= (po->nboff & ~0x07);
- po->nboff &= 0x07;
- }
-
- /*
- * Flush whole-bytes output, if necessary.
- */
- if(po->nboff + obits > po->nbits) {
- int complete_bytes = (po->buffer - po->tmpspace);
- ASN_DEBUG("[PER output %ld complete + %ld]",
- (long)complete_bytes, (long)po->flushed_bytes);
- if(po->outper(po->tmpspace, complete_bytes, po->op_key) < 0)
- return -1;
- if(po->nboff)
- po->tmpspace[0] = po->buffer[0];
- po->buffer = po->tmpspace;
- po->nbits = 8 * sizeof(po->tmpspace);
- po->flushed_bytes += complete_bytes;
- }
-
- /*
- * Now, due to sizeof(tmpspace), we are guaranteed large enough space.
- */
- buf = po->buffer;
- omsk = ~((1 << (8 - po->nboff)) - 1);
- off = (po->nboff + obits);
-
- /* Clear data of debris before meaningful bits */
- bits &= (((uint32_t)1 << obits) - 1);
-
- ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits,
- (int)bits, (int)bits,
- (int)po->nboff, (int)off,
- buf[0], (int)(omsk&0xff),
- (int)(buf[0] & omsk));
-
- if(off <= 8) /* Completely within 1 byte */
- po->nboff = off,
- bits <<= (8 - off),
- buf[0] = (buf[0] & omsk) | bits;
- else if(off <= 16)
- po->nboff = off,
- bits <<= (16 - off),
- buf[0] = (buf[0] & omsk) | (bits >> 8),
- buf[1] = bits;
- else if(off <= 24)
- po->nboff = off,
- bits <<= (24 - off),
- buf[0] = (buf[0] & omsk) | (bits >> 16),
- buf[1] = bits >> 8,
- buf[2] = bits;
- else if(off <= 31)
- po->nboff = off,
- bits <<= (32 - off),
- buf[0] = (buf[0] & omsk) | (bits >> 24),
- buf[1] = bits >> 16,
- buf[2] = bits >> 8,
- buf[3] = bits;
- else {
- per_put_few_bits(po, bits >> (obits - 24), 24);
- per_put_few_bits(po, bits, obits - 24);
- }
-
- ASN_DEBUG("[PER out %u/%x => %02x buf+%ld]",
- (int)bits, (int)bits, buf[0],
- (long)(po->buffer - po->tmpspace));
-
- return 0;
-}
-
-
-/*
- * Output a large number of bits.
- */
-int
-per_put_many_bits(asn_per_outp_t *po, const uint8_t *src, int nbits) {
-
- while(nbits) {
- uint32_t value;
-
- if(nbits >= 24) {
- value = (src[0] << 16) | (src[1] << 8) | src[2];
- src += 3;
- nbits -= 24;
- if(per_put_few_bits(po, value, 24))
- return -1;
- } else {
- value = src[0];
- if(nbits > 8)
- value = (value << 8) | src[1];
- if(nbits > 16)
- value = (value << 8) | src[2];
- if(nbits & 0x07)
- value >>= (8 - (nbits & 0x07));
- if(per_put_few_bits(po, value, nbits))
- return -1;
- break;
- }
- }
-
- return 0;