Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/libfreerdp/codec/rfx_bitstream.h
Line
Count
Source (jump to first uncovered line)
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
  static inline uint32_t rfx_bitstream_get_bits(RFX_BITSTREAM* bs, uint32_t nbits)
54
0
  {
55
0
    UINT16 n = 0;
56
0
    while (bs->byte_pos < bs->nbytes && nbits > 0)
57
0
    {
58
0
      uint32_t b = nbits;
59
0
      if (b > bs->bits_left)
60
0
        b = bs->bits_left;
61
0
      if (n)
62
0
        n <<= b;
63
0
      n |= (bs->buffer[bs->byte_pos] >> (bs->bits_left - b)) & ((1 << b) - 1);
64
0
      bs->bits_left -= b;
65
0
      nbits -= b;
66
0
      if (bs->bits_left == 0)
67
0
      {
68
0
        bs->bits_left = 8;
69
0
        bs->byte_pos++;
70
0
      }
71
0
    }
72
0
    return n;
73
0
  }
74
75
  static inline void rfx_bitstream_put_bits(RFX_BITSTREAM* bs, uint32_t _bits, uint32_t _nbits)
76
0
  {
77
0
    UINT16 bits = WINPR_ASSERTING_INT_CAST(UINT16, _bits);
78
79
0
    uint32_t nbits = (_nbits);
80
0
    while (bs->byte_pos < bs->nbytes && nbits > 0)
81
0
    {
82
0
      uint32_t b = nbits;
83
0
      if (b > bs->bits_left)
84
0
        b = bs->bits_left;
85
0
      bs->buffer[bs->byte_pos] |= ((bits >> (nbits - b)) & ((1 << b) - 1))
86
0
                                  << (bs->bits_left - b);
87
0
      bs->bits_left -= b;
88
0
      nbits -= b;
89
0
      if (bs->bits_left == 0)
90
0
      {
91
0
        bs->bits_left = 8;
92
0
        bs->byte_pos++;
93
0
      }
94
0
    }
95
0
  }
96
97
  static inline void rfx_bitstream_flush(RFX_BITSTREAM* bs)
98
0
  {
99
0
    WINPR_ASSERT(bs);
100
0
    if (bs->bits_left != 8)
101
0
    {
102
0
      uint32_t _nbits = 8 - bs->bits_left;
103
0
      rfx_bitstream_put_bits(bs, 0, _nbits);
104
0
    }
105
0
  }
106
107
  static inline BOOL rfx_bitstream_eos(RFX_BITSTREAM* bs)
108
0
  {
109
0
    WINPR_ASSERT(bs);
110
0
    return ((bs)->byte_pos >= (bs)->nbytes);
111
0
  }
112
113
  static inline uint32_t rfx_bitstream_left(RFX_BITSTREAM* bs)
114
0
  {
115
0
    WINPR_ASSERT(bs);
116
0
117
0
    if ((bs)->byte_pos >= (bs)->nbytes)
118
0
      return 0;
119
0
120
0
    return ((bs)->nbytes - (bs)->byte_pos - 1) * 8 + (bs)->bits_left;
121
0
  }
122
123
  static inline uint32_t rfx_bitstream_get_processed_bytes(RFX_BITSTREAM* bs)
124
0
  {
125
0
    WINPR_ASSERT(bs);
126
0
    if ((bs)->bits_left < 8)
127
0
      return (bs)->byte_pos + 1;
128
0
    return (bs)->byte_pos;
129
0
  }
130
131
#ifdef __cplusplus
132
}
133
#endif
134
#endif /* FREERDP_LIB_CODEC_RFX_BITSTREAM_H */