X-Git-Url: https://git.sesse.net/?p=fjl;a=blobdiff_plain;f=bitsource.h;h=ae29b4c4a2200abce210242a541be2c289ea8163;hp=6cabbf004245139d6f3f5a8f5f5fac5cfdb646e6;hb=9069ae93f3e9e9b44f5f28968f508d09f0711737;hpb=352c52d38c212a2aa93a9970731c9d7a2f8d4dac diff --git a/bitsource.h b/bitsource.h index 6cabbf0..ae29b4c 100644 --- a/bitsource.h +++ b/bitsource.h @@ -34,14 +34,21 @@ struct bit_source { // When this is empty, it needs to be refilled from the input // stream. uint8_t* bytes; + uint8_t* byte_read_ptr; unsigned bytes_available; + // Some clients will purposedly read a bit ahead of the stream, causing + // problems at EOF. Thus, the client is allowed to request that we pad + // the end stream with a few bytes after the source reports EOF. + int padding_bytes_available; + // Data source. input_func_t* input_func; void* userdata; }; -void init_bit_source(struct bit_source* source, input_func_t* input_func, void* userdata); +void init_bit_source(struct bit_source* source, input_func_t* input_func, + unsigned padding_bytes, void* userdata); // Internal function. Do not use. void possibly_refill_slow_path(struct bit_source* source, unsigned num_bits); @@ -62,8 +69,8 @@ static inline void possibly_refill(struct bit_source* source, unsigned num_bits) // Slower path (~99% of remaining invocations?) assert(source->bits_available + BITRESERVOIR_FILL_SIZE < BITRESERVOIR_SIZE); if (source->bytes_available >= sizeof(bitreservoir_fill_t)) { - bitreservoir_fill_t fill = read_bitreservoir_fill(source->bytes); - source->bytes += sizeof(bitreservoir_fill_t); + bitreservoir_fill_t fill = read_bitreservoir_fill(source->byte_read_ptr); + source->byte_read_ptr += sizeof(bitreservoir_fill_t); source->bytes_available -= sizeof(bitreservoir_fill_t); source->bits |= (bitreservoir_t)fill << (BITRESERVOIR_SIZE - BITRESERVOIR_FILL_SIZE - source->bits_available); source->bits_available += BITRESERVOIR_FILL_SIZE;