Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: third_party/lzma_sdk/Lzma2Dec.c

Issue 1700453002: Update lzma_sdk sources to 15.14. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Chromium modifications Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/lzma_sdk/Lzma2Dec.h ('k') | third_party/lzma_sdk/LzmaDec.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Lzma2Dec.c -- LZMA2 Decoder 1 /* Lzma2Dec.c -- LZMA2 Decoder
2 2009-05-03 : Igor Pavlov : Public domain */ 2 2015-11-09 : Igor Pavlov : Public domain */
3 3
4 /* #define SHOW_DEBUG_INFO */ 4 /* #define SHOW_DEBUG_INFO */
5 5
6 #include "Precomp.h"
7
6 #ifdef SHOW_DEBUG_INFO 8 #ifdef SHOW_DEBUG_INFO
7 #include <stdio.h> 9 #include <stdio.h>
8 #endif 10 #endif
9 11
10 #include <string.h> 12 #include <string.h>
11 13
12 #include "Lzma2Dec.h" 14 #include "Lzma2Dec.h"
13 15
14 /* 16 /*
15 00000000 - EOS 17 00000000 - EOS
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 { 92 {
91 p->state = LZMA2_STATE_CONTROL; 93 p->state = LZMA2_STATE_CONTROL;
92 p->needInitDic = True; 94 p->needInitDic = True;
93 p->needInitState = True; 95 p->needInitState = True;
94 p->needInitProp = True; 96 p->needInitProp = True;
95 LzmaDec_Init(&p->decoder); 97 LzmaDec_Init(&p->decoder);
96 } 98 }
97 99
98 static ELzma2State Lzma2Dec_UpdateState(CLzma2Dec *p, Byte b) 100 static ELzma2State Lzma2Dec_UpdateState(CLzma2Dec *p, Byte b)
99 { 101 {
100 switch(p->state) 102 switch (p->state)
101 { 103 {
102 case LZMA2_STATE_CONTROL: 104 case LZMA2_STATE_CONTROL:
103 p->control = b; 105 p->control = b;
104 PRF(printf("\n %4X ", p->decoder.dicPos)); 106 PRF(printf("\n %4X ", (unsigned)p->decoder.dicPos));
105 PRF(printf(" %2X", b)); 107 PRF(printf(" %2X", (unsigned)b));
106 if (p->control == 0) 108 if (p->control == 0)
107 return LZMA2_STATE_FINISHED; 109 return LZMA2_STATE_FINISHED;
108 if (LZMA2_IS_UNCOMPRESSED_STATE(p)) 110 if (LZMA2_IS_UNCOMPRESSED_STATE(p))
109 { 111 {
110 if ((p->control & 0x7F) > 2) 112 if ((p->control & 0x7F) > 2)
111 return LZMA2_STATE_ERROR; 113 return LZMA2_STATE_ERROR;
112 p->unpackSize = 0; 114 p->unpackSize = 0;
113 } 115 }
114 else 116 else
115 p->unpackSize = (UInt32)(p->control & 0x1F) << 16; 117 p->unpackSize = (UInt32)(p->control & 0x1F) << 16;
116 return LZMA2_STATE_UNPACK0; 118 return LZMA2_STATE_UNPACK0;
117 119
118 case LZMA2_STATE_UNPACK0: 120 case LZMA2_STATE_UNPACK0:
119 p->unpackSize |= (UInt32)b << 8; 121 p->unpackSize |= (UInt32)b << 8;
120 return LZMA2_STATE_UNPACK1; 122 return LZMA2_STATE_UNPACK1;
121 123
122 case LZMA2_STATE_UNPACK1: 124 case LZMA2_STATE_UNPACK1:
123 p->unpackSize |= (UInt32)b; 125 p->unpackSize |= (UInt32)b;
124 p->unpackSize++; 126 p->unpackSize++;
125 PRF(printf(" %8d", p->unpackSize)); 127 PRF(printf(" %8u", (unsigned)p->unpackSize));
126 return (LZMA2_IS_UNCOMPRESSED_STATE(p)) ? LZMA2_STATE_DATA : LZMA2_STATE_P ACK0; 128 return (LZMA2_IS_UNCOMPRESSED_STATE(p)) ? LZMA2_STATE_DATA : LZMA2_STATE_P ACK0;
127 129
128 case LZMA2_STATE_PACK0: 130 case LZMA2_STATE_PACK0:
129 p->packSize = (UInt32)b << 8; 131 p->packSize = (UInt32)b << 8;
130 return LZMA2_STATE_PACK1; 132 return LZMA2_STATE_PACK1;
131 133
132 case LZMA2_STATE_PACK1: 134 case LZMA2_STATE_PACK1:
133 p->packSize |= (UInt32)b; 135 p->packSize |= (UInt32)b;
134 p->packSize++; 136 p->packSize++;
135 PRF(printf(" %8d", p->packSize)); 137 PRF(printf(" %8u", (unsigned)p->packSize));
136 return LZMA2_IS_THERE_PROP(LZMA2_GET_LZMA_MODE(p)) ? LZMA2_STATE_PROP: 138 return LZMA2_IS_THERE_PROP(LZMA2_GET_LZMA_MODE(p)) ? LZMA2_STATE_PROP:
137 (p->needInitProp ? LZMA2_STATE_ERROR : LZMA2_STATE_DATA); 139 (p->needInitProp ? LZMA2_STATE_ERROR : LZMA2_STATE_DATA);
138 140
139 case LZMA2_STATE_PROP: 141 case LZMA2_STATE_PROP:
140 { 142 {
141 int lc, lp; 143 unsigned lc, lp;
142 if (b >= (9 * 5 * 5)) 144 if (b >= (9 * 5 * 5))
143 return LZMA2_STATE_ERROR; 145 return LZMA2_STATE_ERROR;
144 lc = b % 9; 146 lc = b % 9;
145 b /= 9; 147 b /= 9;
146 p->decoder.prop.pb = b / 5; 148 p->decoder.prop.pb = b / 5;
147 lp = b % 5; 149 lp = b % 5;
148 if (lc + lp > LZMA2_LCLP_MAX) 150 if (lc + lp > LZMA2_LCLP_MAX)
149 return LZMA2_STATE_ERROR; 151 return LZMA2_STATE_ERROR;
150 p->decoder.prop.lc = lc; 152 p->decoder.prop.lc = lc;
151 p->decoder.prop.lp = lp; 153 p->decoder.prop.lp = lp;
(...skipping 18 matching lines...) Expand all
170 SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit, 172 SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit,
171 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *sta tus) 173 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *sta tus)
172 { 174 {
173 SizeT inSize = *srcLen; 175 SizeT inSize = *srcLen;
174 *srcLen = 0; 176 *srcLen = 0;
175 *status = LZMA_STATUS_NOT_SPECIFIED; 177 *status = LZMA_STATUS_NOT_SPECIFIED;
176 178
177 while (p->state != LZMA2_STATE_FINISHED) 179 while (p->state != LZMA2_STATE_FINISHED)
178 { 180 {
179 SizeT dicPos = p->decoder.dicPos; 181 SizeT dicPos = p->decoder.dicPos;
182
180 if (p->state == LZMA2_STATE_ERROR) 183 if (p->state == LZMA2_STATE_ERROR)
181 return SZ_ERROR_DATA; 184 return SZ_ERROR_DATA;
185
182 if (dicPos == dicLimit && finishMode == LZMA_FINISH_ANY) 186 if (dicPos == dicLimit && finishMode == LZMA_FINISH_ANY)
183 { 187 {
184 *status = LZMA_STATUS_NOT_FINISHED; 188 *status = LZMA_STATUS_NOT_FINISHED;
185 return SZ_OK; 189 return SZ_OK;
186 } 190 }
191
187 if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT) 192 if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT)
188 { 193 {
189 if (*srcLen == inSize) 194 if (*srcLen == inSize)
190 { 195 {
191 *status = LZMA_STATUS_NEEDS_MORE_INPUT; 196 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
192 return SZ_OK; 197 return SZ_OK;
193 } 198 }
194 (*srcLen)++; 199 (*srcLen)++;
195 p->state = Lzma2Dec_UpdateState(p, *src++); 200 p->state = Lzma2Dec_UpdateState(p, *src++);
201
202 if (dicPos == dicLimit && p->state != LZMA2_STATE_FINISHED)
203 {
204 p->state = LZMA2_STATE_ERROR;
205 return SZ_ERROR_DATA;
206 }
196 continue; 207 continue;
197 } 208 }
209
198 { 210 {
199 SizeT destSizeCur = dicLimit - dicPos; 211 SizeT destSizeCur = dicLimit - dicPos;
200 SizeT srcSizeCur = inSize - *srcLen; 212 SizeT srcSizeCur = inSize - *srcLen;
201 ELzmaFinishMode curFinishMode = LZMA_FINISH_ANY; 213 ELzmaFinishMode curFinishMode = LZMA_FINISH_ANY;
202 214
203 if (p->unpackSize <= destSizeCur) 215 if (p->unpackSize <= destSizeCur)
204 { 216 {
205 destSizeCur = (SizeT)p->unpackSize; 217 destSizeCur = (SizeT)p->unpackSize;
206 curFinishMode = LZMA_FINISH_END; 218 curFinishMode = LZMA_FINISH_END;
207 } 219 }
208 220
209 if (LZMA2_IS_UNCOMPRESSED_STATE(p)) 221 if (LZMA2_IS_UNCOMPRESSED_STATE(p))
210 { 222 {
211 if (*srcLen == inSize) 223 if (*srcLen == inSize)
212 { 224 {
213 *status = LZMA_STATUS_NEEDS_MORE_INPUT; 225 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
214 return SZ_OK; 226 return SZ_OK;
215 } 227 }
216 228
217 if (p->state == LZMA2_STATE_DATA) 229 if (p->state == LZMA2_STATE_DATA)
218 { 230 {
219 Bool initDic = (p->control == LZMA2_CONTROL_COPY_RESET_DIC); 231 Bool initDic = (p->control == LZMA2_CONTROL_COPY_RESET_DIC);
220 if (initDic) 232 if (initDic)
221 p->needInitProp = p->needInitState = True; 233 p->needInitProp = p->needInitState = True;
222 else if (p->needInitDic) 234 else if (p->needInitDic)
235 {
236 p->state = LZMA2_STATE_ERROR;
223 return SZ_ERROR_DATA; 237 return SZ_ERROR_DATA;
238 }
224 p->needInitDic = False; 239 p->needInitDic = False;
225 LzmaDec_InitDicAndState(&p->decoder, initDic, False); 240 LzmaDec_InitDicAndState(&p->decoder, initDic, False);
226 } 241 }
227 242
228 if (srcSizeCur > destSizeCur) 243 if (srcSizeCur > destSizeCur)
229 srcSizeCur = destSizeCur; 244 srcSizeCur = destSizeCur;
230 245
231 if (srcSizeCur == 0) 246 if (srcSizeCur == 0)
247 {
248 p->state = LZMA2_STATE_ERROR;
232 return SZ_ERROR_DATA; 249 return SZ_ERROR_DATA;
250 }
233 251
234 LzmaDec_UpdateWithUncompressed(&p->decoder, src, srcSizeCur); 252 LzmaDec_UpdateWithUncompressed(&p->decoder, src, srcSizeCur);
235 253
236 src += srcSizeCur; 254 src += srcSizeCur;
237 *srcLen += srcSizeCur; 255 *srcLen += srcSizeCur;
238 p->unpackSize -= (UInt32)srcSizeCur; 256 p->unpackSize -= (UInt32)srcSizeCur;
239 p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA _CONT; 257 p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA _CONT;
240 } 258 }
241 else 259 else
242 { 260 {
243 SizeT outSizeProcessed; 261 SizeT outSizeProcessed;
244 SRes res; 262 SRes res;
245 263
246 if (p->state == LZMA2_STATE_DATA) 264 if (p->state == LZMA2_STATE_DATA)
247 { 265 {
248 int mode = LZMA2_GET_LZMA_MODE(p); 266 unsigned mode = LZMA2_GET_LZMA_MODE(p);
249 Bool initDic = (mode == 3); 267 Bool initDic = (mode == 3);
250 Bool initState = (mode > 0); 268 Bool initState = (mode != 0);
251 if ((!initDic && p->needInitDic) || (!initState && p->needInitState)) 269 if ((!initDic && p->needInitDic) || (!initState && p->needInitState))
270 {
271 p->state = LZMA2_STATE_ERROR;
252 return SZ_ERROR_DATA; 272 return SZ_ERROR_DATA;
273 }
253 274
254 LzmaDec_InitDicAndState(&p->decoder, initDic, initState); 275 LzmaDec_InitDicAndState(&p->decoder, initDic, initState);
255 p->needInitDic = False; 276 p->needInitDic = False;
256 p->needInitState = False; 277 p->needInitState = False;
257 p->state = LZMA2_STATE_DATA_CONT; 278 p->state = LZMA2_STATE_DATA_CONT;
258 } 279 }
280
259 if (srcSizeCur > p->packSize) 281 if (srcSizeCur > p->packSize)
260 srcSizeCur = (SizeT)p->packSize; 282 srcSizeCur = (SizeT)p->packSize;
261 283
262 res = LzmaDec_DecodeToDic(&p->decoder, dicPos + destSizeCur, src, &srcSi zeCur, curFinishMode, status); 284 res = LzmaDec_DecodeToDic(&p->decoder, dicPos + destSizeCur, src, &srcSi zeCur, curFinishMode, status);
263 285
264 src += srcSizeCur; 286 src += srcSizeCur;
265 *srcLen += srcSizeCur; 287 *srcLen += srcSizeCur;
266 p->packSize -= (UInt32)srcSizeCur; 288 p->packSize -= (UInt32)srcSizeCur;
267 289
268 outSizeProcessed = p->decoder.dicPos - dicPos; 290 outSizeProcessed = p->decoder.dicPos - dicPos;
269 p->unpackSize -= (UInt32)outSizeProcessed; 291 p->unpackSize -= (UInt32)outSizeProcessed;
270 292
271 RINOK(res); 293 RINOK(res);
272 if (*status == LZMA_STATUS_NEEDS_MORE_INPUT) 294 if (*status == LZMA_STATUS_NEEDS_MORE_INPUT)
273 return res; 295 return res;
274 296
275 if (srcSizeCur == 0 && outSizeProcessed == 0) 297 if (srcSizeCur == 0 && outSizeProcessed == 0)
276 { 298 {
277 if (*status != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK || 299 if (*status != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
278 p->unpackSize != 0 || p->packSize != 0) 300 || p->unpackSize != 0
301 || p->packSize != 0)
302 {
303 p->state = LZMA2_STATE_ERROR;
279 return SZ_ERROR_DATA; 304 return SZ_ERROR_DATA;
305 }
280 p->state = LZMA2_STATE_CONTROL; 306 p->state = LZMA2_STATE_CONTROL;
281 } 307 }
308
282 if (*status == LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK) 309 if (*status == LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK)
283 *status = LZMA_STATUS_NOT_FINISHED; 310 *status = LZMA_STATUS_NOT_FINISHED;
284 } 311 }
285 } 312 }
286 } 313 }
314
287 *status = LZMA_STATUS_FINISHED_WITH_MARK; 315 *status = LZMA_STATUS_FINISHED_WITH_MARK;
288 return SZ_OK; 316 return SZ_OK;
289 } 317 }
290 318
291 SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, Byte *dest, SizeT *destLen, const Byte * src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status) 319 SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, Byte *dest, SizeT *destLen, const Byte * src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
292 { 320 {
293 SizeT outSize = *destLen, inSize = *srcLen; 321 SizeT outSize = *destLen, inSize = *srcLen;
294 *srcLen = *destLen = 0; 322 *srcLen = *destLen = 0;
295 for (;;) 323 for (;;)
296 { 324 {
(...skipping 26 matching lines...) Expand all
323 if (res != 0) 351 if (res != 0)
324 return res; 352 return res;
325 if (outSizeCur == 0 || outSize == 0) 353 if (outSizeCur == 0 || outSize == 0)
326 return SZ_OK; 354 return SZ_OK;
327 } 355 }
328 } 356 }
329 357
330 SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, 358 SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
331 Byte prop, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc) 359 Byte prop, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc)
332 { 360 {
333 CLzma2Dec decoder; 361 CLzma2Dec p;
334 SRes res; 362 SRes res;
335 SizeT outSize = *destLen, inSize = *srcLen; 363 SizeT outSize = *destLen, inSize = *srcLen;
336 Byte props[LZMA_PROPS_SIZE];
337
338 Lzma2Dec_Construct(&decoder);
339
340 *destLen = *srcLen = 0; 364 *destLen = *srcLen = 0;
341 *status = LZMA_STATUS_NOT_SPECIFIED; 365 *status = LZMA_STATUS_NOT_SPECIFIED;
342 decoder.decoder.dic = dest; 366 Lzma2Dec_Construct(&p);
343 decoder.decoder.dicBufSize = outSize; 367 RINOK(Lzma2Dec_AllocateProbs(&p, prop, alloc));
344 368 p.decoder.dic = dest;
345 RINOK(Lzma2Dec_GetOldProps(prop, props)); 369 p.decoder.dicBufSize = outSize;
346 RINOK(LzmaDec_AllocateProbs(&decoder.decoder, props, LZMA_PROPS_SIZE, alloc)); 370 Lzma2Dec_Init(&p);
347
348 *srcLen = inSize; 371 *srcLen = inSize;
349 res = Lzma2Dec_DecodeToDic(&decoder, outSize, src, srcLen, finishMode, status) ; 372 res = Lzma2Dec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
350 *destLen = decoder.decoder.dicPos; 373 *destLen = p.decoder.dicPos;
351 if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT) 374 if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
352 res = SZ_ERROR_INPUT_EOF; 375 res = SZ_ERROR_INPUT_EOF;
353 376 Lzma2Dec_FreeProbs(&p, alloc);
354 LzmaDec_FreeProbs(&decoder.decoder, alloc);
355 return res; 377 return res;
356 } 378 }
OLDNEW
« no previous file with comments | « third_party/lzma_sdk/Lzma2Dec.h ('k') | third_party/lzma_sdk/LzmaDec.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698