@@ -80,9 +80,9 @@ extern "C" {
80
80
* bitStream encoding API (write forward)
81
81
********************************************/
82
82
/* bitStream can mix input from multiple sources.
83
- * A critical property of these streams is that they encode and decode in **reverse** direction.
84
- * So the first bit sequence you add will be the last to be read, like a LIFO stack.
85
- */
83
+ * A critical property of these streams is that they encode and decode in **reverse** direction.
84
+ * So the first bit sequence you add will be the last to be read, like a LIFO stack.
85
+ */
86
86
typedef struct
87
87
{
88
88
size_t bitContainer ;
@@ -203,7 +203,7 @@ static const unsigned BIT_mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F,
203
203
/*! BIT_initCStream() :
204
204
* `dstCapacity` must be > sizeof(size_t)
205
205
* @return : 0 if success,
206
- otherwise an error code (can be tested using ERR_isError() ) */
206
+ * otherwise an error code (can be tested using ERR_isError()) */
207
207
MEM_STATIC size_t BIT_initCStream (BIT_CStream_t * bitC ,
208
208
void * startPtr , size_t dstCapacity )
209
209
{
@@ -217,8 +217,8 @@ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC,
217
217
}
218
218
219
219
/*! BIT_addBits() :
220
- can add up to 26 bits into `bitC`.
221
- Does not check for register overflow ! */
220
+ * can add up to 26 bits into `bitC`.
221
+ * Note : does not check for register overflow ! */
222
222
MEM_STATIC void BIT_addBits (BIT_CStream_t * bitC ,
223
223
size_t value , unsigned nbBits )
224
224
{
@@ -268,7 +268,7 @@ MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
268
268
269
269
/*! BIT_closeCStream() :
270
270
* @return : size of CStream, in bytes,
271
- or 0 if it could not fit into dstBuffer */
271
+ * or 0 if it could not fit into dstBuffer */
272
272
MEM_STATIC size_t BIT_closeCStream (BIT_CStream_t * bitC )
273
273
{
274
274
BIT_addBitsFast (bitC , 1 , 1 ); /* endMark */
@@ -279,14 +279,14 @@ MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
279
279
280
280
281
281
/*-********************************************************
282
- * bitStream decoding
282
+ * bitStream decoding
283
283
**********************************************************/
284
284
/*! BIT_initDStream() :
285
- * Initialize a BIT_DStream_t.
286
- * `bitD` : a pointer to an already allocated BIT_DStream_t structure.
287
- * `srcSize` must be the *exact* size of the bitStream, in bytes.
288
- * @return : size of stream (== srcSize) or an errorCode if a problem is detected
289
- */
285
+ * Initialize a BIT_DStream_t.
286
+ * `bitD` : a pointer to an already allocated BIT_DStream_t structure.
287
+ * `srcSize` must be the *exact* size of the bitStream, in bytes.
288
+ * @return : size of stream (== srcSize), or an errorCode if a problem is detected
289
+ */
290
290
MEM_STATIC size_t BIT_initDStream (BIT_DStream_t * bitD , const void * srcBuffer , size_t srcSize )
291
291
{
292
292
if (srcSize < 1 ) { memset (bitD , 0 , sizeof (* bitD )); return ERROR (srcSize_wrong ); }
@@ -305,29 +305,30 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
305
305
bitD -> bitContainer = * (const BYTE * )(bitD -> start );
306
306
switch (srcSize )
307
307
{
308
- case 7 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[6 ]) << (sizeof (bitD -> bitContainer )* 8 - 16 );
309
- /* fall-through */
308
+ case 7 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[6 ]) << (sizeof (bitD -> bitContainer )* 8 - 16 );
309
+ /* fall-through */
310
310
311
- case 6 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[5 ]) << (sizeof (bitD -> bitContainer )* 8 - 24 );
312
- /* fall-through */
311
+ case 6 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[5 ]) << (sizeof (bitD -> bitContainer )* 8 - 24 );
312
+ /* fall-through */
313
313
314
- case 5 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[4 ]) << (sizeof (bitD -> bitContainer )* 8 - 32 );
315
- /* fall-through */
314
+ case 5 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[4 ]) << (sizeof (bitD -> bitContainer )* 8 - 32 );
315
+ /* fall-through */
316
316
317
- case 4 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[3 ]) << 24 ;
318
- /* fall-through */
317
+ case 4 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[3 ]) << 24 ;
318
+ /* fall-through */
319
319
320
- case 3 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[2 ]) << 16 ;
321
- /* fall-through */
320
+ case 3 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[2 ]) << 16 ;
321
+ /* fall-through */
322
322
323
- case 2 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[1 ]) << 8 ;
324
- /* fall-through */
323
+ case 2 : bitD -> bitContainer += (size_t )(((const BYTE * )(srcBuffer ))[1 ]) << 8 ;
324
+ /* fall-through */
325
325
326
- default : break ;
326
+ default : break ;
327
+ }
328
+ { BYTE const lastByte = ((const BYTE * )srcBuffer )[srcSize - 1 ];
329
+ bitD -> bitsConsumed = lastByte ? 8 - BIT_highbit32 (lastByte ) : 0 ;
330
+ if (lastByte == 0 ) return ERROR (corruption_detected ); /* endMark not present */
327
331
}
328
- { BYTE const lastByte = ((const BYTE * )srcBuffer )[srcSize - 1 ];
329
- bitD -> bitsConsumed = lastByte ? 8 - BIT_highbit32 (lastByte ) : 0 ;
330
- if (lastByte == 0 ) return ERROR (GENERIC ); /* endMark not present */ }
331
332
bitD -> bitsConsumed += (U32 )(sizeof (bitD -> bitContainer ) - srcSize )* 8 ;
332
333
}
333
334
@@ -363,9 +364,8 @@ MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
363
364
* local register is not modified.
364
365
* On 32-bits, maxNbBits==24.
365
366
* On 64-bits, maxNbBits==56.
366
- * @return : value extracted
367
- */
368
- MEM_STATIC size_t BIT_lookBits (const BIT_DStream_t * bitD , U32 nbBits )
367
+ * @return : value extracted */
368
+ MEM_STATIC size_t BIT_lookBits (const BIT_DStream_t * bitD , U32 nbBits )
369
369
{
370
370
#if defined(__BMI__ ) && defined(__GNUC__ ) /* experimental; fails if bitD->bitsConsumed + nbBits > sizeof(bitD->bitContainer)*8 */
371
371
return BIT_getMiddleBits (bitD -> bitContainer , (sizeof (bitD -> bitContainer )* 8 ) - bitD -> bitsConsumed - nbBits , nbBits );
@@ -392,8 +392,7 @@ MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
392
392
/*! BIT_readBits() :
393
393
* Read (consume) next n bits from local register and update.
394
394
* Pay attention to not read more than nbBits contained into local register.
395
- * @return : extracted value.
396
- */
395
+ * @return : extracted value. */
397
396
MEM_STATIC size_t BIT_readBits (BIT_DStream_t * bitD , U32 nbBits )
398
397
{
399
398
size_t const value = BIT_lookBits (bitD , nbBits );
@@ -402,7 +401,7 @@ MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
402
401
}
403
402
404
403
/*! BIT_readBitsFast() :
405
- * unsafe version; only works only if nbBits >= 1 */
404
+ * unsafe version; only works only if nbBits >= 1 */
406
405
MEM_STATIC size_t BIT_readBitsFast (BIT_DStream_t * bitD , U32 nbBits )
407
406
{
408
407
size_t const value = BIT_lookBitsFast (bitD , nbBits );
@@ -412,10 +411,10 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
412
411
}
413
412
414
413
/*! BIT_reloadDStream() :
415
- * Refill `bitD` from buffer previously set in BIT_initDStream() .
416
- * This function is safe, it guarantees it will not read beyond src buffer.
417
- * @return : status of `BIT_DStream_t` internal register.
418
- if status == BIT_DStream_unfinished, internal register is filled with >= (sizeof(bitD->bitContainer)*8 - 7) bits */
414
+ * Refill `bitD` from buffer previously set in BIT_initDStream() .
415
+ * This function is safe, it guarantees it will not read beyond src buffer.
416
+ * @return : status of `BIT_DStream_t` internal register.
417
+ * when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */
419
418
MEM_STATIC BIT_DStream_status BIT_reloadDStream (BIT_DStream_t * bitD )
420
419
{
421
420
if (bitD -> bitsConsumed > (sizeof (bitD -> bitContainer )* 8 )) /* overflow detected, like end of stream */
@@ -446,8 +445,8 @@ MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
446
445
}
447
446
448
447
/*! BIT_endOfDStream() :
449
- * @return Tells if DStream has exactly reached its end (all bits consumed).
450
- */
448
+ * @return : 1 if DStream has _exactly_ reached its end (all bits consumed).
449
+ */
451
450
MEM_STATIC unsigned BIT_endOfDStream (const BIT_DStream_t * DStream )
452
451
{
453
452
return ((DStream -> ptr == DStream -> start ) && (DStream -> bitsConsumed == sizeof (DStream -> bitContainer )* 8 ));
0 commit comments