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