2 LZ4 Encoder - Part of LZ4 compression algorithm
\r
3 Copyright (C) 2011-2013, Yann Collet.
\r
4 BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
\r
6 Redistribution and use in source and binary forms, with or without
\r
7 modification, are permitted provided that the following conditions are
\r
10 * Redistributions of source code must retain the above copyright
\r
11 notice, this list of conditions and the following disclaimer.
\r
12 * Redistributions in binary form must reproduce the above
\r
13 copyright notice, this list of conditions and the following disclaimer
\r
14 in the documentation and/or other materials provided with the
\r
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\r
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
\r
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
\r
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
\r
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
29 You can contact the author at :
\r
30 - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
\r
31 - LZ4 source repository : http://code.google.com/p/lz4/
\r
34 /* lz4_encoder.h must be included into lz4.c
\r
35 The objective of this file is to create a single LZ4 compression function source
\r
36 which will be instanciated multiple times with minor variations
\r
37 depending on a set of #define.
\r
42 //****************************
\r
43 // Check required defines
\r
44 //****************************
\r
46 #ifndef FUNCTION_NAME
\r
47 # error "FUNTION_NAME is not defined"
\r
51 //****************************
\r
52 // Local definitions
\r
53 //****************************
\r
56 # define HASHLOG (MEMORY_USAGE-1)
\r
57 # define CURRENT_H_TYPE U16
\r
58 # define CURRENTBASE(base) const BYTE* const base = ip
\r
60 # define HASHLOG (MEMORY_USAGE-2)
\r
61 # define CURRENT_H_TYPE HTYPE
\r
62 # define CURRENTBASE(base) INITBASE(base)
\r
65 #define HASHTABLE_NBCELLS (1U<<HASHLOG)
\r
66 #define LZ4_HASH(i) (((i) * 2654435761U) >> ((MINMATCH*8)-HASHLOG))
\r
67 #define LZ4_HASHVALUE(p) LZ4_HASH(A32(p))
\r
71 //****************************
\r
73 //****************************
\r
76 #ifdef USE_HEAPMEMORY
\r
82 #ifdef LIMITED_OUTPUT
\r
87 #ifdef USE_HEAPMEMORY
\r
88 CURRENT_H_TYPE* HashTable = (CURRENT_H_TYPE*)ctx;
\r
90 CURRENT_H_TYPE HashTable[HASHTABLE_NBCELLS] = {0};
\r
93 const BYTE* ip = (BYTE*) source;
\r
95 const BYTE* anchor = ip;
\r
96 const BYTE* const iend = ip + inputSize;
\r
97 const BYTE* const mflimit = iend - MFLIMIT;
\r
98 #define matchlimit (iend - LASTLITERALS)
\r
100 BYTE* op = (BYTE*) dest;
\r
101 #ifdef LIMITED_OUTPUT
\r
102 BYTE* const oend = op + maxOutputSize;
\r
106 const int skipStrength = SKIPSTRENGTH;
\r
111 if (inputSize<MINLENGTH) goto _last_literals;
\r
112 #ifdef COMPRESS_64K
\r
113 if (inputSize>LZ4_64KLIMIT) return 0; // Size too large (not within 64K limit)
\r
115 #ifdef USE_HEAPMEMORY
\r
116 memset((void*)HashTable, 0, HASHTABLESIZE);
\r
120 HashTable[LZ4_HASHVALUE(ip)] = (CURRENT_H_TYPE)(ip - base);
\r
121 ip++; forwardH = LZ4_HASHVALUE(ip);
\r
126 int findMatchAttempts = (1U << skipStrength) + 3;
\r
127 const BYTE* forwardIp = ip;
\r
134 int step = findMatchAttempts++ >> skipStrength;
\r
136 forwardIp = ip + step;
\r
138 if unlikely(forwardIp > mflimit) { goto _last_literals; }
\r
140 forwardH = LZ4_HASHVALUE(forwardIp);
\r
141 ref = base + HashTable[h];
\r
142 HashTable[h] = (CURRENT_H_TYPE)(ip - base);
\r
144 } while ((ref < ip - MAX_DISTANCE) || (A32(ref) != A32(ip)));
\r
147 while ((ip>anchor) && (ref>(BYTE*)source) && unlikely(ip[-1]==ref[-1])) { ip--; ref--; }
\r
149 // Encode Literal length
\r
150 length = (int)(ip - anchor);
\r
152 #ifdef LIMITED_OUTPUT
\r
153 if unlikely(op + length + (2 + 1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
\r
155 if (length>=(int)RUN_MASK)
\r
157 int len = length-RUN_MASK;
\r
158 *token=(RUN_MASK<<ML_BITS);
\r
159 for(; len >= 255 ; len-=255) *op++ = 255;
\r
162 else *token = (BYTE)(length<<ML_BITS);
\r
165 LZ4_BLINDCOPY(anchor, op, length);
\r
169 LZ4_WRITE_LITTLEENDIAN_16(op,(U16)(ip-ref));
\r
172 ip+=MINMATCH; ref+=MINMATCH; // MinMatch already verified
\r
174 while likely(ip<matchlimit-(STEPSIZE-1))
\r
176 UARCH diff = AARCH(ref) ^ AARCH(ip);
\r
177 if (!diff) { ip+=STEPSIZE; ref+=STEPSIZE; continue; }
\r
178 ip += LZ4_NbCommonBytes(diff);
\r
181 if (LZ4_ARCH64) if ((ip<(matchlimit-3)) && (A32(ref) == A32(ip))) { ip+=4; ref+=4; }
\r
182 if ((ip<(matchlimit-1)) && (A16(ref) == A16(ip))) { ip+=2; ref+=2; }
\r
183 if ((ip<matchlimit) && (*ref == *ip)) ip++;
\r
186 // Encode MatchLength
\r
187 length = (int)(ip - anchor);
\r
188 #ifdef LIMITED_OUTPUT
\r
189 if unlikely(op + (1 + LASTLITERALS) + (length>>8) > oend) return 0; // Check output limit
\r
191 if (length>=(int)ML_MASK)
\r
195 for (; length > 509 ; length-=510) { *op++ = 255; *op++ = 255; }
\r
196 if (length >= 255) { length-=255; *op++ = 255; }
\r
197 *op++ = (BYTE)length;
\r
199 else *token += (BYTE)(length);
\r
201 // Test end of chunk
\r
202 if (ip > mflimit) { anchor = ip; break; }
\r
205 HashTable[LZ4_HASHVALUE(ip-2)] = (CURRENT_H_TYPE)(ip - 2 - base);
\r
207 // Test next position
\r
208 ref = base + HashTable[LZ4_HASHVALUE(ip)];
\r
209 HashTable[LZ4_HASHVALUE(ip)] = (CURRENT_H_TYPE)(ip - base);
\r
210 if ((ref >= ip - MAX_DISTANCE) && (A32(ref) == A32(ip))) { token = op++; *token=0; goto _next_match; }
\r
212 // Prepare next loop
\r
214 forwardH = LZ4_HASHVALUE(ip);
\r
218 // Encode Last Literals
\r
220 int lastRun = (int)(iend - anchor);
\r
221 #ifdef LIMITED_OUTPUT
\r
222 if (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) return 0; // Check output limit
\r
224 if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun >= 255 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
\r
225 else *op++ = (BYTE)(lastRun<<ML_BITS);
\r
226 memcpy(op, anchor, iend - anchor);
\r
231 return (int) (((char*)op)-dest);
\r
236 //****************************
\r
238 //****************************
\r
240 // Required defines
\r
241 #undef FUNCTION_NAME
\r
243 // Locally Generated
\r
245 #undef HASHTABLE_NBCELLS
\r
247 #undef LZ4_HASHVALUE
\r
248 #undef CURRENT_H_TYPE
\r
251 // Optional defines
\r
252 #ifdef LIMITED_OUTPUT
\r
253 #undef LIMITED_OUTPUT
\r
256 #ifdef USE_HEAPMEMORY
\r
257 #undef USE_HEAPMEMORY
\r
260 #ifdef COMPRESS_64K
\r
261 #undef COMPRESS_64K
\r