1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17static void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd)
18{
19 BYTE* d = (BYTE*)dstPtr;
20 const BYTE* s = (const BYTE*)srcPtr;
21 BYTE* e = (BYTE*)dstEnd;
22 do { LZ4_copy8(d,s); d+=8; s+=8; } while (d<e);
23}
24
25
26
27
28
29#define MINMATCH 4
30
31#define COPYLENGTH 8
32#define LASTLITERALS 5
33#define MFLIMIT (COPYLENGTH+MINMATCH)
34static const int LZ4_minLength = (MFLIMIT+1);
35
36#define KB *(1 <<10)
37#define MB *(1 <<20)
38#define GB *(1U<<30)
39
40#define MAXD_LOG 16
41#define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
42
43#define ML_BITS 4
44#define ML_MASK ((1U<<ML_BITS)-1)
45#define RUN_BITS (8-ML_BITS)
46#define RUN_MASK ((1U<<RUN_BITS)-1)
47
48
49
50
51
52typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
53typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
54typedef enum { full = 0, partial = 1 } earlyEnd_directive;
55
56
57
58
59
60
61
62
63
64
65
66
67FORCE_INLINE int LZ4_decompress_generic(
68 const char* const source,
69 char* const dest,
70 int inputSize,
71 int outputSize,
72
73 int endOnInput,
74 int partialDecoding,
75 int targetOutputSize,
76 int dict,
77 const BYTE* const lowPrefix,
78 const BYTE* const dictStart,
79 const size_t dictSize
80 )
81{
82
83 const BYTE* ip = (const BYTE*) source;
84 const BYTE* const iend = ip + inputSize;
85
86 BYTE* op = (BYTE*) dest;
87 BYTE* const oend = op + outputSize;
88 BYTE* cpy;
89 BYTE* oexit = op + targetOutputSize;
90 const BYTE* const lowLimit = lowPrefix - dictSize;
91
92 const BYTE* const dictEnd = (const BYTE*)dictStart + dictSize;
93 const size_t dec32table[] = {4, 1, 2, 1, 4, 4, 4, 4};
94 const size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
95
96 const int safeDecode = (endOnInput==endOnInputSize);
97 const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB)));
98
99
100
101 if ((partialDecoding) && (oexit> oend-MFLIMIT)) oexit = oend-MFLIMIT;
102 if ((endOnInput) && (unlikely(outputSize==0))) return ((inputSize==1) && (*ip==0)) ? 0 : -1;
103 if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0?1:-1);
104
105
106
107 while (1)
108 {
109 unsigned token;
110 size_t length;
111 const BYTE* match;
112
113
114 token = *ip++;
115 if ((length=(token>>ML_BITS)) == RUN_MASK)
116 {
117 unsigned s;
118 do
119 {
120 s = *ip++;
121 length += s;
122 }
123 while (likely((endOnInput)?ip<iend-RUN_MASK:1) && (s==255));
124 if ((safeDecode) && unlikely((size_t)(op+length)<(size_t)(op))) goto _output_error;
125 if ((safeDecode) && unlikely((size_t)(ip+length)<(size_t)(ip))) goto _output_error;
126 }
127
128
129 cpy = op+length;
130 if (((endOnInput) && ((cpy>(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) )
131 || ((!endOnInput) && (cpy>oend-COPYLENGTH)))
132 {
133 if (partialDecoding)
134 {
135 if (cpy > oend) goto _output_error;
136 if ((endOnInput) && (ip+length > iend)) goto _output_error;
137 }
138 else
139 {
140 if ((!endOnInput) && (cpy != oend)) goto _output_error;
141 if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error;
142 }
143 memcpy(op, ip, length);
144 ip += length;
145 op += length;
146 break;
147 }
148 LZ4_wildCopy(op, ip, cpy);
149 ip += length; op = cpy;
150
151
152 match = cpy - LZ4_readLE16(ip); ip+=2;
153 if ((checkOffset) && (unlikely(match < lowLimit))) goto _output_error;
154
155
156 length = token & ML_MASK;
157 if (length == ML_MASK)
158 {
159 unsigned s;
160 do
161 {
162 if ((endOnInput) && (ip > iend-LASTLITERALS)) goto _output_error;
163 s = *ip++;
164 length += s;
165 } while (s==255);
166 if ((safeDecode) && unlikely((size_t)(op+length)<(size_t)op)) goto _output_error;
167 }
168 length += MINMATCH;
169
170
171 if ((dict==usingExtDict) && (match < lowPrefix))
172 {
173 if (unlikely(op+length > oend-LASTLITERALS)) goto _output_error;
174
175 if (length <= (size_t)(lowPrefix-match))
176 {
177
178 match = dictEnd - (lowPrefix-match);
179 memmove(op, match, length); op += length;
180 }
181 else
182 {
183
184 size_t copySize = (size_t)(lowPrefix-match);
185 memcpy(op, dictEnd - copySize, copySize);
186 op += copySize;
187 copySize = length - copySize;
188 if (copySize > (size_t)(op-lowPrefix))
189 {
190 BYTE* const endOfMatch = op + copySize;
191 const BYTE* copyFrom = lowPrefix;
192 while (op < endOfMatch) *op++ = *copyFrom++;
193 }
194 else
195 {
196 memcpy(op, lowPrefix, copySize);
197 op += copySize;
198 }
199 }
200 continue;
201 }
202
203
204 cpy = op + length;
205 if (unlikely((op-match)<8))
206 {
207 const size_t dec64 = dec64table[op-match];
208 op[0] = match[0];
209 op[1] = match[1];
210 op[2] = match[2];
211 op[3] = match[3];
212 match += dec32table[op-match];
213 LZ4_copy4(op+4, match);
214 op += 8; match -= dec64;
215 } else { LZ4_copy8(op, match); op+=8; match+=8; }
216
217 if (unlikely(cpy>oend-12))
218 {
219 if (cpy > oend-LASTLITERALS) goto _output_error;
220 if (op < oend-8)
221 {
222 LZ4_wildCopy(op, match, oend-8);
223 match += (oend-8) - op;
224 op = oend-8;
225 }
226 while (op<cpy) *op++ = *match++;
227 }
228 else
229 LZ4_wildCopy(op, match, cpy);
230 op=cpy;
231 }
232
233
234 if (endOnInput)
235 return (int) (((char*)op)-dest);
236 else
237 return (int) (((const char*)ip)-source);
238
239
240_output_error:
241 return (int) (-(((const char*)ip)-source))-1;
242}
243