18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * bitstream 38c2ecf20Sopenharmony_ci * Part of FSE library 48c2ecf20Sopenharmony_ci * header file (to include) 58c2ecf20Sopenharmony_ci * Copyright (C) 2013-2016, Yann Collet. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 108c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions are 118c2ecf20Sopenharmony_ci * met: 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * * Redistributions of source code must retain the above copyright 148c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 158c2ecf20Sopenharmony_ci * * Redistributions in binary form must reproduce the above 168c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following disclaimer 178c2ecf20Sopenharmony_ci * in the documentation and/or other materials provided with the 188c2ecf20Sopenharmony_ci * distribution. 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 218c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 228c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 238c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 248c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 258c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 268c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 278c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 288c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 298c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 308c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 318c2ecf20Sopenharmony_ci * 328c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it under 338c2ecf20Sopenharmony_ci * the terms of the GNU General Public License version 2 as published by the 348c2ecf20Sopenharmony_ci * Free Software Foundation. This program is dual-licensed; you may select 358c2ecf20Sopenharmony_ci * either version 2 of the GNU General Public License ("GPL") or BSD license 368c2ecf20Sopenharmony_ci * ("BSD"). 378c2ecf20Sopenharmony_ci * 388c2ecf20Sopenharmony_ci * You can contact the author at : 398c2ecf20Sopenharmony_ci * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy 408c2ecf20Sopenharmony_ci */ 418c2ecf20Sopenharmony_ci#ifndef BITSTREAM_H_MODULE 428c2ecf20Sopenharmony_ci#define BITSTREAM_H_MODULE 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* 458c2ecf20Sopenharmony_ci* This API consists of small unitary functions, which must be inlined for best performance. 468c2ecf20Sopenharmony_ci* Since link-time-optimization is not available for all compilers, 478c2ecf20Sopenharmony_ci* these functions are defined into a .h to be included. 488c2ecf20Sopenharmony_ci*/ 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/*-**************************************** 518c2ecf20Sopenharmony_ci* Dependencies 528c2ecf20Sopenharmony_ci******************************************/ 538c2ecf20Sopenharmony_ci#include "error_private.h" /* error codes and messages */ 548c2ecf20Sopenharmony_ci#include "mem.h" /* unaligned access routines */ 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/*========================================= 578c2ecf20Sopenharmony_ci* Target specific 588c2ecf20Sopenharmony_ci=========================================*/ 598c2ecf20Sopenharmony_ci#define STREAM_ACCUMULATOR_MIN_32 25 608c2ecf20Sopenharmony_ci#define STREAM_ACCUMULATOR_MIN_64 57 618c2ecf20Sopenharmony_ci#define STREAM_ACCUMULATOR_MIN ((U32)(ZSTD_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64)) 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci/*-****************************************** 648c2ecf20Sopenharmony_ci* bitStream encoding API (write forward) 658c2ecf20Sopenharmony_ci********************************************/ 668c2ecf20Sopenharmony_ci/* bitStream can mix input from multiple sources. 678c2ecf20Sopenharmony_ci* A critical property of these streams is that they encode and decode in **reverse** direction. 688c2ecf20Sopenharmony_ci* So the first bit sequence you add will be the last to be read, like a LIFO stack. 698c2ecf20Sopenharmony_ci*/ 708c2ecf20Sopenharmony_citypedef struct { 718c2ecf20Sopenharmony_ci size_t bitContainer; 728c2ecf20Sopenharmony_ci int bitPos; 738c2ecf20Sopenharmony_ci char *startPtr; 748c2ecf20Sopenharmony_ci char *ptr; 758c2ecf20Sopenharmony_ci char *endPtr; 768c2ecf20Sopenharmony_ci} BIT_CStream_t; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_initCStream(BIT_CStream_t *bitC, void *dstBuffer, size_t dstCapacity); 798c2ecf20Sopenharmony_ciZSTD_STATIC void BIT_addBits(BIT_CStream_t *bitC, size_t value, unsigned nbBits); 808c2ecf20Sopenharmony_ciZSTD_STATIC void BIT_flushBits(BIT_CStream_t *bitC); 818c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_closeCStream(BIT_CStream_t *bitC); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci/* Start with initCStream, providing the size of buffer to write into. 848c2ecf20Sopenharmony_ci* bitStream will never write outside of this buffer. 858c2ecf20Sopenharmony_ci* `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code. 868c2ecf20Sopenharmony_ci* 878c2ecf20Sopenharmony_ci* bits are first added to a local register. 888c2ecf20Sopenharmony_ci* Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems. 898c2ecf20Sopenharmony_ci* Writing data into memory is an explicit operation, performed by the flushBits function. 908c2ecf20Sopenharmony_ci* Hence keep track how many bits are potentially stored into local register to avoid register overflow. 918c2ecf20Sopenharmony_ci* After a flushBits, a maximum of 7 bits might still be stored into local register. 928c2ecf20Sopenharmony_ci* 938c2ecf20Sopenharmony_ci* Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers. 948c2ecf20Sopenharmony_ci* 958c2ecf20Sopenharmony_ci* Last operation is to close the bitStream. 968c2ecf20Sopenharmony_ci* The function returns the final size of CStream in bytes. 978c2ecf20Sopenharmony_ci* If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable) 988c2ecf20Sopenharmony_ci*/ 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/*-******************************************** 1018c2ecf20Sopenharmony_ci* bitStream decoding API (read backward) 1028c2ecf20Sopenharmony_ci**********************************************/ 1038c2ecf20Sopenharmony_citypedef struct { 1048c2ecf20Sopenharmony_ci size_t bitContainer; 1058c2ecf20Sopenharmony_ci unsigned bitsConsumed; 1068c2ecf20Sopenharmony_ci const char *ptr; 1078c2ecf20Sopenharmony_ci const char *start; 1088c2ecf20Sopenharmony_ci} BIT_DStream_t; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_citypedef enum { 1118c2ecf20Sopenharmony_ci BIT_DStream_unfinished = 0, 1128c2ecf20Sopenharmony_ci BIT_DStream_endOfBuffer = 1, 1138c2ecf20Sopenharmony_ci BIT_DStream_completed = 2, 1148c2ecf20Sopenharmony_ci BIT_DStream_overflow = 3 1158c2ecf20Sopenharmony_ci} BIT_DStream_status; /* result of BIT_reloadDStream() */ 1168c2ecf20Sopenharmony_ci/* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */ 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_initDStream(BIT_DStream_t *bitD, const void *srcBuffer, size_t srcSize); 1198c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_readBits(BIT_DStream_t *bitD, unsigned nbBits); 1208c2ecf20Sopenharmony_ciZSTD_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t *bitD); 1218c2ecf20Sopenharmony_ciZSTD_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t *bitD); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci/* Start by invoking BIT_initDStream(). 1248c2ecf20Sopenharmony_ci* A chunk of the bitStream is then stored into a local register. 1258c2ecf20Sopenharmony_ci* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t). 1268c2ecf20Sopenharmony_ci* You can then retrieve bitFields stored into the local register, **in reverse order**. 1278c2ecf20Sopenharmony_ci* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method. 1288c2ecf20Sopenharmony_ci* A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished. 1298c2ecf20Sopenharmony_ci* Otherwise, it can be less than that, so proceed accordingly. 1308c2ecf20Sopenharmony_ci* Checking if DStream has reached its end can be performed with BIT_endOfDStream(). 1318c2ecf20Sopenharmony_ci*/ 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci/*-**************************************** 1348c2ecf20Sopenharmony_ci* unsafe API 1358c2ecf20Sopenharmony_ci******************************************/ 1368c2ecf20Sopenharmony_ciZSTD_STATIC void BIT_addBitsFast(BIT_CStream_t *bitC, size_t value, unsigned nbBits); 1378c2ecf20Sopenharmony_ci/* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */ 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ciZSTD_STATIC void BIT_flushBitsFast(BIT_CStream_t *bitC); 1408c2ecf20Sopenharmony_ci/* unsafe version; does not check buffer overflow */ 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_readBitsFast(BIT_DStream_t *bitD, unsigned nbBits); 1438c2ecf20Sopenharmony_ci/* faster, but works only if nbBits >= 1 */ 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci/*-************************************************************** 1468c2ecf20Sopenharmony_ci* Internal functions 1478c2ecf20Sopenharmony_ci****************************************************************/ 1488c2ecf20Sopenharmony_ciZSTD_STATIC unsigned BIT_highbit32(register U32 val) { return 31 - __builtin_clz(val); } 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci/*===== Local Constants =====*/ 1518c2ecf20Sopenharmony_cistatic const unsigned BIT_mask[] = {0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 1528c2ecf20Sopenharmony_ci 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 1538c2ecf20Sopenharmony_ci 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF}; /* up to 26 bits */ 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci/*-************************************************************** 1568c2ecf20Sopenharmony_ci* bitStream encoding 1578c2ecf20Sopenharmony_ci****************************************************************/ 1588c2ecf20Sopenharmony_ci/*! BIT_initCStream() : 1598c2ecf20Sopenharmony_ci * `dstCapacity` must be > sizeof(void*) 1608c2ecf20Sopenharmony_ci * @return : 0 if success, 1618c2ecf20Sopenharmony_ci otherwise an error code (can be tested using ERR_isError() ) */ 1628c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_initCStream(BIT_CStream_t *bitC, void *startPtr, size_t dstCapacity) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci bitC->bitContainer = 0; 1658c2ecf20Sopenharmony_ci bitC->bitPos = 0; 1668c2ecf20Sopenharmony_ci bitC->startPtr = (char *)startPtr; 1678c2ecf20Sopenharmony_ci bitC->ptr = bitC->startPtr; 1688c2ecf20Sopenharmony_ci bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->ptr); 1698c2ecf20Sopenharmony_ci if (dstCapacity <= sizeof(bitC->ptr)) 1708c2ecf20Sopenharmony_ci return ERROR(dstSize_tooSmall); 1718c2ecf20Sopenharmony_ci return 0; 1728c2ecf20Sopenharmony_ci} 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci/*! BIT_addBits() : 1758c2ecf20Sopenharmony_ci can add up to 26 bits into `bitC`. 1768c2ecf20Sopenharmony_ci Does not check for register overflow ! */ 1778c2ecf20Sopenharmony_ciZSTD_STATIC void BIT_addBits(BIT_CStream_t *bitC, size_t value, unsigned nbBits) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos; 1808c2ecf20Sopenharmony_ci bitC->bitPos += nbBits; 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/*! BIT_addBitsFast() : 1848c2ecf20Sopenharmony_ci * works only if `value` is _clean_, meaning all high bits above nbBits are 0 */ 1858c2ecf20Sopenharmony_ciZSTD_STATIC void BIT_addBitsFast(BIT_CStream_t *bitC, size_t value, unsigned nbBits) 1868c2ecf20Sopenharmony_ci{ 1878c2ecf20Sopenharmony_ci bitC->bitContainer |= value << bitC->bitPos; 1888c2ecf20Sopenharmony_ci bitC->bitPos += nbBits; 1898c2ecf20Sopenharmony_ci} 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci/*! BIT_flushBitsFast() : 1928c2ecf20Sopenharmony_ci * unsafe version; does not check buffer overflow */ 1938c2ecf20Sopenharmony_ciZSTD_STATIC void BIT_flushBitsFast(BIT_CStream_t *bitC) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci size_t const nbBytes = bitC->bitPos >> 3; 1968c2ecf20Sopenharmony_ci ZSTD_writeLEST(bitC->ptr, bitC->bitContainer); 1978c2ecf20Sopenharmony_ci bitC->ptr += nbBytes; 1988c2ecf20Sopenharmony_ci bitC->bitPos &= 7; 1998c2ecf20Sopenharmony_ci bitC->bitContainer >>= nbBytes * 8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */ 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci/*! BIT_flushBits() : 2038c2ecf20Sopenharmony_ci * safe version; check for buffer overflow, and prevents it. 2048c2ecf20Sopenharmony_ci * note : does not signal buffer overflow. This will be revealed later on using BIT_closeCStream() */ 2058c2ecf20Sopenharmony_ciZSTD_STATIC void BIT_flushBits(BIT_CStream_t *bitC) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci size_t const nbBytes = bitC->bitPos >> 3; 2088c2ecf20Sopenharmony_ci ZSTD_writeLEST(bitC->ptr, bitC->bitContainer); 2098c2ecf20Sopenharmony_ci bitC->ptr += nbBytes; 2108c2ecf20Sopenharmony_ci if (bitC->ptr > bitC->endPtr) 2118c2ecf20Sopenharmony_ci bitC->ptr = bitC->endPtr; 2128c2ecf20Sopenharmony_ci bitC->bitPos &= 7; 2138c2ecf20Sopenharmony_ci bitC->bitContainer >>= nbBytes * 8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */ 2148c2ecf20Sopenharmony_ci} 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci/*! BIT_closeCStream() : 2178c2ecf20Sopenharmony_ci * @return : size of CStream, in bytes, 2188c2ecf20Sopenharmony_ci or 0 if it could not fit into dstBuffer */ 2198c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_closeCStream(BIT_CStream_t *bitC) 2208c2ecf20Sopenharmony_ci{ 2218c2ecf20Sopenharmony_ci BIT_addBitsFast(bitC, 1, 1); /* endMark */ 2228c2ecf20Sopenharmony_ci BIT_flushBits(bitC); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci if (bitC->ptr >= bitC->endPtr) 2258c2ecf20Sopenharmony_ci return 0; /* doesn't fit within authorized budget : cancel */ 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0); 2288c2ecf20Sopenharmony_ci} 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci/*-******************************************************** 2318c2ecf20Sopenharmony_ci* bitStream decoding 2328c2ecf20Sopenharmony_ci**********************************************************/ 2338c2ecf20Sopenharmony_ci/*! BIT_initDStream() : 2348c2ecf20Sopenharmony_ci* Initialize a BIT_DStream_t. 2358c2ecf20Sopenharmony_ci* `bitD` : a pointer to an already allocated BIT_DStream_t structure. 2368c2ecf20Sopenharmony_ci* `srcSize` must be the *exact* size of the bitStream, in bytes. 2378c2ecf20Sopenharmony_ci* @return : size of stream (== srcSize) or an errorCode if a problem is detected 2388c2ecf20Sopenharmony_ci*/ 2398c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_initDStream(BIT_DStream_t *bitD, const void *srcBuffer, size_t srcSize) 2408c2ecf20Sopenharmony_ci{ 2418c2ecf20Sopenharmony_ci if (srcSize < 1) { 2428c2ecf20Sopenharmony_ci memset(bitD, 0, sizeof(*bitD)); 2438c2ecf20Sopenharmony_ci return ERROR(srcSize_wrong); 2448c2ecf20Sopenharmony_ci } 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */ 2478c2ecf20Sopenharmony_ci bitD->start = (const char *)srcBuffer; 2488c2ecf20Sopenharmony_ci bitD->ptr = (const char *)srcBuffer + srcSize - sizeof(bitD->bitContainer); 2498c2ecf20Sopenharmony_ci bitD->bitContainer = ZSTD_readLEST(bitD->ptr); 2508c2ecf20Sopenharmony_ci { 2518c2ecf20Sopenharmony_ci BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1]; 2528c2ecf20Sopenharmony_ci bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */ 2538c2ecf20Sopenharmony_ci if (lastByte == 0) 2548c2ecf20Sopenharmony_ci return ERROR(GENERIC); /* endMark not present */ 2558c2ecf20Sopenharmony_ci } 2568c2ecf20Sopenharmony_ci } else { 2578c2ecf20Sopenharmony_ci bitD->start = (const char *)srcBuffer; 2588c2ecf20Sopenharmony_ci bitD->ptr = bitD->start; 2598c2ecf20Sopenharmony_ci bitD->bitContainer = *(const BYTE *)(bitD->start); 2608c2ecf20Sopenharmony_ci switch (srcSize) { 2618c2ecf20Sopenharmony_ci case 7: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[6]) << (sizeof(bitD->bitContainer) * 8 - 16); 2628c2ecf20Sopenharmony_ci /* fall through */ 2638c2ecf20Sopenharmony_ci case 6: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[5]) << (sizeof(bitD->bitContainer) * 8 - 24); 2648c2ecf20Sopenharmony_ci /* fall through */ 2658c2ecf20Sopenharmony_ci case 5: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[4]) << (sizeof(bitD->bitContainer) * 8 - 32); 2668c2ecf20Sopenharmony_ci /* fall through */ 2678c2ecf20Sopenharmony_ci case 4: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[3]) << 24; 2688c2ecf20Sopenharmony_ci /* fall through */ 2698c2ecf20Sopenharmony_ci case 3: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[2]) << 16; 2708c2ecf20Sopenharmony_ci /* fall through */ 2718c2ecf20Sopenharmony_ci case 2: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[1]) << 8; 2728c2ecf20Sopenharmony_ci default:; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci { 2758c2ecf20Sopenharmony_ci BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1]; 2768c2ecf20Sopenharmony_ci bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; 2778c2ecf20Sopenharmony_ci if (lastByte == 0) 2788c2ecf20Sopenharmony_ci return ERROR(GENERIC); /* endMark not present */ 2798c2ecf20Sopenharmony_ci } 2808c2ecf20Sopenharmony_ci bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize) * 8; 2818c2ecf20Sopenharmony_ci } 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci return srcSize; 2848c2ecf20Sopenharmony_ci} 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start) { return bitContainer >> start; } 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits) { return (bitContainer >> start) & BIT_mask[nbBits]; } 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits) { return bitContainer & BIT_mask[nbBits]; } 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci/*! BIT_lookBits() : 2938c2ecf20Sopenharmony_ci * Provides next n bits from local register. 2948c2ecf20Sopenharmony_ci * local register is not modified. 2958c2ecf20Sopenharmony_ci * On 32-bits, maxNbBits==24. 2968c2ecf20Sopenharmony_ci * On 64-bits, maxNbBits==56. 2978c2ecf20Sopenharmony_ci * @return : value extracted 2988c2ecf20Sopenharmony_ci */ 2998c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_lookBits(const BIT_DStream_t *bitD, U32 nbBits) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1; 3028c2ecf20Sopenharmony_ci return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask - nbBits) & bitMask); 3038c2ecf20Sopenharmony_ci} 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci/*! BIT_lookBitsFast() : 3068c2ecf20Sopenharmony_ci* unsafe version; only works only if nbBits >= 1 */ 3078c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t *bitD, U32 nbBits) 3088c2ecf20Sopenharmony_ci{ 3098c2ecf20Sopenharmony_ci U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1; 3108c2ecf20Sopenharmony_ci return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask + 1) - nbBits) & bitMask); 3118c2ecf20Sopenharmony_ci} 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ciZSTD_STATIC void BIT_skipBits(BIT_DStream_t *bitD, U32 nbBits) { bitD->bitsConsumed += nbBits; } 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci/*! BIT_readBits() : 3168c2ecf20Sopenharmony_ci * Read (consume) next n bits from local register and update. 3178c2ecf20Sopenharmony_ci * Pay attention to not read more than nbBits contained into local register. 3188c2ecf20Sopenharmony_ci * @return : extracted value. 3198c2ecf20Sopenharmony_ci */ 3208c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_readBits(BIT_DStream_t *bitD, U32 nbBits) 3218c2ecf20Sopenharmony_ci{ 3228c2ecf20Sopenharmony_ci size_t const value = BIT_lookBits(bitD, nbBits); 3238c2ecf20Sopenharmony_ci BIT_skipBits(bitD, nbBits); 3248c2ecf20Sopenharmony_ci return value; 3258c2ecf20Sopenharmony_ci} 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci/*! BIT_readBitsFast() : 3288c2ecf20Sopenharmony_ci* unsafe version; only works only if nbBits >= 1 */ 3298c2ecf20Sopenharmony_ciZSTD_STATIC size_t BIT_readBitsFast(BIT_DStream_t *bitD, U32 nbBits) 3308c2ecf20Sopenharmony_ci{ 3318c2ecf20Sopenharmony_ci size_t const value = BIT_lookBitsFast(bitD, nbBits); 3328c2ecf20Sopenharmony_ci BIT_skipBits(bitD, nbBits); 3338c2ecf20Sopenharmony_ci return value; 3348c2ecf20Sopenharmony_ci} 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci/*! BIT_reloadDStream() : 3378c2ecf20Sopenharmony_ci* Refill `bitD` from buffer previously set in BIT_initDStream() . 3388c2ecf20Sopenharmony_ci* This function is safe, it guarantees it will not read beyond src buffer. 3398c2ecf20Sopenharmony_ci* @return : status of `BIT_DStream_t` internal register. 3408c2ecf20Sopenharmony_ci if status == BIT_DStream_unfinished, internal register is filled with >= (sizeof(bitD->bitContainer)*8 - 7) bits */ 3418c2ecf20Sopenharmony_ciZSTD_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t *bitD) 3428c2ecf20Sopenharmony_ci{ 3438c2ecf20Sopenharmony_ci if (bitD->bitsConsumed > (sizeof(bitD->bitContainer) * 8)) /* should not happen => corruption detected */ 3448c2ecf20Sopenharmony_ci return BIT_DStream_overflow; 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) { 3478c2ecf20Sopenharmony_ci bitD->ptr -= bitD->bitsConsumed >> 3; 3488c2ecf20Sopenharmony_ci bitD->bitsConsumed &= 7; 3498c2ecf20Sopenharmony_ci bitD->bitContainer = ZSTD_readLEST(bitD->ptr); 3508c2ecf20Sopenharmony_ci return BIT_DStream_unfinished; 3518c2ecf20Sopenharmony_ci } 3528c2ecf20Sopenharmony_ci if (bitD->ptr == bitD->start) { 3538c2ecf20Sopenharmony_ci if (bitD->bitsConsumed < sizeof(bitD->bitContainer) * 8) 3548c2ecf20Sopenharmony_ci return BIT_DStream_endOfBuffer; 3558c2ecf20Sopenharmony_ci return BIT_DStream_completed; 3568c2ecf20Sopenharmony_ci } 3578c2ecf20Sopenharmony_ci { 3588c2ecf20Sopenharmony_ci U32 nbBytes = bitD->bitsConsumed >> 3; 3598c2ecf20Sopenharmony_ci BIT_DStream_status result = BIT_DStream_unfinished; 3608c2ecf20Sopenharmony_ci if (bitD->ptr - nbBytes < bitD->start) { 3618c2ecf20Sopenharmony_ci nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ 3628c2ecf20Sopenharmony_ci result = BIT_DStream_endOfBuffer; 3638c2ecf20Sopenharmony_ci } 3648c2ecf20Sopenharmony_ci bitD->ptr -= nbBytes; 3658c2ecf20Sopenharmony_ci bitD->bitsConsumed -= nbBytes * 8; 3668c2ecf20Sopenharmony_ci bitD->bitContainer = ZSTD_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */ 3678c2ecf20Sopenharmony_ci return result; 3688c2ecf20Sopenharmony_ci } 3698c2ecf20Sopenharmony_ci} 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci/*! BIT_endOfDStream() : 3728c2ecf20Sopenharmony_ci* @return Tells if DStream has exactly reached its end (all bits consumed). 3738c2ecf20Sopenharmony_ci*/ 3748c2ecf20Sopenharmony_ciZSTD_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t *DStream) 3758c2ecf20Sopenharmony_ci{ 3768c2ecf20Sopenharmony_ci return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer) * 8)); 3778c2ecf20Sopenharmony_ci} 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci#endif /* BITSTREAM_H_MODULE */ 380