c6454fed57ce856c9f6594f3b71801ec4cde00d3
[openwrt/openwrt.git] / package / utils / mtd-utils / patches / 130-lzma_jffs2.patch
1 --- a/Makefile
2 +++ b/Makefile
3 @@ -3,7 +3,7 @@
4
5 VERSION = 1.5.2
6
7 -CPPFLAGS += -D_GNU_SOURCE -I./include -I$(BUILDDIR)/include -I./ubi-utils/include $(ZLIBCPPFLAGS) $(LZOCPPFLAGS) $(UUIDCPPFLAGS)
8 +CPPFLAGS += -D_GNU_SOURCE -I./include -I$(BUILDDIR)/include -I./ubi-utils/include $(ZLIBCPPFLAGS) $(LZOCPPFLAGS) $(UUIDCPPFLAGS) -I./include/linux/lzma
9
10 ifeq ($(WITHOUT_XATTR), 1)
11 CPPFLAGS += -DWITHOUT_XATTR
12 @@ -84,7 +84,7 @@ $(BUILDDIR)/include/version.h.tmp:
13 #
14 # Utils in top level
15 #
16 -obj-mkfs.jffs2 = compr_rtime.o compr_zlib.o compr_lzo.o compr.o rbtree.o
17 +obj-mkfs.jffs2 = compr_rtime.o compr_zlib.o $(if $(WITHOUT_LZO),,compr_lzo.o) compr_lzma.o lzma/LzFind.o lzma/LzmaEnc.o lzma/LzmaDec.o compr.o rbtree.o
18 LDFLAGS_mkfs.jffs2 = $(ZLIBLDFLAGS) $(LZOLDFLAGS)
19 LDLIBS_mkfs.jffs2 = -lz $(LZOLDLIBS)
20
21 --- a/compr.c
22 +++ b/compr.c
23 @@ -520,6 +520,9 @@ int jffs2_compressors_init(void)
24 #ifdef CONFIG_JFFS2_LZO
25 jffs2_lzo_init();
26 #endif
27 +#ifdef CONFIG_JFFS2_LZMA
28 + jffs2_lzma_init();
29 +#endif
30 return 0;
31 }
32
33 @@ -534,5 +537,8 @@ int jffs2_compressors_exit(void)
34 #ifdef CONFIG_JFFS2_LZO
35 jffs2_lzo_exit();
36 #endif
37 +#ifdef CONFIG_JFFS2_LZMA
38 + jffs2_lzma_exit();
39 +#endif
40 return 0;
41 }
42 --- a/compr.h
43 +++ b/compr.h
44 @@ -18,13 +18,14 @@
45
46 #define CONFIG_JFFS2_ZLIB
47 #define CONFIG_JFFS2_RTIME
48 -#define CONFIG_JFFS2_LZO
49 +#define CONFIG_JFFS2_LZMA
50
51 #define JFFS2_RUBINMIPS_PRIORITY 10
52 #define JFFS2_DYNRUBIN_PRIORITY 20
53 #define JFFS2_RTIME_PRIORITY 50
54 -#define JFFS2_ZLIB_PRIORITY 60
55 -#define JFFS2_LZO_PRIORITY 80
56 +#define JFFS2_LZMA_PRIORITY 70
57 +#define JFFS2_ZLIB_PRIORITY 80
58 +#define JFFS2_LZO_PRIORITY 90
59
60 #define JFFS2_COMPR_MODE_NONE 0
61 #define JFFS2_COMPR_MODE_PRIORITY 1
62 @@ -115,5 +116,10 @@ void jffs2_rtime_exit(void);
63 int jffs2_lzo_init(void);
64 void jffs2_lzo_exit(void);
65 #endif
66 +#ifdef CONFIG_JFFS2_LZMA
67 +int jffs2_lzma_init(void);
68 +void jffs2_lzma_exit(void);
69 +#endif
70 +
71
72 #endif /* __JFFS2_COMPR_H__ */
73 --- /dev/null
74 +++ b/compr_lzma.c
75 @@ -0,0 +1,128 @@
76 +/*
77 + * JFFS2 -- Journalling Flash File System, Version 2.
78 + *
79 + * For licensing information, see the file 'LICENCE' in this directory.
80 + *
81 + * JFFS2 wrapper to the LZMA C SDK
82 + *
83 + */
84 +
85 +#include <linux/lzma.h>
86 +#include "compr.h"
87 +
88 +#ifdef __KERNEL__
89 + static DEFINE_MUTEX(deflate_mutex);
90 +#endif
91 +
92 +CLzmaEncHandle *p;
93 +Byte propsEncoded[LZMA_PROPS_SIZE];
94 +SizeT propsSize = sizeof(propsEncoded);
95 +
96 +STATIC void lzma_free_workspace(void)
97 +{
98 + LzmaEnc_Destroy(p, &lzma_alloc, &lzma_alloc);
99 +}
100 +
101 +STATIC int INIT lzma_alloc_workspace(CLzmaEncProps *props)
102 +{
103 + if ((p = (CLzmaEncHandle *)LzmaEnc_Create(&lzma_alloc)) == NULL)
104 + {
105 + PRINT_ERROR("Failed to allocate lzma deflate workspace\n");
106 + return -ENOMEM;
107 + }
108 +
109 + if (LzmaEnc_SetProps(p, props) != SZ_OK)
110 + {
111 + lzma_free_workspace();
112 + return -1;
113 + }
114 +
115 + if (LzmaEnc_WriteProperties(p, propsEncoded, &propsSize) != SZ_OK)
116 + {
117 + lzma_free_workspace();
118 + return -1;
119 + }
120 +
121 + return 0;
122 +}
123 +
124 +STATIC int jffs2_lzma_compress(unsigned char *data_in, unsigned char *cpage_out,
125 + uint32_t *sourcelen, uint32_t *dstlen)
126 +{
127 + SizeT compress_size = (SizeT)(*dstlen);
128 + int ret;
129 +
130 + #ifdef __KERNEL__
131 + mutex_lock(&deflate_mutex);
132 + #endif
133 +
134 + ret = LzmaEnc_MemEncode(p, cpage_out, &compress_size, data_in, *sourcelen,
135 + 0, NULL, &lzma_alloc, &lzma_alloc);
136 +
137 + #ifdef __KERNEL__
138 + mutex_unlock(&deflate_mutex);
139 + #endif
140 +
141 + if (ret != SZ_OK)
142 + return -1;
143 +
144 + *dstlen = (uint32_t)compress_size;
145 +
146 + return 0;
147 +}
148 +
149 +STATIC int jffs2_lzma_decompress(unsigned char *data_in, unsigned char *cpage_out,
150 + uint32_t srclen, uint32_t destlen)
151 +{
152 + int ret;
153 + SizeT dl = (SizeT)destlen;
154 + SizeT sl = (SizeT)srclen;
155 + ELzmaStatus status;
156 +
157 + ret = LzmaDecode(cpage_out, &dl, data_in, &sl, propsEncoded,
158 + propsSize, LZMA_FINISH_ANY, &status, &lzma_alloc);
159 +
160 + if (ret != SZ_OK || status == LZMA_STATUS_NOT_FINISHED || dl != (SizeT)destlen)
161 + return -1;
162 +
163 + return 0;
164 +}
165 +
166 +static struct jffs2_compressor jffs2_lzma_comp = {
167 + .priority = JFFS2_LZMA_PRIORITY,
168 + .name = "lzma",
169 + .compr = JFFS2_COMPR_LZMA,
170 + .compress = &jffs2_lzma_compress,
171 + .decompress = &jffs2_lzma_decompress,
172 + .disabled = 0,
173 +};
174 +
175 +int INIT jffs2_lzma_init(void)
176 +{
177 + int ret;
178 + CLzmaEncProps props;
179 + LzmaEncProps_Init(&props);
180 +
181 + props.dictSize = LZMA_BEST_DICT(0x2000);
182 + props.level = LZMA_BEST_LEVEL;
183 + props.lc = LZMA_BEST_LC;
184 + props.lp = LZMA_BEST_LP;
185 + props.pb = LZMA_BEST_PB;
186 + props.fb = LZMA_BEST_FB;
187 +
188 + ret = lzma_alloc_workspace(&props);
189 + if (ret < 0)
190 + return ret;
191 +
192 + ret = jffs2_register_compressor(&jffs2_lzma_comp);
193 + if (ret)
194 + lzma_free_workspace();
195 +
196 + return ret;
197 +}
198 +
199 +void jffs2_lzma_exit(void)
200 +{
201 + jffs2_unregister_compressor(&jffs2_lzma_comp);
202 + lzma_free_workspace();
203 +}
204 --- a/include/linux/jffs2.h
205 +++ b/include/linux/jffs2.h
206 @@ -47,6 +47,7 @@
207 #define JFFS2_COMPR_DYNRUBIN 0x05
208 #define JFFS2_COMPR_ZLIB 0x06
209 #define JFFS2_COMPR_LZO 0x07
210 +#define JFFS2_COMPR_LZMA 0x08
211 /* Compatibility flags. */
212 #define JFFS2_COMPAT_MASK 0xc000 /* What do to if an unknown nodetype is found */
213 #define JFFS2_NODE_ACCURATE 0x2000
214 --- /dev/null
215 +++ b/include/linux/lzma.h
216 @@ -0,0 +1,61 @@
217 +#ifndef __LZMA_H__
218 +#define __LZMA_H__
219 +
220 +#ifdef __KERNEL__
221 + #include <linux/kernel.h>
222 + #include <linux/sched.h>
223 + #include <linux/slab.h>
224 + #include <linux/vmalloc.h>
225 + #include <linux/init.h>
226 + #define LZMA_MALLOC vmalloc
227 + #define LZMA_FREE vfree
228 + #define PRINT_ERROR(msg) printk(KERN_WARNING #msg)
229 + #define INIT __init
230 + #define STATIC static
231 +#else
232 + #include <stdint.h>
233 + #include <stdlib.h>
234 + #include <stdio.h>
235 + #include <unistd.h>
236 + #include <string.h>
237 + #include <errno.h>
238 + #include <linux/jffs2.h>
239 + #ifndef PAGE_SIZE
240 + extern int page_size;
241 + #define PAGE_SIZE page_size
242 + #endif
243 + #define LZMA_MALLOC malloc
244 + #define LZMA_FREE free
245 + #define PRINT_ERROR(msg) fprintf(stderr, msg)
246 + #define INIT
247 + #define STATIC static
248 +#endif
249 +
250 +#include "lzma/LzmaDec.h"
251 +#include "lzma/LzmaEnc.h"
252 +
253 +#define LZMA_BEST_LEVEL (9)
254 +#define LZMA_BEST_LC (0)
255 +#define LZMA_BEST_LP (0)
256 +#define LZMA_BEST_PB (0)
257 +#define LZMA_BEST_FB (273)
258 +
259 +#define LZMA_BEST_DICT(n) (((int)((n) / 2)) * 2)
260 +
261 +static void *p_lzma_malloc(void *p, size_t size)
262 +{
263 + if (size == 0)
264 + return NULL;
265 +
266 + return LZMA_MALLOC(size);
267 +}
268 +
269 +static void p_lzma_free(void *p, void *address)
270 +{
271 + if (address != NULL)
272 + LZMA_FREE(address);
273 +}
274 +
275 +static ISzAlloc lzma_alloc = {p_lzma_malloc, p_lzma_free};
276 +
277 +#endif
278 --- /dev/null
279 +++ b/include/linux/lzma/LzFind.h
280 @@ -0,0 +1,116 @@
281 +/* LzFind.h -- Match finder for LZ algorithms
282 +2008-04-04
283 +Copyright (c) 1999-2008 Igor Pavlov
284 +You can use any of the following license options:
285 + 1) GNU Lesser General Public License (GNU LGPL)
286 + 2) Common Public License (CPL)
287 + 3) Common Development and Distribution License (CDDL) Version 1.0
288 + 4) Igor Pavlov, as the author of this code, expressly permits you to
289 + statically or dynamically link your code (or bind by name) to this file,
290 + while you keep this file unmodified.
291 +*/
292 +
293 +#ifndef __LZFIND_H
294 +#define __LZFIND_H
295 +
296 +#include "Types.h"
297 +
298 +typedef UInt32 CLzRef;
299 +
300 +typedef struct _CMatchFinder
301 +{
302 + Byte *buffer;
303 + UInt32 pos;
304 + UInt32 posLimit;
305 + UInt32 streamPos;
306 + UInt32 lenLimit;
307 +
308 + UInt32 cyclicBufferPos;
309 + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */
310 +
311 + UInt32 matchMaxLen;
312 + CLzRef *hash;
313 + CLzRef *son;
314 + UInt32 hashMask;
315 + UInt32 cutValue;
316 +
317 + Byte *bufferBase;
318 + ISeqInStream *stream;
319 + int streamEndWasReached;
320 +
321 + UInt32 blockSize;
322 + UInt32 keepSizeBefore;
323 + UInt32 keepSizeAfter;
324 +
325 + UInt32 numHashBytes;
326 + int directInput;
327 + int btMode;
328 + /* int skipModeBits; */
329 + int bigHash;
330 + UInt32 historySize;
331 + UInt32 fixedHashSize;
332 + UInt32 hashSizeSum;
333 + UInt32 numSons;
334 + SRes result;
335 + UInt32 crc[256];
336 +} CMatchFinder;
337 +
338 +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer)
339 +#define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)])
340 +
341 +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos)
342 +
343 +int MatchFinder_NeedMove(CMatchFinder *p);
344 +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p);
345 +void MatchFinder_MoveBlock(CMatchFinder *p);
346 +void MatchFinder_ReadIfRequired(CMatchFinder *p);
347 +
348 +void MatchFinder_Construct(CMatchFinder *p);
349 +
350 +/* Conditions:
351 + historySize <= 3 GB
352 + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB
353 +*/
354 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
355 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
356 + ISzAlloc *alloc);
357 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc);
358 +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems);
359 +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue);
360 +
361 +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son,
362 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue,
363 + UInt32 *distances, UInt32 maxLen);
364 +
365 +/*
366 +Conditions:
367 + Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func.
368 + Mf_GetPointerToCurrentPos_Func's result must be used only before any other function
369 +*/
370 +
371 +typedef void (*Mf_Init_Func)(void *object);
372 +typedef Byte (*Mf_GetIndexByte_Func)(void *object, Int32 index);
373 +typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object);
374 +typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object);
375 +typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances);
376 +typedef void (*Mf_Skip_Func)(void *object, UInt32);
377 +
378 +typedef struct _IMatchFinder
379 +{
380 + Mf_Init_Func Init;
381 + Mf_GetIndexByte_Func GetIndexByte;
382 + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes;
383 + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos;
384 + Mf_GetMatches_Func GetMatches;
385 + Mf_Skip_Func Skip;
386 +} IMatchFinder;
387 +
388 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable);
389 +
390 +void MatchFinder_Init(CMatchFinder *p);
391 +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances);
392 +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances);
393 +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num);
394 +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num);
395 +
396 +#endif
397 --- /dev/null
398 +++ b/include/linux/lzma/LzHash.h
399 @@ -0,0 +1,56 @@
400 +/* LzHash.h -- HASH functions for LZ algorithms
401 +2008-03-26
402 +Copyright (c) 1999-2008 Igor Pavlov
403 +Read LzFind.h for license options */
404 +
405 +#ifndef __LZHASH_H
406 +#define __LZHASH_H
407 +
408 +#define kHash2Size (1 << 10)
409 +#define kHash3Size (1 << 16)
410 +#define kHash4Size (1 << 20)
411 +
412 +#define kFix3HashSize (kHash2Size)
413 +#define kFix4HashSize (kHash2Size + kHash3Size)
414 +#define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size)
415 +
416 +#define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8);
417 +
418 +#define HASH3_CALC { \
419 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
420 + hash2Value = temp & (kHash2Size - 1); \
421 + hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; }
422 +
423 +#define HASH4_CALC { \
424 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
425 + hash2Value = temp & (kHash2Size - 1); \
426 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
427 + hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; }
428 +
429 +#define HASH5_CALC { \
430 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
431 + hash2Value = temp & (kHash2Size - 1); \
432 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
433 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \
434 + hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \
435 + hash4Value &= (kHash4Size - 1); }
436 +
437 +/* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */
438 +#define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF;
439 +
440 +
441 +#define MT_HASH2_CALC \
442 + hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1);
443 +
444 +#define MT_HASH3_CALC { \
445 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
446 + hash2Value = temp & (kHash2Size - 1); \
447 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); }
448 +
449 +#define MT_HASH4_CALC { \
450 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
451 + hash2Value = temp & (kHash2Size - 1); \
452 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
453 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); }
454 +
455 +#endif
456 --- /dev/null
457 +++ b/include/linux/lzma/LzmaDec.h
458 @@ -0,0 +1,232 @@
459 +/* LzmaDec.h -- LZMA Decoder
460 +2008-04-29
461 +Copyright (c) 1999-2008 Igor Pavlov
462 +You can use any of the following license options:
463 + 1) GNU Lesser General Public License (GNU LGPL)
464 + 2) Common Public License (CPL)
465 + 3) Common Development and Distribution License (CDDL) Version 1.0
466 + 4) Igor Pavlov, as the author of this code, expressly permits you to
467 + statically or dynamically link your code (or bind by name) to this file,
468 + while you keep this file unmodified.
469 +*/
470 +
471 +#ifndef __LZMADEC_H
472 +#define __LZMADEC_H
473 +
474 +#include "Types.h"
475 +
476 +/* #define _LZMA_PROB32 */
477 +/* _LZMA_PROB32 can increase the speed on some CPUs,
478 + but memory usage for CLzmaDec::probs will be doubled in that case */
479 +
480 +#ifdef _LZMA_PROB32
481 +#define CLzmaProb UInt32
482 +#else
483 +#define CLzmaProb UInt16
484 +#endif
485 +
486 +
487 +/* ---------- LZMA Properties ---------- */
488 +
489 +#define LZMA_PROPS_SIZE 5
490 +
491 +typedef struct _CLzmaProps
492 +{
493 + unsigned lc, lp, pb;
494 + UInt32 dicSize;
495 +} CLzmaProps;
496 +
497 +/* LzmaProps_Decode - decodes properties
498 +Returns:
499 + SZ_OK
500 + SZ_ERROR_UNSUPPORTED - Unsupported properties
501 +*/
502 +
503 +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
504 +
505 +
506 +/* ---------- LZMA Decoder state ---------- */
507 +
508 +/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
509 + Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
510 +
511 +#define LZMA_REQUIRED_INPUT_MAX 20
512 +
513 +typedef struct
514 +{
515 + CLzmaProps prop;
516 + CLzmaProb *probs;
517 + Byte *dic;
518 + const Byte *buf;
519 + UInt32 range, code;
520 + SizeT dicPos;
521 + SizeT dicBufSize;
522 + UInt32 processedPos;
523 + UInt32 checkDicSize;
524 + unsigned state;
525 + UInt32 reps[4];
526 + unsigned remainLen;
527 + int needFlush;
528 + int needInitState;
529 + UInt32 numProbs;
530 + unsigned tempBufSize;
531 + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
532 +} CLzmaDec;
533 +
534 +#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
535 +
536 +void LzmaDec_Init(CLzmaDec *p);
537 +
538 +/* There are two types of LZMA streams:
539 + 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
540 + 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
541 +
542 +typedef enum
543 +{
544 + LZMA_FINISH_ANY, /* finish at any point */
545 + LZMA_FINISH_END /* block must be finished at the end */
546 +} ELzmaFinishMode;
547 +
548 +/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
549 +
550 + You must use LZMA_FINISH_END, when you know that current output buffer
551 + covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
552 +
553 + If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
554 + and output value of destLen will be less than output buffer size limit.
555 + You can check status result also.
556 +
557 + You can use multiple checks to test data integrity after full decompression:
558 + 1) Check Result and "status" variable.
559 + 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
560 + 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
561 + You must use correct finish mode in that case. */
562 +
563 +typedef enum
564 +{
565 + LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
566 + LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
567 + LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
568 + LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
569 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
570 +} ELzmaStatus;
571 +
572 +/* ELzmaStatus is used only as output value for function call */
573 +
574 +
575 +/* ---------- Interfaces ---------- */
576 +
577 +/* There are 3 levels of interfaces:
578 + 1) Dictionary Interface
579 + 2) Buffer Interface
580 + 3) One Call Interface
581 + You can select any of these interfaces, but don't mix functions from different
582 + groups for same object. */
583 +
584 +
585 +/* There are two variants to allocate state for Dictionary Interface:
586 + 1) LzmaDec_Allocate / LzmaDec_Free
587 + 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
588 + You can use variant 2, if you set dictionary buffer manually.
589 + For Buffer Interface you must always use variant 1.
590 +
591 +LzmaDec_Allocate* can return:
592 + SZ_OK
593 + SZ_ERROR_MEM - Memory allocation error
594 + SZ_ERROR_UNSUPPORTED - Unsupported properties
595 +*/
596 +
597 +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
598 +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
599 +
600 +SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc);
601 +void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc);
602 +
603 +/* ---------- Dictionary Interface ---------- */
604 +
605 +/* You can use it, if you want to eliminate the overhead for data copying from
606 + dictionary to some other external buffer.
607 + You must work with CLzmaDec variables directly in this interface.
608 +
609 + STEPS:
610 + LzmaDec_Constr()
611 + LzmaDec_Allocate()
612 + for (each new stream)
613 + {
614 + LzmaDec_Init()
615 + while (it needs more decompression)
616 + {
617 + LzmaDec_DecodeToDic()
618 + use data from CLzmaDec::dic and update CLzmaDec::dicPos
619 + }
620 + }
621 + LzmaDec_Free()
622 +*/
623 +
624 +/* LzmaDec_DecodeToDic
625 +
626 + The decoding to internal dictionary buffer (CLzmaDec::dic).
627 + You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
628 +
629 +finishMode:
630 + It has meaning only if the decoding reaches output limit (dicLimit).
631 + LZMA_FINISH_ANY - Decode just dicLimit bytes.
632 + LZMA_FINISH_END - Stream must be finished after dicLimit.
633 +
634 +Returns:
635 + SZ_OK
636 + status:
637 + LZMA_STATUS_FINISHED_WITH_MARK
638 + LZMA_STATUS_NOT_FINISHED
639 + LZMA_STATUS_NEEDS_MORE_INPUT
640 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
641 + SZ_ERROR_DATA - Data error
642 +*/
643 +
644 +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
645 + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
646 +
647 +
648 +/* ---------- Buffer Interface ---------- */
649 +
650 +/* It's zlib-like interface.
651 + See LzmaDec_DecodeToDic description for information about STEPS and return results,
652 + but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
653 + to work with CLzmaDec variables manually.
654 +
655 +finishMode:
656 + It has meaning only if the decoding reaches output limit (*destLen).
657 + LZMA_FINISH_ANY - Decode just destLen bytes.
658 + LZMA_FINISH_END - Stream must be finished after (*destLen).
659 +*/
660 +
661 +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
662 + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
663 +
664 +
665 +/* ---------- One Call Interface ---------- */
666 +
667 +/* LzmaDecode
668 +
669 +finishMode:
670 + It has meaning only if the decoding reaches output limit (*destLen).
671 + LZMA_FINISH_ANY - Decode just destLen bytes.
672 + LZMA_FINISH_END - Stream must be finished after (*destLen).
673 +
674 +Returns:
675 + SZ_OK
676 + status:
677 + LZMA_STATUS_FINISHED_WITH_MARK
678 + LZMA_STATUS_NOT_FINISHED
679 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
680 + SZ_ERROR_DATA - Data error
681 + SZ_ERROR_MEM - Memory allocation error
682 + SZ_ERROR_UNSUPPORTED - Unsupported properties
683 + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
684 +*/
685 +
686 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
687 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
688 + ELzmaStatus *status, ISzAlloc *alloc);
689 +
690 +#endif
691 --- /dev/null
692 +++ b/include/linux/lzma/LzmaEnc.h
693 @@ -0,0 +1,74 @@
694 +/* LzmaEnc.h -- LZMA Encoder
695 +2008-04-27
696 +Copyright (c) 1999-2008 Igor Pavlov
697 +Read LzFind.h for license options */
698 +
699 +#ifndef __LZMAENC_H
700 +#define __LZMAENC_H
701 +
702 +#include "Types.h"
703 +
704 +#define LZMA_PROPS_SIZE 5
705 +
706 +typedef struct _CLzmaEncProps
707 +{
708 + int level; /* 0 <= level <= 9 */
709 + UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
710 + (1 << 12) <= dictSize <= (1 << 30) for 64-bit version
711 + default = (1 << 24) */
712 + int lc; /* 0 <= lc <= 8, default = 3 */
713 + int lp; /* 0 <= lp <= 4, default = 0 */
714 + int pb; /* 0 <= pb <= 4, default = 2 */
715 + int algo; /* 0 - fast, 1 - normal, default = 1 */
716 + int fb; /* 5 <= fb <= 273, default = 32 */
717 + int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
718 + int numHashBytes; /* 2, 3 or 4, default = 4 */
719 + UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */
720 + unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
721 + int numThreads; /* 1 or 2, default = 2 */
722 +} CLzmaEncProps;
723 +
724 +void LzmaEncProps_Init(CLzmaEncProps *p);
725 +void LzmaEncProps_Normalize(CLzmaEncProps *p);
726 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2);
727 +
728 +
729 +/* ---------- CLzmaEncHandle Interface ---------- */
730 +
731 +/* LzmaEnc_* functions can return the following exit codes:
732 +Returns:
733 + SZ_OK - OK
734 + SZ_ERROR_MEM - Memory allocation error
735 + SZ_ERROR_PARAM - Incorrect paramater in props
736 + SZ_ERROR_WRITE - Write callback error.
737 + SZ_ERROR_PROGRESS - some break from progress callback
738 + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
739 +*/
740 +
741 +typedef void * CLzmaEncHandle;
742 +
743 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc);
744 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig);
745 +SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props);
746 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size);
747 +SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream,
748 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
749 +SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
750 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
751 +
752 +/* ---------- One Call Interface ---------- */
753 +
754 +/* LzmaEncode
755 +Return code:
756 + SZ_OK - OK
757 + SZ_ERROR_MEM - Memory allocation error
758 + SZ_ERROR_PARAM - Incorrect paramater
759 + SZ_ERROR_OUTPUT_EOF - output buffer overflow
760 + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
761 +*/
762 +
763 +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
764 + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
765 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
766 +
767 +#endif
768 --- /dev/null
769 +++ b/include/linux/lzma/Types.h
770 @@ -0,0 +1,130 @@
771 +/* Types.h -- Basic types
772 +2008-04-11
773 +Igor Pavlov
774 +Public domain */
775 +
776 +#ifndef __7Z_TYPES_H
777 +#define __7Z_TYPES_H
778 +
779 +#define SZ_OK 0
780 +
781 +#define SZ_ERROR_DATA 1
782 +#define SZ_ERROR_MEM 2
783 +#define SZ_ERROR_CRC 3
784 +#define SZ_ERROR_UNSUPPORTED 4
785 +#define SZ_ERROR_PARAM 5
786 +#define SZ_ERROR_INPUT_EOF 6
787 +#define SZ_ERROR_OUTPUT_EOF 7
788 +#define SZ_ERROR_READ 8
789 +#define SZ_ERROR_WRITE 9
790 +#define SZ_ERROR_PROGRESS 10
791 +#define SZ_ERROR_FAIL 11
792 +#define SZ_ERROR_THREAD 12
793 +
794 +#define SZ_ERROR_ARCHIVE 16
795 +#define SZ_ERROR_NO_ARCHIVE 17
796 +
797 +typedef int SRes;
798 +
799 +#ifndef RINOK
800 +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
801 +#endif
802 +
803 +typedef unsigned char Byte;
804 +typedef short Int16;
805 +typedef unsigned short UInt16;
806 +
807 +#ifdef _LZMA_UINT32_IS_ULONG
808 +typedef long Int32;
809 +typedef unsigned long UInt32;
810 +#else
811 +typedef int Int32;
812 +typedef unsigned int UInt32;
813 +#endif
814 +
815 +/* #define _SZ_NO_INT_64 */
816 +/* define it if your compiler doesn't support 64-bit integers */
817 +
818 +#ifdef _SZ_NO_INT_64
819 +
820 +typedef long Int64;
821 +typedef unsigned long UInt64;
822 +
823 +#else
824 +
825 +#if defined(_MSC_VER) || defined(__BORLANDC__)
826 +typedef __int64 Int64;
827 +typedef unsigned __int64 UInt64;
828 +#else
829 +typedef long long int Int64;
830 +typedef unsigned long long int UInt64;
831 +#endif
832 +
833 +#endif
834 +
835 +#ifdef _LZMA_NO_SYSTEM_SIZE_T
836 +typedef UInt32 SizeT;
837 +#else
838 +#include <stddef.h>
839 +typedef size_t SizeT;
840 +#endif
841 +
842 +typedef int Bool;
843 +#define True 1
844 +#define False 0
845 +
846 +
847 +#ifdef _MSC_VER
848 +
849 +#if _MSC_VER >= 1300
850 +#define MY_NO_INLINE __declspec(noinline)
851 +#else
852 +#define MY_NO_INLINE
853 +#endif
854 +
855 +#define MY_CDECL __cdecl
856 +#define MY_STD_CALL __stdcall
857 +#define MY_FAST_CALL MY_NO_INLINE __fastcall
858 +
859 +#else
860 +
861 +#define MY_CDECL
862 +#define MY_STD_CALL
863 +#define MY_FAST_CALL
864 +
865 +#endif
866 +
867 +
868 +/* The following interfaces use first parameter as pointer to structure */
869 +
870 +typedef struct
871 +{
872 + SRes (*Read)(void *p, void *buf, size_t *size);
873 + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
874 + (output(*size) < input(*size)) is allowed */
875 +} ISeqInStream;
876 +
877 +typedef struct
878 +{
879 + size_t (*Write)(void *p, const void *buf, size_t size);
880 + /* Returns: result - the number of actually written bytes.
881 + (result < size) means error */
882 +} ISeqOutStream;
883 +
884 +typedef struct
885 +{
886 + SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize);
887 + /* Returns: result. (result != SZ_OK) means break.
888 + Value (UInt64)(Int64)-1 for size means unknown value. */
889 +} ICompressProgress;
890 +
891 +typedef struct
892 +{
893 + void *(*Alloc)(void *p, size_t size);
894 + void (*Free)(void *p, void *address); /* address can be 0 */
895 +} ISzAlloc;
896 +
897 +#define IAlloc_Alloc(p, size) (p)->Alloc((p), size)
898 +#define IAlloc_Free(p, a) (p)->Free((p), a)
899 +
900 +#endif
901 --- /dev/null
902 +++ b/lzma/LzFind.c
903 @@ -0,0 +1,753 @@
904 +/* LzFind.c -- Match finder for LZ algorithms
905 +2008-04-04
906 +Copyright (c) 1999-2008 Igor Pavlov
907 +Read LzFind.h for license options */
908 +
909 +#include <string.h>
910 +
911 +#include "LzFind.h"
912 +#include "LzHash.h"
913 +
914 +#define kEmptyHashValue 0
915 +#define kMaxValForNormalize ((UInt32)0xFFFFFFFF)
916 +#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */
917 +#define kNormalizeMask (~(kNormalizeStepMin - 1))
918 +#define kMaxHistorySize ((UInt32)3 << 30)
919 +
920 +#define kStartMaxLen 3
921 +
922 +static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
923 +{
924 + if (!p->directInput)
925 + {
926 + alloc->Free(alloc, p->bufferBase);
927 + p->bufferBase = 0;
928 + }
929 +}
930 +
931 +/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
932 +
933 +static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
934 +{
935 + UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
936 + if (p->directInput)
937 + {
938 + p->blockSize = blockSize;
939 + return 1;
940 + }
941 + if (p->bufferBase == 0 || p->blockSize != blockSize)
942 + {
943 + LzInWindow_Free(p, alloc);
944 + p->blockSize = blockSize;
945 + p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
946 + }
947 + return (p->bufferBase != 0);
948 +}
949 +
950 +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
951 +static Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
952 +
953 +static UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
954 +
955 +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
956 +{
957 + p->posLimit -= subValue;
958 + p->pos -= subValue;
959 + p->streamPos -= subValue;
960 +}
961 +
962 +static void MatchFinder_ReadBlock(CMatchFinder *p)
963 +{
964 + if (p->streamEndWasReached || p->result != SZ_OK)
965 + return;
966 + for (;;)
967 + {
968 + Byte *dest = p->buffer + (p->streamPos - p->pos);
969 + size_t size = (p->bufferBase + p->blockSize - dest);
970 + if (size == 0)
971 + return;
972 + p->result = p->stream->Read(p->stream, dest, &size);
973 + if (p->result != SZ_OK)
974 + return;
975 + if (size == 0)
976 + {
977 + p->streamEndWasReached = 1;
978 + return;
979 + }
980 + p->streamPos += (UInt32)size;
981 + if (p->streamPos - p->pos > p->keepSizeAfter)
982 + return;
983 + }
984 +}
985 +
986 +void MatchFinder_MoveBlock(CMatchFinder *p)
987 +{
988 + memmove(p->bufferBase,
989 + p->buffer - p->keepSizeBefore,
990 + (size_t)(p->streamPos - p->pos + p->keepSizeBefore));
991 + p->buffer = p->bufferBase + p->keepSizeBefore;
992 +}
993 +
994 +int MatchFinder_NeedMove(CMatchFinder *p)
995 +{
996 + /* if (p->streamEndWasReached) return 0; */
997 + return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
998 +}
999 +
1000 +void MatchFinder_ReadIfRequired(CMatchFinder *p)
1001 +{
1002 + if (p->streamEndWasReached)
1003 + return;
1004 + if (p->keepSizeAfter >= p->streamPos - p->pos)
1005 + MatchFinder_ReadBlock(p);
1006 +}
1007 +
1008 +static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
1009 +{
1010 + if (MatchFinder_NeedMove(p))
1011 + MatchFinder_MoveBlock(p);
1012 + MatchFinder_ReadBlock(p);
1013 +}
1014 +
1015 +static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
1016 +{
1017 + p->cutValue = 32;
1018 + p->btMode = 1;
1019 + p->numHashBytes = 4;
1020 + /* p->skipModeBits = 0; */
1021 + p->directInput = 0;
1022 + p->bigHash = 0;
1023 +}
1024 +
1025 +#define kCrcPoly 0xEDB88320
1026 +
1027 +void MatchFinder_Construct(CMatchFinder *p)
1028 +{
1029 + UInt32 i;
1030 + p->bufferBase = 0;
1031 + p->directInput = 0;
1032 + p->hash = 0;
1033 + MatchFinder_SetDefaultSettings(p);
1034 +
1035 + for (i = 0; i < 256; i++)
1036 + {
1037 + UInt32 r = i;
1038 + int j;
1039 + for (j = 0; j < 8; j++)
1040 + r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
1041 + p->crc[i] = r;
1042 + }
1043 +}
1044 +
1045 +static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
1046 +{
1047 + alloc->Free(alloc, p->hash);
1048 + p->hash = 0;
1049 +}
1050 +
1051 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
1052 +{
1053 + MatchFinder_FreeThisClassMemory(p, alloc);
1054 + LzInWindow_Free(p, alloc);
1055 +}
1056 +
1057 +static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
1058 +{
1059 + size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
1060 + if (sizeInBytes / sizeof(CLzRef) != num)
1061 + return 0;
1062 + return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
1063 +}
1064 +
1065 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
1066 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
1067 + ISzAlloc *alloc)
1068 +{
1069 + UInt32 sizeReserv;
1070 + if (historySize > kMaxHistorySize)
1071 + {
1072 + MatchFinder_Free(p, alloc);
1073 + return 0;
1074 + }
1075 + sizeReserv = historySize >> 1;
1076 + if (historySize > ((UInt32)2 << 30))
1077 + sizeReserv = historySize >> 2;
1078 + sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
1079 +
1080 + p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
1081 + p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
1082 + /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
1083 + if (LzInWindow_Create(p, sizeReserv, alloc))
1084 + {
1085 + UInt32 newCyclicBufferSize = (historySize /* >> p->skipModeBits */) + 1;
1086 + UInt32 hs;
1087 + p->matchMaxLen = matchMaxLen;
1088 + {
1089 + p->fixedHashSize = 0;
1090 + if (p->numHashBytes == 2)
1091 + hs = (1 << 16) - 1;
1092 + else
1093 + {
1094 + hs = historySize - 1;
1095 + hs |= (hs >> 1);
1096 + hs |= (hs >> 2);
1097 + hs |= (hs >> 4);
1098 + hs |= (hs >> 8);
1099 + hs >>= 1;
1100 + /* hs >>= p->skipModeBits; */
1101 + hs |= 0xFFFF; /* don't change it! It's required for Deflate */
1102 + if (hs > (1 << 24))
1103 + {
1104 + if (p->numHashBytes == 3)
1105 + hs = (1 << 24) - 1;
1106 + else
1107 + hs >>= 1;
1108 + }
1109 + }
1110 + p->hashMask = hs;
1111 + hs++;
1112 + if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
1113 + if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
1114 + if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
1115 + hs += p->fixedHashSize;
1116 + }
1117 +
1118 + {
1119 + UInt32 prevSize = p->hashSizeSum + p->numSons;
1120 + UInt32 newSize;
1121 + p->historySize = historySize;
1122 + p->hashSizeSum = hs;
1123 + p->cyclicBufferSize = newCyclicBufferSize;
1124 + p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);
1125 + newSize = p->hashSizeSum + p->numSons;
1126 + if (p->hash != 0 && prevSize == newSize)
1127 + return 1;
1128 + MatchFinder_FreeThisClassMemory(p, alloc);
1129 + p->hash = AllocRefs(newSize, alloc);
1130 + if (p->hash != 0)
1131 + {
1132 + p->son = p->hash + p->hashSizeSum;
1133 + return 1;
1134 + }
1135 + }
1136 + }
1137 + MatchFinder_Free(p, alloc);
1138 + return 0;
1139 +}
1140 +
1141 +static void MatchFinder_SetLimits(CMatchFinder *p)
1142 +{
1143 + UInt32 limit = kMaxValForNormalize - p->pos;
1144 + UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
1145 + if (limit2 < limit)
1146 + limit = limit2;
1147 + limit2 = p->streamPos - p->pos;
1148 + if (limit2 <= p->keepSizeAfter)
1149 + {
1150 + if (limit2 > 0)
1151 + limit2 = 1;
1152 + }
1153 + else
1154 + limit2 -= p->keepSizeAfter;
1155 + if (limit2 < limit)
1156 + limit = limit2;
1157 + {
1158 + UInt32 lenLimit = p->streamPos - p->pos;
1159 + if (lenLimit > p->matchMaxLen)
1160 + lenLimit = p->matchMaxLen;
1161 + p->lenLimit = lenLimit;
1162 + }
1163 + p->posLimit = p->pos + limit;
1164 +}
1165 +
1166 +void MatchFinder_Init(CMatchFinder *p)
1167 +{
1168 + UInt32 i;
1169 + for(i = 0; i < p->hashSizeSum; i++)
1170 + p->hash[i] = kEmptyHashValue;
1171 + p->cyclicBufferPos = 0;
1172 + p->buffer = p->bufferBase;
1173 + p->pos = p->streamPos = p->cyclicBufferSize;
1174 + p->result = SZ_OK;
1175 + p->streamEndWasReached = 0;
1176 + MatchFinder_ReadBlock(p);
1177 + MatchFinder_SetLimits(p);
1178 +}
1179 +
1180 +static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
1181 +{
1182 + return (p->pos - p->historySize - 1) & kNormalizeMask;
1183 +}
1184 +
1185 +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
1186 +{
1187 + UInt32 i;
1188 + for (i = 0; i < numItems; i++)
1189 + {
1190 + UInt32 value = items[i];
1191 + if (value <= subValue)
1192 + value = kEmptyHashValue;
1193 + else
1194 + value -= subValue;
1195 + items[i] = value;
1196 + }
1197 +}
1198 +
1199 +static void MatchFinder_Normalize(CMatchFinder *p)
1200 +{
1201 + UInt32 subValue = MatchFinder_GetSubValue(p);
1202 + MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
1203 + MatchFinder_ReduceOffsets(p, subValue);
1204 +}
1205 +
1206 +static void MatchFinder_CheckLimits(CMatchFinder *p)
1207 +{
1208 + if (p->pos == kMaxValForNormalize)
1209 + MatchFinder_Normalize(p);
1210 + if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
1211 + MatchFinder_CheckAndMoveAndRead(p);
1212 + if (p->cyclicBufferPos == p->cyclicBufferSize)
1213 + p->cyclicBufferPos = 0;
1214 + MatchFinder_SetLimits(p);
1215 +}
1216 +
1217 +static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1218 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
1219 + UInt32 *distances, UInt32 maxLen)
1220 +{
1221 + son[_cyclicBufferPos] = curMatch;
1222 + for (;;)
1223 + {
1224 + UInt32 delta = pos - curMatch;
1225 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1226 + return distances;
1227 + {
1228 + const Byte *pb = cur - delta;
1229 + curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
1230 + if (pb[maxLen] == cur[maxLen] && *pb == *cur)
1231 + {
1232 + UInt32 len = 0;
1233 + while(++len != lenLimit)
1234 + if (pb[len] != cur[len])
1235 + break;
1236 + if (maxLen < len)
1237 + {
1238 + *distances++ = maxLen = len;
1239 + *distances++ = delta - 1;
1240 + if (len == lenLimit)
1241 + return distances;
1242 + }
1243 + }
1244 + }
1245 + }
1246 +}
1247 +
1248 +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1249 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
1250 + UInt32 *distances, UInt32 maxLen)
1251 +{
1252 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1253 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1254 + UInt32 len0 = 0, len1 = 0;
1255 + for (;;)
1256 + {
1257 + UInt32 delta = pos - curMatch;
1258 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1259 + {
1260 + *ptr0 = *ptr1 = kEmptyHashValue;
1261 + return distances;
1262 + }
1263 + {
1264 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1265 + const Byte *pb = cur - delta;
1266 + UInt32 len = (len0 < len1 ? len0 : len1);
1267 + if (pb[len] == cur[len])
1268 + {
1269 + if (++len != lenLimit && pb[len] == cur[len])
1270 + while(++len != lenLimit)
1271 + if (pb[len] != cur[len])
1272 + break;
1273 + if (maxLen < len)
1274 + {
1275 + *distances++ = maxLen = len;
1276 + *distances++ = delta - 1;
1277 + if (len == lenLimit)
1278 + {
1279 + *ptr1 = pair[0];
1280 + *ptr0 = pair[1];
1281 + return distances;
1282 + }
1283 + }
1284 + }
1285 + if (pb[len] < cur[len])
1286 + {
1287 + *ptr1 = curMatch;
1288 + ptr1 = pair + 1;
1289 + curMatch = *ptr1;
1290 + len1 = len;
1291 + }
1292 + else
1293 + {
1294 + *ptr0 = curMatch;
1295 + ptr0 = pair;
1296 + curMatch = *ptr0;
1297 + len0 = len;
1298 + }
1299 + }
1300 + }
1301 +}
1302 +
1303 +static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1304 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
1305 +{
1306 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1307 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1308 + UInt32 len0 = 0, len1 = 0;
1309 + for (;;)
1310 + {
1311 + UInt32 delta = pos - curMatch;
1312 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1313 + {
1314 + *ptr0 = *ptr1 = kEmptyHashValue;
1315 + return;
1316 + }
1317 + {
1318 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1319 + const Byte *pb = cur - delta;
1320 + UInt32 len = (len0 < len1 ? len0 : len1);
1321 + if (pb[len] == cur[len])
1322 + {
1323 + while(++len != lenLimit)
1324 + if (pb[len] != cur[len])
1325 + break;
1326 + {
1327 + if (len == lenLimit)
1328 + {
1329 + *ptr1 = pair[0];
1330 + *ptr0 = pair[1];
1331 + return;
1332 + }
1333 + }
1334 + }
1335 + if (pb[len] < cur[len])
1336 + {
1337 + *ptr1 = curMatch;
1338 + ptr1 = pair + 1;
1339 + curMatch = *ptr1;
1340 + len1 = len;
1341 + }
1342 + else
1343 + {
1344 + *ptr0 = curMatch;
1345 + ptr0 = pair;
1346 + curMatch = *ptr0;
1347 + len0 = len;
1348 + }
1349 + }
1350 + }
1351 +}
1352 +
1353 +#define MOVE_POS \
1354 + ++p->cyclicBufferPos; \
1355 + p->buffer++; \
1356 + if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
1357 +
1358 +#define MOVE_POS_RET MOVE_POS return offset;
1359 +
1360 +static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
1361 +
1362 +#define GET_MATCHES_HEADER2(minLen, ret_op) \
1363 + UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \
1364 + lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
1365 + cur = p->buffer;
1366 +
1367 +#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
1368 +#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
1369 +
1370 +#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
1371 +
1372 +#define GET_MATCHES_FOOTER(offset, maxLen) \
1373 + offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
1374 + distances + offset, maxLen) - distances); MOVE_POS_RET;
1375 +
1376 +#define SKIP_FOOTER \
1377 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;
1378 +
1379 +static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1380 +{
1381 + UInt32 offset;
1382 + GET_MATCHES_HEADER(2)
1383 + HASH2_CALC;
1384 + curMatch = p->hash[hashValue];
1385 + p->hash[hashValue] = p->pos;
1386 + offset = 0;
1387 + GET_MATCHES_FOOTER(offset, 1)
1388 +}
1389 +
1390 +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1391 +{
1392 + UInt32 offset;
1393 + GET_MATCHES_HEADER(3)
1394 + HASH_ZIP_CALC;
1395 + curMatch = p->hash[hashValue];
1396 + p->hash[hashValue] = p->pos;
1397 + offset = 0;
1398 + GET_MATCHES_FOOTER(offset, 2)
1399 +}
1400 +
1401 +static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1402 +{
1403 + UInt32 hash2Value, delta2, maxLen, offset;
1404 + GET_MATCHES_HEADER(3)
1405 +
1406 + HASH3_CALC;
1407 +
1408 + delta2 = p->pos - p->hash[hash2Value];
1409 + curMatch = p->hash[kFix3HashSize + hashValue];
1410 +
1411 + p->hash[hash2Value] =
1412 + p->hash[kFix3HashSize + hashValue] = p->pos;
1413 +
1414 +
1415 + maxLen = 2;
1416 + offset = 0;
1417 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1418 + {
1419 + for (; maxLen != lenLimit; maxLen++)
1420 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1421 + break;
1422 + distances[0] = maxLen;
1423 + distances[1] = delta2 - 1;
1424 + offset = 2;
1425 + if (maxLen == lenLimit)
1426 + {
1427 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1428 + MOVE_POS_RET;
1429 + }
1430 + }
1431 + GET_MATCHES_FOOTER(offset, maxLen)
1432 +}
1433 +
1434 +static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1435 +{
1436 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1437 + GET_MATCHES_HEADER(4)
1438 +
1439 + HASH4_CALC;
1440 +
1441 + delta2 = p->pos - p->hash[ hash2Value];
1442 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1443 + curMatch = p->hash[kFix4HashSize + hashValue];
1444 +
1445 + p->hash[ hash2Value] =
1446 + p->hash[kFix3HashSize + hash3Value] =
1447 + p->hash[kFix4HashSize + hashValue] = p->pos;
1448 +
1449 + maxLen = 1;
1450 + offset = 0;
1451 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1452 + {
1453 + distances[0] = maxLen = 2;
1454 + distances[1] = delta2 - 1;
1455 + offset = 2;
1456 + }
1457 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1458 + {
1459 + maxLen = 3;
1460 + distances[offset + 1] = delta3 - 1;
1461 + offset += 2;
1462 + delta2 = delta3;
1463 + }
1464 + if (offset != 0)
1465 + {
1466 + for (; maxLen != lenLimit; maxLen++)
1467 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1468 + break;
1469 + distances[offset - 2] = maxLen;
1470 + if (maxLen == lenLimit)
1471 + {
1472 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1473 + MOVE_POS_RET;
1474 + }
1475 + }
1476 + if (maxLen < 3)
1477 + maxLen = 3;
1478 + GET_MATCHES_FOOTER(offset, maxLen)
1479 +}
1480 +
1481 +static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1482 +{
1483 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1484 + GET_MATCHES_HEADER(4)
1485 +
1486 + HASH4_CALC;
1487 +
1488 + delta2 = p->pos - p->hash[ hash2Value];
1489 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1490 + curMatch = p->hash[kFix4HashSize + hashValue];
1491 +
1492 + p->hash[ hash2Value] =
1493 + p->hash[kFix3HashSize + hash3Value] =
1494 + p->hash[kFix4HashSize + hashValue] = p->pos;
1495 +
1496 + maxLen = 1;
1497 + offset = 0;
1498 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1499 + {
1500 + distances[0] = maxLen = 2;
1501 + distances[1] = delta2 - 1;
1502 + offset = 2;
1503 + }
1504 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1505 + {
1506 + maxLen = 3;
1507 + distances[offset + 1] = delta3 - 1;
1508 + offset += 2;
1509 + delta2 = delta3;
1510 + }
1511 + if (offset != 0)
1512 + {
1513 + for (; maxLen != lenLimit; maxLen++)
1514 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1515 + break;
1516 + distances[offset - 2] = maxLen;
1517 + if (maxLen == lenLimit)
1518 + {
1519 + p->son[p->cyclicBufferPos] = curMatch;
1520 + MOVE_POS_RET;
1521 + }
1522 + }
1523 + if (maxLen < 3)
1524 + maxLen = 3;
1525 + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
1526 + distances + offset, maxLen) - (distances));
1527 + MOVE_POS_RET
1528 +}
1529 +
1530 +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1531 +{
1532 + UInt32 offset;
1533 + GET_MATCHES_HEADER(3)
1534 + HASH_ZIP_CALC;
1535 + curMatch = p->hash[hashValue];
1536 + p->hash[hashValue] = p->pos;
1537 + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
1538 + distances, 2) - (distances));
1539 + MOVE_POS_RET
1540 +}
1541 +
1542 +static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1543 +{
1544 + do
1545 + {
1546 + SKIP_HEADER(2)
1547 + HASH2_CALC;
1548 + curMatch = p->hash[hashValue];
1549 + p->hash[hashValue] = p->pos;
1550 + SKIP_FOOTER
1551 + }
1552 + while (--num != 0);
1553 +}
1554 +
1555 +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1556 +{
1557 + do
1558 + {
1559 + SKIP_HEADER(3)
1560 + HASH_ZIP_CALC;
1561 + curMatch = p->hash[hashValue];
1562 + p->hash[hashValue] = p->pos;
1563 + SKIP_FOOTER
1564 + }
1565 + while (--num != 0);
1566 +}
1567 +
1568 +static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1569 +{
1570 + do
1571 + {
1572 + UInt32 hash2Value;
1573 + SKIP_HEADER(3)
1574 + HASH3_CALC;
1575 + curMatch = p->hash[kFix3HashSize + hashValue];
1576 + p->hash[hash2Value] =
1577 + p->hash[kFix3HashSize + hashValue] = p->pos;
1578 + SKIP_FOOTER
1579 + }
1580 + while (--num != 0);
1581 +}
1582 +
1583 +static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1584 +{
1585 + do
1586 + {
1587 + UInt32 hash2Value, hash3Value;
1588 + SKIP_HEADER(4)
1589 + HASH4_CALC;
1590 + curMatch = p->hash[kFix4HashSize + hashValue];
1591 + p->hash[ hash2Value] =
1592 + p->hash[kFix3HashSize + hash3Value] = p->pos;
1593 + p->hash[kFix4HashSize + hashValue] = p->pos;
1594 + SKIP_FOOTER
1595 + }
1596 + while (--num != 0);
1597 +}
1598 +
1599 +static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1600 +{
1601 + do
1602 + {
1603 + UInt32 hash2Value, hash3Value;
1604 + SKIP_HEADER(4)
1605 + HASH4_CALC;
1606 + curMatch = p->hash[kFix4HashSize + hashValue];
1607 + p->hash[ hash2Value] =
1608 + p->hash[kFix3HashSize + hash3Value] =
1609 + p->hash[kFix4HashSize + hashValue] = p->pos;
1610 + p->son[p->cyclicBufferPos] = curMatch;
1611 + MOVE_POS
1612 + }
1613 + while (--num != 0);
1614 +}
1615 +
1616 +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1617 +{
1618 + do
1619 + {
1620 + SKIP_HEADER(3)
1621 + HASH_ZIP_CALC;
1622 + curMatch = p->hash[hashValue];
1623 + p->hash[hashValue] = p->pos;
1624 + p->son[p->cyclicBufferPos] = curMatch;
1625 + MOVE_POS
1626 + }
1627 + while (--num != 0);
1628 +}
1629 +
1630 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
1631 +{
1632 + vTable->Init = (Mf_Init_Func)MatchFinder_Init;
1633 + vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;
1634 + vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
1635 + vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
1636 + if (!p->btMode)
1637 + {
1638 + vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;
1639 + vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;
1640 + }
1641 + else if (p->numHashBytes == 2)
1642 + {
1643 + vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;
1644 + vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;
1645 + }
1646 + else if (p->numHashBytes == 3)
1647 + {
1648 + vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;
1649 + vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;
1650 + }
1651 + else
1652 + {
1653 + vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
1654 + vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
1655 + }
1656 +}
1657 --- /dev/null
1658 +++ b/lzma/LzmaDec.c
1659 @@ -0,0 +1,1014 @@
1660 +/* LzmaDec.c -- LZMA Decoder
1661 +2008-04-29
1662 +Copyright (c) 1999-2008 Igor Pavlov
1663 +Read LzmaDec.h for license options */
1664 +
1665 +#include "LzmaDec.h"
1666 +
1667 +#include <string.h>
1668 +
1669 +#define kNumTopBits 24
1670 +#define kTopValue ((UInt32)1 << kNumTopBits)
1671 +
1672 +#define kNumBitModelTotalBits 11
1673 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
1674 +#define kNumMoveBits 5
1675 +
1676 +#define RC_INIT_SIZE 5
1677 +
1678 +#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
1679 +
1680 +#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1681 +#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
1682 +#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
1683 +#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
1684 + { UPDATE_0(p); i = (i + i); A0; } else \
1685 + { UPDATE_1(p); i = (i + i) + 1; A1; }
1686 +#define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
1687 +
1688 +#define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
1689 +#define TREE_DECODE(probs, limit, i) \
1690 + { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
1691 +
1692 +/* #define _LZMA_SIZE_OPT */
1693 +
1694 +#ifdef _LZMA_SIZE_OPT
1695 +#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
1696 +#else
1697 +#define TREE_6_DECODE(probs, i) \
1698 + { i = 1; \
1699 + TREE_GET_BIT(probs, i); \
1700 + TREE_GET_BIT(probs, i); \
1701 + TREE_GET_BIT(probs, i); \
1702 + TREE_GET_BIT(probs, i); \
1703 + TREE_GET_BIT(probs, i); \
1704 + TREE_GET_BIT(probs, i); \
1705 + i -= 0x40; }
1706 +#endif
1707 +
1708 +#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
1709 +
1710 +#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1711 +#define UPDATE_0_CHECK range = bound;
1712 +#define UPDATE_1_CHECK range -= bound; code -= bound;
1713 +#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
1714 + { UPDATE_0_CHECK; i = (i + i); A0; } else \
1715 + { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
1716 +#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
1717 +#define TREE_DECODE_CHECK(probs, limit, i) \
1718 + { i = 1; do { GET_BIT_CHECK(probs + i, i) } while(i < limit); i -= limit; }
1719 +
1720 +
1721 +#define kNumPosBitsMax 4
1722 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
1723 +
1724 +#define kLenNumLowBits 3
1725 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
1726 +#define kLenNumMidBits 3
1727 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
1728 +#define kLenNumHighBits 8
1729 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
1730 +
1731 +#define LenChoice 0
1732 +#define LenChoice2 (LenChoice + 1)
1733 +#define LenLow (LenChoice2 + 1)
1734 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
1735 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
1736 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
1737 +
1738 +
1739 +#define kNumStates 12
1740 +#define kNumLitStates 7
1741 +
1742 +#define kStartPosModelIndex 4
1743 +#define kEndPosModelIndex 14
1744 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
1745 +
1746 +#define kNumPosSlotBits 6
1747 +#define kNumLenToPosStates 4
1748 +
1749 +#define kNumAlignBits 4
1750 +#define kAlignTableSize (1 << kNumAlignBits)
1751 +
1752 +#define kMatchMinLen 2
1753 +#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
1754 +
1755 +#define IsMatch 0
1756 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
1757 +#define IsRepG0 (IsRep + kNumStates)
1758 +#define IsRepG1 (IsRepG0 + kNumStates)
1759 +#define IsRepG2 (IsRepG1 + kNumStates)
1760 +#define IsRep0Long (IsRepG2 + kNumStates)
1761 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
1762 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
1763 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
1764 +#define LenCoder (Align + kAlignTableSize)
1765 +#define RepLenCoder (LenCoder + kNumLenProbs)
1766 +#define Literal (RepLenCoder + kNumLenProbs)
1767 +
1768 +#define LZMA_BASE_SIZE 1846
1769 +#define LZMA_LIT_SIZE 768
1770 +
1771 +#define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
1772 +
1773 +#if Literal != LZMA_BASE_SIZE
1774 +StopCompilingDueBUG
1775 +#endif
1776 +
1777 +/*
1778 +#define LZMA_STREAM_WAS_FINISHED_ID (-1)
1779 +#define LZMA_SPEC_LEN_OFFSET (-3)
1780 +*/
1781 +
1782 +Byte kLiteralNextStates[kNumStates * 2] =
1783 +{
1784 + 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5,
1785 + 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10
1786 +};
1787 +
1788 +#define LZMA_DIC_MIN (1 << 12)
1789 +
1790 +/* First LZMA-symbol is always decoded.
1791 +And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
1792 +Out:
1793 + Result:
1794 + 0 - OK
1795 + 1 - Error
1796 + p->remainLen:
1797 + < kMatchSpecLenStart : normal remain
1798 + = kMatchSpecLenStart : finished
1799 + = kMatchSpecLenStart + 1 : Flush marker
1800 + = kMatchSpecLenStart + 2 : State Init Marker
1801 +*/
1802 +
1803 +static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
1804 +{
1805 + CLzmaProb *probs = p->probs;
1806 +
1807 + unsigned state = p->state;
1808 + UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
1809 + unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
1810 + unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
1811 + unsigned lc = p->prop.lc;
1812 +
1813 + Byte *dic = p->dic;
1814 + SizeT dicBufSize = p->dicBufSize;
1815 + SizeT dicPos = p->dicPos;
1816 +
1817 + UInt32 processedPos = p->processedPos;
1818 + UInt32 checkDicSize = p->checkDicSize;
1819 + unsigned len = 0;
1820 +
1821 + const Byte *buf = p->buf;
1822 + UInt32 range = p->range;
1823 + UInt32 code = p->code;
1824 +
1825 + do
1826 + {
1827 + CLzmaProb *prob;
1828 + UInt32 bound;
1829 + unsigned ttt;
1830 + unsigned posState = processedPos & pbMask;
1831 +
1832 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
1833 + IF_BIT_0(prob)
1834 + {
1835 + unsigned symbol;
1836 + UPDATE_0(prob);
1837 + prob = probs + Literal;
1838 + if (checkDicSize != 0 || processedPos != 0)
1839 + prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
1840 + (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
1841 +
1842 + if (state < kNumLitStates)
1843 + {
1844 + symbol = 1;
1845 + do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
1846 + }
1847 + else
1848 + {
1849 + unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1850 + unsigned offs = 0x100;
1851 + symbol = 1;
1852 + do
1853 + {
1854 + unsigned bit;
1855 + CLzmaProb *probLit;
1856 + matchByte <<= 1;
1857 + bit = (matchByte & offs);
1858 + probLit = prob + offs + bit + symbol;
1859 + GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
1860 + }
1861 + while (symbol < 0x100);
1862 + }
1863 + dic[dicPos++] = (Byte)symbol;
1864 + processedPos++;
1865 +
1866 + state = kLiteralNextStates[state];
1867 + /* if (state < 4) state = 0; else if (state < 10) state -= 3; else state -= 6; */
1868 + continue;
1869 + }
1870 + else
1871 + {
1872 + UPDATE_1(prob);
1873 + prob = probs + IsRep + state;
1874 + IF_BIT_0(prob)
1875 + {
1876 + UPDATE_0(prob);
1877 + state += kNumStates;
1878 + prob = probs + LenCoder;
1879 + }
1880 + else
1881 + {
1882 + UPDATE_1(prob);
1883 + if (checkDicSize == 0 && processedPos == 0)
1884 + return SZ_ERROR_DATA;
1885 + prob = probs + IsRepG0 + state;
1886 + IF_BIT_0(prob)
1887 + {
1888 + UPDATE_0(prob);
1889 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
1890 + IF_BIT_0(prob)
1891 + {
1892 + UPDATE_0(prob);
1893 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1894 + dicPos++;
1895 + processedPos++;
1896 + state = state < kNumLitStates ? 9 : 11;
1897 + continue;
1898 + }
1899 + UPDATE_1(prob);
1900 + }
1901 + else
1902 + {
1903 + UInt32 distance;
1904 + UPDATE_1(prob);
1905 + prob = probs + IsRepG1 + state;
1906 + IF_BIT_0(prob)
1907 + {
1908 + UPDATE_0(prob);
1909 + distance = rep1;
1910 + }
1911 + else
1912 + {
1913 + UPDATE_1(prob);
1914 + prob = probs + IsRepG2 + state;
1915 + IF_BIT_0(prob)
1916 + {
1917 + UPDATE_0(prob);
1918 + distance = rep2;
1919 + }
1920 + else
1921 + {
1922 + UPDATE_1(prob);
1923 + distance = rep3;
1924 + rep3 = rep2;
1925 + }
1926 + rep2 = rep1;
1927 + }
1928 + rep1 = rep0;
1929 + rep0 = distance;
1930 + }
1931 + state = state < kNumLitStates ? 8 : 11;
1932 + prob = probs + RepLenCoder;
1933 + }
1934 + {
1935 + unsigned limit, offset;
1936 + CLzmaProb *probLen = prob + LenChoice;
1937 + IF_BIT_0(probLen)
1938 + {
1939 + UPDATE_0(probLen);
1940 + probLen = prob + LenLow + (posState << kLenNumLowBits);
1941 + offset = 0;
1942 + limit = (1 << kLenNumLowBits);
1943 + }
1944 + else
1945 + {
1946 + UPDATE_1(probLen);
1947 + probLen = prob + LenChoice2;
1948 + IF_BIT_0(probLen)
1949 + {
1950 + UPDATE_0(probLen);
1951 + probLen = prob + LenMid + (posState << kLenNumMidBits);
1952 + offset = kLenNumLowSymbols;
1953 + limit = (1 << kLenNumMidBits);
1954 + }
1955 + else
1956 + {
1957 + UPDATE_1(probLen);
1958 + probLen = prob + LenHigh;
1959 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
1960 + limit = (1 << kLenNumHighBits);
1961 + }
1962 + }
1963 + TREE_DECODE(probLen, limit, len);
1964 + len += offset;
1965 + }
1966 +
1967 + if (state >= kNumStates)
1968 + {
1969 + UInt32 distance;
1970 + prob = probs + PosSlot +
1971 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
1972 + TREE_6_DECODE(prob, distance);
1973 + if (distance >= kStartPosModelIndex)
1974 + {
1975 + unsigned posSlot = (unsigned)distance;
1976 + int numDirectBits = (int)(((distance >> 1) - 1));
1977 + distance = (2 | (distance & 1));
1978 + if (posSlot < kEndPosModelIndex)
1979 + {
1980 + distance <<= numDirectBits;
1981 + prob = probs + SpecPos + distance - posSlot - 1;
1982 + {
1983 + UInt32 mask = 1;
1984 + unsigned i = 1;
1985 + do
1986 + {
1987 + GET_BIT2(prob + i, i, ; , distance |= mask);
1988 + mask <<= 1;
1989 + }
1990 + while(--numDirectBits != 0);
1991 + }
1992 + }
1993 + else
1994 + {
1995 + numDirectBits -= kNumAlignBits;
1996 + do
1997 + {
1998 + NORMALIZE
1999 + range >>= 1;
2000 +
2001 + {
2002 + UInt32 t;
2003 + code -= range;
2004 + t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
2005 + distance = (distance << 1) + (t + 1);
2006 + code += range & t;
2007 + }
2008 + /*
2009 + distance <<= 1;
2010 + if (code >= range)
2011 + {
2012 + code -= range;
2013 + distance |= 1;
2014 + }
2015 + */
2016 + }
2017 + while (--numDirectBits != 0);
2018 + prob = probs + Align;
2019 + distance <<= kNumAlignBits;
2020 + {
2021 + unsigned i = 1;
2022 + GET_BIT2(prob + i, i, ; , distance |= 1);
2023 + GET_BIT2(prob + i, i, ; , distance |= 2);
2024 + GET_BIT2(prob + i, i, ; , distance |= 4);
2025 + GET_BIT2(prob + i, i, ; , distance |= 8);
2026 + }
2027 + if (distance == (UInt32)0xFFFFFFFF)
2028 + {
2029 + len += kMatchSpecLenStart;
2030 + state -= kNumStates;
2031 + break;
2032 + }
2033 + }
2034 + }
2035 + rep3 = rep2;
2036 + rep2 = rep1;
2037 + rep1 = rep0;
2038 + rep0 = distance + 1;
2039 + if (checkDicSize == 0)
2040 + {
2041 + if (distance >= processedPos)
2042 + return SZ_ERROR_DATA;
2043 + }
2044 + else if (distance >= checkDicSize)
2045 + return SZ_ERROR_DATA;
2046 + state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
2047 + /* state = kLiteralNextStates[state]; */
2048 + }
2049 +
2050 + len += kMatchMinLen;
2051 +
2052 + {
2053 + SizeT rem = limit - dicPos;
2054 + unsigned curLen = ((rem < len) ? (unsigned)rem : len);
2055 + SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
2056 +
2057 + processedPos += curLen;
2058 +
2059 + len -= curLen;
2060 + if (pos + curLen <= dicBufSize)
2061 + {
2062 + Byte *dest = dic + dicPos;
2063 + ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
2064 + const Byte *lim = dest + curLen;
2065 + dicPos += curLen;
2066 + do
2067 + *(dest) = (Byte)*(dest + src);
2068 + while (++dest != lim);
2069 + }
2070 + else
2071 + {
2072 + do
2073 + {
2074 + dic[dicPos++] = dic[pos];
2075 + if (++pos == dicBufSize)
2076 + pos = 0;
2077 + }
2078 + while (--curLen != 0);
2079 + }
2080 + }
2081 + }
2082 + }
2083 + while (dicPos < limit && buf < bufLimit);
2084 + NORMALIZE;
2085 + p->buf = buf;
2086 + p->range = range;
2087 + p->code = code;
2088 + p->remainLen = len;
2089 + p->dicPos = dicPos;
2090 + p->processedPos = processedPos;
2091 + p->reps[0] = rep0;
2092 + p->reps[1] = rep1;
2093 + p->reps[2] = rep2;
2094 + p->reps[3] = rep3;
2095 + p->state = state;
2096 +
2097 + return SZ_OK;
2098 +}
2099 +
2100 +static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
2101 +{
2102 + if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
2103 + {
2104 + Byte *dic = p->dic;
2105 + SizeT dicPos = p->dicPos;
2106 + SizeT dicBufSize = p->dicBufSize;
2107 + unsigned len = p->remainLen;
2108 + UInt32 rep0 = p->reps[0];
2109 + if (limit - dicPos < len)
2110 + len = (unsigned)(limit - dicPos);
2111 +
2112 + if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
2113 + p->checkDicSize = p->prop.dicSize;
2114 +
2115 + p->processedPos += len;
2116 + p->remainLen -= len;
2117 + while (len-- != 0)
2118 + {
2119 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
2120 + dicPos++;
2121 + }
2122 + p->dicPos = dicPos;
2123 + }
2124 +}
2125 +
2126 +/* LzmaDec_DecodeReal2 decodes LZMA-symbols and sets p->needFlush and p->needInit, if required. */
2127 +
2128 +static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
2129 +{
2130 + do
2131 + {
2132 + SizeT limit2 = limit;
2133 + if (p->checkDicSize == 0)
2134 + {
2135 + UInt32 rem = p->prop.dicSize - p->processedPos;
2136 + if (limit - p->dicPos > rem)
2137 + limit2 = p->dicPos + rem;
2138 + }
2139 + RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
2140 + if (p->processedPos >= p->prop.dicSize)
2141 + p->checkDicSize = p->prop.dicSize;
2142 + LzmaDec_WriteRem(p, limit);
2143 + }
2144 + while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
2145 +
2146 + if (p->remainLen > kMatchSpecLenStart)
2147 + {
2148 + p->remainLen = kMatchSpecLenStart;
2149 + }
2150 + return 0;
2151 +}
2152 +
2153 +typedef enum
2154 +{
2155 + DUMMY_ERROR, /* unexpected end of input stream */
2156 + DUMMY_LIT,
2157 + DUMMY_MATCH,
2158 + DUMMY_REP
2159 +} ELzmaDummy;
2160 +
2161 +static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
2162 +{
2163 + UInt32 range = p->range;
2164 + UInt32 code = p->code;
2165 + const Byte *bufLimit = buf + inSize;
2166 + CLzmaProb *probs = p->probs;
2167 + unsigned state = p->state;
2168 + ELzmaDummy res;
2169 +
2170 + {
2171 + CLzmaProb *prob;
2172 + UInt32 bound;
2173 + unsigned ttt;
2174 + unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
2175 +
2176 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
2177 + IF_BIT_0_CHECK(prob)
2178 + {
2179 + UPDATE_0_CHECK
2180 +
2181 + /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
2182 +
2183 + prob = probs + Literal;
2184 + if (p->checkDicSize != 0 || p->processedPos != 0)
2185 + prob += (LZMA_LIT_SIZE *
2186 + ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
2187 + (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
2188 +
2189 + if (state < kNumLitStates)
2190 + {
2191 + unsigned symbol = 1;
2192 + do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
2193 + }
2194 + else
2195 + {
2196 + unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
2197 + ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
2198 + unsigned offs = 0x100;
2199 + unsigned symbol = 1;
2200 + do
2201 + {
2202 + unsigned bit;
2203 + CLzmaProb *probLit;
2204 + matchByte <<= 1;
2205 + bit = (matchByte & offs);
2206 + probLit = prob + offs + bit + symbol;
2207 + GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
2208 + }
2209 + while (symbol < 0x100);
2210 + }
2211 + res = DUMMY_LIT;
2212 + }
2213 + else
2214 + {
2215 + unsigned len;
2216 + UPDATE_1_CHECK;
2217 +
2218 + prob = probs + IsRep + state;
2219 + IF_BIT_0_CHECK(prob)
2220 + {
2221 + UPDATE_0_CHECK;
2222 + state = 0;
2223 + prob = probs + LenCoder;
2224 + res = DUMMY_MATCH;
2225 + }
2226 + else
2227 + {
2228 + UPDATE_1_CHECK;
2229 + res = DUMMY_REP;
2230 + prob = probs + IsRepG0 + state;
2231 + IF_BIT_0_CHECK(prob)
2232 + {
2233 + UPDATE_0_CHECK;
2234 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
2235 + IF_BIT_0_CHECK(prob)
2236 + {
2237 + UPDATE_0_CHECK;
2238 + NORMALIZE_CHECK;
2239 + return DUMMY_REP;
2240 + }
2241 + else
2242 + {
2243 + UPDATE_1_CHECK;
2244 + }
2245 + }
2246 + else
2247 + {
2248 + UPDATE_1_CHECK;
2249 + prob = probs + IsRepG1 + state;
2250 + IF_BIT_0_CHECK(prob)
2251 + {
2252 + UPDATE_0_CHECK;
2253 + }
2254 + else
2255 + {
2256 + UPDATE_1_CHECK;
2257 + prob = probs + IsRepG2 + state;
2258 + IF_BIT_0_CHECK(prob)
2259 + {
2260 + UPDATE_0_CHECK;
2261 + }
2262 + else
2263 + {
2264 + UPDATE_1_CHECK;
2265 + }
2266 + }
2267 + }
2268 + state = kNumStates;
2269 + prob = probs + RepLenCoder;
2270 + }
2271 + {
2272 + unsigned limit, offset;
2273 + CLzmaProb *probLen = prob + LenChoice;
2274 + IF_BIT_0_CHECK(probLen)
2275 + {
2276 + UPDATE_0_CHECK;
2277 + probLen = prob + LenLow + (posState << kLenNumLowBits);
2278 + offset = 0;
2279 + limit = 1 << kLenNumLowBits;
2280 + }
2281 + else
2282 + {
2283 + UPDATE_1_CHECK;
2284 + probLen = prob + LenChoice2;
2285 + IF_BIT_0_CHECK(probLen)
2286 + {
2287 + UPDATE_0_CHECK;
2288 + probLen = prob + LenMid + (posState << kLenNumMidBits);
2289 + offset = kLenNumLowSymbols;
2290 + limit = 1 << kLenNumMidBits;
2291 + }
2292 + else
2293 + {
2294 + UPDATE_1_CHECK;
2295 + probLen = prob + LenHigh;
2296 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
2297 + limit = 1 << kLenNumHighBits;
2298 + }
2299 + }
2300 + TREE_DECODE_CHECK(probLen, limit, len);
2301 + len += offset;
2302 + }
2303 +
2304 + if (state < 4)
2305 + {
2306 + unsigned posSlot;
2307 + prob = probs + PosSlot +
2308 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
2309 + kNumPosSlotBits);
2310 + TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
2311 + if (posSlot >= kStartPosModelIndex)
2312 + {
2313 + int numDirectBits = ((posSlot >> 1) - 1);
2314 +
2315 + /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
2316 +
2317 + if (posSlot < kEndPosModelIndex)
2318 + {
2319 + prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
2320 + }
2321 + else
2322 + {
2323 + numDirectBits -= kNumAlignBits;
2324 + do
2325 + {
2326 + NORMALIZE_CHECK
2327 + range >>= 1;
2328 + code -= range & (((code - range) >> 31) - 1);
2329 + /* if (code >= range) code -= range; */
2330 + }
2331 + while (--numDirectBits != 0);
2332 + prob = probs + Align;
2333 + numDirectBits = kNumAlignBits;
2334 + }
2335 + {
2336 + unsigned i = 1;
2337 + do
2338 + {
2339 + GET_BIT_CHECK(prob + i, i);
2340 + }
2341 + while(--numDirectBits != 0);
2342 + }
2343 + }
2344 + }
2345 + }
2346 + }
2347 + NORMALIZE_CHECK;
2348 + return res;
2349 +}
2350 +
2351 +
2352 +static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
2353 +{
2354 + p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
2355 + p->range = 0xFFFFFFFF;
2356 + p->needFlush = 0;
2357 +}
2358 +
2359 +static void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
2360 +{
2361 + p->needFlush = 1;
2362 + p->remainLen = 0;
2363 + p->tempBufSize = 0;
2364 +
2365 + if (initDic)
2366 + {
2367 + p->processedPos = 0;
2368 + p->checkDicSize = 0;
2369 + p->needInitState = 1;
2370 + }
2371 + if (initState)
2372 + p->needInitState = 1;
2373 +}
2374 +
2375 +void LzmaDec_Init(CLzmaDec *p)
2376 +{
2377 + p->dicPos = 0;
2378 + LzmaDec_InitDicAndState(p, True, True);
2379 +}
2380 +
2381 +static void LzmaDec_InitStateReal(CLzmaDec *p)
2382 +{
2383 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
2384 + UInt32 i;
2385 + CLzmaProb *probs = p->probs;
2386 + for (i = 0; i < numProbs; i++)
2387 + probs[i] = kBitModelTotal >> 1;
2388 + p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
2389 + p->state = 0;
2390 + p->needInitState = 0;
2391 +}
2392 +
2393 +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
2394 + ELzmaFinishMode finishMode, ELzmaStatus *status)
2395 +{
2396 + SizeT inSize = *srcLen;
2397 + (*srcLen) = 0;
2398 + LzmaDec_WriteRem(p, dicLimit);
2399 +
2400 + *status = LZMA_STATUS_NOT_SPECIFIED;
2401 +
2402 + while (p->remainLen != kMatchSpecLenStart)
2403 + {
2404 + int checkEndMarkNow;
2405 +
2406 + if (p->needFlush != 0)
2407 + {
2408 + for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
2409 + p->tempBuf[p->tempBufSize++] = *src++;
2410 + if (p->tempBufSize < RC_INIT_SIZE)
2411 + {
2412 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2413 + return SZ_OK;
2414 + }
2415 + if (p->tempBuf[0] != 0)
2416 + return SZ_ERROR_DATA;
2417 +
2418 + LzmaDec_InitRc(p, p->tempBuf);
2419 + p->tempBufSize = 0;
2420 + }
2421 +
2422 + checkEndMarkNow = 0;
2423 + if (p->dicPos >= dicLimit)
2424 + {
2425 + if (p->remainLen == 0 && p->code == 0)
2426 + {
2427 + *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
2428 + return SZ_OK;
2429 + }
2430 + if (finishMode == LZMA_FINISH_ANY)
2431 + {
2432 + *status = LZMA_STATUS_NOT_FINISHED;
2433 + return SZ_OK;
2434 + }
2435 + if (p->remainLen != 0)
2436 + {
2437 + *status = LZMA_STATUS_NOT_FINISHED;
2438 + return SZ_ERROR_DATA;
2439 + }
2440 + checkEndMarkNow = 1;
2441 + }
2442 +
2443 + if (p->needInitState)
2444 + LzmaDec_InitStateReal(p);
2445 +
2446 + if (p->tempBufSize == 0)
2447 + {
2448 + SizeT processed;
2449 + const Byte *bufLimit;
2450 + if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2451 + {
2452 + int dummyRes = LzmaDec_TryDummy(p, src, inSize);
2453 + if (dummyRes == DUMMY_ERROR)
2454 + {
2455 + memcpy(p->tempBuf, src, inSize);
2456 + p->tempBufSize = (unsigned)inSize;
2457 + (*srcLen) += inSize;
2458 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2459 + return SZ_OK;
2460 + }
2461 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2462 + {
2463 + *status = LZMA_STATUS_NOT_FINISHED;
2464 + return SZ_ERROR_DATA;
2465 + }
2466 + bufLimit = src;
2467 + }
2468 + else
2469 + bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
2470 + p->buf = src;
2471 + if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
2472 + return SZ_ERROR_DATA;
2473 + processed = p->buf - src;
2474 + (*srcLen) += processed;
2475 + src += processed;
2476 + inSize -= processed;
2477 + }
2478 + else
2479 + {
2480 + unsigned rem = p->tempBufSize, lookAhead = 0;
2481 + while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
2482 + p->tempBuf[rem++] = src[lookAhead++];
2483 + p->tempBufSize = rem;
2484 + if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2485 + {
2486 + int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
2487 + if (dummyRes == DUMMY_ERROR)
2488 + {
2489 + (*srcLen) += lookAhead;
2490 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2491 + return SZ_OK;
2492 + }
2493 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2494 + {
2495 + *status = LZMA_STATUS_NOT_FINISHED;
2496 + return SZ_ERROR_DATA;
2497 + }
2498 + }
2499 + p->buf = p->tempBuf;
2500 + if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
2501 + return SZ_ERROR_DATA;
2502 + lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
2503 + (*srcLen) += lookAhead;
2504 + src += lookAhead;
2505 + inSize -= lookAhead;
2506 + p->tempBufSize = 0;
2507 + }
2508 + }
2509 + if (p->code == 0)
2510 + *status = LZMA_STATUS_FINISHED_WITH_MARK;
2511 + return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
2512 +}
2513 +
2514 +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
2515 +{
2516 + SizeT outSize = *destLen;
2517 + SizeT inSize = *srcLen;
2518 + *srcLen = *destLen = 0;
2519 + for (;;)
2520 + {
2521 + SizeT inSizeCur = inSize, outSizeCur, dicPos;
2522 + ELzmaFinishMode curFinishMode;
2523 + SRes res;
2524 + if (p->dicPos == p->dicBufSize)
2525 + p->dicPos = 0;
2526 + dicPos = p->dicPos;
2527 + if (outSize > p->dicBufSize - dicPos)
2528 + {
2529 + outSizeCur = p->dicBufSize;
2530 + curFinishMode = LZMA_FINISH_ANY;
2531 + }
2532 + else
2533 + {
2534 + outSizeCur = dicPos + outSize;
2535 + curFinishMode = finishMode;
2536 + }
2537 +
2538 + res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
2539 + src += inSizeCur;
2540 + inSize -= inSizeCur;
2541 + *srcLen += inSizeCur;
2542 + outSizeCur = p->dicPos - dicPos;
2543 + memcpy(dest, p->dic + dicPos, outSizeCur);
2544 + dest += outSizeCur;
2545 + outSize -= outSizeCur;
2546 + *destLen += outSizeCur;
2547 + if (res != 0)
2548 + return res;
2549 + if (outSizeCur == 0 || outSize == 0)
2550 + return SZ_OK;
2551 + }
2552 +}
2553 +
2554 +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
2555 +{
2556 + alloc->Free(alloc, p->probs);
2557 + p->probs = 0;
2558 +}
2559 +
2560 +static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
2561 +{
2562 + alloc->Free(alloc, p->dic);
2563 + p->dic = 0;
2564 +}
2565 +
2566 +void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
2567 +{
2568 + LzmaDec_FreeProbs(p, alloc);
2569 + LzmaDec_FreeDict(p, alloc);
2570 +}
2571 +
2572 +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
2573 +{
2574 + UInt32 dicSize;
2575 + Byte d;
2576 +
2577 + if (size < LZMA_PROPS_SIZE)
2578 + return SZ_ERROR_UNSUPPORTED;
2579 + else
2580 + dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
2581 +
2582 + if (dicSize < LZMA_DIC_MIN)
2583 + dicSize = LZMA_DIC_MIN;
2584 + p->dicSize = dicSize;
2585 +
2586 + d = data[0];
2587 + if (d >= (9 * 5 * 5))
2588 + return SZ_ERROR_UNSUPPORTED;
2589 +
2590 + p->lc = d % 9;
2591 + d /= 9;
2592 + p->pb = d / 5;
2593 + p->lp = d % 5;
2594 +
2595 + return SZ_OK;
2596 +}
2597 +
2598 +static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
2599 +{
2600 + UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
2601 + if (p->probs == 0 || numProbs != p->numProbs)
2602 + {
2603 + LzmaDec_FreeProbs(p, alloc);
2604 + p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
2605 + p->numProbs = numProbs;
2606 + if (p->probs == 0)
2607 + return SZ_ERROR_MEM;
2608 + }
2609 + return SZ_OK;
2610 +}
2611 +
2612 +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2613 +{
2614 + CLzmaProps propNew;
2615 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2616 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2617 + p->prop = propNew;
2618 + return SZ_OK;
2619 +}
2620 +
2621 +SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2622 +{
2623 + CLzmaProps propNew;
2624 + SizeT dicBufSize;
2625 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2626 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2627 + dicBufSize = propNew.dicSize;
2628 + if (p->dic == 0 || dicBufSize != p->dicBufSize)
2629 + {
2630 + LzmaDec_FreeDict(p, alloc);
2631 + p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
2632 + if (p->dic == 0)
2633 + {
2634 + LzmaDec_FreeProbs(p, alloc);
2635 + return SZ_ERROR_MEM;
2636 + }
2637 + }
2638 + p->dicBufSize = dicBufSize;
2639 + p->prop = propNew;
2640 + return SZ_OK;
2641 +}
2642 +
2643 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
2644 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
2645 + ELzmaStatus *status, ISzAlloc *alloc)
2646 +{
2647 + CLzmaDec p;
2648 + SRes res;
2649 + SizeT inSize = *srcLen;
2650 + SizeT outSize = *destLen;
2651 + *srcLen = *destLen = 0;
2652 + if (inSize < RC_INIT_SIZE)
2653 + return SZ_ERROR_INPUT_EOF;
2654 +
2655 + LzmaDec_Construct(&p);
2656 + res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
2657 + if (res != 0)
2658 + return res;
2659 + p.dic = dest;
2660 + p.dicBufSize = outSize;
2661 +
2662 + LzmaDec_Init(&p);
2663 +
2664 + *srcLen = inSize;
2665 + res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
2666 +
2667 + if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
2668 + res = SZ_ERROR_INPUT_EOF;
2669 +
2670 + (*destLen) = p.dicPos;
2671 + LzmaDec_FreeProbs(&p, alloc);
2672 + return res;
2673 +}
2674 --- /dev/null
2675 +++ b/lzma/LzmaEnc.c
2676 @@ -0,0 +1,2335 @@
2677 +/* LzmaEnc.c -- LZMA Encoder
2678 +2008-04-28
2679 +Copyright (c) 1999-2008 Igor Pavlov
2680 +Read LzmaEnc.h for license options */
2681 +
2682 +#if defined(SHOW_STAT) || defined(SHOW_STAT2)
2683 +#include <stdio.h>
2684 +#endif
2685 +
2686 +#include <string.h>
2687 +
2688 +#include "LzmaEnc.h"
2689 +
2690 +#include "LzFind.h"
2691 +#ifdef COMPRESS_MF_MT
2692 +#include "LzFindMt.h"
2693 +#endif
2694 +
2695 +/* #define SHOW_STAT */
2696 +/* #define SHOW_STAT2 */
2697 +
2698 +#ifdef SHOW_STAT
2699 +static int ttt = 0;
2700 +#endif
2701 +
2702 +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
2703 +
2704 +#define kBlockSize (9 << 10)
2705 +#define kUnpackBlockSize (1 << 18)
2706 +#define kMatchArraySize (1 << 21)
2707 +#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
2708 +
2709 +#define kNumMaxDirectBits (31)
2710 +
2711 +#define kNumTopBits 24
2712 +#define kTopValue ((UInt32)1 << kNumTopBits)
2713 +
2714 +#define kNumBitModelTotalBits 11
2715 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
2716 +#define kNumMoveBits 5
2717 +#define kProbInitValue (kBitModelTotal >> 1)
2718 +
2719 +#define kNumMoveReducingBits 4
2720 +#define kNumBitPriceShiftBits 4
2721 +#define kBitPrice (1 << kNumBitPriceShiftBits)
2722 +
2723 +void LzmaEncProps_Init(CLzmaEncProps *p)
2724 +{
2725 + p->level = 5;
2726 + p->dictSize = p->mc = 0;
2727 + p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
2728 + p->writeEndMark = 0;
2729 +}
2730 +
2731 +void LzmaEncProps_Normalize(CLzmaEncProps *p)
2732 +{
2733 + int level = p->level;
2734 + if (level < 0) level = 5;
2735 + p->level = level;
2736 + if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
2737 + if (p->lc < 0) p->lc = 3;
2738 + if (p->lp < 0) p->lp = 0;
2739 + if (p->pb < 0) p->pb = 2;
2740 + if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
2741 + if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
2742 + if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
2743 + if (p->numHashBytes < 0) p->numHashBytes = 4;
2744 + if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
2745 + if (p->numThreads < 0) p->numThreads = ((p->btMode && p->algo) ? 2 : 1);
2746 +}
2747 +
2748 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
2749 +{
2750 + CLzmaEncProps props = *props2;
2751 + LzmaEncProps_Normalize(&props);
2752 + return props.dictSize;
2753 +}
2754 +
2755 +/* #define LZMA_LOG_BSR */
2756 +/* Define it for Intel's CPU */
2757 +
2758 +
2759 +#ifdef LZMA_LOG_BSR
2760 +
2761 +#define kDicLogSizeMaxCompress 30
2762 +
2763 +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
2764 +
2765 +UInt32 GetPosSlot1(UInt32 pos)
2766 +{
2767 + UInt32 res;
2768 + BSR2_RET(pos, res);
2769 + return res;
2770 +}
2771 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
2772 +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
2773 +
2774 +#else
2775 +
2776 +#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
2777 +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
2778 +
2779 +static void LzmaEnc_FastPosInit(Byte *g_FastPos)
2780 +{
2781 + int c = 2, slotFast;
2782 + g_FastPos[0] = 0;
2783 + g_FastPos[1] = 1;
2784 +
2785 + for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
2786 + {
2787 + UInt32 k = (1 << ((slotFast >> 1) - 1));
2788 + UInt32 j;
2789 + for (j = 0; j < k; j++, c++)
2790 + g_FastPos[c] = (Byte)slotFast;
2791 + }
2792 +}
2793 +
2794 +#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
2795 + (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
2796 + res = p->g_FastPos[pos >> i] + (i * 2); }
2797 +/*
2798 +#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
2799 + p->g_FastPos[pos >> 6] + 12 : \
2800 + p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
2801 +*/
2802 +
2803 +#define GetPosSlot1(pos) p->g_FastPos[pos]
2804 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
2805 +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
2806 +
2807 +#endif
2808 +
2809 +
2810 +#define LZMA_NUM_REPS 4
2811 +
2812 +typedef unsigned CState;
2813 +
2814 +typedef struct _COptimal
2815 +{
2816 + UInt32 price;
2817 +
2818 + CState state;
2819 + int prev1IsChar;
2820 + int prev2;
2821 +
2822 + UInt32 posPrev2;
2823 + UInt32 backPrev2;
2824 +
2825 + UInt32 posPrev;
2826 + UInt32 backPrev;
2827 + UInt32 backs[LZMA_NUM_REPS];
2828 +} COptimal;
2829 +
2830 +#define kNumOpts (1 << 12)
2831 +
2832 +#define kNumLenToPosStates 4
2833 +#define kNumPosSlotBits 6
2834 +#define kDicLogSizeMin 0
2835 +#define kDicLogSizeMax 32
2836 +#define kDistTableSizeMax (kDicLogSizeMax * 2)
2837 +
2838 +
2839 +#define kNumAlignBits 4
2840 +#define kAlignTableSize (1 << kNumAlignBits)
2841 +#define kAlignMask (kAlignTableSize - 1)
2842 +
2843 +#define kStartPosModelIndex 4
2844 +#define kEndPosModelIndex 14
2845 +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
2846 +
2847 +#define kNumFullDistances (1 << (kEndPosModelIndex / 2))
2848 +
2849 +#ifdef _LZMA_PROB32
2850 +#define CLzmaProb UInt32
2851 +#else
2852 +#define CLzmaProb UInt16
2853 +#endif
2854 +
2855 +#define LZMA_PB_MAX 4
2856 +#define LZMA_LC_MAX 8
2857 +#define LZMA_LP_MAX 4
2858 +
2859 +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
2860 +
2861 +
2862 +#define kLenNumLowBits 3
2863 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
2864 +#define kLenNumMidBits 3
2865 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
2866 +#define kLenNumHighBits 8
2867 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
2868 +
2869 +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
2870 +
2871 +#define LZMA_MATCH_LEN_MIN 2
2872 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
2873 +
2874 +#define kNumStates 12
2875 +
2876 +typedef struct
2877 +{
2878 + CLzmaProb choice;
2879 + CLzmaProb choice2;
2880 + CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
2881 + CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
2882 + CLzmaProb high[kLenNumHighSymbols];
2883 +} CLenEnc;
2884 +
2885 +typedef struct
2886 +{
2887 + CLenEnc p;
2888 + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
2889 + UInt32 tableSize;
2890 + UInt32 counters[LZMA_NUM_PB_STATES_MAX];
2891 +} CLenPriceEnc;
2892 +
2893 +typedef struct _CRangeEnc
2894 +{
2895 + UInt32 range;
2896 + Byte cache;
2897 + UInt64 low;
2898 + UInt64 cacheSize;
2899 + Byte *buf;
2900 + Byte *bufLim;
2901 + Byte *bufBase;
2902 + ISeqOutStream *outStream;
2903 + UInt64 processed;
2904 + SRes res;
2905 +} CRangeEnc;
2906 +
2907 +typedef struct _CSeqInStreamBuf
2908 +{
2909 + ISeqInStream funcTable;
2910 + const Byte *data;
2911 + SizeT rem;
2912 +} CSeqInStreamBuf;
2913 +
2914 +static SRes MyRead(void *pp, void *data, size_t *size)
2915 +{
2916 + size_t curSize = *size;
2917 + CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp;
2918 + if (p->rem < curSize)
2919 + curSize = p->rem;
2920 + memcpy(data, p->data, curSize);
2921 + p->rem -= curSize;
2922 + p->data += curSize;
2923 + *size = curSize;
2924 + return SZ_OK;
2925 +}
2926 +
2927 +typedef struct
2928 +{
2929 + CLzmaProb *litProbs;
2930 +
2931 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
2932 + CLzmaProb isRep[kNumStates];
2933 + CLzmaProb isRepG0[kNumStates];
2934 + CLzmaProb isRepG1[kNumStates];
2935 + CLzmaProb isRepG2[kNumStates];
2936 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
2937 +
2938 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
2939 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
2940 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
2941 +
2942 + CLenPriceEnc lenEnc;
2943 + CLenPriceEnc repLenEnc;
2944 +
2945 + UInt32 reps[LZMA_NUM_REPS];
2946 + UInt32 state;
2947 +} CSaveState;
2948 +
2949 +typedef struct _CLzmaEnc
2950 +{
2951 + IMatchFinder matchFinder;
2952 + void *matchFinderObj;
2953 +
2954 + #ifdef COMPRESS_MF_MT
2955 + Bool mtMode;
2956 + CMatchFinderMt matchFinderMt;
2957 + #endif
2958 +
2959 + CMatchFinder matchFinderBase;
2960 +
2961 + #ifdef COMPRESS_MF_MT
2962 + Byte pad[128];
2963 + #endif
2964 +
2965 + UInt32 optimumEndIndex;
2966 + UInt32 optimumCurrentIndex;
2967 +
2968 + Bool longestMatchWasFound;
2969 + UInt32 longestMatchLength;
2970 + UInt32 numDistancePairs;
2971 +
2972 + COptimal opt[kNumOpts];
2973 +
2974 + #ifndef LZMA_LOG_BSR
2975 + Byte g_FastPos[1 << kNumLogBits];
2976 + #endif
2977 +
2978 + UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
2979 + UInt32 matchDistances[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
2980 + UInt32 numFastBytes;
2981 + UInt32 additionalOffset;
2982 + UInt32 reps[LZMA_NUM_REPS];
2983 + UInt32 state;
2984 +
2985 + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
2986 + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
2987 + UInt32 alignPrices[kAlignTableSize];
2988 + UInt32 alignPriceCount;
2989 +
2990 + UInt32 distTableSize;
2991 +
2992 + unsigned lc, lp, pb;
2993 + unsigned lpMask, pbMask;
2994 +
2995 + CLzmaProb *litProbs;
2996 +
2997 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
2998 + CLzmaProb isRep[kNumStates];
2999 + CLzmaProb isRepG0[kNumStates];
3000 + CLzmaProb isRepG1[kNumStates];
3001 + CLzmaProb isRepG2[kNumStates];
3002 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
3003 +
3004 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
3005 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
3006 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
3007 +
3008 + CLenPriceEnc lenEnc;
3009 + CLenPriceEnc repLenEnc;
3010 +
3011 + unsigned lclp;
3012 +
3013 + Bool fastMode;
3014 +
3015 + CRangeEnc rc;
3016 +
3017 + Bool writeEndMark;
3018 + UInt64 nowPos64;
3019 + UInt32 matchPriceCount;
3020 + Bool finished;
3021 + Bool multiThread;
3022 +
3023 + SRes result;
3024 + UInt32 dictSize;
3025 + UInt32 matchFinderCycles;
3026 +
3027 + ISeqInStream *inStream;
3028 + CSeqInStreamBuf seqBufInStream;
3029 +
3030 + CSaveState saveState;
3031 +} CLzmaEnc;
3032 +
3033 +static void LzmaEnc_SaveState(CLzmaEncHandle pp)
3034 +{
3035 + CLzmaEnc *p = (CLzmaEnc *)pp;
3036 + CSaveState *dest = &p->saveState;
3037 + int i;
3038 + dest->lenEnc = p->lenEnc;
3039 + dest->repLenEnc = p->repLenEnc;
3040 + dest->state = p->state;
3041 +
3042 + for (i = 0; i < kNumStates; i++)
3043 + {
3044 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
3045 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
3046 + }
3047 + for (i = 0; i < kNumLenToPosStates; i++)
3048 + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
3049 + memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
3050 + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
3051 + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
3052 + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
3053 + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
3054 + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
3055 + memcpy(dest->reps, p->reps, sizeof(p->reps));
3056 + memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
3057 +}
3058 +
3059 +static void LzmaEnc_RestoreState(CLzmaEncHandle pp)
3060 +{
3061 + CLzmaEnc *dest = (CLzmaEnc *)pp;
3062 + const CSaveState *p = &dest->saveState;
3063 + int i;
3064 + dest->lenEnc = p->lenEnc;
3065 + dest->repLenEnc = p->repLenEnc;
3066 + dest->state = p->state;
3067 +
3068 + for (i = 0; i < kNumStates; i++)
3069 + {
3070 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
3071 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
3072 + }
3073 + for (i = 0; i < kNumLenToPosStates; i++)
3074 + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
3075 + memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
3076 + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
3077 + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
3078 + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
3079 + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
3080 + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
3081 + memcpy(dest->reps, p->reps, sizeof(p->reps));
3082 + memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
3083 +}
3084 +
3085 +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
3086 +{
3087 + CLzmaEnc *p = (CLzmaEnc *)pp;
3088 + CLzmaEncProps props = *props2;
3089 + LzmaEncProps_Normalize(&props);
3090 +
3091 + if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
3092 + props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))
3093 + return SZ_ERROR_PARAM;
3094 + p->dictSize = props.dictSize;
3095 + p->matchFinderCycles = props.mc;
3096 + {
3097 + unsigned fb = props.fb;
3098 + if (fb < 5)
3099 + fb = 5;
3100 + if (fb > LZMA_MATCH_LEN_MAX)
3101 + fb = LZMA_MATCH_LEN_MAX;
3102 + p->numFastBytes = fb;
3103 + }
3104 + p->lc = props.lc;
3105 + p->lp = props.lp;
3106 + p->pb = props.pb;
3107 + p->fastMode = (props.algo == 0);
3108 + p->matchFinderBase.btMode = props.btMode;
3109 + {
3110 + UInt32 numHashBytes = 4;
3111 + if (props.btMode)
3112 + {
3113 + if (props.numHashBytes < 2)
3114 + numHashBytes = 2;
3115 + else if (props.numHashBytes < 4)
3116 + numHashBytes = props.numHashBytes;
3117 + }
3118 + p->matchFinderBase.numHashBytes = numHashBytes;
3119 + }
3120 +
3121 + p->matchFinderBase.cutValue = props.mc;
3122 +
3123 + p->writeEndMark = props.writeEndMark;
3124 +
3125 + #ifdef COMPRESS_MF_MT
3126 + /*
3127 + if (newMultiThread != _multiThread)
3128 + {
3129 + ReleaseMatchFinder();
3130 + _multiThread = newMultiThread;
3131 + }
3132 + */
3133 + p->multiThread = (props.numThreads > 1);
3134 + #endif
3135 +
3136 + return SZ_OK;
3137 +}
3138 +
3139 +static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
3140 +static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
3141 +static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
3142 +static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
3143 +
3144 +/*
3145 + void UpdateChar() { Index = kLiteralNextStates[Index]; }
3146 + void UpdateMatch() { Index = kMatchNextStates[Index]; }
3147 + void UpdateRep() { Index = kRepNextStates[Index]; }
3148 + void UpdateShortRep() { Index = kShortRepNextStates[Index]; }
3149 +*/
3150 +
3151 +#define IsCharState(s) ((s) < 7)
3152 +
3153 +
3154 +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
3155 +
3156 +#define kInfinityPrice (1 << 30)
3157 +
3158 +static void RangeEnc_Construct(CRangeEnc *p)
3159 +{
3160 + p->outStream = 0;
3161 + p->bufBase = 0;
3162 +}
3163 +
3164 +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
3165 +
3166 +#define RC_BUF_SIZE (1 << 16)
3167 +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
3168 +{
3169 + if (p->bufBase == 0)
3170 + {
3171 + p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
3172 + if (p->bufBase == 0)
3173 + return 0;
3174 + p->bufLim = p->bufBase + RC_BUF_SIZE;
3175 + }
3176 + return 1;
3177 +}
3178 +
3179 +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
3180 +{
3181 + alloc->Free(alloc, p->bufBase);
3182 + p->bufBase = 0;
3183 +}
3184 +
3185 +static void RangeEnc_Init(CRangeEnc *p)
3186 +{
3187 + /* Stream.Init(); */
3188 + p->low = 0;
3189 + p->range = 0xFFFFFFFF;
3190 + p->cacheSize = 1;
3191 + p->cache = 0;
3192 +
3193 + p->buf = p->bufBase;
3194 +
3195 + p->processed = 0;
3196 + p->res = SZ_OK;
3197 +}
3198 +
3199 +static void RangeEnc_FlushStream(CRangeEnc *p)
3200 +{
3201 + size_t num;
3202 + if (p->res != SZ_OK)
3203 + return;
3204 + num = p->buf - p->bufBase;
3205 + if (num != p->outStream->Write(p->outStream, p->bufBase, num))
3206 + p->res = SZ_ERROR_WRITE;
3207 + p->processed += num;
3208 + p->buf = p->bufBase;
3209 +}
3210 +
3211 +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
3212 +{
3213 + if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
3214 + {
3215 + Byte temp = p->cache;
3216 + do
3217 + {
3218 + Byte *buf = p->buf;
3219 + *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
3220 + p->buf = buf;
3221 + if (buf == p->bufLim)
3222 + RangeEnc_FlushStream(p);
3223 + temp = 0xFF;
3224 + }
3225 + while (--p->cacheSize != 0);
3226 + p->cache = (Byte)((UInt32)p->low >> 24);
3227 + }
3228 + p->cacheSize++;
3229 + p->low = (UInt32)p->low << 8;
3230 +}
3231 +
3232 +static void RangeEnc_FlushData(CRangeEnc *p)
3233 +{
3234 + int i;
3235 + for (i = 0; i < 5; i++)
3236 + RangeEnc_ShiftLow(p);
3237 +}
3238 +
3239 +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
3240 +{
3241 + do
3242 + {
3243 + p->range >>= 1;
3244 + p->low += p->range & (0 - ((value >> --numBits) & 1));
3245 + if (p->range < kTopValue)
3246 + {
3247 + p->range <<= 8;
3248 + RangeEnc_ShiftLow(p);
3249 + }
3250 + }
3251 + while (numBits != 0);
3252 +}
3253 +
3254 +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
3255 +{
3256 + UInt32 ttt = *prob;
3257 + UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
3258 + if (symbol == 0)
3259 + {
3260 + p->range = newBound;
3261 + ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
3262 + }
3263 + else
3264 + {
3265 + p->low += newBound;
3266 + p->range -= newBound;
3267 + ttt -= ttt >> kNumMoveBits;
3268 + }
3269 + *prob = (CLzmaProb)ttt;
3270 + if (p->range < kTopValue)
3271 + {
3272 + p->range <<= 8;
3273 + RangeEnc_ShiftLow(p);
3274 + }
3275 +}
3276 +
3277 +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
3278 +{
3279 + symbol |= 0x100;
3280 + do
3281 + {
3282 + RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
3283 + symbol <<= 1;
3284 + }
3285 + while (symbol < 0x10000);
3286 +}
3287 +
3288 +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
3289 +{
3290 + UInt32 offs = 0x100;
3291 + symbol |= 0x100;
3292 + do
3293 + {
3294 + matchByte <<= 1;
3295 + RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
3296 + symbol <<= 1;
3297 + offs &= ~(matchByte ^ symbol);
3298 + }
3299 + while (symbol < 0x10000);
3300 +}
3301 +
3302 +static void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
3303 +{
3304 + UInt32 i;
3305 + for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
3306 + {
3307 + const int kCyclesBits = kNumBitPriceShiftBits;
3308 + UInt32 w = i;
3309 + UInt32 bitCount = 0;
3310 + int j;
3311 + for (j = 0; j < kCyclesBits; j++)
3312 + {
3313 + w = w * w;
3314 + bitCount <<= 1;
3315 + while (w >= ((UInt32)1 << 16))
3316 + {
3317 + w >>= 1;
3318 + bitCount++;
3319 + }
3320 + }
3321 + ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
3322 + }
3323 +}
3324 +
3325 +
3326 +#define GET_PRICE(prob, symbol) \
3327 + p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3328 +
3329 +#define GET_PRICEa(prob, symbol) \
3330 + ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3331 +
3332 +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
3333 +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3334 +
3335 +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
3336 +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3337 +
3338 +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
3339 +{
3340 + UInt32 price = 0;
3341 + symbol |= 0x100;
3342 + do
3343 + {
3344 + price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
3345 + symbol <<= 1;
3346 + }
3347 + while (symbol < 0x10000);
3348 + return price;
3349 +};
3350 +
3351 +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
3352 +{
3353 + UInt32 price = 0;
3354 + UInt32 offs = 0x100;
3355 + symbol |= 0x100;
3356 + do
3357 + {
3358 + matchByte <<= 1;
3359 + price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
3360 + symbol <<= 1;
3361 + offs &= ~(matchByte ^ symbol);
3362 + }
3363 + while (symbol < 0x10000);
3364 + return price;
3365 +};
3366 +
3367 +
3368 +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3369 +{
3370 + UInt32 m = 1;
3371 + int i;
3372 + for (i = numBitLevels; i != 0 ;)
3373 + {
3374 + UInt32 bit;
3375 + i--;
3376 + bit = (symbol >> i) & 1;
3377 + RangeEnc_EncodeBit(rc, probs + m, bit);
3378 + m = (m << 1) | bit;
3379 + }
3380 +};
3381 +
3382 +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3383 +{
3384 + UInt32 m = 1;
3385 + int i;
3386 + for (i = 0; i < numBitLevels; i++)
3387 + {
3388 + UInt32 bit = symbol & 1;
3389 + RangeEnc_EncodeBit(rc, probs + m, bit);
3390 + m = (m << 1) | bit;
3391 + symbol >>= 1;
3392 + }
3393 +}
3394 +
3395 +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3396 +{
3397 + UInt32 price = 0;
3398 + symbol |= (1 << numBitLevels);
3399 + while (symbol != 1)
3400 + {
3401 + price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
3402 + symbol >>= 1;
3403 + }
3404 + return price;
3405 +}
3406 +
3407 +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3408 +{
3409 + UInt32 price = 0;
3410 + UInt32 m = 1;
3411 + int i;
3412 + for (i = numBitLevels; i != 0; i--)
3413 + {
3414 + UInt32 bit = symbol & 1;
3415 + symbol >>= 1;
3416 + price += GET_PRICEa(probs[m], bit);
3417 + m = (m << 1) | bit;
3418 + }
3419 + return price;
3420 +}
3421 +
3422 +
3423 +static void LenEnc_Init(CLenEnc *p)
3424 +{
3425 + unsigned i;
3426 + p->choice = p->choice2 = kProbInitValue;
3427 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
3428 + p->low[i] = kProbInitValue;
3429 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
3430 + p->mid[i] = kProbInitValue;
3431 + for (i = 0; i < kLenNumHighSymbols; i++)
3432 + p->high[i] = kProbInitValue;
3433 +}
3434 +
3435 +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
3436 +{
3437 + if (symbol < kLenNumLowSymbols)
3438 + {
3439 + RangeEnc_EncodeBit(rc, &p->choice, 0);
3440 + RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
3441 + }
3442 + else
3443 + {
3444 + RangeEnc_EncodeBit(rc, &p->choice, 1);
3445 + if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
3446 + {
3447 + RangeEnc_EncodeBit(rc, &p->choice2, 0);
3448 + RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
3449 + }
3450 + else
3451 + {
3452 + RangeEnc_EncodeBit(rc, &p->choice2, 1);
3453 + RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
3454 + }
3455 + }
3456 +}
3457 +
3458 +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
3459 +{
3460 + UInt32 a0 = GET_PRICE_0a(p->choice);
3461 + UInt32 a1 = GET_PRICE_1a(p->choice);
3462 + UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
3463 + UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
3464 + UInt32 i = 0;
3465 + for (i = 0; i < kLenNumLowSymbols; i++)
3466 + {
3467 + if (i >= numSymbols)
3468 + return;
3469 + prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
3470 + }
3471 + for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
3472 + {
3473 + if (i >= numSymbols)
3474 + return;
3475 + prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
3476 + }
3477 + for (; i < numSymbols; i++)
3478 + prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
3479 +}
3480 +
3481 +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
3482 +{
3483 + LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
3484 + p->counters[posState] = p->tableSize;
3485 +}
3486 +
3487 +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
3488 +{
3489 + UInt32 posState;
3490 + for (posState = 0; posState < numPosStates; posState++)
3491 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3492 +}
3493 +
3494 +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
3495 +{
3496 + LenEnc_Encode(&p->p, rc, symbol, posState);
3497 + if (updatePrice)
3498 + if (--p->counters[posState] == 0)
3499 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3500 +}
3501 +
3502 +
3503 +
3504 +
3505 +static void MovePos(CLzmaEnc *p, UInt32 num)
3506 +{
3507 + #ifdef SHOW_STAT
3508 + ttt += num;
3509 + printf("\n MovePos %d", num);
3510 + #endif
3511 + if (num != 0)
3512 + {
3513 + p->additionalOffset += num;
3514 + p->matchFinder.Skip(p->matchFinderObj, num);
3515 + }
3516 +}
3517 +
3518 +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
3519 +{
3520 + UInt32 lenRes = 0, numDistancePairs;
3521 + numDistancePairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matchDistances);
3522 + #ifdef SHOW_STAT
3523 + printf("\n i = %d numPairs = %d ", ttt, numDistancePairs / 2);
3524 + if (ttt >= 61994)
3525 + ttt = ttt;
3526 +
3527 + ttt++;
3528 + {
3529 + UInt32 i;
3530 + for (i = 0; i < numDistancePairs; i += 2)
3531 + printf("%2d %6d | ", p->matchDistances[i], p->matchDistances[i + 1]);
3532 + }
3533 + #endif
3534 + if (numDistancePairs > 0)
3535 + {
3536 + lenRes = p->matchDistances[numDistancePairs - 2];
3537 + if (lenRes == p->numFastBytes)
3538 + {
3539 + UInt32 numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) + 1;
3540 + const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3541 + UInt32 distance = p->matchDistances[numDistancePairs - 1] + 1;
3542 + if (numAvail > LZMA_MATCH_LEN_MAX)
3543 + numAvail = LZMA_MATCH_LEN_MAX;
3544 +
3545 + {
3546 + const Byte *pby2 = pby - distance;
3547 + for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
3548 + }
3549 + }
3550 + }
3551 + p->additionalOffset++;
3552 + *numDistancePairsRes = numDistancePairs;
3553 + return lenRes;
3554 +}
3555 +
3556 +
3557 +#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
3558 +#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
3559 +#define IsShortRep(p) ((p)->backPrev == 0)
3560 +
3561 +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
3562 +{
3563 + return
3564 + GET_PRICE_0(p->isRepG0[state]) +
3565 + GET_PRICE_0(p->isRep0Long[state][posState]);
3566 +}
3567 +
3568 +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
3569 +{
3570 + UInt32 price;
3571 + if (repIndex == 0)
3572 + {
3573 + price = GET_PRICE_0(p->isRepG0[state]);
3574 + price += GET_PRICE_1(p->isRep0Long[state][posState]);
3575 + }
3576 + else
3577 + {
3578 + price = GET_PRICE_1(p->isRepG0[state]);
3579 + if (repIndex == 1)
3580 + price += GET_PRICE_0(p->isRepG1[state]);
3581 + else
3582 + {
3583 + price += GET_PRICE_1(p->isRepG1[state]);
3584 + price += GET_PRICE(p->isRepG2[state], repIndex - 2);
3585 + }
3586 + }
3587 + return price;
3588 +}
3589 +
3590 +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
3591 +{
3592 + return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
3593 + GetPureRepPrice(p, repIndex, state, posState);
3594 +}
3595 +
3596 +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
3597 +{
3598 + UInt32 posMem = p->opt[cur].posPrev;
3599 + UInt32 backMem = p->opt[cur].backPrev;
3600 + p->optimumEndIndex = cur;
3601 + do
3602 + {
3603 + if (p->opt[cur].prev1IsChar)
3604 + {
3605 + MakeAsChar(&p->opt[posMem])
3606 + p->opt[posMem].posPrev = posMem - 1;
3607 + if (p->opt[cur].prev2)
3608 + {
3609 + p->opt[posMem - 1].prev1IsChar = False;
3610 + p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
3611 + p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
3612 + }
3613 + }
3614 + {
3615 + UInt32 posPrev = posMem;
3616 + UInt32 backCur = backMem;
3617 +
3618 + backMem = p->opt[posPrev].backPrev;
3619 + posMem = p->opt[posPrev].posPrev;
3620 +
3621 + p->opt[posPrev].backPrev = backCur;
3622 + p->opt[posPrev].posPrev = cur;
3623 + cur = posPrev;
3624 + }
3625 + }
3626 + while (cur != 0);
3627 + *backRes = p->opt[0].backPrev;
3628 + p->optimumCurrentIndex = p->opt[0].posPrev;
3629 + return p->optimumCurrentIndex;
3630 +}
3631 +
3632 +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
3633 +
3634 +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
3635 +{
3636 + UInt32 numAvailableBytes, lenMain, numDistancePairs;
3637 + const Byte *data;
3638 + UInt32 reps[LZMA_NUM_REPS];
3639 + UInt32 repLens[LZMA_NUM_REPS];
3640 + UInt32 repMaxIndex, i;
3641 + UInt32 *matchDistances;
3642 + Byte currentByte, matchByte;
3643 + UInt32 posState;
3644 + UInt32 matchPrice, repMatchPrice;
3645 + UInt32 lenEnd;
3646 + UInt32 len;
3647 + UInt32 normalMatchPrice;
3648 + UInt32 cur;
3649 + if (p->optimumEndIndex != p->optimumCurrentIndex)
3650 + {
3651 + const COptimal *opt = &p->opt[p->optimumCurrentIndex];
3652 + UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
3653 + *backRes = opt->backPrev;
3654 + p->optimumCurrentIndex = opt->posPrev;
3655 + return lenRes;
3656 + }
3657 + p->optimumCurrentIndex = p->optimumEndIndex = 0;
3658 +
3659 + numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
3660 +
3661 + if (!p->longestMatchWasFound)
3662 + {
3663 + lenMain = ReadMatchDistances(p, &numDistancePairs);
3664 + }
3665 + else
3666 + {
3667 + lenMain = p->longestMatchLength;
3668 + numDistancePairs = p->numDistancePairs;
3669 + p->longestMatchWasFound = False;
3670 + }
3671 +
3672 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3673 + if (numAvailableBytes < 2)
3674 + {
3675 + *backRes = (UInt32)(-1);
3676 + return 1;
3677 + }
3678 + if (numAvailableBytes > LZMA_MATCH_LEN_MAX)
3679 + numAvailableBytes = LZMA_MATCH_LEN_MAX;
3680 +
3681 + repMaxIndex = 0;
3682 + for (i = 0; i < LZMA_NUM_REPS; i++)
3683 + {
3684 + UInt32 lenTest;
3685 + const Byte *data2;
3686 + reps[i] = p->reps[i];
3687 + data2 = data - (reps[i] + 1);
3688 + if (data[0] != data2[0] || data[1] != data2[1])
3689 + {
3690 + repLens[i] = 0;
3691 + continue;
3692 + }
3693 + for (lenTest = 2; lenTest < numAvailableBytes && data[lenTest] == data2[lenTest]; lenTest++);
3694 + repLens[i] = lenTest;
3695 + if (lenTest > repLens[repMaxIndex])
3696 + repMaxIndex = i;
3697 + }
3698 + if (repLens[repMaxIndex] >= p->numFastBytes)
3699 + {
3700 + UInt32 lenRes;
3701 + *backRes = repMaxIndex;
3702 + lenRes = repLens[repMaxIndex];
3703 + MovePos(p, lenRes - 1);
3704 + return lenRes;
3705 + }
3706 +
3707 + matchDistances = p->matchDistances;
3708 + if (lenMain >= p->numFastBytes)
3709 + {
3710 + *backRes = matchDistances[numDistancePairs - 1] + LZMA_NUM_REPS;
3711 + MovePos(p, lenMain - 1);
3712 + return lenMain;
3713 + }
3714 + currentByte = *data;
3715 + matchByte = *(data - (reps[0] + 1));
3716 +
3717 + if (lenMain < 2 && currentByte != matchByte && repLens[repMaxIndex] < 2)
3718 + {
3719 + *backRes = (UInt32)-1;
3720 + return 1;
3721 + }
3722 +
3723 + p->opt[0].state = (CState)p->state;
3724 +
3725 + posState = (position & p->pbMask);
3726 +
3727 + {
3728 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
3729 + p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
3730 + (!IsCharState(p->state) ?
3731 + LitEnc_GetPriceMatched(probs, currentByte, matchByte, p->ProbPrices) :
3732 + LitEnc_GetPrice(probs, currentByte, p->ProbPrices));
3733 + }
3734 +
3735 + MakeAsChar(&p->opt[1]);
3736 +
3737 + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
3738 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
3739 +
3740 + if (matchByte == currentByte)
3741 + {
3742 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
3743 + if (shortRepPrice < p->opt[1].price)
3744 + {
3745 + p->opt[1].price = shortRepPrice;
3746 + MakeAsShortRep(&p->opt[1]);
3747 + }
3748 + }
3749 + lenEnd = ((lenMain >= repLens[repMaxIndex]) ? lenMain : repLens[repMaxIndex]);
3750 +
3751 + if (lenEnd < 2)
3752 + {
3753 + *backRes = p->opt[1].backPrev;
3754 + return 1;
3755 + }
3756 +
3757 + p->opt[1].posPrev = 0;
3758 + for (i = 0; i < LZMA_NUM_REPS; i++)
3759 + p->opt[0].backs[i] = reps[i];
3760 +
3761 + len = lenEnd;
3762 + do
3763 + p->opt[len--].price = kInfinityPrice;
3764 + while (len >= 2);
3765 +
3766 + for (i = 0; i < LZMA_NUM_REPS; i++)
3767 + {
3768 + UInt32 repLen = repLens[i];
3769 + UInt32 price;
3770 + if (repLen < 2)
3771 + continue;
3772 + price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
3773 + do
3774 + {
3775 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
3776 + COptimal *opt = &p->opt[repLen];
3777 + if (curAndLenPrice < opt->price)
3778 + {
3779 + opt->price = curAndLenPrice;
3780 + opt->posPrev = 0;
3781 + opt->backPrev = i;
3782 + opt->prev1IsChar = False;
3783 + }
3784 + }
3785 + while (--repLen >= 2);
3786 + }
3787 +
3788 + normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
3789 +
3790 + len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
3791 + if (len <= lenMain)
3792 + {
3793 + UInt32 offs = 0;
3794 + while (len > matchDistances[offs])
3795 + offs += 2;
3796 + for (; ; len++)
3797 + {
3798 + COptimal *opt;
3799 + UInt32 distance = matchDistances[offs + 1];
3800 +
3801 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
3802 + UInt32 lenToPosState = GetLenToPosState(len);
3803 + if (distance < kNumFullDistances)
3804 + curAndLenPrice += p->distancesPrices[lenToPosState][distance];
3805 + else
3806 + {
3807 + UInt32 slot;
3808 + GetPosSlot2(distance, slot);
3809 + curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
3810 + }
3811 + opt = &p->opt[len];
3812 + if (curAndLenPrice < opt->price)
3813 + {
3814 + opt->price = curAndLenPrice;
3815 + opt->posPrev = 0;
3816 + opt->backPrev = distance + LZMA_NUM_REPS;
3817 + opt->prev1IsChar = False;
3818 + }
3819 + if (len == matchDistances[offs])
3820 + {
3821 + offs += 2;
3822 + if (offs == numDistancePairs)
3823 + break;
3824 + }
3825 + }
3826 + }
3827 +
3828 + cur = 0;
3829 +
3830 + #ifdef SHOW_STAT2
3831 + if (position >= 0)
3832 + {
3833 + unsigned i;
3834 + printf("\n pos = %4X", position);
3835 + for (i = cur; i <= lenEnd; i++)
3836 + printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
3837 + }
3838 + #endif
3839 +
3840 + for (;;)
3841 + {
3842 + UInt32 numAvailableBytesFull, newLen, numDistancePairs;
3843 + COptimal *curOpt;
3844 + UInt32 posPrev;
3845 + UInt32 state;
3846 + UInt32 curPrice;
3847 + Bool nextIsChar;
3848 + const Byte *data;
3849 + Byte currentByte, matchByte;
3850 + UInt32 posState;
3851 + UInt32 curAnd1Price;
3852 + COptimal *nextOpt;
3853 + UInt32 matchPrice, repMatchPrice;
3854 + UInt32 numAvailableBytes;
3855 + UInt32 startLen;
3856 +
3857 + cur++;
3858 + if (cur == lenEnd)
3859 + return Backward(p, backRes, cur);
3860 +
3861 + numAvailableBytesFull = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
3862 + newLen = ReadMatchDistances(p, &numDistancePairs);
3863 + if (newLen >= p->numFastBytes)
3864 + {
3865 + p->numDistancePairs = numDistancePairs;
3866 + p->longestMatchLength = newLen;
3867 + p->longestMatchWasFound = True;
3868 + return Backward(p, backRes, cur);
3869 + }
3870 + position++;
3871 + curOpt = &p->opt[cur];
3872 + posPrev = curOpt->posPrev;
3873 + if (curOpt->prev1IsChar)
3874 + {
3875 + posPrev--;
3876 + if (curOpt->prev2)
3877 + {
3878 + state = p->opt[curOpt->posPrev2].state;
3879 + if (curOpt->backPrev2 < LZMA_NUM_REPS)
3880 + state = kRepNextStates[state];
3881 + else
3882 + state = kMatchNextStates[state];
3883 + }
3884 + else
3885 + state = p->opt[posPrev].state;
3886 + state = kLiteralNextStates[state];
3887 + }
3888 + else
3889 + state = p->opt[posPrev].state;
3890 + if (posPrev == cur - 1)
3891 + {
3892 + if (IsShortRep(curOpt))
3893 + state = kShortRepNextStates[state];
3894 + else
3895 + state = kLiteralNextStates[state];
3896 + }
3897 + else
3898 + {
3899 + UInt32 pos;
3900 + const COptimal *prevOpt;
3901 + if (curOpt->prev1IsChar && curOpt->prev2)
3902 + {
3903 + posPrev = curOpt->posPrev2;
3904 + pos = curOpt->backPrev2;
3905 + state = kRepNextStates[state];
3906 + }
3907 + else
3908 + {
3909 + pos = curOpt->backPrev;
3910 + if (pos < LZMA_NUM_REPS)
3911 + state = kRepNextStates[state];
3912 + else
3913 + state = kMatchNextStates[state];
3914 + }
3915 + prevOpt = &p->opt[posPrev];
3916 + if (pos < LZMA_NUM_REPS)
3917 + {
3918 + UInt32 i;
3919 + reps[0] = prevOpt->backs[pos];
3920 + for (i = 1; i <= pos; i++)
3921 + reps[i] = prevOpt->backs[i - 1];
3922 + for (; i < LZMA_NUM_REPS; i++)
3923 + reps[i] = prevOpt->backs[i];
3924 + }
3925 + else
3926 + {
3927 + UInt32 i;
3928 + reps[0] = (pos - LZMA_NUM_REPS);
3929 + for (i = 1; i < LZMA_NUM_REPS; i++)
3930 + reps[i] = prevOpt->backs[i - 1];
3931 + }
3932 + }
3933 + curOpt->state = (CState)state;
3934 +
3935 + curOpt->backs[0] = reps[0];
3936 + curOpt->backs[1] = reps[1];
3937 + curOpt->backs[2] = reps[2];
3938 + curOpt->backs[3] = reps[3];
3939 +
3940 + curPrice = curOpt->price;
3941 + nextIsChar = False;
3942 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3943 + currentByte = *data;
3944 + matchByte = *(data - (reps[0] + 1));
3945 +
3946 + posState = (position & p->pbMask);
3947 +
3948 + curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
3949 + {
3950 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
3951 + curAnd1Price +=
3952 + (!IsCharState(state) ?
3953 + LitEnc_GetPriceMatched(probs, currentByte, matchByte, p->ProbPrices) :
3954 + LitEnc_GetPrice(probs, currentByte, p->ProbPrices));
3955 + }
3956 +
3957 + nextOpt = &p->opt[cur + 1];
3958 +
3959 + if (curAnd1Price < nextOpt->price)
3960 + {
3961 + nextOpt->price = curAnd1Price;
3962 + nextOpt->posPrev = cur;
3963 + MakeAsChar(nextOpt);
3964 + nextIsChar = True;
3965 + }
3966 +
3967 + matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
3968 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
3969 +
3970 + if (matchByte == currentByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
3971 + {
3972 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
3973 + if (shortRepPrice <= nextOpt->price)
3974 + {
3975 + nextOpt->price = shortRepPrice;
3976 + nextOpt->posPrev = cur;
3977 + MakeAsShortRep(nextOpt);
3978 + nextIsChar = True;
3979 + }
3980 + }
3981 +
3982 + {
3983 + UInt32 temp = kNumOpts - 1 - cur;
3984 + if (temp < numAvailableBytesFull)
3985 + numAvailableBytesFull = temp;
3986 + }
3987 + numAvailableBytes = numAvailableBytesFull;
3988 +
3989 + if (numAvailableBytes < 2)
3990 + continue;
3991 + if (numAvailableBytes > p->numFastBytes)
3992 + numAvailableBytes = p->numFastBytes;
3993 + if (!nextIsChar && matchByte != currentByte) /* speed optimization */
3994 + {
3995 + /* try Literal + rep0 */
3996 + UInt32 temp;
3997 + UInt32 lenTest2;
3998 + const Byte *data2 = data - (reps[0] + 1);
3999 + UInt32 limit = p->numFastBytes + 1;
4000 + if (limit > numAvailableBytesFull)
4001 + limit = numAvailableBytesFull;
4002 +
4003 + for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
4004 + lenTest2 = temp - 1;
4005 + if (lenTest2 >= 2)
4006 + {
4007 + UInt32 state2 = kLiteralNextStates[state];
4008 + UInt32 posStateNext = (position + 1) & p->pbMask;
4009 + UInt32 nextRepMatchPrice = curAnd1Price +
4010 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4011 + GET_PRICE_1(p->isRep[state2]);
4012 + /* for (; lenTest2 >= 2; lenTest2--) */
4013 + {
4014 + UInt32 curAndLenPrice;
4015 + COptimal *opt;
4016 + UInt32 offset = cur + 1 + lenTest2;
4017 + while (lenEnd < offset)
4018 + p->opt[++lenEnd].price = kInfinityPrice;
4019 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4020 + opt = &p->opt[offset];
4021 + if (curAndLenPrice < opt->price)
4022 + {
4023 + opt->price = curAndLenPrice;
4024 + opt->posPrev = cur + 1;
4025 + opt->backPrev = 0;
4026 + opt->prev1IsChar = True;
4027 + opt->prev2 = False;
4028 + }
4029 + }
4030 + }
4031 + }
4032 +
4033 + startLen = 2; /* speed optimization */
4034 + {
4035 + UInt32 repIndex;
4036 + for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
4037 + {
4038 + UInt32 lenTest;
4039 + UInt32 lenTestTemp;
4040 + UInt32 price;
4041 + const Byte *data2 = data - (reps[repIndex] + 1);
4042 + if (data[0] != data2[0] || data[1] != data2[1])
4043 + continue;
4044 + for (lenTest = 2; lenTest < numAvailableBytes && data[lenTest] == data2[lenTest]; lenTest++);
4045 + while (lenEnd < cur + lenTest)
4046 + p->opt[++lenEnd].price = kInfinityPrice;
4047 + lenTestTemp = lenTest;
4048 + price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
4049 + do
4050 + {
4051 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
4052 + COptimal *opt = &p->opt[cur + lenTest];
4053 + if (curAndLenPrice < opt->price)
4054 + {
4055 + opt->price = curAndLenPrice;
4056 + opt->posPrev = cur;
4057 + opt->backPrev = repIndex;
4058 + opt->prev1IsChar = False;
4059 + }
4060 + }
4061 + while (--lenTest >= 2);
4062 + lenTest = lenTestTemp;
4063 +
4064 + if (repIndex == 0)
4065 + startLen = lenTest + 1;
4066 +
4067 + /* if (_maxMode) */
4068 + {
4069 + UInt32 lenTest2 = lenTest + 1;
4070 + UInt32 limit = lenTest2 + p->numFastBytes;
4071 + UInt32 nextRepMatchPrice;
4072 + if (limit > numAvailableBytesFull)
4073 + limit = numAvailableBytesFull;
4074 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
4075 + lenTest2 -= lenTest + 1;
4076 + if (lenTest2 >= 2)
4077 + {
4078 + UInt32 state2 = kRepNextStates[state];
4079 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
4080 + UInt32 curAndLenCharPrice =
4081 + price + p->repLenEnc.prices[posState][lenTest - 2] +
4082 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
4083 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
4084 + data[lenTest], data2[lenTest], p->ProbPrices);
4085 + state2 = kLiteralNextStates[state2];
4086 + posStateNext = (position + lenTest + 1) & p->pbMask;
4087 + nextRepMatchPrice = curAndLenCharPrice +
4088 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4089 + GET_PRICE_1(p->isRep[state2]);
4090 +
4091 + /* for (; lenTest2 >= 2; lenTest2--) */
4092 + {
4093 + UInt32 curAndLenPrice;
4094 + COptimal *opt;
4095 + UInt32 offset = cur + lenTest + 1 + lenTest2;
4096 + while (lenEnd < offset)
4097 + p->opt[++lenEnd].price = kInfinityPrice;
4098 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4099 + opt = &p->opt[offset];
4100 + if (curAndLenPrice < opt->price)
4101 + {
4102 + opt->price = curAndLenPrice;
4103 + opt->posPrev = cur + lenTest + 1;
4104 + opt->backPrev = 0;
4105 + opt->prev1IsChar = True;
4106 + opt->prev2 = True;
4107 + opt->posPrev2 = cur;
4108 + opt->backPrev2 = repIndex;
4109 + }
4110 + }
4111 + }
4112 + }
4113 + }
4114 + }
4115 + /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
4116 + if (newLen > numAvailableBytes)
4117 + {
4118 + newLen = numAvailableBytes;
4119 + for (numDistancePairs = 0; newLen > matchDistances[numDistancePairs]; numDistancePairs += 2);
4120 + matchDistances[numDistancePairs] = newLen;
4121 + numDistancePairs += 2;
4122 + }
4123 + if (newLen >= startLen)
4124 + {
4125 + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
4126 + UInt32 offs, curBack, posSlot;
4127 + UInt32 lenTest;
4128 + while (lenEnd < cur + newLen)
4129 + p->opt[++lenEnd].price = kInfinityPrice;
4130 +
4131 + offs = 0;
4132 + while (startLen > matchDistances[offs])
4133 + offs += 2;
4134 + curBack = matchDistances[offs + 1];
4135 + GetPosSlot2(curBack, posSlot);
4136 + for (lenTest = /*2*/ startLen; ; lenTest++)
4137 + {
4138 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
4139 + UInt32 lenToPosState = GetLenToPosState(lenTest);
4140 + COptimal *opt;
4141 + if (curBack < kNumFullDistances)
4142 + curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
4143 + else
4144 + curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
4145 +
4146 + opt = &p->opt[cur + lenTest];
4147 + if (curAndLenPrice < opt->price)
4148 + {
4149 + opt->price = curAndLenPrice;
4150 + opt->posPrev = cur;
4151 + opt->backPrev = curBack + LZMA_NUM_REPS;
4152 + opt->prev1IsChar = False;
4153 + }
4154 +
4155 + if (/*_maxMode && */lenTest == matchDistances[offs])
4156 + {
4157 + /* Try Match + Literal + Rep0 */
4158 + const Byte *data2 = data - (curBack + 1);
4159 + UInt32 lenTest2 = lenTest + 1;
4160 + UInt32 limit = lenTest2 + p->numFastBytes;
4161 + UInt32 nextRepMatchPrice;
4162 + if (limit > numAvailableBytesFull)
4163 + limit = numAvailableBytesFull;
4164 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
4165 + lenTest2 -= lenTest + 1;
4166 + if (lenTest2 >= 2)
4167 + {
4168 + UInt32 state2 = kMatchNextStates[state];
4169 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
4170 + UInt32 curAndLenCharPrice = curAndLenPrice +
4171 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
4172 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
4173 + data[lenTest], data2[lenTest], p->ProbPrices);
4174 + state2 = kLiteralNextStates[state2];
4175 + posStateNext = (posStateNext + 1) & p->pbMask;
4176 + nextRepMatchPrice = curAndLenCharPrice +
4177 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4178 + GET_PRICE_1(p->isRep[state2]);
4179 +
4180 + /* for (; lenTest2 >= 2; lenTest2--) */
4181 + {
4182 + UInt32 offset = cur + lenTest + 1 + lenTest2;
4183 + UInt32 curAndLenPrice;
4184 + COptimal *opt;
4185 + while (lenEnd < offset)
4186 + p->opt[++lenEnd].price = kInfinityPrice;
4187 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4188 + opt = &p->opt[offset];
4189 + if (curAndLenPrice < opt->price)
4190 + {
4191 + opt->price = curAndLenPrice;
4192 + opt->posPrev = cur + lenTest + 1;
4193 + opt->backPrev = 0;
4194 + opt->prev1IsChar = True;
4195 + opt->prev2 = True;
4196 + opt->posPrev2 = cur;
4197 + opt->backPrev2 = curBack + LZMA_NUM_REPS;
4198 + }
4199 + }
4200 + }
4201 + offs += 2;
4202 + if (offs == numDistancePairs)
4203 + break;
4204 + curBack = matchDistances[offs + 1];
4205 + if (curBack >= kNumFullDistances)
4206 + GetPosSlot2(curBack, posSlot);
4207 + }
4208 + }
4209 + }
4210 + }
4211 +}
4212 +
4213 +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
4214 +
4215 +static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
4216 +{
4217 + UInt32 numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
4218 + UInt32 lenMain, numDistancePairs;
4219 + const Byte *data;
4220 + UInt32 repLens[LZMA_NUM_REPS];
4221 + UInt32 repMaxIndex, i;
4222 + UInt32 *matchDistances;
4223 + UInt32 backMain;
4224 +
4225 + if (!p->longestMatchWasFound)
4226 + {
4227 + lenMain = ReadMatchDistances(p, &numDistancePairs);
4228 + }
4229 + else
4230 + {
4231 + lenMain = p->longestMatchLength;
4232 + numDistancePairs = p->numDistancePairs;
4233 + p->longestMatchWasFound = False;
4234 + }
4235 +
4236 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
4237 + if (numAvailableBytes > LZMA_MATCH_LEN_MAX)
4238 + numAvailableBytes = LZMA_MATCH_LEN_MAX;
4239 + if (numAvailableBytes < 2)
4240 + {
4241 + *backRes = (UInt32)(-1);
4242 + return 1;
4243 + }
4244 +
4245 + repMaxIndex = 0;
4246 +
4247 + for (i = 0; i < LZMA_NUM_REPS; i++)
4248 + {
4249 + const Byte *data2 = data - (p->reps[i] + 1);
4250 + UInt32 len;
4251 + if (data[0] != data2[0] || data[1] != data2[1])
4252 + {
4253 + repLens[i] = 0;
4254 + continue;
4255 + }
4256 + for (len = 2; len < numAvailableBytes && data[len] == data2[len]; len++);
4257 + if (len >= p->numFastBytes)
4258 + {
4259 + *backRes = i;
4260 + MovePos(p, len - 1);
4261 + return len;
4262 + }
4263 + repLens[i] = len;
4264 + if (len > repLens[repMaxIndex])
4265 + repMaxIndex = i;
4266 + }
4267 + matchDistances = p->matchDistances;
4268 + if (lenMain >= p->numFastBytes)
4269 + {
4270 + *backRes = matchDistances[numDistancePairs - 1] + LZMA_NUM_REPS;
4271 + MovePos(p, lenMain - 1);
4272 + return lenMain;
4273 + }
4274 +
4275 + backMain = 0; /* for GCC */
4276 + if (lenMain >= 2)
4277 + {
4278 + backMain = matchDistances[numDistancePairs - 1];
4279 + while (numDistancePairs > 2 && lenMain == matchDistances[numDistancePairs - 4] + 1)
4280 + {
4281 + if (!ChangePair(matchDistances[numDistancePairs - 3], backMain))
4282 + break;
4283 + numDistancePairs -= 2;
4284 + lenMain = matchDistances[numDistancePairs - 2];
4285 + backMain = matchDistances[numDistancePairs - 1];
4286 + }
4287 + if (lenMain == 2 && backMain >= 0x80)
4288 + lenMain = 1;
4289 + }
4290 +
4291 + if (repLens[repMaxIndex] >= 2)
4292 + {
4293 + if (repLens[repMaxIndex] + 1 >= lenMain ||
4294 + (repLens[repMaxIndex] + 2 >= lenMain && (backMain > (1 << 9))) ||
4295 + (repLens[repMaxIndex] + 3 >= lenMain && (backMain > (1 << 15))))
4296 + {
4297 + UInt32 lenRes;
4298 + *backRes = repMaxIndex;
4299 + lenRes = repLens[repMaxIndex];
4300 + MovePos(p, lenRes - 1);
4301 + return lenRes;
4302 + }
4303 + }
4304 +
4305 + if (lenMain >= 2 && numAvailableBytes > 2)
4306 + {
4307 + UInt32 i;
4308 + numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
4309 + p->longestMatchLength = ReadMatchDistances(p, &p->numDistancePairs);
4310 + if (p->longestMatchLength >= 2)
4311 + {
4312 + UInt32 newDistance = matchDistances[p->numDistancePairs - 1];
4313 + if ((p->longestMatchLength >= lenMain && newDistance < backMain) ||
4314 + (p->longestMatchLength == lenMain + 1 && !ChangePair(backMain, newDistance)) ||
4315 + (p->longestMatchLength > lenMain + 1) ||
4316 + (p->longestMatchLength + 1 >= lenMain && lenMain >= 3 && ChangePair(newDistance, backMain)))
4317 + {
4318 + p->longestMatchWasFound = True;
4319 + *backRes = (UInt32)(-1);
4320 + return 1;
4321 + }
4322 + }
4323 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
4324 + for (i = 0; i < LZMA_NUM_REPS; i++)
4325 + {
4326 + UInt32 len;
4327 + const Byte *data2 = data - (p->reps[i] + 1);
4328 + if (data[1] != data2[1] || data[2] != data2[2])
4329 + {
4330 + repLens[i] = 0;
4331 + continue;
4332 + }
4333 + for (len = 2; len < numAvailableBytes && data[len] == data2[len]; len++);
4334 + if (len + 1 >= lenMain)
4335 + {
4336 + p->longestMatchWasFound = True;
4337 + *backRes = (UInt32)(-1);
4338 + return 1;
4339 + }
4340 + }
4341 + *backRes = backMain + LZMA_NUM_REPS;
4342 + MovePos(p, lenMain - 2);
4343 + return lenMain;
4344 + }
4345 + *backRes = (UInt32)(-1);
4346 + return 1;
4347 +}
4348 +
4349 +static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
4350 +{
4351 + UInt32 len;
4352 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4353 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4354 + p->state = kMatchNextStates[p->state];
4355 + len = LZMA_MATCH_LEN_MIN;
4356 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4357 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
4358 + RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
4359 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
4360 +}
4361 +
4362 +static SRes CheckErrors(CLzmaEnc *p)
4363 +{
4364 + if (p->result != SZ_OK)
4365 + return p->result;
4366 + if (p->rc.res != SZ_OK)
4367 + p->result = SZ_ERROR_WRITE;
4368 + if (p->matchFinderBase.result != SZ_OK)
4369 + p->result = SZ_ERROR_READ;
4370 + if (p->result != SZ_OK)
4371 + p->finished = True;
4372 + return p->result;
4373 +}
4374 +
4375 +static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
4376 +{
4377 + /* ReleaseMFStream(); */
4378 + p->finished = True;
4379 + if (p->writeEndMark)
4380 + WriteEndMarker(p, nowPos & p->pbMask);
4381 + RangeEnc_FlushData(&p->rc);
4382 + RangeEnc_FlushStream(&p->rc);
4383 + return CheckErrors(p);
4384 +}
4385 +
4386 +static void FillAlignPrices(CLzmaEnc *p)
4387 +{
4388 + UInt32 i;
4389 + for (i = 0; i < kAlignTableSize; i++)
4390 + p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
4391 + p->alignPriceCount = 0;
4392 +}
4393 +
4394 +static void FillDistancesPrices(CLzmaEnc *p)
4395 +{
4396 + UInt32 tempPrices[kNumFullDistances];
4397 + UInt32 i, lenToPosState;
4398 + for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
4399 + {
4400 + UInt32 posSlot = GetPosSlot1(i);
4401 + UInt32 footerBits = ((posSlot >> 1) - 1);
4402 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4403 + tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
4404 + }
4405 +
4406 + for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
4407 + {
4408 + UInt32 posSlot;
4409 + const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
4410 + UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
4411 + for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
4412 + posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
4413 + for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
4414 + posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
4415 +
4416 + {
4417 + UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
4418 + UInt32 i;
4419 + for (i = 0; i < kStartPosModelIndex; i++)
4420 + distancesPrices[i] = posSlotPrices[i];
4421 + for (; i < kNumFullDistances; i++)
4422 + distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
4423 + }
4424 + }
4425 + p->matchPriceCount = 0;
4426 +}
4427 +
4428 +static void LzmaEnc_Construct(CLzmaEnc *p)
4429 +{
4430 + RangeEnc_Construct(&p->rc);
4431 + MatchFinder_Construct(&p->matchFinderBase);
4432 + #ifdef COMPRESS_MF_MT
4433 + MatchFinderMt_Construct(&p->matchFinderMt);
4434 + p->matchFinderMt.MatchFinder = &p->matchFinderBase;
4435 + #endif
4436 +
4437 + {
4438 + CLzmaEncProps props;
4439 + LzmaEncProps_Init(&props);
4440 + LzmaEnc_SetProps(p, &props);
4441 + }
4442 +
4443 + #ifndef LZMA_LOG_BSR
4444 + LzmaEnc_FastPosInit(p->g_FastPos);
4445 + #endif
4446 +
4447 + LzmaEnc_InitPriceTables(p->ProbPrices);
4448 + p->litProbs = 0;
4449 + p->saveState.litProbs = 0;
4450 +}
4451 +
4452 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
4453 +{
4454 + void *p;
4455 + p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
4456 + if (p != 0)
4457 + LzmaEnc_Construct((CLzmaEnc *)p);
4458 + return p;
4459 +}
4460 +
4461 +static void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
4462 +{
4463 + alloc->Free(alloc, p->litProbs);
4464 + alloc->Free(alloc, p->saveState.litProbs);
4465 + p->litProbs = 0;
4466 + p->saveState.litProbs = 0;
4467 +}
4468 +
4469 +static void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
4470 +{
4471 + #ifdef COMPRESS_MF_MT
4472 + MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
4473 + #endif
4474 + MatchFinder_Free(&p->matchFinderBase, allocBig);
4475 + LzmaEnc_FreeLits(p, alloc);
4476 + RangeEnc_Free(&p->rc, alloc);
4477 +}
4478 +
4479 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
4480 +{
4481 + LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
4482 + alloc->Free(alloc, p);
4483 +}
4484 +
4485 +static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
4486 +{
4487 + UInt32 nowPos32, startPos32;
4488 + if (p->inStream != 0)
4489 + {
4490 + p->matchFinderBase.stream = p->inStream;
4491 + p->matchFinder.Init(p->matchFinderObj);
4492 + p->inStream = 0;
4493 + }
4494 +
4495 + if (p->finished)
4496 + return p->result;
4497 + RINOK(CheckErrors(p));
4498 +
4499 + nowPos32 = (UInt32)p->nowPos64;
4500 + startPos32 = nowPos32;
4501 +
4502 + if (p->nowPos64 == 0)
4503 + {
4504 + UInt32 numDistancePairs;
4505 + Byte curByte;
4506 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4507 + return Flush(p, nowPos32);
4508 + ReadMatchDistances(p, &numDistancePairs);
4509 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
4510 + p->state = kLiteralNextStates[p->state];