kernel: fix mtk_eth_soc throughput regressions on gigabit PHY ports
[openwrt/openwrt.git] / target / linux / generic / pending-5.15 / 530-jffs2_make_lzma_available.patch
1 From: Alexandros C. Couloumbis <alex@ozo.com>
2 Subject: fs: add jffs2/lzma support (not activated by default yet)
3
4 lede-commit: c2c88d315fa0e881f8b19da07b62859b915b11b2
5 Signed-off-by: Alexandros C. Couloumbis <alex@ozo.com>
6 ---
7 fs/jffs2/Kconfig | 9 +
8 fs/jffs2/Makefile | 3 +
9 fs/jffs2/compr.c | 6 +
10 fs/jffs2/compr.h | 10 +-
11 fs/jffs2/compr_lzma.c | 128 +++
12 fs/jffs2/super.c | 33 +-
13 include/linux/lzma.h | 62 ++
14 include/linux/lzma/LzFind.h | 115 +++
15 include/linux/lzma/LzHash.h | 54 +
16 include/linux/lzma/LzmaDec.h | 231 +++++
17 include/linux/lzma/LzmaEnc.h | 80 ++
18 include/linux/lzma/Types.h | 226 +++++
19 include/uapi/linux/jffs2.h | 1 +
20 lib/Kconfig | 6 +
21 lib/Makefile | 12 +
22 lib/lzma/LzFind.c | 761 ++++++++++++++
23 lib/lzma/LzmaDec.c | 999 +++++++++++++++++++
24 lib/lzma/LzmaEnc.c | 2271 ++++++++++++++++++++++++++++++++++++++++++
25 lib/lzma/Makefile | 7 +
26 19 files changed, 5008 insertions(+), 6 deletions(-)
27 create mode 100644 fs/jffs2/compr_lzma.c
28 create mode 100644 include/linux/lzma.h
29 create mode 100644 include/linux/lzma/LzFind.h
30 create mode 100644 include/linux/lzma/LzHash.h
31 create mode 100644 include/linux/lzma/LzmaDec.h
32 create mode 100644 include/linux/lzma/LzmaEnc.h
33 create mode 100644 include/linux/lzma/Types.h
34 create mode 100644 lib/lzma/LzFind.c
35 create mode 100644 lib/lzma/LzmaDec.c
36 create mode 100644 lib/lzma/LzmaEnc.c
37 create mode 100644 lib/lzma/Makefile
38
39 --- a/fs/jffs2/Kconfig
40 +++ b/fs/jffs2/Kconfig
41 @@ -136,6 +136,15 @@ config JFFS2_LZO
42 This feature was added in July, 2007. Say 'N' if you need
43 compatibility with older bootloaders or kernels.
44
45 +config JFFS2_LZMA
46 + bool "JFFS2 LZMA compression support" if JFFS2_COMPRESSION_OPTIONS
47 + select LZMA_COMPRESS
48 + select LZMA_DECOMPRESS
49 + depends on JFFS2_FS
50 + default n
51 + help
52 + JFFS2 wrapper to the LZMA C SDK
53 +
54 config JFFS2_RTIME
55 bool "JFFS2 RTIME compression support" if JFFS2_COMPRESSION_OPTIONS
56 depends on JFFS2_FS
57 --- a/fs/jffs2/Makefile
58 +++ b/fs/jffs2/Makefile
59 @@ -19,4 +19,7 @@ jffs2-$(CONFIG_JFFS2_RUBIN) += compr_rub
60 jffs2-$(CONFIG_JFFS2_RTIME) += compr_rtime.o
61 jffs2-$(CONFIG_JFFS2_ZLIB) += compr_zlib.o
62 jffs2-$(CONFIG_JFFS2_LZO) += compr_lzo.o
63 +jffs2-$(CONFIG_JFFS2_LZMA) += compr_lzma.o
64 jffs2-$(CONFIG_JFFS2_SUMMARY) += summary.o
65 +
66 +CFLAGS_compr_lzma.o += -Iinclude/linux -Ilib/lzma
67 --- a/fs/jffs2/compr.c
68 +++ b/fs/jffs2/compr.c
69 @@ -378,6 +378,9 @@ int __init jffs2_compressors_init(void)
70 #ifdef CONFIG_JFFS2_LZO
71 jffs2_lzo_init();
72 #endif
73 +#ifdef CONFIG_JFFS2_LZMA
74 + jffs2_lzma_init();
75 +#endif
76 /* Setting default compression mode */
77 #ifdef CONFIG_JFFS2_CMODE_NONE
78 jffs2_compression_mode = JFFS2_COMPR_MODE_NONE;
79 @@ -401,6 +404,9 @@ int __init jffs2_compressors_init(void)
80 int jffs2_compressors_exit(void)
81 {
82 /* Unregistering compressors */
83 +#ifdef CONFIG_JFFS2_LZMA
84 + jffs2_lzma_exit();
85 +#endif
86 #ifdef CONFIG_JFFS2_LZO
87 jffs2_lzo_exit();
88 #endif
89 --- a/fs/jffs2/compr.h
90 +++ b/fs/jffs2/compr.h
91 @@ -29,9 +29,9 @@
92 #define JFFS2_DYNRUBIN_PRIORITY 20
93 #define JFFS2_LZARI_PRIORITY 30
94 #define JFFS2_RTIME_PRIORITY 50
95 -#define JFFS2_ZLIB_PRIORITY 60
96 -#define JFFS2_LZO_PRIORITY 80
97 -
98 +#define JFFS2_LZMA_PRIORITY 70
99 +#define JFFS2_ZLIB_PRIORITY 80
100 +#define JFFS2_LZO_PRIORITY 90
101
102 #define JFFS2_RUBINMIPS_DISABLED /* RUBINs will be used only */
103 #define JFFS2_DYNRUBIN_DISABLED /* for decompression */
104 @@ -101,5 +101,9 @@ void jffs2_zlib_exit(void);
105 int jffs2_lzo_init(void);
106 void jffs2_lzo_exit(void);
107 #endif
108 +#ifdef CONFIG_JFFS2_LZMA
109 +int jffs2_lzma_init(void);
110 +void jffs2_lzma_exit(void);
111 +#endif
112
113 #endif /* __JFFS2_COMPR_H__ */
114 --- /dev/null
115 +++ b/fs/jffs2/compr_lzma.c
116 @@ -0,0 +1,128 @@
117 +/*
118 + * JFFS2 -- Journalling Flash File System, Version 2.
119 + *
120 + * For licensing information, see the file 'LICENCE' in this directory.
121 + *
122 + * JFFS2 wrapper to the LZMA C SDK
123 + *
124 + */
125 +
126 +#include <linux/lzma.h>
127 +#include "compr.h"
128 +
129 +#ifdef __KERNEL__
130 + static DEFINE_MUTEX(deflate_mutex);
131 +#endif
132 +
133 +CLzmaEncHandle *p;
134 +Byte propsEncoded[LZMA_PROPS_SIZE];
135 +SizeT propsSize = sizeof(propsEncoded);
136 +
137 +STATIC void lzma_free_workspace(void)
138 +{
139 + LzmaEnc_Destroy(p, &lzma_alloc, &lzma_alloc);
140 +}
141 +
142 +STATIC int INIT lzma_alloc_workspace(CLzmaEncProps *props)
143 +{
144 + if ((p = (CLzmaEncHandle *)LzmaEnc_Create(&lzma_alloc)) == NULL)
145 + {
146 + PRINT_ERROR("Failed to allocate lzma deflate workspace\n");
147 + return -ENOMEM;
148 + }
149 +
150 + if (LzmaEnc_SetProps(p, props) != SZ_OK)
151 + {
152 + lzma_free_workspace();
153 + return -1;
154 + }
155 +
156 + if (LzmaEnc_WriteProperties(p, propsEncoded, &propsSize) != SZ_OK)
157 + {
158 + lzma_free_workspace();
159 + return -1;
160 + }
161 +
162 + return 0;
163 +}
164 +
165 +STATIC int jffs2_lzma_compress(unsigned char *data_in, unsigned char *cpage_out,
166 + uint32_t *sourcelen, uint32_t *dstlen)
167 +{
168 + SizeT compress_size = (SizeT)(*dstlen);
169 + int ret;
170 +
171 + #ifdef __KERNEL__
172 + mutex_lock(&deflate_mutex);
173 + #endif
174 +
175 + ret = LzmaEnc_MemEncode(p, cpage_out, &compress_size, data_in, *sourcelen,
176 + 0, NULL, &lzma_alloc, &lzma_alloc);
177 +
178 + #ifdef __KERNEL__
179 + mutex_unlock(&deflate_mutex);
180 + #endif
181 +
182 + if (ret != SZ_OK)
183 + return -1;
184 +
185 + *dstlen = (uint32_t)compress_size;
186 +
187 + return 0;
188 +}
189 +
190 +STATIC int jffs2_lzma_decompress(unsigned char *data_in, unsigned char *cpage_out,
191 + uint32_t srclen, uint32_t destlen)
192 +{
193 + int ret;
194 + SizeT dl = (SizeT)destlen;
195 + SizeT sl = (SizeT)srclen;
196 + ELzmaStatus status;
197 +
198 + ret = LzmaDecode(cpage_out, &dl, data_in, &sl, propsEncoded,
199 + propsSize, LZMA_FINISH_ANY, &status, &lzma_alloc);
200 +
201 + if (ret != SZ_OK || status == LZMA_STATUS_NOT_FINISHED || dl != (SizeT)destlen)
202 + return -1;
203 +
204 + return 0;
205 +}
206 +
207 +static struct jffs2_compressor jffs2_lzma_comp = {
208 + .priority = JFFS2_LZMA_PRIORITY,
209 + .name = "lzma",
210 + .compr = JFFS2_COMPR_LZMA,
211 + .compress = &jffs2_lzma_compress,
212 + .decompress = &jffs2_lzma_decompress,
213 + .disabled = 0,
214 +};
215 +
216 +int INIT jffs2_lzma_init(void)
217 +{
218 + int ret;
219 + CLzmaEncProps props;
220 + LzmaEncProps_Init(&props);
221 +
222 + props.dictSize = LZMA_BEST_DICT(0x2000);
223 + props.level = LZMA_BEST_LEVEL;
224 + props.lc = LZMA_BEST_LC;
225 + props.lp = LZMA_BEST_LP;
226 + props.pb = LZMA_BEST_PB;
227 + props.fb = LZMA_BEST_FB;
228 +
229 + ret = lzma_alloc_workspace(&props);
230 + if (ret < 0)
231 + return ret;
232 +
233 + ret = jffs2_register_compressor(&jffs2_lzma_comp);
234 + if (ret)
235 + lzma_free_workspace();
236 +
237 + return ret;
238 +}
239 +
240 +void jffs2_lzma_exit(void)
241 +{
242 + jffs2_unregister_compressor(&jffs2_lzma_comp);
243 + lzma_free_workspace();
244 +}
245 --- a/fs/jffs2/super.c
246 +++ b/fs/jffs2/super.c
247 @@ -374,14 +374,41 @@ static int __init init_jffs2_fs(void)
248 BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
249 BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
250
251 - pr_info("version 2.2."
252 + pr_info("version 2.2"
253 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
254 " (NAND)"
255 #endif
256 #ifdef CONFIG_JFFS2_SUMMARY
257 - " (SUMMARY) "
258 + " (SUMMARY)"
259 #endif
260 - " © 2001-2006 Red Hat, Inc.\n");
261 +#ifdef CONFIG_JFFS2_ZLIB
262 + " (ZLIB)"
263 +#endif
264 +#ifdef CONFIG_JFFS2_LZO
265 + " (LZO)"
266 +#endif
267 +#ifdef CONFIG_JFFS2_LZMA
268 + " (LZMA)"
269 +#endif
270 +#ifdef CONFIG_JFFS2_RTIME
271 + " (RTIME)"
272 +#endif
273 +#ifdef CONFIG_JFFS2_RUBIN
274 + " (RUBIN)"
275 +#endif
276 +#ifdef CONFIG_JFFS2_CMODE_NONE
277 + " (CMODE_NONE)"
278 +#endif
279 +#ifdef CONFIG_JFFS2_CMODE_PRIORITY
280 + " (CMODE_PRIORITY)"
281 +#endif
282 +#ifdef CONFIG_JFFS2_CMODE_SIZE
283 + " (CMODE_SIZE)"
284 +#endif
285 +#ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
286 + " (CMODE_FAVOURLZO)"
287 +#endif
288 + " (c) 2001-2006 Red Hat, Inc.\n");
289
290 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
291 sizeof(struct jffs2_inode_info),
292 --- /dev/null
293 +++ b/include/linux/lzma.h
294 @@ -0,0 +1,62 @@
295 +#ifndef __LZMA_H__
296 +#define __LZMA_H__
297 +
298 +#ifdef __KERNEL__
299 + #include <linux/kernel.h>
300 + #include <linux/sched.h>
301 + #include <linux/slab.h>
302 + #include <linux/vmalloc.h>
303 + #include <linux/init.h>
304 + #define LZMA_MALLOC vmalloc
305 + #define LZMA_FREE vfree
306 + #define PRINT_ERROR(msg) printk(KERN_WARNING #msg)
307 + #define INIT __init
308 + #define STATIC static
309 +#else
310 + #include <stdint.h>
311 + #include <stdlib.h>
312 + #include <stdio.h>
313 + #include <unistd.h>
314 + #include <string.h>
315 + #include <asm/types.h>
316 + #include <errno.h>
317 + #include <linux/jffs2.h>
318 + #ifndef PAGE_SIZE
319 + extern int page_size;
320 + #define PAGE_SIZE page_size
321 + #endif
322 + #define LZMA_MALLOC malloc
323 + #define LZMA_FREE free
324 + #define PRINT_ERROR(msg) fprintf(stderr, msg)
325 + #define INIT
326 + #define STATIC
327 +#endif
328 +
329 +#include "lzma/LzmaDec.h"
330 +#include "lzma/LzmaEnc.h"
331 +
332 +#define LZMA_BEST_LEVEL (9)
333 +#define LZMA_BEST_LC (0)
334 +#define LZMA_BEST_LP (0)
335 +#define LZMA_BEST_PB (0)
336 +#define LZMA_BEST_FB (273)
337 +
338 +#define LZMA_BEST_DICT(n) (((int)((n) / 2)) * 2)
339 +
340 +static void *p_lzma_malloc(void *p, size_t size)
341 +{
342 + if (size == 0)
343 + return NULL;
344 +
345 + return LZMA_MALLOC(size);
346 +}
347 +
348 +static void p_lzma_free(void *p, void *address)
349 +{
350 + if (address != NULL)
351 + LZMA_FREE(address);
352 +}
353 +
354 +static ISzAlloc lzma_alloc = {p_lzma_malloc, p_lzma_free};
355 +
356 +#endif
357 --- /dev/null
358 +++ b/include/linux/lzma/LzFind.h
359 @@ -0,0 +1,98 @@
360 +/* LzFind.h -- Match finder for LZ algorithms
361 +2009-04-22 : Igor Pavlov : Public domain */
362 +
363 +#ifndef __LZ_FIND_H
364 +#define __LZ_FIND_H
365 +
366 +#include "Types.h"
367 +
368 +#ifdef __cplusplus
369 +extern "C" {
370 +#endif
371 +
372 +typedef UInt32 CLzRef;
373 +
374 +typedef struct _CMatchFinder
375 +{
376 + Byte *buffer;
377 + UInt32 pos;
378 + UInt32 posLimit;
379 + UInt32 streamPos;
380 + UInt32 lenLimit;
381 +
382 + UInt32 cyclicBufferPos;
383 + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */
384 +
385 + UInt32 matchMaxLen;
386 + CLzRef *hash;
387 + CLzRef *son;
388 + UInt32 hashMask;
389 + UInt32 cutValue;
390 +
391 + Byte *bufferBase;
392 + ISeqInStream *stream;
393 + int streamEndWasReached;
394 +
395 + UInt32 blockSize;
396 + UInt32 keepSizeBefore;
397 + UInt32 keepSizeAfter;
398 +
399 + UInt32 numHashBytes;
400 + int directInput;
401 + size_t directInputRem;
402 + int btMode;
403 + int bigHash;
404 + UInt32 historySize;
405 + UInt32 fixedHashSize;
406 + UInt32 hashSizeSum;
407 + UInt32 numSons;
408 + SRes result;
409 + UInt32 crc[256];
410 +} CMatchFinder;
411 +
412 +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer)
413 +#define Inline_MatchFinder_GetIndexByte(p, index) ((p)->buffer[(Int32)(index)])
414 +
415 +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos)
416 +
417 +void MatchFinder_Construct(CMatchFinder *p);
418 +
419 +/* Conditions:
420 + historySize <= 3 GB
421 + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB
422 +*/
423 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
424 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
425 + ISzAlloc *alloc);
426 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc);
427 +
428 +/*
429 +Conditions:
430 + Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func.
431 + Mf_GetPointerToCurrentPos_Func's result must be used only before any other function
432 +*/
433 +
434 +typedef void (*Mf_Init_Func)(void *object);
435 +typedef Byte (*Mf_GetIndexByte_Func)(void *object, Int32 index);
436 +typedef UInt32 (*Mf_GetNumAvailableBytes_Func)(void *object);
437 +typedef const Byte * (*Mf_GetPointerToCurrentPos_Func)(void *object);
438 +typedef UInt32 (*Mf_GetMatches_Func)(void *object, UInt32 *distances);
439 +typedef void (*Mf_Skip_Func)(void *object, UInt32);
440 +
441 +typedef struct _IMatchFinder
442 +{
443 + Mf_Init_Func Init;
444 + Mf_GetIndexByte_Func GetIndexByte;
445 + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes;
446 + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos;
447 + Mf_GetMatches_Func GetMatches;
448 + Mf_Skip_Func Skip;
449 +} IMatchFinder;
450 +
451 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable);
452 +
453 +#ifdef __cplusplus
454 +}
455 +#endif
456 +
457 +#endif
458 --- /dev/null
459 +++ b/include/linux/lzma/LzHash.h
460 @@ -0,0 +1,54 @@
461 +/* LzHash.h -- HASH functions for LZ algorithms
462 +2009-02-07 : Igor Pavlov : Public domain */
463 +
464 +#ifndef __LZ_HASH_H
465 +#define __LZ_HASH_H
466 +
467 +#define kHash2Size (1 << 10)
468 +#define kHash3Size (1 << 16)
469 +#define kHash4Size (1 << 20)
470 +
471 +#define kFix3HashSize (kHash2Size)
472 +#define kFix4HashSize (kHash2Size + kHash3Size)
473 +#define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size)
474 +
475 +#define HASH2_CALC hashValue = cur[0] | ((UInt32)cur[1] << 8);
476 +
477 +#define HASH3_CALC { \
478 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
479 + hash2Value = temp & (kHash2Size - 1); \
480 + hashValue = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; }
481 +
482 +#define HASH4_CALC { \
483 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
484 + hash2Value = temp & (kHash2Size - 1); \
485 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
486 + hashValue = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & p->hashMask; }
487 +
488 +#define HASH5_CALC { \
489 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
490 + hash2Value = temp & (kHash2Size - 1); \
491 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
492 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)); \
493 + hashValue = (hash4Value ^ (p->crc[cur[4]] << 3)) & p->hashMask; \
494 + hash4Value &= (kHash4Size - 1); }
495 +
496 +/* #define HASH_ZIP_CALC hashValue = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */
497 +#define HASH_ZIP_CALC hashValue = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF;
498 +
499 +
500 +#define MT_HASH2_CALC \
501 + hash2Value = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1);
502 +
503 +#define MT_HASH3_CALC { \
504 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
505 + hash2Value = temp & (kHash2Size - 1); \
506 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); }
507 +
508 +#define MT_HASH4_CALC { \
509 + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \
510 + hash2Value = temp & (kHash2Size - 1); \
511 + hash3Value = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); \
512 + hash4Value = (temp ^ ((UInt32)cur[2] << 8) ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); }
513 +
514 +#endif
515 --- /dev/null
516 +++ b/include/linux/lzma/LzmaDec.h
517 @@ -0,0 +1,130 @@
518 +/* LzmaDec.h -- LZMA Decoder
519 +2009-02-07 : Igor Pavlov : Public domain */
520 +
521 +#ifndef __LZMA_DEC_H
522 +#define __LZMA_DEC_H
523 +
524 +#include "Types.h"
525 +
526 +#ifdef __cplusplus
527 +extern "C" {
528 +#endif
529 +
530 +/* #define _LZMA_PROB32 */
531 +/* _LZMA_PROB32 can increase the speed on some CPUs,
532 + but memory usage for CLzmaDec::probs will be doubled in that case */
533 +
534 +#ifdef _LZMA_PROB32
535 +#define CLzmaProb UInt32
536 +#else
537 +#define CLzmaProb UInt16
538 +#endif
539 +
540 +
541 +/* ---------- LZMA Properties ---------- */
542 +
543 +#define LZMA_PROPS_SIZE 5
544 +
545 +typedef struct _CLzmaProps
546 +{
547 + unsigned lc, lp, pb;
548 + UInt32 dicSize;
549 +} CLzmaProps;
550 +
551 +
552 +/* ---------- LZMA Decoder state ---------- */
553 +
554 +/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
555 + Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
556 +
557 +#define LZMA_REQUIRED_INPUT_MAX 20
558 +
559 +typedef struct
560 +{
561 + CLzmaProps prop;
562 + CLzmaProb *probs;
563 + Byte *dic;
564 + const Byte *buf;
565 + UInt32 range, code;
566 + SizeT dicPos;
567 + SizeT dicBufSize;
568 + UInt32 processedPos;
569 + UInt32 checkDicSize;
570 + unsigned state;
571 + UInt32 reps[4];
572 + unsigned remainLen;
573 + int needFlush;
574 + int needInitState;
575 + UInt32 numProbs;
576 + unsigned tempBufSize;
577 + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
578 +} CLzmaDec;
579 +
580 +#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
581 +
582 +/* There are two types of LZMA streams:
583 + 0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
584 + 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
585 +
586 +typedef enum
587 +{
588 + LZMA_FINISH_ANY, /* finish at any point */
589 + LZMA_FINISH_END /* block must be finished at the end */
590 +} ELzmaFinishMode;
591 +
592 +/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
593 +
594 + You must use LZMA_FINISH_END, when you know that current output buffer
595 + covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
596 +
597 + If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
598 + and output value of destLen will be less than output buffer size limit.
599 + You can check status result also.
600 +
601 + You can use multiple checks to test data integrity after full decompression:
602 + 1) Check Result and "status" variable.
603 + 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
604 + 3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
605 + You must use correct finish mode in that case. */
606 +
607 +typedef enum
608 +{
609 + LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
610 + LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
611 + LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
612 + LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
613 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
614 +} ELzmaStatus;
615 +
616 +/* ELzmaStatus is used only as output value for function call */
617 +
618 +/* ---------- One Call Interface ---------- */
619 +
620 +/* LzmaDecode
621 +
622 +finishMode:
623 + It has meaning only if the decoding reaches output limit (*destLen).
624 + LZMA_FINISH_ANY - Decode just destLen bytes.
625 + LZMA_FINISH_END - Stream must be finished after (*destLen).
626 +
627 +Returns:
628 + SZ_OK
629 + status:
630 + LZMA_STATUS_FINISHED_WITH_MARK
631 + LZMA_STATUS_NOT_FINISHED
632 + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
633 + SZ_ERROR_DATA - Data error
634 + SZ_ERROR_MEM - Memory allocation error
635 + SZ_ERROR_UNSUPPORTED - Unsupported properties
636 + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
637 +*/
638 +
639 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
640 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
641 + ELzmaStatus *status, ISzAlloc *alloc);
642 +
643 +#ifdef __cplusplus
644 +}
645 +#endif
646 +
647 +#endif
648 --- /dev/null
649 +++ b/include/linux/lzma/LzmaEnc.h
650 @@ -0,0 +1,60 @@
651 +/* LzmaEnc.h -- LZMA Encoder
652 +2009-02-07 : Igor Pavlov : Public domain */
653 +
654 +#ifndef __LZMA_ENC_H
655 +#define __LZMA_ENC_H
656 +
657 +#include "Types.h"
658 +
659 +#ifdef __cplusplus
660 +extern "C" {
661 +#endif
662 +
663 +#define LZMA_PROPS_SIZE 5
664 +
665 +typedef struct _CLzmaEncProps
666 +{
667 + int level; /* 0 <= level <= 9 */
668 + UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version
669 + (1 << 12) <= dictSize <= (1 << 30) for 64-bit version
670 + default = (1 << 24) */
671 + int lc; /* 0 <= lc <= 8, default = 3 */
672 + int lp; /* 0 <= lp <= 4, default = 0 */
673 + int pb; /* 0 <= pb <= 4, default = 2 */
674 + int algo; /* 0 - fast, 1 - normal, default = 1 */
675 + int fb; /* 5 <= fb <= 273, default = 32 */
676 + int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */
677 + int numHashBytes; /* 2, 3 or 4, default = 4 */
678 + UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */
679 + unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */
680 + int numThreads; /* 1 or 2, default = 2 */
681 +} CLzmaEncProps;
682 +
683 +void LzmaEncProps_Init(CLzmaEncProps *p);
684 +
685 +/* ---------- CLzmaEncHandle Interface ---------- */
686 +
687 +/* LzmaEnc_* functions can return the following exit codes:
688 +Returns:
689 + SZ_OK - OK
690 + SZ_ERROR_MEM - Memory allocation error
691 + SZ_ERROR_PARAM - Incorrect paramater in props
692 + SZ_ERROR_WRITE - Write callback error.
693 + SZ_ERROR_PROGRESS - some break from progress callback
694 + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version)
695 +*/
696 +
697 +typedef void * CLzmaEncHandle;
698 +
699 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc);
700 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig);
701 +SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props);
702 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *properties, SizeT *size);
703 +SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
704 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig);
705 +
706 +#ifdef __cplusplus
707 +}
708 +#endif
709 +
710 +#endif
711 --- /dev/null
712 +++ b/include/linux/lzma/Types.h
713 @@ -0,0 +1,226 @@
714 +/* Types.h -- Basic types
715 +2009-11-23 : Igor Pavlov : Public domain */
716 +
717 +#ifndef __7Z_TYPES_H
718 +#define __7Z_TYPES_H
719 +
720 +#include <stddef.h>
721 +
722 +#ifdef _WIN32
723 +#include <windows.h>
724 +#endif
725 +
726 +#ifndef EXTERN_C_BEGIN
727 +#ifdef __cplusplus
728 +#define EXTERN_C_BEGIN extern "C" {
729 +#define EXTERN_C_END }
730 +#else
731 +#define EXTERN_C_BEGIN
732 +#define EXTERN_C_END
733 +#endif
734 +#endif
735 +
736 +EXTERN_C_BEGIN
737 +
738 +#define SZ_OK 0
739 +
740 +#define SZ_ERROR_DATA 1
741 +#define SZ_ERROR_MEM 2
742 +#define SZ_ERROR_CRC 3
743 +#define SZ_ERROR_UNSUPPORTED 4
744 +#define SZ_ERROR_PARAM 5
745 +#define SZ_ERROR_INPUT_EOF 6
746 +#define SZ_ERROR_OUTPUT_EOF 7
747 +#define SZ_ERROR_READ 8
748 +#define SZ_ERROR_WRITE 9
749 +#define SZ_ERROR_PROGRESS 10
750 +#define SZ_ERROR_FAIL 11
751 +#define SZ_ERROR_THREAD 12
752 +
753 +#define SZ_ERROR_ARCHIVE 16
754 +#define SZ_ERROR_NO_ARCHIVE 17
755 +
756 +typedef int SRes;
757 +
758 +#ifdef _WIN32
759 +typedef DWORD WRes;
760 +#else
761 +typedef int WRes;
762 +#endif
763 +
764 +#ifndef RINOK
765 +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; }
766 +#endif
767 +
768 +typedef unsigned char Byte;
769 +typedef short Int16;
770 +typedef unsigned short UInt16;
771 +
772 +#ifdef _LZMA_UINT32_IS_ULONG
773 +typedef long Int32;
774 +typedef unsigned long UInt32;
775 +#else
776 +typedef int Int32;
777 +typedef unsigned int UInt32;
778 +#endif
779 +
780 +#ifdef _SZ_NO_INT_64
781 +
782 +/* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers.
783 + NOTES: Some code will work incorrectly in that case! */
784 +
785 +typedef long Int64;
786 +typedef unsigned long UInt64;
787 +
788 +#else
789 +
790 +#if defined(_MSC_VER) || defined(__BORLANDC__)
791 +typedef __int64 Int64;
792 +typedef unsigned __int64 UInt64;
793 +#else
794 +typedef long long int Int64;
795 +typedef unsigned long long int UInt64;
796 +#endif
797 +
798 +#endif
799 +
800 +#ifdef _LZMA_NO_SYSTEM_SIZE_T
801 +typedef UInt32 SizeT;
802 +#else
803 +typedef size_t SizeT;
804 +#endif
805 +
806 +typedef int Bool;
807 +#define True 1
808 +#define False 0
809 +
810 +
811 +#ifdef _WIN32
812 +#define MY_STD_CALL __stdcall
813 +#else
814 +#define MY_STD_CALL
815 +#endif
816 +
817 +#ifdef _MSC_VER
818 +
819 +#if _MSC_VER >= 1300
820 +#define MY_NO_INLINE __declspec(noinline)
821 +#else
822 +#define MY_NO_INLINE
823 +#endif
824 +
825 +#define MY_CDECL __cdecl
826 +#define MY_FAST_CALL __fastcall
827 +
828 +#else
829 +
830 +#define MY_CDECL
831 +#define MY_FAST_CALL
832 +
833 +#endif
834 +
835 +
836 +/* The following interfaces use first parameter as pointer to structure */
837 +
838 +typedef struct
839 +{
840 + SRes (*Read)(void *p, void *buf, size_t *size);
841 + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
842 + (output(*size) < input(*size)) is allowed */
843 +} ISeqInStream;
844 +
845 +/* it can return SZ_ERROR_INPUT_EOF */
846 +SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size);
847 +SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType);
848 +SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf);
849 +
850 +typedef struct
851 +{
852 + size_t (*Write)(void *p, const void *buf, size_t size);
853 + /* Returns: result - the number of actually written bytes.
854 + (result < size) means error */
855 +} ISeqOutStream;
856 +
857 +typedef enum
858 +{
859 + SZ_SEEK_SET = 0,
860 + SZ_SEEK_CUR = 1,
861 + SZ_SEEK_END = 2
862 +} ESzSeek;
863 +
864 +typedef struct
865 +{
866 + SRes (*Read)(void *p, void *buf, size_t *size); /* same as ISeqInStream::Read */
867 + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
868 +} ISeekInStream;
869 +
870 +typedef struct
871 +{
872 + SRes (*Look)(void *p, void **buf, size_t *size);
873 + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream.
874 + (output(*size) > input(*size)) is not allowed
875 + (output(*size) < input(*size)) is allowed */
876 + SRes (*Skip)(void *p, size_t offset);
877 + /* offset must be <= output(*size) of Look */
878 +
879 + SRes (*Read)(void *p, void *buf, size_t *size);
880 + /* reads directly (without buffer). It's same as ISeqInStream::Read */
881 + SRes (*Seek)(void *p, Int64 *pos, ESzSeek origin);
882 +} ILookInStream;
883 +
884 +SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size);
885 +SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset);
886 +
887 +/* reads via ILookInStream::Read */
888 +SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType);
889 +SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size);
890 +
891 +#define LookToRead_BUF_SIZE (1 << 14)
892 +
893 +typedef struct
894 +{
895 + ILookInStream s;
896 + ISeekInStream *realStream;
897 + size_t pos;
898 + size_t size;
899 + Byte buf[LookToRead_BUF_SIZE];
900 +} CLookToRead;
901 +
902 +void LookToRead_CreateVTable(CLookToRead *p, int lookahead);
903 +void LookToRead_Init(CLookToRead *p);
904 +
905 +typedef struct
906 +{
907 + ISeqInStream s;
908 + ILookInStream *realStream;
909 +} CSecToLook;
910 +
911 +void SecToLook_CreateVTable(CSecToLook *p);
912 +
913 +typedef struct
914 +{
915 + ISeqInStream s;
916 + ILookInStream *realStream;
917 +} CSecToRead;
918 +
919 +void SecToRead_CreateVTable(CSecToRead *p);
920 +
921 +typedef struct
922 +{
923 + SRes (*Progress)(void *p, UInt64 inSize, UInt64 outSize);
924 + /* Returns: result. (result != SZ_OK) means break.
925 + Value (UInt64)(Int64)-1 for size means unknown value. */
926 +} ICompressProgress;
927 +
928 +typedef struct
929 +{
930 + void *(*Alloc)(void *p, size_t size);
931 + void (*Free)(void *p, void *address); /* address can be 0 */
932 +} ISzAlloc;
933 +
934 +#define IAlloc_Alloc(p, size) (p)->Alloc((p), size)
935 +#define IAlloc_Free(p, a) (p)->Free((p), a)
936 +
937 +EXTERN_C_END
938 +
939 +#endif
940 --- a/include/uapi/linux/jffs2.h
941 +++ b/include/uapi/linux/jffs2.h
942 @@ -46,6 +46,7 @@
943 #define JFFS2_COMPR_DYNRUBIN 0x05
944 #define JFFS2_COMPR_ZLIB 0x06
945 #define JFFS2_COMPR_LZO 0x07
946 +#define JFFS2_COMPR_LZMA 0x08
947 /* Compatibility flags. */
948 #define JFFS2_COMPAT_MASK 0xc000 /* What do to if an unknown nodetype is found */
949 #define JFFS2_NODE_ACCURATE 0x2000
950 --- a/lib/Kconfig
951 +++ b/lib/Kconfig
952 @@ -340,6 +340,12 @@ config ZSTD_DECOMPRESS
953
954 source "lib/xz/Kconfig"
955
956 +config LZMA_COMPRESS
957 + tristate
958 +
959 +config LZMA_DECOMPRESS
960 + tristate
961 +
962 #
963 # These all provide a common interface (hence the apparent duplication with
964 # ZLIB_INFLATE; DECOMPRESS_GZIP is just a wrapper.)
965 --- a/lib/Makefile
966 +++ b/lib/Makefile
967 @@ -135,6 +135,16 @@ CFLAGS_kobject.o += -DDEBUG
968 CFLAGS_kobject_uevent.o += -DDEBUG
969 endif
970
971 +ifdef CONFIG_JFFS2_ZLIB
972 + CONFIG_ZLIB_INFLATE:=y
973 + CONFIG_ZLIB_DEFLATE:=y
974 +endif
975 +
976 +ifdef CONFIG_JFFS2_LZMA
977 + CONFIG_LZMA_DECOMPRESS:=y
978 + CONFIG_LZMA_COMPRESS:=y
979 +endif
980 +
981 obj-$(CONFIG_DEBUG_INFO_REDUCED) += debug_info.o
982 CFLAGS_debug_info.o += $(call cc-option, -femit-struct-debug-detailed=any)
983
984 @@ -192,6 +202,8 @@ obj-$(CONFIG_ZSTD_COMPRESS) += zstd/
985 obj-$(CONFIG_ZSTD_DECOMPRESS) += zstd/
986 obj-$(CONFIG_XZ_DEC) += xz/
987 obj-$(CONFIG_RAID6_PQ) += raid6/
988 +obj-$(CONFIG_LZMA_COMPRESS) += lzma/
989 +obj-$(CONFIG_LZMA_DECOMPRESS) += lzma/
990
991 lib-$(CONFIG_DECOMPRESS_GZIP) += decompress_inflate.o
992 lib-$(CONFIG_DECOMPRESS_BZIP2) += decompress_bunzip2.o
993 --- /dev/null
994 +++ b/lib/lzma/LzFind.c
995 @@ -0,0 +1,522 @@
996 +/* LzFind.c -- Match finder for LZ algorithms
997 +2009-04-22 : Igor Pavlov : Public domain */
998 +
999 +#include <string.h>
1000 +
1001 +#include "LzFind.h"
1002 +#include "LzHash.h"
1003 +
1004 +#define kEmptyHashValue 0
1005 +#define kMaxValForNormalize ((UInt32)0xFFFFFFFF)
1006 +#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */
1007 +#define kNormalizeMask (~(kNormalizeStepMin - 1))
1008 +#define kMaxHistorySize ((UInt32)3 << 30)
1009 +
1010 +#define kStartMaxLen 3
1011 +
1012 +#if 0
1013 +#define DIRECT_INPUT p->directInput
1014 +#else
1015 +#define DIRECT_INPUT 1
1016 +#endif
1017 +
1018 +static void LzInWindow_Free(CMatchFinder *p, ISzAlloc *alloc)
1019 +{
1020 + if (!DIRECT_INPUT)
1021 + {
1022 + alloc->Free(alloc, p->bufferBase);
1023 + p->bufferBase = 0;
1024 + }
1025 +}
1026 +
1027 +/* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */
1028 +
1029 +static int LzInWindow_Create(CMatchFinder *p, UInt32 keepSizeReserv, ISzAlloc *alloc)
1030 +{
1031 + UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv;
1032 + if (DIRECT_INPUT)
1033 + {
1034 + p->blockSize = blockSize;
1035 + return 1;
1036 + }
1037 + if (p->bufferBase == 0 || p->blockSize != blockSize)
1038 + {
1039 + LzInWindow_Free(p, alloc);
1040 + p->blockSize = blockSize;
1041 + p->bufferBase = (Byte *)alloc->Alloc(alloc, (size_t)blockSize);
1042 + }
1043 + return (p->bufferBase != 0);
1044 +}
1045 +
1046 +static Byte *MatchFinder_GetPointerToCurrentPos(CMatchFinder *p) { return p->buffer; }
1047 +static Byte MatchFinder_GetIndexByte(CMatchFinder *p, Int32 index) { return p->buffer[index]; }
1048 +
1049 +static UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder *p) { return p->streamPos - p->pos; }
1050 +
1051 +static void MatchFinder_ReduceOffsets(CMatchFinder *p, UInt32 subValue)
1052 +{
1053 + p->posLimit -= subValue;
1054 + p->pos -= subValue;
1055 + p->streamPos -= subValue;
1056 +}
1057 +
1058 +static void MatchFinder_ReadBlock(CMatchFinder *p)
1059 +{
1060 + if (p->streamEndWasReached || p->result != SZ_OK)
1061 + return;
1062 + if (DIRECT_INPUT)
1063 + {
1064 + UInt32 curSize = 0xFFFFFFFF - p->streamPos;
1065 + if (curSize > p->directInputRem)
1066 + curSize = (UInt32)p->directInputRem;
1067 + p->directInputRem -= curSize;
1068 + p->streamPos += curSize;
1069 + if (p->directInputRem == 0)
1070 + p->streamEndWasReached = 1;
1071 + return;
1072 + }
1073 + for (;;)
1074 + {
1075 + Byte *dest = p->buffer + (p->streamPos - p->pos);
1076 + size_t size = (p->bufferBase + p->blockSize - dest);
1077 + if (size == 0)
1078 + return;
1079 + p->result = p->stream->Read(p->stream, dest, &size);
1080 + if (p->result != SZ_OK)
1081 + return;
1082 + if (size == 0)
1083 + {
1084 + p->streamEndWasReached = 1;
1085 + return;
1086 + }
1087 + p->streamPos += (UInt32)size;
1088 + if (p->streamPos - p->pos > p->keepSizeAfter)
1089 + return;
1090 + }
1091 +}
1092 +
1093 +static void MatchFinder_MoveBlock(CMatchFinder *p)
1094 +{
1095 + memmove(p->bufferBase,
1096 + p->buffer - p->keepSizeBefore,
1097 + (size_t)(p->streamPos - p->pos + p->keepSizeBefore));
1098 + p->buffer = p->bufferBase + p->keepSizeBefore;
1099 +}
1100 +
1101 +static int MatchFinder_NeedMove(CMatchFinder *p)
1102 +{
1103 + if (DIRECT_INPUT)
1104 + return 0;
1105 + /* if (p->streamEndWasReached) return 0; */
1106 + return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter);
1107 +}
1108 +
1109 +static void MatchFinder_CheckAndMoveAndRead(CMatchFinder *p)
1110 +{
1111 + if (MatchFinder_NeedMove(p))
1112 + MatchFinder_MoveBlock(p);
1113 + MatchFinder_ReadBlock(p);
1114 +}
1115 +
1116 +static void MatchFinder_SetDefaultSettings(CMatchFinder *p)
1117 +{
1118 + p->cutValue = 32;
1119 + p->btMode = 1;
1120 + p->numHashBytes = 4;
1121 + p->bigHash = 0;
1122 +}
1123 +
1124 +#define kCrcPoly 0xEDB88320
1125 +
1126 +void MatchFinder_Construct(CMatchFinder *p)
1127 +{
1128 + UInt32 i;
1129 + p->bufferBase = 0;
1130 + p->directInput = 0;
1131 + p->hash = 0;
1132 + MatchFinder_SetDefaultSettings(p);
1133 +
1134 + for (i = 0; i < 256; i++)
1135 + {
1136 + UInt32 r = i;
1137 + int j;
1138 + for (j = 0; j < 8; j++)
1139 + r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
1140 + p->crc[i] = r;
1141 + }
1142 +}
1143 +
1144 +static void MatchFinder_FreeThisClassMemory(CMatchFinder *p, ISzAlloc *alloc)
1145 +{
1146 + alloc->Free(alloc, p->hash);
1147 + p->hash = 0;
1148 +}
1149 +
1150 +void MatchFinder_Free(CMatchFinder *p, ISzAlloc *alloc)
1151 +{
1152 + MatchFinder_FreeThisClassMemory(p, alloc);
1153 + LzInWindow_Free(p, alloc);
1154 +}
1155 +
1156 +static CLzRef* AllocRefs(UInt32 num, ISzAlloc *alloc)
1157 +{
1158 + size_t sizeInBytes = (size_t)num * sizeof(CLzRef);
1159 + if (sizeInBytes / sizeof(CLzRef) != num)
1160 + return 0;
1161 + return (CLzRef *)alloc->Alloc(alloc, sizeInBytes);
1162 +}
1163 +
1164 +int MatchFinder_Create(CMatchFinder *p, UInt32 historySize,
1165 + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter,
1166 + ISzAlloc *alloc)
1167 +{
1168 + UInt32 sizeReserv;
1169 + if (historySize > kMaxHistorySize)
1170 + {
1171 + MatchFinder_Free(p, alloc);
1172 + return 0;
1173 + }
1174 + sizeReserv = historySize >> 1;
1175 + if (historySize > ((UInt32)2 << 30))
1176 + sizeReserv = historySize >> 2;
1177 + sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19);
1178 +
1179 + p->keepSizeBefore = historySize + keepAddBufferBefore + 1;
1180 + p->keepSizeAfter = matchMaxLen + keepAddBufferAfter;
1181 + /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */
1182 + if (LzInWindow_Create(p, sizeReserv, alloc))
1183 + {
1184 + UInt32 newCyclicBufferSize = historySize + 1;
1185 + UInt32 hs;
1186 + p->matchMaxLen = matchMaxLen;
1187 + {
1188 + p->fixedHashSize = 0;
1189 + if (p->numHashBytes == 2)
1190 + hs = (1 << 16) - 1;
1191 + else
1192 + {
1193 + hs = historySize - 1;
1194 + hs |= (hs >> 1);
1195 + hs |= (hs >> 2);
1196 + hs |= (hs >> 4);
1197 + hs |= (hs >> 8);
1198 + hs >>= 1;
1199 + hs |= 0xFFFF; /* don't change it! It's required for Deflate */
1200 + if (hs > (1 << 24))
1201 + {
1202 + if (p->numHashBytes == 3)
1203 + hs = (1 << 24) - 1;
1204 + else
1205 + hs >>= 1;
1206 + }
1207 + }
1208 + p->hashMask = hs;
1209 + hs++;
1210 + if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size;
1211 + if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size;
1212 + if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size;
1213 + hs += p->fixedHashSize;
1214 + }
1215 +
1216 + {
1217 + UInt32 prevSize = p->hashSizeSum + p->numSons;
1218 + UInt32 newSize;
1219 + p->historySize = historySize;
1220 + p->hashSizeSum = hs;
1221 + p->cyclicBufferSize = newCyclicBufferSize;
1222 + p->numSons = (p->btMode ? newCyclicBufferSize * 2 : newCyclicBufferSize);
1223 + newSize = p->hashSizeSum + p->numSons;
1224 + if (p->hash != 0 && prevSize == newSize)
1225 + return 1;
1226 + MatchFinder_FreeThisClassMemory(p, alloc);
1227 + p->hash = AllocRefs(newSize, alloc);
1228 + if (p->hash != 0)
1229 + {
1230 + p->son = p->hash + p->hashSizeSum;
1231 + return 1;
1232 + }
1233 + }
1234 + }
1235 + MatchFinder_Free(p, alloc);
1236 + return 0;
1237 +}
1238 +
1239 +static void MatchFinder_SetLimits(CMatchFinder *p)
1240 +{
1241 + UInt32 limit = kMaxValForNormalize - p->pos;
1242 + UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos;
1243 + if (limit2 < limit)
1244 + limit = limit2;
1245 + limit2 = p->streamPos - p->pos;
1246 + if (limit2 <= p->keepSizeAfter)
1247 + {
1248 + if (limit2 > 0)
1249 + limit2 = 1;
1250 + }
1251 + else
1252 + limit2 -= p->keepSizeAfter;
1253 + if (limit2 < limit)
1254 + limit = limit2;
1255 + {
1256 + UInt32 lenLimit = p->streamPos - p->pos;
1257 + if (lenLimit > p->matchMaxLen)
1258 + lenLimit = p->matchMaxLen;
1259 + p->lenLimit = lenLimit;
1260 + }
1261 + p->posLimit = p->pos + limit;
1262 +}
1263 +
1264 +static void MatchFinder_Init(CMatchFinder *p)
1265 +{
1266 + UInt32 i;
1267 + for (i = 0; i < p->hashSizeSum; i++)
1268 + p->hash[i] = kEmptyHashValue;
1269 + p->cyclicBufferPos = 0;
1270 + p->buffer = p->bufferBase;
1271 + p->pos = p->streamPos = p->cyclicBufferSize;
1272 + p->result = SZ_OK;
1273 + p->streamEndWasReached = 0;
1274 + MatchFinder_ReadBlock(p);
1275 + MatchFinder_SetLimits(p);
1276 +}
1277 +
1278 +static UInt32 MatchFinder_GetSubValue(CMatchFinder *p)
1279 +{
1280 + return (p->pos - p->historySize - 1) & kNormalizeMask;
1281 +}
1282 +
1283 +static void MatchFinder_Normalize3(UInt32 subValue, CLzRef *items, UInt32 numItems)
1284 +{
1285 + UInt32 i;
1286 + for (i = 0; i < numItems; i++)
1287 + {
1288 + UInt32 value = items[i];
1289 + if (value <= subValue)
1290 + value = kEmptyHashValue;
1291 + else
1292 + value -= subValue;
1293 + items[i] = value;
1294 + }
1295 +}
1296 +
1297 +static void MatchFinder_Normalize(CMatchFinder *p)
1298 +{
1299 + UInt32 subValue = MatchFinder_GetSubValue(p);
1300 + MatchFinder_Normalize3(subValue, p->hash, p->hashSizeSum + p->numSons);
1301 + MatchFinder_ReduceOffsets(p, subValue);
1302 +}
1303 +
1304 +static void MatchFinder_CheckLimits(CMatchFinder *p)
1305 +{
1306 + if (p->pos == kMaxValForNormalize)
1307 + MatchFinder_Normalize(p);
1308 + if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos)
1309 + MatchFinder_CheckAndMoveAndRead(p);
1310 + if (p->cyclicBufferPos == p->cyclicBufferSize)
1311 + p->cyclicBufferPos = 0;
1312 + MatchFinder_SetLimits(p);
1313 +}
1314 +
1315 +static UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1316 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,
1317 + UInt32 *distances, UInt32 maxLen)
1318 +{
1319 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1320 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1321 + UInt32 len0 = 0, len1 = 0;
1322 + for (;;)
1323 + {
1324 + UInt32 delta = pos - curMatch;
1325 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1326 + {
1327 + *ptr0 = *ptr1 = kEmptyHashValue;
1328 + return distances;
1329 + }
1330 + {
1331 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1332 + const Byte *pb = cur - delta;
1333 + UInt32 len = (len0 < len1 ? len0 : len1);
1334 + if (pb[len] == cur[len])
1335 + {
1336 + if (++len != lenLimit && pb[len] == cur[len])
1337 + while (++len != lenLimit)
1338 + if (pb[len] != cur[len])
1339 + break;
1340 + if (maxLen < len)
1341 + {
1342 + *distances++ = maxLen = len;
1343 + *distances++ = delta - 1;
1344 + if (len == lenLimit)
1345 + {
1346 + *ptr1 = pair[0];
1347 + *ptr0 = pair[1];
1348 + return distances;
1349 + }
1350 + }
1351 + }
1352 + if (pb[len] < cur[len])
1353 + {
1354 + *ptr1 = curMatch;
1355 + ptr1 = pair + 1;
1356 + curMatch = *ptr1;
1357 + len1 = len;
1358 + }
1359 + else
1360 + {
1361 + *ptr0 = curMatch;
1362 + ptr0 = pair;
1363 + curMatch = *ptr0;
1364 + len0 = len;
1365 + }
1366 + }
1367 + }
1368 +}
1369 +
1370 +static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,
1371 + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)
1372 +{
1373 + CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;
1374 + CLzRef *ptr1 = son + (_cyclicBufferPos << 1);
1375 + UInt32 len0 = 0, len1 = 0;
1376 + for (;;)
1377 + {
1378 + UInt32 delta = pos - curMatch;
1379 + if (cutValue-- == 0 || delta >= _cyclicBufferSize)
1380 + {
1381 + *ptr0 = *ptr1 = kEmptyHashValue;
1382 + return;
1383 + }
1384 + {
1385 + CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);
1386 + const Byte *pb = cur - delta;
1387 + UInt32 len = (len0 < len1 ? len0 : len1);
1388 + if (pb[len] == cur[len])
1389 + {
1390 + while (++len != lenLimit)
1391 + if (pb[len] != cur[len])
1392 + break;
1393 + {
1394 + if (len == lenLimit)
1395 + {
1396 + *ptr1 = pair[0];
1397 + *ptr0 = pair[1];
1398 + return;
1399 + }
1400 + }
1401 + }
1402 + if (pb[len] < cur[len])
1403 + {
1404 + *ptr1 = curMatch;
1405 + ptr1 = pair + 1;
1406 + curMatch = *ptr1;
1407 + len1 = len;
1408 + }
1409 + else
1410 + {
1411 + *ptr0 = curMatch;
1412 + ptr0 = pair;
1413 + curMatch = *ptr0;
1414 + len0 = len;
1415 + }
1416 + }
1417 + }
1418 +}
1419 +
1420 +#define MOVE_POS \
1421 + ++p->cyclicBufferPos; \
1422 + p->buffer++; \
1423 + if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);
1424 +
1425 +static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }
1426 +
1427 +#define MOVE_POS_RET MatchFinder_MovePos(p); return offset;
1428 +
1429 +#define GET_MATCHES_HEADER2(minLen, ret_op) \
1430 + UInt32 lenLimit; UInt32 hashValue; const Byte *cur; UInt32 curMatch; \
1431 + lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \
1432 + cur = p->buffer;
1433 +
1434 +#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)
1435 +#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue)
1436 +
1437 +#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue
1438 +
1439 +#define GET_MATCHES_FOOTER(offset, maxLen) \
1440 + offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \
1441 + distances + offset, maxLen) - distances); MOVE_POS_RET;
1442 +
1443 +#define SKIP_FOOTER \
1444 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MatchFinder_MovePos(p);
1445 +
1446 +static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)
1447 +{
1448 + UInt32 hash2Value, hash3Value, delta2, delta3, maxLen, offset;
1449 + GET_MATCHES_HEADER(4)
1450 +
1451 + HASH4_CALC;
1452 +
1453 + delta2 = p->pos - p->hash[ hash2Value];
1454 + delta3 = p->pos - p->hash[kFix3HashSize + hash3Value];
1455 + curMatch = p->hash[kFix4HashSize + hashValue];
1456 +
1457 + p->hash[ hash2Value] =
1458 + p->hash[kFix3HashSize + hash3Value] =
1459 + p->hash[kFix4HashSize + hashValue] = p->pos;
1460 +
1461 + maxLen = 1;
1462 + offset = 0;
1463 + if (delta2 < p->cyclicBufferSize && *(cur - delta2) == *cur)
1464 + {
1465 + distances[0] = maxLen = 2;
1466 + distances[1] = delta2 - 1;
1467 + offset = 2;
1468 + }
1469 + if (delta2 != delta3 && delta3 < p->cyclicBufferSize && *(cur - delta3) == *cur)
1470 + {
1471 + maxLen = 3;
1472 + distances[offset + 1] = delta3 - 1;
1473 + offset += 2;
1474 + delta2 = delta3;
1475 + }
1476 + if (offset != 0)
1477 + {
1478 + for (; maxLen != lenLimit; maxLen++)
1479 + if (cur[(ptrdiff_t)maxLen - delta2] != cur[maxLen])
1480 + break;
1481 + distances[offset - 2] = maxLen;
1482 + if (maxLen == lenLimit)
1483 + {
1484 + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));
1485 + MOVE_POS_RET;
1486 + }
1487 + }
1488 + if (maxLen < 3)
1489 + maxLen = 3;
1490 + GET_MATCHES_FOOTER(offset, maxLen)
1491 +}
1492 +
1493 +static void Bt4_MatchFinder_Skip(CMatchFinder *p, UInt32 num)
1494 +{
1495 + do
1496 + {
1497 + UInt32 hash2Value, hash3Value;
1498 + SKIP_HEADER(4)
1499 + HASH4_CALC;
1500 + curMatch = p->hash[kFix4HashSize + hashValue];
1501 + p->hash[ hash2Value] =
1502 + p->hash[kFix3HashSize + hash3Value] = p->pos;
1503 + p->hash[kFix4HashSize + hashValue] = p->pos;
1504 + SKIP_FOOTER
1505 + }
1506 + while (--num != 0);
1507 +}
1508 +
1509 +void MatchFinder_CreateVTable(CMatchFinder *p, IMatchFinder *vTable)
1510 +{
1511 + vTable->Init = (Mf_Init_Func)MatchFinder_Init;
1512 + vTable->GetIndexByte = (Mf_GetIndexByte_Func)MatchFinder_GetIndexByte;
1513 + vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes;
1514 + vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos;
1515 + vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches;
1516 + vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip;
1517 +}
1518 --- /dev/null
1519 +++ b/lib/lzma/LzmaDec.c
1520 @@ -0,0 +1,925 @@
1521 +/* LzmaDec.c -- LZMA Decoder
1522 +2009-09-20 : Igor Pavlov : Public domain */
1523 +
1524 +#include "LzmaDec.h"
1525 +
1526 +#include <string.h>
1527 +
1528 +#define kNumTopBits 24
1529 +#define kTopValue ((UInt32)1 << kNumTopBits)
1530 +
1531 +#define kNumBitModelTotalBits 11
1532 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
1533 +#define kNumMoveBits 5
1534 +
1535 +#define RC_INIT_SIZE 5
1536 +
1537 +#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
1538 +
1539 +#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1540 +#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
1541 +#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
1542 +#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
1543 + { UPDATE_0(p); i = (i + i); A0; } else \
1544 + { UPDATE_1(p); i = (i + i) + 1; A1; }
1545 +#define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
1546 +
1547 +#define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
1548 +#define TREE_DECODE(probs, limit, i) \
1549 + { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
1550 +
1551 +/* #define _LZMA_SIZE_OPT */
1552 +
1553 +#ifdef _LZMA_SIZE_OPT
1554 +#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
1555 +#else
1556 +#define TREE_6_DECODE(probs, i) \
1557 + { i = 1; \
1558 + TREE_GET_BIT(probs, i); \
1559 + TREE_GET_BIT(probs, i); \
1560 + TREE_GET_BIT(probs, i); \
1561 + TREE_GET_BIT(probs, i); \
1562 + TREE_GET_BIT(probs, i); \
1563 + TREE_GET_BIT(probs, i); \
1564 + i -= 0x40; }
1565 +#endif
1566 +
1567 +#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
1568 +
1569 +#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
1570 +#define UPDATE_0_CHECK range = bound;
1571 +#define UPDATE_1_CHECK range -= bound; code -= bound;
1572 +#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
1573 + { UPDATE_0_CHECK; i = (i + i); A0; } else \
1574 + { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
1575 +#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
1576 +#define TREE_DECODE_CHECK(probs, limit, i) \
1577 + { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
1578 +
1579 +
1580 +#define kNumPosBitsMax 4
1581 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
1582 +
1583 +#define kLenNumLowBits 3
1584 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
1585 +#define kLenNumMidBits 3
1586 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
1587 +#define kLenNumHighBits 8
1588 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
1589 +
1590 +#define LenChoice 0
1591 +#define LenChoice2 (LenChoice + 1)
1592 +#define LenLow (LenChoice2 + 1)
1593 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
1594 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
1595 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
1596 +
1597 +
1598 +#define kNumStates 12
1599 +#define kNumLitStates 7
1600 +
1601 +#define kStartPosModelIndex 4
1602 +#define kEndPosModelIndex 14
1603 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
1604 +
1605 +#define kNumPosSlotBits 6
1606 +#define kNumLenToPosStates 4
1607 +
1608 +#define kNumAlignBits 4
1609 +#define kAlignTableSize (1 << kNumAlignBits)
1610 +
1611 +#define kMatchMinLen 2
1612 +#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
1613 +
1614 +#define IsMatch 0
1615 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
1616 +#define IsRepG0 (IsRep + kNumStates)
1617 +#define IsRepG1 (IsRepG0 + kNumStates)
1618 +#define IsRepG2 (IsRepG1 + kNumStates)
1619 +#define IsRep0Long (IsRepG2 + kNumStates)
1620 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
1621 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
1622 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
1623 +#define LenCoder (Align + kAlignTableSize)
1624 +#define RepLenCoder (LenCoder + kNumLenProbs)
1625 +#define Literal (RepLenCoder + kNumLenProbs)
1626 +
1627 +#define LZMA_BASE_SIZE 1846
1628 +#define LZMA_LIT_SIZE 768
1629 +
1630 +#define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
1631 +
1632 +#if Literal != LZMA_BASE_SIZE
1633 +StopCompilingDueBUG
1634 +#endif
1635 +
1636 +#define LZMA_DIC_MIN (1 << 12)
1637 +
1638 +/* First LZMA-symbol is always decoded.
1639 +And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
1640 +Out:
1641 + Result:
1642 + SZ_OK - OK
1643 + SZ_ERROR_DATA - Error
1644 + p->remainLen:
1645 + < kMatchSpecLenStart : normal remain
1646 + = kMatchSpecLenStart : finished
1647 + = kMatchSpecLenStart + 1 : Flush marker
1648 + = kMatchSpecLenStart + 2 : State Init Marker
1649 +*/
1650 +
1651 +static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
1652 +{
1653 + CLzmaProb *probs = p->probs;
1654 +
1655 + unsigned state = p->state;
1656 + UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
1657 + unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
1658 + unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
1659 + unsigned lc = p->prop.lc;
1660 +
1661 + Byte *dic = p->dic;
1662 + SizeT dicBufSize = p->dicBufSize;
1663 + SizeT dicPos = p->dicPos;
1664 +
1665 + UInt32 processedPos = p->processedPos;
1666 + UInt32 checkDicSize = p->checkDicSize;
1667 + unsigned len = 0;
1668 +
1669 + const Byte *buf = p->buf;
1670 + UInt32 range = p->range;
1671 + UInt32 code = p->code;
1672 +
1673 + do
1674 + {
1675 + CLzmaProb *prob;
1676 + UInt32 bound;
1677 + unsigned ttt;
1678 + unsigned posState = processedPos & pbMask;
1679 +
1680 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
1681 + IF_BIT_0(prob)
1682 + {
1683 + unsigned symbol;
1684 + UPDATE_0(prob);
1685 + prob = probs + Literal;
1686 + if (checkDicSize != 0 || processedPos != 0)
1687 + prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
1688 + (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
1689 +
1690 + if (state < kNumLitStates)
1691 + {
1692 + state -= (state < 4) ? state : 3;
1693 + symbol = 1;
1694 + do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
1695 + }
1696 + else
1697 + {
1698 + unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1699 + unsigned offs = 0x100;
1700 + state -= (state < 10) ? 3 : 6;
1701 + symbol = 1;
1702 + do
1703 + {
1704 + unsigned bit;
1705 + CLzmaProb *probLit;
1706 + matchByte <<= 1;
1707 + bit = (matchByte & offs);
1708 + probLit = prob + offs + bit + symbol;
1709 + GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
1710 + }
1711 + while (symbol < 0x100);
1712 + }
1713 + dic[dicPos++] = (Byte)symbol;
1714 + processedPos++;
1715 + continue;
1716 + }
1717 + else
1718 + {
1719 + UPDATE_1(prob);
1720 + prob = probs + IsRep + state;
1721 + IF_BIT_0(prob)
1722 + {
1723 + UPDATE_0(prob);
1724 + state += kNumStates;
1725 + prob = probs + LenCoder;
1726 + }
1727 + else
1728 + {
1729 + UPDATE_1(prob);
1730 + if (checkDicSize == 0 && processedPos == 0)
1731 + return SZ_ERROR_DATA;
1732 + prob = probs + IsRepG0 + state;
1733 + IF_BIT_0(prob)
1734 + {
1735 + UPDATE_0(prob);
1736 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
1737 + IF_BIT_0(prob)
1738 + {
1739 + UPDATE_0(prob);
1740 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1741 + dicPos++;
1742 + processedPos++;
1743 + state = state < kNumLitStates ? 9 : 11;
1744 + continue;
1745 + }
1746 + UPDATE_1(prob);
1747 + }
1748 + else
1749 + {
1750 + UInt32 distance;
1751 + UPDATE_1(prob);
1752 + prob = probs + IsRepG1 + state;
1753 + IF_BIT_0(prob)
1754 + {
1755 + UPDATE_0(prob);
1756 + distance = rep1;
1757 + }
1758 + else
1759 + {
1760 + UPDATE_1(prob);
1761 + prob = probs + IsRepG2 + state;
1762 + IF_BIT_0(prob)
1763 + {
1764 + UPDATE_0(prob);
1765 + distance = rep2;
1766 + }
1767 + else
1768 + {
1769 + UPDATE_1(prob);
1770 + distance = rep3;
1771 + rep3 = rep2;
1772 + }
1773 + rep2 = rep1;
1774 + }
1775 + rep1 = rep0;
1776 + rep0 = distance;
1777 + }
1778 + state = state < kNumLitStates ? 8 : 11;
1779 + prob = probs + RepLenCoder;
1780 + }
1781 + {
1782 + unsigned limit, offset;
1783 + CLzmaProb *probLen = prob + LenChoice;
1784 + IF_BIT_0(probLen)
1785 + {
1786 + UPDATE_0(probLen);
1787 + probLen = prob + LenLow + (posState << kLenNumLowBits);
1788 + offset = 0;
1789 + limit = (1 << kLenNumLowBits);
1790 + }
1791 + else
1792 + {
1793 + UPDATE_1(probLen);
1794 + probLen = prob + LenChoice2;
1795 + IF_BIT_0(probLen)
1796 + {
1797 + UPDATE_0(probLen);
1798 + probLen = prob + LenMid + (posState << kLenNumMidBits);
1799 + offset = kLenNumLowSymbols;
1800 + limit = (1 << kLenNumMidBits);
1801 + }
1802 + else
1803 + {
1804 + UPDATE_1(probLen);
1805 + probLen = prob + LenHigh;
1806 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
1807 + limit = (1 << kLenNumHighBits);
1808 + }
1809 + }
1810 + TREE_DECODE(probLen, limit, len);
1811 + len += offset;
1812 + }
1813 +
1814 + if (state >= kNumStates)
1815 + {
1816 + UInt32 distance;
1817 + prob = probs + PosSlot +
1818 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
1819 + TREE_6_DECODE(prob, distance);
1820 + if (distance >= kStartPosModelIndex)
1821 + {
1822 + unsigned posSlot = (unsigned)distance;
1823 + int numDirectBits = (int)(((distance >> 1) - 1));
1824 + distance = (2 | (distance & 1));
1825 + if (posSlot < kEndPosModelIndex)
1826 + {
1827 + distance <<= numDirectBits;
1828 + prob = probs + SpecPos + distance - posSlot - 1;
1829 + {
1830 + UInt32 mask = 1;
1831 + unsigned i = 1;
1832 + do
1833 + {
1834 + GET_BIT2(prob + i, i, ; , distance |= mask);
1835 + mask <<= 1;
1836 + }
1837 + while (--numDirectBits != 0);
1838 + }
1839 + }
1840 + else
1841 + {
1842 + numDirectBits -= kNumAlignBits;
1843 + do
1844 + {
1845 + NORMALIZE
1846 + range >>= 1;
1847 +
1848 + {
1849 + UInt32 t;
1850 + code -= range;
1851 + t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
1852 + distance = (distance << 1) + (t + 1);
1853 + code += range & t;
1854 + }
1855 + /*
1856 + distance <<= 1;
1857 + if (code >= range)
1858 + {
1859 + code -= range;
1860 + distance |= 1;
1861 + }
1862 + */
1863 + }
1864 + while (--numDirectBits != 0);
1865 + prob = probs + Align;
1866 + distance <<= kNumAlignBits;
1867 + {
1868 + unsigned i = 1;
1869 + GET_BIT2(prob + i, i, ; , distance |= 1);
1870 + GET_BIT2(prob + i, i, ; , distance |= 2);
1871 + GET_BIT2(prob + i, i, ; , distance |= 4);
1872 + GET_BIT2(prob + i, i, ; , distance |= 8);
1873 + }
1874 + if (distance == (UInt32)0xFFFFFFFF)
1875 + {
1876 + len += kMatchSpecLenStart;
1877 + state -= kNumStates;
1878 + break;
1879 + }
1880 + }
1881 + }
1882 + rep3 = rep2;
1883 + rep2 = rep1;
1884 + rep1 = rep0;
1885 + rep0 = distance + 1;
1886 + if (checkDicSize == 0)
1887 + {
1888 + if (distance >= processedPos)
1889 + return SZ_ERROR_DATA;
1890 + }
1891 + else if (distance >= checkDicSize)
1892 + return SZ_ERROR_DATA;
1893 + state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
1894 + }
1895 +
1896 + len += kMatchMinLen;
1897 +
1898 + if (limit == dicPos)
1899 + return SZ_ERROR_DATA;
1900 + {
1901 + SizeT rem = limit - dicPos;
1902 + unsigned curLen = ((rem < len) ? (unsigned)rem : len);
1903 + SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
1904 +
1905 + processedPos += curLen;
1906 +
1907 + len -= curLen;
1908 + if (pos + curLen <= dicBufSize)
1909 + {
1910 + Byte *dest = dic + dicPos;
1911 + ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
1912 + const Byte *lim = dest + curLen;
1913 + dicPos += curLen;
1914 + do
1915 + *(dest) = (Byte)*(dest + src);
1916 + while (++dest != lim);
1917 + }
1918 + else
1919 + {
1920 + do
1921 + {
1922 + dic[dicPos++] = dic[pos];
1923 + if (++pos == dicBufSize)
1924 + pos = 0;
1925 + }
1926 + while (--curLen != 0);
1927 + }
1928 + }
1929 + }
1930 + }
1931 + while (dicPos < limit && buf < bufLimit);
1932 + NORMALIZE;
1933 + p->buf = buf;
1934 + p->range = range;
1935 + p->code = code;
1936 + p->remainLen = len;
1937 + p->dicPos = dicPos;
1938 + p->processedPos = processedPos;
1939 + p->reps[0] = rep0;
1940 + p->reps[1] = rep1;
1941 + p->reps[2] = rep2;
1942 + p->reps[3] = rep3;
1943 + p->state = state;
1944 +
1945 + return SZ_OK;
1946 +}
1947 +
1948 +static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
1949 +{
1950 + if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
1951 + {
1952 + Byte *dic = p->dic;
1953 + SizeT dicPos = p->dicPos;
1954 + SizeT dicBufSize = p->dicBufSize;
1955 + unsigned len = p->remainLen;
1956 + UInt32 rep0 = p->reps[0];
1957 + if (limit - dicPos < len)
1958 + len = (unsigned)(limit - dicPos);
1959 +
1960 + if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
1961 + p->checkDicSize = p->prop.dicSize;
1962 +
1963 + p->processedPos += len;
1964 + p->remainLen -= len;
1965 + while (len-- != 0)
1966 + {
1967 + dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
1968 + dicPos++;
1969 + }
1970 + p->dicPos = dicPos;
1971 + }
1972 +}
1973 +
1974 +static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
1975 +{
1976 + do
1977 + {
1978 + SizeT limit2 = limit;
1979 + if (p->checkDicSize == 0)
1980 + {
1981 + UInt32 rem = p->prop.dicSize - p->processedPos;
1982 + if (limit - p->dicPos > rem)
1983 + limit2 = p->dicPos + rem;
1984 + }
1985 + RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
1986 + if (p->processedPos >= p->prop.dicSize)
1987 + p->checkDicSize = p->prop.dicSize;
1988 + LzmaDec_WriteRem(p, limit);
1989 + }
1990 + while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
1991 +
1992 + if (p->remainLen > kMatchSpecLenStart)
1993 + {
1994 + p->remainLen = kMatchSpecLenStart;
1995 + }
1996 + return 0;
1997 +}
1998 +
1999 +typedef enum
2000 +{
2001 + DUMMY_ERROR, /* unexpected end of input stream */
2002 + DUMMY_LIT,
2003 + DUMMY_MATCH,
2004 + DUMMY_REP
2005 +} ELzmaDummy;
2006 +
2007 +static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
2008 +{
2009 + UInt32 range = p->range;
2010 + UInt32 code = p->code;
2011 + const Byte *bufLimit = buf + inSize;
2012 + CLzmaProb *probs = p->probs;
2013 + unsigned state = p->state;
2014 + ELzmaDummy res;
2015 +
2016 + {
2017 + CLzmaProb *prob;
2018 + UInt32 bound;
2019 + unsigned ttt;
2020 + unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
2021 +
2022 + prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
2023 + IF_BIT_0_CHECK(prob)
2024 + {
2025 + UPDATE_0_CHECK
2026 +
2027 + /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
2028 +
2029 + prob = probs + Literal;
2030 + if (p->checkDicSize != 0 || p->processedPos != 0)
2031 + prob += (LZMA_LIT_SIZE *
2032 + ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
2033 + (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
2034 +
2035 + if (state < kNumLitStates)
2036 + {
2037 + unsigned symbol = 1;
2038 + do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
2039 + }
2040 + else
2041 + {
2042 + unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
2043 + ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
2044 + unsigned offs = 0x100;
2045 + unsigned symbol = 1;
2046 + do
2047 + {
2048 + unsigned bit;
2049 + CLzmaProb *probLit;
2050 + matchByte <<= 1;
2051 + bit = (matchByte & offs);
2052 + probLit = prob + offs + bit + symbol;
2053 + GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
2054 + }
2055 + while (symbol < 0x100);
2056 + }
2057 + res = DUMMY_LIT;
2058 + }
2059 + else
2060 + {
2061 + unsigned len;
2062 + UPDATE_1_CHECK;
2063 +
2064 + prob = probs + IsRep + state;
2065 + IF_BIT_0_CHECK(prob)
2066 + {
2067 + UPDATE_0_CHECK;
2068 + state = 0;
2069 + prob = probs + LenCoder;
2070 + res = DUMMY_MATCH;
2071 + }
2072 + else
2073 + {
2074 + UPDATE_1_CHECK;
2075 + res = DUMMY_REP;
2076 + prob = probs + IsRepG0 + state;
2077 + IF_BIT_0_CHECK(prob)
2078 + {
2079 + UPDATE_0_CHECK;
2080 + prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
2081 + IF_BIT_0_CHECK(prob)
2082 + {
2083 + UPDATE_0_CHECK;
2084 + NORMALIZE_CHECK;
2085 + return DUMMY_REP;
2086 + }
2087 + else
2088 + {
2089 + UPDATE_1_CHECK;
2090 + }
2091 + }
2092 + else
2093 + {
2094 + UPDATE_1_CHECK;
2095 + prob = probs + IsRepG1 + state;
2096 + IF_BIT_0_CHECK(prob)
2097 + {
2098 + UPDATE_0_CHECK;
2099 + }
2100 + else
2101 + {
2102 + UPDATE_1_CHECK;
2103 + prob = probs + IsRepG2 + state;
2104 + IF_BIT_0_CHECK(prob)
2105 + {
2106 + UPDATE_0_CHECK;
2107 + }
2108 + else
2109 + {
2110 + UPDATE_1_CHECK;
2111 + }
2112 + }
2113 + }
2114 + state = kNumStates;
2115 + prob = probs + RepLenCoder;
2116 + }
2117 + {
2118 + unsigned limit, offset;
2119 + CLzmaProb *probLen = prob + LenChoice;
2120 + IF_BIT_0_CHECK(probLen)
2121 + {
2122 + UPDATE_0_CHECK;
2123 + probLen = prob + LenLow + (posState << kLenNumLowBits);
2124 + offset = 0;
2125 + limit = 1 << kLenNumLowBits;
2126 + }
2127 + else
2128 + {
2129 + UPDATE_1_CHECK;
2130 + probLen = prob + LenChoice2;
2131 + IF_BIT_0_CHECK(probLen)
2132 + {
2133 + UPDATE_0_CHECK;
2134 + probLen = prob + LenMid + (posState << kLenNumMidBits);
2135 + offset = kLenNumLowSymbols;
2136 + limit = 1 << kLenNumMidBits;
2137 + }
2138 + else
2139 + {
2140 + UPDATE_1_CHECK;
2141 + probLen = prob + LenHigh;
2142 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
2143 + limit = 1 << kLenNumHighBits;
2144 + }
2145 + }
2146 + TREE_DECODE_CHECK(probLen, limit, len);
2147 + len += offset;
2148 + }
2149 +
2150 + if (state < 4)
2151 + {
2152 + unsigned posSlot;
2153 + prob = probs + PosSlot +
2154 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
2155 + kNumPosSlotBits);
2156 + TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
2157 + if (posSlot >= kStartPosModelIndex)
2158 + {
2159 + int numDirectBits = ((posSlot >> 1) - 1);
2160 +
2161 + /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
2162 +
2163 + if (posSlot < kEndPosModelIndex)
2164 + {
2165 + prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
2166 + }
2167 + else
2168 + {
2169 + numDirectBits -= kNumAlignBits;
2170 + do
2171 + {
2172 + NORMALIZE_CHECK
2173 + range >>= 1;
2174 + code -= range & (((code - range) >> 31) - 1);
2175 + /* if (code >= range) code -= range; */
2176 + }
2177 + while (--numDirectBits != 0);
2178 + prob = probs + Align;
2179 + numDirectBits = kNumAlignBits;
2180 + }
2181 + {
2182 + unsigned i = 1;
2183 + do
2184 + {
2185 + GET_BIT_CHECK(prob + i, i);
2186 + }
2187 + while (--numDirectBits != 0);
2188 + }
2189 + }
2190 + }
2191 + }
2192 + }
2193 + NORMALIZE_CHECK;
2194 + return res;
2195 +}
2196 +
2197 +
2198 +static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
2199 +{
2200 + p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
2201 + p->range = 0xFFFFFFFF;
2202 + p->needFlush = 0;
2203 +}
2204 +
2205 +static void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
2206 +{
2207 + p->needFlush = 1;
2208 + p->remainLen = 0;
2209 + p->tempBufSize = 0;
2210 +
2211 + if (initDic)
2212 + {
2213 + p->processedPos = 0;
2214 + p->checkDicSize = 0;
2215 + p->needInitState = 1;
2216 + }
2217 + if (initState)
2218 + p->needInitState = 1;
2219 +}
2220 +
2221 +static void LzmaDec_Init(CLzmaDec *p)
2222 +{
2223 + p->dicPos = 0;
2224 + LzmaDec_InitDicAndState(p, True, True);
2225 +}
2226 +
2227 +static void LzmaDec_InitStateReal(CLzmaDec *p)
2228 +{
2229 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
2230 + UInt32 i;
2231 + CLzmaProb *probs = p->probs;
2232 + for (i = 0; i < numProbs; i++)
2233 + probs[i] = kBitModelTotal >> 1;
2234 + p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
2235 + p->state = 0;
2236 + p->needInitState = 0;
2237 +}
2238 +
2239 +static SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
2240 + ELzmaFinishMode finishMode, ELzmaStatus *status)
2241 +{
2242 + SizeT inSize = *srcLen;
2243 + (*srcLen) = 0;
2244 + LzmaDec_WriteRem(p, dicLimit);
2245 +
2246 + *status = LZMA_STATUS_NOT_SPECIFIED;
2247 +
2248 + while (p->remainLen != kMatchSpecLenStart)
2249 + {
2250 + int checkEndMarkNow;
2251 +
2252 + if (p->needFlush != 0)
2253 + {
2254 + for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
2255 + p->tempBuf[p->tempBufSize++] = *src++;
2256 + if (p->tempBufSize < RC_INIT_SIZE)
2257 + {
2258 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2259 + return SZ_OK;
2260 + }
2261 + if (p->tempBuf[0] != 0)
2262 + return SZ_ERROR_DATA;
2263 +
2264 + LzmaDec_InitRc(p, p->tempBuf);
2265 + p->tempBufSize = 0;
2266 + }
2267 +
2268 + checkEndMarkNow = 0;
2269 + if (p->dicPos >= dicLimit)
2270 + {
2271 + if (p->remainLen == 0 && p->code == 0)
2272 + {
2273 + *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
2274 + return SZ_OK;
2275 + }
2276 + if (finishMode == LZMA_FINISH_ANY)
2277 + {
2278 + *status = LZMA_STATUS_NOT_FINISHED;
2279 + return SZ_OK;
2280 + }
2281 + if (p->remainLen != 0)
2282 + {
2283 + *status = LZMA_STATUS_NOT_FINISHED;
2284 + return SZ_ERROR_DATA;
2285 + }
2286 + checkEndMarkNow = 1;
2287 + }
2288 +
2289 + if (p->needInitState)
2290 + LzmaDec_InitStateReal(p);
2291 +
2292 + if (p->tempBufSize == 0)
2293 + {
2294 + SizeT processed;
2295 + const Byte *bufLimit;
2296 + if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2297 + {
2298 + int dummyRes = LzmaDec_TryDummy(p, src, inSize);
2299 + if (dummyRes == DUMMY_ERROR)
2300 + {
2301 + memcpy(p->tempBuf, src, inSize);
2302 + p->tempBufSize = (unsigned)inSize;
2303 + (*srcLen) += inSize;
2304 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2305 + return SZ_OK;
2306 + }
2307 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2308 + {
2309 + *status = LZMA_STATUS_NOT_FINISHED;
2310 + return SZ_ERROR_DATA;
2311 + }
2312 + bufLimit = src;
2313 + }
2314 + else
2315 + bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
2316 + p->buf = src;
2317 + if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
2318 + return SZ_ERROR_DATA;
2319 + processed = (SizeT)(p->buf - src);
2320 + (*srcLen) += processed;
2321 + src += processed;
2322 + inSize -= processed;
2323 + }
2324 + else
2325 + {
2326 + unsigned rem = p->tempBufSize, lookAhead = 0;
2327 + while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
2328 + p->tempBuf[rem++] = src[lookAhead++];
2329 + p->tempBufSize = rem;
2330 + if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
2331 + {
2332 + int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
2333 + if (dummyRes == DUMMY_ERROR)
2334 + {
2335 + (*srcLen) += lookAhead;
2336 + *status = LZMA_STATUS_NEEDS_MORE_INPUT;
2337 + return SZ_OK;
2338 + }
2339 + if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
2340 + {
2341 + *status = LZMA_STATUS_NOT_FINISHED;
2342 + return SZ_ERROR_DATA;
2343 + }
2344 + }
2345 + p->buf = p->tempBuf;
2346 + if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
2347 + return SZ_ERROR_DATA;
2348 + lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
2349 + (*srcLen) += lookAhead;
2350 + src += lookAhead;
2351 + inSize -= lookAhead;
2352 + p->tempBufSize = 0;
2353 + }
2354 + }
2355 + if (p->code == 0)
2356 + *status = LZMA_STATUS_FINISHED_WITH_MARK;
2357 + return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
2358 +}
2359 +
2360 +static void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
2361 +{
2362 + alloc->Free(alloc, p->probs);
2363 + p->probs = 0;
2364 +}
2365 +
2366 +static SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
2367 +{
2368 + UInt32 dicSize;
2369 + Byte d;
2370 +
2371 + if (size < LZMA_PROPS_SIZE)
2372 + return SZ_ERROR_UNSUPPORTED;
2373 + else
2374 + dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
2375 +
2376 + if (dicSize < LZMA_DIC_MIN)
2377 + dicSize = LZMA_DIC_MIN;
2378 + p->dicSize = dicSize;
2379 +
2380 + d = data[0];
2381 + if (d >= (9 * 5 * 5))
2382 + return SZ_ERROR_UNSUPPORTED;
2383 +
2384 + p->lc = d % 9;
2385 + d /= 9;
2386 + p->pb = d / 5;
2387 + p->lp = d % 5;
2388 +
2389 + return SZ_OK;
2390 +}
2391 +
2392 +static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
2393 +{
2394 + UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
2395 + if (p->probs == 0 || numProbs != p->numProbs)
2396 + {
2397 + LzmaDec_FreeProbs(p, alloc);
2398 + p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
2399 + p->numProbs = numProbs;
2400 + if (p->probs == 0)
2401 + return SZ_ERROR_MEM;
2402 + }
2403 + return SZ_OK;
2404 +}
2405 +
2406 +static SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
2407 +{
2408 + CLzmaProps propNew;
2409 + RINOK(LzmaProps_Decode(&propNew, props, propsSize));
2410 + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
2411 + p->prop = propNew;
2412 + return SZ_OK;
2413 +}
2414 +
2415 +SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
2416 + const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
2417 + ELzmaStatus *status, ISzAlloc *alloc)
2418 +{
2419 + CLzmaDec p;
2420 + SRes res;
2421 + SizeT inSize = *srcLen;
2422 + SizeT outSize = *destLen;
2423 + *srcLen = *destLen = 0;
2424 + if (inSize < RC_INIT_SIZE)
2425 + return SZ_ERROR_INPUT_EOF;
2426 +
2427 + LzmaDec_Construct(&p);
2428 + res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
2429 + if (res != 0)
2430 + return res;
2431 + p.dic = dest;
2432 + p.dicBufSize = outSize;
2433 +
2434 + LzmaDec_Init(&p);
2435 +
2436 + *srcLen = inSize;
2437 + res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
2438 +
2439 + if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
2440 + res = SZ_ERROR_INPUT_EOF;
2441 +
2442 + (*destLen) = p.dicPos;
2443 + LzmaDec_FreeProbs(&p, alloc);
2444 + return res;
2445 +}
2446 --- /dev/null
2447 +++ b/lib/lzma/LzmaEnc.c
2448 @@ -0,0 +1,2123 @@
2449 +/* LzmaEnc.c -- LZMA Encoder
2450 +2009-11-24 : Igor Pavlov : Public domain */
2451 +
2452 +#include <string.h>
2453 +
2454 +/* #define SHOW_STAT */
2455 +/* #define SHOW_STAT2 */
2456 +
2457 +#if defined(SHOW_STAT) || defined(SHOW_STAT2)
2458 +#include <stdio.h>
2459 +#endif
2460 +
2461 +#include "LzmaEnc.h"
2462 +
2463 +/* disable MT */
2464 +#define _7ZIP_ST
2465 +
2466 +#include "LzFind.h"
2467 +#ifndef _7ZIP_ST
2468 +#include "LzFindMt.h"
2469 +#endif
2470 +
2471 +#ifdef SHOW_STAT
2472 +static int ttt = 0;
2473 +#endif
2474 +
2475 +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
2476 +
2477 +#define kBlockSize (9 << 10)
2478 +#define kUnpackBlockSize (1 << 18)
2479 +#define kMatchArraySize (1 << 21)
2480 +#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
2481 +
2482 +#define kNumMaxDirectBits (31)
2483 +
2484 +#define kNumTopBits 24
2485 +#define kTopValue ((UInt32)1 << kNumTopBits)
2486 +
2487 +#define kNumBitModelTotalBits 11
2488 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
2489 +#define kNumMoveBits 5
2490 +#define kProbInitValue (kBitModelTotal >> 1)
2491 +
2492 +#define kNumMoveReducingBits 4
2493 +#define kNumBitPriceShiftBits 4
2494 +#define kBitPrice (1 << kNumBitPriceShiftBits)
2495 +
2496 +void LzmaEncProps_Init(CLzmaEncProps *p)
2497 +{
2498 + p->level = 5;
2499 + p->dictSize = p->mc = 0;
2500 + p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
2501 + p->writeEndMark = 0;
2502 +}
2503 +
2504 +static void LzmaEncProps_Normalize(CLzmaEncProps *p)
2505 +{
2506 + int level = p->level;
2507 + if (level < 0) level = 5;
2508 + p->level = level;
2509 + if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
2510 + if (p->lc < 0) p->lc = 3;
2511 + if (p->lp < 0) p->lp = 0;
2512 + if (p->pb < 0) p->pb = 2;
2513 + if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
2514 + if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
2515 + if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
2516 + if (p->numHashBytes < 0) p->numHashBytes = 4;
2517 + if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
2518 + if (p->numThreads < 0)
2519 + p->numThreads =
2520 + #ifndef _7ZIP_ST
2521 + ((p->btMode && p->algo) ? 2 : 1);
2522 + #else
2523 + 1;
2524 + #endif
2525 +}
2526 +
2527 +static UInt32 __maybe_unused LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
2528 +{
2529 + CLzmaEncProps props = *props2;
2530 + LzmaEncProps_Normalize(&props);
2531 + return props.dictSize;
2532 +}
2533 +
2534 +/* #define LZMA_LOG_BSR */
2535 +/* Define it for Intel's CPU */
2536 +
2537 +
2538 +#ifdef LZMA_LOG_BSR
2539 +
2540 +#define kDicLogSizeMaxCompress 30
2541 +
2542 +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
2543 +
2544 +static UInt32 GetPosSlot1(UInt32 pos)
2545 +{
2546 + UInt32 res;
2547 + BSR2_RET(pos, res);
2548 + return res;
2549 +}
2550 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
2551 +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
2552 +
2553 +#else
2554 +
2555 +#define kNumLogBits (9 + (int)sizeof(size_t) / 2)
2556 +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
2557 +
2558 +static void LzmaEnc_FastPosInit(Byte *g_FastPos)
2559 +{
2560 + int c = 2, slotFast;
2561 + g_FastPos[0] = 0;
2562 + g_FastPos[1] = 1;
2563 +
2564 + for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
2565 + {
2566 + UInt32 k = (1 << ((slotFast >> 1) - 1));
2567 + UInt32 j;
2568 + for (j = 0; j < k; j++, c++)
2569 + g_FastPos[c] = (Byte)slotFast;
2570 + }
2571 +}
2572 +
2573 +#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
2574 + (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
2575 + res = p->g_FastPos[pos >> i] + (i * 2); }
2576 +/*
2577 +#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
2578 + p->g_FastPos[pos >> 6] + 12 : \
2579 + p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
2580 +*/
2581 +
2582 +#define GetPosSlot1(pos) p->g_FastPos[pos]
2583 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
2584 +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
2585 +
2586 +#endif
2587 +
2588 +
2589 +#define LZMA_NUM_REPS 4
2590 +
2591 +typedef unsigned CState;
2592 +
2593 +typedef struct
2594 +{
2595 + UInt32 price;
2596 +
2597 + CState state;
2598 + int prev1IsChar;
2599 + int prev2;
2600 +
2601 + UInt32 posPrev2;
2602 + UInt32 backPrev2;
2603 +
2604 + UInt32 posPrev;
2605 + UInt32 backPrev;
2606 + UInt32 backs[LZMA_NUM_REPS];
2607 +} COptimal;
2608 +
2609 +#define kNumOpts (1 << 12)
2610 +
2611 +#define kNumLenToPosStates 4
2612 +#define kNumPosSlotBits 6
2613 +#define kDicLogSizeMin 0
2614 +#define kDicLogSizeMax 32
2615 +#define kDistTableSizeMax (kDicLogSizeMax * 2)
2616 +
2617 +
2618 +#define kNumAlignBits 4
2619 +#define kAlignTableSize (1 << kNumAlignBits)
2620 +#define kAlignMask (kAlignTableSize - 1)
2621 +
2622 +#define kStartPosModelIndex 4
2623 +#define kEndPosModelIndex 14
2624 +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
2625 +
2626 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
2627 +
2628 +#ifdef _LZMA_PROB32
2629 +#define CLzmaProb UInt32
2630 +#else
2631 +#define CLzmaProb UInt16
2632 +#endif
2633 +
2634 +#define LZMA_PB_MAX 4
2635 +#define LZMA_LC_MAX 8
2636 +#define LZMA_LP_MAX 4
2637 +
2638 +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
2639 +
2640 +
2641 +#define kLenNumLowBits 3
2642 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
2643 +#define kLenNumMidBits 3
2644 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
2645 +#define kLenNumHighBits 8
2646 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
2647 +
2648 +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
2649 +
2650 +#define LZMA_MATCH_LEN_MIN 2
2651 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
2652 +
2653 +#define kNumStates 12
2654 +
2655 +typedef struct
2656 +{
2657 + CLzmaProb choice;
2658 + CLzmaProb choice2;
2659 + CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
2660 + CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
2661 + CLzmaProb high[kLenNumHighSymbols];
2662 +} CLenEnc;
2663 +
2664 +typedef struct
2665 +{
2666 + CLenEnc p;
2667 + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
2668 + UInt32 tableSize;
2669 + UInt32 counters[LZMA_NUM_PB_STATES_MAX];
2670 +} CLenPriceEnc;
2671 +
2672 +typedef struct
2673 +{
2674 + UInt32 range;
2675 + Byte cache;
2676 + UInt64 low;
2677 + UInt64 cacheSize;
2678 + Byte *buf;
2679 + Byte *bufLim;
2680 + Byte *bufBase;
2681 + ISeqOutStream *outStream;
2682 + UInt64 processed;
2683 + SRes res;
2684 +} CRangeEnc;
2685 +
2686 +typedef struct
2687 +{
2688 + CLzmaProb *litProbs;
2689 +
2690 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
2691 + CLzmaProb isRep[kNumStates];
2692 + CLzmaProb isRepG0[kNumStates];
2693 + CLzmaProb isRepG1[kNumStates];
2694 + CLzmaProb isRepG2[kNumStates];
2695 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
2696 +
2697 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
2698 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
2699 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
2700 +
2701 + CLenPriceEnc lenEnc;
2702 + CLenPriceEnc repLenEnc;
2703 +
2704 + UInt32 reps[LZMA_NUM_REPS];
2705 + UInt32 state;
2706 +} CSaveState;
2707 +
2708 +typedef struct
2709 +{
2710 + IMatchFinder matchFinder;
2711 + void *matchFinderObj;
2712 +
2713 + #ifndef _7ZIP_ST
2714 + Bool mtMode;
2715 + CMatchFinderMt matchFinderMt;
2716 + #endif
2717 +
2718 + CMatchFinder matchFinderBase;
2719 +
2720 + #ifndef _7ZIP_ST
2721 + Byte pad[128];
2722 + #endif
2723 +
2724 + UInt32 optimumEndIndex;
2725 + UInt32 optimumCurrentIndex;
2726 +
2727 + UInt32 longestMatchLength;
2728 + UInt32 numPairs;
2729 + UInt32 numAvail;
2730 + COptimal opt[kNumOpts];
2731 +
2732 + #ifndef LZMA_LOG_BSR
2733 + Byte g_FastPos[1 << kNumLogBits];
2734 + #endif
2735 +
2736 + UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
2737 + UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
2738 + UInt32 numFastBytes;
2739 + UInt32 additionalOffset;
2740 + UInt32 reps[LZMA_NUM_REPS];
2741 + UInt32 state;
2742 +
2743 + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
2744 + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
2745 + UInt32 alignPrices[kAlignTableSize];
2746 + UInt32 alignPriceCount;
2747 +
2748 + UInt32 distTableSize;
2749 +
2750 + unsigned lc, lp, pb;
2751 + unsigned lpMask, pbMask;
2752 +
2753 + CLzmaProb *litProbs;
2754 +
2755 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
2756 + CLzmaProb isRep[kNumStates];
2757 + CLzmaProb isRepG0[kNumStates];
2758 + CLzmaProb isRepG1[kNumStates];
2759 + CLzmaProb isRepG2[kNumStates];
2760 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
2761 +
2762 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
2763 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
2764 + CLzmaProb posAlignEncoder[1 << kNumAlignBits];
2765 +
2766 + CLenPriceEnc lenEnc;
2767 + CLenPriceEnc repLenEnc;
2768 +
2769 + unsigned lclp;
2770 +
2771 + Bool fastMode;
2772 +
2773 + CRangeEnc rc;
2774 +
2775 + Bool writeEndMark;
2776 + UInt64 nowPos64;
2777 + UInt32 matchPriceCount;
2778 + Bool finished;
2779 + Bool multiThread;
2780 +
2781 + SRes result;
2782 + UInt32 dictSize;
2783 + UInt32 matchFinderCycles;
2784 +
2785 + int needInit;
2786 +
2787 + CSaveState saveState;
2788 +} CLzmaEnc;
2789 +
2790 +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
2791 +{
2792 + CLzmaEnc *p = (CLzmaEnc *)pp;
2793 + CLzmaEncProps props = *props2;
2794 + LzmaEncProps_Normalize(&props);
2795 +
2796 + if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
2797 + props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))
2798 + return SZ_ERROR_PARAM;
2799 + p->dictSize = props.dictSize;
2800 + p->matchFinderCycles = props.mc;
2801 + {
2802 + unsigned fb = props.fb;
2803 + if (fb < 5)
2804 + fb = 5;
2805 + if (fb > LZMA_MATCH_LEN_MAX)
2806 + fb = LZMA_MATCH_LEN_MAX;
2807 + p->numFastBytes = fb;
2808 + }
2809 + p->lc = props.lc;
2810 + p->lp = props.lp;
2811 + p->pb = props.pb;
2812 + p->fastMode = (props.algo == 0);
2813 + p->matchFinderBase.btMode = props.btMode;
2814 + {
2815 + UInt32 numHashBytes = 4;
2816 + if (props.btMode)
2817 + {
2818 + if (props.numHashBytes < 2)
2819 + numHashBytes = 2;
2820 + else if (props.numHashBytes < 4)
2821 + numHashBytes = props.numHashBytes;
2822 + }
2823 + p->matchFinderBase.numHashBytes = numHashBytes;
2824 + }
2825 +
2826 + p->matchFinderBase.cutValue = props.mc;
2827 +
2828 + p->writeEndMark = props.writeEndMark;
2829 +
2830 + #ifndef _7ZIP_ST
2831 + /*
2832 + if (newMultiThread != _multiThread)
2833 + {
2834 + ReleaseMatchFinder();
2835 + _multiThread = newMultiThread;
2836 + }
2837 + */
2838 + p->multiThread = (props.numThreads > 1);
2839 + #endif
2840 +
2841 + return SZ_OK;
2842 +}
2843 +
2844 +static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
2845 +static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
2846 +static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
2847 +static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
2848 +
2849 +#define IsCharState(s) ((s) < 7)
2850 +
2851 +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
2852 +
2853 +#define kInfinityPrice (1 << 30)
2854 +
2855 +static void RangeEnc_Construct(CRangeEnc *p)
2856 +{
2857 + p->outStream = 0;
2858 + p->bufBase = 0;
2859 +}
2860 +
2861 +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
2862 +
2863 +#define RC_BUF_SIZE (1 << 16)
2864 +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
2865 +{
2866 + if (p->bufBase == 0)
2867 + {
2868 + p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
2869 + if (p->bufBase == 0)
2870 + return 0;
2871 + p->bufLim = p->bufBase + RC_BUF_SIZE;
2872 + }
2873 + return 1;
2874 +}
2875 +
2876 +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
2877 +{
2878 + alloc->Free(alloc, p->bufBase);
2879 + p->bufBase = 0;
2880 +}
2881 +
2882 +static void RangeEnc_Init(CRangeEnc *p)
2883 +{
2884 + /* Stream.Init(); */
2885 + p->low = 0;
2886 + p->range = 0xFFFFFFFF;
2887 + p->cacheSize = 1;
2888 + p->cache = 0;
2889 +
2890 + p->buf = p->bufBase;
2891 +
2892 + p->processed = 0;
2893 + p->res = SZ_OK;
2894 +}
2895 +
2896 +static void RangeEnc_FlushStream(CRangeEnc *p)
2897 +{
2898 + size_t num;
2899 + if (p->res != SZ_OK)
2900 + return;
2901 + num = p->buf - p->bufBase;
2902 + if (num != p->outStream->Write(p->outStream, p->bufBase, num))
2903 + p->res = SZ_ERROR_WRITE;
2904 + p->processed += num;
2905 + p->buf = p->bufBase;
2906 +}
2907 +
2908 +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
2909 +{
2910 + if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
2911 + {
2912 + Byte temp = p->cache;
2913 + do
2914 + {
2915 + Byte *buf = p->buf;
2916 + *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
2917 + p->buf = buf;
2918 + if (buf == p->bufLim)
2919 + RangeEnc_FlushStream(p);
2920 + temp = 0xFF;
2921 + }
2922 + while (--p->cacheSize != 0);
2923 + p->cache = (Byte)((UInt32)p->low >> 24);
2924 + }
2925 + p->cacheSize++;
2926 + p->low = (UInt32)p->low << 8;
2927 +}
2928 +
2929 +static void RangeEnc_FlushData(CRangeEnc *p)
2930 +{
2931 + int i;
2932 + for (i = 0; i < 5; i++)
2933 + RangeEnc_ShiftLow(p);
2934 +}
2935 +
2936 +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
2937 +{
2938 + do
2939 + {
2940 + p->range >>= 1;
2941 + p->low += p->range & (0 - ((value >> --numBits) & 1));
2942 + if (p->range < kTopValue)
2943 + {
2944 + p->range <<= 8;
2945 + RangeEnc_ShiftLow(p);
2946 + }
2947 + }
2948 + while (numBits != 0);
2949 +}
2950 +
2951 +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
2952 +{
2953 + UInt32 ttt = *prob;
2954 + UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
2955 + if (symbol == 0)
2956 + {
2957 + p->range = newBound;
2958 + ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
2959 + }
2960 + else
2961 + {
2962 + p->low += newBound;
2963 + p->range -= newBound;
2964 + ttt -= ttt >> kNumMoveBits;
2965 + }
2966 + *prob = (CLzmaProb)ttt;
2967 + if (p->range < kTopValue)
2968 + {
2969 + p->range <<= 8;
2970 + RangeEnc_ShiftLow(p);
2971 + }
2972 +}
2973 +
2974 +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
2975 +{
2976 + symbol |= 0x100;
2977 + do
2978 + {
2979 + RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
2980 + symbol <<= 1;
2981 + }
2982 + while (symbol < 0x10000);
2983 +}
2984 +
2985 +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
2986 +{
2987 + UInt32 offs = 0x100;
2988 + symbol |= 0x100;
2989 + do
2990 + {
2991 + matchByte <<= 1;
2992 + RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
2993 + symbol <<= 1;
2994 + offs &= ~(matchByte ^ symbol);
2995 + }
2996 + while (symbol < 0x10000);
2997 +}
2998 +
2999 +static void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
3000 +{
3001 + UInt32 i;
3002 + for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
3003 + {
3004 + const int kCyclesBits = kNumBitPriceShiftBits;
3005 + UInt32 w = i;
3006 + UInt32 bitCount = 0;
3007 + int j;
3008 + for (j = 0; j < kCyclesBits; j++)
3009 + {
3010 + w = w * w;
3011 + bitCount <<= 1;
3012 + while (w >= ((UInt32)1 << 16))
3013 + {
3014 + w >>= 1;
3015 + bitCount++;
3016 + }
3017 + }
3018 + ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
3019 + }
3020 +}
3021 +
3022 +
3023 +#define GET_PRICE(prob, symbol) \
3024 + p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3025 +
3026 +#define GET_PRICEa(prob, symbol) \
3027 + ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
3028 +
3029 +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
3030 +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3031 +
3032 +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
3033 +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
3034 +
3035 +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
3036 +{
3037 + UInt32 price = 0;
3038 + symbol |= 0x100;
3039 + do
3040 + {
3041 + price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
3042 + symbol <<= 1;
3043 + }
3044 + while (symbol < 0x10000);
3045 + return price;
3046 +}
3047 +
3048 +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
3049 +{
3050 + UInt32 price = 0;
3051 + UInt32 offs = 0x100;
3052 + symbol |= 0x100;
3053 + do
3054 + {
3055 + matchByte <<= 1;
3056 + price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
3057 + symbol <<= 1;
3058 + offs &= ~(matchByte ^ symbol);
3059 + }
3060 + while (symbol < 0x10000);
3061 + return price;
3062 +}
3063 +
3064 +
3065 +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3066 +{
3067 + UInt32 m = 1;
3068 + int i;
3069 + for (i = numBitLevels; i != 0;)
3070 + {
3071 + UInt32 bit;
3072 + i--;
3073 + bit = (symbol >> i) & 1;
3074 + RangeEnc_EncodeBit(rc, probs + m, bit);
3075 + m = (m << 1) | bit;
3076 + }
3077 +}
3078 +
3079 +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
3080 +{
3081 + UInt32 m = 1;
3082 + int i;
3083 + for (i = 0; i < numBitLevels; i++)
3084 + {
3085 + UInt32 bit = symbol & 1;
3086 + RangeEnc_EncodeBit(rc, probs + m, bit);
3087 + m = (m << 1) | bit;
3088 + symbol >>= 1;
3089 + }
3090 +}
3091 +
3092 +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3093 +{
3094 + UInt32 price = 0;
3095 + symbol |= (1 << numBitLevels);
3096 + while (symbol != 1)
3097 + {
3098 + price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
3099 + symbol >>= 1;
3100 + }
3101 + return price;
3102 +}
3103 +
3104 +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
3105 +{
3106 + UInt32 price = 0;
3107 + UInt32 m = 1;
3108 + int i;
3109 + for (i = numBitLevels; i != 0; i--)
3110 + {
3111 + UInt32 bit = symbol & 1;
3112 + symbol >>= 1;
3113 + price += GET_PRICEa(probs[m], bit);
3114 + m = (m << 1) | bit;
3115 + }
3116 + return price;
3117 +}
3118 +
3119 +
3120 +static void LenEnc_Init(CLenEnc *p)
3121 +{
3122 + unsigned i;
3123 + p->choice = p->choice2 = kProbInitValue;
3124 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
3125 + p->low[i] = kProbInitValue;
3126 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
3127 + p->mid[i] = kProbInitValue;
3128 + for (i = 0; i < kLenNumHighSymbols; i++)
3129 + p->high[i] = kProbInitValue;
3130 +}
3131 +
3132 +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
3133 +{
3134 + if (symbol < kLenNumLowSymbols)
3135 + {
3136 + RangeEnc_EncodeBit(rc, &p->choice, 0);
3137 + RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
3138 + }
3139 + else
3140 + {
3141 + RangeEnc_EncodeBit(rc, &p->choice, 1);
3142 + if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
3143 + {
3144 + RangeEnc_EncodeBit(rc, &p->choice2, 0);
3145 + RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
3146 + }
3147 + else
3148 + {
3149 + RangeEnc_EncodeBit(rc, &p->choice2, 1);
3150 + RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
3151 + }
3152 + }
3153 +}
3154 +
3155 +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
3156 +{
3157 + UInt32 a0 = GET_PRICE_0a(p->choice);
3158 + UInt32 a1 = GET_PRICE_1a(p->choice);
3159 + UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
3160 + UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
3161 + UInt32 i = 0;
3162 + for (i = 0; i < kLenNumLowSymbols; i++)
3163 + {
3164 + if (i >= numSymbols)
3165 + return;
3166 + prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
3167 + }
3168 + for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
3169 + {
3170 + if (i >= numSymbols)
3171 + return;
3172 + prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
3173 + }
3174 + for (; i < numSymbols; i++)
3175 + prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
3176 +}
3177 +
3178 +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
3179 +{
3180 + LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
3181 + p->counters[posState] = p->tableSize;
3182 +}
3183 +
3184 +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
3185 +{
3186 + UInt32 posState;
3187 + for (posState = 0; posState < numPosStates; posState++)
3188 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3189 +}
3190 +
3191 +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
3192 +{
3193 + LenEnc_Encode(&p->p, rc, symbol, posState);
3194 + if (updatePrice)
3195 + if (--p->counters[posState] == 0)
3196 + LenPriceEnc_UpdateTable(p, posState, ProbPrices);
3197 +}
3198 +
3199 +
3200 +
3201 +
3202 +static void MovePos(CLzmaEnc *p, UInt32 num)
3203 +{
3204 + #ifdef SHOW_STAT
3205 + ttt += num;
3206 + printf("\n MovePos %d", num);
3207 + #endif
3208 + if (num != 0)
3209 + {
3210 + p->additionalOffset += num;
3211 + p->matchFinder.Skip(p->matchFinderObj, num);
3212 + }
3213 +}
3214 +
3215 +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
3216 +{
3217 + UInt32 lenRes = 0, numPairs;
3218 + p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
3219 + numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
3220 + #ifdef SHOW_STAT
3221 + printf("\n i = %d numPairs = %d ", ttt, numPairs / 2);
3222 + ttt++;
3223 + {
3224 + UInt32 i;
3225 + for (i = 0; i < numPairs; i += 2)
3226 + printf("%2d %6d | ", p->matches[i], p->matches[i + 1]);
3227 + }
3228 + #endif
3229 + if (numPairs > 0)
3230 + {
3231 + lenRes = p->matches[numPairs - 2];
3232 + if (lenRes == p->numFastBytes)
3233 + {
3234 + const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3235 + UInt32 distance = p->matches[numPairs - 1] + 1;
3236 + UInt32 numAvail = p->numAvail;
3237 + if (numAvail > LZMA_MATCH_LEN_MAX)
3238 + numAvail = LZMA_MATCH_LEN_MAX;
3239 + {
3240 + const Byte *pby2 = pby - distance;
3241 + for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
3242 + }
3243 + }
3244 + }
3245 + p->additionalOffset++;
3246 + *numDistancePairsRes = numPairs;
3247 + return lenRes;
3248 +}
3249 +
3250 +
3251 +#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
3252 +#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
3253 +#define IsShortRep(p) ((p)->backPrev == 0)
3254 +
3255 +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
3256 +{
3257 + return
3258 + GET_PRICE_0(p->isRepG0[state]) +
3259 + GET_PRICE_0(p->isRep0Long[state][posState]);
3260 +}
3261 +
3262 +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
3263 +{
3264 + UInt32 price;
3265 + if (repIndex == 0)
3266 + {
3267 + price = GET_PRICE_0(p->isRepG0[state]);
3268 + price += GET_PRICE_1(p->isRep0Long[state][posState]);
3269 + }
3270 + else
3271 + {
3272 + price = GET_PRICE_1(p->isRepG0[state]);
3273 + if (repIndex == 1)
3274 + price += GET_PRICE_0(p->isRepG1[state]);
3275 + else
3276 + {
3277 + price += GET_PRICE_1(p->isRepG1[state]);
3278 + price += GET_PRICE(p->isRepG2[state], repIndex - 2);
3279 + }
3280 + }
3281 + return price;
3282 +}
3283 +
3284 +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
3285 +{
3286 + return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
3287 + GetPureRepPrice(p, repIndex, state, posState);
3288 +}
3289 +
3290 +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
3291 +{
3292 + UInt32 posMem = p->opt[cur].posPrev;
3293 + UInt32 backMem = p->opt[cur].backPrev;
3294 + p->optimumEndIndex = cur;
3295 + do
3296 + {
3297 + if (p->opt[cur].prev1IsChar)
3298 + {
3299 + MakeAsChar(&p->opt[posMem])
3300 + p->opt[posMem].posPrev = posMem - 1;
3301 + if (p->opt[cur].prev2)
3302 + {
3303 + p->opt[posMem - 1].prev1IsChar = False;
3304 + p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
3305 + p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
3306 + }
3307 + }
3308 + {
3309 + UInt32 posPrev = posMem;
3310 + UInt32 backCur = backMem;
3311 +
3312 + backMem = p->opt[posPrev].backPrev;
3313 + posMem = p->opt[posPrev].posPrev;
3314 +
3315 + p->opt[posPrev].backPrev = backCur;
3316 + p->opt[posPrev].posPrev = cur;
3317 + cur = posPrev;
3318 + }
3319 + }
3320 + while (cur != 0);
3321 + *backRes = p->opt[0].backPrev;
3322 + p->optimumCurrentIndex = p->opt[0].posPrev;
3323 + return p->optimumCurrentIndex;
3324 +}
3325 +
3326 +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
3327 +
3328 +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
3329 +{
3330 + UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur;
3331 + UInt32 matchPrice, repMatchPrice, normalMatchPrice;
3332 + UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
3333 + UInt32 *matches;
3334 + const Byte *data;
3335 + Byte curByte, matchByte;
3336 + if (p->optimumEndIndex != p->optimumCurrentIndex)
3337 + {
3338 + const COptimal *opt = &p->opt[p->optimumCurrentIndex];
3339 + UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
3340 + *backRes = opt->backPrev;
3341 + p->optimumCurrentIndex = opt->posPrev;
3342 + return lenRes;
3343 + }
3344 + p->optimumCurrentIndex = p->optimumEndIndex = 0;
3345 +
3346 + if (p->additionalOffset == 0)
3347 + mainLen = ReadMatchDistances(p, &numPairs);
3348 + else
3349 + {
3350 + mainLen = p->longestMatchLength;
3351 + numPairs = p->numPairs;
3352 + }
3353 +
3354 + numAvail = p->numAvail;
3355 + if (numAvail < 2)
3356 + {
3357 + *backRes = (UInt32)(-1);
3358 + return 1;
3359 + }
3360 + if (numAvail > LZMA_MATCH_LEN_MAX)
3361 + numAvail = LZMA_MATCH_LEN_MAX;
3362 +
3363 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3364 + repMaxIndex = 0;
3365 + for (i = 0; i < LZMA_NUM_REPS; i++)
3366 + {
3367 + UInt32 lenTest;
3368 + const Byte *data2;
3369 + reps[i] = p->reps[i];
3370 + data2 = data - (reps[i] + 1);
3371 + if (data[0] != data2[0] || data[1] != data2[1])
3372 + {
3373 + repLens[i] = 0;
3374 + continue;
3375 + }
3376 + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
3377 + repLens[i] = lenTest;
3378 + if (lenTest > repLens[repMaxIndex])
3379 + repMaxIndex = i;
3380 + }
3381 + if (repLens[repMaxIndex] >= p->numFastBytes)
3382 + {
3383 + UInt32 lenRes;
3384 + *backRes = repMaxIndex;
3385 + lenRes = repLens[repMaxIndex];
3386 + MovePos(p, lenRes - 1);
3387 + return lenRes;
3388 + }
3389 +
3390 + matches = p->matches;
3391 + if (mainLen >= p->numFastBytes)
3392 + {
3393 + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
3394 + MovePos(p, mainLen - 1);
3395 + return mainLen;
3396 + }
3397 + curByte = *data;
3398 + matchByte = *(data - (reps[0] + 1));
3399 +
3400 + if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2)
3401 + {
3402 + *backRes = (UInt32)-1;
3403 + return 1;
3404 + }
3405 +
3406 + p->opt[0].state = (CState)p->state;
3407 +
3408 + posState = (position & p->pbMask);
3409 +
3410 + {
3411 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
3412 + p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
3413 + (!IsCharState(p->state) ?
3414 + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
3415 + LitEnc_GetPrice(probs, curByte, p->ProbPrices));
3416 + }
3417 +
3418 + MakeAsChar(&p->opt[1]);
3419 +
3420 + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
3421 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
3422 +
3423 + if (matchByte == curByte)
3424 + {
3425 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
3426 + if (shortRepPrice < p->opt[1].price)
3427 + {
3428 + p->opt[1].price = shortRepPrice;
3429 + MakeAsShortRep(&p->opt[1]);
3430 + }
3431 + }
3432 + lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
3433 +
3434 + if (lenEnd < 2)
3435 + {
3436 + *backRes = p->opt[1].backPrev;
3437 + return 1;
3438 + }
3439 +
3440 + p->opt[1].posPrev = 0;
3441 + for (i = 0; i < LZMA_NUM_REPS; i++)
3442 + p->opt[0].backs[i] = reps[i];
3443 +
3444 + len = lenEnd;
3445 + do
3446 + p->opt[len--].price = kInfinityPrice;
3447 + while (len >= 2);
3448 +
3449 + for (i = 0; i < LZMA_NUM_REPS; i++)
3450 + {
3451 + UInt32 repLen = repLens[i];
3452 + UInt32 price;
3453 + if (repLen < 2)
3454 + continue;
3455 + price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
3456 + do
3457 + {
3458 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
3459 + COptimal *opt = &p->opt[repLen];
3460 + if (curAndLenPrice < opt->price)
3461 + {
3462 + opt->price = curAndLenPrice;
3463 + opt->posPrev = 0;
3464 + opt->backPrev = i;
3465 + opt->prev1IsChar = False;
3466 + }
3467 + }
3468 + while (--repLen >= 2);
3469 + }
3470 +
3471 + normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
3472 +
3473 + len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
3474 + if (len <= mainLen)
3475 + {
3476 + UInt32 offs = 0;
3477 + while (len > matches[offs])
3478 + offs += 2;
3479 + for (; ; len++)
3480 + {
3481 + COptimal *opt;
3482 + UInt32 distance = matches[offs + 1];
3483 +
3484 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
3485 + UInt32 lenToPosState = GetLenToPosState(len);
3486 + if (distance < kNumFullDistances)
3487 + curAndLenPrice += p->distancesPrices[lenToPosState][distance];
3488 + else
3489 + {
3490 + UInt32 slot;
3491 + GetPosSlot2(distance, slot);
3492 + curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
3493 + }
3494 + opt = &p->opt[len];
3495 + if (curAndLenPrice < opt->price)
3496 + {
3497 + opt->price = curAndLenPrice;
3498 + opt->posPrev = 0;
3499 + opt->backPrev = distance + LZMA_NUM_REPS;
3500 + opt->prev1IsChar = False;
3501 + }
3502 + if (len == matches[offs])
3503 + {
3504 + offs += 2;
3505 + if (offs == numPairs)
3506 + break;
3507 + }
3508 + }
3509 + }
3510 +
3511 + cur = 0;
3512 +
3513 + #ifdef SHOW_STAT2
3514 + if (position >= 0)
3515 + {
3516 + unsigned i;
3517 + printf("\n pos = %4X", position);
3518 + for (i = cur; i <= lenEnd; i++)
3519 + printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
3520 + }
3521 + #endif
3522 +
3523 + for (;;)
3524 + {
3525 + UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen;
3526 + UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice;
3527 + Bool nextIsChar;
3528 + Byte curByte, matchByte;
3529 + const Byte *data;
3530 + COptimal *curOpt;
3531 + COptimal *nextOpt;
3532 +
3533 + cur++;
3534 + if (cur == lenEnd)
3535 + return Backward(p, backRes, cur);
3536 +
3537 + newLen = ReadMatchDistances(p, &numPairs);
3538 + if (newLen >= p->numFastBytes)
3539 + {
3540 + p->numPairs = numPairs;
3541 + p->longestMatchLength = newLen;
3542 + return Backward(p, backRes, cur);
3543 + }
3544 + position++;
3545 + curOpt = &p->opt[cur];
3546 + posPrev = curOpt->posPrev;
3547 + if (curOpt->prev1IsChar)
3548 + {
3549 + posPrev--;
3550 + if (curOpt->prev2)
3551 + {
3552 + state = p->opt[curOpt->posPrev2].state;
3553 + if (curOpt->backPrev2 < LZMA_NUM_REPS)
3554 + state = kRepNextStates[state];
3555 + else
3556 + state = kMatchNextStates[state];
3557 + }
3558 + else
3559 + state = p->opt[posPrev].state;
3560 + state = kLiteralNextStates[state];
3561 + }
3562 + else
3563 + state = p->opt[posPrev].state;
3564 + if (posPrev == cur - 1)
3565 + {
3566 + if (IsShortRep(curOpt))
3567 + state = kShortRepNextStates[state];
3568 + else
3569 + state = kLiteralNextStates[state];
3570 + }
3571 + else
3572 + {
3573 + UInt32 pos;
3574 + const COptimal *prevOpt;
3575 + if (curOpt->prev1IsChar && curOpt->prev2)
3576 + {
3577 + posPrev = curOpt->posPrev2;
3578 + pos = curOpt->backPrev2;
3579 + state = kRepNextStates[state];
3580 + }
3581 + else
3582 + {
3583 + pos = curOpt->backPrev;
3584 + if (pos < LZMA_NUM_REPS)
3585 + state = kRepNextStates[state];
3586 + else
3587 + state = kMatchNextStates[state];
3588 + }
3589 + prevOpt = &p->opt[posPrev];
3590 + if (pos < LZMA_NUM_REPS)
3591 + {
3592 + UInt32 i;
3593 + reps[0] = prevOpt->backs[pos];
3594 + for (i = 1; i <= pos; i++)
3595 + reps[i] = prevOpt->backs[i - 1];
3596 + for (; i < LZMA_NUM_REPS; i++)
3597 + reps[i] = prevOpt->backs[i];
3598 + }
3599 + else
3600 + {
3601 + UInt32 i;
3602 + reps[0] = (pos - LZMA_NUM_REPS);
3603 + for (i = 1; i < LZMA_NUM_REPS; i++)
3604 + reps[i] = prevOpt->backs[i - 1];
3605 + }
3606 + }
3607 + curOpt->state = (CState)state;
3608 +
3609 + curOpt->backs[0] = reps[0];
3610 + curOpt->backs[1] = reps[1];
3611 + curOpt->backs[2] = reps[2];
3612 + curOpt->backs[3] = reps[3];
3613 +
3614 + curPrice = curOpt->price;
3615 + nextIsChar = False;
3616 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3617 + curByte = *data;
3618 + matchByte = *(data - (reps[0] + 1));
3619 +
3620 + posState = (position & p->pbMask);
3621 +
3622 + curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
3623 + {
3624 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
3625 + curAnd1Price +=
3626 + (!IsCharState(state) ?
3627 + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
3628 + LitEnc_GetPrice(probs, curByte, p->ProbPrices));
3629 + }
3630 +
3631 + nextOpt = &p->opt[cur + 1];
3632 +
3633 + if (curAnd1Price < nextOpt->price)
3634 + {
3635 + nextOpt->price = curAnd1Price;
3636 + nextOpt->posPrev = cur;
3637 + MakeAsChar(nextOpt);
3638 + nextIsChar = True;
3639 + }
3640 +
3641 + matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
3642 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
3643 +
3644 + if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
3645 + {
3646 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
3647 + if (shortRepPrice <= nextOpt->price)
3648 + {
3649 + nextOpt->price = shortRepPrice;
3650 + nextOpt->posPrev = cur;
3651 + MakeAsShortRep(nextOpt);
3652 + nextIsChar = True;
3653 + }
3654 + }
3655 + numAvailFull = p->numAvail;
3656 + {
3657 + UInt32 temp = kNumOpts - 1 - cur;
3658 + if (temp < numAvailFull)
3659 + numAvailFull = temp;
3660 + }
3661 +
3662 + if (numAvailFull < 2)
3663 + continue;
3664 + numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
3665 +
3666 + if (!nextIsChar && matchByte != curByte) /* speed optimization */
3667 + {
3668 + /* try Literal + rep0 */
3669 + UInt32 temp;
3670 + UInt32 lenTest2;
3671 + const Byte *data2 = data - (reps[0] + 1);
3672 + UInt32 limit = p->numFastBytes + 1;
3673 + if (limit > numAvailFull)
3674 + limit = numAvailFull;
3675 +
3676 + for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
3677 + lenTest2 = temp - 1;
3678 + if (lenTest2 >= 2)
3679 + {
3680 + UInt32 state2 = kLiteralNextStates[state];
3681 + UInt32 posStateNext = (position + 1) & p->pbMask;
3682 + UInt32 nextRepMatchPrice = curAnd1Price +
3683 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
3684 + GET_PRICE_1(p->isRep[state2]);
3685 + /* for (; lenTest2 >= 2; lenTest2--) */
3686 + {
3687 + UInt32 curAndLenPrice;
3688 + COptimal *opt;
3689 + UInt32 offset = cur + 1 + lenTest2;
3690 + while (lenEnd < offset)
3691 + p->opt[++lenEnd].price = kInfinityPrice;
3692 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
3693 + opt = &p->opt[offset];
3694 + if (curAndLenPrice < opt->price)
3695 + {
3696 + opt->price = curAndLenPrice;
3697 + opt->posPrev = cur + 1;
3698 + opt->backPrev = 0;
3699 + opt->prev1IsChar = True;
3700 + opt->prev2 = False;
3701 + }
3702 + }
3703 + }
3704 + }
3705 +
3706 + startLen = 2; /* speed optimization */
3707 + {
3708 + UInt32 repIndex;
3709 + for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
3710 + {
3711 + UInt32 lenTest;
3712 + UInt32 lenTestTemp;
3713 + UInt32 price;
3714 + const Byte *data2 = data - (reps[repIndex] + 1);
3715 + if (data[0] != data2[0] || data[1] != data2[1])
3716 + continue;
3717 + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
3718 + while (lenEnd < cur + lenTest)
3719 + p->opt[++lenEnd].price = kInfinityPrice;
3720 + lenTestTemp = lenTest;
3721 + price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
3722 + do
3723 + {
3724 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
3725 + COptimal *opt = &p->opt[cur + lenTest];
3726 + if (curAndLenPrice < opt->price)
3727 + {
3728 + opt->price = curAndLenPrice;
3729 + opt->posPrev = cur;
3730 + opt->backPrev = repIndex;
3731 + opt->prev1IsChar = False;
3732 + }
3733 + }
3734 + while (--lenTest >= 2);
3735 + lenTest = lenTestTemp;
3736 +
3737 + if (repIndex == 0)
3738 + startLen = lenTest + 1;
3739 +
3740 + /* if (_maxMode) */
3741 + {
3742 + UInt32 lenTest2 = lenTest + 1;
3743 + UInt32 limit = lenTest2 + p->numFastBytes;
3744 + UInt32 nextRepMatchPrice;
3745 + if (limit > numAvailFull)
3746 + limit = numAvailFull;
3747 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
3748 + lenTest2 -= lenTest + 1;
3749 + if (lenTest2 >= 2)
3750 + {
3751 + UInt32 state2 = kRepNextStates[state];
3752 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
3753 + UInt32 curAndLenCharPrice =
3754 + price + p->repLenEnc.prices[posState][lenTest - 2] +
3755 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
3756 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
3757 + data[lenTest], data2[lenTest], p->ProbPrices);
3758 + state2 = kLiteralNextStates[state2];
3759 + posStateNext = (position + lenTest + 1) & p->pbMask;
3760 + nextRepMatchPrice = curAndLenCharPrice +
3761 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
3762 + GET_PRICE_1(p->isRep[state2]);
3763 +
3764 + /* for (; lenTest2 >= 2; lenTest2--) */
3765 + {
3766 + UInt32 curAndLenPrice;
3767 + COptimal *opt;
3768 + UInt32 offset = cur + lenTest + 1 + lenTest2;
3769 + while (lenEnd < offset)
3770 + p->opt[++lenEnd].price = kInfinityPrice;
3771 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
3772 + opt = &p->opt[offset];
3773 + if (curAndLenPrice < opt->price)
3774 + {
3775 + opt->price = curAndLenPrice;
3776 + opt->posPrev = cur + lenTest + 1;
3777 + opt->backPrev = 0;
3778 + opt->prev1IsChar = True;
3779 + opt->prev2 = True;
3780 + opt->posPrev2 = cur;
3781 + opt->backPrev2 = repIndex;
3782 + }
3783 + }
3784 + }
3785 + }
3786 + }
3787 + }
3788 + /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
3789 + if (newLen > numAvail)
3790 + {
3791 + newLen = numAvail;
3792 + for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2);
3793 + matches[numPairs] = newLen;
3794 + numPairs += 2;
3795 + }
3796 + if (newLen >= startLen)
3797 + {
3798 + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
3799 + UInt32 offs, curBack, posSlot;
3800 + UInt32 lenTest;
3801 + while (lenEnd < cur + newLen)
3802 + p->opt[++lenEnd].price = kInfinityPrice;
3803 +
3804 + offs = 0;
3805 + while (startLen > matches[offs])
3806 + offs += 2;
3807 + curBack = matches[offs + 1];
3808 + GetPosSlot2(curBack, posSlot);
3809 + for (lenTest = /*2*/ startLen; ; lenTest++)
3810 + {
3811 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
3812 + UInt32 lenToPosState = GetLenToPosState(lenTest);
3813 + COptimal *opt;
3814 + if (curBack < kNumFullDistances)
3815 + curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
3816 + else
3817 + curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
3818 +
3819 + opt = &p->opt[cur + lenTest];
3820 + if (curAndLenPrice < opt->price)
3821 + {
3822 + opt->price = curAndLenPrice;
3823 + opt->posPrev = cur;
3824 + opt->backPrev = curBack + LZMA_NUM_REPS;
3825 + opt->prev1IsChar = False;
3826 + }
3827 +
3828 + if (/*_maxMode && */lenTest == matches[offs])
3829 + {
3830 + /* Try Match + Literal + Rep0 */
3831 + const Byte *data2 = data - (curBack + 1);
3832 + UInt32 lenTest2 = lenTest + 1;
3833 + UInt32 limit = lenTest2 + p->numFastBytes;
3834 + UInt32 nextRepMatchPrice;
3835 + if (limit > numAvailFull)
3836 + limit = numAvailFull;
3837 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
3838 + lenTest2 -= lenTest + 1;
3839 + if (lenTest2 >= 2)
3840 + {
3841 + UInt32 state2 = kMatchNextStates[state];
3842 + UInt32 posStateNext = (position + lenTest) & p->pbMask;
3843 + UInt32 curAndLenCharPrice = curAndLenPrice +
3844 + GET_PRICE_0(p->isMatch[state2][posStateNext]) +
3845 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
3846 + data[lenTest], data2[lenTest], p->ProbPrices);
3847 + state2 = kLiteralNextStates[state2];
3848 + posStateNext = (posStateNext + 1) & p->pbMask;
3849 + nextRepMatchPrice = curAndLenCharPrice +
3850 + GET_PRICE_1(p->isMatch[state2][posStateNext]) +
3851 + GET_PRICE_1(p->isRep[state2]);
3852 +
3853 + /* for (; lenTest2 >= 2; lenTest2--) */
3854 + {
3855 + UInt32 offset = cur + lenTest + 1 + lenTest2;
3856 + UInt32 curAndLenPrice;
3857 + COptimal *opt;
3858 + while (lenEnd < offset)
3859 + p->opt[++lenEnd].price = kInfinityPrice;
3860 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
3861 + opt = &p->opt[offset];
3862 + if (curAndLenPrice < opt->price)
3863 + {
3864 + opt->price = curAndLenPrice;
3865 + opt->posPrev = cur + lenTest + 1;
3866 + opt->backPrev = 0;
3867 + opt->prev1IsChar = True;
3868 + opt->prev2 = True;
3869 + opt->posPrev2 = cur;
3870 + opt->backPrev2 = curBack + LZMA_NUM_REPS;
3871 + }
3872 + }
3873 + }
3874 + offs += 2;
3875 + if (offs == numPairs)
3876 + break;
3877 + curBack = matches[offs + 1];
3878 + if (curBack >= kNumFullDistances)
3879 + GetPosSlot2(curBack, posSlot);
3880 + }
3881 + }
3882 + }
3883 + }
3884 +}
3885 +
3886 +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
3887 +
3888 +static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
3889 +{
3890 + UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
3891 + const Byte *data;
3892 + const UInt32 *matches;
3893 +
3894 + if (p->additionalOffset == 0)
3895 + mainLen = ReadMatchDistances(p, &numPairs);
3896 + else
3897 + {
3898 + mainLen = p->longestMatchLength;
3899 + numPairs = p->numPairs;
3900 + }
3901 +
3902 + numAvail = p->numAvail;
3903 + *backRes = (UInt32)-1;
3904 + if (numAvail < 2)
3905 + return 1;
3906 + if (numAvail > LZMA_MATCH_LEN_MAX)
3907 + numAvail = LZMA_MATCH_LEN_MAX;
3908 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3909 +
3910 + repLen = repIndex = 0;
3911 + for (i = 0; i < LZMA_NUM_REPS; i++)
3912 + {
3913 + UInt32 len;
3914 + const Byte *data2 = data - (p->reps[i] + 1);
3915 + if (data[0] != data2[0] || data[1] != data2[1])
3916 + continue;
3917 + for (len = 2; len < numAvail && data[len] == data2[len]; len++);
3918 + if (len >= p->numFastBytes)
3919 + {
3920 + *backRes = i;
3921 + MovePos(p, len - 1);
3922 + return len;
3923 + }
3924 + if (len > repLen)
3925 + {
3926 + repIndex = i;
3927 + repLen = len;
3928 + }
3929 + }
3930 +
3931 + matches = p->matches;
3932 + if (mainLen >= p->numFastBytes)
3933 + {
3934 + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
3935 + MovePos(p, mainLen - 1);
3936 + return mainLen;
3937 + }
3938 +
3939 + mainDist = 0; /* for GCC */
3940 + if (mainLen >= 2)
3941 + {
3942 + mainDist = matches[numPairs - 1];
3943 + while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
3944 + {
3945 + if (!ChangePair(matches[numPairs - 3], mainDist))
3946 + break;
3947 + numPairs -= 2;
3948 + mainLen = matches[numPairs - 2];
3949 + mainDist = matches[numPairs - 1];
3950 + }
3951 + if (mainLen == 2 && mainDist >= 0x80)
3952 + mainLen = 1;
3953 + }
3954 +
3955 + if (repLen >= 2 && (
3956 + (repLen + 1 >= mainLen) ||
3957 + (repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
3958 + (repLen + 3 >= mainLen && mainDist >= (1 << 15))))
3959 + {
3960 + *backRes = repIndex;
3961 + MovePos(p, repLen - 1);
3962 + return repLen;
3963 + }
3964 +
3965 + if (mainLen < 2 || numAvail <= 2)
3966 + return 1;
3967 +
3968 + p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
3969 + if (p->longestMatchLength >= 2)
3970 + {
3971 + UInt32 newDistance = matches[p->numPairs - 1];
3972 + if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
3973 + (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
3974 + (p->longestMatchLength > mainLen + 1) ||
3975 + (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
3976 + return 1;
3977 + }
3978 +
3979 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
3980 + for (i = 0; i < LZMA_NUM_REPS; i++)
3981 + {
3982 + UInt32 len, limit;
3983 + const Byte *data2 = data - (p->reps[i] + 1);
3984 + if (data[0] != data2[0] || data[1] != data2[1])
3985 + continue;
3986 + limit = mainLen - 1;
3987 + for (len = 2; len < limit && data[len] == data2[len]; len++);
3988 + if (len >= limit)
3989 + return 1;
3990 + }
3991 + *backRes = mainDist + LZMA_NUM_REPS;
3992 + MovePos(p, mainLen - 2);
3993 + return mainLen;
3994 +}
3995 +
3996 +static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
3997 +{
3998 + UInt32 len;
3999 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4000 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4001 + p->state = kMatchNextStates[p->state];
4002 + len = LZMA_MATCH_LEN_MIN;
4003 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4004 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
4005 + RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
4006 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
4007 +}
4008 +
4009 +static SRes CheckErrors(CLzmaEnc *p)
4010 +{
4011 + if (p->result != SZ_OK)
4012 + return p->result;
4013 + if (p->rc.res != SZ_OK)
4014 + p->result = SZ_ERROR_WRITE;
4015 + if (p->matchFinderBase.result != SZ_OK)
4016 + p->result = SZ_ERROR_READ;
4017 + if (p->result != SZ_OK)
4018 + p->finished = True;
4019 + return p->result;
4020 +}
4021 +
4022 +static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
4023 +{
4024 + /* ReleaseMFStream(); */
4025 + p->finished = True;
4026 + if (p->writeEndMark)
4027 + WriteEndMarker(p, nowPos & p->pbMask);
4028 + RangeEnc_FlushData(&p->rc);
4029 + RangeEnc_FlushStream(&p->rc);
4030 + return CheckErrors(p);
4031 +}
4032 +
4033 +static void FillAlignPrices(CLzmaEnc *p)
4034 +{
4035 + UInt32 i;
4036 + for (i = 0; i < kAlignTableSize; i++)
4037 + p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
4038 + p->alignPriceCount = 0;
4039 +}
4040 +
4041 +static void FillDistancesPrices(CLzmaEnc *p)
4042 +{
4043 + UInt32 tempPrices[kNumFullDistances];
4044 + UInt32 i, lenToPosState;
4045 + for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
4046 + {
4047 + UInt32 posSlot = GetPosSlot1(i);
4048 + UInt32 footerBits = ((posSlot >> 1) - 1);
4049 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4050 + tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
4051 + }
4052 +
4053 + for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
4054 + {
4055 + UInt32 posSlot;
4056 + const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
4057 + UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
4058 + for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
4059 + posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
4060 + for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
4061 + posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
4062 +
4063 + {
4064 + UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
4065 + UInt32 i;
4066 + for (i = 0; i < kStartPosModelIndex; i++)
4067 + distancesPrices[i] = posSlotPrices[i];
4068 + for (; i < kNumFullDistances; i++)
4069 + distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
4070 + }
4071 + }
4072 + p->matchPriceCount = 0;
4073 +}
4074 +
4075 +static void LzmaEnc_Construct(CLzmaEnc *p)
4076 +{
4077 + RangeEnc_Construct(&p->rc);
4078 + MatchFinder_Construct(&p->matchFinderBase);
4079 + #ifndef _7ZIP_ST
4080 + MatchFinderMt_Construct(&p->matchFinderMt);
4081 + p->matchFinderMt.MatchFinder = &p->matchFinderBase;
4082 + #endif
4083 +
4084 + {
4085 + CLzmaEncProps props;
4086 + LzmaEncProps_Init(&props);
4087 + LzmaEnc_SetProps(p, &props);
4088 + }
4089 +
4090 + #ifndef LZMA_LOG_BSR
4091 + LzmaEnc_FastPosInit(p->g_FastPos);
4092 + #endif
4093 +
4094 + LzmaEnc_InitPriceTables(p->ProbPrices);
4095 + p->litProbs = 0;
4096 + p->saveState.litProbs = 0;
4097 +}
4098 +
4099 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
4100 +{
4101 + void *p;
4102 + p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
4103 + if (p != 0)
4104 + LzmaEnc_Construct((CLzmaEnc *)p);
4105 + return p;
4106 +}
4107 +
4108 +static void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
4109 +{
4110 + alloc->Free(alloc, p->litProbs);
4111 + alloc->Free(alloc, p->saveState.litProbs);
4112 + p->litProbs = 0;
4113 + p->saveState.litProbs = 0;
4114 +}
4115 +
4116 +static void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
4117 +{
4118 + #ifndef _7ZIP_ST
4119 + MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
4120 + #endif
4121 + MatchFinder_Free(&p->matchFinderBase, allocBig);
4122 + LzmaEnc_FreeLits(p, alloc);
4123 + RangeEnc_Free(&p->rc, alloc);
4124 +}
4125 +
4126 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
4127 +{
4128 + LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
4129 + alloc->Free(alloc, p);
4130 +}
4131 +
4132 +static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
4133 +{
4134 + UInt32 nowPos32, startPos32;
4135 + if (p->needInit)
4136 + {
4137 + p->matchFinder.Init(p->matchFinderObj);
4138 + p->needInit = 0;
4139 + }
4140 +
4141 + if (p->finished)
4142 + return p->result;
4143 + RINOK(CheckErrors(p));
4144 +
4145 + nowPos32 = (UInt32)p->nowPos64;
4146 + startPos32 = nowPos32;
4147 +
4148 + if (p->nowPos64 == 0)
4149 + {
4150 + UInt32 numPairs;
4151 + Byte curByte;
4152 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4153 + return Flush(p, nowPos32);
4154 + ReadMatchDistances(p, &numPairs);
4155 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
4156 + p->state = kLiteralNextStates[p->state];
4157 + curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
4158 + LitEnc_Encode(&p->rc, p->litProbs, curByte);
4159 + p->additionalOffset--;
4160 + nowPos32++;
4161 + }
4162 +
4163 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
4164 + for (;;)
4165 + {
4166 + UInt32 pos, len, posState;
4167 +
4168 + if (p->fastMode)
4169 + len = GetOptimumFast(p, &pos);
4170 + else
4171 + len = GetOptimum(p, nowPos32, &pos);
4172 +
4173 + #ifdef SHOW_STAT2
4174 + printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
4175 + #endif
4176 +
4177 + posState = nowPos32 & p->pbMask;
4178 + if (len == 1 && pos == (UInt32)-1)
4179 + {
4180 + Byte curByte;
4181 + CLzmaProb *probs;
4182 + const Byte *data;
4183 +
4184 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
4185 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
4186 + curByte = *data;
4187 + probs = LIT_PROBS(nowPos32, *(data - 1));
4188 + if (IsCharState(p->state))
4189 + LitEnc_Encode(&p->rc, probs, curByte);
4190 + else
4191 + LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
4192 + p->state = kLiteralNextStates[p->state];
4193 + }
4194 + else
4195 + {
4196 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
4197 + if (pos < LZMA_NUM_REPS)
4198 + {
4199 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
4200 + if (pos == 0)
4201 + {
4202 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
4203 + RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
4204 + }
4205 + else
4206 + {
4207 + UInt32 distance = p->reps[pos];
4208 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
4209 + if (pos == 1)
4210 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
4211 + else
4212 + {
4213 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
4214 + RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
4215 + if (pos == 3)
4216 + p->reps[3] = p->reps[2];
4217 + p->reps[2] = p->reps[1];
4218 + }
4219 + p->reps[1] = p->reps[0];
4220 + p->reps[0] = distance;
4221 + }
4222 + if (len == 1)
4223 + p->state = kShortRepNextStates[p->state];
4224 + else
4225 + {
4226 + LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4227 + p->state = kRepNextStates[p->state];
4228 + }
4229 + }
4230 + else
4231 + {
4232 + UInt32 posSlot;
4233 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
4234 + p->state = kMatchNextStates[p->state];
4235 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
4236 + pos -= LZMA_NUM_REPS;
4237 + GetPosSlot(pos, posSlot);
4238 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
4239 +
4240 + if (posSlot >= kStartPosModelIndex)
4241 + {
4242 + UInt32 footerBits = ((posSlot >> 1) - 1);
4243 + UInt32 base = ((2 | (posSlot & 1)) << footerBits);
4244 + UInt32 posReduced = pos - base;
4245 +
4246 + if (posSlot < kEndPosModelIndex)
4247 + RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
4248 + else
4249 + {
4250 + RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
4251 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
4252 + p->alignPriceCount++;
4253 + }
4254 + }
4255 + p->reps[3] = p->reps[2];
4256 + p->reps[2] = p->reps[1];
4257 + p->reps[1] = p->reps[0];
4258 + p->reps[0] = pos;
4259 + p->matchPriceCount++;
4260 + }
4261 + }
4262 + p->additionalOffset -= len;
4263 + nowPos32 += len;
4264 + if (p->additionalOffset == 0)
4265 + {
4266 + UInt32 processed;
4267 + if (!p->fastMode)
4268 + {
4269 + if (p->matchPriceCount >= (1 << 7))
4270 + FillDistancesPrices(p);
4271 + if (p->alignPriceCount >= kAlignTableSize)
4272 + FillAlignPrices(p);
4273 + }
4274 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
4275 + break;
4276 + processed = nowPos32 - startPos32;
4277 + if (useLimits)
4278 + {
4279 + if (processed + kNumOpts + 300 >= maxUnpackSize ||
4280 + RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
4281 + break;
4282 + }
4283 + else if (processed >= (1 << 15))
4284 + {
4285 + p->nowPos64 += nowPos32 - startPos32;
4286 + return CheckErrors(p);
4287 + }
4288 + }
4289 + }
4290 + p->nowPos64 += nowPos32 - startPos32;
4291 + return Flush(p, nowPos32);
4292 +}
4293 +
4294 +#define kBigHashDicLimit ((UInt32)1 << 24)
4295 +
4296 +static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4297 +{
4298 + UInt32 beforeSize = kNumOpts;
4299 + Bool btMode;
4300 + if (!RangeEnc_Alloc(&p->rc, alloc))
4301 + return SZ_ERROR_MEM;
4302 + btMode = (p->matchFinderBase.btMode != 0);
4303 + #ifndef _7ZIP_ST
4304 + p->mtMode = (p->multiThread && !p->fastMode && btMode);
4305 + #endif
4306 +
4307 + {
4308 + unsigned lclp = p->lc + p->lp;
4309 + if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
4310 + {
4311 + LzmaEnc_FreeLits(p, alloc);
4312 + p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
4313 + p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
4314 + if (p->litProbs == 0 || p->saveState.litProbs == 0)
4315 + {
4316 + LzmaEnc_FreeLits(p, alloc);
4317 + return SZ_ERROR_MEM;
4318 + }
4319 + p->lclp = lclp;
4320 + }
4321 + }
4322 +
4323 + p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
4324 +
4325 + if (beforeSize + p->dictSize < keepWindowSize)
4326 + beforeSize = keepWindowSize - p->dictSize;
4327 +
4328 + #ifndef _7ZIP_ST
4329 + if (p->mtMode)
4330 + {
4331 + RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
4332 + p->matchFinderObj = &p->matchFinderMt;
4333 + MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
4334 + }
4335 + else
4336 + #endif
4337 + {
4338 + if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
4339 + return SZ_ERROR_MEM;
4340 + p->matchFinderObj = &p->matchFinderBase;
4341 + MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
4342 + }
4343 + return SZ_OK;
4344 +}
4345 +
4346 +static void LzmaEnc_Init(CLzmaEnc *p)
4347 +{
4348 + UInt32 i;
4349 + p->state = 0;
4350 + for (i = 0 ; i < LZMA_NUM_REPS; i++)
4351 + p->reps[i] = 0;
4352 +
4353 + RangeEnc_Init(&p->rc);
4354 +
4355 +
4356 + for (i = 0; i < kNumStates; i++)
4357 + {
4358 + UInt32 j;
4359 + for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
4360 + {
4361 + p->isMatch[i][j] = kProbInitValue;
4362 + p->isRep0Long[i][j] = kProbInitValue;
4363 + }
4364 + p->isRep[i] = kProbInitValue;
4365 + p->isRepG0[i] = kProbInitValue;
4366 + p->isRepG1[i] = kProbInitValue;
4367 + p->isRepG2[i] = kProbInitValue;
4368 + }
4369 +
4370 + {
4371 + UInt32 num = 0x300 << (p->lp + p->lc);
4372 + for (i = 0; i < num; i++)
4373 + p->litProbs[i] = kProbInitValue;
4374 + }
4375 +
4376 + {
4377 + for (i = 0; i < kNumLenToPosStates; i++)
4378 + {
4379 + CLzmaProb *probs = p->posSlotEncoder[i];
4380 + UInt32 j;
4381 + for (j = 0; j < (1 << kNumPosSlotBits); j++)
4382 + probs[j] = kProbInitValue;
4383 + }
4384 + }
4385 + {
4386 + for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
4387 + p->posEncoders[i] = kProbInitValue;
4388 + }
4389 +
4390 + LenEnc_Init(&p->lenEnc.p);
4391 + LenEnc_Init(&p->repLenEnc.p);
4392 +
4393 + for (i = 0; i < (1 << kNumAlignBits); i++)
4394 + p->posAlignEncoder[i] = kProbInitValue;
4395 +
4396 + p->optimumEndIndex = 0;
4397 + p->optimumCurrentIndex = 0;
4398 + p->additionalOffset = 0;
4399 +
4400 + p->pbMask = (1 << p->pb) - 1;
4401 + p->lpMask = (1 << p->lp) - 1;
4402 +}
4403 +
4404 +static void LzmaEnc_InitPrices(CLzmaEnc *p)
4405 +{
4406 + if (!p->fastMode)
4407 + {
4408 + FillDistancesPrices(p);
4409 + FillAlignPrices(p);
4410 + }
4411 +
4412 + p->lenEnc.tableSize =
4413 + p->repLenEnc.tableSize =
4414 + p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
4415 + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
4416 + LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
4417 +}
4418 +
4419 +static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4420 +{
4421 + UInt32 i;
4422 + for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
4423 + if (p->dictSize <= ((UInt32)1 << i))
4424 + break;
4425 + p->distTableSize = i * 2;
4426 +
4427 + p->finished = False;
4428 + p->result = SZ_OK;
4429 + RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
4430 + LzmaEnc_Init(p);
4431 + LzmaEnc_InitPrices(p);
4432 + p->nowPos64 = 0;
4433 + return SZ_OK;
4434 +}
4435 +
4436 +static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
4437 +{
4438 + p->matchFinderBase.directInput = 1;
4439 + p->matchFinderBase.bufferBase = (Byte *)src;
4440 + p->matchFinderBase.directInputRem = srcLen;
4441 +}
4442 +
4443 +static SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
4444 + UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
4445 +{
4446 + CLzmaEnc *p = (CLzmaEnc *)pp;
4447 + LzmaEnc_SetInputBuf(p, src, srcLen);
4448 + p->needInit = 1;
4449 +
4450 + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
4451 +}
4452 +
4453 +static void LzmaEnc_Finish(CLzmaEncHandle pp)
4454 +{
4455 + #ifndef _7ZIP_ST
4456 + CLzmaEnc *p = (CLzmaEnc *)pp;
4457 + if (p->mtMode)
4458 + MatchFinderMt_ReleaseStream(&p->matchFinderMt);
4459 + #else
4460 + pp = pp;
4461 + #endif
4462 +}
4463 +
4464 +typedef struct
4465 +{
4466 + ISeqOutStream funcTable;
4467 + Byte *data;
4468 + SizeT rem;
4469 + Bool overflow;
4470 +} CSeqOutStreamBuf;
4471 +
4472 +static size_t MyWrite(void *pp, const void *data, size_t size)
4473 +{
4474 + CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
4475 + if (p->rem < size)
4476 + {
4477 + size = p->rem;
4478 + p->overflow = True;
4479 + }
4480 + memcpy(p->data, data, size);
4481 + p->rem -= size;
4482 + p->data += size;
4483 + return size;
4484 +}
4485 +
4486 +static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
4487 +{
4488 + SRes res = SZ_OK;
4489 +
4490 + #ifndef _7ZIP_ST
4491 + Byte allocaDummy[0x300];
4492 + int i = 0;
4493 + for (i = 0; i < 16; i++)
4494 + allocaDummy[i] = (Byte)i;
4495 + #endif
4496 +
4497 + for (;;)
4498 + {
4499 + res = LzmaEnc_CodeOneBlock(p, False, 0, 0);
4500 + if (res != SZ_OK || p->finished != 0)
4501 + break;
4502 + if (progress != 0)
4503 + {
4504 + res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
4505 + if (res != SZ_OK)
4506 + {
4507 + res = SZ_ERROR_PROGRESS;
4508 + break;
4509 + }
4510 + }
4511 + }
4512 + LzmaEnc_Finish(p);
4513 + return res;
4514 +}
4515 +
4516 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
4517 +{
4518 + CLzmaEnc *p = (CLzmaEnc *)pp;
4519 + int i;
4520 + UInt32 dictSize = p->dictSize;
4521 + if (*size < LZMA_PROPS_SIZE)
4522 + return SZ_ERROR_PARAM;
4523 + *size = LZMA_PROPS_SIZE;
4524 + props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
4525 +
4526 + for (i = 11; i <= 30; i++)
4527 + {
4528 + if (dictSize <= ((UInt32)2 << i))
4529 + {
4530 + dictSize = (2 << i);
4531 + break;
4532 + }
4533 + if (dictSize <= ((UInt32)3 << i))
4534 + {
4535 + dictSize = (3 << i);
4536 + break;
4537 + }
4538 + }
4539 +
4540 + for (i = 0; i < 4; i++)
4541 + props[1 + i] = (Byte)(dictSize >> (8 * i));
4542 + return SZ_OK;
4543 +}
4544 +
4545 +SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
4546 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
4547 +{
4548 + SRes res;
4549 + CLzmaEnc *p = (CLzmaEnc *)pp;
4550 +
4551 + CSeqOutStreamBuf outStream;
4552 +
4553 + LzmaEnc_SetInputBuf(p, src, srcLen);
4554 +
4555 + outStream.funcTable.Write = MyWrite;
4556 + outStream.data = dest;
4557 + outStream.rem = *destLen;
4558 + outStream.overflow = False;
4559 +
4560 + p->writeEndMark = writeEndMark;
4561 +
4562 + p->rc.outStream = &outStream.funcTable;
4563 + res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);
4564 + if (res == SZ_OK)
4565 + res = LzmaEnc_Encode2(p, progress);
4566 +
4567 + *destLen -= outStream.rem;
4568 + if (outStream.overflow)
4569 + return SZ_ERROR_OUTPUT_EOF;
4570 + return res;
4571 +}
4572 --- /dev/null
4573 +++ b/lib/lzma/Makefile
4574 @@ -0,0 +1,7 @@
4575 +lzma_compress-objs := LzFind.o LzmaEnc.o
4576 +lzma_decompress-objs := LzmaDec.o
4577 +
4578 +obj-$(CONFIG_LZMA_COMPRESS) += lzma_compress.o
4579 +obj-$(CONFIG_LZMA_DECOMPRESS) += lzma_decompress.o
4580 +
4581 +EXTRA_CFLAGS += -Iinclude/linux -Iinclude/linux/lzma -include types.h