1 Index: linux-2.6.38-rc6/fs/jffs2/Kconfig
2 ===================================================================
3 --- linux-2.6.38-rc6.orig/fs/jffs2/Kconfig 2011-02-22 02:25:52.000000000 +0100
4 +++ linux-2.6.38-rc6/fs/jffs2/Kconfig 2011-02-28 15:34:05.308257697 +0100
5 @@ -139,6 +139,15 @@ config JFFS2_LZO
6 This feature was added in July, 2007. Say 'N' if you need
7 compatibility with older bootloaders or kernels.
10 + bool "JFFS2 LZMA compression support" if JFFS2_COMPRESSION_OPTIONS
11 + select LZMA_COMPRESS
12 + select LZMA_DECOMPRESS
16 + JFFS2 wrapper to the LZMA C SDK
19 bool "JFFS2 RTIME compression support" if JFFS2_COMPRESSION_OPTIONS
21 Index: linux-2.6.38-rc6/fs/jffs2/Makefile
22 ===================================================================
23 --- linux-2.6.38-rc6.orig/fs/jffs2/Makefile 2011-02-22 02:25:52.000000000 +0100
24 +++ linux-2.6.38-rc6/fs/jffs2/Makefile 2011-02-28 15:34:05.308257697 +0100
25 @@ -18,4 +18,7 @@ jffs2-$(CONFIG_JFFS2_RUBIN) += compr_rub
26 jffs2-$(CONFIG_JFFS2_RTIME) += compr_rtime.o
27 jffs2-$(CONFIG_JFFS2_ZLIB) += compr_zlib.o
28 jffs2-$(CONFIG_JFFS2_LZO) += compr_lzo.o
29 +jffs2-$(CONFIG_JFFS2_LZMA) += compr_lzma.o
30 jffs2-$(CONFIG_JFFS2_SUMMARY) += summary.o
32 +CFLAGS_compr_lzma.o += -Iinclude/linux -Ilib/lzma
33 Index: linux-2.6.38-rc6/fs/jffs2/compr.c
34 ===================================================================
35 --- linux-2.6.38-rc6.orig/fs/jffs2/compr.c 2011-02-22 02:25:52.000000000 +0100
36 +++ linux-2.6.38-rc6/fs/jffs2/compr.c 2011-02-28 15:34:05.309257668 +0100
37 @@ -320,6 +320,9 @@ int __init jffs2_compressors_init(void)
38 #ifdef CONFIG_JFFS2_LZO
41 +#ifdef CONFIG_JFFS2_LZMA
44 /* Setting default compression mode */
45 #ifdef CONFIG_JFFS2_CMODE_NONE
46 jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
47 @@ -343,6 +346,9 @@ int __init jffs2_compressors_init(void)
48 int jffs2_compressors_exit(void)
50 /* Unregistering compressors */
51 +#ifdef CONFIG_JFFS2_LZMA
54 #ifdef CONFIG_JFFS2_LZO
57 Index: linux-2.6.38-rc6/fs/jffs2/compr.h
58 ===================================================================
59 --- linux-2.6.38-rc6.orig/fs/jffs2/compr.h 2011-02-22 02:25:52.000000000 +0100
60 +++ linux-2.6.38-rc6/fs/jffs2/compr.h 2011-02-28 15:34:05.309257668 +0100
62 #define JFFS2_DYNRUBIN_PRIORITY 20
63 #define JFFS2_LZARI_PRIORITY 30
64 #define JFFS2_RTIME_PRIORITY 50
65 -#define JFFS2_ZLIB_PRIORITY 60
66 -#define JFFS2_LZO_PRIORITY 80
68 +#define JFFS2_LZMA_PRIORITY 70
69 +#define JFFS2_ZLIB_PRIORITY 80
70 +#define JFFS2_LZO_PRIORITY 90
72 #define JFFS2_RUBINMIPS_DISABLED /* RUBINs will be used only */
73 #define JFFS2_DYNRUBIN_DISABLED /* for decompression */
74 @@ -99,5 +99,9 @@ void jffs2_zlib_exit(void);
75 int jffs2_lzo_init(void);
76 void jffs2_lzo_exit(void);
78 +#ifdef CONFIG_JFFS2_LZMA
79 +int jffs2_lzma_init(void);
80 +void jffs2_lzma_exit(void);
83 #endif /* __JFFS2_COMPR_H__ */
84 Index: linux-2.6.38-rc6/fs/jffs2/compr_lzma.c
85 ===================================================================
86 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
87 +++ linux-2.6.38-rc6/fs/jffs2/compr_lzma.c 2011-02-28 16:15:02.388304647 +0100
90 + * JFFS2 -- Journalling Flash File System, Version 2.
92 + * For licensing information, see the file 'LICENCE' in this directory.
94 + * JFFS2 wrapper to the LZMA C SDK
98 +#include <linux/lzma.h>
102 + static DEFINE_MUTEX(deflate_mutex);
106 +Byte propsEncoded[LZMA_PROPS_SIZE];
107 +SizeT propsSize = sizeof(propsEncoded);
109 +STATIC void lzma_free_workspace(void)
111 + LzmaEnc_Destroy(p, &lzma_alloc, &lzma_alloc);
114 +STATIC int INIT lzma_alloc_workspace(CLzmaEncProps *props)
116 + if ((p = (CLzmaEncHandle *)LzmaEnc_Create(&lzma_alloc)) == NULL)
118 + PRINT_ERROR("Failed to allocate lzma deflate workspace\n");
122 + if (LzmaEnc_SetProps(p, props) != SZ_OK)
124 + lzma_free_workspace();
128 + if (LzmaEnc_WriteProperties(p, propsEncoded, &propsSize) != SZ_OK)
130 + lzma_free_workspace();
137 +STATIC int jffs2_lzma_compress(unsigned char *data_in, unsigned char *cpage_out,
138 + uint32_t *sourcelen, uint32_t *dstlen)
140 + SizeT compress_size = (SizeT)(*dstlen);
144 + mutex_lock(&deflate_mutex);
147 + ret = LzmaEnc_MemEncode(p, cpage_out, &compress_size, data_in, *sourcelen,
148 + 0, NULL, &lzma_alloc, &lzma_alloc);
151 + mutex_unlock(&deflate_mutex);
157 + *dstlen = (uint32_t)compress_size;
162 +STATIC int jffs2_lzma_decompress(unsigned char *data_in, unsigned char *cpage_out,
163 + uint32_t srclen, uint32_t destlen)
166 + SizeT dl = (SizeT)destlen;
167 + SizeT sl = (SizeT)srclen;
168 + ELzmaStatus status;
170 + ret = LzmaDecode(cpage_out, &dl, data_in, &sl, propsEncoded,
171 + propsSize, LZMA_FINISH_ANY, &status, &lzma_alloc);
173 + if (ret != SZ_OK || status == LZMA_STATUS_NOT_FINISHED || dl != (SizeT)destlen)
179 +static struct jffs2_compressor jffs2_lzma_comp = {
180 + .priority = JFFS2_LZMA_PRIORITY,
182 + .compr = JFFS2_COMPR_LZMA,
183 + .compress = &jffs2_lzma_compress,
184 + .decompress = &jffs2_lzma_decompress,
188 +int INIT jffs2_lzma_init(void)
191 + CLzmaEncProps props;
192 + LzmaEncProps_Init(&props);
194 + props.dictSize = LZMA_BEST_DICT(0x2000);
195 + props.level = LZMA_BEST_LEVEL;
196 + props.lc = LZMA_BEST_LC;
197 + props.lp = LZMA_BEST_LP;
198 + props.pb = LZMA_BEST_PB;
199 + props.fb = LZMA_BEST_FB;
201 + ret = lzma_alloc_workspace(&props);
205 + ret = jffs2_register_compressor(&jffs2_lzma_comp);
207 + lzma_free_workspace();
212 +void jffs2_lzma_exit(void)
214 + jffs2_unregister_compressor(&jffs2_lzma_comp);
215 + lzma_free_workspace();
217 Index: linux-2.6.38-rc6/fs/jffs2/super.c
218 ===================================================================
219 --- linux-2.6.38-rc6.orig/fs/jffs2/super.c 2011-02-22 02:25:52.000000000 +0100
220 +++ linux-2.6.38-rc6/fs/jffs2/super.c 2011-02-28 15:34:05.310257639 +0100
221 @@ -255,14 +255,41 @@ static int __init init_jffs2_fs(void)
222 BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
223 BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
225 - printk(KERN_INFO "JFFS2 version 2.2."
226 + printk(KERN_INFO "JFFS2 version 2.2"
227 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
230 #ifdef CONFIG_JFFS2_SUMMARY
234 - " © 2001-2006 Red Hat, Inc.\n");
235 +#ifdef CONFIG_JFFS2_ZLIB
238 +#ifdef CONFIG_JFFS2_LZO
241 +#ifdef CONFIG_JFFS2_LZMA
244 +#ifdef CONFIG_JFFS2_RTIME
247 +#ifdef CONFIG_JFFS2_RUBIN
250 +#ifdef CONFIG_JFFS2_CMODE_NONE
253 +#ifdef CONFIG_JFFS2_CMODE_PRIORITY
254 + " (CMODE_PRIORITY)"
256 +#ifdef CONFIG_JFFS2_CMODE_SIZE
259 +#ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
260 + " (CMODE_FAVOURLZO)"
262 + " (c) 2001-2006 Red Hat, Inc.\n");
264 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
265 sizeof(struct jffs2_inode_info),
266 Index: linux-2.6.38-rc6/include/linux/jffs2.h
267 ===================================================================
268 --- linux-2.6.38-rc6.orig/include/linux/jffs2.h 2011-02-22 02:25:52.000000000 +0100
269 +++ linux-2.6.38-rc6/include/linux/jffs2.h 2011-02-28 15:34:05.310257639 +0100
271 #define JFFS2_COMPR_DYNRUBIN 0x05
272 #define JFFS2_COMPR_ZLIB 0x06
273 #define JFFS2_COMPR_LZO 0x07
274 +#define JFFS2_COMPR_LZMA 0x08
275 /* Compatibility flags. */
276 #define JFFS2_COMPAT_MASK 0xc000 /* What do to if an unknown nodetype is found */
277 #define JFFS2_NODE_ACCURATE 0x2000
278 Index: linux-2.6.38-rc6/include/linux/lzma.h
279 ===================================================================
280 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
281 +++ linux-2.6.38-rc6/include/linux/lzma.h 2011-02-28 15:34:05.310257639 +0100
287 + #include <linux/kernel.h>
288 + #include <linux/sched.h>
289 + #include <linux/slab.h>
290 + #include <linux/vmalloc.h>
291 + #include <linux/init.h>
292 + #define LZMA_MALLOC vmalloc
293 + #define LZMA_FREE vfree
294 + #define PRINT_ERROR(msg) printk(KERN_WARNING #msg)
295 + #define INIT __init
296 + #define STATIC static
298 + #include <stdint.h>
299 + #include <stdlib.h>
301 + #include <unistd.h>
302 + #include <string.h>
303 + #include <asm/types.h>
305 + #include <linux/jffs2.h>
307 + extern int page_size;
308 + #define PAGE_SIZE page_size
310 + #define LZMA_MALLOC malloc
311 + #define LZMA_FREE free
312 + #define PRINT_ERROR(msg) fprintf(stderr, msg)
317 +#include "lzma/LzmaDec.h"
318 +#include "lzma/LzmaEnc.h"
320 +#define LZMA_BEST_LEVEL (9)
321 +#define LZMA_BEST_LC (0)
322 +#define LZMA_BEST_LP (0)
323 +#define LZMA_BEST_PB (0)
324 +#define LZMA_BEST_FB (273)
326 +#define LZMA_BEST_DICT(n) (((int)((n) / 2)) * 2)
328 +static void *p_lzma_malloc(void *p, size_t size)
333 + return LZMA_MALLOC(size);
336 +static void p_lzma_free(void *p, void *address)
338 + if (address != NULL)
339 + LZMA_FREE(address);
342 +static ISzAlloc lzma_alloc = {p_lzma_malloc, p_lzma_free};
345 Index: linux-2.6.38-rc6/include/linux/lzma/LzFind.h
346 ===================================================================
347 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
348 +++ linux-2.6.38-rc6/include/linux/lzma/LzFind.h 2011-02-28 16:14:14.392426757 +0100
350 +/* LzFind.h -- Match finder for LZ algorithms
351 +2009-04-22 : Igor Pavlov : Public domain */
362 +typedef UInt32 CLzRef;
364 +typedef struct _CMatchFinder
372 + UInt32 cyclicBufferPos;
373 + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */
375 + UInt32 matchMaxLen;
382 + ISeqInStream *stream;
383 + int streamEndWasReached;
386 + UInt32 keepSizeBefore;
387 + UInt32 keepSizeAfter;
389 + UInt32 numHashBytes;
391 + size_t directInputRem;
394 + UInt32 historySize;
395 + UInt32 fixedHashSize;
396 + UInt32 hashSizeSum;
402 +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer)
403 +#define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)])
405 +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos)
407 +int MatchFinder_NeedMove(CMatchFinder *p);
408 +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p);
409 +void MatchFinder_MoveBlock(CMatchFinder *p);
410 +void MatchFinder_ReadIfRequired(CMatchFinder *p);
412 +void MatchFinder_Construct(CMatchFinder *p);
415 + historySize <= 3 GB
416 + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB
418 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
419 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
421 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc);
422 +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems);
423 +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue);
425 +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son,
426 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue,
427 + UInt32 *distances, UInt32 maxLen);
431 + Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func.
432 + Mf_GetPointerToCurrentPos_Func's result must be used only before any other function
435 +typedef void (*Mf_Init_Func)(void *object);
436 +typedef Byte (*Mf_GetIndexByte_Func)(void *object, Int32 index);
437 +typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object);
438 +typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object);
439 +typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances);
440 +typedef void (*Mf_Skip_Func)(void *object, UInt32);
442 +typedef struct _IMatchFinder
445 + Mf_GetIndexByte_Func GetIndexByte;
446 + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes;
447 + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos;
448 + Mf_GetMatches_Func GetMatches;
452 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable);
454 +void MatchFinder_Init(CMatchFinder *p);
455 +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances);
456 +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances);
457 +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num);
458 +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num);
465 Index: linux-2.6.38-rc6/include/linux/lzma/LzHash.h
466 ===================================================================
467 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
468 +++ linux-2.6.38-rc6/include/linux/lzma/LzHash.h 2011-02-28 15:34:05.311257610 +0100
470 +/* LzHash.h -- HASH functions for LZ algorithms
471 +2009-02-07 : Igor Pavlov : Public domain */
476 +#define kHash2Size (1 << 10)
477 +#define kHash3Size (1 << 16)
478 +#define kHash4Size (1 << 20)
480 +#define kFix3HashSize (kHash2Size)
481 +#define kFix4HashSize (kHash2Size + kHash3Size)
482 +#define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size)
484 +#define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8);
486 +#define HASH3_CALC { \
487 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
488 + hash2Value = temp & (kHash2Size - 1); \
489 + hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; }
491 +#define HASH4_CALC { \
492 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
493 + hash2Value = temp & (kHash2Size - 1); \
494 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
495 + hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; }
497 +#define HASH5_CALC { \
498 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
499 + hash2Value = temp & (kHash2Size - 1); \
500 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
501 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \
502 + hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \
503 + hash4Value &= (kHash4Size - 1); }
505 +/* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */
506 +#define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF;
509 +#define MT_HASH2_CALC \
510 + hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1);
512 +#define MT_HASH3_CALC { \
513 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
514 + hash2Value = temp & (kHash2Size - 1); \
515 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); }
517 +#define MT_HASH4_CALC { \
518 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
519 + hash2Value = temp & (kHash2Size - 1); \
520 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
521 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); }
524 Index: linux-2.6.38-rc6/include/linux/lzma/LzmaDec.h
525 ===================================================================
526 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
527 +++ linux-2.6.38-rc6/include/linux/lzma/LzmaDec.h 2011-02-28 16:14:14.408426387 +0100
529 +/* LzmaDec.h -- LZMA Decoder
530 +2009-02-07 : Igor Pavlov : Public domain */
532 +#ifndef __LZMA_DEC_H
533 +#define __LZMA_DEC_H
541 +/* #define _LZMA_PROB32 */
542 +/* _LZMA_PROB32 can increase the speed on some CPUs,
543 + but memory usage for CLzmaDec::probs will be doubled in that case */
546 +#define CLzmaProb UInt32
548 +#define CLzmaProb UInt16
552 +/* ---------- LZMA Properties ---------- */
554 +#define LZMA_PROPS_SIZE 5
556 +typedef struct _CLzmaProps
558 + unsigned lc, lp, pb;
562 +/* LzmaProps_Decode - decodes properties
565 + SZ_ERROR_UNSUPPORTED - Unsupported properties
568 +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
571 +/* ---------- LZMA Decoder state ---------- */
573 +/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
574 + Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
576 +#define LZMA_REQUIRED_INPUT_MAX 20
584 + UInt32 range, code;
587 + UInt32 processedPos;
588 + UInt32 checkDicSize;
591 + unsigned remainLen;
595 + unsigned tempBufSize;
596 + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
599 +#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
601 +void LzmaDec_Init(CLzmaDec *p);
603 +/* There are two types of LZMA streams:
604 + 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
605 + 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
609 + LZMA_FINISH_ANY, /* finish at any point */
610 + LZMA_FINISH_END /* block must be finished at the end */
613 +/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
615 + You must use LZMA_FINISH_END, when you know that current output buffer
616 + covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
618 + If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
619 + and output value of destLen will be less than output buffer size limit.
620 + You can check status result also.
622 + You can use multiple checks to test data integrity after full decompression:
623 + 1) Check Result and "status" variable.
624 + 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
625 + 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
626 + You must use correct finish mode in that case. */
630 + LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
631 + LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
632 + LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
633 + LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
634 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
637 +/* ELzmaStatus is used only as output value for function call */
640 +/* ---------- Interfaces ---------- */
642 +/* There are 3 levels of interfaces:
643 + 1) Dictionary Interface
644 + 2) Buffer Interface
645 + 3) One Call Interface
646 + You can select any of these interfaces, but don't mix functions from different
647 + groups for same object. */
650 +/* There are two variants to allocate state for Dictionary Interface:
651 + 1) LzmaDec_Allocate / LzmaDec_Free
652 + 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
653 + You can use variant 2, if you set dictionary buffer manually.
654 + For Buffer Interface you must always use variant 1.
656 +LzmaDec_Allocate* can return:
658 + SZ_ERROR_MEM - Memory allocation error
659 + SZ_ERROR_UNSUPPORTED - Unsupported properties
662 +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
663 +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
665 +SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc);
666 +void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc);
668 +/* ---------- Dictionary Interface ---------- */
670 +/* You can use it, if you want to eliminate the overhead for data copying from
671 + dictionary to some other external buffer.
672 + You must work with CLzmaDec variables directly in this interface.
677 + for (each new stream)
680 + while (it needs more decompression)
682 + LzmaDec_DecodeToDic()
683 + use data from CLzmaDec::dic and update CLzmaDec::dicPos
689 +/* LzmaDec_DecodeToDic
691 + The decoding to internal dictionary buffer (CLzmaDec::dic).
692 + You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
695 + It has meaning only if the decoding reaches output limit (dicLimit).
696 + LZMA_FINISH_ANY - Decode just dicLimit bytes.
697 + LZMA_FINISH_END - Stream must be finished after dicLimit.
702 + LZMA_STATUS_FINISHED_WITH_MARK
703 + LZMA_STATUS_NOT_FINISHED
704 + LZMA_STATUS_NEEDS_MORE_INPUT
705 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
706 + SZ_ERROR_DATA - Data error
709 +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
710 + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
713 +/* ---------- Buffer Interface ---------- */
715 +/* It's zlib-like interface.
716 + See LzmaDec_DecodeToDic description for information about STEPS and return results,
717 + but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
718 + to work with CLzmaDec variables manually.
721 + It has meaning only if the decoding reaches output limit (*destLen).
722 + LZMA_FINISH_ANY - Decode just destLen bytes.
723 + LZMA_FINISH_END - Stream must be finished after (*destLen).
726 +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
727 + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
730 +/* ---------- One Call Interface ---------- */
735 + It has meaning only if the decoding reaches output limit (*destLen).
736 + LZMA_FINISH_ANY - Decode just destLen bytes.
737 + LZMA_FINISH_END - Stream must be finished after (*destLen).
742 + LZMA_STATUS_FINISHED_WITH_MARK
743 + LZMA_STATUS_NOT_FINISHED
744 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
745 + SZ_ERROR_DATA - Data error
746 + SZ_ERROR_MEM - Memory allocation error
747 + SZ_ERROR_UNSUPPORTED - Unsupported properties
748 + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
751 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
752 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
753 + ELzmaStatus *status, ISzAlloc *alloc);
760 Index: linux-2.6.38-rc6/include/linux/lzma/LzmaEnc.h
761 ===================================================================
762 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
763 +++ linux-2.6.38-rc6/include/linux/lzma/LzmaEnc.h 2011-02-28 16:14:14.430425878 +0100
765 +/* LzmaEnc.h -- LZMA Encoder
766 +2009-02-07 : Igor Pavlov : Public domain */
768 +#ifndef __LZMA_ENC_H
769 +#define __LZMA_ENC_H
777 +#define LZMA_PROPS_SIZE 5
779 +typedef struct _CLzmaEncProps
781 + int level; /* 0 <= level <= 9 */
782 + UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
783 + (1 << 12) <= dictSize <= (1 << 30) for 64-bit version
784 + default = (1 << 24) */
785 + int lc; /* 0 <= lc <= 8, default = 3 */
786 + int lp; /* 0 <= lp <= 4, default = 0 */
787 + int pb; /* 0 <= pb <= 4, default = 2 */
788 + int algo; /* 0 - fast, 1 - normal, default = 1 */
789 + int fb; /* 5 <= fb <= 273, default = 32 */
790 + int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
791 + int numHashBytes; /* 2, 3 or 4, default = 4 */
792 + UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */
793 + unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
794 + int numThreads; /* 1 or 2, default = 2 */
797 +void LzmaEncProps_Init(CLzmaEncProps *p);
798 +void LzmaEncProps_Normalize(CLzmaEncProps *p);
799 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2);
802 +/* ---------- CLzmaEncHandle Interface ---------- */
804 +/* LzmaEnc_* functions can return the following exit codes:
807 + SZ_ERROR_MEM - Memory allocation error
808 + SZ_ERROR_PARAM - Incorrect paramater in props
809 + SZ_ERROR_WRITE - Write callback error.
810 + SZ_ERROR_PROGRESS - some break from progress callback
811 + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
814 +typedef void * CLzmaEncHandle;
816 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc);
817 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig);
818 +SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props);
819 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size);
820 +SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream,
821 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
822 +SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
823 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
825 +/* ---------- One Call Interface ---------- */
830 + SZ_ERROR_MEM - Memory allocation error
831 + SZ_ERROR_PARAM - Incorrect paramater
832 + SZ_ERROR_OUTPUT_EOF - output buffer overflow
833 + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
836 +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
837 + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
838 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
845 Index: linux-2.6.38-rc6/include/linux/lzma/Types.h
846 ===================================================================
847 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
848 +++ linux-2.6.38-rc6/include/linux/lzma/Types.h 2011-02-28 15:34:05.313257550 +0100
850 +/* Types.h -- Basic types
851 +2009-11-23 : Igor Pavlov : Public domain */
853 +#ifndef __7Z_TYPES_H
854 +#define __7Z_TYPES_H
859 +#include <windows.h>
862 +#ifndef EXTERN_C_BEGIN
864 +#define EXTERN_C_BEGIN extern "C" {
865 +#define EXTERN_C_END }
867 +#define EXTERN_C_BEGIN
868 +#define EXTERN_C_END
876 +#define SZ_ERROR_DATA 1
877 +#define SZ_ERROR_MEM 2
878 +#define SZ_ERROR_CRC 3
879 +#define SZ_ERROR_UNSUPPORTED 4
880 +#define SZ_ERROR_PARAM 5
881 +#define SZ_ERROR_INPUT_EOF 6
882 +#define SZ_ERROR_OUTPUT_EOF 7
883 +#define SZ_ERROR_READ 8
884 +#define SZ_ERROR_WRITE 9
885 +#define SZ_ERROR_PROGRESS 10
886 +#define SZ_ERROR_FAIL 11
887 +#define SZ_ERROR_THREAD 12
889 +#define SZ_ERROR_ARCHIVE 16
890 +#define SZ_ERROR_NO_ARCHIVE 17
901 +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
904 +typedef unsigned char Byte;
905 +typedef short Int16;
906 +typedef unsigned short UInt16;
908 +#ifdef _LZMA_UINT32_IS_ULONG
910 +typedef unsigned long UInt32;
913 +typedef unsigned int UInt32;
916 +#ifdef _SZ_NO_INT_64
918 +/* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.
919 + NOTES: Some code will work incorrectly in that case! */
922 +typedef unsigned long UInt64;
926 +#if defined(_MSC_VER) || defined(__BORLANDC__)
927 +typedef __int64 Int64;
928 +typedef unsigned __int64 UInt64;
930 +typedef long long int Int64;
931 +typedef unsigned long long int UInt64;
936 +#ifdef _LZMA_NO_SYSTEM_SIZE_T
937 +typedef UInt32 SizeT;
939 +typedef size_t SizeT;
948 +#define MY_STD_CALL __stdcall
955 +#if _MSC_VER >= 1300
956 +#define MY_NO_INLINE __declspec(noinline)
958 +#define MY_NO_INLINE
961 +#define MY_CDECL __cdecl
962 +#define MY_FAST_CALL __fastcall
967 +#define MY_FAST_CALL
972 +/* The following interfaces use first parameter as pointer to structure */
976 + SRes (*Read)(void *p, void *buf, size_t *size);
977 + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
978 + (output(*size) < input(*size)) is allowed */
981 +/* it can return SZ_ERROR_INPUT_EOF */
982 +SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size);
983 +SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType);
984 +SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf);
988 + size_t (*Write)(void *p, const void *buf, size_t size);
989 + /* Returns: result - the number of actually written bytes.
990 + (result < size) means error */
1002 + SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */
1003 + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
1008 + SRes (*Look)(void *p, void **buf, size_t *size);
1009 + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
1010 + (output(*size) > input(*size)) is not allowed
1011 + (output(*size) < input(*size)) is allowed */
1012 + SRes (*Skip)(void *p, size_t offset);
1013 + /* offset must be <= output(*size) of Look */
1015 + SRes (*Read)(void *p, void *buf, size_t *size);
1016 + /* reads directly (without buffer). It's same as ISeqInStream::Read */
1017 + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
1020 +SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size);
1021 +SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset);
1023 +/* reads via ILookInStream::Read */
1024 +SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType);
1025 +SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size);
1027 +#define LookToRead_BUF_SIZE (1 << 14)
1032 + ISeekInStream *realStream;
1035 + Byte buf[LookToRead_BUF_SIZE];
1038 +void LookToRead_CreateVTable(CLookToRead *p, int lookahead);
1039 +void LookToRead_Init(CLookToRead *p);
1044 + ILookInStream *realStream;
1047 +void SecToLook_CreateVTable(CSecToLook *p);
1052 + ILookInStream *realStream;
1055 +void SecToRead_CreateVTable(CSecToRead *p);
1059 + SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize);
1060 + /* Returns: result. (result != SZ_OK) means break.
1061 + Value (UInt64)(Int64)-1 for size means unknown value. */
1062 +} ICompressProgress;
1066 + void *(*Alloc)(void *p, size_t size);
1067 + void (*Free)(void *p, void *address); /* address can be 0 */
1070 +#define IAlloc_Alloc(p, size) (p)->Alloc((p), size)
1071 +#define IAlloc_Free(p, a) (p)->Free((p), a)
1076 Index: linux-2.6.38-rc6/lib/Kconfig
1077 ===================================================================
1078 --- linux-2.6.38-rc6.orig/lib/Kconfig 2011-02-22 02:25:52.000000000 +0100
1079 +++ linux-2.6.38-rc6/lib/Kconfig 2011-02-28 16:14:00.063757281 +0100
1080 @@ -108,6 +108,12 @@ config LZO_DECOMPRESS
1082 source "lib/xz/Kconfig"
1084 +config LZMA_COMPRESS
1087 +config LZMA_DECOMPRESS
1091 # These all provide a common interface (hence the apparent duplication with
1092 # ZLIB_INFLATE; DECOMPRESS_GZIP is just a wrapper.)
1093 Index: linux-2.6.38-rc6/lib/Makefile
1094 ===================================================================
1095 --- linux-2.6.38-rc6.orig/lib/Makefile 2011-02-22 02:25:52.000000000 +0100
1096 +++ linux-2.6.38-rc6/lib/Makefile 2011-02-28 15:34:05.313257550 +0100
1098 # Makefile for some libs needed in the kernel.
1101 +ifdef CONFIG_JFFS2_ZLIB
1102 + CONFIG_ZLIB_INFLATE:=y
1103 + CONFIG_ZLIB_DEFLATE:=y
1106 +ifdef CONFIG_JFFS2_LZMA
1107 + CONFIG_LZMA_DECOMPRESS:=y
1108 + CONFIG_LZMA_COMPRESS:=y
1111 ifdef CONFIG_FUNCTION_TRACER
1112 ORIG_CFLAGS := $(KBUILD_CFLAGS)
1113 KBUILD_CFLAGS = $(subst -pg,,$(ORIG_CFLAGS))
1114 @@ -71,6 +81,8 @@ obj-$(CONFIG_LZO_COMPRESS) += lzo/
1115 obj-$(CONFIG_LZO_DECOMPRESS) += lzo/
1116 obj-$(CONFIG_XZ_DEC) += xz/
1117 obj-$(CONFIG_RAID6_PQ) += raid6/
1118 +obj-$(CONFIG_LZMA_COMPRESS) += lzma/
1119 +obj-$(CONFIG_LZMA_DECOMPRESS) += lzma/
1121 lib-$(CONFIG_DECOMPRESS_GZIP) += decompress_inflate.o
1122 lib-$(CONFIG_DECOMPRESS_BZIP2) += decompress_bunzip2.o
1123 Index: linux-2.6.38-rc6/lib/lzma/LzFind.c
1124 ===================================================================
1125 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1126 +++ linux-2.6.38-rc6/lib/lzma/LzFind.c 2011-02-28 16:14:14.447425484 +0100
1128 +/* LzFind.c -- Match finder for LZ algorithms
1129 +2009-04-22 : Igor Pavlov : Public domain */
1131 +#include <string.h>
1133 +#include "LzFind.h"
1134 +#include "LzHash.h"
1136 +#define kEmptyHashValue 0
1137 +#define kMaxValForNormalize ((UInt32)0xFFFFFFFF)
1138 +#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */
1139 +#define kNormalizeMask (~(kNormalizeStepMin - 1))
1140 +#define kMaxHistorySize ((UInt32)3 << 30)
1142 +#define kStartMaxLen 3
1144 +static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
1146 + if (!p->directInput)
1148 + alloc->Free(alloc, p->bufferBase);
1149 + p->bufferBase = 0;
1153 +/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
1155 +static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
1157 + UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
1158 + if (p->directInput)
1160 + p->blockSize = blockSize;
1163 + if (p->bufferBase == 0 || p->blockSize != blockSize)
1165 + LzInWindow_Free(p, alloc);
1166 + p->blockSize = blockSize;
1167 + p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
1169 + return (p->bufferBase != 0);
1172 +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
1173 +Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
1175 +UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
1177 +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
1179 + p->posLimit -= subValue;
1180 + p->pos -= subValue;
1181 + p->streamPos -= subValue;
1184 +static void MatchFinder_ReadBlock(CMatchFinder *p)
1186 + if (p->streamEndWasReached || p->result != SZ_OK)
1188 + if (p->directInput)
1190 + UInt32 curSize = 0xFFFFFFFF - p->streamPos;
1191 + if (curSize > p->directInputRem)
1192 + curSize = (UInt32)p->directInputRem;
1193 + p->directInputRem -= curSize;
1194 + p->streamPos += curSize;
1195 + if (p->directInputRem == 0)
1196 + p->streamEndWasReached = 1;
1201 + Byte *dest = p->buffer + (p->streamPos - p->pos);
1202 + size_t size = (p->bufferBase + p->blockSize - dest);
1205 + p->result = p->stream->Read(p->stream, dest, &size);
1206 + if (p->result != SZ_OK)
1210 + p->streamEndWasReached = 1;
1213 + p->streamPos += (UInt32)size;
1214 + if (p->streamPos - p->pos > p->keepSizeAfter)
1219 +void MatchFinder_MoveBlock(CMatchFinder *p)
1221 + memmove(p->bufferBase,
1222 + p->buffer - p->keepSizeBefore,
1223 + (size_t)(p->streamPos - p->pos + p->keepSizeBefore));
1224 + p->buffer = p->bufferBase + p->keepSizeBefore;
1227 +int MatchFinder_NeedMove(CMatchFinder *p)
1229 + if (p->directInput)
1231 + /* if (p->streamEndWasReached) return 0; */
1232 + return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
1235 +void MatchFinder_ReadIfRequired(CMatchFinder *p)
1237 + if (p->streamEndWasReached)
1239 + if (p->keepSizeAfter >= p->streamPos - p->pos)
1240 + MatchFinder_ReadBlock(p);
1243 +static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
1245 + if (MatchFinder_NeedMove(p))
1246 + MatchFinder_MoveBlock(p);
1247 + MatchFinder_ReadBlock(p);
1250 +static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
1254 + p->numHashBytes = 4;
1258 +#define kCrcPoly 0xEDB88320
1260 +void MatchFinder_Construct(CMatchFinder *p)
1263 + p->bufferBase = 0;
1264 + p->directInput = 0;
1266 + MatchFinder_SetDefaultSettings(p);
1268 + for (i = 0; i < 256; i++)
1272 + for (j = 0; j < 8; j++)
1273 + r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
1278 +static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
1280 + alloc->Free(alloc, p->hash);
1284 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
1286 + MatchFinder_FreeThisClassMemory(p, alloc);
1287 + LzInWindow_Free(p, alloc);
1290 +static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
1292 + size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
1293 + if (sizeInBytes / sizeof(CLzRef) != num)
1295 + return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
1298 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
1299 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
1302 + UInt32 sizeReserv;
1303 + if (historySize > kMaxHistorySize)
1305 + MatchFinder_Free(p, alloc);
1308 + sizeReserv = historySize >> 1;
1309 + if (historySize > ((UInt32)2 << 30))
1310 + sizeReserv = historySize >> 2;
1311 + sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
1313 + p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
1314 + p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
1315 + /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
1316 + if (LzInWindow_Create(p, sizeReserv, alloc))
1318 + UInt32 newCyclicBufferSize = historySize + 1;
1320 + p->matchMaxLen = matchMaxLen;
1322 + p->fixedHashSize = 0;
1323 + if (p->numHashBytes == 2)
1324 + hs = (1 << 16) - 1;
1327 + hs = historySize - 1;
1333 + hs |= 0xFFFF; /* don't change it! It's required for Deflate */
1334 + if (hs > (1 << 24))
1336 + if (p->numHashBytes == 3)
1337 + hs = (1 << 24) - 1;
1344 + if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
1345 + if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
1346 + if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
1347 + hs += p->fixedHashSize;
1351 + UInt32 prevSize = p->hashSizeSum + p->numSons;
1353 + p->historySize = historySize;
1354 + p->hashSizeSum = hs;
1355 + p->cyclicBufferSize = newCyclicBufferSize;
1356 + p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);
1357 + newSize = p->hashSizeSum + p->numSons;
1358 + if (p->hash != 0 && prevSize == newSize)
1360 + MatchFinder_FreeThisClassMemory(p, alloc);
1361 + p->hash = AllocRefs(newSize, alloc);
1364 + p->son = p->hash + p->hashSizeSum;
1369 + MatchFinder_Free(p, alloc);
1373 +static void MatchFinder_SetLimits(CMatchFinder *p)
1375 + UInt32 limit = kMaxValForNormalize - p->pos;
1376 + UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
1377 + if (limit2 < limit)
1379 + limit2 = p->streamPos - p->pos;
1380 + if (limit2 <= p->keepSizeAfter)
1386 + limit2 -= p->keepSizeAfter;
1387 + if (limit2 < limit)
1390 + UInt32 lenLimit = p->streamPos - p->pos;
1391 + if (lenLimit > p->matchMaxLen)
1392 + lenLimit = p->matchMaxLen;
1393 + p->lenLimit = lenLimit;
1395 + p->posLimit = p->pos + limit;
1398 +void MatchFinder_Init(CMatchFinder *p)
1401 + for (i = 0; i < p->hashSizeSum; i++)
1402 + p->hash[i] = kEmptyHashValue;
1403 + p->cyclicBufferPos = 0;
1404 + p->buffer = p->bufferBase;
1405 + p->pos = p->streamPos = p->cyclicBufferSize;
1406 + p->result = SZ_OK;
1407 + p->streamEndWasReached = 0;
1408 + MatchFinder_ReadBlock(p);
1409 + MatchFinder_SetLimits(p);
1412 +static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
1414 + return (p->pos - p->historySize - 1) & kNormalizeMask;
1417 +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
1420 + for (i = 0; i < numItems; i++)
1422 + UInt32 value = items[i];
1423 + if (value <= subValue)
1424 + value = kEmptyHashValue;
1426 + value -= subValue;
1431 +static void MatchFinder_Normalize(CMatchFinder *p)
1433 + UInt32 subValue = MatchFinder_GetSubValue(p);
1434 + MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
1435 + MatchFinder_ReduceOffsets(p, subValue);
1438 +static void MatchFinder_CheckLimits(CMatchFinder *p)
1440 + if (p->pos == kMaxValForNormalize)
1441 + MatchFinder_Normalize(p);
1442 + if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
1443 + MatchFinder_CheckAndMoveAndRead(p);
1444 + if (p->cyclicBufferPos == p->cyclicBufferSize)
1445 + p->cyclicBufferPos = 0;
1446 + MatchFinder_SetLimits(p);
1449 +static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1450 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
1451 + UInt32 *distances, UInt32 maxLen)
1453 + son[_cyclicBufferPos] = curMatch;
1456 + UInt32 delta = pos - curMatch;
1457 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1460 + const Byte *pb = cur - delta;
1461 + curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
1462 + if (pb[maxLen] == cur[maxLen] && *pb == *cur)
1465 + while (++len != lenLimit)
1466 + if (pb[len] != cur[len])
1470 + *distances++ = maxLen = len;
1471 + *distances++ = delta - 1;
1472 + if (len == lenLimit)
1480 +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1481 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
1482 + UInt32 *distances, UInt32 maxLen)
1484 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1485 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1486 + UInt32 len0 = 0, len1 = 0;
1489 + UInt32 delta = pos - curMatch;
1490 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1492 + *ptr0 = *ptr1 = kEmptyHashValue;
1496 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1497 + const Byte *pb = cur - delta;
1498 + UInt32 len = (len0 < len1 ? len0 : len1);
1499 + if (pb[len] == cur[len])
1501 + if (++len != lenLimit && pb[len] == cur[len])
1502 + while (++len != lenLimit)
1503 + if (pb[len] != cur[len])
1507 + *distances++ = maxLen = len;
1508 + *distances++ = delta - 1;
1509 + if (len == lenLimit)
1517 + if (pb[len] < cur[len])
1535 +static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1536 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
1538 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1539 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1540 + UInt32 len0 = 0, len1 = 0;
1543 + UInt32 delta = pos - curMatch;
1544 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1546 + *ptr0 = *ptr1 = kEmptyHashValue;
1550 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1551 + const Byte *pb = cur - delta;
1552 + UInt32 len = (len0 < len1 ? len0 : len1);
1553 + if (pb[len] == cur[len])
1555 + while (++len != lenLimit)
1556 + if (pb[len] != cur[len])
1559 + if (len == lenLimit)
1567 + if (pb[len] < cur[len])
1586 + ++p->cyclicBufferPos; \
1588 + if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
1590 +#define MOVE_POS_RET MOVE_POS return offset;
1592 +static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
1594 +#define GET_MATCHES_HEADER2(minLen, ret_op) \
1595 + UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \
1596 + lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
1599 +#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
1600 +#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
1602 +#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
1604 +#define GET_MATCHES_FOOTER(offset, maxLen) \
1605 + offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
1606 + distances + offset, maxLen) - distances); MOVE_POS_RET;
1608 +#define SKIP_FOOTER \
1609 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;
1611 +static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1614 + GET_MATCHES_HEADER(2)
1616 + curMatch = p->hash[hashValue];
1617 + p->hash[hashValue] = p->pos;
1619 + GET_MATCHES_FOOTER(offset, 1)
1622 +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1625 + GET_MATCHES_HEADER(3)
1627 + curMatch = p->hash[hashValue];
1628 + p->hash[hashValue] = p->pos;
1630 + GET_MATCHES_FOOTER(offset, 2)
1633 +static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1635 + UInt32 hash2Value, delta2, maxLen, offset;
1636 + GET_MATCHES_HEADER(3)
1640 + delta2 = p->pos - p->hash[hash2Value];
1641 + curMatch = p->hash[kFix3HashSize + hashValue];
1643 + p->hash[hash2Value] =
1644 + p->hash[kFix3HashSize + hashValue] = p->pos;
1649 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1651 + for (; maxLen != lenLimit; maxLen++)
1652 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1654 + distances[0] = maxLen;
1655 + distances[1] = delta2 - 1;
1657 + if (maxLen == lenLimit)
1659 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1663 + GET_MATCHES_FOOTER(offset, maxLen)
1666 +static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1668 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1669 + GET_MATCHES_HEADER(4)
1673 + delta2 = p->pos - p->hash[ hash2Value];
1674 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1675 + curMatch = p->hash[kFix4HashSize + hashValue];
1677 + p->hash[ hash2Value] =
1678 + p->hash[kFix3HashSize + hash3Value] =
1679 + p->hash[kFix4HashSize + hashValue] = p->pos;
1683 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1685 + distances[0] = maxLen = 2;
1686 + distances[1] = delta2 - 1;
1689 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1692 + distances[offset + 1] = delta3 - 1;
1698 + for (; maxLen != lenLimit; maxLen++)
1699 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1701 + distances[offset - 2] = maxLen;
1702 + if (maxLen == lenLimit)
1704 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1710 + GET_MATCHES_FOOTER(offset, maxLen)
1713 +static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1715 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1716 + GET_MATCHES_HEADER(4)
1720 + delta2 = p->pos - p->hash[ hash2Value];
1721 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1722 + curMatch = p->hash[kFix4HashSize + hashValue];
1724 + p->hash[ hash2Value] =
1725 + p->hash[kFix3HashSize + hash3Value] =
1726 + p->hash[kFix4HashSize + hashValue] = p->pos;
1730 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1732 + distances[0] = maxLen = 2;
1733 + distances[1] = delta2 - 1;
1736 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1739 + distances[offset + 1] = delta3 - 1;
1745 + for (; maxLen != lenLimit; maxLen++)
1746 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1748 + distances[offset - 2] = maxLen;
1749 + if (maxLen == lenLimit)
1751 + p->son[p->cyclicBufferPos] = curMatch;
1757 + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
1758 + distances + offset, maxLen) - (distances));
1762 +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1765 + GET_MATCHES_HEADER(3)
1767 + curMatch = p->hash[hashValue];
1768 + p->hash[hashValue] = p->pos;
1769 + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
1770 + distances, 2) - (distances));
1774 +static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1780 + curMatch = p->hash[hashValue];
1781 + p->hash[hashValue] = p->pos;
1784 + while (--num != 0);
1787 +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1793 + curMatch = p->hash[hashValue];
1794 + p->hash[hashValue] = p->pos;
1797 + while (--num != 0);
1800 +static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1804 + UInt32 hash2Value;
1807 + curMatch = p->hash[kFix3HashSize + hashValue];
1808 + p->hash[hash2Value] =
1809 + p->hash[kFix3HashSize + hashValue] = p->pos;
1812 + while (--num != 0);
1815 +static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1819 + UInt32 hash2Value, hash3Value;
1822 + curMatch = p->hash[kFix4HashSize + hashValue];
1823 + p->hash[ hash2Value] =
1824 + p->hash[kFix3HashSize + hash3Value] = p->pos;
1825 + p->hash[kFix4HashSize + hashValue] = p->pos;
1828 + while (--num != 0);
1831 +static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1835 + UInt32 hash2Value, hash3Value;
1838 + curMatch = p->hash[kFix4HashSize + hashValue];
1839 + p->hash[ hash2Value] =
1840 + p->hash[kFix3HashSize + hash3Value] =
1841 + p->hash[kFix4HashSize + hashValue] = p->pos;
1842 + p->son[p->cyclicBufferPos] = curMatch;
1845 + while (--num != 0);
1848 +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1854 + curMatch = p->hash[hashValue];
1855 + p->hash[hashValue] = p->pos;
1856 + p->son[p->cyclicBufferPos] = curMatch;
1859 + while (--num != 0);
1862 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
1864 + vTable->Init = (Mf_Init_Func)MatchFinder_Init;
1865 + vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;
1866 + vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
1867 + vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
1870 + vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;
1871 + vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;
1873 + else if (p->numHashBytes == 2)
1875 + vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;
1876 + vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;
1878 + else if (p->numHashBytes == 3)
1880 + vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;
1881 + vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;
1885 + vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
1886 + vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
1889 Index: linux-2.6.38-rc6/lib/lzma/LzmaDec.c
1890 ===================================================================
1891 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1892 +++ linux-2.6.38-rc6/lib/lzma/LzmaDec.c 2011-02-28 16:14:14.463425114 +0100
1894 +/* LzmaDec.c -- LZMA Decoder
1895 +2009-09-20 : Igor Pavlov : Public domain */
1897 +#include "LzmaDec.h"
1899 +#include <string.h>
1901 +#define kNumTopBits 24
1902 +#define kTopValue ((UInt32)1 << kNumTopBits)
1904 +#define kNumBitModelTotalBits 11
1905 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
1906 +#define kNumMoveBits 5
1908 +#define RC_INIT_SIZE 5
1910 +#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
1912 +#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1913 +#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
1914 +#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
1915 +#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
1916 + { UPDATE_0(p); i = (i + i); A0; } else \
1917 + { UPDATE_1(p); i = (i + i) + 1; A1; }
1918 +#define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
1920 +#define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
1921 +#define TREE_DECODE(probs, limit, i) \
1922 + { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
1924 +/* #define _LZMA_SIZE_OPT */
1926 +#ifdef _LZMA_SIZE_OPT
1927 +#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
1929 +#define TREE_6_DECODE(probs, i) \
1931 + TREE_GET_BIT(probs, i); \
1932 + TREE_GET_BIT(probs, i); \
1933 + TREE_GET_BIT(probs, i); \
1934 + TREE_GET_BIT(probs, i); \
1935 + TREE_GET_BIT(probs, i); \
1936 + TREE_GET_BIT(probs, i); \
1940 +#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
1942 +#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1943 +#define UPDATE_0_CHECK range = bound;
1944 +#define UPDATE_1_CHECK range -= bound; code -= bound;
1945 +#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
1946 + { UPDATE_0_CHECK; i = (i + i); A0; } else \
1947 + { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
1948 +#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
1949 +#define TREE_DECODE_CHECK(probs, limit, i) \
1950 + { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
1953 +#define kNumPosBitsMax 4
1954 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
1956 +#define kLenNumLowBits 3
1957 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
1958 +#define kLenNumMidBits 3
1959 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
1960 +#define kLenNumHighBits 8
1961 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
1963 +#define LenChoice 0
1964 +#define LenChoice2 (LenChoice + 1)
1965 +#define LenLow (LenChoice2 + 1)
1966 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
1967 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
1968 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
1971 +#define kNumStates 12
1972 +#define kNumLitStates 7
1974 +#define kStartPosModelIndex 4
1975 +#define kEndPosModelIndex 14
1976 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
1978 +#define kNumPosSlotBits 6
1979 +#define kNumLenToPosStates 4
1981 +#define kNumAlignBits 4
1982 +#define kAlignTableSize (1 << kNumAlignBits)
1984 +#define kMatchMinLen 2
1985 +#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
1988 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
1989 +#define IsRepG0 (IsRep + kNumStates)
1990 +#define IsRepG1 (IsRepG0 + kNumStates)
1991 +#define IsRepG2 (IsRepG1 + kNumStates)
1992 +#define IsRep0Long (IsRepG2 + kNumStates)
1993 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
1994 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
1995 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
1996 +#define LenCoder (Align + kAlignTableSize)
1997 +#define RepLenCoder (LenCoder + kNumLenProbs)
1998 +#define Literal (RepLenCoder + kNumLenProbs)
2000 +#define LZMA_BASE_SIZE 1846
2001 +#define LZMA_LIT_SIZE 768
2003 +#define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
2005 +#if Literal != LZMA_BASE_SIZE
2006 +StopCompilingDueBUG
2009 +#define LZMA_DIC_MIN (1 << 12)
2011 +/* First LZMA-symbol is always decoded.
2012 +And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
2016 + SZ_ERROR_DATA - Error
2018 + < kMatchSpecLenStart : normal remain
2019 + = kMatchSpecLenStart : finished
2020 + = kMatchSpecLenStart + 1 : Flush marker
2021 + = kMatchSpecLenStart + 2 : State Init Marker
2024 +static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
2026 + CLzmaProb *probs = p->probs;
2028 + unsigned state = p->state;
2029 + UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
2030 + unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
2031 + unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
2032 + unsigned lc = p->prop.lc;
2034 + Byte *dic = p->dic;
2035 + SizeT dicBufSize = p->dicBufSize;
2036 + SizeT dicPos = p->dicPos;
2038 + UInt32 processedPos = p->processedPos;
2039 + UInt32 checkDicSize = p->checkDicSize;
2042 + const Byte *buf = p->buf;
2043 + UInt32 range = p->range;
2044 + UInt32 code = p->code;
2051 + unsigned posState = processedPos & pbMask;
2053 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
2058 + prob = probs + Literal;
2059 + if (checkDicSize != 0 || processedPos != 0)
2060 + prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
2061 + (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
2063 + if (state < kNumLitStates)
2065 + state -= (state < 4) ? state : 3;
2067 + do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
2071 + unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
2072 + unsigned offs = 0x100;
2073 + state -= (state < 10) ? 3 : 6;
2078 + CLzmaProb *probLit;
2080 + bit = (matchByte & offs);
2081 + probLit = prob + offs + bit + symbol;
2082 + GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
2084 + while (symbol < 0x100);
2086 + dic[dicPos++] = (Byte)symbol;
2093 + prob = probs + IsRep + state;
2097 + state += kNumStates;
2098 + prob = probs + LenCoder;
2103 + if (checkDicSize == 0 && processedPos == 0)
2104 + return SZ_ERROR_DATA;
2105 + prob = probs + IsRepG0 + state;
2109 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
2113 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
2116 + state = state < kNumLitStates ? 9 : 11;
2125 + prob = probs + IsRepG1 + state;
2134 + prob = probs + IsRepG2 + state;
2151 + state = state < kNumLitStates ? 8 : 11;
2152 + prob = probs + RepLenCoder;
2155 + unsigned limit, offset;
2156 + CLzmaProb *probLen = prob + LenChoice;
2159 + UPDATE_0(probLen);
2160 + probLen = prob + LenLow + (posState << kLenNumLowBits);
2162 + limit = (1 << kLenNumLowBits);
2166 + UPDATE_1(probLen);
2167 + probLen = prob + LenChoice2;
2170 + UPDATE_0(probLen);
2171 + probLen = prob + LenMid + (posState << kLenNumMidBits);
2172 + offset = kLenNumLowSymbols;
2173 + limit = (1 << kLenNumMidBits);
2177 + UPDATE_1(probLen);
2178 + probLen = prob + LenHigh;
2179 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
2180 + limit = (1 << kLenNumHighBits);
2183 + TREE_DECODE(probLen, limit, len);
2187 + if (state >= kNumStates)
2190 + prob = probs + PosSlot +
2191 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
2192 + TREE_6_DECODE(prob, distance);
2193 + if (distance >= kStartPosModelIndex)
2195 + unsigned posSlot = (unsigned)distance;
2196 + int numDirectBits = (int)(((distance >> 1) - 1));
2197 + distance = (2 | (distance & 1));
2198 + if (posSlot < kEndPosModelIndex)
2200 + distance <<= numDirectBits;
2201 + prob = probs + SpecPos + distance - posSlot - 1;
2207 + GET_BIT2(prob + i, i, ; , distance |= mask);
2210 + while (--numDirectBits != 0);
2215 + numDirectBits -= kNumAlignBits;
2224 + t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
2225 + distance = (distance << 1) + (t + 1);
2226 + code += range & t;
2230 + if (code >= range)
2237 + while (--numDirectBits != 0);
2238 + prob = probs + Align;
2239 + distance <<= kNumAlignBits;
2242 + GET_BIT2(prob + i, i, ; , distance |= 1);
2243 + GET_BIT2(prob + i, i, ; , distance |= 2);
2244 + GET_BIT2(prob + i, i, ; , distance |= 4);
2245 + GET_BIT2(prob + i, i, ; , distance |= 8);
2247 + if (distance == (UInt32)0xFFFFFFFF)
2249 + len += kMatchSpecLenStart;
2250 + state -= kNumStates;
2258 + rep0 = distance + 1;
2259 + if (checkDicSize == 0)
2261 + if (distance >= processedPos)
2262 + return SZ_ERROR_DATA;
2264 + else if (distance >= checkDicSize)
2265 + return SZ_ERROR_DATA;
2266 + state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
2269 + len += kMatchMinLen;
2271 + if (limit == dicPos)
2272 + return SZ_ERROR_DATA;
2274 + SizeT rem = limit - dicPos;
2275 + unsigned curLen = ((rem < len) ? (unsigned)rem : len);
2276 + SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
2278 + processedPos += curLen;
2281 + if (pos + curLen <= dicBufSize)
2283 + Byte *dest = dic + dicPos;
2284 + ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
2285 + const Byte *lim = dest + curLen;
2288 + *(dest) = (Byte)*(dest + src);
2289 + while (++dest != lim);
2295 + dic[dicPos++] = dic[pos];
2296 + if (++pos == dicBufSize)
2299 + while (--curLen != 0);
2304 + while (dicPos < limit && buf < bufLimit);
2309 + p->remainLen = len;
2310 + p->dicPos = dicPos;
2311 + p->processedPos = processedPos;
2312 + p->reps[0] = rep0;
2313 + p->reps[1] = rep1;
2314 + p->reps[2] = rep2;
2315 + p->reps[3] = rep3;
2321 +static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
2323 + if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
2325 + Byte *dic = p->dic;
2326 + SizeT dicPos = p->dicPos;
2327 + SizeT dicBufSize = p->dicBufSize;
2328 + unsigned len = p->remainLen;
2329 + UInt32 rep0 = p->reps[0];
2330 + if (limit - dicPos < len)
2331 + len = (unsigned)(limit - dicPos);
2333 + if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
2334 + p->checkDicSize = p->prop.dicSize;
2336 + p->processedPos += len;
2337 + p->remainLen -= len;
2338 + while (len-- != 0)
2340 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
2343 + p->dicPos = dicPos;
2347 +static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
2351 + SizeT limit2 = limit;
2352 + if (p->checkDicSize == 0)
2354 + UInt32 rem = p->prop.dicSize - p->processedPos;
2355 + if (limit - p->dicPos > rem)
2356 + limit2 = p->dicPos + rem;
2358 + RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
2359 + if (p->processedPos >= p->prop.dicSize)
2360 + p->checkDicSize = p->prop.dicSize;
2361 + LzmaDec_WriteRem(p, limit);
2363 + while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
2365 + if (p->remainLen > kMatchSpecLenStart)
2367 + p->remainLen = kMatchSpecLenStart;
2374 + DUMMY_ERROR, /* unexpected end of input stream */
2380 +static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
2382 + UInt32 range = p->range;
2383 + UInt32 code = p->code;
2384 + const Byte *bufLimit = buf + inSize;
2385 + CLzmaProb *probs = p->probs;
2386 + unsigned state = p->state;
2393 + unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
2395 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
2396 + IF_BIT_0_CHECK(prob)
2400 + /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
2402 + prob = probs + Literal;
2403 + if (p->checkDicSize != 0 || p->processedPos != 0)
2404 + prob += (LZMA_LIT_SIZE *
2405 + ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
2406 + (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
2408 + if (state < kNumLitStates)
2410 + unsigned symbol = 1;
2411 + do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
2415 + unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
2416 + ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
2417 + unsigned offs = 0x100;
2418 + unsigned symbol = 1;
2422 + CLzmaProb *probLit;
2424 + bit = (matchByte & offs);
2425 + probLit = prob + offs + bit + symbol;
2426 + GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
2428 + while (symbol < 0x100);
2437 + prob = probs + IsRep + state;
2438 + IF_BIT_0_CHECK(prob)
2442 + prob = probs + LenCoder;
2443 + res = DUMMY_MATCH;
2449 + prob = probs + IsRepG0 + state;
2450 + IF_BIT_0_CHECK(prob)
2453 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
2454 + IF_BIT_0_CHECK(prob)
2468 + prob = probs + IsRepG1 + state;
2469 + IF_BIT_0_CHECK(prob)
2476 + prob = probs + IsRepG2 + state;
2477 + IF_BIT_0_CHECK(prob)
2487 + state = kNumStates;
2488 + prob = probs + RepLenCoder;
2491 + unsigned limit, offset;
2492 + CLzmaProb *probLen = prob + LenChoice;
2493 + IF_BIT_0_CHECK(probLen)
2496 + probLen = prob + LenLow + (posState << kLenNumLowBits);
2498 + limit = 1 << kLenNumLowBits;
2503 + probLen = prob + LenChoice2;
2504 + IF_BIT_0_CHECK(probLen)
2507 + probLen = prob + LenMid + (posState << kLenNumMidBits);
2508 + offset = kLenNumLowSymbols;
2509 + limit = 1 << kLenNumMidBits;
2514 + probLen = prob + LenHigh;
2515 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
2516 + limit = 1 << kLenNumHighBits;
2519 + TREE_DECODE_CHECK(probLen, limit, len);
2526 + prob = probs + PosSlot +
2527 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
2529 + TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
2530 + if (posSlot >= kStartPosModelIndex)
2532 + int numDirectBits = ((posSlot >> 1) - 1);
2534 + /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
2536 + if (posSlot < kEndPosModelIndex)
2538 + prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
2542 + numDirectBits -= kNumAlignBits;
2547 + code -= range & (((code - range) >> 31) - 1);
2548 + /* if (code >= range) code -= range; */
2550 + while (--numDirectBits != 0);
2551 + prob = probs + Align;
2552 + numDirectBits = kNumAlignBits;
2558 + GET_BIT_CHECK(prob + i, i);
2560 + while (--numDirectBits != 0);
2571 +static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
2573 + p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
2574 + p->range = 0xFFFFFFFF;
2578 +void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
2582 + p->tempBufSize = 0;
2586 + p->processedPos = 0;
2587 + p->checkDicSize = 0;
2588 + p->needInitState = 1;
2591 + p->needInitState = 1;
2594 +void LzmaDec_Init(CLzmaDec *p)
2597 + LzmaDec_InitDicAndState(p, True, True);
2600 +static void LzmaDec_InitStateReal(CLzmaDec *p)
2602 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
2604 + CLzmaProb *probs = p->probs;
2605 + for (i = 0; i < numProbs; i++)
2606 + probs[i] = kBitModelTotal >> 1;
2607 + p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
2609 + p->needInitState = 0;
2612 +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
2613 + ELzmaFinishMode finishMode, ELzmaStatus *status)
2615 + SizeT inSize = *srcLen;
2617 + LzmaDec_WriteRem(p, dicLimit);
2619 + *status = LZMA_STATUS_NOT_SPECIFIED;
2621 + while (p->remainLen != kMatchSpecLenStart)
2623 + int checkEndMarkNow;
2625 + if (p->needFlush != 0)
2627 + for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
2628 + p->tempBuf[p->tempBufSize++] = *src++;
2629 + if (p->tempBufSize < RC_INIT_SIZE)
2631 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2634 + if (p->tempBuf[0] != 0)
2635 + return SZ_ERROR_DATA;
2637 + LzmaDec_InitRc(p, p->tempBuf);
2638 + p->tempBufSize = 0;
2641 + checkEndMarkNow = 0;
2642 + if (p->dicPos >= dicLimit)
2644 + if (p->remainLen == 0 && p->code == 0)
2646 + *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
2649 + if (finishMode == LZMA_FINISH_ANY)
2651 + *status = LZMA_STATUS_NOT_FINISHED;
2654 + if (p->remainLen != 0)
2656 + *status = LZMA_STATUS_NOT_FINISHED;
2657 + return SZ_ERROR_DATA;
2659 + checkEndMarkNow = 1;
2662 + if (p->needInitState)
2663 + LzmaDec_InitStateReal(p);
2665 + if (p->tempBufSize == 0)
2668 + const Byte *bufLimit;
2669 + if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2671 + int dummyRes = LzmaDec_TryDummy(p, src, inSize);
2672 + if (dummyRes == DUMMY_ERROR)
2674 + memcpy(p->tempBuf, src, inSize);
2675 + p->tempBufSize = (unsigned)inSize;
2676 + (*srcLen) += inSize;
2677 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2680 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2682 + *status = LZMA_STATUS_NOT_FINISHED;
2683 + return SZ_ERROR_DATA;
2688 + bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
2690 + if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
2691 + return SZ_ERROR_DATA;
2692 + processed = (SizeT)(p->buf - src);
2693 + (*srcLen) += processed;
2695 + inSize -= processed;
2699 + unsigned rem = p->tempBufSize, lookAhead = 0;
2700 + while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
2701 + p->tempBuf[rem++] = src[lookAhead++];
2702 + p->tempBufSize = rem;
2703 + if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2705 + int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
2706 + if (dummyRes == DUMMY_ERROR)
2708 + (*srcLen) += lookAhead;
2709 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2712 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2714 + *status = LZMA_STATUS_NOT_FINISHED;
2715 + return SZ_ERROR_DATA;
2718 + p->buf = p->tempBuf;
2719 + if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
2720 + return SZ_ERROR_DATA;
2721 + lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
2722 + (*srcLen) += lookAhead;
2724 + inSize -= lookAhead;
2725 + p->tempBufSize = 0;
2729 + *status = LZMA_STATUS_FINISHED_WITH_MARK;
2730 + return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
2733 +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
2735 + SizeT outSize = *destLen;
2736 + SizeT inSize = *srcLen;
2737 + *srcLen = *destLen = 0;
2740 + SizeT inSizeCur = inSize, outSizeCur, dicPos;
2741 + ELzmaFinishMode curFinishMode;
2743 + if (p->dicPos == p->dicBufSize)
2745 + dicPos = p->dicPos;
2746 + if (outSize > p->dicBufSize - dicPos)
2748 + outSizeCur = p->dicBufSize;
2749 + curFinishMode = LZMA_FINISH_ANY;
2753 + outSizeCur = dicPos + outSize;
2754 + curFinishMode = finishMode;
2757 + res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
2759 + inSize -= inSizeCur;
2760 + *srcLen += inSizeCur;
2761 + outSizeCur = p->dicPos - dicPos;
2762 + memcpy(dest, p->dic + dicPos, outSizeCur);
2763 + dest += outSizeCur;
2764 + outSize -= outSizeCur;
2765 + *destLen += outSizeCur;
2768 + if (outSizeCur == 0 || outSize == 0)
2773 +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
2775 + alloc->Free(alloc, p->probs);
2779 +static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
2781 + alloc->Free(alloc, p->dic);
2785 +void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
2787 + LzmaDec_FreeProbs(p, alloc);
2788 + LzmaDec_FreeDict(p, alloc);
2791 +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
2796 + if (size < LZMA_PROPS_SIZE)
2797 + return SZ_ERROR_UNSUPPORTED;
2799 + dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
2801 + if (dicSize < LZMA_DIC_MIN)
2802 + dicSize = LZMA_DIC_MIN;
2803 + p->dicSize = dicSize;
2806 + if (d >= (9 * 5 * 5))
2807 + return SZ_ERROR_UNSUPPORTED;
2817 +static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
2819 + UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
2820 + if (p->probs == 0 || numProbs != p->numProbs)
2822 + LzmaDec_FreeProbs(p, alloc);
2823 + p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
2824 + p->numProbs = numProbs;
2825 + if (p->probs == 0)
2826 + return SZ_ERROR_MEM;
2831 +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2833 + CLzmaProps propNew;
2834 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2835 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2836 + p->prop = propNew;
2840 +SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2842 + CLzmaProps propNew;
2844 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2845 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2846 + dicBufSize = propNew.dicSize;
2847 + if (p->dic == 0 || dicBufSize != p->dicBufSize)
2849 + LzmaDec_FreeDict(p, alloc);
2850 + p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
2853 + LzmaDec_FreeProbs(p, alloc);
2854 + return SZ_ERROR_MEM;
2857 + p->dicBufSize = dicBufSize;
2858 + p->prop = propNew;
2862 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
2863 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
2864 + ELzmaStatus *status, ISzAlloc *alloc)
2868 + SizeT inSize = *srcLen;
2869 + SizeT outSize = *destLen;
2870 + *srcLen = *destLen = 0;
2871 + if (inSize < RC_INIT_SIZE)
2872 + return SZ_ERROR_INPUT_EOF;
2874 + LzmaDec_Construct(&p);
2875 + res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
2879 + p.dicBufSize = outSize;
2884 + res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
2886 + if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
2887 + res = SZ_ERROR_INPUT_EOF;
2889 + (*destLen) = p.dicPos;
2890 + LzmaDec_FreeProbs(&p, alloc);
2893 Index: linux-2.6.38-rc6/lib/lzma/LzmaEnc.c
2894 ===================================================================
2895 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2896 +++ linux-2.6.38-rc6/lib/lzma/LzmaEnc.c 2011-02-28 16:14:14.485424605 +0100
2898 +/* LzmaEnc.c -- LZMA Encoder
2899 +2009-11-24 : Igor Pavlov : Public domain */
2901 +#include <string.h>
2903 +/* #define SHOW_STAT */
2904 +/* #define SHOW_STAT2 */
2906 +#if defined(SHOW_STAT) || defined(SHOW_STAT2)
2910 +#include "LzmaEnc.h"
2915 +#include "LzFind.h"
2917 +#include "LzFindMt.h"
2921 +static int ttt = 0;
2924 +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
2926 +#define kBlockSize (9 << 10)
2927 +#define kUnpackBlockSize (1 << 18)
2928 +#define kMatchArraySize (1 << 21)
2929 +#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
2931 +#define kNumMaxDirectBits (31)
2933 +#define kNumTopBits 24
2934 +#define kTopValue ((UInt32)1 << kNumTopBits)
2936 +#define kNumBitModelTotalBits 11
2937 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
2938 +#define kNumMoveBits 5
2939 +#define kProbInitValue (kBitModelTotal >> 1)
2941 +#define kNumMoveReducingBits 4
2942 +#define kNumBitPriceShiftBits 4
2943 +#define kBitPrice (1 << kNumBitPriceShiftBits)
2945 +void LzmaEncProps_Init(CLzmaEncProps *p)
2948 + p->dictSize = p->mc = 0;
2949 + p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
2950 + p->writeEndMark = 0;
2953 +void LzmaEncProps_Normalize(CLzmaEncProps *p)
2955 + int level = p->level;
2956 + if (level < 0) level = 5;
2958 + if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
2959 + if (p->lc < 0) p->lc = 3;
2960 + if (p->lp < 0) p->lp = 0;
2961 + if (p->pb < 0) p->pb = 2;
2962 + if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
2963 + if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
2964 + if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
2965 + if (p->numHashBytes < 0) p->numHashBytes = 4;
2966 + if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
2967 + if (p->numThreads < 0)
2970 + ((p->btMode && p->algo) ? 2 : 1);
2976 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
2978 + CLzmaEncProps props = *props2;
2979 + LzmaEncProps_Normalize(&props);
2980 + return props.dictSize;
2983 +/* #define LZMA_LOG_BSR */
2984 +/* Define it for Intel's CPU */
2987 +#ifdef LZMA_LOG_BSR
2989 +#define kDicLogSizeMaxCompress 30
2991 +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
2993 +UInt32 GetPosSlot1(UInt32 pos)
2996 + BSR2_RET(pos, res);
2999 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
3000 +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
3004 +#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
3005 +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
3007 +void LzmaEnc_FastPosInit(Byte *g_FastPos)
3009 + int c = 2, slotFast;
3013 + for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
3015 + UInt32 k = (1 << ((slotFast >> 1) - 1));
3017 + for (j = 0; j < k; j++, c++)
3018 + g_FastPos[c] = (Byte)slotFast;
3022 +#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
3023 + (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
3024 + res = p->g_FastPos[pos >> i] + (i * 2); }
3026 +#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
3027 + p->g_FastPos[pos >> 6] + 12 : \
3028 + p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
3031 +#define GetPosSlot1(pos) p->g_FastPos[pos]
3032 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
3033 +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
3038 +#define LZMA_NUM_REPS 4
3040 +typedef unsigned CState;
3055 + UInt32 backs[LZMA_NUM_REPS];
3058 +#define kNumOpts (1 << 12)
3060 +#define kNumLenToPosStates 4
3061 +#define kNumPosSlotBits 6
3062 +#define kDicLogSizeMin 0
3063 +#define kDicLogSizeMax 32
3064 +#define kDistTableSizeMax (kDicLogSizeMax * 2)
3067 +#define kNumAlignBits 4
3068 +#define kAlignTableSize (1 << kNumAlignBits)
3069 +#define kAlignMask (kAlignTableSize - 1)
3071 +#define kStartPosModelIndex 4
3072 +#define kEndPosModelIndex 14
3073 +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
3075 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
3077 +#ifdef _LZMA_PROB32
3078 +#define CLzmaProb UInt32
3080 +#define CLzmaProb UInt16
3083 +#define LZMA_PB_MAX 4
3084 +#define LZMA_LC_MAX 8
3085 +#define LZMA_LP_MAX 4
3087 +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
3090 +#define kLenNumLowBits 3
3091 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
3092 +#define kLenNumMidBits 3
3093 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
3094 +#define kLenNumHighBits 8
3095 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
3097 +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
3099 +#define LZMA_MATCH_LEN_MIN 2
3100 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
3102 +#define kNumStates 12
3107 + CLzmaProb choice2;
3108 + CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
3109 + CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
3110 + CLzmaProb high[kLenNumHighSymbols];
3116 + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
3118 + UInt32 counters[LZMA_NUM_PB_STATES_MAX];
3130 + ISeqOutStream *outStream;
3137 + CLzmaProb *litProbs;
3139 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
3140 + CLzmaProb isRep[kNumStates];
3141 + CLzmaProb isRepG0[kNumStates];
3142 + CLzmaProb isRepG1[kNumStates];
3143 + CLzmaProb isRepG2[kNumStates];
3144 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
3146 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
3147 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
3148 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
3150 + CLenPriceEnc lenEnc;
3151 + CLenPriceEnc repLenEnc;
3153 + UInt32 reps[LZMA_NUM_REPS];
3159 + IMatchFinder matchFinder;
3160 + void *matchFinderObj;
3164 + CMatchFinderMt matchFinderMt;
3167 + CMatchFinder matchFinderBase;
3173 + UInt32 optimumEndIndex;
3174 + UInt32 optimumCurrentIndex;
3176 + UInt32 longestMatchLength;
3179 + COptimal opt[kNumOpts];
3181 + #ifndef LZMA_LOG_BSR
3182 + Byte g_FastPos[1 << kNumLogBits];
3185 + UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
3186 + UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
3187 + UInt32 numFastBytes;
3188 + UInt32 additionalOffset;
3189 + UInt32 reps[LZMA_NUM_REPS];
3192 + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
3193 + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
3194 + UInt32 alignPrices[kAlignTableSize];
3195 + UInt32 alignPriceCount;
3197 + UInt32 distTableSize;
3199 + unsigned lc, lp, pb;
3200 + unsigned lpMask, pbMask;
3202 + CLzmaProb *litProbs;
3204 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
3205 + CLzmaProb isRep[kNumStates];
3206 + CLzmaProb isRepG0[kNumStates];
3207 + CLzmaProb isRepG1[kNumStates];
3208 + CLzmaProb isRepG2[kNumStates];
3209 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
3211 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
3212 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
3213 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
3215 + CLenPriceEnc lenEnc;
3216 + CLenPriceEnc repLenEnc;
3224 + Bool writeEndMark;
3226 + UInt32 matchPriceCount;
3232 + UInt32 matchFinderCycles;
3236 + CSaveState saveState;
3239 +void LzmaEnc_SaveState(CLzmaEncHandle pp)
3241 + CLzmaEnc *p = (CLzmaEnc *)pp;
3242 + CSaveState *dest = &p->saveState;
3244 + dest->lenEnc = p->lenEnc;
3245 + dest->repLenEnc = p->repLenEnc;
3246 + dest->state = p->state;
3248 + for (i = 0; i < kNumStates; i++)
3250 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
3251 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
3253 + for (i = 0; i < kNumLenToPosStates; i++)
3254 + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
3255 + memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
3256 + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
3257 + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
3258 + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
3259 + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
3260 + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
3261 + memcpy(dest->reps, p->reps, sizeof(p->reps));
3262 + memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
3265 +void LzmaEnc_RestoreState(CLzmaEncHandle pp)
3267 + CLzmaEnc *dest = (CLzmaEnc *)pp;
3268 + const CSaveState *p = &dest->saveState;
3270 + dest->lenEnc = p->lenEnc;
3271 + dest->repLenEnc = p->repLenEnc;
3272 + dest->state = p->state;
3274 + for (i = 0; i < kNumStates; i++)
3276 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
3277 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
3279 + for (i = 0; i < kNumLenToPosStates; i++)
3280 + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
3281 + memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
3282 + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
3283 + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
3284 + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
3285 + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
3286 + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
3287 + memcpy(dest->reps, p->reps, sizeof(p->reps));
3288 + memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
3291 +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
3293 + CLzmaEnc *p = (CLzmaEnc *)pp;
3294 + CLzmaEncProps props = *props2;
3295 + LzmaEncProps_Normalize(&props);
3297 + if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
3298 + props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))
3299 + return SZ_ERROR_PARAM;
3300 + p->dictSize = props.dictSize;
3301 + p->matchFinderCycles = props.mc;
3303 + unsigned fb = props.fb;
3306 + if (fb > LZMA_MATCH_LEN_MAX)
3307 + fb = LZMA_MATCH_LEN_MAX;
3308 + p->numFastBytes = fb;
3313 + p->fastMode = (props.algo == 0);
3314 + p->matchFinderBase.btMode = props.btMode;
3316 + UInt32 numHashBytes = 4;
3319 + if (props.numHashBytes < 2)
3321 + else if (props.numHashBytes < 4)
3322 + numHashBytes = props.numHashBytes;
3324 + p->matchFinderBase.numHashBytes = numHashBytes;
3327 + p->matchFinderBase.cutValue = props.mc;
3329 + p->writeEndMark = props.writeEndMark;
3333 + if (newMultiThread != _multiThread)
3335 + ReleaseMatchFinder();
3336 + _multiThread = newMultiThread;
3339 + p->multiThread = (props.numThreads > 1);
3345 +static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
3346 +static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
3347 +static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
3348 +static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
3350 +#define IsCharState(s) ((s) < 7)
3352 +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
3354 +#define kInfinityPrice (1 << 30)
3356 +static void RangeEnc_Construct(CRangeEnc *p)
3362 +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
3364 +#define RC_BUF_SIZE (1 << 16)
3365 +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
3367 + if (p->bufBase == 0)
3369 + p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
3370 + if (p->bufBase == 0)
3372 + p->bufLim = p->bufBase + RC_BUF_SIZE;
3377 +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
3379 + alloc->Free(alloc, p->bufBase);
3383 +static void RangeEnc_Init(CRangeEnc *p)
3385 + /* Stream.Init(); */
3387 + p->range = 0xFFFFFFFF;
3391 + p->buf = p->bufBase;
3397 +static void RangeEnc_FlushStream(CRangeEnc *p)
3400 + if (p->res != SZ_OK)
3402 + num = p->buf - p->bufBase;
3403 + if (num != p->outStream->Write(p->outStream, p->bufBase, num))
3404 + p->res = SZ_ERROR_WRITE;
3405 + p->processed += num;
3406 + p->buf = p->bufBase;
3409 +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
3411 + if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
3413 + Byte temp = p->cache;
3416 + Byte *buf = p->buf;
3417 + *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
3419 + if (buf == p->bufLim)
3420 + RangeEnc_FlushStream(p);
3423 + while (--p->cacheSize != 0);
3424 + p->cache = (Byte)((UInt32)p->low >> 24);
3427 + p->low = (UInt32)p->low << 8;
3430 +static void RangeEnc_FlushData(CRangeEnc *p)
3433 + for (i = 0; i < 5; i++)
3434 + RangeEnc_ShiftLow(p);
3437 +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
3442 + p->low += p->range & (0 - ((value >> --numBits) & 1));
3443 + if (p->range < kTopValue)
3446 + RangeEnc_ShiftLow(p);
3449 + while (numBits != 0);
3452 +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
3454 + UInt32 ttt = *prob;
3455 + UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
3458 + p->range = newBound;
3459 + ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
3463 + p->low += newBound;
3464 + p->range -= newBound;
3465 + ttt -= ttt >> kNumMoveBits;
3467 + *prob = (CLzmaProb)ttt;
3468 + if (p->range < kTopValue)
3471 + RangeEnc_ShiftLow(p);
3475 +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
3480 + RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
3483 + while (symbol < 0x10000);
3486 +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
3488 + UInt32 offs = 0x100;
3493 + RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
3495 + offs &= ~(matchByte ^ symbol);
3497 + while (symbol < 0x10000);
3500 +void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
3503 + for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
3505 + const int kCyclesBits = kNumBitPriceShiftBits;
3507 + UInt32 bitCount = 0;
3509 + for (j = 0; j < kCyclesBits; j++)
3513 + while (w >= ((UInt32)1 << 16))
3519 + ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
3524 +#define GET_PRICE(prob, symbol) \
3525 + p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3527 +#define GET_PRICEa(prob, symbol) \
3528 + ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3530 +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
3531 +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3533 +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
3534 +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3536 +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
3542 + price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
3545 + while (symbol < 0x10000);
3549 +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
3552 + UInt32 offs = 0x100;
3557 + price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
3559 + offs &= ~(matchByte ^ symbol);
3561 + while (symbol < 0x10000);
3566 +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3570 + for (i = numBitLevels; i != 0;)
3574 + bit = (symbol >> i) & 1;
3575 + RangeEnc_EncodeBit(rc, probs + m, bit);
3576 + m = (m << 1) | bit;
3580 +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3584 + for (i = 0; i < numBitLevels; i++)
3586 + UInt32 bit = symbol & 1;
3587 + RangeEnc_EncodeBit(rc, probs + m, bit);
3588 + m = (m << 1) | bit;
3593 +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3596 + symbol |= (1 << numBitLevels);
3597 + while (symbol != 1)
3599 + price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
3605 +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3610 + for (i = numBitLevels; i != 0; i--)
3612 + UInt32 bit = symbol & 1;
3614 + price += GET_PRICEa(probs[m], bit);
3615 + m = (m << 1) | bit;
3621 +static void LenEnc_Init(CLenEnc *p)
3624 + p->choice = p->choice2 = kProbInitValue;
3625 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
3626 + p->low[i] = kProbInitValue;
3627 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
3628 + p->mid[i] = kProbInitValue;
3629 + for (i = 0; i < kLenNumHighSymbols; i++)
3630 + p->high[i] = kProbInitValue;
3633 +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
3635 + if (symbol < kLenNumLowSymbols)
3637 + RangeEnc_EncodeBit(rc, &p->choice, 0);
3638 + RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
3642 + RangeEnc_EncodeBit(rc, &p->choice, 1);
3643 + if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
3645 + RangeEnc_EncodeBit(rc, &p->choice2, 0);
3646 + RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
3650 + RangeEnc_EncodeBit(rc, &p->choice2, 1);
3651 + RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
3656 +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
3658 + UInt32 a0 = GET_PRICE_0a(p->choice);
3659 + UInt32 a1 = GET_PRICE_1a(p->choice);
3660 + UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
3661 + UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
3663 + for (i = 0; i < kLenNumLowSymbols; i++)
3665 + if (i >= numSymbols)
3667 + prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
3669 + for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
3671 + if (i >= numSymbols)
3673 + prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
3675 + for (; i < numSymbols; i++)
3676 + prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
3679 +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
3681 + LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
3682 + p->counters[posState] = p->tableSize;
3685 +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
3688 + for (posState = 0; posState < numPosStates; posState++)
3689 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3692 +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
3694 + LenEnc_Encode(&p->p, rc, symbol, posState);
3696 + if (--p->counters[posState] == 0)
3697 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3703 +static void MovePos(CLzmaEnc *p, UInt32 num)
3707 + printf("\n MovePos %d", num);
3711 + p->additionalOffset += num;
3712 + p->matchFinder.Skip(p->matchFinderObj, num);
3716 +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
3718 + UInt32 lenRes = 0, numPairs;
3719 + p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
3720 + numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
3722 + printf("\n i = %d numPairs = %d ", ttt, numPairs / 2);
3726 + for (i = 0; i < numPairs; i += 2)
3727 + printf("%2d %6d | ", p->matches[i], p->matches[i + 1]);
3732 + lenRes = p->matches[numPairs - 2];
3733 + if (lenRes == p->numFastBytes)
3735 + const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3736 + UInt32 distance = p->matches[numPairs - 1] + 1;
3737 + UInt32 numAvail = p->numAvail;
3738 + if (numAvail > LZMA_MATCH_LEN_MAX)
3739 + numAvail = LZMA_MATCH_LEN_MAX;
3741 + const Byte *pby2 = pby - distance;
3742 + for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
3746 + p->additionalOffset++;
3747 + *numDistancePairsRes = numPairs;
3752 +#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
3753 +#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
3754 +#define IsShortRep(p) ((p)->backPrev == 0)
3756 +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
3759 + GET_PRICE_0(p->isRepG0[state]) +
3760 + GET_PRICE_0(p->isRep0Long[state][posState]);
3763 +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
3766 + if (repIndex == 0)
3768 + price = GET_PRICE_0(p->isRepG0[state]);
3769 + price += GET_PRICE_1(p->isRep0Long[state][posState]);
3773 + price = GET_PRICE_1(p->isRepG0[state]);
3774 + if (repIndex == 1)
3775 + price += GET_PRICE_0(p->isRepG1[state]);
3778 + price += GET_PRICE_1(p->isRepG1[state]);
3779 + price += GET_PRICE(p->isRepG2[state], repIndex - 2);
3785 +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
3787 + return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
3788 + GetPureRepPrice(p, repIndex, state, posState);
3791 +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
3793 + UInt32 posMem = p->opt[cur].posPrev;
3794 + UInt32 backMem = p->opt[cur].backPrev;
3795 + p->optimumEndIndex = cur;
3798 + if (p->opt[cur].prev1IsChar)
3800 + MakeAsChar(&p->opt[posMem])
3801 + p->opt[posMem].posPrev = posMem - 1;
3802 + if (p->opt[cur].prev2)
3804 + p->opt[posMem - 1].prev1IsChar = False;
3805 + p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
3806 + p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
3810 + UInt32 posPrev = posMem;
3811 + UInt32 backCur = backMem;
3813 + backMem = p->opt[posPrev].backPrev;
3814 + posMem = p->opt[posPrev].posPrev;
3816 + p->opt[posPrev].backPrev = backCur;
3817 + p->opt[posPrev].posPrev = cur;
3822 + *backRes = p->opt[0].backPrev;
3823 + p->optimumCurrentIndex = p->opt[0].posPrev;
3824 + return p->optimumCurrentIndex;
3827 +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
3829 +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
3831 + UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;
3832 + UInt32 matchPrice, repMatchPrice, normalMatchPrice;
3833 + UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
3836 + Byte curByte, matchByte;
3837 + if (p->optimumEndIndex != p->optimumCurrentIndex)
3839 + const COptimal *opt = &p->opt[p->optimumCurrentIndex];
3840 + UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
3841 + *backRes = opt->backPrev;
3842 + p->optimumCurrentIndex = opt->posPrev;
3845 + p->optimumCurrentIndex = p->optimumEndIndex = 0;
3847 + if (p->additionalOffset == 0)
3848 + mainLen = ReadMatchDistances(p, &numPairs);
3851 + mainLen = p->longestMatchLength;
3852 + numPairs = p->numPairs;
3855 + numAvail = p->numAvail;
3858 + *backRes = (UInt32)(-1);
3861 + if (numAvail > LZMA_MATCH_LEN_MAX)
3862 + numAvail = LZMA_MATCH_LEN_MAX;
3864 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3866 + for (i = 0; i < LZMA_NUM_REPS; i++)
3869 + const Byte *data2;
3870 + reps[i] = p->reps[i];
3871 + data2 = data - (reps[i] + 1);
3872 + if (data[0] != data2[0] || data[1] != data2[1])
3877 + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
3878 + repLens[i] = lenTest;
3879 + if (lenTest > repLens[repMaxIndex])
3882 + if (repLens[repMaxIndex] >= p->numFastBytes)
3885 + *backRes = repMaxIndex;
3886 + lenRes = repLens[repMaxIndex];
3887 + MovePos(p, lenRes - 1);
3891 + matches = p->matches;
3892 + if (mainLen >= p->numFastBytes)
3894 + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
3895 + MovePos(p, mainLen - 1);
3899 + matchByte = *(data - (reps[0] + 1));
3901 + if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2)
3903 + *backRes = (UInt32)-1;
3907 + p->opt[0].state = (CState)p->state;
3909 + posState = (position & p->pbMask);
3912 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
3913 + p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
3914 + (!IsCharState(p->state) ?
3915 + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
3916 + LitEnc_GetPrice(probs, curByte, p->ProbPrices));
3919 + MakeAsChar(&p->opt[1]);
3921 + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
3922 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
3924 + if (matchByte == curByte)
3926 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
3927 + if (shortRepPrice < p->opt[1].price)
3929 + p->opt[1].price = shortRepPrice;
3930 + MakeAsShortRep(&p->opt[1]);
3933 + lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
3937 + *backRes = p->opt[1].backPrev;
3941 + p->opt[1].posPrev = 0;
3942 + for (i = 0; i < LZMA_NUM_REPS; i++)
3943 + p->opt[0].backs[i] = reps[i];
3947 + p->opt[len--].price = kInfinityPrice;
3950 + for (i = 0; i < LZMA_NUM_REPS; i++)
3952 + UInt32 repLen = repLens[i];
3956 + price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
3959 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
3960 + COptimal *opt = &p->opt[repLen];
3961 + if (curAndLenPrice < opt->price)
3963 + opt->price = curAndLenPrice;
3965 + opt->backPrev = i;
3966 + opt->prev1IsChar = False;
3969 + while (--repLen >= 2);
3972 + normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
3974 + len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
3975 + if (len <= mainLen)
3978 + while (len > matches[offs])
3983 + UInt32 distance = matches[offs + 1];
3985 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
3986 + UInt32 lenToPosState = GetLenToPosState(len);
3987 + if (distance < kNumFullDistances)
3988 + curAndLenPrice += p->distancesPrices[lenToPosState][distance];
3992 + GetPosSlot2(distance, slot);
3993 + curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
3995 + opt = &p->opt[len];
3996 + if (curAndLenPrice < opt->price)
3998 + opt->price = curAndLenPrice;
4000 + opt->backPrev = distance + LZMA_NUM_REPS;
4001 + opt->prev1IsChar = False;
4003 + if (len == matches[offs])
4006 + if (offs == numPairs)
4015 + if (position >= 0)
4018 + printf("\n pos = %4X", position);
4019 + for (i = cur; i <= lenEnd; i++)
4020 + printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
4026 + UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen;
4027 + UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice;
4029 + Byte curByte, matchByte;
4032 + COptimal *nextOpt;
4035 + if (cur == lenEnd)
4036 + return Backward(p, backRes, cur);
4038 + newLen = ReadMatchDistances(p, &numPairs);
4039 + if (newLen >= p->numFastBytes)
4041 + p->numPairs = numPairs;
4042 + p->longestMatchLength = newLen;
4043 + return Backward(p, backRes, cur);
4046 + curOpt = &p->opt[cur];
4047 + posPrev = curOpt->posPrev;
4048 + if (curOpt->prev1IsChar)
4051 + if (curOpt->prev2)
4053 + state = p->opt[curOpt->posPrev2].state;
4054 + if (curOpt->backPrev2 < LZMA_NUM_REPS)
4055 + state = kRepNextStates[state];
4057 + state = kMatchNextStates[state];
4060 + state = p->opt[posPrev].state;
4061 + state = kLiteralNextStates[state];
4064 + state = p->opt[posPrev].state;
4065 + if (posPrev == cur - 1)
4067 + if (IsShortRep(curOpt))
4068 + state = kShortRepNextStates[state];
4070 + state = kLiteralNextStates[state];
4075 + const COptimal *prevOpt;
4076 + if (curOpt->prev1IsChar && curOpt->prev2)
4078 + posPrev = curOpt->posPrev2;
4079 + pos = curOpt->backPrev2;
4080 + state = kRepNextStates[state];
4084 + pos = curOpt->backPrev;
4085 + if (pos < LZMA_NUM_REPS)
4086 + state = kRepNextStates[state];
4088 + state = kMatchNextStates[state];
4090 + prevOpt = &p->opt[posPrev];
4091 + if (pos < LZMA_NUM_REPS)
4094 + reps[0] = prevOpt->backs[pos];
4095 + for (i = 1; i <= pos; i++)
4096 + reps[i] = prevOpt->backs[i - 1];
4097 + for (; i < LZMA_NUM_REPS; i++)
4098 + reps[i] = prevOpt->backs[i];
4103 + reps[0] = (pos - LZMA_NUM_REPS);
4104 + for (i = 1; i < LZMA_NUM_REPS; i++)
4105 + reps[i] = prevOpt->backs[i - 1];
4108 + curOpt->state = (CState)state;
4110 + curOpt->backs[0] = reps[0];
4111 + curOpt->backs[1] = reps[1];
4112 + curOpt->backs[2] = reps[2];
4113 + curOpt->backs[3] = reps[3];
4115 + curPrice = curOpt->price;
4116 + nextIsChar = False;
4117 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
4119 + matchByte = *(data - (reps[0] + 1));
4121 + posState = (position & p->pbMask);
4123 + curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
4125 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
4127 + (!IsCharState(state) ?
4128 + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
4129 + LitEnc_GetPrice(probs, curByte, p->ProbPrices));
4132 + nextOpt = &p->opt[cur + 1];
4134 + if (curAnd1Price < nextOpt->price)
4136 + nextOpt->price = curAnd1Price;
4137 + nextOpt->posPrev = cur;
4138 + MakeAsChar(nextOpt);
4139 + nextIsChar = True;
4142 + matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
4143 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
4145 + if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
4147 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
4148 + if (shortRepPrice <= nextOpt->price)
4150 + nextOpt->price = shortRepPrice;
4151 + nextOpt->posPrev = cur;
4152 + MakeAsShortRep(nextOpt);
4153 + nextIsChar = True;
4156 + numAvailFull = p->numAvail;
4158 + UInt32 temp = kNumOpts - 1 - cur;
4159 + if (temp < numAvailFull)
4160 + numAvailFull = temp;
4163 + if (numAvailFull < 2)
4165 + numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
4167 + if (!nextIsChar && matchByte != curByte) /* speed optimization */
4169 + /* try Literal + rep0 */
4172 + const Byte *data2 = data - (reps[0] + 1);
4173 + UInt32 limit = p->numFastBytes + 1;
4174 + if (limit > numAvailFull)
4175 + limit = numAvailFull;
4177 + for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
4178 + lenTest2 = temp - 1;
4179 + if (lenTest2 >= 2)
4181 + UInt32 state2 = kLiteralNextStates[state];
4182 + UInt32 posStateNext = (position + 1) & p->pbMask;
4183 + UInt32 nextRepMatchPrice = curAnd1Price +
4184 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4185 + GET_PRICE_1(p->isRep[state2]);
4186 + /* for (; lenTest2 >= 2; lenTest2--) */
4188 + UInt32 curAndLenPrice;
4190 + UInt32 offset = cur + 1 + lenTest2;
4191 + while (lenEnd < offset)
4192 + p->opt[++lenEnd].price = kInfinityPrice;
4193 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4194 + opt = &p->opt[offset];
4195 + if (curAndLenPrice < opt->price)
4197 + opt->price = curAndLenPrice;
4198 + opt->posPrev = cur + 1;
4199 + opt->backPrev = 0;
4200 + opt->prev1IsChar = True;
4201 + opt->prev2 = False;
4207 + startLen = 2; /* speed optimization */
4210 + for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
4213 + UInt32 lenTestTemp;
4215 + const Byte *data2 = data - (reps[repIndex] + 1);
4216 + if (data[0] != data2[0] || data[1] != data2[1])
4218 + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
4219 + while (lenEnd < cur + lenTest)
4220 + p->opt[++lenEnd].price = kInfinityPrice;
4221 + lenTestTemp = lenTest;
4222 + price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
4225 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
4226 + COptimal *opt = &p->opt[cur + lenTest];
4227 + if (curAndLenPrice < opt->price)
4229 + opt->price = curAndLenPrice;
4230 + opt->posPrev = cur;
4231 + opt->backPrev = repIndex;
4232 + opt->prev1IsChar = False;
4235 + while (--lenTest >= 2);
4236 + lenTest = lenTestTemp;
4238 + if (repIndex == 0)
4239 + startLen = lenTest + 1;
4241 + /* if (_maxMode) */
4243 + UInt32 lenTest2 = lenTest + 1;
4244 + UInt32 limit = lenTest2 + p->numFastBytes;
4245 + UInt32 nextRepMatchPrice;
4246 + if (limit > numAvailFull)
4247 + limit = numAvailFull;
4248 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
4249 + lenTest2 -= lenTest + 1;
4250 + if (lenTest2 >= 2)
4252 + UInt32 state2 = kRepNextStates[state];
4253 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
4254 + UInt32 curAndLenCharPrice =
4255 + price + p->repLenEnc.prices[posState][lenTest - 2] +
4256 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
4257 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
4258 + data[lenTest], data2[lenTest], p->ProbPrices);
4259 + state2 = kLiteralNextStates[state2];
4260 + posStateNext = (position + lenTest + 1) & p->pbMask;
4261 + nextRepMatchPrice = curAndLenCharPrice +
4262 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4263 + GET_PRICE_1(p->isRep[state2]);
4265 + /* for (; lenTest2 >= 2; lenTest2--) */
4267 + UInt32 curAndLenPrice;
4269 + UInt32 offset = cur + lenTest + 1 + lenTest2;
4270 + while (lenEnd < offset)
4271 + p->opt[++lenEnd].price = kInfinityPrice;
4272 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4273 + opt = &p->opt[offset];
4274 + if (curAndLenPrice < opt->price)
4276 + opt->price = curAndLenPrice;
4277 + opt->posPrev = cur + lenTest + 1;
4278 + opt->backPrev = 0;
4279 + opt->prev1IsChar = True;
4280 + opt->prev2 = True;
4281 + opt->posPrev2 = cur;
4282 + opt->backPrev2 = repIndex;
4289 + /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
4290 + if (newLen > numAvail)
4292 + newLen = numAvail;
4293 + for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2);
4294 + matches[numPairs] = newLen;
4297 + if (newLen >= startLen)
4299 + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
4300 + UInt32 offs, curBack, posSlot;
4302 + while (lenEnd < cur + newLen)
4303 + p->opt[++lenEnd].price = kInfinityPrice;
4306 + while (startLen > matches[offs])
4308 + curBack = matches[offs + 1];
4309 + GetPosSlot2(curBack, posSlot);
4310 + for (lenTest = /*2*/ startLen; ; lenTest++)
4312 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
4313 + UInt32 lenToPosState = GetLenToPosState(lenTest);
4315 + if (curBack < kNumFullDistances)
4316 + curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
4318 + curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
4320 + opt = &p->opt[cur + lenTest];
4321 + if (curAndLenPrice < opt->price)
4323 + opt->price = curAndLenPrice;
4324 + opt->posPrev = cur;
4325 + opt->backPrev = curBack + LZMA_NUM_REPS;
4326 + opt->prev1IsChar = False;
4329 + if (/*_maxMode && */lenTest == matches[offs])
4331 + /* Try Match + Literal + Rep0 */
4332 + const Byte *data2 = data - (curBack + 1);
4333 + UInt32 lenTest2 = lenTest + 1;
4334 + UInt32 limit = lenTest2 + p->numFastBytes;
4335 + UInt32 nextRepMatchPrice;
4336 + if (limit > numAvailFull)
4337 + limit = numAvailFull;
4338 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
4339 + lenTest2 -= lenTest + 1;
4340 + if (lenTest2 >= 2)
4342 + UInt32 state2 = kMatchNextStates[state];
4343 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
4344 + UInt32 curAndLenCharPrice = curAndLenPrice +
4345 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
4346 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
4347 + data[lenTest], data2[lenTest], p->ProbPrices);
4348 + state2 = kLiteralNextStates[state2];
4349 + posStateNext = (posStateNext + 1) & p->pbMask;
4350 + nextRepMatchPrice = curAndLenCharPrice +
4351 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4352 + GET_PRICE_1(p->isRep[state2]);
4354 + /* for (; lenTest2 >= 2; lenTest2--) */
4356 + UInt32 offset = cur + lenTest + 1 + lenTest2;
4357 + UInt32 curAndLenPrice;
4359 + while (lenEnd < offset)
4360 + p->opt[++lenEnd].price = kInfinityPrice;
4361 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4362 + opt = &p->opt[offset];
4363 + if (curAndLenPrice < opt->price)
4365 + opt->price = curAndLenPrice;
4366 + opt->posPrev = cur + lenTest + 1;
4367 + opt->backPrev = 0;
4368 + opt->prev1IsChar = True;
4369 + opt->prev2 = True;
4370 + opt->posPrev2 = cur;
4371 + opt->backPrev2 = curBack + LZMA_NUM_REPS;
4376 + if (offs == numPairs)
4378 + curBack = matches[offs + 1];
4379 + if (curBack >= kNumFullDistances)
4380 + GetPosSlot2(curBack, posSlot);
4387 +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
4389 +static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
4391 + UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
4393 + const UInt32 *matches;
4395 + if (p->additionalOffset == 0)
4396 + mainLen = ReadMatchDistances(p, &numPairs);
4399 + mainLen = p->longestMatchLength;
4400 + numPairs = p->numPairs;
4403 + numAvail = p->numAvail;
4404 + *backRes = (UInt32)-1;
4407 + if (numAvail > LZMA_MATCH_LEN_MAX)
4408 + numAvail = LZMA_MATCH_LEN_MAX;
4409 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
4411 + repLen = repIndex = 0;
4412 + for (i = 0; i < LZMA_NUM_REPS; i++)
4415 + const Byte *data2 = data - (p->reps[i] + 1);
4416 + if (data[0] != data2[0] || data[1] != data2[1])
4418 + for (len = 2; len < numAvail && data[len] == data2[len]; len++);
4419 + if (len >= p->numFastBytes)
4422 + MovePos(p, len - 1);
4432 + matches = p->matches;
4433 + if (mainLen >= p->numFastBytes)
4435 + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
4436 + MovePos(p, mainLen - 1);
4440 + mainDist = 0; /* for GCC */
4443 + mainDist = matches[numPairs - 1];
4444 + while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
4446 + if (!ChangePair(matches[numPairs - 3], mainDist))
4449 + mainLen = matches[numPairs - 2];
4450 + mainDist = matches[numPairs - 1];
4452 + if (mainLen == 2 && mainDist >= 0x80)
4456 + if (repLen >= 2 && (
4457 + (repLen + 1 >= mainLen) ||
4458 + (repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
4459 + (repLen + 3 >= mainLen && mainDist >= (1 << 15))))
4461 + *backRes = repIndex;
4462 + MovePos(p, repLen - 1);
4466 + if (mainLen < 2 || numAvail <= 2)
4469 + p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
4470 + if (p->longestMatchLength >= 2)
4472 + UInt32 newDistance = matches[p->numPairs - 1];
4473 + if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
4474 + (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
4475 + (p->longestMatchLength > mainLen + 1) ||
4476 + (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
4480 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
4481 + for (i = 0; i < LZMA_NUM_REPS; i++)
4483 + UInt32 len, limit;
4484 + const Byte *data2 = data - (p->reps[i] + 1);
4485 + if (data[0] != data2[0] || data[1] != data2[1])
4487 + limit = mainLen - 1;
4488 + for (len = 2; len < limit && data[len] == data2[len]; len++);
4492 + *backRes = mainDist + LZMA_NUM_REPS;
4493 + MovePos(p, mainLen - 2);
4497 +static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
4500 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4501 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4502 + p->state = kMatchNextStates[p->state];
4503 + len = LZMA_MATCH_LEN_MIN;
4504 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4505 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
4506 + RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
4507 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
4510 +static SRes CheckErrors(CLzmaEnc *p)
4512 + if (p->result != SZ_OK)
4514 + if (p->rc.res != SZ_OK)
4515 + p->result = SZ_ERROR_WRITE;
4516 + if (p->matchFinderBase.result != SZ_OK)
4517 + p->result = SZ_ERROR_READ;
4518 + if (p->result != SZ_OK)
4519 + p->finished = True;
4523 +static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
4525 + /* ReleaseMFStream(); */
4526 + p->finished = True;
4527 + if (p->writeEndMark)
4528 + WriteEndMarker(p, nowPos & p->pbMask);
4529 + RangeEnc_FlushData(&p->rc);
4530 + RangeEnc_FlushStream(&p->rc);
4531 + return CheckErrors(p);
4534 +static void FillAlignPrices(CLzmaEnc *p)
4537 + for (i = 0; i < kAlignTableSize; i++)
4538 + p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
4539 + p->alignPriceCount = 0;
4542 +static void FillDistancesPrices(CLzmaEnc *p)
4544 + UInt32 tempPrices[kNumFullDistances];
4545 + UInt32 i, lenToPosState;
4546 + for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
4548 + UInt32 posSlot = GetPosSlot1(i);
4549 + UInt32 footerBits = ((posSlot >> 1) - 1);
4550 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4551 + tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
4554 + for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
4557 + const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
4558 + UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
4559 + for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
4560 + posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
4561 + for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
4562 + posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
4565 + UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
4567 + for (i = 0; i < kStartPosModelIndex; i++)
4568 + distancesPrices[i] = posSlotPrices[i];
4569 + for (; i < kNumFullDistances; i++)
4570 + distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
4573 + p->matchPriceCount = 0;
4576 +void LzmaEnc_Construct(CLzmaEnc *p)
4578 + RangeEnc_Construct(&p->rc);
4579 + MatchFinder_Construct(&p->matchFinderBase);
4581 + MatchFinderMt_Construct(&p->matchFinderMt);
4582 + p->matchFinderMt.MatchFinder = &p->matchFinderBase;
4586 + CLzmaEncProps props;
4587 + LzmaEncProps_Init(&props);
4588 + LzmaEnc_SetProps(p, &props);
4591 + #ifndef LZMA_LOG_BSR
4592 + LzmaEnc_FastPosInit(p->g_FastPos);
4595 + LzmaEnc_InitPriceTables(p->ProbPrices);
4597 + p->saveState.litProbs = 0;
4600 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
4603 + p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
4605 + LzmaEnc_Construct((CLzmaEnc *)p);
4609 +void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
4611 + alloc->Free(alloc, p->litProbs);
4612 + alloc->Free(alloc, p->saveState.litProbs);
4614 + p->saveState.litProbs = 0;
4617 +void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
4620 + MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
4622 + MatchFinder_Free(&p->matchFinderBase, allocBig);
4623 + LzmaEnc_FreeLits(p, alloc);
4624 + RangeEnc_Free(&p->rc, alloc);
4627 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
4629 + LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
4630 + alloc->Free(alloc, p);
4633 +static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
4635 + UInt32 nowPos32, startPos32;
4638 + p->matchFinder.Init(p->matchFinderObj);
4644 + RINOK(CheckErrors(p));
4646 + nowPos32 = (UInt32)p->nowPos64;
4647 + startPos32 = nowPos32;
4649 + if (p->nowPos64 == 0)
4653 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4654 + return Flush(p, nowPos32);
4655 + ReadMatchDistances(p, &numPairs);
4656 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
4657 + p->state = kLiteralNextStates[p->state];
4658 + curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
4659 + LitEnc_Encode(&p->rc, p->litProbs, curByte);
4660 + p->additionalOffset--;
4664 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
4667 + UInt32 pos, len, posState;
4670 + len = GetOptimumFast(p, &pos);
4672 + len = GetOptimum(p, nowPos32, &pos);
4675 + printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
4678 + posState = nowPos32 & p->pbMask;
4679 + if (len == 1 && pos == (UInt32)-1)
4685 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
4686 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
4688 + probs = LIT_PROBS(nowPos32, *(data - 1));
4689 + if (IsCharState(p->state))
4690 + LitEnc_Encode(&p->rc, probs, curByte);
4692 + LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
4693 + p->state = kLiteralNextStates[p->state];
4697 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4698 + if (pos < LZMA_NUM_REPS)
4700 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
4703 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
4704 + RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
4708 + UInt32 distance = p->reps[pos];
4709 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
4711 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
4714 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
4715 + RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
4717 + p->reps[3] = p->reps[2];
4718 + p->reps[2] = p->reps[1];
4720 + p->reps[1] = p->reps[0];
4721 + p->reps[0] = distance;
4724 + p->state = kShortRepNextStates[p->state];
4727 + LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4728 + p->state = kRepNextStates[p->state];
4734 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4735 + p->state = kMatchNextStates[p->state];
4736 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4737 + pos -= LZMA_NUM_REPS;
4738 + GetPosSlot(pos, posSlot);
4739 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
4741 + if (posSlot >= kStartPosModelIndex)
4743 + UInt32 footerBits = ((posSlot >> 1) - 1);
4744 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4745 + UInt32 posReduced = pos - base;
4747 + if (posSlot < kEndPosModelIndex)
4748 + RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
4751 + RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
4752 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
4753 + p->alignPriceCount++;
4756 + p->reps[3] = p->reps[2];
4757 + p->reps[2] = p->reps[1];
4758 + p->reps[1] = p->reps[0];
4760 + p->matchPriceCount++;
4763 + p->additionalOffset -= len;
4765 + if (p->additionalOffset == 0)
4770 + if (p->matchPriceCount >= (1 << 7))
4771 + FillDistancesPrices(p);
4772 + if (p->alignPriceCount >= kAlignTableSize)
4773 + FillAlignPrices(p);
4775 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4777 + processed = nowPos32 - startPos32;
4780 + if (processed + kNumOpts + 300 >= maxUnpackSize ||
4781 + RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
4784 + else if (processed >= (1 << 15))
4786 + p->nowPos64 += nowPos32 - startPos32;
4787 + return CheckErrors(p);
4791 + p->nowPos64 += nowPos32 - startPos32;
4792 + return Flush(p, nowPos32);
4795 +#define kBigHashDicLimit ((UInt32)1 << 24)
4797 +static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4799 + UInt32 beforeSize = kNumOpts;
4801 + if (!RangeEnc_Alloc(&p->rc, alloc))
4802 + return SZ_ERROR_MEM;
4803 + btMode = (p->matchFinderBase.btMode != 0);
4805 + p->mtMode = (p->multiThread && !p->fastMode && btMode);
4809 + unsigned lclp = p->lc + p->lp;
4810 + if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
4812 + LzmaEnc_FreeLits(p, alloc);
4813 + p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
4814 + p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
4815 + if (p->litProbs == 0 || p->saveState.litProbs == 0)
4817 + LzmaEnc_FreeLits(p, alloc);
4818 + return SZ_ERROR_MEM;
4824 + p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
4826 + if (beforeSize + p->dictSize < keepWindowSize)
4827 + beforeSize = keepWindowSize - p->dictSize;
4832 + RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
4833 + p->matchFinderObj = &p->matchFinderMt;
4834 + MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
4839 + if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
4840 + return SZ_ERROR_MEM;
4841 + p->matchFinderObj = &p->matchFinderBase;
4842 + MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
4847 +void LzmaEnc_Init(CLzmaEnc *p)
4851 + for (i = 0 ; i < LZMA_NUM_REPS; i++)
4854 + RangeEnc_Init(&p->rc);
4857 + for (i = 0; i < kNumStates; i++)
4860 + for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
4862 + p->isMatch[i][j] = kProbInitValue;
4863 + p->isRep0Long[i][j] = kProbInitValue;
4865 + p->isRep[i] = kProbInitValue;
4866 + p->isRepG0[i] = kProbInitValue;
4867 + p->isRepG1[i] = kProbInitValue;
4868 + p->isRepG2[i] = kProbInitValue;
4872 + UInt32 num = 0x300 << (p->lp + p->lc);
4873 + for (i = 0; i < num; i++)
4874 + p->litProbs[i] = kProbInitValue;
4878 + for (i = 0; i < kNumLenToPosStates; i++)
4880 + CLzmaProb *probs = p->posSlotEncoder[i];
4882 + for (j = 0; j < (1 << kNumPosSlotBits); j++)
4883 + probs[j] = kProbInitValue;
4887 + for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
4888 + p->posEncoders[i] = kProbInitValue;
4891 + LenEnc_Init(&p->lenEnc.p);
4892 + LenEnc_Init(&p->repLenEnc.p);
4894 + for (i = 0; i < (1 << kNumAlignBits); i++)
4895 + p->posAlignEncoder[i] = kProbInitValue;
4897 + p->optimumEndIndex = 0;
4898 + p->optimumCurrentIndex = 0;
4899 + p->additionalOffset = 0;
4901 + p->pbMask = (1 << p->pb) - 1;
4902 + p->lpMask = (1 << p->lp) - 1;
4905 +void LzmaEnc_InitPrices(CLzmaEnc *p)
4909 + FillDistancesPrices(p);
4910 + FillAlignPrices(p);
4913 + p->lenEnc.tableSize =
4914 + p->repLenEnc.tableSize =
4915 + p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
4916 + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
4917 + LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
4920 +static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4923 + for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
4924 + if (p->dictSize <= ((UInt32)1 << i))
4926 + p->distTableSize = i * 2;
4928 + p->finished = False;
4929 + p->result = SZ_OK;
4930 + RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
4932 + LzmaEnc_InitPrices(p);
4937 +static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream,
4938 + ISzAlloc *alloc, ISzAlloc *allocBig)
4940 + CLzmaEnc *p = (CLzmaEnc *)pp;
4941 + p->matchFinderBase.stream = inStream;
4943 + p->rc.outStream = outStream;
4944 + return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
4947 +SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
4948 + ISeqInStream *inStream, UInt32 keepWindowSize,
4949 + ISzAlloc *alloc, ISzAlloc *allocBig)
4951 + CLzmaEnc *p = (CLzmaEnc *)pp;
4952 + p->matchFinderBase.stream = inStream;
4954 + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
4957 +static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
4959 + p->matchFinderBase.directInput = 1;
4960 + p->matchFinderBase.bufferBase = (Byte *)src;
4961 + p->matchFinderBase.directInputRem = srcLen;
4964 +SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
4965 + UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4967 + CLzmaEnc *p = (CLzmaEnc *)pp;
4968 + LzmaEnc_SetInputBuf(p, src, srcLen);
4971 + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
4974 +void LzmaEnc_Finish(CLzmaEncHandle pp)
4977 + CLzmaEnc *p = (CLzmaEnc *)pp;
4979 + MatchFinderMt_ReleaseStream(&p->matchFinderMt);
4987 + ISeqOutStream funcTable;
4991 +} CSeqOutStreamBuf;
4993 +static size_t MyWrite(void *pp, const void *data, size_t size)
4995 + CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
4996 + if (p->rem < size)
4999 + p->overflow = True;
5001 + memcpy(p->data, data, size);
5008 +UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
5010 + const CLzmaEnc *p = (CLzmaEnc *)pp;
5011 + return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
5014 +const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)
5016 + const CLzmaEnc *p = (CLzmaEnc *)pp;
5017 + return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
5020 +SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
5021 + Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
5023 + CLzmaEnc *p = (CLzmaEnc *)pp;
5026 + CSeqOutStreamBuf outStream;
5028 + outStream.funcTable.Write = MyWrite;
5029 + outStream.data = dest;
5030 + outStream.rem = *destLen;
5031 + outStream.overflow = False;
5033 + p->writeEndMark = False;
5034 + p->finished = False;
5035 + p->result = SZ_OK;
5039 + LzmaEnc_InitPrices(p);
5040 + nowPos64 = p->nowPos64;
5041 + RangeEnc_Init(&p->rc);
5042 + p->rc.outStream = &outStream.funcTable;
5044 + res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize);
5046 + *unpackSize = (UInt32)(p->nowPos64 - nowPos64);
5047 + *destLen -= outStream.rem;
5048 + if (outStream.overflow)
5049 + return SZ_ERROR_OUTPUT_EOF;
5054 +static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
5059 + Byte allocaDummy[0x300];
5061 + for (i = 0; i < 16; i++)
5062 + allocaDummy[i] = (Byte)i;
5067 + res = LzmaEnc_CodeOneBlock(p, False, 0, 0);
5068 + if (res != SZ_OK || p->finished != 0)
5070 + if (progress != 0)
5072 + res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
5075 + res = SZ_ERROR_PROGRESS;
5080 + LzmaEnc_Finish(p);
5084 +SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
5085 + ISzAlloc *alloc, ISzAlloc *allocBig)
5087 + RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig));
5088 + return LzmaEnc_Encode2((CLzmaEnc *)pp, progress);
5091 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
5093 + CLzmaEnc *p = (CLzmaEnc *)pp;
5095 + UInt32 dictSize = p->dictSize;
5096 + if (*size < LZMA_PROPS_SIZE)
5097 + return SZ_ERROR_PARAM;
5098 + *size = LZMA_PROPS_SIZE;
5099 + props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
5101 + for (i = 11; i <= 30; i++)
5103 + if (dictSize <= ((UInt32)2 << i))
5105 + dictSize = (2 << i);
5108 + if (dictSize <= ((UInt32)3 << i))
5110 + dictSize = (3 << i);
5115 + for (i = 0; i < 4; i++)
5116 + props[1 + i] = (Byte)(dictSize >> (8 * i));
5120 +SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
5121 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
5124 + CLzmaEnc *p = (CLzmaEnc *)pp;
5126 + CSeqOutStreamBuf outStream;
5128 + LzmaEnc_SetInputBuf(p, src, srcLen);
5130 + outStream.funcTable.Write = MyWrite;
5131 + outStream.data = dest;
5132 + outStream.rem = *destLen;
5133 + outStream.overflow = False;
5135 + p->writeEndMark = writeEndMark;
5137 + p->rc.outStream = &outStream.funcTable;
5138 + res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);
5140 + res = LzmaEnc_Encode2(p, progress);
5142 + *destLen -= outStream.rem;
5143 + if (outStream.overflow)
5144 + return SZ_ERROR_OUTPUT_EOF;
5148 +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
5149 + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
5150 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
5152 + CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
5155 + return SZ_ERROR_MEM;
5157 + res = LzmaEnc_SetProps(p, props);
5160 + res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
5162 + res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
5163 + writeEndMark, progress, alloc, allocBig);
5166 + LzmaEnc_Destroy(p, alloc, allocBig);
5169 Index: linux-2.6.38-rc6/lib/lzma/Makefile
5170 ===================================================================
5171 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
5172 +++ linux-2.6.38-rc6/lib/lzma/Makefile 2011-02-28 15:34:05.321257314 +0100
5174 +lzma_compress-objs := LzFind.o LzmaEnc.o
5175 +lzma_decompress-objs := LzmaDec.o
5177 +obj-$(CONFIG_LZMA_COMPRESS) += lzma_compress.o
5178 +obj-$(CONFIG_LZMA_DECOMPRESS) += lzma_decompress.o
5180 +EXTRA_CFLAGS += -Iinclude/linux -Iinclude/linux/lzma -include types.h