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 @@ -378,6 +378,9 @@ int __init jffs2_compressors_init(void)
70 #ifdef CONFIG_JFFS2_LZO
73 +#ifdef CONFIG_JFFS2_LZMA
76 /* Setting default compression mode */
77 #ifdef CONFIG_JFFS2_CMODE_NONE
78 jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
79 @@ -401,6 +404,9 @@ int __init jffs2_compressors_init(void)
80 int jffs2_compressors_exit(void)
82 /* Unregistering compressors */
83 +#ifdef CONFIG_JFFS2_LZMA
86 #ifdef CONFIG_JFFS2_LZO
89 --- a/fs/jffs2/compr.h
90 +++ b/fs/jffs2/compr.h
92 #define JFFS2_DYNRUBIN_PRIORITY 20
93 #define JFFS2_LZARI_PRIORITY 30
94 #define JFFS2_RTIME_PRIORITY 50
95 -#define JFFS2_ZLIB_PRIORITY 60
96 -#define JFFS2_LZO_PRIORITY 80
98 +#define JFFS2_LZMA_PRIORITY 70
99 +#define JFFS2_ZLIB_PRIORITY 80
100 +#define JFFS2_LZO_PRIORITY 90
102 #define JFFS2_RUBINMIPS_DISABLED /* RUBINs will be used only */
103 #define JFFS2_DYNRUBIN_DISABLED /* for decompression */
104 @@ -101,5 +101,9 @@ void jffs2_zlib_exit(void);
105 int jffs2_lzo_init(void);
106 void jffs2_lzo_exit(void);
108 +#ifdef CONFIG_JFFS2_LZMA
109 +int jffs2_lzma_init(void);
110 +void jffs2_lzma_exit(void);
113 #endif /* __JFFS2_COMPR_H__ */
115 +++ b/fs/jffs2/compr_lzma.c
118 + * JFFS2 -- Journalling Flash File System, Version 2.
120 + * For licensing information, see the file 'LICENCE' in this directory.
122 + * JFFS2 wrapper to the LZMA C SDK
126 +#include <linux/lzma.h>
130 + static DEFINE_MUTEX(deflate_mutex);
134 +Byte propsEncoded[LZMA_PROPS_SIZE];
135 +SizeT propsSize = sizeof(propsEncoded);
137 +STATIC void lzma_free_workspace(void)
139 + LzmaEnc_Destroy(p, &lzma_alloc, &lzma_alloc);
142 +STATIC int INIT lzma_alloc_workspace(CLzmaEncProps *props)
144 + if ((p = (CLzmaEncHandle *)LzmaEnc_Create(&lzma_alloc)) == NULL)
146 + PRINT_ERROR("Failed to allocate lzma deflate workspace\n");
150 + if (LzmaEnc_SetProps(p, props) != SZ_OK)
152 + lzma_free_workspace();
156 + if (LzmaEnc_WriteProperties(p, propsEncoded, &propsSize) != SZ_OK)
158 + lzma_free_workspace();
165 +STATIC int jffs2_lzma_compress(unsigned char *data_in, unsigned char *cpage_out,
166 + uint32_t *sourcelen, uint32_t *dstlen)
168 + SizeT compress_size = (SizeT)(*dstlen);
172 + mutex_lock(&deflate_mutex);
175 + ret = LzmaEnc_MemEncode(p, cpage_out, &compress_size, data_in, *sourcelen,
176 + 0, NULL, &lzma_alloc, &lzma_alloc);
179 + mutex_unlock(&deflate_mutex);
185 + *dstlen = (uint32_t)compress_size;
190 +STATIC int jffs2_lzma_decompress(unsigned char *data_in, unsigned char *cpage_out,
191 + uint32_t srclen, uint32_t destlen)
194 + SizeT dl = (SizeT)destlen;
195 + SizeT sl = (SizeT)srclen;
196 + ELzmaStatus status;
198 + ret = LzmaDecode(cpage_out, &dl, data_in, &sl, propsEncoded,
199 + propsSize, LZMA_FINISH_ANY, &status, &lzma_alloc);
201 + if (ret != SZ_OK || status == LZMA_STATUS_NOT_FINISHED || dl != (SizeT)destlen)
207 +static struct jffs2_compressor jffs2_lzma_comp = {
208 + .priority = JFFS2_LZMA_PRIORITY,
210 + .compr = JFFS2_COMPR_LZMA,
211 + .compress = &jffs2_lzma_compress,
212 + .decompress = &jffs2_lzma_decompress,
216 +int INIT jffs2_lzma_init(void)
219 + CLzmaEncProps props;
220 + LzmaEncProps_Init(&props);
222 + props.dictSize = LZMA_BEST_DICT(0x2000);
223 + props.level = LZMA_BEST_LEVEL;
224 + props.lc = LZMA_BEST_LC;
225 + props.lp = LZMA_BEST_LP;
226 + props.pb = LZMA_BEST_PB;
227 + props.fb = LZMA_BEST_FB;
229 + ret = lzma_alloc_workspace(&props);
233 + ret = jffs2_register_compressor(&jffs2_lzma_comp);
235 + lzma_free_workspace();
240 +void jffs2_lzma_exit(void)
242 + jffs2_unregister_compressor(&jffs2_lzma_comp);
243 + lzma_free_workspace();
245 --- a/fs/jffs2/super.c
246 +++ b/fs/jffs2/super.c
247 @@ -374,14 +374,41 @@ static int __init init_jffs2_fs(void)
248 BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
249 BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
251 - pr_info("version 2.2."
252 + pr_info("version 2.2"
253 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
256 #ifdef CONFIG_JFFS2_SUMMARY
260 - " © 2001-2006 Red Hat, Inc.\n");
261 +#ifdef CONFIG_JFFS2_ZLIB
264 +#ifdef CONFIG_JFFS2_LZO
267 +#ifdef CONFIG_JFFS2_LZMA
270 +#ifdef CONFIG_JFFS2_RTIME
273 +#ifdef CONFIG_JFFS2_RUBIN
276 +#ifdef CONFIG_JFFS2_CMODE_NONE
279 +#ifdef CONFIG_JFFS2_CMODE_PRIORITY
280 + " (CMODE_PRIORITY)"
282 +#ifdef CONFIG_JFFS2_CMODE_SIZE
285 +#ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
286 + " (CMODE_FAVOURLZO)"
288 + " (c) 2001-2006 Red Hat, Inc.\n");
290 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
291 sizeof(struct jffs2_inode_info),
293 +++ b/include/linux/lzma.h
299 + #include <linux/kernel.h>
300 + #include <linux/sched.h>
301 + #include <linux/slab.h>
302 + #include <linux/vmalloc.h>
303 + #include <linux/init.h>
304 + #define LZMA_MALLOC vmalloc
305 + #define LZMA_FREE vfree
306 + #define PRINT_ERROR(msg) printk(KERN_WARNING #msg)
307 + #define INIT __init
308 + #define STATIC static
310 + #include <stdint.h>
311 + #include <stdlib.h>
313 + #include <unistd.h>
314 + #include <string.h>
315 + #include <asm/types.h>
317 + #include <linux/jffs2.h>
319 + extern int page_size;
320 + #define PAGE_SIZE page_size
322 + #define LZMA_MALLOC malloc
323 + #define LZMA_FREE free
324 + #define PRINT_ERROR(msg) fprintf(stderr, msg)
329 +#include "lzma/LzmaDec.h"
330 +#include "lzma/LzmaEnc.h"
332 +#define LZMA_BEST_LEVEL (9)
333 +#define LZMA_BEST_LC (0)
334 +#define LZMA_BEST_LP (0)
335 +#define LZMA_BEST_PB (0)
336 +#define LZMA_BEST_FB (273)
338 +#define LZMA_BEST_DICT(n) (((int)((n) / 2)) * 2)
340 +static void *p_lzma_malloc(void *p, size_t size)
345 + return LZMA_MALLOC(size);
348 +static void p_lzma_free(void *p, void *address)
350 + if (address != NULL)
351 + LZMA_FREE(address);
354 +static ISzAlloc lzma_alloc = { .Alloc = p_lzma_malloc, .Free = p_lzma_free };
358 +++ b/include/linux/lzma/LzFind.h
360 +/* LzFind.h -- Match finder for LZ algorithms
361 +2009-04-22 : Igor Pavlov : Public domain */
372 +typedef UInt32 CLzRef;
374 +typedef struct _CMatchFinder
382 + UInt32 cyclicBufferPos;
383 + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */
385 + UInt32 matchMaxLen;
392 + ISeqInStream *stream;
393 + int streamEndWasReached;
396 + UInt32 keepSizeBefore;
397 + UInt32 keepSizeAfter;
399 + UInt32 numHashBytes;
401 + size_t directInputRem;
404 + UInt32 historySize;
405 + UInt32 fixedHashSize;
406 + UInt32 hashSizeSum;
412 +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer)
413 +#define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)])
415 +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos)
417 +void MatchFinder_Construct(CMatchFinder *p);
420 + historySize <= 3 GB
421 + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB
423 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
424 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
426 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc);
430 + Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func.
431 + Mf_GetPointerToCurrentPos_Func's result must be used only before any other function
434 +typedef void (*Mf_Init_Func)(void *object);
435 +typedef Byte (*Mf_GetIndexByte_Func)(void *object, Int32 index);
436 +typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object);
437 +typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object);
438 +typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances);
439 +typedef void (*Mf_Skip_Func)(void *object, UInt32);
441 +typedef struct _IMatchFinder
444 + Mf_GetIndexByte_Func GetIndexByte;
445 + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes;
446 + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos;
447 + Mf_GetMatches_Func GetMatches;
451 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable);
459 +++ b/include/linux/lzma/LzHash.h
461 +/* LzHash.h -- HASH functions for LZ algorithms
462 +2009-02-07 : Igor Pavlov : Public domain */
467 +#define kHash2Size (1 << 10)
468 +#define kHash3Size (1 << 16)
469 +#define kHash4Size (1 << 20)
471 +#define kFix3HashSize (kHash2Size)
472 +#define kFix4HashSize (kHash2Size + kHash3Size)
473 +#define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size)
475 +#define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8);
477 +#define HASH3_CALC { \
478 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
479 + hash2Value = temp & (kHash2Size - 1); \
480 + hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; }
482 +#define HASH4_CALC { \
483 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
484 + hash2Value = temp & (kHash2Size - 1); \
485 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
486 + hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; }
488 +#define HASH5_CALC { \
489 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
490 + hash2Value = temp & (kHash2Size - 1); \
491 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
492 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \
493 + hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \
494 + hash4Value &= (kHash4Size - 1); }
496 +/* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */
497 +#define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF;
500 +#define MT_HASH2_CALC \
501 + hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1);
503 +#define MT_HASH3_CALC { \
504 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
505 + hash2Value = temp & (kHash2Size - 1); \
506 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); }
508 +#define MT_HASH4_CALC { \
509 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
510 + hash2Value = temp & (kHash2Size - 1); \
511 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
512 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); }
516 +++ b/include/linux/lzma/LzmaDec.h
518 +/* LzmaDec.h -- LZMA Decoder
519 +2009-02-07 : Igor Pavlov : Public domain */
521 +#ifndef __LZMA_DEC_H
522 +#define __LZMA_DEC_H
530 +/* #define _LZMA_PROB32 */
531 +/* _LZMA_PROB32 can increase the speed on some CPUs,
532 + but memory usage for CLzmaDec::probs will be doubled in that case */
535 +#define CLzmaProb UInt32
537 +#define CLzmaProb UInt16
541 +/* ---------- LZMA Properties ---------- */
543 +#define LZMA_PROPS_SIZE 5
545 +typedef struct _CLzmaProps
547 + unsigned lc, lp, pb;
552 +/* ---------- LZMA Decoder state ---------- */
554 +/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
555 + Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
557 +#define LZMA_REQUIRED_INPUT_MAX 20
565 + UInt32 range, code;
568 + UInt32 processedPos;
569 + UInt32 checkDicSize;
572 + unsigned remainLen;
576 + unsigned tempBufSize;
577 + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
580 +#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
582 +/* There are two types of LZMA streams:
583 + 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
584 + 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
588 + LZMA_FINISH_ANY, /* finish at any point */
589 + LZMA_FINISH_END /* block must be finished at the end */
592 +/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
594 + You must use LZMA_FINISH_END, when you know that current output buffer
595 + covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
597 + If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
598 + and output value of destLen will be less than output buffer size limit.
599 + You can check status result also.
601 + You can use multiple checks to test data integrity after full decompression:
602 + 1) Check Result and "status" variable.
603 + 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
604 + 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
605 + You must use correct finish mode in that case. */
609 + LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
610 + LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
611 + LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
612 + LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
613 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
616 +/* ELzmaStatus is used only as output value for function call */
618 +/* ---------- One Call Interface ---------- */
623 + It has meaning only if the decoding reaches output limit (*destLen).
624 + LZMA_FINISH_ANY - Decode just destLen bytes.
625 + LZMA_FINISH_END - Stream must be finished after (*destLen).
630 + LZMA_STATUS_FINISHED_WITH_MARK
631 + LZMA_STATUS_NOT_FINISHED
632 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
633 + SZ_ERROR_DATA - Data error
634 + SZ_ERROR_MEM - Memory allocation error
635 + SZ_ERROR_UNSUPPORTED - Unsupported properties
636 + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
639 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
640 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
641 + ELzmaStatus *status, ISzAlloc *alloc);
649 +++ b/include/linux/lzma/LzmaEnc.h
651 +/* LzmaEnc.h -- LZMA Encoder
652 +2009-02-07 : Igor Pavlov : Public domain */
654 +#ifndef __LZMA_ENC_H
655 +#define __LZMA_ENC_H
663 +#define LZMA_PROPS_SIZE 5
665 +typedef struct _CLzmaEncProps
667 + int level; /* 0 <= level <= 9 */
668 + UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
669 + (1 << 12) <= dictSize <= (1 << 30) for 64-bit version
670 + default = (1 << 24) */
671 + int lc; /* 0 <= lc <= 8, default = 3 */
672 + int lp; /* 0 <= lp <= 4, default = 0 */
673 + int pb; /* 0 <= pb <= 4, default = 2 */
674 + int algo; /* 0 - fast, 1 - normal, default = 1 */
675 + int fb; /* 5 <= fb <= 273, default = 32 */
676 + int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
677 + int numHashBytes; /* 2, 3 or 4, default = 4 */
678 + UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */
679 + unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
680 + int numThreads; /* 1 or 2, default = 2 */
683 +void LzmaEncProps_Init(CLzmaEncProps *p);
685 +/* ---------- CLzmaEncHandle Interface ---------- */
687 +/* LzmaEnc_* functions can return the following exit codes:
690 + SZ_ERROR_MEM - Memory allocation error
691 + SZ_ERROR_PARAM - Incorrect paramater in props
692 + SZ_ERROR_WRITE - Write callback error.
693 + SZ_ERROR_PROGRESS - some break from progress callback
694 + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
697 +typedef void * CLzmaEncHandle;
699 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc);
700 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig);
701 +SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props);
702 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size);
703 +SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
704 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
712 +++ b/include/linux/lzma/Types.h
714 +/* Types.h -- Basic types
715 +2009-11-23 : Igor Pavlov : Public domain */
717 +#ifndef __7Z_TYPES_H
718 +#define __7Z_TYPES_H
723 +#include <windows.h>
726 +#ifndef EXTERN_C_BEGIN
728 +#define EXTERN_C_BEGIN extern "C" {
729 +#define EXTERN_C_END }
731 +#define EXTERN_C_BEGIN
732 +#define EXTERN_C_END
740 +#define SZ_ERROR_DATA 1
741 +#define SZ_ERROR_MEM 2
742 +#define SZ_ERROR_CRC 3
743 +#define SZ_ERROR_UNSUPPORTED 4
744 +#define SZ_ERROR_PARAM 5
745 +#define SZ_ERROR_INPUT_EOF 6
746 +#define SZ_ERROR_OUTPUT_EOF 7
747 +#define SZ_ERROR_READ 8
748 +#define SZ_ERROR_WRITE 9
749 +#define SZ_ERROR_PROGRESS 10
750 +#define SZ_ERROR_FAIL 11
751 +#define SZ_ERROR_THREAD 12
753 +#define SZ_ERROR_ARCHIVE 16
754 +#define SZ_ERROR_NO_ARCHIVE 17
765 +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
768 +typedef unsigned char Byte;
769 +typedef short Int16;
770 +typedef unsigned short UInt16;
772 +#ifdef _LZMA_UINT32_IS_ULONG
774 +typedef unsigned long UInt32;
777 +typedef unsigned int UInt32;
780 +#ifdef _SZ_NO_INT_64
782 +/* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.
783 + NOTES: Some code will work incorrectly in that case! */
786 +typedef unsigned long UInt64;
790 +#if defined(_MSC_VER) || defined(__BORLANDC__)
791 +typedef __int64 Int64;
792 +typedef unsigned __int64 UInt64;
794 +typedef long long int Int64;
795 +typedef unsigned long long int UInt64;
800 +#ifdef _LZMA_NO_SYSTEM_SIZE_T
801 +typedef UInt32 SizeT;
803 +typedef size_t SizeT;
812 +#define MY_STD_CALL __stdcall
819 +#if _MSC_VER >= 1300
820 +#define MY_NO_INLINE __declspec(noinline)
822 +#define MY_NO_INLINE
825 +#define MY_CDECL __cdecl
826 +#define MY_FAST_CALL __fastcall
831 +#define MY_FAST_CALL
836 +/* The following interfaces use first parameter as pointer to structure */
840 + SRes (*Read)(void *p, void *buf, size_t *size);
841 + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
842 + (output(*size) < input(*size)) is allowed */
845 +/* it can return SZ_ERROR_INPUT_EOF */
846 +SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size);
847 +SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType);
848 +SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf);
852 + size_t (*Write)(void *p, const void *buf, size_t size);
853 + /* Returns: result - the number of actually written bytes.
854 + (result < size) means error */
866 + SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */
867 + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
872 + SRes (*Look)(void *p, void **buf, size_t *size);
873 + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
874 + (output(*size) > input(*size)) is not allowed
875 + (output(*size) < input(*size)) is allowed */
876 + SRes (*Skip)(void *p, size_t offset);
877 + /* offset must be <= output(*size) of Look */
879 + SRes (*Read)(void *p, void *buf, size_t *size);
880 + /* reads directly (without buffer). It's same as ISeqInStream::Read */
881 + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
884 +SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size);
885 +SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset);
887 +/* reads via ILookInStream::Read */
888 +SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType);
889 +SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size);
891 +#define LookToRead_BUF_SIZE (1 << 14)
896 + ISeekInStream *realStream;
899 + Byte buf[LookToRead_BUF_SIZE];
902 +void LookToRead_CreateVTable(CLookToRead *p, int lookahead);
903 +void LookToRead_Init(CLookToRead *p);
908 + ILookInStream *realStream;
911 +void SecToLook_CreateVTable(CSecToLook *p);
916 + ILookInStream *realStream;
919 +void SecToRead_CreateVTable(CSecToRead *p);
923 + SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize);
924 + /* Returns: result. (result != SZ_OK) means break.
925 + Value (UInt64)(Int64)-1 for size means unknown value. */
926 +} ICompressProgress;
930 + void *(*Alloc)(void *p, size_t size);
931 + void (*Free)(void *p, void *address); /* address can be 0 */
934 +#define IAlloc_Alloc(p, size) (p)->Alloc((p), size)
935 +#define IAlloc_Free(p, a) (p)->Free((p), a)
940 --- a/include/uapi/linux/jffs2.h
941 +++ b/include/uapi/linux/jffs2.h
943 #define JFFS2_COMPR_DYNRUBIN 0x05
944 #define JFFS2_COMPR_ZLIB 0x06
945 #define JFFS2_COMPR_LZO 0x07
946 +#define JFFS2_COMPR_LZMA 0x08
947 /* Compatibility flags. */
948 #define JFFS2_COMPAT_MASK 0xc000 /* What do to if an unknown nodetype is found */
949 #define JFFS2_NODE_ACCURATE 0x2000
952 @@ -340,6 +340,12 @@ config ZSTD_DECOMPRESS
954 source "lib/xz/Kconfig"
956 +config LZMA_COMPRESS
959 +config LZMA_DECOMPRESS
963 # These all provide a common interface (hence the apparent duplication with
964 # ZLIB_INFLATE; DECOMPRESS_GZIP is just a wrapper.)
967 @@ -135,6 +135,16 @@ CFLAGS_kobject.o += -DDEBUG
968 CFLAGS_kobject_uevent.o += -DDEBUG
971 +ifdef CONFIG_JFFS2_ZLIB
972 + CONFIG_ZLIB_INFLATE:=y
973 + CONFIG_ZLIB_DEFLATE:=y
976 +ifdef CONFIG_JFFS2_LZMA
977 + CONFIG_LZMA_DECOMPRESS:=y
978 + CONFIG_LZMA_COMPRESS:=y
981 obj-$(CONFIG_DEBUG_INFO_REDUCED) += debug_info.o
982 CFLAGS_debug_info.o += $(call cc-option, -femit-struct-debug-detailed=any)
984 @@ -192,6 +202,8 @@ obj-$(CONFIG_ZSTD_COMPRESS) += zstd/
985 obj-$(CONFIG_ZSTD_DECOMPRESS) += zstd/
986 obj-$(CONFIG_XZ_DEC) += xz/
987 obj-$(CONFIG_RAID6_PQ) += raid6/
988 +obj-$(CONFIG_LZMA_COMPRESS) += lzma/
989 +obj-$(CONFIG_LZMA_DECOMPRESS) += lzma/
991 lib-$(CONFIG_DECOMPRESS_GZIP) += decompress_inflate.o
992 lib-$(CONFIG_DECOMPRESS_BZIP2) += decompress_bunzip2.o
994 +++ b/lib/lzma/LzFind.c
996 +/* LzFind.c -- Match finder for LZ algorithms
997 +2009-04-22 : Igor Pavlov : Public domain */
1001 +#include "LzFind.h"
1002 +#include "LzHash.h"
1004 +#define kEmptyHashValue 0
1005 +#define kMaxValForNormalize ((UInt32)0xFFFFFFFF)
1006 +#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */
1007 +#define kNormalizeMask (~(kNormalizeStepMin - 1))
1008 +#define kMaxHistorySize ((UInt32)3 << 30)
1010 +#define kStartMaxLen 3
1013 +#define DIRECT_INPUT p->directInput
1015 +#define DIRECT_INPUT 1
1018 +static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
1020 + if (!DIRECT_INPUT)
1022 + alloc->Free(alloc, p->bufferBase);
1023 + p->bufferBase = 0;
1027 +/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
1029 +static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
1031 + UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
1034 + p->blockSize = blockSize;
1037 + if (p->bufferBase == 0 || p->blockSize != blockSize)
1039 + LzInWindow_Free(p, alloc);
1040 + p->blockSize = blockSize;
1041 + p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
1043 + return (p->bufferBase != 0);
1046 +static Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
1047 +static Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
1049 +static UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
1051 +static void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
1053 + p->posLimit -= subValue;
1054 + p->pos -= subValue;
1055 + p->streamPos -= subValue;
1058 +static void MatchFinder_ReadBlock(CMatchFinder *p)
1060 + if (p->streamEndWasReached || p->result != SZ_OK)
1064 + UInt32 curSize = 0xFFFFFFFF - p->streamPos;
1065 + if (curSize > p->directInputRem)
1066 + curSize = (UInt32)p->directInputRem;
1067 + p->directInputRem -= curSize;
1068 + p->streamPos += curSize;
1069 + if (p->directInputRem == 0)
1070 + p->streamEndWasReached = 1;
1075 + Byte *dest = p->buffer + (p->streamPos - p->pos);
1076 + size_t size = (p->bufferBase + p->blockSize - dest);
1079 + p->result = p->stream->Read(p->stream, dest, &size);
1080 + if (p->result != SZ_OK)
1084 + p->streamEndWasReached = 1;
1087 + p->streamPos += (UInt32)size;
1088 + if (p->streamPos - p->pos > p->keepSizeAfter)
1093 +static void MatchFinder_MoveBlock(CMatchFinder *p)
1095 + memmove(p->bufferBase,
1096 + p->buffer - p->keepSizeBefore,
1097 + (size_t)(p->streamPos - p->pos + p->keepSizeBefore));
1098 + p->buffer = p->bufferBase + p->keepSizeBefore;
1101 +static int MatchFinder_NeedMove(CMatchFinder *p)
1105 + /* if (p->streamEndWasReached) return 0; */
1106 + return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
1109 +static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
1111 + if (MatchFinder_NeedMove(p))
1112 + MatchFinder_MoveBlock(p);
1113 + MatchFinder_ReadBlock(p);
1116 +static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
1120 + p->numHashBytes = 4;
1124 +#define kCrcPoly 0xEDB88320
1126 +void MatchFinder_Construct(CMatchFinder *p)
1129 + p->bufferBase = 0;
1130 + p->directInput = 0;
1132 + MatchFinder_SetDefaultSettings(p);
1134 + for (i = 0; i < 256; i++)
1138 + for (j = 0; j < 8; j++)
1139 + r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
1144 +static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
1146 + alloc->Free(alloc, p->hash);
1150 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
1152 + MatchFinder_FreeThisClassMemory(p, alloc);
1153 + LzInWindow_Free(p, alloc);
1156 +static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
1158 + size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
1159 + if (sizeInBytes / sizeof(CLzRef) != num)
1161 + return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
1164 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
1165 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
1168 + UInt32 sizeReserv;
1169 + if (historySize > kMaxHistorySize)
1171 + MatchFinder_Free(p, alloc);
1174 + sizeReserv = historySize >> 1;
1175 + if (historySize > ((UInt32)2 << 30))
1176 + sizeReserv = historySize >> 2;
1177 + sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
1179 + p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
1180 + p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
1181 + /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
1182 + if (LzInWindow_Create(p, sizeReserv, alloc))
1184 + UInt32 newCyclicBufferSize = historySize + 1;
1186 + p->matchMaxLen = matchMaxLen;
1188 + p->fixedHashSize = 0;
1189 + if (p->numHashBytes == 2)
1190 + hs = (1 << 16) - 1;
1193 + hs = historySize - 1;
1199 + hs |= 0xFFFF; /* don't change it! It's required for Deflate */
1200 + if (hs > (1 << 24))
1202 + if (p->numHashBytes == 3)
1203 + hs = (1 << 24) - 1;
1210 + if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
1211 + if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
1212 + if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
1213 + hs += p->fixedHashSize;
1217 + UInt32 prevSize = p->hashSizeSum + p->numSons;
1219 + p->historySize = historySize;
1220 + p->hashSizeSum = hs;
1221 + p->cyclicBufferSize = newCyclicBufferSize;
1222 + p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);
1223 + newSize = p->hashSizeSum + p->numSons;
1224 + if (p->hash != 0 && prevSize == newSize)
1226 + MatchFinder_FreeThisClassMemory(p, alloc);
1227 + p->hash = AllocRefs(newSize, alloc);
1230 + p->son = p->hash + p->hashSizeSum;
1235 + MatchFinder_Free(p, alloc);
1239 +static void MatchFinder_SetLimits(CMatchFinder *p)
1241 + UInt32 limit = kMaxValForNormalize - p->pos;
1242 + UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
1243 + if (limit2 < limit)
1245 + limit2 = p->streamPos - p->pos;
1246 + if (limit2 <= p->keepSizeAfter)
1252 + limit2 -= p->keepSizeAfter;
1253 + if (limit2 < limit)
1256 + UInt32 lenLimit = p->streamPos - p->pos;
1257 + if (lenLimit > p->matchMaxLen)
1258 + lenLimit = p->matchMaxLen;
1259 + p->lenLimit = lenLimit;
1261 + p->posLimit = p->pos + limit;
1264 +static void MatchFinder_Init(CMatchFinder *p)
1267 + for (i = 0; i < p->hashSizeSum; i++)
1268 + p->hash[i] = kEmptyHashValue;
1269 + p->cyclicBufferPos = 0;
1270 + p->buffer = p->bufferBase;
1271 + p->pos = p->streamPos = p->cyclicBufferSize;
1272 + p->result = SZ_OK;
1273 + p->streamEndWasReached = 0;
1274 + MatchFinder_ReadBlock(p);
1275 + MatchFinder_SetLimits(p);
1278 +static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
1280 + return (p->pos - p->historySize - 1) & kNormalizeMask;
1283 +static void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
1286 + for (i = 0; i < numItems; i++)
1288 + UInt32 value = items[i];
1289 + if (value <= subValue)
1290 + value = kEmptyHashValue;
1292 + value -= subValue;
1297 +static void MatchFinder_Normalize(CMatchFinder *p)
1299 + UInt32 subValue = MatchFinder_GetSubValue(p);
1300 + MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
1301 + MatchFinder_ReduceOffsets(p, subValue);
1304 +static void MatchFinder_CheckLimits(CMatchFinder *p)
1306 + if (p->pos == kMaxValForNormalize)
1307 + MatchFinder_Normalize(p);
1308 + if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
1309 + MatchFinder_CheckAndMoveAndRead(p);
1310 + if (p->cyclicBufferPos == p->cyclicBufferSize)
1311 + p->cyclicBufferPos = 0;
1312 + MatchFinder_SetLimits(p);
1315 +static UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1316 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
1317 + UInt32 *distances, UInt32 maxLen)
1319 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1320 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1321 + UInt32 len0 = 0, len1 = 0;
1324 + UInt32 delta = pos - curMatch;
1325 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1327 + *ptr0 = *ptr1 = kEmptyHashValue;
1331 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1332 + const Byte *pb = cur - delta;
1333 + UInt32 len = (len0 < len1 ? len0 : len1);
1334 + if (pb[len] == cur[len])
1336 + if (++len != lenLimit && pb[len] == cur[len])
1337 + while (++len != lenLimit)
1338 + if (pb[len] != cur[len])
1342 + *distances++ = maxLen = len;
1343 + *distances++ = delta - 1;
1344 + if (len == lenLimit)
1352 + if (pb[len] < cur[len])
1370 +static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1371 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
1373 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1374 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1375 + UInt32 len0 = 0, len1 = 0;
1378 + UInt32 delta = pos - curMatch;
1379 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1381 + *ptr0 = *ptr1 = kEmptyHashValue;
1385 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1386 + const Byte *pb = cur - delta;
1387 + UInt32 len = (len0 < len1 ? len0 : len1);
1388 + if (pb[len] == cur[len])
1390 + while (++len != lenLimit)
1391 + if (pb[len] != cur[len])
1394 + if (len == lenLimit)
1402 + if (pb[len] < cur[len])
1421 + ++p->cyclicBufferPos; \
1423 + if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
1425 +static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
1427 +#define MOVE_POS_RET MatchFinder_MovePos(p); return offset;
1429 +#define GET_MATCHES_HEADER2(minLen, ret_op) \
1430 + UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \
1431 + lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
1434 +#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
1435 +#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
1437 +#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
1439 +#define GET_MATCHES_FOOTER(offset, maxLen) \
1440 + offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
1441 + distances + offset, maxLen) - distances); MOVE_POS_RET;
1443 +#define SKIP_FOOTER \
1444 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MatchFinder_MovePos(p);
1446 +static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1448 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1449 + GET_MATCHES_HEADER(4)
1453 + delta2 = p->pos - p->hash[ hash2Value];
1454 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1455 + curMatch = p->hash[kFix4HashSize + hashValue];
1457 + p->hash[ hash2Value] =
1458 + p->hash[kFix3HashSize + hash3Value] =
1459 + p->hash[kFix4HashSize + hashValue] = p->pos;
1463 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1465 + distances[0] = maxLen = 2;
1466 + distances[1] = delta2 - 1;
1469 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1472 + distances[offset + 1] = delta3 - 1;
1478 + for (; maxLen != lenLimit; maxLen++)
1479 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1481 + distances[offset - 2] = maxLen;
1482 + if (maxLen == lenLimit)
1484 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1490 + GET_MATCHES_FOOTER(offset, maxLen)
1493 +static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1497 + UInt32 hash2Value, hash3Value;
1500 + curMatch = p->hash[kFix4HashSize + hashValue];
1501 + p->hash[ hash2Value] =
1502 + p->hash[kFix3HashSize + hash3Value] = p->pos;
1503 + p->hash[kFix4HashSize + hashValue] = p->pos;
1506 + while (--num != 0);
1509 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
1511 + vTable->Init = (Mf_Init_Func)MatchFinder_Init;
1512 + vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;
1513 + vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
1514 + vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
1515 + vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
1516 + vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
1519 +++ b/lib/lzma/LzmaDec.c
1521 +/* LzmaDec.c -- LZMA Decoder
1522 +2009-09-20 : Igor Pavlov : Public domain */
1524 +#include "LzmaDec.h"
1526 +#include <string.h>
1528 +#define kNumTopBits 24
1529 +#define kTopValue ((UInt32)1 << kNumTopBits)
1531 +#define kNumBitModelTotalBits 11
1532 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
1533 +#define kNumMoveBits 5
1535 +#define RC_INIT_SIZE 5
1537 +#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
1539 +#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1540 +#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
1541 +#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
1542 +#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
1543 + { UPDATE_0(p); i = (i + i); A0; } else \
1544 + { UPDATE_1(p); i = (i + i) + 1; A1; }
1545 +#define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
1547 +#define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
1548 +#define TREE_DECODE(probs, limit, i) \
1549 + { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
1551 +/* #define _LZMA_SIZE_OPT */
1553 +#ifdef _LZMA_SIZE_OPT
1554 +#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
1556 +#define TREE_6_DECODE(probs, i) \
1558 + TREE_GET_BIT(probs, i); \
1559 + TREE_GET_BIT(probs, i); \
1560 + TREE_GET_BIT(probs, i); \
1561 + TREE_GET_BIT(probs, i); \
1562 + TREE_GET_BIT(probs, i); \
1563 + TREE_GET_BIT(probs, i); \
1567 +#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
1569 +#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1570 +#define UPDATE_0_CHECK range = bound;
1571 +#define UPDATE_1_CHECK range -= bound; code -= bound;
1572 +#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
1573 + { UPDATE_0_CHECK; i = (i + i); A0; } else \
1574 + { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
1575 +#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
1576 +#define TREE_DECODE_CHECK(probs, limit, i) \
1577 + { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
1580 +#define kNumPosBitsMax 4
1581 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
1583 +#define kLenNumLowBits 3
1584 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
1585 +#define kLenNumMidBits 3
1586 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
1587 +#define kLenNumHighBits 8
1588 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
1590 +#define LenChoice 0
1591 +#define LenChoice2 (LenChoice + 1)
1592 +#define LenLow (LenChoice2 + 1)
1593 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
1594 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
1595 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
1598 +#define kNumStates 12
1599 +#define kNumLitStates 7
1601 +#define kStartPosModelIndex 4
1602 +#define kEndPosModelIndex 14
1603 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
1605 +#define kNumPosSlotBits 6
1606 +#define kNumLenToPosStates 4
1608 +#define kNumAlignBits 4
1609 +#define kAlignTableSize (1 << kNumAlignBits)
1611 +#define kMatchMinLen 2
1612 +#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
1615 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
1616 +#define IsRepG0 (IsRep + kNumStates)
1617 +#define IsRepG1 (IsRepG0 + kNumStates)
1618 +#define IsRepG2 (IsRepG1 + kNumStates)
1619 +#define IsRep0Long (IsRepG2 + kNumStates)
1620 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
1621 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
1622 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
1623 +#define LenCoder (Align + kAlignTableSize)
1624 +#define RepLenCoder (LenCoder + kNumLenProbs)
1625 +#define Literal (RepLenCoder + kNumLenProbs)
1627 +#define LZMA_BASE_SIZE 1846
1628 +#define LZMA_LIT_SIZE 768
1630 +#define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
1632 +#if Literal != LZMA_BASE_SIZE
1633 +StopCompilingDueBUG
1636 +#define LZMA_DIC_MIN (1 << 12)
1638 +/* First LZMA-symbol is always decoded.
1639 +And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
1643 + SZ_ERROR_DATA - Error
1645 + < kMatchSpecLenStart : normal remain
1646 + = kMatchSpecLenStart : finished
1647 + = kMatchSpecLenStart + 1 : Flush marker
1648 + = kMatchSpecLenStart + 2 : State Init Marker
1651 +static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
1653 + CLzmaProb *probs = p->probs;
1655 + unsigned state = p->state;
1656 + UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
1657 + unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
1658 + unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
1659 + unsigned lc = p->prop.lc;
1661 + Byte *dic = p->dic;
1662 + SizeT dicBufSize = p->dicBufSize;
1663 + SizeT dicPos = p->dicPos;
1665 + UInt32 processedPos = p->processedPos;
1666 + UInt32 checkDicSize = p->checkDicSize;
1669 + const Byte *buf = p->buf;
1670 + UInt32 range = p->range;
1671 + UInt32 code = p->code;
1678 + unsigned posState = processedPos & pbMask;
1680 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
1685 + prob = probs + Literal;
1686 + if (checkDicSize != 0 || processedPos != 0)
1687 + prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
1688 + (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
1690 + if (state < kNumLitStates)
1692 + state -= (state < 4) ? state : 3;
1694 + do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
1698 + unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1699 + unsigned offs = 0x100;
1700 + state -= (state < 10) ? 3 : 6;
1705 + CLzmaProb *probLit;
1707 + bit = (matchByte & offs);
1708 + probLit = prob + offs + bit + symbol;
1709 + GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
1711 + while (symbol < 0x100);
1713 + dic[dicPos++] = (Byte)symbol;
1720 + prob = probs + IsRep + state;
1724 + state += kNumStates;
1725 + prob = probs + LenCoder;
1730 + if (checkDicSize == 0 && processedPos == 0)
1731 + return SZ_ERROR_DATA;
1732 + prob = probs + IsRepG0 + state;
1736 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
1740 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1743 + state = state < kNumLitStates ? 9 : 11;
1752 + prob = probs + IsRepG1 + state;
1761 + prob = probs + IsRepG2 + state;
1778 + state = state < kNumLitStates ? 8 : 11;
1779 + prob = probs + RepLenCoder;
1782 + unsigned limit, offset;
1783 + CLzmaProb *probLen = prob + LenChoice;
1786 + UPDATE_0(probLen);
1787 + probLen = prob + LenLow + (posState << kLenNumLowBits);
1789 + limit = (1 << kLenNumLowBits);
1793 + UPDATE_1(probLen);
1794 + probLen = prob + LenChoice2;
1797 + UPDATE_0(probLen);
1798 + probLen = prob + LenMid + (posState << kLenNumMidBits);
1799 + offset = kLenNumLowSymbols;
1800 + limit = (1 << kLenNumMidBits);
1804 + UPDATE_1(probLen);
1805 + probLen = prob + LenHigh;
1806 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
1807 + limit = (1 << kLenNumHighBits);
1810 + TREE_DECODE(probLen, limit, len);
1814 + if (state >= kNumStates)
1817 + prob = probs + PosSlot +
1818 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
1819 + TREE_6_DECODE(prob, distance);
1820 + if (distance >= kStartPosModelIndex)
1822 + unsigned posSlot = (unsigned)distance;
1823 + int numDirectBits = (int)(((distance >> 1) - 1));
1824 + distance = (2 | (distance & 1));
1825 + if (posSlot < kEndPosModelIndex)
1827 + distance <<= numDirectBits;
1828 + prob = probs + SpecPos + distance - posSlot - 1;
1834 + GET_BIT2(prob + i, i, ; , distance |= mask);
1837 + while (--numDirectBits != 0);
1842 + numDirectBits -= kNumAlignBits;
1851 + t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
1852 + distance = (distance << 1) + (t + 1);
1853 + code += range & t;
1857 + if (code >= range)
1864 + while (--numDirectBits != 0);
1865 + prob = probs + Align;
1866 + distance <<= kNumAlignBits;
1869 + GET_BIT2(prob + i, i, ; , distance |= 1);
1870 + GET_BIT2(prob + i, i, ; , distance |= 2);
1871 + GET_BIT2(prob + i, i, ; , distance |= 4);
1872 + GET_BIT2(prob + i, i, ; , distance |= 8);
1874 + if (distance == (UInt32)0xFFFFFFFF)
1876 + len += kMatchSpecLenStart;
1877 + state -= kNumStates;
1885 + rep0 = distance + 1;
1886 + if (checkDicSize == 0)
1888 + if (distance >= processedPos)
1889 + return SZ_ERROR_DATA;
1891 + else if (distance >= checkDicSize)
1892 + return SZ_ERROR_DATA;
1893 + state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
1896 + len += kMatchMinLen;
1898 + if (limit == dicPos)
1899 + return SZ_ERROR_DATA;
1901 + SizeT rem = limit - dicPos;
1902 + unsigned curLen = ((rem < len) ? (unsigned)rem : len);
1903 + SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
1905 + processedPos += curLen;
1908 + if (pos + curLen <= dicBufSize)
1910 + Byte *dest = dic + dicPos;
1911 + ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
1912 + const Byte *lim = dest + curLen;
1915 + *(dest) = (Byte)*(dest + src);
1916 + while (++dest != lim);
1922 + dic[dicPos++] = dic[pos];
1923 + if (++pos == dicBufSize)
1926 + while (--curLen != 0);
1931 + while (dicPos < limit && buf < bufLimit);
1936 + p->remainLen = len;
1937 + p->dicPos = dicPos;
1938 + p->processedPos = processedPos;
1939 + p->reps[0] = rep0;
1940 + p->reps[1] = rep1;
1941 + p->reps[2] = rep2;
1942 + p->reps[3] = rep3;
1948 +static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
1950 + if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
1952 + Byte *dic = p->dic;
1953 + SizeT dicPos = p->dicPos;
1954 + SizeT dicBufSize = p->dicBufSize;
1955 + unsigned len = p->remainLen;
1956 + UInt32 rep0 = p->reps[0];
1957 + if (limit - dicPos < len)
1958 + len = (unsigned)(limit - dicPos);
1960 + if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
1961 + p->checkDicSize = p->prop.dicSize;
1963 + p->processedPos += len;
1964 + p->remainLen -= len;
1965 + while (len-- != 0)
1967 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1970 + p->dicPos = dicPos;
1974 +static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
1978 + SizeT limit2 = limit;
1979 + if (p->checkDicSize == 0)
1981 + UInt32 rem = p->prop.dicSize - p->processedPos;
1982 + if (limit - p->dicPos > rem)
1983 + limit2 = p->dicPos + rem;
1985 + RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
1986 + if (p->processedPos >= p->prop.dicSize)
1987 + p->checkDicSize = p->prop.dicSize;
1988 + LzmaDec_WriteRem(p, limit);
1990 + while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
1992 + if (p->remainLen > kMatchSpecLenStart)
1994 + p->remainLen = kMatchSpecLenStart;
2001 + DUMMY_ERROR, /* unexpected end of input stream */
2007 +static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
2009 + UInt32 range = p->range;
2010 + UInt32 code = p->code;
2011 + const Byte *bufLimit = buf + inSize;
2012 + CLzmaProb *probs = p->probs;
2013 + unsigned state = p->state;
2020 + unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
2022 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
2023 + IF_BIT_0_CHECK(prob)
2027 + /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
2029 + prob = probs + Literal;
2030 + if (p->checkDicSize != 0 || p->processedPos != 0)
2031 + prob += (LZMA_LIT_SIZE *
2032 + ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
2033 + (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
2035 + if (state < kNumLitStates)
2037 + unsigned symbol = 1;
2038 + do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
2042 + unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
2043 + ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
2044 + unsigned offs = 0x100;
2045 + unsigned symbol = 1;
2049 + CLzmaProb *probLit;
2051 + bit = (matchByte & offs);
2052 + probLit = prob + offs + bit + symbol;
2053 + GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
2055 + while (symbol < 0x100);
2064 + prob = probs + IsRep + state;
2065 + IF_BIT_0_CHECK(prob)
2069 + prob = probs + LenCoder;
2070 + res = DUMMY_MATCH;
2076 + prob = probs + IsRepG0 + state;
2077 + IF_BIT_0_CHECK(prob)
2080 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
2081 + IF_BIT_0_CHECK(prob)
2095 + prob = probs + IsRepG1 + state;
2096 + IF_BIT_0_CHECK(prob)
2103 + prob = probs + IsRepG2 + state;
2104 + IF_BIT_0_CHECK(prob)
2114 + state = kNumStates;
2115 + prob = probs + RepLenCoder;
2118 + unsigned limit, offset;
2119 + CLzmaProb *probLen = prob + LenChoice;
2120 + IF_BIT_0_CHECK(probLen)
2123 + probLen = prob + LenLow + (posState << kLenNumLowBits);
2125 + limit = 1 << kLenNumLowBits;
2130 + probLen = prob + LenChoice2;
2131 + IF_BIT_0_CHECK(probLen)
2134 + probLen = prob + LenMid + (posState << kLenNumMidBits);
2135 + offset = kLenNumLowSymbols;
2136 + limit = 1 << kLenNumMidBits;
2141 + probLen = prob + LenHigh;
2142 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
2143 + limit = 1 << kLenNumHighBits;
2146 + TREE_DECODE_CHECK(probLen, limit, len);
2153 + prob = probs + PosSlot +
2154 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
2156 + TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
2157 + if (posSlot >= kStartPosModelIndex)
2159 + int numDirectBits = ((posSlot >> 1) - 1);
2161 + /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
2163 + if (posSlot < kEndPosModelIndex)
2165 + prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
2169 + numDirectBits -= kNumAlignBits;
2174 + code -= range & (((code - range) >> 31) - 1);
2175 + /* if (code >= range) code -= range; */
2177 + while (--numDirectBits != 0);
2178 + prob = probs + Align;
2179 + numDirectBits = kNumAlignBits;
2185 + GET_BIT_CHECK(prob + i, i);
2187 + while (--numDirectBits != 0);
2198 +static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
2200 + p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
2201 + p->range = 0xFFFFFFFF;
2205 +static void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
2209 + p->tempBufSize = 0;
2213 + p->processedPos = 0;
2214 + p->checkDicSize = 0;
2215 + p->needInitState = 1;
2218 + p->needInitState = 1;
2221 +static void LzmaDec_Init(CLzmaDec *p)
2224 + LzmaDec_InitDicAndState(p, True, True);
2227 +static void LzmaDec_InitStateReal(CLzmaDec *p)
2229 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
2231 + CLzmaProb *probs = p->probs;
2232 + for (i = 0; i < numProbs; i++)
2233 + probs[i] = kBitModelTotal >> 1;
2234 + p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
2236 + p->needInitState = 0;
2239 +static SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
2240 + ELzmaFinishMode finishMode, ELzmaStatus *status)
2242 + SizeT inSize = *srcLen;
2244 + LzmaDec_WriteRem(p, dicLimit);
2246 + *status = LZMA_STATUS_NOT_SPECIFIED;
2248 + while (p->remainLen != kMatchSpecLenStart)
2250 + int checkEndMarkNow;
2252 + if (p->needFlush != 0)
2254 + for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
2255 + p->tempBuf[p->tempBufSize++] = *src++;
2256 + if (p->tempBufSize < RC_INIT_SIZE)
2258 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2261 + if (p->tempBuf[0] != 0)
2262 + return SZ_ERROR_DATA;
2264 + LzmaDec_InitRc(p, p->tempBuf);
2265 + p->tempBufSize = 0;
2268 + checkEndMarkNow = 0;
2269 + if (p->dicPos >= dicLimit)
2271 + if (p->remainLen == 0 && p->code == 0)
2273 + *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
2276 + if (finishMode == LZMA_FINISH_ANY)
2278 + *status = LZMA_STATUS_NOT_FINISHED;
2281 + if (p->remainLen != 0)
2283 + *status = LZMA_STATUS_NOT_FINISHED;
2284 + return SZ_ERROR_DATA;
2286 + checkEndMarkNow = 1;
2289 + if (p->needInitState)
2290 + LzmaDec_InitStateReal(p);
2292 + if (p->tempBufSize == 0)
2295 + const Byte *bufLimit;
2296 + if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2298 + int dummyRes = LzmaDec_TryDummy(p, src, inSize);
2299 + if (dummyRes == DUMMY_ERROR)
2301 + memcpy(p->tempBuf, src, inSize);
2302 + p->tempBufSize = (unsigned)inSize;
2303 + (*srcLen) += inSize;
2304 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2307 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2309 + *status = LZMA_STATUS_NOT_FINISHED;
2310 + return SZ_ERROR_DATA;
2315 + bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
2317 + if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
2318 + return SZ_ERROR_DATA;
2319 + processed = (SizeT)(p->buf - src);
2320 + (*srcLen) += processed;
2322 + inSize -= processed;
2326 + unsigned rem = p->tempBufSize, lookAhead = 0;
2327 + while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
2328 + p->tempBuf[rem++] = src[lookAhead++];
2329 + p->tempBufSize = rem;
2330 + if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2332 + int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
2333 + if (dummyRes == DUMMY_ERROR)
2335 + (*srcLen) += lookAhead;
2336 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2339 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2341 + *status = LZMA_STATUS_NOT_FINISHED;
2342 + return SZ_ERROR_DATA;
2345 + p->buf = p->tempBuf;
2346 + if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
2347 + return SZ_ERROR_DATA;
2348 + lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
2349 + (*srcLen) += lookAhead;
2351 + inSize -= lookAhead;
2352 + p->tempBufSize = 0;
2356 + *status = LZMA_STATUS_FINISHED_WITH_MARK;
2357 + return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
2360 +static void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
2362 + alloc->Free(alloc, p->probs);
2366 +static SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
2371 + if (size < LZMA_PROPS_SIZE)
2372 + return SZ_ERROR_UNSUPPORTED;
2374 + dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
2376 + if (dicSize < LZMA_DIC_MIN)
2377 + dicSize = LZMA_DIC_MIN;
2378 + p->dicSize = dicSize;
2381 + if (d >= (9 * 5 * 5))
2382 + return SZ_ERROR_UNSUPPORTED;
2392 +static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
2394 + UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
2395 + if (p->probs == 0 || numProbs != p->numProbs)
2397 + LzmaDec_FreeProbs(p, alloc);
2398 + p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
2399 + p->numProbs = numProbs;
2400 + if (p->probs == 0)
2401 + return SZ_ERROR_MEM;
2406 +static SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2408 + CLzmaProps propNew;
2409 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2410 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2411 + p->prop = propNew;
2415 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
2416 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
2417 + ELzmaStatus *status, ISzAlloc *alloc)
2421 + SizeT inSize = *srcLen;
2422 + SizeT outSize = *destLen;
2423 + *srcLen = *destLen = 0;
2424 + if (inSize < RC_INIT_SIZE)
2425 + return SZ_ERROR_INPUT_EOF;
2427 + LzmaDec_Construct(&p);
2428 + res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
2432 + p.dicBufSize = outSize;
2437 + res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
2439 + if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
2440 + res = SZ_ERROR_INPUT_EOF;
2442 + (*destLen) = p.dicPos;
2443 + LzmaDec_FreeProbs(&p, alloc);
2447 +++ b/lib/lzma/LzmaEnc.c
2449 +/* LzmaEnc.c -- LZMA Encoder
2450 +2009-11-24 : Igor Pavlov : Public domain */
2452 +#include <string.h>
2454 +/* #define SHOW_STAT */
2455 +/* #define SHOW_STAT2 */
2457 +#if defined(SHOW_STAT) || defined(SHOW_STAT2)
2461 +#include "LzmaEnc.h"
2466 +#include "LzFind.h"
2468 +#include "LzFindMt.h"
2472 +static int ttt = 0;
2475 +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
2477 +#define kBlockSize (9 << 10)
2478 +#define kUnpackBlockSize (1 << 18)
2479 +#define kMatchArraySize (1 << 21)
2480 +#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
2482 +#define kNumMaxDirectBits (31)
2484 +#define kNumTopBits 24
2485 +#define kTopValue ((UInt32)1 << kNumTopBits)
2487 +#define kNumBitModelTotalBits 11
2488 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
2489 +#define kNumMoveBits 5
2490 +#define kProbInitValue (kBitModelTotal >> 1)
2492 +#define kNumMoveReducingBits 4
2493 +#define kNumBitPriceShiftBits 4
2494 +#define kBitPrice (1 << kNumBitPriceShiftBits)
2496 +void LzmaEncProps_Init(CLzmaEncProps *p)
2499 + p->dictSize = p->mc = 0;
2500 + p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
2501 + p->writeEndMark = 0;
2504 +static void LzmaEncProps_Normalize(CLzmaEncProps *p)
2506 + int level = p->level;
2507 + if (level < 0) level = 5;
2509 + if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
2510 + if (p->lc < 0) p->lc = 3;
2511 + if (p->lp < 0) p->lp = 0;
2512 + if (p->pb < 0) p->pb = 2;
2513 + if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
2514 + if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
2515 + if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
2516 + if (p->numHashBytes < 0) p->numHashBytes = 4;
2517 + if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
2518 + if (p->numThreads < 0)
2521 + ((p->btMode && p->algo) ? 2 : 1);
2527 +static UInt32 __maybe_unused LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
2529 + CLzmaEncProps props = *props2;
2530 + LzmaEncProps_Normalize(&props);
2531 + return props.dictSize;
2534 +/* #define LZMA_LOG_BSR */
2535 +/* Define it for Intel's CPU */
2538 +#ifdef LZMA_LOG_BSR
2540 +#define kDicLogSizeMaxCompress 30
2542 +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
2544 +static UInt32 GetPosSlot1(UInt32 pos)
2547 + BSR2_RET(pos, res);
2550 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
2551 +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
2555 +#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
2556 +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
2558 +static void LzmaEnc_FastPosInit(Byte *g_FastPos)
2560 + int c = 2, slotFast;
2564 + for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
2566 + UInt32 k = (1 << ((slotFast >> 1) - 1));
2568 + for (j = 0; j < k; j++, c++)
2569 + g_FastPos[c] = (Byte)slotFast;
2573 +#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
2574 + (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
2575 + res = p->g_FastPos[pos >> i] + (i * 2); }
2577 +#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
2578 + p->g_FastPos[pos >> 6] + 12 : \
2579 + p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
2582 +#define GetPosSlot1(pos) p->g_FastPos[pos]
2583 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
2584 +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
2589 +#define LZMA_NUM_REPS 4
2591 +typedef unsigned CState;
2606 + UInt32 backs[LZMA_NUM_REPS];
2609 +#define kNumOpts (1 << 12)
2611 +#define kNumLenToPosStates 4
2612 +#define kNumPosSlotBits 6
2613 +#define kDicLogSizeMin 0
2614 +#define kDicLogSizeMax 32
2615 +#define kDistTableSizeMax (kDicLogSizeMax * 2)
2618 +#define kNumAlignBits 4
2619 +#define kAlignTableSize (1 << kNumAlignBits)
2620 +#define kAlignMask (kAlignTableSize - 1)
2622 +#define kStartPosModelIndex 4
2623 +#define kEndPosModelIndex 14
2624 +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
2626 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
2628 +#ifdef _LZMA_PROB32
2629 +#define CLzmaProb UInt32
2631 +#define CLzmaProb UInt16
2634 +#define LZMA_PB_MAX 4
2635 +#define LZMA_LC_MAX 8
2636 +#define LZMA_LP_MAX 4
2638 +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
2641 +#define kLenNumLowBits 3
2642 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
2643 +#define kLenNumMidBits 3
2644 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
2645 +#define kLenNumHighBits 8
2646 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
2648 +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
2650 +#define LZMA_MATCH_LEN_MIN 2
2651 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
2653 +#define kNumStates 12
2658 + CLzmaProb choice2;
2659 + CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
2660 + CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
2661 + CLzmaProb high[kLenNumHighSymbols];
2667 + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
2669 + UInt32 counters[LZMA_NUM_PB_STATES_MAX];
2681 + ISeqOutStream *outStream;
2688 + CLzmaProb *litProbs;
2690 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
2691 + CLzmaProb isRep[kNumStates];
2692 + CLzmaProb isRepG0[kNumStates];
2693 + CLzmaProb isRepG1[kNumStates];
2694 + CLzmaProb isRepG2[kNumStates];
2695 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
2697 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
2698 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
2699 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
2701 + CLenPriceEnc lenEnc;
2702 + CLenPriceEnc repLenEnc;
2704 + UInt32 reps[LZMA_NUM_REPS];
2710 + IMatchFinder matchFinder;
2711 + void *matchFinderObj;
2715 + CMatchFinderMt matchFinderMt;
2718 + CMatchFinder matchFinderBase;
2724 + UInt32 optimumEndIndex;
2725 + UInt32 optimumCurrentIndex;
2727 + UInt32 longestMatchLength;
2730 + COptimal opt[kNumOpts];
2732 + #ifndef LZMA_LOG_BSR
2733 + Byte g_FastPos[1 << kNumLogBits];
2736 + UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
2737 + UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
2738 + UInt32 numFastBytes;
2739 + UInt32 additionalOffset;
2740 + UInt32 reps[LZMA_NUM_REPS];
2743 + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
2744 + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
2745 + UInt32 alignPrices[kAlignTableSize];
2746 + UInt32 alignPriceCount;
2748 + UInt32 distTableSize;
2750 + unsigned lc, lp, pb;
2751 + unsigned lpMask, pbMask;
2753 + CLzmaProb *litProbs;
2755 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
2756 + CLzmaProb isRep[kNumStates];
2757 + CLzmaProb isRepG0[kNumStates];
2758 + CLzmaProb isRepG1[kNumStates];
2759 + CLzmaProb isRepG2[kNumStates];
2760 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
2762 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
2763 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
2764 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
2766 + CLenPriceEnc lenEnc;
2767 + CLenPriceEnc repLenEnc;
2775 + Bool writeEndMark;
2777 + UInt32 matchPriceCount;
2783 + UInt32 matchFinderCycles;
2787 + CSaveState saveState;
2790 +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
2792 + CLzmaEnc *p = (CLzmaEnc *)pp;
2793 + CLzmaEncProps props = *props2;
2794 + LzmaEncProps_Normalize(&props);
2796 + if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
2797 + props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))
2798 + return SZ_ERROR_PARAM;
2799 + p->dictSize = props.dictSize;
2800 + p->matchFinderCycles = props.mc;
2802 + unsigned fb = props.fb;
2805 + if (fb > LZMA_MATCH_LEN_MAX)
2806 + fb = LZMA_MATCH_LEN_MAX;
2807 + p->numFastBytes = fb;
2812 + p->fastMode = (props.algo == 0);
2813 + p->matchFinderBase.btMode = props.btMode;
2815 + UInt32 numHashBytes = 4;
2818 + if (props.numHashBytes < 2)
2820 + else if (props.numHashBytes < 4)
2821 + numHashBytes = props.numHashBytes;
2823 + p->matchFinderBase.numHashBytes = numHashBytes;
2826 + p->matchFinderBase.cutValue = props.mc;
2828 + p->writeEndMark = props.writeEndMark;
2832 + if (newMultiThread != _multiThread)
2834 + ReleaseMatchFinder();
2835 + _multiThread = newMultiThread;
2838 + p->multiThread = (props.numThreads > 1);
2844 +static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
2845 +static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
2846 +static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
2847 +static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
2849 +#define IsCharState(s) ((s) < 7)
2851 +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
2853 +#define kInfinityPrice (1 << 30)
2855 +static void RangeEnc_Construct(CRangeEnc *p)
2861 +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
2863 +#define RC_BUF_SIZE (1 << 16)
2864 +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
2866 + if (p->bufBase == 0)
2868 + p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
2869 + if (p->bufBase == 0)
2871 + p->bufLim = p->bufBase + RC_BUF_SIZE;
2876 +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
2878 + alloc->Free(alloc, p->bufBase);
2882 +static void RangeEnc_Init(CRangeEnc *p)
2884 + /* Stream.Init(); */
2886 + p->range = 0xFFFFFFFF;
2890 + p->buf = p->bufBase;
2896 +static void RangeEnc_FlushStream(CRangeEnc *p)
2899 + if (p->res != SZ_OK)
2901 + num = p->buf - p->bufBase;
2902 + if (num != p->outStream->Write(p->outStream, p->bufBase, num))
2903 + p->res = SZ_ERROR_WRITE;
2904 + p->processed += num;
2905 + p->buf = p->bufBase;
2908 +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
2910 + if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
2912 + Byte temp = p->cache;
2915 + Byte *buf = p->buf;
2916 + *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
2918 + if (buf == p->bufLim)
2919 + RangeEnc_FlushStream(p);
2922 + while (--p->cacheSize != 0);
2923 + p->cache = (Byte)((UInt32)p->low >> 24);
2926 + p->low = (UInt32)p->low << 8;
2929 +static void RangeEnc_FlushData(CRangeEnc *p)
2932 + for (i = 0; i < 5; i++)
2933 + RangeEnc_ShiftLow(p);
2936 +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
2941 + p->low += p->range & (0 - ((value >> --numBits) & 1));
2942 + if (p->range < kTopValue)
2945 + RangeEnc_ShiftLow(p);
2948 + while (numBits != 0);
2951 +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
2953 + UInt32 ttt = *prob;
2954 + UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
2957 + p->range = newBound;
2958 + ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
2962 + p->low += newBound;
2963 + p->range -= newBound;
2964 + ttt -= ttt >> kNumMoveBits;
2966 + *prob = (CLzmaProb)ttt;
2967 + if (p->range < kTopValue)
2970 + RangeEnc_ShiftLow(p);
2974 +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
2979 + RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
2982 + while (symbol < 0x10000);
2985 +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
2987 + UInt32 offs = 0x100;
2992 + RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
2994 + offs &= ~(matchByte ^ symbol);
2996 + while (symbol < 0x10000);
2999 +static void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
3002 + for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
3004 + const int kCyclesBits = kNumBitPriceShiftBits;
3006 + UInt32 bitCount = 0;
3008 + for (j = 0; j < kCyclesBits; j++)
3012 + while (w >= ((UInt32)1 << 16))
3018 + ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
3023 +#define GET_PRICE(prob, symbol) \
3024 + p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3026 +#define GET_PRICEa(prob, symbol) \
3027 + ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3029 +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
3030 +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3032 +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
3033 +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3035 +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
3041 + price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
3044 + while (symbol < 0x10000);
3048 +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
3051 + UInt32 offs = 0x100;
3056 + price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
3058 + offs &= ~(matchByte ^ symbol);
3060 + while (symbol < 0x10000);
3065 +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3069 + for (i = numBitLevels; i != 0;)
3073 + bit = (symbol >> i) & 1;
3074 + RangeEnc_EncodeBit(rc, probs + m, bit);
3075 + m = (m << 1) | bit;
3079 +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3083 + for (i = 0; i < numBitLevels; i++)
3085 + UInt32 bit = symbol & 1;
3086 + RangeEnc_EncodeBit(rc, probs + m, bit);
3087 + m = (m << 1) | bit;
3092 +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3095 + symbol |= (1 << numBitLevels);
3096 + while (symbol != 1)
3098 + price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
3104 +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3109 + for (i = numBitLevels; i != 0; i--)
3111 + UInt32 bit = symbol & 1;
3113 + price += GET_PRICEa(probs[m], bit);
3114 + m = (m << 1) | bit;
3120 +static void LenEnc_Init(CLenEnc *p)
3123 + p->choice = p->choice2 = kProbInitValue;
3124 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
3125 + p->low[i] = kProbInitValue;
3126 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
3127 + p->mid[i] = kProbInitValue;
3128 + for (i = 0; i < kLenNumHighSymbols; i++)
3129 + p->high[i] = kProbInitValue;
3132 +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
3134 + if (symbol < kLenNumLowSymbols)
3136 + RangeEnc_EncodeBit(rc, &p->choice, 0);
3137 + RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
3141 + RangeEnc_EncodeBit(rc, &p->choice, 1);
3142 + if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
3144 + RangeEnc_EncodeBit(rc, &p->choice2, 0);
3145 + RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
3149 + RangeEnc_EncodeBit(rc, &p->choice2, 1);
3150 + RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
3155 +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
3157 + UInt32 a0 = GET_PRICE_0a(p->choice);
3158 + UInt32 a1 = GET_PRICE_1a(p->choice);
3159 + UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
3160 + UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
3162 + for (i = 0; i < kLenNumLowSymbols; i++)
3164 + if (i >= numSymbols)
3166 + prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
3168 + for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
3170 + if (i >= numSymbols)
3172 + prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
3174 + for (; i < numSymbols; i++)
3175 + prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
3178 +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
3180 + LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
3181 + p->counters[posState] = p->tableSize;
3184 +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
3187 + for (posState = 0; posState < numPosStates; posState++)
3188 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3191 +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
3193 + LenEnc_Encode(&p->p, rc, symbol, posState);
3195 + if (--p->counters[posState] == 0)
3196 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3202 +static void MovePos(CLzmaEnc *p, UInt32 num)
3206 + printf("\n MovePos %d", num);
3210 + p->additionalOffset += num;
3211 + p->matchFinder.Skip(p->matchFinderObj, num);
3215 +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
3217 + UInt32 lenRes = 0, numPairs;
3218 + p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
3219 + numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
3221 + printf("\n i = %d numPairs = %d ", ttt, numPairs / 2);
3225 + for (i = 0; i < numPairs; i += 2)
3226 + printf("%2d %6d | ", p->matches[i], p->matches[i + 1]);
3231 + lenRes = p->matches[numPairs - 2];
3232 + if (lenRes == p->numFastBytes)
3234 + const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3235 + UInt32 distance = p->matches[numPairs - 1] + 1;
3236 + UInt32 numAvail = p->numAvail;
3237 + if (numAvail > LZMA_MATCH_LEN_MAX)
3238 + numAvail = LZMA_MATCH_LEN_MAX;
3240 + const Byte *pby2 = pby - distance;
3241 + for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
3245 + p->additionalOffset++;
3246 + *numDistancePairsRes = numPairs;
3251 +#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
3252 +#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
3253 +#define IsShortRep(p) ((p)->backPrev == 0)
3255 +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
3258 + GET_PRICE_0(p->isRepG0[state]) +
3259 + GET_PRICE_0(p->isRep0Long[state][posState]);
3262 +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
3265 + if (repIndex == 0)
3267 + price = GET_PRICE_0(p->isRepG0[state]);
3268 + price += GET_PRICE_1(p->isRep0Long[state][posState]);
3272 + price = GET_PRICE_1(p->isRepG0[state]);
3273 + if (repIndex == 1)
3274 + price += GET_PRICE_0(p->isRepG1[state]);
3277 + price += GET_PRICE_1(p->isRepG1[state]);
3278 + price += GET_PRICE(p->isRepG2[state], repIndex - 2);
3284 +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
3286 + return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
3287 + GetPureRepPrice(p, repIndex, state, posState);
3290 +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
3292 + UInt32 posMem = p->opt[cur].posPrev;
3293 + UInt32 backMem = p->opt[cur].backPrev;
3294 + p->optimumEndIndex = cur;
3297 + if (p->opt[cur].prev1IsChar)
3299 + MakeAsChar(&p->opt[posMem])
3300 + p->opt[posMem].posPrev = posMem - 1;
3301 + if (p->opt[cur].prev2)
3303 + p->opt[posMem - 1].prev1IsChar = False;
3304 + p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
3305 + p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
3309 + UInt32 posPrev = posMem;
3310 + UInt32 backCur = backMem;
3312 + backMem = p->opt[posPrev].backPrev;
3313 + posMem = p->opt[posPrev].posPrev;
3315 + p->opt[posPrev].backPrev = backCur;
3316 + p->opt[posPrev].posPrev = cur;
3321 + *backRes = p->opt[0].backPrev;
3322 + p->optimumCurrentIndex = p->opt[0].posPrev;
3323 + return p->optimumCurrentIndex;
3326 +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
3328 +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
3330 + UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;
3331 + UInt32 matchPrice, repMatchPrice, normalMatchPrice;
3332 + UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
3335 + Byte curByte, matchByte;
3336 + if (p->optimumEndIndex != p->optimumCurrentIndex)
3338 + const COptimal *opt = &p->opt[p->optimumCurrentIndex];
3339 + UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
3340 + *backRes = opt->backPrev;
3341 + p->optimumCurrentIndex = opt->posPrev;
3344 + p->optimumCurrentIndex = p->optimumEndIndex = 0;
3346 + if (p->additionalOffset == 0)
3347 + mainLen = ReadMatchDistances(p, &numPairs);
3350 + mainLen = p->longestMatchLength;
3351 + numPairs = p->numPairs;
3354 + numAvail = p->numAvail;
3357 + *backRes = (UInt32)(-1);
3360 + if (numAvail > LZMA_MATCH_LEN_MAX)
3361 + numAvail = LZMA_MATCH_LEN_MAX;
3363 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3365 + for (i = 0; i < LZMA_NUM_REPS; i++)
3368 + const Byte *data2;
3369 + reps[i] = p->reps[i];
3370 + data2 = data - (reps[i] + 1);
3371 + if (data[0] != data2[0] || data[1] != data2[1])
3376 + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
3377 + repLens[i] = lenTest;
3378 + if (lenTest > repLens[repMaxIndex])
3381 + if (repLens[repMaxIndex] >= p->numFastBytes)
3384 + *backRes = repMaxIndex;
3385 + lenRes = repLens[repMaxIndex];
3386 + MovePos(p, lenRes - 1);
3390 + matches = p->matches;
3391 + if (mainLen >= p->numFastBytes)
3393 + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
3394 + MovePos(p, mainLen - 1);
3398 + matchByte = *(data - (reps[0] + 1));
3400 + if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2)
3402 + *backRes = (UInt32)-1;
3406 + p->opt[0].state = (CState)p->state;
3408 + posState = (position & p->pbMask);
3411 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
3412 + p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
3413 + (!IsCharState(p->state) ?
3414 + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
3415 + LitEnc_GetPrice(probs, curByte, p->ProbPrices));
3418 + MakeAsChar(&p->opt[1]);
3420 + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
3421 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
3423 + if (matchByte == curByte)
3425 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
3426 + if (shortRepPrice < p->opt[1].price)
3428 + p->opt[1].price = shortRepPrice;
3429 + MakeAsShortRep(&p->opt[1]);
3432 + lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
3436 + *backRes = p->opt[1].backPrev;
3440 + p->opt[1].posPrev = 0;
3441 + for (i = 0; i < LZMA_NUM_REPS; i++)
3442 + p->opt[0].backs[i] = reps[i];
3446 + p->opt[len--].price = kInfinityPrice;
3449 + for (i = 0; i < LZMA_NUM_REPS; i++)
3451 + UInt32 repLen = repLens[i];
3455 + price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
3458 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
3459 + COptimal *opt = &p->opt[repLen];
3460 + if (curAndLenPrice < opt->price)
3462 + opt->price = curAndLenPrice;
3464 + opt->backPrev = i;
3465 + opt->prev1IsChar = False;
3468 + while (--repLen >= 2);
3471 + normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
3473 + len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
3474 + if (len <= mainLen)
3477 + while (len > matches[offs])
3482 + UInt32 distance = matches[offs + 1];
3484 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
3485 + UInt32 lenToPosState = GetLenToPosState(len);
3486 + if (distance < kNumFullDistances)
3487 + curAndLenPrice += p->distancesPrices[lenToPosState][distance];
3491 + GetPosSlot2(distance, slot);
3492 + curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
3494 + opt = &p->opt[len];
3495 + if (curAndLenPrice < opt->price)
3497 + opt->price = curAndLenPrice;
3499 + opt->backPrev = distance + LZMA_NUM_REPS;
3500 + opt->prev1IsChar = False;
3502 + if (len == matches[offs])
3505 + if (offs == numPairs)
3514 + if (position >= 0)
3517 + printf("\n pos = %4X", position);
3518 + for (i = cur; i <= lenEnd; i++)
3519 + printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
3525 + UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen;
3526 + UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice;
3528 + Byte curByte, matchByte;
3531 + COptimal *nextOpt;
3534 + if (cur == lenEnd)
3535 + return Backward(p, backRes, cur);
3537 + newLen = ReadMatchDistances(p, &numPairs);
3538 + if (newLen >= p->numFastBytes)
3540 + p->numPairs = numPairs;
3541 + p->longestMatchLength = newLen;
3542 + return Backward(p, backRes, cur);
3545 + curOpt = &p->opt[cur];
3546 + posPrev = curOpt->posPrev;
3547 + if (curOpt->prev1IsChar)
3550 + if (curOpt->prev2)
3552 + state = p->opt[curOpt->posPrev2].state;
3553 + if (curOpt->backPrev2 < LZMA_NUM_REPS)
3554 + state = kRepNextStates[state];
3556 + state = kMatchNextStates[state];
3559 + state = p->opt[posPrev].state;
3560 + state = kLiteralNextStates[state];
3563 + state = p->opt[posPrev].state;
3564 + if (posPrev == cur - 1)
3566 + if (IsShortRep(curOpt))
3567 + state = kShortRepNextStates[state];
3569 + state = kLiteralNextStates[state];
3574 + const COptimal *prevOpt;
3575 + if (curOpt->prev1IsChar && curOpt->prev2)
3577 + posPrev = curOpt->posPrev2;
3578 + pos = curOpt->backPrev2;
3579 + state = kRepNextStates[state];
3583 + pos = curOpt->backPrev;
3584 + if (pos < LZMA_NUM_REPS)
3585 + state = kRepNextStates[state];
3587 + state = kMatchNextStates[state];
3589 + prevOpt = &p->opt[posPrev];
3590 + if (pos < LZMA_NUM_REPS)
3593 + reps[0] = prevOpt->backs[pos];
3594 + for (i = 1; i <= pos; i++)
3595 + reps[i] = prevOpt->backs[i - 1];
3596 + for (; i < LZMA_NUM_REPS; i++)
3597 + reps[i] = prevOpt->backs[i];
3602 + reps[0] = (pos - LZMA_NUM_REPS);
3603 + for (i = 1; i < LZMA_NUM_REPS; i++)
3604 + reps[i] = prevOpt->backs[i - 1];
3607 + curOpt->state = (CState)state;
3609 + curOpt->backs[0] = reps[0];
3610 + curOpt->backs[1] = reps[1];
3611 + curOpt->backs[2] = reps[2];
3612 + curOpt->backs[3] = reps[3];
3614 + curPrice = curOpt->price;
3615 + nextIsChar = False;
3616 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3618 + matchByte = *(data - (reps[0] + 1));
3620 + posState = (position & p->pbMask);
3622 + curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
3624 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
3626 + (!IsCharState(state) ?
3627 + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
3628 + LitEnc_GetPrice(probs, curByte, p->ProbPrices));
3631 + nextOpt = &p->opt[cur + 1];
3633 + if (curAnd1Price < nextOpt->price)
3635 + nextOpt->price = curAnd1Price;
3636 + nextOpt->posPrev = cur;
3637 + MakeAsChar(nextOpt);
3638 + nextIsChar = True;
3641 + matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
3642 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
3644 + if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
3646 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
3647 + if (shortRepPrice <= nextOpt->price)
3649 + nextOpt->price = shortRepPrice;
3650 + nextOpt->posPrev = cur;
3651 + MakeAsShortRep(nextOpt);
3652 + nextIsChar = True;
3655 + numAvailFull = p->numAvail;
3657 + UInt32 temp = kNumOpts - 1 - cur;
3658 + if (temp < numAvailFull)
3659 + numAvailFull = temp;
3662 + if (numAvailFull < 2)
3664 + numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
3666 + if (!nextIsChar && matchByte != curByte) /* speed optimization */
3668 + /* try Literal + rep0 */
3671 + const Byte *data2 = data - (reps[0] + 1);
3672 + UInt32 limit = p->numFastBytes + 1;
3673 + if (limit > numAvailFull)
3674 + limit = numAvailFull;
3676 + for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
3677 + lenTest2 = temp - 1;
3678 + if (lenTest2 >= 2)
3680 + UInt32 state2 = kLiteralNextStates[state];
3681 + UInt32 posStateNext = (position + 1) & p->pbMask;
3682 + UInt32 nextRepMatchPrice = curAnd1Price +
3683 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
3684 + GET_PRICE_1(p->isRep[state2]);
3685 + /* for (; lenTest2 >= 2; lenTest2--) */
3687 + UInt32 curAndLenPrice;
3689 + UInt32 offset = cur + 1 + lenTest2;
3690 + while (lenEnd < offset)
3691 + p->opt[++lenEnd].price = kInfinityPrice;
3692 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
3693 + opt = &p->opt[offset];
3694 + if (curAndLenPrice < opt->price)
3696 + opt->price = curAndLenPrice;
3697 + opt->posPrev = cur + 1;
3698 + opt->backPrev = 0;
3699 + opt->prev1IsChar = True;
3700 + opt->prev2 = False;
3706 + startLen = 2; /* speed optimization */
3709 + for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
3712 + UInt32 lenTestTemp;
3714 + const Byte *data2 = data - (reps[repIndex] + 1);
3715 + if (data[0] != data2[0] || data[1] != data2[1])
3717 + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
3718 + while (lenEnd < cur + lenTest)
3719 + p->opt[++lenEnd].price = kInfinityPrice;
3720 + lenTestTemp = lenTest;
3721 + price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
3724 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
3725 + COptimal *opt = &p->opt[cur + lenTest];
3726 + if (curAndLenPrice < opt->price)
3728 + opt->price = curAndLenPrice;
3729 + opt->posPrev = cur;
3730 + opt->backPrev = repIndex;
3731 + opt->prev1IsChar = False;
3734 + while (--lenTest >= 2);
3735 + lenTest = lenTestTemp;
3737 + if (repIndex == 0)
3738 + startLen = lenTest + 1;
3740 + /* if (_maxMode) */
3742 + UInt32 lenTest2 = lenTest + 1;
3743 + UInt32 limit = lenTest2 + p->numFastBytes;
3744 + UInt32 nextRepMatchPrice;
3745 + if (limit > numAvailFull)
3746 + limit = numAvailFull;
3747 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
3748 + lenTest2 -= lenTest + 1;
3749 + if (lenTest2 >= 2)
3751 + UInt32 state2 = kRepNextStates[state];
3752 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
3753 + UInt32 curAndLenCharPrice =
3754 + price + p->repLenEnc.prices[posState][lenTest - 2] +
3755 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
3756 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
3757 + data[lenTest], data2[lenTest], p->ProbPrices);
3758 + state2 = kLiteralNextStates[state2];
3759 + posStateNext = (position + lenTest + 1) & p->pbMask;
3760 + nextRepMatchPrice = curAndLenCharPrice +
3761 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
3762 + GET_PRICE_1(p->isRep[state2]);
3764 + /* for (; lenTest2 >= 2; lenTest2--) */
3766 + UInt32 curAndLenPrice;
3768 + UInt32 offset = cur + lenTest + 1 + lenTest2;
3769 + while (lenEnd < offset)
3770 + p->opt[++lenEnd].price = kInfinityPrice;
3771 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
3772 + opt = &p->opt[offset];
3773 + if (curAndLenPrice < opt->price)
3775 + opt->price = curAndLenPrice;
3776 + opt->posPrev = cur + lenTest + 1;
3777 + opt->backPrev = 0;
3778 + opt->prev1IsChar = True;
3779 + opt->prev2 = True;
3780 + opt->posPrev2 = cur;
3781 + opt->backPrev2 = repIndex;
3788 + /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
3789 + if (newLen > numAvail)
3791 + newLen = numAvail;
3792 + for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2);
3793 + matches[numPairs] = newLen;
3796 + if (newLen >= startLen)
3798 + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
3799 + UInt32 offs, curBack, posSlot;
3801 + while (lenEnd < cur + newLen)
3802 + p->opt[++lenEnd].price = kInfinityPrice;
3805 + while (startLen > matches[offs])
3807 + curBack = matches[offs + 1];
3808 + GetPosSlot2(curBack, posSlot);
3809 + for (lenTest = /*2*/ startLen; ; lenTest++)
3811 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
3812 + UInt32 lenToPosState = GetLenToPosState(lenTest);
3814 + if (curBack < kNumFullDistances)
3815 + curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
3817 + curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
3819 + opt = &p->opt[cur + lenTest];
3820 + if (curAndLenPrice < opt->price)
3822 + opt->price = curAndLenPrice;
3823 + opt->posPrev = cur;
3824 + opt->backPrev = curBack + LZMA_NUM_REPS;
3825 + opt->prev1IsChar = False;
3828 + if (/*_maxMode && */lenTest == matches[offs])
3830 + /* Try Match + Literal + Rep0 */
3831 + const Byte *data2 = data - (curBack + 1);
3832 + UInt32 lenTest2 = lenTest + 1;
3833 + UInt32 limit = lenTest2 + p->numFastBytes;
3834 + UInt32 nextRepMatchPrice;
3835 + if (limit > numAvailFull)
3836 + limit = numAvailFull;
3837 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
3838 + lenTest2 -= lenTest + 1;
3839 + if (lenTest2 >= 2)
3841 + UInt32 state2 = kMatchNextStates[state];
3842 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
3843 + UInt32 curAndLenCharPrice = curAndLenPrice +
3844 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
3845 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
3846 + data[lenTest], data2[lenTest], p->ProbPrices);
3847 + state2 = kLiteralNextStates[state2];
3848 + posStateNext = (posStateNext + 1) & p->pbMask;
3849 + nextRepMatchPrice = curAndLenCharPrice +
3850 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
3851 + GET_PRICE_1(p->isRep[state2]);
3853 + /* for (; lenTest2 >= 2; lenTest2--) */
3855 + UInt32 offset = cur + lenTest + 1 + lenTest2;
3856 + UInt32 curAndLenPrice;
3858 + while (lenEnd < offset)
3859 + p->opt[++lenEnd].price = kInfinityPrice;
3860 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
3861 + opt = &p->opt[offset];
3862 + if (curAndLenPrice < opt->price)
3864 + opt->price = curAndLenPrice;
3865 + opt->posPrev = cur + lenTest + 1;
3866 + opt->backPrev = 0;
3867 + opt->prev1IsChar = True;
3868 + opt->prev2 = True;
3869 + opt->posPrev2 = cur;
3870 + opt->backPrev2 = curBack + LZMA_NUM_REPS;
3875 + if (offs == numPairs)
3877 + curBack = matches[offs + 1];
3878 + if (curBack >= kNumFullDistances)
3879 + GetPosSlot2(curBack, posSlot);
3886 +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
3888 +static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
3890 + UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
3892 + const UInt32 *matches;
3894 + if (p->additionalOffset == 0)
3895 + mainLen = ReadMatchDistances(p, &numPairs);
3898 + mainLen = p->longestMatchLength;
3899 + numPairs = p->numPairs;
3902 + numAvail = p->numAvail;
3903 + *backRes = (UInt32)-1;
3906 + if (numAvail > LZMA_MATCH_LEN_MAX)
3907 + numAvail = LZMA_MATCH_LEN_MAX;
3908 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3910 + repLen = repIndex = 0;
3911 + for (i = 0; i < LZMA_NUM_REPS; i++)
3914 + const Byte *data2 = data - (p->reps[i] + 1);
3915 + if (data[0] != data2[0] || data[1] != data2[1])
3917 + for (len = 2; len < numAvail && data[len] == data2[len]; len++);
3918 + if (len >= p->numFastBytes)
3921 + MovePos(p, len - 1);
3931 + matches = p->matches;
3932 + if (mainLen >= p->numFastBytes)
3934 + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
3935 + MovePos(p, mainLen - 1);
3939 + mainDist = 0; /* for GCC */
3942 + mainDist = matches[numPairs - 1];
3943 + while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
3945 + if (!ChangePair(matches[numPairs - 3], mainDist))
3948 + mainLen = matches[numPairs - 2];
3949 + mainDist = matches[numPairs - 1];
3951 + if (mainLen == 2 && mainDist >= 0x80)
3955 + if (repLen >= 2 && (
3956 + (repLen + 1 >= mainLen) ||
3957 + (repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
3958 + (repLen + 3 >= mainLen && mainDist >= (1 << 15))))
3960 + *backRes = repIndex;
3961 + MovePos(p, repLen - 1);
3965 + if (mainLen < 2 || numAvail <= 2)
3968 + p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
3969 + if (p->longestMatchLength >= 2)
3971 + UInt32 newDistance = matches[p->numPairs - 1];
3972 + if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
3973 + (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
3974 + (p->longestMatchLength > mainLen + 1) ||
3975 + (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
3979 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3980 + for (i = 0; i < LZMA_NUM_REPS; i++)
3982 + UInt32 len, limit;
3983 + const Byte *data2 = data - (p->reps[i] + 1);
3984 + if (data[0] != data2[0] || data[1] != data2[1])
3986 + limit = mainLen - 1;
3987 + for (len = 2; len < limit && data[len] == data2[len]; len++);
3991 + *backRes = mainDist + LZMA_NUM_REPS;
3992 + MovePos(p, mainLen - 2);
3996 +static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
3999 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4000 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4001 + p->state = kMatchNextStates[p->state];
4002 + len = LZMA_MATCH_LEN_MIN;
4003 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4004 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
4005 + RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
4006 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
4009 +static SRes CheckErrors(CLzmaEnc *p)
4011 + if (p->result != SZ_OK)
4013 + if (p->rc.res != SZ_OK)
4014 + p->result = SZ_ERROR_WRITE;
4015 + if (p->matchFinderBase.result != SZ_OK)
4016 + p->result = SZ_ERROR_READ;
4017 + if (p->result != SZ_OK)
4018 + p->finished = True;
4022 +static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
4024 + /* ReleaseMFStream(); */
4025 + p->finished = True;
4026 + if (p->writeEndMark)
4027 + WriteEndMarker(p, nowPos & p->pbMask);
4028 + RangeEnc_FlushData(&p->rc);
4029 + RangeEnc_FlushStream(&p->rc);
4030 + return CheckErrors(p);
4033 +static void FillAlignPrices(CLzmaEnc *p)
4036 + for (i = 0; i < kAlignTableSize; i++)
4037 + p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
4038 + p->alignPriceCount = 0;
4041 +static void FillDistancesPrices(CLzmaEnc *p)
4043 + UInt32 tempPrices[kNumFullDistances];
4044 + UInt32 i, lenToPosState;
4045 + for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
4047 + UInt32 posSlot = GetPosSlot1(i);
4048 + UInt32 footerBits = ((posSlot >> 1) - 1);
4049 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4050 + tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
4053 + for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
4056 + const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
4057 + UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
4058 + for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
4059 + posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
4060 + for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
4061 + posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
4064 + UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
4066 + for (i = 0; i < kStartPosModelIndex; i++)
4067 + distancesPrices[i] = posSlotPrices[i];
4068 + for (; i < kNumFullDistances; i++)
4069 + distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
4072 + p->matchPriceCount = 0;
4075 +static void LzmaEnc_Construct(CLzmaEnc *p)
4077 + RangeEnc_Construct(&p->rc);
4078 + MatchFinder_Construct(&p->matchFinderBase);
4080 + MatchFinderMt_Construct(&p->matchFinderMt);
4081 + p->matchFinderMt.MatchFinder = &p->matchFinderBase;
4085 + CLzmaEncProps props;
4086 + LzmaEncProps_Init(&props);
4087 + LzmaEnc_SetProps(p, &props);
4090 + #ifndef LZMA_LOG_BSR
4091 + LzmaEnc_FastPosInit(p->g_FastPos);
4094 + LzmaEnc_InitPriceTables(p->ProbPrices);
4096 + p->saveState.litProbs = 0;
4099 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
4102 + p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
4104 + LzmaEnc_Construct((CLzmaEnc *)p);
4108 +static void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
4110 + alloc->Free(alloc, p->litProbs);
4111 + alloc->Free(alloc, p->saveState.litProbs);
4113 + p->saveState.litProbs = 0;
4116 +static void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
4119 + MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
4121 + MatchFinder_Free(&p->matchFinderBase, allocBig);
4122 + LzmaEnc_FreeLits(p, alloc);
4123 + RangeEnc_Free(&p->rc, alloc);
4126 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
4128 + LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
4129 + alloc->Free(alloc, p);
4132 +static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
4134 + UInt32 nowPos32, startPos32;
4137 + p->matchFinder.Init(p->matchFinderObj);
4143 + RINOK(CheckErrors(p));
4145 + nowPos32 = (UInt32)p->nowPos64;
4146 + startPos32 = nowPos32;
4148 + if (p->nowPos64 == 0)
4152 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4153 + return Flush(p, nowPos32);
4154 + ReadMatchDistances(p, &numPairs);
4155 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
4156 + p->state = kLiteralNextStates[p->state];
4157 + curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
4158 + LitEnc_Encode(&p->rc, p->litProbs, curByte);
4159 + p->additionalOffset--;
4163 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
4166 + UInt32 pos, len, posState;
4169 + len = GetOptimumFast(p, &pos);
4171 + len = GetOptimum(p, nowPos32, &pos);
4174 + printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
4177 + posState = nowPos32 & p->pbMask;
4178 + if (len == 1 && pos == (UInt32)-1)
4184 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
4185 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
4187 + probs = LIT_PROBS(nowPos32, *(data - 1));
4188 + if (IsCharState(p->state))
4189 + LitEnc_Encode(&p->rc, probs, curByte);
4191 + LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
4192 + p->state = kLiteralNextStates[p->state];
4196 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4197 + if (pos < LZMA_NUM_REPS)
4199 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
4202 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
4203 + RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
4207 + UInt32 distance = p->reps[pos];
4208 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
4210 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
4213 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
4214 + RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
4216 + p->reps[3] = p->reps[2];
4217 + p->reps[2] = p->reps[1];
4219 + p->reps[1] = p->reps[0];
4220 + p->reps[0] = distance;
4223 + p->state = kShortRepNextStates[p->state];
4226 + LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4227 + p->state = kRepNextStates[p->state];
4233 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4234 + p->state = kMatchNextStates[p->state];
4235 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4236 + pos -= LZMA_NUM_REPS;
4237 + GetPosSlot(pos, posSlot);
4238 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
4240 + if (posSlot >= kStartPosModelIndex)
4242 + UInt32 footerBits = ((posSlot >> 1) - 1);
4243 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4244 + UInt32 posReduced = pos - base;
4246 + if (posSlot < kEndPosModelIndex)
4247 + RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
4250 + RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
4251 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
4252 + p->alignPriceCount++;
4255 + p->reps[3] = p->reps[2];
4256 + p->reps[2] = p->reps[1];
4257 + p->reps[1] = p->reps[0];
4259 + p->matchPriceCount++;
4262 + p->additionalOffset -= len;
4264 + if (p->additionalOffset == 0)
4269 + if (p->matchPriceCount >= (1 << 7))
4270 + FillDistancesPrices(p);
4271 + if (p->alignPriceCount >= kAlignTableSize)
4272 + FillAlignPrices(p);
4274 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4276 + processed = nowPos32 - startPos32;
4279 + if (processed + kNumOpts + 300 >= maxUnpackSize ||
4280 + RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
4283 + else if (processed >= (1 << 15))
4285 + p->nowPos64 += nowPos32 - startPos32;
4286 + return CheckErrors(p);
4290 + p->nowPos64 += nowPos32 - startPos32;
4291 + return Flush(p, nowPos32);
4294 +#define kBigHashDicLimit ((UInt32)1 << 24)
4296 +static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4298 + UInt32 beforeSize = kNumOpts;
4300 + if (!RangeEnc_Alloc(&p->rc, alloc))
4301 + return SZ_ERROR_MEM;
4302 + btMode = (p->matchFinderBase.btMode != 0);
4304 + p->mtMode = (p->multiThread && !p->fastMode && btMode);
4308 + unsigned lclp = p->lc + p->lp;
4309 + if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
4311 + LzmaEnc_FreeLits(p, alloc);
4312 + p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
4313 + p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
4314 + if (p->litProbs == 0 || p->saveState.litProbs == 0)
4316 + LzmaEnc_FreeLits(p, alloc);
4317 + return SZ_ERROR_MEM;
4323 + p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
4325 + if (beforeSize + p->dictSize < keepWindowSize)
4326 + beforeSize = keepWindowSize - p->dictSize;
4331 + RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
4332 + p->matchFinderObj = &p->matchFinderMt;
4333 + MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
4338 + if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
4339 + return SZ_ERROR_MEM;
4340 + p->matchFinderObj = &p->matchFinderBase;
4341 + MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
4346 +static void LzmaEnc_Init(CLzmaEnc *p)
4350 + for (i = 0 ; i < LZMA_NUM_REPS; i++)
4353 + RangeEnc_Init(&p->rc);
4356 + for (i = 0; i < kNumStates; i++)
4359 + for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
4361 + p->isMatch[i][j] = kProbInitValue;
4362 + p->isRep0Long[i][j] = kProbInitValue;
4364 + p->isRep[i] = kProbInitValue;
4365 + p->isRepG0[i] = kProbInitValue;
4366 + p->isRepG1[i] = kProbInitValue;
4367 + p->isRepG2[i] = kProbInitValue;
4371 + UInt32 num = 0x300 << (p->lp + p->lc);
4372 + for (i = 0; i < num; i++)
4373 + p->litProbs[i] = kProbInitValue;
4377 + for (i = 0; i < kNumLenToPosStates; i++)
4379 + CLzmaProb *probs = p->posSlotEncoder[i];
4381 + for (j = 0; j < (1 << kNumPosSlotBits); j++)
4382 + probs[j] = kProbInitValue;
4386 + for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
4387 + p->posEncoders[i] = kProbInitValue;
4390 + LenEnc_Init(&p->lenEnc.p);
4391 + LenEnc_Init(&p->repLenEnc.p);
4393 + for (i = 0; i < (1 << kNumAlignBits); i++)
4394 + p->posAlignEncoder[i] = kProbInitValue;
4396 + p->optimumEndIndex = 0;
4397 + p->optimumCurrentIndex = 0;
4398 + p->additionalOffset = 0;
4400 + p->pbMask = (1 << p->pb) - 1;
4401 + p->lpMask = (1 << p->lp) - 1;
4404 +static void LzmaEnc_InitPrices(CLzmaEnc *p)
4408 + FillDistancesPrices(p);
4409 + FillAlignPrices(p);
4412 + p->lenEnc.tableSize =
4413 + p->repLenEnc.tableSize =
4414 + p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
4415 + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
4416 + LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
4419 +static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4422 + for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
4423 + if (p->dictSize <= ((UInt32)1 << i))
4425 + p->distTableSize = i * 2;
4427 + p->finished = False;
4428 + p->result = SZ_OK;
4429 + RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
4431 + LzmaEnc_InitPrices(p);
4436 +static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
4438 + p->matchFinderBase.directInput = 1;
4439 + p->matchFinderBase.bufferBase = (Byte *)src;
4440 + p->matchFinderBase.directInputRem = srcLen;
4443 +static SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
4444 + UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4446 + CLzmaEnc *p = (CLzmaEnc *)pp;
4447 + LzmaEnc_SetInputBuf(p, src, srcLen);
4450 + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
4453 +static void LzmaEnc_Finish(CLzmaEncHandle pp)
4456 + CLzmaEnc *p = (CLzmaEnc *)pp;
4458 + MatchFinderMt_ReleaseStream(&p->matchFinderMt);
4466 + ISeqOutStream funcTable;
4470 +} CSeqOutStreamBuf;
4472 +static size_t MyWrite(void *pp, const void *data, size_t size)
4474 + CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
4475 + if (p->rem < size)
4478 + p->overflow = True;
4480 + memcpy(p->data, data, size);
4486 +static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
4491 + Byte allocaDummy[0x300];
4493 + for (i = 0; i < 16; i++)
4494 + allocaDummy[i] = (Byte)i;
4499 + res = LzmaEnc_CodeOneBlock(p, False, 0, 0);
4500 + if (res != SZ_OK || p->finished != 0)
4502 + if (progress != 0)
4504 + res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
4507 + res = SZ_ERROR_PROGRESS;
4512 + LzmaEnc_Finish(p);
4516 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
4518 + CLzmaEnc *p = (CLzmaEnc *)pp;
4520 + UInt32 dictSize = p->dictSize;
4521 + if (*size < LZMA_PROPS_SIZE)
4522 + return SZ_ERROR_PARAM;
4523 + *size = LZMA_PROPS_SIZE;
4524 + props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
4526 + for (i = 11; i <= 30; i++)
4528 + if (dictSize <= ((UInt32)2 << i))
4530 + dictSize = (2 << i);
4533 + if (dictSize <= ((UInt32)3 << i))
4535 + dictSize = (3 << i);
4540 + for (i = 0; i < 4; i++)
4541 + props[1 + i] = (Byte)(dictSize >> (8 * i));
4545 +SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
4546 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
4549 + CLzmaEnc *p = (CLzmaEnc *)pp;
4551 + CSeqOutStreamBuf outStream;
4553 + LzmaEnc_SetInputBuf(p, src, srcLen);
4555 + outStream.funcTable.Write = MyWrite;
4556 + outStream.data = dest;
4557 + outStream.rem = *destLen;
4558 + outStream.overflow = False;
4560 + p->writeEndMark = writeEndMark;
4562 + p->rc.outStream = &outStream.funcTable;
4563 + res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);
4565 + res = LzmaEnc_Encode2(p, progress);
4567 + *destLen -= outStream.rem;
4568 + if (outStream.overflow)
4569 + return SZ_ERROR_OUTPUT_EOF;
4573 +++ b/lib/lzma/Makefile
4575 +lzma_compress-objs := LzFind.o LzmaEnc.o
4576 +lzma_decompress-objs := LzmaDec.o
4578 +obj-$(CONFIG_LZMA_COMPRESS) += lzma_compress.o
4579 +obj-$(CONFIG_LZMA_DECOMPRESS) += lzma_decompress.o
4581 +EXTRA_CFLAGS += -Iinclude/linux -Iinclude/linux/lzma -include types.h