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