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);
54 static inline uint32_t rfx_bitstream_get_bits(
RFX_BITSTREAM* bs, uint32_t nbits)
57 while (bs->byte_pos < bs->nbytes && nbits > 0)
60 if (b > bs->bits_left)
64 n |= (bs->buffer[bs->byte_pos] >> (bs->bits_left - b)) & ((1 << b) - 1);
67 if (bs->bits_left == 0)
76 static inline void rfx_bitstream_put_bits(
RFX_BITSTREAM* bs, uint32_t _bits, uint32_t _nbits)
78 UINT16 bits = WINPR_ASSERTING_INT_CAST(UINT16, _bits);
80 uint32_t nbits = (_nbits);
81 while (bs->byte_pos < bs->nbytes && nbits > 0)
84 if (b > bs->bits_left)
86 bs->buffer[bs->byte_pos] |= ((bits >> (nbits - b)) & ((1 << b) - 1))
87 << (bs->bits_left - b);
90 if (bs->bits_left == 0)
101 if (bs->bits_left != 8)
103 uint32_t _nbits = 8 - bs->bits_left;
104 rfx_bitstream_put_bits(bs, 0, _nbits);
112 return ((bs)->byte_pos >= (bs)->nbytes);
120 if ((bs)->byte_pos >= (bs)->nbytes)
123 return ((bs)->nbytes - (bs)->byte_pos - 1) * 8 + (bs)->bits_left;
127 static inline uint32_t rfx_bitstream_get_processed_bytes(
RFX_BITSTREAM* bs)
130 if ((bs)->bits_left < 8)
131 return (bs)->byte_pos + 1;
132 return (bs)->byte_pos;