aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLode Vandevenne <lode@google.com>2013-03-05 18:01:47 +0100
committerLode Vandevenne <lode@google.com>2013-03-05 18:01:47 +0100
commit981df0fe897c94382b9b963eb72bc36cbc2e729c (patch)
treedc2a75c97f6f054c633d99065e9efc58a09e22ad
parent42afd06de11fca2625b1d2b43a21f252b1b35627 (diff)
downloadzopfli-981df0fe897c94382b9b963eb72bc36cbc2e729c.tar.gz
Add Zopfli prefix to symbols
-rw-r--r--CONTRIBUTORS5
-rw-r--r--README9
-rw-r--r--blocksplitter.c59
-rw-r--r--blocksplitter.h21
-rw-r--r--cache.c52
-rw-r--r--cache.h37
-rw-r--r--deflate.c351
-rw-r--r--deflate.h41
-rw-r--r--gzip_container.c53
-rw-r--r--gzip_container.h6
-rw-r--r--hash.c37
-rw-r--r--hash.h26
-rw-r--r--katajainen.c2
-rw-r--r--katajainen.h2
-rw-r--r--lz77.c158
-rw-r--r--lz77.h50
-rw-r--r--squeeze.c199
-rw-r--r--squeeze.h19
-rw-r--r--tree.c12
-rw-r--r--tree.h17
-rw-r--r--util.c14
-rw-r--r--util.h64
-rw-r--r--zlib_container.c22
-rw-r--r--zlib_container.h6
-rw-r--r--zopfli.c24
25 files changed, 671 insertions, 615 deletions
diff --git a/CONTRIBUTORS b/CONTRIBUTORS
new file mode 100644
index 0000000..7294d3f
--- /dev/null
+++ b/CONTRIBUTORS
@@ -0,0 +1,5 @@
+Mark Adler
+Jyrki Alakuijala
+Daniel Reed
+Péter Szabó
+Lode Vandevenne
diff --git a/README b/README
index d75856f..0f2fe30 100644
--- a/README
+++ b/README
@@ -4,10 +4,11 @@ very good, but slow, deflate or zlib compression.
zopfli.c is separate from the library and contains an example program to create
very well compressed gzip files.
-The basic functions to compress data are Deflate in deflate.h, ZlibCompress in
-zlib_container.h and GzipCompress in gzip_container.h. Use the Options
-object to set parameters that affect the speed and compression. Use the
-InitOptions function to place the default values in the Options first.
+The basic functions to compress data are ZopfliDeflate in deflate.h,
+ZopfliZlibCompress in zlib_container.h and ZopfliGzipCompress in
+gzip_container.h. Use the ZopfliOptions object to set parameters that affect the
+speed and compression. Use the ZopfliInitOptions function to place the default
+values in the ZopfliOptions first.
Deflate creates a valid deflate stream in memory, see:
http://www.ietf.org/rfc/rfc1951.txt
diff --git a/blocksplitter.c b/blocksplitter.c
index 157273e..db3e24a 100644
--- a/blocksplitter.c
+++ b/blocksplitter.c
@@ -42,7 +42,7 @@ double, i is in range start-end (excluding end).
static size_t FindMinimum(FindMinimumFun f, void* context,
size_t start, size_t end) {
if (end - start < 1024) {
- double best = LARGE_FLOAT;
+ double best = ZOPFLI_LARGE_FLOAT;
size_t result = start;
size_t i;
for (i = start; i < end; i++) {
@@ -61,7 +61,7 @@ static size_t FindMinimum(FindMinimumFun f, void* context,
double vp[NUM];
size_t besti;
double best;
- double lastbest = LARGE_FLOAT;
+ double lastbest = ZOPFLI_LARGE_FLOAT;
size_t pos = start;
for (;;) {
@@ -102,9 +102,10 @@ dists: ll77 distances
lstart: start of block
lend: end of block (not inclusive)
*/
-double EstimateCost(const unsigned short* litlens, const unsigned short* dists,
- size_t lstart, size_t lend) {
- return CalculateBlockSize(litlens, dists, lstart, lend, 2);
+static double EstimateCost(const unsigned short* litlens,
+ const unsigned short* dists,
+ size_t lstart, size_t lend) {
+ return ZopfliCalculateBlockSize(litlens, dists, lstart, lend, 2);
}
typedef struct SplitCostContext {
@@ -129,7 +130,7 @@ static double SplitCost(size_t i, void* context) {
static void AddSorted(size_t value, size_t** out, size_t* outsize) {
size_t i;
- APPEND_DATA(value, out, outsize);
+ ZOPFLI_APPEND_DATA(value, out, outsize);
if (*outsize > 0) {
for (i = 0; i < *outsize - 1; i++) {
if ((*out)[i] > value) {
@@ -161,7 +162,7 @@ static void PrintBlockSplitPoints(const unsigned short* litlens,
for (i = 0; i < llsize; i++) {
size_t length = dists[i] == 0 ? 1 : litlens[i];
if (lz77splitpoints[npoints] == i) {
- APPEND_DATA(pos, &splitpoints, &npoints);
+ ZOPFLI_APPEND_DATA(pos, &splitpoints, &npoints);
if (npoints == nlz77points) break;
}
pos += length;
@@ -215,10 +216,11 @@ static int FindLargestSplittableBlock(
return found;
}
-void BlockSplitLZ77(const Options* options,
- const unsigned short* litlens, const unsigned short* dists,
- size_t llsize, size_t maxblocks,
- size_t** splitpoints, size_t* npoints) {
+void ZopfliBlockSplitLZ77(const ZopfliOptions* options,
+ const unsigned short* litlens,
+ const unsigned short* dists,
+ size_t llsize, size_t maxblocks,
+ size_t** splitpoints, size_t* npoints) {
size_t lstart, lend;
size_t i;
size_t llpos = 0;
@@ -280,34 +282,35 @@ void BlockSplitLZ77(const Options* options,
free(done);
}
-void BlockSplit(const Options* options,
- const unsigned char* in, size_t instart, size_t inend,
- size_t maxblocks, size_t** splitpoints, size_t* npoints) {
+void ZopfliBlockSplit(const ZopfliOptions* options,
+ const unsigned char* in, size_t instart, size_t inend,
+ size_t maxblocks, size_t** splitpoints, size_t* npoints) {
size_t pos = 0;
size_t i;
- BlockState s;
+ ZopfliBlockState s;
size_t* lz77splitpoints = 0;
size_t nlz77points = 0;
- LZ77Store store;
+ ZopfliLZ77Store store;
- InitLZ77Store(&store);
+ ZopfliInitLZ77Store(&store);
s.options = options;
s.blockstart = instart;
s.blockend = inend;
-#ifdef USE_LONGEST_MATCH_CACHE
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
s.lmc = 0;
#endif
*npoints = 0;
*splitpoints = 0;
- /* Unintuitively, Using a simple LZ77 method here instead of LZ77Optimal
+ /* Unintuitively, Using a simple LZ77 method here instead of ZopfliLZ77Optimal
results in better blocks. */
- LZ77Greedy(&s, in, instart, inend, &store);
+ ZopfliLZ77Greedy(&s, in, instart, inend, &store);
- BlockSplitLZ77(options, store.litlens, store.dists, store.size, maxblocks,
- &lz77splitpoints, &nlz77points);
+ ZopfliBlockSplitLZ77(options,
+ store.litlens, store.dists, store.size, maxblocks,
+ &lz77splitpoints, &nlz77points);
/* Convert LZ77 positions to positions in the uncompressed input. */
pos = instart;
@@ -315,7 +318,7 @@ void BlockSplit(const Options* options,
for (i = 0; i < store.size; i++) {
size_t length = store.dists[i] == 0 ? 1 : store.litlens[i];
if (lz77splitpoints[*npoints] == i) {
- APPEND_DATA(pos, splitpoints, npoints);
+ ZOPFLI_APPEND_DATA(pos, splitpoints, npoints);
if (*npoints == nlz77points) break;
}
pos += length;
@@ -324,14 +327,16 @@ void BlockSplit(const Options* options,
assert(*npoints == nlz77points);
free(lz77splitpoints);
- CleanLZ77Store(&store);
+ ZopfliCleanLZ77Store(&store);
}
-void BlockSplitSimple(const unsigned char* in, size_t instart, size_t inend,
- size_t blocksize, size_t** splitpoints, size_t* npoints) {
+void ZopfliBlockSplitSimple(const unsigned char* in,
+ size_t instart, size_t inend,
+ size_t blocksize,
+ size_t** splitpoints, size_t* npoints) {
size_t i = instart;
while (i < inend) {
- APPEND_DATA(i, splitpoints, npoints);
+ ZOPFLI_APPEND_DATA(i, splitpoints, npoints);
i += blocksize;
}
(void)in;
diff --git a/blocksplitter.h b/blocksplitter.h
index bb7b231..74b8d7e 100644
--- a/blocksplitter.h
+++ b/blocksplitter.h
@@ -41,10 +41,11 @@ dists: lz77 distances
llsize: size of litlens and dists
maxblocks: set a limit to the amount of blocks. Set to 0 to mean no limit.
*/
-void BlockSplitLZ77(const Options* options,
- const unsigned short* litlens, const unsigned short* dists,
- size_t llsize, size_t maxblocks,
- size_t** splitpoints, size_t* npoints);
+void ZopfliBlockSplitLZ77(const ZopfliOptions* options,
+ const unsigned short* litlens,
+ const unsigned short* dists,
+ size_t llsize, size_t maxblocks,
+ size_t** splitpoints, size_t* npoints);
/*
Does blocksplitting on uncompressed data.
@@ -60,15 +61,17 @@ splitpoints: dynamic array to put the resulting split point coordinates into.
npoints: pointer to amount of splitpoints, for the dynamic array. The amount of
blocks is the amount of splitpoitns + 1.
*/
-void BlockSplit(const Options* options,
- const unsigned char* in, size_t instart, size_t inend,
- size_t maxblocks, size_t** splitpoints, size_t* npoints);
+void ZopfliBlockSplit(const ZopfliOptions* options,
+ const unsigned char* in, size_t instart, size_t inend,
+ size_t maxblocks, size_t** splitpoints, size_t* npoints);
/*
Divides the input into equal blocks, does not even take LZ77 lengths into
account.
*/
-void BlockSplitSimple(const unsigned char* in, size_t instart, size_t inend,
- size_t blocksize, size_t** splitpoints, size_t* npoints);
+void ZopfliBlockSplitSimple(const unsigned char* in,
+ size_t instart, size_t inend,
+ size_t blocksize,
+ size_t** splitpoints, size_t* npoints);
#endif /* ZOPFLI_BLOCKSPLITTER_H_ */
diff --git a/cache.c b/cache.c
index cbc112b..88a49ac 100644
--- a/cache.c
+++ b/cache.c
@@ -23,40 +23,41 @@ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
#include <stdio.h>
#include <stdlib.h>
-#ifdef USE_LONGEST_MATCH_CACHE
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
-void InitLongestMatchCache(size_t blocksize, LongestMatchCache* lmc) {
+void ZopfliInitCache(size_t blocksize, ZopfliLongestMatchCache* lmc) {
size_t i;
lmc->length = (unsigned short*)malloc(sizeof(unsigned short) * blocksize);
lmc->dist = (unsigned short*)malloc(sizeof(unsigned short) * blocksize);
/* Rather large amount of memory. */
- lmc->sublen = (unsigned char*)malloc(NUM_CACHED_LENGTHS * 3 * blocksize);
+ lmc->sublen = (unsigned char*)malloc(ZOPFLI_CACHE_LENGTH * 3 * blocksize);
/* length > 0 and dist 0 is invalid combination, which indicates on purpose
that this cache value is not filled in yet. */
for (i = 0; i < blocksize; i++) lmc->length[i] = 1;
for (i = 0; i < blocksize; i++) lmc->dist[i] = 0;
- for (i = 0; i < NUM_CACHED_LENGTHS * blocksize * 3; i++) lmc->sublen[i] = 0;
+ for (i = 0; i < ZOPFLI_CACHE_LENGTH * blocksize * 3; i++) lmc->sublen[i] = 0;
}
-void CleanLongestMatchCache(LongestMatchCache* lmc) {
+void ZopfliCleanCache(ZopfliLongestMatchCache* lmc) {
free(lmc->length);
free(lmc->dist);
free(lmc->sublen);
}
-void SublenToCache(const unsigned short* sublen, size_t pos, size_t length,
- LongestMatchCache* lmc) {
+void ZopfliSublenToCache(const unsigned short* sublen,
+ size_t pos, size_t length,
+ ZopfliLongestMatchCache* lmc) {
size_t i;
size_t j = 0;
unsigned bestlength = 0;
unsigned char* cache;
-#if NUM_CACHED_LENGTHS == 0
+#if ZOPFLI_CACHE_LENGTH == 0
return;
#endif
- cache = &lmc->sublen[NUM_CACHED_LENGTHS * pos * 3];
+ cache = &lmc->sublen[ZOPFLI_CACHE_LENGTH * pos * 3];
if (length < 3) return;
for (i = 3; i <= length; i++) {
if (i == length || sublen[i] != sublen[i + 1]) {
@@ -65,30 +66,31 @@ void SublenToCache(const unsigned short* sublen, size_t pos, size_t length,
cache[j * 3 + 2] = (sublen[i] >> 8) % 256;
bestlength = i;
j++;
- if (j >= NUM_CACHED_LENGTHS) break;
+ if (j >= ZOPFLI_CACHE_LENGTH) break;
}
}
- if (j < NUM_CACHED_LENGTHS) {
+ if (j < ZOPFLI_CACHE_LENGTH) {
assert(bestlength == length);
- cache[(NUM_CACHED_LENGTHS - 1) * 3] = bestlength - 3;
+ cache[(ZOPFLI_CACHE_LENGTH - 1) * 3] = bestlength - 3;
} else {
assert(bestlength <= length);
}
- assert(bestlength == MaxCachedSublen(lmc, pos, length));
+ assert(bestlength == ZopfliMaxCachedSublen(lmc, pos, length));
}
-void CacheToSublen(const LongestMatchCache* lmc, size_t pos, size_t length,
- unsigned short* sublen) {
+void ZopfliCacheToSublen(const ZopfliLongestMatchCache* lmc,
+ size_t pos, size_t length,
+ unsigned short* sublen) {
size_t i, j;
- unsigned maxlength = MaxCachedSublen(lmc, pos, length);
+ unsigned maxlength = ZopfliMaxCachedSublen(lmc, pos, length);
unsigned prevlength = 0;
unsigned char* cache;
-#if NUM_CACHED_LENGTHS == 0
+#if ZOPFLI_CACHE_LENGTH == 0
return;
#endif
if (length < 3) return;
- cache = &lmc->sublen[NUM_CACHED_LENGTHS * pos * 3];
- for (j = 0; j < NUM_CACHED_LENGTHS; j++) {
+ cache = &lmc->sublen[ZOPFLI_CACHE_LENGTH * pos * 3];
+ for (j = 0; j < ZOPFLI_CACHE_LENGTH; j++) {
unsigned length = cache[j * 3] + 3;
unsigned dist = cache[j * 3 + 1] + 256 * cache[j * 3 + 2];
for (i = prevlength; i <= length; i++) {
@@ -102,16 +104,16 @@ void CacheToSublen(const LongestMatchCache* lmc, size_t pos, size_t length,
/*
Returns the length up to which could be stored in the cache.
*/
-unsigned MaxCachedSublen(const LongestMatchCache* lmc,
- size_t pos, size_t length) {
+unsigned ZopfliMaxCachedSublen(const ZopfliLongestMatchCache* lmc,
+ size_t pos, size_t length) {
unsigned char* cache;
-#if NUM_CACHED_LENGTHS == 0
+#if ZOPFLI_CACHE_LENGTH == 0
return 0;
#endif
- cache = &lmc->sublen[NUM_CACHED_LENGTHS * pos * 3];
+ cache = &lmc->sublen[ZOPFLI_CACHE_LENGTH * pos * 3];
(void)length;
if (cache[1] == 0 && cache[2] == 0) return 0; /* No sublen cached. */
- return cache[(NUM_CACHED_LENGTHS - 1) * 3] + 3;
+ return cache[(ZOPFLI_CACHE_LENGTH - 1) * 3] + 3;
}
-#endif /* USE_LONGEST_MATCH_CACHE */
+#endif /* ZOPFLI_LONGEST_MATCH_CACHE */
diff --git a/cache.h b/cache.h
index 9f6b81e..5ca0c50 100644
--- a/cache.h
+++ b/cache.h
@@ -18,7 +18,7 @@ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
*/
/*
-The cache that speeds up FindLongestMatch of lz77.c.
+The cache that speeds up ZopfliFindLongestMatch of lz77.c.
*/
#ifndef ZOPFLI_CACHE_H_
@@ -26,38 +26,41 @@ The cache that speeds up FindLongestMatch of lz77.c.
#include "util.h"
-#ifdef USE_LONGEST_MATCH_CACHE
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
/*
-Cache used by FindLongestMatch to remember previously found length/dist values.
+Cache used by ZopfliFindLongestMatch to remember previously found length/dist
+values.
This is needed because the squeeze runs will ask these values multiple times for
the same position.
Uses large amounts of memory, since it has to remember the distance belonging
to every possible shorter-than-the-best length (the so called "sublen" array).
*/
-typedef struct LongestMatchCache {
+typedef struct ZopfliLongestMatchCache {
unsigned short* length;
unsigned short* dist;
- unsigned char* sublen; /* For each length, the distance */
-} LongestMatchCache;
+ unsigned char* sublen;
+} ZopfliLongestMatchCache;
-/* Initializes the LongestMatchCache. */
-void InitLongestMatchCache(size_t blocksize, LongestMatchCache* lmc);
+/* Initializes the ZopfliLongestMatchCache. */
+void ZopfliInitCache(size_t blocksize, ZopfliLongestMatchCache* lmc);
-/* Frees up the memory of the LongestMatchCache. */
-void CleanLongestMatchCache(LongestMatchCache* lmc);
+/* Frees up the memory of the ZopfliLongestMatchCache. */
+void ZopfliCleanCache(ZopfliLongestMatchCache* lmc);
/* Stores sublen array in the cache. */
-void SublenToCache(const unsigned short* sublen, size_t pos, size_t length,
- LongestMatchCache* lmc);
+void ZopfliSublenToCache(const unsigned short* sublen,
+ size_t pos, size_t length,
+ ZopfliLongestMatchCache* lmc);
/* Extracts sublen array from the cache. */
-void CacheToSublen(const LongestMatchCache* lmc, size_t pos, size_t length,
- unsigned short* sublen);
+void ZopfliCacheToSublen(const ZopfliLongestMatchCache* lmc,
+ size_t pos, size_t length,
+ unsigned short* sublen);
/* Returns the length up to which could be stored in the cache. */
-unsigned MaxCachedSublen(const LongestMatchCache* lmc,
- size_t pos, size_t length);
+unsigned ZopfliMaxCachedSublen(const ZopfliLongestMatchCache* lmc,
+ size_t pos, size_t length);
-#endif /* USE_LONGEST_MATCH_CACHE */
+#endif /* ZOPFLI_LONGEST_MATCH_CACHE */
#endif /* ZOPFLI_CACHE_H_ */
diff --git a/deflate.c b/deflate.c
index ada9a40..8ff1b8f 100644
--- a/deflate.c
+++ b/deflate.c
@@ -35,7 +35,7 @@ Exposed DeflatePart() as an external function.
static void AddBit(int bit,
unsigned char* bp, unsigned char** out, size_t* outsize) {
- if (((*bp) & 7) == 0) APPEND_DATA(0, out, outsize);
+ if (((*bp) & 7) == 0) ZOPFLI_APPEND_DATA(0, out, outsize);
(*out)[*outsize - 1] |= bit << ((*bp) & 7);
(*bp)++;
}
@@ -46,7 +46,7 @@ static void AddBits(unsigned symbol, unsigned length,
unsigned i;
for (i = 0; i < length; i++) {
unsigned bit = (symbol >> i) & 1;
- if (((*bp) & 7) == 0) APPEND_DATA(0, out, outsize);
+ if (((*bp) & 7) == 0) ZOPFLI_APPEND_DATA(0, out, outsize);
(*out)[*outsize - 1] |= bit << ((*bp) & 7);
(*bp)++;
}
@@ -63,7 +63,7 @@ static void AddHuffmanBits(unsigned symbol, unsigned length,
unsigned i;
for (i = 0; i < length; i++) {
unsigned bit = (symbol >> (length - i - 1)) & 1;
- if (((*bp) & 7) == 0) APPEND_DATA(0, out, outsize);
+ if (((*bp) & 7) == 0) ZOPFLI_APPEND_DATA(0, out, outsize);
(*out)[*outsize - 1] |= bit << ((*bp) & 7);
(*bp)++;
}
@@ -96,26 +96,10 @@ static void PatchDistanceCodesForBuggyDecoders(unsigned* d_lengths) {
}
}
-/*
-Gives the exact size of the tree, in bits, as it will be encoded in DEFLATE.
-*/
-size_t CalculateTreeSize(const unsigned* ll_lengths, const unsigned* d_lengths,
- size_t* ll_counts, size_t* d_counts) {
- unsigned char* dummy = 0;
- size_t dummysize = 0;
- unsigned char bp = 0;
-
- (void)ll_counts;
- (void)d_counts;
-
- AddDynamicTree(ll_lengths, d_lengths, &bp, &dummy, &dummysize);
- free(dummy);
-
- return dummysize * 8 + (bp & 7);
-}
-
-void AddDynamicTree(const unsigned* ll_lengths, const unsigned* d_lengths,
- unsigned char* bp, unsigned char** out, size_t* outsize) {
+static void AddDynamicTree(const unsigned* ll_lengths,
+ const unsigned* d_lengths,
+ unsigned char* bp,
+ unsigned char** out, size_t* outsize) {
unsigned* lld_lengths = 0; /* All litlen and dist lengthts with ending zeros
trimmed together in one array. */
unsigned lld_total; /* Size of lld_lengths. */
@@ -159,37 +143,37 @@ void AddDynamicTree(const unsigned* ll_lengths, const unsigned* d_lengths,
if (lld_lengths[i] == 0) {
if (count > 10) {
if (count > 138) count = 138;
- APPEND_DATA(18, &rle, &rle_size);
- APPEND_DATA(count - 11, &rle_bits, &rle_bits_size);
+ ZOPFLI_APPEND_DATA(18, &rle, &rle_size);
+ ZOPFLI_APPEND_DATA(count - 11, &rle_bits, &rle_bits_size);
} else {
- APPEND_DATA(17, &rle, &rle_size);
- APPEND_DATA(count - 3, &rle_bits, &rle_bits_size);
+ ZOPFLI_APPEND_DATA(17, &rle, &rle_size);
+ ZOPFLI_APPEND_DATA(count - 3, &rle_bits, &rle_bits_size);
}
} else {
unsigned repeat = count - 1; /* Since the first one is hardcoded. */
- APPEND_DATA(lld_lengths[i], &rle, &rle_size);
- APPEND_DATA(0, &rle_bits, &rle_bits_size);
+ ZOPFLI_APPEND_DATA(lld_lengths[i], &rle, &rle_size);
+ ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
while (repeat >= 6) {
- APPEND_DATA(16, &rle, &rle_size);
- APPEND_DATA(6 - 3, &rle_bits, &rle_bits_size);
+ ZOPFLI_APPEND_DATA(16, &rle, &rle_size);
+ ZOPFLI_APPEND_DATA(6 - 3, &rle_bits, &rle_bits_size);
repeat -= 6;
}
if (repeat >= 3) {
- APPEND_DATA(16, &rle, &rle_size);
- APPEND_DATA(3 - 3, &rle_bits, &rle_bits_size);
+ ZOPFLI_APPEND_DATA(16, &rle, &rle_size);
+ ZOPFLI_APPEND_DATA(3 - 3, &rle_bits, &rle_bits_size);
repeat -= 3;
}
while (repeat != 0) {
- APPEND_DATA(lld_lengths[i], &rle, &rle_size);
- APPEND_DATA(0, &rle_bits, &rle_bits_size);
+ ZOPFLI_APPEND_DATA(lld_lengths[i], &rle, &rle_size);
+ ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
repeat--;
}
}
i += count - 1;
} else {
- APPEND_DATA(lld_lengths[i], &rle, &rle_size);
- APPEND_DATA(0, &rle_bits, &rle_bits_size);
+ ZOPFLI_APPEND_DATA(lld_lengths[i], &rle, &rle_size);
+ ZOPFLI_APPEND_DATA(0, &rle_bits, &rle_bits_size);
}
assert(rle[rle_size - 1] <= 18);
}
@@ -201,8 +185,8 @@ void AddDynamicTree(const unsigned* ll_lengths, const unsigned* d_lengths,
clcounts[rle[i]]++;
}
- CalculateBitLengths(clcounts, 19, 7, clcl);
- LengthsToSymbols(clcl, 19, 7, clsymbols);
+ ZopfliCalculateBitLengths(clcounts, 19, 7, clcl);
+ ZopfliLengthsToSymbols(clcl, 19, 7, clsymbols);
hclen = 15;
/* Trim zeros. */
@@ -231,16 +215,37 @@ void AddDynamicTree(const unsigned* ll_lengths, const unsigned* d_lengths,
}
/*
+Gives the exact size of the tree, in bits, as it will be encoded in DEFLATE.
+*/
+static size_t CalculateTreeSize(const unsigned* ll_lengths,
+ const unsigned* d_lengths,
+ size_t* ll_counts, size_t* d_counts) {
+ unsigned char* dummy = 0;
+ size_t dummysize = 0;
+ unsigned char bp = 0;
+
+ (void)ll_counts;
+ (void)d_counts;
+
+ AddDynamicTree(ll_lengths, d_lengths, &bp, &dummy, &dummysize);
+ free(dummy);
+
+ return dummysize * 8 + (bp & 7);
+}
+
+/*
Adds all lit/len and dist codes from the lists as huffman symbols. Does not add
end code 256. expected_data_size is the uncompressed block size, used for
assert, but you can set it to 0 to not do the assertion.
*/
-void AddLZ77Data(const unsigned short* litlens, const unsigned short* dists,
- size_t lstart, size_t lend,
- size_t expected_data_size,
- const unsigned* ll_symbols, const unsigned* ll_lengths,
- const unsigned* d_symbols, const unsigned* d_lengths,
- unsigned char* bp, unsigned char** out, size_t* outsize) {
+static void AddLZ77Data(const unsigned short* litlens,
+ const unsigned short* dists,
+ size_t lstart, size_t lend,
+ size_t expected_data_size,
+ const unsigned* ll_symbols, const unsigned* ll_lengths,
+ const unsigned* d_symbols, const unsigned* d_lengths,
+ unsigned char* bp,
+ unsigned char** out, size_t* outsize) {
size_t testlength = 0;
size_t i;
@@ -253,16 +258,18 @@ void AddLZ77Data(const unsigned short* litlens, const unsigned short* dists,
AddHuffmanBits(ll_symbols[litlen], ll_lengths[litlen], bp, out, outsize);
testlength++;
} else {
- unsigned lls = GetLengthSymbol(litlen);
- unsigned ds = GetDistSymbol(dist);
+ unsigned lls = ZopfliGetLengthSymbol(litlen);
+ unsigned ds = ZopfliGetDistSymbol(dist);
assert(litlen >= 3 && litlen <= 288);
assert(ll_lengths[lls] > 0);
assert(d_lengths[ds] > 0);
AddHuffmanBits(ll_symbols[lls], ll_lengths[lls], bp, out, outsize);
- AddBits(GetLengthExtraBitsValue(litlen), GetLengthExtraBits(litlen),
+ AddBits(ZopfliGetLengthExtraBitsValue(litlen),
+ ZopfliGetLengthExtraBits(litlen),
bp, out, outsize);
AddHuffmanBits(d_symbols[ds], d_lengths[ds], bp, out, outsize);
- AddBits(GetDistExtraBitsValue(dist), GetDistExtraBits(dist),
+ AddBits(ZopfliGetDistExtraBitsValue(dist),
+ ZopfliGetDistExtraBits(dist),
bp, out, outsize);
testlength += litlen;
}
@@ -270,7 +277,7 @@ void AddLZ77Data(const unsigned short* litlens, const unsigned short* dists,
assert(expected_data_size == 0 || testlength == expected_data_size);
}
-void GetFixedTree(unsigned* ll_lengths, unsigned* d_lengths) {
+static void GetFixedTree(unsigned* ll_lengths, unsigned* d_lengths) {
size_t i;
for (i = 0; i < 144; i++) ll_lengths[i] = 8;
for (i = 144; i < 256; i++) ll_lengths[i] = 9;
@@ -282,30 +289,30 @@ void GetFixedTree(unsigned* ll_lengths, unsigned* d_lengths) {
/*
Calculates size of the part after the header and tree of an LZ77 block, in bits.
*/
-size_t CalculateBlockSymbolSize(const unsigned* ll_lengths,
- const unsigned* d_lengths,
- const unsigned short* litlens,
- const unsigned short* dists,
- size_t lstart, size_t lend) {
+static size_t CalculateBlockSymbolSize(const unsigned* ll_lengths,
+ const unsigned* d_lengths,
+ const unsigned short* litlens,
+ const unsigned short* dists,
+ size_t lstart, size_t lend) {
size_t result = 0;
size_t i;
for (i = lstart; i < lend; i++) {
if (dists[i] == 0) {
result += ll_lengths[litlens[i]];
} else {
- result += ll_lengths[GetLengthSymbol(litlens[i])];
- result += d_lengths[GetDistSymbol(dists[i])];
- result += GetLengthExtraBits(litlens[i]);
- result += GetDistExtraBits(dists[i]);
+ result += ll_lengths[ZopfliGetLengthSymbol(litlens[i])];
+ result += d_lengths[ZopfliGetDistSymbol(dists[i])];
+ result += ZopfliGetLengthExtraBits(litlens[i]);
+ result += ZopfliGetDistExtraBits(dists[i]);
}
}
result += ll_lengths[256]; /*end symbol*/
return result;
}
-double CalculateBlockSize(
- const unsigned short* litlens, const unsigned short* dists,
- size_t lstart, size_t lend, int btype) {
+double ZopfliCalculateBlockSize(const unsigned short* litlens,
+ const unsigned short* dists,
+ size_t lstart, size_t lend, int btype) {
size_t ll_counts[288];
size_t d_counts[32];
@@ -319,9 +326,9 @@ double CalculateBlockSize(
if(btype == 1) {
GetFixedTree(ll_lengths, d_lengths);
} else {
- GetLZ77Counts(litlens, dists, lstart, lend, ll_counts, d_counts);
- CalculateBitLengths(ll_counts, 288, 15, ll_lengths);
- CalculateBitLengths(d_counts, 32, 15, d_lengths);
+ ZopfliLZ77Counts(litlens, dists, lstart, lend, ll_counts, d_counts);
+ ZopfliCalculateBitLengths(ll_counts, 288, 15, ll_lengths);
+ ZopfliCalculateBitLengths(d_counts, 32, 15, d_lengths);
PatchDistanceCodesForBuggyDecoders(d_lengths);
result += CalculateTreeSize(ll_lengths, d_lengths, ll_counts, d_counts);
}
@@ -338,8 +345,9 @@ options: global program options
btype: the block type, must be 1 or 2
final: whether to set the "final" bit on this block, must be the last block
litlens: literal/length array of the LZ77 data, in the same format as in
- LZ77Store.
-dists: distance array of the LZ77 data, in the same format as in LZ77Store.
+ ZopfliLZ77Store.
+dists: distance array of the LZ77 data, in the same format as in
+ ZopfliLZ77Store.
lstart: where to start in the LZ77 data
lend: where to end in the LZ77 data (not inclusive)
expected_data_size: the uncompressed block size, used for assert, but you can
@@ -348,11 +356,12 @@ bp: output bit pointer
out: dynamic output array to append to
outsize: dynamic output array size
*/
-void AddLZ77Block(const Options* options, int btype, int final,
- const unsigned short* litlens, const unsigned short* dists,
- size_t lstart, size_t lend,
- size_t expected_data_size,
- unsigned char* bp, unsigned char** out, size_t* outsize) {
+static void AddLZ77Block(const ZopfliOptions* options, int btype, int final,
+ const unsigned short* litlens,
+ const unsigned short* dists,
+ size_t lstart, size_t lend,
+ size_t expected_data_size,
+ unsigned char* bp, unsigned char** out, size_t* outsize) {
size_t ll_counts[288];
size_t d_counts[32];
unsigned ll_lengths[288];
@@ -375,9 +384,9 @@ void AddLZ77Block(const Options* options, int btype, int final,
/* Dynamic block. */
unsigned detect_tree_size;
assert(btype == 2);
- GetLZ77Counts(litlens, dists, lstart, lend, ll_counts, d_counts);
- CalculateBitLengths(ll_counts, 288, 15, ll_lengths);
- CalculateBitLengths(d_counts, 32, 15, d_lengths);
+ ZopfliLZ77Counts(litlens, dists, lstart, lend, ll_counts, d_counts);
+ ZopfliCalculateBitLengths(ll_counts, 288, 15, ll_lengths);
+ ZopfliCalculateBitLengths(d_counts, 32, 15, d_lengths);
PatchDistanceCodesForBuggyDecoders(d_lengths);
detect_tree_size = *outsize;
AddDynamicTree(ll_lengths, d_lengths, bp, out, outsize);
@@ -391,8 +400,8 @@ void AddLZ77Block(const Options* options, int btype, int final,
for (i = 0; i < 32; i++) assert(d_counts[i] == 0 || d_lengths[i] > 0);
}
- LengthsToSymbols(ll_lengths, 288, 15, ll_symbols);
- LengthsToSymbols(d_lengths, 32, 15, d_symbols);
+ ZopfliLengthsToSymbols(ll_lengths, 288, 15, ll_symbols);
+ ZopfliLengthsToSymbols(d_lengths, 32, 15, d_symbols);
detect_block_size = *outsize;
AddLZ77Data(litlens, dists, lstart, lend, expected_data_size,
@@ -412,43 +421,45 @@ void AddLZ77Block(const Options* options, int btype, int final,
}
}
-void DeflateDynamicBlock(const Options* options, int final,
- const unsigned char* in, size_t instart, size_t inend,
- unsigned char* bp,
- unsigned char** out, size_t* outsize) {
- BlockState s;
+static void DeflateDynamicBlock(const ZopfliOptions* options, int final,
+ const unsigned char* in,
+ size_t instart, size_t inend,
+ unsigned char* bp,
+ unsigned char** out, size_t* outsize) {
+ ZopfliBlockState s;
size_t blocksize = inend - instart;
- LZ77Store store;
+ ZopfliLZ77Store store;
int btype = 2;
- InitLZ77Store(&store);
+ ZopfliInitLZ77Store(&store);
s.options = options;
s.blockstart = instart;
s.blockend = inend;
-#ifdef USE_LONGEST_MATCH_CACHE
- s.lmc = (LongestMatchCache*)malloc(sizeof(LongestMatchCache));
- InitLongestMatchCache(blocksize, s.lmc);
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
+ s.lmc = (ZopfliLongestMatchCache*)malloc(sizeof(ZopfliLongestMatchCache));
+ ZopfliInitCache(blocksize, s.lmc);
#endif
- LZ77Optimal(&s, in, instart, inend, &store);
+ ZopfliLZ77Optimal(&s, in, instart, inend, &store);
/* For small block, encoding with fixed tree can be smaller. For large block,
don't bother doing this expensive test, dynamic tree will be better.*/
if (store.size < 1000) {
double dyncost, fixedcost;
- LZ77Store fixedstore;
- InitLZ77Store(&fixedstore);
- LZ77OptimalFixed(&s, in, instart, inend, &fixedstore);
- dyncost = CalculateBlockSize(store.litlens, store.dists, 0, store.size, 2);
- fixedcost = CalculateBlockSize(fixedstore.litlens, fixedstore.dists,
+ ZopfliLZ77Store fixedstore;
+ ZopfliInitLZ77Store(&fixedstore);
+ ZopfliLZ77OptimalFixed(&s, in, instart, inend, &fixedstore);
+ dyncost = ZopfliCalculateBlockSize(store.litlens, store.dists,
+ 0, store.size, 2);
+ fixedcost = ZopfliCalculateBlockSize(fixedstore.litlens, fixedstore.dists,
0, fixedstore.size, 1);
if (fixedcost < dyncost) {
btype = 1;
- CleanLZ77Store(&store);
+ ZopfliCleanLZ77Store(&store);
store = fixedstore;
} else {
- CleanLZ77Store(&fixedstore);
+ ZopfliCleanLZ77Store(&fixedstore);
}
}
@@ -456,48 +467,49 @@ void DeflateDynamicBlock(const Options* options, int final,
store.litlens, store.dists, 0, store.size,
blocksize, bp, out, outsize);
-#ifdef USE_LONGEST_MATCH_CACHE
- CleanLongestMatchCache(s.lmc);
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
+ ZopfliCleanCache(s.lmc);
free(s.lmc);
#endif
- CleanLZ77Store(&store);
+ ZopfliCleanLZ77Store(&store);
}
-void DeflateFixedBlock(const Options* options, int final,
- const unsigned char* in, size_t instart, size_t inend,
- unsigned char* bp,
- unsigned char** out, size_t* outsize) {
- BlockState s;
+static void DeflateFixedBlock(const ZopfliOptions* options, int final,
+ const unsigned char* in,
+ size_t instart, size_t inend,
+ unsigned char* bp,
+ unsigned char** out, size_t* outsize) {
+ ZopfliBlockState s;
size_t blocksize = inend - instart;
- LZ77Store store;
+ ZopfliLZ77Store store;
- InitLZ77Store(&store);
+ ZopfliInitLZ77Store(&store);
s.options = options;
s.blockstart = instart;
s.blockend = inend;
-#ifdef USE_LONGEST_MATCH_CACHE
- s.lmc = (LongestMatchCache*)malloc(sizeof(LongestMatchCache));
- InitLongestMatchCache(blocksize, s.lmc);
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
+ s.lmc = (ZopfliLongestMatchCache*)malloc(sizeof(ZopfliLongestMatchCache));
+ ZopfliInitCache(blocksize, s.lmc);
#endif
- LZ77OptimalFixed(&s, in, instart, inend, &store);
+ ZopfliLZ77OptimalFixed(&s, in, instart, inend, &store);
AddLZ77Block(s.options, 1, final, store.litlens, store.dists, 0, store.size,
blocksize, bp, out, outsize);
-#ifdef USE_LONGEST_MATCH_CACHE
- CleanLongestMatchCache(s.lmc);
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
+ ZopfliCleanCache(s.lmc);
free(s.lmc);
#endif
- CleanLZ77Store(&store);
+ ZopfliCleanLZ77Store(&store);
}
-void DeflateNonCompressedBlock(const Options* options, int final,
- const unsigned char* in, size_t instart,
- size_t inend,
- unsigned char* bp,
- unsigned char** out, size_t* outsize) {
+static void DeflateNonCompressedBlock(const ZopfliOptions* options, int final,
+ const unsigned char* in, size_t instart,
+ size_t inend,
+ unsigned char* bp,
+ unsigned char** out, size_t* outsize) {
size_t i;
size_t blocksize = inend - instart;
unsigned short nlen = ~blocksize;
@@ -513,20 +525,21 @@ void DeflateNonCompressedBlock(const Options* options, int final,
/* Any bits of input up to the next byte boundary are ignored. */
*bp = 0;
- APPEND_DATA(blocksize % 256, out, outsize);
- APPEND_DATA((blocksize / 256) % 256, out, outsize);
- APPEND_DATA(nlen % 256, out, outsize);
- APPEND_DATA((nlen / 256) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA(blocksize % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((blocksize / 256) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA(nlen % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((nlen / 256) % 256, out, outsize);
for (i = instart; i < inend; i++) {
- APPEND_DATA(in[i], out, outsize);
+ ZOPFLI_APPEND_DATA(in[i], out, outsize);
}
}
-void DeflateBlock(const Options* options,
- int btype, int final,
- const unsigned char* in, size_t instart, size_t inend,
- unsigned char* bp, unsigned char** out, size_t* outsize) {
+static void DeflateBlock(const ZopfliOptions* options,
+ int btype, int final,
+ const unsigned char* in, size_t instart, size_t inend,
+ unsigned char* bp,
+ unsigned char** out, size_t* outsize) {
if (btype == 0) {
DeflateNonCompressedBlock(
options, final, in, instart, inend, bp, out, outsize);
@@ -541,24 +554,25 @@ void DeflateBlock(const Options* options,
/*
Does squeeze strategy where first block splitting is done, then each block is
squeezed.
-Parameters: see description of the Deflate function.
+Parameters: see description of the ZopfliDeflate function.
*/
-void DeflateSplittingFirst(const Options* options, int btype, int final,
- const unsigned char* in,
- size_t instart, size_t inend,
- unsigned char* bp,
- unsigned char** out, size_t* outsize) {
+static void DeflateSplittingFirst(const ZopfliOptions* options,
+ int btype, int final,
+ const unsigned char* in,
+ size_t instart, size_t inend,
+ unsigned char* bp,
+ unsigned char** out, size_t* outsize) {
size_t i;
size_t* splitpoints = 0;
size_t npoints = 0;
if (btype == 0) {
- BlockSplitSimple(in, instart, inend, 65535, &splitpoints, &npoints);
+ ZopfliBlockSplitSimple(in, instart, inend, 65535, &splitpoints, &npoints);
} else if (btype == 1) {
/* If all blocks are fixed tree, splitting into separate blocks only
increases the total size. Leave npoints at 0, this represents 1 block. */
} else {
- BlockSplit(options, in, instart, inend,
- options->blocksplittingmax, &splitpoints, &npoints);
+ ZopfliBlockSplit(options, in, instart, inend,
+ options->blocksplittingmax, &splitpoints, &npoints);
}
for (i = 0; i <= npoints; i++) {
@@ -574,16 +588,17 @@ void DeflateSplittingFirst(const Options* options, int btype, int final,
/*
Does squeeze strategy where first the best possible lz77 is done, and then based
on that data, block splitting is done.
-Parameters: see description of the Deflate function.
+Parameters: see description of the ZopfliDeflate function.
*/
-void DeflateSplittingLast(const Options* options, int btype, int final,
- const unsigned char* in,
- size_t instart, size_t inend,
- unsigned char* bp,
- unsigned char** out, size_t* outsize) {
+static void DeflateSplittingLast(const ZopfliOptions* options,
+ int btype, int final,
+ const unsigned char* in,
+ size_t instart, size_t inend,
+ unsigned char* bp,
+ unsigned char** out, size_t* outsize) {
size_t i;
- BlockState s;
- LZ77Store store;
+ ZopfliBlockState s;
+ ZopfliLZ77Store store;
size_t* splitpoints = 0;
size_t npoints = 0;
@@ -596,29 +611,29 @@ void DeflateSplittingLast(const Options* options, int btype, int final,
}
assert(btype == 1 || btype == 2);
- InitLZ77Store(&store);
+ ZopfliInitLZ77Store(&store);
s.options = options;
s.blockstart = instart;
s.blockend = inend;
-#ifdef USE_LONGEST_MATCH_CACHE
- s.lmc = (LongestMatchCache*)malloc(sizeof(LongestMatchCache));
- InitLongestMatchCache(inend - instart, s.lmc);
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
+ s.lmc = (ZopfliLongestMatchCache*)malloc(sizeof(ZopfliLongestMatchCache));
+ ZopfliInitCache(inend - instart, s.lmc);
#endif
if (btype == 2) {
- LZ77Optimal(&s, in, instart, inend, &store);
+ ZopfliLZ77Optimal(&s, in, instart, inend, &store);
} else {
assert (btype == 1);
- LZ77OptimalFixed(&s, in, instart, inend, &store);
+ ZopfliLZ77OptimalFixed(&s, in, instart, inend, &store);
}
if (btype == 1) {
/* If all blocks are fixed tree, splitting into separate blocks only
increases the total size. Leave npoints at 0, this represents 1 block. */
} else {
- BlockSplitLZ77(options, store.litlens, store.dists, store.size,
- options->blocksplittingmax, &splitpoints, &npoints);
+ ZopfliBlockSplitLZ77(options, store.litlens, store.dists, store.size,
+ options->blocksplittingmax, &splitpoints, &npoints);
}
for (i = 0; i <= npoints; i++) {
@@ -629,26 +644,27 @@ void DeflateSplittingLast(const Options* options, int btype, int final,
bp, out, outsize);
}
-#ifdef USE_LONGEST_MATCH_CACHE
- CleanLongestMatchCache(s.lmc);
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
+ ZopfliCleanCache(s.lmc);
free(s.lmc);
#endif
- CleanLZ77Store(&store);
+ ZopfliCleanLZ77Store(&store);
}
/*
-Deflate a part, to allow Deflate() to use multiple master blocks if needed.
+Deflate a part, to allow ZopfliDeflate() to use multiple master blocks if
+needed.
It is possible to call this function multiple times in a row, shifting
instart and inend to next bytes of the data. If instart is larger than 0, then
previous bytes are used as the initial dictionary for LZ77.
This function will usually output multiple deflate blocks. If final is 1, then
the final bit will be set on the last block.
*/
-void DeflatePart(const Options* options, int btype, int final,
- const unsigned char* in, size_t instart, size_t inend,
- unsigned char* bp, unsigned char** out,
- size_t* outsize) {
+void ZopfliDeflatePart(const ZopfliOptions* options, int btype, int final,
+ const unsigned char* in, size_t instart, size_t inend,
+ unsigned char* bp, unsigned char** out,
+ size_t* outsize) {
if (options->blocksplitting) {
if (options->blocksplittinglast) {
DeflateSplittingLast(options, btype, final, in, instart, inend,
@@ -662,18 +678,19 @@ void DeflatePart(const Options* options, int btype, int final,
}
}
-void Deflate(const Options* options, int btype, int final,
- const unsigned char* in, size_t insize,
- unsigned char* bp, unsigned char** out, size_t* outsize) {
-#if MASTER_BLOCK_SIZE == 0
- DeflatePart(options, btype, final, in, 0, insize, bp, out, outsize);
+void ZopfliDeflate(const ZopfliOptions* options, int btype, int final,
+ const unsigned char* in, size_t insize,
+ unsigned char* bp, unsigned char** out, size_t* outsize) {
+#if ZOPFLI_MASTER_BLOCK_SIZE == 0
+ ZopfliDeflatePart(options, btype, final, in, 0, insize, bp, out, outsize);
#else
size_t i = 0;
while (i < insize) {
- int masterfinal = (i + MASTER_BLOCK_SIZE >= insize);
+ int masterfinal = (i + ZOPFLI_MASTER_BLOCK_SIZE >= insize);
int final2 = final && masterfinal;
- size_t size = masterfinal ? insize - i : MASTER_BLOCK_SIZE;
- DeflatePart(options, btype, final2, in, i, i + size, bp, out, outsize);
+ size_t size = masterfinal ? insize - i : ZOPFLI_MASTER_BLOCK_SIZE;
+ ZopfliDeflatePart(options, btype, final2,
+ in, i, i + size, bp, out, outsize);
i += size;
}
#endif
diff --git a/deflate.h b/deflate.h
index 542358f..5c54b6d 100644
--- a/deflate.h
+++ b/deflate.h
@@ -17,16 +17,12 @@ Author: lode.vandevenne@gmail.com (Lode Vandevenne)
Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
*/
-/*
-Modified by madler@alumni.caltech.edu (Mark Adler)
-Exposed DeflatePart() as an external function.
-*/
-
#ifndef ZOPFLI_DEFLATE_H_
#define ZOPFLI_DEFLATE_H_
/*
-Functions to compress compatible with the deflate specification.
+Functions to compress according to the DEFLATE specification, using the
+"squeeze" LZ77 compression backend.
*/
#include "util.h"
@@ -45,11 +41,7 @@ btype: the deflate block type. Use 2 for best compression.
final: whether this is the last section of the input, sets the final bit to the
last deflate block.
in: the input bytes
-insize (Deflate() only): number of input bytes
-instart (DeflatePart() only): offset of the start of the data to compress at in
- if instart is not zero, then the data preceding instart will be used as the
- LZ77 dictionary
-inend (DeflatePart() only): offset + 1 of the end of the data to compress at in
+insize: number of input bytes
bp: bit pointer for the output array. This must initially be 0, and for
consecutive calls must be reused (it can have values from 0-7). This is
because deflate appends blocks as bit-based data, rather than on byte
@@ -58,20 +50,19 @@ out: pointer to the dynamic output array to which the result is appended. Must
be freed after use.
outsize: pointer to the dynamic output array size.
*/
-void Deflate(const Options* options, int btype, int final,
- const unsigned char* in, size_t insize,
- unsigned char* bp, unsigned char** out, size_t* outsize);
-void DeflatePart(const Options* options, int btype, int final,
- const unsigned char* in, size_t instart, size_t inend,
- unsigned char* bp, unsigned char** out,
- size_t* outsize);
+void ZopfliDeflate(const ZopfliOptions* options, int btype, int final,
+ const unsigned char* in, size_t insize,
+ unsigned char* bp, unsigned char** out, size_t* outsize);
/*
-Outputs the tree to a dynamic block (btype 10) according to the deflate
-specification.
+Like ZopfliDeflate, but allows to specify start and end byte with instart and
+inend. Only that part is compressed, but earlier bytes are still used for the
+back window.
*/
-void AddDynamicTree(const unsigned* ll_lengths, const unsigned* d_lengths,
- unsigned char* bp, unsigned char** out, size_t* outsize);
+void ZopfliDeflatePart(const ZopfliOptions* options, int btype, int final,
+ const unsigned char* in, size_t instart, size_t inend,
+ unsigned char* bp, unsigned char** out,
+ size_t* outsize);
/*
Calculates block size in bits.
@@ -80,7 +71,7 @@ dists: ll77 distances
lstart: start of block
lend: end of block (not inclusive)
*/
-double CalculateBlockSize(
- const unsigned short* litlens, const unsigned short* dists,
- size_t lstart, size_t lend, int btype);
+double ZopfliCalculateBlockSize(const unsigned short* litlens,
+ const unsigned short* dists,
+ size_t lstart, size_t lend, int btype);
#endif /* ZOPFLI_DEFLATE_H_ */
diff --git a/gzip_container.c b/gzip_container.c
index 6d38381..e94a3bd 100644
--- a/gzip_container.c
+++ b/gzip_container.c
@@ -30,7 +30,7 @@ static unsigned long crc_table[256];
static int crc_table_computed = 0;
/* Makes the table for a fast CRC. */
-void MakeCRCTable() {
+static void MakeCRCTable() {
unsigned long c;
int n, k;
for (n = 0; n < 256; n++) {
@@ -52,8 +52,8 @@ void MakeCRCTable() {
Updates a running crc with the bytes buf[0..len-1] and returns
the updated crc. The crc should be initialized to zero.
*/
-unsigned long UpdateCRC(unsigned long crc,
- const unsigned char *buf, size_t len) {
+static unsigned long UpdateCRC(unsigned long crc,
+ const unsigned char *buf, size_t len) {
unsigned long c = crc ^ 0xffffffffL;
unsigned n;
@@ -66,45 +66,46 @@ unsigned long UpdateCRC(unsigned long crc,
}
/* Returns the CRC of the bytes buf[0..len-1]. */
-unsigned long CRC(const unsigned char* buf, int len) {
+static unsigned long CRC(const unsigned char* buf, int len) {
return UpdateCRC(0L, buf, len);
}
/*
Compresses the data according to the gzip specification.
*/
-void GzipCompress(const Options* options,
- const unsigned char* in, size_t insize,
- unsigned char** out, size_t* outsize) {
+void ZopfliGzipCompress(const ZopfliOptions* options,
+ const unsigned char* in, size_t insize,
+ unsigned char** out, size_t* outsize) {
unsigned long crcvalue = CRC(in, insize);
unsigned char bp = 0;
- APPEND_DATA(31, out, outsize); /* ID1 */
- APPEND_DATA(139, out, outsize); /* ID2 */
- APPEND_DATA(8, out, outsize); /* CM */
- APPEND_DATA(0, out, outsize); /* FLG */
+ ZOPFLI_APPEND_DATA(31, out, outsize); /* ID1 */
+ ZOPFLI_APPEND_DATA(139, out, outsize); /* ID2 */
+ ZOPFLI_APPEND_DATA(8, out, outsize); /* CM */
+ ZOPFLI_APPEND_DATA(0, out, outsize); /* FLG */
/* MTIME */
- APPEND_DATA(0, out, outsize);
- APPEND_DATA(0, out, outsize);
- APPEND_DATA(0, out, outsize);
- APPEND_DATA(0, out, outsize);
+ ZOPFLI_APPEND_DATA(0, out, outsize);
+ ZOPFLI_APPEND_DATA(0, out, outsize);
+ ZOPFLI_APPEND_DATA(0, out, outsize);
+ ZOPFLI_APPEND_DATA(0, out, outsize);
- APPEND_DATA(2, out, outsize); /* XFL, 2 indicates best compression. */
- APPEND_DATA(3, out, outsize); /* OS follows Unix conventions. */
+ ZOPFLI_APPEND_DATA(2, out, outsize); /* XFL, 2 indicates best compression. */
+ ZOPFLI_APPEND_DATA(3, out, outsize); /* OS follows Unix conventions. */
- Deflate(options, 2 /* Dynamic block */, 1, in, insize, &bp, out, outsize);
+ ZopfliDeflate(options, 2 /* Dynamic block */, 1,
+ in, insize, &bp, out, outsize);
/* CRC */
- APPEND_DATA(crcvalue % 256, out, outsize);
- APPEND_DATA((crcvalue >> 8) % 256, out, outsize);
- APPEND_DATA((crcvalue >> 16) % 256, out, outsize);
- APPEND_DATA((crcvalue >> 24) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA(crcvalue % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((crcvalue >> 8) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((crcvalue >> 16) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((crcvalue >> 24) % 256, out, outsize);
/* ISIZE */
- APPEND_DATA(insize % 256, out, outsize);
- APPEND_DATA((insize >> 8) % 256, out, outsize);
- APPEND_DATA((insize >> 16) % 256, out, outsize);
- APPEND_DATA((insize >> 24) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA(insize % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((insize >> 8) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((insize >> 16) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((insize >> 24) % 256, out, outsize);
if (options->verbose) {
fprintf(stderr,
diff --git a/gzip_container.h b/gzip_container.h
index c03e9cd..5558eb1 100644
--- a/gzip_container.h
+++ b/gzip_container.h
@@ -35,8 +35,8 @@ out: pointer to the dynamic output array to which the result is appended. Must
be freed after use.
outsize: pointer to the dynamic output array size.
*/
-void GzipCompress(const Options* options,
- const unsigned char* in, size_t insize,
- unsigned char** out, size_t* outsize);
+void ZopfliGzipCompress(const ZopfliOptions* options,
+ const unsigned char* in, size_t insize,
+ unsigned char** out, size_t* outsize);
#endif /* ZOPFLI_GZIP_H_ */
diff --git a/hash.c b/hash.c
index 20d2871..a3b294f 100644
--- a/hash.c
+++ b/hash.c
@@ -26,7 +26,7 @@ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
#define HASH_SHIFT 5
#define HASH_MASK 32767
-void InitHash(size_t window_size, Hash* h) {
+void ZopfliInitHash(size_t window_size, ZopfliHash* h) {
size_t i;
h->val = 0;
@@ -41,14 +41,14 @@ void InitHash(size_t window_size, Hash* h) {
h->hashval[i] = -1;
}
-#ifdef USE_HASH_SAME
+#ifdef ZOPFLI_HASH_SAME
h->same = (unsigned short*)malloc(sizeof(*h->same) * window_size);
for (i = 0; i < window_size; i++) {
h->same[i] = 0;
}
#endif
-#ifdef USE_HASH_SAME_HASH
+#ifdef ZOPFLI_HASH_SAME_HASH
h->val2 = 0;
h->head2 = (int*)malloc(sizeof(*h->head2) * 65536);
h->prev2 = (unsigned short*)malloc(sizeof(*h->prev2) * window_size);
@@ -63,18 +63,18 @@ void InitHash(size_t window_size, Hash* h) {
#endif
}
-void CleanHash(Hash* h) {
+void ZopfliCleanHash(ZopfliHash* h) {
free(h->head);
free(h->prev);
free(h->hashval);
-#ifdef USE_HASH_SAME_HASH
+#ifdef ZOPFLI_HASH_SAME_HASH
free(h->head2);
free(h->prev2);
free(h->hashval2);
#endif
-#ifdef USE_HASH_SAME
+#ifdef ZOPFLI_HASH_SAME
free(h->same);
#endif
}
@@ -84,17 +84,19 @@ Update the sliding hash value with the given byte. All calls to this function
must be made on consecutive input characters. Since the hash value exists out
of multiple input bytes, a few warmups with this function are needed initially.
*/
-static void UpdateHashValue(Hash* h, unsigned char c) {
+static void UpdateHashValue(ZopfliHash* h, unsigned char c) {
h->val = (((h->val) << HASH_SHIFT) ^ (c)) & HASH_MASK;
}
-void UpdateHash(const unsigned char* array, size_t pos, size_t end, Hash* h) {
- unsigned short hpos = pos & WINDOW_MASK;
-#ifdef USE_HASH_SAME
+void ZopfliUpdateHash(const unsigned char* array, size_t pos, size_t end,
+ ZopfliHash* h) {
+ unsigned short hpos = pos & ZOPFLI_WINDOW_MASK;
+#ifdef ZOPFLI_HASH_SAME
size_t amount = 0;
#endif
- UpdateHashValue(h, pos + MIN_MATCH <= end ? array[pos + MIN_MATCH - 1] : 0);
+ UpdateHashValue(h, pos + ZOPFLI_MIN_MATCH <= end ?
+ array[pos + ZOPFLI_MIN_MATCH - 1] : 0);
h->hashval[hpos] = h->val;
if (h->head[h->val] != -1 && h->hashval[h->head[h->val]] == h->val) {
h->prev[hpos] = h->head[h->val];
@@ -102,10 +104,10 @@ void UpdateHash(const unsigned char* array, size_t pos, size_t end, Hash* h) {
else h->prev[hpos] = hpos;
h->head[h->val] = hpos;
-#ifdef USE_HASH_SAME
+#ifdef ZOPFLI_HASH_SAME
/* Update "same". */
- if (h->same[(pos - 1) & WINDOW_MASK] > 1) {
- amount = h->same[(pos - 1) & WINDOW_MASK] - 1;
+ if (h->same[(pos - 1) & ZOPFLI_WINDOW_MASK] > 1) {
+ amount = h->same[(pos - 1) & ZOPFLI_WINDOW_MASK] - 1;
}
while (pos + amount + 1 < end &&
array[pos] == array[pos + amount + 1] && amount < (unsigned short)(-1)) {
@@ -114,8 +116,8 @@ void UpdateHash(const unsigned char* array, size_t pos, size_t end, Hash* h) {
h->same[hpos] = amount;
#endif
-#ifdef USE_HASH_SAME_HASH
- h->val2 = ((h->same[hpos] - MIN_MATCH) & 255) ^ h->val;
+#ifdef ZOPFLI_HASH_SAME_HASH
+ h->val2 = ((h->same[hpos] - ZOPFLI_MIN_MATCH) & 255) ^ h->val;
h->hashval2[hpos] = h->val2;
if (h->head2[h->val2] != -1 && h->hashval2[h->head2[h->val2]] == h->val2) {
h->prev2[hpos] = h->head2[h->val2];
@@ -125,7 +127,8 @@ void UpdateHash(const unsigned char* array, size_t pos, size_t end, Hash* h) {
#endif
}
-void WarmupHash(const unsigned char* array, size_t pos, size_t end, Hash* h) {
+void ZopfliWarmupHash(const unsigned char* array, size_t pos, size_t end,
+ ZopfliHash* h) {
(void)end;
UpdateHashValue(h, array[pos + 0]);
UpdateHashValue(h, array[pos + 1]);
diff --git a/hash.h b/hash.h
index c89db4f..79c2479 100644
--- a/hash.h
+++ b/hash.h
@@ -18,7 +18,7 @@ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
*/
/*
-The hash for FindLongestMatch of lz77.c.
+The hash for ZopfliFindLongestMatch of lz77.c.
*/
#ifndef ZOPFLI_HASH_H_
@@ -26,13 +26,13 @@ The hash for FindLongestMatch of lz77.c.
#include "util.h"
-typedef struct Hash {
+typedef struct ZopfliHash {
int* head; /* Hash value to index of its most recent occurance. */
unsigned short* prev; /* Index to index of prev. occurance of same hash. */
int* hashval; /* Index to hash value at this index. */
int val; /* Current hash value. */
-#ifdef USE_HASH_SAME_HASH
+#ifdef ZOPFLI_HASH_SAME_HASH
/* Fields with similar purpose as the above hash, but for the second hash with
a value that is calculated differently. */
int* head2; /* Hash value to index of its most recent occurance. */
@@ -41,28 +41,30 @@ typedef struct Hash {
int val2; /* Current hash value. */
#endif
-#ifdef USE_HASH_SAME
+#ifdef ZOPFLI_HASH_SAME
unsigned short* same; /* Amount of repetitions of same byte after this .*/
#endif
-} Hash;
+} ZopfliHash;
-/* Allocates and initializes all fields of Hash. */
-void InitHash(size_t window_size, Hash* h);
+/* Allocates and initializes all fields of ZopfliHash. */
+void ZopfliInitHash(size_t window_size, ZopfliHash* h);
-/* Frees all fields of Hash. */
-void CleanHash(Hash* h);
+/* Frees all fields of ZopfliHash. */
+void ZopfliCleanHash(ZopfliHash* h);
/*
Updates the hash values based on the current position in the array. All calls
to this must be made for consecutive bytes.
*/
-void UpdateHash(const unsigned char* array, size_t pos, size_t end, Hash* h);
+void ZopfliUpdateHash(const unsigned char* array, size_t pos, size_t end,
+ ZopfliHash* h);
/*
Prepopulates hash:
-Fills in the initial values in the hash, before UpdateHash can be used
+Fills in the initial values in the hash, before ZopfliUpdateHash can be used
correctly.
*/
-void WarmupHash(const unsigned char* array, size_t pos, size_t end, Hash* h);
+void ZopfliWarmupHash(const unsigned char* array, size_t pos, size_t end,
+ ZopfliHash* h);
#endif /* ZOPFLI_HASH_H_ */
diff --git a/katajainen.c b/katajainen.c
index 7cf4d48..783ea08 100644
--- a/katajainen.c
+++ b/katajainen.c
@@ -177,7 +177,7 @@ static int LeafComparator(const void* a, const void* b) {
return ((const Node*)a)->weight - ((const Node*)b)->weight;
}
-int LengthLimitedCodeLengths(
+int ZopfliLengthLimitedCodeLengths(
const size_t* frequencies, int n, int maxbits, unsigned* bitlengths) {
NodePool pool;
int i;
diff --git a/katajainen.h b/katajainen.h
index 39946c8..ee8a91e 100644
--- a/katajainen.h
+++ b/katajainen.h
@@ -36,7 +36,7 @@ maxbits: Maximum bit length, inclusive.
bitlengths: Output, the bitlengths for the symbol prefix codes.
return: 0 for OK, non-0 for error.
*/
-int LengthLimitedCodeLengths(
+int ZopfliLengthLimitedCodeLengths(
const size_t* frequencies, int n, int maxbits, unsigned* bitlengths);
#endif /* ZOPFLI_KATAJAINEN_H_ */
diff --git a/lz77.c b/lz77.c
index dcb8b2c..5b74e1e 100644
--- a/lz77.c
+++ b/lz77.c
@@ -23,21 +23,21 @@ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
#include <stdio.h>
#include <stdlib.h>
-void InitLZ77Store(LZ77Store* store) {
+void ZopfliInitLZ77Store(ZopfliLZ77Store* store) {
store->size = 0;
store->litlens = 0;
store->dists = 0;
}
-void CleanLZ77Store(LZ77Store* store) {
+void ZopfliCleanLZ77Store(ZopfliLZ77Store* store) {
free(store->litlens);
free(store->dists);
}
-void CopyLZ77Store(
- const LZ77Store* source, LZ77Store* dest) {
+void ZopfliCopyLZ77Store(
+ const ZopfliLZ77Store* source, ZopfliLZ77Store* dest) {
size_t i;
- CleanLZ77Store(dest);
+ ZopfliCleanLZ77Store(dest);
dest->litlens =
(unsigned short*)malloc(sizeof(*dest->litlens) * source->size);
dest->dists = (unsigned short*)malloc(sizeof(*dest->dists) * source->size);
@@ -52,14 +52,14 @@ void CopyLZ77Store(
}
/*
-Appends the length and distance to the LZ77 arrays of the LZ77Store.
-context must be a LZ77Store*.
+Appends the length and distance to the LZ77 arrays of the ZopfliLZ77Store.
+context must be a ZopfliLZ77Store*.
*/
-void StoreLitLenDist(unsigned short length, unsigned short dist,
- LZ77Store* store) {
- size_t size2 = store->size; /* Needed for using APPEND_DATA twice. */
- APPEND_DATA(length, &store->litlens, &store->size);
- APPEND_DATA(dist, &store->dists, &size2);
+void ZopfliStoreLitLenDist(unsigned short length, unsigned short dist,
+ ZopfliLZ77Store* store) {
+ size_t size2 = store->size; /* Needed for using ZOPFLI_APPEND_DATA twice. */
+ ZOPFLI_APPEND_DATA(length, &store->litlens, &store->size);
+ ZOPFLI_APPEND_DATA(dist, &store->dists, &size2);
}
/*
@@ -77,8 +77,8 @@ static int GetLengthValue(int length, int distance) {
return distance > 1024 ? length - 1 : length;
}
-void VerifyLenDist(const unsigned char* data, size_t datasize, size_t pos,
- unsigned short dist, unsigned short length) {
+void ZopfliVerifyLenDist(const unsigned char* data, size_t datasize, size_t pos,
+ unsigned short dist, unsigned short length) {
/* TODO(lode): make this only run in a debug compile, it's for assert only. */
size_t i;
@@ -137,14 +137,15 @@ static const unsigned char* GetMatch(const unsigned char* scan,
return scan;
}
-#ifdef USE_LONGEST_MATCH_CACHE
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
/*
Gets distance, length and sublen values from the cache if possible.
Returns 1 if it got the values from the cache, 0 if not.
Updates the limit value to a smaller one if possible with more limited
information from the cache.
*/
-int TryGetFromLongestMatchCache(BlockState* s, size_t pos, size_t* limit,
+static int TryGetFromLongestMatchCache(ZopfliBlockState* s,
+ size_t pos, size_t* limit,
unsigned short* sublen, unsigned short* distance, unsigned short* length) {
/* The LMC cache starts at the beginning of the block rather than the
beginning of the whole array. */
@@ -154,20 +155,20 @@ int TryGetFromLongestMatchCache(BlockState* s, size_t pos, size_t* limit,
that this cache value is not filled in yet. */
unsigned char cache_available = s->lmc && (s->lmc->length[lmcpos] == 0 ||
s->lmc->dist[lmcpos] != 0);
- unsigned char limit_ok_for_cache = cache_available && (*limit == MAX_MATCH ||
- s->lmc->length[lmcpos] <= *limit ||
- (sublen && MaxCachedSublen(s->lmc,
+ unsigned char limit_ok_for_cache = cache_available &&
+ (*limit == ZOPFLI_MAX_MATCH || s->lmc->length[lmcpos] <= *limit ||
+ (sublen && ZopfliMaxCachedSublen(s->lmc,
lmcpos, s->lmc->length[lmcpos]) >= *limit));
if (s->lmc && limit_ok_for_cache && cache_available) {
if (!sublen || s->lmc->length[lmcpos]
- <= MaxCachedSublen(s->lmc, lmcpos, s->lmc->length[lmcpos])) {
+ <= ZopfliMaxCachedSublen(s->lmc, lmcpos, s->lmc->length[lmcpos])) {
*length = s->lmc->length[lmcpos];
if (*length > *limit) *length = *limit;
if (sublen) {
- CacheToSublen(s->lmc, lmcpos, *length, sublen);
+ ZopfliCacheToSublen(s->lmc, lmcpos, *length, sublen);
*distance = sublen[*length];
- if (*limit == MAX_MATCH && *length >= MIN_MATCH) {
+ if (*limit == ZOPFLI_MAX_MATCH && *length >= ZOPFLI_MIN_MATCH) {
assert(sublen[*length] == s->lmc->dist[lmcpos]);
}
} else {
@@ -187,7 +188,8 @@ int TryGetFromLongestMatchCache(BlockState* s, size_t pos, size_t* limit,
Stores the found sublen, distance and length in the longest match cache, if
possible.
*/
-void StoreInLongestMatchCache(BlockState* s, size_t pos, size_t limit,
+static void StoreInLongestMatchCache(ZopfliBlockState* s,
+ size_t pos, size_t limit,
const unsigned short* sublen,
unsigned short distance, unsigned short length) {
/* The LMC cache starts at the beginning of the block rather than the
@@ -199,28 +201,29 @@ void StoreInLongestMatchCache(BlockState* s, size_t pos, size_t limit,
unsigned char cache_available = s->lmc && (s->lmc->length[lmcpos] == 0 ||
s->lmc->dist[lmcpos] != 0);
- if (s->lmc && limit == MAX_MATCH && sublen && !cache_available) {
+ if (s->lmc && limit == ZOPFLI_MAX_MATCH && sublen && !cache_available) {
assert(s->lmc->length[lmcpos] == 1 && s->lmc->dist[lmcpos] == 0);
- s->lmc->dist[lmcpos] = length < MIN_MATCH ? 0 : distance;
- s->lmc->length[lmcpos] = length < MIN_MATCH ? 0 : length;
+ s->lmc->dist[lmcpos] = length < ZOPFLI_MIN_MATCH ? 0 : distance;
+ s->lmc->length[lmcpos] = length < ZOPFLI_MIN_MATCH ? 0 : length;
assert(!(s->lmc->length[lmcpos] == 1 && s->lmc->dist[lmcpos] == 0));
- SublenToCache(sublen, lmcpos, length, s->lmc);
+ ZopfliSublenToCache(sublen, lmcpos, length, s->lmc);
}
}
#endif
-void FindLongestMatch(BlockState* s, const Hash* h, const unsigned char* array,
+void ZopfliFindLongestMatch(ZopfliBlockState* s, const ZopfliHash* h,
+ const unsigned char* array,
size_t pos, size_t size, size_t limit,
unsigned short* sublen, unsigned short* distance, unsigned short* length) {
- unsigned short hpos = pos & WINDOW_MASK, p, pp;
+ unsigned short hpos = pos & ZOPFLI_WINDOW_MASK, p, pp;
unsigned short bestdist = 0;
unsigned short bestlength = 1;
const unsigned char* scan;
const unsigned char* match;
const unsigned char* arrayend;
const unsigned char* arrayend_safe;
-#if MAX_CHAIN_HITS < WINDOW_SIZE
- int chain_counter = MAX_CHAIN_HITS; /* For quitting early. */
+#if ZOPFLI_MAX_CHAIN_HITS < ZOPFLI_WINDOW_SIZE
+ int chain_counter = ZOPFLI_MAX_CHAIN_HITS; /* For quitting early. */
#endif
unsigned dist = 0; /* Not unsigned short on purpose. */
@@ -230,19 +233,19 @@ void FindLongestMatch(BlockState* s, const Hash* h, const unsigned char* array,
int* hhashval = h->hashval;
int hval = h->val;
-#ifdef USE_LONGEST_MATCH_CACHE
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
if (TryGetFromLongestMatchCache(s, pos, &limit, sublen, distance, length)) {
assert(pos + *length <= size);
return;
}
#endif
- assert(limit <= MAX_MATCH);
- assert(limit >= MIN_MATCH);
+ assert(limit <= ZOPFLI_MAX_MATCH);
+ assert(limit >= ZOPFLI_MIN_MATCH);
assert(pos < size);
- if (size - pos < MIN_MATCH) {
- /* The rest of the code assumes there are at least MIN_MATCH bytes to
+ if (size - pos < ZOPFLI_MIN_MATCH) {
+ /* The rest of the code assumes there are at least ZOPFLI_MIN_MATCH bytes to
try. */
*length = 0;
*distance = 0;
@@ -262,13 +265,13 @@ void FindLongestMatch(BlockState* s, const Hash* h, const unsigned char* array,
assert(pp == hpos);
- dist = p < pp ? pp - p : ((WINDOW_SIZE - p) + pp);
+ dist = p < pp ? pp - p : ((ZOPFLI_WINDOW_SIZE - p) + pp);
/* Go through all distances. */
- while (dist < WINDOW_SIZE) {
+ while (dist < ZOPFLI_WINDOW_SIZE) {
unsigned short currentlength = 0;
- assert(p < WINDOW_SIZE);
+ assert(p < ZOPFLI_WINDOW_SIZE);
assert(p == hprev[pp]);
assert(hhashval[p] == hval);
@@ -282,10 +285,10 @@ void FindLongestMatch(BlockState* s, const Hash* h, const unsigned char* array,
if (pos + bestlength >= size
|| *(scan + bestlength) == *(match + bestlength)) {
-#ifdef USE_HASH_SAME
- unsigned short same0 = h->same[pos & WINDOW_MASK];
+#ifdef ZOPFLI_HASH_SAME
+ unsigned short same0 = h->same[pos & ZOPFLI_WINDOW_MASK];
if (same0 > 2 && *scan == *match) {
- unsigned short same1 = h->same[(pos - dist) & WINDOW_MASK];
+ unsigned short same1 = h->same[(pos - dist) & ZOPFLI_WINDOW_MASK];
unsigned short same = same0 < same1 ? same0 : same1;
if (same > limit) same = limit;
scan += same;
@@ -310,7 +313,7 @@ void FindLongestMatch(BlockState* s, const Hash* h, const unsigned char* array,
}
-#ifdef USE_HASH_SAME_HASH
+#ifdef ZOPFLI_HASH_SAME_HASH
/* Switch to the other hash once this will be more efficient. */
if (hhead != h->head2 && bestlength >= h->same[hpos] &&
h->val2 == h->hashval2[p]) {
@@ -326,15 +329,15 @@ void FindLongestMatch(BlockState* s, const Hash* h, const unsigned char* array,
p = hprev[p];
if (p == pp) break; /* Uninited prev value. */
- dist += p < pp ? pp - p : ((WINDOW_SIZE - p) + pp);
+ dist += p < pp ? pp - p : ((ZOPFLI_WINDOW_SIZE - p) + pp);
-#if MAX_CHAIN_HITS < WINDOW_SIZE
+#if ZOPFLI_MAX_CHAIN_HITS < ZOPFLI_WINDOW_SIZE
chain_counter--;
if (chain_counter <= 0) break;
#endif
}
-#ifdef USE_LONGEST_MATCH_CACHE
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
StoreInLongestMatchCache(s, pos, limit, sublen, bestdist, bestlength);
#endif
@@ -345,20 +348,21 @@ void FindLongestMatch(BlockState* s, const Hash* h, const unsigned char* array,
assert(pos + *length <= size);
}
-void LZ77Greedy(BlockState* s, const unsigned char* in,
- size_t instart, size_t inend,
- LZ77Store* store) {
+void ZopfliLZ77Greedy(ZopfliBlockState* s, const unsigned char* in,
+ size_t instart, size_t inend,
+ ZopfliLZ77Store* store) {
size_t i = 0, j;
unsigned short leng;
unsigned short dist;
int lengvalue;
- size_t windowstart = instart > WINDOW_SIZE ? instart - WINDOW_SIZE : 0;
+ size_t windowstart = instart > ZOPFLI_WINDOW_SIZE
+ ? instart - ZOPFLI_WINDOW_SIZE : 0;
unsigned short dummysublen[259];
- Hash hash;
- Hash* h = &hash;
+ ZopfliHash hash;
+ ZopfliHash* h = &hash;
-#ifdef LAZY_MATCHING
+#ifdef ZOPFLI_LAZY_MATCHING
/* Lazy matching. */
unsigned prev_length = 0;
unsigned prev_match = 0;
@@ -368,26 +372,27 @@ void LZ77Greedy(BlockState* s, const unsigned char* in,
if (instart == inend) return;
- InitHash(WINDOW_SIZE, h);
- WarmupHash(in, windowstart, inend, h);
+ ZopfliInitHash(ZOPFLI_WINDOW_SIZE, h);
+ ZopfliWarmupHash(in, windowstart, inend, h);
for (i = windowstart; i < instart; i++) {
- UpdateHash(in, i, inend, h);
+ ZopfliUpdateHash(in, i, inend, h);
}
for (i = instart; i < inend; i++) {
- UpdateHash(in, i, inend, h);
+ ZopfliUpdateHash(in, i, inend, h);
- FindLongestMatch(s, h, in, i, inend, MAX_MATCH, dummysublen, &dist, &leng);
+ ZopfliFindLongestMatch(s, h, in, i, inend, ZOPFLI_MAX_MATCH, dummysublen,
+ &dist, &leng);
lengvalue = GetLengthValue(leng, dist);
-#ifdef LAZY_MATCHING
+#ifdef ZOPFLI_LAZY_MATCHING
/* Lazy matching. */
prevlengvalue = GetLengthValue(prev_length, prev_match);
if (match_available) {
match_available = 0;
if (lengvalue > prevlengvalue + 1) {
- StoreLitLenDist(in[i - 1], 0, store);
- if (lengvalue >= MIN_MATCH && lengvalue < MAX_MATCH) {
+ ZopfliStoreLitLenDist(in[i - 1], 0, store);
+ if (lengvalue >= ZOPFLI_MIN_MATCH && lengvalue < ZOPFLI_MAX_MATCH) {
match_available = 1;
prev_length = leng;
prev_match = dist;
@@ -399,17 +404,17 @@ void LZ77Greedy(BlockState* s, const unsigned char* in,
dist = prev_match;
lengvalue = prevlengvalue;
/* Add to output. */
- VerifyLenDist(in, inend, i - 1, dist, leng);
- StoreLitLenDist(leng, dist, store);
+ ZopfliVerifyLenDist(in, inend, i - 1, dist, leng);
+ ZopfliStoreLitLenDist(leng, dist, store);
for (j = 2; j < leng; j++) {
assert(i < inend);
i++;
- UpdateHash(in, i, inend, h);
+ ZopfliUpdateHash(in, i, inend, h);
}
continue;
}
}
- else if (lengvalue >= MIN_MATCH && leng < MAX_MATCH) {
+ else if (lengvalue >= ZOPFLI_MIN_MATCH && leng < ZOPFLI_MAX_MATCH) {
match_available = 1;
prev_length = leng;
prev_match = dist;
@@ -419,26 +424,27 @@ void LZ77Greedy(BlockState* s, const unsigned char* in,
#endif
/* Add to output. */
- if (lengvalue >= MIN_MATCH) {
- VerifyLenDist(in, inend, i, dist, leng);
- StoreLitLenDist(leng, dist, store);
+ if (lengvalue >= ZOPFLI_MIN_MATCH) {
+ ZopfliVerifyLenDist(in, inend, i, dist, leng);
+ ZopfliStoreLitLenDist(leng, dist, store);
} else {
leng = 1;
- StoreLitLenDist(in[i], 0, store);
+ ZopfliStoreLitLenDist(in[i], 0, store);
}
for (j = 1; j < leng; j++) {
assert(i < inend);
i++;
- UpdateHash(in, i, inend, h);
+ ZopfliUpdateHash(in, i, inend, h);
}
}
- CleanHash(h);
+ ZopfliCleanHash(h);
}
-void GetLZ77Counts(const unsigned short* litlens, const unsigned short* dists,
- size_t start, size_t end,
- size_t* ll_count, size_t* d_count) {
+void ZopfliLZ77Counts(const unsigned short* litlens,
+ const unsigned short* dists,
+ size_t start, size_t end,
+ size_t* ll_count, size_t* d_count) {
size_t i;
for (i = 0; i < 288; i++) {
@@ -452,8 +458,8 @@ void GetLZ77Counts(const unsigned short* litlens, const unsigned short* dists,
if (dists[i] == 0) {
ll_count[litlens[i]]++;
} else {
- ll_count[GetLengthSymbol(litlens[i])]++;
- d_count[GetDistSymbol(dists[i])]++;
+ ll_count[ZopfliGetLengthSymbol(litlens[i])]++;
+ d_count[ZopfliGetDistSymbol(dists[i])]++;
}
}
diff --git a/lz77.h b/lz77.h
index b6488e3..83ef1c8 100644
--- a/lz77.h
+++ b/lz77.h
@@ -38,36 +38,36 @@ dists: Indicates the distance, or 0 to indicate that there is no distance and
litlens contains a literal instead of a length.
litlens and dists both have the same size.
*/
-typedef struct LZ77Store {
+typedef struct ZopfliLZ77Store {
unsigned short* litlens; /* Lit or len. */
unsigned short* dists; /* If 0: indicates literal in corresponding litlens,
if > 0: length in corresponding litlens, this is the distance. */
size_t size;
-} LZ77Store;
+} ZopfliLZ77Store;
-void InitLZ77Store(LZ77Store* store);
-void CleanLZ77Store(LZ77Store* store);
-void CopyLZ77Store(const LZ77Store* source, LZ77Store* dest);
-void StoreLitLenDist(unsigned short length, unsigned short dist,
- LZ77Store* store);
+void ZopfliInitLZ77Store(ZopfliLZ77Store* store);
+void ZopfliCleanLZ77Store(ZopfliLZ77Store* store);
+void ZopfliCopyLZ77Store(const ZopfliLZ77Store* source, ZopfliLZ77Store* dest);
+void ZopfliStoreLitLenDist(unsigned short length, unsigned short dist,
+ ZopfliLZ77Store* store);
/*
Some state information for compressing a block.
This is currently a bit under-used (with mainly only the longest match cache),
but is kept for easy future expansion.
*/
-typedef struct BlockState {
- const Options* options;
+typedef struct ZopfliBlockState {
+ const ZopfliOptions* options;
-#ifdef USE_LONGEST_MATCH_CACHE
+#ifdef ZOPFLI_LONGEST_MATCH_CACHE
/* Cache for length/distance pairs found so far. */
- LongestMatchCache* lmc;
+ ZopfliLongestMatchCache* lmc;
#endif
/* The start (inclusive) and end (not inclusive) of the current block. */
size_t blockstart;
size_t blockend;
-} BlockState;
+} ZopfliBlockState;
/*
Finds the longest match (length and corresponding distance) for LZ77
@@ -79,23 +79,22 @@ pos: position in the data to find the match for
size: size of the data
limit: limit length to maximum this value (default should be 258). This allows
finding a shorter dist for that length (= less extra bits). Must be
- in the range [MIN_MATCH, MAX_MATCH].
+ in the range [ZOPFLI_MIN_MATCH, ZOPFLI_MAX_MATCH].
sublen: output array of 259 elements, or null. Has, for each length, the
smallest distance required to reach this length. Only 256 of its 259 values
are used, the first 3 are ignored (the shortest length is 3. It is purely
for convenience that the array is made 3 longer).
*/
-
-void FindLongestMatch(
- BlockState *s, const Hash* h, const unsigned char* array,
+void ZopfliFindLongestMatch(
+ ZopfliBlockState *s, const ZopfliHash* h, const unsigned char* array,
size_t pos, size_t size, size_t limit,
unsigned short* sublen, unsigned short* distance, unsigned short* length);
/*
Verifies if length and dist are indeed valid, only used for assertion.
*/
-void VerifyLenDist(const unsigned char* data, size_t datasize, size_t pos,
- unsigned short dist, unsigned short length);
+void ZopfliVerifyLenDist(const unsigned char* data, size_t datasize, size_t pos,
+ unsigned short dist, unsigned short length);
/*
Counts the number of literal, length and distance symbols in the given lz77
@@ -108,19 +107,20 @@ ll_count: count of each lit/len symbol, must have size 288 (see deflate
standard)
d_count: count of each dist symbol, must have size 32 (see deflate standard)
*/
-void GetLZ77Counts(const unsigned short* litlens, const unsigned short* dists,
- size_t start, size_t end,
- size_t* ll_count, size_t* d_count);
+void ZopfliLZ77Counts(const unsigned short* litlens,
+ const unsigned short* dists,
+ size_t start, size_t end,
+ size_t* ll_count, size_t* d_count);
/*
Does LZ77 using an algorithm similar to gzip, with lazy matching, rather than
with the slow but better "squeeze" implementation.
-The result is placed in the LZ77Store.
+The result is placed in the ZopfliLZ77Store.
If instart is larger than 0, it uses values before instart as starting
dictionary.
*/
-void LZ77Greedy(BlockState* s, const unsigned char* in,
- size_t instart, size_t inend,
- LZ77Store* store);
+void ZopfliLZ77Greedy(ZopfliBlockState* s, const unsigned char* in,
+ size_t instart, size_t inend,
+ ZopfliLZ77Store* store);
#endif /* ZOPFLI_LZ77_H_ */
diff --git a/squeeze.c b/squeeze.c
index 1620783..7558b34 100644
--- a/squeeze.c
+++ b/squeeze.c
@@ -72,25 +72,32 @@ static void AddWeighedStatFreqs(const SymbolStats* stats1, double w1,
result->litlens[256] = 1; /* End symbol. */
}
+typedef struct RanState {
+ unsigned int m_w, m_z;
+} RanState;
+
+static void InitRanState(RanState* state) {
+ state->m_w = 1;
+ state->m_z = 2;
+}
+
/* Get random number: "Multiply-With-Carry" generator of G. Marsaglia */
-static unsigned int Ran() {
- static unsigned int m_w = 1;
- static unsigned int m_z = 2;
- m_z = 36969 * (m_z & 65535) + (m_z >> 16);
- m_w = 18000 * (m_w & 65535) + (m_w >> 16);
- return (m_z << 16) + m_w; /* 32-bit result. */
+static unsigned int Ran(RanState* state) {
+ state->m_z = 36969 * (state->m_z & 65535) + (state->m_z >> 16);
+ state->m_w = 18000 * (state->m_w & 65535) + (state->m_w >> 16);
+ return (state->m_z << 16) + state->m_w; /* 32-bit result. */
}
-static void RandomizeFreqs(size_t* freqs, int n) {
+static void RandomizeFreqs(RanState* state, size_t* freqs, int n) {
int i;
for (i = 0; i < n; i++) {
- if ((Ran() >> 4) % 3 == 0) freqs[i] = freqs[Ran() % n];
+ if ((Ran(state) >> 4) % 3 == 0) freqs[i] = freqs[Ran(state) % n];
}
}
-static void RandomizeStatFreqs(SymbolStats* stats) {
- RandomizeFreqs(stats->litlens, 288);
- RandomizeFreqs(stats->dists, 32);
+static void RandomizeStatFreqs(RanState* state, SymbolStats* stats) {
+ RandomizeFreqs(state, stats->litlens, 288);
+ RandomizeFreqs(state, stats->dists, 32);
stats->litlens[256] = 1; /* End symbol. */
}
@@ -116,9 +123,9 @@ static double GetCostFixed(unsigned litlen, unsigned dist, void* unused) {
if (litlen <= 143) return 8;
else return 9;
} else {
- int dbits = GetDistExtraBits(dist);
- int lbits = GetLengthExtraBits(litlen);
- int lsym = GetLengthSymbol(litlen);
+ int dbits = ZopfliGetDistExtraBits(dist);
+ int lbits = ZopfliGetLengthExtraBits(litlen);
+ int lsym = ZopfliGetLengthSymbol(litlen);
double cost = 0;
if (lsym <= 279) cost += 7;
else cost += 8;
@@ -136,10 +143,10 @@ static double GetCostStat(unsigned litlen, unsigned dist, void* context) {
if (dist == 0) {
return stats->ll_symbols[litlen];
} else {
- int lsym = GetLengthSymbol(litlen);
- int lbits = GetLengthExtraBits(litlen);
- int dsym = GetDistSymbol(dist);
- int dbits = GetDistExtraBits(dist);
+ int lsym = ZopfliGetLengthSymbol(litlen);
+ int lbits = ZopfliGetLengthExtraBits(litlen);
+ int dsym = ZopfliGetDistSymbol(dist);
+ int dbits = ZopfliGetDistExtraBits(dist);
return stats->ll_symbols[lsym] + lbits + stats->d_symbols[dsym] + dbits;
}
}
@@ -164,7 +171,7 @@ static double GetCostModelMinCost(CostModelFun* costmodel, void* costcontext) {
769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
};
- mincost = LARGE_FLOAT;
+ mincost = ZOPFLI_LARGE_FLOAT;
for (i = 3; i < 259; i++) {
double c = costmodel(i, 1, costcontext);
if (c < mincost) {
@@ -173,7 +180,7 @@ static double GetCostModelMinCost(CostModelFun* costmodel, void* costcontext) {
}
}
- mincost = LARGE_FLOAT;
+ mincost = ZOPFLI_LARGE_FLOAT;
for (i = 0; i < 30; i++) {
double c = costmodel(3, dsymbols[i], costcontext);
if (c < mincost) {
@@ -188,7 +195,7 @@ static double GetCostModelMinCost(CostModelFun* costmodel, void* costcontext) {
/*
Performs the forward pass for "squeeze". Gets the most optimal length to reach
every byte from a previous byte, using cost calculations.
-s: the BlockState
+s: the ZopfliBlockState
in: the input data array
instart: where to start
inend: where to stop (not inclusive)
@@ -198,7 +205,7 @@ length_array: output array of size (inend - instart) which will receive the best
length to reach this byte from a previous byte.
returns the cost that was, according to the costmodel, needed to get to the end.
*/
-static double GetBestLengths(BlockState *s,
+static double GetBestLengths(ZopfliBlockState *s,
const unsigned char* in,
size_t instart, size_t inend,
CostModelFun* costmodel, void* costcontext,
@@ -210,9 +217,10 @@ static double GetBestLengths(BlockState *s,
unsigned short leng;
unsigned short dist;
unsigned short sublen[259];
- size_t windowstart = instart > WINDOW_SIZE ? instart - WINDOW_SIZE : 0;
- Hash hash;
- Hash* h = &hash;
+ size_t windowstart = instart > ZOPFLI_WINDOW_SIZE
+ ? instart - ZOPFLI_WINDOW_SIZE : 0;
+ ZopfliHash hash;
+ ZopfliHash* h = &hash;
double result;
double mincost = GetCostModelMinCost(costmodel, costcontext);
@@ -221,42 +229,44 @@ static double GetBestLengths(BlockState *s,
costs = (float*)malloc(sizeof(float) * (blocksize + 1));
if (!costs) exit(-1); /* Allocation failed. */
- InitHash(WINDOW_SIZE, h);
- WarmupHash(in, windowstart, inend, h);
+ ZopfliInitHash(ZOPFLI_WINDOW_SIZE, h);
+ ZopfliWarmupHash(in, windowstart, inend, h);
for (i = windowstart; i < instart; i++) {
- UpdateHash(in, i, inend, h);
+ ZopfliUpdateHash(in, i, inend, h);
}
- for (i = 1; i < blocksize + 1; i++) costs[i] = LARGE_FLOAT;
+ for (i = 1; i < blocksize + 1; i++) costs[i] = ZOPFLI_LARGE_FLOAT;
costs[0] = 0; /* Because it's the start. */
length_array[0] = 0;
for (i = instart; i < inend; i++) {
size_t j = i - instart; /* Index in the costs array and length_array. */
- UpdateHash(in, i, inend, h);
+ ZopfliUpdateHash(in, i, inend, h);
-#ifdef SHORTCUT_LONG_REPETITIONS
+#ifdef ZOPFLI_SHORTCUT_LONG_REPETITIONS
/* If we're in a long repetition of the same character and have more than
- MAX_MATCH characters before and after our position. */
- if (h->same[i & WINDOW_MASK] > MAX_MATCH * 2
- && i > instart + MAX_MATCH + 1
- && i + MAX_MATCH * 2 + 1 < inend
- && h->same[(i - MAX_MATCH) & WINDOW_MASK] > MAX_MATCH) {
- double symbolcost = costmodel(MAX_MATCH, 1, costcontext);
- /* Set the length to reach each one to MAX_MATCH, and the cost to the
- cost corresponding to that length. Doing this, we skip MAX_MATCH
- values to avoid calling FindLongestMatch. */
- for (k = 0; k < MAX_MATCH; k++) {
- costs[j + MAX_MATCH] = costs[j] + symbolcost;
- length_array[j + MAX_MATCH] = MAX_MATCH;
+ ZOPFLI_MAX_MATCH characters before and after our position. */
+ if (h->same[i & ZOPFLI_WINDOW_MASK] > ZOPFLI_MAX_MATCH * 2
+ && i > instart + ZOPFLI_MAX_MATCH + 1
+ && i + ZOPFLI_MAX_MATCH * 2 + 1 < inend
+ && h->same[(i - ZOPFLI_MAX_MATCH) & ZOPFLI_WINDOW_MASK]
+ > ZOPFLI_MAX_MATCH) {
+ double symbolcost = costmodel(ZOPFLI_MAX_MATCH, 1, costcontext);
+ /* Set the length to reach each one to ZOPFLI_MAX_MATCH, and the cost to
+ the cost corresponding to that length. Doing this, we skip
+ ZOPFLI_MAX_MATCH values to avoid calling ZopfliFindLongestMatch. */
+ for (k = 0; k < ZOPFLI_MAX_MATCH; k++) {
+ costs[j + ZOPFLI_MAX_MATCH] = costs[j] + symbolcost;
+ length_array[j + ZOPFLI_MAX_MATCH] = ZOPFLI_MAX_MATCH;
i++;
j++;
- UpdateHash(in, i, inend, h);
+ ZopfliUpdateHash(in, i, inend, h);
}
}
#endif
- FindLongestMatch(s, h, in, i, inend, MAX_MATCH, sublen, &dist, &leng);
+ ZopfliFindLongestMatch(s, h, in, i, inend, ZOPFLI_MAX_MATCH, sublen,
+ &dist, &leng);
/* Literal. */
if (i + 1 <= inend) {
@@ -278,7 +288,7 @@ static double GetBestLengths(BlockState *s,
newCost = costs[j] + costmodel(k, sublen[k], costcontext);
assert(newCost >= 0);
if (newCost < costs[j + k]) {
- assert(k <= MAX_MATCH);
+ assert(k <= ZOPFLI_MAX_MATCH);
costs[j + k] = newCost;
length_array[j + k] = k;
}
@@ -288,7 +298,7 @@ static double GetBestLengths(BlockState *s,
assert(costs[blocksize] >= 0);
result = costs[blocksize];
- CleanHash(h);
+ ZopfliCleanHash(h);
free(costs);
return result;
@@ -305,9 +315,9 @@ static void TraceBackwards(size_t size, const unsigned short* length_array,
size_t index = size;
if (size == 0) return;
for (;;) {
- APPEND_DATA(length_array[index], path, pathsize);
+ ZOPFLI_APPEND_DATA(length_array[index], path, pathsize);
assert(length_array[index] <= index);
- assert(length_array[index] <= MAX_MATCH);
+ assert(length_array[index] <= ZOPFLI_MAX_MATCH);
assert(length_array[index] != 0);
index -= length_array[index];
if (index == 0) break;
@@ -321,24 +331,25 @@ static void TraceBackwards(size_t size, const unsigned short* length_array,
}
}
-static void FollowPath(BlockState* s,
+static void FollowPath(ZopfliBlockState* s,
const unsigned char* in, size_t instart, size_t inend,
unsigned short* path, size_t pathsize,
- LZ77Store* store) {
+ ZopfliLZ77Store* store) {
size_t i, j, pos = 0;
- size_t windowstart = instart > WINDOW_SIZE ? instart - WINDOW_SIZE : 0;
+ size_t windowstart = instart > ZOPFLI_WINDOW_SIZE
+ ? instart - ZOPFLI_WINDOW_SIZE : 0;
size_t total_length_test = 0;
- Hash hash;
- Hash* h = &hash;
+ ZopfliHash hash;
+ ZopfliHash* h = &hash;
if (instart == inend) return;
- InitHash(WINDOW_SIZE, h);
- WarmupHash(in, windowstart, inend, h);
+ ZopfliInitHash(ZOPFLI_WINDOW_SIZE, h);
+ ZopfliWarmupHash(in, windowstart, inend, h);
for (i = windowstart; i < instart; i++) {
- UpdateHash(in, i, inend, h);
+ ZopfliUpdateHash(in, i, inend, h);
}
pos = instart;
@@ -348,50 +359,51 @@ static void FollowPath(BlockState* s,
unsigned short dist;
assert(pos < inend);
- UpdateHash(in, pos, inend, h);
+ ZopfliUpdateHash(in, pos, inend, h);
/* Add to output. */
- if (length >= MIN_MATCH) {
+ if (length >= ZOPFLI_MIN_MATCH) {
/* Get the distance by recalculating longest match. The found length
should match the length from the path. */
- FindLongestMatch(s, h, in, pos, inend, length, 0, &dist, &dummy_length);
+ ZopfliFindLongestMatch(s, h, in, pos, inend, length, 0,
+ &dist, &dummy_length);
assert(!(dummy_length != length && length > 2 && dummy_length > 2));
- VerifyLenDist(in, inend, pos, dist, length);
- StoreLitLenDist(length, dist, store);
+ ZopfliVerifyLenDist(in, inend, pos, dist, length);
+ ZopfliStoreLitLenDist(length, dist, store);
total_length_test += length;
} else {
length = 1;
- StoreLitLenDist(in[pos], 0, store);
+ ZopfliStoreLitLenDist(in[pos], 0, store);
total_length_test++;
}
assert(pos + length <= inend);
for (j = 1; j < length; j++) {
- UpdateHash(in, pos + j, inend, h);
+ ZopfliUpdateHash(in, pos + j, inend, h);
}
pos += length;
}
- CleanHash(h);
+ ZopfliCleanHash(h);
}
/* Calculates the entropy of the statistics */
static void CalculateStatistics(SymbolStats* stats) {
- CalculateEntropy(stats->litlens, 288, stats->ll_symbols);
- CalculateEntropy(stats->dists, 32, stats->d_symbols);
+ ZopfliCalculateEntropy(stats->litlens, 288, stats->ll_symbols);
+ ZopfliCalculateEntropy(stats->dists, 32, stats->d_symbols);
}
/* Appends the symbol statistics from the store. */
-static void GetStatistics(const LZ77Store* store, SymbolStats* stats) {
+static void GetStatistics(const ZopfliLZ77Store* store, SymbolStats* stats) {
size_t i;
for (i = 0; i < store->size; i++) {
if (store->dists[i] == 0) {
stats->litlens[store->litlens[i]]++;
} else {
- stats->litlens[GetLengthSymbol(store->litlens[i])]++;
- stats->dists[GetDistSymbol(store->dists[i])]++;
+ stats->litlens[ZopfliGetLengthSymbol(store->litlens[i])]++;
+ stats->dists[ZopfliGetDistSymbol(store->dists[i])]++;
}
}
stats->litlens[256] = 1; /* End symbol. */
@@ -400,8 +412,8 @@ static void GetStatistics(const LZ77Store* store, SymbolStats* stats) {
}
/*
-Does a single run for LZ77Optimal. For good compression, repeated runs with
-updated statistics should be performed.
+Does a single run for ZopfliLZ77Optimal. For good compression, repeated runs
+with updated statistics should be performed.
s: the block state
in: the input data array
@@ -416,11 +428,11 @@ store: place to output the LZ77 data
returns the cost that was, according to the costmodel, needed to get to the end.
This is not the actual cost.
*/
-static double LZ77OptimalRun(BlockState* s,
+static double LZ77OptimalRun(ZopfliBlockState* s,
const unsigned char* in, size_t instart, size_t inend,
unsigned short** path, size_t* pathsize,
unsigned short* length_array, CostModelFun* costmodel,
- void* costcontext, LZ77Store* store) {
+ void* costcontext, ZopfliLZ77Store* store) {
double cost = GetBestLengths(
s, in, instart, inend, costmodel, costcontext, length_array);
free(*path);
@@ -428,53 +440,55 @@ static double LZ77OptimalRun(BlockState* s,
*pathsize = 0;
TraceBackwards(inend - instart, length_array, path, pathsize);
FollowPath(s, in, instart, inend, *path, *pathsize, store);
- assert(cost < LARGE_FLOAT);
+ assert(cost < ZOPFLI_LARGE_FLOAT);
return cost;
}
-void LZ77Optimal(BlockState *s,
- const unsigned char* in, size_t instart, size_t inend,
- LZ77Store* store) {
+void ZopfliLZ77Optimal(ZopfliBlockState *s,
+ const unsigned char* in, size_t instart, size_t inend,
+ ZopfliLZ77Store* store) {
/* Dist to get to here with smallest cost. */
size_t blocksize = inend - instart;
unsigned short* length_array =
(unsigned short*)malloc(sizeof(unsigned short) * (blocksize + 1));
unsigned short* path = 0;
size_t pathsize = 0;
- LZ77Store currentstore;
+ ZopfliLZ77Store currentstore;
SymbolStats stats, beststats, laststats;
int i;
double cost;
- double bestcost = LARGE_FLOAT;
+ double bestcost = ZOPFLI_LARGE_FLOAT;
double lastcost = 0;
/* Try randomizing the costs a bit once the size stabilizes. */
+ RanState ran_state;
int lastrandomstep = -1;
if (!length_array) exit(-1); /* Allocation failed. */
+ InitRanState(&ran_state);
InitStats(&stats);
- InitLZ77Store(&currentstore);
+ ZopfliInitLZ77Store(&currentstore);
/* Do regular deflate, then loop multiple shortest path runs, each time using
the statistics of the previous run. */
/* Initial run. */
- LZ77Greedy(s, in, instart, inend, &currentstore);
+ ZopfliLZ77Greedy(s, in, instart, inend, &currentstore);
GetStatistics(&currentstore, &stats);
/* Repeat statistics with each time the cost model from the previous stat
run. */
for (i = 0; i < s->options->numiterations; i++) {
- CleanLZ77Store(&currentstore);
- InitLZ77Store(&currentstore);
+ ZopfliCleanLZ77Store(&currentstore);
+ ZopfliInitLZ77Store(&currentstore);
LZ77OptimalRun(s, in, instart, inend, &path, &pathsize,
length_array, GetCostStat, (void*)&stats,
&currentstore);
- cost = CalculateBlockSize(currentstore.litlens, currentstore.dists,
- 0, currentstore.size, 2);
+ cost = ZopfliCalculateBlockSize(currentstore.litlens, currentstore.dists,
+ 0, currentstore.size, 2);
if (cost < bestcost) {
/* Copy to the output store. */
- CopyLZ77Store(&currentstore, store);
+ ZopfliCopyLZ77Store(&currentstore, store);
CopyStats(&stats, &beststats);
bestcost = cost;
}
@@ -490,7 +504,7 @@ void LZ77Optimal(BlockState *s,
}
if (i > 5 && cost == lastcost) {
CopyStats(&beststats, &stats);
- RandomizeStatFreqs(&stats);
+ RandomizeStatFreqs(&ran_state, &stats);
CalculateStatistics(&stats);
lastrandomstep = i;
}
@@ -499,12 +513,13 @@ void LZ77Optimal(BlockState *s,
free(length_array);
free(path);
- CleanLZ77Store(&currentstore);
+ ZopfliCleanLZ77Store(&currentstore);
}
-void LZ77OptimalFixed(BlockState *s,
- const unsigned char* in, size_t instart, size_t inend,
- LZ77Store* store)
+void ZopfliLZ77OptimalFixed(ZopfliBlockState *s,
+ const unsigned char* in,
+ size_t instart, size_t inend,
+ ZopfliLZ77Store* store)
{
/* Dist to get to here with smallest cost. */
size_t blocksize = inend - instart;
diff --git a/squeeze.h b/squeeze.h
index 31895c0..e850aaa 100644
--- a/squeeze.h
+++ b/squeeze.h
@@ -38,22 +38,23 @@ Calculates lit/len and dist pairs for given data.
If instart is larger than 0, it uses values before instart as starting
dictionary.
*/
-void LZ77Optimal(BlockState *s,
- const unsigned char* in, size_t instart, size_t inend,
- LZ77Store* store);
+void ZopfliLZ77Optimal(ZopfliBlockState *s,
+ const unsigned char* in, size_t instart, size_t inend,
+ ZopfliLZ77Store* store);
/*
-Does the same as LZ77Optimal, but optimized for the fixed tree of the deflate
-standard.
-The fixed tree rarely gives the best compression. But this gives the best
+Does the same as ZopfliLZ77Optimal, but optimized for the fixed tree of the
+deflate standard.
+The fixed tree never gives the best compression. But this gives the best
possible LZ77 encoding possible with the fixed tree.
This does not create or output any fixed tree, only LZ77 data optimized for
using with a fixed tree.
If instart is larger than 0, it uses values before instart as starting
dictionary.
*/
-void LZ77OptimalFixed(BlockState *s,
- const unsigned char* in, size_t instart, size_t inend,
- LZ77Store* store);
+void ZopfliLZ77OptimalFixed(ZopfliBlockState *s,
+ const unsigned char* in,
+ size_t instart, size_t inend,
+ ZopfliLZ77Store* store);
#endif /* ZOPFLI_SQUEEZE_H_ */
diff --git a/tree.c b/tree.c
index 98abe48..c457511 100644
--- a/tree.c
+++ b/tree.c
@@ -27,8 +27,8 @@ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
#include "katajainen.h"
#include "util.h"
-void LengthsToSymbols(const unsigned* lengths, size_t n, unsigned maxbits,
- unsigned* symbols) {
+void ZopfliLengthsToSymbols(const unsigned* lengths, size_t n, unsigned maxbits,
+ unsigned* symbols) {
size_t* bl_count = (size_t*)malloc(sizeof(size_t) * (maxbits + 1));
size_t* next_code = (size_t*)malloc(sizeof(size_t) * (maxbits + 1));
unsigned bits, i;
@@ -68,7 +68,7 @@ void LengthsToSymbols(const unsigned* lengths, size_t n, unsigned maxbits,
free(next_code);
}
-void CalculateEntropy(const size_t* count, size_t n, double* bitlengths) {
+void ZopfliCalculateEntropy(const size_t* count, size_t n, double* bitlengths) {
static const double kInvLog2 = 1.4426950408889; /* 1.0 / log(2.0) */
unsigned sum = 0;
unsigned i;
@@ -93,9 +93,9 @@ void CalculateEntropy(const size_t* count, size_t n, double* bitlengths) {
}
}
-void CalculateBitLengths(const size_t* count, size_t n, int maxbits,
- unsigned* bitlengths) {
- int error = LengthLimitedCodeLengths(count, n, maxbits, bitlengths);
+void ZopfliCalculateBitLengths(const size_t* count, size_t n, int maxbits,
+ unsigned* bitlengths) {
+ int error = ZopfliLengthLimitedCodeLengths(count, n, maxbits, bitlengths);
(void) error;
assert(!error);
}
diff --git a/tree.h b/tree.h
index 5f45bc5..4d6f469 100644
--- a/tree.h
+++ b/tree.h
@@ -30,21 +30,22 @@ Utilities for creating and using Huffman trees.
Calculates the bitlengths for the Huffman tree, based on the counts of each
symbol.
*/
-void CalculateBitLengths(const size_t* count, size_t n, int maxbits,
- unsigned *bitlengths);
+void ZopfliCalculateBitLengths(const size_t* count, size_t n, int maxbits,
+ unsigned *bitlengths);
/*
Converts a series of Huffman tree bitlengths, to the bit values of the symbols.
*/
-void LengthsToSymbols(const unsigned* lengths, size_t n, unsigned maxbits,
- unsigned* symbols);
+void ZopfliLengthsToSymbols(const unsigned* lengths, size_t n, unsigned maxbits,
+ unsigned* symbols);
/*
Calculates the entropy of each symbol, based on the counts of each symbol. The
-result is similar to the result of CalculateBitLengths, but with the actual
-theoritical bit lengths according to the entropy. Since the resulting values
-are fractional, they cannot be used to encode the tree specified by DEFLATE.
+result is similar to the result of ZopfliCalculateBitLengths, but with the
+actual theoritical bit lengths according to the entropy. Since the resulting
+values are fractional, they cannot be used to encode the tree specified by
+DEFLATE.
*/
-void CalculateEntropy(const size_t* count, size_t n, double* bitlengths);
+void ZopfliCalculateEntropy(const size_t* count, size_t n, double* bitlengths);
#endif /* ZOPFLI_TREE_H_ */
diff --git a/util.c b/util.c
index d135dd2..17cb111 100644
--- a/util.c
+++ b/util.c
@@ -23,7 +23,7 @@ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
#include <stdio.h>
#include <stdlib.h>
-int GetDistExtraBits(int dist) {
+int ZopfliGetDistExtraBits(int dist) {
#ifdef __GNUC__
if (dist < 5) return 0;
return (31 ^ __builtin_clz(dist - 1)) - 1; /* log2(dist - 1) - 1 */
@@ -45,7 +45,7 @@ int GetDistExtraBits(int dist) {
#endif
}
-int GetDistExtraBitsValue(int dist) {
+int ZopfliGetDistExtraBitsValue(int dist) {
#ifdef __GNUC__
if (dist < 5) {
return 0;
@@ -71,7 +71,7 @@ int GetDistExtraBitsValue(int dist) {
#endif
}
-int GetDistSymbol(int dist) {
+int ZopfliGetDistSymbol(int dist) {
#ifdef __GNUC__
if (dist < 5) {
return dist - 1;
@@ -120,7 +120,7 @@ int GetDistSymbol(int dist) {
#endif
}
-int GetLengthExtraBits(int l) {
+int ZopfliGetLengthExtraBits(int l) {
static const int table[259] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
@@ -142,7 +142,7 @@ int GetLengthExtraBits(int l) {
return table[l];
}
-int GetLengthExtraBitsValue(int l) {
+int ZopfliGetLengthExtraBitsValue(int l) {
static const int table[259] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 0,
1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5,
@@ -163,7 +163,7 @@ int GetLengthExtraBitsValue(int l) {
/*
Returns symbol in range [257-285] (inclusive).
*/
-int GetLengthSymbol(int l) {
+int ZopfliGetLengthSymbol(int l) {
static const int table[259] = {
0, 0, 0, 257, 258, 259, 260, 261, 262, 263, 264,
265, 265, 266, 266, 267, 267, 268, 268,
@@ -201,7 +201,7 @@ int GetLengthSymbol(int l) {
return table[l];
}
-void InitOptions(Options* options) {
+void ZopfliInitOptions(ZopfliOptions* options) {
options->verbose = 0;
options->numiterations = 15;
options->blocksplitting = 1;
diff --git a/util.h b/util.h
index a1ac653..6a7aafa 100644
--- a/util.h
+++ b/util.h
@@ -29,21 +29,21 @@ basic deflate specification values and generic program options.
#include <stdlib.h>
/* Minimum and maximum length that can be encoded in deflate. */
-#define MAX_MATCH 258
-#define MIN_MATCH 3
+#define ZOPFLI_MAX_MATCH 258
+#define ZOPFLI_MIN_MATCH 3
/*
The window size for deflate. Must be a power of two. This should be 32768, the
maximum possible by the deflate spec. Anything less hurts compression more than
speed.
*/
-#define WINDOW_SIZE 32768
+#define ZOPFLI_WINDOW_SIZE 32768
/*
The window mask used to wrap indices into the window. This is why the
window size must be a power of two.
*/
-#define WINDOW_MASK (WINDOW_SIZE - 1)
+#define ZOPFLI_WINDOW_MASK (ZOPFLI_WINDOW_SIZE - 1)
/*
A block structure of huge, non-smart, blocks to divide the input into, to allow
@@ -53,12 +53,12 @@ be executed independently on each huge block.
Dividing into huge blocks hurts compression, but not much relative to the size.
Set this to, for example, 20MB (20000000). Set it to 0 to disable master blocks.
*/
-#define MASTER_BLOCK_SIZE 20000000
+#define ZOPFLI_MASTER_BLOCK_SIZE 20000000
/*
Used to initialize costs for example
*/
-#define LARGE_FLOAT 1e30
+#define ZOPFLI_LARGE_FLOAT 1e30
/*
For longest match cache. max 256. Uses huge amounts of memory but makes it
@@ -67,80 +67,80 @@ This is so because longest match finding has to find the exact distance
that belongs to each length for the best lz77 strategy.
Good values: e.g. 5, 8.
*/
-#define NUM_CACHED_LENGTHS 8
+#define ZOPFLI_CACHE_LENGTH 8
/*
limit the max hash chain hits for this hash value. This has an effect only
on files where the hash value is the same very often. On these files, this
gives worse compression (the value should ideally be 32768, which is the
-WINDOW_SIZE, while zlib uses 4096 even for best level), but makes it faster on
-some specific files.
+ZOPFLI_WINDOW_SIZE, while zlib uses 4096 even for best level), but makes it
+faster on some specific files.
Good value: e.g. 8192.
*/
-#define MAX_CHAIN_HITS 8192
+#define ZOPFLI_MAX_CHAIN_HITS 8192
/*
-Whether to use the longest match cache for FindLongestMatch. This cache
+Whether to use the longest match cache for ZopfliFindLongestMatch. This cache
consumes a lot of memory but speeds it up. No effect on compression size.
*/
-#define USE_LONGEST_MATCH_CACHE
+#define ZOPFLI_LONGEST_MATCH_CACHE
/*
Enable to remember amount of successive identical bytes in the hash chain for
finding longest match
-required for USE_HASH_SAME_HASH and SHORTCUT_LONG_REPETITIONS
+required for ZOPFLI_HASH_SAME_HASH and ZOPFLI_SHORTCUT_LONG_REPETITIONS
This has no effect on the compression result, and enabling it increases speed.
*/
-#define USE_HASH_SAME
+#define ZOPFLI_HASH_SAME
/*
-Switch to a faster hash based on the info from USE_HASH_SAME once the
+Switch to a faster hash based on the info from ZOPFLI_HASH_SAME once the
best length so far is long enough. This is way faster for files with lots of
identical bytes, on which the compressor is otherwise too slow. Regular files
are unaffected or maybe a tiny bit slower.
This has no effect on the compression result, only on speed.
*/
-#define USE_HASH_SAME_HASH
+#define ZOPFLI_HASH_SAME_HASH
/*
Enable this, to avoid slowness for files which are a repetition of the same
-character more than a multiple of MAX_MATCH times. This should not affect the
-compression result.
+character more than a multiple of ZOPFLI_MAX_MATCH times. This should not affect
+the compression result.
*/
-#define SHORTCUT_LONG_REPETITIONS
+#define ZOPFLI_SHORTCUT_LONG_REPETITIONS
/*
Whether to use lazy matching in the greedy LZ77 implementation. This gives a
-better result of LZ77Greedy, but the effect this has on the optimal LZ77
+better result of ZopfliLZ77Greedy, but the effect this has on the optimal LZ77
varies from file to file.
*/
-#define LAZY_MATCHING
+#define ZOPFLI_LAZY_MATCHING
/*
Gets the symbol for the given length, cfr. the DEFLATE spec.
Returns the symbol in the range [257-285] (inclusive)
*/
-int GetLengthSymbol(int l);
+int ZopfliGetLengthSymbol(int l);
/* Gets the amount of extra bits for the given length, cfr. the DEFLATE spec. */
-int GetLengthExtraBits(int l);
+int ZopfliGetLengthExtraBits(int l);
/* Gets value of the extra bits for the given length, cfr. the DEFLATE spec. */
-int GetLengthExtraBitsValue(int l);
+int ZopfliGetLengthExtraBitsValue(int l);
/* Gets the symbol for the given dist, cfr. the DEFLATE spec. */
-int GetDistSymbol(int dist);
+int ZopfliGetDistSymbol(int dist);
/* Gets the amount of extra bits for the given dist, cfr. the DEFLATE spec. */
-int GetDistExtraBits(int dist);
+int ZopfliGetDistExtraBits(int dist);
/* Gets value of the extra bits for the given dist, cfr. the DEFLATE spec. */
-int GetDistExtraBitsValue(int dist);
+int ZopfliGetDistExtraBitsValue(int dist);
/*
Options used throughout the program.
*/
-typedef struct Options {
+typedef struct ZopfliOptions {
/* Whether to print output */
int verbose;
@@ -171,10 +171,10 @@ typedef struct Options {
extreme results that hurt compression on some files). Default value: 15.
*/
int blocksplittingmax;
-} Options;
+} ZopfliOptions;
/* Initializes options with default values. */
-void InitOptions(Options* options);
+void ZopfliInitOptions(ZopfliOptions* options);
/*
Appends value to dynamically allocated memory, doubling its allocation size
@@ -188,7 +188,7 @@ Precondition: allocated size of data is at least a power of two greater than or
equal than *size.
*/
#ifdef __cplusplus /* C++ cannot assign void* from malloc to *data */
-#define APPEND_DATA(/* T */ value, /* T** */ data, /* size_t* */ size) {\
+#define ZOPFLI_APPEND_DATA(/* T */ value, /* T** */ data, /* size_t* */ size) {\
if (!((*size) & ((*size) - 1))) {\
/*double alloc size if it's a power of two*/\
void** data_void = reinterpret_cast<void**>(data);\
@@ -199,7 +199,7 @@ equal than *size.
(*size)++;\
}
#else /* C gives problems with strict-aliasing rules for (void**) cast */
-#define APPEND_DATA(/* T */ value, /* T** */ data, /* size_t* */ size) {\
+#define ZOPFLI_APPEND_DATA(/* T */ value, /* T** */ data, /* size_t* */ size) {\
if (!((*size) & ((*size) - 1))) {\
/*double alloc size if it's a power of two*/\
(*data) = (*size) == 0 ? malloc(sizeof(**data))\
diff --git a/zlib_container.c b/zlib_container.c
index b77d023..7d9f195 100644
--- a/zlib_container.c
+++ b/zlib_container.c
@@ -46,9 +46,9 @@ static unsigned adler32(const unsigned char* data, size_t size)
return (s2 << 16) | s1;
}
-void ZlibCompress(const Options* options,
- const unsigned char* in, size_t insize,
- unsigned char** out, size_t* outsize) {
+void ZopfliZlibCompress(const ZopfliOptions* options,
+ const unsigned char* in, size_t insize,
+ unsigned char** out, size_t* outsize) {
unsigned char bitpointer = 0;
unsigned checksum = adler32(in, (unsigned)insize);
unsigned cmf = 120; /* CM 8, CINFO 7. See zlib spec.*/
@@ -58,16 +58,16 @@ void ZlibCompress(const Options* options,
unsigned fcheck = 31 - cmfflg % 31;
cmfflg += fcheck;
- APPEND_DATA(cmfflg / 256, out, outsize);
- APPEND_DATA(cmfflg % 256, out, outsize);
+ ZOPFLI_APPEND_DATA(cmfflg / 256, out, outsize);
+ ZOPFLI_APPEND_DATA(cmfflg % 256, out, outsize);
- Deflate(options, 2 /* dynamic block */, 1 /* final */,
- in, insize, &bitpointer, out, outsize);
+ ZopfliDeflate(options, 2 /* dynamic block */, 1 /* final */,
+ in, insize, &bitpointer, out, outsize);
- APPEND_DATA((checksum >> 24) % 256, out, outsize);
- APPEND_DATA((checksum >> 16) % 256, out, outsize);
- APPEND_DATA((checksum >> 8) % 256, out, outsize);
- APPEND_DATA(checksum % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((checksum >> 24) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((checksum >> 16) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA((checksum >> 8) % 256, out, outsize);
+ ZOPFLI_APPEND_DATA(checksum % 256, out, outsize);
if (options->verbose) {
fprintf(stderr,
diff --git a/zlib_container.h b/zlib_container.h
index 3b11e7a..05644a2 100644
--- a/zlib_container.h
+++ b/zlib_container.h
@@ -35,8 +35,8 @@ out: pointer to the dynamic output array to which the result is appended. Must
be freed after use.
outsize: pointer to the dynamic output array size.
*/
-void ZlibCompress(const Options* options,
- const unsigned char* in, size_t insize,
- unsigned char** out, size_t* outsize);
+void ZopfliZlibCompress(const ZopfliOptions* options,
+ const unsigned char* in, size_t insize,
+ unsigned char** out, size_t* outsize);
#endif /* ZOPFLI_ZLIB_H_ */
diff --git a/zopfli.c b/zopfli.c
index 377b43f..f56d7d8 100644
--- a/zopfli.c
+++ b/zopfli.c
@@ -85,10 +85,10 @@ typedef enum {
/*
outfilename: filename to write output to, or 0 to write to stdout instead
*/
-void CompressFile(const Options* options,
- OutputType output_type,
- const char* infilename,
- const char* outfilename) {
+static void CompressFile(const ZopfliOptions* options,
+ OutputType output_type,
+ const char* infilename,
+ const char* outfilename) {
unsigned char* in;
size_t insize;
unsigned char* out = 0;
@@ -99,12 +99,13 @@ void CompressFile(const Options* options,
return;
}
if (output_type == OUTPUT_GZIP) {
- GzipCompress(options, in, insize, &out, &outsize);
+ ZopfliGzipCompress(options, in, insize, &out, &outsize);
} else if (output_type == OUTPUT_ZLIB) {
- ZlibCompress(options, in, insize, &out, &outsize);
+ ZopfliZlibCompress(options, in, insize, &out, &outsize);
} else if (output_type == OUTPUT_DEFLATE) {
unsigned char bp = 0;
- Deflate(options, 2 /* Dynamic block */, 1, in, insize, &bp, &out, &outsize);
+ ZopfliDeflate(options, 2 /* Dynamic block */, 1,
+ in, insize, &bp, &out, &outsize);
} else {
assert(0);
}
@@ -139,13 +140,13 @@ static char StringsEqual(const char* str1, const char* str2) {
}
int main(int argc, char* argv[]) {
- Options options;
+ ZopfliOptions options;
const char* filename = 0;
int output_to_stdout = 0;
int i;
OutputType output_type = OUTPUT_GZIP;
- InitOptions(&options);
+ ZopfliInitOptions(&options);
for (i = 1; i < argc; i++) {
if (StringsEqual(argv[i], "-v")) options.verbose = 1;
@@ -194,10 +195,9 @@ int main(int argc, char* argv[]) {
outfilename = AddStrings(filename, ".gz");
} else if (output_type == OUTPUT_ZLIB) {
outfilename = AddStrings(filename, ".zlib");
- } else if (output_type == OUTPUT_DEFLATE) {
- outfilename = AddStrings(filename, ".deflate");
} else {
- assert(0);
+ assert(output_type == OUTPUT_DEFLATE);
+ outfilename = AddStrings(filename, ".deflate");
}
if (options.verbose && outfilename) {
fprintf(stderr, "Saving to: %s\n", outfilename);