lz77.h revision 1.1.1.1.8.2 1 1.1.1.1.8.2 snj /*
2 1.1.1.1.8.2 snj Copyright 2011 Google Inc. All Rights Reserved.
3 1.1.1.1.8.2 snj
4 1.1.1.1.8.2 snj Licensed under the Apache License, Version 2.0 (the "License");
5 1.1.1.1.8.2 snj you may not use this file except in compliance with the License.
6 1.1.1.1.8.2 snj You may obtain a copy of the License at
7 1.1.1.1.8.2 snj
8 1.1.1.1.8.2 snj http://www.apache.org/licenses/LICENSE-2.0
9 1.1.1.1.8.2 snj
10 1.1.1.1.8.2 snj Unless required by applicable law or agreed to in writing, software
11 1.1.1.1.8.2 snj distributed under the License is distributed on an "AS IS" BASIS,
12 1.1.1.1.8.2 snj WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 1.1.1.1.8.2 snj See the License for the specific language governing permissions and
14 1.1.1.1.8.2 snj limitations under the License.
15 1.1.1.1.8.2 snj
16 1.1.1.1.8.2 snj Author: lode.vandevenne (at) gmail.com (Lode Vandevenne)
17 1.1.1.1.8.2 snj Author: jyrki.alakuijala (at) gmail.com (Jyrki Alakuijala)
18 1.1.1.1.8.2 snj */
19 1.1.1.1.8.2 snj
20 1.1.1.1.8.2 snj /*
21 1.1.1.1.8.2 snj Functions for basic LZ77 compression and utilities for the "squeeze" LZ77
22 1.1.1.1.8.2 snj compression.
23 1.1.1.1.8.2 snj */
24 1.1.1.1.8.2 snj
25 1.1.1.1.8.2 snj #ifndef ZOPFLI_LZ77_H_
26 1.1.1.1.8.2 snj #define ZOPFLI_LZ77_H_
27 1.1.1.1.8.2 snj
28 1.1.1.1.8.2 snj #include <stdlib.h>
29 1.1.1.1.8.2 snj
30 1.1.1.1.8.2 snj #include "cache.h"
31 1.1.1.1.8.2 snj #include "hash.h"
32 1.1.1.1.8.2 snj #include "zopfli.h"
33 1.1.1.1.8.2 snj
34 1.1.1.1.8.2 snj /*
35 1.1.1.1.8.2 snj Stores lit/length and dist pairs for LZ77.
36 1.1.1.1.8.2 snj litlens: Contains the literal symbols or length values.
37 1.1.1.1.8.2 snj dists: Indicates the distance, or 0 to indicate that there is no distance and
38 1.1.1.1.8.2 snj litlens contains a literal instead of a length.
39 1.1.1.1.8.2 snj litlens and dists both have the same size.
40 1.1.1.1.8.2 snj */
41 1.1.1.1.8.2 snj typedef struct ZopfliLZ77Store {
42 1.1.1.1.8.2 snj unsigned short* litlens; /* Lit or len. */
43 1.1.1.1.8.2 snj unsigned short* dists; /* If 0: indicates literal in corresponding litlens,
44 1.1.1.1.8.2 snj if > 0: length in corresponding litlens, this is the distance. */
45 1.1.1.1.8.2 snj size_t size;
46 1.1.1.1.8.2 snj } ZopfliLZ77Store;
47 1.1.1.1.8.2 snj
48 1.1.1.1.8.2 snj void ZopfliInitLZ77Store(ZopfliLZ77Store* store);
49 1.1.1.1.8.2 snj void ZopfliCleanLZ77Store(ZopfliLZ77Store* store);
50 1.1.1.1.8.2 snj void ZopfliCopyLZ77Store(const ZopfliLZ77Store* source, ZopfliLZ77Store* dest);
51 1.1.1.1.8.2 snj void ZopfliStoreLitLenDist(unsigned short length, unsigned short dist,
52 1.1.1.1.8.2 snj ZopfliLZ77Store* store);
53 1.1.1.1.8.2 snj
54 1.1.1.1.8.2 snj /*
55 1.1.1.1.8.2 snj Some state information for compressing a block.
56 1.1.1.1.8.2 snj This is currently a bit under-used (with mainly only the longest match cache),
57 1.1.1.1.8.2 snj but is kept for easy future expansion.
58 1.1.1.1.8.2 snj */
59 1.1.1.1.8.2 snj typedef struct ZopfliBlockState {
60 1.1.1.1.8.2 snj const ZopfliOptions* options;
61 1.1.1.1.8.2 snj
62 1.1.1.1.8.2 snj #ifdef ZOPFLI_LONGEST_MATCH_CACHE
63 1.1.1.1.8.2 snj /* Cache for length/distance pairs found so far. */
64 1.1.1.1.8.2 snj ZopfliLongestMatchCache* lmc;
65 1.1.1.1.8.2 snj #endif
66 1.1.1.1.8.2 snj
67 1.1.1.1.8.2 snj /* The start (inclusive) and end (not inclusive) of the current block. */
68 1.1.1.1.8.2 snj size_t blockstart;
69 1.1.1.1.8.2 snj size_t blockend;
70 1.1.1.1.8.2 snj } ZopfliBlockState;
71 1.1.1.1.8.2 snj
72 1.1.1.1.8.2 snj /*
73 1.1.1.1.8.2 snj Finds the longest match (length and corresponding distance) for LZ77
74 1.1.1.1.8.2 snj compression.
75 1.1.1.1.8.2 snj Even when not using "sublen", it can be more efficient to provide an array,
76 1.1.1.1.8.2 snj because only then the caching is used.
77 1.1.1.1.8.2 snj array: the data
78 1.1.1.1.8.2 snj pos: position in the data to find the match for
79 1.1.1.1.8.2 snj size: size of the data
80 1.1.1.1.8.2 snj limit: limit length to maximum this value (default should be 258). This allows
81 1.1.1.1.8.2 snj finding a shorter dist for that length (= less extra bits). Must be
82 1.1.1.1.8.2 snj in the range [ZOPFLI_MIN_MATCH, ZOPFLI_MAX_MATCH].
83 1.1.1.1.8.2 snj sublen: output array of 259 elements, or null. Has, for each length, the
84 1.1.1.1.8.2 snj smallest distance required to reach this length. Only 256 of its 259 values
85 1.1.1.1.8.2 snj are used, the first 3 are ignored (the shortest length is 3. It is purely
86 1.1.1.1.8.2 snj for convenience that the array is made 3 longer).
87 1.1.1.1.8.2 snj */
88 1.1.1.1.8.2 snj void ZopfliFindLongestMatch(
89 1.1.1.1.8.2 snj ZopfliBlockState *s, const ZopfliHash* h, const unsigned char* array,
90 1.1.1.1.8.2 snj size_t pos, size_t size, size_t limit,
91 1.1.1.1.8.2 snj unsigned short* sublen, unsigned short* distance, unsigned short* length);
92 1.1.1.1.8.2 snj
93 1.1.1.1.8.2 snj /*
94 1.1.1.1.8.2 snj Verifies if length and dist are indeed valid, only used for assertion.
95 1.1.1.1.8.2 snj */
96 1.1.1.1.8.2 snj void ZopfliVerifyLenDist(const unsigned char* data, size_t datasize, size_t pos,
97 1.1.1.1.8.2 snj unsigned short dist, unsigned short length);
98 1.1.1.1.8.2 snj
99 1.1.1.1.8.2 snj /*
100 1.1.1.1.8.2 snj Counts the number of literal, length and distance symbols in the given lz77
101 1.1.1.1.8.2 snj arrays.
102 1.1.1.1.8.2 snj litlens: lz77 lit/lengths
103 1.1.1.1.8.2 snj dists: ll77 distances
104 1.1.1.1.8.2 snj start: where to begin counting in litlens and dists
105 1.1.1.1.8.2 snj end: where to stop counting in litlens and dists (not inclusive)
106 1.1.1.1.8.2 snj ll_count: count of each lit/len symbol, must have size 288 (see deflate
107 1.1.1.1.8.2 snj standard)
108 1.1.1.1.8.2 snj d_count: count of each dist symbol, must have size 32 (see deflate standard)
109 1.1.1.1.8.2 snj */
110 1.1.1.1.8.2 snj void ZopfliLZ77Counts(const unsigned short* litlens,
111 1.1.1.1.8.2 snj const unsigned short* dists,
112 1.1.1.1.8.2 snj size_t start, size_t end,
113 1.1.1.1.8.2 snj size_t* ll_count, size_t* d_count);
114 1.1.1.1.8.2 snj
115 1.1.1.1.8.2 snj /*
116 1.1.1.1.8.2 snj Does LZ77 using an algorithm similar to gzip, with lazy matching, rather than
117 1.1.1.1.8.2 snj with the slow but better "squeeze" implementation.
118 1.1.1.1.8.2 snj The result is placed in the ZopfliLZ77Store.
119 1.1.1.1.8.2 snj If instart is larger than 0, it uses values before instart as starting
120 1.1.1.1.8.2 snj dictionary.
121 1.1.1.1.8.2 snj */
122 1.1.1.1.8.2 snj void ZopfliLZ77Greedy(ZopfliBlockState* s, const unsigned char* in,
123 1.1.1.1.8.2 snj size_t instart, size_t inend,
124 1.1.1.1.8.2 snj ZopfliLZ77Store* store);
125 1.1.1.1.8.2 snj
126 1.1.1.1.8.2 snj #endif /* ZOPFLI_LZ77_H_ */
127