7 -CPPFLAGS += -I./include $(ZLIBCPPFLAGS) $(LZOCPPFLAGS)
8 +CPPFLAGS += -I./include $(ZLIBCPPFLAGS) $(LZOCPPFLAGS) -I./include/linux/lzma
10 ifeq ($(WITHOUT_XATTR), 1)
11 CPPFLAGS += -DWITHOUT_XATTR
13 ln -sf ../fs/jffs2/$@ $@
15 $(BUILDDIR)/mkfs.jffs2: $(addprefix $(BUILDDIR)/,\
16 - crc32.o compr_rtime.o mkfs.jffs2.o compr_zlib.o $(if $(NO_LZO),,compr_lzo.o) \
17 + crc32.o compr_rtime.o mkfs.jffs2.o compr_zlib.o \
18 + $(if $(NO_LZO),,compr_lzo.o) \
19 + compr_lzma.o lzma/LzFind.o lzma/LzmaEnc.o lzma/LzmaDec.o\
22 LDFLAGS_mkfs.jffs2 := $(ZLIBLDFLAGS)
23 --- a/compr.c 2009-06-05 16:59:08.000000000 +0200
24 +++ b/compr.c 2010-03-20 23:16:14.556367000 +0100
26 #ifdef CONFIG_JFFS2_LZO
29 +#ifdef CONFIG_JFFS2_LZMA
36 #ifdef CONFIG_JFFS2_LZO
39 +#ifdef CONFIG_JFFS2_LZMA
44 --- a/compr.h 2010-03-20 23:08:46.289595000 +0100
45 +++ b/compr.h 2010-03-20 23:51:41.953345921 +0100
48 #define CONFIG_JFFS2_ZLIB
49 #define CONFIG_JFFS2_RTIME
50 +#define CONFIG_JFFS2_LZMA
52 #define JFFS2_RUBINMIPS_PRIORITY 10
53 #define JFFS2_DYNRUBIN_PRIORITY 20
54 #define JFFS2_RTIME_PRIORITY 50
55 -#define JFFS2_ZLIB_PRIORITY 60
56 -#define JFFS2_LZO_PRIORITY 80
57 +#define JFFS2_LZMA_PRIORITY 70
58 +#define JFFS2_ZLIB_PRIORITY 80
59 +#define JFFS2_LZO_PRIORITY 90
61 #define JFFS2_COMPR_MODE_NONE 0
62 #define JFFS2_COMPR_MODE_PRIORITY 1
64 int jffs2_lzo_init(void);
65 void jffs2_lzo_exit(void);
67 +#ifdef CONFIG_JFFS2_LZMA
68 +int jffs2_lzma_init(void);
69 +void jffs2_lzma_exit(void);
73 #endif /* __JFFS2_COMPR_H__ */
74 --- a/compr_lzma.c 1970-01-01 01:00:00.000000000 +0100
75 +++ b/compr_lzma.c 2010-03-20 23:16:15.048654497 +0100
78 + * JFFS2 -- Journalling Flash File System, Version 2.
80 + * For licensing information, see the file 'LICENCE' in this directory.
82 + * JFFS2 wrapper to the LZMA C SDK
86 +#include <linux/lzma.h>
90 + static DEFINE_MUTEX(deflate_mutex);
94 +Byte propsEncoded[LZMA_PROPS_SIZE];
95 +SizeT propsSize = sizeof(propsEncoded);
97 +STATIC void lzma_free_workspace(void)
99 + LzmaEnc_Destroy(p, &lzma_alloc, &lzma_alloc);
102 +STATIC int INIT lzma_alloc_workspace(CLzmaEncProps *props)
104 + if ((p = (CLzmaEncHandle *)LzmaEnc_Create(&lzma_alloc)) == NULL)
106 + PRINT_ERROR("Failed to allocate lzma deflate workspace\n");
110 + if (LzmaEnc_SetProps(p, props) != SZ_OK)
112 + lzma_free_workspace();
116 + if (LzmaEnc_WriteProperties(p, propsEncoded, &propsSize) != SZ_OK)
118 + lzma_free_workspace();
125 +STATIC int jffs2_lzma_compress(unsigned char *data_in, unsigned char *cpage_out,
126 + uint32_t *sourcelen, uint32_t *dstlen, void *model)
128 + SizeT compress_size = (SizeT)(*dstlen);
132 + mutex_lock(&deflate_mutex);
135 + ret = LzmaEnc_MemEncode(p, cpage_out, &compress_size, data_in, *sourcelen,
136 + 0, NULL, &lzma_alloc, &lzma_alloc);
139 + mutex_unlock(&deflate_mutex);
145 + *dstlen = (uint32_t)compress_size;
150 +STATIC int jffs2_lzma_decompress(unsigned char *data_in, unsigned char *cpage_out,
151 + uint32_t srclen, uint32_t destlen, void *model)
154 + SizeT dl = (SizeT)destlen;
155 + SizeT sl = (SizeT)srclen;
156 + ELzmaStatus status;
158 + ret = LzmaDecode(cpage_out, &dl, data_in, &sl, propsEncoded,
159 + propsSize, LZMA_FINISH_ANY, &status, &lzma_alloc);
161 + if (ret != SZ_OK || status == LZMA_STATUS_NOT_FINISHED || dl != (SizeT)destlen)
167 +static struct jffs2_compressor jffs2_lzma_comp = {
168 + .priority = JFFS2_LZMA_PRIORITY,
170 + .compr = JFFS2_COMPR_LZMA,
171 + .compress = &jffs2_lzma_compress,
172 + .decompress = &jffs2_lzma_decompress,
176 +int INIT jffs2_lzma_init(void)
179 + CLzmaEncProps props;
180 + LzmaEncProps_Init(&props);
182 + props.dictSize = LZMA_BEST_DICT(0x2000);
183 + props.level = LZMA_BEST_LEVEL;
184 + props.lc = LZMA_BEST_LC;
185 + props.lp = LZMA_BEST_LP;
186 + props.pb = LZMA_BEST_PB;
187 + props.fb = LZMA_BEST_FB;
189 + ret = lzma_alloc_workspace(&props);
193 + ret = jffs2_register_compressor(&jffs2_lzma_comp);
195 + lzma_free_workspace();
200 +void jffs2_lzma_exit(void)
202 + jffs2_unregister_compressor(&jffs2_lzma_comp);
203 + lzma_free_workspace();
205 --- a/include/linux/jffs2.h 2009-06-05 16:59:08.000000000 +0200
206 +++ b/include/linux/jffs2.h 2010-03-20 23:16:15.305246000 +0100
208 #define JFFS2_COMPR_DYNRUBIN 0x05
209 #define JFFS2_COMPR_ZLIB 0x06
210 #define JFFS2_COMPR_LZO 0x07
211 +#define JFFS2_COMPR_LZMA 0x08
212 /* Compatibility flags. */
213 #define JFFS2_COMPAT_MASK 0xc000 /* What do to if an unknown nodetype is found */
214 #define JFFS2_NODE_ACCURATE 0x2000
215 --- a/include/linux/lzma.h 1970-01-01 01:00:00.000000000 +0100
216 +++ b/include/linux/lzma.h 2010-03-20 23:16:16.380508712 +0100
222 + #include <linux/kernel.h>
223 + #include <linux/sched.h>
224 + #include <linux/slab.h>
225 + #include <linux/vmalloc.h>
226 + #include <linux/init.h>
227 + #define LZMA_MALLOC vmalloc
228 + #define LZMA_FREE vfree
229 + #define PRINT_ERROR(msg) printk(KERN_WARNING #msg)
230 + #define INIT __init
231 + #define STATIC static
233 + #include <stdint.h>
234 + #include <stdlib.h>
236 + #include <unistd.h>
237 + #include <string.h>
239 + #include <linux/jffs2.h>
241 + extern int page_size;
242 + #define PAGE_SIZE page_size
244 + #define LZMA_MALLOC malloc
245 + #define LZMA_FREE free
246 + #define PRINT_ERROR(msg) fprintf(stderr, msg)
251 +#include "lzma/LzmaDec.h"
252 +#include "lzma/LzmaEnc.h"
254 +#define LZMA_BEST_LEVEL (9)
255 +#define LZMA_BEST_LC (0)
256 +#define LZMA_BEST_LP (0)
257 +#define LZMA_BEST_PB (0)
258 +#define LZMA_BEST_FB (273)
260 +#define LZMA_BEST_DICT(n) (((int)((n) / 2)) * 2)
262 +static void *p_lzma_malloc(void *p, size_t size)
267 + return LZMA_MALLOC(size);
270 +static void p_lzma_free(void *p, void *address)
272 + if (address != NULL)
273 + LZMA_FREE(address);
276 +static ISzAlloc lzma_alloc = {p_lzma_malloc, p_lzma_free};
279 --- a/include/linux/lzma/LzFind.h 1970-01-01 01:00:00.000000000 +0100
280 +++ b/include/linux/lzma/LzFind.h 2010-03-20 23:19:44.189059515 +0100
282 +/* LzFind.h -- Match finder for LZ algorithms
284 +Copyright (c) 1999-2008 Igor Pavlov
285 +You can use any of the following license options:
286 + 1) GNU Lesser General Public License (GNU LGPL)
287 + 2) Common Public License (CPL)
288 + 3) Common Development and Distribution License (CDDL) Version 1.0
289 + 4) Igor Pavlov, as the author of this code, expressly permits you to
290 + statically or dynamically link your code (or bind by name) to this file,
291 + while you keep this file unmodified.
299 +typedef UInt32 CLzRef;
301 +typedef struct _CMatchFinder
309 + UInt32 cyclicBufferPos;
310 + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */
312 + UInt32 matchMaxLen;
319 + ISeqInStream *stream;
320 + int streamEndWasReached;
323 + UInt32 keepSizeBefore;
324 + UInt32 keepSizeAfter;
326 + UInt32 numHashBytes;
329 + /* int skipModeBits; */
331 + UInt32 historySize;
332 + UInt32 fixedHashSize;
333 + UInt32 hashSizeSum;
339 +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer)
340 +#define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)])
342 +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos)
344 +int MatchFinder_NeedMove(CMatchFinder *p);
345 +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p);
346 +void MatchFinder_MoveBlock(CMatchFinder *p);
347 +void MatchFinder_ReadIfRequired(CMatchFinder *p);
349 +void MatchFinder_Construct(CMatchFinder *p);
352 + historySize <= 3 GB
353 + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB
355 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
356 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
358 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc);
359 +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems);
360 +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue);
362 +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *buffer, CLzRef *son,
363 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue,
364 + UInt32 *distances, UInt32 maxLen);
368 + Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func.
369 + Mf_GetPointerToCurrentPos_Func's result must be used only before any other function
372 +typedef void (*Mf_Init_Func)(void *object);
373 +typedef Byte (*Mf_GetIndexByte_Func)(void *object, Int32 index);
374 +typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object);
375 +typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object);
376 +typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances);
377 +typedef void (*Mf_Skip_Func)(void *object, UInt32);
379 +typedef struct _IMatchFinder
382 + Mf_GetIndexByte_Func GetIndexByte;
383 + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes;
384 + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos;
385 + Mf_GetMatches_Func GetMatches;
389 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable);
391 +void MatchFinder_Init(CMatchFinder *p);
392 +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances);
393 +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances);
394 +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num);
395 +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num);
398 --- a/include/linux/lzma/LzHash.h 1970-01-01 01:00:00.000000000 +0100
399 +++ b/include/linux/lzma/LzHash.h 2010-03-20 23:19:44.588791287 +0100
401 +/* LzHash.h -- HASH functions for LZ algorithms
403 +Copyright (c) 1999-2008 Igor Pavlov
404 +Read LzFind.h for license options */
409 +#define kHash2Size (1 << 10)
410 +#define kHash3Size (1 << 16)
411 +#define kHash4Size (1 << 20)
413 +#define kFix3HashSize (kHash2Size)
414 +#define kFix4HashSize (kHash2Size + kHash3Size)
415 +#define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size)
417 +#define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8);
419 +#define HASH3_CALC { \
420 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
421 + hash2Value = temp & (kHash2Size - 1); \
422 + hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; }
424 +#define HASH4_CALC { \
425 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
426 + hash2Value = temp & (kHash2Size - 1); \
427 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
428 + hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; }
430 +#define HASH5_CALC { \
431 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
432 + hash2Value = temp & (kHash2Size - 1); \
433 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
434 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \
435 + hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \
436 + hash4Value &= (kHash4Size - 1); }
438 +/* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */
439 +#define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF;
442 +#define MT_HASH2_CALC \
443 + hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1);
445 +#define MT_HASH3_CALC { \
446 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
447 + hash2Value = temp & (kHash2Size - 1); \
448 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); }
450 +#define MT_HASH4_CALC { \
451 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
452 + hash2Value = temp & (kHash2Size - 1); \
453 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
454 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); }
457 --- a/include/linux/lzma/LzmaDec.h 1970-01-01 01:00:00.000000000 +0100
458 +++ b/include/linux/lzma/LzmaDec.h 2010-03-20 23:19:43.766328000 +0100
460 +/* LzmaDec.h -- LZMA Decoder
462 +Copyright (c) 1999-2008 Igor Pavlov
463 +You can use any of the following license options:
464 + 1) GNU Lesser General Public License (GNU LGPL)
465 + 2) Common Public License (CPL)
466 + 3) Common Development and Distribution License (CDDL) Version 1.0
467 + 4) Igor Pavlov, as the author of this code, expressly permits you to
468 + statically or dynamically link your code (or bind by name) to this file,
469 + while you keep this file unmodified.
477 +/* #define _LZMA_PROB32 */
478 +/* _LZMA_PROB32 can increase the speed on some CPUs,
479 + but memory usage for CLzmaDec::probs will be doubled in that case */
482 +#define CLzmaProb UInt32
484 +#define CLzmaProb UInt16
488 +/* ---------- LZMA Properties ---------- */
490 +#define LZMA_PROPS_SIZE 5
492 +typedef struct _CLzmaProps
494 + unsigned lc, lp, pb;
498 +/* LzmaProps_Decode - decodes properties
501 + SZ_ERROR_UNSUPPORTED - Unsupported properties
504 +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
507 +/* ---------- LZMA Decoder state ---------- */
509 +/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
510 + Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
512 +#define LZMA_REQUIRED_INPUT_MAX 20
520 + UInt32 range, code;
523 + UInt32 processedPos;
524 + UInt32 checkDicSize;
527 + unsigned remainLen;
531 + unsigned tempBufSize;
532 + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
535 +#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
537 +void LzmaDec_Init(CLzmaDec *p);
539 +/* There are two types of LZMA streams:
540 + 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
541 + 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
545 + LZMA_FINISH_ANY, /* finish at any point */
546 + LZMA_FINISH_END /* block must be finished at the end */
549 +/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
551 + You must use LZMA_FINISH_END, when you know that current output buffer
552 + covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
554 + If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
555 + and output value of destLen will be less than output buffer size limit.
556 + You can check status result also.
558 + You can use multiple checks to test data integrity after full decompression:
559 + 1) Check Result and "status" variable.
560 + 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
561 + 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
562 + You must use correct finish mode in that case. */
566 + LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
567 + LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
568 + LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
569 + LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
570 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
573 +/* ELzmaStatus is used only as output value for function call */
576 +/* ---------- Interfaces ---------- */
578 +/* There are 3 levels of interfaces:
579 + 1) Dictionary Interface
580 + 2) Buffer Interface
581 + 3) One Call Interface
582 + You can select any of these interfaces, but don't mix functions from different
583 + groups for same object. */
586 +/* There are two variants to allocate state for Dictionary Interface:
587 + 1) LzmaDec_Allocate / LzmaDec_Free
588 + 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
589 + You can use variant 2, if you set dictionary buffer manually.
590 + For Buffer Interface you must always use variant 1.
592 +LzmaDec_Allocate* can return:
594 + SZ_ERROR_MEM - Memory allocation error
595 + SZ_ERROR_UNSUPPORTED - Unsupported properties
598 +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
599 +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
601 +SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc);
602 +void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc);
604 +/* ---------- Dictionary Interface ---------- */
606 +/* You can use it, if you want to eliminate the overhead for data copying from
607 + dictionary to some other external buffer.
608 + You must work with CLzmaDec variables directly in this interface.
613 + for (each new stream)
616 + while (it needs more decompression)
618 + LzmaDec_DecodeToDic()
619 + use data from CLzmaDec::dic and update CLzmaDec::dicPos
625 +/* LzmaDec_DecodeToDic
627 + The decoding to internal dictionary buffer (CLzmaDec::dic).
628 + You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
631 + It has meaning only if the decoding reaches output limit (dicLimit).
632 + LZMA_FINISH_ANY - Decode just dicLimit bytes.
633 + LZMA_FINISH_END - Stream must be finished after dicLimit.
638 + LZMA_STATUS_FINISHED_WITH_MARK
639 + LZMA_STATUS_NOT_FINISHED
640 + LZMA_STATUS_NEEDS_MORE_INPUT
641 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
642 + SZ_ERROR_DATA - Data error
645 +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,
646 + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
649 +/* ---------- Buffer Interface ---------- */
651 +/* It's zlib-like interface.
652 + See LzmaDec_DecodeToDic description for information about STEPS and return results,
653 + but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
654 + to work with CLzmaDec variables manually.
657 + It has meaning only if the decoding reaches output limit (*destLen).
658 + LZMA_FINISH_ANY - Decode just destLen bytes.
659 + LZMA_FINISH_END - Stream must be finished after (*destLen).
662 +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
663 + const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
666 +/* ---------- One Call Interface ---------- */
671 + It has meaning only if the decoding reaches output limit (*destLen).
672 + LZMA_FINISH_ANY - Decode just destLen bytes.
673 + LZMA_FINISH_END - Stream must be finished after (*destLen).
678 + LZMA_STATUS_FINISHED_WITH_MARK
679 + LZMA_STATUS_NOT_FINISHED
680 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
681 + SZ_ERROR_DATA - Data error
682 + SZ_ERROR_MEM - Memory allocation error
683 + SZ_ERROR_UNSUPPORTED - Unsupported properties
684 + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
687 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
688 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
689 + ELzmaStatus *status, ISzAlloc *alloc);
692 --- a/include/linux/lzma/LzmaEnc.h 1970-01-01 01:00:00.000000000 +0100
693 +++ b/include/linux/lzma/LzmaEnc.h 2010-03-20 23:19:47.392642470 +0100
695 +/* LzmaEnc.h -- LZMA Encoder
697 +Copyright (c) 1999-2008 Igor Pavlov
698 +Read LzFind.h for license options */
705 +#define LZMA_PROPS_SIZE 5
707 +typedef struct _CLzmaEncProps
709 + int level; /* 0 <= level <= 9 */
710 + UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
711 + (1 << 12) <= dictSize <= (1 << 30) for 64-bit version
712 + default = (1 << 24) */
713 + int lc; /* 0 <= lc <= 8, default = 3 */
714 + int lp; /* 0 <= lp <= 4, default = 0 */
715 + int pb; /* 0 <= pb <= 4, default = 2 */
716 + int algo; /* 0 - fast, 1 - normal, default = 1 */
717 + int fb; /* 5 <= fb <= 273, default = 32 */
718 + int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
719 + int numHashBytes; /* 2, 3 or 4, default = 4 */
720 + UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */
721 + unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
722 + int numThreads; /* 1 or 2, default = 2 */
725 +void LzmaEncProps_Init(CLzmaEncProps *p);
726 +void LzmaEncProps_Normalize(CLzmaEncProps *p);
727 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2);
730 +/* ---------- CLzmaEncHandle Interface ---------- */
732 +/* LzmaEnc_* functions can return the following exit codes:
735 + SZ_ERROR_MEM - Memory allocation error
736 + SZ_ERROR_PARAM - Incorrect paramater in props
737 + SZ_ERROR_WRITE - Write callback error.
738 + SZ_ERROR_PROGRESS - some break from progress callback
739 + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
742 +typedef void * CLzmaEncHandle;
744 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc);
745 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig);
746 +SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props);
747 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size);
748 +SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream *outStream, ISeqInStream *inStream,
749 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
750 +SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
751 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
753 +/* ---------- One Call Interface ---------- */
758 + SZ_ERROR_MEM - Memory allocation error
759 + SZ_ERROR_PARAM - Incorrect paramater
760 + SZ_ERROR_OUTPUT_EOF - output buffer overflow
761 + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
764 +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
765 + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
766 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
769 --- a/include/linux/lzma/Types.h 1970-01-01 01:00:00.000000000 +0100
770 +++ b/include/linux/lzma/Types.h 2010-03-20 23:19:44.013465852 +0100
772 +/* Types.h -- Basic types
777 +#ifndef __7Z_TYPES_H
778 +#define __7Z_TYPES_H
782 +#define SZ_ERROR_DATA 1
783 +#define SZ_ERROR_MEM 2
784 +#define SZ_ERROR_CRC 3
785 +#define SZ_ERROR_UNSUPPORTED 4
786 +#define SZ_ERROR_PARAM 5
787 +#define SZ_ERROR_INPUT_EOF 6
788 +#define SZ_ERROR_OUTPUT_EOF 7
789 +#define SZ_ERROR_READ 8
790 +#define SZ_ERROR_WRITE 9
791 +#define SZ_ERROR_PROGRESS 10
792 +#define SZ_ERROR_FAIL 11
793 +#define SZ_ERROR_THREAD 12
795 +#define SZ_ERROR_ARCHIVE 16
796 +#define SZ_ERROR_NO_ARCHIVE 17
801 +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
804 +typedef unsigned char Byte;
805 +typedef short Int16;
806 +typedef unsigned short UInt16;
808 +#ifdef _LZMA_UINT32_IS_ULONG
810 +typedef unsigned long UInt32;
813 +typedef unsigned int UInt32;
816 +/* #define _SZ_NO_INT_64 */
817 +/* define it if your compiler doesn't support 64-bit integers */
819 +#ifdef _SZ_NO_INT_64
822 +typedef unsigned long UInt64;
826 +#if defined(_MSC_VER) || defined(__BORLANDC__)
827 +typedef __int64 Int64;
828 +typedef unsigned __int64 UInt64;
830 +typedef long long int Int64;
831 +typedef unsigned long long int UInt64;
836 +#ifdef _LZMA_NO_SYSTEM_SIZE_T
837 +typedef UInt32 SizeT;
840 +typedef size_t SizeT;
850 +#if _MSC_VER >= 1300
851 +#define MY_NO_INLINE __declspec(noinline)
853 +#define MY_NO_INLINE
856 +#define MY_CDECL __cdecl
857 +#define MY_STD_CALL __stdcall
858 +#define MY_FAST_CALL MY_NO_INLINE __fastcall
864 +#define MY_FAST_CALL
869 +/* The following interfaces use first parameter as pointer to structure */
873 + SRes (*Read)(void *p, void *buf, size_t *size);
874 + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
875 + (output(*size) < input(*size)) is allowed */
880 + size_t (*Write)(void *p, const void *buf, size_t size);
881 + /* Returns: result - the number of actually written bytes.
882 + (result < size) means error */
887 + SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize);
888 + /* Returns: result. (result != SZ_OK) means break.
889 + Value (UInt64)(Int64)-1 for size means unknown value. */
890 +} ICompressProgress;
894 + void *(*Alloc)(void *p, size_t size);
895 + void (*Free)(void *p, void *address); /* address can be 0 */
898 +#define IAlloc_Alloc(p, size) (p)->Alloc((p), size)
899 +#define IAlloc_Free(p, a) (p)->Free((p), a)
902 --- a/lzma/LzFind.c 1970-01-01 01:00:00.000000000 +0100
903 +++ b/lzma/LzFind.c 2010-03-20 23:19:47.990406000 +0100
905 +/* LzFind.c -- Match finder for LZ algorithms
907 +Copyright (c) 1999-2008 Igor Pavlov
908 +Read LzFind.h for license options */
915 +#define kEmptyHashValue 0
916 +#define kMaxValForNormalize ((UInt32)0xFFFFFFFF)
917 +#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */
918 +#define kNormalizeMask (~(kNormalizeStepMin - 1))
919 +#define kMaxHistorySize ((UInt32)3 << 30)
921 +#define kStartMaxLen 3
923 +static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
925 + if (!p->directInput)
927 + alloc->Free(alloc, p->bufferBase);
932 +/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
934 +static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
936 + UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
937 + if (p->directInput)
939 + p->blockSize = blockSize;
942 + if (p->bufferBase == 0 || p->blockSize != blockSize)
944 + LzInWindow_Free(p, alloc);
945 + p->blockSize = blockSize;
946 + p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
948 + return (p->bufferBase != 0);
951 +Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
952 +Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
954 +UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
956 +void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
958 + p->posLimit -= subValue;
959 + p->pos -= subValue;
960 + p->streamPos -= subValue;
963 +static void MatchFinder_ReadBlock(CMatchFinder *p)
965 + if (p->streamEndWasReached || p->result != SZ_OK)
969 + Byte *dest = p->buffer + (p->streamPos - p->pos);
970 + size_t size = (p->bufferBase + p->blockSize - dest);
973 + p->result = p->stream->Read(p->stream, dest, &size);
974 + if (p->result != SZ_OK)
978 + p->streamEndWasReached = 1;
981 + p->streamPos += (UInt32)size;
982 + if (p->streamPos - p->pos > p->keepSizeAfter)
987 +void MatchFinder_MoveBlock(CMatchFinder *p)
989 + memmove(p->bufferBase,
990 + p->buffer - p->keepSizeBefore,
991 + (size_t)(p->streamPos - p->pos + p->keepSizeBefore));
992 + p->buffer = p->bufferBase + p->keepSizeBefore;
995 +int MatchFinder_NeedMove(CMatchFinder *p)
997 + /* if (p->streamEndWasReached) return 0; */
998 + return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
1001 +void MatchFinder_ReadIfRequired(CMatchFinder *p)
1003 + if (p->streamEndWasReached)
1005 + if (p->keepSizeAfter >= p->streamPos - p->pos)
1006 + MatchFinder_ReadBlock(p);
1009 +static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
1011 + if (MatchFinder_NeedMove(p))
1012 + MatchFinder_MoveBlock(p);
1013 + MatchFinder_ReadBlock(p);
1016 +static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
1020 + p->numHashBytes = 4;
1021 + /* p->skipModeBits = 0; */
1022 + p->directInput = 0;
1026 +#define kCrcPoly 0xEDB88320
1028 +void MatchFinder_Construct(CMatchFinder *p)
1031 + p->bufferBase = 0;
1032 + p->directInput = 0;
1034 + MatchFinder_SetDefaultSettings(p);
1036 + for (i = 0; i < 256; i++)
1040 + for (j = 0; j < 8; j++)
1041 + r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
1046 +static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
1048 + alloc->Free(alloc, p->hash);
1052 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
1054 + MatchFinder_FreeThisClassMemory(p, alloc);
1055 + LzInWindow_Free(p, alloc);
1058 +static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
1060 + size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
1061 + if (sizeInBytes / sizeof(CLzRef) != num)
1063 + return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
1066 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
1067 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
1070 + UInt32 sizeReserv;
1071 + if (historySize > kMaxHistorySize)
1073 + MatchFinder_Free(p, alloc);
1076 + sizeReserv = historySize >> 1;
1077 + if (historySize > ((UInt32)2 << 30))
1078 + sizeReserv = historySize >> 2;
1079 + sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
1081 + p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
1082 + p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
1083 + /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
1084 + if (LzInWindow_Create(p, sizeReserv, alloc))
1086 + UInt32 newCyclicBufferSize = (historySize /* >> p->skipModeBits */) + 1;
1088 + p->matchMaxLen = matchMaxLen;
1090 + p->fixedHashSize = 0;
1091 + if (p->numHashBytes == 2)
1092 + hs = (1 << 16) - 1;
1095 + hs = historySize - 1;
1101 + /* hs >>= p->skipModeBits; */
1102 + hs |= 0xFFFF; /* don't change it! It's required for Deflate */
1103 + if (hs > (1 << 24))
1105 + if (p->numHashBytes == 3)
1106 + hs = (1 << 24) - 1;
1113 + if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
1114 + if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
1115 + if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
1116 + hs += p->fixedHashSize;
1120 + UInt32 prevSize = p->hashSizeSum + p->numSons;
1122 + p->historySize = historySize;
1123 + p->hashSizeSum = hs;
1124 + p->cyclicBufferSize = newCyclicBufferSize;
1125 + p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);
1126 + newSize = p->hashSizeSum + p->numSons;
1127 + if (p->hash != 0 && prevSize == newSize)
1129 + MatchFinder_FreeThisClassMemory(p, alloc);
1130 + p->hash = AllocRefs(newSize, alloc);
1133 + p->son = p->hash + p->hashSizeSum;
1138 + MatchFinder_Free(p, alloc);
1142 +static void MatchFinder_SetLimits(CMatchFinder *p)
1144 + UInt32 limit = kMaxValForNormalize - p->pos;
1145 + UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
1146 + if (limit2 < limit)
1148 + limit2 = p->streamPos - p->pos;
1149 + if (limit2 <= p->keepSizeAfter)
1155 + limit2 -= p->keepSizeAfter;
1156 + if (limit2 < limit)
1159 + UInt32 lenLimit = p->streamPos - p->pos;
1160 + if (lenLimit > p->matchMaxLen)
1161 + lenLimit = p->matchMaxLen;
1162 + p->lenLimit = lenLimit;
1164 + p->posLimit = p->pos + limit;
1167 +void MatchFinder_Init(CMatchFinder *p)
1170 + for(i = 0; i < p->hashSizeSum; i++)
1171 + p->hash[i] = kEmptyHashValue;
1172 + p->cyclicBufferPos = 0;
1173 + p->buffer = p->bufferBase;
1174 + p->pos = p->streamPos = p->cyclicBufferSize;
1175 + p->result = SZ_OK;
1176 + p->streamEndWasReached = 0;
1177 + MatchFinder_ReadBlock(p);
1178 + MatchFinder_SetLimits(p);
1181 +static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
1183 + return (p->pos - p->historySize - 1) & kNormalizeMask;
1186 +void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
1189 + for (i = 0; i < numItems; i++)
1191 + UInt32 value = items[i];
1192 + if (value <= subValue)
1193 + value = kEmptyHashValue;
1195 + value -= subValue;
1200 +static void MatchFinder_Normalize(CMatchFinder *p)
1202 + UInt32 subValue = MatchFinder_GetSubValue(p);
1203 + MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
1204 + MatchFinder_ReduceOffsets(p, subValue);
1207 +static void MatchFinder_CheckLimits(CMatchFinder *p)
1209 + if (p->pos == kMaxValForNormalize)
1210 + MatchFinder_Normalize(p);
1211 + if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
1212 + MatchFinder_CheckAndMoveAndRead(p);
1213 + if (p->cyclicBufferPos == p->cyclicBufferSize)
1214 + p->cyclicBufferPos = 0;
1215 + MatchFinder_SetLimits(p);
1218 +static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1219 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
1220 + UInt32 *distances, UInt32 maxLen)
1222 + son[_cyclicBufferPos] = curMatch;
1225 + UInt32 delta = pos - curMatch;
1226 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1229 + const Byte *pb = cur - delta;
1230 + curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];
1231 + if (pb[maxLen] == cur[maxLen] && *pb == *cur)
1234 + while(++len != lenLimit)
1235 + if (pb[len] != cur[len])
1239 + *distances++ = maxLen = len;
1240 + *distances++ = delta - 1;
1241 + if (len == lenLimit)
1249 +UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1250 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
1251 + UInt32 *distances, UInt32 maxLen)
1253 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1254 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1255 + UInt32 len0 = 0, len1 = 0;
1258 + UInt32 delta = pos - curMatch;
1259 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1261 + *ptr0 = *ptr1 = kEmptyHashValue;
1265 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1266 + const Byte *pb = cur - delta;
1267 + UInt32 len = (len0 < len1 ? len0 : len1);
1268 + if (pb[len] == cur[len])
1270 + if (++len != lenLimit && pb[len] == cur[len])
1271 + while(++len != lenLimit)
1272 + if (pb[len] != cur[len])
1276 + *distances++ = maxLen = len;
1277 + *distances++ = delta - 1;
1278 + if (len == lenLimit)
1286 + if (pb[len] < cur[len])
1304 +static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1305 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
1307 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1308 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1309 + UInt32 len0 = 0, len1 = 0;
1312 + UInt32 delta = pos - curMatch;
1313 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1315 + *ptr0 = *ptr1 = kEmptyHashValue;
1319 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1320 + const Byte *pb = cur - delta;
1321 + UInt32 len = (len0 < len1 ? len0 : len1);
1322 + if (pb[len] == cur[len])
1324 + while(++len != lenLimit)
1325 + if (pb[len] != cur[len])
1328 + if (len == lenLimit)
1336 + if (pb[len] < cur[len])
1355 + ++p->cyclicBufferPos; \
1357 + if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
1359 +#define MOVE_POS_RET MOVE_POS return offset;
1361 +static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
1363 +#define GET_MATCHES_HEADER2(minLen, ret_op) \
1364 + UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \
1365 + lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
1368 +#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
1369 +#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
1371 +#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
1373 +#define GET_MATCHES_FOOTER(offset, maxLen) \
1374 + offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
1375 + distances + offset, maxLen) - distances); MOVE_POS_RET;
1377 +#define SKIP_FOOTER \
1378 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;
1380 +static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1383 + GET_MATCHES_HEADER(2)
1385 + curMatch = p->hash[hashValue];
1386 + p->hash[hashValue] = p->pos;
1388 + GET_MATCHES_FOOTER(offset, 1)
1391 +UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1394 + GET_MATCHES_HEADER(3)
1396 + curMatch = p->hash[hashValue];
1397 + p->hash[hashValue] = p->pos;
1399 + GET_MATCHES_FOOTER(offset, 2)
1402 +static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1404 + UInt32 hash2Value, delta2, maxLen, offset;
1405 + GET_MATCHES_HEADER(3)
1409 + delta2 = p->pos - p->hash[hash2Value];
1410 + curMatch = p->hash[kFix3HashSize + hashValue];
1412 + p->hash[hash2Value] =
1413 + p->hash[kFix3HashSize + hashValue] = p->pos;
1418 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1420 + for (; maxLen != lenLimit; maxLen++)
1421 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1423 + distances[0] = maxLen;
1424 + distances[1] = delta2 - 1;
1426 + if (maxLen == lenLimit)
1428 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1432 + GET_MATCHES_FOOTER(offset, maxLen)
1435 +static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1437 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1438 + GET_MATCHES_HEADER(4)
1442 + delta2 = p->pos - p->hash[ hash2Value];
1443 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1444 + curMatch = p->hash[kFix4HashSize + hashValue];
1446 + p->hash[ hash2Value] =
1447 + p->hash[kFix3HashSize + hash3Value] =
1448 + p->hash[kFix4HashSize + hashValue] = p->pos;
1452 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1454 + distances[0] = maxLen = 2;
1455 + distances[1] = delta2 - 1;
1458 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1461 + distances[offset + 1] = delta3 - 1;
1467 + for (; maxLen != lenLimit; maxLen++)
1468 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1470 + distances[offset - 2] = maxLen;
1471 + if (maxLen == lenLimit)
1473 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1479 + GET_MATCHES_FOOTER(offset, maxLen)
1482 +static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1484 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1485 + GET_MATCHES_HEADER(4)
1489 + delta2 = p->pos - p->hash[ hash2Value];
1490 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1491 + curMatch = p->hash[kFix4HashSize + hashValue];
1493 + p->hash[ hash2Value] =
1494 + p->hash[kFix3HashSize + hash3Value] =
1495 + p->hash[kFix4HashSize + hashValue] = p->pos;
1499 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1501 + distances[0] = maxLen = 2;
1502 + distances[1] = delta2 - 1;
1505 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1508 + distances[offset + 1] = delta3 - 1;
1514 + for (; maxLen != lenLimit; maxLen++)
1515 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1517 + distances[offset - 2] = maxLen;
1518 + if (maxLen == lenLimit)
1520 + p->son[p->cyclicBufferPos] = curMatch;
1526 + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
1527 + distances + offset, maxLen) - (distances));
1531 +UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1534 + GET_MATCHES_HEADER(3)
1536 + curMatch = p->hash[hashValue];
1537 + p->hash[hashValue] = p->pos;
1538 + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),
1539 + distances, 2) - (distances));
1543 +static void Bt2_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1549 + curMatch = p->hash[hashValue];
1550 + p->hash[hashValue] = p->pos;
1553 + while (--num != 0);
1556 +void Bt3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1562 + curMatch = p->hash[hashValue];
1563 + p->hash[hashValue] = p->pos;
1566 + while (--num != 0);
1569 +static void Bt3_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1573 + UInt32 hash2Value;
1576 + curMatch = p->hash[kFix3HashSize + hashValue];
1577 + p->hash[hash2Value] =
1578 + p->hash[kFix3HashSize + hashValue] = p->pos;
1581 + while (--num != 0);
1584 +static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1588 + UInt32 hash2Value, hash3Value;
1591 + curMatch = p->hash[kFix4HashSize + hashValue];
1592 + p->hash[ hash2Value] =
1593 + p->hash[kFix3HashSize + hash3Value] = p->pos;
1594 + p->hash[kFix4HashSize + hashValue] = p->pos;
1597 + while (--num != 0);
1600 +static void Hc4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1604 + UInt32 hash2Value, hash3Value;
1607 + curMatch = p->hash[kFix4HashSize + hashValue];
1608 + p->hash[ hash2Value] =
1609 + p->hash[kFix3HashSize + hash3Value] =
1610 + p->hash[kFix4HashSize + hashValue] = p->pos;
1611 + p->son[p->cyclicBufferPos] = curMatch;
1614 + while (--num != 0);
1617 +void Hc3Zip_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1623 + curMatch = p->hash[hashValue];
1624 + p->hash[hashValue] = p->pos;
1625 + p->son[p->cyclicBufferPos] = curMatch;
1628 + while (--num != 0);
1631 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
1633 + vTable->Init = (Mf_Init_Func)MatchFinder_Init;
1634 + vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;
1635 + vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
1636 + vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
1639 + vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches;
1640 + vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip;
1642 + else if (p->numHashBytes == 2)
1644 + vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches;
1645 + vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip;
1647 + else if (p->numHashBytes == 3)
1649 + vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches;
1650 + vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip;
1654 + vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
1655 + vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
1658 --- a/lzma/LzmaDec.c 1970-01-01 01:00:00.000000000 +0100
1659 +++ b/lzma/LzmaDec.c 2010-03-20 23:19:44.413562000 +0100
1661 +/* LzmaDec.c -- LZMA Decoder
1663 +Copyright (c) 1999-2008 Igor Pavlov
1664 +Read LzmaDec.h for license options */
1666 +#include "LzmaDec.h"
1668 +#include <string.h>
1670 +#define kNumTopBits 24
1671 +#define kTopValue ((UInt32)1 << kNumTopBits)
1673 +#define kNumBitModelTotalBits 11
1674 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
1675 +#define kNumMoveBits 5
1677 +#define RC_INIT_SIZE 5
1679 +#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
1681 +#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1682 +#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
1683 +#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
1684 +#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
1685 + { UPDATE_0(p); i = (i + i); A0; } else \
1686 + { UPDATE_1(p); i = (i + i) + 1; A1; }
1687 +#define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
1689 +#define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
1690 +#define TREE_DECODE(probs, limit, i) \
1691 + { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
1693 +/* #define _LZMA_SIZE_OPT */
1695 +#ifdef _LZMA_SIZE_OPT
1696 +#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
1698 +#define TREE_6_DECODE(probs, i) \
1700 + TREE_GET_BIT(probs, i); \
1701 + TREE_GET_BIT(probs, i); \
1702 + TREE_GET_BIT(probs, i); \
1703 + TREE_GET_BIT(probs, i); \
1704 + TREE_GET_BIT(probs, i); \
1705 + TREE_GET_BIT(probs, i); \
1709 +#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
1711 +#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1712 +#define UPDATE_0_CHECK range = bound;
1713 +#define UPDATE_1_CHECK range -= bound; code -= bound;
1714 +#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
1715 + { UPDATE_0_CHECK; i = (i + i); A0; } else \
1716 + { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
1717 +#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
1718 +#define TREE_DECODE_CHECK(probs, limit, i) \
1719 + { i = 1; do { GET_BIT_CHECK(probs + i, i) } while(i < limit); i -= limit; }
1722 +#define kNumPosBitsMax 4
1723 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
1725 +#define kLenNumLowBits 3
1726 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
1727 +#define kLenNumMidBits 3
1728 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
1729 +#define kLenNumHighBits 8
1730 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
1732 +#define LenChoice 0
1733 +#define LenChoice2 (LenChoice + 1)
1734 +#define LenLow (LenChoice2 + 1)
1735 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
1736 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
1737 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
1740 +#define kNumStates 12
1741 +#define kNumLitStates 7
1743 +#define kStartPosModelIndex 4
1744 +#define kEndPosModelIndex 14
1745 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
1747 +#define kNumPosSlotBits 6
1748 +#define kNumLenToPosStates 4
1750 +#define kNumAlignBits 4
1751 +#define kAlignTableSize (1 << kNumAlignBits)
1753 +#define kMatchMinLen 2
1754 +#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
1757 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
1758 +#define IsRepG0 (IsRep + kNumStates)
1759 +#define IsRepG1 (IsRepG0 + kNumStates)
1760 +#define IsRepG2 (IsRepG1 + kNumStates)
1761 +#define IsRep0Long (IsRepG2 + kNumStates)
1762 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
1763 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
1764 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
1765 +#define LenCoder (Align + kAlignTableSize)
1766 +#define RepLenCoder (LenCoder + kNumLenProbs)
1767 +#define Literal (RepLenCoder + kNumLenProbs)
1769 +#define LZMA_BASE_SIZE 1846
1770 +#define LZMA_LIT_SIZE 768
1772 +#define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
1774 +#if Literal != LZMA_BASE_SIZE
1775 +StopCompilingDueBUG
1779 +#define LZMA_STREAM_WAS_FINISHED_ID (-1)
1780 +#define LZMA_SPEC_LEN_OFFSET (-3)
1783 +Byte kLiteralNextStates[kNumStates * 2] =
1785 + 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5,
1786 + 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10
1789 +#define LZMA_DIC_MIN (1 << 12)
1791 +/* First LZMA-symbol is always decoded.
1792 +And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
1798 + < kMatchSpecLenStart : normal remain
1799 + = kMatchSpecLenStart : finished
1800 + = kMatchSpecLenStart + 1 : Flush marker
1801 + = kMatchSpecLenStart + 2 : State Init Marker
1804 +static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
1806 + CLzmaProb *probs = p->probs;
1808 + unsigned state = p->state;
1809 + UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
1810 + unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
1811 + unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
1812 + unsigned lc = p->prop.lc;
1814 + Byte *dic = p->dic;
1815 + SizeT dicBufSize = p->dicBufSize;
1816 + SizeT dicPos = p->dicPos;
1818 + UInt32 processedPos = p->processedPos;
1819 + UInt32 checkDicSize = p->checkDicSize;
1822 + const Byte *buf = p->buf;
1823 + UInt32 range = p->range;
1824 + UInt32 code = p->code;
1831 + unsigned posState = processedPos & pbMask;
1833 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
1838 + prob = probs + Literal;
1839 + if (checkDicSize != 0 || processedPos != 0)
1840 + prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
1841 + (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
1843 + if (state < kNumLitStates)
1846 + do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
1850 + unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1851 + unsigned offs = 0x100;
1856 + CLzmaProb *probLit;
1858 + bit = (matchByte & offs);
1859 + probLit = prob + offs + bit + symbol;
1860 + GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
1862 + while (symbol < 0x100);
1864 + dic[dicPos++] = (Byte)symbol;
1867 + state = kLiteralNextStates[state];
1868 + /* if (state < 4) state = 0; else if (state < 10) state -= 3; else state -= 6; */
1874 + prob = probs + IsRep + state;
1878 + state += kNumStates;
1879 + prob = probs + LenCoder;
1884 + if (checkDicSize == 0 && processedPos == 0)
1885 + return SZ_ERROR_DATA;
1886 + prob = probs + IsRepG0 + state;
1890 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
1894 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1897 + state = state < kNumLitStates ? 9 : 11;
1906 + prob = probs + IsRepG1 + state;
1915 + prob = probs + IsRepG2 + state;
1932 + state = state < kNumLitStates ? 8 : 11;
1933 + prob = probs + RepLenCoder;
1936 + unsigned limit, offset;
1937 + CLzmaProb *probLen = prob + LenChoice;
1940 + UPDATE_0(probLen);
1941 + probLen = prob + LenLow + (posState << kLenNumLowBits);
1943 + limit = (1 << kLenNumLowBits);
1947 + UPDATE_1(probLen);
1948 + probLen = prob + LenChoice2;
1951 + UPDATE_0(probLen);
1952 + probLen = prob + LenMid + (posState << kLenNumMidBits);
1953 + offset = kLenNumLowSymbols;
1954 + limit = (1 << kLenNumMidBits);
1958 + UPDATE_1(probLen);
1959 + probLen = prob + LenHigh;
1960 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
1961 + limit = (1 << kLenNumHighBits);
1964 + TREE_DECODE(probLen, limit, len);
1968 + if (state >= kNumStates)
1971 + prob = probs + PosSlot +
1972 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
1973 + TREE_6_DECODE(prob, distance);
1974 + if (distance >= kStartPosModelIndex)
1976 + unsigned posSlot = (unsigned)distance;
1977 + int numDirectBits = (int)(((distance >> 1) - 1));
1978 + distance = (2 | (distance & 1));
1979 + if (posSlot < kEndPosModelIndex)
1981 + distance <<= numDirectBits;
1982 + prob = probs + SpecPos + distance - posSlot - 1;
1988 + GET_BIT2(prob + i, i, ; , distance |= mask);
1991 + while(--numDirectBits != 0);
1996 + numDirectBits -= kNumAlignBits;
2005 + t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
2006 + distance = (distance << 1) + (t + 1);
2007 + code += range & t;
2011 + if (code >= range)
2018 + while (--numDirectBits != 0);
2019 + prob = probs + Align;
2020 + distance <<= kNumAlignBits;
2023 + GET_BIT2(prob + i, i, ; , distance |= 1);
2024 + GET_BIT2(prob + i, i, ; , distance |= 2);
2025 + GET_BIT2(prob + i, i, ; , distance |= 4);
2026 + GET_BIT2(prob + i, i, ; , distance |= 8);
2028 + if (distance == (UInt32)0xFFFFFFFF)
2030 + len += kMatchSpecLenStart;
2031 + state -= kNumStates;
2039 + rep0 = distance + 1;
2040 + if (checkDicSize == 0)
2042 + if (distance >= processedPos)
2043 + return SZ_ERROR_DATA;
2045 + else if (distance >= checkDicSize)
2046 + return SZ_ERROR_DATA;
2047 + state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
2048 + /* state = kLiteralNextStates[state]; */
2051 + len += kMatchMinLen;
2054 + SizeT rem = limit - dicPos;
2055 + unsigned curLen = ((rem < len) ? (unsigned)rem : len);
2056 + SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
2058 + processedPos += curLen;
2061 + if (pos + curLen <= dicBufSize)
2063 + Byte *dest = dic + dicPos;
2064 + ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
2065 + const Byte *lim = dest + curLen;
2068 + *(dest) = (Byte)*(dest + src);
2069 + while (++dest != lim);
2075 + dic[dicPos++] = dic[pos];
2076 + if (++pos == dicBufSize)
2079 + while (--curLen != 0);
2084 + while (dicPos < limit && buf < bufLimit);
2089 + p->remainLen = len;
2090 + p->dicPos = dicPos;
2091 + p->processedPos = processedPos;
2092 + p->reps[0] = rep0;
2093 + p->reps[1] = rep1;
2094 + p->reps[2] = rep2;
2095 + p->reps[3] = rep3;
2101 +static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
2103 + if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
2105 + Byte *dic = p->dic;
2106 + SizeT dicPos = p->dicPos;
2107 + SizeT dicBufSize = p->dicBufSize;
2108 + unsigned len = p->remainLen;
2109 + UInt32 rep0 = p->reps[0];
2110 + if (limit - dicPos < len)
2111 + len = (unsigned)(limit - dicPos);
2113 + if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
2114 + p->checkDicSize = p->prop.dicSize;
2116 + p->processedPos += len;
2117 + p->remainLen -= len;
2118 + while (len-- != 0)
2120 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
2123 + p->dicPos = dicPos;
2127 +/* LzmaDec_DecodeReal2 decodes LZMA-symbols and sets p->needFlush and p->needInit, if required. */
2129 +static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
2133 + SizeT limit2 = limit;
2134 + if (p->checkDicSize == 0)
2136 + UInt32 rem = p->prop.dicSize - p->processedPos;
2137 + if (limit - p->dicPos > rem)
2138 + limit2 = p->dicPos + rem;
2140 + RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
2141 + if (p->processedPos >= p->prop.dicSize)
2142 + p->checkDicSize = p->prop.dicSize;
2143 + LzmaDec_WriteRem(p, limit);
2145 + while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
2147 + if (p->remainLen > kMatchSpecLenStart)
2149 + p->remainLen = kMatchSpecLenStart;
2156 + DUMMY_ERROR, /* unexpected end of input stream */
2162 +static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
2164 + UInt32 range = p->range;
2165 + UInt32 code = p->code;
2166 + const Byte *bufLimit = buf + inSize;
2167 + CLzmaProb *probs = p->probs;
2168 + unsigned state = p->state;
2175 + unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
2177 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
2178 + IF_BIT_0_CHECK(prob)
2182 + /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
2184 + prob = probs + Literal;
2185 + if (p->checkDicSize != 0 || p->processedPos != 0)
2186 + prob += (LZMA_LIT_SIZE *
2187 + ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
2188 + (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
2190 + if (state < kNumLitStates)
2192 + unsigned symbol = 1;
2193 + do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
2197 + unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
2198 + ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
2199 + unsigned offs = 0x100;
2200 + unsigned symbol = 1;
2204 + CLzmaProb *probLit;
2206 + bit = (matchByte & offs);
2207 + probLit = prob + offs + bit + symbol;
2208 + GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
2210 + while (symbol < 0x100);
2219 + prob = probs + IsRep + state;
2220 + IF_BIT_0_CHECK(prob)
2224 + prob = probs + LenCoder;
2225 + res = DUMMY_MATCH;
2231 + prob = probs + IsRepG0 + state;
2232 + IF_BIT_0_CHECK(prob)
2235 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
2236 + IF_BIT_0_CHECK(prob)
2250 + prob = probs + IsRepG1 + state;
2251 + IF_BIT_0_CHECK(prob)
2258 + prob = probs + IsRepG2 + state;
2259 + IF_BIT_0_CHECK(prob)
2269 + state = kNumStates;
2270 + prob = probs + RepLenCoder;
2273 + unsigned limit, offset;
2274 + CLzmaProb *probLen = prob + LenChoice;
2275 + IF_BIT_0_CHECK(probLen)
2278 + probLen = prob + LenLow + (posState << kLenNumLowBits);
2280 + limit = 1 << kLenNumLowBits;
2285 + probLen = prob + LenChoice2;
2286 + IF_BIT_0_CHECK(probLen)
2289 + probLen = prob + LenMid + (posState << kLenNumMidBits);
2290 + offset = kLenNumLowSymbols;
2291 + limit = 1 << kLenNumMidBits;
2296 + probLen = prob + LenHigh;
2297 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
2298 + limit = 1 << kLenNumHighBits;
2301 + TREE_DECODE_CHECK(probLen, limit, len);
2308 + prob = probs + PosSlot +
2309 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
2311 + TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
2312 + if (posSlot >= kStartPosModelIndex)
2314 + int numDirectBits = ((posSlot >> 1) - 1);
2316 + /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
2318 + if (posSlot < kEndPosModelIndex)
2320 + prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
2324 + numDirectBits -= kNumAlignBits;
2329 + code -= range & (((code - range) >> 31) - 1);
2330 + /* if (code >= range) code -= range; */
2332 + while (--numDirectBits != 0);
2333 + prob = probs + Align;
2334 + numDirectBits = kNumAlignBits;
2340 + GET_BIT_CHECK(prob + i, i);
2342 + while(--numDirectBits != 0);
2353 +static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
2355 + p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
2356 + p->range = 0xFFFFFFFF;
2360 +void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
2364 + p->tempBufSize = 0;
2368 + p->processedPos = 0;
2369 + p->checkDicSize = 0;
2370 + p->needInitState = 1;
2373 + p->needInitState = 1;
2376 +void LzmaDec_Init(CLzmaDec *p)
2379 + LzmaDec_InitDicAndState(p, True, True);
2382 +static void LzmaDec_InitStateReal(CLzmaDec *p)
2384 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
2386 + CLzmaProb *probs = p->probs;
2387 + for (i = 0; i < numProbs; i++)
2388 + probs[i] = kBitModelTotal >> 1;
2389 + p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
2391 + p->needInitState = 0;
2394 +SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
2395 + ELzmaFinishMode finishMode, ELzmaStatus *status)
2397 + SizeT inSize = *srcLen;
2399 + LzmaDec_WriteRem(p, dicLimit);
2401 + *status = LZMA_STATUS_NOT_SPECIFIED;
2403 + while (p->remainLen != kMatchSpecLenStart)
2405 + int checkEndMarkNow;
2407 + if (p->needFlush != 0)
2409 + for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
2410 + p->tempBuf[p->tempBufSize++] = *src++;
2411 + if (p->tempBufSize < RC_INIT_SIZE)
2413 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2416 + if (p->tempBuf[0] != 0)
2417 + return SZ_ERROR_DATA;
2419 + LzmaDec_InitRc(p, p->tempBuf);
2420 + p->tempBufSize = 0;
2423 + checkEndMarkNow = 0;
2424 + if (p->dicPos >= dicLimit)
2426 + if (p->remainLen == 0 && p->code == 0)
2428 + *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
2431 + if (finishMode == LZMA_FINISH_ANY)
2433 + *status = LZMA_STATUS_NOT_FINISHED;
2436 + if (p->remainLen != 0)
2438 + *status = LZMA_STATUS_NOT_FINISHED;
2439 + return SZ_ERROR_DATA;
2441 + checkEndMarkNow = 1;
2444 + if (p->needInitState)
2445 + LzmaDec_InitStateReal(p);
2447 + if (p->tempBufSize == 0)
2450 + const Byte *bufLimit;
2451 + if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2453 + int dummyRes = LzmaDec_TryDummy(p, src, inSize);
2454 + if (dummyRes == DUMMY_ERROR)
2456 + memcpy(p->tempBuf, src, inSize);
2457 + p->tempBufSize = (unsigned)inSize;
2458 + (*srcLen) += inSize;
2459 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2462 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2464 + *status = LZMA_STATUS_NOT_FINISHED;
2465 + return SZ_ERROR_DATA;
2470 + bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
2472 + if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
2473 + return SZ_ERROR_DATA;
2474 + processed = p->buf - src;
2475 + (*srcLen) += processed;
2477 + inSize -= processed;
2481 + unsigned rem = p->tempBufSize, lookAhead = 0;
2482 + while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
2483 + p->tempBuf[rem++] = src[lookAhead++];
2484 + p->tempBufSize = rem;
2485 + if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2487 + int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
2488 + if (dummyRes == DUMMY_ERROR)
2490 + (*srcLen) += lookAhead;
2491 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2494 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2496 + *status = LZMA_STATUS_NOT_FINISHED;
2497 + return SZ_ERROR_DATA;
2500 + p->buf = p->tempBuf;
2501 + if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
2502 + return SZ_ERROR_DATA;
2503 + lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
2504 + (*srcLen) += lookAhead;
2506 + inSize -= lookAhead;
2507 + p->tempBufSize = 0;
2511 + *status = LZMA_STATUS_FINISHED_WITH_MARK;
2512 + return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
2515 +SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
2517 + SizeT outSize = *destLen;
2518 + SizeT inSize = *srcLen;
2519 + *srcLen = *destLen = 0;
2522 + SizeT inSizeCur = inSize, outSizeCur, dicPos;
2523 + ELzmaFinishMode curFinishMode;
2525 + if (p->dicPos == p->dicBufSize)
2527 + dicPos = p->dicPos;
2528 + if (outSize > p->dicBufSize - dicPos)
2530 + outSizeCur = p->dicBufSize;
2531 + curFinishMode = LZMA_FINISH_ANY;
2535 + outSizeCur = dicPos + outSize;
2536 + curFinishMode = finishMode;
2539 + res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
2541 + inSize -= inSizeCur;
2542 + *srcLen += inSizeCur;
2543 + outSizeCur = p->dicPos - dicPos;
2544 + memcpy(dest, p->dic + dicPos, outSizeCur);
2545 + dest += outSizeCur;
2546 + outSize -= outSizeCur;
2547 + *destLen += outSizeCur;
2550 + if (outSizeCur == 0 || outSize == 0)
2555 +void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
2557 + alloc->Free(alloc, p->probs);
2561 +static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
2563 + alloc->Free(alloc, p->dic);
2567 +void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
2569 + LzmaDec_FreeProbs(p, alloc);
2570 + LzmaDec_FreeDict(p, alloc);
2573 +SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
2578 + if (size < LZMA_PROPS_SIZE)
2579 + return SZ_ERROR_UNSUPPORTED;
2581 + dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
2583 + if (dicSize < LZMA_DIC_MIN)
2584 + dicSize = LZMA_DIC_MIN;
2585 + p->dicSize = dicSize;
2588 + if (d >= (9 * 5 * 5))
2589 + return SZ_ERROR_UNSUPPORTED;
2599 +static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
2601 + UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
2602 + if (p->probs == 0 || numProbs != p->numProbs)
2604 + LzmaDec_FreeProbs(p, alloc);
2605 + p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
2606 + p->numProbs = numProbs;
2607 + if (p->probs == 0)
2608 + return SZ_ERROR_MEM;
2613 +SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2615 + CLzmaProps propNew;
2616 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2617 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2618 + p->prop = propNew;
2622 +SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2624 + CLzmaProps propNew;
2626 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2627 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2628 + dicBufSize = propNew.dicSize;
2629 + if (p->dic == 0 || dicBufSize != p->dicBufSize)
2631 + LzmaDec_FreeDict(p, alloc);
2632 + p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
2635 + LzmaDec_FreeProbs(p, alloc);
2636 + return SZ_ERROR_MEM;
2639 + p->dicBufSize = dicBufSize;
2640 + p->prop = propNew;
2644 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
2645 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
2646 + ELzmaStatus *status, ISzAlloc *alloc)
2650 + SizeT inSize = *srcLen;
2651 + SizeT outSize = *destLen;
2652 + *srcLen = *destLen = 0;
2653 + if (inSize < RC_INIT_SIZE)
2654 + return SZ_ERROR_INPUT_EOF;
2656 + LzmaDec_Construct(&p);
2657 + res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
2661 + p.dicBufSize = outSize;
2666 + res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
2668 + if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
2669 + res = SZ_ERROR_INPUT_EOF;
2671 + (*destLen) = p.dicPos;
2672 + LzmaDec_FreeProbs(&p, alloc);
2675 --- a/lzma/LzmaEnc.c 1970-01-01 01:00:00.000000000 +0100
2676 +++ b/lzma/LzmaEnc.c 2010-03-20 23:19:47.815156000 +0100
2678 +/* LzmaEnc.c -- LZMA Encoder
2680 +Copyright (c) 1999-2008 Igor Pavlov
2681 +Read LzmaEnc.h for license options */
2683 +#if defined(SHOW_STAT) || defined(SHOW_STAT2)
2687 +#include <string.h>
2689 +#include "LzmaEnc.h"
2691 +#include "LzFind.h"
2692 +#ifdef COMPRESS_MF_MT
2693 +#include "LzFindMt.h"
2696 +/* #define SHOW_STAT */
2697 +/* #define SHOW_STAT2 */
2700 +static int ttt = 0;
2703 +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
2705 +#define kBlockSize (9 << 10)
2706 +#define kUnpackBlockSize (1 << 18)
2707 +#define kMatchArraySize (1 << 21)
2708 +#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
2710 +#define kNumMaxDirectBits (31)
2712 +#define kNumTopBits 24
2713 +#define kTopValue ((UInt32)1 << kNumTopBits)
2715 +#define kNumBitModelTotalBits 11
2716 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
2717 +#define kNumMoveBits 5
2718 +#define kProbInitValue (kBitModelTotal >> 1)
2720 +#define kNumMoveReducingBits 4
2721 +#define kNumBitPriceShiftBits 4
2722 +#define kBitPrice (1 << kNumBitPriceShiftBits)
2724 +void LzmaEncProps_Init(CLzmaEncProps *p)
2727 + p->dictSize = p->mc = 0;
2728 + p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
2729 + p->writeEndMark = 0;
2732 +void LzmaEncProps_Normalize(CLzmaEncProps *p)
2734 + int level = p->level;
2735 + if (level < 0) level = 5;
2737 + if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
2738 + if (p->lc < 0) p->lc = 3;
2739 + if (p->lp < 0) p->lp = 0;
2740 + if (p->pb < 0) p->pb = 2;
2741 + if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
2742 + if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
2743 + if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
2744 + if (p->numHashBytes < 0) p->numHashBytes = 4;
2745 + if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
2746 + if (p->numThreads < 0) p->numThreads = ((p->btMode && p->algo) ? 2 : 1);
2749 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
2751 + CLzmaEncProps props = *props2;
2752 + LzmaEncProps_Normalize(&props);
2753 + return props.dictSize;
2756 +/* #define LZMA_LOG_BSR */
2757 +/* Define it for Intel's CPU */
2760 +#ifdef LZMA_LOG_BSR
2762 +#define kDicLogSizeMaxCompress 30
2764 +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
2766 +UInt32 GetPosSlot1(UInt32 pos)
2769 + BSR2_RET(pos, res);
2772 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
2773 +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
2777 +#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
2778 +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
2780 +void LzmaEnc_FastPosInit(Byte *g_FastPos)
2782 + int c = 2, slotFast;
2786 + for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
2788 + UInt32 k = (1 << ((slotFast >> 1) - 1));
2790 + for (j = 0; j < k; j++, c++)
2791 + g_FastPos[c] = (Byte)slotFast;
2795 +#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
2796 + (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
2797 + res = p->g_FastPos[pos >> i] + (i * 2); }
2799 +#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
2800 + p->g_FastPos[pos >> 6] + 12 : \
2801 + p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
2804 +#define GetPosSlot1(pos) p->g_FastPos[pos]
2805 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
2806 +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
2811 +#define LZMA_NUM_REPS 4
2813 +typedef unsigned CState;
2815 +typedef struct _COptimal
2828 + UInt32 backs[LZMA_NUM_REPS];
2831 +#define kNumOpts (1 << 12)
2833 +#define kNumLenToPosStates 4
2834 +#define kNumPosSlotBits 6
2835 +#define kDicLogSizeMin 0
2836 +#define kDicLogSizeMax 32
2837 +#define kDistTableSizeMax (kDicLogSizeMax * 2)
2840 +#define kNumAlignBits 4
2841 +#define kAlignTableSize (1 << kNumAlignBits)
2842 +#define kAlignMask (kAlignTableSize - 1)
2844 +#define kStartPosModelIndex 4
2845 +#define kEndPosModelIndex 14
2846 +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
2848 +#define kNumFullDistances (1 << (kEndPosModelIndex / 2))
2850 +#ifdef _LZMA_PROB32
2851 +#define CLzmaProb UInt32
2853 +#define CLzmaProb UInt16
2856 +#define LZMA_PB_MAX 4
2857 +#define LZMA_LC_MAX 8
2858 +#define LZMA_LP_MAX 4
2860 +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
2863 +#define kLenNumLowBits 3
2864 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
2865 +#define kLenNumMidBits 3
2866 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
2867 +#define kLenNumHighBits 8
2868 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
2870 +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
2872 +#define LZMA_MATCH_LEN_MIN 2
2873 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
2875 +#define kNumStates 12
2880 + CLzmaProb choice2;
2881 + CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
2882 + CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
2883 + CLzmaProb high[kLenNumHighSymbols];
2889 + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
2891 + UInt32 counters[LZMA_NUM_PB_STATES_MAX];
2894 +typedef struct _CRangeEnc
2903 + ISeqOutStream *outStream;
2908 +typedef struct _CSeqInStreamBuf
2910 + ISeqInStream funcTable;
2915 +static SRes MyRead(void *pp, void *data, size_t *size)
2917 + size_t curSize = *size;
2918 + CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp;
2919 + if (p->rem < curSize)
2921 + memcpy(data, p->data, curSize);
2922 + p->rem -= curSize;
2923 + p->data += curSize;
2930 + CLzmaProb *litProbs;
2932 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
2933 + CLzmaProb isRep[kNumStates];
2934 + CLzmaProb isRepG0[kNumStates];
2935 + CLzmaProb isRepG1[kNumStates];
2936 + CLzmaProb isRepG2[kNumStates];
2937 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
2939 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
2940 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
2941 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
2943 + CLenPriceEnc lenEnc;
2944 + CLenPriceEnc repLenEnc;
2946 + UInt32 reps[LZMA_NUM_REPS];
2950 +typedef struct _CLzmaEnc
2952 + IMatchFinder matchFinder;
2953 + void *matchFinderObj;
2955 + #ifdef COMPRESS_MF_MT
2957 + CMatchFinderMt matchFinderMt;
2960 + CMatchFinder matchFinderBase;
2962 + #ifdef COMPRESS_MF_MT
2966 + UInt32 optimumEndIndex;
2967 + UInt32 optimumCurrentIndex;
2969 + Bool longestMatchWasFound;
2970 + UInt32 longestMatchLength;
2971 + UInt32 numDistancePairs;
2973 + COptimal opt[kNumOpts];
2975 + #ifndef LZMA_LOG_BSR
2976 + Byte g_FastPos[1 << kNumLogBits];
2979 + UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
2980 + UInt32 matchDistances[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
2981 + UInt32 numFastBytes;
2982 + UInt32 additionalOffset;
2983 + UInt32 reps[LZMA_NUM_REPS];
2986 + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
2987 + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
2988 + UInt32 alignPrices[kAlignTableSize];
2989 + UInt32 alignPriceCount;
2991 + UInt32 distTableSize;
2993 + unsigned lc, lp, pb;
2994 + unsigned lpMask, pbMask;
2996 + CLzmaProb *litProbs;
2998 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
2999 + CLzmaProb isRep[kNumStates];
3000 + CLzmaProb isRepG0[kNumStates];
3001 + CLzmaProb isRepG1[kNumStates];
3002 + CLzmaProb isRepG2[kNumStates];
3003 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
3005 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
3006 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
3007 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
3009 + CLenPriceEnc lenEnc;
3010 + CLenPriceEnc repLenEnc;
3018 + Bool writeEndMark;
3020 + UInt32 matchPriceCount;
3026 + UInt32 matchFinderCycles;
3028 + ISeqInStream *inStream;
3029 + CSeqInStreamBuf seqBufInStream;
3031 + CSaveState saveState;
3034 +void LzmaEnc_SaveState(CLzmaEncHandle pp)
3036 + CLzmaEnc *p = (CLzmaEnc *)pp;
3037 + CSaveState *dest = &p->saveState;
3039 + dest->lenEnc = p->lenEnc;
3040 + dest->repLenEnc = p->repLenEnc;
3041 + dest->state = p->state;
3043 + for (i = 0; i < kNumStates; i++)
3045 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
3046 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
3048 + for (i = 0; i < kNumLenToPosStates; i++)
3049 + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
3050 + memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
3051 + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
3052 + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
3053 + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
3054 + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
3055 + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
3056 + memcpy(dest->reps, p->reps, sizeof(p->reps));
3057 + memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
3060 +void LzmaEnc_RestoreState(CLzmaEncHandle pp)
3062 + CLzmaEnc *dest = (CLzmaEnc *)pp;
3063 + const CSaveState *p = &dest->saveState;
3065 + dest->lenEnc = p->lenEnc;
3066 + dest->repLenEnc = p->repLenEnc;
3067 + dest->state = p->state;
3069 + for (i = 0; i < kNumStates; i++)
3071 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
3072 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
3074 + for (i = 0; i < kNumLenToPosStates; i++)
3075 + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
3076 + memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
3077 + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
3078 + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
3079 + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
3080 + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
3081 + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
3082 + memcpy(dest->reps, p->reps, sizeof(p->reps));
3083 + memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
3086 +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
3088 + CLzmaEnc *p = (CLzmaEnc *)pp;
3089 + CLzmaEncProps props = *props2;
3090 + LzmaEncProps_Normalize(&props);
3092 + if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
3093 + props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))
3094 + return SZ_ERROR_PARAM;
3095 + p->dictSize = props.dictSize;
3096 + p->matchFinderCycles = props.mc;
3098 + unsigned fb = props.fb;
3101 + if (fb > LZMA_MATCH_LEN_MAX)
3102 + fb = LZMA_MATCH_LEN_MAX;
3103 + p->numFastBytes = fb;
3108 + p->fastMode = (props.algo == 0);
3109 + p->matchFinderBase.btMode = props.btMode;
3111 + UInt32 numHashBytes = 4;
3114 + if (props.numHashBytes < 2)
3116 + else if (props.numHashBytes < 4)
3117 + numHashBytes = props.numHashBytes;
3119 + p->matchFinderBase.numHashBytes = numHashBytes;
3122 + p->matchFinderBase.cutValue = props.mc;
3124 + p->writeEndMark = props.writeEndMark;
3126 + #ifdef COMPRESS_MF_MT
3128 + if (newMultiThread != _multiThread)
3130 + ReleaseMatchFinder();
3131 + _multiThread = newMultiThread;
3134 + p->multiThread = (props.numThreads > 1);
3140 +static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
3141 +static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
3142 +static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
3143 +static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
3146 + void UpdateChar() { Index = kLiteralNextStates[Index]; }
3147 + void UpdateMatch() { Index = kMatchNextStates[Index]; }
3148 + void UpdateRep() { Index = kRepNextStates[Index]; }
3149 + void UpdateShortRep() { Index = kShortRepNextStates[Index]; }
3152 +#define IsCharState(s) ((s) < 7)
3155 +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
3157 +#define kInfinityPrice (1 << 30)
3159 +static void RangeEnc_Construct(CRangeEnc *p)
3165 +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
3167 +#define RC_BUF_SIZE (1 << 16)
3168 +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
3170 + if (p->bufBase == 0)
3172 + p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
3173 + if (p->bufBase == 0)
3175 + p->bufLim = p->bufBase + RC_BUF_SIZE;
3180 +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
3182 + alloc->Free(alloc, p->bufBase);
3186 +static void RangeEnc_Init(CRangeEnc *p)
3188 + /* Stream.Init(); */
3190 + p->range = 0xFFFFFFFF;
3194 + p->buf = p->bufBase;
3200 +static void RangeEnc_FlushStream(CRangeEnc *p)
3203 + if (p->res != SZ_OK)
3205 + num = p->buf - p->bufBase;
3206 + if (num != p->outStream->Write(p->outStream, p->bufBase, num))
3207 + p->res = SZ_ERROR_WRITE;
3208 + p->processed += num;
3209 + p->buf = p->bufBase;
3212 +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
3214 + if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
3216 + Byte temp = p->cache;
3219 + Byte *buf = p->buf;
3220 + *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
3222 + if (buf == p->bufLim)
3223 + RangeEnc_FlushStream(p);
3226 + while (--p->cacheSize != 0);
3227 + p->cache = (Byte)((UInt32)p->low >> 24);
3230 + p->low = (UInt32)p->low << 8;
3233 +static void RangeEnc_FlushData(CRangeEnc *p)
3236 + for (i = 0; i < 5; i++)
3237 + RangeEnc_ShiftLow(p);
3240 +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
3245 + p->low += p->range & (0 - ((value >> --numBits) & 1));
3246 + if (p->range < kTopValue)
3249 + RangeEnc_ShiftLow(p);
3252 + while (numBits != 0);
3255 +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
3257 + UInt32 ttt = *prob;
3258 + UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
3261 + p->range = newBound;
3262 + ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
3266 + p->low += newBound;
3267 + p->range -= newBound;
3268 + ttt -= ttt >> kNumMoveBits;
3270 + *prob = (CLzmaProb)ttt;
3271 + if (p->range < kTopValue)
3274 + RangeEnc_ShiftLow(p);
3278 +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
3283 + RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
3286 + while (symbol < 0x10000);
3289 +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
3291 + UInt32 offs = 0x100;
3296 + RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
3298 + offs &= ~(matchByte ^ symbol);
3300 + while (symbol < 0x10000);
3303 +void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
3306 + for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
3308 + const int kCyclesBits = kNumBitPriceShiftBits;
3310 + UInt32 bitCount = 0;
3312 + for (j = 0; j < kCyclesBits; j++)
3316 + while (w >= ((UInt32)1 << 16))
3322 + ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
3327 +#define GET_PRICE(prob, symbol) \
3328 + p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3330 +#define GET_PRICEa(prob, symbol) \
3331 + ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3333 +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
3334 +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3336 +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
3337 +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3339 +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
3345 + price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
3348 + while (symbol < 0x10000);
3352 +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
3355 + UInt32 offs = 0x100;
3360 + price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
3362 + offs &= ~(matchByte ^ symbol);
3364 + while (symbol < 0x10000);
3369 +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3373 + for (i = numBitLevels; i != 0 ;)
3377 + bit = (symbol >> i) & 1;
3378 + RangeEnc_EncodeBit(rc, probs + m, bit);
3379 + m = (m << 1) | bit;
3383 +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3387 + for (i = 0; i < numBitLevels; i++)
3389 + UInt32 bit = symbol & 1;
3390 + RangeEnc_EncodeBit(rc, probs + m, bit);
3391 + m = (m << 1) | bit;
3396 +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3399 + symbol |= (1 << numBitLevels);
3400 + while (symbol != 1)
3402 + price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
3408 +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3413 + for (i = numBitLevels; i != 0; i--)
3415 + UInt32 bit = symbol & 1;
3417 + price += GET_PRICEa(probs[m], bit);
3418 + m = (m << 1) | bit;
3424 +static void LenEnc_Init(CLenEnc *p)
3427 + p->choice = p->choice2 = kProbInitValue;
3428 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
3429 + p->low[i] = kProbInitValue;
3430 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
3431 + p->mid[i] = kProbInitValue;
3432 + for (i = 0; i < kLenNumHighSymbols; i++)
3433 + p->high[i] = kProbInitValue;
3436 +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
3438 + if (symbol < kLenNumLowSymbols)
3440 + RangeEnc_EncodeBit(rc, &p->choice, 0);
3441 + RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
3445 + RangeEnc_EncodeBit(rc, &p->choice, 1);
3446 + if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
3448 + RangeEnc_EncodeBit(rc, &p->choice2, 0);
3449 + RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
3453 + RangeEnc_EncodeBit(rc, &p->choice2, 1);
3454 + RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
3459 +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
3461 + UInt32 a0 = GET_PRICE_0a(p->choice);
3462 + UInt32 a1 = GET_PRICE_1a(p->choice);
3463 + UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
3464 + UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
3466 + for (i = 0; i < kLenNumLowSymbols; i++)
3468 + if (i >= numSymbols)
3470 + prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
3472 + for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
3474 + if (i >= numSymbols)
3476 + prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
3478 + for (; i < numSymbols; i++)
3479 + prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
3482 +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
3484 + LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
3485 + p->counters[posState] = p->tableSize;
3488 +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
3491 + for (posState = 0; posState < numPosStates; posState++)
3492 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3495 +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
3497 + LenEnc_Encode(&p->p, rc, symbol, posState);
3499 + if (--p->counters[posState] == 0)
3500 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3506 +static void MovePos(CLzmaEnc *p, UInt32 num)
3510 + printf("\n MovePos %d", num);
3514 + p->additionalOffset += num;
3515 + p->matchFinder.Skip(p->matchFinderObj, num);
3519 +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
3521 + UInt32 lenRes = 0, numDistancePairs;
3522 + numDistancePairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matchDistances);
3524 + printf("\n i = %d numPairs = %d ", ttt, numDistancePairs / 2);
3531 + for (i = 0; i < numDistancePairs; i += 2)
3532 + printf("%2d %6d | ", p->matchDistances[i], p->matchDistances[i + 1]);
3535 + if (numDistancePairs > 0)
3537 + lenRes = p->matchDistances[numDistancePairs - 2];
3538 + if (lenRes == p->numFastBytes)
3540 + UInt32 numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) + 1;
3541 + const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3542 + UInt32 distance = p->matchDistances[numDistancePairs - 1] + 1;
3543 + if (numAvail > LZMA_MATCH_LEN_MAX)
3544 + numAvail = LZMA_MATCH_LEN_MAX;
3547 + const Byte *pby2 = pby - distance;
3548 + for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
3552 + p->additionalOffset++;
3553 + *numDistancePairsRes = numDistancePairs;
3558 +#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
3559 +#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
3560 +#define IsShortRep(p) ((p)->backPrev == 0)
3562 +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
3565 + GET_PRICE_0(p->isRepG0[state]) +
3566 + GET_PRICE_0(p->isRep0Long[state][posState]);
3569 +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
3572 + if (repIndex == 0)
3574 + price = GET_PRICE_0(p->isRepG0[state]);
3575 + price += GET_PRICE_1(p->isRep0Long[state][posState]);
3579 + price = GET_PRICE_1(p->isRepG0[state]);
3580 + if (repIndex == 1)
3581 + price += GET_PRICE_0(p->isRepG1[state]);
3584 + price += GET_PRICE_1(p->isRepG1[state]);
3585 + price += GET_PRICE(p->isRepG2[state], repIndex - 2);
3591 +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
3593 + return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
3594 + GetPureRepPrice(p, repIndex, state, posState);
3597 +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
3599 + UInt32 posMem = p->opt[cur].posPrev;
3600 + UInt32 backMem = p->opt[cur].backPrev;
3601 + p->optimumEndIndex = cur;
3604 + if (p->opt[cur].prev1IsChar)
3606 + MakeAsChar(&p->opt[posMem])
3607 + p->opt[posMem].posPrev = posMem - 1;
3608 + if (p->opt[cur].prev2)
3610 + p->opt[posMem - 1].prev1IsChar = False;
3611 + p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
3612 + p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
3616 + UInt32 posPrev = posMem;
3617 + UInt32 backCur = backMem;
3619 + backMem = p->opt[posPrev].backPrev;
3620 + posMem = p->opt[posPrev].posPrev;
3622 + p->opt[posPrev].backPrev = backCur;
3623 + p->opt[posPrev].posPrev = cur;
3628 + *backRes = p->opt[0].backPrev;
3629 + p->optimumCurrentIndex = p->opt[0].posPrev;
3630 + return p->optimumCurrentIndex;
3633 +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
3635 +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
3637 + UInt32 numAvailableBytes, lenMain, numDistancePairs;
3639 + UInt32 reps[LZMA_NUM_REPS];
3640 + UInt32 repLens[LZMA_NUM_REPS];
3641 + UInt32 repMaxIndex, i;
3642 + UInt32 *matchDistances;
3643 + Byte currentByte, matchByte;
3645 + UInt32 matchPrice, repMatchPrice;
3648 + UInt32 normalMatchPrice;
3650 + if (p->optimumEndIndex != p->optimumCurrentIndex)
3652 + const COptimal *opt = &p->opt[p->optimumCurrentIndex];
3653 + UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
3654 + *backRes = opt->backPrev;
3655 + p->optimumCurrentIndex = opt->posPrev;
3658 + p->optimumCurrentIndex = p->optimumEndIndex = 0;
3660 + numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
3662 + if (!p->longestMatchWasFound)
3664 + lenMain = ReadMatchDistances(p, &numDistancePairs);
3668 + lenMain = p->longestMatchLength;
3669 + numDistancePairs = p->numDistancePairs;
3670 + p->longestMatchWasFound = False;
3673 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3674 + if (numAvailableBytes < 2)
3676 + *backRes = (UInt32)(-1);
3679 + if (numAvailableBytes > LZMA_MATCH_LEN_MAX)
3680 + numAvailableBytes = LZMA_MATCH_LEN_MAX;
3683 + for (i = 0; i < LZMA_NUM_REPS; i++)
3686 + const Byte *data2;
3687 + reps[i] = p->reps[i];
3688 + data2 = data - (reps[i] + 1);
3689 + if (data[0] != data2[0] || data[1] != data2[1])
3694 + for (lenTest = 2; lenTest < numAvailableBytes && data[lenTest] == data2[lenTest]; lenTest++);
3695 + repLens[i] = lenTest;
3696 + if (lenTest > repLens[repMaxIndex])
3699 + if (repLens[repMaxIndex] >= p->numFastBytes)
3702 + *backRes = repMaxIndex;
3703 + lenRes = repLens[repMaxIndex];
3704 + MovePos(p, lenRes - 1);
3708 + matchDistances = p->matchDistances;
3709 + if (lenMain >= p->numFastBytes)
3711 + *backRes = matchDistances[numDistancePairs - 1] + LZMA_NUM_REPS;
3712 + MovePos(p, lenMain - 1);
3715 + currentByte = *data;
3716 + matchByte = *(data - (reps[0] + 1));
3718 + if (lenMain < 2 && currentByte != matchByte && repLens[repMaxIndex] < 2)
3720 + *backRes = (UInt32)-1;
3724 + p->opt[0].state = (CState)p->state;
3726 + posState = (position & p->pbMask);
3729 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
3730 + p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
3731 + (!IsCharState(p->state) ?
3732 + LitEnc_GetPriceMatched(probs, currentByte, matchByte, p->ProbPrices) :
3733 + LitEnc_GetPrice(probs, currentByte, p->ProbPrices));
3736 + MakeAsChar(&p->opt[1]);
3738 + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
3739 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
3741 + if (matchByte == currentByte)
3743 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
3744 + if (shortRepPrice < p->opt[1].price)
3746 + p->opt[1].price = shortRepPrice;
3747 + MakeAsShortRep(&p->opt[1]);
3750 + lenEnd = ((lenMain >= repLens[repMaxIndex]) ? lenMain : repLens[repMaxIndex]);
3754 + *backRes = p->opt[1].backPrev;
3758 + p->opt[1].posPrev = 0;
3759 + for (i = 0; i < LZMA_NUM_REPS; i++)
3760 + p->opt[0].backs[i] = reps[i];
3764 + p->opt[len--].price = kInfinityPrice;
3767 + for (i = 0; i < LZMA_NUM_REPS; i++)
3769 + UInt32 repLen = repLens[i];
3773 + price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
3776 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
3777 + COptimal *opt = &p->opt[repLen];
3778 + if (curAndLenPrice < opt->price)
3780 + opt->price = curAndLenPrice;
3782 + opt->backPrev = i;
3783 + opt->prev1IsChar = False;
3786 + while (--repLen >= 2);
3789 + normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
3791 + len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
3792 + if (len <= lenMain)
3795 + while (len > matchDistances[offs])
3800 + UInt32 distance = matchDistances[offs + 1];
3802 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
3803 + UInt32 lenToPosState = GetLenToPosState(len);
3804 + if (distance < kNumFullDistances)
3805 + curAndLenPrice += p->distancesPrices[lenToPosState][distance];
3809 + GetPosSlot2(distance, slot);
3810 + curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
3812 + opt = &p->opt[len];
3813 + if (curAndLenPrice < opt->price)
3815 + opt->price = curAndLenPrice;
3817 + opt->backPrev = distance + LZMA_NUM_REPS;
3818 + opt->prev1IsChar = False;
3820 + if (len == matchDistances[offs])
3823 + if (offs == numDistancePairs)
3832 + if (position >= 0)
3835 + printf("\n pos = %4X", position);
3836 + for (i = cur; i <= lenEnd; i++)
3837 + printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
3843 + UInt32 numAvailableBytesFull, newLen, numDistancePairs;
3850 + Byte currentByte, matchByte;
3852 + UInt32 curAnd1Price;
3853 + COptimal *nextOpt;
3854 + UInt32 matchPrice, repMatchPrice;
3855 + UInt32 numAvailableBytes;
3859 + if (cur == lenEnd)
3860 + return Backward(p, backRes, cur);
3862 + numAvailableBytesFull = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
3863 + newLen = ReadMatchDistances(p, &numDistancePairs);
3864 + if (newLen >= p->numFastBytes)
3866 + p->numDistancePairs = numDistancePairs;
3867 + p->longestMatchLength = newLen;
3868 + p->longestMatchWasFound = True;
3869 + return Backward(p, backRes, cur);
3872 + curOpt = &p->opt[cur];
3873 + posPrev = curOpt->posPrev;
3874 + if (curOpt->prev1IsChar)
3877 + if (curOpt->prev2)
3879 + state = p->opt[curOpt->posPrev2].state;
3880 + if (curOpt->backPrev2 < LZMA_NUM_REPS)
3881 + state = kRepNextStates[state];
3883 + state = kMatchNextStates[state];
3886 + state = p->opt[posPrev].state;
3887 + state = kLiteralNextStates[state];
3890 + state = p->opt[posPrev].state;
3891 + if (posPrev == cur - 1)
3893 + if (IsShortRep(curOpt))
3894 + state = kShortRepNextStates[state];
3896 + state = kLiteralNextStates[state];
3901 + const COptimal *prevOpt;
3902 + if (curOpt->prev1IsChar && curOpt->prev2)
3904 + posPrev = curOpt->posPrev2;
3905 + pos = curOpt->backPrev2;
3906 + state = kRepNextStates[state];
3910 + pos = curOpt->backPrev;
3911 + if (pos < LZMA_NUM_REPS)
3912 + state = kRepNextStates[state];
3914 + state = kMatchNextStates[state];
3916 + prevOpt = &p->opt[posPrev];
3917 + if (pos < LZMA_NUM_REPS)
3920 + reps[0] = prevOpt->backs[pos];
3921 + for (i = 1; i <= pos; i++)
3922 + reps[i] = prevOpt->backs[i - 1];
3923 + for (; i < LZMA_NUM_REPS; i++)
3924 + reps[i] = prevOpt->backs[i];
3929 + reps[0] = (pos - LZMA_NUM_REPS);
3930 + for (i = 1; i < LZMA_NUM_REPS; i++)
3931 + reps[i] = prevOpt->backs[i - 1];
3934 + curOpt->state = (CState)state;
3936 + curOpt->backs[0] = reps[0];
3937 + curOpt->backs[1] = reps[1];
3938 + curOpt->backs[2] = reps[2];
3939 + curOpt->backs[3] = reps[3];
3941 + curPrice = curOpt->price;
3942 + nextIsChar = False;
3943 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3944 + currentByte = *data;
3945 + matchByte = *(data - (reps[0] + 1));
3947 + posState = (position & p->pbMask);
3949 + curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
3951 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
3953 + (!IsCharState(state) ?
3954 + LitEnc_GetPriceMatched(probs, currentByte, matchByte, p->ProbPrices) :
3955 + LitEnc_GetPrice(probs, currentByte, p->ProbPrices));
3958 + nextOpt = &p->opt[cur + 1];
3960 + if (curAnd1Price < nextOpt->price)
3962 + nextOpt->price = curAnd1Price;
3963 + nextOpt->posPrev = cur;
3964 + MakeAsChar(nextOpt);
3965 + nextIsChar = True;
3968 + matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
3969 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
3971 + if (matchByte == currentByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
3973 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
3974 + if (shortRepPrice <= nextOpt->price)
3976 + nextOpt->price = shortRepPrice;
3977 + nextOpt->posPrev = cur;
3978 + MakeAsShortRep(nextOpt);
3979 + nextIsChar = True;
3984 + UInt32 temp = kNumOpts - 1 - cur;
3985 + if (temp < numAvailableBytesFull)
3986 + numAvailableBytesFull = temp;
3988 + numAvailableBytes = numAvailableBytesFull;
3990 + if (numAvailableBytes < 2)
3992 + if (numAvailableBytes > p->numFastBytes)
3993 + numAvailableBytes = p->numFastBytes;
3994 + if (!nextIsChar && matchByte != currentByte) /* speed optimization */
3996 + /* try Literal + rep0 */
3999 + const Byte *data2 = data - (reps[0] + 1);
4000 + UInt32 limit = p->numFastBytes + 1;
4001 + if (limit > numAvailableBytesFull)
4002 + limit = numAvailableBytesFull;
4004 + for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
4005 + lenTest2 = temp - 1;
4006 + if (lenTest2 >= 2)
4008 + UInt32 state2 = kLiteralNextStates[state];
4009 + UInt32 posStateNext = (position + 1) & p->pbMask;
4010 + UInt32 nextRepMatchPrice = curAnd1Price +
4011 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4012 + GET_PRICE_1(p->isRep[state2]);
4013 + /* for (; lenTest2 >= 2; lenTest2--) */
4015 + UInt32 curAndLenPrice;
4017 + UInt32 offset = cur + 1 + lenTest2;
4018 + while (lenEnd < offset)
4019 + p->opt[++lenEnd].price = kInfinityPrice;
4020 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4021 + opt = &p->opt[offset];
4022 + if (curAndLenPrice < opt->price)
4024 + opt->price = curAndLenPrice;
4025 + opt->posPrev = cur + 1;
4026 + opt->backPrev = 0;
4027 + opt->prev1IsChar = True;
4028 + opt->prev2 = False;
4034 + startLen = 2; /* speed optimization */
4037 + for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
4040 + UInt32 lenTestTemp;
4042 + const Byte *data2 = data - (reps[repIndex] + 1);
4043 + if (data[0] != data2[0] || data[1] != data2[1])
4045 + for (lenTest = 2; lenTest < numAvailableBytes && data[lenTest] == data2[lenTest]; lenTest++);
4046 + while (lenEnd < cur + lenTest)
4047 + p->opt[++lenEnd].price = kInfinityPrice;
4048 + lenTestTemp = lenTest;
4049 + price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
4052 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
4053 + COptimal *opt = &p->opt[cur + lenTest];
4054 + if (curAndLenPrice < opt->price)
4056 + opt->price = curAndLenPrice;
4057 + opt->posPrev = cur;
4058 + opt->backPrev = repIndex;
4059 + opt->prev1IsChar = False;
4062 + while (--lenTest >= 2);
4063 + lenTest = lenTestTemp;
4065 + if (repIndex == 0)
4066 + startLen = lenTest + 1;
4068 + /* if (_maxMode) */
4070 + UInt32 lenTest2 = lenTest + 1;
4071 + UInt32 limit = lenTest2 + p->numFastBytes;
4072 + UInt32 nextRepMatchPrice;
4073 + if (limit > numAvailableBytesFull)
4074 + limit = numAvailableBytesFull;
4075 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
4076 + lenTest2 -= lenTest + 1;
4077 + if (lenTest2 >= 2)
4079 + UInt32 state2 = kRepNextStates[state];
4080 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
4081 + UInt32 curAndLenCharPrice =
4082 + price + p->repLenEnc.prices[posState][lenTest - 2] +
4083 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
4084 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
4085 + data[lenTest], data2[lenTest], p->ProbPrices);
4086 + state2 = kLiteralNextStates[state2];
4087 + posStateNext = (position + lenTest + 1) & p->pbMask;
4088 + nextRepMatchPrice = curAndLenCharPrice +
4089 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4090 + GET_PRICE_1(p->isRep[state2]);
4092 + /* for (; lenTest2 >= 2; lenTest2--) */
4094 + UInt32 curAndLenPrice;
4096 + UInt32 offset = cur + lenTest + 1 + lenTest2;
4097 + while (lenEnd < offset)
4098 + p->opt[++lenEnd].price = kInfinityPrice;
4099 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4100 + opt = &p->opt[offset];
4101 + if (curAndLenPrice < opt->price)
4103 + opt->price = curAndLenPrice;
4104 + opt->posPrev = cur + lenTest + 1;
4105 + opt->backPrev = 0;
4106 + opt->prev1IsChar = True;
4107 + opt->prev2 = True;
4108 + opt->posPrev2 = cur;
4109 + opt->backPrev2 = repIndex;
4116 + /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
4117 + if (newLen > numAvailableBytes)
4119 + newLen = numAvailableBytes;
4120 + for (numDistancePairs = 0; newLen > matchDistances[numDistancePairs]; numDistancePairs += 2);
4121 + matchDistances[numDistancePairs] = newLen;
4122 + numDistancePairs += 2;
4124 + if (newLen >= startLen)
4126 + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
4127 + UInt32 offs, curBack, posSlot;
4129 + while (lenEnd < cur + newLen)
4130 + p->opt[++lenEnd].price = kInfinityPrice;
4133 + while (startLen > matchDistances[offs])
4135 + curBack = matchDistances[offs + 1];
4136 + GetPosSlot2(curBack, posSlot);
4137 + for (lenTest = /*2*/ startLen; ; lenTest++)
4139 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
4140 + UInt32 lenToPosState = GetLenToPosState(lenTest);
4142 + if (curBack < kNumFullDistances)
4143 + curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
4145 + curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
4147 + opt = &p->opt[cur + lenTest];
4148 + if (curAndLenPrice < opt->price)
4150 + opt->price = curAndLenPrice;
4151 + opt->posPrev = cur;
4152 + opt->backPrev = curBack + LZMA_NUM_REPS;
4153 + opt->prev1IsChar = False;
4156 + if (/*_maxMode && */lenTest == matchDistances[offs])
4158 + /* Try Match + Literal + Rep0 */
4159 + const Byte *data2 = data - (curBack + 1);
4160 + UInt32 lenTest2 = lenTest + 1;
4161 + UInt32 limit = lenTest2 + p->numFastBytes;
4162 + UInt32 nextRepMatchPrice;
4163 + if (limit > numAvailableBytesFull)
4164 + limit = numAvailableBytesFull;
4165 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
4166 + lenTest2 -= lenTest + 1;
4167 + if (lenTest2 >= 2)
4169 + UInt32 state2 = kMatchNextStates[state];
4170 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
4171 + UInt32 curAndLenCharPrice = curAndLenPrice +
4172 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
4173 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
4174 + data[lenTest], data2[lenTest], p->ProbPrices);
4175 + state2 = kLiteralNextStates[state2];
4176 + posStateNext = (posStateNext + 1) & p->pbMask;
4177 + nextRepMatchPrice = curAndLenCharPrice +
4178 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
4179 + GET_PRICE_1(p->isRep[state2]);
4181 + /* for (; lenTest2 >= 2; lenTest2--) */
4183 + UInt32 offset = cur + lenTest + 1 + lenTest2;
4184 + UInt32 curAndLenPrice;
4186 + while (lenEnd < offset)
4187 + p->opt[++lenEnd].price = kInfinityPrice;
4188 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
4189 + opt = &p->opt[offset];
4190 + if (curAndLenPrice < opt->price)
4192 + opt->price = curAndLenPrice;
4193 + opt->posPrev = cur + lenTest + 1;
4194 + opt->backPrev = 0;
4195 + opt->prev1IsChar = True;
4196 + opt->prev2 = True;
4197 + opt->posPrev2 = cur;
4198 + opt->backPrev2 = curBack + LZMA_NUM_REPS;
4203 + if (offs == numDistancePairs)
4205 + curBack = matchDistances[offs + 1];
4206 + if (curBack >= kNumFullDistances)
4207 + GetPosSlot2(curBack, posSlot);
4214 +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
4216 +static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
4218 + UInt32 numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
4219 + UInt32 lenMain, numDistancePairs;
4221 + UInt32 repLens[LZMA_NUM_REPS];
4222 + UInt32 repMaxIndex, i;
4223 + UInt32 *matchDistances;
4226 + if (!p->longestMatchWasFound)
4228 + lenMain = ReadMatchDistances(p, &numDistancePairs);
4232 + lenMain = p->longestMatchLength;
4233 + numDistancePairs = p->numDistancePairs;
4234 + p->longestMatchWasFound = False;
4237 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
4238 + if (numAvailableBytes > LZMA_MATCH_LEN_MAX)
4239 + numAvailableBytes = LZMA_MATCH_LEN_MAX;
4240 + if (numAvailableBytes < 2)
4242 + *backRes = (UInt32)(-1);
4248 + for (i = 0; i < LZMA_NUM_REPS; i++)
4250 + const Byte *data2 = data - (p->reps[i] + 1);
4252 + if (data[0] != data2[0] || data[1] != data2[1])
4257 + for (len = 2; len < numAvailableBytes && data[len] == data2[len]; len++);
4258 + if (len >= p->numFastBytes)
4261 + MovePos(p, len - 1);
4265 + if (len > repLens[repMaxIndex])
4268 + matchDistances = p->matchDistances;
4269 + if (lenMain >= p->numFastBytes)
4271 + *backRes = matchDistances[numDistancePairs - 1] + LZMA_NUM_REPS;
4272 + MovePos(p, lenMain - 1);
4276 + backMain = 0; /* for GCC */
4279 + backMain = matchDistances[numDistancePairs - 1];
4280 + while (numDistancePairs > 2 && lenMain == matchDistances[numDistancePairs - 4] + 1)
4282 + if (!ChangePair(matchDistances[numDistancePairs - 3], backMain))
4284 + numDistancePairs -= 2;
4285 + lenMain = matchDistances[numDistancePairs - 2];
4286 + backMain = matchDistances[numDistancePairs - 1];
4288 + if (lenMain == 2 && backMain >= 0x80)
4292 + if (repLens[repMaxIndex] >= 2)
4294 + if (repLens[repMaxIndex] + 1 >= lenMain ||
4295 + (repLens[repMaxIndex] + 2 >= lenMain && (backMain > (1 << 9))) ||
4296 + (repLens[repMaxIndex] + 3 >= lenMain && (backMain > (1 << 15))))
4299 + *backRes = repMaxIndex;
4300 + lenRes = repLens[repMaxIndex];
4301 + MovePos(p, lenRes - 1);
4306 + if (lenMain >= 2 && numAvailableBytes > 2)
4309 + numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
4310 + p->longestMatchLength = ReadMatchDistances(p, &p->numDistancePairs);
4311 + if (p->longestMatchLength >= 2)
4313 + UInt32 newDistance = matchDistances[p->numDistancePairs - 1];
4314 + if ((p->longestMatchLength >= lenMain && newDistance < backMain) ||
4315 + (p->longestMatchLength == lenMain + 1 && !ChangePair(backMain, newDistance)) ||
4316 + (p->longestMatchLength > lenMain + 1) ||
4317 + (p->longestMatchLength + 1 >= lenMain && lenMain >= 3 && ChangePair(newDistance, backMain)))
4319 + p->longestMatchWasFound = True;
4320 + *backRes = (UInt32)(-1);
4324 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
4325 + for (i = 0; i < LZMA_NUM_REPS; i++)
4328 + const Byte *data2 = data - (p->reps[i] + 1);
4329 + if (data[1] != data2[1] || data[2] != data2[2])
4334 + for (len = 2; len < numAvailableBytes && data[len] == data2[len]; len++);
4335 + if (len + 1 >= lenMain)
4337 + p->longestMatchWasFound = True;
4338 + *backRes = (UInt32)(-1);
4342 + *backRes = backMain + LZMA_NUM_REPS;
4343 + MovePos(p, lenMain - 2);
4346 + *backRes = (UInt32)(-1);
4350 +static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
4353 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4354 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4355 + p->state = kMatchNextStates[p->state];
4356 + len = LZMA_MATCH_LEN_MIN;
4357 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4358 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
4359 + RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
4360 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
4363 +static SRes CheckErrors(CLzmaEnc *p)
4365 + if (p->result != SZ_OK)
4367 + if (p->rc.res != SZ_OK)
4368 + p->result = SZ_ERROR_WRITE;
4369 + if (p->matchFinderBase.result != SZ_OK)
4370 + p->result = SZ_ERROR_READ;
4371 + if (p->result != SZ_OK)
4372 + p->finished = True;
4376 +static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
4378 + /* ReleaseMFStream(); */
4379 + p->finished = True;
4380 + if (p->writeEndMark)
4381 + WriteEndMarker(p, nowPos & p->pbMask);
4382 + RangeEnc_FlushData(&p->rc);
4383 + RangeEnc_FlushStream(&p->rc);
4384 + return CheckErrors(p);
4387 +static void FillAlignPrices(CLzmaEnc *p)
4390 + for (i = 0; i < kAlignTableSize; i++)
4391 + p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
4392 + p->alignPriceCount = 0;
4395 +static void FillDistancesPrices(CLzmaEnc *p)
4397 + UInt32 tempPrices[kNumFullDistances];
4398 + UInt32 i, lenToPosState;
4399 + for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
4401 + UInt32 posSlot = GetPosSlot1(i);
4402 + UInt32 footerBits = ((posSlot >> 1) - 1);
4403 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4404 + tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
4407 + for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
4410 + const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
4411 + UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
4412 + for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
4413 + posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
4414 + for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
4415 + posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
4418 + UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
4420 + for (i = 0; i < kStartPosModelIndex; i++)
4421 + distancesPrices[i] = posSlotPrices[i];
4422 + for (; i < kNumFullDistances; i++)
4423 + distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
4426 + p->matchPriceCount = 0;
4429 +void LzmaEnc_Construct(CLzmaEnc *p)
4431 + RangeEnc_Construct(&p->rc);
4432 + MatchFinder_Construct(&p->matchFinderBase);
4433 + #ifdef COMPRESS_MF_MT
4434 + MatchFinderMt_Construct(&p->matchFinderMt);
4435 + p->matchFinderMt.MatchFinder = &p->matchFinderBase;
4439 + CLzmaEncProps props;
4440 + LzmaEncProps_Init(&props);
4441 + LzmaEnc_SetProps(p, &props);
4444 + #ifndef LZMA_LOG_BSR
4445 + LzmaEnc_FastPosInit(p->g_FastPos);
4448 + LzmaEnc_InitPriceTables(p->ProbPrices);
4450 + p->saveState.litProbs = 0;
4453 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
4456 + p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
4458 + LzmaEnc_Construct((CLzmaEnc *)p);
4462 +void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
4464 + alloc->Free(alloc, p->litProbs);
4465 + alloc->Free(alloc, p->saveState.litProbs);
4467 + p->saveState.litProbs = 0;
4470 +void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
4472 + #ifdef COMPRESS_MF_MT
4473 + MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
4475 + MatchFinder_Free(&p->matchFinderBase, allocBig);
4476 + LzmaEnc_FreeLits(p, alloc);
4477 + RangeEnc_Free(&p->rc, alloc);
4480 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
4482 + LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
4483 + alloc->Free(alloc, p);
4486 +static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
4488 + UInt32 nowPos32, startPos32;
4489 + if (p->inStream != 0)
4491 + p->matchFinderBase.stream = p->inStream;
4492 + p->matchFinder.Init(p->matchFinderObj);
4498 + RINOK(CheckErrors(p));
4500 + nowPos32 = (UInt32)p->nowPos64;
4501 + startPos32 = nowPos32;
4503 + if (p->nowPos64 == 0)
4505 + UInt32 numDistancePairs;
4507 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4508 + return Flush(p, nowPos32);
4509 + ReadMatchDistances(p, &numDistancePairs);
4510 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
4511 + p->state = kLiteralNextStates[p->state];
4512 + curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
4513 + LitEnc_Encode(&p->rc, p->litProbs, curByte);
4514 + p->additionalOffset--;
4518 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
4521 + UInt32 pos, len, posState;
4524 + len = GetOptimumFast(p, &pos);
4526 + len = GetOptimum(p, nowPos32, &pos);
4529 + printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
4532 + posState = nowPos32 & p->pbMask;
4533 + if (len == 1 && pos == 0xFFFFFFFF)
4539 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
4540 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
4542 + probs = LIT_PROBS(nowPos32, *(data - 1));
4543 + if (IsCharState(p->state))
4544 + LitEnc_Encode(&p->rc, probs, curByte);
4546 + LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
4547 + p->state = kLiteralNextStates[p->state];
4551 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4552 + if (pos < LZMA_NUM_REPS)
4554 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
4557 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
4558 + RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
4562 + UInt32 distance = p->reps[pos];
4563 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
4565 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
4568 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
4569 + RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
4571 + p->reps[3] = p->reps[2];
4572 + p->reps[2] = p->reps[1];
4574 + p->reps[1] = p->reps[0];
4575 + p->reps[0] = distance;
4578 + p->state = kShortRepNextStates[p->state];
4581 + LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4582 + p->state = kRepNextStates[p->state];
4588 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4589 + p->state = kMatchNextStates[p->state];
4590 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4591 + pos -= LZMA_NUM_REPS;
4592 + GetPosSlot(pos, posSlot);
4593 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
4595 + if (posSlot >= kStartPosModelIndex)
4597 + UInt32 footerBits = ((posSlot >> 1) - 1);
4598 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4599 + UInt32 posReduced = pos - base;
4601 + if (posSlot < kEndPosModelIndex)
4602 + RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
4605 + RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
4606 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
4607 + p->alignPriceCount++;
4610 + p->reps[3] = p->reps[2];
4611 + p->reps[2] = p->reps[1];
4612 + p->reps[1] = p->reps[0];
4614 + p->matchPriceCount++;
4617 + p->additionalOffset -= len;
4619 + if (p->additionalOffset == 0)
4624 + if (p->matchPriceCount >= (1 << 7))
4625 + FillDistancesPrices(p);
4626 + if (p->alignPriceCount >= kAlignTableSize)
4627 + FillAlignPrices(p);
4629 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4631 + processed = nowPos32 - startPos32;
4634 + if (processed + kNumOpts + 300 >= maxUnpackSize ||
4635 + RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
4638 + else if (processed >= (1 << 15))
4640 + p->nowPos64 += nowPos32 - startPos32;
4641 + return CheckErrors(p);
4645 + p->nowPos64 += nowPos32 - startPos32;
4646 + return Flush(p, nowPos32);
4649 +#define kBigHashDicLimit ((UInt32)1 << 24)
4651 +static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4653 + UInt32 beforeSize = kNumOpts;
4655 + if (!RangeEnc_Alloc(&p->rc, alloc))
4656 + return SZ_ERROR_MEM;
4657 + btMode = (p->matchFinderBase.btMode != 0);
4658 + #ifdef COMPRESS_MF_MT
4659 + p->mtMode = (p->multiThread && !p->fastMode && btMode);
4663 + unsigned lclp = p->lc + p->lp;
4664 + if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
4666 + LzmaEnc_FreeLits(p, alloc);
4667 + p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
4668 + p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
4669 + if (p->litProbs == 0 || p->saveState.litProbs == 0)
4671 + LzmaEnc_FreeLits(p, alloc);
4672 + return SZ_ERROR_MEM;
4678 + p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
4680 + if (beforeSize + p->dictSize < keepWindowSize)
4681 + beforeSize = keepWindowSize - p->dictSize;
4683 + #ifdef COMPRESS_MF_MT
4686 + RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
4687 + p->matchFinderObj = &p->matchFinderMt;
4688 + MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
4693 + if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
4694 + return SZ_ERROR_MEM;
4695 + p->matchFinderObj = &p->matchFinderBase;
4696 + MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
4701 +void LzmaEnc_Init(CLzmaEnc *p)
4705 + for(i = 0 ; i < LZMA_NUM_REPS; i++)
4708 + RangeEnc_Init(&p->rc);
4711 + for (i = 0; i < kNumStates; i++)
4714 + for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
4716 + p->isMatch[i][j] = kProbInitValue;
4717 + p->isRep0Long[i][j] = kProbInitValue;
4719 + p->isRep[i] = kProbInitValue;
4720 + p->isRepG0[i] = kProbInitValue;
4721 + p->isRepG1[i] = kProbInitValue;
4722 + p->isRepG2[i] = kProbInitValue;
4726 + UInt32 num = 0x300 << (p->lp + p->lc);
4727 + for (i = 0; i < num; i++)
4728 + p->litProbs[i] = kProbInitValue;
4732 + for (i = 0; i < kNumLenToPosStates; i++)
4734 + CLzmaProb *probs = p->posSlotEncoder[i];
4736 + for (j = 0; j < (1 << kNumPosSlotBits); j++)
4737 + probs[j] = kProbInitValue;
4741 + for(i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
4742 + p->posEncoders[i] = kProbInitValue;
4745 + LenEnc_Init(&p->lenEnc.p);
4746 + LenEnc_Init(&p->repLenEnc.p);
4748 + for (i = 0; i < (1 << kNumAlignBits); i++)
4749 + p->posAlignEncoder[i] = kProbInitValue;
4751 + p->longestMatchWasFound = False;
4752 + p->optimumEndIndex = 0;
4753 + p->optimumCurrentIndex = 0;
4754 + p->additionalOffset = 0;
4756 + p->pbMask = (1 << p->pb) - 1;
4757 + p->lpMask = (1 << p->lp) - 1;
4760 +void LzmaEnc_InitPrices(CLzmaEnc *p)
4764 + FillDistancesPrices(p);
4765 + FillAlignPrices(p);
4768 + p->lenEnc.tableSize =
4769 + p->repLenEnc.tableSize =
4770 + p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
4771 + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
4772 + LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
4775 +static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4778 + for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
4779 + if (p->dictSize <= ((UInt32)1 << i))
4781 + p->distTableSize = i * 2;
4783 + p->finished = False;
4784 + p->result = SZ_OK;
4785 + RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
4787 + LzmaEnc_InitPrices(p);
4792 +static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqInStream *inStream, ISeqOutStream *outStream,
4793 + ISzAlloc *alloc, ISzAlloc *allocBig)
4795 + CLzmaEnc *p = (CLzmaEnc *)pp;
4796 + p->inStream = inStream;
4797 + p->rc.outStream = outStream;
4798 + return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
4801 +SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
4802 + ISeqInStream *inStream, UInt32 keepWindowSize,
4803 + ISzAlloc *alloc, ISzAlloc *allocBig)
4805 + CLzmaEnc *p = (CLzmaEnc *)pp;
4806 + p->inStream = inStream;
4807 + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
4810 +static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
4812 + p->seqBufInStream.funcTable.Read = MyRead;
4813 + p->seqBufInStream.data = src;
4814 + p->seqBufInStream.rem = srcLen;
4817 +SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
4818 + UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4820 + CLzmaEnc *p = (CLzmaEnc *)pp;
4821 + LzmaEnc_SetInputBuf(p, src, srcLen);
4822 + p->inStream = &p->seqBufInStream.funcTable;
4823 + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
4826 +void LzmaEnc_Finish(CLzmaEncHandle pp)
4828 + #ifdef COMPRESS_MF_MT
4829 + CLzmaEnc *p = (CLzmaEnc *)pp;
4831 + MatchFinderMt_ReleaseStream(&p->matchFinderMt);
4835 +typedef struct _CSeqOutStreamBuf
4837 + ISeqOutStream funcTable;
4841 +} CSeqOutStreamBuf;
4843 +static size_t MyWrite(void *pp, const void *data, size_t size)
4845 + CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
4846 + if (p->rem < size)
4849 + p->overflow = True;
4851 + memcpy(p->data, data, size);
4858 +UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
4860 + const CLzmaEnc *p = (CLzmaEnc *)pp;
4861 + return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
4864 +const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)
4866 + const CLzmaEnc *p = (CLzmaEnc *)pp;
4867 + return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
4870 +SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
4871 + Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
4873 + CLzmaEnc *p = (CLzmaEnc *)pp;
4876 + CSeqOutStreamBuf outStream;
4878 + outStream.funcTable.Write = MyWrite;
4879 + outStream.data = dest;
4880 + outStream.rem = *destLen;
4881 + outStream.overflow = False;
4883 + p->writeEndMark = False;
4884 + p->finished = False;
4885 + p->result = SZ_OK;
4889 + LzmaEnc_InitPrices(p);
4890 + nowPos64 = p->nowPos64;
4891 + RangeEnc_Init(&p->rc);
4892 + p->rc.outStream = &outStream.funcTable;
4894 + res = LzmaEnc_CodeOneBlock(pp, True, desiredPackSize, *unpackSize);
4896 + *unpackSize = (UInt32)(p->nowPos64 - nowPos64);
4897 + *destLen -= outStream.rem;
4898 + if (outStream.overflow)
4899 + return SZ_ERROR_OUTPUT_EOF;
4904 +SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
4905 + ISzAlloc *alloc, ISzAlloc *allocBig)
4907 + CLzmaEnc *p = (CLzmaEnc *)pp;
4910 + #ifdef COMPRESS_MF_MT
4911 + Byte allocaDummy[0x300];
4913 + for (i = 0; i < 16; i++)
4914 + allocaDummy[i] = (Byte)i;
4917 + RINOK(LzmaEnc_Prepare(pp, inStream, outStream, alloc, allocBig));
4921 + res = LzmaEnc_CodeOneBlock(pp, False, 0, 0);
4922 + if (res != SZ_OK || p->finished != 0)
4924 + if (progress != 0)
4926 + res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
4929 + res = SZ_ERROR_PROGRESS;
4934 + LzmaEnc_Finish(pp);
4938 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
4940 + CLzmaEnc *p = (CLzmaEnc *)pp;
4942 + UInt32 dictSize = p->dictSize;
4943 + if (*size < LZMA_PROPS_SIZE)
4944 + return SZ_ERROR_PARAM;
4945 + *size = LZMA_PROPS_SIZE;
4946 + props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
4948 + for (i = 11; i <= 30; i++)
4950 + if (dictSize <= ((UInt32)2 << i))
4952 + dictSize = (2 << i);
4955 + if (dictSize <= ((UInt32)3 << i))
4957 + dictSize = (3 << i);
4962 + for (i = 0; i < 4; i++)
4963 + props[1 + i] = (Byte)(dictSize >> (8 * i));
4967 +SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
4968 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
4971 + CLzmaEnc *p = (CLzmaEnc *)pp;
4973 + CSeqOutStreamBuf outStream;
4975 + LzmaEnc_SetInputBuf(p, src, srcLen);
4977 + outStream.funcTable.Write = MyWrite;
4978 + outStream.data = dest;
4979 + outStream.rem = *destLen;
4980 + outStream.overflow = False;
4982 + p->writeEndMark = writeEndMark;
4983 + res = LzmaEnc_Encode(pp, &outStream.funcTable, &p->seqBufInStream.funcTable,
4984 + progress, alloc, allocBig);
4986 + *destLen -= outStream.rem;
4987 + if (outStream.overflow)
4988 + return SZ_ERROR_OUTPUT_EOF;
4992 +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
4993 + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
4994 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
4996 + CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
4999 + return SZ_ERROR_MEM;
5001 + res = LzmaEnc_SetProps(p, props);
5004 + res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
5006 + res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
5007 + writeEndMark, progress, alloc, allocBig);
5010 + LzmaEnc_Destroy(p, alloc, allocBig);
5013 --- a/mkfs.jffs2.c 2009-06-05 16:59:08.000000000 +0200
5014 +++ b/mkfs.jffs2.c 2010-03-20 23:16:16.877026000 +0100
5015 @@ -1761,11 +1761,11 @@ int main(int argc, char **argv)
5017 erase_block_size *= units;
5019 - /* If it's less than 8KiB, they're not allowed */
5020 - if (erase_block_size < 0x2000) {
5021 - fprintf(stderr, "Erase size 0x%x too small. Increasing to 8KiB minimum\n",
5022 + /* If it's less than 4KiB, they're not allowed */
5023 + if (erase_block_size < 0x1000) {
5024 + fprintf(stderr, "Erase size 0x%x too small. Increasing to 4KiB minimum\n",
5026 - erase_block_size = 0x2000;
5027 + erase_block_size = 0x1000;