1 From: Alexandros C. Couloumbis <alex@ozo.com>
2 Subject: fs: add jffs2/lzma support (not activated by default yet)
4 lede-commit: c2c88d315fa0e881f8b19da07b62859b915b11b2
5 Signed-off-by: Alexandros C. Couloumbis <alex@ozo.com>
8 fs/jffs2/Makefile | 3 +
10 fs/jffs2/compr.h | 10 +-
11 fs/jffs2/compr_lzma.c | 128 +++
12 fs/jffs2/super.c | 33 +-
13 include/linux/lzma.h | 62 ++
14 include/linux/lzma/LzFind.h | 115 +++
15 include/linux/lzma/LzHash.h | 54 +
16 include/linux/lzma/LzmaDec.h | 231 +++++
17 include/linux/lzma/LzmaEnc.h | 80 ++
18 include/linux/lzma/Types.h | 226 +++++
19 include/uapi/linux/jffs2.h | 1 +
22 lib/lzma/LzFind.c | 761 ++++++++++++++
23 lib/lzma/LzmaDec.c | 999 +++++++++++++++++++
24 lib/lzma/LzmaEnc.c | 2271 ++++++++++++++++++++++++++++++++++++++++++
25 lib/lzma/Makefile | 7 +
26 19 files changed, 5008 insertions(+), 6 deletions(-)
27 create mode 100644 fs/jffs2/compr_lzma.c
28 create mode 100644 include/linux/lzma.h
29 create mode 100644 include/linux/lzma/LzFind.h
30 create mode 100644 include/linux/lzma/LzHash.h
31 create mode 100644 include/linux/lzma/LzmaDec.h
32 create mode 100644 include/linux/lzma/LzmaEnc.h
33 create mode 100644 include/linux/lzma/Types.h
34 create mode 100644 lib/lzma/LzFind.c
35 create mode 100644 lib/lzma/LzmaDec.c
36 create mode 100644 lib/lzma/LzmaEnc.c
37 create mode 100644 lib/lzma/Makefile
39 --- a/fs/jffs2/Kconfig
40 +++ b/fs/jffs2/Kconfig
41 @@ -136,6 +136,15 @@ config JFFS2_LZO
42 This feature was added in July, 2007. Say 'N' if you need
43 compatibility with older bootloaders or kernels.
46 + bool "JFFS2 LZMA compression support" if JFFS2_COMPRESSION_OPTIONS
47 + select LZMA_COMPRESS
48 + select LZMA_DECOMPRESS
52 + JFFS2 wrapper to the LZMA C SDK
55 bool "JFFS2 RTIME compression support" if JFFS2_COMPRESSION_OPTIONS
57 --- a/fs/jffs2/Makefile
58 +++ b/fs/jffs2/Makefile
59 @@ -19,4 +19,7 @@ jffs2-$(CONFIG_JFFS2_RUBIN) += compr_rub
60 jffs2-$(CONFIG_JFFS2_RTIME) += compr_rtime.o
61 jffs2-$(CONFIG_JFFS2_ZLIB) += compr_zlib.o
62 jffs2-$(CONFIG_JFFS2_LZO) += compr_lzo.o
63 +jffs2-$(CONFIG_JFFS2_LZMA) += compr_lzma.o
64 jffs2-$(CONFIG_JFFS2_SUMMARY) += summary.o
66 +CFLAGS_compr_lzma.o += -Iinclude/linux -Ilib/lzma
67 --- a/fs/jffs2/compr.c
68 +++ b/fs/jffs2/compr.c
69 @@ -381,6 +381,9 @@ int __init jffs2_compressors_init(void)
70 ret = jffs2_lzo_init();
73 + ret = jffs2_lzma_init();
78 /* Setting default compression mode */
79 @@ -402,6 +405,8 @@ int __init jffs2_compressors_init(void)
86 jffs2_dynrubin_exit();
88 @@ -417,6 +422,7 @@ exit:
89 int jffs2_compressors_exit(void)
91 /* Unregistering compressors */
94 jffs2_dynrubin_exit();
95 jffs2_rubinmips_exit();
96 --- a/fs/jffs2/compr.h
97 +++ b/fs/jffs2/compr.h
99 #define JFFS2_DYNRUBIN_PRIORITY 20
100 #define JFFS2_LZARI_PRIORITY 30
101 #define JFFS2_RTIME_PRIORITY 50
102 -#define JFFS2_ZLIB_PRIORITY 60
103 -#define JFFS2_LZO_PRIORITY 80
105 +#define JFFS2_LZMA_PRIORITY 70
106 +#define JFFS2_ZLIB_PRIORITY 80
107 +#define JFFS2_LZO_PRIORITY 90
109 #define JFFS2_RUBINMIPS_DISABLED /* RUBINs will be used only */
110 #define JFFS2_DYNRUBIN_DISABLED /* for decompression */
111 @@ -115,5 +115,12 @@ extern void jffs2_lzo_exit(void);
112 static inline int jffs2_lzo_init(void) { return 0; }
113 static inline void jffs2_lzo_exit(void) {}
115 +#ifdef CONFIG_JFFS2_LZMA
116 +extern int jffs2_lzma_init(void);
117 +extern void jffs2_lzma_exit(void);
119 +static inline int jffs2_lzma_init(void) { return 0; }
120 +static inline void jffs2_lzma_exit(void) {}
123 #endif /* __JFFS2_COMPR_H__ */
125 +++ b/fs/jffs2/compr_lzma.c
128 + * JFFS2 -- Journalling Flash File System, Version 2.
130 + * For licensing information, see the file 'LICENCE' in this directory.
132 + * JFFS2 wrapper to the LZMA C SDK
136 +#include <linux/lzma.h>
140 + static DEFINE_MUTEX(deflate_mutex);
144 +Byte propsEncoded[LZMA_PROPS_SIZE];
145 +SizeT propsSize = sizeof(propsEncoded);
147 +STATIC void lzma_free_workspace(void)
149 + LzmaEnc_Destroy(p, &lzma_alloc, &lzma_alloc);
152 +STATIC int INIT lzma_alloc_workspace(CLzmaEncProps *props)
154 + if ((p = (CLzmaEncHandle *)LzmaEnc_Create(&lzma_alloc)) == NULL)
156 + PRINT_ERROR("Failed to allocate lzma deflate workspace\n");
160 + if (LzmaEnc_SetProps(p, props) != SZ_OK)
162 + lzma_free_workspace();
166 + if (LzmaEnc_WriteProperties(p, propsEncoded, &propsSize) != SZ_OK)
168 + lzma_free_workspace();
175 +STATIC int jffs2_lzma_compress(unsigned char *data_in, unsigned char *cpage_out,
176 + uint32_t *sourcelen, uint32_t *dstlen)
178 + SizeT compress_size = (SizeT)(*dstlen);
182 + mutex_lock(&deflate_mutex);
185 + ret = LzmaEnc_MemEncode(p, cpage_out, &compress_size, data_in, *sourcelen,
186 + 0, NULL, &lzma_alloc, &lzma_alloc);
189 + mutex_unlock(&deflate_mutex);
195 + *dstlen = (uint32_t)compress_size;
200 +STATIC int jffs2_lzma_decompress(unsigned char *data_in, unsigned char *cpage_out,
201 + uint32_t srclen, uint32_t destlen)
204 + SizeT dl = (SizeT)destlen;
205 + SizeT sl = (SizeT)srclen;
206 + ELzmaStatus status;
208 + ret = LzmaDecode(cpage_out, &dl, data_in, &sl, propsEncoded,
209 + propsSize, LZMA_FINISH_ANY, &status, &lzma_alloc);
211 + if (ret != SZ_OK || status == LZMA_STATUS_NOT_FINISHED || dl != (SizeT)destlen)
217 +static struct jffs2_compressor jffs2_lzma_comp = {
218 + .priority = JFFS2_LZMA_PRIORITY,
220 + .compr = JFFS2_COMPR_LZMA,
221 + .compress = &jffs2_lzma_compress,
222 + .decompress = &jffs2_lzma_decompress,
226 +int INIT jffs2_lzma_init(void)
229 + CLzmaEncProps props;
230 + LzmaEncProps_Init(&props);
232 + props.dictSize = LZMA_BEST_DICT(0x2000);
233 + props.level = LZMA_BEST_LEVEL;
234 + props.lc = LZMA_BEST_LC;
235 + props.lp = LZMA_BEST_LP;
236 + props.pb = LZMA_BEST_PB;
237 + props.fb = LZMA_BEST_FB;
239 + ret = lzma_alloc_workspace(&props);
243 + ret = jffs2_register_compressor(&jffs2_lzma_comp);
245 + lzma_free_workspace();
250 +void jffs2_lzma_exit(void)
252 + jffs2_unregister_compressor(&jffs2_lzma_comp);
253 + lzma_free_workspace();
255 --- a/fs/jffs2/super.c
256 +++ b/fs/jffs2/super.c
257 @@ -374,14 +374,41 @@ static int __init init_jffs2_fs(void)
258 BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
259 BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
261 - pr_info("version 2.2."
262 + pr_info("version 2.2"
263 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
266 #ifdef CONFIG_JFFS2_SUMMARY
270 - " © 2001-2006 Red Hat, Inc.\n");
271 +#ifdef CONFIG_JFFS2_ZLIB
274 +#ifdef CONFIG_JFFS2_LZO
277 +#ifdef CONFIG_JFFS2_LZMA
280 +#ifdef CONFIG_JFFS2_RTIME
283 +#ifdef CONFIG_JFFS2_RUBIN
286 +#ifdef CONFIG_JFFS2_CMODE_NONE
289 +#ifdef CONFIG_JFFS2_CMODE_PRIORITY
290 + " (CMODE_PRIORITY)"
292 +#ifdef CONFIG_JFFS2_CMODE_SIZE
295 +#ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
296 + " (CMODE_FAVOURLZO)"
298 + " (c) 2001-2006 Red Hat, Inc.\n");
300 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
301 sizeof(struct jffs2_inode_info),
303 +++ b/include/linux/lzma.h
309 + #include <linux/kernel.h>
310 + #include <linux/sched.h>
311 + #include <linux/slab.h>
312 + #include <linux/vmalloc.h>
313 + #include <linux/init.h>
314 + #define LZMA_MALLOC vmalloc
315 + #define LZMA_FREE vfree
316 + #define PRINT_ERROR(msg) printk(KERN_WARNING #msg)
317 + #define INIT __init
318 + #define STATIC static
320 + #include <stdint.h>
321 + #include <stdlib.h>
323 + #include <unistd.h>
324 + #include <string.h>
325 + #include <asm/types.h>
327 + #include <linux/jffs2.h>
329 + extern int page_size;
330 + #define PAGE_SIZE page_size
332 + #define LZMA_MALLOC malloc
333 + #define LZMA_FREE free
334 + #define PRINT_ERROR(msg) fprintf(stderr, msg)
339 +#include "lzma/LzmaDec.h"
340 +#include "lzma/LzmaEnc.h"
342 +#define LZMA_BEST_LEVEL (9)
343 +#define LZMA_BEST_LC (0)
344 +#define LZMA_BEST_LP (0)
345 +#define LZMA_BEST_PB (0)
346 +#define LZMA_BEST_FB (273)
348 +#define LZMA_BEST_DICT(n) (((int)((n) / 2)) * 2)
350 +static void *p_lzma_malloc(void *p, size_t size)
355 + return LZMA_MALLOC(size);
358 +static void p_lzma_free(void *p, void *address)
360 + if (address != NULL)
361 + LZMA_FREE(address);
364 +static ISzAlloc lzma_alloc = { .Alloc = p_lzma_malloc, .Free = p_lzma_free };
368 +++ b/include/linux/lzma/LzFind.h
370 +/* LzFind.h -- Match finder for LZ algorithms
371 +2009-04-22 : Igor Pavlov : Public domain */
382 +typedef UInt32 CLzRef;
384 +typedef struct _CMatchFinder
392 + UInt32 cyclicBufferPos;
393 + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */
395 + UInt32 matchMaxLen;
402 + ISeqInStream *stream;
403 + int streamEndWasReached;
406 + UInt32 keepSizeBefore;
407 + UInt32 keepSizeAfter;
409 + UInt32 numHashBytes;
411 + size_t directInputRem;
414 + UInt32 historySize;
415 + UInt32 fixedHashSize;
416 + UInt32 hashSizeSum;
422 +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer)
423 +#define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)])
425 +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos)
427 +int MatchFinder_NeedMove(CMatchFinder *p);
428 +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p);
429 +void MatchFinder_MoveBlock(CMatchFinder *p);
430 +void MatchFinder_ReadIfRequired(CMatchFinder *p);
432 +void MatchFinder_Construct(CMatchFinder *p);
435 + historySize <= 3 GB
436 + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB
438 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
439 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
441 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc);
442 +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems);
443 +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue);
445 +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son,
446 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue,
447 + UInt32 *distances, UInt32 maxLen);
451 + Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func.
452 + Mf_GetPointerToCurrentPos_Func's result must be used only before any other function
455 +typedef void (*Mf_Init_Func)(void *object);
456 +typedef Byte (*Mf_GetIndexByte_Func)(void *object, Int32 index);
457 +typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object);
458 +typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object);
459 +typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances);
460 +typedef void (*Mf_Skip_Func)(void *object, UInt32);
462 +typedef struct _IMatchFinder
465 + Mf_GetIndexByte_Func GetIndexByte;
466 + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes;
467 + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos;
468 + Mf_GetMatches_Func GetMatches;
472 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable);
474 +void MatchFinder_Init(CMatchFinder *p);
475 +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances);
476 +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances);
477 +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num);
478 +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num);
486 +++ b/include/linux/lzma/LzHash.h
488 +/* LzHash.h -- HASH functions for LZ algorithms
489 +2009-02-07 : Igor Pavlov : Public domain */
494 +#define kHash2Size (1 << 10)
495 +#define kHash3Size (1 << 16)
496 +#define kHash4Size (1 << 20)
498 +#define kFix3HashSize (kHash2Size)
499 +#define kFix4HashSize (kHash2Size + kHash3Size)
500 +#define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size)
502 +#define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8);
504 +#define HASH3_CALC { \
505 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
506 + hash2Value = temp & (kHash2Size - 1); \
507 + hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; }
509 +#define HASH4_CALC { \
510 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
511 + hash2Value = temp & (kHash2Size - 1); \
512 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
513 + hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; }
515 +#define HASH5_CALC { \
516 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
517 + hash2Value = temp & (kHash2Size - 1); \
518 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
519 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \
520 + hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \
521 + hash4Value &= (kHash4Size - 1); }
523 +/* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */
524 +#define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF;
527 +#define MT_HASH2_CALC \
528 + hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1);
530 +#define MT_HASH3_CALC { \
531 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
532 + hash2Value = temp & (kHash2Size - 1); \
533 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); }
535 +#define MT_HASH4_CALC { \
536 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
537 + hash2Value = temp & (kHash2Size - 1); \
538 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
539 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); }
543 +++ b/include/linux/lzma/LzmaDec.h
545 +/* LzmaDec.h -- LZMA Decoder
546 +2009-02-07 : Igor Pavlov : Public domain */
548 +#ifndef __LZMA_DEC_H
549 +#define __LZMA_DEC_H
557 +/* #define _LZMA_PROB32 */
558 +/* _LZMA_PROB32 can increase the speed on some CPUs,
559 + but memory usage for CLzmaDec::probs will be doubled in that case */
562 +#define CLzmaProb UInt32
564 +#define CLzmaProb UInt16
568 +/* ---------- LZMA Properties ---------- */
570 +#define LZMA_PROPS_SIZE 5
572 +typedef struct _CLzmaProps
574 + unsigned lc, lp, pb;
578 +/* LzmaProps_Decode - decodes properties
581 + SZ_ERROR_UNSUPPORTED - Unsupported properties
584 +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
587 +/* ---------- LZMA Decoder state ---------- */
589 +/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
590 + Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
592 +#define LZMA_REQUIRED_INPUT_MAX 20
600 + UInt32 range, code;
603 + UInt32 processedPos;
604 + UInt32 checkDicSize;
607 + unsigned remainLen;
611 + unsigned tempBufSize;
612 + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
615 +#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
617 +void LzmaDec_Init(CLzmaDec *p);
619 +/* There are two types of LZMA streams:
620 + 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
621 + 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
625 + LZMA_FINISH_ANY, /* finish at any point */
626 + LZMA_FINISH_END /* block must be finished at the end */
629 +/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
631 + You must use LZMA_FINISH_END, when you know that current output buffer
632 + covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
634 + If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
635 + and output value of destLen will be less than output buffer size limit.
636 + You can check status result also.
638 + You can use multiple checks to test data integrity after full decompression:
639 + 1) Check Result and "status" variable.
640 + 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
641 + 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
642 + You must use correct finish mode in that case. */
646 + LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
647 + LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
648 + LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
649 + LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
650 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
653 +/* ELzmaStatus is used only as output value for function call */
656 +/* ---------- Interfaces ---------- */
658 +/* There are 3 levels of interfaces:
659 + 1) Dictionary Interface
660 + 2) Buffer Interface
661 + 3) One Call Interface
662 + You can select any of these interfaces, but don't mix functions from different
663 + groups for same object. */
666 +/* There are two variants to allocate state for Dictionary Interface:
667 + 1) LzmaDec_Allocate / LzmaDec_Free
668 + 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
669 + You can use variant 2, if you set dictionary buffer manually.
670 + For Buffer Interface you must always use variant 1.
672 +LzmaDec_Allocate* can return:
674 + SZ_ERROR_MEM - Memory allocation error
675 + SZ_ERROR_UNSUPPORTED - Unsupported properties
678 +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
679 +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
681 +SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc);
682 +void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc);
684 +/* ---------- Dictionary Interface ---------- */
686 +/* You can use it, if you want to eliminate the overhead for data copying from
687 + dictionary to some other external buffer.
688 + You must work with CLzmaDec variables directly in this interface.
693 + for (each new stream)
696 + while (it needs more decompression)
698 + LzmaDec_DecodeToDic()
699 + use data from CLzmaDec::dic and update CLzmaDec::dicPos
705 +/* LzmaDec_DecodeToDic
707 + The decoding to internal dictionary buffer (CLzmaDec::dic).
708 + You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
711 + It has meaning only if the decoding reaches output limit (dicLimit).
712 + LZMA_FINISH_ANY - Decode just dicLimit bytes.
713 + LZMA_FINISH_END - Stream must be finished after dicLimit.
718 + LZMA_STATUS_FINISHED_WITH_MARK
719 + LZMA_STATUS_NOT_FINISHED
720 + LZMA_STATUS_NEEDS_MORE_INPUT
721 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
722 + SZ_ERROR_DATA - Data error
725 +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
726 + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
729 +/* ---------- Buffer Interface ---------- */
731 +/* It's zlib-like interface.
732 + See LzmaDec_DecodeToDic description for information about STEPS and return results,
733 + but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
734 + to work with CLzmaDec variables manually.
737 + It has meaning only if the decoding reaches output limit (*destLen).
738 + LZMA_FINISH_ANY - Decode just destLen bytes.
739 + LZMA_FINISH_END - Stream must be finished after (*destLen).
742 +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
743 + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
746 +/* ---------- One Call Interface ---------- */
751 + It has meaning only if the decoding reaches output limit (*destLen).
752 + LZMA_FINISH_ANY - Decode just destLen bytes.
753 + LZMA_FINISH_END - Stream must be finished after (*destLen).
758 + LZMA_STATUS_FINISHED_WITH_MARK
759 + LZMA_STATUS_NOT_FINISHED
760 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
761 + SZ_ERROR_DATA - Data error
762 + SZ_ERROR_MEM - Memory allocation error
763 + SZ_ERROR_UNSUPPORTED - Unsupported properties
764 + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
767 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
768 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
769 + ELzmaStatus *status, ISzAlloc *alloc);
777 +++ b/include/linux/lzma/LzmaEnc.h
779 +/* LzmaEnc.h -- LZMA Encoder
780 +2009-02-07 : Igor Pavlov : Public domain */
782 +#ifndef __LZMA_ENC_H
783 +#define __LZMA_ENC_H
791 +#define LZMA_PROPS_SIZE 5
793 +typedef struct _CLzmaEncProps
795 + int level; /* 0 <= level <= 9 */
796 + UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
797 + (1 << 12) <= dictSize <= (1 << 30) for 64-bit version
798 + default = (1 << 24) */
799 + int lc; /* 0 <= lc <= 8, default = 3 */
800 + int lp; /* 0 <= lp <= 4, default = 0 */
801 + int pb; /* 0 <= pb <= 4, default = 2 */
802 + int algo; /* 0 - fast, 1 - normal, default = 1 */
803 + int fb; /* 5 <= fb <= 273, default = 32 */
804 + int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
805 + int numHashBytes; /* 2, 3 or 4, default = 4 */
806 + UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */
807 + unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
808 + int numThreads; /* 1 or 2, default = 2 */
811 +void LzmaEncProps_Init(CLzmaEncProps *p);
812 +void LzmaEncProps_Normalize(CLzmaEncProps *p);
813 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2);
816 +/* ---------- CLzmaEncHandle Interface ---------- */
818 +/* LzmaEnc_* functions can return the following exit codes:
821 + SZ_ERROR_MEM - Memory allocation error
822 + SZ_ERROR_PARAM - Incorrect paramater in props
823 + SZ_ERROR_WRITE - Write callback error.
824 + SZ_ERROR_PROGRESS - some break from progress callback
825 + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
828 +typedef void * CLzmaEncHandle;
830 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc);
831 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig);
832 +SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props);
833 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size);
834 +SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream,
835 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
836 +SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
837 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
839 +/* ---------- One Call Interface ---------- */
844 + SZ_ERROR_MEM - Memory allocation error
845 + SZ_ERROR_PARAM - Incorrect paramater
846 + SZ_ERROR_OUTPUT_EOF - output buffer overflow
847 + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
850 +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
851 + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
852 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
860 +++ b/include/linux/lzma/Types.h
862 +/* Types.h -- Basic types
863 +2009-11-23 : Igor Pavlov : Public domain */
865 +#ifndef __7Z_TYPES_H
866 +#define __7Z_TYPES_H
871 +#include <windows.h>
874 +#ifndef EXTERN_C_BEGIN
876 +#define EXTERN_C_BEGIN extern "C" {
877 +#define EXTERN_C_END }
879 +#define EXTERN_C_BEGIN
880 +#define EXTERN_C_END
888 +#define SZ_ERROR_DATA 1
889 +#define SZ_ERROR_MEM 2
890 +#define SZ_ERROR_CRC 3
891 +#define SZ_ERROR_UNSUPPORTED 4
892 +#define SZ_ERROR_PARAM 5
893 +#define SZ_ERROR_INPUT_EOF 6
894 +#define SZ_ERROR_OUTPUT_EOF 7
895 +#define SZ_ERROR_READ 8
896 +#define SZ_ERROR_WRITE 9
897 +#define SZ_ERROR_PROGRESS 10
898 +#define SZ_ERROR_FAIL 11
899 +#define SZ_ERROR_THREAD 12
901 +#define SZ_ERROR_ARCHIVE 16
902 +#define SZ_ERROR_NO_ARCHIVE 17
913 +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
916 +typedef unsigned char Byte;
917 +typedef short Int16;
918 +typedef unsigned short UInt16;
920 +#ifdef _LZMA_UINT32_IS_ULONG
922 +typedef unsigned long UInt32;
925 +typedef unsigned int UInt32;
928 +#ifdef _SZ_NO_INT_64
930 +/* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.
931 + NOTES: Some code will work incorrectly in that case! */
934 +typedef unsigned long UInt64;
938 +#if defined(_MSC_VER) || defined(__BORLANDC__)
939 +typedef __int64 Int64;
940 +typedef unsigned __int64 UInt64;
942 +typedef long long int Int64;
943 +typedef unsigned long long int UInt64;
948 +#ifdef _LZMA_NO_SYSTEM_SIZE_T
949 +typedef UInt32 SizeT;
951 +typedef size_t SizeT;
960 +#define MY_STD_CALL __stdcall
967 +#if _MSC_VER >= 1300
968 +#define MY_NO_INLINE __declspec(noinline)
970 +#define MY_NO_INLINE
973 +#define MY_CDECL __cdecl
974 +#define MY_FAST_CALL __fastcall
979 +#define MY_FAST_CALL
984 +/* The following interfaces use first parameter as pointer to structure */
988 + SRes (*Read)(void *p, void *buf, size_t *size);
989 + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
990 + (output(*size) < input(*size)) is allowed */
993 +/* it can return SZ_ERROR_INPUT_EOF */
994 +SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size);
995 +SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType);
996 +SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf);
1000 + size_t (*Write)(void *p, const void *buf, size_t size);
1001 + /* Returns: result - the number of actually written bytes.
1002 + (result < size) means error */
1014 + SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */
1015 + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
1020 + SRes (*Look)(void *p, void **buf, size_t *size);
1021 + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
1022 + (output(*size) > input(*size)) is not allowed
1023 + (output(*size) < input(*size)) is allowed */
1024 + SRes (*Skip)(void *p, size_t offset);
1025 + /* offset must be <= output(*size) of Look */
1027 + SRes (*Read)(void *p, void *buf, size_t *size);
1028 + /* reads directly (without buffer). It's same as ISeqInStream::Read */
1029 + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
1032 +SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size);
1033 +SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset);
1035 +/* reads via ILookInStream::Read */
1036 +SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType);
1037 +SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size);
1039 +#define LookToRead_BUF_SIZE (1 << 14)
1044 + ISeekInStream *realStream;
1047 + Byte buf[LookToRead_BUF_SIZE];
1050 +void LookToRead_CreateVTable(CLookToRead *p, int lookahead);
1051 +void LookToRead_Init(CLookToRead *p);
1056 + ILookInStream *realStream;
1059 +void SecToLook_CreateVTable(CSecToLook *p);
1064 + ILookInStream *realStream;
1067 +void SecToRead_CreateVTable(CSecToRead *p);
1071 + SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize);
1072 + /* Returns: result. (result != SZ_OK) means break.
1073 + Value (UInt64)(Int64)-1 for size means unknown value. */
1074 +} ICompressProgress;
1078 + void *(*Alloc)(void *p, size_t size);
1079 + void (*Free)(void *p, void *address); /* address can be 0 */
1082 +#define IAlloc_Alloc(p, size) (p)->Alloc((p), size)
1083 +#define IAlloc_Free(p, a) (p)->Free((p), a)
1088 --- a/include/uapi/linux/jffs2.h
1089 +++ b/include/uapi/linux/jffs2.h
1091 #define JFFS2_COMPR_DYNRUBIN 0x05
1092 #define JFFS2_COMPR_ZLIB 0x06
1093 #define JFFS2_COMPR_LZO 0x07
1094 +#define JFFS2_COMPR_LZMA 0x08
1095 /* Compatibility flags. */
1096 #define JFFS2_COMPAT_MASK 0xc000 /* What do to if an unknown nodetype is found */
1097 #define JFFS2_NODE_ACCURATE 0x2000
1100 @@ -356,6 +356,12 @@ config ZSTD_DECOMPRESS
1102 source "lib/xz/Kconfig"
1104 +config LZMA_COMPRESS
1107 +config LZMA_DECOMPRESS
1111 # These all provide a common interface (hence the apparent duplication with
1112 # ZLIB_INFLATE; DECOMPRESS_GZIP is just a wrapper.)
1115 @@ -145,6 +145,16 @@ CFLAGS_kobject.o += -DDEBUG
1116 CFLAGS_kobject_uevent.o += -DDEBUG
1119 +ifdef CONFIG_JFFS2_ZLIB
1120 + CONFIG_ZLIB_INFLATE:=y
1121 + CONFIG_ZLIB_DEFLATE:=y
1124 +ifdef CONFIG_JFFS2_LZMA
1125 + CONFIG_LZMA_DECOMPRESS:=y
1126 + CONFIG_LZMA_COMPRESS:=y
1129 obj-$(CONFIG_DEBUG_INFO_REDUCED) += debug_info.o
1130 CFLAGS_debug_info.o += $(call cc-option, -femit-struct-debug-detailed=any)
1132 @@ -205,6 +215,8 @@ obj-$(CONFIG_ZSTD_COMPRESS) += zstd/
1133 obj-$(CONFIG_ZSTD_DECOMPRESS) += zstd/
1134 obj-$(CONFIG_XZ_DEC) += xz/
1135 obj-$(CONFIG_RAID6_PQ) += raid6/
1136 +obj-$(CONFIG_LZMA_COMPRESS) += lzma/
1137 +obj-$(CONFIG_LZMA_DECOMPRESS) += lzma/
1139 lib-$(CONFIG_DECOMPRESS_GZIP) += decompress_inflate.o
1140 lib-$(CONFIG_DECOMPRESS_BZIP2) += decompress_bunzip2.o
1142 +++ b/lib/lzma/LzFind.c
1144 +/* LzFind.c -- Match finder for LZ algorithms
1145 +2009-04-22 : Igor Pavlov : Public domain */
1147 +#include <string.h>
1149 +#include "LzFind.h"
1150 +#include "LzHash.h"
1152 +#define kEmptyHashValue 0
1153 +#define kMaxValForNormalize ((UInt32)0xFFFFFFFF)
1154 +#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */
1155 +#define kNormalizeMask (~(kNormalizeStepMin - 1))
1156 +#define kMaxHistorySize ((UInt32)3 << 30)
1158 +#define kStartMaxLen 3
1160 +static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
1162 + if (!p->directInput)
1164 + alloc->Free(alloc, p->bufferBase);
1165 + p->bufferBase = 0;
1169 +/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
1171 +static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
1173 + UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
1174 + if (p->directInput)
1176 + p->blockSize = blockSize;
1179 + if (p->bufferBase == 0 || p->blockSize != blockSize)
1181 + LzInWindow_Free(p, alloc);
1182 + p->blockSize = blockSize;
1183 + p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
1185 + return (p->bufferBase != 0);
1188 +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
1189 +Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
1191 +UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
1193 +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
1195 + p->posLimit -= subValue;
1196 + p->pos -= subValue;
1197 + p->streamPos -= subValue;
1200 +static void MatchFinder_ReadBlock(CMatchFinder *p)
1202 + if (p->streamEndWasReached || p->result != SZ_OK)
1204 + if (p->directInput)
1206 + UInt32 curSize = 0xFFFFFFFF - p->streamPos;
1207 + if (curSize > p->directInputRem)
1208 + curSize = (UInt32)p->directInputRem;
1209 + p->directInputRem -= curSize;
1210 + p->streamPos += curSize;
1211 + if (p->directInputRem == 0)
1212 + p->streamEndWasReached = 1;
1217 + Byte *dest = p->buffer + (p->streamPos - p->pos);
1218 + size_t size = (p->bufferBase + p->blockSize - dest);
1221 + p->result = p->stream->Read(p->stream, dest, &size);
1222 + if (p->result != SZ_OK)
1226 + p->streamEndWasReached = 1;
1229 + p->streamPos += (UInt32)size;
1230 + if (p->streamPos - p->pos > p->keepSizeAfter)
1235 +void MatchFinder_MoveBlock(CMatchFinder *p)
1237 + memmove(p->bufferBase,
1238 + p->buffer - p->keepSizeBefore,
1239 + (size_t)(p->streamPos - p->pos + p->keepSizeBefore));
1240 + p->buffer = p->bufferBase + p->keepSizeBefore;
1243 +int MatchFinder_NeedMove(CMatchFinder *p)
1245 + if (p->directInput)
1247 + /* if (p->streamEndWasReached) return 0; */
1248 + return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
1251 +void MatchFinder_ReadIfRequired(CMatchFinder *p)
1253 + if (p->streamEndWasReached)
1255 + if (p->keepSizeAfter >= p->streamPos - p->pos)
1256 + MatchFinder_ReadBlock(p);
1259 +static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
1261 + if (MatchFinder_NeedMove(p))
1262 + MatchFinder_MoveBlock(p);
1263 + MatchFinder_ReadBlock(p);
1266 +static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
1270 + p->numHashBytes = 4;
1274 +#define kCrcPoly 0xEDB88320
1276 +void MatchFinder_Construct(CMatchFinder *p)
1279 + p->bufferBase = 0;
1280 + p->directInput = 0;
1282 + MatchFinder_SetDefaultSettings(p);
1284 + for (i = 0; i < 256; i++)
1288 + for (j = 0; j < 8; j++)
1289 + r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
1294 +static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
1296 + alloc->Free(alloc, p->hash);
1300 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
1302 + MatchFinder_FreeThisClassMemory(p, alloc);
1303 + LzInWindow_Free(p, alloc);
1306 +static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
1308 + size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
1309 + if (sizeInBytes / sizeof(CLzRef) != num)
1311 + return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
1314 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
1315 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
1318 + UInt32 sizeReserv;
1319 + if (historySize > kMaxHistorySize)
1321 + MatchFinder_Free(p, alloc);
1324 + sizeReserv = historySize >> 1;
1325 + if (historySize > ((UInt32)2 << 30))
1326 + sizeReserv = historySize >> 2;
1327 + sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
1329 + p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
1330 + p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
1331 + /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
1332 + if (LzInWindow_Create(p, sizeReserv, alloc))
1334 + UInt32 newCyclicBufferSize = historySize + 1;
1336 + p->matchMaxLen = matchMaxLen;
1338 + p->fixedHashSize = 0;
1339 + if (p->numHashBytes == 2)
1340 + hs = (1 << 16) - 1;
1343 + hs = historySize - 1;
1349 + hs |= 0xFFFF; /* don't change it! It's required for Deflate */
1350 + if (hs > (1 << 24))
1352 + if (p->numHashBytes == 3)
1353 + hs = (1 << 24) - 1;
1360 + if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
1361 + if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
1362 + if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
1363 + hs += p->fixedHashSize;
1367 + UInt32 prevSize = p->hashSizeSum + p->numSons;
1369 + p->historySize = historySize;
1370 + p->hashSizeSum = hs;
1371 + p->cyclicBufferSize = newCyclicBufferSize;
1372 + p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);
1373 + newSize = p->hashSizeSum + p->numSons;
1374 + if (p->hash != 0 && prevSize == newSize)
1376 + MatchFinder_FreeThisClassMemory(p, alloc);
1377 + p->hash = AllocRefs(newSize, alloc);
1380 + p->son = p->hash + p->hashSizeSum;
1385 + MatchFinder_Free(p, alloc);
1389 +static void MatchFinder_SetLimits(CMatchFinder *p)
1391 + UInt32 limit = kMaxValForNormalize - p->pos;
1392 + UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
1393 + if (limit2 < limit)
1395 + limit2 = p->streamPos - p->pos;
1396 + if (limit2 <= p->keepSizeAfter)
1402 + limit2 -= p->keepSizeAfter;
1403 + if (limit2 < limit)
1406 + UInt32 lenLimit = p->streamPos - p->pos;
1407 + if (lenLimit > p->matchMaxLen)
1408 + lenLimit = p->matchMaxLen;
1409 + p->lenLimit = lenLimit;
1411 + p->posLimit = p->pos + limit;
1414 +void MatchFinder_Init(CMatchFinder *p)
1417 + for (i = 0; i < p->hashSizeSum; i++)
1418 + p->hash[i] = kEmptyHashValue;
1419 + p->cyclicBufferPos = 0;
1420 + p->buffer = p->bufferBase;
1421 + p->pos = p->streamPos = p->cyclicBufferSize;
1422 + p->result = SZ_OK;
1423 + p->streamEndWasReached = 0;
1424 + MatchFinder_ReadBlock(p);
1425 + MatchFinder_SetLimits(p);
1428 +static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
1430 + return (p->pos - p->historySize - 1) & kNormalizeMask;
1433 +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
1436 + for (i = 0; i < numItems; i++)
1438 + UInt32 value = items[i];
1439 + if (value <= subValue)
1440 + value = kEmptyHashValue;
1442 + value -= subValue;
1447 +static void MatchFinder_Normalize(CMatchFinder *p)
1449 + UInt32 subValue = MatchFinder_GetSubValue(p);
1450 + MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
1451 + MatchFinder_ReduceOffsets(p, subValue);
1454 +static void MatchFinder_CheckLimits(CMatchFinder *p)
1456 + if (p->pos == kMaxValForNormalize)
1457 + MatchFinder_Normalize(p);
1458 + if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
1459 + MatchFinder_CheckAndMoveAndRead(p);
1460 + if (p->cyclicBufferPos == p->cyclicBufferSize)
1461 + p->cyclicBufferPos = 0;
1462 + MatchFinder_SetLimits(p);
1465 +static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1466 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
1467 + UInt32 *distances, UInt32 maxLen)
1469 + son[_cyclicBufferPos] = curMatch;
1472 + UInt32 delta = pos - curMatch;
1473 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1476 + const Byte *pb = cur - delta;
1477 + curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
1478 + if (pb[maxLen] == cur[maxLen] && *pb == *cur)
1481 + while (++len != lenLimit)
1482 + if (pb[len] != cur[len])
1486 + *distances++ = maxLen = len;
1487 + *distances++ = delta - 1;
1488 + if (len == lenLimit)
1496 +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1497 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
1498 + UInt32 *distances, UInt32 maxLen)
1500 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1501 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1502 + UInt32 len0 = 0, len1 = 0;
1505 + UInt32 delta = pos - curMatch;
1506 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1508 + *ptr0 = *ptr1 = kEmptyHashValue;
1512 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1513 + const Byte *pb = cur - delta;
1514 + UInt32 len = (len0 < len1 ? len0 : len1);
1515 + if (pb[len] == cur[len])
1517 + if (++len != lenLimit && pb[len] == cur[len])
1518 + while (++len != lenLimit)
1519 + if (pb[len] != cur[len])
1523 + *distances++ = maxLen = len;
1524 + *distances++ = delta - 1;
1525 + if (len == lenLimit)
1533 + if (pb[len] < cur[len])
1551 +static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1552 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
1554 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1555 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1556 + UInt32 len0 = 0, len1 = 0;
1559 + UInt32 delta = pos - curMatch;
1560 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1562 + *ptr0 = *ptr1 = kEmptyHashValue;
1566 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1567 + const Byte *pb = cur - delta;
1568 + UInt32 len = (len0 < len1 ? len0 : len1);
1569 + if (pb[len] == cur[len])
1571 + while (++len != lenLimit)
1572 + if (pb[len] != cur[len])
1575 + if (len == lenLimit)
1583 + if (pb[len] < cur[len])
1602 + ++p->cyclicBufferPos; \
1604 + if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
1606 +#define MOVE_POS_RET MOVE_POS return offset;
1608 +static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
1610 +#define GET_MATCHES_HEADER2(minLen, ret_op) \
1611 + UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \
1612 + lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
1615 +#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
1616 +#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
1618 +#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
1620 +#define GET_MATCHES_FOOTER(offset, maxLen) \
1621 + offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
1622 + distances + offset, maxLen) - distances); MOVE_POS_RET;
1624 +#define SKIP_FOOTER \
1625 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;
1627 +static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1630 + GET_MATCHES_HEADER(2)
1632 + curMatch = p->hash[hashValue];
1633 + p->hash[hashValue] = p->pos;
1635 + GET_MATCHES_FOOTER(offset, 1)
1638 +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1641 + GET_MATCHES_HEADER(3)
1643 + curMatch = p->hash[hashValue];
1644 + p->hash[hashValue] = p->pos;
1646 + GET_MATCHES_FOOTER(offset, 2)
1649 +static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1651 + UInt32 hash2Value, delta2, maxLen, offset;
1652 + GET_MATCHES_HEADER(3)
1656 + delta2 = p->pos - p->hash[hash2Value];
1657 + curMatch = p->hash[kFix3HashSize + hashValue];
1659 + p->hash[hash2Value] =
1660 + p->hash[kFix3HashSize + hashValue] = p->pos;
1665 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1667 + for (; maxLen != lenLimit; maxLen++)
1668 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1670 + distances[0] = maxLen;
1671 + distances[1] = delta2 - 1;
1673 + if (maxLen == lenLimit)
1675 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1679 + GET_MATCHES_FOOTER(offset, maxLen)
1682 +static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1684 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1685 + GET_MATCHES_HEADER(4)
1689 + delta2 = p->pos - p->hash[ hash2Value];
1690 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1691 + curMatch = p->hash[kFix4HashSize + hashValue];
1693 + p->hash[ hash2Value] =
1694 + p->hash[kFix3HashSize + hash3Value] =
1695 + p->hash[kFix4HashSize + hashValue] = p->pos;
1699 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1701 + distances[0] = maxLen = 2;
1702 + distances[1] = delta2 - 1;
1705 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1708 + distances[offset + 1] = delta3 - 1;
1714 + for (; maxLen != lenLimit; maxLen++)
1715 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1717 + distances[offset - 2] = maxLen;
1718 + if (maxLen == lenLimit)
1720 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1726 + GET_MATCHES_FOOTER(offset, maxLen)
1729 +static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1731 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1732 + GET_MATCHES_HEADER(4)
1736 + delta2 = p->pos - p->hash[ hash2Value];
1737 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1738 + curMatch = p->hash[kFix4HashSize + hashValue];
1740 + p->hash[ hash2Value] =
1741 + p->hash[kFix3HashSize + hash3Value] =
1742 + p->hash[kFix4HashSize + hashValue] = p->pos;
1746 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1748 + distances[0] = maxLen = 2;
1749 + distances[1] = delta2 - 1;
1752 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1755 + distances[offset + 1] = delta3 - 1;
1761 + for (; maxLen != lenLimit; maxLen++)
1762 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1764 + distances[offset - 2] = maxLen;
1765 + if (maxLen == lenLimit)
1767 + p->son[p->cyclicBufferPos] = curMatch;
1773 + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
1774 + distances + offset, maxLen) - (distances));
1778 +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1781 + GET_MATCHES_HEADER(3)
1783 + curMatch = p->hash[hashValue];
1784 + p->hash[hashValue] = p->pos;
1785 + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
1786 + distances, 2) - (distances));
1790 +static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1796 + curMatch = p->hash[hashValue];
1797 + p->hash[hashValue] = p->pos;
1800 + while (--num != 0);
1803 +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1809 + curMatch = p->hash[hashValue];
1810 + p->hash[hashValue] = p->pos;
1813 + while (--num != 0);
1816 +static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1820 + UInt32 hash2Value;
1823 + curMatch = p->hash[kFix3HashSize + hashValue];
1824 + p->hash[hash2Value] =
1825 + p->hash[kFix3HashSize + hashValue] = p->pos;
1828 + while (--num != 0);
1831 +static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1835 + UInt32 hash2Value, hash3Value;
1838 + curMatch = p->hash[kFix4HashSize + hashValue];
1839 + p->hash[ hash2Value] =
1840 + p->hash[kFix3HashSize + hash3Value] = p->pos;
1841 + p->hash[kFix4HashSize + hashValue] = p->pos;
1844 + while (--num != 0);
1847 +static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1851 + UInt32 hash2Value, hash3Value;
1854 + curMatch = p->hash[kFix4HashSize + hashValue];
1855 + p->hash[ hash2Value] =
1856 + p->hash[kFix3HashSize + hash3Value] =
1857 + p->hash[kFix4HashSize + hashValue] = p->pos;
1858 + p->son[p->cyclicBufferPos] = curMatch;
1861 + while (--num != 0);
1864 +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1870 + curMatch = p->hash[hashValue];
1871 + p->hash[hashValue] = p->pos;
1872 + p->son[p->cyclicBufferPos] = curMatch;
1875 + while (--num != 0);
1878 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
1880 + vTable->Init = (Mf_Init_Func)MatchFinder_Init;
1881 + vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;
1882 + vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
1883 + vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
1886 + vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;
1887 + vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;
1889 + else if (p->numHashBytes == 2)
1891 + vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;
1892 + vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;
1894 + else if (p->numHashBytes == 3)
1896 + vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;
1897 + vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;
1901 + vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
1902 + vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
1906 +++ b/lib/lzma/LzmaDec.c
1908 +/* LzmaDec.c -- LZMA Decoder
1909 +2009-09-20 : Igor Pavlov : Public domain */
1911 +#include "LzmaDec.h"
1913 +#include <string.h>
1915 +#define kNumTopBits 24
1916 +#define kTopValue ((UInt32)1 << kNumTopBits)
1918 +#define kNumBitModelTotalBits 11
1919 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
1920 +#define kNumMoveBits 5
1922 +#define RC_INIT_SIZE 5
1924 +#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
1926 +#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1927 +#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
1928 +#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
1929 +#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
1930 + { UPDATE_0(p); i = (i + i); A0; } else \
1931 + { UPDATE_1(p); i = (i + i) + 1; A1; }
1932 +#define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
1934 +#define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
1935 +#define TREE_DECODE(probs, limit, i) \
1936 + { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
1938 +/* #define _LZMA_SIZE_OPT */
1940 +#ifdef _LZMA_SIZE_OPT
1941 +#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
1943 +#define TREE_6_DECODE(probs, i) \
1945 + TREE_GET_BIT(probs, i); \
1946 + TREE_GET_BIT(probs, i); \
1947 + TREE_GET_BIT(probs, i); \
1948 + TREE_GET_BIT(probs, i); \
1949 + TREE_GET_BIT(probs, i); \
1950 + TREE_GET_BIT(probs, i); \
1954 +#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
1956 +#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1957 +#define UPDATE_0_CHECK range = bound;
1958 +#define UPDATE_1_CHECK range -= bound; code -= bound;
1959 +#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
1960 + { UPDATE_0_CHECK; i = (i + i); A0; } else \
1961 + { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
1962 +#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
1963 +#define TREE_DECODE_CHECK(probs, limit, i) \
1964 + { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
1967 +#define kNumPosBitsMax 4
1968 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
1970 +#define kLenNumLowBits 3
1971 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
1972 +#define kLenNumMidBits 3
1973 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
1974 +#define kLenNumHighBits 8
1975 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
1977 +#define LenChoice 0
1978 +#define LenChoice2 (LenChoice + 1)
1979 +#define LenLow (LenChoice2 + 1)
1980 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
1981 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
1982 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
1985 +#define kNumStates 12
1986 +#define kNumLitStates 7
1988 +#define kStartPosModelIndex 4
1989 +#define kEndPosModelIndex 14
1990 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
1992 +#define kNumPosSlotBits 6
1993 +#define kNumLenToPosStates 4
1995 +#define kNumAlignBits 4
1996 +#define kAlignTableSize (1 << kNumAlignBits)
1998 +#define kMatchMinLen 2
1999 +#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
2002 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
2003 +#define IsRepG0 (IsRep + kNumStates)
2004 +#define IsRepG1 (IsRepG0 + kNumStates)
2005 +#define IsRepG2 (IsRepG1 + kNumStates)
2006 +#define IsRep0Long (IsRepG2 + kNumStates)
2007 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
2008 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
2009 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
2010 +#define LenCoder (Align + kAlignTableSize)
2011 +#define RepLenCoder (LenCoder + kNumLenProbs)
2012 +#define Literal (RepLenCoder + kNumLenProbs)
2014 +#define LZMA_BASE_SIZE 1846
2015 +#define LZMA_LIT_SIZE 768
2017 +#define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
2019 +#if Literal != LZMA_BASE_SIZE
2020 +StopCompilingDueBUG
2023 +#define LZMA_DIC_MIN (1 << 12)
2025 +/* First LZMA-symbol is always decoded.
2026 +And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
2030 + SZ_ERROR_DATA - Error
2032 + < kMatchSpecLenStart : normal remain
2033 + = kMatchSpecLenStart : finished
2034 + = kMatchSpecLenStart + 1 : Flush marker
2035 + = kMatchSpecLenStart + 2 : State Init Marker
2038 +static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
2040 + CLzmaProb *probs = p->probs;
2042 + unsigned state = p->state;
2043 + UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
2044 + unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
2045 + unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
2046 + unsigned lc = p->prop.lc;
2048 + Byte *dic = p->dic;
2049 + SizeT dicBufSize = p->dicBufSize;
2050 + SizeT dicPos = p->dicPos;
2052 + UInt32 processedPos = p->processedPos;
2053 + UInt32 checkDicSize = p->checkDicSize;
2056 + const Byte *buf = p->buf;
2057 + UInt32 range = p->range;
2058 + UInt32 code = p->code;
2065 + unsigned posState = processedPos & pbMask;
2067 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
2072 + prob = probs + Literal;
2073 + if (checkDicSize != 0 || processedPos != 0)
2074 + prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
2075 + (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
2077 + if (state < kNumLitStates)
2079 + state -= (state < 4) ? state : 3;
2081 + do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
2085 + unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
2086 + unsigned offs = 0x100;
2087 + state -= (state < 10) ? 3 : 6;
2092 + CLzmaProb *probLit;
2094 + bit = (matchByte & offs);
2095 + probLit = prob + offs + bit + symbol;
2096 + GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
2098 + while (symbol < 0x100);
2100 + dic[dicPos++] = (Byte)symbol;
2107 + prob = probs + IsRep + state;
2111 + state += kNumStates;
2112 + prob = probs + LenCoder;
2117 + if (checkDicSize == 0 && processedPos == 0)
2118 + return SZ_ERROR_DATA;
2119 + prob = probs + IsRepG0 + state;
2123 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
2127 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
2130 + state = state < kNumLitStates ? 9 : 11;
2139 + prob = probs + IsRepG1 + state;
2148 + prob = probs + IsRepG2 + state;
2165 + state = state < kNumLitStates ? 8 : 11;
2166 + prob = probs + RepLenCoder;
2169 + unsigned limit, offset;
2170 + CLzmaProb *probLen = prob + LenChoice;
2173 + UPDATE_0(probLen);
2174 + probLen = prob + LenLow + (posState << kLenNumLowBits);
2176 + limit = (1 << kLenNumLowBits);
2180 + UPDATE_1(probLen);
2181 + probLen = prob + LenChoice2;
2184 + UPDATE_0(probLen);
2185 + probLen = prob + LenMid + (posState << kLenNumMidBits);
2186 + offset = kLenNumLowSymbols;
2187 + limit = (1 << kLenNumMidBits);
2191 + UPDATE_1(probLen);
2192 + probLen = prob + LenHigh;
2193 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
2194 + limit = (1 << kLenNumHighBits);
2197 + TREE_DECODE(probLen, limit, len);
2201 + if (state >= kNumStates)
2204 + prob = probs + PosSlot +
2205 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
2206 + TREE_6_DECODE(prob, distance);
2207 + if (distance >= kStartPosModelIndex)
2209 + unsigned posSlot = (unsigned)distance;
2210 + int numDirectBits = (int)(((distance >> 1) - 1));
2211 + distance = (2 | (distance & 1));
2212 + if (posSlot < kEndPosModelIndex)
2214 + distance <<= numDirectBits;
2215 + prob = probs + SpecPos + distance - posSlot - 1;
2221 + GET_BIT2(prob + i, i, ; , distance |= mask);
2224 + while (--numDirectBits != 0);
2229 + numDirectBits -= kNumAlignBits;
2238 + t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
2239 + distance = (distance << 1) + (t + 1);
2240 + code += range & t;
2244 + if (code >= range)
2251 + while (--numDirectBits != 0);
2252 + prob = probs + Align;
2253 + distance <<= kNumAlignBits;
2256 + GET_BIT2(prob + i, i, ; , distance |= 1);
2257 + GET_BIT2(prob + i, i, ; , distance |= 2);
2258 + GET_BIT2(prob + i, i, ; , distance |= 4);
2259 + GET_BIT2(prob + i, i, ; , distance |= 8);
2261 + if (distance == (UInt32)0xFFFFFFFF)
2263 + len += kMatchSpecLenStart;
2264 + state -= kNumStates;
2272 + rep0 = distance + 1;
2273 + if (checkDicSize == 0)
2275 + if (distance >= processedPos)
2276 + return SZ_ERROR_DATA;
2278 + else if (distance >= checkDicSize)
2279 + return SZ_ERROR_DATA;
2280 + state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
2283 + len += kMatchMinLen;
2285 + if (limit == dicPos)
2286 + return SZ_ERROR_DATA;
2288 + SizeT rem = limit - dicPos;
2289 + unsigned curLen = ((rem < len) ? (unsigned)rem : len);
2290 + SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
2292 + processedPos += curLen;
2295 + if (pos + curLen <= dicBufSize)
2297 + Byte *dest = dic + dicPos;
2298 + ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
2299 + const Byte *lim = dest + curLen;
2302 + *(dest) = (Byte)*(dest + src);
2303 + while (++dest != lim);
2309 + dic[dicPos++] = dic[pos];
2310 + if (++pos == dicBufSize)
2313 + while (--curLen != 0);
2318 + while (dicPos < limit && buf < bufLimit);
2323 + p->remainLen = len;
2324 + p->dicPos = dicPos;
2325 + p->processedPos = processedPos;
2326 + p->reps[0] = rep0;
2327 + p->reps[1] = rep1;
2328 + p->reps[2] = rep2;
2329 + p->reps[3] = rep3;
2335 +static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
2337 + if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
2339 + Byte *dic = p->dic;
2340 + SizeT dicPos = p->dicPos;
2341 + SizeT dicBufSize = p->dicBufSize;
2342 + unsigned len = p->remainLen;
2343 + UInt32 rep0 = p->reps[0];
2344 + if (limit - dicPos < len)
2345 + len = (unsigned)(limit - dicPos);
2347 + if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
2348 + p->checkDicSize = p->prop.dicSize;
2350 + p->processedPos += len;
2351 + p->remainLen -= len;
2352 + while (len-- != 0)
2354 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
2357 + p->dicPos = dicPos;
2361 +static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
2365 + SizeT limit2 = limit;
2366 + if (p->checkDicSize == 0)
2368 + UInt32 rem = p->prop.dicSize - p->processedPos;
2369 + if (limit - p->dicPos > rem)
2370 + limit2 = p->dicPos + rem;
2372 + RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
2373 + if (p->processedPos >= p->prop.dicSize)
2374 + p->checkDicSize = p->prop.dicSize;
2375 + LzmaDec_WriteRem(p, limit);
2377 + while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
2379 + if (p->remainLen > kMatchSpecLenStart)
2381 + p->remainLen = kMatchSpecLenStart;
2388 + DUMMY_ERROR, /* unexpected end of input stream */
2394 +static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
2396 + UInt32 range = p->range;
2397 + UInt32 code = p->code;
2398 + const Byte *bufLimit = buf + inSize;
2399 + CLzmaProb *probs = p->probs;
2400 + unsigned state = p->state;
2407 + unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
2409 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
2410 + IF_BIT_0_CHECK(prob)
2414 + /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
2416 + prob = probs + Literal;
2417 + if (p->checkDicSize != 0 || p->processedPos != 0)
2418 + prob += (LZMA_LIT_SIZE *
2419 + ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
2420 + (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
2422 + if (state < kNumLitStates)
2424 + unsigned symbol = 1;
2425 + do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
2429 + unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
2430 + ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
2431 + unsigned offs = 0x100;
2432 + unsigned symbol = 1;
2436 + CLzmaProb *probLit;
2438 + bit = (matchByte & offs);
2439 + probLit = prob + offs + bit + symbol;
2440 + GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
2442 + while (symbol < 0x100);
2451 + prob = probs + IsRep + state;
2452 + IF_BIT_0_CHECK(prob)
2456 + prob = probs + LenCoder;
2457 + res = DUMMY_MATCH;
2463 + prob = probs + IsRepG0 + state;
2464 + IF_BIT_0_CHECK(prob)
2467 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
2468 + IF_BIT_0_CHECK(prob)
2482 + prob = probs + IsRepG1 + state;
2483 + IF_BIT_0_CHECK(prob)
2490 + prob = probs + IsRepG2 + state;
2491 + IF_BIT_0_CHECK(prob)
2501 + state = kNumStates;
2502 + prob = probs + RepLenCoder;
2505 + unsigned limit, offset;
2506 + CLzmaProb *probLen = prob + LenChoice;
2507 + IF_BIT_0_CHECK(probLen)
2510 + probLen = prob + LenLow + (posState << kLenNumLowBits);
2512 + limit = 1 << kLenNumLowBits;
2517 + probLen = prob + LenChoice2;
2518 + IF_BIT_0_CHECK(probLen)
2521 + probLen = prob + LenMid + (posState << kLenNumMidBits);
2522 + offset = kLenNumLowSymbols;
2523 + limit = 1 << kLenNumMidBits;
2528 + probLen = prob + LenHigh;
2529 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
2530 + limit = 1 << kLenNumHighBits;
2533 + TREE_DECODE_CHECK(probLen, limit, len);
2540 + prob = probs + PosSlot +
2541 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
2543 + TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
2544 + if (posSlot >= kStartPosModelIndex)
2546 + int numDirectBits = ((posSlot >> 1) - 1);
2548 + /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
2550 + if (posSlot < kEndPosModelIndex)
2552 + prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
2556 + numDirectBits -= kNumAlignBits;
2561 + code -= range & (((code - range) >> 31) - 1);
2562 + /* if (code >= range) code -= range; */
2564 + while (--numDirectBits != 0);
2565 + prob = probs + Align;
2566 + numDirectBits = kNumAlignBits;
2572 + GET_BIT_CHECK(prob + i, i);
2574 + while (--numDirectBits != 0);
2585 +static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
2587 + p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
2588 + p->range = 0xFFFFFFFF;
2592 +void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
2596 + p->tempBufSize = 0;
2600 + p->processedPos = 0;
2601 + p->checkDicSize = 0;
2602 + p->needInitState = 1;
2605 + p->needInitState = 1;
2608 +void LzmaDec_Init(CLzmaDec *p)
2611 + LzmaDec_InitDicAndState(p, True, True);
2614 +static void LzmaDec_InitStateReal(CLzmaDec *p)
2616 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
2618 + CLzmaProb *probs = p->probs;
2619 + for (i = 0; i < numProbs; i++)
2620 + probs[i] = kBitModelTotal >> 1;
2621 + p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
2623 + p->needInitState = 0;
2626 +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
2627 + ELzmaFinishMode finishMode, ELzmaStatus *status)
2629 + SizeT inSize = *srcLen;
2631 + LzmaDec_WriteRem(p, dicLimit);
2633 + *status = LZMA_STATUS_NOT_SPECIFIED;
2635 + while (p->remainLen != kMatchSpecLenStart)
2637 + int checkEndMarkNow;
2639 + if (p->needFlush != 0)
2641 + for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
2642 + p->tempBuf[p->tempBufSize++] = *src++;
2643 + if (p->tempBufSize < RC_INIT_SIZE)
2645 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2648 + if (p->tempBuf[0] != 0)
2649 + return SZ_ERROR_DATA;
2651 + LzmaDec_InitRc(p, p->tempBuf);
2652 + p->tempBufSize = 0;
2655 + checkEndMarkNow = 0;
2656 + if (p->dicPos >= dicLimit)
2658 + if (p->remainLen == 0 && p->code == 0)
2660 + *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
2663 + if (finishMode == LZMA_FINISH_ANY)
2665 + *status = LZMA_STATUS_NOT_FINISHED;
2668 + if (p->remainLen != 0)
2670 + *status = LZMA_STATUS_NOT_FINISHED;
2671 + return SZ_ERROR_DATA;
2673 + checkEndMarkNow = 1;
2676 + if (p->needInitState)
2677 + LzmaDec_InitStateReal(p);
2679 + if (p->tempBufSize == 0)
2682 + const Byte *bufLimit;
2683 + if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2685 + int dummyRes = LzmaDec_TryDummy(p, src, inSize);
2686 + if (dummyRes == DUMMY_ERROR)
2688 + memcpy(p->tempBuf, src, inSize);
2689 + p->tempBufSize = (unsigned)inSize;
2690 + (*srcLen) += inSize;
2691 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2694 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2696 + *status = LZMA_STATUS_NOT_FINISHED;
2697 + return SZ_ERROR_DATA;
2702 + bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
2704 + if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
2705 + return SZ_ERROR_DATA;
2706 + processed = (SizeT)(p->buf - src);
2707 + (*srcLen) += processed;
2709 + inSize -= processed;
2713 + unsigned rem = p->tempBufSize, lookAhead = 0;
2714 + while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
2715 + p->tempBuf[rem++] = src[lookAhead++];
2716 + p->tempBufSize = rem;
2717 + if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2719 + int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
2720 + if (dummyRes == DUMMY_ERROR)
2722 + (*srcLen) += lookAhead;
2723 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2726 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2728 + *status = LZMA_STATUS_NOT_FINISHED;
2729 + return SZ_ERROR_DATA;
2732 + p->buf = p->tempBuf;
2733 + if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
2734 + return SZ_ERROR_DATA;
2735 + lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
2736 + (*srcLen) += lookAhead;
2738 + inSize -= lookAhead;
2739 + p->tempBufSize = 0;
2743 + *status = LZMA_STATUS_FINISHED_WITH_MARK;
2744 + return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
2747 +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
2749 + SizeT outSize = *destLen;
2750 + SizeT inSize = *srcLen;
2751 + *srcLen = *destLen = 0;
2754 + SizeT inSizeCur = inSize, outSizeCur, dicPos;
2755 + ELzmaFinishMode curFinishMode;
2757 + if (p->dicPos == p->dicBufSize)
2759 + dicPos = p->dicPos;
2760 + if (outSize > p->dicBufSize - dicPos)
2762 + outSizeCur = p->dicBufSize;
2763 + curFinishMode = LZMA_FINISH_ANY;
2767 + outSizeCur = dicPos + outSize;
2768 + curFinishMode = finishMode;
2771 + res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
2773 + inSize -= inSizeCur;
2774 + *srcLen += inSizeCur;
2775 + outSizeCur = p->dicPos - dicPos;
2776 + memcpy(dest, p->dic + dicPos, outSizeCur);
2777 + dest += outSizeCur;
2778 + outSize -= outSizeCur;
2779 + *destLen += outSizeCur;
2782 + if (outSizeCur == 0 || outSize == 0)
2787 +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
2789 + alloc->Free(alloc, p->probs);
2793 +static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
2795 + alloc->Free(alloc, p->dic);
2799 +void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
2801 + LzmaDec_FreeProbs(p, alloc);
2802 + LzmaDec_FreeDict(p, alloc);
2805 +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
2810 + if (size < LZMA_PROPS_SIZE)
2811 + return SZ_ERROR_UNSUPPORTED;
2813 + dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
2815 + if (dicSize < LZMA_DIC_MIN)
2816 + dicSize = LZMA_DIC_MIN;
2817 + p->dicSize = dicSize;
2820 + if (d >= (9 * 5 * 5))
2821 + return SZ_ERROR_UNSUPPORTED;
2831 +static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
2833 + UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
2834 + if (p->probs == 0 || numProbs != p->numProbs)
2836 + LzmaDec_FreeProbs(p, alloc);
2837 + p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
2838 + p->numProbs = numProbs;
2839 + if (p->probs == 0)
2840 + return SZ_ERROR_MEM;
2845 +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2847 + CLzmaProps propNew;
2848 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2849 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2850 + p->prop = propNew;
2854 +SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2856 + CLzmaProps propNew;
2858 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2859 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2860 + dicBufSize = propNew.dicSize;
2861 + if (p->dic == 0 || dicBufSize != p->dicBufSize)
2863 + LzmaDec_FreeDict(p, alloc);
2864 + p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
2867 + LzmaDec_FreeProbs(p, alloc);
2868 + return SZ_ERROR_MEM;
2871 + p->dicBufSize = dicBufSize;
2872 + p->prop = propNew;
2876 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
2877 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
2878 + ELzmaStatus *status, ISzAlloc *alloc)
2882 + SizeT inSize = *srcLen;
2883 + SizeT outSize = *destLen;
2884 + *srcLen = *destLen = 0;
2885 + if (inSize < RC_INIT_SIZE)
2886 + return SZ_ERROR_INPUT_EOF;
2888 + LzmaDec_Construct(&p);
2889 + res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
2893 + p.dicBufSize = outSize;
2898 + res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
2900 + if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
2901 + res = SZ_ERROR_INPUT_EOF;
2903 + (*destLen) = p.dicPos;
2904 + LzmaDec_FreeProbs(&p, alloc);
2908 +++ b/lib/lzma/LzmaEnc.c
2910 +/* LzmaEnc.c -- LZMA Encoder
2911 +2009-11-24 : Igor Pavlov : Public domain */
2913 +#include <string.h>
2915 +/* #define SHOW_STAT */
2916 +/* #define SHOW_STAT2 */
2918 +#if defined(SHOW_STAT) || defined(SHOW_STAT2)
2922 +#include "LzmaEnc.h"
2927 +#include "LzFind.h"
2929 +#include "LzFindMt.h"
2933 +static int ttt = 0;
2936 +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
2938 +#define kBlockSize (9 << 10)
2939 +#define kUnpackBlockSize (1 << 18)
2940 +#define kMatchArraySize (1 << 21)
2941 +#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
2943 +#define kNumMaxDirectBits (31)
2945 +#define kNumTopBits 24
2946 +#define kTopValue ((UInt32)1 << kNumTopBits)
2948 +#define kNumBitModelTotalBits 11
2949 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
2950 +#define kNumMoveBits 5
2951 +#define kProbInitValue (kBitModelTotal >> 1)
2953 +#define kNumMoveReducingBits 4
2954 +#define kNumBitPriceShiftBits 4
2955 +#define kBitPrice (1 << kNumBitPriceShiftBits)
2957 +void LzmaEncProps_Init(CLzmaEncProps *p)
2960 + p->dictSize = p->mc = 0;
2961 + p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
2962 + p->writeEndMark = 0;
2965 +void LzmaEncProps_Normalize(CLzmaEncProps *p)
2967 + int level = p->level;
2968 + if (level < 0) level = 5;
2970 + if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
2971 + if (p->lc < 0) p->lc = 3;
2972 + if (p->lp < 0) p->lp = 0;
2973 + if (p->pb < 0) p->pb = 2;
2974 + if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
2975 + if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
2976 + if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
2977 + if (p->numHashBytes < 0) p->numHashBytes = 4;
2978 + if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
2979 + if (p->numThreads < 0)
2982 + ((p->btMode && p->algo) ? 2 : 1);
2988 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
2990 + CLzmaEncProps props = *props2;
2991 + LzmaEncProps_Normalize(&props);
2992 + return props.dictSize;
2995 +/* #define LZMA_LOG_BSR */
2996 +/* Define it for Intel's CPU */
2999 +#ifdef LZMA_LOG_BSR
3001 +#define kDicLogSizeMaxCompress 30
3003 +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
3005 +UInt32 GetPosSlot1(UInt32 pos)
3008 + BSR2_RET(pos, res);
3011 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
3012 +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
3016 +#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
3017 +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
3019 +void LzmaEnc_FastPosInit(Byte *g_FastPos)
3021 + int c = 2, slotFast;
3025 + for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
3027 + UInt32 k = (1 << ((slotFast >> 1) - 1));
3029 + for (j = 0; j < k; j++, c++)
3030 + g_FastPos[c] = (Byte)slotFast;
3034 +#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
3035 + (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
3036 + res = p->g_FastPos[pos >> i] + (i * 2); }
3038 +#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
3039 + p->g_FastPos[pos >> 6] + 12 : \
3040 + p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
3043 +#define GetPosSlot1(pos) p->g_FastPos[pos]
3044 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
3045 +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
3050 +#define LZMA_NUM_REPS 4
3052 +typedef unsigned CState;
3067 + UInt32 backs[LZMA_NUM_REPS];
3070 +#define kNumOpts (1 << 12)
3072 +#define kNumLenToPosStates 4
3073 +#define kNumPosSlotBits 6
3074 +#define kDicLogSizeMin 0
3075 +#define kDicLogSizeMax 32
3076 +#define kDistTableSizeMax (kDicLogSizeMax * 2)
3079 +#define kNumAlignBits 4
3080 +#define kAlignTableSize (1 << kNumAlignBits)
3081 +#define kAlignMask (kAlignTableSize - 1)
3083 +#define kStartPosModelIndex 4
3084 +#define kEndPosModelIndex 14
3085 +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
3087 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
3089 +#ifdef _LZMA_PROB32
3090 +#define CLzmaProb UInt32
3092 +#define CLzmaProb UInt16
3095 +#define LZMA_PB_MAX 4
3096 +#define LZMA_LC_MAX 8
3097 +#define LZMA_LP_MAX 4
3099 +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
3102 +#define kLenNumLowBits 3
3103 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
3104 +#define kLenNumMidBits 3
3105 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
3106 +#define kLenNumHighBits 8
3107 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
3109 +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
3111 +#define LZMA_MATCH_LEN_MIN 2
3112 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
3114 +#define kNumStates 12
3119 + CLzmaProb choice2;
3120 + CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
3121 + CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
3122 + CLzmaProb high[kLenNumHighSymbols];
3128 + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
3130 + UInt32 counters[LZMA_NUM_PB_STATES_MAX];
3142 + ISeqOutStream *outStream;
3149 + CLzmaProb *litProbs;
3151 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
3152 + CLzmaProb isRep[kNumStates];
3153 + CLzmaProb isRepG0[kNumStates];
3154 + CLzmaProb isRepG1[kNumStates];
3155 + CLzmaProb isRepG2[kNumStates];
3156 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
3158 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
3159 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
3160 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
3162 + CLenPriceEnc lenEnc;
3163 + CLenPriceEnc repLenEnc;
3165 + UInt32 reps[LZMA_NUM_REPS];
3171 + IMatchFinder matchFinder;
3172 + void *matchFinderObj;
3176 + CMatchFinderMt matchFinderMt;
3179 + CMatchFinder matchFinderBase;
3185 + UInt32 optimumEndIndex;
3186 + UInt32 optimumCurrentIndex;
3188 + UInt32 longestMatchLength;
3191 + COptimal opt[kNumOpts];
3193 + #ifndef LZMA_LOG_BSR
3194 + Byte g_FastPos[1 << kNumLogBits];
3197 + UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
3198 + UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
3199 + UInt32 numFastBytes;
3200 + UInt32 additionalOffset;
3201 + UInt32 reps[LZMA_NUM_REPS];
3204 + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
3205 + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
3206 + UInt32 alignPrices[kAlignTableSize];
3207 + UInt32 alignPriceCount;
3209 + UInt32 distTableSize;
3211 + unsigned lc, lp, pb;
3212 + unsigned lpMask, pbMask;
3214 + CLzmaProb *litProbs;
3216 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
3217 + CLzmaProb isRep[kNumStates];
3218 + CLzmaProb isRepG0[kNumStates];
3219 + CLzmaProb isRepG1[kNumStates];
3220 + CLzmaProb isRepG2[kNumStates];
3221 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
3223 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
3224 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
3225 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
3227 + CLenPriceEnc lenEnc;
3228 + CLenPriceEnc repLenEnc;
3236 + Bool writeEndMark;
3238 + UInt32 matchPriceCount;
3244 + UInt32 matchFinderCycles;
3248 + CSaveState saveState;
3251 +void LzmaEnc_SaveState(CLzmaEncHandle pp)
3253 + CLzmaEnc *p = (CLzmaEnc *)pp;
3254 + CSaveState *dest = &p->saveState;
3256 + dest->lenEnc = p->lenEnc;
3257 + dest->repLenEnc = p->repLenEnc;
3258 + dest->state = p->state;
3260 + for (i = 0; i < kNumStates; i++)
3262 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
3263 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
3265 + for (i = 0; i < kNumLenToPosStates; i++)
3266 + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
3267 + memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
3268 + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
3269 + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
3270 + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
3271 + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
3272 + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
3273 + memcpy(dest->reps, p->reps, sizeof(p->reps));
3274 + memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
3277 +void LzmaEnc_RestoreState(CLzmaEncHandle pp)
3279 + CLzmaEnc *dest = (CLzmaEnc *)pp;
3280 + const CSaveState *p = &dest->saveState;
3282 + dest->lenEnc = p->lenEnc;
3283 + dest->repLenEnc = p->repLenEnc;
3284 + dest->state = p->state;
3286 + for (i = 0; i < kNumStates; i++)
3288 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
3289 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
3291 + for (i = 0; i < kNumLenToPosStates; i++)
3292 + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
3293 + memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
3294 + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
3295 + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
3296 + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
3297 + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
3298 + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
3299 + memcpy(dest->reps, p->reps, sizeof(p->reps));
3300 + memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
3303 +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
3305 + CLzmaEnc *p = (CLzmaEnc *)pp;
3306 + CLzmaEncProps props = *props2;
3307 + LzmaEncProps_Normalize(&props);
3309 + if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
3310 + props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))
3311 + return SZ_ERROR_PARAM;
3312 + p->dictSize = props.dictSize;
3313 + p->matchFinderCycles = props.mc;
3315 + unsigned fb = props.fb;
3318 + if (fb > LZMA_MATCH_LEN_MAX)
3319 + fb = LZMA_MATCH_LEN_MAX;
3320 + p->numFastBytes = fb;
3325 + p->fastMode = (props.algo == 0);
3326 + p->matchFinderBase.btMode = props.btMode;
3328 + UInt32 numHashBytes = 4;
3331 + if (props.numHashBytes < 2)
3333 + else if (props.numHashBytes < 4)
3334 + numHashBytes = props.numHashBytes;
3336 + p->matchFinderBase.numHashBytes = numHashBytes;
3339 + p->matchFinderBase.cutValue = props.mc;
3341 + p->writeEndMark = props.writeEndMark;
3345 + if (newMultiThread != _multiThread)
3347 + ReleaseMatchFinder();
3348 + _multiThread = newMultiThread;
3351 + p->multiThread = (props.numThreads > 1);
3357 +static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
3358 +static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
3359 +static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
3360 +static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
3362 +#define IsCharState(s) ((s) < 7)
3364 +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
3366 +#define kInfinityPrice (1 << 30)
3368 +static void RangeEnc_Construct(CRangeEnc *p)
3374 +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
3376 +#define RC_BUF_SIZE (1 << 16)
3377 +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
3379 + if (p->bufBase == 0)
3381 + p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
3382 + if (p->bufBase == 0)
3384 + p->bufLim = p->bufBase + RC_BUF_SIZE;
3389 +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
3391 + alloc->Free(alloc, p->bufBase);
3395 +static void RangeEnc_Init(CRangeEnc *p)
3397 + /* Stream.Init(); */
3399 + p->range = 0xFFFFFFFF;
3403 + p->buf = p->bufBase;
3409 +static void RangeEnc_FlushStream(CRangeEnc *p)
3412 + if (p->res != SZ_OK)
3414 + num = p->buf - p->bufBase;
3415 + if (num != p->outStream->Write(p->outStream, p->bufBase, num))
3416 + p->res = SZ_ERROR_WRITE;
3417 + p->processed += num;
3418 + p->buf = p->bufBase;
3421 +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
3423 + if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
3425 + Byte temp = p->cache;
3428 + Byte *buf = p->buf;
3429 + *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
3431 + if (buf == p->bufLim)
3432 + RangeEnc_FlushStream(p);
3435 + while (--p->cacheSize != 0);
3436 + p->cache = (Byte)((UInt32)p->low >> 24);
3439 + p->low = (UInt32)p->low << 8;
3442 +static void RangeEnc_FlushData(CRangeEnc *p)
3445 + for (i = 0; i < 5; i++)
3446 + RangeEnc_ShiftLow(p);
3449 +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
3454 + p->low += p->range & (0 - ((value >> --numBits) & 1));
3455 + if (p->range < kTopValue)
3458 + RangeEnc_ShiftLow(p);
3461 + while (numBits != 0);
3464 +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
3466 + UInt32 ttt = *prob;
3467 + UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
3470 + p->range = newBound;
3471 + ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
3475 + p->low += newBound;
3476 + p->range -= newBound;
3477 + ttt -= ttt >> kNumMoveBits;
3479 + *prob = (CLzmaProb)ttt;
3480 + if (p->range < kTopValue)
3483 + RangeEnc_ShiftLow(p);
3487 +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
3492 + RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
3495 + while (symbol < 0x10000);
3498 +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
3500 + UInt32 offs = 0x100;
3505 + RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
3507 + offs &= ~(matchByte ^ symbol);
3509 + while (symbol < 0x10000);
3512 +void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
3515 + for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
3517 + const int kCyclesBits = kNumBitPriceShiftBits;
3519 + UInt32 bitCount = 0;
3521 + for (j = 0; j < kCyclesBits; j++)
3525 + while (w >= ((UInt32)1 << 16))
3531 + ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
3536 +#define GET_PRICE(prob, symbol) \
3537 + p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3539 +#define GET_PRICEa(prob, symbol) \
3540 + ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3542 +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
3543 +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3545 +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
3546 +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3548 +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
3554 + price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
3557 + while (symbol < 0x10000);
3561 +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
3564 + UInt32 offs = 0x100;
3569 + price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
3571 + offs &= ~(matchByte ^ symbol);
3573 + while (symbol < 0x10000);
3578 +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3582 + for (i = numBitLevels; i != 0;)
3586 + bit = (symbol >> i) & 1;
3587 + RangeEnc_EncodeBit(rc, probs + m, bit);
3588 + m = (m << 1) | bit;
3592 +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3596 + for (i = 0; i < numBitLevels; i++)
3598 + UInt32 bit = symbol & 1;
3599 + RangeEnc_EncodeBit(rc, probs + m, bit);
3600 + m = (m << 1) | bit;
3605 +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3608 + symbol |= (1 << numBitLevels);
3609 + while (symbol != 1)
3611 + price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
3617 +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3622 + for (i = numBitLevels; i != 0; i--)
3624 + UInt32 bit = symbol & 1;
3626 + price += GET_PRICEa(probs[m], bit);
3627 + m = (m << 1) | bit;
3633 +static void LenEnc_Init(CLenEnc *p)
3636 + p->choice = p->choice2 = kProbInitValue;
3637 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
3638 + p->low[i] = kProbInitValue;
3639 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
3640 + p->mid[i] = kProbInitValue;
3641 + for (i = 0; i < kLenNumHighSymbols; i++)
3642 + p->high[i] = kProbInitValue;
3645 +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
3647 + if (symbol < kLenNumLowSymbols)
3649 + RangeEnc_EncodeBit(rc, &p->choice, 0);
3650 + RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
3654 + RangeEnc_EncodeBit(rc, &p->choice, 1);
3655 + if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
3657 + RangeEnc_EncodeBit(rc, &p->choice2, 0);
3658 + RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
3662 + RangeEnc_EncodeBit(rc, &p->choice2, 1);
3663 + RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
3668 +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
3670 + UInt32 a0 = GET_PRICE_0a(p->choice);
3671 + UInt32 a1 = GET_PRICE_1a(p->choice);
3672 + UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
3673 + UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
3675 + for (i = 0; i < kLenNumLowSymbols; i++)
3677 + if (i >= numSymbols)
3679 + prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
3681 + for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
3683 + if (i >= numSymbols)
3685 + prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
3687 + for (; i < numSymbols; i++)
3688 + prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
3691 +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
3693 + LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
3694 + p->counters[posState] = p->tableSize;
3697 +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
3700 + for (posState = 0; posState < numPosStates; posState++)
3701 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3704 +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
3706 + LenEnc_Encode(&p->p, rc, symbol, posState);
3708 + if (--p->counters[posState] == 0)
3709 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3715 +static void MovePos(CLzmaEnc *p, UInt32 num)
3719 + printf("\n MovePos %d", num);
3723 + p->additionalOffset += num;
3724 + p->matchFinder.Skip(p->matchFinderObj, num);
3728 +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
3730 + UInt32 lenRes = 0, numPairs;
3731 + p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
3732 + numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
3734 + printf("\n i = %d numPairs = %d ", ttt, numPairs / 2);
3738 + for (i = 0; i < numPairs; i += 2)
3739 + printf("%2d %6d | ", p->matches[i], p->matches[i + 1]);
3744 + lenRes = p->matches[numPairs - 2];
3745 + if (lenRes == p->numFastBytes)
3747 + const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3748 + UInt32 distance = p->matches[numPairs - 1] + 1;
3749 + UInt32 numAvail = p->numAvail;
3750 + if (numAvail > LZMA_MATCH_LEN_MAX)
3751 + numAvail = LZMA_MATCH_LEN_MAX;
3753 + const Byte *pby2 = pby - distance;
3754 + for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
3758 + p->additionalOffset++;
3759 + *numDistancePairsRes = numPairs;
3764 +#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
3765 +#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
3766 +#define IsShortRep(p) ((p)->backPrev == 0)
3768 +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
3771 + GET_PRICE_0(p->isRepG0[state]) +
3772 + GET_PRICE_0(p->isRep0Long[state][posState]);
3775 +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
3778 + if (repIndex == 0)
3780 + price = GET_PRICE_0(p->isRepG0[state]);
3781 + price += GET_PRICE_1(p->isRep0Long[state][posState]);
3785 + price = GET_PRICE_1(p->isRepG0[state]);
3786 + if (repIndex == 1)
3787 + price += GET_PRICE_0(p->isRepG1[state]);
3790 + price += GET_PRICE_1(p->isRepG1[state]);
3791 + price += GET_PRICE(p->isRepG2[state], repIndex - 2);
3797 +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
3799 + return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
3800 + GetPureRepPrice(p, repIndex, state, posState);
3803 +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
3805 + UInt32 posMem = p->opt[cur].posPrev;
3806 + UInt32 backMem = p->opt[cur].backPrev;
3807 + p->optimumEndIndex = cur;
3810 + if (p->opt[cur].prev1IsChar)
3812 + MakeAsChar(&p->opt[posMem])
3813 + p->opt[posMem].posPrev = posMem - 1;
3814 + if (p->opt[cur].prev2)
3816 + p->opt[posMem - 1].prev1IsChar = False;
3817 + p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
3818 + p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
3822 + UInt32 posPrev = posMem;
3823 + UInt32 backCur = backMem;
3825 + backMem = p->opt[posPrev].backPrev;
3826 + posMem = p->opt[posPrev].posPrev;
3828 + p->opt[posPrev].backPrev = backCur;
3829 + p->opt[posPrev].posPrev = cur;
3834 + *backRes = p->opt[0].backPrev;
3835 + p->optimumCurrentIndex = p->opt[0].posPrev;
3836 + return p->optimumCurrentIndex;
3839 +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
3841 +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
3843 + UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;
3844 + UInt32 matchPrice, repMatchPrice, normalMatchPrice;
3845 + UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
3848 + Byte curByte, matchByte;
3849 + if (p->optimumEndIndex != p->optimumCurrentIndex)
3851 + const COptimal *opt = &p->opt[p->optimumCurrentIndex];
3852 + UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
3853 + *backRes = opt->backPrev;
3854 + p->optimumCurrentIndex = opt->posPrev;
3857 + p->optimumCurrentIndex = p->optimumEndIndex = 0;
3859 + if (p->additionalOffset == 0)
3860 + mainLen = ReadMatchDistances(p, &numPairs);
3863 + mainLen = p->longestMatchLength;
3864 + numPairs = p->numPairs;
3867 + numAvail = p->numAvail;
3870 + *backRes = (UInt32)(-1);
3873 + if (numAvail > LZMA_MATCH_LEN_MAX)
3874 + numAvail = LZMA_MATCH_LEN_MAX;
3876 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3878 + for (i = 0; i < LZMA_NUM_REPS; i++)
3881 + const Byte *data2;
3882 + reps[i] = p->reps[i];
3883 + data2 = data - (reps[i] + 1);
3884 + if (data[0] != data2[0] || data[1] != data2[1])
3889 + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
3890 + repLens[i] = lenTest;
3891 + if (lenTest > repLens[repMaxIndex])
3894 + if (repLens[repMaxIndex] >= p->numFastBytes)
3897 + *backRes = repMaxIndex;
3898 + lenRes = repLens[repMaxIndex];
3899 + MovePos(p, lenRes - 1);
3903 + matches = p->matches;
3904 + if (mainLen >= p->numFastBytes)
3906 + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
3907 + MovePos(p, mainLen - 1);
3911 + matchByte = *(data - (reps[0] + 1));
3913 + if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2)
3915 + *backRes = (UInt32)-1;
3919 + p->opt[0].state = (CState)p->state;
3921 + posState = (position & p->pbMask);
3924 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
3925 + p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
3926 + (!IsCharState(p->state) ?
3927 + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
3928 + LitEnc_GetPrice(probs, curByte, p->ProbPrices));
3931 + MakeAsChar(&p->opt[1]);
3933 + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
3934 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
3936 + if (matchByte == curByte)
3938 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
3939 + if (shortRepPrice < p->opt[1].price)
3941 + p->opt[1].price = shortRepPrice;
3942 + MakeAsShortRep(&p->opt[1]);
3945 + lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
3949 + *backRes = p->opt[1].backPrev;
3953 + p->opt[1].posPrev = 0;
3954 + for (i = 0; i < LZMA_NUM_REPS; i++)
3955 + p->opt[0].backs[i] = reps[i];
3959 + p->opt[len--].price = kInfinityPrice;
3962 + for (i = 0; i < LZMA_NUM_REPS; i++)
3964 + UInt32 repLen = repLens[i];
3968 + price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
3971 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
3972 + COptimal *opt = &p->opt[repLen];
3973 + if (curAndLenPrice < opt->price)
3975 + opt->price = curAndLenPrice;
3977 + opt->backPrev = i;
3978 + opt->prev1IsChar = False;
3981 + while (--repLen >= 2);
3984 + normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
3986 + len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
3987 + if (len <= mainLen)
3990 + while (len > matches[offs])
3995 + UInt32 distance = matches[offs + 1];
3997 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
3998 + UInt32 lenToPosState = GetLenToPosState(len);
3999 + if (distance < kNumFullDistances)
4000 + curAndLenPrice += p->distancesPrices[lenToPosState][distance];
4004 + GetPosSlot2(distance, slot);
4005 + curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
4007 + opt = &p->opt[len];
4008 + if (curAndLenPrice < opt->price)
4010 + opt->price = curAndLenPrice;
4012 + opt->backPrev = distance + LZMA_NUM_REPS;
4013 + opt->prev1IsChar = False;
4015 + if (len == matches[offs])
4018 + if (offs == numPairs)
4027 + if (position >= 0)
4030 + printf("\n pos = %4X", position);
4031 + for (i = cur; i <= lenEnd; i++)
4032 + printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
4038 + UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen;
4039 + UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice;
4041 + Byte curByte, matchByte;
4044 + COptimal *nextOpt;
4047 + if (cur == lenEnd)
4048 + return Backward(p, backRes, cur);
4050 + newLen = ReadMatchDistances(p, &numPairs);
4051 + if (newLen >= p->numFastBytes)
4053 + p->numPairs = numPairs;
4054 + p->longestMatchLength = newLen;
4055 + return Backward(p, backRes, cur);
4058 + curOpt = &p->opt[cur];
4059 + posPrev = curOpt->posPrev;
4060 + if (curOpt->prev1IsChar)
4063 + if (curOpt->prev2)
4065 + state = p->opt[curOpt->posPrev2].state;
4066 + if (curOpt->backPrev2 < LZMA_NUM_REPS)
4067 + state = kRepNextStates[state];
4069 + state = kMatchNextStates[state];
4072 + state = p->opt[posPrev].state;
4073 + state = kLiteralNextStates[state];
4076 + state = p->opt[posPrev].state;
4077 + if (posPrev == cur - 1)
4079 + if (IsShortRep(curOpt))
4080 + state = kShortRepNextStates[state];
4082 + state = kLiteralNextStates[state];
4087 + const COptimal *prevOpt;
4088 + if (curOpt->prev1IsChar && curOpt->prev2)
4090 + posPrev = curOpt->posPrev2;
4091 + pos = curOpt->backPrev2;
4092 + state = kRepNextStates[state];
4096 + pos = curOpt->backPrev;
4097 + if (pos < LZMA_NUM_REPS)
4098 + state = kRepNextStates[state];
4100 + state = kMatchNextStates[state];
4102 + prevOpt = &p->opt[posPrev];
4103 + if (pos < LZMA_NUM_REPS)
4106 + reps[0] = prevOpt->backs[pos];
4107 + for (i = 1; i <= pos; i++)
4108 + reps[i] = prevOpt->backs[i - 1];
4109 + for (; i < LZMA_NUM_REPS; i++)
4110 + reps[i] = prevOpt->backs[i];
4115 + reps[0] = (pos - LZMA_NUM_REPS);
4116 + for (i = 1; i < LZMA_NUM_REPS; i++)
4117 + reps[i] = prevOpt->backs[i - 1];
4120 + curOpt->state = (CState)state;
4122 + curOpt->backs[0] = reps[0];
4123 + curOpt->backs[1] = reps[1];
4124 + curOpt->backs[2] = reps[2];
4125 + curOpt->backs[3] = reps[3];
4127 + curPrice = curOpt->price;
4128 + nextIsChar = False;
4129 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
4131 + matchByte = *(data - (reps[0] + 1));
4133 + posState = (position & p->pbMask);
4135 + curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
4137 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
4139 + (!IsCharState(state) ?
4140 + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
4141 + LitEnc_GetPrice(probs, curByte, p->ProbPrices));
4144 + nextOpt = &p->opt[cur + 1];
4146 + if (curAnd1Price < nextOpt->price)
4148 + nextOpt->price = curAnd1Price;
4149 + nextOpt->posPrev = cur;
4150 + MakeAsChar(nextOpt);
4151 + nextIsChar = True;
4154 + matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
4155 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
4157 + if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
4159 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
4160 + if (shortRepPrice <= nextOpt->price)
4162 + nextOpt->price = shortRepPrice;
4163 + nextOpt->posPrev = cur;
4164 + MakeAsShortRep(nextOpt);
4165 + nextIsChar = True;
4168 + numAvailFull = p->numAvail;
4170 + UInt32 temp = kNumOpts - 1 - cur;
4171 + if (temp < numAvailFull)
4172 + numAvailFull = temp;
4175 + if (numAvailFull < 2)
4177 + numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
4179 + if (!nextIsChar && matchByte != curByte) /* speed optimization */
4181 + /* try Literal + rep0 */
4184 + const Byte *data2 = data - (reps[0] + 1);
4185 + UInt32 limit = p->numFastBytes + 1;
4186 + if (limit > numAvailFull)
4187 + limit = numAvailFull;
4189 + for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
4190 + lenTest2 = temp - 1;
4191 + if (lenTest2 >= 2)
4193 + UInt32 state2 = kLiteralNextStates[state];
4194 + UInt32 posStateNext = (position + 1) & p->pbMask;
4195 + UInt32 nextRepMatchPrice = curAnd1Price +
4196 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4197 + GET_PRICE_1(p->isRep[state2]);
4198 + /* for (; lenTest2 >= 2; lenTest2--) */
4200 + UInt32 curAndLenPrice;
4202 + UInt32 offset = cur + 1 + lenTest2;
4203 + while (lenEnd < offset)
4204 + p->opt[++lenEnd].price = kInfinityPrice;
4205 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4206 + opt = &p->opt[offset];
4207 + if (curAndLenPrice < opt->price)
4209 + opt->price = curAndLenPrice;
4210 + opt->posPrev = cur + 1;
4211 + opt->backPrev = 0;
4212 + opt->prev1IsChar = True;
4213 + opt->prev2 = False;
4219 + startLen = 2; /* speed optimization */
4222 + for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
4225 + UInt32 lenTestTemp;
4227 + const Byte *data2 = data - (reps[repIndex] + 1);
4228 + if (data[0] != data2[0] || data[1] != data2[1])
4230 + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
4231 + while (lenEnd < cur + lenTest)
4232 + p->opt[++lenEnd].price = kInfinityPrice;
4233 + lenTestTemp = lenTest;
4234 + price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
4237 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
4238 + COptimal *opt = &p->opt[cur + lenTest];
4239 + if (curAndLenPrice < opt->price)
4241 + opt->price = curAndLenPrice;
4242 + opt->posPrev = cur;
4243 + opt->backPrev = repIndex;
4244 + opt->prev1IsChar = False;
4247 + while (--lenTest >= 2);
4248 + lenTest = lenTestTemp;
4250 + if (repIndex == 0)
4251 + startLen = lenTest + 1;
4253 + /* if (_maxMode) */
4255 + UInt32 lenTest2 = lenTest + 1;
4256 + UInt32 limit = lenTest2 + p->numFastBytes;
4257 + UInt32 nextRepMatchPrice;
4258 + if (limit > numAvailFull)
4259 + limit = numAvailFull;
4260 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
4261 + lenTest2 -= lenTest + 1;
4262 + if (lenTest2 >= 2)
4264 + UInt32 state2 = kRepNextStates[state];
4265 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
4266 + UInt32 curAndLenCharPrice =
4267 + price + p->repLenEnc.prices[posState][lenTest - 2] +
4268 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
4269 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
4270 + data[lenTest], data2[lenTest], p->ProbPrices);
4271 + state2 = kLiteralNextStates[state2];
4272 + posStateNext = (position + lenTest + 1) & p->pbMask;
4273 + nextRepMatchPrice = curAndLenCharPrice +
4274 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4275 + GET_PRICE_1(p->isRep[state2]);
4277 + /* for (; lenTest2 >= 2; lenTest2--) */
4279 + UInt32 curAndLenPrice;
4281 + UInt32 offset = cur + lenTest + 1 + lenTest2;
4282 + while (lenEnd < offset)
4283 + p->opt[++lenEnd].price = kInfinityPrice;
4284 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4285 + opt = &p->opt[offset];
4286 + if (curAndLenPrice < opt->price)
4288 + opt->price = curAndLenPrice;
4289 + opt->posPrev = cur + lenTest + 1;
4290 + opt->backPrev = 0;
4291 + opt->prev1IsChar = True;
4292 + opt->prev2 = True;
4293 + opt->posPrev2 = cur;
4294 + opt->backPrev2 = repIndex;
4301 + /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
4302 + if (newLen > numAvail)
4304 + newLen = numAvail;
4305 + for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2);
4306 + matches[numPairs] = newLen;
4309 + if (newLen >= startLen)
4311 + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
4312 + UInt32 offs, curBack, posSlot;
4314 + while (lenEnd < cur + newLen)
4315 + p->opt[++lenEnd].price = kInfinityPrice;
4318 + while (startLen > matches[offs])
4320 + curBack = matches[offs + 1];
4321 + GetPosSlot2(curBack, posSlot);
4322 + for (lenTest = /*2*/ startLen; ; lenTest++)
4324 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
4325 + UInt32 lenToPosState = GetLenToPosState(lenTest);
4327 + if (curBack < kNumFullDistances)
4328 + curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
4330 + curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
4332 + opt = &p->opt[cur + lenTest];
4333 + if (curAndLenPrice < opt->price)
4335 + opt->price = curAndLenPrice;
4336 + opt->posPrev = cur;
4337 + opt->backPrev = curBack + LZMA_NUM_REPS;
4338 + opt->prev1IsChar = False;
4341 + if (/*_maxMode && */lenTest == matches[offs])
4343 + /* Try Match + Literal + Rep0 */
4344 + const Byte *data2 = data - (curBack + 1);
4345 + UInt32 lenTest2 = lenTest + 1;
4346 + UInt32 limit = lenTest2 + p->numFastBytes;
4347 + UInt32 nextRepMatchPrice;
4348 + if (limit > numAvailFull)
4349 + limit = numAvailFull;
4350 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
4351 + lenTest2 -= lenTest + 1;
4352 + if (lenTest2 >= 2)
4354 + UInt32 state2 = kMatchNextStates[state];
4355 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
4356 + UInt32 curAndLenCharPrice = curAndLenPrice +
4357 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
4358 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
4359 + data[lenTest], data2[lenTest], p->ProbPrices);
4360 + state2 = kLiteralNextStates[state2];
4361 + posStateNext = (posStateNext + 1) & p->pbMask;
4362 + nextRepMatchPrice = curAndLenCharPrice +
4363 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4364 + GET_PRICE_1(p->isRep[state2]);
4366 + /* for (; lenTest2 >= 2; lenTest2--) */
4368 + UInt32 offset = cur + lenTest + 1 + lenTest2;
4369 + UInt32 curAndLenPrice;
4371 + while (lenEnd < offset)
4372 + p->opt[++lenEnd].price = kInfinityPrice;
4373 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4374 + opt = &p->opt[offset];
4375 + if (curAndLenPrice < opt->price)
4377 + opt->price = curAndLenPrice;
4378 + opt->posPrev = cur + lenTest + 1;
4379 + opt->backPrev = 0;
4380 + opt->prev1IsChar = True;
4381 + opt->prev2 = True;
4382 + opt->posPrev2 = cur;
4383 + opt->backPrev2 = curBack + LZMA_NUM_REPS;
4388 + if (offs == numPairs)
4390 + curBack = matches[offs + 1];
4391 + if (curBack >= kNumFullDistances)
4392 + GetPosSlot2(curBack, posSlot);
4399 +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
4401 +static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
4403 + UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
4405 + const UInt32 *matches;
4407 + if (p->additionalOffset == 0)
4408 + mainLen = ReadMatchDistances(p, &numPairs);
4411 + mainLen = p->longestMatchLength;
4412 + numPairs = p->numPairs;
4415 + numAvail = p->numAvail;
4416 + *backRes = (UInt32)-1;
4419 + if (numAvail > LZMA_MATCH_LEN_MAX)
4420 + numAvail = LZMA_MATCH_LEN_MAX;
4421 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
4423 + repLen = repIndex = 0;
4424 + for (i = 0; i < LZMA_NUM_REPS; i++)
4427 + const Byte *data2 = data - (p->reps[i] + 1);
4428 + if (data[0] != data2[0] || data[1] != data2[1])
4430 + for (len = 2; len < numAvail && data[len] == data2[len]; len++);
4431 + if (len >= p->numFastBytes)
4434 + MovePos(p, len - 1);
4444 + matches = p->matches;
4445 + if (mainLen >= p->numFastBytes)
4447 + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
4448 + MovePos(p, mainLen - 1);
4452 + mainDist = 0; /* for GCC */
4455 + mainDist = matches[numPairs - 1];
4456 + while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
4458 + if (!ChangePair(matches[numPairs - 3], mainDist))
4461 + mainLen = matches[numPairs - 2];
4462 + mainDist = matches[numPairs - 1];
4464 + if (mainLen == 2 && mainDist >= 0x80)
4468 + if (repLen >= 2 && (
4469 + (repLen + 1 >= mainLen) ||
4470 + (repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
4471 + (repLen + 3 >= mainLen && mainDist >= (1 << 15))))
4473 + *backRes = repIndex;
4474 + MovePos(p, repLen - 1);
4478 + if (mainLen < 2 || numAvail <= 2)
4481 + p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
4482 + if (p->longestMatchLength >= 2)
4484 + UInt32 newDistance = matches[p->numPairs - 1];
4485 + if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
4486 + (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
4487 + (p->longestMatchLength > mainLen + 1) ||
4488 + (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
4492 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
4493 + for (i = 0; i < LZMA_NUM_REPS; i++)
4495 + UInt32 len, limit;
4496 + const Byte *data2 = data - (p->reps[i] + 1);
4497 + if (data[0] != data2[0] || data[1] != data2[1])
4499 + limit = mainLen - 1;
4500 + for (len = 2; len < limit && data[len] == data2[len]; len++);
4504 + *backRes = mainDist + LZMA_NUM_REPS;
4505 + MovePos(p, mainLen - 2);
4509 +static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
4512 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4513 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4514 + p->state = kMatchNextStates[p->state];
4515 + len = LZMA_MATCH_LEN_MIN;
4516 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4517 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
4518 + RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
4519 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
4522 +static SRes CheckErrors(CLzmaEnc *p)
4524 + if (p->result != SZ_OK)
4526 + if (p->rc.res != SZ_OK)
4527 + p->result = SZ_ERROR_WRITE;
4528 + if (p->matchFinderBase.result != SZ_OK)
4529 + p->result = SZ_ERROR_READ;
4530 + if (p->result != SZ_OK)
4531 + p->finished = True;
4535 +static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
4537 + /* ReleaseMFStream(); */
4538 + p->finished = True;
4539 + if (p->writeEndMark)
4540 + WriteEndMarker(p, nowPos & p->pbMask);
4541 + RangeEnc_FlushData(&p->rc);
4542 + RangeEnc_FlushStream(&p->rc);
4543 + return CheckErrors(p);
4546 +static void FillAlignPrices(CLzmaEnc *p)
4549 + for (i = 0; i < kAlignTableSize; i++)
4550 + p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
4551 + p->alignPriceCount = 0;
4554 +static void FillDistancesPrices(CLzmaEnc *p)
4556 + UInt32 tempPrices[kNumFullDistances];
4557 + UInt32 i, lenToPosState;
4558 + for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
4560 + UInt32 posSlot = GetPosSlot1(i);
4561 + UInt32 footerBits = ((posSlot >> 1) - 1);
4562 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4563 + tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
4566 + for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
4569 + const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
4570 + UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
4571 + for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
4572 + posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
4573 + for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
4574 + posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
4577 + UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
4579 + for (i = 0; i < kStartPosModelIndex; i++)
4580 + distancesPrices[i] = posSlotPrices[i];
4581 + for (; i < kNumFullDistances; i++)
4582 + distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
4585 + p->matchPriceCount = 0;
4588 +void LzmaEnc_Construct(CLzmaEnc *p)
4590 + RangeEnc_Construct(&p->rc);
4591 + MatchFinder_Construct(&p->matchFinderBase);
4593 + MatchFinderMt_Construct(&p->matchFinderMt);
4594 + p->matchFinderMt.MatchFinder = &p->matchFinderBase;
4598 + CLzmaEncProps props;
4599 + LzmaEncProps_Init(&props);
4600 + LzmaEnc_SetProps(p, &props);
4603 + #ifndef LZMA_LOG_BSR
4604 + LzmaEnc_FastPosInit(p->g_FastPos);
4607 + LzmaEnc_InitPriceTables(p->ProbPrices);
4609 + p->saveState.litProbs = 0;
4612 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
4615 + p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
4617 + LzmaEnc_Construct((CLzmaEnc *)p);
4621 +void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
4623 + alloc->Free(alloc, p->litProbs);
4624 + alloc->Free(alloc, p->saveState.litProbs);
4626 + p->saveState.litProbs = 0;
4629 +void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
4632 + MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
4634 + MatchFinder_Free(&p->matchFinderBase, allocBig);
4635 + LzmaEnc_FreeLits(p, alloc);
4636 + RangeEnc_Free(&p->rc, alloc);
4639 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
4641 + LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
4642 + alloc->Free(alloc, p);
4645 +static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
4647 + UInt32 nowPos32, startPos32;
4650 + p->matchFinder.Init(p->matchFinderObj);
4656 + RINOK(CheckErrors(p));
4658 + nowPos32 = (UInt32)p->nowPos64;
4659 + startPos32 = nowPos32;
4661 + if (p->nowPos64 == 0)
4665 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4666 + return Flush(p, nowPos32);
4667 + ReadMatchDistances(p, &numPairs);
4668 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
4669 + p->state = kLiteralNextStates[p->state];
4670 + curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
4671 + LitEnc_Encode(&p->rc, p->litProbs, curByte);
4672 + p->additionalOffset--;
4676 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
4679 + UInt32 pos, len, posState;
4682 + len = GetOptimumFast(p, &pos);
4684 + len = GetOptimum(p, nowPos32, &pos);
4687 + printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
4690 + posState = nowPos32 & p->pbMask;
4691 + if (len == 1 && pos == (UInt32)-1)
4697 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
4698 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
4700 + probs = LIT_PROBS(nowPos32, *(data - 1));
4701 + if (IsCharState(p->state))
4702 + LitEnc_Encode(&p->rc, probs, curByte);
4704 + LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
4705 + p->state = kLiteralNextStates[p->state];
4709 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4710 + if (pos < LZMA_NUM_REPS)
4712 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
4715 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
4716 + RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
4720 + UInt32 distance = p->reps[pos];
4721 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
4723 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
4726 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
4727 + RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
4729 + p->reps[3] = p->reps[2];
4730 + p->reps[2] = p->reps[1];
4732 + p->reps[1] = p->reps[0];
4733 + p->reps[0] = distance;
4736 + p->state = kShortRepNextStates[p->state];
4739 + LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4740 + p->state = kRepNextStates[p->state];
4746 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4747 + p->state = kMatchNextStates[p->state];
4748 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4749 + pos -= LZMA_NUM_REPS;
4750 + GetPosSlot(pos, posSlot);
4751 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
4753 + if (posSlot >= kStartPosModelIndex)
4755 + UInt32 footerBits = ((posSlot >> 1) - 1);
4756 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4757 + UInt32 posReduced = pos - base;
4759 + if (posSlot < kEndPosModelIndex)
4760 + RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
4763 + RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
4764 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
4765 + p->alignPriceCount++;
4768 + p->reps[3] = p->reps[2];
4769 + p->reps[2] = p->reps[1];
4770 + p->reps[1] = p->reps[0];
4772 + p->matchPriceCount++;
4775 + p->additionalOffset -= len;
4777 + if (p->additionalOffset == 0)
4782 + if (p->matchPriceCount >= (1 << 7))
4783 + FillDistancesPrices(p);
4784 + if (p->alignPriceCount >= kAlignTableSize)
4785 + FillAlignPrices(p);
4787 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4789 + processed = nowPos32 - startPos32;
4792 + if (processed + kNumOpts + 300 >= maxUnpackSize ||
4793 + RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
4796 + else if (processed >= (1 << 15))
4798 + p->nowPos64 += nowPos32 - startPos32;
4799 + return CheckErrors(p);
4803 + p->nowPos64 += nowPos32 - startPos32;
4804 + return Flush(p, nowPos32);
4807 +#define kBigHashDicLimit ((UInt32)1 << 24)
4809 +static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4811 + UInt32 beforeSize = kNumOpts;
4813 + if (!RangeEnc_Alloc(&p->rc, alloc))
4814 + return SZ_ERROR_MEM;
4815 + btMode = (p->matchFinderBase.btMode != 0);
4817 + p->mtMode = (p->multiThread && !p->fastMode && btMode);
4821 + unsigned lclp = p->lc + p->lp;
4822 + if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
4824 + LzmaEnc_FreeLits(p, alloc);
4825 + p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
4826 + p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
4827 + if (p->litProbs == 0 || p->saveState.litProbs == 0)
4829 + LzmaEnc_FreeLits(p, alloc);
4830 + return SZ_ERROR_MEM;
4836 + p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
4838 + if (beforeSize + p->dictSize < keepWindowSize)
4839 + beforeSize = keepWindowSize - p->dictSize;
4844 + RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
4845 + p->matchFinderObj = &p->matchFinderMt;
4846 + MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
4851 + if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
4852 + return SZ_ERROR_MEM;
4853 + p->matchFinderObj = &p->matchFinderBase;
4854 + MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
4859 +void LzmaEnc_Init(CLzmaEnc *p)
4863 + for (i = 0 ; i < LZMA_NUM_REPS; i++)
4866 + RangeEnc_Init(&p->rc);
4869 + for (i = 0; i < kNumStates; i++)
4872 + for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
4874 + p->isMatch[i][j] = kProbInitValue;
4875 + p->isRep0Long[i][j] = kProbInitValue;
4877 + p->isRep[i] = kProbInitValue;
4878 + p->isRepG0[i] = kProbInitValue;
4879 + p->isRepG1[i] = kProbInitValue;
4880 + p->isRepG2[i] = kProbInitValue;
4884 + UInt32 num = 0x300 << (p->lp + p->lc);
4885 + for (i = 0; i < num; i++)
4886 + p->litProbs[i] = kProbInitValue;
4890 + for (i = 0; i < kNumLenToPosStates; i++)
4892 + CLzmaProb *probs = p->posSlotEncoder[i];
4894 + for (j = 0; j < (1 << kNumPosSlotBits); j++)
4895 + probs[j] = kProbInitValue;
4899 + for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
4900 + p->posEncoders[i] = kProbInitValue;
4903 + LenEnc_Init(&p->lenEnc.p);
4904 + LenEnc_Init(&p->repLenEnc.p);
4906 + for (i = 0; i < (1 << kNumAlignBits); i++)
4907 + p->posAlignEncoder[i] = kProbInitValue;
4909 + p->optimumEndIndex = 0;
4910 + p->optimumCurrentIndex = 0;
4911 + p->additionalOffset = 0;
4913 + p->pbMask = (1 << p->pb) - 1;
4914 + p->lpMask = (1 << p->lp) - 1;
4917 +void LzmaEnc_InitPrices(CLzmaEnc *p)
4921 + FillDistancesPrices(p);
4922 + FillAlignPrices(p);
4925 + p->lenEnc.tableSize =
4926 + p->repLenEnc.tableSize =
4927 + p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
4928 + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
4929 + LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
4932 +static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4935 + for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
4936 + if (p->dictSize <= ((UInt32)1 << i))
4938 + p->distTableSize = i * 2;
4940 + p->finished = False;
4941 + p->result = SZ_OK;
4942 + RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
4944 + LzmaEnc_InitPrices(p);
4949 +static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream,
4950 + ISzAlloc *alloc, ISzAlloc *allocBig)
4952 + CLzmaEnc *p = (CLzmaEnc *)pp;
4953 + p->matchFinderBase.stream = inStream;
4955 + p->rc.outStream = outStream;
4956 + return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
4959 +SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
4960 + ISeqInStream *inStream, UInt32 keepWindowSize,
4961 + ISzAlloc *alloc, ISzAlloc *allocBig)
4963 + CLzmaEnc *p = (CLzmaEnc *)pp;
4964 + p->matchFinderBase.stream = inStream;
4966 + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
4969 +static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
4971 + p->matchFinderBase.directInput = 1;
4972 + p->matchFinderBase.bufferBase = (Byte *)src;
4973 + p->matchFinderBase.directInputRem = srcLen;
4976 +SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
4977 + UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4979 + CLzmaEnc *p = (CLzmaEnc *)pp;
4980 + LzmaEnc_SetInputBuf(p, src, srcLen);
4983 + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
4986 +void LzmaEnc_Finish(CLzmaEncHandle pp)
4989 + CLzmaEnc *p = (CLzmaEnc *)pp;
4991 + MatchFinderMt_ReleaseStream(&p->matchFinderMt);
4999 + ISeqOutStream funcTable;
5003 +} CSeqOutStreamBuf;
5005 +static size_t MyWrite(void *pp, const void *data, size_t size)
5007 + CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
5008 + if (p->rem < size)
5011 + p->overflow = True;
5013 + memcpy(p->data, data, size);
5020 +UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
5022 + const CLzmaEnc *p = (CLzmaEnc *)pp;
5023 + return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
5026 +const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)
5028 + const CLzmaEnc *p = (CLzmaEnc *)pp;
5029 + return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
5032 +SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
5033 + Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
5035 + CLzmaEnc *p = (CLzmaEnc *)pp;
5038 + CSeqOutStreamBuf outStream;
5040 + outStream.funcTable.Write = MyWrite;
5041 + outStream.data = dest;
5042 + outStream.rem = *destLen;
5043 + outStream.overflow = False;
5045 + p->writeEndMark = False;
5046 + p->finished = False;
5047 + p->result = SZ_OK;
5051 + LzmaEnc_InitPrices(p);
5052 + nowPos64 = p->nowPos64;
5053 + RangeEnc_Init(&p->rc);
5054 + p->rc.outStream = &outStream.funcTable;
5056 + res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize);
5058 + *unpackSize = (UInt32)(p->nowPos64 - nowPos64);
5059 + *destLen -= outStream.rem;
5060 + if (outStream.overflow)
5061 + return SZ_ERROR_OUTPUT_EOF;
5066 +static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
5071 + Byte allocaDummy[0x300];
5073 + for (i = 0; i < 16; i++)
5074 + allocaDummy[i] = (Byte)i;
5079 + res = LzmaEnc_CodeOneBlock(p, False, 0, 0);
5080 + if (res != SZ_OK || p->finished != 0)
5082 + if (progress != 0)
5084 + res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
5087 + res = SZ_ERROR_PROGRESS;
5092 + LzmaEnc_Finish(p);
5096 +SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
5097 + ISzAlloc *alloc, ISzAlloc *allocBig)
5099 + RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig));
5100 + return LzmaEnc_Encode2((CLzmaEnc *)pp, progress);
5103 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
5105 + CLzmaEnc *p = (CLzmaEnc *)pp;
5107 + UInt32 dictSize = p->dictSize;
5108 + if (*size < LZMA_PROPS_SIZE)
5109 + return SZ_ERROR_PARAM;
5110 + *size = LZMA_PROPS_SIZE;
5111 + props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
5113 + for (i = 11; i <= 30; i++)
5115 + if (dictSize <= ((UInt32)2 << i))
5117 + dictSize = (2 << i);
5120 + if (dictSize <= ((UInt32)3 << i))
5122 + dictSize = (3 << i);
5127 + for (i = 0; i < 4; i++)
5128 + props[1 + i] = (Byte)(dictSize >> (8 * i));
5132 +SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
5133 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
5136 + CLzmaEnc *p = (CLzmaEnc *)pp;
5138 + CSeqOutStreamBuf outStream;
5140 + LzmaEnc_SetInputBuf(p, src, srcLen);
5142 + outStream.funcTable.Write = MyWrite;
5143 + outStream.data = dest;
5144 + outStream.rem = *destLen;
5145 + outStream.overflow = False;
5147 + p->writeEndMark = writeEndMark;
5149 + p->rc.outStream = &outStream.funcTable;
5150 + res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);
5152 + res = LzmaEnc_Encode2(p, progress);
5154 + *destLen -= outStream.rem;
5155 + if (outStream.overflow)
5156 + return SZ_ERROR_OUTPUT_EOF;
5160 +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
5161 + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
5162 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
5164 + CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
5167 + return SZ_ERROR_MEM;
5169 + res = LzmaEnc_SetProps(p, props);
5172 + res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
5174 + res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
5175 + writeEndMark, progress, alloc, allocBig);
5178 + LzmaEnc_Destroy(p, alloc, allocBig);
5182 +++ b/lib/lzma/Makefile
5184 +lzma_compress-objs := LzFind.o LzmaEnc.o
5185 +lzma_decompress-objs := LzmaDec.o
5187 +obj-$(CONFIG_LZMA_COMPRESS) += lzma_compress.o
5188 +obj-$(CONFIG_LZMA_DECOMPRESS) += lzma_decompress.o
5190 +EXTRA_CFLAGS += -Iinclude/linux -Iinclude/linux/lzma -include types.h