/* ******************************************************************
bitstream
- Part of NewGen Entropy library
+ Part of FSE library
header file (to include)
- Copyright (C) 2013-2015, Yann Collet.
+ Copyright (C) 2013-2016, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
You can contact the author at :
- Source repository : https://github.com/Cyan4973/FiniteStateEntropy
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
****************************************************************** */
#ifndef BITSTREAM_H_MODULE
#define BITSTREAM_H_MODULE
* these functions are defined into a .h to be included.
*/
-/******************************************
-* Includes
+/*-****************************************
+* Dependencies
******************************************/
#include "mem.h" /* unaligned access routines */
#include "error_private.h" /* error codes and messages */
-/********************************************
-* bitStream compression API (write forward)
+/*-******************************************
+* bitStream encoding API (write forward)
********************************************/
-/*
+/*!
* bitStream can mix input from multiple sources.
* A critical property of these streams is that they encode and decode in **reverse** direction.
* So the first bit sequence you add will be the last to be read, like a LIFO stack.
char* endPtr;
} BIT_CStream_t;
-MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t maxDstSize);
+MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity);
MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC);
MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
-/*
-* Start by initCStream, providing the maximum size of write buffer to write into.
+/*!
+* Start by initCStream, providing the size of buffer to write into.
* bitStream will never write outside of this buffer.
-* buffer must be at least as large as a size_t, otherwise function result will be an error code.
+* @dstCapacity must be >= sizeof(size_t), otherwise @return will be an error code.
*
* bits are first added to a local register.
* Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
-* Writing data into memory is a manual operation, performed by the flushBits function.
+* Writing data into memory is an explicit operation, performed by the flushBits function.
* Hence keep track how many bits are potentially stored into local register to avoid register overflow.
* After a flushBits, a maximum of 7 bits might still be stored into local register.
*
-* Avoid storing elements of more than 25 bits if you want compatibility with 32-bits bitstream readers.
+* Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers.
*
* Last operation is to close the bitStream.
* The function returns the final size of CStream in bytes.
-* If data couldn't fit into dstBuffer, it will return a 0 ( == not storable)
+* If data couldn't fit into @dstBuffer, it will return a 0 ( == not storable)
*/
-/**********************************************
-* bitStream decompression API (read backward)
+/*-********************************************
+* bitStream decoding API (read backward)
**********************************************/
typedef struct
{
MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
-/*
+/*!
* Start by invoking BIT_initDStream().
* A chunk of the bitStream is then stored into a local register.
* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
* You can then retrieve bitFields stored into the local register, **in reverse order**.
-* Local register is manually filled from memory by the BIT_reloadDStream() method.
+* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
* A reload guarantee a minimum of ((8*sizeof(size_t))-7) bits when its result is BIT_DStream_unfinished.
* Otherwise, it can be less than that, so proceed accordingly.
* Checking if DStream has reached its end can be performed with BIT_endOfDStream()
*/
-/******************************************
+/*-****************************************
* unsafe API
******************************************/
MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits);
-/****************************************************************
+/*-**************************************************************
* Helper functions
****************************************************************/
MEM_STATIC unsigned BIT_highbit32 (register U32 val)
}
-/****************************************************************
+/*-**************************************************************
* bitStream encoding
****************************************************************/
-
MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, size_t maxSize)
{
bitC->bitContainer = 0;
}
-/**********************************************************
+/*-********************************************************
* bitStream decoding
**********************************************************/
-
/*!BIT_initDStream
* Initialize a BIT_DStream_t.
* @bitD : a pointer to an already allocated BIT_DStream_t structure
{
if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); }
- if (srcSize >= sizeof(size_t)) /* normal case */
- {
+ if (srcSize >= sizeof(size_t)) { /* normal case */
U32 contain32;
bitD->start = (const char*)srcBuffer;
bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t);
contain32 = ((const BYTE*)srcBuffer)[srcSize-1];
if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */
bitD->bitsConsumed = 8 - BIT_highbit32(contain32);
- }
- else
- {
+ } else {
U32 contain32;
bitD->start = (const char*)srcBuffer;
bitD->ptr = bitD->start;
if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
return BIT_DStream_overflow;
- if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer))
- {
+ if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) {
bitD->ptr -= bitD->bitsConsumed >> 3;
bitD->bitsConsumed &= 7;
bitD->bitContainer = MEM_readLEST(bitD->ptr);
return BIT_DStream_unfinished;
}
- if (bitD->ptr == bitD->start)
- {
+ if (bitD->ptr == bitD->start) {
if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
return BIT_DStream_completed;
}
{
U32 nbBytes = bitD->bitsConsumed >> 3;
BIT_DStream_status result = BIT_DStream_unfinished;
- if (bitD->ptr - nbBytes < bitD->start)
- {
+ if (bitD->ptr - nbBytes < bitD->start) {
nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
result = BIT_DStream_endOfBuffer;
}
/*!
FSE_buildCTable():
- Builds 'ct', which must be already allocated, using FSE_createCTable()
+ Builds @ct, which must be already allocated, using FSE_createCTable()
return : 0
or an errorCode, which can be tested using FSE_isError() */
size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
/*!
FSE_compress_usingCTable():
- Compress 'src' using 'ct' into 'dst' which must be already allocated
- return : size of compressed data (<= maxDstSize)
- or 0 if compressed data could not fit into 'dst'
+ Compress @src using @ct into @dst which must be already allocated
+ return : size of compressed data (<= @dstCapacity)
+ or 0 if compressed data could not fit into @dst
or an errorCode, which can be tested using FSE_isError() */
-size_t FSE_compress_usingCTable (void* dst, size_t maxDstSize, const void* src, size_t srcSize, const FSE_CTable* ct);
+size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct);
/*!
Tutorial :
'CTable' can then be used to compress 'src', with FSE_compress_usingCTable().
Similar to FSE_count(), the convention is that 'src' is assumed to be a table of char of size 'srcSize'
-The function returns the size of compressed data (without header), necessarily <= maxDstSize.
+The function returns the size of compressed data (without header), necessarily <= @dstCapacity.
If it returns '0', compressed data could not fit into 'dst'.
If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()).
*/
/*!
FSE_decompress_usingDTable():
- Decompress compressed source 'cSrc' of size 'cSrcSize' using 'dt'
- into 'dst' which must be already allocated.
- return : size of regenerated data (necessarily <= maxDstSize)
+ Decompress compressed source @cSrc of size @cSrcSize using @dt
+ into @dst which must be already allocated.
+ return : size of regenerated data (necessarily <= @dstCapacity)
or an errorCode, which can be tested using FSE_isError() */
-size_t FSE_decompress_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt);
+size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt);
/*!
Tutorial :
extern "C" {
#endif
-/******************************************
-* Includes
+/*-****************************************
+* Dependencies
******************************************/
#include <stddef.h> /* size_t, ptrdiff_t */
#include <string.h> /* memcpy */
-/******************************************
-* Compiler-specific
+/*-****************************************
+* Compiler specifics
******************************************/
#if defined(__GNUC__)
# define MEM_STATIC static __attribute__((unused))
#endif
-/****************************************************************
+/*-**************************************************************
* Basic Types
*****************************************************************/
#if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
#endif
-/****************************************************************
+/*-**************************************************************
* Memory I/O
*****************************************************************/
-/* MEM_FORCE_MEMORY_ACCESS
+/*!MEM_FORCE_MEMORY_ACCESS
* By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
* Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
* The below switch allow to select different access method for improved performance.
* Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
* This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
* Method 2 : direct access. This method is portable but violate C standard.
- * It can generate buggy code on targets generating assembly depending on alignment.
- * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
+ * It can generate buggy code on targets depending on alignment.
+ * In some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
* See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
* Prefer these methods in priority order (0 > 1 > 2)
*/
{
if (MEM_isLittleEndian())
return MEM_read16(memPtr);
- else
- {
+ else {
const BYTE* p = (const BYTE*)memPtr;
return (U16)(p[0] + (p[1]<<8));
}
MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
{
- if (MEM_isLittleEndian())
- {
+ if (MEM_isLittleEndian()) {
MEM_write16(memPtr, val);
- }
- else
- {
+ } else {
BYTE* p = (BYTE*)memPtr;
p[0] = (BYTE)val;
p[1] = (BYTE)(val>>8);
{
if (MEM_isLittleEndian())
return MEM_read32(memPtr);
- else
- {
+ else {
const BYTE* p = (const BYTE*)memPtr;
return (U32)((U32)p[0] + ((U32)p[1]<<8) + ((U32)p[2]<<16) + ((U32)p[3]<<24));
}
MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)
{
- if (MEM_isLittleEndian())
- {
+ if (MEM_isLittleEndian()) {
MEM_write32(memPtr, val32);
- }
- else
- {
+ } else {
BYTE* p = (BYTE*)memPtr;
p[0] = (BYTE)val32;
p[1] = (BYTE)(val32>>8);
{
if (MEM_isLittleEndian())
return MEM_read64(memPtr);
- else
- {
+ else {
const BYTE* p = (const BYTE*)memPtr;
return (U64)((U64)p[0] + ((U64)p[1]<<8) + ((U64)p[2]<<16) + ((U64)p[3]<<24)
+ ((U64)p[4]<<32) + ((U64)p[5]<<40) + ((U64)p[6]<<48) + ((U64)p[7]<<56));
MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)
{
- if (MEM_isLittleEndian())
- {
+ if (MEM_isLittleEndian()) {
MEM_write64(memPtr, val64);
- }
- else
- {
+ } else {
BYTE* p = (BYTE*)memPtr;
p[0] = (BYTE)val64;
p[1] = (BYTE)(val64>>8);
*/
/* *************************************
-* Includes
+* Dependencies
***************************************/
#include <stdlib.h>
#include "error_private.h"
#include "zstd_buffered_static.h"
+/* *************************************
+* Constants
+***************************************/
+static size_t ZBUFF_blockHeaderSize = 3;
+static size_t ZBUFF_endFrameSize = 3;
+
/** ************************************************
* Streaming compression
*
{
size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zbc->zc);
- if (nextSrcSizeHint > 3) nextSrcSizeHint+= 3; /* get the next block header while at it */
+ if (nextSrcSizeHint > ZBUFF_blockHeaderSize) nextSrcSizeHint+= ZBUFF_blockHeaderSize; /* get next block header too */
nextSrcSizeHint -= zbc->inPos; /* already loaded*/
return nextSrcSizeHint;
}
unsigned ZBUFF_isError(size_t errorCode) { return ERR_isError(errorCode); }
const char* ZBUFF_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
-size_t ZBUFF_recommendedCInSize() { return BLOCKSIZE; }
-size_t ZBUFF_recommendedCOutSize() { return ZSTD_compressBound(BLOCKSIZE) + 6; }
-size_t ZBUFF_recommendedDInSize() { return BLOCKSIZE + 3; }
-size_t ZBUFF_recommendedDOutSize() { return BLOCKSIZE; }
+size_t ZBUFF_recommendedCInSize(void) { return BLOCKSIZE; }
+size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_compressBound(BLOCKSIZE) + ZBUFF_blockHeaderSize + ZBUFF_endFrameSize; }
+size_t ZBUFF_recommendedDInSize(void) { return BLOCKSIZE + ZBUFF_blockHeaderSize /* block header size*/ ; }
+size_t ZBUFF_recommendedDOutSize(void) { return BLOCKSIZE; }
/* *************************************
-* Includes
+* Dependencies
***************************************/
#include <stdlib.h> /* malloc */
#include <string.h> /* memset */
#include "mem.h"
#include "fse_static.h"
#include "huff0_static.h"
-#include "zstd_static.h"
#include "zstd_internal.h"
/*- Pre-defined compression levels -*/
-#define ZSTD_MAX_CLEVEL 20
unsigned ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; }
static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = {
/*
zstd - standard compression library
- Copyright (C) 2014-2015, Yann Collet.
+ Copyright (C) 2014-2016, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
You can contact the author at :
- zstd source repository : https://github.com/Cyan4973/zstd
- - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
*/
/* ***************************************************************
/*!
* LEGACY_SUPPORT :
-* ZSTD_decompress() can decode older formats (v0.1+) if set to 1
+* if set to 1, ZSTD_decompress() can decode older formats (v0.1+)
*/
#ifndef ZSTD_LEGACY_SUPPORT
# define ZSTD_LEGACY_SUPPORT 0
#include <string.h> /* memcpy, memmove */
#include <stdio.h> /* debug : printf */
#include "mem.h" /* low level memory routines */
-#include "zstd_static.h"
#include "zstd_internal.h"
#include "fse_static.h"
#include "huff0_static.h"
/* *************************************
* Advanced functions
***************************************/
+#define ZSTD_MAX_CLEVEL 20
ZSTDLIB_API unsigned ZSTD_maxCLevel (void);
/*! ZSTD_getParams