Coverage Report

Created: 2026-02-26 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/rfx_bitstream.h
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * RemoteFX Codec Library - Bit Stream
4
 *
5
 * Copyright 2011 Vic Lee
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#ifndef FREERDP_LIB_CODEC_RFX_BITSTREAM_H
21
#define FREERDP_LIB_CODEC_RFX_BITSTREAM_H
22
23
#include <winpr/assert.h>
24
#include <winpr/cast.h>
25
26
#include <freerdp/codec/rfx.h>
27
28
#ifdef __cplusplus
29
extern "C"
30
{
31
#endif
32
33
  typedef struct
34
  {
35
    BYTE* buffer;
36
    uint32_t nbytes;
37
    uint32_t byte_pos;
38
    uint32_t bits_left;
39
  } RFX_BITSTREAM;
40
41
  static inline void rfx_bitstream_attach(RFX_BITSTREAM* bs, BYTE* WINPR_RESTRICT buffer,
42
                                          size_t nbytes)
43
0
  {
44
0
    WINPR_ASSERT(bs);
45
0
    bs->buffer = (buffer);
46
47
0
    WINPR_ASSERT(nbytes <= UINT32_MAX);
48
0
    bs->nbytes = WINPR_ASSERTING_INT_CAST(uint32_t, nbytes);
49
0
    bs->byte_pos = 0;
50
0
    bs->bits_left = 8;
51
0
  }
52
53
  WINPR_ATTR_NODISCARD
54
  static inline uint32_t rfx_bitstream_get_bits(RFX_BITSTREAM* bs, uint32_t nbits)
55
0
  {
56
0
    UINT16 n = 0;
57
0
    while (bs->byte_pos < bs->nbytes && nbits > 0)
58
0
    {
59
0
      uint32_t b = nbits;
60
0
      if (b > bs->bits_left)
61
0
        b = bs->bits_left;
62
0
      if (n)
63
0
        n <<= b;
64
0
      n |= (bs->buffer[bs->byte_pos] >> (bs->bits_left - b)) & ((1 << b) - 1);
65
0
      bs->bits_left -= b;
66
0
      nbits -= b;
67
0
      if (bs->bits_left == 0)
68
0
      {
69
0
        bs->bits_left = 8;
70
0
        bs->byte_pos++;
71
0
      }
72
0
    }
73
0
    return n;
74
0
  }
75
76
  static inline void rfx_bitstream_put_bits(RFX_BITSTREAM* bs, uint32_t _bits, uint32_t _nbits)
77
0
  {
78
0
    UINT16 bits = WINPR_ASSERTING_INT_CAST(UINT16, _bits);
79
80
0
    uint32_t nbits = (_nbits);
81
0
    while (bs->byte_pos < bs->nbytes && nbits > 0)
82
0
    {
83
0
      uint32_t b = nbits;
84
0
      if (b > bs->bits_left)
85
0
        b = bs->bits_left;
86
0
      bs->buffer[bs->byte_pos] |= ((bits >> (nbits - b)) & ((1 << b) - 1))
87
0
                                  << (bs->bits_left - b);
88
0
      bs->bits_left -= b;
89
0
      nbits -= b;
90
0
      if (bs->bits_left == 0)
91
0
      {
92
0
        bs->bits_left = 8;
93
0
        bs->byte_pos++;
94
0
      }
95
0
    }
96
0
  }
97
98
  static inline void rfx_bitstream_flush(RFX_BITSTREAM* bs)
99
0
  {
100
0
    WINPR_ASSERT(bs);
101
0
    if (bs->bits_left != 8)
102
0
    {
103
0
      uint32_t _nbits = 8 - bs->bits_left;
104
0
      rfx_bitstream_put_bits(bs, 0, _nbits);
105
0
    }
106
0
  }
107
108
  WINPR_ATTR_NODISCARD
109
  static inline BOOL rfx_bitstream_eos(RFX_BITSTREAM* bs)
110
0
  {
111
0
    WINPR_ASSERT(bs);
112
0
    return ((bs)->byte_pos >= (bs)->nbytes);
113
0
  }
114
115
  WINPR_ATTR_NODISCARD
116
  static inline uint32_t rfx_bitstream_left(RFX_BITSTREAM* bs)
117
0
  {
118
0
    WINPR_ASSERT(bs);
119
0
120
0
    if ((bs)->byte_pos >= (bs)->nbytes)
121
0
      return 0;
122
0
123
0
    return ((bs)->nbytes - (bs)->byte_pos - 1) * 8 + (bs)->bits_left;
124
0
  }
125
126
  WINPR_ATTR_NODISCARD
127
  static inline uint32_t rfx_bitstream_get_processed_bytes(RFX_BITSTREAM* bs)
128
0
  {
129
0
    WINPR_ASSERT(bs);
130
0
    if ((bs)->bits_left < 8)
131
0
      return (bs)->byte_pos + 1;
132
0
    return (bs)->byte_pos;
133
0
  }
134
135
#ifdef __cplusplus
136
}
137
#endif
138
#endif /* FREERDP_LIB_CODEC_RFX_BITSTREAM_H */