Coverage Report

Created: 2025-08-03 07:06

/src/FreeRDP/winpr/include/winpr/bitstream.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * WinPR: Windows Portable Runtime
3
 * BitStream Utils
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
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 WINPR_UTILS_BITSTREAM_H
21
#define WINPR_UTILS_BITSTREAM_H
22
23
#include <winpr/assert.h>
24
#include <winpr/winpr.h>
25
#include <winpr/wtypes.h>
26
27
#include <winpr/crt.h>
28
#include <winpr/wlog.h>
29
30
typedef struct
31
{
32
  const BYTE* buffer;
33
  BYTE* pointer;
34
  UINT32 position;
35
  UINT32 length;
36
  UINT32 capacity;
37
  UINT32 mask;
38
  UINT32 offset;
39
  UINT32 prefetch;
40
  UINT32 accumulator;
41
} wBitStream;
42
43
0
#define BITDUMP_MSB_FIRST 0x00000001
44
#define BITDUMP_STDERR 0x00000002
45
46
#ifdef __cplusplus
47
extern "C"
48
{
49
#endif
50
51
  static INLINE void BitStream_Prefetch(wBitStream* _bs)
52
51.8M
  {
53
51.8M
    WINPR_ASSERT(_bs);
54
55
51.8M
    (_bs->prefetch) = 0;
56
57
51.8M
    const intptr_t diff = _bs->pointer - _bs->buffer;
58
51.8M
    if ((diff + 4) < (intptr_t)_bs->capacity)
59
8.89M
      (_bs->prefetch) |= ((UINT32)_bs->pointer[4] << 24);
60
51.8M
    if ((diff + 5) < (intptr_t)_bs->capacity)
61
8.88M
      (_bs->prefetch) |= ((UINT32)_bs->pointer[5] << 16);
62
51.8M
    if ((diff + 6) < (intptr_t)_bs->capacity)
63
8.88M
      (_bs->prefetch) |= ((UINT32)_bs->pointer[6] << 8);
64
51.8M
    if ((diff + 7) < (intptr_t)_bs->capacity)
65
8.88M
      (_bs->prefetch) |= ((UINT32)_bs->pointer[7] << 0);
66
51.8M
  }
Unexecuted instantiation: TestFuzzCodecs.c:BitStream_Prefetch
Unexecuted instantiation: progressive.c:BitStream_Prefetch
Unexecuted instantiation: ncrush.c:BitStream_Prefetch
Unexecuted instantiation: xcrush.c:BitStream_Prefetch
mppc.c:BitStream_Prefetch
Line
Count
Source
52
51.8M
  {
53
51.8M
    WINPR_ASSERT(_bs);
54
55
51.8M
    (_bs->prefetch) = 0;
56
57
51.8M
    const intptr_t diff = _bs->pointer - _bs->buffer;
58
51.8M
    if ((diff + 4) < (intptr_t)_bs->capacity)
59
8.89M
      (_bs->prefetch) |= ((UINT32)_bs->pointer[4] << 24);
60
51.8M
    if ((diff + 5) < (intptr_t)_bs->capacity)
61
8.88M
      (_bs->prefetch) |= ((UINT32)_bs->pointer[5] << 16);
62
51.8M
    if ((diff + 6) < (intptr_t)_bs->capacity)
63
8.88M
      (_bs->prefetch) |= ((UINT32)_bs->pointer[6] << 8);
64
51.8M
    if ((diff + 7) < (intptr_t)_bs->capacity)
65
8.88M
      (_bs->prefetch) |= ((UINT32)_bs->pointer[7] << 0);
66
51.8M
  }
Unexecuted instantiation: zgfx.c:BitStream_Prefetch
Unexecuted instantiation: clear.c:BitStream_Prefetch
Unexecuted instantiation: rfx_rlgr.c:BitStream_Prefetch
Unexecuted instantiation: BitStream.c:BitStream_Prefetch
67
68
  static INLINE void BitStream_Fetch(wBitStream* _bs)
69
20.9k
  {
70
20.9k
    WINPR_ASSERT(_bs);
71
20.9k
    (_bs->accumulator) = 0;
72
73
20.9k
    const intptr_t diff = _bs->pointer - _bs->buffer;
74
20.9k
    if ((diff + 0) < (intptr_t)_bs->capacity)
75
20.9k
      (_bs->accumulator) |= ((UINT32)_bs->pointer[0] << 24);
76
20.9k
    if ((diff + 1) < (intptr_t)_bs->capacity)
77
20.9k
      (_bs->accumulator) |= ((UINT32)_bs->pointer[1] << 16);
78
20.9k
    if ((diff + 2) < (intptr_t)_bs->capacity)
79
20.6k
      (_bs->accumulator) |= ((UINT32)_bs->pointer[2] << 8);
80
20.9k
    if ((diff + 3) < (intptr_t)_bs->capacity)
81
20.5k
      (_bs->accumulator) |= ((UINT32)_bs->pointer[3] << 0);
82
20.9k
    BitStream_Prefetch(_bs);
83
20.9k
  }
Unexecuted instantiation: TestFuzzCodecs.c:BitStream_Fetch
Unexecuted instantiation: progressive.c:BitStream_Fetch
Unexecuted instantiation: ncrush.c:BitStream_Fetch
Unexecuted instantiation: xcrush.c:BitStream_Fetch
mppc.c:BitStream_Fetch
Line
Count
Source
69
20.9k
  {
70
20.9k
    WINPR_ASSERT(_bs);
71
20.9k
    (_bs->accumulator) = 0;
72
73
20.9k
    const intptr_t diff = _bs->pointer - _bs->buffer;
74
20.9k
    if ((diff + 0) < (intptr_t)_bs->capacity)
75
20.9k
      (_bs->accumulator) |= ((UINT32)_bs->pointer[0] << 24);
76
20.9k
    if ((diff + 1) < (intptr_t)_bs->capacity)
77
20.9k
      (_bs->accumulator) |= ((UINT32)_bs->pointer[1] << 16);
78
20.9k
    if ((diff + 2) < (intptr_t)_bs->capacity)
79
20.6k
      (_bs->accumulator) |= ((UINT32)_bs->pointer[2] << 8);
80
20.9k
    if ((diff + 3) < (intptr_t)_bs->capacity)
81
20.5k
      (_bs->accumulator) |= ((UINT32)_bs->pointer[3] << 0);
82
20.9k
    BitStream_Prefetch(_bs);
83
20.9k
  }
Unexecuted instantiation: zgfx.c:BitStream_Fetch
Unexecuted instantiation: clear.c:BitStream_Fetch
Unexecuted instantiation: rfx_rlgr.c:BitStream_Fetch
Unexecuted instantiation: BitStream.c:BitStream_Fetch
84
85
  static INLINE void BitStream_Flush(wBitStream* _bs)
86
0
  {
87
0
    WINPR_ASSERT(_bs);
88
0
    const intptr_t diff = _bs->pointer - _bs->buffer;
89
0
    if ((diff + 0) < (intptr_t)_bs->capacity)
90
0
      _bs->pointer[0] = (_bs->accumulator >> 24) & 0xFF;
91
0
    if ((diff + 1) < (intptr_t)_bs->capacity)
92
0
      _bs->pointer[1] = (_bs->accumulator >> 16) & 0xFF;
93
0
    if ((diff + 2) < (intptr_t)_bs->capacity)
94
0
      _bs->pointer[2] = (_bs->accumulator >> 8) & 0xFF;
95
0
    if ((diff + 3) < (intptr_t)_bs->capacity)
96
0
      _bs->pointer[3] = (_bs->accumulator >> 0) & 0xFF;
97
0
  }
Unexecuted instantiation: TestFuzzCodecs.c:BitStream_Flush
Unexecuted instantiation: progressive.c:BitStream_Flush
Unexecuted instantiation: ncrush.c:BitStream_Flush
Unexecuted instantiation: xcrush.c:BitStream_Flush
Unexecuted instantiation: mppc.c:BitStream_Flush
Unexecuted instantiation: zgfx.c:BitStream_Flush
Unexecuted instantiation: clear.c:BitStream_Flush
Unexecuted instantiation: rfx_rlgr.c:BitStream_Flush
Unexecuted instantiation: BitStream.c:BitStream_Flush
98
99
  static INLINE void BitStream_Shift(wBitStream* _bs, UINT32 _nbits)
100
207M
  {
101
207M
    WINPR_ASSERT(_bs);
102
207M
    if (_nbits == 0)
103
0
    {
104
0
    }
105
207M
    else if ((_nbits > 0) && (_nbits < 32))
106
207M
    {
107
207M
      _bs->accumulator <<= _nbits;
108
207M
      _bs->position += _nbits;
109
207M
      _bs->offset += _nbits;
110
207M
      if (_bs->offset < 32)
111
155M
      {
112
155M
        _bs->mask = (UINT32)((1UL << _nbits) - 1UL);
113
155M
        _bs->accumulator |= ((_bs->prefetch >> (32 - _nbits)) & _bs->mask);
114
155M
        _bs->prefetch <<= _nbits;
115
155M
      }
116
51.8M
      else
117
51.8M
      {
118
51.8M
        _bs->mask = (UINT32)((1UL << _nbits) - 1UL);
119
51.8M
        _bs->accumulator |= ((_bs->prefetch >> (32 - _nbits)) & _bs->mask);
120
51.8M
        _bs->prefetch <<= _nbits;
121
51.8M
        _bs->offset -= 32;
122
51.8M
        _bs->pointer += 4;
123
51.8M
        BitStream_Prefetch(_bs);
124
51.8M
        if (_bs->offset)
125
46.1M
        {
126
46.1M
          _bs->mask = (UINT32)((1UL << _bs->offset) - 1UL);
127
46.1M
          _bs->accumulator |= ((_bs->prefetch >> (32 - _bs->offset)) & _bs->mask);
128
46.1M
          _bs->prefetch <<= _bs->offset;
129
46.1M
        }
130
51.8M
      }
131
207M
    }
132
0
    else
133
0
    {
134
0
      WLog_WARN("com.winpr.bitstream", "warning: BitStream_Shift(%u)", (unsigned)_nbits);
135
0
    }
136
207M
  }
Unexecuted instantiation: TestFuzzCodecs.c:BitStream_Shift
Unexecuted instantiation: progressive.c:BitStream_Shift
Unexecuted instantiation: ncrush.c:BitStream_Shift
Unexecuted instantiation: xcrush.c:BitStream_Shift
mppc.c:BitStream_Shift
Line
Count
Source
100
207M
  {
101
207M
    WINPR_ASSERT(_bs);
102
207M
    if (_nbits == 0)
103
0
    {
104
0
    }
105
207M
    else if ((_nbits > 0) && (_nbits < 32))
106
207M
    {
107
207M
      _bs->accumulator <<= _nbits;
108
207M
      _bs->position += _nbits;
109
207M
      _bs->offset += _nbits;
110
207M
      if (_bs->offset < 32)
111
155M
      {
112
155M
        _bs->mask = (UINT32)((1UL << _nbits) - 1UL);
113
155M
        _bs->accumulator |= ((_bs->prefetch >> (32 - _nbits)) & _bs->mask);
114
155M
        _bs->prefetch <<= _nbits;
115
155M
      }
116
51.8M
      else
117
51.8M
      {
118
51.8M
        _bs->mask = (UINT32)((1UL << _nbits) - 1UL);
119
51.8M
        _bs->accumulator |= ((_bs->prefetch >> (32 - _nbits)) & _bs->mask);
120
51.8M
        _bs->prefetch <<= _nbits;
121
51.8M
        _bs->offset -= 32;
122
51.8M
        _bs->pointer += 4;
123
51.8M
        BitStream_Prefetch(_bs);
124
51.8M
        if (_bs->offset)
125
46.1M
        {
126
46.1M
          _bs->mask = (UINT32)((1UL << _bs->offset) - 1UL);
127
46.1M
          _bs->accumulator |= ((_bs->prefetch >> (32 - _bs->offset)) & _bs->mask);
128
46.1M
          _bs->prefetch <<= _bs->offset;
129
46.1M
        }
130
51.8M
      }
131
207M
    }
132
0
    else
133
0
    {
134
0
      WLog_WARN("com.winpr.bitstream", "warning: BitStream_Shift(%u)", (unsigned)_nbits);
135
0
    }
136
207M
  }
Unexecuted instantiation: zgfx.c:BitStream_Shift
Unexecuted instantiation: clear.c:BitStream_Shift
Unexecuted instantiation: rfx_rlgr.c:BitStream_Shift
Unexecuted instantiation: BitStream.c:BitStream_Shift
137
138
  static INLINE void BitStream_Shift32(wBitStream* _bs)
139
0
  {
140
0
    WINPR_ASSERT(_bs);
141
0
    BitStream_Shift(_bs, 16);
142
0
    BitStream_Shift(_bs, 16);
143
0
  }
Unexecuted instantiation: TestFuzzCodecs.c:BitStream_Shift32
Unexecuted instantiation: progressive.c:BitStream_Shift32
Unexecuted instantiation: ncrush.c:BitStream_Shift32
Unexecuted instantiation: xcrush.c:BitStream_Shift32
Unexecuted instantiation: mppc.c:BitStream_Shift32
Unexecuted instantiation: zgfx.c:BitStream_Shift32
Unexecuted instantiation: clear.c:BitStream_Shift32
Unexecuted instantiation: rfx_rlgr.c:BitStream_Shift32
Unexecuted instantiation: BitStream.c:BitStream_Shift32
144
145
  static INLINE void BitStream_Write_Bits(wBitStream* _bs, UINT32 _bits, UINT32 _nbits)
146
0
  {
147
0
    WINPR_ASSERT(_bs);
148
0
    _bs->position += _nbits;
149
0
    _bs->offset += _nbits;
150
0
    if (_bs->offset < 32)
151
0
    {
152
0
      _bs->accumulator |= (_bits << (32 - _bs->offset));
153
0
    }
154
0
    else
155
0
    {
156
0
      _bs->offset -= 32;
157
0
      _bs->mask = ((1 << (_nbits - _bs->offset)) - 1);
158
0
      _bs->accumulator |= ((_bits >> _bs->offset) & _bs->mask);
159
0
      BitStream_Flush(_bs);
160
0
      _bs->accumulator = 0;
161
0
      _bs->pointer += 4;
162
0
      if (_bs->offset)
163
0
      {
164
0
        _bs->mask = (UINT32)((1UL << _bs->offset) - 1);
165
0
        _bs->accumulator |= ((_bits & _bs->mask) << (32 - _bs->offset));
166
0
      }
167
0
    }
168
0
  }
Unexecuted instantiation: TestFuzzCodecs.c:BitStream_Write_Bits
Unexecuted instantiation: progressive.c:BitStream_Write_Bits
Unexecuted instantiation: ncrush.c:BitStream_Write_Bits
Unexecuted instantiation: xcrush.c:BitStream_Write_Bits
Unexecuted instantiation: mppc.c:BitStream_Write_Bits
Unexecuted instantiation: zgfx.c:BitStream_Write_Bits
Unexecuted instantiation: clear.c:BitStream_Write_Bits
Unexecuted instantiation: rfx_rlgr.c:BitStream_Write_Bits
Unexecuted instantiation: BitStream.c:BitStream_Write_Bits
169
170
  static INLINE size_t BitStream_GetRemainingLength(wBitStream* _bs)
171
0
  {
172
0
    WINPR_ASSERT(_bs);
173
0
    return (_bs->length - _bs->position);
174
0
  }
Unexecuted instantiation: TestFuzzCodecs.c:BitStream_GetRemainingLength
Unexecuted instantiation: progressive.c:BitStream_GetRemainingLength
Unexecuted instantiation: ncrush.c:BitStream_GetRemainingLength
Unexecuted instantiation: xcrush.c:BitStream_GetRemainingLength
Unexecuted instantiation: mppc.c:BitStream_GetRemainingLength
Unexecuted instantiation: zgfx.c:BitStream_GetRemainingLength
Unexecuted instantiation: clear.c:BitStream_GetRemainingLength
Unexecuted instantiation: rfx_rlgr.c:BitStream_GetRemainingLength
Unexecuted instantiation: BitStream.c:BitStream_GetRemainingLength
175
176
  WINPR_API void BitDump(const char* tag, UINT32 level, const BYTE* buffer, UINT32 length,
177
                         UINT32 flags);
178
  WINPR_API UINT32 ReverseBits32(UINT32 bits, UINT32 nbits);
179
180
  WINPR_API void BitStream_Attach(wBitStream* bs, const BYTE* buffer, UINT32 capacity);
181
182
  WINPR_API void BitStream_Free(wBitStream* bs);
183
184
  WINPR_ATTR_MALLOC(BitStream_Free, 1)
185
  WINPR_API wBitStream* BitStream_New(void);
186
187
#ifdef __cplusplus
188
}
189
#endif
190
191
#endif /* WINPR_UTILS_BITSTREAM_H */