Merge pull request #167 from Cyan4973/dev

v0.6.0
This commit is contained in:
Yann Collet 2016-04-13 01:28:41 +02:00
commit d0407789f6
56 changed files with 9225 additions and 3836 deletions

5
.gitignore vendored
View File

@ -20,6 +20,7 @@
# Visual solution files # Visual solution files
*.suo *.suo
*.user *.user
*.VC.db
# Build results # Build results
[Dd]ebug/ [Dd]ebug/
@ -46,7 +47,3 @@ ipch/
.directory .directory
_codelite _codelite
_zstdbench _zstdbench
lib/zstd_opt_LZ5.c
lib/zstd_opt_llen.c
lib/zstd_opt_nollen.c

View File

@ -89,9 +89,8 @@ gpptest: clean
$(MAKE) all CC=g++ CFLAGS="-O3 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror" $(MAKE) all CC=g++ CFLAGS="-O3 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror"
armtest: clean armtest: clean
# $(MAKE) -C $(ZSTDDIR) all CC=arm-linux-gnueabi-gcc MOREFLAGS="-Werror"
$(MAKE) -C $(PRGDIR) datagen # use native, faster $(MAKE) -C $(PRGDIR) datagen # use native, faster
$(MAKE) -C $(PRGDIR) test CC=arm-linux-gnueabi-gcc ZSTDRTTEST= MOREFLAGS=-static # MOREFLAGS="-Werror -static" $(MAKE) -C $(PRGDIR) test CC=arm-linux-gnueabi-gcc ZSTDRTTEST= MOREFLAGS="-Werror -static"
# for Travis CI # for Travis CI
arminstall: clean arminstall: clean
@ -105,7 +104,7 @@ armtest-w-install: clean arminstall armtest
ppctest: clean ppctest: clean
$(MAKE) -C $(PRGDIR) datagen # use native, faster $(MAKE) -C $(PRGDIR) datagen # use native, faster
$(MAKE) -C $(PRGDIR) test CC=powerpc-linux-gnu-gcc ZSTDRTTEST= MOREFLAGS=-static # MOREFLAGS="-Werror -static" $(MAKE) -C $(PRGDIR) test CC=powerpc-linux-gnu-gcc ZSTDRTTEST= MOREFLAGS="-Werror -static"
# for Travis CI # for Travis CI
ppcinstall: clean ppcinstall: clean

6
NEWS
View File

@ -1,3 +1,9 @@
v0.6.0
Stronger high compression modes, thanks to Przemyslaw Skibinski
API : ZSTD_getFrameParams() provides size of decompressed content
New : highest compression modes require `--ultra` command to fully unleash their capacity
Fixed : zstd cli return error code > 0 and removes dst file artifact when decompression fails, thanks to Chip Turner
v0.5.1 v0.5.1
New : Optimal parsing => Very high compression modes, thanks to Przemyslaw Skibinski New : Optimal parsing => Very high compression modes, thanks to Przemyslaw Skibinski
Changed : Dictionary builder integrated into libzstd and zstd cli Changed : Dictionary builder integrated into libzstd and zstd cli

View File

@ -16,7 +16,7 @@ As a reference, several fast compression algorithms were tested and compared to
|Name | Ratio | C.speed | D.speed | |Name | Ratio | C.speed | D.speed |
|-----------------|-------|--------:|--------:| |-----------------|-------|--------:|--------:|
| | | MB/s | MB/s | | | | MB/s | MB/s |
|**zstd 0.5.1 -1**|**2.876**|**330**| **890** | |**zstd 0.6.0 -1**|**2.877**|**330**| **915** |
| [zlib] 1.2.8 -1 | 2.730 | 95 | 360 | | [zlib] 1.2.8 -1 | 2.730 | 95 | 360 |
| brotli -0 | 2.708 | 220 | 430 | | brotli -0 | 2.708 | 220 | 430 |
| QuickLZ 1.5 | 2.237 | 510 | 605 | | QuickLZ 1.5 | 2.237 | 510 | 605 |
@ -43,33 +43,18 @@ For a larger picture including very slow modes, [click on this link](images/DCsp
### The case for Small Data compression ### The case for Small Data compression
Above chart provides results applicable to large files or large streams scenarios (200 MB for this case). Previous charts provide results applicable to typical files and streams scenarios (several MB). Small data come with different perspectives. The smaller the amount of data to compress, the more difficult it is to achieve any significant compression.
Small data (< 64 KB) come with different perspectives.
The smaller the amount of data to compress, the more difficult it is to achieve any significant compression.
On reaching the 1 KB region, it becomes almost impossible to compress anything.
This problem is common to any compression algorithms, and throwing CPU power at it achieves little gains.
The reason is, compression algorithms learn from past data how to compress future data. This problem is common to any compression algorithm. The reason is, compression algorithms learn from past data how to compress future data. But at the beginning of a new file, there is no "past" to build upon.
But at the beginning of a new file, there is no "past" to build upon.
To solve this situation, Zstd now offers a __training mode__, To solve this situation, Zstd offers a __training mode__, which can be used to tune the algorithm for a selected type of data, by providing it with a few samples. The result of the training is stored in a file called "dictionary", which can be loaded before compression and decompression. Using this dictionary, the compression ratio achievable on small data improves dramatically :
which can be used to make the algorithm fit a selected type of data, by providing it with some samples.
The result of the training is a file called "dictionary", which can be loaded before compression and decompression.
Using this dictionary, the compression ratio achievable on small data improves dramatically :
| Collection Name | Direct compression | Dictionary Compression | Gains | Average unit | Range | ![Compressing Small Data](images/smallData.png "Compressing Small Data")
| --------------- | ------------------ | ---------------------- | --------- | ------------:| ----- |
| Small JSON records | x1.331 - x1.366 | x5.860 - x6.830 | ~ __x4.7__ | 300 | 200 - 400 |
| Mercurial events | x2.322 - x2.538 | x3.377 - x4.462 | ~ __x1.5__ | 1.5 KB | 20 - 200 KB |
| Large JSON docs | x3.813 - x4.043 | x8.935 - x13.366 | ~ __x2.8__ | 6 KB | 800 - 20 KB |
These compression gains are achieved without any speed loss, and prove in general a bit faster to compress and decompress. These compression gains are achieved while simultaneously providing faster compression and decompression speeds.
Dictionary work if there is some correlation in a family of small data (there is no _universal dictionary_). Dictionary work if there is some correlation in a family of small data (there is no _universal dictionary_).
Hence, deploying one dictionary per type of data will provide the greater benefits. Hence, deploying one dictionary per type of data will provide the greater benefits. Dictionary gains are mostly effective in the first few KB. Then, the compression algorithm will rely more and more on previously decoded content to compress the rest of the file.
Large documents will benefit proportionally less, since dictionary gains are mostly effective in the first few KB.
Then, the compression algorithm will rely more and more on already decoded content to compress the rest of the file.
#### Dictionary compression How To : #### Dictionary compression How To :
@ -79,7 +64,7 @@ Then, the compression algorithm will rely more and more on already decoded conte
`zstd --train FullPathToTrainingSet/* -o dictionaryName` `zstd --train FullPathToTrainingSet/* -o dictionaryName`
2) Compression with dictionary 2) Compress with dictionary
`zstd FILE -D dictionaryName` `zstd FILE -D dictionaryName`
@ -87,53 +72,15 @@ Then, the compression algorithm will rely more and more on already decoded conte
`zstd --decompress FILE.zst -D dictionaryName` `zstd --decompress FILE.zst -D dictionaryName`
##### _Using API_ :
1) Create dictionary
```
#include "zdict.h"
(...)
/* Train a dictionary from a memory buffer `samplesBuffer`,
where `nbSamples` samples have been stored concatenated. */
size_t dictSize = ZDICT_trainFromBuffer(dictBuffer, dictBufferCapacity,
samplesBuffer, samplesSizes, nbSamples);
```
2) Compression with dictionary
```
#include "zstd.h"
(...)
ZSTD_CCtx* context = ZSTD_createCCtx();
size_t compressedSize = ZSTD_compress_usingDict(context, dst, dstCapacity, src, srcSize, dict, dictSize, compressionLevel);
```
3) Decompress with dictionary
```
#include "zstd.h"
(...)
ZSTD_DCtx* context = ZSTD_createDCtx();
size_t regeneratedSize = ZSTD_decompress_usingDict(context, dst, dstCapacity, cSrc, cSrcSize, dict, dictSize);
```
### Status ### Status
Zstd has not yet reached "stable format" status. It doesn't guarantee yet that its current compression format will remain stable in future versions. During this period, it can still change to adapt new optimizations still being investigated. "Stable Format" is projected H1 2016, and will be tagged `v1.0`. Zstd is in development. The internal format evolves to reach better performance. "Final Format" is projected H1 2016, and will be tagged `v1.0`. Zstd offers legacy support, meaning any data compressed by any version >= 0.1 (therefore including current one) remain decodable in the future.
The library is also quite robust, able to withstand hazards situations, including invalid inputs. Library reliability has been tested using [Fuzz Testing](https://en.wikipedia.org/wiki/Fuzz_testing), with both [internal tools](programs/fuzzer.c) and [external ones](http://lcamtuf.coredump.cx/afl). Therefore, Zstandard is considered safe for production environments.
That being said, the library is now fairly robust, able to withstand hazards situations, including invalid inputs. It also features legacy support, so that documents compressed with current and previous version of zstd can still be decoded in the future.
Library reliability has been tested using [Fuzz Testing](https://en.wikipedia.org/wiki/Fuzz_testing), with both [internal tools](programs/fuzzer.c) and [external ones](http://lcamtuf.coredump.cx/afl). Therefore, Zstandard is considered safe for testings, even within production environments.
### Branch Policy ### Branch Policy
The "dev" branch is the one where all contributions will be merged before reaching "master". If you plan to propose a patch, please commit into the "dev" branch or its own feature branch. Direct commit to "master" are not permitted. The "dev" branch is the one where all contributions will be merged before reaching "master". If you plan to propose a patch, please commit into the "dev" branch or its own feature branch. Direct commit to "master" are not permitted.
### Miscellaneous
### Trivia
Zstd entropy stage is provided by [Huff0 and FSE, from Finite State Entropy library](https://github.com/Cyan4973/FiniteStateEntropy). Zstd entropy stage is provided by [Huff0 and FSE, from Finite State Entropy library](https://github.com/Cyan4973/FiniteStateEntropy).
Its memory requirement can be configured to fit into low-memory hardware configurations, or servers handling multiple connections/contexts in parallel.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.3 KiB

After

Width:  |  Height:  |  Size: 9.7 KiB

BIN
images/smallData.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

164
lib/.debug/zstd_stats.h Normal file
View File

@ -0,0 +1,164 @@
/*
zstd - standard compression library
Header File for static linking only
Copyright (C) 2014-2016, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at :
- zstd homepage : http://www.zstd.net
*/
#ifndef ZSTD_STATS_H
#define ZSTD_STATS_H
#if defined (__cplusplus)
extern "C" {
#endif
/*-*************************************
* Dependencies
***************************************/
//#include "zstd.h"
//#include "mem.h"
/*-*************************************
* Constants
***************************************/
//#define ZSTD_MAGICNUMBER 0xFD2FB526 /* v0.6 */
/*-*************************************
* Types
***************************************/
typedef struct {
U32 priceOffset, priceOffCode, priceMatchLength, priceLiteral, priceLitLength, priceDumpsLength;
U32 totalMatchSum, totalLitSum, totalSeqSum, totalRepSum;
U32 litSum, matchLengthSum, litLengthSum, offCodeSum;
U32 matchLengthFreq[1<<MLbits];
U32 litLengthFreq[1<<LLbits];
U32 litFreq[1<<Litbits];
U32 offCodeFreq[1<<Offbits];
} ZSTD_stats_t;
/*-*************************************
* Advanced functions
***************************************/
MEM_STATIC void ZSTD_statsPrint(ZSTD_stats_t* stats, U32 searchLength)
{
stats->totalMatchSum += stats->totalSeqSum * ((searchLength == 3) ? 3 : 4);
printf("avgMatchL=%.2f avgLitL=%.2f match=%.1f%% lit=%.1f%% reps=%d seq=%d\n", (float)stats->totalMatchSum/stats->totalSeqSum, (float)stats->totalLitSum/stats->totalSeqSum, 100.0*stats->totalMatchSum/(stats->totalMatchSum+stats->totalLitSum), 100.0*stats->totalLitSum/(stats->totalMatchSum+stats->totalLitSum), stats->totalRepSum, stats->totalSeqSum);
printf("SumBytes=%d Offset=%d OffCode=%d Match=%d Literal=%d LitLength=%d DumpsLength=%d\n", (stats->priceOffset+stats->priceOffCode+stats->priceMatchLength+stats->priceLiteral+stats->priceLitLength+stats->priceDumpsLength)/8, stats->priceOffset/8, stats->priceOffCode/8, stats->priceMatchLength/8, stats->priceLiteral/8, stats->priceLitLength/8, stats->priceDumpsLength/8);
}
MEM_STATIC void ZSTD_statsInit(ZSTD_stats_t* stats)
{
stats->totalLitSum = stats->totalMatchSum = stats->totalSeqSum = stats->totalRepSum = 1;
stats->priceOffset = stats->priceOffCode = stats->priceMatchLength = stats->priceLiteral = stats->priceLitLength = stats->priceDumpsLength = 0;
}
MEM_STATIC void ZSTD_statsResetFreqs(ZSTD_stats_t* stats)
{
unsigned u;
stats->litSum = (1<<Litbits);
stats->litLengthSum = (1<<LLbits);
stats->matchLengthSum = (1<<MLbits);
stats->offCodeSum = (1<<Offbits);
for (u=0; u<=MaxLit; u++)
stats->litFreq[u] = 1;
for (u=0; u<=MaxLL; u++)
stats->litLengthFreq[u] = 1;
for (u=0; u<=MaxML; u++)
stats->matchLengthFreq[u] = 1;
for (u=0; u<=MaxOff; u++)
stats->offCodeFreq[u] = 1;
}
MEM_STATIC void ZSTD_statsUpdatePrices(ZSTD_stats_t* stats, size_t litLength, const BYTE* literals, size_t offset, size_t matchLength)
{
/* offset */
BYTE offCode = offset ? (BYTE)ZSTD_highbit(offset+1) + 1 : 0;
stats->priceOffCode += ZSTD_highbit(stats->offCodeSum+1) - ZSTD_highbit(stats->offCodeFreq[offCode]+1);
stats->priceOffset += (offCode-1) + (!offCode);
/* match Length */
stats->priceDumpsLength += ((matchLength >= MaxML)<<3) + ((matchLength >= 255+MaxML)<<4) + ((matchLength>=(1<<15))<<3);
stats->priceMatchLength += ZSTD_highbit(stats->matchLengthSum+1) - ZSTD_highbit(stats->matchLengthFreq[(matchLength >= MaxML) ? MaxML : matchLength]+1);
if (litLength) {
/* literals */
U32 u;
stats->priceLiteral += litLength * ZSTD_highbit(stats->litSum+1);
for (u=0; u < litLength; u++)
stats->priceLiteral -= ZSTD_highbit(stats->litFreq[literals[u]]+1);
/* literal Length */
stats->priceDumpsLength += ((litLength >= MaxLL)<<3) + ((litLength >= 255+MaxLL)<<4) + ((litLength>=(1<<15))<<3);
stats->priceLitLength += ZSTD_highbit(stats->litLengthSum+1) - ZSTD_highbit(stats->litLengthFreq[(litLength >= MaxLL) ? MaxLL : litLength]+1);
} else {
stats->priceLitLength += ZSTD_highbit(stats->litLengthSum+1) - ZSTD_highbit(stats->litLengthFreq[0]+1);
}
if (offset == 0) stats->totalRepSum++;
stats->totalSeqSum++;
stats->totalMatchSum += matchLength;
stats->totalLitSum += litLength;
U32 u;
/* literals */
stats->litSum += litLength;
for (u=0; u < litLength; u++)
stats->litFreq[literals[u]]++;
/* literal Length */
stats->litLengthSum++;
if (litLength >= MaxLL)
stats->litLengthFreq[MaxLL]++;
else
stats->litLengthFreq[litLength]++;
/* match offset */
stats->offCodeSum++;
stats->offCodeFreq[offCode]++;
/* match Length */
stats->matchLengthSum++;
if (matchLength >= MaxML)
stats->matchLengthFreq[MaxML]++;
else
stats->matchLengthFreq[matchLength]++;
}
#if defined (__cplusplus)
}
#endif
#endif /* ZSTD_STATIC_H */

View File

@ -51,8 +51,8 @@ FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(MOREFLAGS)
LIBDIR ?= $(PREFIX)/lib LIBDIR ?= $(PREFIX)/lib
INCLUDEDIR=$(PREFIX)/include INCLUDEDIR=$(PREFIX)/include
ZSTD_FILES := zstd_compress.c zstd_decompress.c fse.c huff0.c zdict.c divsufsort.c ZSTD_FILES := zstd_compress.c zstd_decompress.c fse.c huff0.c zbuff.c zdict.c divsufsort.c
ZSTD_LEGACY:= legacy/zstd_v01.c legacy/zstd_v02.c legacy/zstd_v03.c legacy/zstd_v04.c ZSTD_LEGACY:= legacy/zstd_v01.c legacy/zstd_v02.c legacy/zstd_v03.c legacy/zstd_v04.c legacy/zstd_v05.c
ifeq ($(ZSTD_LEGACY_SUPPORT), 0) ifeq ($(ZSTD_LEGACY_SUPPORT), 0)
CPPFLAGS += -DZSTD_LEGACY_SUPPORT=0 CPPFLAGS += -DZSTD_LEGACY_SUPPORT=0
@ -118,8 +118,8 @@ install: libzstd libzstd.pc
@cp -a libzstd.pc $(DESTDIR)$(LIBDIR)/pkgconfig/ @cp -a libzstd.pc $(DESTDIR)$(LIBDIR)/pkgconfig/
@install -m 644 libzstd.a $(DESTDIR)$(LIBDIR)/libzstd.a @install -m 644 libzstd.a $(DESTDIR)$(LIBDIR)/libzstd.a
@install -m 644 zstd.h $(DESTDIR)$(INCLUDEDIR)/zstd.h @install -m 644 zstd.h $(DESTDIR)$(INCLUDEDIR)/zstd.h
@install -m 644 zstd.h $(DESTDIR)$(INCLUDEDIR)/zbuff.h @install -m 644 zbuff.h $(DESTDIR)$(INCLUDEDIR)/zbuff.h
@install -m 644 zstd.h $(DESTDIR)$(INCLUDEDIR)/zdict.h @install -m 644 zdict.h $(DESTDIR)$(INCLUDEDIR)/zdict.h
@echo zstd static and shared library installed @echo zstd static and shared library installed
uninstall: uninstall:
@ -129,8 +129,8 @@ uninstall:
@[ -x $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_VER) ] && rm -f $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_VER) @[ -x $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_VER) ] && rm -f $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_VER)
@[ -f $(DESTDIR)$(LIBDIR)/libzstd.a ] && rm -f $(DESTDIR)$(LIBDIR)/libzstd.a @[ -f $(DESTDIR)$(LIBDIR)/libzstd.a ] && rm -f $(DESTDIR)$(LIBDIR)/libzstd.a
@[ -f $(DESTDIR)$(INCLUDEDIR)/zstd.h ] && rm -f $(DESTDIR)$(INCLUDEDIR)/zstd.h @[ -f $(DESTDIR)$(INCLUDEDIR)/zstd.h ] && rm -f $(DESTDIR)$(INCLUDEDIR)/zstd.h
@[ -f $(DESTDIR)$(INCLUDEDIR)/zstd.h ] && rm -f $(DESTDIR)$(INCLUDEDIR)/zbuff.h @[ -f $(DESTDIR)$(INCLUDEDIR)/zbuff.h ] && rm -f $(DESTDIR)$(INCLUDEDIR)/zbuff.h
@[ -f $(DESTDIR)$(INCLUDEDIR)/zstd.h ] && rm -f $(DESTDIR)$(INCLUDEDIR)/zdict.h @[ -f $(DESTDIR)$(INCLUDEDIR)/zdict.h ] && rm -f $(DESTDIR)$(INCLUDEDIR)/zdict.h
@echo zstd libraries successfully uninstalled @echo zstd libraries successfully uninstalled
endif endif

View File

@ -13,6 +13,11 @@ The __lib__ directory contains several files, but depending on target use case,
##### zstd core compression ##### zstd core compression
Stable API is exposed in [zstd.h].
Advanced and experimental API is exposed in `zstd_static.h`.
`zstd_static.h` API elements should be used with static linking only,
as their definition may change in future version of the library.
- [bitstream.h](bitstream.h) - [bitstream.h](bitstream.h)
- fse.c - fse.c
- fse.h - fse.h
@ -24,13 +29,15 @@ The __lib__ directory contains several files, but depending on target use case,
- zstd_decompress.c - zstd_decompress.c
- zstd_internal.h - zstd_internal.h
- zstd_opt.h - zstd_opt.h
- zstd.h - [zstd.h]
- zstd_static.h - zstd_static.h
[zstd.h]: zstd.h
#### Buffered streaming #### Buffered streaming
This complementary API makes streaming integration easier. This complementary API makes streaming integration easier.
It is used by `zstd` command line utility : It is used by `zstd` command line utility, and [7zip plugin](http://mcmilk.de/projects/7-Zip-ZStd) :
- zbuff.c - zbuff.c
- zbuff.h - zbuff.h

View File

@ -41,7 +41,7 @@ extern "C" {
/* /*
* This API consists of small unitary functions, which highly benefit from being inlined. * This API consists of small unitary functions, which must be inlined for best performance.
* Since link-time-optimization is not available for all compilers, * Since link-time-optimization is not available for all compilers,
* these functions are defined into a .h to be included. * these functions are defined into a .h to be included.
*/ */
@ -53,13 +53,20 @@ extern "C" {
#include "error_private.h" /* error codes and messages */ #include "error_private.h" /* error codes and messages */
/*=========================================
* Target specific
=========================================*/
#if defined(__BMI__) && defined(__GNUC__)
# include <immintrin.h> /* support for bextr (experimental) */
#endif
/*-****************************************** /*-******************************************
* bitStream encoding API (write forward) * bitStream encoding API (write forward)
********************************************/ ********************************************/
/*! /* bitStream can mix input from multiple sources.
* bitStream can mix input from multiple sources. * A critical property of these streams is that they encode and decode in **reverse** direction.
* 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.
* So the first bit sequence you add will be the last to be read, like a LIFO stack.
*/ */
typedef struct typedef struct
{ {
@ -75,22 +82,21 @@ 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 void BIT_flushBits(BIT_CStream_t* bitC);
MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC); MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
/*! /* Start with initCStream, providing the size of buffer to write into.
* Start by initCStream, providing the size of buffer to write into. * bitStream will never write outside of this buffer.
* bitStream will never write outside of this buffer. * `dstCapacity` must be >= sizeof(size_t), otherwise @return 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. * 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. * Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems.
* Writing data into memory is an explicit 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. * 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. * After a flushBits, a maximum of 7 bits might still be stored into local register.
* *
* Avoid storing elements of more than 24 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. * Last operation is to close the bitStream.
* The function returns the final size of CStream in bytes. * 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)
*/ */
@ -117,15 +123,14 @@ MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD);
MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD); MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
/*! /* Start by invoking BIT_initDStream().
* Start by invoking BIT_initDStream(). * A chunk of the bitStream is then stored into a local register.
* 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).
* 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**.
* You can then retrieve bitFields stored into the local register, **in reverse order**. * Local register is explicitly reloaded 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.
* 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.
* Otherwise, it can be less than that, so proceed accordingly. * Checking if DStream has reached its end can be performed with BIT_endOfDStream().
* Checking if DStream has reached its end can be performed with BIT_endOfDStream()
*/ */
@ -144,7 +149,7 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
/*-************************************************************** /*-**************************************************************
* Helper functions * Internal functions
****************************************************************/ ****************************************************************/
MEM_STATIC unsigned BIT_highbit32 (register U32 val) MEM_STATIC unsigned BIT_highbit32 (register U32 val)
{ {
@ -168,29 +173,38 @@ MEM_STATIC unsigned BIT_highbit32 (register U32 val)
# endif # endif
} }
/*===== Local Constants =====*/
static const unsigned BIT_mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF }; /* up to 26 bits */
/*-************************************************************** /*-**************************************************************
* bitStream encoding * bitStream encoding
****************************************************************/ ****************************************************************/
MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, size_t maxSize) /*! BIT_initCStream() :
* `dstCapacity` must be > sizeof(void*)
* @return : 0 if success,
otherwise an error code (can be tested using ERR_isError() ) */
MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, size_t dstCapacity)
{ {
bitC->bitContainer = 0; bitC->bitContainer = 0;
bitC->bitPos = 0; bitC->bitPos = 0;
bitC->startPtr = (char*)startPtr; bitC->startPtr = (char*)startPtr;
bitC->ptr = bitC->startPtr; bitC->ptr = bitC->startPtr;
bitC->endPtr = bitC->startPtr + maxSize - sizeof(bitC->ptr); bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->ptr);
if (maxSize < sizeof(bitC->ptr)) return ERROR(dstSize_tooSmall); if (dstCapacity <= sizeof(bitC->ptr)) return ERROR(dstSize_tooSmall);
return 0; return 0;
} }
/*! BIT_addBits() :
can add up to 26 bits into `bitC`.
Does not check for register overflow ! */
MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits) MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
{ {
static const unsigned mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF }; /* up to 25 bits */ bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
bitC->bitContainer |= (value & mask[nbBits]) << bitC->bitPos;
bitC->bitPos += nbBits; bitC->bitPos += nbBits;
} }
/*! BIT_addBitsFast /*! BIT_addBitsFast() :
* works only if `value` is _clean_, meaning all high bits above nbBits are 0 */ * works only if `value` is _clean_, meaning all high bits above nbBits are 0 */
MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits) MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
{ {
@ -198,20 +212,23 @@ MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBi
bitC->bitPos += nbBits; bitC->bitPos += nbBits;
} }
/*! BIT_flushBitsFast /*! BIT_flushBitsFast() :
* unsafe version; does not check buffer overflow */ * unsafe version; does not check buffer overflow */
MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC) MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC)
{ {
size_t nbBytes = bitC->bitPos >> 3; size_t const nbBytes = bitC->bitPos >> 3;
MEM_writeLEST(bitC->ptr, bitC->bitContainer); MEM_writeLEST(bitC->ptr, bitC->bitContainer);
bitC->ptr += nbBytes; bitC->ptr += nbBytes;
bitC->bitPos &= 7; bitC->bitPos &= 7;
bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */ bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
} }
/*! BIT_flushBits() :
* safe version; check for buffer overflow, and prevents it.
* note : does not signal buffer overflow. This will be revealed later on using BIT_closeCStream() */
MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC) MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
{ {
size_t nbBytes = bitC->bitPos >> 3; size_t const nbBytes = bitC->bitPos >> 3;
MEM_writeLEST(bitC->ptr, bitC->bitContainer); MEM_writeLEST(bitC->ptr, bitC->bitContainer);
bitC->ptr += nbBytes; bitC->ptr += nbBytes;
if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr; if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr;
@ -219,49 +236,41 @@ MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC)
bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */ bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */
} }
/*! BIT_closeCStream /*! BIT_closeCStream() :
* @result : size of CStream, in bytes, or 0 if it cannot fit into dstBuffer */ * @return : size of CStream, in bytes,
or 0 if it could not fit into dstBuffer */
MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC) MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC)
{ {
char* endPtr;
BIT_addBitsFast(bitC, 1, 1); /* endMark */ BIT_addBitsFast(bitC, 1, 1); /* endMark */
BIT_flushBits(bitC); BIT_flushBits(bitC);
if (bitC->ptr >= bitC->endPtr) /* too close to buffer's end */ if (bitC->ptr >= bitC->endPtr) return 0; /* doesn't fit within authorized budget : cancel */
return 0; /* not storable */
endPtr = bitC->ptr; return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0);
endPtr += bitC->bitPos > 0; /* remaining bits (incomplete byte) */
return (endPtr - bitC->startPtr);
} }
/*-******************************************************** /*-********************************************************
* bitStream decoding * bitStream decoding
**********************************************************/ **********************************************************/
/*!BIT_initDStream /*! BIT_initDStream() :
* Initialize a BIT_DStream_t. * Initialize a BIT_DStream_t.
* @bitD : a pointer to an already allocated BIT_DStream_t structure * `bitD` : a pointer to an already allocated BIT_DStream_t structure.
* @srcBuffer must point at the beginning of a bitStream * `srcSize` must be the *exact* size of the bitStream, in bytes.
* @srcSize must be the exact size of the bitStream * @return : size of stream (== srcSize) or an errorCode if a problem is detected
* @result : size of stream (== srcSize) or an errorCode if a problem is detected
*/ */
MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize) MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize)
{ {
if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); } 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->start = (const char*)srcBuffer;
bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t); bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t);
bitD->bitContainer = MEM_readLEST(bitD->ptr); bitD->bitContainer = MEM_readLEST(bitD->ptr);
contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */ if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */
bitD->bitsConsumed = 8 - BIT_highbit32(contain32); bitD->bitsConsumed = 8 - BIT_highbit32(lastByte); }
} else { } else {
U32 contain32;
bitD->start = (const char*)srcBuffer; bitD->start = (const char*)srcBuffer;
bitD->ptr = bitD->start; bitD->ptr = bitD->start;
bitD->bitContainer = *(const BYTE*)(bitD->start); bitD->bitContainer = *(const BYTE*)(bitD->start);
@ -275,33 +284,56 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8; case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8;
default:; default:;
} }
contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; { BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */ if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */
bitD->bitsConsumed = 8 - BIT_highbit32(contain32); bitD->bitsConsumed = 8 - BIT_highbit32(lastByte); }
bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8; bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8;
} }
return srcSize; return srcSize;
} }
/*!BIT_lookBits MEM_STATIC size_t BIT_getUpperBits(size_t bitD, U32 const start)
* Provides next n bits from local register
* local register is not modified (bits are still present for next read/look)
* On 32-bits, maxNbBits==25
* On 64-bits, maxNbBits==57
* @return : value extracted
*/
MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits)
{ {
const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1; return bitD >> start;
return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
} }
/*! BIT_lookBitsFast : MEM_STATIC size_t BIT_getMiddleBits(size_t bitD, U32 const nbBits, U32 const start)
* unsafe version; only works only if nbBits >= 1 */
MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits)
{ {
const U32 bitMask = sizeof(bitD->bitContainer)*8 - 1; #if defined(__BMI__) && defined(__GNUC__) /* experimental */
return __builtin_ia32_bextr_u64(bitD, (nbBits<<8) | start );
#else
return (bitD >> start) & BIT_mask[nbBits];
#endif
}
MEM_STATIC size_t BIT_getLowerBits(size_t bitD, U32 const nbBits)
{
return bitD & BIT_mask[nbBits];
}
/*! BIT_lookBits() :
* Provides next n bits from local register.
* local register is not modified (bits are still present for next read/look).
* On 32-bits, maxNbBits==24.
* On 64-bits, maxNbBits==56.
* @return : value extracted
*/
MEM_STATIC size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
{
#if defined(__BMI__) && defined(__GNUC__) /* experimental */
return __builtin_ia32_bextr_u64(bitD->bitContainer, (nbBits<<8) | (64 - bitD->bitsConsumed - nbBits) );
#else
U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask);
#endif
}
/*! BIT_lookBitsFast() :
* unsafe version; only works only if nbBits >= 1 */
MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
{
U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask); return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
} }
@ -310,27 +342,32 @@ MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
bitD->bitsConsumed += nbBits; bitD->bitsConsumed += nbBits;
} }
/*!BIT_readBits /*! BIT_readBits() :
* Read next n bits from local register. * Read (consume) next n bits from local register and update.
* pay attention to not read more than nbBits contained into local register. * Pay attention to not read more than nbBits contained into local register.
* @return : extracted value. * @return : extracted value.
*/ */
MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits) MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
{ {
size_t value = BIT_lookBits(bitD, nbBits); size_t const value = BIT_lookBits(bitD, nbBits);
BIT_skipBits(bitD, nbBits); BIT_skipBits(bitD, nbBits);
return value; return value;
} }
/*!BIT_readBitsFast : /*! BIT_readBitsFast() :
* unsafe version; only works only if nbBits >= 1 */ * unsafe version; only works only if nbBits >= 1 */
MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits) MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
{ {
size_t value = BIT_lookBitsFast(bitD, nbBits); size_t const value = BIT_lookBitsFast(bitD, nbBits);
BIT_skipBits(bitD, nbBits); BIT_skipBits(bitD, nbBits);
return value; return value;
} }
/*! BIT_reloadDStream() :
* Refill `BIT_DStream_t` from src buffer previously defined (see BIT_initDStream() ).
* This function is safe, it guarantees it will not read beyond src buffer.
* @return : status of `BIT_DStream_t` internal register.
if status == unfinished, internal register is filled with >= (sizeof(size_t)*8 - 7) bits */
MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
{ {
if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */ if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */
@ -346,8 +383,7 @@ MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer; if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
return BIT_DStream_completed; return BIT_DStream_completed;
} }
{ { U32 nbBytes = bitD->bitsConsumed >> 3;
U32 nbBytes = bitD->bitsConsumed >> 3;
BIT_DStream_status result = BIT_DStream_unfinished; 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 */ nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */
@ -360,8 +396,8 @@ MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
} }
} }
/*! BIT_endOfDStream /*! BIT_endOfDStream() :
* @return Tells if DStream has reached its exact end * @return Tells if DStream has exactly reached its end (all bits consumed).
*/ */
MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream) MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream)
{ {

View File

@ -28,7 +28,7 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at : You can contact the author at :
- Source repository : https://github.com/Cyan4973/zstd - Homepage : http://www.zstd.net
****************************************************************** */ ****************************************************************** */
/* Note : this module is expected to remain private, do not expose it */ /* Note : this module is expected to remain private, do not expose it */
@ -62,7 +62,7 @@ extern "C" {
/*-**************************************** /*-****************************************
* Customization * Customization (error_public.h)
******************************************/ ******************************************/
typedef ZSTD_ErrorCode ERR_enum; typedef ZSTD_ErrorCode ERR_enum;
#define PREFIX(name) ZSTD_error_##name #define PREFIX(name) ZSTD_error_##name
@ -74,7 +74,7 @@ typedef ZSTD_ErrorCode ERR_enum;
#ifdef ERROR #ifdef ERROR
# undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */ # undef ERROR /* reported already defined on VS 2015 (Rich Geldreich) */
#endif #endif
#define ERROR(name) (size_t)-PREFIX(name) #define ERROR(name) ((size_t)-PREFIX(name))
ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); } ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
@ -95,18 +95,19 @@ ERR_STATIC const char* ERR_getErrorName(size_t code)
case PREFIX(prefix_unknown): return "Unknown frame descriptor"; case PREFIX(prefix_unknown): return "Unknown frame descriptor";
case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter"; case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter";
case PREFIX(frameParameter_unsupportedBy32bits): return "Frame parameter unsupported in 32-bits mode"; case PREFIX(frameParameter_unsupportedBy32bits): return "Frame parameter unsupported in 32-bits mode";
case PREFIX(compressionParameter_unsupported): return "Compression parameter is out of bound";
case PREFIX(init_missing): return "Context should be init first"; case PREFIX(init_missing): return "Context should be init first";
case PREFIX(memory_allocation): return "Allocation error : not enough memory"; case PREFIX(memory_allocation): return "Allocation error : not enough memory";
case PREFIX(stage_wrong): return "Operation not authorized at current processing stage"; case PREFIX(stage_wrong): return "Operation not authorized at current processing stage";
case PREFIX(dstSize_tooSmall): return "Destination buffer is too small"; case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
case PREFIX(srcSize_wrong): return "Src size incorrect"; case PREFIX(srcSize_wrong): return "Src size incorrect";
case PREFIX(corruption_detected): return "Corrupted block detected"; case PREFIX(corruption_detected): return "Corrupted block detected";
case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory"; case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported";
case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max possible Symbol Value : too large"; case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large";
case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small"; case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
case PREFIX(dictionary_corrupted): return "Dictionary is corrupted"; case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
case PREFIX(maxCode): case PREFIX(maxCode):
default: return notErrorCode; /* should be impossible, due to ERR_getError() */ default: return notErrorCode; /* impossible, due to ERR_getError() */
} }
} }

View File

@ -28,7 +28,7 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at : You can contact the author at :
- Source repository : https://github.com/Cyan4973/zstd - Homepage : http://www.zstd.net
****************************************************************** */ ****************************************************************** */
#ifndef ERROR_PUBLIC_H_MODULE #ifndef ERROR_PUBLIC_H_MODULE
#define ERROR_PUBLIC_H_MODULE #define ERROR_PUBLIC_H_MODULE
@ -47,6 +47,7 @@ typedef enum {
ZSTD_error_prefix_unknown, ZSTD_error_prefix_unknown,
ZSTD_error_frameParameter_unsupported, ZSTD_error_frameParameter_unsupported,
ZSTD_error_frameParameter_unsupportedBy32bits, ZSTD_error_frameParameter_unsupportedBy32bits,
ZSTD_error_compressionParameter_unsupported,
ZSTD_error_init_missing, ZSTD_error_init_missing,
ZSTD_error_memory_allocation, ZSTD_error_memory_allocation,
ZSTD_error_stage_wrong, ZSTD_error_stage_wrong,
@ -60,8 +61,7 @@ typedef enum {
ZSTD_error_maxCode ZSTD_error_maxCode
} ZSTD_ErrorCode; } ZSTD_ErrorCode;
/* note : functions provide error codes in reverse negative order, /* note : compare with size_t function results using ZSTD_getError() */
so compare with (size_t)(0-enum) */
#if defined (__cplusplus) #if defined (__cplusplus)

215
lib/fse.c
View File

@ -145,21 +145,18 @@ static U32 FSE_tableStep(U32 tableSize) { return (tableSize>>1) + (tableSize>>3)
size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog) size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
{ {
const unsigned tableSize = 1 << tableLog; U32 const tableSize = 1 << tableLog;
const unsigned tableMask = tableSize - 1; U32 const tableMask = tableSize - 1;
void* const ptr = ct; void* const ptr = ct;
U16* const tableU16 = ( (U16*) ptr) + 2; U16* const tableU16 = ( (U16*) ptr) + 2;
void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableLog ? tableSize>>1 : 1) ; void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableLog ? tableSize>>1 : 1) ;
FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT); FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);
const unsigned step = FSE_tableStep(tableSize); U32 const step = FSE_tableStep(tableSize);
unsigned cumul[FSE_MAX_SYMBOL_VALUE+2]; U32 cumul[FSE_MAX_SYMBOL_VALUE+2];
U32 position = 0;
FSE_FUNCTION_TYPE tableSymbol[FSE_MAX_TABLESIZE]; /* memset() is not necessary, even if static analyzer complain about it */ FSE_FUNCTION_TYPE tableSymbol[FSE_MAX_TABLESIZE]; /* memset() is not necessary, even if static analyzer complain about it */
U32 highThreshold = tableSize-1; U32 highThreshold = tableSize-1;
unsigned symbol;
unsigned i;
/* header */ /* CTable header */
tableU16[-2] = (U16) tableLog; tableU16[-2] = (U16) tableLog;
tableU16[-1] = (U16) maxSymbolValue; tableU16[-1] = (U16) maxSymbolValue;
@ -167,42 +164,44 @@ size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned
* http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */ * http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */
/* symbol start positions */ /* symbol start positions */
cumul[0] = 0; { U32 u;
for (i=1; i<=maxSymbolValue+1; i++) { cumul[0] = 0;
if (normalizedCounter[i-1]==-1) { /* Low proba symbol */ for (u=1; u<=maxSymbolValue+1; u++) {
cumul[i] = cumul[i-1] + 1; if (normalizedCounter[u-1]==-1) { /* Low proba symbol */
tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(i-1); cumul[u] = cumul[u-1] + 1;
} else { tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(u-1);
cumul[i] = cumul[i-1] + normalizedCounter[i-1]; } else {
} } cumul[u] = cumul[u-1] + normalizedCounter[u-1];
cumul[maxSymbolValue+1] = tableSize+1; } }
cumul[maxSymbolValue+1] = tableSize+1;
/* Spread symbols */
for (symbol=0; symbol<=maxSymbolValue; symbol++) {
int nbOccurences;
for (nbOccurences=0; nbOccurences<normalizedCounter[symbol]; nbOccurences++) {
tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;
position = (position + step) & tableMask;
while (position > highThreshold) position = (position + step) & tableMask; /* Low proba area */
} }
if (position!=0) return ERROR(GENERIC); /* Must have gone through all positions */
/* Build table */
for (i=0; i<tableSize; i++) {
FSE_FUNCTION_TYPE s = tableSymbol[i]; /* note : static analyzer may not understand tableSymbol is properly initialized */
tableU16[cumul[s]++] = (U16) (tableSize+i); /* TableU16 : sorted by symbol order; gives next state value */
} }
/* Spread symbols */
{ U32 position = 0;
U32 symbol;
for (symbol=0; symbol<=maxSymbolValue; symbol++) {
int nbOccurences;
for (nbOccurences=0; nbOccurences<normalizedCounter[symbol]; nbOccurences++) {
tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;
position = (position + step) & tableMask;
while (position > highThreshold) position = (position + step) & tableMask; /* Low proba area */
} }
if (position!=0) return ERROR(GENERIC); /* Must have gone through all positions */
}
/* Build table */
{ U32 u; for (u=0; u<tableSize; u++) {
FSE_FUNCTION_TYPE s = tableSymbol[u]; /* note : static analyzer may not understand tableSymbol is properly initialized */
tableU16[cumul[s]++] = (U16) (tableSize+u); /* TableU16 : sorted by symbol order; gives next state value */
}}
/* Build Symbol Transformation Table */ /* Build Symbol Transformation Table */
{ { unsigned total = 0;
unsigned s; unsigned s;
unsigned total = 0;
for (s=0; s<=maxSymbolValue; s++) { for (s=0; s<=maxSymbolValue; s++) {
switch (normalizedCounter[s]) switch (normalizedCounter[s])
{ {
case 0: case 0: break;
break;
case -1: case -1:
case 1: case 1:
symbolTT[s].deltaNbBits = (tableLog << 16) - (1<<tableLog); symbolTT[s].deltaNbBits = (tableLog << 16) - (1<<tableLog);
@ -211,8 +210,8 @@ size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned
break; break;
default : default :
{ {
U32 maxBitsOut = tableLog - BIT_highbit32 (normalizedCounter[s]-1); U32 const maxBitsOut = tableLog - BIT_highbit32 (normalizedCounter[s]-1);
U32 minStatePlus = normalizedCounter[s] << maxBitsOut; U32 const minStatePlus = normalizedCounter[s] << maxBitsOut;
symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus; symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus;
symbolTT[s].deltaFindState = total - normalizedCounter[s]; symbolTT[s].deltaFindState = total - normalizedCounter[s];
total += normalizedCounter[s]; total += normalizedCounter[s];
@ -242,9 +241,8 @@ size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned
const U32 tableMask = tableSize-1; const U32 tableMask = tableSize-1;
const U32 step = FSE_tableStep(tableSize); const U32 step = FSE_tableStep(tableSize);
U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1]; U16 symbolNext[FSE_MAX_SYMBOL_VALUE+1];
U32 position = 0;
U32 highThreshold = tableSize-1; U32 highThreshold = tableSize-1;
const S16 largeLimit= (S16)(1 << (tableLog-1)); S16 const largeLimit= (S16)(1 << (tableLog-1));
U32 noLarge = 1; U32 noLarge = 1;
U32 s; U32 s;
@ -264,24 +262,24 @@ size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned
} } } }
/* Spread symbols */ /* Spread symbols */
for (s=0; s<=maxSymbolValue; s++) { { U32 position = 0;
int i; for (s=0; s<=maxSymbolValue; s++) {
for (i=0; i<normalizedCounter[s]; i++) { int i;
tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s; for (i=0; i<normalizedCounter[s]; i++) {
position = (position + step) & tableMask; tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s;
while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */ position = (position + step) & tableMask;
} } while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
} }
if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */ if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
}
/* Build Decoding table */ /* Build Decoding table */
{ { U32 u;
U32 i; for (u=0; u<tableSize; u++) {
for (i=0; i<tableSize; i++) { FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
FSE_FUNCTION_TYPE symbol = (FSE_FUNCTION_TYPE)(tableDecode[i].symbol);
U16 nextState = symbolNext[symbol]++; U16 nextState = symbolNext[symbol]++;
tableDecode[i].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) ); tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) );
tableDecode[i].newState = (U16) ( (nextState << tableDecode[i].nbBits) - tableSize); tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
} } } }
DTableH.fastMode = (U16)noLarge; DTableH.fastMode = (U16)noLarge;
@ -365,8 +363,7 @@ static size_t FSE_writeNCount_generic (void* header, size_t headerBufferSize,
bitStream >>= 16; bitStream >>= 16;
bitCount -= 16; bitCount -= 16;
} } } }
{ { short count = normalizedCounter[charnum++];
short count = normalizedCounter[charnum++];
const short max = (short)((2*threshold-1)-remaining); const short max = (short)((2*threshold-1)-remaining);
remaining -= FSE_abs(count); remaining -= FSE_abs(count);
if (remaining<1) return ERROR(GENERIC); if (remaining<1) return ERROR(GENERIC);
@ -465,8 +462,7 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
else else
bitStream >>= 2; bitStream >>= 2;
} }
{ { short const max = (short)((2*threshold-1)-remaining);
const short max = (short)((2*threshold-1)-remaining);
short count; short count;
if ((bitStream & (threshold-1)) < (U32)max) { if ((bitStream & (threshold-1)) < (U32)max) {
@ -509,11 +505,11 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
* Counting histogram * Counting histogram
****************************************************************/ ****************************************************************/
/*! FSE_count_simple /*! FSE_count_simple
This function just counts byte values within @src, This function just counts byte values within `src`,
and store the histogram into @count. and store the histogram into table `count`.
This function is unsafe : it doesn't check that all values within @src can fit into @count. This function is unsafe : it doesn't check that all values within `src` can fit into `count`.
For this reason, prefer using a table @count with 256 elements. For this reason, prefer using a table `count` with 256 elements.
@return : highest count for a single element @return : count of most numerous element
*/ */
static size_t FSE_count_simple(unsigned* count, unsigned* maxSymbolValuePtr, static size_t FSE_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,
const void* src, size_t srcSize) const void* src, size_t srcSize)
@ -522,7 +518,6 @@ static size_t FSE_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,
const BYTE* const end = ip + srcSize; const BYTE* const end = ip + srcSize;
unsigned maxSymbolValue = *maxSymbolValuePtr; unsigned maxSymbolValue = *maxSymbolValuePtr;
unsigned max=0; unsigned max=0;
U32 s;
memset(count, 0, (maxSymbolValue+1)*sizeof(*count)); memset(count, 0, (maxSymbolValue+1)*sizeof(*count));
if (srcSize==0) { *maxSymbolValuePtr = 0; return 0; } if (srcSize==0) { *maxSymbolValuePtr = 0; return 0; }
@ -532,7 +527,7 @@ static size_t FSE_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,
while (!count[maxSymbolValue]) maxSymbolValue--; while (!count[maxSymbolValue]) maxSymbolValue--;
*maxSymbolValuePtr = maxSymbolValue; *maxSymbolValuePtr = maxSymbolValue;
for (s=0; s<=maxSymbolValue; s++) if (count[s] > max) max = count[s]; { U32 s; for (s=0; s<=maxSymbolValue; s++) if (count[s] > max) max = count[s]; }
return (size_t)max; return (size_t)max;
} }
@ -546,7 +541,6 @@ static size_t FSE_count_parallel(unsigned* count, unsigned* maxSymbolValuePtr,
const BYTE* const iend = ip+sourceSize; const BYTE* const iend = ip+sourceSize;
unsigned maxSymbolValue = *maxSymbolValuePtr; unsigned maxSymbolValue = *maxSymbolValuePtr;
unsigned max=0; unsigned max=0;
U32 s;
U32 Counting1[256] = { 0 }; U32 Counting1[256] = { 0 };
U32 Counting2[256] = { 0 }; U32 Counting2[256] = { 0 };
@ -561,8 +555,8 @@ static size_t FSE_count_parallel(unsigned* count, unsigned* maxSymbolValuePtr,
} }
if (!maxSymbolValue) maxSymbolValue = 255; /* 0 == default */ if (!maxSymbolValue) maxSymbolValue = 255; /* 0 == default */
{ /* by stripes of 16 bytes */ /* by stripes of 16 bytes */
U32 cached = MEM_read32(ip); ip += 4; { U32 cached = MEM_read32(ip); ip += 4;
while (ip < iend-15) { while (ip < iend-15) {
U32 c = cached; cached = MEM_read32(ip); ip += 4; U32 c = cached; cached = MEM_read32(ip); ip += 4;
Counting1[(BYTE) c ]++; Counting1[(BYTE) c ]++;
@ -592,15 +586,15 @@ static size_t FSE_count_parallel(unsigned* count, unsigned* maxSymbolValuePtr,
while (ip<iend) Counting1[*ip++]++; while (ip<iend) Counting1[*ip++]++;
if (checkMax) { /* verify stats will fit into destination table */ if (checkMax) { /* verify stats will fit into destination table */
for (s=255; s>maxSymbolValue; s--) { U32 s; for (s=255; s>maxSymbolValue; s--) {
Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s]; Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s];
if (Counting1[s]) return ERROR(maxSymbolValue_tooSmall); if (Counting1[s]) return ERROR(maxSymbolValue_tooSmall);
} } } }
for (s=0; s<=maxSymbolValue; s++) { { U32 s; for (s=0; s<=maxSymbolValue; s++) {
count[s] = Counting1[s] + Counting2[s] + Counting3[s] + Counting4[s]; count[s] = Counting1[s] + Counting2[s] + Counting3[s] + Counting4[s];
if (count[s] > max) max = count[s]; if (count[s] > max) max = count[s];
} }}
while (!count[maxSymbolValue]) maxSymbolValue--; while (!count[maxSymbolValue]) maxSymbolValue--;
*maxSymbolValuePtr = maxSymbolValue; *maxSymbolValuePtr = maxSymbolValue;
@ -628,13 +622,13 @@ size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr,
/*-************************************************************** /*-**************************************************************
* FSE Compression Code * FSE Compression Code
****************************************************************/ ****************************************************************/
/*! /*! FSE_sizeof_CTable() :
FSE_CTable is a variable size structure which contains : FSE_CTable is a variable size structure which contains :
U16 tableLog; `U16 tableLog;`
U16 maxSymbolValue; `U16 maxSymbolValue;`
U16 nextStateNumber[1 << tableLog]; // This size is variable `U16 nextStateNumber[1 << tableLog];` // This size is variable
FSE_symbolCompressionTransform symbolTT[maxSymbolValue+1]; // This size is variable `FSE_symbolCompressionTransform symbolTT[maxSymbolValue+1];` // This size is variable
Allocation is manual, since C standard does not support variable-size structures. Allocation is manual (C standard does not support variable-size structures).
*/ */
size_t FSE_sizeof_CTable (unsigned maxSymbolValue, unsigned tableLog) size_t FSE_sizeof_CTable (unsigned maxSymbolValue, unsigned tableLog)
@ -654,10 +648,7 @@ FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog)
return (FSE_CTable*)malloc(size); return (FSE_CTable*)malloc(size);
} }
void FSE_freeCTable (FSE_CTable* ct) void FSE_freeCTable (FSE_CTable* ct) { free(ct); }
{
free(ct);
}
/* provides the minimum logSize to safely represent a distribution */ /* provides the minimum logSize to safely represent a distribution */
static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue) static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue)
@ -733,7 +724,7 @@ static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count,
/* all values are pretty poor; /* all values are pretty poor;
probably incompressible data (should have already been detected); probably incompressible data (should have already been detected);
find max, then give all remaining points to max */ find max, then give all remaining points to max */
U32 maxV = 0, maxC =0; U32 maxV = 0, maxC = 0;
for (s=0; s<=maxSymbolValue; s++) for (s=0; s<=maxSymbolValue; s++)
if (count[s] > maxC) maxV=s, maxC=count[s]; if (count[s] > maxC) maxV=s, maxC=count[s];
norm[maxV] += (short)ToDistribute; norm[maxV] += (short)ToDistribute;
@ -771,8 +762,7 @@ size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); /* Unsupported size */ if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge); /* Unsupported size */
if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC); /* Too small tableLog, compression potentially impossible */ if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC); /* Too small tableLog, compression potentially impossible */
{ { U32 const rtbTable[] = { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
U32 const rtbTable[] = { 0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
U64 const scale = 62 - tableLog; U64 const scale = 62 - tableLog;
U64 const step = ((U64)1<<62) / total; /* <== here, one division ! */ U64 const step = ((U64)1<<62) / total; /* <== here, one division ! */
U64 const vStep = 1ULL<<(scale-20); U64 const vStep = 1ULL<<(scale-20);
@ -848,13 +838,11 @@ size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits)
tableU16[s] = (U16)(tableSize + s); tableU16[s] = (U16)(tableSize + s);
/* Build Symbol Transformation Table */ /* Build Symbol Transformation Table */
{ { const U32 deltaNbBits = (nbBits << 16) - (1 << nbBits);
const U32 deltaNbBits = (nbBits << 16) - (1 << nbBits);
for (s=0; s<=maxSymbolValue; s++) { for (s=0; s<=maxSymbolValue; s++) {
symbolTT[s].deltaNbBits = deltaNbBits; symbolTT[s].deltaNbBits = deltaNbBits;
symbolTT[s].deltaFindState = s-1; symbolTT[s].deltaFindState = s-1;
} } }
}
return 0; return 0;
} }
@ -888,31 +876,30 @@ static size_t FSE_compress_usingCTable_generic (void* dst, size_t dstSize,
const FSE_CTable* ct, const unsigned fast) const FSE_CTable* ct, const unsigned fast)
{ {
const BYTE* const istart = (const BYTE*) src; const BYTE* const istart = (const BYTE*) src;
const BYTE* ip;
const BYTE* const iend = istart + srcSize; const BYTE* const iend = istart + srcSize;
const BYTE* ip=iend;
size_t errorCode;
BIT_CStream_t bitC; BIT_CStream_t bitC;
FSE_CState_t CState1, CState2; FSE_CState_t CState1, CState2;
/* init */ /* init */
errorCode = BIT_initCStream(&bitC, dst, dstSize); if (srcSize <= 2) return 0;
if (FSE_isError(errorCode)) return 0; { size_t const errorCode = BIT_initCStream(&bitC, dst, dstSize);
FSE_initCState(&CState1, ct); if (FSE_isError(errorCode)) return 0; }
CState2 = CState1;
ip=iend;
#define FSE_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s)) #define FSE_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s))
/* join to even */
if (srcSize & 1) { if (srcSize & 1) {
FSE_initCState2(&CState1, ct, *--ip);
FSE_initCState2(&CState2, ct, *--ip);
FSE_encodeSymbol(&bitC, &CState1, *--ip); FSE_encodeSymbol(&bitC, &CState1, *--ip);
FSE_FLUSHBITS(&bitC); FSE_FLUSHBITS(&bitC);
} else {
FSE_initCState2(&CState2, ct, *--ip);
FSE_initCState2(&CState1, ct, *--ip);
} }
/* join to mod 4 */ /* join to mod 4 */
srcSize -= 2;
if ((sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) { /* test bit 2 */ if ((sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) { /* test bit 2 */
FSE_encodeSymbol(&bitC, &CState2, *--ip); FSE_encodeSymbol(&bitC, &CState2, *--ip);
FSE_encodeSymbol(&bitC, &CState1, *--ip); FSE_encodeSymbol(&bitC, &CState1, *--ip);
@ -920,8 +907,7 @@ static size_t FSE_compress_usingCTable_generic (void* dst, size_t dstSize,
} }
/* 2 or 4 encoding per loop */ /* 2 or 4 encoding per loop */
for ( ; ip>istart ; ) for ( ; ip>istart ; ) {
{
FSE_encodeSymbol(&bitC, &CState2, *--ip); FSE_encodeSymbol(&bitC, &CState2, *--ip);
if (sizeof(bitC.bitContainer)*8 < FSE_MAX_TABLELOG*2+7 ) /* this test must be static */ if (sizeof(bitC.bitContainer)*8 < FSE_MAX_TABLELOG*2+7 ) /* this test must be static */
@ -1106,24 +1092,25 @@ FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
/* tail */ /* tail */
/* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */ /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
while (1) { while (1) {
if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state1))) ) if (op>(omax-2)) return ERROR(dstSize_tooSmall);
break;
*op++ = FSE_GETSYMBOL(&state1); *op++ = FSE_GETSYMBOL(&state1);
if ( (BIT_reloadDStream(&bitD)>BIT_DStream_completed) || (op==omax) || (BIT_endOfDStream(&bitD) && (fast || FSE_endOfDState(&state2))) ) if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
*op++ = FSE_GETSYMBOL(&state2);
break; break;
}
if (op>(omax-2)) return ERROR(dstSize_tooSmall);
*op++ = FSE_GETSYMBOL(&state2); *op++ = FSE_GETSYMBOL(&state2);
}
/* end ? */ if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
if (BIT_endOfDStream(&bitD) && FSE_endOfDState(&state1) && FSE_endOfDState(&state2)) *op++ = FSE_GETSYMBOL(&state1);
return op-ostart; break;
} }
if (op==omax) return ERROR(dstSize_tooSmall); /* dst buffer is full, but cSrc unfinished */ return op-ostart;
return ERROR(corruption_detected);
} }

164
lib/fse.h
View File

@ -1,7 +1,7 @@
/* ****************************************************************** /* ******************************************************************
FSE : Finite State Entropy coder FSE : Finite State Entropy codec
header file Public Prototypes declaration
Copyright (C) 2013-2015, Yann Collet. Copyright (C) 2013-2016, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
@ -30,7 +30,6 @@
You can contact the author at : You can contact the author at :
- Source repository : https://github.com/Cyan4973/FiniteStateEntropy - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
- Public forum : https://groups.google.com/forum/#!forum/lz4c
****************************************************************** */ ****************************************************************** */
#ifndef FSE_H #ifndef FSE_H
#define FSE_H #define FSE_H
@ -40,8 +39,8 @@ extern "C" {
#endif #endif
/* ***************************************** /*-*****************************************
* Includes * Dependencies
******************************************/ ******************************************/
#include <stddef.h> /* size_t, ptrdiff_t */ #include <stddef.h> /* size_t, ptrdiff_t */
@ -49,32 +48,32 @@ extern "C" {
/*-**************************************** /*-****************************************
* FSE simple functions * FSE simple functions
******************************************/ ******************************************/
size_t FSE_compress(void* dst, size_t maxDstSize, /*! FSE_compress() :
const void* src, size_t srcSize);
size_t FSE_decompress(void* dst, size_t maxDstSize,
const void* cSrc, size_t cSrcSize);
/*!
FSE_compress():
Compress content of buffer 'src', of size 'srcSize', into destination buffer 'dst'. Compress content of buffer 'src', of size 'srcSize', into destination buffer 'dst'.
'dst' buffer must be already allocated. Compression runs faster is maxDstSize >= FSE_compressBound(srcSize) 'dst' buffer must be already allocated. Compression runs faster is dstCapacity >= FSE_compressBound(srcSize).
return : size of compressed data (<= maxDstSize) @return : size of compressed data (<= dstCapacity).
Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead. if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead.
if FSE_isError(return), compression failed (more details using FSE_getErrorName()) if FSE_isError(return), compression failed (more details using FSE_getErrorName())
*/
size_t FSE_compress(void* dst, size_t dstCapacity,
const void* src, size_t srcSize);
FSE_decompress(): /*! FSE_decompress():
Decompress FSE data from buffer 'cSrc', of size 'cSrcSize', Decompress FSE data from buffer 'cSrc', of size 'cSrcSize',
into already allocated destination buffer 'dst', of size 'maxDstSize'. into already allocated destination buffer 'dst', of size 'dstCapacity'.
return : size of regenerated data (<= maxDstSize) @return : size of regenerated data (<= maxDstSize),
or an error code, which can be tested using FSE_isError() or an error code, which can be tested using FSE_isError() .
** Important ** : FSE_decompress() doesn't decompress non-compressible nor RLE data !!! ** Important ** : FSE_decompress() does not decompress non-compressible nor RLE data !!!
Why ? : making this distinction requires a header. Why ? : making this distinction requires a header.
Header management is intentionally delegated to the user layer, which can better manage special cases. Header management is intentionally delegated to the user layer, which can better manage special cases.
*/ */
size_t FSE_decompress(void* dst, size_t dstCapacity,
const void* cSrc, size_t cSrcSize);
/* ***************************************** /*-*****************************************
* Tool functions * Tool functions
******************************************/ ******************************************/
size_t FSE_compressBound(size_t size); /* maximum compressed size */ size_t FSE_compressBound(size_t size); /* maximum compressed size */
@ -84,14 +83,13 @@ unsigned FSE_isError(size_t code); /* tells if a return value is an er
const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */ const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */
/* ***************************************** /*-*****************************************
* FSE advanced functions * FSE advanced functions
******************************************/ ******************************************/
/*! /*! FSE_compress2() :
FSE_compress2():
Same as FSE_compress(), but allows the selection of 'maxSymbolValue' and 'tableLog' Same as FSE_compress(), but allows the selection of 'maxSymbolValue' and 'tableLog'
Both parameters can be defined as '0' to mean : use default value Both parameters can be defined as '0' to mean : use default value
return : size of compressed data @return : size of compressed data
Special values : if return == 0, srcData is not compressible => Nothing is stored within cSrc !!! Special values : if return == 0, srcData is not compressible => Nothing is stored within cSrc !!!
if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression. if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression.
if FSE_isError(return), it's an error code. if FSE_isError(return), it's an error code.
@ -99,7 +97,7 @@ FSE_compress2():
size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog); size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
/* ***************************************** /*-*****************************************
* FSE detailed API * FSE detailed API
******************************************/ ******************************************/
/*! /*!
@ -122,65 +120,56 @@ or to save and provide normalized distribution using external method.
/* *** COMPRESSION *** */ /* *** COMPRESSION *** */
/*! /*! FSE_count():
FSE_count(): Provides the precise count of each byte within a table 'count'.
Provides the precise count of each byte within a table 'count' 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1).
'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1). *maxSymbolValuePtr will be updated if detected smaller than initial value.
*maxSymbolValuePtr will be updated if detected smaller than initial value. @return : the count of the most frequent symbol (which is not identified).
@return : the count of the most frequent symbol (which is not identified) if return == srcSize, there is only one symbol.
if return == srcSize, there is only one symbol. Can also return an error code, which can be tested with FSE_isError(). */
Can also return an error code, which can be tested with FSE_isError() */
size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize); size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
/*! /*! FSE_optimalTableLog():
FSE_optimalTableLog(): dynamically downsize 'tableLog' when conditions are met.
dynamically downsize 'tableLog' when conditions are met. It saves CPU time, by using smaller tables, while preserving or even improving compression ratio.
It saves CPU time, by using smaller tables, while preserving or even improving compression ratio. @return : recommended tableLog (necessarily <= initial 'tableLog') */
return : recommended tableLog (necessarily <= initial 'tableLog') */
unsigned FSE_optimalTableLog(unsigned tableLog, size_t srcSize, unsigned maxSymbolValue); unsigned FSE_optimalTableLog(unsigned tableLog, size_t srcSize, unsigned maxSymbolValue);
/*! /*! FSE_normalizeCount():
FSE_normalizeCount(): normalize counts so that sum(count[]) == Power_of_2 (2^tableLog)
normalize counters so that sum(count[]) == Power_of_2 (2^tableLog) 'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1).
'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1). @return : tableLog,
return : tableLog, or an errorCode, which can be tested using FSE_isError() */
or an errorCode, which can be tested using FSE_isError() */
size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, const unsigned* count, size_t srcSize, unsigned maxSymbolValue); size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, const unsigned* count, size_t srcSize, unsigned maxSymbolValue);
/*! /*! FSE_NCountWriteBound():
FSE_NCountWriteBound(): Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'.
Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog' Typically useful for allocation purpose. */
Typically useful for allocation purpose. */
size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog); size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog);
/*! /*! FSE_writeNCount():
FSE_writeNCount(): Compactly save 'normalizedCounter' into 'buffer'.
Compactly save 'normalizedCounter' into 'buffer'. @return : size of the compressed table,
return : size of the compressed table or an errorCode, which can be tested using FSE_isError(). */
or an errorCode, which can be tested using FSE_isError() */
size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
/*! /*! Constructor and Destructor of FSE_CTable.
Constructor and Destructor of type FSE_CTable Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */
Note that its size depends on 'tableLog' and 'maxSymbolValue' */
typedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */ typedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */
FSE_CTable* FSE_createCTable (unsigned tableLog, unsigned maxSymbolValue); FSE_CTable* FSE_createCTable (unsigned tableLog, unsigned maxSymbolValue);
void FSE_freeCTable (FSE_CTable* ct); void FSE_freeCTable (FSE_CTable* ct);
/*! /*! FSE_buildCTable():
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() */
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); size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
/*! /*! FSE_compress_usingCTable():
FSE_compress_usingCTable(): Compress `src` using `ct` into `dst` which must be already allocated.
Compress @src using @ct into @dst which must be already allocated @return : size of compressed data (<= `dstCapacity`),
return : size of compressed data (<= @dstCapacity) or 0 if compressed data could not fit into `dst`,
or 0 if compressed data could not fit into @dst or an errorCode, which can be tested using FSE_isError() */
or an errorCode, which can be tested using FSE_isError() */
size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, 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);
/*! /*!
@ -221,7 +210,7 @@ If there is an error, both functions will return an ErrorCode (which can be test
'CTable' can then be used to compress 'src', with FSE_compress_usingCTable(). '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' 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 <= @dstCapacity. The function returns the size of compressed data (without header), necessarily <= `dstCapacity`.
If it returns '0', compressed data could not fit into 'dst'. 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()). If there is an error, the function will return an ErrorCode (which can be tested using FSE_isError()).
*/ */
@ -229,34 +218,29 @@ If there is an error, the function will return an ErrorCode (which can be tested
/* *** DECOMPRESSION *** */ /* *** DECOMPRESSION *** */
/*! /*! FSE_readNCount():
FSE_readNCount(): Read compactly saved 'normalizedCounter' from 'rBuffer'.
Read compactly saved 'normalizedCounter' from 'rBuffer'. @return : size read from 'rBuffer',
return : size read from 'rBuffer' or an errorCode, which can be tested using FSE_isError().
or an errorCode, which can be tested using FSE_isError() maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */
maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */
size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize); size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize);
/*! /*! Constructor and Destructor of FSE_DTable.
Constructor and Destructor of type FSE_DTable
Note that its size depends on 'tableLog' */ Note that its size depends on 'tableLog' */
typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */ typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
FSE_DTable* FSE_createDTable(unsigned tableLog); FSE_DTable* FSE_createDTable(unsigned tableLog);
void FSE_freeDTable(FSE_DTable* dt); void FSE_freeDTable(FSE_DTable* dt);
/*! /*! FSE_buildDTable():
FSE_buildDTable(): Builds 'dt', which must be already allocated, using FSE_createDTable().
Builds 'dt', which must be already allocated, using FSE_createDTable() return : 0, or an errorCode, which can be tested using FSE_isError() */
return : 0,
or an errorCode, which can be tested using FSE_isError() */
size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog); size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
/*! /*! FSE_decompress_usingDTable():
FSE_decompress_usingDTable(): Decompress compressed source `cSrc` of size `cSrcSize` using `dt`
Decompress compressed source @cSrc of size @cSrcSize using @dt into `dst` which must be already allocated.
into @dst which must be already allocated. @return : size of regenerated data (necessarily <= `dstCapacity`),
return : size of regenerated data (necessarily <= @dstCapacity) or an errorCode, which can be tested using FSE_isError() */
or an errorCode, which can be tested using FSE_isError() */
size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, 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);
/*! /*!
@ -281,9 +265,9 @@ This is performed by the function FSE_buildDTable().
The space required by 'FSE_DTable' must be already allocated using FSE_createDTable(). The space required by 'FSE_DTable' must be already allocated using FSE_createDTable().
If there is an error, the function will return an error code, which can be tested using FSE_isError(). If there is an error, the function will return an error code, which can be tested using FSE_isError().
'FSE_DTable' can then be used to decompress 'cSrc', with FSE_decompress_usingDTable(). `FSE_DTable` can then be used to decompress `cSrc`, with FSE_decompress_usingDTable().
'cSrcSize' must be strictly correct, otherwise decompression will fail. `cSrcSize` must be strictly correct, otherwise decompression will fail.
FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=maxDstSize). FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<=`dstCapacity`).
If there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small) If there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small)
*/ */

View File

@ -267,7 +267,7 @@ MEM_STATIC void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* statePt
BIT_flushBits(bitC); BIT_flushBits(bitC);
} }
/* decompression */ /*<===== Decompression =====>*/
typedef struct { typedef struct {
U16 tableLog; U16 tableLog;
@ -290,34 +290,39 @@ MEM_STATIC void FSE_initDState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD, con
DStatePtr->table = dt + 1; DStatePtr->table = dt + 1;
} }
MEM_STATIC size_t FSE_getStateValue(FSE_DState_t* DStatePtr) MEM_STATIC BYTE FSE_peekSymbol(const FSE_DState_t* DStatePtr)
{ {
return DStatePtr->state; FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
return DInfo.symbol;
} }
MEM_STATIC BYTE FSE_peakSymbol(FSE_DState_t* DStatePtr) MEM_STATIC void FSE_updateState(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
{ {
const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
return DInfo.symbol; U32 const nbBits = DInfo.nbBits;
size_t const lowBits = BIT_readBits(bitD, nbBits);
DStatePtr->state = DInfo.newState + lowBits;
} }
MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) MEM_STATIC BYTE FSE_decodeSymbol(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
{ {
const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
const U32 nbBits = DInfo.nbBits; U32 const nbBits = DInfo.nbBits;
BYTE symbol = DInfo.symbol; BYTE const symbol = DInfo.symbol;
size_t lowBits = BIT_readBits(bitD, nbBits); size_t const lowBits = BIT_readBits(bitD, nbBits);
DStatePtr->state = DInfo.newState + lowBits; DStatePtr->state = DInfo.newState + lowBits;
return symbol; return symbol;
} }
/*! FSE_decodeSymbolFast() :
unsafe, only works if no symbol has a probability > 50% */
MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD) MEM_STATIC BYTE FSE_decodeSymbolFast(FSE_DState_t* DStatePtr, BIT_DStream_t* bitD)
{ {
const FSE_decode_t DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state]; FSE_decode_t const DInfo = ((const FSE_decode_t*)(DStatePtr->table))[DStatePtr->state];
const U32 nbBits = DInfo.nbBits; U32 const nbBits = DInfo.nbBits;
BYTE symbol = DInfo.symbol; BYTE const symbol = DInfo.symbol;
size_t lowBits = BIT_readBitsFast(bitD, nbBits); size_t const lowBits = BIT_readBitsFast(bitD, nbBits);
DStatePtr->state = DInfo.newState + lowBits; DStatePtr->state = DInfo.newState + lowBits;
return symbol; return symbol;

View File

@ -1,6 +1,6 @@
/* ****************************************************************** /* ******************************************************************
Huff0 : Huffman coder, part of New Generation Entropy library Huff0 : Huffman coder, part of New Generation Entropy library
Copyright (C) 2013-2015, Yann Collet. Copyright (C) 2013-2016, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
@ -103,8 +103,7 @@ typedef struct nodeElt_s {
} nodeElt; } nodeElt;
/*! HUF_writeCTable() : /*! HUF_writeCTable() :
@dst : destination buffer `CTable` : huffman tree to save, using huff0 representation.
@CTable : huffman tree to save, using huff0 representation
@return : size of saved CTable */ @return : size of saved CTable */
size_t HUF_writeCTable (void* dst, size_t maxDstSize, size_t HUF_writeCTable (void* dst, size_t maxDstSize,
const HUF_CElt* CTable, U32 maxSymbolValue, U32 huffLog) const HUF_CElt* CTable, U32 maxSymbolValue, U32 huffLog)
@ -181,66 +180,58 @@ size_t HUF_readCTable (HUF_CElt* CTable, U32 maxSymbolValue, const void* src, si
BYTE huffWeight[HUF_MAX_SYMBOL_VALUE + 1]; BYTE huffWeight[HUF_MAX_SYMBOL_VALUE + 1];
U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; /* large enough for values from 0 to 16 */ U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; /* large enough for values from 0 to 16 */
U32 tableLog = 0; U32 tableLog = 0;
size_t iSize; size_t readSize;
U32 nbSymbols = 0; U32 nbSymbols = 0;
U32 n;
U32 nextRankStart;
//memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */ //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */
/* get symbol weights */ /* get symbol weights */
iSize = HUF_readStats(huffWeight, HUF_MAX_SYMBOL_VALUE+1, rankVal, &nbSymbols, &tableLog, src, srcSize); readSize = HUF_readStats(huffWeight, HUF_MAX_SYMBOL_VALUE+1, rankVal, &nbSymbols, &tableLog, src, srcSize);
if (HUF_isError(iSize)) return iSize; if (HUF_isError(readSize)) return readSize;
/* check result */ /* check result */
if (tableLog > HUF_MAX_TABLELOG) return ERROR(tableLog_tooLarge); if (tableLog > HUF_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
if (nbSymbols > maxSymbolValue+1) return ERROR(maxSymbolValue_tooSmall); if (nbSymbols > maxSymbolValue+1) return ERROR(maxSymbolValue_tooSmall);
/* Prepare base value per rank */ /* Prepare base value per rank */
nextRankStart = 0; { U32 n, nextRankStart = 0;
for (n=1; n<=tableLog; n++) { for (n=1; n<=tableLog; n++) {
U32 current = nextRankStart; U32 current = nextRankStart;
nextRankStart += (rankVal[n] << (n-1)); nextRankStart += (rankVal[n] << (n-1));
rankVal[n] = current; rankVal[n] = current;
} } }
/* fill nbBits */ /* fill nbBits */
for (n=0; n<nbSymbols; n++) { { U32 n; for (n=0; n<nbSymbols; n++) {
const U32 w = huffWeight[n]; const U32 w = huffWeight[n];
CTable[n].nbBits = (BYTE)(tableLog + 1 - w); CTable[n].nbBits = (BYTE)(tableLog + 1 - w);
} }}
/* fill val */ /* fill val */
{ { U16 nbPerRank[HUF_MAX_TABLELOG+1] = {0};
U16 nbPerRank[HUF_MAX_TABLELOG+1] = {0};
U16 valPerRank[HUF_MAX_TABLELOG+1] = {0}; U16 valPerRank[HUF_MAX_TABLELOG+1] = {0};
for (n=0; n<nbSymbols; n++) { U32 n; for (n=0; n<nbSymbols; n++) nbPerRank[CTable[n].nbBits]++; }
nbPerRank[CTable[n].nbBits]++; /* determine stating value per rank */
{ { U16 min = 0;
/* determine stating value per rank */ U32 n; for (n=HUF_MAX_TABLELOG; n>0; n--) {
U16 min = 0;
for (n=HUF_MAX_TABLELOG; n>0; n--) {
valPerRank[n] = min; /* get starting value within each rank */ valPerRank[n] = min; /* get starting value within each rank */
min += nbPerRank[n]; min += nbPerRank[n];
min >>= 1; min >>= 1;
} } } }
for (n=0; n<=maxSymbolValue; n++) /* assign value within rank, symbol order */
CTable[n].val = valPerRank[CTable[n].nbBits]++; /* assign value within rank, symbol order */ { U32 n; for (n=0; n<=maxSymbolValue; n++) CTable[n].val = valPerRank[CTable[n].nbBits]++; }
} }
return iSize; return readSize;
} }
static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits) static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
{ {
int totalCost = 0;
const U32 largestBits = huffNode[lastNonNull].nbBits; const U32 largestBits = huffNode[lastNonNull].nbBits;
if (largestBits <= maxNbBits) return largestBits; /* early exit : no elt > maxNbBits */
/* early exit : all is fine */
if (largestBits <= maxNbBits) return largestBits;
/* there are several too large elements (at least >= 2) */ /* there are several too large elements (at least >= 2) */
{ { int totalCost = 0;
const U32 baseCost = 1 << (largestBits - maxNbBits); const U32 baseCost = 1 << (largestBits - maxNbBits);
U32 n = lastNonNull; U32 n = lastNonNull;
@ -248,26 +239,25 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits)); totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits));
huffNode[n].nbBits = (BYTE)maxNbBits; huffNode[n].nbBits = (BYTE)maxNbBits;
n --; n --;
} /* n stops at huffNode[n].nbBits <= maxNbBits */ } /* n stops at huffNode[n].nbBits <= maxNbBits */
while (huffNode[n].nbBits == maxNbBits) n--; /* n end at index of smallest symbol using (maxNbBits-1) */ while (huffNode[n].nbBits == maxNbBits) n--; /* n end at index of smallest symbol using < maxNbBits */
/* renorm totalCost */ /* renorm totalCost */
totalCost >>= (largestBits - maxNbBits); /* note : totalCost is necessarily a multiple of baseCost */ totalCost >>= (largestBits - maxNbBits); /* note : totalCost is necessarily a multiple of baseCost */
/* repay normalized cost */ /* repay normalized cost */
{ { U32 const noSymbol = 0xF0F0F0F0;
const U32 noSymbol = 0xF0F0F0F0;
U32 rankLast[HUF_MAX_TABLELOG+1]; U32 rankLast[HUF_MAX_TABLELOG+1];
U32 currentNbBits = maxNbBits;
int pos; int pos;
/* Get pos of last (smallest) symbol per rank */ /* Get pos of last (smallest) symbol per rank */
memset(rankLast, 0xF0, sizeof(rankLast)); memset(rankLast, 0xF0, sizeof(rankLast));
for (pos=n ; pos >= 0; pos--) { { U32 currentNbBits = maxNbBits;
if (huffNode[pos].nbBits >= currentNbBits) continue; for (pos=n ; pos >= 0; pos--) {
currentNbBits = huffNode[pos].nbBits; /* < maxNbBits */ if (huffNode[pos].nbBits >= currentNbBits) continue;
rankLast[maxNbBits-currentNbBits] = pos; currentNbBits = huffNode[pos].nbBits; /* < maxNbBits */
} rankLast[maxNbBits-currentNbBits] = pos;
} }
while (totalCost > 0) { while (totalCost > 0) {
U32 nBitsToDecrease = BIT_highbit32(totalCost) + 1; U32 nBitsToDecrease = BIT_highbit32(totalCost) + 1;
@ -276,9 +266,8 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
U32 lowPos = rankLast[nBitsToDecrease-1]; U32 lowPos = rankLast[nBitsToDecrease-1];
if (highPos == noSymbol) continue; if (highPos == noSymbol) continue;
if (lowPos == noSymbol) break; if (lowPos == noSymbol) break;
{ { U32 const highTotal = huffNode[highPos].count;
U32 highTotal = huffNode[highPos].count; U32 const lowTotal = 2 * huffNode[lowPos].count;
U32 lowTotal = 2 * huffNode[lowPos].count;
if (highTotal <= lowTotal) break; if (highTotal <= lowTotal) break;
} } } }
/* only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !) */ /* only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !) */
@ -294,7 +283,7 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
rankLast[nBitsToDecrease]--; rankLast[nBitsToDecrease]--;
if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits-nBitsToDecrease) if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits-nBitsToDecrease)
rankLast[nBitsToDecrease] = noSymbol; /* this rank is now empty */ rankLast[nBitsToDecrease] = noSymbol; /* this rank is now empty */
} } } } /* while (totalCost > 0) */
while (totalCost < 0) { /* Sometimes, cost correction overshoot */ while (totalCost < 0) { /* Sometimes, cost correction overshoot */
if (rankLast[1] == noSymbol) { /* special case : no rank 1 symbol (using maxNbBits-1); let's create one from largest rank 0 (using maxNbBits) */ if (rankLast[1] == noSymbol) { /* special case : no rank 1 symbol (using maxNbBits-1); let's create one from largest rank 0 (using maxNbBits) */
@ -307,7 +296,7 @@ static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
huffNode[ rankLast[1] + 1 ].nbBits--; huffNode[ rankLast[1] + 1 ].nbBits--;
rankLast[1]++; rankLast[1]++;
totalCost ++; totalCost ++;
} } } } } } /* there are several too large elements (at least >= 2) */
return maxNbBits; return maxNbBits;
} }
@ -331,8 +320,8 @@ static void HUF_sort(nodeElt* huffNode, const U32* count, U32 maxSymbolValue)
for (n=30; n>0; n--) rank[n-1].base += rank[n].base; for (n=30; n>0; n--) rank[n-1].base += rank[n].base;
for (n=0; n<32; n++) rank[n].current = rank[n].base; for (n=0; n<32; n++) rank[n].current = rank[n].base;
for (n=0; n<=maxSymbolValue; n++) { for (n=0; n<=maxSymbolValue; n++) {
U32 c = count[n]; U32 const c = count[n];
U32 r = BIT_highbit32(c+1) + 1; U32 const r = BIT_highbit32(c+1) + 1;
U32 pos = rank[r].current++; U32 pos = rank[r].current++;
while ((pos > rank[r].base) && (c > huffNode[pos-1].count)) huffNode[pos]=huffNode[pos-1], pos--; while ((pos > rank[r].base) && (c > huffNode[pos-1].count)) huffNode[pos]=huffNode[pos-1], pos--;
huffNode[pos].count = c; huffNode[pos].count = c;
@ -389,21 +378,18 @@ size_t HUF_buildCTable (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U3
maxNbBits = HUF_setMaxHeight(huffNode, nonNullRank, maxNbBits); maxNbBits = HUF_setMaxHeight(huffNode, nonNullRank, maxNbBits);
/* fill result into tree (val, nbBits) */ /* fill result into tree (val, nbBits) */
{ { U16 nbPerRank[HUF_MAX_TABLELOG+1] = {0};
U16 nbPerRank[HUF_MAX_TABLELOG+1] = {0};
U16 valPerRank[HUF_MAX_TABLELOG+1] = {0}; U16 valPerRank[HUF_MAX_TABLELOG+1] = {0};
if (maxNbBits > HUF_MAX_TABLELOG) return ERROR(GENERIC); /* check fit into table */ if (maxNbBits > HUF_MAX_TABLELOG) return ERROR(GENERIC); /* check fit into table */
for (n=0; n<=nonNullRank; n++) for (n=0; n<=nonNullRank; n++)
nbPerRank[huffNode[n].nbBits]++; nbPerRank[huffNode[n].nbBits]++;
{ /* determine stating value per rank */
/* determine stating value per rank */ { U16 min = 0;
U16 min = 0;
for (n=maxNbBits; n>0; n--) { for (n=maxNbBits; n>0; n--) {
valPerRank[n] = min; /* get starting value within each rank */ valPerRank[n] = min; /* get starting value within each rank */
min += nbPerRank[n]; min += nbPerRank[n];
min >>= 1; min >>= 1;
} } }
}
for (n=0; n<=maxSymbolValue; n++) for (n=0; n<=maxSymbolValue; n++)
tree[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */ tree[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */
for (n=0; n<=maxSymbolValue; n++) for (n=0; n<=maxSymbolValue; n++)
@ -432,17 +418,16 @@ size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, si
{ {
const BYTE* ip = (const BYTE*) src; const BYTE* ip = (const BYTE*) src;
BYTE* const ostart = (BYTE*)dst; BYTE* const ostart = (BYTE*)dst;
BYTE* op = ostart;
BYTE* const oend = ostart + dstSize; BYTE* const oend = ostart + dstSize;
BYTE* op = ostart;
size_t n; size_t n;
const unsigned fast = (dstSize >= HUF_BLOCKBOUND(srcSize)); const unsigned fast = (dstSize >= HUF_BLOCKBOUND(srcSize));
size_t errorCode;
BIT_CStream_t bitC; BIT_CStream_t bitC;
/* init */ /* init */
if (dstSize < 8) return 0; /* not enough space to compress */ if (dstSize < 8) return 0; /* not enough space to compress */
errorCode = BIT_initCStream(&bitC, op, oend-op); { size_t const errorCode = BIT_initCStream(&bitC, op, oend-op);
if (HUF_isError(errorCode)) return 0; if (HUF_isError(errorCode)) return 0; }
n = srcSize & ~3; /* join to mod 4 */ n = srcSize & ~3; /* join to mod 4 */
switch (srcSize & 3) switch (srcSize & 3)
@ -475,12 +460,12 @@ size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, si
size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable) size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable)
{ {
size_t segmentSize = (srcSize+3)/4; /* first 3 segments */ size_t segmentSize = (srcSize+3)/4; /* first 3 segments */
size_t errorCode;
const BYTE* ip = (const BYTE*) src; const BYTE* ip = (const BYTE*) src;
const BYTE* const iend = ip + srcSize; const BYTE* const iend = ip + srcSize;
BYTE* const ostart = (BYTE*) dst; BYTE* const ostart = (BYTE*) dst;
BYTE* op = ostart;
BYTE* const oend = ostart + dstSize; BYTE* const oend = ostart + dstSize;
BYTE* op = ostart;
size_t errorCode;
if (dstSize < 6 + 1 + 1 + 1 + 8) return 0; /* minimum space to compress successfully */ if (dstSize < 6 + 1 + 1 + 1 + 8) return 0; /* minimum space to compress successfully */
if (srcSize < 12) return 0; /* no saving possible : too small input */ if (srcSize < 12) return 0; /* no saving possible : too small input */
@ -523,8 +508,8 @@ static size_t HUF_compress_internal (
unsigned singleStream) unsigned singleStream)
{ {
BYTE* const ostart = (BYTE*)dst; BYTE* const ostart = (BYTE*)dst;
BYTE* op = ostart;
BYTE* const oend = ostart + dstSize; BYTE* const oend = ostart + dstSize;
BYTE* op = ostart;
U32 count[HUF_MAX_SYMBOL_VALUE+1]; U32 count[HUF_MAX_SYMBOL_VALUE+1];
HUF_CElt CTable[HUF_MAX_SYMBOL_VALUE+1]; HUF_CElt CTable[HUF_MAX_SYMBOL_VALUE+1];
@ -573,8 +558,8 @@ static size_t HUF_compress_internal (
size_t HUF_compress1X (void* dst, size_t dstSize, size_t HUF_compress1X (void* dst, size_t dstSize,
const void* src, size_t srcSize, const void* src, size_t srcSize,
unsigned maxSymbolValue, unsigned huffLog) unsigned maxSymbolValue, unsigned huffLog)
{ {
return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 1); return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 1);
} }
@ -602,9 +587,9 @@ typedef struct { U16 sequence; BYTE nbBits; BYTE length; } HUF_DEltX4; /* doubl
typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t; typedef struct { BYTE symbol; BYTE weight; } sortedSymbol_t;
/*! HUF_readStats /*! HUF_readStats() :
Read compact Huffman tree, saved by HUF_writeCTable Read compact Huffman tree, saved by HUF_writeCTable().
@huffWeight : destination buffer `huffWeight` is destination buffer.
@return : size read from `src` @return : size read from `src`
*/ */
static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
@ -616,13 +601,12 @@ static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
const BYTE* ip = (const BYTE*) src; const BYTE* ip = (const BYTE*) src;
size_t iSize = ip[0]; size_t iSize = ip[0];
size_t oSize; size_t oSize;
U32 n;
//memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */ //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
if (iSize >= 128) { /* special header */ if (iSize >= 128) { /* special header */
if (iSize >= (242)) { /* RLE */ if (iSize >= (242)) { /* RLE */
static int l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 }; static U32 l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };
oSize = l[iSize-242]; oSize = l[iSize-242];
memset(huffWeight, 1, hwSize); memset(huffWeight, 1, hwSize);
iSize = 0; iSize = 0;
@ -633,10 +617,11 @@ static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
if (iSize+1 > srcSize) return ERROR(srcSize_wrong); if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
if (oSize >= hwSize) return ERROR(corruption_detected); if (oSize >= hwSize) return ERROR(corruption_detected);
ip += 1; ip += 1;
for (n=0; n<oSize; n+=2) { { U32 n;
huffWeight[n] = ip[n/2] >> 4; for (n=0; n<oSize; n+=2) {
huffWeight[n+1] = ip[n/2] & 15; huffWeight[n] = ip[n/2] >> 4;
} } } huffWeight[n+1] = ip[n/2] & 15;
} } } }
else { /* header compressed with FSE (normal case) */ else { /* header compressed with FSE (normal case) */
if (iSize+1 > srcSize) return ERROR(srcSize_wrong); if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */ oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */
@ -646,20 +631,20 @@ static size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
/* collect weight stats */ /* collect weight stats */
memset(rankStats, 0, (HUF_ABSOLUTEMAX_TABLELOG + 1) * sizeof(U32)); memset(rankStats, 0, (HUF_ABSOLUTEMAX_TABLELOG + 1) * sizeof(U32));
weightTotal = 0; weightTotal = 0;
for (n=0; n<oSize; n++) { { U32 n; for (n=0; n<oSize; n++) {
if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected); if (huffWeight[n] >= HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
rankStats[huffWeight[n]]++; rankStats[huffWeight[n]]++;
weightTotal += (1 << huffWeight[n]) >> 1; weightTotal += (1 << huffWeight[n]) >> 1;
} }}
/* get last non-null symbol weight (implied, total must be 2^n) */ /* get last non-null symbol weight (implied, total must be 2^n) */
tableLog = BIT_highbit32(weightTotal) + 1; tableLog = BIT_highbit32(weightTotal) + 1;
if (tableLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected); if (tableLog > HUF_ABSOLUTEMAX_TABLELOG) return ERROR(corruption_detected);
{ /* determine last weight */ /* determine last weight */
U32 total = 1 << tableLog; { U32 const total = 1 << tableLog;
U32 rest = total - weightTotal; U32 const rest = total - weightTotal;
U32 verif = 1 << BIT_highbit32(rest); U32 const verif = 1 << BIT_highbit32(rest);
U32 lastWeight = BIT_highbit32(rest) + 1; U32 const lastWeight = BIT_highbit32(rest) + 1;
if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */ if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
huffWeight[oSize] = (BYTE)lastWeight; huffWeight[oSize] = (BYTE)lastWeight;
rankStats[lastWeight]++; rankStats[lastWeight]++;
@ -724,12 +709,13 @@ size_t HUF_readDTableX2 (U16* DTable, const void* src, size_t srcSize)
return iSize; return iSize;
} }
static BYTE HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog) static BYTE HUF_decodeSymbolX2(BIT_DStream_t* Dstream, const HUF_DEltX2* dt, const U32 dtLog)
{ {
const size_t val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */ const size_t val = BIT_lookBitsFast(Dstream, dtLog); /* note : dtLog >= 1 */
const BYTE c = dt[val].byte; const BYTE c = dt[val].byte;
BIT_skipBits(Dstream, dt[val].nbBits); BIT_skipBits(Dstream, dt[val].nbBits);
return c; return c;
} }
#define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \ #define HUF_DECODE_SYMBOLX2_0(ptr, DStreamPtr) \
@ -773,13 +759,13 @@ size_t HUF_decompress1X2_usingDTable(
{ {
BYTE* op = (BYTE*)dst; BYTE* op = (BYTE*)dst;
BYTE* const oend = op + dstSize; BYTE* const oend = op + dstSize;
size_t errorCode;
const U32 dtLog = DTable[0]; const U32 dtLog = DTable[0];
const void* dtPtr = DTable; const void* dtPtr = DTable;
const HUF_DEltX2* const dt = ((const HUF_DEltX2*)dtPtr)+1; const HUF_DEltX2* const dt = ((const HUF_DEltX2*)dtPtr)+1;
BIT_DStream_t bitD; BIT_DStream_t bitD;
errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize);
if (HUF_isError(errorCode)) return errorCode; { size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize);
if (HUF_isError(errorCode)) return errorCode; }
HUF_decodeStreamX2(op, &bitD, oend, dt, dtLog); HUF_decodeStreamX2(op, &bitD, oend, dt, dtLog);
@ -793,9 +779,8 @@ size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cS
{ {
HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_MAX_TABLELOG); HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_MAX_TABLELOG);
const BYTE* ip = (const BYTE*) cSrc; const BYTE* ip = (const BYTE*) cSrc;
size_t errorCode;
errorCode = HUF_readDTableX2 (DTable, cSrc, cSrcSize); size_t const errorCode = HUF_readDTableX2 (DTable, cSrc, cSrcSize);
if (HUF_isError(errorCode)) return errorCode; if (HUF_isError(errorCode)) return errorCode;
if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); if (errorCode >= cSrcSize) return ERROR(srcSize_wrong);
ip += errorCode; ip += errorCode;
@ -810,91 +795,92 @@ size_t HUF_decompress4X2_usingDTable(
const void* cSrc, size_t cSrcSize, const void* cSrc, size_t cSrcSize,
const U16* DTable) const U16* DTable)
{ {
const BYTE* const istart = (const BYTE*) cSrc;
BYTE* const ostart = (BYTE*) dst;
BYTE* const oend = ostart + dstSize;
const void* const dtPtr = DTable;
const HUF_DEltX2* const dt = ((const HUF_DEltX2*)dtPtr) +1;
const U32 dtLog = DTable[0];
size_t errorCode;
/* Check */ /* Check */
if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
/* Init */ { const BYTE* const istart = (const BYTE*) cSrc;
BIT_DStream_t bitD1; BYTE* const ostart = (BYTE*) dst;
BIT_DStream_t bitD2; BYTE* const oend = ostart + dstSize;
BIT_DStream_t bitD3; const void* const dtPtr = DTable;
BIT_DStream_t bitD4; const HUF_DEltX2* const dt = ((const HUF_DEltX2*)dtPtr) +1;
const size_t length1 = MEM_readLE16(istart); const U32 dtLog = DTable[0];
const size_t length2 = MEM_readLE16(istart+2); size_t errorCode;
const size_t length3 = MEM_readLE16(istart+4);
size_t length4;
const BYTE* const istart1 = istart + 6; /* jumpTable */
const BYTE* const istart2 = istart1 + length1;
const BYTE* const istart3 = istart2 + length2;
const BYTE* const istart4 = istart3 + length3;
const size_t segmentSize = (dstSize+3) / 4;
BYTE* const opStart2 = ostart + segmentSize;
BYTE* const opStart3 = opStart2 + segmentSize;
BYTE* const opStart4 = opStart3 + segmentSize;
BYTE* op1 = ostart;
BYTE* op2 = opStart2;
BYTE* op3 = opStart3;
BYTE* op4 = opStart4;
U32 endSignal;
length4 = cSrcSize - (length1 + length2 + length3 + 6); /* Init */
if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */ BIT_DStream_t bitD1;
errorCode = BIT_initDStream(&bitD1, istart1, length1); BIT_DStream_t bitD2;
if (HUF_isError(errorCode)) return errorCode; BIT_DStream_t bitD3;
errorCode = BIT_initDStream(&bitD2, istart2, length2); BIT_DStream_t bitD4;
if (HUF_isError(errorCode)) return errorCode; const size_t length1 = MEM_readLE16(istart);
errorCode = BIT_initDStream(&bitD3, istart3, length3); const size_t length2 = MEM_readLE16(istart+2);
if (HUF_isError(errorCode)) return errorCode; const size_t length3 = MEM_readLE16(istart+4);
errorCode = BIT_initDStream(&bitD4, istart4, length4); size_t length4;
if (HUF_isError(errorCode)) return errorCode; const BYTE* const istart1 = istart + 6; /* jumpTable */
const BYTE* const istart2 = istart1 + length1;
const BYTE* const istart3 = istart2 + length2;
const BYTE* const istart4 = istart3 + length3;
const size_t segmentSize = (dstSize+3) / 4;
BYTE* const opStart2 = ostart + segmentSize;
BYTE* const opStart3 = opStart2 + segmentSize;
BYTE* const opStart4 = opStart3 + segmentSize;
BYTE* op1 = ostart;
BYTE* op2 = opStart2;
BYTE* op3 = opStart3;
BYTE* op4 = opStart4;
U32 endSignal;
/* 16-32 symbols per loop (4-8 symbols per stream) */ length4 = cSrcSize - (length1 + length2 + length3 + 6);
endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; ) { errorCode = BIT_initDStream(&bitD1, istart1, length1);
HUF_DECODE_SYMBOLX2_2(op1, &bitD1); if (HUF_isError(errorCode)) return errorCode;
HUF_DECODE_SYMBOLX2_2(op2, &bitD2); errorCode = BIT_initDStream(&bitD2, istart2, length2);
HUF_DECODE_SYMBOLX2_2(op3, &bitD3); if (HUF_isError(errorCode)) return errorCode;
HUF_DECODE_SYMBOLX2_2(op4, &bitD4); errorCode = BIT_initDStream(&bitD3, istart3, length3);
HUF_DECODE_SYMBOLX2_1(op1, &bitD1); if (HUF_isError(errorCode)) return errorCode;
HUF_DECODE_SYMBOLX2_1(op2, &bitD2); errorCode = BIT_initDStream(&bitD4, istart4, length4);
HUF_DECODE_SYMBOLX2_1(op3, &bitD3); if (HUF_isError(errorCode)) return errorCode;
HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
HUF_DECODE_SYMBOLX2_2(op1, &bitD1); /* 16-32 symbols per loop (4-8 symbols per stream) */
HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
for ( ; (endSignal==BIT_DStream_unfinished) && (op4<(oend-7)) ; ) {
HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
HUF_DECODE_SYMBOLX2_1(op1, &bitD1);
HUF_DECODE_SYMBOLX2_1(op2, &bitD2);
HUF_DECODE_SYMBOLX2_1(op3, &bitD3);
HUF_DECODE_SYMBOLX2_1(op4, &bitD4);
HUF_DECODE_SYMBOLX2_2(op1, &bitD1);
HUF_DECODE_SYMBOLX2_2(op2, &bitD2);
HUF_DECODE_SYMBOLX2_2(op3, &bitD3);
HUF_DECODE_SYMBOLX2_2(op4, &bitD4);
HUF_DECODE_SYMBOLX2_0(op1, &bitD1);
HUF_DECODE_SYMBOLX2_0(op2, &bitD2);
HUF_DECODE_SYMBOLX2_0(op3, &bitD3);
HUF_DECODE_SYMBOLX2_0(op4, &bitD4);
endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
}
/* check corruption */
if (op1 > opStart2) return ERROR(corruption_detected);
if (op2 > opStart3) return ERROR(corruption_detected);
if (op3 > opStart4) return ERROR(corruption_detected);
/* note : op4 supposed already verified within main loop */
/* finish bitStreams one by one */
HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
/* check */
endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
if (!endSignal) return ERROR(corruption_detected);
/* decoded size */
return dstSize;
} }
/* check corruption */
if (op1 > opStart2) return ERROR(corruption_detected);
if (op2 > opStart3) return ERROR(corruption_detected);
if (op3 > opStart4) return ERROR(corruption_detected);
/* note : op4 supposed already verified within main loop */
/* finish bitStreams one by one */
HUF_decodeStreamX2(op1, &bitD1, opStart2, dt, dtLog);
HUF_decodeStreamX2(op2, &bitD2, opStart3, dt, dtLog);
HUF_decodeStreamX2(op3, &bitD3, opStart4, dt, dtLog);
HUF_decodeStreamX2(op4, &bitD4, oend, dt, dtLog);
/* check */
endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
if (!endSignal) return ERROR(corruption_detected);
/* decoded size */
return dstSize;
} }
@ -902,9 +888,8 @@ size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cS
{ {
HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_MAX_TABLELOG); HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_MAX_TABLELOG);
const BYTE* ip = (const BYTE*) cSrc; const BYTE* ip = (const BYTE*) cSrc;
size_t errorCode;
errorCode = HUF_readDTableX2 (DTable, cSrc, cSrcSize); size_t const errorCode = HUF_readDTableX2 (DTable, cSrc, cSrcSize);
if (HUF_isError(errorCode)) return errorCode; if (HUF_isError(errorCode)) return errorCode;
if (errorCode >= cSrcSize) return ERROR(srcSize_wrong); if (errorCode >= cSrcSize) return ERROR(srcSize_wrong);
ip += errorCode; ip += errorCode;
@ -925,7 +910,6 @@ static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 co
{ {
HUF_DEltX4 DElt; HUF_DEltX4 DElt;
U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1]; U32 rankVal[HUF_ABSOLUTEMAX_TABLELOG + 1];
U32 s;
/* get pre-calculated rankVal */ /* get pre-calculated rankVal */
memcpy(rankVal, rankValOrigin, sizeof(rankVal)); memcpy(rankVal, rankValOrigin, sizeof(rankVal));
@ -941,7 +925,7 @@ static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 co
} }
/* fill DTable */ /* fill DTable */
for (s=0; s<sortedListSize; s++) { /* note : sortedSymbols already skipped */ { U32 s; for (s=0; s<sortedListSize; s++) { /* note : sortedSymbols already skipped */
const U32 symbol = sortedSymbols[s].symbol; const U32 symbol = sortedSymbols[s].symbol;
const U32 weight = sortedSymbols[s].weight; const U32 weight = sortedSymbols[s].weight;
const U32 nbBits = nbBitsBaseline - weight; const U32 nbBits = nbBitsBaseline - weight;
@ -956,7 +940,7 @@ static void HUF_fillDTableX4Level2(HUF_DEltX4* DTable, U32 sizeLog, const U32 co
do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */ do { DTable[i++] = DElt; } while (i<end); /* since length >= 1 */
rankVal[weight] += length; rankVal[weight] += length;
} }}
} }
typedef U32 rankVal_t[HUF_ABSOLUTEMAX_TABLELOG][HUF_ABSOLUTEMAX_TABLELOG + 1]; typedef U32 rankVal_t[HUF_ABSOLUTEMAX_TABLELOG][HUF_ABSOLUTEMAX_TABLELOG + 1];
@ -991,16 +975,14 @@ static void HUF_fillDTableX4(HUF_DEltX4* DTable, const U32 targetLog,
sortedList+sortedRank, sortedListSize-sortedRank, sortedList+sortedRank, sortedListSize-sortedRank,
nbBitsBaseline, symbol); nbBitsBaseline, symbol);
} else { } else {
U32 i;
const U32 end = start + length;
HUF_DEltX4 DElt; HUF_DEltX4 DElt;
MEM_writeLE16(&(DElt.sequence), symbol); MEM_writeLE16(&(DElt.sequence), symbol);
DElt.nbBits = (BYTE)(nbBits); DElt.nbBits = (BYTE)(nbBits);
DElt.length = 1; DElt.length = 1;
for (i = start; i < end; i++) { U32 u;
DTable[i] = DElt; const U32 end = start + length;
} for (u = start; u < end; u++) DTable[u] = DElt;
} }
rankVal[weight] += length; rankVal[weight] += length;
} }
} }
@ -1033,8 +1015,7 @@ size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
for (maxW = tableLog; rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */ for (maxW = tableLog; rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */
/* Get start index of each weight */ /* Get start index of each weight */
{ { U32 w, nextRankStart = 0;
U32 w, nextRankStart = 0;
for (w=1; w<=maxW; w++) { for (w=1; w<=maxW; w++) {
U32 current = nextRankStart; U32 current = nextRankStart;
nextRankStart += rankStats[w]; nextRankStart += rankStats[w];
@ -1045,8 +1026,7 @@ size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
} }
/* sort symbols by weight */ /* sort symbols by weight */
{ { U32 s;
U32 s;
for (s=0; s<nbSymbols; s++) { for (s=0; s<nbSymbols; s++) {
U32 w = weightList[s]; U32 w = weightList[s];
U32 r = rankStart[w]++; U32 r = rankStart[w]++;
@ -1057,8 +1037,7 @@ size_t HUF_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
} }
/* Build rankVal */ /* Build rankVal */
{ { const U32 minBits = tableLog+1 - maxW;
const U32 minBits = tableLog+1 - maxW;
U32 nextRankVal = 0; U32 nextRankVal = 0;
U32 w, consumed; U32 w, consumed;
const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */ const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
@ -1155,15 +1134,14 @@ size_t HUF_decompress1X4_usingDTable(
const U32 dtLog = DTable[0]; const U32 dtLog = DTable[0];
const void* const dtPtr = DTable; const void* const dtPtr = DTable;
const HUF_DEltX4* const dt = ((const HUF_DEltX4*)dtPtr) +1; const HUF_DEltX4* const dt = ((const HUF_DEltX4*)dtPtr) +1;
size_t errorCode;
/* Init */ /* Init */
BIT_DStream_t bitD; BIT_DStream_t bitD;
errorCode = BIT_initDStream(&bitD, istart, cSrcSize); { size_t const errorCode = BIT_initDStream(&bitD, istart, cSrcSize);
if (HUF_isError(errorCode)) return errorCode; if (HUF_isError(errorCode)) return errorCode; }
/* finish bitStreams one by one */ /* decode */
HUF_decodeStreamX4(ostart, &bitD, oend, dt, dtLog); HUF_decodeStreamX4(ostart, &bitD, oend, dt, dtLog);
/* check */ /* check */
if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected); if (!BIT_endOfDStream(&bitD)) return ERROR(corruption_detected);
@ -1177,7 +1155,7 @@ size_t HUF_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cS
HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_MAX_TABLELOG); HUF_CREATE_STATIC_DTABLEX4(DTable, HUF_MAX_TABLELOG);
const BYTE* ip = (const BYTE*) cSrc; const BYTE* ip = (const BYTE*) cSrc;
size_t hSize = HUF_readDTableX4 (DTable, cSrc, cSrcSize); size_t const hSize = HUF_readDTableX4 (DTable, cSrc, cSrcSize);
if (HUF_isError(hSize)) return hSize; if (HUF_isError(hSize)) return hSize;
if (hSize >= cSrcSize) return ERROR(srcSize_wrong); if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
ip += hSize; ip += hSize;
@ -1193,8 +1171,7 @@ size_t HUF_decompress4X4_usingDTable(
{ {
if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
{ { const BYTE* const istart = (const BYTE*) cSrc;
const BYTE* const istart = (const BYTE*) cSrc;
BYTE* const ostart = (BYTE*) dst; BYTE* const ostart = (BYTE*) dst;
BYTE* const oend = ostart + dstSize; BYTE* const oend = ostart + dstSize;
const void* const dtPtr = DTable; const void* const dtPtr = DTable;
@ -1381,11 +1358,10 @@ size_t HUF_readDTableX6 (U32* DTable, const void* src, size_t srcSize)
if (tableLog > memLog) return ERROR(tableLog_tooLarge); /* DTable is too small */ if (tableLog > memLog) return ERROR(tableLog_tooLarge); /* DTable is too small */
/* find maxWeight */ /* find maxWeight */
for (maxW = tableLog; rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */ for (maxW = tableLog; maxW && rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */
/* Get start index of each weight */ /* Get start index of each weight */
{ { U32 w, nextRankStart = 0;
U32 w, nextRankStart = 0;
for (w=1; w<=maxW; w++) { for (w=1; w<=maxW; w++) {
U32 current = nextRankStart; U32 current = nextRankStart;
nextRankStart += rankStats[w]; nextRankStart += rankStats[w];
@ -1396,8 +1372,7 @@ size_t HUF_readDTableX6 (U32* DTable, const void* src, size_t srcSize)
} }
/* sort symbols by weight */ /* sort symbols by weight */
{ { U32 s;
U32 s;
for (s=0; s<nbSymbols; s++) { for (s=0; s<nbSymbols; s++) {
U32 w = weightList[s]; U32 w = weightList[s];
U32 r = rankStart[w]++; U32 r = rankStart[w]++;
@ -1408,8 +1383,7 @@ size_t HUF_readDTableX6 (U32* DTable, const void* src, size_t srcSize)
} }
/* Build rankVal */ /* Build rankVal */
{ { const U32 minBits = tableLog+1 - maxW;
const U32 minBits = tableLog+1 - maxW;
U32 nextRankVal = 0; U32 nextRankVal = 0;
U32 w, consumed; U32 w, consumed;
const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */ const int rescale = (memLog-tableLog) - 1; /* tableLog <= memLog */
@ -1426,8 +1400,7 @@ size_t HUF_readDTableX6 (U32* DTable, const void* src, size_t srcSize)
} } } } } }
/* fill tables */ /* fill tables */
{ { void* ddPtr = DTable+1;
void* ddPtr = DTable+1;
HUF_DDescX6* DDescription = (HUF_DDescX6*)ddPtr; HUF_DDescX6* DDescription = (HUF_DDescX6*)ddPtr;
void* dsPtr = DTable + 1 + ((size_t)1<<(memLog-1)); void* dsPtr = DTable + 1 + ((size_t)1<<(memLog-1));
HUF_DSeqX6* DSequence = (HUF_DSeqX6*)dsPtr; HUF_DSeqX6* DSequence = (HUF_DSeqX6*)dsPtr;
@ -1559,95 +1532,96 @@ size_t HUF_decompress4X6_usingDTable(
const void* cSrc, size_t cSrcSize, const void* cSrc, size_t cSrcSize,
const U32* DTable) const U32* DTable)
{ {
const BYTE* const istart = (const BYTE*) cSrc;
BYTE* const ostart = (BYTE*) dst;
BYTE* const oend = ostart + dstSize;
const U32 dtLog = DTable[0];
const void* const ddPtr = DTable+1;
const HUF_DDescX6* dd = (const HUF_DDescX6*)ddPtr;
const void* const dsPtr = DTable + 1 + ((size_t)1<<(dtLog-1));
const HUF_DSeqX6* ds = (const HUF_DSeqX6*)dsPtr;
size_t errorCode;
/* Check */ /* Check */
if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */ if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
/* Init */ { const BYTE* const istart = (const BYTE*) cSrc;
BIT_DStream_t bitD1; BYTE* const ostart = (BYTE*) dst;
BIT_DStream_t bitD2; BYTE* const oend = ostart + dstSize;
BIT_DStream_t bitD3;
BIT_DStream_t bitD4;
const size_t length1 = MEM_readLE16(istart);
const size_t length2 = MEM_readLE16(istart+2);
const size_t length3 = MEM_readLE16(istart+4);
size_t length4;
const BYTE* const istart1 = istart + 6; /* jumpTable */
const BYTE* const istart2 = istart1 + length1;
const BYTE* const istart3 = istart2 + length2;
const BYTE* const istart4 = istart3 + length3;
const size_t segmentSize = (dstSize+3) / 4;
BYTE* const opStart2 = ostart + segmentSize;
BYTE* const opStart3 = opStart2 + segmentSize;
BYTE* const opStart4 = opStart3 + segmentSize;
BYTE* op1 = ostart;
BYTE* op2 = opStart2;
BYTE* op3 = opStart3;
BYTE* op4 = opStart4;
U32 endSignal;
length4 = cSrcSize - (length1 + length2 + length3 + 6); const U32 dtLog = DTable[0];
if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */ const void* const ddPtr = DTable+1;
errorCode = BIT_initDStream(&bitD1, istart1, length1); const HUF_DDescX6* dd = (const HUF_DDescX6*)ddPtr;
if (HUF_isError(errorCode)) return errorCode; const void* const dsPtr = DTable + 1 + ((size_t)1<<(dtLog-1));
errorCode = BIT_initDStream(&bitD2, istart2, length2); const HUF_DSeqX6* ds = (const HUF_DSeqX6*)dsPtr;
if (HUF_isError(errorCode)) return errorCode; size_t errorCode;
errorCode = BIT_initDStream(&bitD3, istart3, length3);
if (HUF_isError(errorCode)) return errorCode;
errorCode = BIT_initDStream(&bitD4, istart4, length4);
if (HUF_isError(errorCode)) return errorCode;
/* 16-64 symbols per loop (4-16 symbols per stream) */ /* Init */
endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); BIT_DStream_t bitD1;
for ( ; (op3 <= opStart4) && (endSignal==BIT_DStream_unfinished) && (op4<=(oend-16)) ; ) { BIT_DStream_t bitD2;
HUF_DECODE_SYMBOLX6_2(op1, &bitD1); BIT_DStream_t bitD3;
HUF_DECODE_SYMBOLX6_2(op2, &bitD2); BIT_DStream_t bitD4;
HUF_DECODE_SYMBOLX6_2(op3, &bitD3); const size_t length1 = MEM_readLE16(istart);
HUF_DECODE_SYMBOLX6_2(op4, &bitD4); const size_t length2 = MEM_readLE16(istart+2);
HUF_DECODE_SYMBOLX6_1(op1, &bitD1); const size_t length3 = MEM_readLE16(istart+4);
HUF_DECODE_SYMBOLX6_1(op2, &bitD2); size_t length4;
HUF_DECODE_SYMBOLX6_1(op3, &bitD3); const BYTE* const istart1 = istart + 6; /* jumpTable */
HUF_DECODE_SYMBOLX6_1(op4, &bitD4); const BYTE* const istart2 = istart1 + length1;
HUF_DECODE_SYMBOLX6_2(op1, &bitD1); const BYTE* const istart3 = istart2 + length2;
HUF_DECODE_SYMBOLX6_2(op2, &bitD2); const BYTE* const istart4 = istart3 + length3;
HUF_DECODE_SYMBOLX6_2(op3, &bitD3); const size_t segmentSize = (dstSize+3) / 4;
HUF_DECODE_SYMBOLX6_2(op4, &bitD4); BYTE* const opStart2 = ostart + segmentSize;
HUF_DECODE_SYMBOLX6_0(op1, &bitD1); BYTE* const opStart3 = opStart2 + segmentSize;
HUF_DECODE_SYMBOLX6_0(op2, &bitD2); BYTE* const opStart4 = opStart3 + segmentSize;
HUF_DECODE_SYMBOLX6_0(op3, &bitD3); BYTE* op1 = ostart;
HUF_DECODE_SYMBOLX6_0(op4, &bitD4); BYTE* op2 = opStart2;
BYTE* op3 = opStart3;
BYTE* op4 = opStart4;
U32 endSignal;
length4 = cSrcSize - (length1 + length2 + length3 + 6);
if (length4 > cSrcSize) return ERROR(corruption_detected); /* overflow */
errorCode = BIT_initDStream(&bitD1, istart1, length1);
if (HUF_isError(errorCode)) return errorCode;
errorCode = BIT_initDStream(&bitD2, istart2, length2);
if (HUF_isError(errorCode)) return errorCode;
errorCode = BIT_initDStream(&bitD3, istart3, length3);
if (HUF_isError(errorCode)) return errorCode;
errorCode = BIT_initDStream(&bitD4, istart4, length4);
if (HUF_isError(errorCode)) return errorCode;
/* 16-64 symbols per loop (4-16 symbols per stream) */
endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4); endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
for ( ; (op3 <= opStart4) && (endSignal==BIT_DStream_unfinished) && (op4<=(oend-16)) ; ) {
HUF_DECODE_SYMBOLX6_2(op1, &bitD1);
HUF_DECODE_SYMBOLX6_2(op2, &bitD2);
HUF_DECODE_SYMBOLX6_2(op3, &bitD3);
HUF_DECODE_SYMBOLX6_2(op4, &bitD4);
HUF_DECODE_SYMBOLX6_1(op1, &bitD1);
HUF_DECODE_SYMBOLX6_1(op2, &bitD2);
HUF_DECODE_SYMBOLX6_1(op3, &bitD3);
HUF_DECODE_SYMBOLX6_1(op4, &bitD4);
HUF_DECODE_SYMBOLX6_2(op1, &bitD1);
HUF_DECODE_SYMBOLX6_2(op2, &bitD2);
HUF_DECODE_SYMBOLX6_2(op3, &bitD3);
HUF_DECODE_SYMBOLX6_2(op4, &bitD4);
HUF_DECODE_SYMBOLX6_0(op1, &bitD1);
HUF_DECODE_SYMBOLX6_0(op2, &bitD2);
HUF_DECODE_SYMBOLX6_0(op3, &bitD3);
HUF_DECODE_SYMBOLX6_0(op4, &bitD4);
endSignal = BIT_reloadDStream(&bitD1) | BIT_reloadDStream(&bitD2) | BIT_reloadDStream(&bitD3) | BIT_reloadDStream(&bitD4);
}
/* check corruption */
if (op1 > opStart2) return ERROR(corruption_detected);
if (op2 > opStart3) return ERROR(corruption_detected);
if (op3 > opStart4) return ERROR(corruption_detected);
/* note : op4 supposed already verified within main loop */
/* finish bitStreams one by one */
HUF_decodeStreamX6(op1, &bitD1, opStart2, DTable, dtLog);
HUF_decodeStreamX6(op2, &bitD2, opStart3, DTable, dtLog);
HUF_decodeStreamX6(op3, &bitD3, opStart4, DTable, dtLog);
HUF_decodeStreamX6(op4, &bitD4, oend, DTable, dtLog);
/* check */
endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
if (!endSignal) return ERROR(corruption_detected);
/* decoded size */
return dstSize;
} }
/* check corruption */
if (op1 > opStart2) return ERROR(corruption_detected);
if (op2 > opStart3) return ERROR(corruption_detected);
if (op3 > opStart4) return ERROR(corruption_detected);
/* note : op4 supposed already verified within main loop */
/* finish bitStreams one by one */
HUF_decodeStreamX6(op1, &bitD1, opStart2, DTable, dtLog);
HUF_decodeStreamX6(op2, &bitD2, opStart3, DTable, dtLog);
HUF_decodeStreamX6(op3, &bitD3, opStart4, DTable, dtLog);
HUF_decodeStreamX6(op4, &bitD4, oend, DTable, dtLog);
/* check */
endSignal = BIT_endOfDStream(&bitD1) & BIT_endOfDStream(&bitD2) & BIT_endOfDStream(&bitD3) & BIT_endOfDStream(&bitD4);
if (!endSignal) return ERROR(corruption_detected);
/* decoded size */
return dstSize;
} }
@ -1656,7 +1630,7 @@ size_t HUF_decompress4X6 (void* dst, size_t dstSize, const void* cSrc, size_t cS
HUF_CREATE_STATIC_DTABLEX6(DTable, HUF_MAX_TABLELOG); HUF_CREATE_STATIC_DTABLEX6(DTable, HUF_MAX_TABLELOG);
const BYTE* ip = (const BYTE*) cSrc; const BYTE* ip = (const BYTE*) cSrc;
size_t hSize = HUF_readDTableX6 (DTable, cSrc, cSrcSize); size_t const hSize = HUF_readDTableX6 (DTable, cSrc, cSrcSize);
if (HUF_isError(hSize)) return hSize; if (HUF_isError(hSize)) return hSize;
if (hSize >= cSrcSize) return ERROR(srcSize_wrong); if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
ip += hSize; ip += hSize;

View File

@ -48,24 +48,24 @@ extern "C" {
/* **************************************** /* ****************************************
* Huff0 simple functions * Huff0 simple functions
******************************************/ ******************************************/
size_t HUF_compress(void* dst, size_t maxDstSize, size_t HUF_compress(void* dst, size_t dstCapacity,
const void* src, size_t srcSize); const void* src, size_t srcSize);
size_t HUF_decompress(void* dst, size_t dstSize, size_t HUF_decompress(void* dst, size_t dstSize,
const void* cSrc, size_t cSrcSize); const void* cSrc, size_t cSrcSize);
/*! /*
HUF_compress(): HUF_compress() :
Compress content of buffer 'src', of size 'srcSize', into destination buffer 'dst'. Compress content of buffer 'src', of size 'srcSize', into destination buffer 'dst'.
'dst' buffer must be already allocated. Compression runs faster if maxDstSize >= HUF_compressBound(srcSize). 'dst' buffer must be already allocated. Compression runs faster if dstCapacity >= HUF_compressBound(srcSize).
Note : srcSize must be <= 128 KB Note : srcSize must be <= 128 KB
@return : size of compressed data (<= maxDstSize) @return : size of compressed data (<= dstCapacity)
Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!! Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
if return == 1, srcData is a single repeated byte symbol (RLE compression) if return == 1, srcData is a single repeated byte symbol (RLE compression).
if HUF_isError(return), compression failed (more details using HUF_getErrorName()) if HUF_isError(return), compression failed (more details using HUF_getErrorName())
HUF_decompress(): HUF_decompress() :
Decompress Huff0 data from buffer 'cSrc', of size 'cSrcSize', Decompress Huff0 data from buffer 'cSrc', of size 'cSrcSize',
into already allocated destination buffer 'dst', of size 'dstSize'. into already allocated destination buffer 'dst', of size 'dstSize'.
@dstSize : must be the **exact** size of original (uncompressed) data. `dstSize` : must be the **exact** size of original (uncompressed) data.
Note : in contrast with FSE, HUF_decompress can regenerate Note : in contrast with FSE, HUF_decompress can regenerate
RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data, RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,
because it knows size to regenerate. because it knows size to regenerate.
@ -77,11 +77,11 @@ HUF_decompress():
/* **************************************** /* ****************************************
* Tool functions * Tool functions
******************************************/ ******************************************/
size_t HUF_compressBound(size_t size); /* maximum compressed size */ size_t HUF_compressBound(size_t size); /**< maximum compressed size */
/* Error Management */ /* Error Management */
unsigned HUF_isError(size_t code); /* tells if a return value is an error code */ unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */
const char* HUF_getErrorName(size_t code); /* provides error code string (useful for debugging) */ const char* HUF_getErrorName(size_t code); /**< provides error code string (useful for debugging) */
/* **************************************** /* ****************************************

View File

@ -85,7 +85,7 @@ HUF_compress() does the following:
1. count symbol occurrence from source[] into table count[] using FSE_count() 1. count symbol occurrence from source[] into table count[] using FSE_count()
2. build Huffman table from count using HUF_buildCTable() 2. build Huffman table from count using HUF_buildCTable()
3. save Huffman table to memory buffer using HUF_writeCTable() 3. save Huffman table to memory buffer using HUF_writeCTable()
4. encode the data stream using HUF_compress_usingCTable() 4. encode the data stream using HUF_compress4X_usingCTable()
The following API allows targeting specific sub-functions for advanced tasks. The following API allows targeting specific sub-functions for advanced tasks.
For example, it's possible to compress several blocks using the same 'CTable', For example, it's possible to compress several blocks using the same 'CTable',
@ -95,7 +95,7 @@ or to save and regenerate 'CTable' using external methods.
typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */ typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */
size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits); size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits);
size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog); size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
size_t HUF_compress4X_into4Segments(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable); size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
/*! /*!

View File

@ -46,6 +46,7 @@ extern "C" {
#include "zstd_v02.h" #include "zstd_v02.h"
#include "zstd_v03.h" #include "zstd_v03.h"
#include "zstd_v04.h" #include "zstd_v04.h"
#include "zstd_v05.h"
MEM_STATIC unsigned ZSTD_isLegacy (U32 magicNumberLE) MEM_STATIC unsigned ZSTD_isLegacy (U32 magicNumberLE)
{ {
@ -53,28 +54,32 @@ MEM_STATIC unsigned ZSTD_isLegacy (U32 magicNumberLE)
{ {
case ZSTDv01_magicNumberLE : case ZSTDv01_magicNumberLE :
case ZSTDv02_magicNumber : case ZSTDv02_magicNumber :
case ZSTDv03_magicNumber : case ZSTDv03_magicNumber :
case ZSTDv04_magicNumber : return 1; case ZSTDv04_magicNumber :
case ZSTDv05_MAGICNUMBER :
return 1;
default : return 0; default : return 0;
} }
} }
MEM_STATIC size_t ZSTD_decompressLegacy( MEM_STATIC size_t ZSTD_decompressLegacy(
void* dst, size_t maxOriginalSize, void* dst, size_t dstCapacity,
const void* src, size_t compressedSize, const void* src, size_t compressedSize,
U32 magicNumberLE) U32 magicNumberLE)
{ {
switch(magicNumberLE) switch(magicNumberLE)
{ {
case ZSTDv01_magicNumberLE : case ZSTDv01_magicNumberLE :
return ZSTDv01_decompress(dst, maxOriginalSize, src, compressedSize); return ZSTDv01_decompress(dst, dstCapacity, src, compressedSize);
case ZSTDv02_magicNumber : case ZSTDv02_magicNumber :
return ZSTDv02_decompress(dst, maxOriginalSize, src, compressedSize); return ZSTDv02_decompress(dst, dstCapacity, src, compressedSize);
case ZSTDv03_magicNumber : case ZSTDv03_magicNumber :
return ZSTDv03_decompress(dst, maxOriginalSize, src, compressedSize); return ZSTDv03_decompress(dst, dstCapacity, src, compressedSize);
case ZSTDv04_magicNumber : case ZSTDv04_magicNumber :
return ZSTDv04_decompress(dst, maxOriginalSize, src, compressedSize); return ZSTDv04_decompress(dst, dstCapacity, src, compressedSize);
case ZSTDv05_MAGICNUMBER :
return ZSTDv05_decompress(dst, dstCapacity, src, compressedSize);
default : default :
return ERROR(prefix_unknown); return ERROR(prefix_unknown);
} }

View File

@ -95,7 +95,7 @@ size_t ZSTDv04_decompressContinue(ZSTDv04_Dctx* dctx, void* dst, size_t maxDstSi
***************************************/ ***************************************/
typedef struct ZBUFFv04_DCtx_s ZBUFFv04_DCtx; typedef struct ZBUFFv04_DCtx_s ZBUFFv04_DCtx;
ZBUFFv04_DCtx* ZBUFFv04_createDCtx(void); ZBUFFv04_DCtx* ZBUFFv04_createDCtx(void);
size_t ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx); size_t ZBUFFv04_freeDCtx(ZBUFFv04_DCtx* dctx);
size_t ZBUFFv04_decompressInit(ZBUFFv04_DCtx* dctx); size_t ZBUFFv04_decompressInit(ZBUFFv04_DCtx* dctx);
size_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* dict, size_t dictSize); size_t ZBUFFv04_decompressWithDictionary(ZBUFFv04_DCtx* dctx, const void* dict, size_t dictSize);

4726
lib/legacy/zstd_v05.c Normal file

File diff suppressed because it is too large Load Diff

156
lib/legacy/zstd_v05.h Normal file
View File

@ -0,0 +1,156 @@
/*
zstd_v05 - decoder for 0.5 format
Header File
Copyright (C) 2014-2016, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at :
- zstd source repository : https://github.com/Cyan4973/zstd
*/
#ifndef ZSTDv05_H
#define ZSTDv05_H
#if defined (__cplusplus)
extern "C" {
#endif
/*-*************************************
* Dependencies
***************************************/
#include <stddef.h> /* size_t */
/* *************************************
* Simple functions
***************************************/
/*! ZSTDv05_decompress() :
`compressedSize` : is the _exact_ size of the compressed blob, otherwise decompression will fail.
`dstCapacity` must be large enough, equal or larger than originalSize.
@return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
or an errorCode if it fails (which can be tested using ZSTDv05_isError()) */
size_t ZSTDv05_decompress( void* dst, size_t dstCapacity,
const void* src, size_t compressedSize);
/* *************************************
* Helper functions
***************************************/
/* Error Management */
unsigned ZSTDv05_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
const char* ZSTDv05_getErrorName(size_t code); /*!< provides readable string for an error code */
/* *************************************
* Explicit memory management
***************************************/
/** Decompression context */
typedef struct ZSTDv05_DCtx_s ZSTDv05_DCtx;
ZSTDv05_DCtx* ZSTDv05_createDCtx(void);
size_t ZSTDv05_freeDCtx(ZSTDv05_DCtx* dctx); /*!< @return : errorCode */
/** ZSTDv05_decompressDCtx() :
* Same as ZSTDv05_decompress(), but requires an already allocated ZSTDv05_DCtx (see ZSTDv05_createDCtx()) */
size_t ZSTDv05_decompressDCtx(ZSTDv05_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
/*-***********************
* Dictionary API
*************************/
/*! ZSTDv05_decompress_usingDict() :
* Decompression using a pre-defined Dictionary content (see dictBuilder).
* Dictionary must be identical to the one used during compression, otherwise regenerated data will be corrupted.
* Note : dict can be NULL, in which case, it's equivalent to ZSTDv05_decompressDCtx() */
size_t ZSTDv05_decompress_usingDict(ZSTDv05_DCtx* dctx,
void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
const void* dict,size_t dictSize);
typedef struct ZBUFFv05_DCtx_s ZBUFFv05_DCtx;
ZBUFFv05_DCtx* ZBUFFv05_createDCtx(void);
size_t ZBUFFv05_freeDCtx(ZBUFFv05_DCtx* dctx);
size_t ZBUFFv05_decompressInit(ZBUFFv05_DCtx* dctx);
size_t ZBUFFv05_decompressInitDictionary(ZBUFFv05_DCtx* dctx, const void* dict, size_t dictSize);
size_t ZBUFFv05_decompressContinue(ZBUFFv05_DCtx* dctx,
void* dst, size_t* dstCapacityPtr,
const void* src, size_t* srcSizePtr);
/*-***************************************************************************
* Streaming decompression
*
* A ZBUFFv05_DCtx object is required to track streaming operations.
* Use ZBUFFv05_createDCtx() and ZBUFFv05_freeDCtx() to create/release resources.
* Use ZBUFFv05_decompressInit() to start a new decompression operation,
* or ZBUFFv05_decompressInitDictionary() if decompression requires a dictionary.
* Note that ZBUFFv05_DCtx objects can be reused multiple times.
*
* Use ZBUFFv05_decompressContinue() repetitively to consume your input.
* *srcSizePtr and *dstCapacityPtr can be any size.
* The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
* Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
* The content of @dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change @dst.
* @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency)
* or 0 when a frame is completely decoded
* or an error code, which can be tested using ZBUFFv05_isError().
*
* Hint : recommended buffer sizes (not compulsory) : ZBUFFv05_recommendedDInSize() / ZBUFFv05_recommendedDOutSize()
* output : ZBUFFv05_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
* input : ZBUFFv05_recommendedDInSize==128Kb+3; just follow indications from ZBUFFv05_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
* *******************************************************************************/
/* *************************************
* Tool functions
***************************************/
unsigned ZBUFFv05_isError(size_t errorCode);
const char* ZBUFFv05_getErrorName(size_t errorCode);
/** Functions below provide recommended buffer sizes for Compression or Decompression operations.
* These sizes are just hints, and tend to offer better latency */
size_t ZBUFFv05_recommendedDInSize(void);
size_t ZBUFFv05_recommendedDOutSize(void);
/*-*************************************
* Constants
***************************************/
#define ZSTDv05_MAGICNUMBER 0xFD2FB525 /* v0.5 */
#if defined (__cplusplus)
}
#endif
#endif /* ZSTDv0505_H */

View File

@ -187,7 +187,6 @@ MEM_STATIC void MEM_write64(void* memPtr, U64 value)
#endif /* MEM_FORCE_MEMORY_ACCESS */ #endif /* MEM_FORCE_MEMORY_ACCESS */
MEM_STATIC U16 MEM_readLE16(const void* memPtr) MEM_STATIC U16 MEM_readLE16(const void* memPtr)
{ {
if (MEM_isLittleEndian()) if (MEM_isLittleEndian())
@ -276,6 +275,20 @@ MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
MEM_writeLE64(memPtr, (U64)val); MEM_writeLE64(memPtr, (U64)val);
} }
/* function safe only for comparisons */
MEM_STATIC U32 MEM_readMINMATCH(const void* memPtr, U32 length)
{
switch (length)
{
default :
case 4 : return MEM_read32(memPtr);
case 3 : if (MEM_isLittleEndian())
return MEM_read32(memPtr)<<8;
else
return MEM_read32(memPtr)>>8;
}
}
#if defined (__cplusplus) #if defined (__cplusplus)
} }
#endif #endif

View File

@ -26,31 +26,27 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at : You can contact the author at :
- zstd source repository : https://github.com/Cyan4973/zstd - zstd homepage : http://www.zstd.net/
- ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
*/ */
/* The objects defined into this file should be considered experimental.
* They are not labelled stable, as their prototype may change in the future.
* You can use them for tests, provide feedback, or if you can endure risk of future changes.
*/
/* ************************************* /* *************************************
* Dependencies * Dependencies
***************************************/ ***************************************/
#include <stdlib.h> #include <stdlib.h>
#include "error_private.h" #include "error_private.h"
#include "zstd_static.h" #include "zstd_internal.h" /* MIN, ZSTD_blockHeaderSize */
#include "zstd_static.h" /* ZSTD_BLOCKSIZE_MAX */
#include "zbuff_static.h" #include "zbuff_static.h"
/* ************************************* /* *************************************
* Constants * Constants
***************************************/ ***************************************/
static size_t ZBUFF_blockHeaderSize = 3; static size_t const ZBUFF_endFrameSize = ZSTD_BLOCKHEADERSIZE;
static size_t ZBUFF_endFrameSize = 3;
/** ************************************************
/*_**************************************************
* Streaming compression * Streaming compression
* *
* A ZBUFF_CCtx object is required to track streaming operation. * A ZBUFF_CCtx object is required to track streaming operation.
@ -59,28 +55,28 @@ static size_t ZBUFF_endFrameSize = 3;
* ZBUFF_CCtx objects can be reused multiple times. * ZBUFF_CCtx objects can be reused multiple times.
* *
* Use ZBUFF_compressContinue() repetitively to consume your input. * Use ZBUFF_compressContinue() repetitively to consume your input.
* *srcSizePtr and *maxDstSizePtr can be any size. * *srcSizePtr and *dstCapacityPtr can be any size.
* The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
* Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input. * Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input.
* The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst . * The content of dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change dst .
* @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency) * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
* or an error code, which can be tested using ZBUFF_isError(). * or an error code, which can be tested using ZBUFF_isError().
* *
* ZBUFF_compressFlush() can be used to instruct ZBUFF to compress and output whatever remains within its buffer. * ZBUFF_compressFlush() can be used to instruct ZBUFF to compress and output whatever remains within its buffer.
* Note that it will not output more than *maxDstSizePtr. * Note that it will not output more than *dstCapacityPtr.
* Therefore, some content might still be left into its internal buffer if dst buffer is too small. * Therefore, some content might still be left into its internal buffer if dst buffer is too small.
* @return : nb of bytes still present into internal buffer (0 if it's empty) * @return : nb of bytes still present into internal buffer (0 if it's empty)
* or an error code, which can be tested using ZBUFF_isError(). * or an error code, which can be tested using ZBUFF_isError().
* *
* ZBUFF_compressEnd() instructs to finish a frame. * ZBUFF_compressEnd() instructs to finish a frame.
* It will perform a flush and write frame epilogue. * It will perform a flush and write frame epilogue.
* Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *maxDstSizePtr is too small. * Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.
* @return : nb of bytes still present into internal buffer (0 if it's empty) * @return : nb of bytes still present into internal buffer (0 if it's empty)
* or an error code, which can be tested using ZBUFF_isError(). * or an error code, which can be tested using ZBUFF_isError().
* *
* Hint : recommended buffer sizes (not compulsory) * Hint : recommended buffer sizes (not compulsory)
* input : 128 KB block size is the internal unit, it improves latency to use this value. * input : ZSTD_BLOCKSIZE_MAX (128 KB), internal unit size, it improves latency to use this value.
* output : ZSTD_compressBound(128 KB) + 3 + 3 : ensures it's always possible to write/flush/end a full block at best speed. * output : ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + ZBUFF_endFrameSize : ensures it's always possible to write/flush/end a full block at best speed.
* **************************************************/ * **************************************************/
typedef enum { ZBUFFcs_init, ZBUFFcs_load, ZBUFFcs_flush } ZBUFF_cStage; typedef enum { ZBUFFcs_init, ZBUFFcs_load, ZBUFFcs_flush } ZBUFF_cStage;
@ -88,13 +84,13 @@ typedef enum { ZBUFFcs_init, ZBUFFcs_load, ZBUFFcs_flush } ZBUFF_cStage;
/* *** Ressources *** */ /* *** Ressources *** */
struct ZBUFF_CCtx_s { struct ZBUFF_CCtx_s {
ZSTD_CCtx* zc; ZSTD_CCtx* zc;
char* inBuff; char* inBuff;
size_t inBuffSize; size_t inBuffSize;
size_t inToCompress; size_t inToCompress;
size_t inBuffPos; size_t inBuffPos;
size_t inBuffTarget; size_t inBuffTarget;
size_t blockSize; size_t blockSize;
char* outBuff; char* outBuff;
size_t outBuffSize; size_t outBuffSize;
size_t outBuffContentSize; size_t outBuffContentSize;
size_t outBuffFlushedSize; size_t outBuffFlushedSize;
@ -123,23 +119,20 @@ size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
/* *** Initialization *** */ /* *** Initialization *** */
#define MIN(a,b) ( ((a)<(b)) ? (a) : (b) ) size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
#define BLOCKSIZE (128 * 1024) /* a bit too "magic", should come from reference */ const void* dict, size_t dictSize,
size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, ZSTD_parameters params) ZSTD_parameters params, U64 pledgedSrcSize)
{ {
size_t neededInBuffSize;
ZSTD_validateParams(&params);
neededInBuffSize = (size_t)1 << params.windowLog;
/* allocate buffers */ /* allocate buffers */
if (zbc->inBuffSize < neededInBuffSize) { { size_t const neededInBuffSize = (size_t)1 << params.cParams.windowLog;
zbc->inBuffSize = neededInBuffSize; if (zbc->inBuffSize < neededInBuffSize) {
free(zbc->inBuff); /* should not be necessary */ zbc->inBuffSize = neededInBuffSize;
zbc->inBuff = (char*)malloc(neededInBuffSize); free(zbc->inBuff); /* should not be necessary */
if (zbc->inBuff == NULL) return ERROR(memory_allocation); zbc->inBuff = (char*)malloc(neededInBuffSize);
if (zbc->inBuff == NULL) return ERROR(memory_allocation);
}
zbc->blockSize = MIN(ZSTD_BLOCKSIZE_MAX, neededInBuffSize/2);
} }
zbc->blockSize = MIN(BLOCKSIZE, zbc->inBuffSize);
if (zbc->outBuffSize < ZSTD_compressBound(zbc->blockSize)+1) { if (zbc->outBuffSize < ZSTD_compressBound(zbc->blockSize)+1) {
zbc->outBuffSize = ZSTD_compressBound(zbc->blockSize)+1; zbc->outBuffSize = ZSTD_compressBound(zbc->blockSize)+1;
free(zbc->outBuff); /* should not be necessary */ free(zbc->outBuff); /* should not be necessary */
@ -147,50 +140,54 @@ size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc, const void* dict, size_t dic
if (zbc->outBuff == NULL) return ERROR(memory_allocation); if (zbc->outBuff == NULL) return ERROR(memory_allocation);
} }
zbc->outBuffContentSize = ZSTD_compressBegin_advanced(zbc->zc, dict, dictSize, params); { size_t const errorCode = ZSTD_compressBegin_advanced(zbc->zc, dict, dictSize, params, pledgedSrcSize);
if (ZSTD_isError(zbc->outBuffContentSize)) return zbc->outBuffContentSize; if (ZSTD_isError(errorCode)) return errorCode; }
zbc->inToCompress = 0; zbc->inToCompress = 0;
zbc->inBuffPos = 0; zbc->inBuffPos = 0;
zbc->inBuffTarget = zbc->blockSize; zbc->inBuffTarget = zbc->blockSize;
zbc->outBuffFlushedSize = 0; zbc->outBuffFlushedSize = 0;
zbc->stage = ZBUFFcs_flush; /* starts by flushing the header */ zbc->stage = ZBUFFcs_load;
return 0; /* ready to go */ return 0; /* ready to go */
} }
size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
{
ZSTD_parameters params;
params.cParams = ZSTD_getCParams(compressionLevel, 0, dictSize);
params.fParams.contentSizeFlag = 0;
ZSTD_adjustCParams(&params.cParams, 0, dictSize);
return ZBUFF_compressInit_advanced(zbc, dict, dictSize, params, 0);
}
size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel) size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)
{ {
return ZBUFF_compressInit_advanced(zbc, NULL, 0, ZSTD_getParams(compressionLevel, 0)); return ZBUFF_compressInitDictionary(zbc, NULL, 0, compressionLevel);
}
ZSTDLIB_API size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
{
return ZBUFF_compressInit_advanced(zbc, dict, dictSize, ZSTD_getParams(compressionLevel, 0));
} }
/* *** Compression *** */ /* *** Compression *** */
static size_t ZBUFF_limitCopy(void* dst, size_t maxDstSize, const void* src, size_t srcSize) static size_t ZBUFF_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
{ {
size_t length = MIN(maxDstSize, srcSize); size_t length = MIN(dstCapacity, srcSize);
memcpy(dst, src, length); memcpy(dst, src, length);
return length; return length;
} }
static size_t ZBUFF_compressContinue_generic(ZBUFF_CCtx* zbc, static size_t ZBUFF_compressContinue_generic(ZBUFF_CCtx* zbc,
void* dst, size_t* maxDstSizePtr, void* dst, size_t* dstCapacityPtr,
const void* src, size_t* srcSizePtr, const void* src, size_t* srcSizePtr,
int flush) /* aggregate : wait for full block before compressing */ int flush) /* aggregate : wait for full block before compressing */
{ {
U32 notDone = 1; U32 notDone = 1;
const char* const istart = (const char*)src; const char* const istart = (const char*)src;
const char* ip = istart;
const char* const iend = istart + *srcSizePtr; const char* const iend = istart + *srcSizePtr;
const char* ip = istart;
char* const ostart = (char*)dst; char* const ostart = (char*)dst;
char* const oend = ostart + *dstCapacityPtr;
char* op = ostart; char* op = ostart;
char* const oend = ostart + *maxDstSizePtr;
while (notDone) { while (notDone) {
switch(zbc->stage) switch(zbc->stage)
@ -199,19 +196,17 @@ static size_t ZBUFF_compressContinue_generic(ZBUFF_CCtx* zbc,
case ZBUFFcs_load: case ZBUFFcs_load:
/* complete inBuffer */ /* complete inBuffer */
{ { size_t const toLoad = zbc->inBuffTarget - zbc->inBuffPos;
size_t toLoad = zbc->inBuffTarget - zbc->inBuffPos; size_t const loaded = ZBUFF_limitCopy(zbc->inBuff + zbc->inBuffPos, toLoad, ip, iend-ip);
size_t loaded = ZBUFF_limitCopy(zbc->inBuff + zbc->inBuffPos, toLoad, ip, iend-ip);
zbc->inBuffPos += loaded; zbc->inBuffPos += loaded;
ip += loaded; ip += loaded;
if ( (zbc->inBuffPos==zbc->inToCompress) || (!flush && (toLoad != loaded)) ) { if ( (zbc->inBuffPos==zbc->inToCompress) || (!flush && (toLoad != loaded)) ) {
notDone = 0; break; /* not enough input to get a full block : stop there, wait for more */ notDone = 0; break; /* not enough input to get a full block : stop there, wait for more */
} } } }
/* compress current block (note : this stage cannot be stopped in the middle) */ /* compress current block (note : this stage cannot be stopped in the middle) */
{ { void* cDst;
void* cDst;
size_t cSize; size_t cSize;
size_t iSize = zbc->inBuffPos - zbc->inToCompress; size_t const iSize = zbc->inBuffPos - zbc->inToCompress;
size_t oSize = oend-op; size_t oSize = oend-op;
if (oSize >= ZSTD_compressBound(iSize)) if (oSize >= ZSTD_compressBound(iSize))
cDst = op; /* compress directly into output buffer (avoid flush stage) */ cDst = op; /* compress directly into output buffer (avoid flush stage) */
@ -222,20 +217,18 @@ static size_t ZBUFF_compressContinue_generic(ZBUFF_CCtx* zbc,
/* prepare next block */ /* prepare next block */
zbc->inBuffTarget = zbc->inBuffPos + zbc->blockSize; zbc->inBuffTarget = zbc->inBuffPos + zbc->blockSize;
if (zbc->inBuffTarget > zbc->inBuffSize) if (zbc->inBuffTarget > zbc->inBuffSize)
{ zbc->inBuffPos = 0; zbc->inBuffTarget = zbc->blockSize; } /* note : inBuffSize >= blockSize */ zbc->inBuffPos = 0, zbc->inBuffTarget = zbc->blockSize; /* note : inBuffSize >= blockSize */
zbc->inToCompress = zbc->inBuffPos; zbc->inToCompress = zbc->inBuffPos;
if (cDst == op) { op += cSize; break; } /* no need to flush */ if (cDst == op) { op += cSize; break; } /* no need to flush */
zbc->outBuffContentSize = cSize; zbc->outBuffContentSize = cSize;
zbc->outBuffFlushedSize = 0; zbc->outBuffFlushedSize = 0;
zbc->stage = ZBUFFcs_flush; zbc->stage = ZBUFFcs_flush; /* continue to flush stage */
// break; /* flush stage follows */
} }
case ZBUFFcs_flush: case ZBUFFcs_flush:
/* flush into dst */ /* flush into dst */
{ { size_t const toFlush = zbc->outBuffContentSize - zbc->outBuffFlushedSize;
size_t toFlush = zbc->outBuffContentSize - zbc->outBuffFlushedSize; size_t const flushed = ZBUFF_limitCopy(op, oend-op, zbc->outBuff + zbc->outBuffFlushedSize, toFlush);
size_t flushed = ZBUFF_limitCopy(op, oend-op, zbc->outBuff + zbc->outBuffFlushedSize, toFlush);
op += flushed; op += flushed;
zbc->outBuffFlushedSize += flushed; zbc->outBuffFlushedSize += flushed;
if (toFlush!=flushed) { notDone = 0; break; } /* not enough space within dst to store compressed block : stop there */ if (toFlush!=flushed) { notDone = 0; break; } /* not enough space within dst to store compressed block : stop there */
@ -250,41 +243,40 @@ static size_t ZBUFF_compressContinue_generic(ZBUFF_CCtx* zbc,
} }
*srcSizePtr = ip - istart; *srcSizePtr = ip - istart;
*maxDstSizePtr = op - ostart; *dstCapacityPtr = op - ostart;
{ { size_t hintInSize = zbc->inBuffTarget - zbc->inBuffPos;
size_t hintInSize = zbc->inBuffTarget - zbc->inBuffPos;
if (hintInSize==0) hintInSize = zbc->blockSize; if (hintInSize==0) hintInSize = zbc->blockSize;
return hintInSize; return hintInSize;
} }
} }
size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc, size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,
void* dst, size_t* maxDstSizePtr, void* dst, size_t* dstCapacityPtr,
const void* src, size_t* srcSizePtr) const void* src, size_t* srcSizePtr)
{ {
return ZBUFF_compressContinue_generic(zbc, dst, maxDstSizePtr, src, srcSizePtr, 0); return ZBUFF_compressContinue_generic(zbc, dst, dstCapacityPtr, src, srcSizePtr, 0);
} }
/* *** Finalize *** */ /* *** Finalize *** */
size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* maxDstSizePtr) size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
{ {
size_t srcSize = 0; size_t srcSize = 0;
ZBUFF_compressContinue_generic(zbc, dst, maxDstSizePtr, &srcSize, &srcSize, 1); /* use a valid src address instead of NULL, as some sanitizer don't like it */ ZBUFF_compressContinue_generic(zbc, dst, dstCapacityPtr, &srcSize, &srcSize, 1); /* use a valid src address instead of NULL */
return zbc->outBuffContentSize - zbc->outBuffFlushedSize; return zbc->outBuffContentSize - zbc->outBuffFlushedSize;
} }
size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* maxDstSizePtr) size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
{ {
BYTE* const ostart = (BYTE*)dst; BYTE* const ostart = (BYTE*)dst;
BYTE* const oend = ostart + *dstCapacityPtr;
BYTE* op = ostart; BYTE* op = ostart;
BYTE* const oend = ostart + *maxDstSizePtr; size_t outSize = *dstCapacityPtr;
size_t outSize = *maxDstSizePtr;
size_t epilogueSize, remaining; size_t epilogueSize, remaining;
ZBUFF_compressFlush(zbc, dst, &outSize); /* flush any remaining inBuff */ ZBUFF_compressFlush(zbc, dst, &outSize); /* flush any remaining inBuff */
op += outSize; op += outSize;
epilogueSize = ZSTD_compressEnd(zbc->zc, zbc->outBuff + zbc->outBuffContentSize, zbc->outBuffSize - zbc->outBuffContentSize); /* epilogue into outBuff */ epilogueSize = ZSTD_compressEnd(zbc->zc, zbc->outBuff + zbc->outBuffContentSize, zbc->outBuffSize - zbc->outBuffContentSize); /* epilogue into outBuff */
zbc->outBuffContentSize += epilogueSize; zbc->outBuffContentSize += epilogueSize;
@ -292,228 +284,193 @@ size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* maxDstSizePtr)
zbc->stage = ZBUFFcs_flush; zbc->stage = ZBUFFcs_flush;
remaining = ZBUFF_compressFlush(zbc, op, &outSize); /* attempt to flush epilogue into dst */ remaining = ZBUFF_compressFlush(zbc, op, &outSize); /* attempt to flush epilogue into dst */
op += outSize; op += outSize;
if (!remaining) zbc->stage = ZBUFFcs_init; /* close only if nothing left to flush */ if (!remaining) zbc->stage = ZBUFFcs_init; /* close only if nothing left to flush */
*maxDstSizePtr = op-ostart; /* tells how many bytes were written */ *dstCapacityPtr = op-ostart; /* tells how many bytes were written */
return remaining; return remaining;
} }
/*-***************************************************************************
/** ************************************************ * Streaming decompression howto
* Streaming decompression
* *
* A ZBUFF_DCtx object is required to track streaming operation. * A ZBUFF_DCtx object is required to track streaming operations.
* Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources. * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources.
* Use ZBUFF_decompressInit() to start a new decompression operation. * Use ZBUFF_decompressInit() to start a new decompression operation,
* ZBUFF_DCtx objects can be reused multiple times. * or ZBUFF_decompressInitDictionary() if decompression requires a dictionary.
* Note that ZBUFF_DCtx objects can be re-init multiple times.
* *
* Use ZBUFF_decompressContinue() repetitively to consume your input. * Use ZBUFF_decompressContinue() repetitively to consume your input.
* *srcSizePtr and *maxDstSizePtr can be any size. * *srcSizePtr and *dstCapacityPtr can be any size.
* The function will report how many bytes were read or written by modifying *srcSizePtr and *maxDstSizePtr. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
* Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
* The content of dst will be overwritten (up to *maxDstSizePtr) at each function call, so save its content if it matters or change dst . * The content of @dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change @dst.
* @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency) * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
* or 0 when a frame is completely decoded * or 0 when a frame is completely decoded,
* or an error code, which can be tested using ZBUFF_isError(). * or an error code, which can be tested using ZBUFF_isError().
* *
* Hint : recommended buffer sizes (not compulsory) * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize() and ZBUFF_recommendedDOutSize()
* output : 128 KB block size is the internal unit, it ensures it's always possible to write a full block when it's decoded. * output : ZBUFF_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
* input : just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 . * input : ZBUFF_recommendedDInSize == 128KB + 3;
* **************************************************/ * just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
* *******************************************************************************/
typedef enum { ZBUFFds_init, ZBUFFds_readHeader, ZBUFFds_loadHeader, ZBUFFds_decodeHeader, typedef enum { ZBUFFds_init, ZBUFFds_readHeader,
ZBUFFds_read, ZBUFFds_load, ZBUFFds_flush } ZBUFF_dStage; ZBUFFds_read, ZBUFFds_load, ZBUFFds_flush } ZBUFF_dStage;
/* *** Resource management *** */ /* *** Resource management *** */
#define ZSTD_frameHeaderSize_max 5 /* too magical, should come from reference */
struct ZBUFF_DCtx_s { struct ZBUFF_DCtx_s {
ZSTD_DCtx* zc; ZSTD_DCtx* zd;
ZSTD_parameters params; ZSTD_frameParams fParams;
char* inBuff; size_t blockSize;
char* inBuff;
size_t inBuffSize; size_t inBuffSize;
size_t inPos; size_t inPos;
char* outBuff; char* outBuff;
size_t outBuffSize; size_t outBuffSize;
size_t outStart; size_t outStart;
size_t outEnd; size_t outEnd;
size_t hPos;
ZBUFF_dStage stage; ZBUFF_dStage stage;
unsigned char headerBuffer[ZSTD_frameHeaderSize_max];
}; /* typedef'd to ZBUFF_DCtx within "zstd_buffered.h" */ }; /* typedef'd to ZBUFF_DCtx within "zstd_buffered.h" */
ZBUFF_DCtx* ZBUFF_createDCtx(void) ZBUFF_DCtx* ZBUFF_createDCtx(void)
{ {
ZBUFF_DCtx* zbc = (ZBUFF_DCtx*)malloc(sizeof(ZBUFF_DCtx)); ZBUFF_DCtx* zbd = (ZBUFF_DCtx*)malloc(sizeof(ZBUFF_DCtx));
if (zbc==NULL) return NULL; if (zbd==NULL) return NULL;
memset(zbc, 0, sizeof(*zbc)); memset(zbd, 0, sizeof(*zbd));
zbc->zc = ZSTD_createDCtx(); zbd->zd = ZSTD_createDCtx();
zbc->stage = ZBUFFds_init; zbd->stage = ZBUFFds_init;
return zbc; return zbd;
} }
size_t ZBUFF_freeDCtx(ZBUFF_DCtx* zbc) size_t ZBUFF_freeDCtx(ZBUFF_DCtx* zbd)
{ {
if (zbc==NULL) return 0; /* support free on null */ if (zbd==NULL) return 0; /* support free on null */
ZSTD_freeDCtx(zbc->zc); ZSTD_freeDCtx(zbd->zd);
free(zbc->inBuff); free(zbd->inBuff);
free(zbc->outBuff); free(zbd->outBuff);
free(zbc); free(zbd);
return 0; return 0;
} }
/* *** Initialization *** */ /* *** Initialization *** */
size_t ZBUFF_decompressInitDictionary(ZBUFF_DCtx* zbc, const void* dict, size_t dictSize) size_t ZBUFF_decompressInitDictionary(ZBUFF_DCtx* zbd, const void* dict, size_t dictSize)
{ {
zbc->stage = ZBUFFds_readHeader; zbd->stage = ZBUFFds_readHeader;
zbc->hPos = zbc->inPos = zbc->outStart = zbc->outEnd = 0; zbd->inPos = zbd->outStart = zbd->outEnd = 0;
return ZSTD_decompressBegin_usingDict(zbc->zc, dict, dictSize); return ZSTD_decompressBegin_usingDict(zbd->zd, dict, dictSize);
} }
size_t ZBUFF_decompressInit(ZBUFF_DCtx* zbc) size_t ZBUFF_decompressInit(ZBUFF_DCtx* zbd)
{ {
return ZBUFF_decompressInitDictionary(zbc, NULL, 0); return ZBUFF_decompressInitDictionary(zbd, NULL, 0);
} }
/* *** Decompression *** */ /* *** Decompression *** */
size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDstSizePtr, const void* src, size_t* srcSizePtr) size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbd,
void* dst, size_t* dstCapacityPtr,
const void* src, size_t* srcSizePtr)
{ {
const char* const istart = (const char*)src; const char* const istart = (const char*)src;
const char* ip = istart;
const char* const iend = istart + *srcSizePtr; const char* const iend = istart + *srcSizePtr;
const char* ip = istart;
char* const ostart = (char*)dst; char* const ostart = (char*)dst;
char* const oend = ostart + *dstCapacityPtr;
char* op = ostart; char* op = ostart;
char* const oend = ostart + *maxDstSizePtr;
U32 notDone = 1; U32 notDone = 1;
while (notDone) { while (notDone) {
switch(zbc->stage) switch(zbd->stage)
{ {
case ZBUFFds_init : case ZBUFFds_init :
return ERROR(init_missing); return ERROR(init_missing);
case ZBUFFds_readHeader : case ZBUFFds_readHeader :
/* read header from src */ /* read header from src */
{ { size_t const headerSize = ZSTD_getFrameParams(&(zbd->fParams), src, *srcSizePtr);
size_t headerSize = ZSTD_getFrameParams(&(zbc->params), src, *srcSizePtr);
if (ZSTD_isError(headerSize)) return headerSize; if (ZSTD_isError(headerSize)) return headerSize;
if (headerSize) { if (headerSize) {
/* not enough input to decode header : tell how many bytes would be necessary */ /* not enough input to decode header : needs headerSize > *srcSizePtr */
memcpy(zbc->headerBuffer+zbc->hPos, src, *srcSizePtr); *dstCapacityPtr = 0;
zbc->hPos += *srcSizePtr; *srcSizePtr = 0;
*maxDstSizePtr = 0; return headerSize;
zbc->stage = ZBUFFds_loadHeader; } }
return headerSize - zbc->hPos;
}
zbc->stage = ZBUFFds_decodeHeader;
break;
}
case ZBUFFds_loadHeader: /* Frame header instruct buffer sizes */
/* complete header from src */ { size_t const blockSize = MIN(1 << zbd->fParams.windowLog, ZSTD_BLOCKSIZE_MAX);
{ zbd->blockSize = blockSize;
size_t headerSize = ZBUFF_limitCopy( if (zbd->inBuffSize < blockSize) {
zbc->headerBuffer + zbc->hPos, ZSTD_frameHeaderSize_max - zbc->hPos, free(zbd->inBuff);
src, *srcSizePtr); zbd->inBuffSize = blockSize;
zbc->hPos += headerSize; zbd->inBuff = (char*)malloc(blockSize);
ip += headerSize; if (zbd->inBuff == NULL) return ERROR(memory_allocation);
headerSize = ZSTD_getFrameParams(&(zbc->params), zbc->headerBuffer, zbc->hPos);
if (ZSTD_isError(headerSize)) return headerSize;
if (headerSize) {
/* not enough input to decode header : tell how many bytes would be necessary */
*maxDstSizePtr = 0;
return headerSize - zbc->hPos;
} }
// zbc->stage = ZBUFFds_decodeHeader; break; /* useless : stage follows */ { size_t const neededOutSize = ((size_t)1 << zbd->fParams.windowLog) + blockSize;
} if (zbd->outBuffSize < neededOutSize) {
free(zbd->outBuff);
case ZBUFFds_decodeHeader: zbd->outBuffSize = neededOutSize;
/* apply header to create / resize buffers */ zbd->outBuff = (char*)malloc(neededOutSize);
{ if (zbd->outBuff == NULL) return ERROR(memory_allocation);
size_t neededOutSize = (size_t)1 << zbc->params.windowLog; } } }
size_t neededInSize = BLOCKSIZE; /* a block is never > BLOCKSIZE */ zbd->stage = ZBUFFds_read;
if (zbc->inBuffSize < neededInSize) {
free(zbc->inBuff);
zbc->inBuffSize = neededInSize;
zbc->inBuff = (char*)malloc(neededInSize);
if (zbc->inBuff == NULL) return ERROR(memory_allocation);
}
if (zbc->outBuffSize < neededOutSize) {
free(zbc->outBuff);
zbc->outBuffSize = neededOutSize;
zbc->outBuff = (char*)malloc(neededOutSize);
if (zbc->outBuff == NULL) return ERROR(memory_allocation);
} }
if (zbc->hPos) {
/* some data already loaded into headerBuffer : transfer into inBuff */
memcpy(zbc->inBuff, zbc->headerBuffer, zbc->hPos);
zbc->inPos = zbc->hPos;
zbc->hPos = 0;
zbc->stage = ZBUFFds_load;
break;
}
zbc->stage = ZBUFFds_read;
case ZBUFFds_read: case ZBUFFds_read:
{ { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zbd->zd);
size_t neededInSize = ZSTD_nextSrcSizeToDecompress(zbc->zc);
if (neededInSize==0) { /* end of frame */ if (neededInSize==0) { /* end of frame */
zbc->stage = ZBUFFds_init; zbd->stage = ZBUFFds_init;
notDone = 0; notDone = 0;
break; break;
} }
if ((size_t)(iend-ip) >= neededInSize) { if ((size_t)(iend-ip) >= neededInSize) {
/* directly decode from src */ /* directly decode from src */
size_t decodedSize = ZSTD_decompressContinue(zbc->zc, size_t const decodedSize = ZSTD_decompressContinue(zbd->zd,
zbc->outBuff + zbc->outStart, zbc->outBuffSize - zbc->outStart, zbd->outBuff + zbd->outStart, zbd->outBuffSize - zbd->outStart,
ip, neededInSize); ip, neededInSize);
if (ZSTD_isError(decodedSize)) return decodedSize; if (ZSTD_isError(decodedSize)) return decodedSize;
ip += neededInSize; ip += neededInSize;
if (!decodedSize) break; /* this was just a header */ if (!decodedSize) break; /* this was just a header */
zbc->outEnd = zbc->outStart + decodedSize; zbd->outEnd = zbd->outStart + decodedSize;
zbc->stage = ZBUFFds_flush; zbd->stage = ZBUFFds_flush;
break; break;
} }
if (ip==iend) { notDone = 0; break; } /* no more input */ if (ip==iend) { notDone = 0; break; } /* no more input */
zbc->stage = ZBUFFds_load; zbd->stage = ZBUFFds_load;
} }
case ZBUFFds_load: case ZBUFFds_load:
{ { size_t const neededInSize = ZSTD_nextSrcSizeToDecompress(zbd->zd);
size_t neededInSize = ZSTD_nextSrcSizeToDecompress(zbc->zc); size_t const toLoad = neededInSize - zbd->inPos; /* should always be <= remaining space within inBuff */
size_t toLoad = neededInSize - zbc->inPos; /* should always be <= remaining space within inBuff */
size_t loadedSize; size_t loadedSize;
if (toLoad > zbc->inBuffSize - zbc->inPos) return ERROR(corruption_detected); /* should never happen */ if (toLoad > zbd->inBuffSize - zbd->inPos) return ERROR(corruption_detected); /* should never happen */
loadedSize = ZBUFF_limitCopy(zbc->inBuff + zbc->inPos, toLoad, ip, iend-ip); loadedSize = ZBUFF_limitCopy(zbd->inBuff + zbd->inPos, toLoad, ip, iend-ip);
ip += loadedSize; ip += loadedSize;
zbc->inPos += loadedSize; zbd->inPos += loadedSize;
if (loadedSize < toLoad) { notDone = 0; break; } /* not enough input, wait for more */ if (loadedSize < toLoad) { notDone = 0; break; } /* not enough input, wait for more */
{ /* decode loaded input */
size_t decodedSize = ZSTD_decompressContinue(zbc->zc, { size_t const decodedSize = ZSTD_decompressContinue(zbd->zd,
zbc->outBuff + zbc->outStart, zbc->outBuffSize - zbc->outStart, zbd->outBuff + zbd->outStart, zbd->outBuffSize - zbd->outStart,
zbc->inBuff, neededInSize); zbd->inBuff, neededInSize);
if (ZSTD_isError(decodedSize)) return decodedSize; if (ZSTD_isError(decodedSize)) return decodedSize;
zbc->inPos = 0; /* input is consumed */ zbd->inPos = 0; /* input is consumed */
if (!decodedSize) { zbc->stage = ZBUFFds_read; break; } /* this was just a header */ if (!decodedSize) { zbd->stage = ZBUFFds_read; break; } /* this was just a header */
zbc->outEnd = zbc->outStart + decodedSize; zbd->outEnd = zbd->outStart + decodedSize;
zbc->stage = ZBUFFds_flush; zbd->stage = ZBUFFds_flush;
// break; /* ZBUFFds_flush follows */ // break; /* ZBUFFds_flush follows */
} } } }
case ZBUFFds_flush: case ZBUFFds_flush:
{ { size_t const toFlushSize = zbd->outEnd - zbd->outStart;
size_t toFlushSize = zbc->outEnd - zbc->outStart; size_t const flushedSize = ZBUFF_limitCopy(op, oend-op, zbd->outBuff + zbd->outStart, toFlushSize);
size_t flushedSize = ZBUFF_limitCopy(op, oend-op, zbc->outBuff + zbc->outStart, toFlushSize);
op += flushedSize; op += flushedSize;
zbc->outStart += flushedSize; zbd->outStart += flushedSize;
if (flushedSize == toFlushSize) { if (flushedSize == toFlushSize) {
zbc->stage = ZBUFFds_read; zbd->stage = ZBUFFds_read;
if (zbc->outStart + BLOCKSIZE > zbc->outBuffSize) if (zbd->outStart + zbd->blockSize > zbd->outBuffSize)
zbc->outStart = zbc->outEnd = 0; zbd->outStart = zbd->outEnd = 0;
break; break;
} }
/* cannot flush everything */ /* cannot flush everything */
@ -523,13 +480,12 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDstSizePt
default: return ERROR(GENERIC); /* impossible */ default: return ERROR(GENERIC); /* impossible */
} } } }
/* result */
*srcSizePtr = ip-istart; *srcSizePtr = ip-istart;
*maxDstSizePtr = op-ostart; *dstCapacityPtr = op-ostart;
{ size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zbd->zd);
{ if (nextSrcSizeHint > ZSTD_blockHeaderSize) nextSrcSizeHint+= ZSTD_blockHeaderSize; /* get following block header too */
size_t nextSrcSizeHint = ZSTD_nextSrcSizeToDecompress(zbc->zc); nextSrcSizeHint -= zbd->inPos; /* already loaded*/
if (nextSrcSizeHint > ZBUFF_blockHeaderSize) nextSrcSizeHint+= ZBUFF_blockHeaderSize; /* get next block header too */
nextSrcSizeHint -= zbc->inPos; /* already loaded*/
return nextSrcSizeHint; return nextSrcSizeHint;
} }
} }
@ -542,7 +498,7 @@ size_t ZBUFF_decompressContinue(ZBUFF_DCtx* zbc, void* dst, size_t* maxDstSizePt
unsigned ZBUFF_isError(size_t errorCode) { return ERR_isError(errorCode); } unsigned ZBUFF_isError(size_t errorCode) { return ERR_isError(errorCode); }
const char* ZBUFF_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); } const char* ZBUFF_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
size_t ZBUFF_recommendedCInSize(void) { return BLOCKSIZE; } size_t ZBUFF_recommendedCInSize(void) { return ZSTD_BLOCKSIZE_MAX; }
size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_compressBound(BLOCKSIZE) + ZBUFF_blockHeaderSize + ZBUFF_endFrameSize; } size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + ZBUFF_endFrameSize; }
size_t ZBUFF_recommendedDInSize(void) { return BLOCKSIZE + ZBUFF_blockHeaderSize /* block header size*/ ; } size_t ZBUFF_recommendedDInSize(void) { return ZSTD_BLOCKSIZE_MAX + ZSTD_blockHeaderSize /* block header size*/ ; }
size_t ZBUFF_recommendedDOutSize(void) { return BLOCKSIZE; } size_t ZBUFF_recommendedDOutSize(void) { return ZSTD_BLOCKSIZE_MAX; }

View File

@ -31,11 +31,6 @@
#ifndef ZSTD_BUFFERED_H #ifndef ZSTD_BUFFERED_H
#define ZSTD_BUFFERED_H #define ZSTD_BUFFERED_H
/* The objects defined into this file should be considered experimental.
* They are not considered stable, as their prototype may change in the future.
* You can use them for tests, provide feedback, or if you can endure risk of future changes.
*/
#if defined (__cplusplus) #if defined (__cplusplus)
extern "C" { extern "C" {
#endif #endif
@ -75,7 +70,7 @@ ZSTDLIB_API size_t ZBUFF_compressFlush(ZBUFF_CCtx* cctx, void* dst, size_t* dstC
ZSTDLIB_API size_t ZBUFF_compressEnd(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr); ZSTDLIB_API size_t ZBUFF_compressEnd(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr);
/*-************************************************* /*-*************************************************
* Streaming compression * Streaming compression - howto
* *
* A ZBUFF_CCtx object is required to track streaming operation. * A ZBUFF_CCtx object is required to track streaming operation.
* Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources. * Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.
@ -89,12 +84,12 @@ ZSTDLIB_API size_t ZBUFF_compressEnd(ZBUFF_CCtx* cctx, void* dst, size_t* dstCap
* *srcSizePtr and *dstCapacityPtr can be any size. * *srcSizePtr and *dstCapacityPtr can be any size.
* The function will report how many bytes were read or written within *srcSizePtr and *dstCapacityPtr. * The function will report how many bytes were read or written within *srcSizePtr and *dstCapacityPtr.
* Note that it may not consume the entire input, in which case it's up to the caller to present again remaining data. * Note that it may not consume the entire input, in which case it's up to the caller to present again remaining data.
* The content of @dst will be overwritten (up to *dstCapacityPtr) at each call, so save its content if it matters or change @dst . * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each call, so save its content if it matters or change @dst .
* @return : a hint to preferred nb of bytes to use as input for next function call (it's just a hint, to improve latency) * @return : a hint to preferred nb of bytes to use as input for next function call (it's just a hint, to improve latency)
* or an error code, which can be tested using ZBUFF_isError(). * or an error code, which can be tested using ZBUFF_isError().
* *
* At any moment, it's possible to flush whatever data remains within buffer, using ZBUFF_compressFlush(). * At any moment, it's possible to flush whatever data remains within buffer, using ZBUFF_compressFlush().
* The nb of bytes written into @dst will be reported into *dstCapacityPtr. * The nb of bytes written into `dst` will be reported into *dstCapacityPtr.
* Note that the function cannot output more than *dstCapacityPtr, * Note that the function cannot output more than *dstCapacityPtr,
* therefore, some content might still be left into internal buffer if *dstCapacityPtr is too small. * therefore, some content might still be left into internal buffer if *dstCapacityPtr is too small.
* @return : nb of bytes still present into internal buffer (0 if it's empty) * @return : nb of bytes still present into internal buffer (0 if it's empty)
@ -127,26 +122,27 @@ ZSTDLIB_API size_t ZBUFF_decompressContinue(ZBUFF_DCtx* dctx,
const void* src, size_t* srcSizePtr); const void* src, size_t* srcSizePtr);
/*-*************************************************************************** /*-***************************************************************************
* Streaming decompression * Streaming decompression howto
* *
* A ZBUFF_DCtx object is required to track streaming operations. * A ZBUFF_DCtx object is required to track streaming operations.
* Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources. * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources.
* Use ZBUFF_decompressInit() to start a new decompression operation, * Use ZBUFF_decompressInit() to start a new decompression operation,
* or ZBUFF_decompressInitDictionary() if decompression requires a dictionary. * or ZBUFF_decompressInitDictionary() if decompression requires a dictionary.
* Note that ZBUFF_DCtx objects can be reused multiple times. * Note that ZBUFF_DCtx objects can be re-init multiple times.
* *
* Use ZBUFF_decompressContinue() repetitively to consume your input. * Use ZBUFF_decompressContinue() repetitively to consume your input.
* *srcSizePtr and *dstCapacityPtr can be any size. * *srcSizePtr and *dstCapacityPtr can be any size.
* The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr. * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
* Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again. * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
* The content of @dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change @dst. * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.
* @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency) * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
* or 0 when a frame is completely decoded * or 0 when a frame is completely decoded,
* or an error code, which can be tested using ZBUFF_isError(). * or an error code, which can be tested using ZBUFF_isError().
* *
* Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize() / ZBUFF_recommendedDOutSize() * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize() and ZBUFF_recommendedDOutSize()
* output : ZBUFF_recommendedDOutSize==128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded. * output : ZBUFF_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
* input : ZBUFF_recommendedDInSize==128Kb+3; just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 . * input : ZBUFF_recommendedDInSize == 128KB + 3;
* just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
* *******************************************************************************/ * *******************************************************************************/

View File

@ -51,7 +51,9 @@ extern "C" {
/* ************************************* /* *************************************
* Advanced Streaming functions * Advanced Streaming functions
***************************************/ ***************************************/
ZSTDLIB_API size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params); ZSTDLIB_API size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* cctx,
const void* dict, size_t dictSize,
ZSTD_parameters params, U64 pledgedSrcSize);
#if defined (__cplusplus) #if defined (__cplusplus)

View File

@ -284,8 +284,7 @@ static dictItem ZDICT_analyzePos(
return solution; return solution;
} }
{ { int i;
int i;
U32 searchLength; U32 searchLength;
U32 refinedStart = start; U32 refinedStart = start;
U32 refinedEnd = end; U32 refinedEnd = end;
@ -575,7 +574,6 @@ static void ZDICT_fillNoise(void* buffer, size_t length)
{ {
unsigned acc = PRIME1; unsigned acc = PRIME1;
size_t p=0;; size_t p=0;;
for (p=0; p<length; p++) { for (p=0; p<length; p++) {
acc *= PRIME2; acc *= PRIME2;
((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21); ((unsigned char*)buffer)[p] = (unsigned char)(acc >> 21);
@ -587,7 +585,7 @@ typedef struct
{ {
ZSTD_CCtx* ref; ZSTD_CCtx* ref;
ZSTD_CCtx* zc; ZSTD_CCtx* zc;
void* workPlace; /* must be BLOCKSIZE allocated */ void* workPlace; /* must be ZSTD_BLOCKSIZE_MAX allocated */
} EStats_ress_t; } EStats_ress_t;
@ -595,29 +593,57 @@ static void ZDICT_countEStats(EStats_ress_t esr,
U32* countLit, U32* offsetcodeCount, U32* matchlengthCount, U32* litlengthCount, U32* countLit, U32* offsetcodeCount, U32* matchlengthCount, U32* litlengthCount,
const void* src, size_t srcSize) const void* src, size_t srcSize)
{ {
const BYTE* bytePtr; const seqStore_t* seqStorePtr;
const U32* u32Ptr;
seqStore_t seqStore;
if (srcSize > BLOCKSIZE) srcSize = BLOCKSIZE; /* protection vs large samples */ if (srcSize > ZSTD_BLOCKSIZE_MAX) srcSize = ZSTD_BLOCKSIZE_MAX; /* protection vs large samples */
ZSTD_copyCCtx(esr.zc, esr.ref); ZSTD_copyCCtx(esr.zc, esr.ref);
ZSTD_compressBlock(esr.zc, esr.workPlace, BLOCKSIZE, src, srcSize); ZSTD_compressBlock(esr.zc, esr.workPlace, ZSTD_BLOCKSIZE_MAX, src, srcSize);
seqStore = ZSTD_copySeqStore(esr.zc); seqStorePtr = ZSTD_getSeqStore(esr.zc);
/* count stats */ /* literals stats */
for(bytePtr = seqStore.litStart; bytePtr < seqStore.lit; bytePtr++) { const BYTE* bytePtr;
countLit[*bytePtr]++; for(bytePtr = seqStorePtr->litStart; bytePtr < seqStorePtr->lit; bytePtr++)
for(u32Ptr = seqStore.offsetStart; u32Ptr < seqStore.offset; u32Ptr++) { countLit[*bytePtr]++;
BYTE offcode = (BYTE)ZSTD_highbit(*u32Ptr) + 1;
if (*u32Ptr==0) offcode=0;
offsetcodeCount[offcode]++;
} }
for(bytePtr = seqStore.matchLengthStart; bytePtr < seqStore.matchLength; bytePtr++)
matchlengthCount[*bytePtr]++; /* seqStats */
for(bytePtr = seqStore.litLengthStart; bytePtr < seqStore.litLength; bytePtr++) { size_t const nbSeq = (size_t)(seqStorePtr->offset - seqStorePtr->offsetStart);
litlengthCount[*bytePtr]++; ZSTD_seqToCodes(seqStorePtr, nbSeq);
{ const BYTE* codePtr = seqStorePtr->offCodeStart;
size_t u;
for (u=0; u<nbSeq; u++) offsetcodeCount[codePtr[u]]++;
}
{ const BYTE* codePtr = seqStorePtr->mlCodeStart;
size_t u;
for (u=0; u<nbSeq; u++) matchlengthCount[codePtr[u]]++;
}
{ const BYTE* codePtr = seqStorePtr->llCodeStart;
size_t u;
for (u=0; u<nbSeq; u++) litlengthCount[codePtr[u]]++;
} }
} }
/*
static size_t ZDICT_maxSampleSize(const size_t* fileSizes, unsigned nbFiles)
{
unsigned u;
size_t max=0;
for (u=0; u<nbFiles; u++)
if (max < fileSizes[u]) max = fileSizes[u];
return max;
}
*/
static size_t ZDICT_totalSampleSize(const size_t* fileSizes, unsigned nbFiles)
{
size_t total;
unsigned u;
for (u=0, total=0; u<nbFiles; u++) total += fileSizes[u];
return total;
}
#define OFFCODE_MAX 18 /* only applicable to first block */ #define OFFCODE_MAX 18 /* only applicable to first block */
static size_t ZDICT_analyzeEntropy(void* dstBuffer, size_t maxDstSize, static size_t ZDICT_analyzeEntropy(void* dstBuffer, size_t maxDstSize,
@ -626,41 +652,44 @@ static size_t ZDICT_analyzeEntropy(void* dstBuffer, size_t maxDstSize,
const void* dictBuffer, size_t dictBufferSize) const void* dictBuffer, size_t dictBufferSize)
{ {
U32 countLit[256]; U32 countLit[256];
U32 offcodeCount[MaxOff+1];
HUF_CREATE_STATIC_CTABLE(hufTable, 255); HUF_CREATE_STATIC_CTABLE(hufTable, 255);
short offcodeNCount[MaxOff+1]; U32 offcodeCount[OFFCODE_MAX+1];
short offcodeNCount[OFFCODE_MAX+1];
U32 matchLengthCount[MaxML+1]; U32 matchLengthCount[MaxML+1];
short matchLengthNCount[MaxML+1]; short matchLengthNCount[MaxML+1];
U32 litlengthCount[MaxLL+1]; U32 litLengthCount[MaxLL+1];
short litlengthNCount[MaxLL+1]; short litLengthNCount[MaxLL+1];
EStats_ress_t esr; EStats_ress_t esr;
ZSTD_parameters params; ZSTD_parameters params;
U32 u, huffLog = 12, Offlog = OffFSELog, mlLog = MLFSELog, llLog = LLFSELog, total; U32 u, huffLog = 12, Offlog = OffFSELog, mlLog = MLFSELog, llLog = LLFSELog, total;
size_t pos = 0, errorCode; size_t pos = 0, errorCode;
size_t eSize = 0; size_t eSize = 0;
size_t const totalSrcSize = ZDICT_totalSampleSize(fileSizes, nbFiles);
size_t const averageSampleSize = totalSrcSize / nbFiles;
/* init */ /* init */
for (u=0; u<256; u++) countLit[u]=1; /* any character must be described */ for (u=0; u<256; u++) countLit[u]=1; /* any character must be described */
for (u=0; u<=OFFCODE_MAX; u++) offcodeCount[u]=1; for (u=0; u<=OFFCODE_MAX; u++) offcodeCount[u]=1;
for (u=0; u<=MaxML; u++) matchLengthCount[u]=1; for (u=0; u<=MaxML; u++) matchLengthCount[u]=1;
for (u=0; u<=MaxLL; u++) litlengthCount[u]=1; for (u=0; u<=MaxLL; u++) litLengthCount[u]=1;
esr.ref = ZSTD_createCCtx(); esr.ref = ZSTD_createCCtx();
esr.zc = ZSTD_createCCtx(); esr.zc = ZSTD_createCCtx();
esr.workPlace = malloc(BLOCKSIZE); esr.workPlace = malloc(ZSTD_BLOCKSIZE_MAX);
if (!esr.ref || !esr.zc || !esr.workPlace) { if (!esr.ref || !esr.zc || !esr.workPlace) {
eSize = ERROR(memory_allocation); eSize = ERROR(memory_allocation);
DISPLAYLEVEL(1, "Not enough memory"); DISPLAYLEVEL(1, "Not enough memory");
goto _cleanup; goto _cleanup;
} }
if (compressionLevel==0) compressionLevel=g_compressionLevel_default; if (compressionLevel==0) compressionLevel=g_compressionLevel_default;
params = ZSTD_getParams(compressionLevel, dictBufferSize + 15 KB); params.cParams = ZSTD_getCParams(compressionLevel, averageSampleSize, dictBufferSize);
params.strategy = ZSTD_greedy; params.cParams.strategy = ZSTD_greedy;
ZSTD_compressBegin_advanced(esr.ref, dictBuffer, dictBufferSize, params); params.fParams.contentSizeFlag = 0;
ZSTD_compressBegin_advanced(esr.ref, dictBuffer, dictBufferSize, params, 0);
/* collect stats on all files */ /* collect stats on all files */
for (u=0; u<nbFiles; u++) { for (u=0; u<nbFiles; u++) {
ZDICT_countEStats(esr, ZDICT_countEStats(esr,
countLit, offcodeCount, matchLengthCount, litlengthCount, countLit, offcodeCount, matchLengthCount, litLengthCount,
(const char*)srcBuffer + pos, fileSizes[u]); (const char*)srcBuffer + pos, fileSizes[u]);
pos += fileSizes[u]; pos += fileSizes[u];
} }
@ -692,11 +721,11 @@ static size_t ZDICT_analyzeEntropy(void* dstBuffer, size_t maxDstSize,
} }
mlLog = (U32)errorCode; mlLog = (U32)errorCode;
total=0; for (u=0; u<=MaxLL; u++) total+=litlengthCount[u]; total=0; for (u=0; u<=MaxLL; u++) total+=litLengthCount[u];
errorCode = FSE_normalizeCount(litlengthNCount, llLog, litlengthCount, total, MaxLL); errorCode = FSE_normalizeCount(litLengthNCount, llLog, litLengthCount, total, MaxLL);
if (FSE_isError(errorCode)) { if (FSE_isError(errorCode)) {
eSize = ERROR(GENERIC); eSize = ERROR(GENERIC);
DISPLAYLEVEL(1, "FSE_normalizeCount error with litlengthCount"); DISPLAYLEVEL(1, "FSE_normalizeCount error with litLengthCount");
goto _cleanup; goto _cleanup;
} }
llLog = (U32)errorCode; llLog = (U32)errorCode;
@ -732,7 +761,7 @@ static size_t ZDICT_analyzeEntropy(void* dstBuffer, size_t maxDstSize,
maxDstSize -= errorCode; maxDstSize -= errorCode;
eSize += errorCode; eSize += errorCode;
errorCode = FSE_writeNCount(dstBuffer, maxDstSize, litlengthNCount, MaxLL, llLog); errorCode = FSE_writeNCount(dstBuffer, maxDstSize, litLengthNCount, MaxLL, llLog);
if (FSE_isError(errorCode)) { if (FSE_isError(errorCode)) {
eSize = ERROR(GENERIC); eSize = ERROR(GENERIC);
DISPLAYLEVEL(1, "FSE_writeNCount error with litlengthNCount"); DISPLAYLEVEL(1, "FSE_writeNCount error with litlengthNCount");
@ -791,25 +820,31 @@ static size_t ZDICT_fastSampling(void* dictBuffer, size_t dictSize,
} }
#define DIB_MINSAMPLESSIZE (DIB_FASTSEGMENTSIZE*3)
/*! ZDICT_trainFromBuffer_unsafe() :
* `samplesBuffer` must be followed by noisy guard band.
* @return : size of dictionary.
*/
size_t ZDICT_trainFromBuffer_unsafe( size_t ZDICT_trainFromBuffer_unsafe(
void* dictBuffer, size_t maxDictSize, void* dictBuffer, size_t maxDictSize,
const void* samplesBuffer, const size_t* sampleSizes, unsigned nbSamples, const void* samplesBuffer, const size_t* sampleSizes, unsigned nbSamples,
ZDICT_params_t params) ZDICT_params_t params)
{ {
const U32 dictListSize = MAX( MAX(DICTLISTSIZE, nbSamples), (U32)(maxDictSize/16)); U32 const dictListSize = MAX( MAX(DICTLISTSIZE, nbSamples), (U32)(maxDictSize/16));
dictItem* dictList = (dictItem*)malloc(dictListSize * sizeof(*dictList)); dictItem* dictList = (dictItem*)malloc(dictListSize * sizeof(*dictList));
unsigned selectivity = params.selectivityLevel; unsigned selectivity = params.selectivityLevel;
unsigned compressionLevel = params.compressionLevel; unsigned compressionLevel = params.compressionLevel;
size_t targetDictSize = maxDictSize - g_provision_entropySize; size_t targetDictSize = maxDictSize;
size_t sBuffSize; size_t sBuffSize;
size_t dictSize = 0; size_t dictSize = 0;
/* checks */ /* checks */
if (maxDictSize <= g_provision_entropySize + g_min_fast_dictContent) return ERROR(dstSize_tooSmall); if (maxDictSize <= g_provision_entropySize + g_min_fast_dictContent) return ERROR(dstSize_tooSmall);
if (!dictList) return ERROR(memory_allocation);
/* init */ /* init */
{ unsigned u; for (u=0, sBuffSize=0; u<nbSamples; u++) sBuffSize += sampleSizes[u]; } { unsigned u; for (u=0, sBuffSize=0; u<nbSamples; u++) sBuffSize += sampleSizes[u]; }
if (!dictList) return ERROR(memory_allocation); if (sBuffSize < DIB_MINSAMPLESSIZE) return 0; /* not enough source to create dictionary */
ZDICT_initDictItem(dictList); ZDICT_initDictItem(dictList);
g_displayLevel = params.notificationLevel; g_displayLevel = params.notificationLevel;
if (selectivity==0) selectivity = g_selectivity_default; if (selectivity==0) selectivity = g_selectivity_default;
@ -824,9 +859,9 @@ size_t ZDICT_trainFromBuffer_unsafe(
/* display best matches */ /* display best matches */
if (g_displayLevel>= 3) { if (g_displayLevel>= 3) {
const U32 nb = 25; U32 const nb = 25;
U32 const dictContentSize = ZDICT_dictSize(dictList);
U32 u; U32 u;
U32 dictContentSize = ZDICT_dictSize(dictList);
DISPLAYLEVEL(3, "\n %u segments found, of total size %u \n", dictList[0].pos, dictContentSize); DISPLAYLEVEL(3, "\n %u segments found, of total size %u \n", dictList[0].pos, dictContentSize);
DISPLAYLEVEL(3, "list %u best segments \n", nb); DISPLAYLEVEL(3, "list %u best segments \n", nb);
for (u=1; u<=nb; u++) { for (u=1; u<=nb; u++) {
@ -840,8 +875,7 @@ size_t ZDICT_trainFromBuffer_unsafe(
} } } } } }
/* create dictionary */ /* create dictionary */
{ { U32 dictContentSize = ZDICT_dictSize(dictList);
U32 dictContentSize = ZDICT_dictSize(dictList);
size_t hSize; size_t hSize;
BYTE* ptr; BYTE* ptr;
U32 u; U32 u;
@ -859,8 +893,8 @@ size_t ZDICT_trainFromBuffer_unsafe(
if (selectivity==1) { /* note could also be used to complete a dictionary, but not necessarily better */ if (selectivity==1) { /* note could also be used to complete a dictionary, but not necessarily better */
DISPLAYLEVEL(3, "\r%70s\r", ""); /* clean display line */ DISPLAYLEVEL(3, "\r%70s\r", ""); /* clean display line */
DISPLAYLEVEL(3, "Adding %u KB with fast sampling \n", (U32)(targetDictSize>>10)); DISPLAYLEVEL(3, "Adding %u KB with fast sampling \n", (U32)(targetDictSize>>10));
dictContentSize = (U32)ZDICT_fastSampling((char*)dictBuffer + g_provision_entropySize, dictContentSize = (U32)ZDICT_fastSampling(dictBuffer, targetDictSize,
targetDictSize, samplesBuffer, sBuffSize); samplesBuffer, sBuffSize);
} }
/* dictionary header */ /* dictionary header */
@ -886,31 +920,32 @@ size_t ZDICT_trainFromBuffer_unsafe(
} }
/* issue : samplesBuffer need to be followed by a noisy guard band.
* work around : duplicate the buffer, and add the noise */
size_t ZDICT_trainFromBuffer_advanced(void* dictBuffer, size_t dictBufferCapacity, size_t ZDICT_trainFromBuffer_advanced(void* dictBuffer, size_t dictBufferCapacity,
const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
ZDICT_params_t params) ZDICT_params_t params)
{ {
size_t sBuffSize;
void* newBuff; void* newBuff;
size_t result; size_t sBuffSize;
{ unsigned u; for (u=0, sBuffSize=0; u<nbSamples; u++) sBuffSize += samplesSizes[u]; } { unsigned u; for (u=0, sBuffSize=0; u<nbSamples; u++) sBuffSize += samplesSizes[u]; }
if (sBuffSize==0) return 0; /* empty content => no dictionary */
newBuff = malloc(sBuffSize + NOISELENGTH); newBuff = malloc(sBuffSize + NOISELENGTH);
if (!newBuff) return ERROR(memory_allocation); if (!newBuff) return ERROR(memory_allocation);
memcpy(newBuff, samplesBuffer, sBuffSize); memcpy(newBuff, samplesBuffer, sBuffSize);
ZDICT_fillNoise((char*)newBuff + sBuffSize, NOISELENGTH); /* guard band, for end of buffer condition */ ZDICT_fillNoise((char*)newBuff + sBuffSize, NOISELENGTH); /* guard band, for end of buffer condition */
result = ZDICT_trainFromBuffer_unsafe(dictBuffer, dictBufferCapacity, { size_t const result = ZDICT_trainFromBuffer_unsafe(
dictBuffer, dictBufferCapacity,
newBuff, samplesSizes, nbSamples, newBuff, samplesSizes, nbSamples,
params); params);
free(newBuff); free(newBuff);
return result; return result; }
} }
/* issue : samplesBuffer need to be followed by a noisy guard band.
* work around : duplicate the buffer, and add the noise ? */
size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity, size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity,
const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples) const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples)
{ {

View File

@ -60,8 +60,8 @@ extern "C" {
* Version * Version
***************************************/ ***************************************/
#define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */ #define ZSTD_VERSION_MAJOR 0 /* for breaking interface changes */
#define ZSTD_VERSION_MINOR 5 /* for new (non-breaking) interface capabilities */ #define ZSTD_VERSION_MINOR 6 /* for new (non-breaking) interface capabilities */
#define ZSTD_VERSION_RELEASE 1 /* for tweaks, bug-fixes, or development */ #define ZSTD_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */
#define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
ZSTDLIB_API unsigned ZSTD_versionNumber (void); ZSTDLIB_API unsigned ZSTD_versionNumber (void);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,7 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at : You can contact the author at :
- zstd source repository : https://github.com/Cyan4973/zstd - zstd homepage : https://www.zstd.net
*/ */
#ifndef ZSTD_CCOMMON_H_MODULE #ifndef ZSTD_CCOMMON_H_MODULE
#define ZSTD_CCOMMON_H_MODULE #define ZSTD_CCOMMON_H_MODULE
@ -50,18 +50,29 @@
/*-************************************* /*-*************************************
* Common constants * Common constants
***************************************/ ***************************************/
#define ZSTD_DICT_MAGIC 0xEC30A435 #define ZSTD_OPT_DEBUG 0 // 3 = compression stats; 5 = check encoded sequences; 9 = full logs
#include <stdio.h>
#if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9
#define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__)
#define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__)
#define ZSTD_LOG_BLOCK(...) printf(__VA_ARGS__)
#else
#define ZSTD_LOG_PARSER(...)
#define ZSTD_LOG_ENCODE(...)
#define ZSTD_LOG_BLOCK(...)
#endif
#define ZSTD_OPT_NUM (1<<12)
#define ZSTD_DICT_MAGIC 0xEC30A436
#define ZSTD_REP_NUM 3
#define ZSTD_REP_INIT ZSTD_REP_NUM
#define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
#define KB *(1 <<10) #define KB *(1 <<10)
#define MB *(1 <<20) #define MB *(1 <<20)
#define GB *(1U<<30) #define GB *(1U<<30)
#define BLOCKSIZE (128 KB) /* define, for static allocation */
static const size_t ZSTD_blockHeaderSize = 3;
static const size_t ZSTD_frameHeaderSize_min = 5;
#define ZSTD_frameHeaderSize_max 5 /* define, for static allocation */
#define BIT7 128 #define BIT7 128
#define BIT6 64 #define BIT6 64
#define BIT5 32 #define BIT5 32
@ -69,52 +80,76 @@ static const size_t ZSTD_frameHeaderSize_min = 5;
#define BIT1 2 #define BIT1 2
#define BIT0 1 #define BIT0 1
#define ZSTD_WINDOWLOG_ABSOLUTEMIN 12
static const size_t ZSTD_fcs_fieldSize[4] = { 0, 1, 2, 8 };
#define ZSTD_BLOCKHEADERSIZE 3 /* because C standard does not allow a static const value to be defined using another static const value .... :( */
static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
#define HufLog 12
#define IS_HUF 0 #define IS_HUF 0
#define IS_PCH 1 #define IS_PCH 1
#define IS_RAW 2 #define IS_RAW 2
#define IS_RLE 3 #define IS_RLE 3
#define MINMATCH 4 #define LONGNBSEQ 0x7F00
#define MINMATCH 3
#define EQUAL_READ32 4
#define REPCODE_STARTVALUE 1 #define REPCODE_STARTVALUE 1
#define Litbits 8 #define Litbits 8
#define MLbits 7
#define LLbits 6
#define Offbits 5
#define MaxLit ((1<<Litbits) - 1) #define MaxLit ((1<<Litbits) - 1)
#define MaxML ((1<<MLbits) - 1) #define MaxML 52
#define MaxLL ((1<<LLbits) - 1) #define MaxLL 35
#define MaxOff ((1<<Offbits)- 1) #define MaxOff 28
#define MLFSELog 10 #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
#define LLFSELog 10 #define MLFSELog 9
#define OffFSELog 9 #define LLFSELog 9
#define MaxSeq MAX(MaxLL, MaxML) #define OffFSELog 8
#define FSE_ENCODING_RAW 0 #define FSE_ENCODING_RAW 0
#define FSE_ENCODING_RLE 1 #define FSE_ENCODING_RLE 1
#define FSE_ENCODING_STATIC 2 #define FSE_ENCODING_STATIC 2
#define FSE_ENCODING_DYNAMIC 3 #define FSE_ENCODING_DYNAMIC 3
static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
13,14,15,16 };
static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
-1,-1,-1,-1 };
static const U32 LL_defaultNormLog = 6;
#define HufLog 12 static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
12,13,14,15,16 };
static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
-1,-1,-1,-1,-1 };
static const U32 ML_defaultNormLog = 6;
#define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */ static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
#define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */ 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 };
static const U32 OF_defaultNormLog = 5;
#define WILDCOPY_OVERLENGTH 8
typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
/*-******************************************* /*-*******************************************
* Shared functions to include for inlining * Shared functions to include for inlining
*********************************************/ *********************************************/
static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); } static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
#define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; } #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
/*! ZSTD_wildcopy() : /*! ZSTD_wildcopy() :
* custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */ * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
#define WILDCOPY_OVERLENGTH 8
MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length) MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length)
{ {
const BYTE* ip = (const BYTE*)src; const BYTE* ip = (const BYTE*)src;
@ -151,32 +186,68 @@ MEM_STATIC unsigned ZSTD_highbit(U32 val)
/*-******************************************* /*-*******************************************
* Private interfaces * Private interfaces
*********************************************/ *********************************************/
typedef struct {
U32 off;
U32 len;
} ZSTD_match_t;
typedef struct {
U32 price;
U32 off;
U32 mlen;
U32 litlen;
U32 rep[ZSTD_REP_INIT];
} ZSTD_optimal_t;
#if ZSTD_OPT_DEBUG == 3
#include ".debug/zstd_stats.h"
#else
typedef struct { U32 unused; } ZSTD_stats_t;
MEM_STATIC void ZSTD_statsPrint(ZSTD_stats_t* stats, U32 searchLength) { (void)stats; (void)searchLength; };
MEM_STATIC void ZSTD_statsInit(ZSTD_stats_t* stats) { (void)stats; };
MEM_STATIC void ZSTD_statsResetFreqs(ZSTD_stats_t* stats) { (void)stats; };
MEM_STATIC void ZSTD_statsUpdatePrices(ZSTD_stats_t* stats, size_t litLength, const BYTE* literals, size_t offset, size_t matchLength) { (void)stats; (void)litLength; (void)literals; (void)offset; (void)matchLength; };
#endif
typedef struct { typedef struct {
void* buffer; void* buffer;
U32* offsetStart; U32* offsetStart;
U32* offset; U32* offset;
BYTE* offCodeStart; BYTE* offCodeStart;
BYTE* offCode;
BYTE* litStart; BYTE* litStart;
BYTE* lit; BYTE* lit;
BYTE* litLengthStart; U16* litLengthStart;
BYTE* litLength; U16* litLength;
BYTE* matchLengthStart; BYTE* llCodeStart;
BYTE* matchLength; U16* matchLengthStart;
BYTE* dumpsStart; U16* matchLength;
BYTE* dumps; BYTE* mlCodeStart;
U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
U32 longLengthPos;
/* opt */ /* opt */
ZSTD_optimal_t* priceTable;
ZSTD_match_t* matchTable;
U32* matchLengthFreq; U32* matchLengthFreq;
U32* litLengthFreq; U32* litLengthFreq;
U32* litFreq; U32* litFreq;
U32* offCodeFreq; U32* offCodeFreq;
U32 matchLengthSum; U32 matchLengthSum;
U32 matchSum;
U32 litLengthSum; U32 litLengthSum;
U32 litSum; U32 litSum;
U32 offCodeSum; U32 offCodeSum;
U32 log2matchLengthSum;
U32 log2matchSum;
U32 log2litLengthSum;
U32 log2litSum;
U32 log2offCodeSum;
U32 factor;
ZSTD_stats_t stats;
} seqStore_t; } seqStore_t;
seqStore_t ZSTD_copySeqStore(const ZSTD_CCtx* ctx); const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);
void ZSTD_seqToCodes(const seqStore_t* seqStorePtr, size_t const nbSeq);
size_t ZSTD_compressBegin_targetSrcSize(ZSTD_CCtx* zc, const void* dict, size_t dictSize, size_t targetSrcSize, int compressionLevel);
#endif /* ZSTD_CCOMMON_H_MODULE */ #endif /* ZSTD_CCOMMON_H_MODULE */

File diff suppressed because it is too large Load Diff

View File

@ -51,39 +51,47 @@ extern "C" {
/*-************************************* /*-*************************************
* Constants * Constants
***************************************/ ***************************************/
#define ZSTD_MAGICNUMBER 0xFD2FB525 /* v0.5 */ #define ZSTD_MAGICNUMBER 0xFD2FB526 /* v0.6 */
/*-************************************* /*-*************************************
* Types * Types
***************************************/ ***************************************/
#define ZSTD_WINDOWLOG_MAX 26 #define ZSTD_WINDOWLOG_MAX ((U32)(MEM_32bits() ? 25 : 27))
#define ZSTD_WINDOWLOG_MIN 18 #define ZSTD_WINDOWLOG_MIN 18
#define ZSTD_WINDOWLOG_ABSOLUTEMIN 11 #define ZSTD_CHAINLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
#define ZSTD_CONTENTLOG_MAX (ZSTD_WINDOWLOG_MAX+1) #define ZSTD_CHAINLOG_MIN 4
#define ZSTD_CONTENTLOG_MIN 4 #define ZSTD_HASHLOG_MAX ZSTD_WINDOWLOG_MAX
#define ZSTD_HASHLOG_MAX 28 #define ZSTD_HASHLOG_MIN 12
#define ZSTD_HASHLOG_MIN 12 #define ZSTD_HASHLOG3_MAX 17
#define ZSTD_SEARCHLOG_MAX (ZSTD_CONTENTLOG_MAX-1) #define ZSTD_HASHLOG3_MIN 15
#define ZSTD_SEARCHLOG_MIN 1 #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
#define ZSTD_SEARCHLENGTH_MAX 7 #define ZSTD_SEARCHLOG_MIN 1
#define ZSTD_SEARCHLENGTH_MIN 4 #define ZSTD_SEARCHLENGTH_MAX 7
#define ZSTD_TARGETLENGTH_MIN 4 #define ZSTD_SEARCHLENGTH_MIN 3
#define ZSTD_TARGETLENGTH_MIN 4
#define ZSTD_TARGETLENGTH_MAX 999 #define ZSTD_TARGETLENGTH_MAX 999
/* from faster to stronger */ /* from faster to stronger */
typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_opt, ZSTD_btopt } ZSTD_strategy; typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_btopt } ZSTD_strategy;
typedef struct typedef struct {
{
U64 srcSize; /* optional : tells how much bytes are present in the frame. Use 0 if not known. */
U32 windowLog; /* largest match distance : larger == more compression, more memory needed during decompression */ U32 windowLog; /* largest match distance : larger == more compression, more memory needed during decompression */
U32 contentLog; /* full search segment : larger == more compression, slower, more memory (useless for fast) */ U32 chainLog; /* fully searched segment : larger == more compression, slower, more memory (useless for fast) */
U32 hashLog; /* dispatch table : larger == faster, more memory */ U32 hashLog; /* dispatch table : larger == faster, more memory */
U32 searchLog; /* nb of searches : larger == more compression, slower */ U32 searchLog; /* nb of searches : larger == more compression, slower */
U32 searchLength; /* match length searched : larger == faster decompression, sometimes less compression */ U32 searchLength; /* match length searched : larger == faster decompression, sometimes less compression */
U32 targetLength; /* acceptable match size for optimal parser (only) : larger == more compression, slower */ U32 targetLength; /* acceptable match size for optimal parser (only) : larger == more compression, slower */
ZSTD_strategy strategy; ZSTD_strategy strategy;
} ZSTD_compressionParameters;
typedef struct {
U32 contentSizeFlag; /* 1: content size will be in frame header (if known). */
} ZSTD_frameParameters;
typedef struct {
ZSTD_compressionParameters cParams;
ZSTD_frameParameters fParams;
} ZSTD_parameters; } ZSTD_parameters;
@ -92,14 +100,19 @@ typedef struct
***************************************/ ***************************************/
ZSTDLIB_API unsigned ZSTD_maxCLevel (void); ZSTDLIB_API unsigned ZSTD_maxCLevel (void);
/*! ZSTD_getParams() : /*! ZSTD_getCParams() :
* @return ZSTD_parameters structure for a selected compression level and srcSize. * @return ZSTD_compressionParameters structure for a selected compression level and srcSize.
* `srcSizeHint` value is optional, select 0 if not known */ * `srcSize` value is optional, select 0 if not known */
ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSizeHint); ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, U64 srcSize, size_t dictSize);
/*! ZSTD_validateParams() : /*! ZSTD_checkParams() :
* correct params value to remain within authorized range */ * Ensure param values remain within authorized range */
ZSTDLIB_API void ZSTD_validateParams(ZSTD_parameters* params); ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
/*! ZSTD_adjustParams() :
* optimize params for a given `srcSize` and `dictSize`.
* both values are optional, select `0` if unknown. */
ZSTDLIB_API void ZSTD_adjustCParams(ZSTD_compressionParameters* params, U64 srcSize, size_t dictSize);
/*! ZSTD_compress_advanced() : /*! ZSTD_compress_advanced() :
* Same as ZSTD_compress_usingDict(), with fine-tune control of each compression parameter */ * Same as ZSTD_compress_usingDict(), with fine-tune control of each compression parameter */
@ -113,7 +126,7 @@ ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx,
* Same as ZSTD_compress_usingDict, but using a reference context `preparedCCtx`, where dictionary has been loaded. * Same as ZSTD_compress_usingDict, but using a reference context `preparedCCtx`, where dictionary has been loaded.
* It avoids reloading the dictionary each time. * It avoids reloading the dictionary each time.
* `preparedCCtx` must have been properly initialized using ZSTD_compressBegin_usingDict() or ZSTD_compressBegin_advanced(). * `preparedCCtx` must have been properly initialized using ZSTD_compressBegin_usingDict() or ZSTD_compressBegin_advanced().
* Requires 2 contexts : 1 for reference, which will not be modified, and 1 to run the compression operation */ * Requires 2 contexts : 1 for reference (preparedCCtx) which will not be modified, and 1 to run the compression operation (cctx) */
ZSTDLIB_API size_t ZSTD_compress_usingPreparedCCtx( ZSTDLIB_API size_t ZSTD_compress_usingPreparedCCtx(
ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx,
void* dst, size_t dstCapacity, void* dst, size_t dstCapacity,
@ -125,19 +138,19 @@ ZSTDLIB_API size_t ZSTD_compress_usingPreparedCCtx(
* Same as ZSTD_decompress_usingDict, but using a reference context `preparedDCtx`, where dictionary has been loaded. * Same as ZSTD_decompress_usingDict, but using a reference context `preparedDCtx`, where dictionary has been loaded.
* It avoids reloading the dictionary each time. * It avoids reloading the dictionary each time.
* `preparedDCtx` must have been properly initialized using ZSTD_decompressBegin_usingDict(). * `preparedDCtx` must have been properly initialized using ZSTD_decompressBegin_usingDict().
* Requires 2 contexts : 1 for reference, which will not be modified, and 1 to run the decompression operation */ * Requires 2 contexts : 1 for reference (preparedDCtx), which will not be modified, and 1 to run the decompression operation (dctx) */
ZSTDLIB_API size_t ZSTD_decompress_usingPreparedDCtx( ZSTDLIB_API size_t ZSTD_decompress_usingPreparedDCtx(
ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx, ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx,
void* dst, size_t dstCapacity, void* dst, size_t dstCapacity,
const void* src, size_t srcSize); const void* src, size_t srcSize);
/* ************************************** /* **************************************
* Streaming functions (direct mode) * Streaming functions (direct mode)
****************************************/ ****************************************/
ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict,size_t dictSize, int compressionLevel); ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict,size_t dictSize, ZSTD_parameters params); ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, U64 pledgedSrcSize);
ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx); ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx);
ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
@ -153,7 +166,7 @@ ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapaci
Start by initializing a context. Start by initializing a context.
Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
or ZSTD_compressBegin_advanced(), for finer parameter control. or ZSTD_compressBegin_advanced(), for finer parameter control.
It's also possible to duplicate a reference context which has been initialized, using ZSTD_copyCCtx() It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
Then, consume your input using ZSTD_compressContinue(). Then, consume your input using ZSTD_compressContinue().
The interface is synchronous, so all input will be consumed and produce a compressed output. The interface is synchronous, so all input will be consumed and produce a compressed output.
@ -166,13 +179,17 @@ ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapaci
You can then reuse ZSTD_CCtx to compress some new frame. You can then reuse ZSTD_CCtx to compress some new frame.
*/ */
typedef struct { U64 frameContentSize; U32 windowLog; } ZSTD_frameParams;
#define ZSTD_FRAMEHEADERSIZE_MAX 13 /* for static allocation */
static const size_t ZSTD_frameHeaderSize_min = 5;
static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX;
ZSTDLIB_API size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */
ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx); ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx); ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
ZSTDLIB_API size_t ZSTD_getFrameParams(ZSTD_parameters* params, const void* src, size_t srcSize);
ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
@ -183,15 +200,19 @@ ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t ds
Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
A ZSTD_DCtx object can be re-used multiple times. A ZSTD_DCtx object can be re-used multiple times.
First typical operation is to retrieve frame parameters, using ZSTD_getFrameParams(). First optional operation is to retrieve frame parameters, using ZSTD_getFrameParams(), which doesn't consume the input.
This operation is independent, and just needs enough input data to properly decode the frame header. It can provide the minimum size of rolling buffer required to properly decompress data,
Objective is to retrieve *params.windowlog, to know minimum amount of memory required during decoding. and optionally the final size of uncompressed content.
Result : 0 when successful, it means the ZSTD_parameters structure has been filled. (Note : content size is an optional info that may not be present. 0 means : content size unknown)
>0 : means there is not enough data into src. Provides the expected size to successfully decode header. Frame parameters are extracted from the beginning of compressed frame.
The amount of data to read is variable, from ZSTD_frameHeaderSize_min to ZSTD_frameHeaderSize_max (so if `srcSize` >= ZSTD_frameHeaderSize_max, it will always work)
If `srcSize` is too small for operation to succeed, function will return the minimum size it requires to produce a result.
Result : 0 when successful, it means the ZSTD_frameParams structure has been filled.
>0 : means there is not enough data into `src`. Provides the expected size to successfully decode header.
errorCode, which can be tested using ZSTD_isError() errorCode, which can be tested using ZSTD_isError()
Start decompression, with ZSTD_decompressBegin() or ZSTD_decompressBegin_usingDict() Start decompression, with ZSTD_decompressBegin() or ZSTD_decompressBegin_usingDict().
Alternatively, you can copy a prepared context, using ZSTD_copyDCtx() Alternatively, you can copy a prepared context, using ZSTD_copyDCtx().
Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue(). ZSTD_nextSrcSizeToDecompress() tells how much bytes to provide as 'srcSize' to ZSTD_decompressContinue().
@ -199,7 +220,7 @@ ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t ds
ZSTD_decompressContinue() needs previous data blocks during decompression, up to (1 << windowlog). ZSTD_decompressContinue() needs previous data blocks during decompression, up to (1 << windowlog).
They should preferably be located contiguously, prior to current block. Alternatively, a round buffer is also possible. They should preferably be located contiguously, prior to current block. Alternatively, a round buffer is also possible.
@result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst'. @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity)
It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header. It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some header.
A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
@ -211,10 +232,10 @@ ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t ds
* Block functions * Block functions
****************************************/ ****************************************/
/*! Block functions produce and decode raw zstd blocks, without frame metadata. /*! Block functions produce and decode raw zstd blocks, without frame metadata.
User will have to take in charge required information to regenerate data, such as block sizes. User will have to take in charge required information to regenerate data, such as compressed and content sizes.
A few rules to respect : A few rules to respect :
- Uncompressed block size must be <= 128 KB - Uncompressed block size must be <= ZSTD_BLOCKSIZE_MAX (128 KB)
- Compressing or decompressing requires a context structure - Compressing or decompressing requires a context structure
+ Use ZSTD_createCCtx() and ZSTD_createDCtx() + Use ZSTD_createCCtx() and ZSTD_createDCtx()
- It is necessary to init context before starting - It is necessary to init context before starting
@ -228,6 +249,7 @@ ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t ds
+ ZSTD_decompressBlock() doesn't accept uncompressed data as input !! + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!
*/ */
#define ZSTD_BLOCKSIZE_MAX (128 * 1024) /* define, for static allocation */
size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
@ -237,7 +259,7 @@ size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, cons
***************************************/ ***************************************/
#include "error_public.h" #include "error_public.h"
/*! ZSTD_getErrorCode() : /*! ZSTD_getErrorCode() :
convert a `size_t` function result into a `ZSTD_error_code` enum type, convert a `size_t` function result into a `ZSTD_ErrorCode` enum type,
which can be used to compare directly with enum list published into "error_public.h" */ which can be used to compare directly with enum list published into "error_public.h" */
ZSTD_ErrorCode ZSTD_getError(size_t code); ZSTD_ErrorCode ZSTD_getError(size_t code);

15
programs/.gitignore vendored
View File

@ -5,7 +5,10 @@ fullbench
fullbench32 fullbench32
fuzzer fuzzer
fuzzer32 fuzzer32
zbufftest
zbufftest32
datagen datagen
paramgrill
# Object files # Object files
*.o *.o
@ -29,3 +32,15 @@ datagen
# Visual solution files # Visual solution files
*.suo *.suo
*.user *.user
# Default result files
dictionary
grillResults.txt
_*
# fuzzer
afl
# Misc files
*.bat
fileTests.sh

View File

@ -53,7 +53,7 @@ BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/share/man/man1 MANDIR = $(PREFIX)/share/man/man1
ZSTDDIR = ../lib ZSTDDIR = ../lib
ZSTD_FILES := $(ZSTDDIR)/huff0.c $(ZSTDDIR)/fse.c $(ZSTDDIR)/zstd_compress.c $(ZSTDDIR)/zstd_decompress.c ZSTD_FILES := $(ZSTDDIR)/huff0.c $(ZSTDDIR)/fse.c $(ZSTDDIR)/zstd_decompress.c $(ZSTDDIR)/zstd_compress.c
ifeq ($(ZSTD_LEGACY_SUPPORT), 0) ifeq ($(ZSTD_LEGACY_SUPPORT), 0)
CPPFLAGS += -DZSTD_LEGACY_SUPPORT=0 CPPFLAGS += -DZSTD_LEGACY_SUPPORT=0
@ -61,7 +61,8 @@ ZSTD_FILES_LEGACY:=
else else
ZSTD_LEGACY_SUPPORT:=1 ZSTD_LEGACY_SUPPORT:=1
CPPFLAGS += -I../lib/legacy -I./legacy CPPFLAGS += -I../lib/legacy -I./legacy
ZSTD_FILES_LEGACY:= $(ZSTDDIR)/legacy/zstd_v01.c $(ZSTDDIR)/legacy/zstd_v02.c $(ZSTDDIR)/legacy/zstd_v03.c $(ZSTDDIR)/legacy/zstd_v04.c legacy/fileio_legacy.c ZSTD_FILES_LEGACY:= $(ZSTDDIR)/legacy/zstd_v01.c $(ZSTDDIR)/legacy/zstd_v02.c $(ZSTDDIR)/legacy/zstd_v03.c \
$(ZSTDDIR)/legacy/zstd_v04.c $(ZSTDDIR)/legacy/zstd_v05.c legacy/fileio_legacy.c
endif endif
@ -112,11 +113,11 @@ zstd-frugal: $(ZSTD_FILES) $(ZSTDDIR)/zbuff.c zstdcli.c fileio.c
zstd-small: clean zstd-small: clean
CFLAGS="-Os -s" $(MAKE) zstd-frugal CFLAGS="-Os -s" $(MAKE) zstd-frugal
fullbench : $(ZSTD_FILES) \ fullbench : $(ZSTD_FILES) $(ZSTDDIR)/zbuff.c \
datagen.c fullbench.c datagen.c fullbench.c
$(CC) $(FLAGS) $^ -o $@$(EXT) $(CC) $(FLAGS) $^ -o $@$(EXT)
fullbench32: $(ZSTD_FILES) \ fullbench32: $(ZSTD_FILES) $(ZSTDDIR)/zbuff.c \
datagen.c fullbench.c datagen.c fullbench.c
$(CC) -m32 $(FLAGS) $^ -o $@$(EXT) $(CC) -m32 $(FLAGS) $^ -o $@$(EXT)
@ -215,19 +216,19 @@ test-zbuff: zbufftest
test-zbuff32: zbufftest32 test-zbuff32: zbufftest32
./zbufftest32 $(ZBUFFTEST) ./zbufftest32 $(ZBUFFTEST)
valgrindTest: VALGRIND = valgrind --leak-check=full --error-exitcode=1
valgrindTest: zstd datagen fuzzer fullbench zbufftest valgrindTest: zstd datagen fuzzer fullbench zbufftest
@echo "\n ---- valgrind tests : memory analyzer ----" @echo "\n ---- valgrind tests : memory analyzer ----"
valgrind --leak-check=yes --error-exitcode=1 ./datagen -g50M > $(VOID) $(VALGRIND) ./datagen -g50M > $(VOID)
./datagen -g16KB > tmp $(VALGRIND) ./zstd ; if [ $$? -eq 0 ] ; then echo "zstd without argument should have failed"; false; fi
valgrind --leak-check=yes --error-exitcode=1 ./zstd -vf tmp -o $(VOID) ./datagen -g80 | $(VALGRIND) ./zstd - -c > $(VOID)
./datagen -g2930KB > tmp ./datagen -g16KB | $(VALGRIND) ./zstd -vf - -o $(VOID)
valgrind --leak-check=yes --error-exitcode=1 ./zstd -5 -vf tmp -o tmp2 ./datagen -g2930KB | $(VALGRIND) ./zstd -5 -vf - -o tmp
valgrind --leak-check=yes --error-exitcode=1 ./zstd -vdf tmp2 -o $(VOID) $(VALGRIND) ./zstd -vdf tmp -o $(VOID)
./datagen -g64MB > tmp ./datagen -g64MB | $(VALGRIND) ./zstd -vf - -o $(VOID)
valgrind --leak-check=yes --error-exitcode=1 ./zstd -vf tmp -o $(VOID)
@rm tmp @rm tmp
valgrind --leak-check=yes --error-exitcode=1 ./fuzzer -T1mn -t1 $(VALGRIND) ./fuzzer -T1mn -t1
valgrind --leak-check=yes --error-exitcode=1 ./fullbench -i1 $(VALGRIND) ./fullbench -i1
valgrind --leak-check=yes --error-exitcode=1 ./zbufftest -T1mn $(VALGRIND) ./zbufftest -T1mn
endif endif

View File

@ -1,6 +1,6 @@
/* /*
bench.c - Demo module to benchmark open-source compression algorithms bench.c - open-source compression benchmark module
Copyright (C) Yann Collet 2012-2015 Copyright (C) Yann Collet 2012-2016
GPL v2 License GPL v2 License
@ -19,8 +19,8 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at : You can contact the author at :
- zstd homepage : http://www.zstd.net
- zstd source repository : https://github.com/Cyan4973/zstd - zstd source repository : https://github.com/Cyan4973/zstd
- ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
*/ */
/* ************************************** /* **************************************
@ -40,32 +40,61 @@
# define _LARGEFILE64_SOURCE # define _LARGEFILE64_SOURCE
#endif #endif
/* S_ISREG & gettimeofday() are not supported by MSVC */
#if defined(_MSC_VER) || defined(_WIN32)
# define BMK_LEGACY_TIMER 1
#endif
/* ************************************* /* *************************************
* Includes * Includes
***************************************/ ***************************************/
#define _POSIX_C_SOURCE 199309L /* before <time.h> - needed for nanosleep() */
#include <stdlib.h> /* malloc, free */ #include <stdlib.h> /* malloc, free */
#include <string.h> /* memset */ #include <string.h> /* memset */
#include <stdio.h> /* fprintf, fopen, ftello64 */ #include <stdio.h> /* fprintf, fopen, ftello64 */
#include <sys/types.h> /* stat64 */ #include <sys/types.h> /* stat64 */
#include <sys/stat.h> /* stat64 */ #include <sys/stat.h> /* stat64 */
#include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
/* Use ftime() if gettimeofday() is not available */ /* sleep : posix - windows - others */
#if defined(BMK_LEGACY_TIMER) #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
# include <sys/timeb.h> /* timeb, ftime */ # include <unistd.h>
# include <sys/resource.h> /* setpriority */
# define BMK_sleep(s) sleep(s)
# define mili_sleep(mili) { struct timespec t; t.tv_sec=0; t.tv_nsec=mili*1000000ULL; nanosleep(&t, NULL); }
# define SET_HIGH_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
#elif defined(_WIN32)
# include <windows.h>
# define BMK_sleep(s) Sleep(1000*s)
# define mili_sleep(mili) Sleep(mili)
# define SET_HIGH_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
#else #else
# include <sys/time.h> /* gettimeofday */ # define BMK_sleep(s) /* disabled */
# define mili_sleep(mili) /* disabled */
# define SET_HIGH_PRIORITY /* disabled */
#endif
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
typedef clock_t BMK_time_t;
# define BMK_initTimer(ticksPerSecond) ticksPerSecond=0
# define BMK_getTime(x) x = clock()
# define BMK_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd) (1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC)
# define BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) (1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC)
#elif defined(_WIN32)
typedef LARGE_INTEGER BMK_time_t;
# define BMK_initTimer(x) if (!QueryPerformanceFrequency(&x)) { fprintf(stderr, "ERROR: QueryPerformance not present\n"); }
# define BMK_getTime(x) QueryPerformanceCounter(&x)
# define BMK_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd) (1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart)
# define BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) (1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart)
#else
typedef int BMK_time_t;
# define BMK_initTimer(ticksPerSecond) ticksPerSecond=0
# define BMK_getTimeMicro(clockStart) clockStart=1
# define BMK_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd) (TIMELOOP_S*1000000ULL+clockEnd-clockStart)
# define BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) (TIMELOOP_S*1000000000ULL+clockEnd-clockStart)
#endif #endif
#include "mem.h" #include "mem.h"
#include "zstd_static.h" #include "zstd_static.h"
#include "zstd_internal.h" /* ZSTD_compressBegin_targetSrcSize */
#include "datagen.h" /* RDG_genBuffer */
#include "xxhash.h" #include "xxhash.h"
#include "datagen.h" /* RDG_genBuffer */
/* ************************************* /* *************************************
@ -75,23 +104,32 @@
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
#endif #endif
#ifdef _MSC_VER #if defined(_MSC_VER)
#define snprintf sprintf_s # define snprintf sprintf_s
#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))
/* part of <stdio.h> */
#else
extern int snprintf (char* s, size_t maxlen, const char* format, ...); /* not declared in <stdio.h> when C version < c99 */
#endif #endif
/* ************************************* /* *************************************
* Constants * Constants
***************************************/ ***************************************/
#define NBLOOPS 3 #ifndef ZSTD_VERSION
#define TIMELOOP 2500 # define ZSTD_VERSION ""
#endif
#define NBLOOPS 3
#define TIMELOOP_S 1
#define ACTIVEPERIOD_S 70
#define COOLPERIOD_S 10
#define KB *(1 <<10) #define KB *(1 <<10)
#define MB *(1 <<20) #define MB *(1 <<20)
#define GB *(1U<<30) #define GB *(1U<<30)
static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31)); static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
#define DEFAULT_CHUNKSIZE (4 MB)
static U32 g_compressibilityDefault = 50; static U32 g_compressibilityDefault = 50;
@ -124,62 +162,38 @@ static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result
/* ************************************* /* *************************************
* Benchmark Parameters * Benchmark Parameters
***************************************/ ***************************************/
static int nbIterations = NBLOOPS; static U32 g_nbIterations = NBLOOPS;
static size_t g_blockSize = 0; static size_t g_blockSize = 0;
int g_additionalParam = 0;
void BMK_SetNbIterations(int nbLoops) void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
void BMK_SetNbIterations(unsigned nbLoops)
{ {
nbIterations = nbLoops; g_nbIterations = nbLoops;
DISPLAY("- %i iterations -\n", nbIterations); DISPLAYLEVEL(2, "- %i iterations -\n", g_nbIterations);
} }
void BMK_SetBlockSize(size_t blockSize) void BMK_SetBlockSize(size_t blockSize)
{ {
g_blockSize = blockSize; g_blockSize = blockSize;
DISPLAY("using blocks of size %u KB \n", (U32)(blockSize>>10)); DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
} }
/* ******************************************************** /* ********************************************************
* Private functions * Private functions
**********************************************************/ **********************************************************/
/* returns time span in microseconds */
#if defined(BMK_LEGACY_TIMER) static U64 BMK_clockSpan( BMK_time_t clockStart, BMK_time_t ticksPerSecond )
static int BMK_GetMilliStart(void)
{ {
/* Based on Legacy ftime() BMK_time_t clockEnd;
* Rolls over every ~ 12.1 days (0x100000/24/60/60)
* Use GetMilliSpan to correct for rollover */
struct timeb tb;
int nCount;
ftime( &tb );
nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000);
return nCount;
}
#else (void)ticksPerSecond;
BMK_getTime(clockEnd);
static int BMK_GetMilliStart(void) return BMK_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd);
{
/* Based on newer gettimeofday()
* Use GetMilliSpan to correct for rollover */
struct timeval tv;
int nCount;
gettimeofday(&tv, NULL);
nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000);
return nCount;
}
#endif
static int BMK_GetMilliSpan( int nTimeStart )
{
int nSpan = BMK_GetMilliStart() - nTimeStart;
if ( nSpan < 0 )
nSpan += 0x100000 * 1000;
return nSpan;
} }
static U64 BMK_getFileSize(const char* infilename) static U64 BMK_getFileSize(const char* infilename)
@ -211,47 +225,55 @@ typedef struct
size_t resSize; size_t resSize;
} blockParam_t; } blockParam_t;
typedef struct
{
double ratio;
size_t cSize;
double cSpeed;
double dSpeed;
} benchResult_t;
#define MIN(a,b) ((a)<(b) ? (a) : (b)) #define MIN(a,b) ((a)<(b) ? (a) : (b))
#define MAX(a,b) ((a)>(b) ? (a) : (b)) #define MAX(a,b) ((a)>(b) ? (a) : (b))
static int BMK_benchMem(const void* srcBuffer, size_t srcSize, static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
const char* displayName, int cLevel, const char* displayName, int cLevel,
const size_t* fileSizes, U32 nbFiles, const size_t* fileSizes, U32 nbFiles,
const void* dictBuffer, size_t dictBufferSize) const void* dictBuffer, size_t dictBufferSize, benchResult_t *result)
{ {
const size_t blockSize = (g_blockSize ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */ size_t const blockSize = (g_blockSize ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */
const U32 maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
size_t largestBlockSize = 0;
blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
const size_t maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
void* const compressedBuffer = malloc(maxCompressedSize); void* const compressedBuffer = malloc(maxCompressedSize);
void* const resultBuffer = malloc(srcSize); void* const resultBuffer = malloc(srcSize);
ZSTD_CCtx* refCtx = ZSTD_createCCtx(); ZSTD_CCtx* refCtx = ZSTD_createCCtx();
ZSTD_CCtx* ctx = ZSTD_createCCtx(); ZSTD_CCtx* ctx = ZSTD_createCCtx();
ZSTD_DCtx* refDCtx = ZSTD_createDCtx(); ZSTD_DCtx* refDCtx = ZSTD_createDCtx();
ZSTD_DCtx* dctx = ZSTD_createDCtx(); ZSTD_DCtx* dctx = ZSTD_createDCtx();
U64 crcOrig = XXH64(srcBuffer, srcSize, 0); U32 nbBlocks;
U32 nbBlocks = 0; BMK_time_t ticksPerSecond;
/* init */ /* checks */
if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
/* Memory allocation & restrictions */
if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx) if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx)
EXM_THROW(31, "not enough memory"); EXM_THROW(31, "not enough memory");
/* init */
if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
BMK_initTimer(ticksPerSecond);
/* Init blockTable data */ /* Init blockTable data */
{ { const char* srcPtr = (const char*)srcBuffer;
U32 fileNb;
const char* srcPtr = (const char*)srcBuffer;
char* cPtr = (char*)compressedBuffer; char* cPtr = (char*)compressedBuffer;
char* resPtr = (char*)resultBuffer; char* resPtr = (char*)resultBuffer;
for (fileNb=0; fileNb<nbFiles; fileNb++) { U32 fileNb;
for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
size_t remaining = fileSizes[fileNb]; size_t remaining = fileSizes[fileNb];
U32 nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize); U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
U32 blockEnd = nbBlocks + nbBlocksforThisFile; U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
for ( ; nbBlocks<blockEnd; nbBlocks++) { for ( ; nbBlocks<blockEnd; nbBlocks++) {
size_t thisBlockSize = MIN(remaining, blockSize); size_t const thisBlockSize = MIN(remaining, blockSize);
blockTable[nbBlocks].srcPtr = srcPtr; blockTable[nbBlocks].srcPtr = srcPtr;
blockTable[nbBlocks].cPtr = cPtr; blockTable[nbBlocks].cPtr = cPtr;
blockTable[nbBlocks].resPtr = resPtr; blockTable[nbBlocks].resPtr = resPtr;
@ -261,114 +283,137 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
cPtr += blockTable[nbBlocks].cRoom; cPtr += blockTable[nbBlocks].cRoom;
resPtr += thisBlockSize; resPtr += thisBlockSize;
remaining -= thisBlockSize; remaining -= thisBlockSize;
if (thisBlockSize > largestBlockSize) largestBlockSize = thisBlockSize;
} } } } } }
/* warmimg up memory */ /* warmimg up memory */
RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1); RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
/* Bench */ /* Bench */
{ { double fastestC = 100000000., fastestD = 100000000.;
int loopNb; U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
size_t cSize = 0;
double fastestC = 100000000., fastestD = 100000000.;
double ratio = 0.;
U64 crcCheck = 0; U64 crcCheck = 0;
BMK_time_t coolTime;
U32 testNb;
size_t cSize = 0;
double ratio = 0.;
DISPLAY("\r%79s\r", ""); BMK_getTime(coolTime);
for (loopNb = 1; loopNb <= nbIterations; loopNb++) { DISPLAYLEVEL(2, "\r%79s\r", "");
int nbLoops; for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) {
int milliTime; BMK_time_t clockStart, clockEnd;
U32 blockNb; U64 clockLoop = g_nbIterations ? TIMELOOP_S*1000000ULL : 10;
/* overheat protection */
if (BMK_clockSpan(coolTime, ticksPerSecond) > ACTIVEPERIOD_S*1000000ULL) {
DISPLAY("\rcooling down ... \r");
BMK_sleep(COOLPERIOD_S);
BMK_getTime(coolTime);
}
/* Compression */ /* Compression */
DISPLAY("%2i-%-17.17s :%10u ->\r", loopNb, displayName, (U32)srcSize); DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize);
memset(compressedBuffer, 0xE5, maxCompressedSize); memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
nbLoops = 0; mili_sleep(1); /* give processor time to other processes */
milliTime = BMK_GetMilliStart(); BMK_getTime(clockStart);
while (BMK_GetMilliStart() == milliTime); do { BMK_getTime(clockEnd); }
milliTime = BMK_GetMilliStart(); while (BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0);
while (BMK_GetMilliSpan(milliTime) < TIMELOOP) { BMK_getTime(clockStart);
ZSTD_compressBegin_advanced(refCtx, dictBuffer, dictBufferSize, ZSTD_getParams(cLevel, MAX(dictBufferSize, largestBlockSize)));
for (blockNb=0; blockNb<nbBlocks; blockNb++) { { U32 nbLoops;
size_t rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx, for (nbLoops = 0 ; BMK_clockSpan(clockStart, ticksPerSecond) < clockLoop ; nbLoops++) {
blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, U32 blockNb;
blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize); ZSTD_compressBegin_targetSrcSize(refCtx, dictBuffer, dictBufferSize, blockSize, cLevel);
if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize)); // ZSTD_compressBegin_usingDict(refCtx, dictBuffer, dictBufferSize, cLevel);
blockTable[blockNb].cSize = rSize; for (blockNb=0; blockNb<nbBlocks; blockNb++) {
} size_t const rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx,
nbLoops++; blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
} blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize);
milliTime = BMK_GetMilliSpan(milliTime); if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize));
blockTable[blockNb].cSize = rSize;
} }
{ U64 const clockSpan = BMK_clockSpan(clockStart, ticksPerSecond);
if ((double)clockSpan < fastestC*nbLoops) fastestC = (double)clockSpan / nbLoops;
} }
cSize = 0; cSize = 0;
for (blockNb=0; blockNb<nbBlocks; blockNb++) { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
cSize += blockTable[blockNb].cSize;
if ((double)milliTime < fastestC*nbLoops) fastestC = (double)milliTime / nbLoops;
ratio = (double)srcSize / (double)cSize; ratio = (double)srcSize / (double)cSize;
DISPLAY("%2i-%-17.17s :%10i ->%10i (%5.3f),%6.1f MB/s\r", loopNb, displayName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000.); DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
(double)srcSize / fastestC );
(void)fastestD; (void)crcOrig; /* unused when decompression disabled */
#if 1 #if 1
/* Decompression */ /* Decompression */
memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */ memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
nbLoops = 0; mili_sleep(1); /* give processor time to other processes */
milliTime = BMK_GetMilliStart(); BMK_getTime(clockStart);
while (BMK_GetMilliStart() == milliTime); do { BMK_getTime(clockEnd); }
milliTime = BMK_GetMilliStart(); while (BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0);
BMK_getTime(clockStart);
for ( ; BMK_GetMilliSpan(milliTime) < TIMELOOP; nbLoops++) { { U32 nbLoops;
ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize); for (nbLoops = 0 ; BMK_clockSpan(clockStart, ticksPerSecond) < clockLoop ; nbLoops++) {
for (blockNb=0; blockNb<nbBlocks; blockNb++) { U32 blockNb;
size_t regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx, ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize);
blockTable[blockNb].resPtr, blockTable[blockNb].srcSize, for (blockNb=0; blockNb<nbBlocks; blockNb++) {
blockTable[blockNb].cPtr, blockTable[blockNb].cSize); size_t const regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx,
if (ZSTD_isError(regenSize)) { blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s", blockTable[blockNb].cPtr, blockTable[blockNb].cSize);
blockNb, ZSTD_getErrorName(regenSize)); if (ZSTD_isError(regenSize)) {
goto _findError; DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s \n",
} blockNb, ZSTD_getErrorName(regenSize));
blockTable[blockNb].resSize = regenSize; clockLoop = 0; /* force immediate test end */
break;
}
blockTable[blockNb].resSize = regenSize;
} }
{ U64 const clockSpan = BMK_clockSpan(clockStart, ticksPerSecond);
if ((double)clockSpan < fastestD*nbLoops) fastestD = (double)clockSpan / nbLoops;
} } } }
milliTime = BMK_GetMilliSpan(milliTime); DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
if ((double)milliTime < fastestD*nbLoops) fastestD = (double)milliTime / nbLoops; testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
DISPLAY("%2i-%-17.17s :%10i ->%10i (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", loopNb, displayName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000., (double)srcSize / fastestD / 1000.); (double)srcSize / fastestC,
(double)srcSize / fastestD );
/* CRC Checking */ /* CRC Checking */
_findError: { crcCheck = XXH64(resultBuffer, srcSize, 0);
crcCheck = XXH64(resultBuffer, srcSize, 0); if (crcOrig!=crcCheck) {
if (crcOrig!=crcCheck) { size_t u;
size_t u; DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", displayName, (unsigned)crcOrig, (unsigned)crcCheck); for (u=0; u<srcSize; u++) {
for (u=0; u<srcSize; u++) { if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) { U32 segNb, bNb, pos;
U32 segNb, bNb, pos; size_t bacc = 0;
size_t bacc = 0; DISPLAY("Decoding error at pos %u ", (U32)u);
printf("Decoding error at pos %u ", (U32)u); for (segNb = 0; segNb < nbBlocks; segNb++) {
for (segNb = 0; segNb < nbBlocks; segNb++) { if (bacc + blockTable[segNb].srcSize > u) break;
if (bacc + blockTable[segNb].srcSize > u) break; bacc += blockTable[segNb].srcSize;
bacc += blockTable[segNb].srcSize; }
pos = (U32)(u - bacc);
bNb = pos / (128 KB);
DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
break;
} }
pos = (U32)(u - bacc); if (u==srcSize-1) { /* should never happen */
bNb = pos / (128 KB); DISPLAY("no difference detected\n");
printf("(block %u, sub %u, pos %u) \n", segNb, bNb, pos); } }
break; break;
} } } /* CRC Checking */
if (u==srcSize-1) { /* should never happen */
printf("no difference detected\n");
} }
break;
}
#endif #endif
} } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */
if (crcOrig == crcCheck) if (crcOrig == crcCheck) {
DISPLAY("%2i-%-17.17s :%10i ->%10i (%5.3f),%6.1f MB/s ,%6.1f MB/s \n", cLevel, displayName, (int)srcSize, (int)cSize, ratio, (double)srcSize / fastestC / 1000., (double)srcSize / fastestD / 1000.); result->ratio = ratio;
else result->cSize = cSize;
DISPLAY("%2i-\n", cLevel); result->cSpeed = (double)srcSize / fastestC;
} result->dSpeed = (double)srcSize / fastestD;
}
DISPLAYLEVEL(2, "%2i#\n", cLevel);
} /* Bench */
/* clean up */ /* clean up */
free(compressedBuffer); free(compressedBuffer);
@ -383,39 +428,66 @@ _findError:
static size_t BMK_findMaxMem(U64 requiredMem) static size_t BMK_findMaxMem(U64 requiredMem)
{ {
size_t step = 64 MB; size_t const step = 64 MB;
BYTE* testmem = NULL; BYTE* testmem = NULL;
requiredMem = (((requiredMem >> 26) + 1) << 26); requiredMem = (((requiredMem >> 26) + 1) << 26);
requiredMem += 2 * step; requiredMem += step;
if (requiredMem > maxMemory) requiredMem = maxMemory; if (requiredMem > maxMemory) requiredMem = maxMemory;
while (!testmem) { do {
requiredMem -= step;
testmem = (BYTE*)malloc((size_t)requiredMem); testmem = (BYTE*)malloc((size_t)requiredMem);
} requiredMem -= step;
} while (!testmem);
free(testmem); free(testmem);
return (size_t)(requiredMem - step); return (size_t)(requiredMem);
} }
static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize, static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
const char* displayName, int cLevel, const char* displayName, int cLevel, int cLevelLast,
const size_t* fileSizes, unsigned nbFiles, const size_t* fileSizes, unsigned nbFiles,
const void* dictBuffer, size_t dictBufferSize) const void* dictBuffer, size_t dictBufferSize)
{ {
if (cLevel < 0) { benchResult_t result, total;
int l; int l;
for (l=1; l <= -cLevel; l++)
BMK_benchMem(srcBuffer, benchedSize, SET_HIGH_PRIORITY;
displayName, l,
fileSizes, nbFiles, const char* pch = strrchr(displayName, '\\'); /* Windows */
dictBuffer, dictBufferSize); if (!pch) pch = strrchr(displayName, '/'); /* Linux */
return; if (pch) displayName = pch+1;
memset(&result, 0, sizeof(result));
memset(&total, 0, sizeof(total));
if (g_displayLevel == 1 && !g_additionalParam)
DISPLAY("bench %s: input %u bytes, %i iterations, %u KB blocks\n", ZSTD_VERSION, (U32)benchedSize, g_nbIterations, (U32)(g_blockSize>>10));
if (cLevelLast < cLevel) cLevelLast = cLevel;
for (l=cLevel; l <= cLevelLast; l++) {
BMK_benchMem(srcBuffer, benchedSize,
displayName, l,
fileSizes, nbFiles,
dictBuffer, dictBufferSize, &result);
if (g_displayLevel == 1) {
if (g_additionalParam)
DISPLAY("%-3i%11i (%5.3f) %6.1f MB/s %6.1f MB/s %s (param=%d)\n", -l, (int)result.cSize, result.ratio, result.cSpeed, result.dSpeed, displayName, g_additionalParam);
else
DISPLAY("%-3i%11i (%5.3f) %6.1f MB/s %6.1f MB/s %s\n", -l, (int)result.cSize, result.ratio, result.cSpeed, result.dSpeed, displayName);
total.cSize += result.cSize;
total.cSpeed += result.cSpeed;
total.dSpeed += result.dSpeed;
total.ratio += result.ratio;
} }
if (g_displayLevel == 1 && cLevelLast > cLevel) {
total.cSize /= 1+cLevelLast-cLevel;
total.cSpeed /= 1+cLevelLast-cLevel;
total.dSpeed /= 1+cLevelLast-cLevel;
total.ratio /= 1+cLevelLast-cLevel;
DISPLAY("avg%11i (%5.3f) %6.1f MB/s %6.1f MB/s %s\n", (int)total.cSize, total.ratio, total.cSpeed, total.dSpeed, displayName);
} }
BMK_benchMem(srcBuffer, benchedSize,
displayName, cLevel,
fileSizes, nbFiles,
dictBuffer, dictBufferSize);
} }
static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles) static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
@ -427,31 +499,32 @@ static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
return total; return total;
} }
/*! BMK_loadFiles() :
Loads `buffer` with content of files listed within `fileNamesTable`.
At most, fills `buffer` entirely */
static void BMK_loadFiles(void* buffer, size_t bufferSize, static void BMK_loadFiles(void* buffer, size_t bufferSize,
size_t* fileSizes, size_t* fileSizes,
const char** fileNamesTable, unsigned nbFiles) const char** fileNamesTable, unsigned nbFiles)
{ {
BYTE* buff = (BYTE*)buffer;
size_t pos = 0; size_t pos = 0;
unsigned n;
unsigned n;
for (n=0; n<nbFiles; n++) { for (n=0; n<nbFiles; n++) {
size_t readSize;
U64 fileSize = BMK_getFileSize(fileNamesTable[n]); U64 fileSize = BMK_getFileSize(fileNamesTable[n]);
FILE* f = fopen(fileNamesTable[n], "rb"); FILE* const f = fopen(fileNamesTable[n], "rb");
if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]); if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]); DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
if (fileSize > bufferSize-pos) fileSize = bufferSize-pos; if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
readSize = fread(buff+pos, 1, (size_t)fileSize, f); { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]); if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
pos += readSize; pos += readSize; }
fileSizes[n] = (size_t)fileSize; fileSizes[n] = (size_t)fileSize;
fclose(f); fclose(f);
} }
} }
static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
const char* dictFileName, int cLevel) const char* dictFileName, int cLevel, int cLevelLast)
{ {
void* srcBuffer; void* srcBuffer;
size_t benchedSize; size_t benchedSize;
@ -491,7 +564,7 @@ static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
else displayName = fileNamesTable[0]; else displayName = fileNamesTable[0];
BMK_benchCLevel(srcBuffer, benchedSize, BMK_benchCLevel(srcBuffer, benchedSize,
displayName, cLevel, displayName, cLevel, cLevelLast,
fileSizes, nbFiles, fileSizes, nbFiles,
dictBuffer, dictBufferSize); dictBuffer, dictBufferSize);
@ -502,7 +575,7 @@ static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
} }
static void BMK_syntheticTest(int cLevel, double compressibility) static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility)
{ {
char name[20] = {0}; char name[20] = {0};
size_t benchedSize = 10000000; size_t benchedSize = 10000000;
@ -516,7 +589,7 @@ static void BMK_syntheticTest(int cLevel, double compressibility)
/* Bench */ /* Bench */
snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, &benchedSize, 1, NULL, 0); BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0);
/* clean up */ /* clean up */
free(srcBuffer); free(srcBuffer);
@ -524,14 +597,14 @@ static void BMK_syntheticTest(int cLevel, double compressibility)
int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
const char* dictFileName, int cLevel) const char* dictFileName, int cLevel, int cLevelLast)
{ {
double compressibility = (double)g_compressibilityDefault / 100; double const compressibility = (double)g_compressibilityDefault / 100;
if (nbFiles == 0) if (nbFiles == 0)
BMK_syntheticTest(cLevel, compressibility); BMK_syntheticTest(cLevel, cLevelLast, compressibility);
else else
BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel); BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast);
return 0; return 0;
} }

View File

@ -27,10 +27,11 @@
/* Main function */ /* Main function */
int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
const char* dictFileName, int cLevel); const char* dictFileName, int cLevel, int cLevelLast);
/* Set Parameters */ /* Set Parameters */
void BMK_SetNbIterations(int nbLoops); void BMK_SetNbIterations(unsigned nbLoops);
void BMK_SetBlockSize(size_t blockSize); void BMK_SetBlockSize(size_t blockSize);
void BMK_setAdditionalParam(int additionalParam);
void BMK_setNotificationLevel(unsigned level);

View File

@ -1,6 +1,6 @@
/* /*
datagen.c - compressible data generator test tool datagen.c - compressible data generator test tool
Copyright (C) Yann Collet 2012-2015 Copyright (C) Yann Collet 2012-2016
GPL v2 License GPL v2 License
@ -19,8 +19,8 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at : You can contact the author at :
- ZSTD source repository : https://github.com/Cyan4973/zstd - zstd homepage : http://www.zstd.net/
- Public forum : https://groups.google.com/forum/#!forum/lz4c - source repository : https://github.com/Cyan4973/zstd
*/ */
/*-************************************ /*-************************************
@ -67,9 +67,6 @@
**************************************/ **************************************/
#define KB *(1 <<10) #define KB *(1 <<10)
#define PRIME1 2654435761U
#define PRIME2 2246822519U
/*-************************************ /*-************************************
* Local types * Local types
@ -87,9 +84,11 @@ typedef BYTE litDistribTable[LTSIZE];
#define RDG_rotl32(x,r) ((x << r) | (x >> (32 - r))) #define RDG_rotl32(x,r) ((x << r) | (x >> (32 - r)))
static unsigned int RDG_rand(U32* src) static unsigned int RDG_rand(U32* src)
{ {
static const U32 prime1 = 2654435761U;
static const U32 prime2 = 2246822519U;
U32 rand32 = *src; U32 rand32 = *src;
rand32 *= PRIME1; rand32 *= prime1;
rand32 ^= PRIME2; rand32 ^= prime2;
rand32 = RDG_rotl32(rand32, 13); rand32 = RDG_rotl32(rand32, 13);
*src = rand32; *src = rand32;
return rand32; return rand32;
@ -103,7 +102,7 @@ static void RDG_fillLiteralDistrib(litDistribTable lt, double ld)
BYTE firstChar = '('; BYTE firstChar = '(';
BYTE lastChar = '}'; BYTE lastChar = '}';
if (ld==0.0) { if (ld<=0.0) {
character = 0; character = 0;
firstChar = 0; firstChar = 0;
lastChar =255; lastChar =255;
@ -122,7 +121,7 @@ static void RDG_fillLiteralDistrib(litDistribTable lt, double ld)
static BYTE RDG_genChar(U32* seed, const litDistribTable lt) static BYTE RDG_genChar(U32* seed, const litDistribTable lt)
{ {
U32 id = RDG_rand(seed) & LTMASK; U32 const id = RDG_rand(seed) & LTMASK;
return (lt[id]); return (lt[id]);
} }
@ -194,28 +193,28 @@ void RDG_genBuffer(void* buffer, size_t size, double matchProba, double litProba
#define RDG_DICTSIZE (32 KB) #define RDG_DICTSIZE (32 KB)
#define RDG_BLOCKSIZE (128 KB) #define RDG_BLOCKSIZE (128 KB)
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
void RDG_genStdout(unsigned long long size, double matchProba, double litProba, unsigned seed) void RDG_genStdout(unsigned long long size, double matchProba, double litProba, unsigned seed)
{ {
BYTE* buff = (BYTE*)malloc(RDG_DICTSIZE + RDG_BLOCKSIZE); BYTE* buff = (BYTE*)malloc(RDG_DICTSIZE + RDG_BLOCKSIZE);
U64 total = 0; U64 total = 0;
size_t genBlockSize = RDG_BLOCKSIZE; litDistribTable ldt;
litDistribTable lt;
/* init */ /* init */
if (buff==NULL) { fprintf(stdout, "not enough memory\n"); exit(1); } if (buff==NULL) { fprintf(stdout, "not enough memory\n"); exit(1); }
if (litProba==0.0) litProba = matchProba / 4.5; if (litProba<=0.0) litProba = matchProba / 4.5;
RDG_fillLiteralDistrib(lt, litProba); RDG_fillLiteralDistrib(ldt, litProba);
SET_BINARY_MODE(stdout); SET_BINARY_MODE(stdout);
/* Generate initial dict */ /* Generate initial dict */
RDG_genBlock(buff, RDG_DICTSIZE, 0, matchProba, lt, &seed); RDG_genBlock(buff, RDG_DICTSIZE, 0, matchProba, ldt, &seed);
/* Generate compressible data */ /* Generate compressible data */
while (total < size) { while (total < size) {
RDG_genBlock(buff, RDG_DICTSIZE+RDG_BLOCKSIZE, RDG_DICTSIZE, matchProba, lt, &seed); size_t const genBlockSize = (size_t) (MIN (RDG_BLOCKSIZE, size-total));
if (size-total < RDG_BLOCKSIZE) genBlockSize = (size_t)(size-total); RDG_genBlock(buff, RDG_DICTSIZE+RDG_BLOCKSIZE, RDG_DICTSIZE, matchProba, ldt, &seed);
total += genBlockSize; total += genBlockSize;
fwrite(buff, 1, genBlockSize, stdout); { size_t const unused = fwrite(buff, 1, genBlockSize, stdout); (void)unused; }
/* update dict */ /* update dict */
memcpy(buff, buff + RDG_BLOCKSIZE, RDG_DICTSIZE); memcpy(buff, buff + RDG_BLOCKSIZE, RDG_DICTSIZE);
} }

View File

@ -259,7 +259,7 @@ int DiB_trainFromFiles(const char* dictFileName, unsigned maxDictSize,
srcBuffer, fileSizes, nbFiles, srcBuffer, fileSizes, nbFiles,
params); params);
if (ZDICT_isError(dictSize)) { if (ZDICT_isError(dictSize)) {
DISPLAYLEVEL(1, "dictionary training failed : %s", ZDICT_getErrorName(dictSize)); /* should not happen */ DISPLAYLEVEL(1, "dictionary training failed : %s \n", ZDICT_getErrorName(dictSize)); /* should not happen */
result = 1; result = 1;
goto _cleanup; goto _cleanup;
} }

View File

@ -64,7 +64,7 @@
#include <sys/stat.h> /* stat64 */ #include <sys/stat.h> /* stat64 */
#include "mem.h" #include "mem.h"
#include "fileio.h" #include "fileio.h"
#include "zstd_static.h" /* ZSTD_magicNumber */ #include "zstd_static.h" /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
#include "zbuff_static.h" #include "zbuff_static.h"
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
@ -126,6 +126,7 @@
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \ if ((FIO_GetMilliSpan(g_time) > refreshRate) || (g_displayLevel>=4)) \
@ -142,7 +143,8 @@ static clock_t g_time = 0;
***************************************/ ***************************************/
static U32 g_overwrite = 0; static U32 g_overwrite = 0;
void FIO_overwriteMode(void) { g_overwrite=1; } void FIO_overwriteMode(void) { g_overwrite=1; }
void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; } static U32 g_maxWLog = 23;
void FIO_setMaxWLog(unsigned maxWLog) { g_maxWLog = maxWLog; }
/*-************************************* /*-*************************************
@ -239,10 +241,10 @@ static FILE* FIO_openDstFile(const char* dstFileName)
} }
/*!FIO_loadFile /*! FIO_loadFile() :
* creates a buffer, pointed by *bufferPtr, * creates a buffer, pointed by `*bufferPtr`,
* loads "filename" content into it * loads `filename` content into it,
* up to MAX_DICT_SIZE bytes * up to MAX_DICT_SIZE bytes
*/ */
static size_t FIO_loadFile(void** bufferPtr, const char* fileName) static size_t FIO_loadFile(void** bufferPtr, const char* fileName)
{ {
@ -274,7 +276,7 @@ static size_t FIO_loadFile(void** bufferPtr, const char* fileName)
} }
/* ********************************************************************** /*-**********************************************************************
* Compression * Compression
************************************************************************/ ************************************************************************/
typedef struct { typedef struct {
@ -321,7 +323,7 @@ static void FIO_freeCResources(cRess_t ress)
/*! FIO_compressFilename_internal() : /*! FIO_compressFilename_internal() :
* same as FIO_compressFilename_extRess(), with ress.desFile already opened * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
* @return : 0 : compression completed correctly, * @return : 0 : compression completed correctly,
* 1 : missing or pb opening srcFileName * 1 : missing or pb opening srcFileName
*/ */
@ -331,24 +333,28 @@ static int FIO_compressFilename_internal(cRess_t ress,
{ {
FILE* srcFile = ress.srcFile; FILE* srcFile = ress.srcFile;
FILE* dstFile = ress.dstFile; FILE* dstFile = ress.dstFile;
U64 filesize = 0; U64 readsize = 0;
U64 compressedfilesize = 0; U64 compressedfilesize = 0;
size_t dictSize = ress.dictBufferSize; size_t dictSize = ress.dictBufferSize;
size_t sizeCheck, errorCode; size_t sizeCheck;
ZSTD_parameters params;
U64 const fileSize = FIO_getFileSize(srcFileName);
/* init */ /* init */
filesize = MAX(FIO_getFileSize(srcFileName),dictSize); params.cParams = ZSTD_getCParams(cLevel, fileSize, dictSize);
errorCode = ZBUFF_compressInit_advanced(ress.ctx, ress.dictBuffer, ress.dictBufferSize, ZSTD_getParams(cLevel, filesize)); params.fParams.contentSizeFlag = 1;
if (ZBUFF_isError(errorCode)) EXM_THROW(21, "Error initializing compression : %s", ZBUFF_getErrorName(errorCode)); if (g_maxWLog) if (params.cParams.windowLog > g_maxWLog) params.cParams.windowLog = g_maxWLog;
{ size_t const errorCode = ZBUFF_compressInit_advanced(ress.ctx, ress.dictBuffer, ress.dictBufferSize, params, fileSize);
if (ZBUFF_isError(errorCode)) EXM_THROW(21, "Error initializing compression : %s", ZBUFF_getErrorName(errorCode)); }
/* Main compression loop */ /* Main compression loop */
filesize = 0; readsize = 0;
while (1) { while (1) {
/* Fill input Buffer */ /* Fill input Buffer */
size_t inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile); size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
if (inSize==0) break; if (inSize==0) break;
filesize += inSize; readsize += inSize;
DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(filesize>>20)); DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(readsize>>20));
{ /* Compress using buffered streaming */ { /* Compress using buffered streaming */
size_t usedInSize = inSize; size_t usedInSize = inSize;
@ -365,13 +371,12 @@ static int FIO_compressFilename_internal(cRess_t ress,
if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName);
compressedfilesize += cSize; compressedfilesize += cSize;
} }
DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(filesize>>20), (double)compressedfilesize/filesize*100); DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(readsize>>20), (double)compressedfilesize/readsize*100);
} }
/* End of Frame */ /* End of Frame */
{ { size_t cSize = ress.dstBufferSize;
size_t cSize = ress.dstBufferSize; size_t const result = ZBUFF_compressEnd(ress.ctx, ress.dstBuffer, &cSize);
size_t result = ZBUFF_compressEnd(ress.ctx, ress.dstBuffer, &cSize);
if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end"); if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end");
sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile); sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile);
@ -382,14 +387,14 @@ static int FIO_compressFilename_internal(cRess_t ress,
/* Status */ /* Status */
DISPLAYLEVEL(2, "\r%79s\r", ""); DISPLAYLEVEL(2, "\r%79s\r", "");
DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n", DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n",
(unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100); (unsigned long long)readsize, (unsigned long long) compressedfilesize, (double)compressedfilesize/readsize*100);
return 0; return 0;
} }
/*! FIO_compressFilename_internal() : /*! FIO_compressFilename_internal() :
* same as FIO_compressFilename_extRess(), with ress.desFile already opened * same as FIO_compressFilename_extRess(), with ress.destFile already opened (typically stdout)
* @return : 0 : compression completed correctly, * @return : 0 : compression completed correctly,
* 1 : missing or pb opening srcFileName * 1 : missing or pb opening srcFileName
*/ */
@ -427,6 +432,7 @@ static int FIO_compressFilename_extRess(cRess_t ress,
if (ress.dstFile==0) { fclose(ress.srcFile); return 1; } if (ress.dstFile==0) { fclose(ress.srcFile); return 1; }
result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel); result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel);
if (result!=0) remove(dstFileName); /* remove operation artefact */
fclose(ress.srcFile); /* no pb to expect : only reading */ fclose(ress.srcFile); /* no pb to expect : only reading */
if (fclose(ress.dstFile)) EXM_THROW(28, "Write error : cannot properly close %s", dstFileName); if (fclose(ress.dstFile)) EXM_THROW(28, "Write error : cannot properly close %s", dstFileName);
@ -482,7 +488,7 @@ int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFile
ress.dstFile = stdout; ress.dstFile = stdout;
for (u=0; u<nbFiles; u++) for (u=0; u<nbFiles; u++)
missed_files += FIO_compressFilename_srcFile(ress, stdoutmark, missed_files += FIO_compressFilename_srcFile(ress, stdoutmark,
inFileNamesTable[u], compressionLevel); inFileNamesTable[u], compressionLevel);
if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close %s", stdoutmark); if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close %s", stdoutmark);
} else { } else {
for (u=0; u<nbFiles; u++) { for (u=0; u<nbFiles; u++) {
@ -547,35 +553,46 @@ static void FIO_freeDResources(dRess_t ress)
} }
/** FIO_decompressFrame() :
@return : size of decoded frame
*/
unsigned long long FIO_decompressFrame(dRess_t ress, unsigned long long FIO_decompressFrame(dRess_t ress,
FILE* foutput, FILE* finput, size_t alreadyLoaded) FILE* foutput, FILE* finput, size_t alreadyLoaded)
{ {
U64 frameSize = 0; U64 frameSize = 0;
size_t readSize=alreadyLoaded; size_t readSize;
ZBUFF_decompressInitDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize);
/* Complete Header loading */
{ size_t const toLoad = ZSTD_frameHeaderSize_max - alreadyLoaded; /* assumption : alreadyLoaded <= ZSTD_frameHeaderSize_max */
size_t const checkSize = fread(((char*)ress.srcBuffer) + alreadyLoaded, 1, toLoad, finput);
if (checkSize != toLoad) EXM_THROW(32, "Read error"); /* assumption : srcSize >= ZSTD_frameHeaderSize_max */
}
readSize = ZSTD_frameHeaderSize_max;
/* Main decompression Loop */ /* Main decompression Loop */
ZBUFF_decompressInitDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize);
while (1) { while (1) {
/* Decode */ /* Decode */
size_t sizeCheck;
size_t inSize=readSize, decodedSize=ress.dstBufferSize; size_t inSize=readSize, decodedSize=ress.dstBufferSize;
size_t toRead = ZBUFF_decompressContinue(ress.dctx, ress.dstBuffer, &decodedSize, ress.srcBuffer, &inSize); size_t toRead = ZBUFF_decompressContinue(ress.dctx, ress.dstBuffer, &decodedSize, ress.srcBuffer, &inSize);
if (ZBUFF_isError(toRead)) EXM_THROW(36, "Decoding error : %s", ZBUFF_getErrorName(toRead)); if (ZBUFF_isError(toRead)) EXM_THROW(36, "Decoding error : %s", ZBUFF_getErrorName(toRead));
readSize -= inSize; readSize -= inSize;
/* Write block */ /* Write block */
sizeCheck = fwrite(ress.dstBuffer, 1, decodedSize, foutput); { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, decodedSize, foutput);
if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block to destination file"); if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block into destination"); }
frameSize += decodedSize; frameSize += decodedSize;
DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) ); DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) );
if (toRead == 0) break; if (toRead == 0) break; /* end of frame */
if (readSize) EXM_THROW(38, "Decoding error : should consume entire input"); if (readSize) EXM_THROW(38, "Decoding error : should consume entire input");
/* Fill input buffer */ /* Fill input buffer */
if (toRead > ress.srcBufferSize) EXM_THROW(34, "too large block"); if (toRead > ress.srcBufferSize) EXM_THROW(34, "too large block");
readSize = fread(ress.srcBuffer, 1, toRead, finput); readSize = fread(ress.srcBuffer, 1, toRead, finput);
if (readSize != toRead) EXM_THROW(35, "Read error"); if (readSize != toRead)
EXM_THROW(35, "Read error");
} }
return frameSize; return frameSize;
@ -596,10 +613,9 @@ static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
/* for each frame */ /* for each frame */
for ( ; ; ) { for ( ; ; ) {
size_t sizeCheck;
/* check magic number -> version */ /* check magic number -> version */
size_t toRead = 4; size_t const toRead = 4;
sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile); size_t const sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile);
if (sizeCheck==0) break; /* no more input */ if (sizeCheck==0) break; /* no more input */
if (sizeCheck != toRead) EXM_THROW(31, "zstd: %s read error : cannot read header", srcFileName); if (sizeCheck != toRead) EXM_THROW(31, "zstd: %s read error : cannot read header", srcFileName);
#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1) #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
@ -633,13 +649,17 @@ static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
static int FIO_decompressFile_extRess(dRess_t ress, static int FIO_decompressFile_extRess(dRess_t ress,
const char* dstFileName, const char* srcFileName) const char* dstFileName, const char* srcFileName)
{ {
int result;
ress.dstFile = FIO_openDstFile(dstFileName); ress.dstFile = FIO_openDstFile(dstFileName);
if (ress.dstFile==0) return 1; if (ress.dstFile==0) return 1;
FIO_decompressSrcFile(ress, srcFileName); result = FIO_decompressSrcFile(ress, srcFileName);
if (result != 0) {
remove(dstFileName);
}
if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName); if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName);
return 0; return result;
} }

View File

@ -41,14 +41,15 @@ extern "C" {
#endif #endif
/* ************************************* /*-*************************************
* Parameters * Parameters
***************************************/ ***************************************/
void FIO_overwriteMode(void); void FIO_overwriteMode(void);
void FIO_setNotificationLevel(unsigned level); void FIO_setNotificationLevel(unsigned level);
void FIO_setMaxWLog(unsigned maxWLog); /**< if `maxWLog` == 0, no max enforced */
/* ************************************* /*-*************************************
* Single File functions * Single File functions
***************************************/ ***************************************/
/** FIO_compressFilename() : /** FIO_compressFilename() :
@ -60,7 +61,7 @@ int FIO_compressFilename (const char* outfilename, const char* infilename, const
int FIO_decompressFilename (const char* outfilename, const char* infilename, const char* dictFileName); int FIO_decompressFilename (const char* outfilename, const char* infilename, const char* dictFileName);
/* ************************************* /*-*************************************
* Multiple File functions * Multiple File functions
***************************************/ ***************************************/
/** FIO_compressMultipleFilenames() : /** FIO_compressMultipleFilenames() :

View File

@ -1,6 +1,6 @@
/* /*
fullbench.c - Detailed bench program for zstd fullbench.c - Detailed bench program for zstd
Copyright (C) Yann Collet 2014-2015 Copyright (C) Yann Collet 2014-2016
GPL v2 License GPL v2 License
@ -19,11 +19,10 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at : You can contact the author at :
- zstd source repository : https://github.com/Cyan4973/zstd - zstd homepage : http://www.zstd.net
- ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
*/ */
/************************************** /*_************************************
* Compiler Options * Compiler Options
**************************************/ **************************************/
/* Disable some Visual warning messages */ /* Disable some Visual warning messages */
@ -38,13 +37,8 @@
# define _LARGEFILE64_SOURCE # define _LARGEFILE64_SOURCE
#endif #endif
/* S_ISREG & gettimeofday() are not supported by MSVC */
#if defined(_MSC_VER) || defined(_WIN32)
# define BMK_LEGACY_TIMER 1
#endif
/*_************************************
/**************************************
* Includes * Includes
**************************************/ **************************************/
#include <stdlib.h> /* malloc */ #include <stdlib.h> /* malloc */
@ -52,21 +46,16 @@
#include <sys/types.h> /* stat64 */ #include <sys/types.h> /* stat64 */
#include <sys/stat.h> /* stat64 */ #include <sys/stat.h> /* stat64 */
#include <string.h> /* strcmp */ #include <string.h> /* strcmp */
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
/* Use ftime() if gettimeofday() is not available on your target */
#if defined(BMK_LEGACY_TIMER)
# include <sys/timeb.h> /* timeb, ftime */
#else
# include <sys/time.h> /* gettimeofday */
#endif
#include "mem.h" #include "mem.h"
#include "zstd_static.h" #include "zstd_static.h"
#include "fse_static.h" #include "fse_static.h"
#include "zbuff.h"
#include "datagen.h" #include "datagen.h"
/************************************** /*_************************************
* Compiler Options * Compiler Options
**************************************/ **************************************/
/* S_ISREG & gettimeofday() are not supported by MSVC */ /* S_ISREG & gettimeofday() are not supported by MSVC */
@ -75,7 +64,7 @@
#endif #endif
/************************************** /*_************************************
* Constants * Constants
**************************************/ **************************************/
#define PROGRAM_DESCRIPTION "Zstandard speed analyzer" #define PROGRAM_DESCRIPTION "Zstandard speed analyzer"
@ -90,99 +79,63 @@
#define MB *(1<<20) #define MB *(1<<20)
#define NBLOOPS 6 #define NBLOOPS 6
#define TIMELOOP 2500 #define TIMELOOP_S 2
#define KNUTH 2654435761U #define KNUTH 2654435761U
#define MAX_MEM (1984 MB) #define MAX_MEM (1984 MB)
#define DEFAULT_CHUNKSIZE (4<<20)
#define COMPRESSIBILITY_DEFAULT 0.50 #define COMPRESSIBILITY_DEFAULT 0.50
static const size_t sampleSize = 10000000; static const size_t g_sampleSize = 10000000;
/************************************** /*_************************************
* Macros * Macros
**************************************/ **************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
/************************************** /*_************************************
* Benchmark Parameters * Benchmark Parameters
**************************************/ **************************************/
static int nbIterations = NBLOOPS; static U32 g_nbIterations = NBLOOPS;
static double g_compressibility = COMPRESSIBILITY_DEFAULT; static double g_compressibility = COMPRESSIBILITY_DEFAULT;
void BMK_SetNbIterations(int nbLoops) static void BMK_SetNbIterations(U32 nbLoops)
{ {
nbIterations = nbLoops; g_nbIterations = nbLoops;
DISPLAY("- %i iterations -\n", nbIterations); DISPLAY("- %i iterations -\n", g_nbIterations);
} }
/********************************************************* /*_*******************************************************
* Private functions * Private functions
*********************************************************/ *********************************************************/
static clock_t BMK_clockSpan( clock_t clockStart )
#if defined(BMK_LEGACY_TIMER)
static int BMK_GetMilliStart(void)
{ {
/* Based on Legacy ftime() return clock() - clockStart; /* works even if overflow, span limited to <= ~30mn */
* Rolls over every ~ 12.1 days (0x100000/24/60/60)
* Use GetMilliSpan to correct for rollover */
struct timeb tb;
int nCount;
ftime( &tb );
nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000);
return nCount;
}
#else
static int BMK_GetMilliStart(void)
{
/* Based on newer gettimeofday()
* Use GetMilliSpan to correct for rollover */
struct timeval tv;
int nCount;
gettimeofday(&tv, NULL);
nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000);
return nCount;
}
#endif
static int BMK_GetMilliSpan( int nTimeStart )
{
int nSpan = BMK_GetMilliStart() - nTimeStart;
if ( nSpan < 0 )
nSpan += 0x100000 * 1000;
return nSpan;
} }
static size_t BMK_findMaxMem(U64 requiredMem) static size_t BMK_findMaxMem(U64 requiredMem)
{ {
size_t step = 64 MB; size_t const step = 64 MB;
BYTE* testmem=NULL; void* testmem = NULL;
requiredMem = (((requiredMem >> 26) + 1) << 26); requiredMem = (((requiredMem >> 26) + 1) << 26);
if (requiredMem > MAX_MEM) requiredMem = MAX_MEM; if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
requiredMem += 2*step; requiredMem += step;
while (!testmem) do {
{ testmem = malloc ((size_t)requiredMem);
requiredMem -= step; requiredMem -= step;
testmem = (BYTE*) malloc ((size_t)requiredMem); } while (!testmem);
}
free (testmem); free (testmem);
return (size_t) (requiredMem - step); return (size_t) requiredMem;
} }
static U64 BMK_GetFileSize(char* infilename) static U64 BMK_GetFileSize(const char* infilename)
{ {
int r; int r;
#if defined(_MSC_VER) #if defined(_MSC_VER)
@ -197,67 +150,118 @@ static U64 BMK_GetFileSize(char* infilename)
} }
/********************************************************* /*_*******************************************************
* Benchmark wrappers * Benchmark wrappers
*********************************************************/ *********************************************************/
typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t; typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
typedef struct typedef struct {
{
blockType_t blockType; blockType_t blockType;
U32 unusedBits; U32 unusedBits;
U32 origSize; U32 origSize;
} blockProperties_t; } blockProperties_t;
static size_t g_cSize = 0;
static ZSTD_DCtx* g_dctxPtr = NULL;
extern size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr);
extern size_t ZSTD_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t* dumpsLengthPtr, FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb, const void* src, size_t srcSize);
size_t local_ZSTD_compress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize) size_t local_ZSTD_compress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
{ {
(void)buff2; (void)buff2;
return ZSTD_compress(dst, dstSize, src, srcSize, 1); return ZSTD_compress(dst, dstSize, src, srcSize, 1);
} }
static size_t g_cSize = 0;
size_t local_ZSTD_decompress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize) size_t local_ZSTD_decompress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
{ {
(void)src; (void)srcSize; (void)src; (void)srcSize;
return ZSTD_decompress(dst, dstSize, buff2, g_cSize); return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
} }
static ZSTD_DCtx* g_zdc = NULL;
extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize); extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize);
size_t local_ZSTD_decodeLiteralsBlock(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize) size_t local_ZSTD_decodeLiteralsBlock(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
{ {
(void)src; (void)srcSize; (void)dst; (void)dstSize; (void)src; (void)srcSize; (void)dst; (void)dstSize;
return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_dctxPtr, buff2, g_cSize); return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_zdc, buff2, g_cSize);
} }
extern size_t ZSTD_getcBlockSize(const void* src, size_t srcSize, blockProperties_t* bpPtr);
extern size_t ZSTD_decodeSeqHeaders(int* nbSeq, FSE_DTable* DTableLL, FSE_DTable* DTableML, FSE_DTable* DTableOffb, U32 tableRepeatFlag, const void* src, size_t srcSize);
size_t local_ZSTD_decodeSeqHeaders(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize) size_t local_ZSTD_decodeSeqHeaders(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
{ {
U32 DTableML[FSE_DTABLE_SIZE_U32(10)], DTableLL[FSE_DTABLE_SIZE_U32(10)], DTableOffb[FSE_DTABLE_SIZE_U32(9)]; /* MLFSELog, LLFSELog and OffFSELog are not public values */ U32 DTableML[FSE_DTABLE_SIZE_U32(10)], DTableLL[FSE_DTABLE_SIZE_U32(10)], DTableOffb[FSE_DTABLE_SIZE_U32(9)]; /* MLFSELog, LLFSELog and OffFSELog are not public values */
const BYTE* dumps;
size_t length;
int nbSeq; int nbSeq;
(void)src; (void)srcSize; (void)dst; (void)dstSize; (void)src; (void)srcSize; (void)dst; (void)dstSize;
return ZSTD_decodeSeqHeaders(&nbSeq, &dumps, &length, DTableLL, DTableML, DTableOffb, buff2, g_cSize); return ZSTD_decodeSeqHeaders(&nbSeq, DTableLL, DTableML, DTableOffb, 0, buff2, g_cSize);
} }
static ZBUFF_CCtx* g_zbcc = NULL;
size_t local_ZBUFF_compress(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
{
size_t compressedSize;
size_t srcRead = srcSize, dstWritten = dstCapacity;
(void)buff2;
ZBUFF_compressInit(g_zbcc, 1);
ZBUFF_compressContinue(g_zbcc, dst, &dstWritten, src, &srcRead);
compressedSize = dstWritten;
dstWritten = dstCapacity-compressedSize;
ZBUFF_compressEnd(g_zbcc, ((char*)dst)+compressedSize, &dstWritten);
compressedSize += dstWritten;
return compressedSize;
}
/********************************************************* static ZBUFF_DCtx* g_zbdc = NULL;
static size_t local_ZBUFF_decompress(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
{
size_t srcRead = g_cSize, dstWritten = dstCapacity;
(void)src; (void)srcSize;
ZBUFF_decompressInit(g_zbdc);
ZBUFF_decompressContinue(g_zbdc, dst, &dstWritten, buff2, &srcRead);
return dstWritten;
}
static ZSTD_CCtx* g_zcc = NULL;
size_t local_ZSTD_compressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
{
size_t compressedSize;
(void)buff2;
ZSTD_compressBegin(g_zcc, 1);
compressedSize = ZSTD_compressContinue(g_zcc, dst, dstCapacity, src, srcSize);
compressedSize += ZSTD_compressEnd(g_zcc, ((char*)dst)+compressedSize, dstCapacity-compressedSize);
return compressedSize;
}
size_t local_ZSTD_decompressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
{
size_t regeneratedSize = 0;
const BYTE* ip = (const BYTE*)buff2;
const BYTE* const iend = ip + g_cSize;
BYTE* op = (BYTE*)dst;
size_t remainingCapacity = dstCapacity;
(void)src; (void)srcSize;
ZSTD_decompressBegin(g_zdc);
while (ip < iend) {
size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize);
ip += iSize;
regeneratedSize += decodedSize;
op += decodedSize;
remainingCapacity -= decodedSize;
}
return regeneratedSize;
}
/*_*******************************************************
* Bench functions * Bench functions
*********************************************************/ *********************************************************/
size_t benchMem(void* src, size_t srcSize, U32 benchNb) static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
{ {
BYTE* dstBuff; BYTE* dstBuff;
size_t dstBuffSize; size_t dstBuffSize;
BYTE* buff2; BYTE* buff2;
int loopNb;
const char* benchName; const char* benchName;
size_t (*benchFunction)(void* dst, size_t dstSize, void* verifBuff, const void* src, size_t srcSize); size_t (*benchFunction)(void* dst, size_t dstSize, void* verifBuff, const void* src, size_t srcSize);
double bestTime = 100000000.; double bestTime = 100000000.;
size_t errorCode = 0;
/* Selection */ /* Selection */
switch(benchNb) switch(benchNb)
@ -265,15 +269,27 @@ size_t benchMem(void* src, size_t srcSize, U32 benchNb)
case 1: case 1:
benchFunction = local_ZSTD_compress; benchName = "ZSTD_compress"; benchFunction = local_ZSTD_compress; benchName = "ZSTD_compress";
break; break;
case 11: case 2:
benchFunction = local_ZSTD_decompress; benchName = "ZSTD_decompress"; benchFunction = local_ZSTD_decompress; benchName = "ZSTD_decompress";
break; break;
case 11:
benchFunction = local_ZSTD_compressContinue; benchName = "ZSTD_compressContinue";
break;
case 12:
benchFunction = local_ZSTD_decompressContinue; benchName = "ZSTD_decompressContinue";
break;
case 31: case 31:
benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "ZSTD_decodeLiteralsBlock"; benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "ZSTD_decodeLiteralsBlock";
break; break;
case 32: case 32:
benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "ZSTD_decodeSeqHeaders"; benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "ZSTD_decodeSeqHeaders";
break; break;
case 41:
benchFunction = local_ZBUFF_compress; benchName = "ZBUFF_compressContinue";
break;
case 42:
benchFunction = local_ZBUFF_decompress; benchName = "ZBUFF_decompressContinue";
break;
default : default :
return 0; return 0;
} }
@ -282,9 +298,7 @@ size_t benchMem(void* src, size_t srcSize, U32 benchNb)
dstBuffSize = ZSTD_compressBound(srcSize); dstBuffSize = ZSTD_compressBound(srcSize);
dstBuff = (BYTE*)malloc(dstBuffSize); dstBuff = (BYTE*)malloc(dstBuffSize);
buff2 = (BYTE*)malloc(dstBuffSize); buff2 = (BYTE*)malloc(dstBuffSize);
g_dctxPtr = ZSTD_createDCtx(); if ((!dstBuff) || (!buff2)) {
if ((!dstBuff) || (!buff2))
{
DISPLAY("\nError: not enough memory!\n"); DISPLAY("\nError: not enough memory!\n");
free(dstBuff); free(buff2); free(dstBuff); free(buff2);
return 12; return 12;
@ -293,45 +307,66 @@ size_t benchMem(void* src, size_t srcSize, U32 benchNb)
/* Preparation */ /* Preparation */
switch(benchNb) switch(benchNb)
{ {
case 11: case 2:
g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
break;
case 11 :
if (g_zcc==NULL) g_zcc = ZSTD_createCCtx();
break;
case 12 :
if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1); g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
break; break;
case 31: /* ZSTD_decodeLiteralsBlock */ case 31: /* ZSTD_decodeLiteralsBlock */
{ if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
blockProperties_t bp; { blockProperties_t bp;
ZSTD_frameParams zfp;
size_t frameHeaderSize, skippedSize;
g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1);
ZSTD_getcBlockSize(dstBuff+4, dstBuffSize, &bp); /* Get 1st block type */ frameHeaderSize = ZSTD_getFrameParams(&zfp, dstBuff, ZSTD_frameHeaderSize_min);
if (bp.blockType != bt_compressed) if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min;
{ ZSTD_getcBlockSize(dstBuff+frameHeaderSize, dstBuffSize, &bp); /* Get 1st block type */
if (bp.blockType != bt_compressed) {
DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n"); DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n");
goto _cleanOut; goto _cleanOut;
} }
memcpy(buff2, dstBuff+8, g_cSize-8); skippedSize = frameHeaderSize + 3 /* ZSTD_blockHeaderSize */;
memcpy(buff2, dstBuff+skippedSize, g_cSize-skippedSize);
srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */ srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
break; break;
} }
case 32: /* ZSTD_decodeSeqHeaders */ case 32: /* ZSTD_decodeSeqHeaders */
{ if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
blockProperties_t bp; { blockProperties_t bp;
ZSTD_frameParams zfp;
const BYTE* ip = dstBuff; const BYTE* ip = dstBuff;
const BYTE* iend; const BYTE* iend;
size_t blockSize; size_t frameHeaderSize, cBlockSize;
ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); /* it would be better to use direct block compression here */ ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); /* it would be better to use direct block compression here */
ip += 5; /* Skip frame Header */ g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1);
blockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */ frameHeaderSize = ZSTD_getFrameParams(&zfp, dstBuff, ZSTD_frameHeaderSize_min);
if (bp.blockType != bt_compressed) if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min;
{ ip += frameHeaderSize; /* Skip frame Header */
cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */
if (bp.blockType != bt_compressed) {
DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n"); DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n");
goto _cleanOut; goto _cleanOut;
} }
iend = ip + 3 + blockSize; /* End of first block */ iend = ip + 3 /* ZSTD_blockHeaderSize */ + cBlockSize; /* End of first block */
ip += 3; /* skip block header */ ip += 3 /* ZSTD_blockHeaderSize */; /* skip block header */
ip += ZSTD_decodeLiteralsBlock(g_dctxPtr, ip, iend-ip); /* skip literal segment */ ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, iend-ip); /* skip literal segment */
g_cSize = iend-ip; g_cSize = iend-ip;
memcpy(buff2, ip, g_cSize); /* copy rest of block (it starts by SeqHeader) */ memcpy(buff2, ip, g_cSize); /* copy rest of block (it starts by SeqHeader) */
srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */ srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
break; break;
} }
case 41 :
if (g_zbcc==NULL) g_zbcc = ZBUFF_createCCtx();
break;
case 42 :
if (g_zbdc==NULL) g_zbdc = ZBUFF_createDCtx();
g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
break;
/* test functions */ /* test functions */
/* by convention, test functions can be added > 100 */ /* by convention, test functions can be added > 100 */
@ -341,53 +376,44 @@ size_t benchMem(void* src, size_t srcSize, U32 benchNb)
{ size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; } /* warming up memory */ { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; } /* warming up memory */
for (loopNb = 1; loopNb <= nbIterations; loopNb++) { U32 loopNb;
{ for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
clock_t const timeLoop = TIMELOOP_S * CLOCKS_PER_SEC;
clock_t clockStart;
U32 nbRounds;
size_t benchResult=0;
double averageTime; double averageTime;
int milliTime;
U32 nbRounds=0;
DISPLAY("%2i- %-30.30s : \r", loopNb, benchName); DISPLAY("%2i- %-30.30s : \r", loopNb, benchName);
milliTime = BMK_GetMilliStart(); clockStart = clock();
while(BMK_GetMilliStart() == milliTime); while (clock() == clockStart);
milliTime = BMK_GetMilliStart(); clockStart = clock();
while(BMK_GetMilliSpan(milliTime) < TIMELOOP) for (nbRounds=0; BMK_clockSpan(clockStart) < timeLoop; nbRounds++) {
{ benchResult = benchFunction(dstBuff, dstBuffSize, buff2, src, srcSize);
errorCode = benchFunction(dstBuff, dstBuffSize, buff2, src, srcSize); if (ZSTD_isError(benchResult)) { DISPLAY("ERROR ! %s() => %s !! \n", benchName, ZSTD_getErrorName(benchResult)); exit(1); }
if (ZSTD_isError(errorCode)) { DISPLAY("ERROR ! %s() => %s !! \n", benchName, ZSTD_getErrorName(errorCode)); exit(1); }
nbRounds++;
} }
milliTime = BMK_GetMilliSpan(milliTime); averageTime = (((double)BMK_clockSpan(clockStart)) / CLOCKS_PER_SEC) / nbRounds;
averageTime = (double)milliTime / nbRounds;
if (averageTime < bestTime) bestTime = averageTime; if (averageTime < bestTime) bestTime = averageTime;
DISPLAY("%2i- %-30.30s : %7.1f MB/s (%9u)\r", loopNb, benchName, (double)srcSize / bestTime / 1000., (U32)errorCode); DISPLAY("%2i- %-30.30s : %7.1f MB/s (%9u)\r", loopNb, benchName, (double)srcSize / (1 MB) / bestTime, (U32)benchResult);
} }}
DISPLAY("%2u\n", benchNb);
DISPLAY("%2u- %-30.30s : %7.1f MB/s (%9u)\n", benchNb, benchName, (double)srcSize / bestTime / 1000., (U32)errorCode);
_cleanOut: _cleanOut:
free(dstBuff); free(dstBuff);
free(buff2); free(buff2);
ZSTD_freeDCtx(g_dctxPtr);
return 0; return 0;
} }
int benchSample(U32 benchNb) static int benchSample(U32 benchNb)
{ {
char* origBuff; size_t const benchedSize = g_sampleSize;
size_t benchedSize = sampleSize;
const char* name = "Sample 10MiB"; const char* name = "Sample 10MiB";
/* Allocation */ /* Allocation */
origBuff = (char*) malloc((size_t)benchedSize); void* origBuff = malloc(benchedSize);
if(!origBuff) if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
{
DISPLAY("\nError: not enough memory!\n");
return 12;
}
/* Fill buffer */ /* Fill buffer */
RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0); RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0);
@ -405,58 +431,41 @@ int benchSample(U32 benchNb)
} }
int benchFiles(char** fileNamesTable, int nbFiles, U32 benchNb) static int benchFiles(const char** fileNamesTable, const int nbFiles, U32 benchNb)
{ {
int fileIdx=0;
/* Loop for each file */ /* Loop for each file */
while (fileIdx<nbFiles) int fileIdx;
{ for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
FILE* inFile; const char* inFileName = fileNamesTable[fileIdx];
char* inFileName; FILE* inFile = fopen( inFileName, "rb" );
U64 inFileSize; U64 inFileSize;
size_t benchedSize; size_t benchedSize;
size_t readSize; void* origBuff;
char* origBuff;
/* Check file existence */ /* Check file existence */
inFileName = fileNamesTable[fileIdx++]; if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
inFile = fopen( inFileName, "rb" );
if (inFile==NULL)
{
DISPLAY( "Pb opening %s\n", inFileName);
return 11;
}
/* Memory allocation & restrictions */ /* Memory allocation & restrictions */
inFileSize = BMK_GetFileSize(inFileName); inFileSize = BMK_GetFileSize(inFileName);
benchedSize = (size_t) BMK_findMaxMem(inFileSize*3) / 3; benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize; if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
if (benchedSize < inFileSize) if (benchedSize < inFileSize)
{ DISPLAY("Not enough memory for '%s' full size; testing %u MB only...\n", inFileName, (U32)(benchedSize>>20));
DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20));
}
/* Alloc */ /* Alloc */
origBuff = (char*) malloc((size_t)benchedSize); origBuff = malloc(benchedSize);
if(!origBuff) if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
{
DISPLAY("\nError: not enough memory!\n");
fclose(inFile);
return 12;
}
/* Fill input buffer */ /* Fill input buffer */
DISPLAY("Loading %s... \r", inFileName); DISPLAY("Loading %s... \r", inFileName);
readSize = fread(origBuff, 1, benchedSize, inFile);
fclose(inFile);
if(readSize != benchedSize)
{ {
DISPLAY("\nError: problem reading file '%s' !! \n", inFileName); size_t readSize = fread(origBuff, 1, benchedSize, inFile);
free(origBuff); fclose(inFile);
return 13; if (readSize != benchedSize) {
} DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
free(origBuff);
return 13;
} }
/* bench */ /* bench */
DISPLAY("\r%79s\r", ""); DISPLAY("\r%79s\r", "");
@ -465,13 +474,15 @@ int benchFiles(char** fileNamesTable, int nbFiles, U32 benchNb)
benchMem(origBuff, benchedSize, benchNb); benchMem(origBuff, benchedSize, benchNb);
else else
for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb); for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
free(origBuff);
} }
return 0; return 0;
} }
int usage(char* exename) static int usage(const char* exename)
{ {
DISPLAY( "Usage :\n"); DISPLAY( "Usage :\n");
DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename); DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
@ -480,8 +491,9 @@ int usage(char* exename)
return 0; return 0;
} }
int usage_advanced(void) static int usage_advanced(const char* exename)
{ {
usage(exename);
DISPLAY( "\nAdvanced options :\n"); DISPLAY( "\nAdvanced options :\n");
DISPLAY( " -b# : test only function # \n"); DISPLAY( " -b# : test only function # \n");
DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS); DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
@ -489,45 +501,38 @@ int usage_advanced(void)
return 0; return 0;
} }
int badusage(char* exename) static int badusage(const char* exename)
{ {
DISPLAY("Wrong parameters\n"); DISPLAY("Wrong parameters\n");
usage(exename); usage(exename);
return 0; return 1;
} }
int main(int argc, char** argv) int main(int argc, const char** argv)
{ {
int i, int i, filenamesStart=0, result;
filenamesStart=0, const char* exename = argv[0];
result; const char* input_filename = NULL;
char* exename=argv[0];
char* input_filename=0;
U32 benchNb = 0, main_pause = 0; U32 benchNb = 0, main_pause = 0;
// Welcome message
DISPLAY(WELCOME_MESSAGE); DISPLAY(WELCOME_MESSAGE);
if (argc<1) return badusage(exename);
if (argc<1) { badusage(exename); return 1; } for(i=1; i<argc; i++) {
const char* argument = argv[i];
if(!argument) continue; /* Protection if argument empty */
for(i=1; i<argc; i++) /* Commands (note : aggregated commands are allowed) */
{ if (argument[0]=='-') {
char* argument = argv[i];
if(!argument) continue; // Protection if argument empty while (argument[1]!=0) {
argument++;
// Decode command (note : aggregated commands are allowed)
if (argument[0]=='-')
{
while (argument[1]!=0)
{
argument ++;
switch(argument[0]) switch(argument[0])
{ {
/* Display help on usage */ /* Display help on usage */
case 'h' : case 'h' :
case 'H': usage(exename); usage_advanced(); return 0; case 'H': return usage_advanced(exename);
/* Pause at the end (hidden option) */ /* Pause at the end (hidden option) */
case 'p': main_pause = 1; break; case 'p': main_pause = 1; break;
@ -535,8 +540,7 @@ int main(int argc, char** argv)
/* Select specific algorithm to bench */ /* Select specific algorithm to bench */
case 'b': case 'b':
benchNb = 0; benchNb = 0;
while ((argument[1]>= '0') && (argument[1]<= '9')) while ((argument[1]>= '0') && (argument[1]<= '9')) {
{
benchNb *= 10; benchNb *= 10;
benchNb += argument[1] - '0'; benchNb += argument[1] - '0';
argument++; argument++;
@ -545,20 +549,17 @@ int main(int argc, char** argv)
/* Modify Nb Iterations */ /* Modify Nb Iterations */
case 'i': case 'i':
if ((argument[1] >='0') && (argument[1] <='9')) if ((argument[1] >='0') && (argument[1] <='9')) {
{
int iters = argument[1] - '0'; int iters = argument[1] - '0';
BMK_SetNbIterations(iters); BMK_SetNbIterations(iters);
argument++; argument++;
} }
break; break;
/* Select specific algorithm to bench */ /* Select compressibility of synthetic sample */
case 'P': case 'P':
{ { U32 proba32 = 0;
U32 proba32 = 0; while ((argument[1]>= '0') && (argument[1]<= '9')) {
while ((argument[1]>= '0') && (argument[1]<= '9'))
{
proba32 *= 10; proba32 *= 10;
proba32 += argument[1] - '0'; proba32 += argument[1] - '0';
argument++; argument++;
@ -568,7 +569,7 @@ int main(int argc, char** argv)
break; break;
/* Unknown command */ /* Unknown command */
default : badusage(exename); return 1; default : return badusage(exename);
} }
} }
continue; continue;
@ -578,9 +579,10 @@ int main(int argc, char** argv)
if (!input_filename) { input_filename=argument; filenamesStart=i; continue; } if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
} }
if (filenamesStart==0) if (filenamesStart==0) /* no input file */
result = benchSample(benchNb); result = benchSample(benchNb);
else result = benchFiles(argv+filenamesStart, argc-filenamesStart, benchNb); else
result = benchFiles(argv+filenamesStart, argc-filenamesStart, benchNb);
if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; } if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }

View File

@ -1,6 +1,6 @@
/* /*
Fuzzer test tool for zstd Fuzzer test tool for zstd
Copyright (C) Yann Collet 2014-2105 Copyright (C) Yann Collet 2014-2016
GPL v2 License GPL v2 License
@ -19,11 +19,10 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at : You can contact the author at :
- ZSTD source repository : https://github.com/Cyan4973/zstd - ZSTD homepage : http://www.zstd.net
- ZSTD public forum : https://groups.google.com/forum/#!forum/lz4c
*/ */
/************************************** /*-************************************
* Compiler specific * Compiler specific
**************************************/ **************************************/
#ifdef _MSC_VER /* Visual Studio */ #ifdef _MSC_VER /* Visual Studio */
@ -32,28 +31,23 @@
# pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */ # pragma warning(disable : 4146) /* disable: C4146: minus unsigned expression */
#endif #endif
#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wmissing-braces" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */
# pragma GCC diagnostic ignored "-Wmissing-field-initializers" /* GCC bug 53119 : doesn't accept { 0 } as initializer (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119) */
#endif
/*-************************************
/**************************************
* Includes * Includes
**************************************/ **************************************/
#include <stdlib.h> /* free */ #include <stdlib.h> /* free */
#include <stdio.h> /* fgets, sscanf */ #include <stdio.h> /* fgets, sscanf */
#include <sys/timeb.h> /* timeb */ #include <sys/timeb.h> /* timeb */
#include <string.h> /* strcmp */ #include <string.h> /* strcmp */
#include <time.h> /* clock_t */
#include "zstd_static.h" #include "zstd_static.h"
#include "datagen.h" /* RDG_genBuffer */ #include "datagen.h" /* RDG_genBuffer */
#include "xxhash.h" /* XXH64 */ #include "xxhash.h" /* XXH64 */
#include "mem.h" #include "mem.h"
/************************************** /*-************************************
Constants * Constants
**************************************/ **************************************/
#ifndef ZSTD_VERSION #ifndef ZSTD_VERSION
# define ZSTD_VERSION "" # define ZSTD_VERSION ""
@ -63,15 +57,12 @@
#define MB *(1U<<20) #define MB *(1U<<20)
#define GB *(1U<<30) #define GB *(1U<<30)
static const size_t COMPRESSIBLE_NOISE_LENGTH = 10 MB; /* capital, used to be a macro */
static const U32 FUZ_compressibility_default = 50;
static const U32 nbTestsDefault = 30000; static const U32 nbTestsDefault = 30000;
#define COMPRESSIBLE_NOISE_LENGTH (10 MB)
#define FUZ_COMPRESSIBILITY_DEFAULT 50
static const U32 prime1 = 2654435761U;
static const U32 prime2 = 2246822519U;
/*-************************************
/**************************************
* Display Macros * Display Macros
**************************************/ **************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
@ -79,44 +70,30 @@ static const U32 prime2 = 2246822519U;
static U32 g_displayLevel = 2; static U32 g_displayLevel = 2;
#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
if ((FUZ_GetMilliSpan(g_displayTime) > g_refreshRate) || (g_displayLevel>=4)) \ if ((FUZ_clockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
{ g_displayTime = FUZ_GetMilliStart(); DISPLAY(__VA_ARGS__); \ { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \
if (g_displayLevel>=4) fflush(stdout); } } if (g_displayLevel>=4) fflush(stdout); } }
static const U32 g_refreshRate = 150; static const clock_t g_refreshRate = CLOCKS_PER_SEC * 150 / 1000;
static U32 g_displayTime = 0; static clock_t g_displayClock = 0;
static U32 g_testTime = 0;
/********************************************************* /*-*******************************************************
* Fuzzer functions * Fuzzer functions
*********************************************************/ *********************************************************/
#define MIN(a,b) ((a)<(b)?(a):(b)) #define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
static U32 FUZ_GetMilliStart(void) static clock_t FUZ_clockSpan(clock_t cStart)
{ {
struct timeb tb; return clock() - cStart; /* works even when overflow; max span ~ 30mn */
U32 nCount;
ftime( &tb );
nCount = (U32) (((tb.time & 0xFFFFF) * 1000) + tb.millitm);
return nCount;
}
static U32 FUZ_GetMilliSpan(U32 nTimeStart)
{
U32 nCurrent = FUZ_GetMilliStart();
U32 nSpan = nCurrent - nTimeStart;
if (nTimeStart > nCurrent)
nSpan += 0x100000 * 1000;
return nSpan;
} }
# define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
unsigned int FUZ_rand(unsigned int* src) unsigned int FUZ_rand(unsigned int* src)
{ {
static const U32 prime1 = 2654435761U;
static const U32 prime2 = 2246822519U;
U32 rand32 = *src; U32 rand32 = *src;
rand32 *= prime1; rand32 *= prime1;
rand32 += prime2; rand32 += prime2;
@ -130,8 +107,7 @@ static unsigned FUZ_highbit32(U32 v32)
{ {
unsigned nbBits = 0; unsigned nbBits = 0;
if (v32==0) return 0; if (v32==0) return 0;
while (v32) while (v32) {
{
v32 >>= 1; v32 >>= 1;
nbBits ++; nbBits ++;
} }
@ -153,8 +129,7 @@ static int basicUnitTests(U32 seed, double compressibility)
CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); CNBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH);
compressedBuffer = malloc(ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH)); compressedBuffer = malloc(ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH));
decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH); decodedBuffer = malloc(COMPRESSIBLE_NOISE_LENGTH);
if (!CNBuffer || !compressedBuffer || !decodedBuffer) if (!CNBuffer || !compressedBuffer || !decodedBuffer) {
{
DISPLAY("Not enough memory, aborting\n"); DISPLAY("Not enough memory, aborting\n");
testResult = 1; testResult = 1;
goto _end; goto _end;
@ -162,22 +137,21 @@ static int basicUnitTests(U32 seed, double compressibility)
RDG_genBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, 0., randState); RDG_genBuffer(CNBuffer, COMPRESSIBLE_NOISE_LENGTH, compressibility, 0., randState);
/* Basic tests */ /* Basic tests */
DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); DISPLAYLEVEL(4, "test%3i : compress %u bytes : ", testNb++, (U32)COMPRESSIBLE_NOISE_LENGTH);
result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1); result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), CNBuffer, COMPRESSIBLE_NOISE_LENGTH, 1);
if (ZSTD_isError(result)) goto _output_error; if (ZSTD_isError(result)) goto _output_error;
cSize = result; cSize = result;
DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH); DISPLAYLEVEL(4, "test%3i : decompress %u bytes : ", testNb++, (U32)COMPRESSIBLE_NOISE_LENGTH);
result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize); result = ZSTD_decompress(decodedBuffer, COMPRESSIBLE_NOISE_LENGTH, compressedBuffer, cSize);
if (ZSTD_isError(result)) goto _output_error; if (ZSTD_isError(result)) goto _output_error;
if (result != COMPRESSIBLE_NOISE_LENGTH) goto _output_error;
DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "OK \n");
{ { size_t i;
size_t i;
DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
for (i=0; i<COMPRESSIBLE_NOISE_LENGTH; i++) for (i=0; i<COMPRESSIBLE_NOISE_LENGTH; i++) {
{
if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
} }
DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "OK \n");
@ -195,19 +169,22 @@ static int basicUnitTests(U32 seed, double compressibility)
if (result != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error; if (result != (size_t)-ZSTD_error_srcSize_wrong) goto _output_error;
DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "OK \n");
/* Dictionary and Duplication tests */ /* Dictionary and CCtx Duplication tests */
{ { ZSTD_CCtx* ctxOrig = ZSTD_createCCtx();
ZSTD_CCtx* ctxOrig = ZSTD_createCCtx();
ZSTD_CCtx* ctxDuplicated = ZSTD_createCCtx(); ZSTD_CCtx* ctxDuplicated = ZSTD_createCCtx();
ZSTD_DCtx* dctx = ZSTD_createDCtx(); ZSTD_DCtx* dctx = ZSTD_createDCtx();
const size_t dictSize = 500; size_t const dictSize = 500;
size_t cSizeOrig;
DISPLAYLEVEL(4, "test%3i : copy context too soon : ", testNb++);
{ size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig);
if (!ZSTD_isError(copyResult)) goto _output_error; } /* error should be detected */
DISPLAYLEVEL(4, "OK \n");
DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++); DISPLAYLEVEL(4, "test%3i : load dictionary into context : ", testNb++);
result = ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2); { size_t const initResult = ZSTD_compressBegin_usingDict(ctxOrig, CNBuffer, dictSize, 2);
if (ZSTD_isError(result)) goto _output_error; if (ZSTD_isError(initResult)) goto _output_error; }
result = ZSTD_copyCCtx(ctxDuplicated, ctxOrig); { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig);
if (ZSTD_isError(result)) goto _output_error; if (ZSTD_isError(copyResult)) goto _output_error; }
DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "OK \n");
DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++); DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++);
@ -227,20 +204,19 @@ static int basicUnitTests(U32 seed, double compressibility)
CNBuffer, dictSize); CNBuffer, dictSize);
if (ZSTD_isError(result)) goto _output_error; if (ZSTD_isError(result)) goto _output_error;
if (result != COMPRESSIBLE_NOISE_LENGTH - dictSize) goto _output_error; if (result != COMPRESSIBLE_NOISE_LENGTH - dictSize) goto _output_error;
ZSTD_freeCCtx(ctxOrig); /* if ctxOrig is read, will produce segfault */
DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "OK \n");
DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++); DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++);
cSizeOrig = cSize; { size_t const cSizeOrig = cSize;
cSize = 0; cSize = 0;
result = ZSTD_compressContinue(ctxDuplicated, compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), (const char*)CNBuffer + dictSize, COMPRESSIBLE_NOISE_LENGTH - dictSize); result = ZSTD_compressContinue(ctxDuplicated, compressedBuffer, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH), (const char*)CNBuffer + dictSize, COMPRESSIBLE_NOISE_LENGTH - dictSize);
if (ZSTD_isError(result)) goto _output_error; if (ZSTD_isError(result)) goto _output_error;
cSize += result; cSize += result;
result = ZSTD_compressEnd(ctxDuplicated, (char*)compressedBuffer+cSize, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH)-cSize); result = ZSTD_compressEnd(ctxDuplicated, (char*)compressedBuffer+cSize, ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH)-cSize);
if (ZSTD_isError(result)) goto _output_error; if (ZSTD_isError(result)) goto _output_error;
cSize += result; cSize += result;
if (cSize != cSizeOrig) goto _output_error; /* should be identical == have same size */ if (cSize != cSizeOrig) goto _output_error; /* should be identical == have same size */
ZSTD_freeCCtx(ctxDuplicated); }
DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100); DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++); DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++);
@ -250,8 +226,30 @@ static int basicUnitTests(U32 seed, double compressibility)
CNBuffer, dictSize); CNBuffer, dictSize);
if (ZSTD_isError(result)) goto _output_error; if (ZSTD_isError(result)) goto _output_error;
if (result != COMPRESSIBLE_NOISE_LENGTH - dictSize) goto _output_error; if (result != COMPRESSIBLE_NOISE_LENGTH - dictSize) goto _output_error;
ZSTD_freeDCtx(dctx);
DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "OK \n");
DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++);
{ size_t const testSize = COMPRESSIBLE_NOISE_LENGTH / 3;
{ ZSTD_parameters p;
p.cParams = ZSTD_getCParams(2, testSize, dictSize);
p.fParams.contentSizeFlag = 1;
{ size_t const initResult = ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1);
if (ZSTD_isError(initResult)) goto _output_error;
} }
{ size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig);
if (ZSTD_isError(copyResult)) goto _output_error; }
cSize = ZSTD_compressContinue(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize), (const char*)CNBuffer + dictSize, COMPRESSIBLE_NOISE_LENGTH - dictSize);
if (ZSTD_isError(cSize)) goto _output_error;
{ ZSTD_frameParams fp;
size_t const gfpResult = ZSTD_getFrameParams(&fp, compressedBuffer, cSize);
if (gfpResult!=0) goto _output_error;
if ((fp.frameContentSize != testSize) && (fp.frameContentSize != 0)) goto _output_error;
} }
DISPLAYLEVEL(4, "OK \n");
ZSTD_freeCCtx(ctxOrig);
ZSTD_freeCCtx(ctxDuplicated);
ZSTD_freeDCtx(dctx);
} }
/* Decompression defense tests */ /* Decompression defense tests */
@ -268,8 +266,7 @@ static int basicUnitTests(U32 seed, double compressibility)
DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "OK \n");
/* block API tests */ /* block API tests */
{ { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
ZSTD_DCtx* const dctx = ZSTD_createDCtx(); ZSTD_DCtx* const dctx = ZSTD_createDCtx();
const size_t blockSize = 100 KB; const size_t blockSize = 100 KB;
const size_t dictSize = 16 KB; const size_t dictSize = 16 KB;
@ -311,8 +308,7 @@ static int basicUnitTests(U32 seed, double compressibility)
} }
/* long rle test */ /* long rle test */
{ { size_t sampleSize = 0;
size_t sampleSize = 0;
DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++); DISPLAYLEVEL(4, "test%3i : Long RLE test : ", testNb++);
RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., randState); RDG_genBuffer(CNBuffer, sampleSize, compressibility, 0., randState);
memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1); memset((char*)CNBuffer+sampleSize, 'B', 256 KB - 1);
@ -327,6 +323,57 @@ static int basicUnitTests(U32 seed, double compressibility)
DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "OK \n");
} }
/* All zeroes test (#137 verif) */
#define ZEROESLENGTH 100
DISPLAYLEVEL(4, "test%3i : compress %u zeroes : ", testNb++, ZEROESLENGTH);
memset(CNBuffer, 0, ZEROESLENGTH);
result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(ZEROESLENGTH), CNBuffer, ZEROESLENGTH, 1);
if (ZSTD_isError(result)) goto _output_error;
cSize = result;
DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/ZEROESLENGTH*100);
DISPLAYLEVEL(4, "test%3i : decompress %u zeroes : ", testNb++, ZEROESLENGTH);
result = ZSTD_decompress(decodedBuffer, ZEROESLENGTH, compressedBuffer, cSize);
if (ZSTD_isError(result)) goto _output_error;
if (result != ZEROESLENGTH) goto _output_error;
DISPLAYLEVEL(4, "OK \n");
/* nbSeq limit test */
#define _3BYTESTESTLENGTH 131000
#define NB3BYTESSEQLOG 9
#define NB3BYTESSEQ (1 << NB3BYTESSEQLOG)
#define NB3BYTESSEQMASK (NB3BYTESSEQ-1)
/* creates a buffer full of 3-bytes sequences */
{ BYTE _3BytesSeqs[NB3BYTESSEQ][3];
U32 rSeed = 1;
/* create batch of 3-bytes sequences */
{ int i; for (i=0; i < NB3BYTESSEQ; i++) {
_3BytesSeqs[i][0] = (BYTE)(FUZ_rand(&rSeed) & 255);
_3BytesSeqs[i][1] = (BYTE)(FUZ_rand(&rSeed) & 255);
_3BytesSeqs[i][2] = (BYTE)(FUZ_rand(&rSeed) & 255);
}}
/* randomly fills CNBuffer with prepared 3-bytes sequences */
{ int i; for (i=0; i < _3BYTESTESTLENGTH; ) { /* note : CNBuffer size > _3BYTESTESTLENGTH+3 */
U32 id = FUZ_rand(&rSeed) & NB3BYTESSEQMASK;
((BYTE*)CNBuffer)[i+0] = _3BytesSeqs[id][0];
((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1];
((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2];
i += 3;
} }}
DISPLAYLEVEL(4, "test%3i : compress lots 3-bytes sequences : ", testNb++);
result = ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH), CNBuffer, _3BYTESTESTLENGTH, 19);
if (ZSTD_isError(result)) goto _output_error;
cSize = result;
DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/_3BYTESTESTLENGTH*100);
DISPLAYLEVEL(4, "test%3i : decompress lots 3-bytes sequence : ", testNb++);
result = ZSTD_decompress(decodedBuffer, _3BYTESTESTLENGTH, compressedBuffer, cSize);
if (ZSTD_isError(result)) goto _output_error;
if (result != _3BYTESTESTLENGTH) goto _output_error;
DISPLAYLEVEL(4, "OK \n");
_end: _end:
free(CNBuffer); free(CNBuffer);
free(compressedBuffer); free(compressedBuffer);
@ -345,21 +392,32 @@ static size_t findDiff(const void* buf1, const void* buf2, size_t max)
const BYTE* b1 = (const BYTE*)buf1; const BYTE* b1 = (const BYTE*)buf1;
const BYTE* b2 = (const BYTE*)buf2; const BYTE* b2 = (const BYTE*)buf2;
size_t i; size_t i;
for (i=0; i<max; i++) for (i=0; i<max; i++) {
{
if (b1[i] != b2[i]) break; if (b1[i] != b2[i]) break;
} }
return i; return i;
} }
# define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
static const U32 maxSrcLog = 23; static size_t FUZ_rLogLength(U32* seed, U32 logLength)
static const U32 maxSampleLog = 22;
int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
{ {
size_t const lengthMask = ((size_t)1 << logLength) - 1;
return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
}
static size_t FUZ_randomLength(U32* seed, U32 maxLog)
{
U32 const logLength = FUZ_rand(seed) % maxLog;
return FUZ_rLogLength(seed, logLength);
}
#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxDurationS, double compressibility)
{
static const U32 maxSrcLog = 23;
static const U32 maxSampleLog = 22;
BYTE* cNoiseBuffer[5]; BYTE* cNoiseBuffer[5];
BYTE* srcBuffer; BYTE* srcBuffer;
BYTE* cBuffer; BYTE* cBuffer;
@ -374,7 +432,8 @@ int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibilit
ZSTD_CCtx* refCtx; ZSTD_CCtx* refCtx;
ZSTD_CCtx* ctx; ZSTD_CCtx* ctx;
ZSTD_DCtx* dctx; ZSTD_DCtx* dctx;
U32 startTime = FUZ_GetMilliStart(); clock_t startClock = clock();
clock_t const maxClockSpan = maxDurationS * CLOCKS_PER_SEC;
/* allocation */ /* allocation */
refCtx = ZSTD_createCCtx(); refCtx = ZSTD_createCCtx();
@ -401,49 +460,44 @@ int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibilit
srcBuffer = cNoiseBuffer[2]; srcBuffer = cNoiseBuffer[2];
/* catch up testNb */ /* catch up testNb */
for (testNb=1; testNb < startTest; testNb++) for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed);
FUZ_rand(&coreSeed);
/* test loop */ /* main test loop */
for ( ; (testNb <= nbTests) || (FUZ_GetMilliSpan(startTime) < g_testTime); testNb++ ) for ( ; (testNb <= nbTests) || (FUZ_clockSpan(startClock) < maxClockSpan); testNb++ ) {
{
size_t sampleSize, sampleStart, maxTestSize, totalTestSize; size_t sampleSize, sampleStart, maxTestSize, totalTestSize;
size_t cSize, dSize, dSupSize, errorCode, totalCSize, totalGenSize; size_t cSize, dSize, totalCSize, totalGenSize;
U32 sampleSizeLog, buffNb, cLevelMod, nbChunks, n; U32 sampleSizeLog, nbChunks, n;
XXH64_CREATESTATE_STATIC(xxh64); XXH64_CREATESTATE_STATIC(xxh64);
U64 crcOrig, crcDest; U64 crcOrig;
int cLevel;
BYTE* sampleBuffer; BYTE* sampleBuffer;
const BYTE* dict; const BYTE* dict;
size_t dictSize; size_t dictSize;
/* init */ /* notification */
if (nbTests >= testNb) if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
{ DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
else { DISPLAYUPDATE(2, "\r%6u ", testNb); } else { DISPLAYUPDATE(2, "\r%6u ", testNb); }
FUZ_rand(&coreSeed); FUZ_rand(&coreSeed);
lseed = coreSeed ^ prime1; { U32 const prime1 = 2654435761U; lseed = coreSeed ^ prime1; }
buffNb = FUZ_rand(&lseed) & 127;
if (buffNb & 7) buffNb=2; /* srcBuffer selection [0-4] */
else { U32 buffNb = FUZ_rand(&lseed) & 0x7F;
{ if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
buffNb >>= 3; else {
if (buffNb & 7) buffNb >>= 3;
{ if (buffNb & 7) {
const U32 tnb[2] = { 1, 3 }; const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
buffNb = tnb[buffNb >> 3]; buffNb = tnb[buffNb >> 3];
} } else {
else const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
{ buffNb = tnb[buffNb >> 3];
const U32 tnb[2] = { 0, 4 }; } }
buffNb = tnb[buffNb >> 3]; srcBuffer = cNoiseBuffer[buffNb];
}
} }
srcBuffer = cNoiseBuffer[buffNb];
/* select src segment */
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog;
sampleSize = (size_t)1 << sampleSizeLog; sampleSize = FUZ_rLogLength(&lseed, sampleSizeLog);
sampleSize += FUZ_rand(&lseed) & (sampleSize-1);
sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
/* create sample buffer (to catch read error with valgrind & sanitizers) */ /* create sample buffer (to catch read error with valgrind & sanitizers) */
@ -452,160 +506,151 @@ int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibilit
memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize); memcpy(sampleBuffer, srcBuffer + sampleStart, sampleSize);
crcOrig = XXH64(sampleBuffer, sampleSize, 0); crcOrig = XXH64(sampleBuffer, sampleSize, 0);
/* compression test */ /* compression tests */
//cLevelMod = MAX(1, 38 - (int)(MAX(9, sampleSizeLog) * 2)); /* high levels only for small samples, for manageable speed */ { int const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (sampleSizeLog/3))) + 1;
cLevelMod = MIN( ZSTD_maxCLevel(), (U32)MAX(1, 55 - 3*(int)sampleSizeLog) ); /* high levels only for small samples, for manageable speed */ cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel);
cLevel = (FUZ_rand(&lseed) % cLevelMod) +1; CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed");
cSize = ZSTD_compressCCtx(ctx, cBuffer, cBufferSize, sampleBuffer, sampleSize, cLevel);
CHECK(ZSTD_isError(cSize), "ZSTD_compressCCtx failed");
/* compression failure test : too small dest buffer */ /* compression failure test : too small dest buffer */
if (cSize > 3) if (cSize > 3) {
{ const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ const size_t tooSmallSize = cSize - missing;
const size_t tooSmallSize = cSize - missing; const U32 endMark = 0x4DC2B1A9;
static const U32 endMark = 0x4DC2B1A9; memcpy(dstBuffer+tooSmallSize, &endMark, 4);
U32 endCheck; { size_t const errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel);
memcpy(dstBuffer+tooSmallSize, &endMark, 4); CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); }
errorCode = ZSTD_compressCCtx(ctx, dstBuffer, tooSmallSize, sampleBuffer, sampleSize, cLevel); { U32 endCheck; memcpy(&endCheck, dstBuffer+tooSmallSize, 4);
CHECK(!ZSTD_isError(errorCode), "ZSTD_compressCCtx should have failed ! (buffer too small : %u < %u)", (U32)tooSmallSize, (U32)cSize); CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow"); }
memcpy(&endCheck, dstBuffer+tooSmallSize, 4); }
CHECK(endCheck != endMark, "ZSTD_compressCCtx : dst buffer overflow");
} }
/* successfull decompression tests*/
dSupSize = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1; /* frame header decompression test */
dSize = ZSTD_decompress(dstBuffer, sampleSize + dSupSize, cBuffer, cSize); { ZSTD_frameParams dParams;
CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (U32)sampleSize, (U32)cSize); size_t const check = ZSTD_getFrameParams(&dParams, cBuffer, cSize);
crcDest = XXH64(dstBuffer, sampleSize, 0); CHECK(ZSTD_isError(check), "Frame Parameters extraction failed");
CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize); CHECK(dParams.frameContentSize != sampleSize, "Frame content size incorrect");
}
/* successful decompression test */
{ size_t const margin = (FUZ_rand(&lseed) & 1) ? 0 : (FUZ_rand(&lseed) & 31) + 1;
dSize = ZSTD_decompress(dstBuffer, sampleSize + margin, cBuffer, cSize);
CHECK(dSize != sampleSize, "ZSTD_decompress failed (%s) (srcSize : %u ; cSize : %u)", ZSTD_getErrorName(dSize), (U32)sampleSize, (U32)cSize);
{ U64 const crcDest = XXH64(dstBuffer, sampleSize, 0);
CHECK(crcOrig != crcDest, "decompression result corrupted (pos %u / %u)", (U32)findDiff(sampleBuffer, dstBuffer, sampleSize), (U32)sampleSize);
} }
free(sampleBuffer); /* no longer useful after this point */ free(sampleBuffer); /* no longer useful after this point */
/* truncated src decompression test */ /* truncated src decompression test */
{ { size_t const missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
const size_t missing = (FUZ_rand(&lseed) % (cSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ size_t const tooSmallSize = cSize - missing;
const size_t tooSmallSize = cSize - missing;
void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch overflows */ void* cBufferTooSmall = malloc(tooSmallSize); /* valgrind will catch overflows */
CHECK(cBufferTooSmall == NULL, "not enough memory !"); CHECK(cBufferTooSmall == NULL, "not enough memory !");
memcpy(cBufferTooSmall, cBuffer, tooSmallSize); memcpy(cBufferTooSmall, cBuffer, tooSmallSize);
errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize); { size_t const errorCode = ZSTD_decompress(dstBuffer, dstBufferSize, cBufferTooSmall, tooSmallSize);
CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed ! (truncated src buffer)"); }
free(cBufferTooSmall); free(cBufferTooSmall);
} }
/* too small dst decompression test */ /* too small dst decompression test */
if (sampleSize > 3) if (sampleSize > 3) {
{ size_t const missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */
const size_t missing = (FUZ_rand(&lseed) % (sampleSize-2)) + 1; /* no problem, as cSize > 4 (frameHeaderSizer) */ size_t const tooSmallSize = sampleSize - missing;
const size_t tooSmallSize = sampleSize - missing;
static const BYTE token = 0xA9; static const BYTE token = 0xA9;
dstBuffer[tooSmallSize] = token; dstBuffer[tooSmallSize] = token;
errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize); { size_t const errorCode = ZSTD_decompress(dstBuffer, tooSmallSize, cBuffer, cSize);
CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); CHECK(!ZSTD_isError(errorCode), "ZSTD_decompress should have failed : %u > %u (dst buffer too small)", (U32)errorCode, (U32)tooSmallSize); }
CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow"); CHECK(dstBuffer[tooSmallSize] != token, "ZSTD_decompress : dst buffer overflow");
} }
/* noisy src decompression test */ /* noisy src decompression test */
if (cSize > 6) if (cSize > 6) {
{ /* insert noise into src */
const U32 maxNbBits = FUZ_highbit32((U32)(cSize-4)); { U32 const maxNbBits = FUZ_highbit32((U32)(cSize-4));
size_t pos = 4; /* preserve magic number (too easy to detect) */ size_t pos = 4; /* preserve magic number (too easy to detect) */
U32 nbBits = FUZ_rand(&lseed) % maxNbBits; for (;;) {
size_t mask = (1<<nbBits) - 1; /* keep some original src */
size_t skipLength = FUZ_rand(&lseed) & mask; { U32 const nbBits = FUZ_rand(&lseed) % maxNbBits;
pos += skipLength; size_t const mask = (1<<nbBits) - 1;
size_t const skipLength = FUZ_rand(&lseed) & mask;
while (pos < cSize) pos += skipLength;
{ }
/* add noise */ if (pos <= cSize) break;
size_t noiseStart, noiseLength; /* add noise */
nbBits = FUZ_rand(&lseed) % maxNbBits; { U32 nbBits = FUZ_rand(&lseed) % maxNbBits;
if (nbBits>0) nbBits--; size_t mask, noiseStart, noiseLength;
mask = (1<<nbBits) - 1; if (nbBits>0) nbBits--;
noiseLength = (FUZ_rand(&lseed) & mask) + 1; mask = (1<<nbBits) - 1;
if ( pos+noiseLength > cSize ) noiseLength = cSize-pos; noiseLength = (FUZ_rand(&lseed) & mask) + 1;
noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength); if ( pos+noiseLength > cSize ) noiseLength = cSize-pos;
memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength); noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseLength);
pos += noiseLength; memcpy(cBuffer + pos, srcBuffer + noiseStart, noiseLength);
pos += noiseLength;
/* keep some original src */ } } }
nbBits = FUZ_rand(&lseed) % maxNbBits;
mask = (1<<nbBits) - 1;
skipLength = FUZ_rand(&lseed) & mask;
pos += skipLength;
}
/* decompress noisy source */ /* decompress noisy source */
{ { U32 const endMark = 0xA9B1C3D6;
U32 noiseSrc = FUZ_rand(&lseed) % 5;
const U32 endMark = 0xA9B1C3D6;
U32 endCheck;
srcBuffer = cNoiseBuffer[noiseSrc];
memcpy(dstBuffer+sampleSize, &endMark, 4); memcpy(dstBuffer+sampleSize, &endMark, 4);
errorCode = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize); { size_t const decompressResult = ZSTD_decompress(dstBuffer, sampleSize, cBuffer, cSize);
/* result *may* be an unlikely success, but even then, it must strictly respect dest buffer boundaries */ /* result *may* be an unlikely success, but even then, it must strictly respect dst buffer boundaries */
CHECK((!ZSTD_isError(errorCode)) && (errorCode>sampleSize), CHECK((!ZSTD_isError(decompressResult)) && (decompressResult>sampleSize),
"ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)errorCode, (U32)sampleSize); "ZSTD_decompress on noisy src : result is too large : %u > %u (dst buffer)", (U32)decompressResult, (U32)sampleSize);
memcpy(&endCheck, dstBuffer+sampleSize, 4); }
CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow"); { U32 endCheck; memcpy(&endCheck, dstBuffer+sampleSize, 4);
} CHECK(endMark!=endCheck, "ZSTD_decompress on noisy src : dst buffer overflow");
} } } } /* noisy src decompression test */
/* Streaming compression of scattered segments test */ /*===== Streaming compression test, scattered segments and dictionary =====*/
{ U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
int const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
maxTestSize = FUZ_rLogLength(&lseed, testLog);
if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1;
sampleSize = FUZ_randomLength(&lseed, maxSampleLog);
sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
dict = srcBuffer + sampleStart;
dictSize = sampleSize;
{ size_t const errorCode = ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, cLevel);
CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode)); }
{ size_t const errorCode = ZSTD_copyCCtx(ctx, refCtx);
CHECK (ZSTD_isError(errorCode), "ZSTD_copyCCtx error : %s", ZSTD_getErrorName(errorCode)); }
}
XXH64_reset(xxh64, 0); XXH64_reset(xxh64, 0);
nbChunks = (FUZ_rand(&lseed) & 127) + 2; nbChunks = (FUZ_rand(&lseed) & 127) + 2;
sampleSizeLog = FUZ_rand(&lseed) % maxSrcLog; for (totalTestSize=0, cSize=0, n=0 ; n<nbChunks ; n++) {
maxTestSize = (size_t)1 << sampleSizeLog;
maxTestSize += FUZ_rand(&lseed) & (maxTestSize-1);
if (maxTestSize >= dstBufferSize) maxTestSize = dstBufferSize-1;
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog;
sampleSize = (size_t)1 << sampleSizeLog;
sampleSize += FUZ_rand(&lseed) & (sampleSize-1);
sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
dict = srcBuffer + sampleStart;
dictSize = sampleSize;
errorCode = ZSTD_compressBegin_usingDict(refCtx, dict, dictSize, (FUZ_rand(&lseed) % (20 - (sampleSizeLog/3))) + 1);
CHECK (ZSTD_isError(errorCode), "ZSTD_compressBegin_usingDict error : %s", ZSTD_getErrorName(errorCode));
errorCode = ZSTD_copyCCtx(ctx, refCtx);
CHECK (ZSTD_isError(errorCode), "ZSTD_copyCCtx error : %s", ZSTD_getErrorName(errorCode));
totalTestSize = 0; cSize = 0;
for (n=0; n<nbChunks; n++)
{
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog;
sampleSize = (size_t)1 << sampleSizeLog; sampleSize = (size_t)1 << sampleSizeLog;
sampleSize += FUZ_rand(&lseed) & (sampleSize-1); sampleSize += FUZ_rand(&lseed) & (sampleSize-1);
sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize); sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
if (cBufferSize-cSize < ZSTD_compressBound(sampleSize)) if (cBufferSize-cSize < ZSTD_compressBound(sampleSize)) break; /* avoid invalid dstBufferTooSmall */
/* avoid invalid dstBufferTooSmall */
break;
if (totalTestSize+sampleSize > maxTestSize) break; if (totalTestSize+sampleSize > maxTestSize) break;
errorCode = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+sampleStart, sampleSize); { size_t const compressResult = ZSTD_compressContinue(ctx, cBuffer+cSize, cBufferSize-cSize, srcBuffer+sampleStart, sampleSize);
CHECK (ZSTD_isError(errorCode), "multi-segments compression error : %s", ZSTD_getErrorName(errorCode)); CHECK (ZSTD_isError(compressResult), "multi-segments compression error : %s", ZSTD_getErrorName(compressResult));
cSize += errorCode; cSize += compressResult;
}
XXH64_update(xxh64, srcBuffer+sampleStart, sampleSize); XXH64_update(xxh64, srcBuffer+sampleStart, sampleSize);
memcpy(mirrorBuffer + totalTestSize, srcBuffer+sampleStart, sampleSize); memcpy(mirrorBuffer + totalTestSize, srcBuffer+sampleStart, sampleSize);
totalTestSize += sampleSize; totalTestSize += sampleSize;
} }
errorCode = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize); { size_t const flushResult = ZSTD_compressEnd(ctx, cBuffer+cSize, cBufferSize-cSize);
CHECK (ZSTD_isError(errorCode), "multi-segments epilogue error : %s", ZSTD_getErrorName(errorCode)); CHECK (ZSTD_isError(flushResult), "multi-segments epilogue error : %s", ZSTD_getErrorName(flushResult));
cSize += errorCode; cSize += flushResult;
}
crcOrig = XXH64_digest(xxh64); crcOrig = XXH64_digest(xxh64);
/* streaming decompression test */ /* streaming decompression test */
errorCode = ZSTD_decompressBegin_usingDict(dctx, dict, dictSize); { size_t const errorCode = ZSTD_decompressBegin_usingDict(dctx, dict, dictSize);
CHECK (ZSTD_isError(errorCode), "cannot init DCtx : %s", ZSTD_getErrorName(errorCode)); CHECK (ZSTD_isError(errorCode), "cannot init DCtx : %s", ZSTD_getErrorName(errorCode)); }
totalCSize = 0; totalCSize = 0;
totalGenSize = 0; totalGenSize = 0;
while (totalCSize < cSize) while (totalCSize < cSize) {
{ size_t const inSize = ZSTD_nextSrcSizeToDecompress(dctx);
size_t inSize = ZSTD_nextSrcSizeToDecompress(dctx); size_t const genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize);
size_t genSize = ZSTD_decompressContinue(dctx, dstBuffer+totalGenSize, dstBufferSize-totalGenSize, cBuffer+totalCSize, inSize);
CHECK (ZSTD_isError(genSize), "streaming decompression error : %s", ZSTD_getErrorName(genSize)); CHECK (ZSTD_isError(genSize), "streaming decompression error : %s", ZSTD_getErrorName(genSize));
totalGenSize += genSize; totalGenSize += genSize;
totalCSize += inSize; totalCSize += inSize;
@ -613,13 +658,13 @@ int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibilit
CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded"); CHECK (ZSTD_nextSrcSizeToDecompress(dctx) != 0, "frame not fully decoded");
CHECK (totalGenSize != totalTestSize, "decompressed data : wrong size") CHECK (totalGenSize != totalTestSize, "decompressed data : wrong size")
CHECK (totalCSize != cSize, "compressed data should be fully read") CHECK (totalCSize != cSize, "compressed data should be fully read")
crcDest = XXH64(dstBuffer, totalTestSize, 0); { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
if (crcDest!=crcOrig) if (crcDest!=crcOrig) {
errorCode = findDiff(mirrorBuffer, dstBuffer, totalTestSize); size_t const errorPos = findDiff(mirrorBuffer, dstBuffer, totalTestSize);
CHECK (crcDest!=crcOrig, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)", CHECK (crcDest!=crcOrig, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)",
(U32)errorCode, (U32)totalTestSize, dstBuffer[errorCode], mirrorBuffer[errorCode]); (U32)errorPos, (U32)totalTestSize, dstBuffer[errorPos], mirrorBuffer[errorPos]);
} }
} } /* for ( ; (testNb <= nbTests) */
DISPLAY("\r%u fuzzer tests completed \n", testNb-1); DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
_cleanup: _cleanup:
@ -642,10 +687,10 @@ _output_error:
} }
/********************************************************* /*_*******************************************************
* Command line * Command line
*********************************************************/ *********************************************************/
int FUZ_usage(char* programName) int FUZ_usage(const char* programName)
{ {
DISPLAY( "Usage :\n"); DISPLAY( "Usage :\n");
DISPLAY( " %s [args]\n", programName); DISPLAY( " %s [args]\n", programName);
@ -654,7 +699,7 @@ int FUZ_usage(char* programName)
DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault); DISPLAY( " -i# : Nb of tests (default:%u) \n", nbTestsDefault);
DISPLAY( " -s# : Select seed (default:prompt user)\n"); DISPLAY( " -s# : Select seed (default:prompt user)\n");
DISPLAY( " -t# : Select starting test number (default:0)\n"); DISPLAY( " -t# : Select starting test number (default:0)\n");
DISPLAY( " -P# : Select compressibility in %% (default:%i%%)\n", FUZ_COMPRESSIBILITY_DEFAULT); DISPLAY( " -P# : Select compressibility in %% (default:%u%%)\n", FUZ_compressibility_default);
DISPLAY( " -v : verbose\n"); DISPLAY( " -v : verbose\n");
DISPLAY( " -p : pause at the end\n"); DISPLAY( " -p : pause at the end\n");
DISPLAY( " -h : display help and exit\n"); DISPLAY( " -h : display help and exit\n");
@ -662,33 +707,28 @@ int FUZ_usage(char* programName)
} }
int main(int argc, char** argv) int main(int argc, const char** argv)
{ {
U32 seed=0; U32 seed=0;
int seedset=0; int seedset=0;
int argNb; int argNb;
int nbTests = nbTestsDefault; int nbTests = nbTestsDefault;
int testNb = 0; int testNb = 0;
int proba = FUZ_COMPRESSIBILITY_DEFAULT; U32 proba = FUZ_compressibility_default;
int result=0; int result=0;
U32 mainPause = 0; U32 mainPause = 0;
char* programName; U32 maxDuration = 0;
const char* programName = argv[0];
/* Check command line */ /* Check command line */
programName = argv[0]; for (argNb=1; argNb<argc; argNb++) {
for(argNb=1; argNb<argc; argNb++) const char* argument = argv[argNb];
{
char* argument = argv[argNb];
if(!argument) continue; /* Protection if argument empty */ if(!argument) continue; /* Protection if argument empty */
/* Handle commands. Aggregated commands are allowed */ /* Handle commands. Aggregated commands are allowed */
if (argument[0]=='-') if (argument[0]=='-') {
{
argument++; argument++;
while (*argument!=0) {
while (*argument!=0)
{
switch(*argument) switch(*argument)
{ {
case 'h': case 'h':
@ -707,10 +747,9 @@ int main(int argc, char** argv)
break; break;
case 'i': case 'i':
argument++; g_testTime=0; argument++; maxDuration=0;
nbTests=0; nbTests=0;
while ((*argument>='0') && (*argument<='9')) while ((*argument>='0') && (*argument<='9')) {
{
nbTests *= 10; nbTests *= 10;
nbTests += *argument - '0'; nbTests += *argument - '0';
argument++; argument++;
@ -719,24 +758,21 @@ int main(int argc, char** argv)
case 'T': case 'T':
argument++; argument++;
nbTests=0; g_testTime=0; nbTests=0; maxDuration=0;
while ((*argument>='0') && (*argument<='9')) while ((*argument>='0') && (*argument<='9')) {
{ maxDuration *= 10;
g_testTime *= 10; maxDuration += *argument - '0';
g_testTime += *argument - '0';
argument++; argument++;
} }
if (*argument=='m') g_testTime *=60, argument++; if (*argument=='m') maxDuration *=60, argument++;
if (*argument=='n') argument++; if (*argument=='n') argument++;
g_testTime *= 1000;
break; break;
case 's': case 's':
argument++; argument++;
seed=0; seed=0;
seedset=1; seedset=1;
while ((*argument>='0') && (*argument<='9')) while ((*argument>='0') && (*argument<='9')) {
{
seed *= 10; seed *= 10;
seed += *argument - '0'; seed += *argument - '0';
argument++; argument++;
@ -746,8 +782,7 @@ int main(int argc, char** argv)
case 't': case 't':
argument++; argument++;
testNb=0; testNb=0;
while ((*argument>='0') && (*argument<='9')) while ((*argument>='0') && (*argument<='9')) {
{
testNb *= 10; testNb *= 10;
testNb += *argument - '0'; testNb += *argument - '0';
argument++; argument++;
@ -757,35 +792,30 @@ int main(int argc, char** argv)
case 'P': /* compressibility % */ case 'P': /* compressibility % */
argument++; argument++;
proba=0; proba=0;
while ((*argument>='0') && (*argument<='9')) while ((*argument>='0') && (*argument<='9')) {
{
proba *= 10; proba *= 10;
proba += *argument - '0'; proba += *argument - '0';
argument++; argument++;
} }
if (proba<0) proba=0;
if (proba>100) proba=100; if (proba>100) proba=100;
break; break;
default: default:
return FUZ_usage(programName); return FUZ_usage(programName);
} } } } } /* for (argNb=1; argNb<argc; argNb++) */
}
}
}
/* Get Seed */ /* Get Seed */
DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION); DISPLAY("Starting zstd tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION);
if (!seedset) seed = FUZ_GetMilliStart() % 10000; if (!seedset) seed = (U32)(clock() % 10000);
DISPLAY("Seed = %u\n", seed); DISPLAY("Seed = %u\n", seed);
if (proba!=FUZ_COMPRESSIBILITY_DEFAULT) DISPLAY("Compressibility : %i%%\n", proba); if (proba!=FUZ_compressibility_default) DISPLAY("Compressibility : %u%%\n", proba);
if (testNb==0) result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ if (testNb==0)
result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */
if (!result) if (!result)
result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); result = fuzzerTests(seed, nbTests, testNb, maxDuration, ((double)proba) / 100);
if (mainPause) if (mainPause) {
{
int unused; int unused;
DISPLAY("Press Enter \n"); DISPLAY("Press Enter \n");
unused = getchar(); unused = getchar();

View File

@ -1,6 +1,6 @@
/* /*
fileio.c - File i/o handler fileio_legacy.c - File i/o handler for legacy format
Copyright (C) Yann Collet 2013-2015 Copyright (C) Yann Collet 2015-2016
GPL v2 License GPL v2 License
@ -19,12 +19,11 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at : You can contact the author at :
- zstd homepage : http://www.zstd.net
- zstd source repository : https://github.com/Cyan4973/zstd - zstd source repository : https://github.com/Cyan4973/zstd
- Public forum : https://groups.google.com/forum/#!forum/lz4c
*/ */
/* /*
Note : this is stand-alone program. Note : this file is not part of ZSTD compression library.
It is not part of ZSTD compression library, it is a user program of ZSTD library.
The license of ZSTD library is BSD. The license of ZSTD library is BSD.
The license of this file is GPLv2. The license of this file is GPLv2.
*/ */
@ -172,8 +171,7 @@ unsigned long long FIOv01_decompressFrame(FILE* foutput, FILE* finput)
/* Main decompression Loop */ /* Main decompression Loop */
toRead = ZSTDv01_nextSrcSizeToDecompress(dctx); toRead = ZSTDv01_nextSrcSizeToDecompress(dctx);
while (toRead) while (toRead){
{
size_t readSize, decodedSize; size_t readSize, decodedSize;
/* Fill input buffer */ /* Fill input buffer */
@ -187,8 +185,7 @@ unsigned long long FIOv01_decompressFrame(FILE* foutput, FILE* finput)
decodedSize = ZSTDv01_decompressContinue(dctx, op, oend-op, inBuff, readSize); decodedSize = ZSTDv01_decompressContinue(dctx, op, oend-op, inBuff, readSize);
if (ZSTDv01_isError(decodedSize)) EXM_THROW(45, "Decoding error : input corrupted"); if (ZSTDv01_isError(decodedSize)) EXM_THROW(45, "Decoding error : input corrupted");
if (decodedSize) /* not a header */ if (decodedSize) { /* not a header */
{
/* Write block */ /* Write block */
sizeCheck = fwrite(op, 1, decodedSize, foutput); sizeCheck = fwrite(op, 1, decodedSize, foutput);
if (sizeCheck != decodedSize) EXM_THROW(46, "Write error : unable to write data block to destination file"); if (sizeCheck != decodedSize) EXM_THROW(46, "Write error : unable to write data block to destination file");
@ -232,8 +229,7 @@ unsigned long long FIOv02_decompressFrame(FILE* foutput, FILE* finput)
/* Main decompression Loop */ /* Main decompression Loop */
toRead = ZSTDv02_nextSrcSizeToDecompress(dctx); toRead = ZSTDv02_nextSrcSizeToDecompress(dctx);
while (toRead) while (toRead) {
{
size_t readSize, decodedSize; size_t readSize, decodedSize;
/* Fill input buffer */ /* Fill input buffer */
@ -247,8 +243,7 @@ unsigned long long FIOv02_decompressFrame(FILE* foutput, FILE* finput)
decodedSize = ZSTDv02_decompressContinue(dctx, op, oend-op, inBuff, readSize); decodedSize = ZSTDv02_decompressContinue(dctx, op, oend-op, inBuff, readSize);
if (ZSTDv02_isError(decodedSize)) EXM_THROW(45, "Decoding error : input corrupted"); if (ZSTDv02_isError(decodedSize)) EXM_THROW(45, "Decoding error : input corrupted");
if (decodedSize) /* not a header */ if (decodedSize) { /* not a header */
{
/* Write block */ /* Write block */
sizeCheck = fwrite(op, 1, decodedSize, foutput); sizeCheck = fwrite(op, 1, decodedSize, foutput);
if (sizeCheck != decodedSize) EXM_THROW(46, "Write error : unable to write data block to destination file"); if (sizeCheck != decodedSize) EXM_THROW(46, "Write error : unable to write data block to destination file");
@ -292,8 +287,7 @@ unsigned long long FIOv03_decompressFrame(FILE* foutput, FILE* finput)
/* Main decompression Loop */ /* Main decompression Loop */
toRead = ZSTDv03_nextSrcSizeToDecompress(dctx); toRead = ZSTDv03_nextSrcSizeToDecompress(dctx);
while (toRead) while (toRead) {
{
size_t readSize, decodedSize; size_t readSize, decodedSize;
/* Fill input buffer */ /* Fill input buffer */
@ -307,8 +301,7 @@ unsigned long long FIOv03_decompressFrame(FILE* foutput, FILE* finput)
decodedSize = ZSTDv03_decompressContinue(dctx, op, oend-op, inBuff, readSize); decodedSize = ZSTDv03_decompressContinue(dctx, op, oend-op, inBuff, readSize);
if (ZSTDv03_isError(decodedSize)) EXM_THROW(45, "Decoding error : input corrupted"); if (ZSTDv03_isError(decodedSize)) EXM_THROW(45, "Decoding error : input corrupted");
if (decodedSize) /* not a header */ if (decodedSize) { /* not a header */
{
/* Write block */ /* Write block */
sizeCheck = fwrite(op, 1, decodedSize, foutput); sizeCheck = fwrite(op, 1, decodedSize, foutput);
if (sizeCheck != decodedSize) EXM_THROW(46, "Write error : unable to write data block to destination file"); if (sizeCheck != decodedSize) EXM_THROW(46, "Write error : unable to write data block to destination file");
@ -329,7 +322,7 @@ unsigned long long FIOv03_decompressFrame(FILE* foutput, FILE* finput)
} }
/*- v0.4.x -*/ /*===== v0.4.x =====*/
typedef struct { typedef struct {
void* srcBuffer; void* srcBuffer;
@ -380,8 +373,7 @@ unsigned long long FIOv04_decompressFrame(dRessv04_t ress,
ZBUFFv04_decompressInit(ress.dctx); ZBUFFv04_decompressInit(ress.dctx);
ZBUFFv04_decompressWithDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize); ZBUFFv04_decompressWithDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize);
while (1) while (1) {
{
/* Decode */ /* Decode */
size_t sizeCheck; size_t sizeCheck;
size_t inSize=readSize, decodedSize=ress.dstBufferSize; size_t inSize=readSize, decodedSize=ress.dstBufferSize;
@ -404,11 +396,89 @@ unsigned long long FIOv04_decompressFrame(dRessv04_t ress,
if (readSize != toRead) EXM_THROW(35, "Read error"); if (readSize != toRead) EXM_THROW(35, "Read error");
} }
FIOv04_freeDResources(ress);
return frameSize; return frameSize;
} }
/*===== v0.5.x =====*/
typedef struct {
void* srcBuffer;
size_t srcBufferSize;
void* dstBuffer;
size_t dstBufferSize;
void* dictBuffer;
size_t dictBufferSize;
ZBUFFv05_DCtx* dctx;
} dRessv05_t;
static dRessv05_t FIOv05_createDResources(void)
{
dRessv05_t ress;
/* init */
ress.dctx = ZBUFFv05_createDCtx();
if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZBUFF decompression context");
ress.dictBuffer = NULL; ress.dictBufferSize=0;
/* Allocate Memory */
ress.srcBufferSize = ZBUFFv05_recommendedDInSize();
ress.srcBuffer = malloc(ress.srcBufferSize);
ress.dstBufferSize = ZBUFFv05_recommendedDOutSize();
ress.dstBuffer = malloc(ress.dstBufferSize);
if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
return ress;
}
static void FIOv05_freeDResources(dRessv05_t ress)
{
size_t const errorCode = ZBUFFv05_freeDCtx(ress.dctx);
if (ZBUFFv05_isError(errorCode)) EXM_THROW(69, "Error : can't free ZBUFF context resource : %s", ZBUFFv05_getErrorName(errorCode));
free(ress.srcBuffer);
free(ress.dstBuffer);
free(ress.dictBuffer);
}
unsigned long long FIOv05_decompressFrame(dRessv05_t ress,
FILE* foutput, FILE* finput)
{
U64 frameSize = 0;
size_t readSize = 4;
MEM_writeLE32(ress.srcBuffer, ZSTDv05_MAGICNUMBER);
ZBUFFv05_decompressInitDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize);
while (1) {
/* Decode */
size_t sizeCheck;
size_t inSize=readSize, decodedSize=ress.dstBufferSize;
size_t toRead = ZBUFFv05_decompressContinue(ress.dctx, ress.dstBuffer, &decodedSize, ress.srcBuffer, &inSize);
if (ZBUFFv05_isError(toRead)) EXM_THROW(36, "Decoding error : %s", ZBUFFv05_getErrorName(toRead));
readSize -= inSize;
/* Write block */
sizeCheck = fwrite(ress.dstBuffer, 1, decodedSize, foutput);
if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block to destination file");
frameSize += decodedSize;
DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) );
if (toRead == 0) break;
if (readSize) EXM_THROW(38, "Decoding error : should consume entire input");
/* Fill input buffer */
if (toRead > ress.srcBufferSize) EXM_THROW(34, "too large block");
readSize = fread(ress.srcBuffer, 1, toRead, finput);
if (readSize != toRead) EXM_THROW(35, "Read error");
}
return frameSize;
}
/*===== General legacy dispatcher =====*/
unsigned long long FIO_decompressLegacyFrame(FILE* foutput, FILE* finput, U32 magicNumberLE) unsigned long long FIO_decompressLegacyFrame(FILE* foutput, FILE* finput, U32 magicNumberLE)
{ {
switch(magicNumberLE) switch(magicNumberLE)
@ -420,7 +490,17 @@ unsigned long long FIO_decompressLegacyFrame(FILE* foutput, FILE* finput, U32 ma
case ZSTDv03_magicNumber : case ZSTDv03_magicNumber :
return FIOv03_decompressFrame(foutput, finput); return FIOv03_decompressFrame(foutput, finput);
case ZSTDv04_magicNumber : case ZSTDv04_magicNumber :
return FIOv04_decompressFrame(FIOv04_createDResources(), foutput, finput); { dRessv04_t r = FIOv04_createDResources();
unsigned long long const s = FIOv04_decompressFrame(r, foutput, finput);
FIOv04_freeDResources(r);
return s;
}
case ZSTDv05_MAGICNUMBER :
{ dRessv05_t r = FIOv05_createDResources();
unsigned long long const s = FIOv05_decompressFrame(r, foutput, finput);
FIOv05_freeDResources(r);
return s;
}
default : default :
return ERROR(prefix_unknown); return ERROR(prefix_unknown);
} }

View File

@ -100,7 +100,6 @@
#define NB_LEVELS_TRACKED 30 #define NB_LEVELS_TRACKED 30
static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31)); static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
#define DEFAULT_CHUNKSIZE (4<<20)
#define COMPRESSIBILITY_DEFAULT 0.50 #define COMPRESSIBILITY_DEFAULT 0.50
static const size_t sampleSize = 10000000; static const size_t sampleSize = 10000000;
@ -127,7 +126,7 @@ static U32 g_rand = 1;
static U32 g_singleRun = 0; static U32 g_singleRun = 0;
static U32 g_target = 0; static U32 g_target = 0;
static U32 g_noSeed = 0; static U32 g_noSeed = 0;
static ZSTD_parameters g_params = { 0, 0, 0, 0, 0, 0, 0, ZSTD_greedy }; static ZSTD_compressionParameters g_params = { 0, 0, 0, 0, 0, 0, ZSTD_greedy };
void BMK_SetNbIterations(int nbLoops) void BMK_SetNbIterations(int nbLoops)
{ {
@ -252,7 +251,7 @@ typedef struct
static size_t BMK_benchParam(BMK_result_t* resultPtr, static size_t BMK_benchParam(BMK_result_t* resultPtr,
const void* srcBuffer, size_t srcSize, const void* srcBuffer, size_t srcSize,
ZSTD_CCtx* ctx, ZSTD_CCtx* ctx,
const ZSTD_parameters params) const ZSTD_compressionParameters cParams)
{ {
const size_t blockSize = g_blockSize ? g_blockSize : srcSize; const size_t blockSize = g_blockSize ? g_blockSize : srcSize;
const U32 nbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize); const U32 nbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize);
@ -260,13 +259,14 @@ static size_t BMK_benchParam(BMK_result_t* resultPtr,
const size_t maxCompressedSize = (size_t)nbBlocks * ZSTD_compressBound(blockSize); const size_t maxCompressedSize = (size_t)nbBlocks * ZSTD_compressBound(blockSize);
void* const compressedBuffer = malloc(maxCompressedSize); void* const compressedBuffer = malloc(maxCompressedSize);
void* const resultBuffer = malloc(srcSize); void* const resultBuffer = malloc(srcSize);
U32 Wlog = params.windowLog; ZSTD_parameters params;
U32 Clog = params.contentLog; U32 Wlog = cParams.windowLog;
U32 Hlog = params.hashLog; U32 Clog = cParams.chainLog;
U32 Slog = params.searchLog; U32 Hlog = cParams.hashLog;
U32 Slength = params.searchLength; U32 Slog = cParams.searchLog;
U32 Tlength = params.targetLength; U32 Slength = cParams.searchLength;
ZSTD_strategy strat = params.strategy; U32 Tlength = cParams.targetLength;
ZSTD_strategy strat = cParams.strategy;
char name[30] = { 0 }; char name[30] = { 0 };
U64 crcOrig; U64 crcOrig;
@ -316,6 +316,8 @@ static size_t BMK_benchParam(BMK_result_t* resultPtr,
const int startTime =BMK_GetMilliStart(); const int startTime =BMK_GetMilliStart();
DISPLAY("\r%79s\r", ""); DISPLAY("\r%79s\r", "");
params.cParams = cParams;
params.fParams.contentSizeFlag = 0;
for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) { for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
int nbLoops; int nbLoops;
int milliTime; int milliTime;
@ -406,14 +408,13 @@ const char* g_stratName[] = { "ZSTD_fast ",
"ZSTD_lazy ", "ZSTD_lazy ",
"ZSTD_lazy2 ", "ZSTD_lazy2 ",
"ZSTD_btlazy2", "ZSTD_btlazy2",
"ZSTD_opt ",
"ZSTD_btopt " }; "ZSTD_btopt " };
static void BMK_printWinner(FILE* f, U32 cLevel, BMK_result_t result, ZSTD_parameters params, size_t srcSize) static void BMK_printWinner(FILE* f, U32 cLevel, BMK_result_t result, ZSTD_compressionParameters params, size_t srcSize)
{ {
DISPLAY("\r%79s\r", ""); DISPLAY("\r%79s\r", "");
fprintf(f," {%3u,%3u,%3u,%3u,%3u,%3u,%3u, %s }, ", fprintf(f," {%3u,%3u,%3u,%3u,%3u,%3u, %s }, ",
0, params.windowLog, params.contentLog, params.hashLog, params.searchLog, params.searchLength, params.windowLog, params.chainLog, params.hashLog, params.searchLog, params.searchLength,
params.targetLength, g_stratName[(U32)(params.strategy)]); params.targetLength, g_stratName[(U32)(params.strategy)]);
fprintf(f, fprintf(f,
"/* level %2u */ /* R:%5.3f at %5.1f MB/s - %5.1f MB/s */\n", "/* level %2u */ /* R:%5.3f at %5.1f MB/s - %5.1f MB/s */\n",
@ -425,7 +426,7 @@ static U32 g_cSpeedTarget[NB_LEVELS_TRACKED] = { 0 }; /* NB_LEVELS_TRACKED : c
typedef struct { typedef struct {
BMK_result_t result; BMK_result_t result;
ZSTD_parameters params; ZSTD_compressionParameters params;
} winnerInfo_t; } winnerInfo_t;
static void BMK_printWinners2(FILE* f, const winnerInfo_t* winners, size_t srcSize) static void BMK_printWinners2(FILE* f, const winnerInfo_t* winners, size_t srcSize)
@ -433,7 +434,7 @@ static void BMK_printWinners2(FILE* f, const winnerInfo_t* winners, size_t srcSi
unsigned cLevel; unsigned cLevel;
fprintf(f, "\n /* Proposed configurations : */ \n"); fprintf(f, "\n /* Proposed configurations : */ \n");
fprintf(f, " /* l, W, C, H, S, L, T, strat */ \n"); fprintf(f, " /* W, C, H, S, L, T, strat */ \n");
for (cLevel=0; cLevel <= ZSTD_maxCLevel(); cLevel++) for (cLevel=0; cLevel <= ZSTD_maxCLevel(); cLevel++)
BMK_printWinner(f, cLevel, winners[cLevel].result, winners[cLevel].params, srcSize); BMK_printWinner(f, cLevel, winners[cLevel].result, winners[cLevel].params, srcSize);
@ -448,8 +449,9 @@ static void BMK_printWinners(FILE* f, const winnerInfo_t* winners, size_t srcSiz
BMK_printWinners2(stdout, winners, srcSize); BMK_printWinners2(stdout, winners, srcSize);
} }
size_t ZSTD_sizeofCCtx(ZSTD_compressionParameters params); /* hidden interface, declared here */
static int BMK_seed(winnerInfo_t* winners, const ZSTD_parameters params, static int BMK_seed(winnerInfo_t* winners, const ZSTD_compressionParameters params,
const void* srcBuffer, size_t srcSize, const void* srcBuffer, size_t srcSize,
ZSTD_CCtx* ctx) ZSTD_CCtx* ctx)
{ {
@ -482,10 +484,8 @@ static int BMK_seed(winnerInfo_t* winners, const ZSTD_parameters params,
double W_DMemUsed_note = W_ratioNote * ( 40 + 9*cLevel) - log((double)W_DMemUsed); double W_DMemUsed_note = W_ratioNote * ( 40 + 9*cLevel) - log((double)W_DMemUsed);
double O_DMemUsed_note = O_ratioNote * ( 40 + 9*cLevel) - log((double)O_DMemUsed); double O_DMemUsed_note = O_ratioNote * ( 40 + 9*cLevel) - log((double)O_DMemUsed);
size_t W_CMemUsed = (1 << params.windowLog) + 4 * (1 << params.hashLog) + size_t W_CMemUsed = (1 << params.windowLog) + ZSTD_sizeofCCtx(params);
((params.strategy==ZSTD_fast) ? 0 : 4 * (1 << params.contentLog)); size_t O_CMemUsed = (1 << winners[cLevel].params.windowLog) + ZSTD_sizeofCCtx(winners[cLevel].params);
size_t O_CMemUsed = (1 << winners[cLevel].params.windowLog) + 4 * (1 << winners[cLevel].params.hashLog) +
((winners[cLevel].params.strategy==ZSTD_fast) ? 0 : 4 * (1 << winners[cLevel].params.contentLog));
double W_CMemUsed_note = W_ratioNote * ( 50 + 13*cLevel) - log((double)W_CMemUsed); double W_CMemUsed_note = W_ratioNote * ( 50 + 13*cLevel) - log((double)W_CMemUsed);
double O_CMemUsed_note = O_ratioNote * ( 50 + 13*cLevel) - log((double)O_CMemUsed); double O_CMemUsed_note = O_ratioNote * ( 50 + 13*cLevel) - log((double)O_CMemUsed);
@ -544,55 +544,61 @@ static int BMK_seed(winnerInfo_t* winners, const ZSTD_parameters params,
/* nullified useless params, to ensure count stats */ /* nullified useless params, to ensure count stats */
static ZSTD_parameters* sanitizeParams(ZSTD_parameters params) static ZSTD_compressionParameters* sanitizeParams(ZSTD_compressionParameters params)
{ {
g_params = params; g_params = params;
if (params.strategy == ZSTD_fast) if (params.strategy == ZSTD_fast)
g_params.contentLog = 0, g_params.searchLog = 0; g_params.chainLog = 0, g_params.searchLog = 0;
if ((params.strategy != ZSTD_opt) && (params.strategy != ZSTD_btopt )) if (params.strategy != ZSTD_btopt )
g_params.targetLength = 0; g_params.targetLength = 0;
return &g_params; return &g_params;
} }
static void paramVariation(ZSTD_parameters* p) static void paramVariation(ZSTD_compressionParameters* ptr)
{ {
U32 nbChanges = (FUZ_rand(&g_rand) & 3) + 1; ZSTD_compressionParameters p;
for (; nbChanges; nbChanges--) { U32 validated = 0;
const U32 changeID = FUZ_rand(&g_rand) % 14; while (!validated) {
switch(changeID) U32 nbChanges = (FUZ_rand(&g_rand) & 3) + 1;
{ p = *ptr;
case 0: for ( ; nbChanges ; nbChanges--) {
p->contentLog++; break; const U32 changeID = FUZ_rand(&g_rand) % 14;
case 1: switch(changeID)
p->contentLog--; break; {
case 2: case 0:
p->hashLog++; break; p.chainLog++; break;
case 3: case 1:
p->hashLog--; break; p.chainLog--; break;
case 4: case 2:
p->searchLog++; break; p.hashLog++; break;
case 5: case 3:
p->searchLog--; break; p.hashLog--; break;
case 6: case 4:
p->windowLog++; break; p.searchLog++; break;
case 7: case 5:
p->windowLog--; break; p.searchLog--; break;
case 8: case 6:
p->searchLength++; break; p.windowLog++; break;
case 9: case 7:
p->searchLength--; break; p.windowLog--; break;
case 10: case 8:
p->strategy = (ZSTD_strategy)(((U32)p->strategy)+1); break; p.searchLength++; break;
case 11: case 9:
p->strategy = (ZSTD_strategy)(((U32)p->strategy)-1); break; p.searchLength--; break;
case 12: case 10:
p->targetLength *= 1 + ((double)(FUZ_rand(&g_rand)&255)) / 256.; break; p.strategy = (ZSTD_strategy)(((U32)p.strategy)+1); break;
case 13: case 11:
p->targetLength /= 1 + ((double)(FUZ_rand(&g_rand)&255)) / 256.; break; p.strategy = (ZSTD_strategy)(((U32)p.strategy)-1); break;
case 12:
p.targetLength *= 1 + ((double)(FUZ_rand(&g_rand)&255)) / 256.; break;
case 13:
p.targetLength /= 1 + ((double)(FUZ_rand(&g_rand)&255)) / 256.; break;
}
} }
validated = !ZSTD_isError(ZSTD_checkCParams(p));
} }
ZSTD_validateParams(p); *ptr = p;
} }
@ -608,7 +614,7 @@ static BYTE g_alreadyTested[PARAMTABLESIZE] = {0}; /* init to zero */
#define MAX(a,b) ( (a) > (b) ? (a) : (b) ) #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
static void playAround(FILE* f, winnerInfo_t* winners, static void playAround(FILE* f, winnerInfo_t* winners,
ZSTD_parameters params, ZSTD_compressionParameters params,
const void* srcBuffer, size_t srcSize, const void* srcBuffer, size_t srcSize,
ZSTD_CCtx* ctx) ZSTD_CCtx* ctx)
{ {
@ -616,7 +622,7 @@ static void playAround(FILE* f, winnerInfo_t* winners,
const int startTime = BMK_GetMilliStart(); const int startTime = BMK_GetMilliStart();
while (BMK_GetMilliSpan(startTime) < g_maxVariationTime) { while (BMK_GetMilliSpan(startTime) < g_maxVariationTime) {
ZSTD_parameters p = params; ZSTD_compressionParameters p = params;
if (nbVariations++ > g_maxNbVariations) break; if (nbVariations++ > g_maxNbVariations) break;
paramVariation(&p); paramVariation(&p);
@ -637,19 +643,21 @@ static void playAround(FILE* f, winnerInfo_t* winners,
} }
static void potentialRandomParams(ZSTD_parameters* p, U32 inverseChance) static void potentialRandomParams(ZSTD_compressionParameters* p, U32 inverseChance)
{ {
U32 chance = (FUZ_rand(&g_rand) % (inverseChance+1)); U32 chance = (FUZ_rand(&g_rand) % (inverseChance+1));
if (!chance) { U32 validated = 0;
if (!chance)
while (!validated) {
/* totally random entry */ /* totally random entry */
p->contentLog = FUZ_rand(&g_rand) % (ZSTD_CONTENTLOG_MAX+1 - ZSTD_CONTENTLOG_MIN) + ZSTD_CONTENTLOG_MIN; p->chainLog = FUZ_rand(&g_rand) % (ZSTD_CHAINLOG_MAX+1 - ZSTD_CHAINLOG_MIN) + ZSTD_CHAINLOG_MIN;
p->hashLog = FUZ_rand(&g_rand) % (ZSTD_HASHLOG_MAX+1 - ZSTD_HASHLOG_MIN) + ZSTD_HASHLOG_MIN; p->hashLog = FUZ_rand(&g_rand) % (ZSTD_HASHLOG_MAX+1 - ZSTD_HASHLOG_MIN) + ZSTD_HASHLOG_MIN;
p->searchLog = FUZ_rand(&g_rand) % (ZSTD_SEARCHLOG_MAX+1 - ZSTD_SEARCHLOG_MIN) + ZSTD_SEARCHLOG_MIN; p->searchLog = FUZ_rand(&g_rand) % (ZSTD_SEARCHLOG_MAX+1 - ZSTD_SEARCHLOG_MIN) + ZSTD_SEARCHLOG_MIN;
p->windowLog = FUZ_rand(&g_rand) % (ZSTD_WINDOWLOG_MAX+1 - ZSTD_WINDOWLOG_MIN) + ZSTD_WINDOWLOG_MIN; p->windowLog = FUZ_rand(&g_rand) % (ZSTD_WINDOWLOG_MAX+1 - ZSTD_WINDOWLOG_MIN) + ZSTD_WINDOWLOG_MIN;
p->searchLength=FUZ_rand(&g_rand) % (ZSTD_SEARCHLENGTH_MAX+1 - ZSTD_SEARCHLENGTH_MIN) + ZSTD_SEARCHLENGTH_MIN; p->searchLength=FUZ_rand(&g_rand) % (ZSTD_SEARCHLENGTH_MAX+1 - ZSTD_SEARCHLENGTH_MIN) + ZSTD_SEARCHLENGTH_MIN;
p->targetLength=FUZ_rand(&g_rand) % (ZSTD_TARGETLENGTH_MAX+1 - ZSTD_TARGETLENGTH_MIN) + ZSTD_TARGETLENGTH_MIN; p->targetLength=FUZ_rand(&g_rand) % (ZSTD_TARGETLENGTH_MAX+1 - ZSTD_TARGETLENGTH_MIN) + ZSTD_TARGETLENGTH_MIN;
p->strategy = (ZSTD_strategy) (FUZ_rand(&g_rand) % (ZSTD_btopt +1)); p->strategy = (ZSTD_strategy) (FUZ_rand(&g_rand) % (ZSTD_btopt +1));
ZSTD_validateParams(p); validated = !ZSTD_isError(ZSTD_checkCParams(*p));
} }
} }
@ -661,10 +669,9 @@ static void BMK_selectRandomStart(
U32 id = (FUZ_rand(&g_rand) % (ZSTD_maxCLevel()+1)); U32 id = (FUZ_rand(&g_rand) % (ZSTD_maxCLevel()+1));
if ((id==0) || (winners[id].params.windowLog==0)) { if ((id==0) || (winners[id].params.windowLog==0)) {
/* totally random entry */ /* totally random entry */
ZSTD_parameters p; ZSTD_compressionParameters p;
potentialRandomParams(&p, 1); potentialRandomParams(&p, 1);
p.srcSize = srcSize; ZSTD_adjustCParams(&p, srcSize, 0);
ZSTD_validateParams(&p);
playAround(f, winners, p, srcBuffer, srcSize, ctx); playAround(f, winners, p, srcBuffer, srcSize, ctx);
} }
else else
@ -675,7 +682,7 @@ static void BMK_selectRandomStart(
static void BMK_benchMem(void* srcBuffer, size_t srcSize) static void BMK_benchMem(void* srcBuffer, size_t srcSize)
{ {
ZSTD_CCtx* ctx = ZSTD_createCCtx(); ZSTD_CCtx* ctx = ZSTD_createCCtx();
ZSTD_parameters params; ZSTD_compressionParameters params;
winnerInfo_t winners[NB_LEVELS_TRACKED]; winnerInfo_t winners[NB_LEVELS_TRACKED];
int i; int i;
unsigned u; unsigned u;
@ -685,8 +692,7 @@ static void BMK_benchMem(void* srcBuffer, size_t srcSize)
if (g_singleRun) { if (g_singleRun) {
BMK_result_t testResult; BMK_result_t testResult;
g_params.srcSize = blockSize; ZSTD_adjustCParams(&g_params, srcSize, 0);
ZSTD_validateParams(&g_params);
BMK_benchParam(&testResult, srcBuffer, srcSize, ctx, g_params); BMK_benchParam(&testResult, srcBuffer, srcSize, ctx, g_params);
DISPLAY("\n"); DISPLAY("\n");
return; return;
@ -702,7 +708,7 @@ static void BMK_benchMem(void* srcBuffer, size_t srcSize)
else { else {
/* baseline config for level 1 */ /* baseline config for level 1 */
BMK_result_t testResult; BMK_result_t testResult;
params = ZSTD_getParams(1, blockSize); params = ZSTD_getCParams(1, blockSize, 0);
BMK_benchParam(&testResult, srcBuffer, srcSize, ctx, params); BMK_benchParam(&testResult, srcBuffer, srcSize, ctx, params);
g_cSpeedTarget[1] = (testResult.cSpeed * 31) >> 5; g_cSpeedTarget[1] = (testResult.cSpeed * 31) >> 5;
} }
@ -715,8 +721,7 @@ static void BMK_benchMem(void* srcBuffer, size_t srcSize)
{ {
const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel(); const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel();
for (i=1; i<=maxSeeds; i++) { for (i=1; i<=maxSeeds; i++) {
params = ZSTD_getParams(i, blockSize); params = ZSTD_getCParams(i, blockSize, 0);
ZSTD_validateParams(&params);
BMK_seed(winners, params, srcBuffer, srcSize, ctx); BMK_seed(winners, params, srcBuffer, srcSize, ctx);
} }
} }
@ -867,7 +872,7 @@ int optimizeForSize(char* inFileName)
{ {
ZSTD_CCtx* ctx = ZSTD_createCCtx(); ZSTD_CCtx* ctx = ZSTD_createCCtx();
ZSTD_parameters params; ZSTD_compressionParameters params;
winnerInfo_t winner; winnerInfo_t winner;
BMK_result_t candidate; BMK_result_t candidate;
const size_t blockSize = g_blockSize ? g_blockSize : benchedSize; const size_t blockSize = g_blockSize ? g_blockSize : benchedSize;
@ -881,7 +886,7 @@ int optimizeForSize(char* inFileName)
{ {
const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel(); const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel();
for (i=1; i<=maxSeeds; i++) { for (i=1; i<=maxSeeds; i++) {
params = ZSTD_getParams(i, blockSize); params = ZSTD_getCParams(i, blockSize, 0);
BMK_benchParam(&candidate, origBuff, benchedSize, ctx, params); BMK_benchParam(&candidate, origBuff, benchedSize, ctx, params);
if ( (candidate.cSize < winner.result.cSize) if ( (candidate.cSize < winner.result.cSize)
||((candidate.cSize == winner.result.cSize) && (candidate.cSpeed > winner.result.cSpeed)) ) ||((candidate.cSize == winner.result.cSize) && (candidate.cSpeed > winner.result.cSpeed)) )
@ -978,8 +983,7 @@ int main(int argc, char** argv)
if (argc<1) { badusage(exename); return 1; } if (argc<1) { badusage(exename); return 1; }
for(i=1; i<argc; i++) for(i=1; i<argc; i++) {
{
char* argument = argv[i]; char* argument = argv[i];
if(!argument) continue; /* Protection if argument empty */ if(!argument) continue; /* Protection if argument empty */
@ -1011,13 +1015,9 @@ int main(int argc, char** argv)
/* Sample compressibility (when no file provided) */ /* Sample compressibility (when no file provided) */
case 'P': case 'P':
argument++; argument++;
{ { U32 proba32 = 0;
U32 proba32 = 0; while ((argument[0]>= '0') && (argument[0]<= '9'))
while ((argument[0]>= '0') && (argument[0]<= '9')) { proba32 = (proba32*10) + (*argument++ - '0');
proba32 *= 10;
proba32 += argument[0] - '0';
argument++;
}
g_compressibility = (double)proba32 / 100.; g_compressibility = (double)proba32 / 100.;
} }
break; break;
@ -1031,7 +1031,7 @@ int main(int argc, char** argv)
case 'S': case 'S':
g_singleRun = 1; g_singleRun = 1;
argument++; argument++;
g_params = ZSTD_getParams(2, g_blockSize); g_params = ZSTD_getCParams(2, g_blockSize, 0);
for ( ; ; ) { for ( ; ; ) {
switch(*argument) switch(*argument)
{ {
@ -1042,10 +1042,10 @@ int main(int argc, char** argv)
g_params.windowLog *= 10, g_params.windowLog += *argument++ - '0'; g_params.windowLog *= 10, g_params.windowLog += *argument++ - '0';
continue; continue;
case 'c': case 'c':
g_params.contentLog = 0; g_params.chainLog = 0;
argument++; argument++;
while ((*argument>= '0') && (*argument<='9')) while ((*argument>= '0') && (*argument<='9'))
g_params.contentLog *= 10, g_params.contentLog += *argument++ - '0'; g_params.chainLog *= 10, g_params.chainLog += *argument++ - '0';
continue; continue;
case 'h': case 'h':
g_params.hashLog = 0; g_params.hashLog = 0;
@ -1077,12 +1077,11 @@ int main(int argc, char** argv)
g_params.strategy = (ZSTD_strategy)(*argument++ - '0'); g_params.strategy = (ZSTD_strategy)(*argument++ - '0');
continue; continue;
case 'L': case 'L':
{ { int cLevel = 0;
int cLevel = 0;
argument++; argument++;
while ((*argument>= '0') && (*argument<='9')) while ((*argument>= '0') && (*argument<='9'))
cLevel *= 10, cLevel += *argument++ - '0'; cLevel *= 10, cLevel += *argument++ - '0';
g_params = ZSTD_getParams(cLevel, g_blockSize); g_params = ZSTD_getCParams(cLevel, g_blockSize, 0);
continue; continue;
} }
default : ; default : ;
@ -1095,25 +1094,20 @@ int main(int argc, char** argv)
case 'T': case 'T':
argument++; argument++;
g_target = 0; g_target = 0;
while ((*argument >= '0') && (*argument <= '9')) { while ((*argument >= '0') && (*argument <= '9'))
g_target *= 10; g_target = (g_target*10) + (*argument++ - '0');
g_target += *argument - '0';
argument++;
}
break; break;
/* cut input into blocks */ /* cut input into blocks */
case 'B': case 'B':
{ g_blockSize = 0;
g_blockSize = 0; argument++;
argument++; while ((*argument >='0') && (*argument <='9'))
while ((*argument >='0') && (*argument <='9')) g_blockSize = (g_blockSize*10) + (*argument++ - '0');
g_blockSize *= 10, g_blockSize += *argument++ - '0'; if (*argument=='K') g_blockSize<<=10, argument++; /* allows using KB notation */
if (*argument=='K') g_blockSize<<=10, argument++; /* allows using KB notation */ if (*argument=='M') g_blockSize<<=20, argument++;
if (*argument=='M') g_blockSize<<=20, argument++; if (*argument=='B') argument++;
if (*argument=='B') argument++; DISPLAY("using %u KB block size \n", g_blockSize>>10);
DISPLAY("using %u KB block size \n", g_blockSize>>10);
}
break; break;
/* Unknown command */ /* Unknown command */
@ -1121,7 +1115,7 @@ int main(int argc, char** argv)
} }
} }
continue; continue;
} } /* if (argument[0]=='-') */
/* first provided filename is input */ /* first provided filename is input */
if (!input_filename) { input_filename=argument; filenamesStart=i; continue; } if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }

View File

@ -25,18 +25,28 @@ roundTripTest() {
echo "\n**** simple tests **** " echo "\n**** simple tests **** "
./datagen > tmp ./datagen > tmp
$ZSTD tmp $ZSTD -f tmp # trivial compression case, creates tmp.zst
$ZSTD -df tmp.zst # trivial decompression case (overwrites tmp)
echo "test : too large compression level (must fail)"
$ZSTD -99 tmp && die "too large compression level undetected" $ZSTD -99 tmp && die "too large compression level undetected"
$ZSTD tmp -c > tmpCompressed $ZSTD tmp -c > tmpCompressed # compression using stdout
$ZSTD tmp --stdout > tmpCompressed $ZSTD tmp --stdout > tmpCompressed # compressoin using stdout, long format
echo "test : decompress file with wrong suffix (must fail)"
$ZSTD -d tmpCompressed && die "wrong suffix error not detected!" $ZSTD -d tmpCompressed && die "wrong suffix error not detected!"
$ZSTD -d tmpCompressed -c > tmpResult $ZSTD -d tmpCompressed -c > tmpResult # decompression using stdout
$ZSTD --decompress tmpCompressed -c > tmpResult $ZSTD --decompress tmpCompressed -c > tmpResult
$ZSTD --decompress tmpCompressed --stdout > tmpResult $ZSTD --decompress tmpCompressed --stdout > tmpResult
$ZSTD -d < tmp.zst > /dev/null # combine decompression, stdin & stdout
$ZSTD -d - < tmp.zst > /dev/null
$ZSTD -dc < tmp.zst > /dev/null
$ZSTD -dc - < tmp.zst > /dev/null
$ZSTD -q tmp && die "overwrite check failed!" $ZSTD -q tmp && die "overwrite check failed!"
$ZSTD -q -f tmp $ZSTD -q -f tmp
$ZSTD -q --force tmp $ZSTD -q --force tmp
$ZSTD -df tmp && die "should have refused : wrong extension"
cp tmp tmp2.zst
$ZSTD -df tmp2.zst && die "should have failed : wrong format"
rm tmp2.zst
echo "\n**** frame concatenation **** " echo "\n**** frame concatenation **** "
@ -68,6 +78,11 @@ echo "\n**** dictionary tests **** "
./datagen -g1M | md5sum > tmp1 ./datagen -g1M | md5sum > tmp1
./datagen -g1M | $ZSTD -D tmpDict | $ZSTD -D tmpDict -dvq | md5sum > tmp2 ./datagen -g1M | $ZSTD -D tmpDict | $ZSTD -D tmpDict -dvq | md5sum > tmp2
diff -q tmp1 tmp2 diff -q tmp1 tmp2
$ZSTD --train *.c *.h -o tmpDict
$ZSTD xxhash.c -D tmpDict -of tmp
$ZSTD -d tmp -D tmpDict -of result
diff xxhash.c result
echo "\n**** multiple files tests **** " echo "\n**** multiple files tests **** "
@ -103,6 +118,10 @@ $ZSTD -t * && die "bad files not detected !"
echo "\n**** zstd round-trip tests **** " echo "\n**** zstd round-trip tests **** "
roundTripTest roundTripTest
roundTripTest -g15K # TableID==3
roundTripTest -g127K # TableID==2
roundTripTest -g255K # TableID==1
roundTripTest -g513K # TableID==0
roundTripTest -g512K 6 # greedy, hash chain roundTripTest -g512K 6 # greedy, hash chain
roundTripTest -g512K 16 # btlazy2 roundTripTest -g512K 16 # btlazy2
roundTripTest -g512K 19 # btopt roundTripTest -g512K 19 # btopt
@ -140,7 +159,7 @@ roundTripTest -g50000000 -P94 18
roundTripTest -g50000000 -P94 19 roundTripTest -g50000000 -P94 19
roundTripTest -g99000000 -P99 20 roundTripTest -g99000000 -P99 20
roundTripTest -g6000000000 -P99 q roundTripTest -g6000000000 -P99 1
rm tmp* rm tmp*

View File

@ -1,6 +1,6 @@
/* /*
Fuzzer test tool for zstd_buffered Fuzzer test tool for zstd_buffered
Copyright (C) Yann Collet 2105 Copyright (C) Yann Collet 2015-2016
GPL v2 License GPL v2 License
@ -19,11 +19,10 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at : You can contact the author at :
- ZSTD source repository : https://github.com/Cyan4973/zstd - ZSTD homepage : https://www.zstd.net/
- ZSTD public forum : https://groups.google.com/forum/#!forum/lz4c
*/ */
/************************************** /*-************************************
* Compiler specific * Compiler specific
**************************************/ **************************************/
#ifdef _MSC_VER /* Visual Studio */ #ifdef _MSC_VER /* Visual Studio */
@ -33,7 +32,7 @@
#endif #endif
/************************************** /*-************************************
* Includes * Includes
**************************************/ **************************************/
#include <stdlib.h> /* free */ #include <stdlib.h> /* free */
@ -42,13 +41,13 @@
#include <string.h> /* strcmp */ #include <string.h> /* strcmp */
#include "mem.h" #include "mem.h"
#include "zbuff.h" #include "zbuff.h"
#include "zstd.h" /* ZSTD_compressBound() */ #include "zstd_static.h" /* ZSTD_compressBound(), ZSTD_maxCLevel() */
#include "datagen.h" /* RDG_genBuffer */ #include "datagen.h" /* RDG_genBuffer */
#include "xxhash.h" /* XXH64 */ #include "xxhash.h" /* XXH64 */
/************************************** /*-************************************
Constants * Constants
**************************************/ **************************************/
#ifndef ZSTD_VERSION #ifndef ZSTD_VERSION
# define ZSTD_VERSION "" # define ZSTD_VERSION ""
@ -66,7 +65,7 @@ static const U32 prime2 = 2246822519U;
/************************************** /*-************************************
* Display Macros * Display Macros
**************************************/ **************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
@ -83,7 +82,7 @@ static U32 g_displayTime = 0;
static U32 g_testTime = 0; static U32 g_testTime = 0;
/********************************************************* /*-*******************************************************
* Fuzzer functions * Fuzzer functions
*********************************************************/ *********************************************************/
#define MAX(a,b) ((a)>(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
@ -107,15 +106,17 @@ static U32 FUZ_GetMilliSpan(U32 nTimeStart)
return nSpan; return nSpan;
} }
/*! FUZ_rand() :
@return : a 27 bits random value, from a 32-bits `seed`.
`seed` is also modified */
# define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
unsigned int FUZ_rand(unsigned int* src) unsigned int FUZ_rand(unsigned int* seedPtr)
{ {
U32 rand32 = *src; U32 rand32 = *seedPtr;
rand32 *= prime1; rand32 *= prime1;
rand32 += prime2; rand32 += prime2;
rand32 = FUZ_rotl32(rand32, 13); rand32 = FUZ_rotl32(rand32, 13);
*src = rand32; *seedPtr = rand32;
return rand32 >> 5; return rand32 >> 5;
} }
@ -133,12 +134,12 @@ static unsigned FUZ_highbit32(U32 v32)
static int basicUnitTests(U32 seed, double compressibility) static int basicUnitTests(U32 seed, double compressibility)
{ {
int testResult = 0; int testResult = 0;
void* CNBuffer;
size_t CNBufferSize = COMPRESSIBLE_NOISE_LENGTH; size_t CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
void* compressedBuffer; void* CNBuffer = malloc(CNBufferSize);
size_t compressedBufferSize = ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH); size_t const compressedBufferSize = ZSTD_compressBound(COMPRESSIBLE_NOISE_LENGTH);
void* decodedBuffer; void* compressedBuffer = malloc(compressedBufferSize);
size_t decodedBufferSize = CNBufferSize; size_t const decodedBufferSize = CNBufferSize;
void* decodedBuffer = malloc(decodedBufferSize);
U32 randState = seed; U32 randState = seed;
size_t result, cSize, readSize, genSize; size_t result, cSize, readSize, genSize;
U32 testNb=0; U32 testNb=0;
@ -146,11 +147,7 @@ static int basicUnitTests(U32 seed, double compressibility)
ZBUFF_DCtx* zd = ZBUFF_createDCtx(); ZBUFF_DCtx* zd = ZBUFF_createDCtx();
/* Create compressible test buffer */ /* Create compressible test buffer */
CNBuffer = malloc(CNBufferSize); if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd) {
compressedBuffer = malloc(compressedBufferSize);
decodedBuffer = malloc(decodedBufferSize);
if (!CNBuffer || !compressedBuffer || !decodedBuffer || !zc || !zd)
{
DISPLAY("Not enough memory, aborting\n"); DISPLAY("Not enough memory, aborting\n");
goto _output_error; goto _output_error;
} }
@ -183,11 +180,9 @@ static int basicUnitTests(U32 seed, double compressibility)
DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "OK \n");
/* check regenerated data is byte exact */ /* check regenerated data is byte exact */
{ { size_t i;
size_t i;
DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++); DISPLAYLEVEL(4, "test%3i : check decompressed result : ", testNb++);
for (i=0; i<CNBufferSize; i++) for (i=0; i<CNBufferSize; i++) {
{
if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;; if (((BYTE*)decodedBuffer)[i] != ((BYTE*)CNBuffer)[i]) goto _output_error;;
} }
DISPLAYLEVEL(4, "OK \n"); DISPLAYLEVEL(4, "OK \n");
@ -213,8 +208,7 @@ static size_t findDiff(const void* buf1, const void* buf2, size_t max)
const BYTE* b1 = (const BYTE*)buf1; const BYTE* b1 = (const BYTE*)buf1;
const BYTE* b2 = (const BYTE*)buf2; const BYTE* b2 = (const BYTE*)buf2;
size_t i; size_t i;
for (i=0; i<max; i++) for (i=0; i<max; i++) {
{
if (b1[i] != b2[i]) break; if (b1[i] != b2[i]) break;
} }
return i; return i;
@ -225,28 +219,39 @@ static size_t findDiff(const void* buf1, const void* buf2, size_t max)
#define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \
DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; }
static const U32 maxSrcLog = 24;
static const U32 maxSampleLog = 19;
int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility) static size_t FUZ_rLogLength(U32* seed, U32 logLength)
{ {
size_t const lengthMask = ((size_t)1 << logLength) - 1;
return (lengthMask+1) + (FUZ_rand(seed) & lengthMask);
}
static size_t FUZ_randomLength(U32* seed, U32 maxLog)
{
U32 const logLength = FUZ_rand(seed) % maxLog;
return FUZ_rLogLength(seed, logLength);
}
static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibility)
{
static const U32 maxSrcLog = 24;
static const U32 maxSampleLog = 19;
BYTE* cNoiseBuffer[5]; BYTE* cNoiseBuffer[5];
BYTE* srcBuffer;
size_t srcBufferSize = (size_t)1<<maxSrcLog; size_t srcBufferSize = (size_t)1<<maxSrcLog;
BYTE* copyBuffer; BYTE* copyBuffer;
size_t copyBufferSize = srcBufferSize + (1<<maxSampleLog); size_t copyBufferSize= srcBufferSize + (1<<maxSampleLog);
BYTE* cBuffer; BYTE* cBuffer;
size_t cBufferSize = ZSTD_compressBound(srcBufferSize); size_t cBufferSize = ZSTD_compressBound(srcBufferSize);
BYTE* dstBuffer; BYTE* dstBuffer;
size_t dstBufferSize = srcBufferSize; size_t dstBufferSize = srcBufferSize;
U32 result = 0; U32 result = 0;
U32 testNb = 0; U32 testNb = 0;
U32 coreSeed = seed, lseed = 0; U32 coreSeed = seed;
ZBUFF_CCtx* zc; ZBUFF_CCtx* zc;
ZBUFF_DCtx* zd; ZBUFF_DCtx* zd;
U32 startTime = FUZ_GetMilliStart(); U32 startTime = FUZ_GetMilliStart();
/* allocation */ /* allocations */
zc = ZBUFF_createCCtx(); zc = ZBUFF_createCCtx();
zd = ZBUFF_createDCtx(); zd = ZBUFF_createDCtx();
cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize); cNoiseBuffer[0] = (BYTE*)malloc (srcBufferSize);
@ -267,172 +272,144 @@ int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compressibilit
RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed); RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */ RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */ RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
srcBuffer = cNoiseBuffer[2]; memset(copyBuffer, 0x65, copyBufferSize); /* make copyBuffer considered initialized */
memset(copyBuffer, 0x65, copyBufferSize);
memcpy(copyBuffer, srcBuffer, MIN(copyBufferSize,srcBufferSize)); /* make copyBuffer considered initialized */
/* catch up testNb */ /* catch up testNb */
for (testNb=1; testNb < startTest; testNb++) for (testNb=1; testNb < startTest; testNb++)
FUZ_rand(&coreSeed); FUZ_rand(&coreSeed);
/* test loop */ /* test loop */
for ( ; (testNb <= nbTests) || (FUZ_GetMilliSpan(startTime) < g_testTime); testNb++ ) for ( ; (testNb <= nbTests) || (FUZ_GetMilliSpan(startTime) < g_testTime) ; testNb++ ) {
{ U32 lseed;
size_t sampleSize, sampleStart; const BYTE* srcBuffer;
const BYTE* dict; const BYTE* dict;
size_t cSize, dictSize; size_t maxTestSize, dictSize;
size_t maxTestSize, totalTestSize, readSize, totalCSize, genSize, totalGenSize; size_t cSize, totalTestSize, totalCSize, totalGenSize;
size_t errorCode; size_t errorCode;
U32 sampleSizeLog, buffNb, n, nbChunks; U32 n, nbChunks;
XXH64_CREATESTATE_STATIC(xxh64); XXH64_CREATESTATE_STATIC(xxh64);
U64 crcOrig, crcDest; U64 crcOrig;
/* init */ /* init */
DISPLAYUPDATE(2, "\r%6u", testNb); DISPLAYUPDATE(2, "\r%6u", testNb);
if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests); if (nbTests >= testNb) DISPLAYUPDATE(2, "/%6u ", nbTests);
FUZ_rand(&coreSeed); FUZ_rand(&coreSeed);
lseed = coreSeed ^ prime1; lseed = coreSeed ^ prime1;
buffNb = FUZ_rand(&lseed) & 127;
if (buffNb & 7) buffNb=2; /* select buffer */
else
{
buffNb >>= 3;
if (buffNb & 7)
{
const U32 tnb[2] = { 1, 3 };
buffNb = tnb[buffNb >> 3];
}
else
{
const U32 tnb[2] = { 0, 4 };
buffNb = tnb[buffNb >> 3];
}
}
srcBuffer = cNoiseBuffer[buffNb];
/* Multi - segments compression test */ /* state total reset */
/* some problems only happen when states are re-used in a specific order */
if ((FUZ_rand(&lseed) & 0xFF) == 131) { ZBUFF_freeCCtx(zc); zc = ZBUFF_createCCtx(); }
if ((FUZ_rand(&lseed) & 0xFF) == 132) { ZBUFF_freeDCtx(zd); zd = ZBUFF_createDCtx(); }
/* srcBuffer selection [0-4] */
{ U32 buffNb = FUZ_rand(&lseed) & 0x7F;
if (buffNb & 7) buffNb=2; /* most common : compressible (P) */
else {
buffNb >>= 3;
if (buffNb & 7) {
const U32 tnb[2] = { 1, 3 }; /* barely/highly compressible */
buffNb = tnb[buffNb >> 3];
} else {
const U32 tnb[2] = { 0, 4 }; /* not compressible / sparse */
buffNb = tnb[buffNb >> 3];
} }
srcBuffer = cNoiseBuffer[buffNb];
}
/* compression init */
{ U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
U32 const cLevel = (FUZ_rand(&lseed) % (ZSTD_maxCLevel() - (testLog/3))) + 1;
maxTestSize = FUZ_rLogLength(&lseed, testLog);
/* random dictionary selection */
{ size_t dictStart;
dictSize = (FUZ_rand(&lseed)==1) ? FUZ_randomLength(&lseed, maxSampleLog) : 0;
dictStart = FUZ_rand(&lseed) % (srcBufferSize - dictSize);
dict = srcBuffer + dictStart;
}
{ size_t const initError = ZBUFF_compressInitDictionary(zc, dict, dictSize, cLevel);
CHECK (ZBUFF_isError(initError),"init error : %s", ZBUFF_getErrorName(initError));
} }
/* multi-segments compression test */
XXH64_reset(xxh64, 0); XXH64_reset(xxh64, 0);
nbChunks = (FUZ_rand(&lseed) & 127) + 2; nbChunks = (FUZ_rand(&lseed) & 127) + 2;
sampleSizeLog = FUZ_rand(&lseed) % maxSrcLog; for (n=0, cSize=0, totalTestSize=0 ; (n<nbChunks) && (totalTestSize < maxTestSize) ; n++) {
maxTestSize = (size_t)1 << sampleSizeLog; /* compress random chunk into random size dst buffer */
maxTestSize += FUZ_rand(&lseed) & (maxTestSize-1); { size_t readChunkSize = FUZ_randomLength(&lseed, maxSampleLog);
size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
size_t dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
size_t const srcStart = FUZ_rand(&lseed) % (srcBufferSize - readChunkSize);
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; size_t const compressionError = ZBUFF_compressContinue(zc, cBuffer+cSize, &dstBuffSize, srcBuffer+srcStart, &readChunkSize);
sampleSize = (size_t)1 << sampleSizeLog; CHECK (ZBUFF_isError(compressionError), "compression error : %s", ZBUFF_getErrorName(compressionError));
sampleSize += FUZ_rand(&lseed) & (sampleSize-1);
sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
dict = srcBuffer + sampleStart;
dictSize = sampleSize;
ZBUFF_compressInitDictionary(zc, dict, dictSize, (FUZ_rand(&lseed) % (20 - (sampleSizeLog/3))) + 1);
totalTestSize = 0; XXH64_update(xxh64, srcBuffer+srcStart, readChunkSize);
cSize = 0; memcpy(copyBuffer+totalTestSize, srcBuffer+srcStart, readChunkSize);
for (n=0; n<nbChunks; n++) cSize += dstBuffSize;
{ totalTestSize += readChunkSize;
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog;
sampleSize = (size_t)1 << sampleSizeLog;
sampleSize += FUZ_rand(&lseed) & (sampleSize-1);
sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
readSize = sampleSize;
/* random size output buffer */
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog;
sampleSize = (size_t)1 << sampleSizeLog;
sampleSize += FUZ_rand(&lseed) & (sampleSize-1);
genSize = MIN (cBufferSize - cSize, sampleSize);
errorCode = ZBUFF_compressContinue(zc, cBuffer+cSize, &genSize, srcBuffer+sampleStart, &readSize);
CHECK (ZBUFF_isError(errorCode), "compression error : %s", ZBUFF_getErrorName(errorCode));
XXH64_update(xxh64, srcBuffer+sampleStart, readSize);
memcpy(copyBuffer+totalTestSize, srcBuffer+sampleStart, readSize);
cSize += genSize;
totalTestSize += readSize;
if ((FUZ_rand(&lseed) & 15) == 0)
{
/* add a few random flushes operations, to mess around */
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog;
sampleSize = (size_t)1 << sampleSizeLog;
sampleSize += FUZ_rand(&lseed) & (sampleSize-1);
genSize = MIN (cBufferSize - cSize, sampleSize);
errorCode = ZBUFF_compressFlush(zc, cBuffer+cSize, &genSize);
CHECK (ZBUFF_isError(errorCode), "flush error : %s", ZBUFF_getErrorName(errorCode));
cSize += genSize;
} }
if (totalTestSize > maxTestSize) break; /* random flush operation, to mess around */
if ((FUZ_rand(&lseed) & 15) == 0) {
size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
size_t dstBuffSize = MIN(cBufferSize - cSize, randomDstSize);
size_t const flushError = ZBUFF_compressFlush(zc, cBuffer+cSize, &dstBuffSize);
CHECK (ZBUFF_isError(flushError), "flush error : %s", ZBUFF_getErrorName(flushError));
cSize += dstBuffSize;
} }
/* final frame epilogue */
{ size_t dstBuffSize = cBufferSize - cSize;
size_t const flushError = ZBUFF_compressEnd(zc, cBuffer+cSize, &dstBuffSize);
CHECK (ZBUFF_isError(flushError), "flush error : %s", ZBUFF_getErrorName(flushError));
cSize += dstBuffSize;
} }
genSize = cBufferSize - cSize;
errorCode = ZBUFF_compressEnd(zc, cBuffer+cSize, &genSize);
CHECK (ZBUFF_isError(errorCode), "compression error : %s", ZBUFF_getErrorName(errorCode));
CHECK (errorCode != 0, "frame epilogue not fully consumed");
cSize += genSize;
crcOrig = XXH64_digest(xxh64); crcOrig = XXH64_digest(xxh64);
/* multi - fragments decompression test */ /* multi - fragments decompression test */
ZBUFF_decompressInitDictionary(zd, dict, dictSize); ZBUFF_decompressInitDictionary(zd, dict, dictSize);
totalCSize = 0; for (totalCSize = 0, totalGenSize = 0 ; totalCSize < cSize ; ) {
totalGenSize = 0; size_t readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
while (totalCSize < cSize) size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
{ size_t dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; size_t const decompressError = ZBUFF_decompressContinue(zd, dstBuffer+totalGenSize, &dstBuffSize, cBuffer+totalCSize, &readCSrcSize);
sampleSize = (size_t)1 << sampleSizeLog; CHECK (ZBUFF_isError(decompressError), "decompression error : %s", ZBUFF_getErrorName(decompressError));
sampleSize += FUZ_rand(&lseed) & (sampleSize-1); totalGenSize += dstBuffSize;
readSize = sampleSize; totalCSize += readCSrcSize;
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; errorCode = decompressError; /* needed for != 0 last test */
sampleSize = (size_t)1 << sampleSizeLog;
sampleSize += FUZ_rand(&lseed) & (sampleSize-1);
genSize = MIN(sampleSize, dstBufferSize - totalGenSize);
errorCode = ZBUFF_decompressContinue(zd, dstBuffer+totalGenSize, &genSize, cBuffer+totalCSize, &readSize);
CHECK (ZBUFF_isError(errorCode), "decompression error : %s", ZBUFF_getErrorName(errorCode));
totalGenSize += genSize;
totalCSize += readSize;
} }
CHECK (errorCode != 0, "frame not fully decoded"); CHECK (errorCode != 0, "frame not fully decoded");
CHECK (totalGenSize != totalTestSize, "decompressed data : wrong size") CHECK (totalGenSize != totalTestSize, "decompressed data : wrong size")
CHECK (totalCSize != cSize, "compressed data should be fully read") CHECK (totalCSize != cSize, "compressed data should be fully read")
crcDest = XXH64(dstBuffer, totalTestSize, 0); { U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize); if (crcDest!=crcOrig) findDiff(copyBuffer, dstBuffer, totalTestSize);
CHECK (crcDest!=crcOrig, "decompressed data corrupted"); CHECK (crcDest!=crcOrig, "decompressed data corrupted"); }
/*===== noisy/erroneous src decompression test =====*/
/* noisy/erroneous src decompression test */
/* add some noise */ /* add some noise */
nbChunks = (FUZ_rand(&lseed) & 7) + 2; { U32 const nbNoiseChunks = (FUZ_rand(&lseed) & 7) + 2;
for (n=0; n<nbChunks; n++) U32 nn; for (nn=0; nn<nbNoiseChunks; nn++) {
{ size_t const randomNoiseSize = FUZ_randomLength(&lseed, maxSampleLog);
size_t cStart; size_t const noiseSize = MIN((cSize/3) , randomNoiseSize);
size_t const noiseStart = FUZ_rand(&lseed) % (srcBufferSize - noiseSize);
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; size_t const cStart = FUZ_rand(&lseed) % (cSize - noiseSize);
sampleSize = (size_t)1 << sampleSizeLog; memcpy(cBuffer+cStart, srcBuffer+noiseStart, noiseSize);
sampleSize += FUZ_rand(&lseed) & (sampleSize-1); } }
if (sampleSize > cSize/3) sampleSize = cSize/3;
sampleStart = FUZ_rand(&lseed) % (srcBufferSize - sampleSize);
cStart = FUZ_rand(&lseed) % (cSize - sampleSize);
memcpy(cBuffer+cStart, srcBuffer+sampleStart, sampleSize);
}
/* try decompression on noisy data */ /* try decompression on noisy data */
ZBUFF_decompressInit(zd); ZBUFF_decompressInit(zd);
totalCSize = 0; totalCSize = 0;
totalGenSize = 0; totalGenSize = 0;
while ( (totalCSize < cSize) && (totalGenSize < dstBufferSize) ) while ( (totalCSize < cSize) && (totalGenSize < dstBufferSize) ) {
{ size_t readCSrcSize = FUZ_randomLength(&lseed, maxSampleLog);
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; size_t const randomDstSize = FUZ_randomLength(&lseed, maxSampleLog);
sampleSize = (size_t)1 << sampleSizeLog; size_t dstBuffSize = MIN(dstBufferSize - totalGenSize, randomDstSize);
sampleSize += FUZ_rand(&lseed) & (sampleSize-1); size_t const decompressError = ZBUFF_decompressContinue(zd, dstBuffer+totalGenSize, &dstBuffSize, cBuffer+totalCSize, &readCSrcSize);
readSize = sampleSize; if (ZBUFF_isError(decompressError)) break; /* error correctly detected */
sampleSizeLog = FUZ_rand(&lseed) % maxSampleLog; totalGenSize += dstBuffSize;
sampleSize = (size_t)1 << sampleSizeLog; totalCSize += readCSrcSize;
sampleSize += FUZ_rand(&lseed) & (sampleSize-1); } }
genSize = MIN(sampleSize, dstBufferSize - totalGenSize);
errorCode = ZBUFF_decompressContinue(zd, dstBuffer+totalGenSize, &genSize, cBuffer+totalCSize, &readSize);
if (ZBUFF_isError(errorCode)) break; /* error correctly detected */
totalGenSize += genSize;
totalCSize += readSize;
}
}
DISPLAY("\r%u fuzzer tests completed \n", testNb); DISPLAY("\r%u fuzzer tests completed \n", testNb);
_cleanup: _cleanup:
@ -454,10 +431,10 @@ _output_error:
} }
/********************************************************* /*-*******************************************************
* Command line * Command line
*********************************************************/ *********************************************************/
int FUZ_usage(char* programName) int FUZ_usage(const char* programName)
{ {
DISPLAY( "Usage :\n"); DISPLAY( "Usage :\n");
DISPLAY( " %s [args]\n", programName); DISPLAY( " %s [args]\n", programName);
@ -474,7 +451,7 @@ int FUZ_usage(char* programName)
} }
int main(int argc, char** argv) int main(int argc, const char** argv)
{ {
U32 seed=0; U32 seed=0;
int seedset=0; int seedset=0;
@ -484,23 +461,18 @@ int main(int argc, char** argv)
int proba = FUZ_COMPRESSIBILITY_DEFAULT; int proba = FUZ_COMPRESSIBILITY_DEFAULT;
int result=0; int result=0;
U32 mainPause = 0; U32 mainPause = 0;
char* programName; const char* programName = argv[0];
/* Check command line */ /* Check command line */
programName = argv[0]; for(argNb=1; argNb<argc; argNb++) {
for(argNb=1; argNb<argc; argNb++) const char* argument = argv[argNb];
{
char* argument = argv[argNb];
if(!argument) continue; /* Protection if argument empty */ if(!argument) continue; /* Protection if argument empty */
/* Handle commands. Aggregated commands are allowed */ /* Parsing commands. Aggregated commands are allowed */
if (argument[0]=='-') if (argument[0]=='-') {
{
argument++; argument++;
while (*argument!=0) while (*argument!=0) {
{
switch(*argument) switch(*argument)
{ {
case 'h': case 'h':
@ -521,8 +493,7 @@ int main(int argc, char** argv)
case 'i': case 'i':
argument++; argument++;
nbTests=0; g_testTime=0; nbTests=0; g_testTime=0;
while ((*argument>='0') && (*argument<='9')) while ((*argument>='0') && (*argument<='9')) {
{
nbTests *= 10; nbTests *= 10;
nbTests += *argument - '0'; nbTests += *argument - '0';
argument++; argument++;
@ -532,8 +503,7 @@ int main(int argc, char** argv)
case 'T': case 'T':
argument++; argument++;
nbTests=0; g_testTime=0; nbTests=0; g_testTime=0;
while ((*argument>='0') && (*argument<='9')) while ((*argument>='0') && (*argument<='9')) {
{
g_testTime *= 10; g_testTime *= 10;
g_testTime += *argument - '0'; g_testTime += *argument - '0';
argument++; argument++;
@ -547,8 +517,7 @@ int main(int argc, char** argv)
argument++; argument++;
seed=0; seed=0;
seedset=1; seedset=1;
while ((*argument>='0') && (*argument<='9')) while ((*argument>='0') && (*argument<='9')) {
{
seed *= 10; seed *= 10;
seed += *argument - '0'; seed += *argument - '0';
argument++; argument++;
@ -558,8 +527,7 @@ int main(int argc, char** argv)
case 't': case 't':
argument++; argument++;
testNb=0; testNb=0;
while ((*argument>='0') && (*argument<='9')) while ((*argument>='0') && (*argument<='9')) {
{
testNb *= 10; testNb *= 10;
testNb += *argument - '0'; testNb += *argument - '0';
argument++; argument++;
@ -569,8 +537,7 @@ int main(int argc, char** argv)
case 'P': /* compressibility % */ case 'P': /* compressibility % */
argument++; argument++;
proba=0; proba=0;
while ((*argument>='0') && (*argument<='9')) while ((*argument>='0') && (*argument<='9')) {
{
proba *= 10; proba *= 10;
proba += *argument - '0'; proba += *argument - '0';
argument++; argument++;
@ -582,9 +549,7 @@ int main(int argc, char** argv)
default: default:
return FUZ_usage(programName); return FUZ_usage(programName);
} }
} } } } /* for(argNb=1; argNb<argc; argNb++) */
}
}
/* Get Seed */ /* Get Seed */
DISPLAY("Starting zstd_buffered tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION); DISPLAY("Starting zstd_buffered tester (%i-bits, %s)\n", (int)(sizeof(size_t)*8), ZSTD_VERSION);
@ -598,8 +563,8 @@ int main(int argc, char** argv)
if (testNb==0) result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */ if (testNb==0) result = basicUnitTests(0, ((double)proba) / 100); /* constant seed for predictability */
if (!result) if (!result)
result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100); result = fuzzerTests(seed, nbTests, testNb, ((double)proba) / 100);
if (mainPause)
{ if (mainPause) {
int unused; int unused;
DISPLAY("Press Enter \n"); DISPLAY("Press Enter \n");
unused = getchar(); unused = getchar();

View File

@ -22,9 +22,9 @@
- zstd homepage : http://www.zstd.net/ - zstd homepage : http://www.zstd.net/
*/ */
/* /*
Note : this is user program, not part of libzstd. Note : this is a user program, not part of libzstd.
The license of this command line program is GPLv2.
The license of libzstd is BSD. The license of libzstd is BSD.
The license of this command line program is GPLv2.
*/ */
@ -71,9 +71,10 @@
**************************************/ **************************************/
#define COMPRESSOR_NAME "zstd command line interface" #define COMPRESSOR_NAME "zstd command line interface"
#ifndef ZSTD_VERSION #ifndef ZSTD_VERSION
# define LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
# define QUOTE(str) #str # define QUOTE(str) #str
# define EXPAND_AND_QUOTE(str) QUOTE(str) # define EXPAND_AND_QUOTE(str) QUOTE(str)
# define ZSTD_VERSION "v" EXPAND_AND_QUOTE(ZSTD_VERSION_MAJOR) "." EXPAND_AND_QUOTE(ZSTD_VERSION_MINOR) "." EXPAND_AND_QUOTE(ZSTD_VERSION_RELEASE) # define ZSTD_VERSION "v" EXPAND_AND_QUOTE(LIB_VERSION)
#endif #endif
#define AUTHOR "Yann Collet" #define AUTHOR "Yann Collet"
#define WELCOME_MESSAGE "*** %s %i-bits %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(void*)*8), ZSTD_VERSION, AUTHOR #define WELCOME_MESSAGE "*** %s %i-bits %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(void*)*8), ZSTD_VERSION, AUTHOR
@ -132,6 +133,7 @@ static int usage_advanced(const char* programName)
DISPLAY( " -v : verbose mode\n"); DISPLAY( " -v : verbose mode\n");
DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n"); DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n");
DISPLAY( " -c : force write to standard output, even if it is the console\n"); DISPLAY( " -c : force write to standard output, even if it is the console\n");
DISPLAY( "--ultra : enable ultra modes (requires more memory to decompress)\n");
#ifndef ZSTD_NODICT #ifndef ZSTD_NODICT
DISPLAY( "Dictionary builder :\n"); DISPLAY( "Dictionary builder :\n");
DISPLAY( "--train : create a dictionary from a training set of files \n"); DISPLAY( "--train : create a dictionary from a training set of files \n");
@ -142,9 +144,9 @@ static int usage_advanced(const char* programName)
#ifndef ZSTD_NOBENCH #ifndef ZSTD_NOBENCH
DISPLAY( "Benchmark arguments :\n"); DISPLAY( "Benchmark arguments :\n");
DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n"); DISPLAY( " -b# : benchmark file(s), using # compression level (default : 1) \n");
DISPLAY( " -r# : test all compression levels from -bX to # (default: 1)\n");
DISPLAY( " -i# : iteration loops [1-9](default : 3)\n"); DISPLAY( " -i# : iteration loops [1-9](default : 3)\n");
DISPLAY( " -B# : cut file into independent blocks of size # (default: no block)\n"); DISPLAY( " -B# : cut file into independent blocks of size # (default: no block)\n");
DISPLAY( " -r# : test all compression levels from 1 to # (default: disabled)\n");
#endif #endif
return 0; return 0;
} }
@ -166,6 +168,8 @@ static void waitEnter(void)
} }
#define CLEAN_RETURN(i) { operationResult = (i); goto _end; }
int main(int argCount, const char** argv) int main(int argCount, const char** argv)
{ {
int i, int i,
@ -179,19 +183,19 @@ int main(int argCount, const char** argv)
nextArgumentIsOutFileName=0, nextArgumentIsOutFileName=0,
nextArgumentIsMaxDict=0; nextArgumentIsMaxDict=0;
unsigned cLevel = 1; unsigned cLevel = 1;
unsigned cLevelLast = 1;
const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */ const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */
unsigned filenameIdx = 0; unsigned filenameIdx = 0;
const char* programName = argv[0]; const char* programName = argv[0];
const char* outFileName = NULL; const char* outFileName = NULL;
const char* dictFileName = NULL; const char* dictFileName = NULL;
char* dynNameSpace = NULL; char* dynNameSpace = NULL;
int rangeBench = 1;
unsigned maxDictSize = g_defaultMaxDictSize; unsigned maxDictSize = g_defaultMaxDictSize;
unsigned dictCLevel = g_defaultDictCLevel; unsigned dictCLevel = g_defaultDictCLevel;
unsigned dictSelect = g_defaultSelectivityLevel; unsigned dictSelect = g_defaultSelectivityLevel;
/* init */ /* init */
(void)rangeBench; (void)dictCLevel; /* not used when ZSTD_NOBENCH / ZSTD_NODICT set */ (void)cLevelLast; (void)dictCLevel; /* not used when ZSTD_NOBENCH / ZSTD_NODICT set */
if (filenameTable==NULL) { DISPLAY("not enough memory\n"); exit(1); } if (filenameTable==NULL) { DISPLAY("not enough memory\n"); exit(1); }
displayOut = stderr; displayOut = stderr;
/* Pick out program name from path. Don't rely on stdlib because of conflicting behavior */ /* Pick out program name from path. Don't rely on stdlib because of conflicting behavior */
@ -210,8 +214,8 @@ int main(int argCount, const char** argv)
/* long commands (--long-word) */ /* long commands (--long-word) */
if (!strcmp(argument, "--decompress")) { decode=1; continue; } if (!strcmp(argument, "--decompress")) { decode=1; continue; }
if (!strcmp(argument, "--force")) { FIO_overwriteMode(); continue; } if (!strcmp(argument, "--force")) { FIO_overwriteMode(); continue; }
if (!strcmp(argument, "--version")) { displayOut=stdout; DISPLAY(WELCOME_MESSAGE); return 0; } if (!strcmp(argument, "--version")) { displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); }
if (!strcmp(argument, "--help")) { displayOut=stdout; return usage_advanced(programName); } if (!strcmp(argument, "--help")) { displayOut=stdout; CLEAN_RETURN(usage_advanced(programName)); }
if (!strcmp(argument, "--verbose")) { displayLevel=4; continue; } if (!strcmp(argument, "--verbose")) { displayLevel=4; continue; }
if (!strcmp(argument, "--quiet")) { displayLevel--; continue; } if (!strcmp(argument, "--quiet")) { displayLevel--; continue; }
if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; displayLevel=1; continue; } if (!strcmp(argument, "--stdout")) { forceStdout=1; outFileName=stdoutmark; displayLevel=1; continue; }
@ -219,11 +223,11 @@ int main(int argCount, const char** argv)
if (!strcmp(argument, "--train")) { dictBuild=1; outFileName=g_defaultDictName; continue; } if (!strcmp(argument, "--train")) { dictBuild=1; outFileName=g_defaultDictName; continue; }
if (!strcmp(argument, "--maxdict")) { nextArgumentIsMaxDict=1; continue; } if (!strcmp(argument, "--maxdict")) { nextArgumentIsMaxDict=1; continue; }
if (!strcmp(argument, "--keep")) { continue; } /* does nothing, since preserving input is default; for gzip/xz compatibility */ if (!strcmp(argument, "--keep")) { continue; } /* does nothing, since preserving input is default; for gzip/xz compatibility */
if (!strcmp(argument, "--ultra")) { FIO_setMaxWLog(0); continue; }
/* '-' means stdin/stdout */ /* '-' means stdin/stdout */
if (!strcmp(argument, "-")){ if (!strcmp(argument, "-")){
if (!filenameIdx) { filenameIdx=1, filenameTable[0]=stdinmark; continue; } if (!filenameIdx) { filenameIdx=1, filenameTable[0]=stdinmark; outFileName=stdoutmark; continue; }
outFileName=stdoutmark; continue;
} }
/* Decode commands (note : aggregated commands are allowed) */ /* Decode commands (note : aggregated commands are allowed) */
@ -231,7 +235,6 @@ int main(int argCount, const char** argv)
argument++; argument++;
while (argument[0]!=0) { while (argument[0]!=0) {
/* compression Level */ /* compression Level */
if ((*argument>='0') && (*argument<='9')) { if ((*argument>='0') && (*argument<='9')) {
cLevel = 0; cLevel = 0;
@ -242,16 +245,16 @@ int main(int argCount, const char** argv)
} }
dictCLevel = cLevel; dictCLevel = cLevel;
if (dictCLevel > ZSTD_maxCLevel()) if (dictCLevel > ZSTD_maxCLevel())
return badusage(programName); CLEAN_RETURN(badusage(programName));
continue; continue;
} }
switch(argument[0]) switch(argument[0])
{ {
/* Display help */ /* Display help */
case 'V': displayOut=stdout; DISPLAY(WELCOME_MESSAGE); return 0; /* Version Only */ case 'V': displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); /* Version Only */
case 'H': case 'H':
case 'h': displayOut=stdout; return usage_advanced(programName); case 'h': displayOut=stdout; CLEAN_RETURN(usage_advanced(programName));
/* Decoding */ /* Decoding */
case 'd': decode=1; argument++; break; case 'd': decode=1; argument++; break;
@ -286,33 +289,38 @@ int main(int argCount, const char** argv)
/* Modify Nb Iterations (benchmark only) */ /* Modify Nb Iterations (benchmark only) */
case 'i': case 'i':
{ { U32 iters= 0;
int iters= 0;
argument++; argument++;
while ((*argument >='0') && (*argument <='9')) while ((*argument >='0') && (*argument <='9'))
iters *= 10, iters += *argument++ - '0'; iters *= 10, iters += *argument++ - '0';
BMK_setNotificationLevel(displayLevel);
BMK_SetNbIterations(iters); BMK_SetNbIterations(iters);
} }
break; break;
/* cut input into blocks (benchmark only) */ /* cut input into blocks (benchmark only) */
case 'B': case 'B':
{ { size_t bSize = 0;
size_t bSize = 0;
argument++; argument++;
while ((*argument >='0') && (*argument <='9')) while ((*argument >='0') && (*argument <='9'))
bSize *= 10, bSize += *argument++ - '0'; bSize *= 10, bSize += *argument++ - '0';
if (*argument=='K') bSize<<=10, argument++; /* allows using KB notation */ if (*argument=='K') bSize<<=10, argument++; /* allows using KB notation */
if (*argument=='M') bSize<<=20, argument++; if (*argument=='M') bSize<<=20, argument++;
if (*argument=='B') argument++; if (*argument=='B') argument++;
BMK_setNotificationLevel(displayLevel);
BMK_SetBlockSize(bSize); BMK_SetBlockSize(bSize);
} }
break; break;
/* range bench (benchmark only) */ /* range bench (benchmark only) */
case 'r': case 'r':
rangeBench = -1; /* compression Level */
argument++; argument++;
if ((*argument>='0') && (*argument<='9')) {
cLevelLast = 0;
while ((*argument >= '0') && (*argument <= '9'))
cLevelLast *= 10, cLevelLast += *argument++ - '0';
}
break; break;
#endif /* ZSTD_NOBENCH */ #endif /* ZSTD_NOBENCH */
@ -323,15 +331,24 @@ int main(int argCount, const char** argv)
dictSelect *= 10, dictSelect += *argument++ - '0'; dictSelect *= 10, dictSelect += *argument++ - '0';
break; break;
/* Pause at the end (hidden option) */ /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */
case 'p': main_pause=1; argument++; break; case 'p': argument++;
#ifndef ZSTD_NOBENCH
if ((*argument>='0') && (*argument<='9')) {
int additionalParam = 0;
while ((*argument >= '0') && (*argument <= '9'))
additionalParam *= 10, additionalParam += *argument++ - '0';
BMK_setAdditionalParam(additionalParam);
} else
#endif
main_pause=1;
break;
/* unknown command */ /* unknown command */
default : return badusage(programName); default : CLEAN_RETURN(badusage(programName));
} }
} }
continue; continue;
} } /* if (argument[0]=='-') */
if (nextEntryIsDictionary) { if (nextEntryIsDictionary) {
nextEntryIsDictionary = 0; nextEntryIsDictionary = 0;
@ -366,7 +383,8 @@ int main(int argCount, const char** argv)
/* Check if benchmark is selected */ /* Check if benchmark is selected */
if (bench) { if (bench) {
#ifndef ZSTD_NOBENCH #ifndef ZSTD_NOBENCH
BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel*rangeBench); BMK_setNotificationLevel(displayLevel);
BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel, cLevelLast);
#endif #endif
goto _end; goto _end;
} }
@ -387,17 +405,17 @@ int main(int argCount, const char** argv)
if(!filenameIdx) filenameIdx=1, filenameTable[0]=stdinmark, outFileName=stdoutmark; if(!filenameIdx) filenameIdx=1, filenameTable[0]=stdinmark, outFileName=stdoutmark;
/* Check if input/output defined as console; trigger an error in this case */ /* Check if input/output defined as console; trigger an error in this case */
if (!strcmp(filenameTable[0], stdinmark) && IS_CONSOLE(stdin) ) return badusage(programName); if (!strcmp(filenameTable[0], stdinmark) && IS_CONSOLE(stdin) ) CLEAN_RETURN(badusage(programName));
if (outFileName && !strcmp(outFileName, stdoutmark) && IS_CONSOLE(stdout) && !forceStdout) return badusage(programName); if (outFileName && !strcmp(outFileName, stdoutmark) && IS_CONSOLE(stdout) && !forceStdout) CLEAN_RETURN(badusage(programName));
/* user-selected output filename, only possible with a single file */ /* user-selected output filename, only possible with a single file */
if (outFileName && strcmp(outFileName,stdoutmark) && strcmp(outFileName,nulmark) && (filenameIdx>1)) { if (outFileName && strcmp(outFileName,stdoutmark) && strcmp(outFileName,nulmark) && (filenameIdx>1)) {
DISPLAY("Too many files (%u) on the command line. \n", filenameIdx); DISPLAY("Too many files (%u) on the command line. \n", filenameIdx);
return filenameIdx; CLEAN_RETURN(filenameIdx);
} }
/* No warning message in pipe mode (stdin + stdout) or multiple mode */ /* No warning message in pipe mode (stdin + stdout) or multiple mode */
if (!strcmp(filenameTable[0], stdinmark) && !strcmp(outFileName,stdoutmark) && (displayLevel==2)) displayLevel=1; if (!strcmp(filenameTable[0], stdinmark) && outFileName && !strcmp(outFileName,stdoutmark) && (displayLevel==2)) displayLevel=1;
if ((filenameIdx>1) && (displayLevel==2)) displayLevel=1; if ((filenameIdx>1) && (displayLevel==2)) displayLevel=1;
/* IO Stream/File */ /* IO Stream/File */

View File

@ -22,6 +22,7 @@
<ProjectGuid>{61ABD629-1CC8-4FD7-9281-6B8DBB9D3DF8}</ProjectGuid> <ProjectGuid>{61ABD629-1CC8-4FD7-9281-6B8DBB9D3DF8}</ProjectGuid>
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
<RootNamespace>fullbench</RootNamespace> <RootNamespace>fullbench</RootNamespace>
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@ -161,6 +162,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\..\lib\fse.c" /> <ClCompile Include="..\..\..\lib\fse.c" />
<ClCompile Include="..\..\..\lib\huff0.c" /> <ClCompile Include="..\..\..\lib\huff0.c" />
<ClCompile Include="..\..\..\lib\zbuff.c" />
<ClCompile Include="..\..\..\lib\zstd_compress.c" /> <ClCompile Include="..\..\..\lib\zstd_compress.c" />
<ClCompile Include="..\..\..\lib\zstd_decompress.c" /> <ClCompile Include="..\..\..\lib\zstd_decompress.c" />
<ClCompile Include="..\..\..\programs\datagen.c" /> <ClCompile Include="..\..\..\programs\datagen.c" />
@ -175,6 +177,8 @@
<ClInclude Include="..\..\..\lib\legacy\zstd_v01.h" /> <ClInclude Include="..\..\..\lib\legacy\zstd_v01.h" />
<ClInclude Include="..\..\..\lib\legacy\zstd_v02.h" /> <ClInclude Include="..\..\..\lib\legacy\zstd_v02.h" />
<ClInclude Include="..\..\..\lib\legacy\zstd_v03.h" /> <ClInclude Include="..\..\..\lib\legacy\zstd_v03.h" />
<ClInclude Include="..\..\..\lib\zbuff.h" />
<ClInclude Include="..\..\..\lib\zbuff_static.h" />
<ClInclude Include="..\..\..\lib\zstd.h" /> <ClInclude Include="..\..\..\lib\zstd.h" />
<ClInclude Include="..\..\..\lib\zstd_static.h" /> <ClInclude Include="..\..\..\lib\zstd_static.h" />
<ClInclude Include="..\..\..\programs\datagen.h" /> <ClInclude Include="..\..\..\programs\datagen.h" />

View File

@ -33,6 +33,9 @@
<ClCompile Include="..\..\..\lib\zstd_decompress.c"> <ClCompile Include="..\..\..\lib\zstd_decompress.c">
<Filter>Fichiers sources</Filter> <Filter>Fichiers sources</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\lib\zbuff.c">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\lib\fse.h"> <ClInclude Include="..\..\..\lib\fse.h">
@ -68,5 +71,11 @@
<ClInclude Include="..\..\..\lib\legacy\zstd_v03.h"> <ClInclude Include="..\..\..\lib\legacy\zstd_v03.h">
<Filter>Fichiers d%27en-tête</Filter> <Filter>Fichiers d%27en-tête</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\..\lib\zbuff.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="..\..\..\lib\zbuff_static.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -22,6 +22,7 @@
<ProjectGuid>{6FD4352B-346C-4703-96EA-D4A8B9A6976E}</ProjectGuid> <ProjectGuid>{6FD4352B-346C-4703-96EA-D4A8B9A6976E}</ProjectGuid>
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
<RootNamespace>fuzzer</RootNamespace> <RootNamespace>fuzzer</RootNamespace>
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">

15
visual/2013/zstd/zstd.vcxproj Normal file → Executable file
View File

@ -26,6 +26,7 @@
<ClCompile Include="..\..\..\lib\legacy\zstd_v02.c" /> <ClCompile Include="..\..\..\lib\legacy\zstd_v02.c" />
<ClCompile Include="..\..\..\lib\legacy\zstd_v03.c" /> <ClCompile Include="..\..\..\lib\legacy\zstd_v03.c" />
<ClCompile Include="..\..\..\lib\legacy\zstd_v04.c" /> <ClCompile Include="..\..\..\lib\legacy\zstd_v04.c" />
<ClCompile Include="..\..\..\lib\legacy\zstd_v05.c" />
<ClCompile Include="..\..\..\lib\zbuff.c" /> <ClCompile Include="..\..\..\lib\zbuff.c" />
<ClCompile Include="..\..\..\lib\zdict.c" /> <ClCompile Include="..\..\..\lib\zdict.c" />
<ClCompile Include="..\..\..\lib\zstd_compress.c" /> <ClCompile Include="..\..\..\lib\zstd_compress.c" />
@ -49,6 +50,7 @@
<ClInclude Include="..\..\..\lib\legacy\zstd_v02.h" /> <ClInclude Include="..\..\..\lib\legacy\zstd_v02.h" />
<ClInclude Include="..\..\..\lib\legacy\zstd_v03.h" /> <ClInclude Include="..\..\..\lib\legacy\zstd_v03.h" />
<ClInclude Include="..\..\..\lib\legacy\zstd_v04.h" /> <ClInclude Include="..\..\..\lib\legacy\zstd_v04.h" />
<ClInclude Include="..\..\..\lib\legacy\zstd_v05.h" />
<ClInclude Include="..\..\..\lib\zbuff.h" /> <ClInclude Include="..\..\..\lib\zbuff.h" />
<ClInclude Include="..\..\..\lib\zbuff_static.h" /> <ClInclude Include="..\..\..\lib\zbuff_static.h" />
<ClInclude Include="..\..\..\lib\zdict.h" /> <ClInclude Include="..\..\..\lib\zdict.h" />
@ -69,6 +71,7 @@
<ProjectGuid>{4E52A41A-F33B-4C7A-8C36-A1A6B4F4277C}</ProjectGuid> <ProjectGuid>{4E52A41A-F33B-4C7A-8C36-A1A6B4F4277C}</ProjectGuid>
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
<RootNamespace>zstd</RootNamespace> <RootNamespace>zstd</RootNamespace>
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@ -87,7 +90,7 @@
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<PlatformToolset>v120</PlatformToolset> <PlatformToolset>v120_xp</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
@ -126,7 +129,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
<IncludePath>$(SolutionDir)..\..\programs\legacy;$(SolutionDir)..\..\lib\legacy;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);</IncludePath> <IncludePath>$(SolutionDir)..\..\programs\legacy;$(SolutionDir)..\..\lib\legacy;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);</IncludePath>
<RunCodeAnalysis>true</RunCodeAnalysis> <RunCodeAnalysis>false</RunCodeAnalysis>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
@ -170,19 +173,20 @@
<WarningLevel>Level4</WarningLevel> <WarningLevel>Level4</WarningLevel>
<PrecompiledHeader> <PrecompiledHeader>
</PrecompiledHeader> </PrecompiledHeader>
<Optimization>MaxSpeed</Optimization> <Optimization>Full</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<EnablePREfast>true</EnablePREfast> <EnablePREfast>false</EnablePREfast>
<TreatWarningAsError>true</TreatWarningAsError> <TreatWarningAsError>true</TreatWarningAsError>
<AdditionalOptions>/analyze:stacksize19000 %(AdditionalOptions)</AdditionalOptions> <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>setargv.obj; kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -203,6 +207,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>setargv.obj;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

6
visual/2013/zstd/zstd.vcxproj.filters Normal file → Executable file
View File

@ -69,6 +69,9 @@
<ClCompile Include="..\..\..\programs\dibio.c"> <ClCompile Include="..\..\..\programs\dibio.c">
<Filter>Fichiers sources</Filter> <Filter>Fichiers sources</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\lib\legacy\zstd_v05.c">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\lib\fse.h"> <ClInclude Include="..\..\..\lib\fse.h">
@ -146,5 +149,8 @@
<ClInclude Include="..\..\..\programs\dibio.h"> <ClInclude Include="..\..\..\programs\dibio.h">
<Filter>Fichiers d%27en-tête</Filter> <Filter>Fichiers d%27en-tête</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\..\lib\legacy\zstd_v05.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -24,8 +24,6 @@
<ClCompile Include="..\..\..\lib\zbuff.c" /> <ClCompile Include="..\..\..\lib\zbuff.c" />
<ClCompile Include="..\..\..\lib\zstd_compress.c" /> <ClCompile Include="..\..\..\lib\zstd_compress.c" />
<ClCompile Include="..\..\..\lib\zstd_decompress.c" /> <ClCompile Include="..\..\..\lib\zstd_decompress.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\lib\bitstream.h" /> <ClInclude Include="..\..\..\lib\bitstream.h" />
<ClInclude Include="..\..\..\lib\error_private.h" /> <ClInclude Include="..\..\..\lib\error_private.h" />
<ClInclude Include="..\..\..\lib\error_public.h" /> <ClInclude Include="..\..\..\lib\error_public.h" />
@ -48,6 +46,7 @@
<ProjectGuid>{8BFD8150-94D5-4BF9-8A50-7BD9929A0850}</ProjectGuid> <ProjectGuid>{8BFD8150-94D5-4BF9-8A50-7BD9929A0850}</ProjectGuid>
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
<RootNamespace>zstdlib</RootNamespace> <RootNamespace>zstdlib</RootNamespace>
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@ -96,7 +95,6 @@
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<TargetName>zstdlib_x86</TargetName> <TargetName>zstdlib_x86</TargetName>
<IntDir>$(Platform)\$(Configuration)\</IntDir> <IntDir>$(Platform)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<IncludePath>$(SolutionDir)..\..\programs\legacy;$(SolutionDir)..\..\lib\legacy;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);</IncludePath> <IncludePath>$(SolutionDir)..\..\programs\legacy;$(SolutionDir)..\..\lib\legacy;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);</IncludePath>
<RunCodeAnalysis>true</RunCodeAnalysis> <RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup> </PropertyGroup>
@ -104,14 +102,12 @@
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<TargetName>zstdlib_x64</TargetName> <TargetName>zstdlib_x64</TargetName>
<IntDir>$(Platform)\$(Configuration)\</IntDir> <IntDir>$(Platform)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<IncludePath>$(SolutionDir)..\..\programs\legacy;$(SolutionDir)..\..\lib\legacy;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);</IncludePath> <IncludePath>$(SolutionDir)..\..\programs\legacy;$(SolutionDir)..\..\lib\legacy;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);</IncludePath>
<RunCodeAnalysis>true</RunCodeAnalysis> <RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
<TargetName>zstdlib_x86</TargetName> <TargetName>zstdlib_x86</TargetName>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(Platform)\$(Configuration)\</IntDir> <IntDir>$(Platform)\$(Configuration)\</IntDir>
<IncludePath>$(SolutionDir)..\..\programs\legacy;$(SolutionDir)..\..\lib\legacy;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);</IncludePath> <IncludePath>$(SolutionDir)..\..\programs\legacy;$(SolutionDir)..\..\lib\legacy;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);</IncludePath>
<RunCodeAnalysis>false</RunCodeAnalysis> <RunCodeAnalysis>false</RunCodeAnalysis>
@ -119,7 +115,6 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
<TargetName>zstdlib_x64</TargetName> <TargetName>zstdlib_x64</TargetName>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(Platform)\$(Configuration)\</IntDir> <IntDir>$(Platform)\$(Configuration)\</IntDir>
<IncludePath>$(SolutionDir)..\..\programs\legacy;$(SolutionDir)..\..\lib\legacy;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);</IncludePath> <IncludePath>$(SolutionDir)..\..\programs\legacy;$(SolutionDir)..\..\lib\legacy;$(SolutionDir)..\..\lib;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);</IncludePath>
<RunCodeAnalysis>false</RunCodeAnalysis> <RunCodeAnalysis>false</RunCodeAnalysis>