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
10 ifeq ($(WITHOUT_XATTR), 1)
11 CPPFLAGS += -DWITHOUT_XATTR
12 @@ -84,7 +84,7 @@ $(BUILDDIR)/include/version.h.tmp:
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)
23 @@ -520,6 +520,9 @@ int jffs2_compressors_init(void)
24 #ifdef CONFIG_JFFS2_LZO
27 +#ifdef CONFIG_JFFS2_LZMA
33 @@ -534,5 +537,8 @@ int jffs2_compressors_exit(void)
34 #ifdef CONFIG_JFFS2_LZO
37 +#ifdef CONFIG_JFFS2_LZMA
46 #define CONFIG_JFFS2_ZLIB
47 #define CONFIG_JFFS2_RTIME
48 -#define CONFIG_JFFS2_LZO
49 +#define CONFIG_JFFS2_LZMA
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
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);
66 +#ifdef CONFIG_JFFS2_LZMA
67 +int jffs2_lzma_init(void);
68 +void jffs2_lzma_exit(void);
72 #endif /* __JFFS2_COMPR_H__ */
77 + * JFFS2 -- Journalling Flash File System, Version 2.
79 + * For licensing information, see the file 'LICENCE' in this directory.
81 + * JFFS2 wrapper to the LZMA C SDK
85 +#include <linux/lzma.h>
89 + static DEFINE_MUTEX(deflate_mutex);
93 +Byte propsEncoded[LZMA_PROPS_SIZE];
94 +SizeT propsSize = sizeof(propsEncoded);
96 +STATIC void lzma_free_workspace(void)
98 + LzmaEnc_Destroy(p, &lzma_alloc, &lzma_alloc);
101 +STATIC int INIT lzma_alloc_workspace(CLzmaEncProps *props)
103 + if ((p = (CLzmaEncHandle *)LzmaEnc_Create(&lzma_alloc)) == NULL)
105 + PRINT_ERROR("Failed to allocate lzma deflate workspace\n");
109 + if (LzmaEnc_SetProps(p, props) != SZ_OK)
111 + lzma_free_workspace();
115 + if (LzmaEnc_WriteProperties(p, propsEncoded, &propsSize) != SZ_OK)
117 + lzma_free_workspace();
124 +STATIC int jffs2_lzma_compress(unsigned char *data_in, unsigned char *cpage_out,
125 + uint32_t *sourcelen, uint32_t *dstlen)
127 + SizeT compress_size = (SizeT)(*dstlen);
131 + mutex_lock(&deflate_mutex);
134 + ret = LzmaEnc_MemEncode(p, cpage_out, &compress_size, data_in, *sourcelen,
135 + 0, NULL, &lzma_alloc, &lzma_alloc);
138 + mutex_unlock(&deflate_mutex);
144 + *dstlen = (uint32_t)compress_size;
149 +STATIC int jffs2_lzma_decompress(unsigned char *data_in, unsigned char *cpage_out,
150 + uint32_t srclen, uint32_t destlen)
153 + SizeT dl = (SizeT)destlen;
154 + SizeT sl = (SizeT)srclen;
155 + ELzmaStatus status;
157 + ret = LzmaDecode(cpage_out, &dl, data_in, &sl, propsEncoded,
158 + propsSize, LZMA_FINISH_ANY, &status, &lzma_alloc);
160 + if (ret != SZ_OK || status == LZMA_STATUS_NOT_FINISHED || dl != (SizeT)destlen)
166 +static struct jffs2_compressor jffs2_lzma_comp = {
167 + .priority = JFFS2_LZMA_PRIORITY,
169 + .compr = JFFS2_COMPR_LZMA,
170 + .compress = &jffs2_lzma_compress,
171 + .decompress = &jffs2_lzma_decompress,
175 +int INIT jffs2_lzma_init(void)
178 + CLzmaEncProps props;
179 + LzmaEncProps_Init(&props);
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;
188 + ret = lzma_alloc_workspace(&props);
192 + ret = jffs2_register_compressor(&jffs2_lzma_comp);
194 + lzma_free_workspace();
199 +void jffs2_lzma_exit(void)
201 + jffs2_unregister_compressor(&jffs2_lzma_comp);
202 + lzma_free_workspace();
204 --- a/include/linux/jffs2.h
205 +++ b/include/linux/jffs2.h
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
215 +++ b/include/linux/lzma.h
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
232 + #include <stdint.h>
233 + #include <stdlib.h>
235 + #include <unistd.h>
236 + #include <string.h>
238 + #include <linux/jffs2.h>
240 + extern int page_size;
241 + #define PAGE_SIZE page_size
243 + #define LZMA_MALLOC malloc
244 + #define LZMA_FREE free
245 + #define PRINT_ERROR(msg) fprintf(stderr, msg)
250 +#include "lzma/LzmaDec.h"
251 +#include "lzma/LzmaEnc.h"
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)
259 +#define LZMA_BEST_DICT(n) (((int)((n) / 2)) * 2)
261 +static void *p_lzma_malloc(void *p, size_t size)
266 + return LZMA_MALLOC(size);
269 +static void p_lzma_free(void *p, void *address)
271 + if (address != NULL)
272 + LZMA_FREE(address);
275 +static ISzAlloc lzma_alloc = {p_lzma_malloc, p_lzma_free};
279 +++ b/include/linux/lzma/LzFind.h
281 +/* LzFind.h -- Match finder for LZ algorithms
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.
298 +typedef UInt32 CLzRef;
300 +typedef struct _CMatchFinder
308 + UInt32 cyclicBufferPos;
309 + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */
311 + UInt32 matchMaxLen;
318 + ISeqInStream *stream;
319 + int streamEndWasReached;
322 + UInt32 keepSizeBefore;
323 + UInt32 keepSizeAfter;
325 + UInt32 numHashBytes;
328 + /* int skipModeBits; */
330 + UInt32 historySize;
331 + UInt32 fixedHashSize;
332 + UInt32 hashSizeSum;
338 +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer)
339 +#define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)])
341 +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos)
343 +int MatchFinder_NeedMove(CMatchFinder *p);
344 +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p);
345 +void MatchFinder_MoveBlock(CMatchFinder *p);
346 +void MatchFinder_ReadIfRequired(CMatchFinder *p);
348 +void MatchFinder_Construct(CMatchFinder *p);
351 + historySize <= 3 GB
352 + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB
354 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
355 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
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);
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);
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
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);
378 +typedef struct _IMatchFinder
381 + Mf_GetIndexByte_Func GetIndexByte;
382 + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes;
383 + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos;
384 + Mf_GetMatches_Func GetMatches;
388 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable);
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);
398 +++ b/include/linux/lzma/LzHash.h
400 +/* LzHash.h -- HASH functions for LZ algorithms
402 +Copyright (c) 1999-2008 Igor Pavlov
403 +Read LzFind.h for license options */
408 +#define kHash2Size (1 << 10)
409 +#define kHash3Size (1 << 16)
410 +#define kHash4Size (1 << 20)
412 +#define kFix3HashSize (kHash2Size)
413 +#define kFix4HashSize (kHash2Size + kHash3Size)
414 +#define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size)
416 +#define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8);
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; }
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; }
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); }
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;
441 +#define MT_HASH2_CALC \
442 + hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1);
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); }
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); }
457 +++ b/include/linux/lzma/LzmaDec.h
459 +/* LzmaDec.h -- LZMA Decoder
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.
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 */
481 +#define CLzmaProb UInt32
483 +#define CLzmaProb UInt16
487 +/* ---------- LZMA Properties ---------- */
489 +#define LZMA_PROPS_SIZE 5
491 +typedef struct _CLzmaProps
493 + unsigned lc, lp, pb;
497 +/* LzmaProps_Decode - decodes properties
500 + SZ_ERROR_UNSUPPORTED - Unsupported properties
503 +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
506 +/* ---------- LZMA Decoder state ---------- */
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; */
511 +#define LZMA_REQUIRED_INPUT_MAX 20
519 + UInt32 range, code;
522 + UInt32 processedPos;
523 + UInt32 checkDicSize;
526 + unsigned remainLen;
530 + unsigned tempBufSize;
531 + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
534 +#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
536 +void LzmaDec_Init(CLzmaDec *p);
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. */
544 + LZMA_FINISH_ANY, /* finish at any point */
545 + LZMA_FINISH_END /* block must be finished at the end */
548 +/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
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.
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.
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. */
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 */
572 +/* ELzmaStatus is used only as output value for function call */
575 +/* ---------- Interfaces ---------- */
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. */
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.
591 +LzmaDec_Allocate* can return:
593 + SZ_ERROR_MEM - Memory allocation error
594 + SZ_ERROR_UNSUPPORTED - Unsupported properties
597 +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
598 +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
600 +SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc);
601 +void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc);
603 +/* ---------- Dictionary Interface ---------- */
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.
612 + for (each new stream)
615 + while (it needs more decompression)
617 + LzmaDec_DecodeToDic()
618 + use data from CLzmaDec::dic and update CLzmaDec::dicPos
624 +/* LzmaDec_DecodeToDic
626 + The decoding to internal dictionary buffer (CLzmaDec::dic).
627 + You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
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.
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
644 +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
645 + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
648 +/* ---------- Buffer Interface ---------- */
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.
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).
661 +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
662 + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
665 +/* ---------- One Call Interface ---------- */
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).
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).
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);
692 +++ b/include/linux/lzma/LzmaEnc.h
694 +/* LzmaEnc.h -- LZMA Encoder
696 +Copyright (c) 1999-2008 Igor Pavlov
697 +Read LzFind.h for license options */
704 +#define LZMA_PROPS_SIZE 5
706 +typedef struct _CLzmaEncProps
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 */
724 +void LzmaEncProps_Init(CLzmaEncProps *p);
725 +void LzmaEncProps_Normalize(CLzmaEncProps *p);
726 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2);
729 +/* ---------- CLzmaEncHandle Interface ---------- */
731 +/* LzmaEnc_* functions can return the following exit codes:
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)
741 +typedef void * CLzmaEncHandle;
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);
752 +/* ---------- One Call Interface ---------- */
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)
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);
769 +++ b/include/linux/lzma/Types.h
771 +/* Types.h -- Basic types
776 +#ifndef __7Z_TYPES_H
777 +#define __7Z_TYPES_H
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
794 +#define SZ_ERROR_ARCHIVE 16
795 +#define SZ_ERROR_NO_ARCHIVE 17
800 +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
803 +typedef unsigned char Byte;
804 +typedef short Int16;
805 +typedef unsigned short UInt16;
807 +#ifdef _LZMA_UINT32_IS_ULONG
809 +typedef unsigned long UInt32;
812 +typedef unsigned int UInt32;
815 +/* #define _SZ_NO_INT_64 */
816 +/* define it if your compiler doesn't support 64-bit integers */
818 +#ifdef _SZ_NO_INT_64
821 +typedef unsigned long UInt64;
825 +#if defined(_MSC_VER) || defined(__BORLANDC__)
826 +typedef __int64 Int64;
827 +typedef unsigned __int64 UInt64;
829 +typedef long long int Int64;
830 +typedef unsigned long long int UInt64;
835 +#ifdef _LZMA_NO_SYSTEM_SIZE_T
836 +typedef UInt32 SizeT;
839 +typedef size_t SizeT;
849 +#if _MSC_VER >= 1300
850 +#define MY_NO_INLINE __declspec(noinline)
852 +#define MY_NO_INLINE
855 +#define MY_CDECL __cdecl
856 +#define MY_STD_CALL __stdcall
857 +#define MY_FAST_CALL MY_NO_INLINE __fastcall
863 +#define MY_FAST_CALL
868 +/* The following interfaces use first parameter as pointer to structure */
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 */
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 */
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;
893 + void *(*Alloc)(void *p, size_t size);
894 + void (*Free)(void *p, void *address); /* address can be 0 */
897 +#define IAlloc_Alloc(p, size) (p)->Alloc((p), size)
898 +#define IAlloc_Free(p, a) (p)->Free((p), a)
904 +/* LzFind.c -- Match finder for LZ algorithms
906 +Copyright (c) 1999-2008 Igor Pavlov
907 +Read LzFind.h for license options */
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)
920 +#define kStartMaxLen 3
922 +static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
924 + if (!p->directInput)
926 + alloc->Free(alloc, p->bufferBase);
931 +/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
933 +static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
935 + UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
936 + if (p->directInput)
938 + p->blockSize = blockSize;
941 + if (p->bufferBase == 0 || p->blockSize != blockSize)
943 + LzInWindow_Free(p, alloc);
944 + p->blockSize = blockSize;
945 + p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
947 + return (p->bufferBase != 0);
950 +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
951 +Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
953 +UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
955 +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
957 + p->posLimit -= subValue;
958 + p->pos -= subValue;
959 + p->streamPos -= subValue;
962 +static void MatchFinder_ReadBlock(CMatchFinder *p)
964 + if (p->streamEndWasReached || p->result != SZ_OK)
968 + Byte *dest = p->buffer + (p->streamPos - p->pos);
969 + size_t size = (p->bufferBase + p->blockSize - dest);
972 + p->result = p->stream->Read(p->stream, dest, &size);
973 + if (p->result != SZ_OK)
977 + p->streamEndWasReached = 1;
980 + p->streamPos += (UInt32)size;
981 + if (p->streamPos - p->pos > p->keepSizeAfter)
986 +void MatchFinder_MoveBlock(CMatchFinder *p)
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;
994 +int MatchFinder_NeedMove(CMatchFinder *p)
996 + /* if (p->streamEndWasReached) return 0; */
997 + return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
1000 +void MatchFinder_ReadIfRequired(CMatchFinder *p)
1002 + if (p->streamEndWasReached)
1004 + if (p->keepSizeAfter >= p->streamPos - p->pos)
1005 + MatchFinder_ReadBlock(p);
1008 +static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
1010 + if (MatchFinder_NeedMove(p))
1011 + MatchFinder_MoveBlock(p);
1012 + MatchFinder_ReadBlock(p);
1015 +static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
1019 + p->numHashBytes = 4;
1020 + /* p->skipModeBits = 0; */
1021 + p->directInput = 0;
1025 +#define kCrcPoly 0xEDB88320
1027 +void MatchFinder_Construct(CMatchFinder *p)
1030 + p->bufferBase = 0;
1031 + p->directInput = 0;
1033 + MatchFinder_SetDefaultSettings(p);
1035 + for (i = 0; i < 256; i++)
1039 + for (j = 0; j < 8; j++)
1040 + r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
1045 +static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
1047 + alloc->Free(alloc, p->hash);
1051 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
1053 + MatchFinder_FreeThisClassMemory(p, alloc);
1054 + LzInWindow_Free(p, alloc);
1057 +static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
1059 + size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
1060 + if (sizeInBytes / sizeof(CLzRef) != num)
1062 + return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
1065 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
1066 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
1069 + UInt32 sizeReserv;
1070 + if (historySize > kMaxHistorySize)
1072 + MatchFinder_Free(p, alloc);
1075 + sizeReserv = historySize >> 1;
1076 + if (historySize > ((UInt32)2 << 30))
1077 + sizeReserv = historySize >> 2;
1078 + sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
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))
1085 + UInt32 newCyclicBufferSize = (historySize /* >> p->skipModeBits */) + 1;
1087 + p->matchMaxLen = matchMaxLen;
1089 + p->fixedHashSize = 0;
1090 + if (p->numHashBytes == 2)
1091 + hs = (1 << 16) - 1;
1094 + hs = historySize - 1;
1100 + /* hs >>= p->skipModeBits; */
1101 + hs |= 0xFFFF; /* don't change it! It's required for Deflate */
1102 + if (hs > (1 << 24))
1104 + if (p->numHashBytes == 3)
1105 + hs = (1 << 24) - 1;
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;
1119 + UInt32 prevSize = p->hashSizeSum + p->numSons;
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)
1128 + MatchFinder_FreeThisClassMemory(p, alloc);
1129 + p->hash = AllocRefs(newSize, alloc);
1132 + p->son = p->hash + p->hashSizeSum;
1137 + MatchFinder_Free(p, alloc);
1141 +static void MatchFinder_SetLimits(CMatchFinder *p)
1143 + UInt32 limit = kMaxValForNormalize - p->pos;
1144 + UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
1145 + if (limit2 < limit)
1147 + limit2 = p->streamPos - p->pos;
1148 + if (limit2 <= p->keepSizeAfter)
1154 + limit2 -= p->keepSizeAfter;
1155 + if (limit2 < limit)
1158 + UInt32 lenLimit = p->streamPos - p->pos;
1159 + if (lenLimit > p->matchMaxLen)
1160 + lenLimit = p->matchMaxLen;
1161 + p->lenLimit = lenLimit;
1163 + p->posLimit = p->pos + limit;
1166 +void MatchFinder_Init(CMatchFinder *p)
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);
1180 +static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
1182 + return (p->pos - p->historySize - 1) & kNormalizeMask;
1185 +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
1188 + for (i = 0; i < numItems; i++)
1190 + UInt32 value = items[i];
1191 + if (value <= subValue)
1192 + value = kEmptyHashValue;
1194 + value -= subValue;
1199 +static void MatchFinder_Normalize(CMatchFinder *p)
1201 + UInt32 subValue = MatchFinder_GetSubValue(p);
1202 + MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
1203 + MatchFinder_ReduceOffsets(p, subValue);
1206 +static void MatchFinder_CheckLimits(CMatchFinder *p)
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);
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)
1221 + son[_cyclicBufferPos] = curMatch;
1224 + UInt32 delta = pos - curMatch;
1225 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1228 + const Byte *pb = cur - delta;
1229 + curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
1230 + if (pb[maxLen] == cur[maxLen] && *pb == *cur)
1233 + while(++len != lenLimit)
1234 + if (pb[len] != cur[len])
1238 + *distances++ = maxLen = len;
1239 + *distances++ = delta - 1;
1240 + if (len == lenLimit)
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)
1252 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1253 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1254 + UInt32 len0 = 0, len1 = 0;
1257 + UInt32 delta = pos - curMatch;
1258 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1260 + *ptr0 = *ptr1 = kEmptyHashValue;
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])
1269 + if (++len != lenLimit && pb[len] == cur[len])
1270 + while(++len != lenLimit)
1271 + if (pb[len] != cur[len])
1275 + *distances++ = maxLen = len;
1276 + *distances++ = delta - 1;
1277 + if (len == lenLimit)
1285 + if (pb[len] < cur[len])
1303 +static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1304 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
1306 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1307 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1308 + UInt32 len0 = 0, len1 = 0;
1311 + UInt32 delta = pos - curMatch;
1312 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1314 + *ptr0 = *ptr1 = kEmptyHashValue;
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])
1323 + while(++len != lenLimit)
1324 + if (pb[len] != cur[len])
1327 + if (len == lenLimit)
1335 + if (pb[len] < cur[len])
1354 + ++p->cyclicBufferPos; \
1356 + if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
1358 +#define MOVE_POS_RET MOVE_POS return offset;
1360 +static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
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; }} \
1367 +#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
1368 +#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
1370 +#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
1372 +#define GET_MATCHES_FOOTER(offset, maxLen) \
1373 + offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
1374 + distances + offset, maxLen) - distances); MOVE_POS_RET;
1376 +#define SKIP_FOOTER \
1377 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;
1379 +static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1382 + GET_MATCHES_HEADER(2)
1384 + curMatch = p->hash[hashValue];
1385 + p->hash[hashValue] = p->pos;
1387 + GET_MATCHES_FOOTER(offset, 1)
1390 +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1393 + GET_MATCHES_HEADER(3)
1395 + curMatch = p->hash[hashValue];
1396 + p->hash[hashValue] = p->pos;
1398 + GET_MATCHES_FOOTER(offset, 2)
1401 +static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1403 + UInt32 hash2Value, delta2, maxLen, offset;
1404 + GET_MATCHES_HEADER(3)
1408 + delta2 = p->pos - p->hash[hash2Value];
1409 + curMatch = p->hash[kFix3HashSize + hashValue];
1411 + p->hash[hash2Value] =
1412 + p->hash[kFix3HashSize + hashValue] = p->pos;
1417 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1419 + for (; maxLen != lenLimit; maxLen++)
1420 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1422 + distances[0] = maxLen;
1423 + distances[1] = delta2 - 1;
1425 + if (maxLen == lenLimit)
1427 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1431 + GET_MATCHES_FOOTER(offset, maxLen)
1434 +static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1436 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1437 + GET_MATCHES_HEADER(4)
1441 + delta2 = p->pos - p->hash[ hash2Value];
1442 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1443 + curMatch = p->hash[kFix4HashSize + hashValue];
1445 + p->hash[ hash2Value] =
1446 + p->hash[kFix3HashSize + hash3Value] =
1447 + p->hash[kFix4HashSize + hashValue] = p->pos;
1451 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1453 + distances[0] = maxLen = 2;
1454 + distances[1] = delta2 - 1;
1457 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1460 + distances[offset + 1] = delta3 - 1;
1466 + for (; maxLen != lenLimit; maxLen++)
1467 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1469 + distances[offset - 2] = maxLen;
1470 + if (maxLen == lenLimit)
1472 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1478 + GET_MATCHES_FOOTER(offset, maxLen)
1481 +static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1483 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1484 + GET_MATCHES_HEADER(4)
1488 + delta2 = p->pos - p->hash[ hash2Value];
1489 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1490 + curMatch = p->hash[kFix4HashSize + hashValue];
1492 + p->hash[ hash2Value] =
1493 + p->hash[kFix3HashSize + hash3Value] =
1494 + p->hash[kFix4HashSize + hashValue] = p->pos;
1498 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1500 + distances[0] = maxLen = 2;
1501 + distances[1] = delta2 - 1;
1504 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1507 + distances[offset + 1] = delta3 - 1;
1513 + for (; maxLen != lenLimit; maxLen++)
1514 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1516 + distances[offset - 2] = maxLen;
1517 + if (maxLen == lenLimit)
1519 + p->son[p->cyclicBufferPos] = curMatch;
1525 + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
1526 + distances + offset, maxLen) - (distances));
1530 +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1533 + GET_MATCHES_HEADER(3)
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));
1542 +static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1548 + curMatch = p->hash[hashValue];
1549 + p->hash[hashValue] = p->pos;
1552 + while (--num != 0);
1555 +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1561 + curMatch = p->hash[hashValue];
1562 + p->hash[hashValue] = p->pos;
1565 + while (--num != 0);
1568 +static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1572 + UInt32 hash2Value;
1575 + curMatch = p->hash[kFix3HashSize + hashValue];
1576 + p->hash[hash2Value] =
1577 + p->hash[kFix3HashSize + hashValue] = p->pos;
1580 + while (--num != 0);
1583 +static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1587 + UInt32 hash2Value, hash3Value;
1590 + curMatch = p->hash[kFix4HashSize + hashValue];
1591 + p->hash[ hash2Value] =
1592 + p->hash[kFix3HashSize + hash3Value] = p->pos;
1593 + p->hash[kFix4HashSize + hashValue] = p->pos;
1596 + while (--num != 0);
1599 +static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1603 + UInt32 hash2Value, hash3Value;
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;
1613 + while (--num != 0);
1616 +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1622 + curMatch = p->hash[hashValue];
1623 + p->hash[hashValue] = p->pos;
1624 + p->son[p->cyclicBufferPos] = curMatch;
1627 + while (--num != 0);
1630 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
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;
1638 + vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;
1639 + vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;
1641 + else if (p->numHashBytes == 2)
1643 + vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;
1644 + vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;
1646 + else if (p->numHashBytes == 3)
1648 + vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;
1649 + vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;
1653 + vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
1654 + vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
1658 +++ b/lzma/LzmaDec.c
1660 +/* LzmaDec.c -- LZMA Decoder
1662 +Copyright (c) 1999-2008 Igor Pavlov
1663 +Read LzmaDec.h for license options */
1665 +#include "LzmaDec.h"
1667 +#include <string.h>
1669 +#define kNumTopBits 24
1670 +#define kTopValue ((UInt32)1 << kNumTopBits)
1672 +#define kNumBitModelTotalBits 11
1673 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
1674 +#define kNumMoveBits 5
1676 +#define RC_INIT_SIZE 5
1678 +#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
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, ; , ;)
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; }
1692 +/* #define _LZMA_SIZE_OPT */
1694 +#ifdef _LZMA_SIZE_OPT
1695 +#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
1697 +#define TREE_6_DECODE(probs, i) \
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); \
1708 +#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
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; }
1721 +#define kNumPosBitsMax 4
1722 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
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)
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)
1739 +#define kNumStates 12
1740 +#define kNumLitStates 7
1742 +#define kStartPosModelIndex 4
1743 +#define kEndPosModelIndex 14
1744 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
1746 +#define kNumPosSlotBits 6
1747 +#define kNumLenToPosStates 4
1749 +#define kNumAlignBits 4
1750 +#define kAlignTableSize (1 << kNumAlignBits)
1752 +#define kMatchMinLen 2
1753 +#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
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)
1768 +#define LZMA_BASE_SIZE 1846
1769 +#define LZMA_LIT_SIZE 768
1771 +#define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
1773 +#if Literal != LZMA_BASE_SIZE
1774 +StopCompilingDueBUG
1778 +#define LZMA_STREAM_WAS_FINISHED_ID (-1)
1779 +#define LZMA_SPEC_LEN_OFFSET (-3)
1782 +Byte kLiteralNextStates[kNumStates * 2] =
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
1788 +#define LZMA_DIC_MIN (1 << 12)
1790 +/* First LZMA-symbol is always decoded.
1791 +And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
1797 + < kMatchSpecLenStart : normal remain
1798 + = kMatchSpecLenStart : finished
1799 + = kMatchSpecLenStart + 1 : Flush marker
1800 + = kMatchSpecLenStart + 2 : State Init Marker
1803 +static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
1805 + CLzmaProb *probs = p->probs;
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;
1813 + Byte *dic = p->dic;
1814 + SizeT dicBufSize = p->dicBufSize;
1815 + SizeT dicPos = p->dicPos;
1817 + UInt32 processedPos = p->processedPos;
1818 + UInt32 checkDicSize = p->checkDicSize;
1821 + const Byte *buf = p->buf;
1822 + UInt32 range = p->range;
1823 + UInt32 code = p->code;
1830 + unsigned posState = processedPos & pbMask;
1832 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
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))));
1842 + if (state < kNumLitStates)
1845 + do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
1849 + unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1850 + unsigned offs = 0x100;
1855 + CLzmaProb *probLit;
1857 + bit = (matchByte & offs);
1858 + probLit = prob + offs + bit + symbol;
1859 + GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
1861 + while (symbol < 0x100);
1863 + dic[dicPos++] = (Byte)symbol;
1866 + state = kLiteralNextStates[state];
1867 + /* if (state < 4) state = 0; else if (state < 10) state -= 3; else state -= 6; */
1873 + prob = probs + IsRep + state;
1877 + state += kNumStates;
1878 + prob = probs + LenCoder;
1883 + if (checkDicSize == 0 && processedPos == 0)
1884 + return SZ_ERROR_DATA;
1885 + prob = probs + IsRepG0 + state;
1889 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
1893 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1896 + state = state < kNumLitStates ? 9 : 11;
1905 + prob = probs + IsRepG1 + state;
1914 + prob = probs + IsRepG2 + state;
1931 + state = state < kNumLitStates ? 8 : 11;
1932 + prob = probs + RepLenCoder;
1935 + unsigned limit, offset;
1936 + CLzmaProb *probLen = prob + LenChoice;
1939 + UPDATE_0(probLen);
1940 + probLen = prob + LenLow + (posState << kLenNumLowBits);
1942 + limit = (1 << kLenNumLowBits);
1946 + UPDATE_1(probLen);
1947 + probLen = prob + LenChoice2;
1950 + UPDATE_0(probLen);
1951 + probLen = prob + LenMid + (posState << kLenNumMidBits);
1952 + offset = kLenNumLowSymbols;
1953 + limit = (1 << kLenNumMidBits);
1957 + UPDATE_1(probLen);
1958 + probLen = prob + LenHigh;
1959 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
1960 + limit = (1 << kLenNumHighBits);
1963 + TREE_DECODE(probLen, limit, len);
1967 + if (state >= kNumStates)
1970 + prob = probs + PosSlot +
1971 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
1972 + TREE_6_DECODE(prob, distance);
1973 + if (distance >= kStartPosModelIndex)
1975 + unsigned posSlot = (unsigned)distance;
1976 + int numDirectBits = (int)(((distance >> 1) - 1));
1977 + distance = (2 | (distance & 1));
1978 + if (posSlot < kEndPosModelIndex)
1980 + distance <<= numDirectBits;
1981 + prob = probs + SpecPos + distance - posSlot - 1;
1987 + GET_BIT2(prob + i, i, ; , distance |= mask);
1990 + while(--numDirectBits != 0);
1995 + numDirectBits -= kNumAlignBits;
2004 + t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
2005 + distance = (distance << 1) + (t + 1);
2006 + code += range & t;
2010 + if (code >= range)
2017 + while (--numDirectBits != 0);
2018 + prob = probs + Align;
2019 + distance <<= kNumAlignBits;
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);
2027 + if (distance == (UInt32)0xFFFFFFFF)
2029 + len += kMatchSpecLenStart;
2030 + state -= kNumStates;
2038 + rep0 = distance + 1;
2039 + if (checkDicSize == 0)
2041 + if (distance >= processedPos)
2042 + return SZ_ERROR_DATA;
2044 + else if (distance >= checkDicSize)
2045 + return SZ_ERROR_DATA;
2046 + state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
2047 + /* state = kLiteralNextStates[state]; */
2050 + len += kMatchMinLen;
2053 + SizeT rem = limit - dicPos;
2054 + unsigned curLen = ((rem < len) ? (unsigned)rem : len);
2055 + SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
2057 + processedPos += curLen;
2060 + if (pos + curLen <= dicBufSize)
2062 + Byte *dest = dic + dicPos;
2063 + ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
2064 + const Byte *lim = dest + curLen;
2067 + *(dest) = (Byte)*(dest + src);
2068 + while (++dest != lim);
2074 + dic[dicPos++] = dic[pos];
2075 + if (++pos == dicBufSize)
2078 + while (--curLen != 0);
2083 + while (dicPos < limit && buf < bufLimit);
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;
2100 +static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
2102 + if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
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);
2112 + if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
2113 + p->checkDicSize = p->prop.dicSize;
2115 + p->processedPos += len;
2116 + p->remainLen -= len;
2117 + while (len-- != 0)
2119 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
2122 + p->dicPos = dicPos;
2126 +/* LzmaDec_DecodeReal2 decodes LZMA-symbols and sets p->needFlush and p->needInit, if required. */
2128 +static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
2132 + SizeT limit2 = limit;
2133 + if (p->checkDicSize == 0)
2135 + UInt32 rem = p->prop.dicSize - p->processedPos;
2136 + if (limit - p->dicPos > rem)
2137 + limit2 = p->dicPos + rem;
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);
2144 + while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
2146 + if (p->remainLen > kMatchSpecLenStart)
2148 + p->remainLen = kMatchSpecLenStart;
2155 + DUMMY_ERROR, /* unexpected end of input stream */
2161 +static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
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;
2174 + unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
2176 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
2177 + IF_BIT_0_CHECK(prob)
2181 + /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
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))));
2189 + if (state < kNumLitStates)
2191 + unsigned symbol = 1;
2192 + do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
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;
2203 + CLzmaProb *probLit;
2205 + bit = (matchByte & offs);
2206 + probLit = prob + offs + bit + symbol;
2207 + GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
2209 + while (symbol < 0x100);
2218 + prob = probs + IsRep + state;
2219 + IF_BIT_0_CHECK(prob)
2223 + prob = probs + LenCoder;
2224 + res = DUMMY_MATCH;
2230 + prob = probs + IsRepG0 + state;
2231 + IF_BIT_0_CHECK(prob)
2234 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
2235 + IF_BIT_0_CHECK(prob)
2249 + prob = probs + IsRepG1 + state;
2250 + IF_BIT_0_CHECK(prob)
2257 + prob = probs + IsRepG2 + state;
2258 + IF_BIT_0_CHECK(prob)
2268 + state = kNumStates;
2269 + prob = probs + RepLenCoder;
2272 + unsigned limit, offset;
2273 + CLzmaProb *probLen = prob + LenChoice;
2274 + IF_BIT_0_CHECK(probLen)
2277 + probLen = prob + LenLow + (posState << kLenNumLowBits);
2279 + limit = 1 << kLenNumLowBits;
2284 + probLen = prob + LenChoice2;
2285 + IF_BIT_0_CHECK(probLen)
2288 + probLen = prob + LenMid + (posState << kLenNumMidBits);
2289 + offset = kLenNumLowSymbols;
2290 + limit = 1 << kLenNumMidBits;
2295 + probLen = prob + LenHigh;
2296 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
2297 + limit = 1 << kLenNumHighBits;
2300 + TREE_DECODE_CHECK(probLen, limit, len);
2307 + prob = probs + PosSlot +
2308 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
2310 + TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
2311 + if (posSlot >= kStartPosModelIndex)
2313 + int numDirectBits = ((posSlot >> 1) - 1);
2315 + /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
2317 + if (posSlot < kEndPosModelIndex)
2319 + prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
2323 + numDirectBits -= kNumAlignBits;
2328 + code -= range & (((code - range) >> 31) - 1);
2329 + /* if (code >= range) code -= range; */
2331 + while (--numDirectBits != 0);
2332 + prob = probs + Align;
2333 + numDirectBits = kNumAlignBits;
2339 + GET_BIT_CHECK(prob + i, i);
2341 + while(--numDirectBits != 0);
2352 +static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
2354 + p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
2355 + p->range = 0xFFFFFFFF;
2359 +void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
2363 + p->tempBufSize = 0;
2367 + p->processedPos = 0;
2368 + p->checkDicSize = 0;
2369 + p->needInitState = 1;
2372 + p->needInitState = 1;
2375 +void LzmaDec_Init(CLzmaDec *p)
2378 + LzmaDec_InitDicAndState(p, True, True);
2381 +static void LzmaDec_InitStateReal(CLzmaDec *p)
2383 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
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;
2390 + p->needInitState = 0;
2393 +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
2394 + ELzmaFinishMode finishMode, ELzmaStatus *status)
2396 + SizeT inSize = *srcLen;
2398 + LzmaDec_WriteRem(p, dicLimit);
2400 + *status = LZMA_STATUS_NOT_SPECIFIED;
2402 + while (p->remainLen != kMatchSpecLenStart)
2404 + int checkEndMarkNow;
2406 + if (p->needFlush != 0)
2408 + for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
2409 + p->tempBuf[p->tempBufSize++] = *src++;
2410 + if (p->tempBufSize < RC_INIT_SIZE)
2412 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2415 + if (p->tempBuf[0] != 0)
2416 + return SZ_ERROR_DATA;
2418 + LzmaDec_InitRc(p, p->tempBuf);
2419 + p->tempBufSize = 0;
2422 + checkEndMarkNow = 0;
2423 + if (p->dicPos >= dicLimit)
2425 + if (p->remainLen == 0 && p->code == 0)
2427 + *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
2430 + if (finishMode == LZMA_FINISH_ANY)
2432 + *status = LZMA_STATUS_NOT_FINISHED;
2435 + if (p->remainLen != 0)
2437 + *status = LZMA_STATUS_NOT_FINISHED;
2438 + return SZ_ERROR_DATA;
2440 + checkEndMarkNow = 1;
2443 + if (p->needInitState)
2444 + LzmaDec_InitStateReal(p);
2446 + if (p->tempBufSize == 0)
2449 + const Byte *bufLimit;
2450 + if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2452 + int dummyRes = LzmaDec_TryDummy(p, src, inSize);
2453 + if (dummyRes == DUMMY_ERROR)
2455 + memcpy(p->tempBuf, src, inSize);
2456 + p->tempBufSize = (unsigned)inSize;
2457 + (*srcLen) += inSize;
2458 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2461 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2463 + *status = LZMA_STATUS_NOT_FINISHED;
2464 + return SZ_ERROR_DATA;
2469 + bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
2471 + if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
2472 + return SZ_ERROR_DATA;
2473 + processed = p->buf - src;
2474 + (*srcLen) += processed;
2476 + inSize -= processed;
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)
2486 + int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
2487 + if (dummyRes == DUMMY_ERROR)
2489 + (*srcLen) += lookAhead;
2490 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2493 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2495 + *status = LZMA_STATUS_NOT_FINISHED;
2496 + return SZ_ERROR_DATA;
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;
2505 + inSize -= lookAhead;
2506 + p->tempBufSize = 0;
2510 + *status = LZMA_STATUS_FINISHED_WITH_MARK;
2511 + return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
2514 +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
2516 + SizeT outSize = *destLen;
2517 + SizeT inSize = *srcLen;
2518 + *srcLen = *destLen = 0;
2521 + SizeT inSizeCur = inSize, outSizeCur, dicPos;
2522 + ELzmaFinishMode curFinishMode;
2524 + if (p->dicPos == p->dicBufSize)
2526 + dicPos = p->dicPos;
2527 + if (outSize > p->dicBufSize - dicPos)
2529 + outSizeCur = p->dicBufSize;
2530 + curFinishMode = LZMA_FINISH_ANY;
2534 + outSizeCur = dicPos + outSize;
2535 + curFinishMode = finishMode;
2538 + res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
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;
2549 + if (outSizeCur == 0 || outSize == 0)
2554 +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
2556 + alloc->Free(alloc, p->probs);
2560 +static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
2562 + alloc->Free(alloc, p->dic);
2566 +void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
2568 + LzmaDec_FreeProbs(p, alloc);
2569 + LzmaDec_FreeDict(p, alloc);
2572 +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
2577 + if (size < LZMA_PROPS_SIZE)
2578 + return SZ_ERROR_UNSUPPORTED;
2580 + dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
2582 + if (dicSize < LZMA_DIC_MIN)
2583 + dicSize = LZMA_DIC_MIN;
2584 + p->dicSize = dicSize;
2587 + if (d >= (9 * 5 * 5))
2588 + return SZ_ERROR_UNSUPPORTED;
2598 +static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
2600 + UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
2601 + if (p->probs == 0 || numProbs != p->numProbs)
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;
2612 +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2614 + CLzmaProps propNew;
2615 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2616 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2617 + p->prop = propNew;
2621 +SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2623 + CLzmaProps propNew;
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)
2630 + LzmaDec_FreeDict(p, alloc);
2631 + p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
2634 + LzmaDec_FreeProbs(p, alloc);
2635 + return SZ_ERROR_MEM;
2638 + p->dicBufSize = dicBufSize;
2639 + p->prop = propNew;
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)
2649 + SizeT inSize = *srcLen;
2650 + SizeT outSize = *destLen;
2651 + *srcLen = *destLen = 0;
2652 + if (inSize < RC_INIT_SIZE)
2653 + return SZ_ERROR_INPUT_EOF;
2655 + LzmaDec_Construct(&p);
2656 + res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
2660 + p.dicBufSize = outSize;
2665 + res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
2667 + if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
2668 + res = SZ_ERROR_INPUT_EOF;
2670 + (*destLen) = p.dicPos;
2671 + LzmaDec_FreeProbs(&p, alloc);
2675 +++ b/lzma/LzmaEnc.c
2677 +/* LzmaEnc.c -- LZMA Encoder
2679 +Copyright (c) 1999-2008 Igor Pavlov
2680 +Read LzmaEnc.h for license options */
2682 +#if defined(SHOW_STAT) || defined(SHOW_STAT2)
2686 +#include <string.h>
2688 +#include "LzmaEnc.h"
2690 +#include "LzFind.h"
2691 +#ifdef COMPRESS_MF_MT
2692 +#include "LzFindMt.h"
2695 +/* #define SHOW_STAT */
2696 +/* #define SHOW_STAT2 */
2699 +static int ttt = 0;
2702 +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
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)
2709 +#define kNumMaxDirectBits (31)
2711 +#define kNumTopBits 24
2712 +#define kTopValue ((UInt32)1 << kNumTopBits)
2714 +#define kNumBitModelTotalBits 11
2715 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
2716 +#define kNumMoveBits 5
2717 +#define kProbInitValue (kBitModelTotal >> 1)
2719 +#define kNumMoveReducingBits 4
2720 +#define kNumBitPriceShiftBits 4
2721 +#define kBitPrice (1 << kNumBitPriceShiftBits)
2723 +void LzmaEncProps_Init(CLzmaEncProps *p)
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;
2731 +void LzmaEncProps_Normalize(CLzmaEncProps *p)
2733 + int level = p->level;
2734 + if (level < 0) level = 5;
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);
2748 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
2750 + CLzmaEncProps props = *props2;
2751 + LzmaEncProps_Normalize(&props);
2752 + return props.dictSize;
2755 +/* #define LZMA_LOG_BSR */
2756 +/* Define it for Intel's CPU */
2759 +#ifdef LZMA_LOG_BSR
2761 +#define kDicLogSizeMaxCompress 30
2763 +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
2765 +UInt32 GetPosSlot1(UInt32 pos)
2768 + BSR2_RET(pos, res);
2771 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
2772 +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
2776 +#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
2777 +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
2779 +void LzmaEnc_FastPosInit(Byte *g_FastPos)
2781 + int c = 2, slotFast;
2785 + for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
2787 + UInt32 k = (1 << ((slotFast >> 1) - 1));
2789 + for (j = 0; j < k; j++, c++)
2790 + g_FastPos[c] = (Byte)slotFast;
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); }
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; }
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); }
2810 +#define LZMA_NUM_REPS 4
2812 +typedef unsigned CState;
2814 +typedef struct _COptimal
2827 + UInt32 backs[LZMA_NUM_REPS];
2830 +#define kNumOpts (1 << 12)
2832 +#define kNumLenToPosStates 4
2833 +#define kNumPosSlotBits 6
2834 +#define kDicLogSizeMin 0
2835 +#define kDicLogSizeMax 32
2836 +#define kDistTableSizeMax (kDicLogSizeMax * 2)
2839 +#define kNumAlignBits 4
2840 +#define kAlignTableSize (1 << kNumAlignBits)
2841 +#define kAlignMask (kAlignTableSize - 1)
2843 +#define kStartPosModelIndex 4
2844 +#define kEndPosModelIndex 14
2845 +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
2847 +#define kNumFullDistances (1 << (kEndPosModelIndex / 2))
2849 +#ifdef _LZMA_PROB32
2850 +#define CLzmaProb UInt32
2852 +#define CLzmaProb UInt16
2855 +#define LZMA_PB_MAX 4
2856 +#define LZMA_LC_MAX 8
2857 +#define LZMA_LP_MAX 4
2859 +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
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)
2869 +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
2871 +#define LZMA_MATCH_LEN_MIN 2
2872 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
2874 +#define kNumStates 12
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];
2888 + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
2890 + UInt32 counters[LZMA_NUM_PB_STATES_MAX];
2893 +typedef struct _CRangeEnc
2902 + ISeqOutStream *outStream;
2907 +typedef struct _CSeqInStreamBuf
2909 + ISeqInStream funcTable;
2914 +static SRes MyRead(void *pp, void *data, size_t *size)
2916 + size_t curSize = *size;
2917 + CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp;
2918 + if (p->rem < curSize)
2920 + memcpy(data, p->data, curSize);
2921 + p->rem -= curSize;
2922 + p->data += curSize;
2929 + CLzmaProb *litProbs;
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];
2938 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
2939 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
2940 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
2942 + CLenPriceEnc lenEnc;
2943 + CLenPriceEnc repLenEnc;
2945 + UInt32 reps[LZMA_NUM_REPS];
2949 +typedef struct _CLzmaEnc
2951 + IMatchFinder matchFinder;
2952 + void *matchFinderObj;
2954 + #ifdef COMPRESS_MF_MT
2956 + CMatchFinderMt matchFinderMt;
2959 + CMatchFinder matchFinderBase;
2961 + #ifdef COMPRESS_MF_MT
2965 + UInt32 optimumEndIndex;
2966 + UInt32 optimumCurrentIndex;
2968 + Bool longestMatchWasFound;
2969 + UInt32 longestMatchLength;
2970 + UInt32 numDistancePairs;
2972 + COptimal opt[kNumOpts];
2974 + #ifndef LZMA_LOG_BSR
2975 + Byte g_FastPos[1 << kNumLogBits];
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];
2985 + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
2986 + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
2987 + UInt32 alignPrices[kAlignTableSize];
2988 + UInt32 alignPriceCount;
2990 + UInt32 distTableSize;
2992 + unsigned lc, lp, pb;
2993 + unsigned lpMask, pbMask;
2995 + CLzmaProb *litProbs;
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];
3004 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
3005 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
3006 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
3008 + CLenPriceEnc lenEnc;
3009 + CLenPriceEnc repLenEnc;
3017 + Bool writeEndMark;
3019 + UInt32 matchPriceCount;
3025 + UInt32 matchFinderCycles;
3027 + ISeqInStream *inStream;
3028 + CSeqInStreamBuf seqBufInStream;
3030 + CSaveState saveState;
3033 +void LzmaEnc_SaveState(CLzmaEncHandle pp)
3035 + CLzmaEnc *p = (CLzmaEnc *)pp;
3036 + CSaveState *dest = &p->saveState;
3038 + dest->lenEnc = p->lenEnc;
3039 + dest->repLenEnc = p->repLenEnc;
3040 + dest->state = p->state;
3042 + for (i = 0; i < kNumStates; i++)
3044 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
3045 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
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));
3059 +void LzmaEnc_RestoreState(CLzmaEncHandle pp)
3061 + CLzmaEnc *dest = (CLzmaEnc *)pp;
3062 + const CSaveState *p = &dest->saveState;
3064 + dest->lenEnc = p->lenEnc;
3065 + dest->repLenEnc = p->repLenEnc;
3066 + dest->state = p->state;
3068 + for (i = 0; i < kNumStates; i++)
3070 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
3071 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
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));
3085 +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
3087 + CLzmaEnc *p = (CLzmaEnc *)pp;
3088 + CLzmaEncProps props = *props2;
3089 + LzmaEncProps_Normalize(&props);
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;
3097 + unsigned fb = props.fb;
3100 + if (fb > LZMA_MATCH_LEN_MAX)
3101 + fb = LZMA_MATCH_LEN_MAX;
3102 + p->numFastBytes = fb;
3107 + p->fastMode = (props.algo == 0);
3108 + p->matchFinderBase.btMode = props.btMode;
3110 + UInt32 numHashBytes = 4;
3113 + if (props.numHashBytes < 2)
3115 + else if (props.numHashBytes < 4)
3116 + numHashBytes = props.numHashBytes;
3118 + p->matchFinderBase.numHashBytes = numHashBytes;
3121 + p->matchFinderBase.cutValue = props.mc;
3123 + p->writeEndMark = props.writeEndMark;
3125 + #ifdef COMPRESS_MF_MT
3127 + if (newMultiThread != _multiThread)
3129 + ReleaseMatchFinder();
3130 + _multiThread = newMultiThread;
3133 + p->multiThread = (props.numThreads > 1);
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};
3145 + void UpdateChar() { Index = kLiteralNextStates[Index]; }
3146 + void UpdateMatch() { Index = kMatchNextStates[Index]; }
3147 + void UpdateRep() { Index = kRepNextStates[Index]; }
3148 + void UpdateShortRep() { Index = kShortRepNextStates[Index]; }
3151 +#define IsCharState(s) ((s) < 7)
3154 +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
3156 +#define kInfinityPrice (1 << 30)
3158 +static void RangeEnc_Construct(CRangeEnc *p)
3164 +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
3166 +#define RC_BUF_SIZE (1 << 16)
3167 +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
3169 + if (p->bufBase == 0)
3171 + p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
3172 + if (p->bufBase == 0)
3174 + p->bufLim = p->bufBase + RC_BUF_SIZE;
3179 +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
3181 + alloc->Free(alloc, p->bufBase);
3185 +static void RangeEnc_Init(CRangeEnc *p)
3187 + /* Stream.Init(); */
3189 + p->range = 0xFFFFFFFF;
3193 + p->buf = p->bufBase;
3199 +static void RangeEnc_FlushStream(CRangeEnc *p)
3202 + if (p->res != SZ_OK)
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;
3211 +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
3213 + if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
3215 + Byte temp = p->cache;
3218 + Byte *buf = p->buf;
3219 + *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
3221 + if (buf == p->bufLim)
3222 + RangeEnc_FlushStream(p);
3225 + while (--p->cacheSize != 0);
3226 + p->cache = (Byte)((UInt32)p->low >> 24);
3229 + p->low = (UInt32)p->low << 8;
3232 +static void RangeEnc_FlushData(CRangeEnc *p)
3235 + for (i = 0; i < 5; i++)
3236 + RangeEnc_ShiftLow(p);
3239 +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
3244 + p->low += p->range & (0 - ((value >> --numBits) & 1));
3245 + if (p->range < kTopValue)
3248 + RangeEnc_ShiftLow(p);
3251 + while (numBits != 0);
3254 +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
3256 + UInt32 ttt = *prob;
3257 + UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
3260 + p->range = newBound;
3261 + ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
3265 + p->low += newBound;
3266 + p->range -= newBound;
3267 + ttt -= ttt >> kNumMoveBits;
3269 + *prob = (CLzmaProb)ttt;
3270 + if (p->range < kTopValue)
3273 + RangeEnc_ShiftLow(p);
3277 +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
3282 + RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
3285 + while (symbol < 0x10000);
3288 +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
3290 + UInt32 offs = 0x100;
3295 + RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
3297 + offs &= ~(matchByte ^ symbol);
3299 + while (symbol < 0x10000);
3302 +void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
3305 + for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
3307 + const int kCyclesBits = kNumBitPriceShiftBits;
3309 + UInt32 bitCount = 0;
3311 + for (j = 0; j < kCyclesBits; j++)
3315 + while (w >= ((UInt32)1 << 16))
3321 + ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
3326 +#define GET_PRICE(prob, symbol) \
3327 + p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3329 +#define GET_PRICEa(prob, symbol) \
3330 + ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3332 +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
3333 +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3335 +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
3336 +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3338 +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
3344 + price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
3347 + while (symbol < 0x10000);
3351 +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
3354 + UInt32 offs = 0x100;
3359 + price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
3361 + offs &= ~(matchByte ^ symbol);
3363 + while (symbol < 0x10000);
3368 +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3372 + for (i = numBitLevels; i != 0 ;)
3376 + bit = (symbol >> i) & 1;
3377 + RangeEnc_EncodeBit(rc, probs + m, bit);
3378 + m = (m << 1) | bit;
3382 +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3386 + for (i = 0; i < numBitLevels; i++)
3388 + UInt32 bit = symbol & 1;
3389 + RangeEnc_EncodeBit(rc, probs + m, bit);
3390 + m = (m << 1) | bit;
3395 +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3398 + symbol |= (1 << numBitLevels);
3399 + while (symbol != 1)
3401 + price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
3407 +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3412 + for (i = numBitLevels; i != 0; i--)
3414 + UInt32 bit = symbol & 1;
3416 + price += GET_PRICEa(probs[m], bit);
3417 + m = (m << 1) | bit;
3423 +static void LenEnc_Init(CLenEnc *p)
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;
3435 +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
3437 + if (symbol < kLenNumLowSymbols)
3439 + RangeEnc_EncodeBit(rc, &p->choice, 0);
3440 + RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
3444 + RangeEnc_EncodeBit(rc, &p->choice, 1);
3445 + if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
3447 + RangeEnc_EncodeBit(rc, &p->choice2, 0);
3448 + RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
3452 + RangeEnc_EncodeBit(rc, &p->choice2, 1);
3453 + RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
3458 +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
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);
3465 + for (i = 0; i < kLenNumLowSymbols; i++)
3467 + if (i >= numSymbols)
3469 + prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
3471 + for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
3473 + if (i >= numSymbols)
3475 + prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
3477 + for (; i < numSymbols; i++)
3478 + prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
3481 +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
3483 + LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
3484 + p->counters[posState] = p->tableSize;
3487 +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
3490 + for (posState = 0; posState < numPosStates; posState++)
3491 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3494 +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
3496 + LenEnc_Encode(&p->p, rc, symbol, posState);
3498 + if (--p->counters[posState] == 0)
3499 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3505 +static void MovePos(CLzmaEnc *p, UInt32 num)
3509 + printf("\n MovePos %d", num);
3513 + p->additionalOffset += num;
3514 + p->matchFinder.Skip(p->matchFinderObj, num);
3518 +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
3520 + UInt32 lenRes = 0, numDistancePairs;
3521 + numDistancePairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matchDistances);
3523 + printf("\n i = %d numPairs = %d ", ttt, numDistancePairs / 2);
3530 + for (i = 0; i < numDistancePairs; i += 2)
3531 + printf("%2d %6d | ", p->matchDistances[i], p->matchDistances[i + 1]);
3534 + if (numDistancePairs > 0)
3536 + lenRes = p->matchDistances[numDistancePairs - 2];
3537 + if (lenRes == p->numFastBytes)
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;
3546 + const Byte *pby2 = pby - distance;
3547 + for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
3551 + p->additionalOffset++;
3552 + *numDistancePairsRes = numDistancePairs;
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)
3561 +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
3564 + GET_PRICE_0(p->isRepG0[state]) +
3565 + GET_PRICE_0(p->isRep0Long[state][posState]);
3568 +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
3571 + if (repIndex == 0)
3573 + price = GET_PRICE_0(p->isRepG0[state]);
3574 + price += GET_PRICE_1(p->isRep0Long[state][posState]);
3578 + price = GET_PRICE_1(p->isRepG0[state]);
3579 + if (repIndex == 1)
3580 + price += GET_PRICE_0(p->isRepG1[state]);
3583 + price += GET_PRICE_1(p->isRepG1[state]);
3584 + price += GET_PRICE(p->isRepG2[state], repIndex - 2);
3590 +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
3592 + return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
3593 + GetPureRepPrice(p, repIndex, state, posState);
3596 +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
3598 + UInt32 posMem = p->opt[cur].posPrev;
3599 + UInt32 backMem = p->opt[cur].backPrev;
3600 + p->optimumEndIndex = cur;
3603 + if (p->opt[cur].prev1IsChar)
3605 + MakeAsChar(&p->opt[posMem])
3606 + p->opt[posMem].posPrev = posMem - 1;
3607 + if (p->opt[cur].prev2)
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;
3615 + UInt32 posPrev = posMem;
3616 + UInt32 backCur = backMem;
3618 + backMem = p->opt[posPrev].backPrev;
3619 + posMem = p->opt[posPrev].posPrev;
3621 + p->opt[posPrev].backPrev = backCur;
3622 + p->opt[posPrev].posPrev = cur;
3627 + *backRes = p->opt[0].backPrev;
3628 + p->optimumCurrentIndex = p->opt[0].posPrev;
3629 + return p->optimumCurrentIndex;
3632 +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
3634 +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
3636 + UInt32 numAvailableBytes, lenMain, numDistancePairs;
3638 + UInt32 reps[LZMA_NUM_REPS];
3639 + UInt32 repLens[LZMA_NUM_REPS];
3640 + UInt32 repMaxIndex, i;
3641 + UInt32 *matchDistances;
3642 + Byte currentByte, matchByte;
3644 + UInt32 matchPrice, repMatchPrice;
3647 + UInt32 normalMatchPrice;
3649 + if (p->optimumEndIndex != p->optimumCurrentIndex)
3651 + const COptimal *opt = &p->opt[p->optimumCurrentIndex];
3652 + UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
3653 + *backRes = opt->backPrev;
3654 + p->optimumCurrentIndex = opt->posPrev;
3657 + p->optimumCurrentIndex = p->optimumEndIndex = 0;
3659 + numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
3661 + if (!p->longestMatchWasFound)
3663 + lenMain = ReadMatchDistances(p, &numDistancePairs);
3667 + lenMain = p->longestMatchLength;
3668 + numDistancePairs = p->numDistancePairs;
3669 + p->longestMatchWasFound = False;
3672 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3673 + if (numAvailableBytes < 2)
3675 + *backRes = (UInt32)(-1);
3678 + if (numAvailableBytes > LZMA_MATCH_LEN_MAX)
3679 + numAvailableBytes = LZMA_MATCH_LEN_MAX;
3682 + for (i = 0; i < LZMA_NUM_REPS; i++)
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])
3693 + for (lenTest = 2; lenTest < numAvailableBytes && data[lenTest] == data2[lenTest]; lenTest++);
3694 + repLens[i] = lenTest;
3695 + if (lenTest > repLens[repMaxIndex])
3698 + if (repLens[repMaxIndex] >= p->numFastBytes)
3701 + *backRes = repMaxIndex;
3702 + lenRes = repLens[repMaxIndex];
3703 + MovePos(p, lenRes - 1);
3707 + matchDistances = p->matchDistances;
3708 + if (lenMain >= p->numFastBytes)
3710 + *backRes = matchDistances[numDistancePairs - 1] + LZMA_NUM_REPS;
3711 + MovePos(p, lenMain - 1);
3714 + currentByte = *data;
3715 + matchByte = *(data - (reps[0] + 1));
3717 + if (lenMain < 2 && currentByte != matchByte && repLens[repMaxIndex] < 2)
3719 + *backRes = (UInt32)-1;
3723 + p->opt[0].state = (CState)p->state;
3725 + posState = (position & p->pbMask);
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));
3735 + MakeAsChar(&p->opt[1]);
3737 + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
3738 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
3740 + if (matchByte == currentByte)
3742 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
3743 + if (shortRepPrice < p->opt[1].price)
3745 + p->opt[1].price = shortRepPrice;
3746 + MakeAsShortRep(&p->opt[1]);
3749 + lenEnd = ((lenMain >= repLens[repMaxIndex]) ? lenMain : repLens[repMaxIndex]);
3753 + *backRes = p->opt[1].backPrev;
3757 + p->opt[1].posPrev = 0;
3758 + for (i = 0; i < LZMA_NUM_REPS; i++)
3759 + p->opt[0].backs[i] = reps[i];
3763 + p->opt[len--].price = kInfinityPrice;
3766 + for (i = 0; i < LZMA_NUM_REPS; i++)
3768 + UInt32 repLen = repLens[i];
3772 + price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
3775 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
3776 + COptimal *opt = &p->opt[repLen];
3777 + if (curAndLenPrice < opt->price)
3779 + opt->price = curAndLenPrice;
3781 + opt->backPrev = i;
3782 + opt->prev1IsChar = False;
3785 + while (--repLen >= 2);
3788 + normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
3790 + len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
3791 + if (len <= lenMain)
3794 + while (len > matchDistances[offs])
3799 + UInt32 distance = matchDistances[offs + 1];
3801 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];