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