Coverage Report

Created: 2026-07-16 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsndfile/src/ALAC/ALACBitUtilities.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2011 Apple Inc. All rights reserved.
3
 *
4
 * @APPLE_APACHE_LICENSE_HEADER_START@
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License") ;
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 * @APPLE_APACHE_LICENSE_HEADER_END@
19
 */
20
21
/*=============================================================================
22
  File:   ALACBitUtilities.c
23
24
  $NoKeywords: $
25
=============================================================================*/
26
27
#include <stdio.h>
28
#include "ALACBitUtilities.h"
29
30
#define PRAGMA_MARK 0
31
32
// BitBufferInit
33
//
34
void BitBufferInit (BitBuffer * bits, uint8_t * buffer, uint32_t byteSize)
35
80.5k
{
36
80.5k
  bits->cur   = buffer ;
37
80.5k
  bits->end   = bits->cur + byteSize ;
38
80.5k
  bits->bitIndex  = 0 ;
39
80.5k
  bits->byteSize  = byteSize ;
40
80.5k
}
41
42
// BitBufferRead
43
//
44
uint32_t BitBufferRead (BitBuffer * bits, uint8_t numBits)
45
4.68M
{
46
4.68M
  uint32_t    returnBits ;
47
48
  //Assert (numBits <= 16) ;
49
50
4.68M
  returnBits = ((uint32_t) bits->cur [0] << 16) | ((uint32_t) bits->cur [1] << 8) | ((uint32_t) bits->cur [2]) ;
51
4.68M
  returnBits = returnBits << bits->bitIndex ;
52
4.68M
  returnBits &= 0x00FFFFFF ;
53
54
4.68M
  bits->bitIndex += numBits ;
55
56
4.68M
  returnBits = returnBits >> (24 - numBits) ;
57
58
4.68M
  bits->cur   += (bits->bitIndex >> 3) ;
59
4.68M
  bits->bitIndex  &= 7 ;
60
61
  //Assert (bits->cur <= bits->end) ;
62
63
4.68M
  return returnBits ;
64
4.68M
}
65
66
// BitBufferReadSmall
67
//
68
// Reads up to 8 bits
69
uint8_t BitBufferReadSmall (BitBuffer * bits, uint8_t numBits)
70
229k
{
71
229k
  uint16_t    returnBits ;
72
73
  //Assert (numBits <= 8) ;
74
75
229k
  returnBits = (bits->cur [0] << 8) | bits->cur [1] ;
76
229k
  returnBits = returnBits << bits->bitIndex ;
77
78
229k
  bits->bitIndex += numBits ;
79
80
229k
  returnBits = returnBits >> (16 - numBits) ;
81
82
229k
  bits->cur   += (bits->bitIndex >> 3) ;
83
229k
  bits->bitIndex  &= 7 ;
84
85
  //Assert (bits->cur <= bits->end) ;
86
87
229k
  return (uint8_t) returnBits ;
88
229k
}
89
90
// BitBufferReadOne
91
//
92
// Reads one byte
93
uint8_t BitBufferReadOne (BitBuffer * bits)
94
4.16k
{
95
4.16k
  uint8_t   returnBits ;
96
97
4.16k
  returnBits = (bits->cur [0] >> (7 - bits->bitIndex)) & 1 ;
98
99
4.16k
  bits->bitIndex++ ;
100
101
4.16k
  bits->cur   += (bits->bitIndex >> 3) ;
102
4.16k
  bits->bitIndex  &= 7 ;
103
104
  //Assert (bits->cur <= bits->end) ;
105
106
4.16k
  return returnBits ;
107
4.16k
}
108
109
// BitBufferPeek
110
//
111
uint32_t BitBufferPeek (BitBuffer * bits, uint8_t numBits)
112
0
{
113
0
  return ((((((uint32_t) bits->cur [0] << 16) | ((uint32_t) bits->cur [1] << 8) |
114
0
      ((uint32_t) bits->cur [2])) << bits->bitIndex) & 0x00FFFFFF) >> (24 - numBits)) ;
115
0
}
116
117
// BitBufferPeekOne
118
//
119
uint32_t BitBufferPeekOne (BitBuffer * bits)
120
0
{
121
0
  return ((bits->cur [0] >> (7 - bits->bitIndex)) & 1) ;
122
0
}
123
124
// BitBufferUnpackBERSize
125
//
126
uint32_t BitBufferUnpackBERSize (BitBuffer * bits)
127
0
{
128
0
  uint32_t    size ;
129
0
  uint8_t   tmp ;
130
131
0
  for (size = 0, tmp = 0x80u ; tmp &= 0x80u ; size = (size << 7u) | (tmp & 0x7fu))
132
0
    tmp = (uint8_t) BitBufferReadSmall (bits, 8) ;
133
134
0
  return size ;
135
0
}
136
137
// BitBufferGetPosition
138
//
139
uint32_t BitBufferGetPosition (BitBuffer * bits)
140
0
{
141
0
  uint8_t *   begin ;
142
143
0
  begin = bits->end - bits->byteSize ;
144
145
0
  return ((uint32_t) (bits->cur - begin) * 8) + bits->bitIndex ;
146
0
}
147
148
// BitBufferByteAlign
149
//
150
void BitBufferByteAlign (BitBuffer * bits, int32_t addZeros)
151
6.24k
{
152
  // align bit buffer to next byte boundary, writing zeros if requested
153
6.24k
  if (bits->bitIndex == 0)
154
1.44k
    return ;
155
156
4.80k
  if (addZeros)
157
0
    BitBufferWrite (bits, 0, 8 - bits->bitIndex) ;
158
4.80k
  else
159
4.80k
    BitBufferAdvance (bits, 8 - bits->bitIndex) ;
160
4.80k
}
161
162
// BitBufferAdvance
163
//
164
void BitBufferAdvance (BitBuffer * bits, uint32_t numBits)
165
80.7k
{
166
80.7k
  if (numBits)
167
63.1k
  {
168
63.1k
    bits->bitIndex += numBits ;
169
63.1k
    bits->cur += (bits->bitIndex >> 3) ;
170
63.1k
    bits->bitIndex &= 7 ;
171
63.1k
  }
172
80.7k
}
173
174
// BitBufferRewind
175
//
176
void BitBufferRewind (BitBuffer * bits, uint32_t numBits)
177
0
{
178
0
  uint32_t  numBytes ;
179
180
0
  if (numBits == 0)
181
0
    return ;
182
183
0
  if (bits->bitIndex >= numBits)
184
0
  {
185
0
    bits->bitIndex -= numBits ;
186
0
    return ;
187
0
  }
188
189
0
  numBits -= bits->bitIndex ;
190
0
  bits->bitIndex = 0 ;
191
192
0
  numBytes  = numBits / 8 ;
193
0
  numBits   = numBits % 8 ;
194
195
0
  bits->cur -= numBytes ;
196
197
0
  if (numBits > 0)
198
0
  {
199
0
    bits->bitIndex = 8 - numBits ;
200
0
    bits->cur-- ;
201
0
  }
202
203
0
  if (bits->cur < (bits->end - bits->byteSize))
204
0
  {
205
    //DebugCMsg ("BitBufferRewind: Rewound too far.") ;
206
207
0
    bits->cur   = (bits->end - bits->byteSize) ;
208
0
    bits->bitIndex  = 0 ;
209
0
  }
210
0
}
211
212
// BitBufferWrite
213
//
214
void BitBufferWrite (BitBuffer * bits, uint32_t bitValues, uint32_t numBits)
215
0
{
216
0
  uint32_t        invBitIndex ;
217
218
0
  RequireAction (bits != NULL, return ;) ;
219
0
  RequireActionSilent (numBits > 0, return ;) ;
220
221
0
  invBitIndex = 8 - bits->bitIndex ;
222
223
0
  while (numBits > 0)
224
0
  {
225
0
    uint32_t    tmp ;
226
0
    uint8_t   shift ;
227
0
    uint8_t   mask ;
228
0
    uint32_t    curNum ;
229
230
0
    curNum = MIN (invBitIndex, numBits) ;
231
232
0
    tmp = bitValues >> (numBits - curNum) ;
233
234
0
    shift = (uint8_t) (invBitIndex - curNum) ;
235
0
    mask = 0xffu >> (8 - curNum) ;    // must be done in two steps to avoid compiler sequencing ambiguity
236
0
    mask <<= shift ;
237
238
0
    bits->cur [0] = (bits->cur [0] & ~mask) | (((uint8_t) tmp << shift) & mask) ;
239
0
    numBits -= curNum ;
240
241
    // increment to next byte if need be
242
0
    invBitIndex -= curNum ;
243
0
    if (invBitIndex == 0)
244
0
    {
245
0
      invBitIndex = 8 ;
246
0
      bits->cur++ ;
247
0
    }
248
0
  }
249
250
0
  bits->bitIndex = 8 - invBitIndex ;
251
0
}
252
253
void  BitBufferReset (BitBuffer * bits)
254
//void BitBufferInit (BitBuffer * bits, uint8_t * buffer, uint32_t byteSize)
255
0
{
256
0
  bits->cur = bits->end - bits->byteSize ;
257
0
  bits->bitIndex = 0 ;
258
0
}
259
260
#if PRAGMA_MARK
261
#pragma mark -
262
#endif