20#ifndef FREERDP_LIB_CODEC_RFX_BITSTREAM_H
21#define FREERDP_LIB_CODEC_RFX_BITSTREAM_H
23#include <winpr/assert.h>
24#include <winpr/cast.h>
26#include <freerdp/codec/rfx.h>
41 static inline void rfx_bitstream_attach(
RFX_BITSTREAM* bs, BYTE* WINPR_RESTRICT buffer,
45 bs->buffer = (buffer);
47 WINPR_ASSERT(nbytes <= UINT32_MAX);
48 bs->nbytes = WINPR_ASSERTING_INT_CAST(uint32_t, nbytes);
53 static inline uint32_t rfx_bitstream_get_bits(
RFX_BITSTREAM* bs, uint32_t nbits)
56 while (bs->byte_pos < bs->nbytes && nbits > 0)
59 if (b > bs->bits_left)
63 n |= (bs->buffer[bs->byte_pos] >> (bs->bits_left - b)) & ((1 << b) - 1);
66 if (bs->bits_left == 0)
75 static inline void rfx_bitstream_put_bits(
RFX_BITSTREAM* bs, uint32_t _bits, uint32_t _nbits)
77 UINT16 bits = WINPR_ASSERTING_INT_CAST(UINT16, _bits);
79 uint32_t nbits = (_nbits);
80 while (bs->byte_pos < bs->nbytes && nbits > 0)
83 if (b > bs->bits_left)
85 bs->buffer[bs->byte_pos] |= ((bits >> (nbits - b)) & ((1 << b) - 1))
86 << (bs->bits_left - b);
89 if (bs->bits_left == 0)
100 if (bs->bits_left != 8)
102 uint32_t _nbits = 8 - bs->bits_left;
103 rfx_bitstream_put_bits(bs, 0, _nbits);
110 return ((bs)->byte_pos >= (bs)->nbytes);
117 if ((bs)->byte_pos >= (bs)->nbytes)
120 return ((bs)->nbytes - (bs)->byte_pos - 1) * 8 + (bs)->bits_left;
123 static inline uint32_t rfx_bitstream_get_processed_bytes(
RFX_BITSTREAM* bs)
126 if ((bs)->bits_left < 8)
127 return (bs)->byte_pos + 1;
128 return (bs)->byte_pos;