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