1 --- a/Documentation/Configure.help
2 +++ b/Documentation/Configure.help
3 @@ -17592,6 +17592,32 @@ CONFIG_JFFS2_FS_DEBUG
4 If reporting bugs, please try to have available a full dump of the
5 messages at debug level 1 while the misbehaviour was occurring.
7 +ARMLIB compression support for BBC (EXPERIMENTAL)
8 +CONFIG_JFFS2_BBC_ARMLIB
9 + This enables ARMLIB support for BBC.
11 +LZO1X-* compression support for BBC (EXPERIMENTAL)
13 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
14 + compressor, beats ZLIB in everything)
16 +LZARI compression support for BBC (EXPERIMENTAL)
17 +CONFIG_JFFS2_BBC_LZARI
18 + This enables LempelZiv-Storer-Szymanski compression for BBC with
19 + additional arithmetic coding (damn slow, but best compresor).
21 +LZHD compression support for BBC (EXPERIMENTAL)
22 +CONFIG_JFFS2_BBC_LZHD
23 + This enables LempelZiv-Storer-Szymanski compression for BBC with
24 + additional dynamic Huffman coding (a little faster than LZARI, and
25 + it's compression ratio is a little worse than LZARI's)
27 +LZSS compression support for BBC (EXPERIMENTAL)
28 +CONFIG_JFFS2_BBC_LZSS
29 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
30 + (faster than LZHD, and, and has a not-so-good compression ratio,
31 + was included just for testing)
33 JFFS stats available in /proc filesystem
35 Enabling this option will cause statistics from mounted JFFS file systems
38 @@ -50,6 +50,12 @@ dep_tristate 'Journalling Flash File Sys
39 if [ "$CONFIG_JFFS2_FS" = "y" -o "$CONFIG_JFFS2_FS" = "m" ] ; then
40 int 'JFFS2 debugging verbosity (0 = quiet, 2 = noisy)' CONFIG_JFFS2_FS_DEBUG 0
42 +dep_mbool ' ARMLIB compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_ARMLIB $CONFIG_JFFS2_FS
43 +dep_mbool ' LZO1X-* compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZO $CONFIG_JFFS2_FS
44 +dep_mbool ' LZARI compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZARI $CONFIG_JFFS2_FS
45 +dep_mbool ' LZHD compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZHD $CONFIG_JFFS2_FS
46 +dep_mbool ' LZSS compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZSS $CONFIG_JFFS2_FS
48 tristate 'Compressed ROM file system support' CONFIG_CRAMFS
49 tristate 'Squashed file system support' CONFIG_SQUASHFS
50 if [ "$CONFIG_SQUASHFS" = "y" -o "$CONFIG_SQUASHFS" = "m" ] ; then
52 +++ b/fs/jffs2/Config.in.bbc.inc
54 +dep_mbool ' ARMLIB compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_ARMLIB $CONFIG_JFFS2_FS
55 +dep_mbool ' LZO1X-* compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZO $CONFIG_JFFS2_FS
56 +dep_mbool ' LZARI compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZARI $CONFIG_JFFS2_FS
57 +dep_mbool ' LZHD compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZHD $CONFIG_JFFS2_FS
58 +dep_mbool ' LZSS compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZSS $CONFIG_JFFS2_FS
60 +++ b/fs/jffs2/Configure.help.bbc.inc
62 +ARMLIB compression support for BBC (EXPERIMENTAL)
63 +CONFIG_JFFS2_BBC_ARMLIB
64 + This enables ARMLIB support for BBC.
66 +LZO1X-* compression support for BBC (EXPERIMENTAL)
68 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
69 + compressor, beats ZLIB in everything)
71 +LZARI compression support for BBC (EXPERIMENTAL)
72 +CONFIG_JFFS2_BBC_LZARI
73 + This enables LempelZiv-Storer-Szymanski compression for BBC with
74 + additional arithmetic coding (damn slow, but best compresor).
76 +LZHD compression support for BBC (EXPERIMENTAL)
77 +CONFIG_JFFS2_BBC_LZHD
78 + This enables LempelZiv-Storer-Szymanski compression for BBC with
79 + additional dynamic Huffman coding (a little faster than LZARI, and
80 + it's compression ratio is a little worse than LZARI's)
82 +LZSS compression support for BBC (EXPERIMENTAL)
83 +CONFIG_JFFS2_BBC_LZSS
84 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
85 + (faster than LZHD, and, and has a not-so-good compression ratio,
86 + was included just for testing)
88 +++ b/fs/jffs2/Kconfig.bbc.inc
90 +config JFFS2_BBC_ARMLIB
91 + bool "ARMLIB compression support for BBC (EXPERIMENTAL)"
92 + depends on JFFS2_FS && EXPERIMENTAL
95 + This enables ARMLIB support for BBC.
98 + bool "LZO1X-* compression support for BBC (EXPERIMENTAL)"
99 + depends on JFFS2_FS && EXPERIMENTAL
102 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
105 +config JFFS2_BBC_LZARI
106 + bool "LZARI compression support for BBC (EXPERIMENTAL)"
107 + depends on JFFS2_FS && EXPERIMENTAL
110 + This enables LempelZiv-Storer-Szymanski compression for BBC with
111 + additional arithmetic coding (damn slow, but best compresor).
113 +config JFFS2_BBC_LZHD
114 + bool "LZHD compression support for BBC (EXPERIMENTAL)"
115 + depends on JFFS2_FS && EXPERIMENTAL
118 + This enables LempelZiv-Storer-Szymanski compression for BBC with
119 + additional dynamic Huffman coding (a little faster than LZARI, and
120 + it's compression ratio is a little worse than LZARI's)
122 +config JFFS2_BBC_LZSS
123 + bool "LZSS compression support for BBC (EXPERIMENTAL)"
124 + depends on JFFS2_FS && EXPERIMENTAL
127 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
128 + (faster than LZHD, and, and has a not-so-good compression ratio,
129 + was included just for testing)
130 --- a/fs/jffs2/Makefile
131 +++ b/fs/jffs2/Makefile
133 # Note 2! The CFLAGS definitions are now in the main makefile...
136 +JFFS2_BBC_KERNEL_OBJS-y = jffs2_bbc_framework.o jffs2_bbc_fs.o
138 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_ARMLIB) += jffs2_bbc_armlib_comp.o
139 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZO) += jffs2_bbc_lzo_comp.o
140 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZSS) += jffs2_bbc_lzss_comp.o
141 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZARI) += jffs2_bbc_lzari_comp.o
142 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZHD) += jffs2_bbc_lzhd_comp.o
144 +JFFS2_BBC_KERNEL_OBJS := $(JFFS2_BBC_KERNEL_OBJS-y)
146 +JFFS2_BBC_MKFS_OBJS = jffs2_bbc_mkfs.o jffs2_bbc_framework.o jffs2_bbc_armlib_comp.o jffs2_bbc_lzo_comp.o\
147 + jffs2_bbc_lzss_comp.o jffs2_bbc_lzari_comp.o jffs2_bbc_lzhd_comp.o
149 COMPR_OBJS := compr.o compr_rubin.o compr_rtime.o pushpull.o \
151 JFFS2_OBJS := dir.o file.o ioctl.o nodelist.o malloc.o \
152 + $(JFFS2_BBC_KERNEL_OBJS) \
153 read.o nodemgmt.o readinode.o super.o write.o scan.o gc.o \
154 symlink.o build.o erase.o background.o
157 +++ b/fs/jffs2/Makefile.bbc.inc
159 +JFFS2_BBC_KERNEL_OBJS-y = jffs2_bbc_framework.o jffs2_bbc_fs.o
161 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_ARMLIB) += jffs2_bbc_armlib_comp.o
162 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZO) += jffs2_bbc_lzo_comp.o
163 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZSS) += jffs2_bbc_lzss_comp.o
164 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZARI) += jffs2_bbc_lzari_comp.o
165 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZHD) += jffs2_bbc_lzhd_comp.o
167 +JFFS2_BBC_KERNEL_OBJS := $(JFFS2_BBC_KERNEL_OBJS-y)
169 +JFFS2_BBC_MKFS_OBJS = jffs2_bbc_mkfs.o jffs2_bbc_framework.o jffs2_bbc_armlib_comp.o jffs2_bbc_lzo_comp.o\
170 + jffs2_bbc_lzss_comp.o jffs2_bbc_lzari_comp.o jffs2_bbc_lzhd_comp.o
171 --- a/fs/jffs2/compr_zlib.c
172 +++ b/fs/jffs2/compr_zlib.c
173 @@ -85,7 +85,7 @@ void jffs2_zlib_exit(void)
174 vfree(inflate_workspace);
177 -int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
178 +int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out,
179 __u32 *sourcelen, __u32 *dstlen)
182 @@ -145,7 +145,7 @@ int zlib_compress(unsigned char *data_in
186 -void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
187 +void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out,
188 __u32 srclen, __u32 destlen)
191 @@ -175,3 +175,19 @@ void zlib_decompress(unsigned char *data
192 zlib_inflateEnd(&strm);
196 +extern int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 * sourcelen, __u32 * dstlen);
197 +extern void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
199 +int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
200 + __u32 *sourcelen, __u32 *dstlen)
202 + return jffs2_zlib_compress(data_in,cpage_out,sourcelen,dstlen);
205 +void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
206 + __u32 srclen, __u32 destlen)
208 + jffs2_zlib_decompress(data_in,cpage_out,srclen,destlen);
211 --- a/fs/jffs2/file.c
212 +++ b/fs/jffs2/file.c
217 +#include "jffs2_bbc_framework.h" /**BBC**/
218 #include <linux/kernel.h>
219 #include <linux/mtd/compatmac.h> /* for min() */
220 #include <linux/slab.h>
221 @@ -459,6 +460,7 @@ int jffs2_commit_write (struct file *fil
223 comprbuf = kmalloc(cdatalen, GFP_KERNEL);
225 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
226 comprtype = jffs2_compress(page_address(pg)+ (file_ofs & (PAGE_CACHE_SIZE-1)), comprbuf, &datalen, &cdatalen);
228 if (comprtype == JFFS2_COMPR_NONE) {
235 +#include "jffs2_bbc_framework.h" /**BBC**/
236 #include <linux/kernel.h>
237 #include <linux/mtd/mtd.h>
238 #include <linux/slab.h>
239 @@ -651,6 +652,7 @@ static int jffs2_garbage_collect_dnode(s
240 writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
243 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
244 comprtype = jffs2_compress(writebuf, comprbuf, &datalen, &cdatalen);
248 +++ b/fs/jffs2/hpatch
251 +# A patch-like utility
252 +# Designed for patching different version of jffs2 with the same hpatch file
254 +# Copyright (C) 2004, Ferenc Havasi
256 +# This program is free software; you can redistribute it and/or
257 +# modify it under the terms of the GNU General Public License
258 +# as published by the Free Software Foundation; either version 2
259 +# of the License, or (at your option) any later version.
261 +# This program is distributed in the hope that it will be useful,
262 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
263 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
264 +# GNU General Public License for more details.
266 +# You should have received a copy of the GNU General Public License
267 +# along with this program; if not, write to the Free Software
268 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
270 +$filename_tmp1="file1.tmp";
271 +$filename_tmp2="file2.tmp";
274 +$filename_out=$filename_tmp1;
281 +# 3: expecting F (first state)
287 + if (($mode!=2)&&($modified==1)) {
293 + if ($cmd_name ne "") { $rules{"$cmd_name"}=1; }
294 + $filename_result=$filename_out;
295 + if ($filename_result ne $filename_in_save) {
296 + open(RES,"<$filename_result") or die "Cannot open $filename_result.\n";
297 + open(DST,">$filename_in_save") or die "Cannot open $filename_in_save.\n";
304 + unlink($filename_tmp1) && unlink($filename_tmp2);
309 + $filename_result=$filename_in;
310 + if ($filename_result ne $filename_in_save) {
311 + open(RES,"<$filename_result") or die "Cannot open $filename_result.\n";
312 + open(DST,">$filename_in_save") or die "Cannot open $filename_in_save.\n";
319 + unlink($filename_tmp1);
322 + foreach $rulename (keys %rules) {
323 + if ($rules{"$rulename"}==0) { print(STDERR "On $filename_in_save error applying rule $rulename.\n"); }
329 + print ("usage: hpatch hpatch_file\n");
333 +open(CMD,"<$ARGV[0]") or die "Cannot open $ARGV[0].\n";
336 +while (chomp($cmd_line=<CMD>)) {
338 + if ($cmd_line eq "") {next;}
339 + #$cmd_line =~ s/\#.*//;
340 + $cmd_line =~ s/\ *$//;
341 + if ($cmd_line eq "") {next;}
342 + if ($cmd_line =~ /^F(.*)/) {
343 + $tmp_filename_in=$1;
347 + $filename_in=$tmp_filename_in;
348 + $filename_in_save=$filename_in;
349 + open(SRC,"<$filename_in") or die "Cannot open $filename_in.\n";
350 + open(DST,">$filename_out") or die "Cannot open $filename_out.\n";;
355 + if ($mode==3) {die "error: F expression expected in line $cmd_linenum\n";}
356 + if ($cmd_line =~ /^=(.*)/) {
358 + if (($mode!=2)&&($modified==1)) {
364 + if (($cmd_name ne "")) {$rules{"$cmd_name"}=1;};
366 + if ($filename_cnt%2==1) {
367 + $filename_in=$filename_tmp1;
368 + $filename_out=$filename_tmp2;
371 + $filename_in=$filename_tmp2;
372 + $filename_out=$filename_tmp1;
380 + $cmd_name=$tmp_cmd_name;
381 + if (($cmd_name ne "")) {
382 + if ($rules{"$cmd_name"}==1) {
386 + $rules{"$cmd_name"}=0;
389 + open(SRC,"<$filename_in") or die "Cannot open $filename_in.\n";
390 + open(DST,">$filename_out") or die "Cannot open $filename_out.\n";
394 + if ($mode == 0) {die "error: = expression expected in line $cmd_linenum\n";}
395 + if ($mode == 2) {next;}
396 + if ($cmd_line =~ /^!(.*)/) {
401 + if ($cmd_line =~ /^\?(.*)/) {
406 + if (index($_,$search_str)>=0) {$found=1; last;}
408 + if ($found==0) { $mode=2; }
411 + if ($cmd_line =~ /^\+(.*)/) {
416 + if ($cmd_line =~ /^\-(.*)/) {
420 + if (index($_,$search_str)>=0) {$saved_line=$_; $found=1; $modified=1; last;}
423 + if ($found==0) { $mode=2; }
426 + if ($cmd_line =~ /^i(.*)/) {
428 + open(INCSRC,"<$filename_inc") or die "Cannot open $filename_inc.\n";
434 + if ($cmd_line =~ /^I/) {
435 + print DST $saved_line;
442 +++ b/fs/jffs2/jffs2_bbc_armlib_comp.c
445 + * JFFS2-BBC: armlib compressor plugin
447 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
449 + * Copyright (C) 2004, Ferenc Havasi & Tamas Gergely
451 + * This program is free software; you can redistribute it and/or
452 + * modify it under the terms of the GNU General Public License
453 + * as published by the Free Software Foundation; either version 2
454 + * of the License, or (at your option) any later version.
456 + * This program is distributed in the hope that it will be useful,
457 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
458 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
459 + * GNU General Public License for more details.
461 + * You should have received a copy of the GNU General Public License
462 + * along with this program; if not, write to the Free Software
463 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
467 +#include "jffs2_bbc_framework.h"
470 +#include <linux/string.h>
475 +//ORIGIN: include/DataStructures/TypeDefs.h
477 +/*******************************************************************************
479 +* AUTHOR: Tamás Gergely
480 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
481 +*******************************************************************************/
495 +#define u8 unsigned char
498 +#define u16 unsigned short
501 +#define u32 unsigned long
504 +#define s8 signed char
507 +#define s16 signed short
510 +#define s32 signed long
521 +#define VECTOR_P_END(vct) ((void*)(((char*)((vct)->ptr)) + (vct)->size))
522 +#define VECTOR_S_END(vct) ((void*)(((char*)((vct).ptr)) + (vct).size))
524 +static void vector_clear(vector *);
525 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
526 +static void vector_reset(vector *);
527 +static void vector_clr_ptr(vector *);
528 +static void vector_add_u8(vector *, u8);
529 +static void vector_add_u16(vector *, u16);
530 +static void vector_add_u32(vector *, u32);
531 +static void vector_add_s8(vector *, s8);
532 +static void vector_add_s16(vector *, s16);
533 +static void vector_add_s32(vector *, s32);
534 +static void vector_add_ptr(vector *, void *);
535 +static void vector_concat(vector *, vector *);
540 +//ORIGIN: include/DataStructures/DataTypes.h
542 +/*******************************************************************************
544 +* AUTHOR: Tamás Gergely
545 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
546 +*******************************************************************************/
551 +//#include "DataStructures/TypeDefs.h"
553 +typedef u16 THUMB_DataType;
554 +typedef u32 ARM_DataType;
555 +typedef u8 TokenType;
556 +typedef u8 PredictorType;
557 +typedef u8 *ProbDist;
559 +typedef vector RawData;
560 +typedef vector RawBlocks;
561 +typedef vector TokenStream;
562 +typedef vector TokenBlocks;
563 +typedef vector LatType;
565 +#define THUMB_DATA_LENGTH 16
566 +#define ARM_DATA_LENGTH 32
567 +#define TOKEN_LENGTH 8
568 +#define TOKEN_MAXVALUE 0xff
569 +#define PREDICTOR_LENGTH 8
570 +#define PREDICTOR_MAXVALUE 0xff
574 +//ORIGIN: include/DataStructures/BitVector.h
576 +/*******************************************************************************
578 +* AUTHOR: Tamás Gergely
579 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
580 +*******************************************************************************/
585 +//#include "DataStructures/TypeDefs.h"
587 +typedef vector BitBlocks;
600 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
601 +static void bitblocks_clear(BitBlocks *);
602 +static void bitvector_clear(BitVector *);
603 +static void bitvector_W_reset(BitVector *);
604 +static void bitvector_W_add0(BitVector *);
605 +static void bitvector_W_add1(BitVector *);
606 +static void bitvector_W_concat_b(BitVector *, BitVector *);
607 +static void bitvector_W_concat_v(BitVector *, vector *);
608 +static void bitvector_W_flush(BitVector *);
609 +static void bitvector_R_reset(BitVector *);
610 +static u8 bitvector_R_get1(BitVector *);
611 +static u8 bitvector_R_get8(BitVector *);
614 +#define BITVECTOR_P_END(bv) ((void*)(((bv)->base)+((bv)->size)))
615 +#define BITVECTOR_S_END(bv) ((void*)( ((bv).base)+ ((bv).size)))
616 +#define BITVECTOR_SKIP(bv,num) ((bv)->ptr) += (num)
620 +//ORIGIN: include/DataStructures/DecisionTree.h
622 +/*******************************************************************************
623 +* FILE: DecisionTree.h
624 +* AUTHOR: Tamás Gergely
625 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
626 +*******************************************************************************/
628 +#ifndef DECISIONTREE_H
629 +#define DECISIONTREE_H
631 +//#include "DataStructures/DataTypes.h"
635 +#define TREENODETYPE_NULLNODE 0
636 +#define TREENODETYPE_NODE_BINARY_EQ 1
637 +#define TREENODETYPE_LEAF_P 2
638 +#define TREENODETYPE_LEAF_C 3
639 +#define TREENODETYPE_NODE_BINARY_LT 5
640 +#define TREENODETYPE_IS_NODE(n) (((n) == TREENODETYPE_NODE_BINARY_EQ) || \
641 + ((n) == TREENODETYPE_NODE_BINARY_LT))
642 +#define TREENODETYPE_IS_NODE_BINARY(n) (((n) == TREENODETYPE_NODE_BINARY_EQ) || \
643 + ((n) == TREENODETYPE_NODE_BINARY_LT))
645 +#define TREENODETYPE_IS_LEAF(n) (((n) == TREENODETYPE_LEAF_P) || \
646 + ((n) == TREENODETYPE_LEAF_C))
649 +#define TREE_SUBTREE_RELATION_LEFT_EQ !=
650 +#define TREE_SUBTREE_RELATION_RIGHT_EQ ==
651 +#define TREE_SUBTREE_RELATION_LEFT_LT <
652 +#define TREE_SUBTREE_RELATION_RIGHT_LT >=
654 +#define GET_NODE_PTR_TYPE(n) (((TreeNodeDummy*)(n))->type)
663 + u8 type; // [TREENODETYPE_NODE_BINARY]
665 + PredictorType value;
672 + u8 type; // [TREENODETYPE_LEAF_P]
674 + PredictorType *probabilities;
679 + u8 type; // [TREENODETYPE_LEAF_C]
680 + PredictorType predicted_class;
694 + u16 number_of_classes;
695 + u16 number_of_predictors;
696 + PredictorType *predictor_max_values;
699 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
700 +static void decisiontree_delete(DecisionTree *);
701 +static void decisiontree_get_probability_for_token(void *, PredictorType *, TokenType, ProbabilityType *);
702 +static TokenType decisiontree_get_token_for_range(void *, PredictorType *, u32, u32, ProbabilityType *);
707 +//ORIGIN: include/DataStructures/PredictorTable.h
709 +/*******************************************************************************
710 +* FILE: PredictorTable.h
711 +* AUTHOR: Tamás Gergely
712 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
713 +*******************************************************************************/
715 +#ifndef PREDICTORTABLE_H
716 +#define PREDICTORTABLE_H
718 +//#include "DataStructures/TypeDefs.h"
719 +//#include "DataStructures/DataTypes.h"
720 +////#include "DataStructures/Filter.h"
721 +////#include "DataStructures/Converter.h"
722 +////#include "DataStructures/Manipulator.h"
724 +#define NUMBER_OF_PREDICTORS_ARM 17
727 +#define NUMBER_OF_PREDICTORS_TXT 2
730 +#endif // __KERNEL__
733 +#define NUMBER_OF_PREDICTORS NUMBER_OF_PREDICTORS_TXT
734 +#define predictortable_reset predictortable_resetTXT
735 +#define predictortable_update predictortable_updateTXT
736 +#define predictortable_minvalue predictortable_minvalueTXT
737 +#define predictortable_maxvalue predictortable_maxvalueTXT
739 +#define NUMBER_OF_PREDICTORS NUMBER_OF_PREDICTORS_ARM
740 +#define predictortable_reset predictortable_resetARM
741 +#define predictortable_update predictortable_updateARM
742 +#define predictortable_minvalue predictortable_minvalueARM
743 +#define predictortable_maxvalue predictortable_maxvalueARM
751 + PredictorType *predictors;
754 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
755 +static void predictortable_clear(PredictorTable *);
756 +static void predictortable_free(PredictorTable *);
757 +static void predictortable_resetARM(PredictorTable *);
758 +static void predictortable_updateARM(PredictorTable *, TokenType);
759 +static PredictorType predictortable_minvalueARM(PredictorTable *, u32);
760 +static PredictorType predictortable_maxvalueARM(PredictorTable *, u32);
765 +static void predictortable_resetTXT(PredictorTable *);
766 +static void predictortable_updateTXT(PredictorTable *, TokenType);
767 +static PredictorType predictortable_minvalueTXT(PredictorTable *, u32);
768 +static PredictorType predictortable_maxvalueTXT(PredictorTable *, u32);
770 +#endif // __KERNEL__
774 +//ORIGIN: include/DataStructures/ipack_model.h
776 +/*******************************************************************************
777 +* FILE: ipack_model.h
778 +* AUTHOR: Tamás Gergely
779 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
780 +*******************************************************************************/
782 +#ifndef IPACK_MODEL_H
783 +#define IPACK_MODEL_H
785 +//#include "DataStructures/DataTypes.h"
786 +//#include "DataStructures/DecisionTree.h"
787 +//#include "DataStructures/PredictorTable.h"
789 +#define PROBABILITY_SHIFT 12
790 +#define PROBABILITY_MAX 0x00001000l
792 +#define NUMBER_OF_TOKENS_ARM 16
793 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION_ARM 8
796 +#define NUMBER_OF_TOKENS_TXT 256
797 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION_TXT 4
800 +#endif // __KERNEL__
803 +#define NUMBER_OF_TOKENS NUMBER_OF_TOKENS_TXT
804 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION NUMBER_OF_TOKENS_PER_INSTRUCTION_TXT
806 +#define NUMBER_OF_TOKENS NUMBER_OF_TOKENS_ARM
807 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION NUMBER_OF_TOKENS_PER_INSTRUCTION_ARM
813 + Data structure of an internal node of the tree
817 + PredictorType *attribute_ptr;
818 + u32 value; // PredictorType
819 + void *right_child_ptr;
820 +} ipack_treenodeBin;
822 + Data structure of a leaf with probabilities
826 + u16 probabilities[0]; // PredictorType[0]
829 + Data structure of a leaf with class prediction
833 + PredictorType predicted_class; // PredictorType
836 + Possible data structures of a tree node
840 + ipack_treenodeBin nodeBin;
841 + ipack_treeleafP leafP;
842 + ipack_treeleafC leafC;
850 + ipack_node_data data; // ipack_node_data
858 + u16 probabilities[NUMBER_OF_TOKENS];
861 + Model for ipack project
865 + char ID[4]; // char[4]
866 + char block_sign[4]; // only the first 2 are used!
867 + void *tree_root_ptr; // void*
868 + void *tree_code; // generated ARM code
869 + PredictorType *predictors_ptr; // PredictorType*
870 + ipack_nullnode nullnode;
877 +} ipack_probability_type;
880 +static void ipack_model_get_probability_for_token(ipack_nodetype *, TokenType, ipack_probability_type *);
881 +static TokenType ipack_model_get_token_for_range(ipack_nodetype *, u32, u32, ipack_probability_type *);
882 +/*void ipack_model_predictortable_reset (PredictorType*);
883 +void ipack_model_predictortable_update (PredictorType*, TokenType);*/
886 +/*static void ipack_model_printinfo(ipack_model_type *);
887 +static void ipack_dumpmodel(void *);*/
892 +//ORIGIN: include/Builders/PredictorGenerator.h
894 +/*******************************************************************************
895 +* FILE: PredictorGenerator.h
896 +* AUTHOR: Tamás Gergely
897 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
898 +*******************************************************************************/
900 +#ifndef PREDICTORGENERATOR_H
901 +#define PREDICTORGENERATOR_H
903 +//#include "DataStructures.h"
905 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
906 +static PredictorTable *predictorgenerator_generate(void);
911 +//ORIGIN: include/Builders/Coder.h
913 +/*******************************************************************************
915 +* AUTHOR: Tamás Gergely
916 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
917 +*******************************************************************************/
922 +#define CODER_VALUEBITS 16
923 +#define CODER_VALUEMAX 0x00010000l
924 +#define CODER_VALUE3RD 0x0000c000l
925 +#define CODER_VALUEHLF 0x00008000l
926 +#define CODER_VALUE1ST 0x00004000l
930 +//ORIGIN: DataStructures/src/TypeDefs.c
932 +/*******************************************************************************
934 +* AUTHOR: Tamás Gergely
935 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
936 +*******************************************************************************/
938 +//#include "ipack_common.h"
939 +//#include "DataStructures/TypeDefs.h"
944 +#define VECTOR_ALLOC_SIZE 0x00001000
946 +static void vector_clear(vector * vct)
949 + jffs2_bbc_free(vct->ptr);
955 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
956 +static void vector_extend(vector * vct)
959 + vct->capacity += vct->alloc_size;
960 + tmp = jffs2_bbc_malloc(vct->capacity);
962 + memcpy(tmp, vct->ptr, vct->size);
963 + jffs2_bbc_free(vct->ptr);
968 +static void vector_reset(vector * vct)
972 + vct->alloc_size = VECTOR_ALLOC_SIZE;
976 +static void vector_clr_ptr(vector * vct)
980 + for (it = vct->ptr, end_it = (((char *) (vct->ptr)) + vct->size); it != end_it; it++) {
982 + jffs2_bbc_free(*it);
985 + jffs2_bbc_free(vct->ptr);
991 +static void vector_add_u8(vector * vct, u8 val)
993 + if ((vct->size) + sizeof(u8) > (vct->capacity)) {
994 + vector_extend(vct);
996 + *(u8 *) ((char *) (vct->ptr) + (vct->size)) = val;
997 + vct->size += sizeof(u8);
1000 +static void vector_add_u16(vector * vct, u16 val)
1002 + if ((vct->size) + sizeof(u16) > (vct->capacity)) {
1003 + vector_extend(vct);
1005 + *(u16 *) ((char *) (vct->ptr) + (vct->size)) = val;
1006 + vct->size += sizeof(u16);
1009 +static void vector_add_u32(vector * vct, u32 val)
1011 + if ((vct->size) + sizeof(u32) > (vct->capacity)) {
1012 + vector_extend(vct);
1014 + *(u32 *) ((char *) (vct->ptr) + (vct->size)) = val;
1015 + vct->size += sizeof(u32);
1018 +static void vector_add_s8(vector * vct, s8 val)
1020 + if ((vct->size) + sizeof(s8) > (vct->capacity)) {
1021 + vector_extend(vct);
1023 + *(s8 *) ((char *) (vct->ptr) + (vct->size)) = val;
1024 + vct->size += sizeof(s8);
1027 +static void vector_add_s16(vector * vct, s16 val)
1029 + if ((vct->size) + sizeof(s16) > (vct->capacity)) {
1030 + vector_extend(vct);
1032 + *(s16 *) ((char *) (vct->ptr) + (vct->size)) = val;
1033 + vct->size += sizeof(s16);
1036 +static void vector_add_s32(vector * vct, s32 val)
1038 + if ((vct->size) + sizeof(s32) > (vct->capacity)) {
1039 + vector_extend(vct);
1041 + *(s32 *) ((char *) (vct->ptr) + (vct->size)) = val;
1042 + vct->size += sizeof(s32);
1045 +static void vector_add_ptr(vector * vct, void *ptr)
1047 + if ((vct->size) + sizeof(void *) > (vct->capacity)) {
1048 + vector_extend(vct);
1050 + *(void **) ((char *) (vct->ptr) + (vct->size)) = ptr;
1051 + vct->size += sizeof(void *);
1054 +static void vector_concat(vector * lhs, vector * rhs)
1057 + if (!(rhs->size)) {
1061 + lhs->capacity = (lhs->size) + (rhs->size);
1062 + lhs->ptr = jffs2_bbc_malloc(lhs->capacity);
1064 + memcpy(lhs->ptr, tmp, lhs->size);
1065 + jffs2_bbc_free(tmp);
1067 + memcpy((((u8 *) lhs->ptr) + lhs->size), rhs->ptr, rhs->size);
1068 + lhs->size += rhs->size;
1073 +//ORIGIN: DataStructures/src/BitVector.c
1075 +/*******************************************************************************
1076 +* FILE: BitVector.c
1077 +* AUTHOR: Tamás Gergely
1078 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1079 +*******************************************************************************/
1081 +//#include "ipack_common.h"
1082 +//#include "DataStructures/BitVector.h"
1084 +#include <memory.h>
1087 +#define VECTOR_ALLOC_SIZE 0x00001000
1089 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1091 +static void bitblocks_clear(BitBlocks * this)
1095 + for (it = this->ptr, end_it = VECTOR_P_END(this); it != end_it; it++) {
1096 + bitvector_clear(*it);
1097 + jffs2_bbc_free(*it);
1099 + jffs2_bbc_free(this->ptr);
1103 +static void bitvector_clear(BitVector * this)
1106 + jffs2_bbc_free(this->base);
1108 + this->freebits = 0;
1109 + this->capacity = 0;
1115 +static void bitvector_W_reset(BitVector * this)
1117 + this->freebits = 0;
1118 + this->capacity = 0;
1124 +static void bitvector_W_add0(BitVector * this)
1126 + if (!(this->freebits)) {
1127 + if (this->size == this->capacity) {
1128 + void *tmp = this->base;
1129 + this->capacity += VECTOR_ALLOC_SIZE;
1130 + this->base = jffs2_bbc_malloc(this->capacity);
1131 + this->ptr = ((u8 *) (this->base)) + this->size;
1132 + memcpy(this->base, tmp, this->size);
1133 + jffs2_bbc_free(tmp);
1139 + this->freebits = 7;
1140 + *(this->ptr) = 0x00;
1144 + (*(this->ptr)) <<= 1;
1148 +static void bitvector_W_add1(BitVector * this)
1150 + if (!(this->freebits)) {
1151 + if (this->size == this->capacity) {
1152 + void *tmp = this->base;
1153 + this->capacity += VECTOR_ALLOC_SIZE;
1154 + this->base = jffs2_bbc_malloc(this->capacity);
1155 + this->ptr = ((u8 *) (this->base)) + this->size;
1156 + memcpy(this->base, tmp, this->size);
1157 + jffs2_bbc_free(tmp);
1163 + this->freebits = 7;
1164 + *(this->ptr) = 0x01;
1168 + (*(this->ptr)) <<= 1;
1169 + (*(this->ptr)) |= 0x01;
1173 +static void bitvector_W_concat_b(BitVector * lhs, BitVector * rhs)
1176 + if (!(rhs->size)) {
1180 + lhs->capacity = ((((lhs->size) + (rhs->size) - 1) / VECTOR_ALLOC_SIZE) + 1) * VECTOR_ALLOC_SIZE;
1181 + lhs->base = jffs2_bbc_malloc(lhs->capacity);
1183 + memcpy(lhs->base, tmp, lhs->size);
1184 + jffs2_bbc_free(tmp);
1186 + memcpy((((u8 *) (lhs->base)) + lhs->size), rhs->base, rhs->size);
1187 + lhs->freebits = 0;
1188 + lhs->size += rhs->size;
1189 + lhs->ptr = ((u8 *) (lhs->base)) + lhs->size;
1192 +static void bitvector_W_concat_v(BitVector * lhs, vector * rhs)
1195 + if (!(rhs->size)) {
1199 + lhs->capacity = ((((lhs->size) + (rhs->size) - 1) / VECTOR_ALLOC_SIZE) + 1) * VECTOR_ALLOC_SIZE;
1200 + lhs->base = jffs2_bbc_malloc(lhs->capacity);
1202 + memcpy(lhs->base, tmp, lhs->size);
1203 + jffs2_bbc_free(tmp);
1205 + memcpy((((u8 *) (lhs->base)) + lhs->size), rhs->ptr, rhs->size);
1206 + lhs->freebits = 0;
1207 + lhs->size += rhs->size;
1208 + lhs->ptr = ((u8 *) (lhs->base)) + lhs->size;
1211 +static void bitvector_W_flush(BitVector * this)
1213 + (*(this->ptr)) <<= this->freebits;
1214 + this->freebits = 0;
1217 +static void bitvector_R_reset(BitVector * this)
1219 + this->freebits = 7;
1220 + this->ptr = this->base;
1223 +static u8 bitvector_R_get1(BitVector * this)
1225 + u8 tmp = ((*(this->ptr)) >> this->freebits) & 0x01;
1226 + if (!(this->freebits)) {
1227 + this->freebits = 7;
1236 +static u8 bitvector_R_get8(BitVector * this)
1238 + u8 tmp = (*(this->ptr));
1245 +//ORIGIN: DataStructures/src/DecisionTree.c
1247 +/*******************************************************************************
1248 +* FILE: DecisionTree.c
1249 +* AUTHOR: Tamás Gergely
1250 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1251 +*******************************************************************************/
1253 +//#include "ipack_common.h"
1254 +//#include "DataStructures/DecisionTree.h"
1256 +static void decisiontree_delete_node(void *root)
1258 + u8 tmp = GET_NODE_PTR_TYPE(root);
1259 + if (TREENODETYPE_IS_NODE_BINARY(tmp)) {
1260 + decisiontree_delete_node(((TreeNodeBinary *) root)->left);
1261 + decisiontree_delete_node(((TreeNodeBinary *) root)->right);
1263 + else if ((tmp) == TREENODETYPE_LEAF_P) {
1264 + if (((TreeLeafP *) root)->probabilities) {
1265 + jffs2_bbc_free(((TreeLeafP *) root)->probabilities);
1268 + else if ((tmp) == TREENODETYPE_LEAF_C) {
1270 + jffs2_bbc_free(root);
1273 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1275 +static void decisiontree_delete(DecisionTree * dt)
1277 + decisiontree_delete_node(dt->root);
1278 + jffs2_bbc_free(dt->predictor_max_values);
1281 +static void decisiontree_get_probability_for_token(void *root, PredictorType * preds, TokenType token, ProbabilityType * prob)
1284 + while (TREENODETYPE_IS_NODE(((TreeNodeBinary *) tmp)->type)) {
1285 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_EQ) {
1286 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_EQ((TreeNodeBinary *) tmp)->value) {
1287 + tmp = ((TreeNodeBinary *) tmp)->left;
1290 + tmp = ((TreeNodeBinary *) tmp)->right;
1293 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_LT) {
1294 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_LT((TreeNodeBinary *) tmp)->value) {
1295 + tmp = ((TreeNodeBinary *) tmp)->left;
1298 + tmp = ((TreeNodeBinary *) tmp)->right;
1305 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_P) {
1307 + u32 lngth = ((TreeLeafP *) tmp)->pairs << 1;
1308 + for (i = 0; i < lngth;) {
1309 + TokenType at = ((TreeLeafP *) tmp)->probabilities[i++];
1310 + TokenType av = ((TreeLeafP *) tmp)->probabilities[i++];
1318 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_C) {
1319 + if (((TreeLeafC *) tmp)->predicted_class == token) {
1320 + prob->high = TOKEN_MAXVALUE;
1321 + prob->max = TOKEN_MAXVALUE;
1326 +static TokenType decisiontree_get_token_for_range(void *root, PredictorType * preds, u32 value, u32 range, ProbabilityType * prob)
1329 + TokenType token = 0;
1330 + while (TREENODETYPE_IS_NODE(((TreeNodeBinary *) tmp)->type)) {
1331 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_EQ) {
1332 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_EQ((TreeNodeBinary *) tmp)->value) {
1333 + tmp = ((TreeNodeBinary *) tmp)->left;
1336 + tmp = ((TreeNodeBinary *) tmp)->right;
1339 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_LT) {
1340 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_LT((TreeNodeBinary *) tmp)->value) {
1341 + tmp = ((TreeNodeBinary *) tmp)->left;
1344 + tmp = ((TreeNodeBinary *) tmp)->right;
1351 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_P) {
1356 + u32 lngth = ((TreeLeafP *) tmp)->pairs << 1;
1357 + for (i = 0; i < lngth;) {
1359 + prob->max += ((TreeLeafP *) tmp)->probabilities[i++];
1361 + norm = (value * prob->max - 1) / range;
1362 + for (i = 0; prob->high <= norm;) {
1363 + at = ((TreeLeafP *) tmp)->probabilities[i++];
1364 + av = ((TreeLeafP *) tmp)->probabilities[i++];
1366 + if (prob->high <= norm)
1371 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_C) {
1372 + token = ((TreeLeafC *) tmp)->predicted_class;
1373 + prob->high = TOKEN_MAXVALUE;
1374 + prob->max = TOKEN_MAXVALUE;
1380 +//ORIGIN: DataStructures/src/PredictorTable.c
1382 +/*******************************************************************************
1383 +* FILE: PredictorTable.c
1384 +* AUTHOR: Tamás Gergely
1385 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1386 +*******************************************************************************/
1388 +//#include "ipack_common.h"
1389 +//#include "DataStructures/PredictorTable.h"
1391 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1393 +static void predictortable_clear(PredictorTable * table)
1395 + table->predictors = 0;
1398 +static void predictortable_free(PredictorTable * table)
1400 + if (table->predictors) {
1401 + jffs2_bbc_free(table->predictors);
1402 + table->predictors = 0;
1406 +static void predictortable_resetARM(PredictorTable * table)
1408 + register PredictorType *ptr = table->predictors;
1409 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS_ARM;
1410 + while (ptr < end) {
1415 +static void predictortable_updateARM(PredictorTable * table, TokenType token)
1417 + register PredictorType *ptr = table->predictors;
1418 + register u32 ndx = ptr[0] + 1;
1419 + ptr[ndx + 8] = ptr[ndx];
1429 +static PredictorType predictortable_minvalueARM(PredictorTable * table, u32 index)
1434 +static PredictorType predictortable_maxvalueARM(PredictorTable * table, u32 index)
1448 +/*static void predictortable_resetTXT(PredictorTable * table)
1450 + register PredictorType *ptr = table->predictors;
1451 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS_TXT;
1452 + while (ptr < end) {
1457 +static void predictortable_updateTXT(PredictorTable * table, TokenType token)
1459 + register PredictorType *ptr = table->predictors;
1460 +// register u32 ndx;
1462 + if ((('a' <= token) && (token <= 'z')) || (('A' <= token) && (token <= 'Z'))) {
1470 +static PredictorType predictortable_minvalueTXT(PredictorTable * table, u32 index)
1475 +static PredictorType predictortable_maxvalueTXT(PredictorTable * table, u32 index)
1480 +#endif // __KERNEL__
1482 +//ORIGIN: DataStructures/src/ipack_model.c
1484 +/*******************************************************************************
1485 +* FILE: ipack_model.c
1486 +* AUTHOR: Tamás Gergely
1487 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1488 +*******************************************************************************/
1490 +//#include "DataStructures/ipack_model.h"
1491 +//#include "measuredef.h"
1492 +//#include "ipack_common.h"
1494 +#ifdef __MEASURE_TIME_MODEL_GETPROB
1495 +#define __MT_P_MAX 256
1496 +#define __MT_P_DIV 128
1497 +#define __MT_P_MIN 0
1500 +static void ipack_model_get_probability_for_token(ipack_nodetype * tmp, TokenType token, ipack_probability_type * prob)
1502 +// register ipack_nodetype* tmp = model->tree_root_ptr;
1503 +// register ipack_nodetype* tmp = root;
1504 + while (TREENODETYPE_IS_NODE(tmp->type)) {
1505 + if (tmp->type == TREENODETYPE_NODE_BINARY_EQ) {
1506 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_EQ tmp->data.nodeBin.value) {
1507 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1510 + tmp = tmp->data.nodeBin.right_child_ptr;
1513 + else if (tmp->type == TREENODETYPE_NODE_BINARY_LT) {
1514 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_LT tmp->data.nodeBin.value) {
1515 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1518 + tmp = tmp->data.nodeBin.right_child_ptr;
1525 + if (tmp->type == TREENODETYPE_LEAF_P) {
1527 + prob->low = tmp->data.leafP.probabilities[token - 1];
1529 + prob->high = tmp->data.leafP.probabilities[token];
1530 +// prob->max = tmp->data.leafP.probabilities[15];
1532 + else if (tmp->type == TREENODETYPE_LEAF_C) {
1533 + if (tmp->data.leafC.predicted_class == token) {
1534 + prob->high = TOKEN_MAXVALUE;
1535 +// prob->max = TOKEN_MAXVALUE;
1540 +#ifndef IPACK_ARM_ASM
1542 +//return ipack_model_get_token_for_range2(tmp,value,range,prob);
1544 +static TokenType ipack_model_get_token_for_range(ipack_nodetype * tmp, u32 value, u32 range, ipack_probability_type * prob)
1546 +// register ipack_nodetype* tmp = model->tree_root_ptr;
1547 +// register ipack_nodetype* tmp = root;
1548 + register TokenType token = 0;
1549 + while (TREENODETYPE_IS_NODE(tmp->type)) {
1550 + if (tmp->type == TREENODETYPE_NODE_BINARY_EQ) {
1551 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_EQ tmp->data.nodeBin.value) {
1552 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1555 + tmp = tmp->data.nodeBin.right_child_ptr;
1558 + else if (tmp->type == TREENODETYPE_NODE_BINARY_LT) {
1559 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_LT tmp->data.nodeBin.value) {
1560 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1563 + tmp = tmp->data.nodeBin.right_child_ptr;
1570 + if (tmp->type == TREENODETYPE_LEAF_P) {
1573 +// prob->max = tmp->data.leafP.probabilities[15];
1574 +/* norm = (value * prob->max -1)/range;
1575 + for(i = 0; i < 15; ++i) {
1576 + if(tmp->data.leafP.probabilities[i] > norm) {
1580 + norm = ((value << PROBABILITY_SHIFT) - 1);
1581 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1582 + if (range * tmp->data.leafP.probabilities[i] > norm) {
1586 + token = (TokenType) i;
1587 + prob->high = tmp->data.leafP.probabilities[i];
1589 + prob->low = tmp->data.leafP.probabilities[token - 1];
1592 + else if (tmp->type == TREENODETYPE_LEAF_C) {
1593 + token = tmp->data.leafC.predicted_class;
1594 + prob->high = PROBABILITY_MAX;
1595 +// prob->max = PROBABILITY_MAX;
1601 +void ipack_model_predictortable_reset(PredictorType* ptr)
1603 +// register PredictorType* ptr = model->predictors_ptr;
1604 +// register PredictorType* ptr = preds;
1605 + register PredictorType* end = ptr + NUMBER_OF_PREDICTORS;
1606 + while(ptr < end) {
1611 +void ipack_model_predictortable_update(PredictorType* ptr, TokenType token)
1613 +// register PredictorType* ptr = model->predictors_ptr;
1614 +// register PredictorType* ptr = preds;
1615 + register u32 ndx = ptr[0] + 1;
1616 + ptr[ndx + 8] = ptr[ndx];
1624 +/****************************************************************************/
1627 +static void ipack_model_countpreds(void *ptr, ipack_nodetype * node, double *table, double val)
1629 + if ((node->type == TREENODETYPE_NODE_BINARY_EQ) || (node->type == TREENODETYPE_NODE_BINARY_LT)) {
1630 + table[(u32) (node->data.nodeBin.attribute_ptr) - (u32) (ptr)] += val;
1631 + ipack_model_countpreds(ptr, (void *) (((u8 *) (node)) + sizeof(node->type) + sizeof(ipack_treenodeBin)), table, val / 2);
1632 + ipack_model_countpreds(ptr, node->data.nodeBin.right_child_ptr, table, val / 2);
1638 +/*static void ipack_model_printinfo(ipack_model_type * model)
1640 + double *prcnt = jffs2_bbc_malloc(sizeof(double) * NUMBER_OF_PREDICTORS);
1642 + for (i = 0; i < NUMBER_OF_PREDICTORS; i++) {
1645 + ipack_model_countpreds(model->predictors_ptr, model->tree_root_ptr, prcnt, 100);
1646 + for (i = 0; i < NUMBER_OF_PREDICTORS; i++) {
1647 + jffs2_bbc_print3(" p[%3d] = %10.6lf\n", (int) i, prcnt[i]);
1649 + jffs2_bbc_free(prcnt);
1652 +static void ipack_dumpnode(unsigned char **ptr, FILE * file, char *prefs)
1654 + switch (*((*ptr)++)) {
1659 + case TREENODETYPE_NODE_BINARY_EQ:
1662 + fprintf(file, "%s+->\tBinary node: P[%u] equals %u\n", prefs, (unsigned int)x, (unsigned int)y);
1663 + for (j = 0; j < 4096 && prefs[j]; ++j);
1666 + ipack_dumpnode(ptr, file, prefs);
1668 + ipack_dumpnode(ptr, file, prefs);
1671 + case TREENODETYPE_NODE_BINARY_LT:
1674 + fprintf(file, "%s+->\tBinary node: P[%u] greater than %u\n", prefs, (unsigned int)x, (unsigned int)y);
1675 + for (j = 0; j < 4096 && prefs[j]; ++j);
1678 + ipack_dumpnode(ptr, file, prefs);
1680 + ipack_dumpnode(ptr, file, prefs);
1683 + case TREENODETYPE_LEAF_P:
1685 + fprintf(file, "%s+->\tLeaf: %u pairs\n", prefs, (unsigned int)x);
1686 + (*ptr) += (x << 1);
1688 + case TREENODETYPE_LEAF_C:
1690 + fprintf(file, "%s+->\tLeaf: class %u\n", prefs, (unsigned int)x);
1693 + fprintf(file, "%s+->\tLeaf: nullnode\n", prefs);
1697 +/*static void ipack_dumpmodel(void *model)
1699 + unsigned char *tmp_ptr = model;
1702 + if ((file = fopen("DUMPED_MODEL", "wa"))) {
1704 + for (i = 0; i < 4096; C[i++] = 0);
1706 + tmp_ptr += sizeof(u32);
1707 + ipack_dumpnode(&tmp_ptr, file, C);
1714 +//ORIGIN: Builders/src/PredictorGenerator.c
1716 +/*******************************************************************************
1717 +* FILE: PredictorGenerator.c
1718 +* AUTHOR: Tamás Gergely
1719 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1720 +*******************************************************************************/
1722 +//#include "ipack_common.h"
1723 +//#include "Builders/PredictorGenerator.h"
1725 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1726 +static PredictorTable *predictorgenerator_generate( /*PredictorGeneratorSettings* settings */ )
1728 + PredictorTable *ptr = jffs2_bbc_malloc(sizeof(PredictorTable));
1729 + predictortable_clear(ptr);
1730 + ptr->predictors = jffs2_bbc_malloc(NUMBER_OF_PREDICTORS * sizeof(PredictorType));
1735 +//ORIGIN: Builders/src/ipack_armlib_compressor.c
1737 +/*******************************************************************************
1738 +* FILE: ipack_armlim_compressor.c
1739 +* AUTHOR: Tamás Gergely
1740 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1741 +*******************************************************************************/
1743 +//#include "ipack_common.h"
1744 +//#include "DataStructures.h"
1745 +//#include "Builders/PredictorGenerator.h"
1746 +//#include "Builders/Tokenizer.h"
1747 +//#include "Builders/Coder.h"
1749 +#define EC_NO_ERROR 0
1750 +#define EC_NOT_IPMF_FILE -1
1751 +#define EC_NOT_IPMF_MODEL -2
1752 +#define EC_NOT_HG_BLOCK -3
1753 +#define EC_WRONG_INPUT_LENGTH -501
1754 +#define EC_CODER_WRONG_PROBABILITY 1
1755 +#define EC_CODER_WRONG_RANGE 2
1756 +#define EC_BUFFER_OVERFLOW 501
1757 +#define EC_BUFFER_UNDERFLOW 502
1758 +#define EC_UNKNOWN_TOKEN_TYPE 1001
1759 +#define EC_UNKNOWN_FILTER 1002
1760 +#define EC_UNKNOWN_CONVERTER 1003
1761 +#define EC_UNKNOWN_MANIPULATOR 1004
1763 +/*******************************************************************************
1765 + COMPRESSOR INIT FUNCTIONS
1767 +*******************************************************************************/
1769 +#define ROUND_UP_TO_DWORD(val) ( ( (val) + 3 ) & 0xfffffffc )
1772 +int ipack_glb_endian_X;
1775 +static int ipack_compressor_init_tree(unsigned char **ptr, ipack_model_type * model, ipack_nodetype * node, void *nullnode)
1778 + node->type = *((*ptr)++);
1779 + switch (node->type) {
1786 + case TREENODETYPE_NODE_BINARY_EQ:
1787 + case TREENODETYPE_NODE_BINARY_LT:
1788 + node->data.nodeBin.attribute_ptr = (model->predictors_ptr) + (*((*ptr)++));
1789 + node->data.nodeBin.value = *((*ptr)++);
1790 + retval = sizeof(node->data.nodeBin);
1791 + retval += ipack_compressor_init_tree(ptr, model, (void *) ROUND_UP_TO_DWORD(((u32) node) + sizeof(node->type) + sizeof(node->data.nodeBin)), nullnode);
1792 + node->data.nodeBin.right_child_ptr = (void *) ROUND_UP_TO_DWORD(((u32) node) + retval + sizeof(node->type));
1793 + retval += ipack_compressor_init_tree(ptr, model, node->data.nodeBin.right_child_ptr, nullnode);
1795 + case TREENODETYPE_LEAF_P:
1796 + lngth = *((*ptr)++);
1798 + for (i = 0, j = 0; i < lngth; ++i) {
1801 + node->data.leafP.probabilities[j++] = av;
1803 + av += *((*ptr)++);
1805 + while (j < NUMBER_OF_TOKENS) {
1806 + node->data.leafP.probabilities[j++] = av;
1808 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1809 + node->data.leafP.probabilities[i] = ((node->data.leafP.probabilities[i] << PROBABILITY_SHIFT) / node->data.leafP.probabilities[NUMBER_OF_TOKENS - 1]);
1811 + retval = ROUND_UP_TO_DWORD(NUMBER_OF_TOKENS * sizeof(u16));
1813 + case TREENODETYPE_LEAF_C:
1814 + node->data.leafC.predicted_class = *((*ptr)++);
1815 + retval = sizeof(node->data.leafC);
1816 + retval = ROUND_UP_TO_DWORD(retval);
1821 + return retval + sizeof(node->type);
1824 +#define IPACK_TREE_CONVERT_REPLACE 0
1825 +#define IPACK_TREE_CONVERT_KEEP 1
1827 +static void *ipack_tree_to_code(ipack_model_type * model, int *code_size);
1829 +static int ipack_armlib_convert_tree_to_code(ipack_model_type * model_img, int mode)
1831 +#ifdef IPACK_TREE_TO_CODE
1834 + model_img->tree_code = ipack_tree_to_code(model_img, &tree_size);
1835 + jffs2_bbc_print2("Convertation done. Code size=%d\n", tree_size);
1836 + if (mode == IPACK_TREE_CONVERT_REPLACE) {
1837 + jffs2_bbc_print1("Freeing original tree.\n");
1838 + jffs2_bbc_free(model_img->tree_root_ptr);
1839 + model_img->tree_root_ptr = NULL;
1846 +static int ipack_armlib_compressor_init(void **model)
1848 + int retval = EC_NO_ERROR;
1849 + unsigned char *tmp_ptr = *model;
1851 + ipack_model_type *model_img;
1854 + if (*(tmp_ptr++) != 'i') {
1855 + return EC_NOT_IPMF_FILE;
1857 + else if (*(tmp_ptr++) != 'P') {
1858 + return EC_NOT_IPMF_FILE;
1860 + else if (*(tmp_ptr++) != 'M') {
1861 + return EC_NOT_IPMF_FILE;
1863 + else if (*(tmp_ptr++) != 'F') {
1864 + return EC_NOT_IPMF_FILE;
1866 + tmp_c[0] = *(tmp_ptr++);
1867 + tmp_c[1] = *(tmp_ptr++);
1870 + //model_img = jffs2_bbc_malloc(*((u32*)tmp_ptr));
1871 + model_img = jffs2_bbc_malloc(sizeof(ipack_model_type) + ROUND_UP_TO_DWORD(NUMBER_OF_PREDICTORS));
1872 + model_img->tree_root_ptr = jffs2_bbc_malloc(*((u32 *) tmp_ptr)); //it is smaller a little but, but...
1874 + tmp_ptr += sizeof(u32);
1876 + model_img->ID[0] = 'i';
1877 + model_img->ID[1] = 'P';
1878 + model_img->ID[2] = 'M';
1879 + model_img->ID[3] = 'F';
1881 + model_img->block_sign[0] = tmp_c[0];
1882 + model_img->block_sign[1] = tmp_c[1];
1884 + model_img->nullnode.type = TREENODETYPE_LEAF_P;
1885 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1886 + model_img->nullnode.probabilities[i] = 0;
1888 + model_img->predictors_ptr = (void *) (((u32) model_img) + sizeof(ipack_model_type));
1889 + //model_img->tree_root_ptr = (void*)ROUND_UP_TO_DWORD(((u32)(model_img->predictors_ptr)) + NUMBER_OF_PREDICTORS);//ALIGN
1891 + ipack_compressor_init_tree(&tmp_ptr, model_img, model_img->tree_root_ptr, &(model_img->nullnode));
1893 +#ifdef IPACK_TREE_TO_CODE
1894 +#ifdef IPACK_AUTO_TREE_TO_CODE
1895 + jffs2_bbc_print1("Automatically converting tree to ARM code...\n");
1896 + ipack_armlib_convert_tree_to_code(model_img, IPACK_TREE_CONVERT_REPLACE);
1898 + model_img->tree_code = NULL;
1901 + model_img->tree_code = NULL;
1904 + jffs2_bbc_free(*model);
1905 + *model = model_img;
1909 +/*******************************************************************************
1911 + COMPRESSOR DEINIT FUNCTIONS
1913 +*******************************************************************************/
1916 +/* Descructor of compressor (model will be freed with jffs2_bbc_free() after it)*/
1917 +static void ipack_armlib_compressor_deinit(void)
1921 +/*******************************************************************************
1923 + COMPRESS FUNCTIONS
1925 +*******************************************************************************/
1927 +static int writebits0(unsigned char **dest, u8 * freebits, u32 * opposite, unsigned char *end)
1929 + if (!(*freebits)) {
1938 + if ((*dest == end) && !(*freebits)) {
1939 + return EC_BUFFER_OVERFLOW;
1941 + while (*opposite) {
1943 + if (!(*freebits)) {
1953 + if ((*dest == end) && !(*freebits)) {
1954 + return EC_BUFFER_OVERFLOW;
1960 +static int writebits1(unsigned char **dest, u8 * freebits, u32 * opposite, unsigned char *end)
1962 + if (!(*freebits)) {
1972 + if ((*dest == end) && !(*freebits)) {
1973 + return EC_BUFFER_OVERFLOW;
1975 + while (*opposite) {
1977 + if (!(*freebits)) {
1986 + if ((*dest == end) && !(*freebits)) {
1987 + return EC_BUFFER_OVERFLOW;
1997 + * *dstlen bytes are allocated.
1998 + * if it is not enough write *sourcelen over to the processed amount of data
1999 + * returns non zero if fails
2001 +static int ipack_armlib_compress(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen)
2003 + register u32 coder_high = CODER_VALUEMAX - 1;
2004 + register u32 coder_low = 0;
2005 + u32 coder_opbits = 0;
2006 + u8 bitvector_freebits = 8;
2007 + unsigned char *bitvector_ptr = output;
2008 + unsigned char *bitvector_end = output + (*dstlen - 1);
2009 + ARM_DataType *tmpp;
2014 + ipack_nodetype *treeroot = ((ipack_model_type *) model)->tree_root_ptr;
2015 + PredictorType *predctrs = ((ipack_model_type *) model)->predictors_ptr;
2017 +#ifdef IPACK_TREE_TO_CODE
2018 + void (*treefunc) (ipack_nodetype *, TokenType, ipack_probability_type *);
2020 + treefunc = ((ipack_model_type *) model)->tree_code;
2021 + if (treefunc != NULL)
2025 + if ((*sourcelen % 4) != 0) {
2026 + return EC_WRONG_INPUT_LENGTH;
2028 + if (*dstlen <= 4) {
2029 + return EC_BUFFER_OVERFLOW;
2032 + if (((ipack_model_type *) model)->ID[0] != 'i') {
2033 + return EC_NOT_IPMF_MODEL;
2035 + else if (((ipack_model_type *) model)->ID[1] != 'P') {
2036 + return EC_NOT_IPMF_MODEL;
2038 + else if (((ipack_model_type *) model)->ID[2] != 'M') {
2039 + return EC_NOT_IPMF_MODEL;
2041 + else if (((ipack_model_type *) model)->ID[3] != 'F') {
2042 + return EC_NOT_IPMF_MODEL;
2045 + tmpv.capacity = (*sourcelen);
2047 + tmpv.capacity = (*sourcelen) << 1;
2049 + tmpv.size = tmpv.capacity;
2050 + tmpv.ptr = jffs2_bbc_malloc(tmpv.size);
2054 + if (ipack_glb_endian_X) {
2055 + for (tmpp = (void *) input; (u32) tmpp < (u32) (input + *sourcelen); ++tmpp) {
2057 + *(it++) = (u8) ((*tmpp & 0xff000000) >> 24);
2058 + *(it++) = (u8) ((*tmpp & 0x00ff0000) >> 16);
2059 + *(it++) = (u8) ((*tmpp & 0x0000ff00) >> 8);
2060 + *(it++) = (u8) ((*tmpp & 0x000000ff));
2062 + *(it++) = (u8) ((*tmpp & 0x0000f000) >> 12);
2063 + *(it++) = (u8) ((*tmpp & 0x0000000f));
2064 + *(it++) = (u8) ((*tmpp & 0xf0000000) >> 28);
2065 + *(it++) = (u8) ((*tmpp & 0x000f0000) >> 16);
2066 + *(it++) = (u8) ((*tmpp & 0x00000f00) >> 8);
2067 + *(it++) = (u8) ((*tmpp & 0x00f00000) >> 20);
2068 + *(it++) = (u8) ((*tmpp & 0x0f000000) >> 24);
2069 + *(it++) = (u8) ((*tmpp & 0x000000f0) >> 4);
2070 +#endif //TXT_TOKENS
2075 + for (tmpp = (void *) input; (u32) tmpp < (u32) (input + *sourcelen); ++tmpp) {
2077 + *(it++) = (u8) ((*tmpp & 0x000000ff));
2078 + *(it++) = (u8) ((*tmpp & 0x0000ff00) >> 8);
2079 + *(it++) = (u8) ((*tmpp & 0x00ff0000) >> 16);
2080 + *(it++) = (u8) ((*tmpp & 0xff000000) >> 24);
2082 + *(it++) = (u8) ((*tmpp & 0x00f00000) >> 20);
2083 + *(it++) = (u8) ((*tmpp & 0x0f000000) >> 24);
2084 + *(it++) = (u8) ((*tmpp & 0x000000f0) >> 4);
2085 + *(it++) = (u8) ((*tmpp & 0x00000f00) >> 8);
2086 + *(it++) = (u8) ((*tmpp & 0x000f0000) >> 16);
2087 + *(it++) = (u8) ((*tmpp & 0x0000f000) >> 12);
2088 + *(it++) = (u8) ((*tmpp & 0x0000000f));
2089 + *(it++) = (u8) ((*tmpp & 0xf0000000) >> 28);
2090 +#endif //TXT_TOKENS
2098 + { //predictor reset
2099 + register PredictorType *ptr = predctrs;
2100 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS;
2101 + while (ptr < end) {
2106 + //*(bitvector_ptr++) = 'H';
2107 + //*(bitvector_ptr++) = 'G';
2108 + *(bitvector_ptr++) = ((ipack_model_type *) model)->block_sign[0];
2109 + *(bitvector_ptr++) = ((ipack_model_type *) model)->block_sign[1];
2111 + *(bitvector_ptr++) = (unsigned char) (((*sourcelen) >> 8) & 0xff);
2112 + *(bitvector_ptr++) = (unsigned char) ((*sourcelen) & 0xff);
2113 + for (it = tmpv.ptr, end_it = VECTOR_S_END(tmpv); it != end_it; ++it) {
2114 + ipack_probability_type prob;
2117 +#ifdef IPACK_TREE_TO_CODE
2118 + if (treefunc != NULL)
2119 + (*treefunc) (treeroot, *it, &prob);
2121 + ipack_model_get_probability_for_token(treeroot, *it, &prob);
2123 + ipack_model_get_probability_for_token(treeroot, *it, &prob);
2126 + if (prob.high == prob.low) {
2127 + vector_clear(&tmpv);
2128 + return EC_CODER_WRONG_PROBABILITY;
2130 + range = coder_high - coder_low + 1;
2131 + coder_high = coder_low + ((range * prob.high) >> PROBABILITY_SHIFT) - 1;
2132 + coder_low += ((range * prob.low) >> PROBABILITY_SHIFT);
2134 + if (coder_high < CODER_VALUEHLF) {
2135 + if (writebits0(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2136 + vector_clear(&tmpv);
2137 + return EC_BUFFER_OVERFLOW;
2140 + else if (coder_low >= CODER_VALUEHLF) {
2141 + if (writebits1(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2142 + vector_clear(&tmpv);
2143 + return EC_BUFFER_OVERFLOW;
2145 + coder_high -= CODER_VALUEHLF;
2146 + coder_low -= CODER_VALUEHLF;
2148 + else if ((CODER_VALUE1ST <= coder_low) && (coder_high < CODER_VALUE3RD)) {
2150 + coder_high -= CODER_VALUE1ST;
2151 + coder_low -= CODER_VALUE1ST;
2159 + if (coder_high < coder_low) {
2160 + vector_clear(&tmpv);
2161 + return EC_CODER_WRONG_RANGE;
2166 +// register u32 ndx;
2167 + predctrs[0] = *it;
2168 + if ((('a' <= *it) && (*it <= 'z')) || (('A' <= *it) && (*it <= 'Z'))) {
2175 + register u32 ndx = predctrs[0] + 1;
2176 + predctrs[ndx + 8] = predctrs[ndx];
2177 + predctrs[ndx] = *it;
2188 + vector_clear(&tmpv);
2190 + if (coder_low < CODER_VALUE1ST) {
2191 + if (writebits0(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2192 + return EC_BUFFER_OVERFLOW;
2196 + if (writebits1(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2197 + return EC_BUFFER_OVERFLOW;
2200 + (*(bitvector_ptr)) <<= bitvector_freebits;
2201 + *dstlen = ((u32) bitvector_ptr - (u32) output + 1);
2202 + return EC_NO_ERROR;
2205 +/*******************************************************************************
2207 + DECOMPRESS FUNCTIONS
2209 +*******************************************************************************/
2217 +} ipack_decompressor_values;
2222 + unsigned char *ptr;
2223 + unsigned char *end;
2224 +} ipack_decompressor_bitvector;
2226 +static u8 ipack_bitvector_R_get1(ipack_decompressor_bitvector * bv)
2229 + if (bv->ptr == bv->end) {
2233 + tmp = (*(bv->ptr) >> bv->freebits) & 0x01;
2234 + if (!(bv->freebits)) {
2244 +/* Decompress block
2245 + * returns non zero if fails
2247 +static int ipack_armlib_decompress(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen)
2249 + ARM_DataType *data;
2250 + register u32 coder_high = CODER_VALUEMAX - 1;
2251 + register u32 coder_low = 0;
2252 + register u32 coder_value = 0;
2253 + u32 coder_overread = 0;
2254 + ipack_decompressor_bitvector bitvector;
2258 + TokenType tkns[8];
2261 + ipack_nodetype *treeroot = ((ipack_model_type *) model)->tree_root_ptr;
2262 + PredictorType *predctrs = ((ipack_model_type *) model)->predictors_ptr;
2264 +#ifdef IPACK_TREE_TO_CODE
2265 + TokenType(*treefunc) (ipack_nodetype *, u32, u32, ipack_probability_type *);
2267 + treefunc = ((ipack_model_type *) model)->tree_code;
2271 + if (((ipack_model_type *) model)->ID[0] != 'i') {
2272 + return EC_NOT_IPMF_MODEL;
2274 + else if (((ipack_model_type *) model)->ID[1] != 'P') {
2275 + return EC_NOT_IPMF_MODEL;
2277 + else if (((ipack_model_type *) model)->ID[2] != 'M') {
2278 + return EC_NOT_IPMF_MODEL;
2280 + else if (((ipack_model_type *) model)->ID[3] != 'F') {
2281 + return EC_NOT_IPMF_MODEL;
2284 + bitvector.freebits = 7;
2285 + bitvector.ptr = input;
2286 + bitvector.end = input + sourcelen;
2288 + /*if(*(bitvector.ptr++) != 'H') {
2289 + return EC_NOT_HG_BLOCK;
2290 + } else if(*(bitvector.ptr++) != 'G') {
2291 + return EC_NOT_HG_BLOCK;
2296 + data = (void *) output;
2297 + cntbytes = *(bitvector.ptr++);
2299 + cntbytes += *(bitvector.ptr++);
2301 + { //predictor reset
2302 + register PredictorType *ptr = predctrs;
2303 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS;
2304 + while (ptr < end) {
2308 + for (i = 0; i < CODER_VALUEBITS; ++i) {
2309 + coder_value <<= 1;
2310 + coder_value += ipack_bitvector_R_get1(&bitvector);
2312 + lngth = dstlen >> 2;
2313 + if (lngth > (cntbytes >> 2)) {
2314 + lngth = cntbytes >> 2;
2316 + for (i = 0; (i < lngth); ++i) {
2320 + for (j = 0; j < NUMBER_OF_TOKENS_PER_INSTRUCTION; ++j) {
2321 + ipack_probability_type prob;
2322 + u32 range = coder_high - coder_low + 1;
2324 +#ifdef IPACK_TREE_TO_CODE
2325 + if (treefunc != NULL)
2326 + itoken = (*treefunc) (treeroot, coder_value - coder_low + 1, range, &prob);
2329 + itoken = ipack_model_get_token_for_range(treeroot, coder_value - coder_low + 1, range, &prob);
2332 + if (prob.high == prob.low) {
2333 + return EC_CODER_WRONG_PROBABILITY;
2335 + coder_high = coder_low + ((range * prob.high) >> PROBABILITY_SHIFT) - 1;
2336 + coder_low += ((range * prob.low) >> PROBABILITY_SHIFT);
2338 + if (coder_high < CODER_VALUEHLF) {
2340 + else if (CODER_VALUEHLF <= coder_low) {
2341 + coder_value -= CODER_VALUEHLF;
2342 + coder_high -= CODER_VALUEHLF;
2343 + coder_low -= CODER_VALUEHLF;
2345 + else if ((CODER_VALUE1ST <= coder_low) && (coder_high < CODER_VALUE3RD)) {
2346 + coder_value -= CODER_VALUE1ST;
2347 + coder_high -= CODER_VALUE1ST;
2348 + coder_low -= CODER_VALUE1ST;
2356 + coder_value <<= 1;
2357 + if (bitvector.ptr == bitvector.end) {
2358 + bitvector.freebits = 0;
2360 + coder_value += ((*(bitvector.ptr) >> bitvector.freebits) & 0x01);
2361 + if (bitvector.freebits) {
2362 + --bitvector.freebits;
2365 + bitvector.freebits = 7;
2368 + if (coder_high < coder_low) {
2369 + return EC_CODER_WRONG_RANGE;
2371 + if ((bitvector.ptr == bitvector.end) && !(bitvector.freebits)) {
2372 + if ((coder_overread++) > CODER_VALUEBITS) {
2373 + return EC_BUFFER_UNDERFLOW;
2379 +// register u32 ndx;
2380 + predctrs[0] = itoken;
2381 + if ((('a' <= itoken) && (itoken <= 'z')) || (('A' <= itoken) && (itoken <= 'Z'))) {
2389 + register u32 ndx = predctrs[0] + 1;
2390 + predctrs[ndx + 8] = predctrs[ndx];
2391 + predctrs[ndx] = itoken;
2401 + (*(tptr++)) = itoken;
2405 + if (ipack_glb_endian_X) {
2407 + (*data) = ((*tptr) << 24);
2409 + (*data) |= ((*tptr) << 16);
2411 + (*data) |= ((*tptr) << 8);
2413 + (*data) |= (*tptr);
2416 + (*data) = (((*tptr) & 0xf) << 12);
2418 + (*data) |= ((*tptr) & 0xf);
2420 + (*data) |= (((*tptr) & 0xf) << 28);
2422 + (*data) |= (((*tptr) & 0xf) << 16);
2424 + (*data) |= (((*tptr) & 0xf) << 8);
2426 + (*data) |= (((*tptr) & 0xf) << 20);
2428 + (*data) |= (((*tptr) & 0xf) << 24);
2430 + (*data) |= (((*tptr) & 0xf) << 4);
2437 + (*data) = (*tptr);
2439 + (*data) |= ((*tptr) << 8);
2441 + (*data) |= ((*tptr) << 16);
2443 + (*data) |= ((*tptr) << 24);
2446 + (*data) = (((*tptr) & 0xf) << 20);
2448 + (*data) |= (((*tptr) & 0xf) << 24);
2450 + (*data) |= (((*tptr) & 0xf) << 4);
2452 + (*data) |= (((*tptr) & 0xf) << 8);
2454 + (*data) |= (((*tptr) & 0xf) << 16);
2456 + (*data) |= (((*tptr) & 0xf) << 12);
2458 + (*data) |= ((*tptr) & 0xf);
2460 + (*data) |= (((*tptr) & 0xf) << 28);
2467 + return EC_NO_ERROR;
2470 +static int ipack_armlib_estimate(void *model, unsigned char *input, unsigned long sourcelen, unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime)
2472 + int i, tmp, tmp2, max, maxi;
2473 + int cnt_cond[] = { 0, 0, 0, 0 };
2474 + int cnt_inst[] = { 0, 0, 0, 0 };
2476 + // TODO: make a more precise estimation!!!
2477 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 6;
2478 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 2;
2480 + if (sourcelen % 4 != 0) {
2481 + *dstlen = sourcelen;
2484 + for (i = 0; i < sourcelen; i++, input++) {
2485 + tmp2 = tmp = *input;
2486 + tmp = ((tmp) & 0xf0) >> 4;
2487 + tmp2 = tmp2 & 0xf;
2489 + cnt_cond[i % 4]++;
2490 + if ((tmp2 == 2) || (tmp2 == 3))
2491 + cnt_inst[i % 4]++;
2495 + for (i = 0; i < 4; i++)
2496 + if (max < cnt_cond[i]) {
2497 + max = cnt_cond[i];
2500 + /*jffs2_bbc_print("armlib_EST: %d/%d : %d/%d %d/%d %d/%d %d/%d",
2501 + cnt_cond[maxi],cnt_inst[maxi],
2502 + cnt_cond[0],cnt_inst[0],
2503 + cnt_cond[1],cnt_inst[1],
2504 + cnt_cond[2],cnt_inst[2],
2505 + cnt_cond[3],cnt_inst[3]); */
2507 + if (cnt_cond[maxi] < (sourcelen >> 4)) {
2508 + *dstlen = sourcelen;
2511 + *dstlen = sourcelen / 3;
2517 +static char *ipack_armlib_proc_info(void);
2518 +static int ipack_armlib_proc_command(char *command);
2519 +static void ipack_armlib_destroy_model(void **model);
2521 +struct jffs2_bbc_compressor_type jffs2_bbc_armlib = {
2526 + ipack_armlib_compressor_init, // init_model
2527 + ipack_armlib_destroy_model, // destroy_model
2528 + ipack_armlib_compressor_deinit, // deinit
2529 + ipack_armlib_compress,
2530 + ipack_armlib_estimate,
2531 + ipack_armlib_decompress,
2532 + ipack_armlib_proc_info,
2533 + ipack_armlib_proc_command
2537 +static char *ipack_armlib_proc_info()
2539 +#ifdef IPACK_TREE_TO_CODE
2540 +#ifdef IPACK_AUTO_TREE_TO_CODE
2541 + return "automatic tree to code conversion";
2543 + return "manual tree to code conversion possibility";
2546 + return "tree in memory version";
2550 +static int ipack_armlib_proc_command(char *command)
2552 + struct jffs2_bbc_model_list_node *model;
2553 + ipack_model_type *armlib_model;
2555 + if ((*command == 'g') || (*command == 'G')) {
2556 + jffs2_bbc_print1("Converting tree(s) to ARM code... (keeping original)\n");
2557 + model = jffs2_bbc_armlib.models;
2558 + if (model == NULL)
2559 + jffs2_bbc_print1("no model found!\n");
2560 + while (model != NULL) {
2561 + armlib_model = model->model;
2562 + if (armlib_model == NULL) {
2563 + jffs2_bbc_print1("Error: NULL model!\n");
2566 + ipack_armlib_convert_tree_to_code(armlib_model, IPACK_TREE_CONVERT_KEEP);
2568 + model = model->next_compr_model;
2571 + else if ((*command == 'r') || (*command == 'R')) {
2572 + jffs2_bbc_print1("Converting tree(s) to ARM code... (deleting original)\n");
2573 + model = jffs2_bbc_armlib.models;
2574 + if (model == NULL)
2575 + jffs2_bbc_print1("no model found!\n");
2576 + while (model != NULL) {
2577 + armlib_model = model->model;
2578 + if (armlib_model == NULL) {
2579 + jffs2_bbc_print1("Error: NULL model!\n");
2582 + //armlib_model->tree_code = ipack_tree_to_code(armlib_model, &tree_size);
2583 + //jffs2_bbc_print("Convertation done. Code size=%d\n",tree_size);
2584 + ipack_armlib_convert_tree_to_code(armlib_model, IPACK_TREE_CONVERT_REPLACE);
2586 + model = model->next_compr_model;
2589 + else if ((*command == 'c') || (*command == 'C')) {
2590 + jffs2_bbc_print1("Deleting ARM representation of the tree(s)...\n");
2591 + model = jffs2_bbc_armlib.models;
2592 + if (model == NULL)
2593 + jffs2_bbc_print1("no model found!\n");
2594 + while (model != NULL) {
2595 + armlib_model = model->model;
2596 + if (armlib_model == NULL) {
2597 + jffs2_bbc_print1("Error: NULL model!\n");
2600 + if (armlib_model->tree_code == NULL) {
2601 + jffs2_bbc_print1("already deleted.\n");
2604 + if (armlib_model->tree_root_ptr == NULL) {
2605 + jffs2_bbc_print1("cannot delete this ARM tree - original tree has deleted\n");
2608 + jffs2_bbc_print1("deleting...");
2609 + jffs2_bbc_free(armlib_model->tree_code);
2610 + armlib_model->tree_code = NULL;
2611 + jffs2_bbc_print1("done.\n");
2615 + model = model->next_compr_model;
2618 + else if (*command == '?') {
2619 + jffs2_bbc_print1("ARMLIB commands:\n");
2620 + jffs2_bbc_print1(" g: convert TREEs to ARM code and keep the original\n");
2621 + jffs2_bbc_print1(" r: convert TREEs to ARM code and remove the original\n");
2622 + jffs2_bbc_print1(" c: delete the original TREEs - if there is any\n");
2625 + jffs2_bbc_print1("Unknown command.\n");
2630 +static void ipack_armlib_destroy_model(void **model)
2632 + ipack_model_type *model_img;
2634 + model_img = *model;
2635 + if (model_img == NULL) {
2636 + jffs2_bbc_print1("jffs2.bbc: armlib: NULL model at destoying model!\n");
2639 + if (model_img->tree_code != NULL) {
2640 + //jffs2_bbc_print1("jffs2.bbc: armlib: debug: freeing code...\n");
2641 + jffs2_bbc_free(model_img->tree_code);
2642 + model_img->tree_code = NULL;
2644 + if (model_img->tree_root_ptr != NULL) {
2645 + //jffs2_bbc_print1("jffs2.bbc: armlib: debug: freeing tree...\n");
2646 + jffs2_bbc_free(model_img->tree_root_ptr);
2647 + model_img->tree_root_ptr = NULL;
2650 + jffs2_bbc_free(model_img);
2654 +struct jffs2_bbc_compressor_type *jffs2_bbc_armlib_init(int mode)
2656 + if (jffs2_bbc_register_compressor(&jffs2_bbc_armlib) == 0)
2657 + return &jffs2_bbc_armlib;
2662 +void jffs2_bbc_armlib_deinit(void)
2664 + jffs2_bbc_unregister_compressor(&jffs2_bbc_armlib);
2669 +++ b/fs/jffs2/jffs2_bbc_framework.c
2672 + * JFFS2-BBC: Compression Framework
2674 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
2676 + * Copyright (C) 2004, Ferenc Havasi
2678 + * This program is free software; you can redistribute it and/or
2679 + * modify it under the terms of the GNU General Public License
2680 + * as published by the Free Software Foundation; either version 2
2681 + * of the License, or (at your option) any later version.
2683 + * This program is distributed in the hope that it will be useful,
2684 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2685 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2686 + * GNU General Public License for more details.
2688 + * You should have received a copy of the GNU General Public License
2689 + * along with this program; if not, write to the Free Software
2690 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2694 +/* USE JFFS2_BBC_STANDALONE define if you don't want to compile without JFFS2 */
2696 +//#define DEBUG_COMPRESSORS
2697 +//#define DEBUG_SHOW_BLOCK_SIZES
2699 +#define JFFS2_BBC_STAT_BUFF_SIZE 8000
2704 +#include <malloc.h>
2705 +typedef unsigned long uint32_t;
2709 +#include <linux/kernel.h>
2710 +#include <linux/slab.h>
2711 +#include <linux/vmalloc.h>
2715 +#define JFFS2_BBC_ZLIB_BLOCK_SIGN_0 (120)
2716 +#define JFFS2_BBC_ZLIB_BLOCK_SIGN_1 (94)
2718 +#define JFFS2_BBC_DUMMY_BLOCKSIGN_0 (0x54)
2719 +#define JFFS2_BBC_DUMMY_BLOCKSIGN_1 (0x01)
2722 +#define NULL ((void*)(0))
2725 +#include "jffs2_bbc_framework.h"
2727 +/*********************************************************************
2729 + *********************************************************************/
2731 +static int jffs2_bbc_compression_mode = JFFS2_BBC_ZLIB_MODE;
2732 +static struct jffs2_bbc_compressor_type *jffs2_bbc_manual_compressor = NULL;
2733 +static struct jffs2_bbc_compressor_type *jffs2_bbc_compressors = NULL;
2734 +static struct jffs2_bbc_model_list_node *jffs2_bbc_model_list = NULL;
2735 +static void *last_sb = NULL; /* previously activated sb */
2737 +/*********************************************************************
2738 + * Compressor initialization *
2739 + *********************************************************************/
2741 +#ifndef JFFS2_BBC_STANDALONE
2743 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2744 +struct jffs2_bbc_compressor_type *jffs2_bbc_armlib_init(void);
2745 +void jffs2_bbc_armlib_deinit(void);
2748 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2749 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzo_init(void);
2750 +void jffs2_bbc_lzo_deinit(void);
2753 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2754 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzss_init(void);
2755 +void jffs2_bbc_lzss_deinit(void);
2758 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2759 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzari_init(void);
2760 +void jffs2_bbc_lzari_deinit(void);
2763 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2764 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzhd_init(void);
2765 +void jffs2_bbc_lzhd_deinit(void);
2768 +void jffs2_bbc_compressor_init()
2770 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2771 + jffs2_bbc_armlib_init();
2773 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2774 + jffs2_bbc_lzo_init();
2776 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2777 + jffs2_bbc_lzss_init();
2779 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2780 + jffs2_bbc_lzari_init();
2782 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2783 + jffs2_bbc_lzhd_init();
2787 +void jffs2_bbc_compressor_deinit()
2789 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2790 + jffs2_bbc_lzhd_deinit();
2792 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2793 + jffs2_bbc_lzari_deinit();
2795 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2796 + jffs2_bbc_lzss_deinit();
2798 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2799 + jffs2_bbc_lzo_deinit();
2801 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2802 + jffs2_bbc_armlib_deinit();
2808 +#ifndef JFFS2_BBC_STANDALONE
2810 +/*********************************************************************
2811 + * ZLIB COMPRESSOR *
2812 + *********************************************************************/
2814 +extern int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out, uint32_t * sourcelen, uint32_t * dstlen);
2815 +extern void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen);
2817 +static int jffs2_bbc_zlib_compress(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen)
2819 + return jffs2_zlib_compress2(input, output, sourcelen, dstlen);
2822 +static int jffs2_bbc_zlib_decompress(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen)
2824 + jffs2_zlib_decompress2(input, output, sourcelen, dstlen);
2828 +static int jffs2_bbc_zlib_estimate(void *model, unsigned char *input, unsigned long sourcelen, unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime)
2830 + *dstlen = sourcelen * 65 / 100;
2831 + *readtime = JFFS2_BBC_ZLIB_READ_TIME;
2832 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME;
2836 +static struct jffs2_bbc_compressor_type jffs2_bbc_zlib = {
2839 + {JFFS2_BBC_ZLIB_BLOCK_SIGN_0, JFFS2_BBC_ZLIB_BLOCK_SIGN_1, 0, 0},
2844 + jffs2_bbc_zlib_compress,
2845 + jffs2_bbc_zlib_estimate,
2846 + jffs2_bbc_zlib_decompress,
2852 +static struct jffs2_bbc_compressor_type *jffs2_bbc_original_compressor = &jffs2_bbc_zlib;
2856 +/*********************************************************************
2857 + * Compression mode handling *
2858 + *********************************************************************/
2860 +int jffs2_bbc_get_compression_mode(void)
2862 + return jffs2_bbc_compression_mode;
2865 +void jffs2_bbc_set_compression_mode(int mode)
2867 + jffs2_bbc_compression_mode = mode;
2870 +void jffs2_bbc_set_manual_compressor(struct jffs2_bbc_compressor_type *c)
2872 + jffs2_bbc_manual_compressor = c;
2873 + jffs2_bbc_set_compression_mode(JFFS2_BBC_MANUAL_MODE);
2876 +int jffs2_bbc_set_manual_compressor_by_name(char *name)
2878 + struct jffs2_bbc_compressor_type *l;
2881 + l = jffs2_bbc_compressors;
2882 + while (l != NULL) {
2883 + for (i = 0; i < 1000; i++) {
2884 + if (l->name[i] == 0) {
2885 + jffs2_bbc_set_manual_compressor(l);
2888 + else if (name[i] == 0)
2890 + else if (name[i] != l->name[i])
2895 + jffs2_bbc_set_manual_compressor(NULL);
2899 +static struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_by_name(char *name)
2901 + struct jffs2_bbc_compressor_type *l;
2904 +#ifndef JFFS2_BBC_STANDALONE
2905 + l = jffs2_bbc_original_compressor;
2906 + for (i = 0; i < 1000; i++) {
2907 + if (l->name[i] == 0) {
2910 + else if (name[i] == 0)
2912 + else if (name[i] != l->name[i])
2917 + l = jffs2_bbc_compressors;
2918 + while (l != NULL) {
2919 + for (i = 0; i < 1000; i++) {
2920 + if (l->name[i] == 0) {
2923 + else if (name[i] == 0)
2925 + else if (name[i] != l->name[i])
2934 +int jffs2_bbc_disable_compressor_by_name(char *name)
2936 + struct jffs2_bbc_compressor_type *l;
2938 + l = jffs2_bbc_get_compressor_by_name(name);
2939 + if (l == NULL) return 1;
2944 +int jffs2_bbc_enable_compressor_by_name(char *name)
2946 + struct jffs2_bbc_compressor_type *l;
2948 + l = jffs2_bbc_get_compressor_by_name(name);
2949 + if (l == NULL) return 1;
2954 +void jffs2_bbc_compressor_command_by_name(char *name_and_command)
2956 + struct jffs2_bbc_compressor_type *l;
2959 + l = jffs2_bbc_compressors;
2960 + while (l != NULL) {
2961 + for (i = 0; i < 1000; i++) {
2962 + if (l->name[i] == 0) {
2963 + if (name_and_command[i] != ':') {
2964 + jffs2_bbc_print1("jffs2.bbc: ':' missing after compressor name\n");
2967 + if (l->proc_command != NULL)
2968 + l->proc_command(name_and_command + i + 1);
2973 + else if (name_and_command[i] == 0) {
2976 + else if (name_and_command[i] != l->name[i]) {
2984 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_manual_compressor(void)
2986 + if (jffs2_bbc_get_compression_mode() != JFFS2_BBC_MANUAL_MODE) {
2987 + jffs2_bbc_manual_compressor = NULL;
2989 + return jffs2_bbc_manual_compressor;
2992 +/*********************************************************************
2993 + * Compressor handling *
2994 + *********************************************************************/
2996 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_list(void)
2998 + return jffs2_bbc_compressors;
3001 +struct jffs2_bbc_model_list_node *jffs2_bbc_get_model_list(void)
3003 + return jffs2_bbc_model_list;
3006 +int jffs2_bbc_register_compressor(struct jffs2_bbc_compressor_type *c)
3008 + struct jffs2_bbc_compressor_type *l;
3009 + struct jffs2_bbc_model_list_node *l2;
3010 + int model_found = 0;
3012 + l = jffs2_bbc_compressors;
3013 + /* Check for confilcts */
3014 + while (l != NULL) {
3016 + /*if (strcmp(c->name,l->name)==0) {
3017 + jffs2_bbc_print1("jffs2.bbc: compressor is already loaded.");
3020 + if ((l->model_file_sign == c->model_file_sign) && (c->model_file_sign != 0)) {
3021 + jffs2_bbc_print1("jffs2.bbc: already used model file sign. fail.");
3026 + /* Search and initialize model */
3029 + if (c->init != NULL) {
3030 + if (c->init() != 0) {
3031 + jffs2_bbc_print2("jffs2.bbc: cannot initialize compressor %s.\n", c->name);
3035 + if (c->model_file_sign != 0) {
3036 + l2 = jffs2_bbc_model_list;
3040 + if (c->model_file_sign == l2->sign) {
3041 + if (l2->compressor != NULL) {
3042 + jffs2_bbc_print2("jffs2.bbc: register for %s: BUG, model file already reserved!!!!\n", c->name);
3045 + if (c->init_model(&(l2->model)) != 0) {
3046 + jffs2_bbc_print2("jffs2.bbc: cannot initialize compressor %s for a model", c->name);
3049 + l2->compressor = c;
3050 + l2->next_compr_model = c->models;
3057 + l2 = l2->next_model;
3059 + /*if (model_found==0) {
3060 + jffs2_bbc_print2("jffs2.bbc: no macthing model file found for %s at this time (maybe later)\n",c->name);
3063 + /* Insert to the end of the compressor list */
3066 + c->buffer_size = 0;
3067 + c->stat_compr_orig = c->stat_compr_new = c->stat_decompr = 0;
3069 + if (jffs2_bbc_compressors == NULL) {
3070 + jffs2_bbc_compressors = c;
3073 + l = jffs2_bbc_compressors;
3074 + while (l->next != NULL)
3081 +int jffs2_bbc_unregister_compressor(struct jffs2_bbc_compressor_type *c)
3083 + struct jffs2_bbc_compressor_type *l;
3084 + struct jffs2_bbc_model_list_node *l2;
3086 + if (c->mounted != 0) {
3087 + jffs2_bbc_print1("jffs2.bbc: Compressor is in use. Sorry.");
3090 + if (jffs2_bbc_compressors == NULL) {
3091 + jffs2_bbc_print1("jffs2.bbc: unregister: empty list.");
3094 + else if (jffs2_bbc_compressors == c) {
3095 + if (c->deinit != NULL)
3097 + jffs2_bbc_compressors = c->next;
3100 + l = jffs2_bbc_compressors;
3101 + while (l->next != c) {
3102 + if (l->next == NULL) {
3103 + jffs2_bbc_print2("jffs2.bbc: unregister: cannot find compressor %s in the list.", c->name);
3108 + if (c->deinit != NULL)
3110 + l->next = c->next;
3112 + if (c->buffer != NULL) {
3113 + jffs2_bbc_free(c->buffer);
3115 + c->buffer_size = 0;
3118 + l2 = jffs2_bbc_model_list;
3119 + while (l2 != NULL) {
3120 + if (l2->compressor == c) {
3121 + jffs2_bbc_print1("jffs2.bbc: unregister: BUG: model found!!!");
3122 + l2->compressor = NULL;
3123 + l2->next_compr_model = NULL;
3125 + l2 = l2->next_model;
3131 +int jffs2_bbc_model_new(void *sb, int i_num, void *model)
3133 + struct jffs2_bbc_model_list_node *node;
3134 + struct jffs2_bbc_compressor_type *l;
3135 + char block_sign[2];
3139 + /* check for conflicts... */
3140 + sign = *((int *) model);
3141 + block_sign[0] = *(((char *) model) + 4);
3142 + block_sign[1] = *(((char *) model) + 5);
3143 + node = jffs2_bbc_model_list;
3144 + while (node != NULL) {
3145 + if ((node->block_sign[0] == block_sign[0]) && (node->block_sign[1] == block_sign[1]) && (node->sb == sb)) {
3146 + //jffs2_bbc_print2("jffs2.bbc: model_new: model conflict (inode=%d)!\n",i_num);
3149 + node = node->next_model;
3153 + node = jffs2_bbc_malloc_small((long)sizeof(struct jffs2_bbc_model_list_node));
3155 + node->model = model;
3156 + node->sign = *((int *) model);
3157 + node->block_sign[0] = *(((char *) model) + 4);
3158 + node->block_sign[1] = *(((char *) model) + 5);
3159 + node->inode = i_num;
3160 + node->next_model = jffs2_bbc_model_list;
3161 + node->compressor = NULL;
3162 + node->stat_decompr = 0;
3163 + node->next_compr_model = NULL;
3164 + jffs2_bbc_model_list = node;
3166 + /* search for matching compressor */
3167 + l = jffs2_bbc_compressors;
3168 + while (l != NULL) {
3169 + if (l->model_file_sign == sign) {
3170 + //jffs2_bbc_print2("jffs2.bbc: compressor for model found: %s ",l->name);
3171 + if (l->init_model(&(node->model)) != 0) {
3172 + jffs2_bbc_print1("jffs2.bbc: cannot initialize compressor for a model");
3176 + node->compressor = l;
3177 + node->next_compr_model = l->models;
3187 +static void jffs2_bbc_model_del_from_compressor(struct jffs2_bbc_model_list_node *node)
3189 + struct jffs2_bbc_model_list_node *l;
3191 + if (node->model != NULL) {
3192 + if (node->compressor != NULL) {
3193 + if (node->compressor->destroy_model == NULL) {
3194 + jffs2_bbc_free(node->model);
3195 + node->model = NULL;
3198 + node->compressor->destroy_model(&(node->model));
3199 + if (node->model != NULL)
3200 + jffs2_bbc_print1("jffs2.bbc: warning: not NULL model after destroying!\n");
3205 + if (node->compressor == NULL) {
3206 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor: no compressor!\n");
3209 + l = node->compressor->models;
3211 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor error, models=NULL!\n");
3215 + node->compressor->models = node->next_compr_model;
3216 + node->compressor->mounted--;
3220 + if (l->next_compr_model == node) {
3221 + l->next_compr_model = node->next_compr_model;
3222 + node->compressor->mounted--;
3225 + l = l->next_compr_model;
3227 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor: not found\n");
3233 +void jffs2_bbc_model_del(void *sb)
3235 + struct jffs2_bbc_model_list_node *l, *l2;
3237 + l = jffs2_bbc_model_list;
3240 + if (l->sb == sb) {
3241 + jffs2_bbc_model_list = l->next_model;
3242 + jffs2_bbc_model_del_from_compressor(l);
3243 + jffs2_bbc_free_small(l);
3244 + jffs2_bbc_model_del(sb);
3248 + if (l->next_model == NULL) {
3251 + if (l->next_model->sb == sb) {
3252 + l2 = l->next_model;
3253 + l->next_model = l->next_model->next_model;
3254 + jffs2_bbc_model_del_from_compressor(l2);
3255 + jffs2_bbc_free_small(l2);
3256 + jffs2_bbc_model_del(sb);
3259 + l = l->next_model;
3264 +void jffs2_bbc_model_set_act_sb(void *sb)
3269 +void *jffs2_bbc_model_get_act_sb(void)
3274 +void *jffs2_bbc_model_get_newest(struct jffs2_bbc_compressor_type *compressor)
3276 + struct jffs2_bbc_model_list_node *m, *best_m;
3277 + int max_sign, sign;
3279 + if (compressor == NULL) {
3280 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_get: NULL!!\n");
3286 + m = compressor->models;
3287 + while (m != NULL) {
3288 + if (m->sb == last_sb) {
3289 + sign = (int) (m->block_sign[0]) * 256 + (int) (m->block_sign[1]);
3290 + if (sign > max_sign) {
3295 + m = m->next_compr_model;
3297 + if (best_m != NULL)
3298 + return best_m->model;
3303 +/*********************************************************************
3305 + *********************************************************************/
3307 +static char *jffs2_bbc_stat_buff = NULL;
3309 +char *jffs2_bbc_get_model_stats(void)
3312 + struct jffs2_bbc_model_list_node *m;
3313 + struct jffs2_bbc_compressor_type *c;
3315 + if (jffs2_bbc_stat_buff == NULL)
3316 + jffs2_bbc_stat_buff = jffs2_bbc_malloc(8000);
3318 + b = jffs2_bbc_stat_buff;
3320 + b += sprintf(b, "Loaded compressors:");
3321 + c = jffs2_bbc_compressors;
3322 + while (c != NULL) {
3323 + b += sprintf(b, "\n %s (%d) ", c->name, c->enabled);
3324 + if (c->model_file_sign != 0) {
3325 + b += sprintf(b, "m_sign=%d ", c->model_file_sign);
3326 + b += sprintf(b, "models=");
3328 + while (m != NULL) {
3329 + b += sprintf(b, "(inode=%d)", m->inode);
3330 + m = m->next_compr_model;
3334 + b += sprintf(b, "b_sign=(%d,%d) nomodel", (int) (c->block_sign[0]), (int) (c->block_sign[1]));
3336 + if (c->proc_info != NULL) {
3337 + b += sprintf(b, "\n %s", c->proc_info());
3342 + m = jffs2_bbc_model_list;
3345 + b += sprintf(b, "\nPresent models: NONE\n");
3348 + b += sprintf(b, "\nPresent models:\n");
3349 + while (m != NULL) {
3350 + b += sprintf(b, " b_sign=(%d,%d),inode=%d,decompr=%d", (int) (m->block_sign[0]), (int) (m->block_sign[1]), m->inode, m->stat_decompr);
3351 + if (m->compressor == NULL)
3352 + b += sprintf(b, ",compressor=NULL\n");
3354 + b += sprintf(b, ",compressor=%s\n", m->compressor->name);
3355 + m = m->next_model;
3359 + return jffs2_bbc_stat_buff;
3362 +/*********************************************************************
3363 + * Memory handling, debug *
3364 + *********************************************************************/
3366 +static int jffs2_bbc_mem_counter = 0;
3370 +void *jffs2_bbc_malloc(long size)
3372 + void *addr = vmalloc(size);
3374 + jffs2_bbc_mem_counter++;
3376 + jffs2_bbc_print2("DEBUG: not enough memory (%ld)\n", size);
3381 +void jffs2_bbc_free(void *addr)
3383 + jffs2_bbc_mem_counter--;
3387 +void *jffs2_bbc_malloc_small(long size)
3390 + addr = kmalloc(size, 0);
3392 + jffs2_bbc_mem_counter++;
3396 +void jffs2_bbc_free_small(void *addr)
3398 + jffs2_bbc_mem_counter--;
3404 +void *jffs2_bbc_malloc(long size)
3406 + void *addr = malloc(size);
3408 + jffs2_bbc_mem_counter++;
3412 +void jffs2_bbc_free(void *addr)
3414 + jffs2_bbc_mem_counter--;
3418 +void *jffs2_bbc_malloc_small(long size)
3420 + return jffs2_bbc_malloc(size);
3423 +void jffs2_bbc_free_small(void *addr)
3425 + jffs2_bbc_free(addr);
3430 +int jffs2_bbc_test_memory_counter(int verbose)
3432 + if (verbose > 0) {
3433 + jffs2_bbc_print2("jffs2.bbc: mem_counter=%d!\n", jffs2_bbc_mem_counter);
3435 + return jffs2_bbc_mem_counter;
3438 +int jffs2_bbc_get_memory_counter(void)
3440 + return jffs2_bbc_mem_counter;
3443 +static char mem_stat[200];
3445 +char *jffs2_bbc_get_mem_stats(void)
3447 + sprintf(mem_stat, "Memcounter=%d\n", jffs2_bbc_mem_counter);
3451 +void jffs2_bbc_print_flush(void)
3461 +/*********************************************************************
3462 + * FRAMEWORK - ZLIB REPLACEMENT *
3463 + *********************************************************************/
3465 +#ifndef JFFS2_BBC_STANDALONE
3467 +/* Temorary buffers */
3468 +static char stat_str[JFFS2_BBC_STAT_BUFF_SIZE];
3469 +static int tmp_buffer_size = 0;
3470 +static char *tmp_buffer = NULL;
3472 +/* Statistic - used by /proc/jffs2_bbc and mkfs.jffs2 */
3473 +char *jffs2_bbc_get_compr_stats(void)
3475 + struct jffs2_bbc_compressor_type *l;
3476 + char *s = stat_str;
3478 + s += sprintf(s, "Compression statistics:\n");
3479 + l = jffs2_bbc_original_compressor;
3480 + //s += sprintf(s, " zlib: compr=%d/%d decompr=%d\n", stat_zlib_compr_new, stat_zlib_compr_orig, stat_zlib_decompr);
3481 + s += sprintf(s, " %s: compr=%d/%d decompr=%d\n", l->name, l->stat_compr_new, l->stat_compr_orig, l->stat_decompr);
3482 + l = jffs2_bbc_get_compressor_list();
3483 + while (l != NULL) {
3484 + s += sprintf(s, " %s: compr=%d/%d decompr=%d\n", l->name, l->stat_compr_new, l->stat_compr_orig, l->stat_decompr);
3490 +static void jffs2_bbc_buffer_fill(unsigned char *buff, int size)
3492 + for (; size > 0; size--, buff++)
3497 +static int jffs2_bbc_update_compr_buf(unsigned long size)
3499 + struct jffs2_bbc_compressor_type *l;
3503 + if (tmp_buffer == NULL) {
3504 + tmp_buffer = jffs2_bbc_malloc(size);
3505 + jffs2_bbc_buffer_fill(tmp_buffer, size);
3506 + tmp_buffer_size = size;
3508 + else if (tmp_buffer_size < size) {
3509 + jffs2_bbc_free(tmp_buffer);
3510 + tmp_buffer = jffs2_bbc_malloc(size);
3511 + jffs2_bbc_buffer_fill(tmp_buffer, size);
3512 + tmp_buffer_size = size;
3514 + l = jffs2_bbc_get_compressor_list();
3515 + while (l != NULL) {
3516 + if (l->buffer == NULL) {
3517 + l->buffer_size = size;
3518 + l->buffer = jffs2_bbc_malloc(size);
3519 + jffs2_bbc_buffer_fill(l->buffer, size);
3521 + else if (l->buffer_size < size) {
3522 + jffs2_bbc_free(l->buffer);
3523 + l->buffer_size = size;
3524 + l->buffer = jffs2_bbc_malloc(size);
3525 + jffs2_bbc_buffer_fill(l->buffer, size);
3532 +#ifdef DEBUG_COMPRESSORS
3534 +static unsigned char *debug_tmp_buff = NULL;
3535 +static long debug_orig_srclen = -1;
3536 +static long debug_orig_dstlen = -1;
3537 +static int debug_mem_counter = -1;
3540 +void debug_before_compress(struct jffs2_bbc_compressor_type *c, void *model, unsigned char *input, unsigned char *output, long *sourcelen, long *dstlen)
3543 + debug_orig_srclen = *sourcelen; // for buffer overflow test
3544 + debug_orig_dstlen = *dstlen; // for buffer overflow test
3545 + output[debug_orig_dstlen + 1] = 255;
3547 + debug_mem_counter = jffs2_bbc_get_memory_counter(); // for memory guard
3550 +void debug_after_compress(struct jffs2_bbc_compressor_type *c, int back, void *model, unsigned char *input, unsigned char *output, long *sourcelen, long *dstlen)
3552 + long dst_len = *dstlen;
3553 + long src_len = *sourcelen;
3557 + if (debug_mem_counter != jffs2_bbc_get_memory_counter()) {
3558 + jffs2_bbc_print4("!!!!!!!! %s error: possible COMPRESSOR MEMORY LEAK: %d->%d\n", c->name, debug_mem_counter, jffs2_bbc_get_memory_counter());
3559 + debug_mem_counter = jffs2_bbc_get_memory_counter();
3562 + // Buffer overflow test
3563 + if (output[debug_orig_dstlen + 1] != 255) {
3564 + jffs2_bbc_print7("!!!!!!!! %s error: BUFFER OVERFLOW !!!!!!!!!!!! b[%d]=%d (srclen=%d dstlen=%d, back=%d)\n", c->name, (int) (debug_orig_dstlen + 1), (int) (output[debug_orig_dstlen + 1]), (int) (debug_orig_srclen), (int) (*dstlen), back);
3567 + // Decompression check
3569 + if (debug_tmp_buff == NULL)
3570 + debug_tmp_buff = jffs2_bbc_malloc(17000);
3571 + for (i = 0; i < src_len; i++) debug_tmp_buff[i] = 0xf6;
3572 + c->decompress(model, output, debug_tmp_buff, dst_len, src_len);
3573 + // Memory guard for decompressor
3574 + if (debug_mem_counter != jffs2_bbc_get_memory_counter()) {
3575 + jffs2_bbc_print4("!!!!!!!! %s error: possible DECOMPRESSOR MEMORY LEAK: %d->%d\n", c->name, debug_mem_counter, jffs2_bbc_get_memory_counter());
3576 + debug_mem_counter = jffs2_bbc_get_memory_counter();
3579 + for (i = 0; i < src_len; i++)
3580 + if (input[i] != debug_tmp_buff[i]) {
3581 + jffs2_bbc_print7("!!!!!!!! %s error: BLOCK DECOMPRESSED BADLY (first bad: %d in %d: %d!=%d (compressed size=%d)) !!!!!!!!!!!!\n", c->name, i, src_len, (int)input[i], (int)debug_tmp_buff[i], dst_len);
3587 + // Return value test
3588 + //jffs2_bbc_print3("!!!!!!!! %s error: %d !!!!!!!!!!!!\n", c->name, back);
3593 +int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, uint32_t * sourcelen, uint32_t * dstlen)
3595 + struct jffs2_bbc_compressor_type *c;
3596 + int back, back_zlib, mode, min, i, i2;
3597 + long tmp = 0, tmp_read_time = 1000, tmp_write_time = 1000, orig_src, orig_dest, src, dest;
3598 + struct jffs2_bbc_model_list_node *m;
3600 + unsigned char *tmp_p = NULL;
3602 + sb = jffs2_bbc_model_get_act_sb();
3604 + orig_src = *sourcelen;
3605 + orig_dest = *dstlen;
3607 + mode = jffs2_bbc_get_compression_mode();
3609 + if (mode == JFFS2_BBC_DUMMY_MODE) {
3612 + cpage_out[i++]=JFFS2_BBC_DUMMY_BLOCKSIGN_0;
3613 + cpage_out[i++]=JFFS2_BBC_DUMMY_BLOCKSIGN_1;
3616 + for (;((i < *dstlen) && (i < (*sourcelen)+i2));i++) {
3617 + cpage_out[i] = data_in[i-i2];
3624 + if (mode == JFFS2_BBC_ZLIB_MODE) {
3625 + /*if (!jffs2_bbc_original_compressor->enabled) {
3626 + jffs2_bbc_print2("jffs2.bbc: WARNING: ZLIB mode but %s disabled! Enabling for this procedure...\n",jffs2_bbc_original_compressor->name);
3628 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3629 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3630 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3634 + jffs2_bbc_update_compr_buf(orig_dest);
3636 + if (mode == JFFS2_BBC_SIZE_MODE) {
3637 + // Testing all compressors
3638 + if (!jffs2_bbc_original_compressor->enabled) {
3642 + back_zlib = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3645 + c = jffs2_bbc_get_compressor_list();
3646 + while (c != NULL) {
3647 + c->buffer_cnt = -1;
3648 + if (c->enabled == 0) {
3652 + if (c->model_file_sign == 0) {
3655 +#ifdef DEBUG_COMPRESSORS
3656 + debug_before_compress(c, NULL, data_in, c->buffer, &src, &dest);
3658 + back = c->compress(NULL, data_in, c->buffer, &src, &dest);
3659 +#ifdef DEBUG_COMPRESSORS
3660 + debug_after_compress(c, back, NULL, data_in, c->buffer, &src, &dest);
3663 + c->buffer_cnt = dest;
3664 + if ((min < 0) || (min > dest))
3670 + while (m != NULL) {
3673 + if (m->sb == sb) {
3674 + if (c->buffer_cnt == -1) {
3675 +#ifdef DEBUG_COMPRESSORS
3676 + debug_before_compress(c, m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3678 + back = c->compress(m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3679 +#ifdef DEBUG_COMPRESSORS
3680 + debug_after_compress(c, back, m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3683 + c->buffer_cnt = dest;
3684 + if ((min < 0) || (min > dest))
3689 +#ifdef DEBUG_COMPRESSORS
3690 + debug_before_compress(c, m->model, data_in, tmp_buffer, &src, &dest);
3692 + back = c->compress(m->model, data_in, tmp_buffer, &src, &dest);
3693 +#ifdef DEBUG_COMPRESSORS
3694 + debug_after_compress(c, back, m->model, data_in, tmp_buffer, &src, &dest);
3697 + if (c->buffer_cnt > dest) {
3698 + c->buffer_cnt = dest;
3699 + tmp_p = c->buffer;
3700 + c->buffer = tmp_buffer;
3701 + tmp_buffer = tmp_p;
3702 + if ((min < 0) || (min > dest))
3708 + m = m->next_compr_model;
3713 + //Finding the best and copy its result
3715 +#ifdef DEBUG_SHOW_BLOCK_SIZES
3716 + jffs2_bbc_print1("\n");
3717 + if (jffs2_bbc_original_compressor->enabled) {
3718 + if (min == *dstlen) {
3719 + jffs2_bbc_print3("%s:%d* ", jffs2_bbc_original_compressor->name, (int) (*dstlen));
3722 + jffs2_bbc_print3("%s:%d ", jffs2_bbc_original_compressor->name, (int) (*dstlen));
3725 + c = jffs2_bbc_get_compressor_list();
3726 + while (c != NULL) {
3727 + if (c->enabled == 0) {
3731 + if (c->buffer_cnt == min)
3732 + jffs2_bbc_print3("%s:%d* ", c->name, c->buffer_cnt);
3734 + jffs2_bbc_print3("%s:%d ", c->name, c->buffer_cnt);
3740 + return -1; // none of compressors work (maybe too short output buffer)
3743 + if (jffs2_bbc_original_compressor->enabled) {
3744 + if (min == *dstlen) {
3745 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3746 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3751 + c = jffs2_bbc_get_compressor_list();
3752 + while (c != NULL) {
3753 + if (c->enabled == 0) {
3757 + if (c->buffer_cnt == min) {
3758 + *dstlen = c->buffer_cnt;
3759 + *sourcelen = orig_src;
3760 + for (i = 0; i < *dstlen; i++) {
3761 + cpage_out[i] = c->buffer[i];
3763 + c->stat_compr_orig += *sourcelen;
3764 + c->stat_compr_new += *dstlen;
3769 + jffs2_bbc_print1("jffs2.bbc: compr (full): BUG!!!\n");
3773 + if ((mode == JFFS2_BBC_FASTR_MODE)||(mode == JFFS2_BBC_FASTW_MODE)||(mode == JFFS2_BBC_FASTS_MODE)) {
3774 + // Estimating all compressors
3775 + if (jffs2_bbc_original_compressor->enabled) {
3776 + back = jffs2_bbc_original_compressor->estimate(NULL, data_in, *sourcelen, &tmp, &tmp_read_time, &tmp_write_time);
3780 + tmp_read_time = -1;
3781 + tmp_write_time = -1;
3783 + if (mode == JFFS2_BBC_FASTR_MODE) tmp = tmp_read_time;
3784 + if (mode == JFFS2_BBC_FASTW_MODE) tmp = tmp_write_time;
3786 + c = jffs2_bbc_get_compressor_list();
3787 + while (c != NULL) {
3790 + c->buffer_cnt = -1;
3791 + if (c->enabled == 0) {
3795 + if ((c->model_file_sign == 0) || (jffs2_bbc_model_get_newest(c) != NULL)) {
3796 + back = c->estimate(jffs2_bbc_model_get_newest(c), data_in, src, &dest, &tmp_read_time, &tmp_write_time);
3797 + if (mode == JFFS2_BBC_FASTR_MODE) dest = tmp_read_time;
3798 + if (mode == JFFS2_BBC_FASTW_MODE) dest = tmp_write_time;
3800 + c->buffer_cnt = dest;
3801 + if ((min < 0) || (min > dest))
3805 + c->buffer_cnt = -1;
3810 + // Finding the best and compress with it
3814 + if (jffs2_bbc_original_compressor->enabled) {
3816 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3817 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3818 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3822 + c = jffs2_bbc_get_compressor_list();
3823 + while (c != NULL) {
3824 + if (c->enabled == 0) {
3828 + if (c->buffer_cnt == min) {
3829 + back = c->compress(jffs2_bbc_model_get_newest(c), data_in, cpage_out, (unsigned long*)sourcelen, (unsigned long*)dstlen);
3830 + if ((back == 0) && (*dstlen < orig_dest) && (*dstlen > 4)) {
3831 + c->stat_compr_orig += *sourcelen;
3832 + c->stat_compr_new += *dstlen;
3834 + else { // fallback will always be available
3835 + *sourcelen = orig_src;
3836 + *dstlen = orig_dest;
3837 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3838 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3839 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3846 + jffs2_bbc_print1("jffs2.bbc: compress (fastX mode): BUG!!!\n");
3850 + if (mode == JFFS2_BBC_MANUAL_MODE) {
3851 + c = jffs2_bbc_get_manual_compressor();
3853 + if (c->model_file_sign == 0) {
3856 + back = c->compress(NULL, data_in, cpage_out, &src, &dest);
3860 + c->stat_compr_orig += *sourcelen;
3861 + c->stat_compr_new += *dstlen;
3866 + c->buffer_cnt = -1;
3869 + while (m != NULL) {
3872 + if (m->sb == sb) {
3874 + back = c->compress(m->model, data_in, cpage_out, (unsigned long*)sourcelen, (unsigned long*)dstlen);
3875 + if ((back == 0) && (*dstlen < orig_dest) && (*dstlen > 4)) {
3877 + tmp_p = cpage_out;
3881 + back = c->compress(m->model, data_in, tmp_buffer, &src, &dest);
3882 + if ((back == 0) && (dest < orig_dest) && (dest > 4)) {
3883 + if (c->buffer_cnt > dest) {
3886 + tmp_p = tmp_buffer;
3892 + m = m->next_compr_model;
3895 + if (tmp_p != cpage_out) {
3896 + for (i = 0; i < min; i++)
3897 + cpage_out[i] = tmp_p[i];
3898 + *sourcelen = orig_src;
3901 + c->stat_compr_orig += *sourcelen;
3902 + c->stat_compr_new += *dstlen;
3908 + jffs2_bbc_print1("iPack: manual mode without selected compressor!\n");
3911 + /*if (!jffs2_bbc_original_compressor->enabled) {
3912 + jffs2_bbc_print2("jffs2.bbc: WARNING: %s must be enabled! Enabling for this procedure...\n",jffs2_bbc_original_compressor->name);
3914 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3915 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3916 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3922 + jffs2_bbc_print1("jffs2.bbc: compress: unimlemented compress mode!!!\n");
3926 +void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen)
3928 + struct jffs2_bbc_model_list_node *m;
3929 + struct jffs2_bbc_compressor_type *c;
3934 + /* If the input too small... */
3936 + cpage_out[0]=data_in[0];
3937 + if (destlen==2) cpage_out[1]=data_in[1];
3941 + sb = jffs2_bbc_model_get_act_sb();
3942 + d[0] = *(data_in);
3943 + d[1] = *(data_in + 1);
3945 + d[0] &= 0x7f; // Variants support...
3947 + /* Search for model based decompressors... */
3948 + m = jffs2_bbc_get_model_list();
3949 + while (m != NULL) {
3950 + if ((d[0] == m->block_sign[0]) && (d[1] == m->block_sign[1]) && (sb == m->sb)) {
3951 + if (m->compressor == NULL) {
3952 + jffs2_bbc_print3("jffs2.bbc: decompressor for block_sign (%d,%d) not loaded!\n", (int) (d[0]), (int) (d[1]));
3955 + m->compressor->decompress(m->model, data_in, cpage_out, srclen, destlen);
3956 + m->compressor->stat_decompr++;
3957 + m->stat_decompr++;
3961 + m = m->next_model;
3964 + if ((((int) d[0]) == (int)(jffs2_bbc_original_compressor->block_sign[0])) && (((int) d[1]) == (int)(jffs2_bbc_original_compressor->block_sign[1]))) {
3965 + jffs2_bbc_original_compressor->decompress(NULL, data_in, cpage_out, srclen, destlen);
3966 + jffs2_bbc_original_compressor->stat_decompr++;
3969 + /* Search for non model based decompressors... */
3970 + c = jffs2_bbc_get_compressor_list();
3971 + while (c != NULL) {
3972 + if (c->model_file_sign == 0) {
3973 + if (((int) (d[0]) == (int) (c->block_sign[0])) && ((int) (d[1]) == (int) (c->block_sign[1]))) {
3974 + c->decompress(NULL, data_in, cpage_out, srclen, destlen);
3975 + c->stat_decompr++;
3981 + /* Is it DUMMY? */
3982 + if ((((int) d[0]) == JFFS2_BBC_DUMMY_BLOCKSIGN_0) && (((int) d[1]) == JFFS2_BBC_DUMMY_BLOCKSIGN_1)) {
3983 + for (i=0;i<destlen;i++) {
3984 + cpage_out[i]=data_in[i+2];
3988 + /* No matching decompressor found... */
3989 + jffs2_bbc_print4("jffs2.bbc: cannot find model for decompress: bsign=(%d,%d),sb=%d. Using zlib.\n", (int) d[0], (int) d[1], (int) sb);
3990 + jffs2_bbc_original_compressor->decompress(NULL, data_in, cpage_out, srclen, destlen);
3991 + jffs2_bbc_original_compressor->stat_decompr++;
3996 +++ b/fs/jffs2/jffs2_bbc_framework.h
3999 + * JFFS2-BBC: Compression Framework - headers
4001 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4003 + * Copyright (C) 2004, Ferenc Havasi
4005 + * This program is free software; you can redistribute it and/or
4006 + * modify it under the terms of the GNU General Public License
4007 + * as published by the Free Software Foundation; either version 2
4008 + * of the License, or (at your option) any later version.
4010 + * This program is distributed in the hope that it will be useful,
4011 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4012 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4013 + * GNU General Public License for more details.
4015 + * You should have received a copy of the GNU General Public License
4016 + * along with this program; if not, write to the Free Software
4017 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4021 +#ifndef __JFFS2_BBC_FRAMEWORK_H__
4023 +#define __JFFS2_BBC_FRAMEWORK_H__
4025 +#define JFFS2_BBC_VERSION "0.54.3"
4027 +#define JFFS2_BBC_CONFIG_FILE "bbc.conf"
4029 +/*********************************************************************
4030 + * Compression mode handling *
4031 + *********************************************************************/
4033 +#define JFFS2_BBC_ZLIB_MODE 1
4034 +#define JFFS2_BBC_SIZE_MODE 2
4035 +#define JFFS2_BBC_FASTR_MODE 3
4036 +#define JFFS2_BBC_FASTW_MODE 4
4037 +#define JFFS2_BBC_FASTS_MODE 5
4038 +#define JFFS2_BBC_MANUAL_MODE 6
4039 +#define JFFS2_BBC_DUMMY_MODE 7
4041 +int jffs2_bbc_get_compression_mode(void);
4042 +void jffs2_bbc_set_compression_mode(int mode);
4044 +/*********************************************************************
4045 + * Read/write speed unit *
4046 + * everything is relative to the speed of zlib *
4047 + * bigger number means slower speed! *
4048 + *********************************************************************/
4050 +#define JFFS2_BBC_ZLIB_READ_TIME 10000
4051 +#define JFFS2_BBC_ZLIB_WRITE_TIME 10000
4053 +/*********************************************************************
4054 + * Compressor handling *
4055 + *********************************************************************/
4057 +struct jffs2_bbc_compressor_type
4060 + int model_file_sign; /* 0 for no model file needed */
4061 + char block_sign[4]; /* only nomodel compressors, and only the first 2 _bytes are used! */
4062 + int (*init)(void);
4063 + int (*init_model)(void **model);
4064 + void (*destroy_model)(void **model);
4065 + void (*deinit)(void);
4067 + * *dstlen bytes are allocated.
4068 + * if it is not enough write *sourcelen over to the processed amount of data
4069 + * returns non zero if fails
4071 + int (*compress)(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen);
4072 + int (*estimate)(void *model, unsigned char *input, unsigned long sourcelen,
4073 + unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime);
4074 + /* Decompress block
4075 + * returns non zero if fails
4077 + int (*decompress)(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen);
4078 + char *(*proc_info)(void);
4079 + int (*proc_command)(char *command);
4080 + int enabled; /* filled by BBC */
4081 + int mounted; /* filled by BBC */
4082 + void *models; /* filled by BBC */
4083 + char *buffer; /* filled by BBC */
4084 + int buffer_size; /* filled by BBC */
4085 + int buffer_cnt; /* filled by BBC */
4086 + int buffer_tmp; /* filled by BBC */
4087 + int stat_compr_orig; /* filled by BBC */
4088 + int stat_compr_new; /* filled by BBC */
4089 + int stat_decompr; /* filled by BBC */
4090 + struct jffs2_bbc_compressor_type *next; /* filled by BBC */
4093 +/* It sets the compression mode to JFFS2_BBC_MANUAL_MODE */
4095 +void jffs2_bbc_set_manual_compressor(struct jffs2_bbc_compressor_type *c); /* NULL = ZLIB */
4096 +int jffs2_bbc_set_manual_compressor_by_name(char *name);
4097 +int jffs2_bbc_disable_compressor_by_name(char *name);
4098 +int jffs2_bbc_enable_compressor_by_name(char *name);
4099 +void jffs2_bbc_compressor_command_by_name(char *name_and_command);
4101 +/* If the compression mode is JFFS2_BCC_MANUAL_MODE the manually setted
4102 + compressor can be get using it. Otherwise it returns with NULL. */
4104 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_manual_compressor(void);
4106 +struct jffs2_bbc_model_list_node
4108 + void *sb; /* FS idendifier (JFFS2_SB_INFO(sb) at this moment) */
4109 + void *model; /* model data */
4110 + int sign; /* sign of the model (first 4 bytes) */
4111 + char block_sign[4]; /* block sign - only the first 2 bytes are used! */
4112 + int inode; /* inode number of the model file */
4114 + struct jffs2_bbc_compressor_type *compressor;
4115 + struct jffs2_bbc_model_list_node *next_model;
4116 + struct jffs2_bbc_model_list_node *next_compr_model;
4119 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_list(void);
4120 +struct jffs2_bbc_model_list_node *jffs2_bbc_get_model_list(void);
4122 +int jffs2_bbc_register_compressor(struct jffs2_bbc_compressor_type *c);
4123 +int jffs2_bbc_unregister_compressor(struct jffs2_bbc_compressor_type *c);
4125 +int jffs2_bbc_model_new(void *sb, int i_num, void *model);
4126 +void jffs2_bbc_model_del(void *sb);
4127 +void jffs2_bbc_model_set_act_sb(void *sb);
4128 +void *jffs2_bbc_model_get_act_sb(void);
4129 +void *jffs2_bbc_model_get_newest(struct jffs2_bbc_compressor_type *compressor);
4131 +/*********************************************************************
4132 + * Compressor init function *
4133 + *********************************************************************/
4135 +void jffs2_bbc_compressor_init(void);
4136 +void jffs2_bbc_compressor_deinit(void);
4138 +/*********************************************************************
4140 + *********************************************************************/
4142 +char *jffs2_bbc_get_compr_stats(void);
4143 +char *jffs2_bbc_get_model_stats(void);
4145 +/*********************************************************************
4147 + *********************************************************************/
4150 +void jffs2_bbc_print_flush(void);
4153 +#include <linux/kernel.h>
4154 +#define jffs2_bbc_print1(a) printk(a)
4155 +#define jffs2_bbc_print2(a,b) printk(a,b)
4156 +#define jffs2_bbc_print3(a,b,c) printk(a,b,c)
4157 +#define jffs2_bbc_print4(a,b,c,d) printk(a,b,c,d)
4158 +#define jffs2_bbc_print5(a,b,c,d,e) printk(a,b,c,d,e)
4159 +#define jffs2_bbc_print6(a,b,c,d,e,f) printk(a,b,c,d,e,f)
4160 +#define jffs2_bbc_print7(a,b,c,d,e,f,g) printk(a,b,c,d,e,f,g)
4161 +#define jffs2_bbc_print8(a,b,c,d,e,f,g,h) printk(a,b,c,d,e,f,g,h)
4162 +#define jffs2_bbc_print9(a,b,c,d,e,f,g,h,i) printk(a,b,c,d,e,f,g,h,i)
4165 +#define jffs2_bbc_print1(a) fprintf(stderr,a)
4166 +#define jffs2_bbc_print2(a,b) fprintf(stderr,a,b)
4167 +#define jffs2_bbc_print3(a,b,c) fprintf(stderr,a,b,c)
4168 +#define jffs2_bbc_print4(a,b,c,d) fprintf(stderr,a,b,c,d)
4169 +#define jffs2_bbc_print5(a,b,c,d,e) fprintf(stderr,a,b,c,d,e)
4170 +#define jffs2_bbc_print6(a,b,c,d,e,f) fprintf(stderr,a,b,c,d,e,f)
4171 +#define jffs2_bbc_print7(a,b,c,d,e,f,g) fprintf(stderr,a,b,c,d,e,f,g)
4172 +#define jffs2_bbc_print8(a,b,c,d,e,f,g,h) fprintf(stderr,a,b,c,d,e,f,g,h)
4173 +#define jffs2_bbc_print9(a,b,c,d,e,f,g,h,i) fprintf(stderr,a,b,c,d,e,f,g,h,i)
4176 +/* Handle endianness */
4179 +#define ENDIAN_HOST_AND_TARGET_SAME 0
4180 +#define ENDIAN_HOST_AND_TARGET_DIFFERENT 1
4182 +extern int jffs2_bbc_glb_endian_X;
4186 +/* Allocating more than one page (tip. 4096 byte) */
4187 +void *jffs2_bbc_malloc(long size);
4188 +void jffs2_bbc_free(void *addr);
4190 +/* Allocating less than one page (tip. 4096 byte) */
4191 +void *jffs2_bbc_malloc_small(long size);
4192 +void jffs2_bbc_free_small(void *addr);
4194 +/* Memory guarding */
4195 +int jffs2_bbc_test_memory_counter(int verbose);
4196 +char *jffs2_bbc_get_mem_stats(void);
4197 +int jffs2_bbc_get_memory_counter(void);
4201 +++ b/fs/jffs2/jffs2_bbc_fs.c
4204 + * JFFS2-BBC: File System Extension for Linux Kernel
4206 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4208 + * Copyright (C) 2004, Ferenc Havasi
4210 + * This program is free software; you can redistribute it and/or
4211 + * modify it under the terms of the GNU General Public License
4212 + * as published by the Free Software Foundation; either version 2
4213 + * of the License, or (at your option) any later version.
4215 + * This program is distributed in the hope that it will be useful,
4216 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4217 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4218 + * GNU General Public License for more details.
4220 + * You should have received a copy of the GNU General Public License
4221 + * along with this program; if not, write to the Free Software
4222 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4226 +#include <linux/kernel.h>
4227 +#include <linux/fs.h>
4228 +#include <linux/jffs2.h>
4229 +#include <linux/proc_fs.h>
4230 +#include <linux/version.h>
4232 +#include "nodelist.h"
4234 +#include "jffs2_bbc_framework.h"
4236 +struct jffs2_bbc_fs_sb_list {
4237 + struct super_block *sb;
4238 + struct jffs2_bbc_fs_sb_list *next;
4241 +static struct jffs2_bbc_fs_sb_list *sb_list = NULL;
4243 +void jffs2_bbc_proc_init(void);
4244 +void jffs2_bbc_proc_deinit(void);
4246 +void jffs2_bbc_load_model(void *sb_par) {
4247 + struct jffs2_sb_info *c;
4248 + //struct jffs2_inode_info *f;
4249 + struct dentry *config_dentry,*model_dentry;
4250 + struct qstr config_name,model_name;
4251 + struct file *config_file,*model_file;
4252 + char *buff=NULL,*model_buff;
4253 + int config_size,model_size;
4255 + struct super_block *sb;
4256 + struct jffs2_bbc_fs_sb_list *sb_l;
4259 + sb_l = jffs2_bbc_malloc_small(sizeof(struct jffs2_bbc_fs_sb_list));
4261 + sb_l->next = sb_list;
4263 + config_name.name = JFFS2_BBC_CONFIG_FILE;
4264 + config_name.len = strlen(config_name.name);
4265 + config_name.hash = full_name_hash(config_name.name,config_name.len);
4266 + config_dentry = d_alloc(sb->s_root,&config_name);
4267 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4268 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,config_dentry);
4270 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,config_dentry,NULL);
4273 + if (config_dentry->d_inode != NULL) {
4274 + config_size = config_dentry->d_inode->i_size;
4275 + //printk("config_file_size=%d\n",config_size);
4276 + if (config_size > 0) {
4277 + buff = jffs2_bbc_malloc(config_size+1);
4278 + config_file = dentry_open(config_dentry,NULL,O_RDONLY);
4279 + kernel_read(config_file,0,buff,config_size);
4280 + buff[config_size] = 0;
4281 + for (prev_i = i = 0 ; i < config_size+1 ; i++) {
4282 + if (buff[i] == '\n') buff[i]=0;
4283 + if (buff[i] == 0) {
4284 + if (prev_i != i) {
4285 + if ((buff[prev_i] == '-') && (buff[prev_i+1] == 0)) break;
4286 + printk("reading model file %s... ",buff+prev_i);
4287 + model_name.name = buff+prev_i;
4288 + model_name.len = strlen(buff+prev_i);
4289 + model_name.hash = full_name_hash(model_name.name,model_name.len);
4290 + model_dentry = d_alloc(sb->s_root,&model_name);
4291 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4292 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,model_dentry);
4294 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,model_dentry,NULL);
4296 + if (model_dentry->d_inode != NULL) {
4297 + c = JFFS2_SB_INFO(model_dentry->d_inode->i_sb);
4298 + //f = JFFS2_INODE_INFO(model_dentry->d_inode);
4299 + model_size = model_dentry->d_inode->i_size;
4300 + model_buff = jffs2_bbc_malloc(model_size);
4301 + model_file = dentry_open(model_dentry,NULL,O_RDONLY);
4302 + kernel_read(model_file,0,model_buff,model_size);
4303 + if (jffs2_bbc_model_new(c,model_dentry->d_inode->i_ino,model_buff) != 0) {
4304 + printk("already loaded.\n");
4305 + jffs2_bbc_free(model_buff);
4308 + printk("done (%d bytes readed from inode %d).\n",model_size,(int)(model_dentry->d_inode->i_ino));
4312 + printk("not found.\n");
4314 + dput(model_dentry);
4321 + dput(config_dentry);
4322 + if (buff != NULL) jffs2_bbc_free(buff);
4325 +void jffs2_bbc_unload_model(void *sb_par)
4327 + struct jffs2_sb_info *c;
4328 + struct super_block *sb = sb_par;
4329 + struct jffs2_bbc_fs_sb_list *sb_l,*sb_l2;
4332 + c = JFFS2_SB_INFO(sb);
4333 + jffs2_bbc_model_del(c);
4334 + if (sb_list == NULL) printk("jffs2.bbc: error! NULL sb list!\n");
4336 + if (sb_list->sb == sb) {
4337 + jffs2_bbc_free_small(sb_list);
4343 + while (sb_l->next != NULL) {
4344 + if (sb_l->next->sb == sb) {
4345 + sb_l2 = sb_l->next->next;
4346 + jffs2_bbc_free_small(sb_l->next);
4347 + sb_l->next = sb_l2;
4350 + sb_l = sb_l->next;
4355 + printk("jffs2.bbc: cannot delete sb from sblist!\n");
4360 +static int jffs2_bbc_get_mounted(void) {
4361 + struct jffs2_bbc_fs_sb_list *sb_l;
4365 + while (sb_l != NULL) {
4367 + sb_l = sb_l->next;
4373 +int jffs2_bbc_proc_read(char *buf, char **start, off_t offset, int count, int *eof, void *data)
4375 + int len = 0, mode;
4377 + mode = jffs2_bbc_get_compression_mode();
4378 + len += sprintf(buf + len, "BBC version: %s\n", JFFS2_BBC_VERSION);
4379 + len += sprintf(buf+len,"Mounted jffs2 filesystems: %d\n",jffs2_bbc_get_mounted());
4380 + //len += sprintf(buf+len,"actual model file inode: %d\n",jffs2_bbc_model_get_inum());
4381 + len += sprintf(buf + len, "Compression mode: ");
4382 + if (mode == JFFS2_BBC_ZLIB_MODE)
4383 + len += sprintf(buf + len, "ZLIB mode");
4384 + else if (mode == JFFS2_BBC_SIZE_MODE)
4385 + len += sprintf(buf + len, "SIZE mode");
4386 + else if (mode == JFFS2_BBC_FASTR_MODE)
4387 + len += sprintf(buf + len, "FASTR mode");
4388 + else if (mode == JFFS2_BBC_FASTW_MODE)
4389 + len += sprintf(buf + len, "FASTW mode");
4390 + else if (mode == JFFS2_BBC_FASTS_MODE)
4391 + len += sprintf(buf + len, "FASTS mode");
4392 + else if (mode == JFFS2_BBC_DUMMY_MODE)
4393 + len += sprintf(buf + len, "DUMMY mode");
4394 + else if (mode == JFFS2_BBC_MANUAL_MODE) {
4395 + len += sprintf(buf + len, "MANUAL mode (");
4396 + if (jffs2_bbc_get_manual_compressor() != NULL)
4397 + len += sprintf(buf + len, "%s)", jffs2_bbc_get_manual_compressor()->name);
4399 + len += sprintf(buf + len, "ZLIB)");
4402 + len += sprintf(buf + len, "unknown mode");
4403 + len += sprintf(buf + len, "\n");
4404 + len += sprintf(buf + len, "%s", jffs2_bbc_get_compr_stats());
4405 + len += sprintf(buf + len, "%s", jffs2_bbc_get_model_stats());
4406 + len += sprintf(buf + len, "%s", jffs2_bbc_get_mem_stats());
4411 +int jffs2_bbc_proc_write(struct file *file, const char *buffer_orig, unsigned long count, void *data)
4415 + struct jffs2_bbc_fs_sb_list *sb_l;
4417 + if (buffer_orig == NULL) return 0;
4419 + buffer = jffs2_bbc_malloc(count+2);
4420 + for (i=0;i<count;i++) buffer[i]=buffer_orig[i];
4421 + buffer[count] = 0;
4422 + if ((*buffer == 'z') || (*buffer == 'Z')) {
4423 + jffs2_bbc_set_compression_mode(JFFS2_BBC_ZLIB_MODE);
4424 + jffs2_bbc_print1("jffs2.bbc: ZLIB compression mode activated.\n");
4425 + jffs2_bbc_free(buffer);
4428 + else if ((*buffer == 's') || (*buffer == 'S')) {
4429 + jffs2_bbc_set_compression_mode(JFFS2_BBC_SIZE_MODE);
4430 + jffs2_bbc_print1("jffs2.bbc: SIZE compression mode activated.\n");
4431 + jffs2_bbc_free(buffer);
4434 + else if ((*buffer == 'd') || (*buffer == 'D')) {
4435 + jffs2_bbc_set_compression_mode(JFFS2_BBC_DUMMY_MODE);
4436 + jffs2_bbc_print1("jffs2.bbc: DUMMY compression mode activated.\n");
4437 + jffs2_bbc_free(buffer);
4440 + else if (((*buffer == 'm') || (*buffer == 'M')) && (count >= 3) && (buffer[1] == ':')) {
4441 + jffs2_bbc_print1("jffs2.bbc: activating MANUAL mode.\n");
4442 + jffs2_bbc_set_manual_compressor_by_name(buffer + 2);
4443 + jffs2_bbc_free(buffer);
4446 + else if (((*buffer == '0')) && (count >= 3) && (buffer[1] == ':')) {
4447 + jffs2_bbc_print1("jffs2.bbc: disabling a compressor... ");
4448 + if (jffs2_bbc_disable_compressor_by_name(buffer + 2) == 0) {
4449 + jffs2_bbc_print1("done.\n");
4452 + jffs2_bbc_print1("not found.\n");
4454 + jffs2_bbc_free(buffer);
4457 + else if (((*buffer == '1')) && (count >= 3) && (buffer[1] == ':')) {
4458 + jffs2_bbc_print1("jffs2.bbc: enabling a compressor... ");
4459 + if (jffs2_bbc_enable_compressor_by_name(buffer + 2) == 0) {
4460 + jffs2_bbc_print1("done.\n");
4463 + jffs2_bbc_print1("not found.\n");
4465 + jffs2_bbc_free(buffer);
4468 + else if (((*buffer == 'c') || (*buffer == 'C')) && (count >= 3) && (buffer[1] == ':')) {
4469 + jffs2_bbc_compressor_command_by_name(buffer + 2);
4470 + jffs2_bbc_free(buffer);
4473 + else if ((*buffer == 'r') || (*buffer == 'R')) {
4474 + jffs2_bbc_print1("jffs2.bbc: reloading model files:\n");
4476 + while (sb_l != NULL) {
4477 + jffs2_bbc_unload_model(sb_l->sb);
4478 + jffs2_bbc_load_model(sb_l->sb);
4479 + sb_l = sb_l->next;
4481 + jffs2_bbc_free(buffer);
4484 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 'r') || (buffer[1] == 'R'))) {
4485 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTR_MODE);
4486 + jffs2_bbc_print1("jffs2.bbc: FASTR compression mode activated.\n");
4487 + jffs2_bbc_free(buffer);
4490 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 'w') || (buffer[1] == 'W'))) {
4491 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTW_MODE);
4492 + jffs2_bbc_print1("jffs2.bbc: FASTW compression mode activated.\n");
4493 + jffs2_bbc_free(buffer);
4496 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 's') || (buffer[1] == 'S'))) {
4497 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTS_MODE);
4498 + jffs2_bbc_print1("jffs2.bbc: FASTS compression mode activated.\n");
4499 + jffs2_bbc_free(buffer);
4503 + jffs2_bbc_print1("jffs2.bbc: unkown command. Valid commands are:\n"
4504 + " z = switch to ZLIB compression mode\n"
4505 + " s = switch to SIZE compression mode\n"
4506 + " d = switch to DUMMY compression mode\n"
4507 + " fr = switch to FASTR compression mode\n"
4508 + " fw = switch to FASTW compression mode\n"
4509 + " fs = switch to FASTS compression mode\n"
4510 + " r = reread model files from actual file system\n"
4511 + " m:compressor_name = switch to MANUAL compression mode\n"
4512 + " 0:compressor_name = disable a compressor\n"
4513 + " 1:compressor_name = enable a compressor\n"
4514 + " c:compressor_name:command = enable a compressor\n");
4515 + jffs2_bbc_free(buffer);
4519 +void jffs2_bbc_proc_init()
4521 + struct proc_dir_entry *res = create_proc_entry("jffs2_bbc", 0, NULL);
4522 + jffs2_bbc_compressor_init();
4524 + res->read_proc = jffs2_bbc_proc_read;
4525 + res->write_proc = jffs2_bbc_proc_write;
4529 +void jffs2_bbc_proc_deinit()
4531 + jffs2_bbc_compressor_deinit();
4532 + remove_proc_entry("jffs2_bbc", NULL);
4535 +++ b/fs/jffs2/jffs2_bbc_fs.h
4538 + * JFFS2 BBC: File System Extension for Linux Kernel - headers
4540 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4542 + * Copyright (C) 2004, Ferenc Havasi
4544 + * This program is free software; you can redistribute it and/or
4545 + * modify it under the terms of the GNU General Public License
4546 + * as published by the Free Software Foundation; either version 2
4547 + * of the License, or (at your option) any later version.
4549 + * This program is distributed in the hope that it will be useful,
4550 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4551 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4552 + * GNU General Public License for more details.
4554 + * You should have received a copy of the GNU General Public License
4555 + * along with this program; if not, write to the Free Software
4556 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4560 +extern int jffs2_bbc_inode_not_found;
4562 +void jffs2_bbc_load_model(void *sb);
4563 +void jffs2_bbc_unload_model(void *sb);
4565 +void jffs2_bbc_proc_init(void);
4566 +void jffs2_bbc_proc_deinit(void);
4568 +++ b/fs/jffs2/jffs2_bbc_lzari_comp.c
4570 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4573 + jffs2_bbc_lzari_comp.c -- Lempel-Ziv-Arithmetic coding compression module for jffs2
4574 + Copyright (C) 2004 Patrik Kluba
4575 + Based on the LZARI source included in LDS (lossless datacompression sources)
4576 + Block-compression and bitstream modifications by Patrik Kluba
4577 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4581 +Original copyright follows:
4583 +**************************************************************
4584 + LZARI.C -- A Data Compression Program
4586 +**************************************************************
4587 + 4/7/1989 Haruhiko Okumura
4588 + Use, distribute, and modify this program freely.
4589 + Please send me your improved versions.
4591 + NIFTY-Serve PAF01022
4592 + CompuServe 74050,1022
4593 +**************************************************************
4595 +LZARI.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
4596 +All rights reserved. Permission granted for non-commercial use.
4602 + 2004-02-18 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
4603 + Removed unused variables and fixed no return value
4605 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
4612 +#define N 4096 /* size of ring buffer */
4613 +#define F 60 /* upper limit for match_length */
4614 +#define THRESHOLD 2 /* encode string into position and length
4615 + if match_length is greater than this */
4616 +#define NIL N /* index for root of binary search trees */
4618 +static unsigned char
4619 + text_buf[N + F - 1]; /* ring buffer of size N,
4620 + with extra F-1 bytes to facilitate string comparison */
4621 +static unsigned long match_position, match_length, /* of longest match. These are
4622 + set by the InsertNode() procedure. */
4623 + lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
4624 + parents -- These constitute binary search trees. */
4626 +static void InitTree(void) /* Initialize trees */
4630 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
4631 + left children of node i. These nodes need not be initialized.
4632 + Also, dad[i] is the parent of node i. These are initialized to
4633 + NIL (= N), which stands for 'not used.'
4634 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
4635 + for strings that begin with character i. These are initialized
4636 + to NIL. Note there are 256 trees. */
4638 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL; /* root */
4639 + for (i = 0; i < N; i++) dad[i] = NIL; /* node */
4642 +static void InsertNode(unsigned long r)
4643 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
4644 + trees (text_buf[r]'th tree) and returns the longest-match position
4645 + and length via the global variables match_position and match_length.
4646 + If match_length = F, then removes the old node in favor of the new
4647 + one, because the old one will be deleted sooner.
4648 + Note r plays double role, as tree node and position in buffer. */
4650 + unsigned long i, p, temp;
4651 + unsigned char *key;
4654 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
4655 + rson[r] = lson[r] = NIL; match_length = 0;
4658 + if (rson[p] != NIL) p = rson[p];
4659 + else { rson[p] = r; dad[r] = p; return; }
4661 + if (lson[p] != NIL) p = lson[p];
4662 + else { lson[p] = r; dad[r] = p; return; }
4664 + for (i = 1; i < F; i++)
4665 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
4666 + if (i > THRESHOLD) {
4667 + if (i > match_length) {
4668 + match_position = (r - p) & (N - 1);
4669 + if ((match_length = i) >= F) break;
4670 + } else if (i == match_length) {
4671 + if ((temp = (r - p) & (N - 1)) < match_position)
4672 + match_position = temp;
4676 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
4677 + dad[lson[p]] = r; dad[rson[p]] = r;
4678 + if (rson[dad[p]] == p) rson[dad[p]] = r;
4679 + else lson[dad[p]] = r;
4680 + dad[p] = NIL; /* remove p */
4683 +static void DeleteNode(unsigned long p) /* Delete node p from tree */
4687 + if (dad[p] == NIL) return; /* not in tree */
4688 + if (rson[p] == NIL) q = lson[p];
4689 + else if (lson[p] == NIL) q = rson[p];
4692 + if (rson[q] != NIL) {
4693 + do { q = rson[q]; } while (rson[q] != NIL);
4694 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
4695 + lson[q] = lson[p]; dad[lson[p]] = q;
4697 + rson[q] = rson[p]; dad[rson[p]] = q;
4700 + if (rson[dad[p]] == p) rson[dad[p]] = q;
4701 + else lson[dad[p]] = q;
4705 +/********** Arithmetic Compression **********/
4707 +/* If you are not familiar with arithmetic compression, you should read
4708 + I. E. Witten, R. M. Neal, and J. G. Cleary,
4709 + Communications of the ACM, Vol. 30, pp. 520-540 (1987),
4710 + from which much have been borrowed. */
4714 +/* Q1 (= 2 to the M) must be sufficiently large, but not so
4715 + large as the unsigned long 4 * Q1 * (Q1 - 1) overflows. */
4717 +#define Q1 (1UL << M)
4718 +#define Q2 (2 * Q1)
4719 +#define Q3 (3 * Q1)
4720 +#define Q4 (4 * Q1)
4721 +#define MAX_CUM (Q1 - 1)
4723 +#define N_CHAR (256 - THRESHOLD + F)
4724 + /* character code = 0, 1, ..., N_CHAR - 1 */
4726 +static unsigned long char_to_sym[N_CHAR], sym_to_char[N_CHAR + 1];
4727 +static unsigned long
4728 + sym_freq[N_CHAR + 1], /* frequency for symbols */
4729 + sym_cum[N_CHAR + 1], /* cumulative freq for symbols */
4730 + position_cum[N + 1]; /* cumulative freq for positions */
4732 +static void StartModel(void) /* Initialize model */
4734 + unsigned long ch, sym, i;
4736 + sym_cum[N_CHAR] = 0;
4737 + for (sym = N_CHAR; sym >= 1; sym--) {
4739 + char_to_sym[ch] = sym; sym_to_char[sym] = ch;
4740 + sym_freq[sym] = 1;
4741 + sym_cum[sym - 1] = sym_cum[sym] + sym_freq[sym];
4743 + sym_freq[0] = 0; /* sentinel (!= sym_freq[1]) */
4744 + position_cum[N] = 0;
4745 + for (i = N; i >= 1; i--)
4746 + position_cum[i - 1] = position_cum[i] + 10000 / (i + 200);
4747 + /* empirical distribution function (quite tentative) */
4748 + /* Please devise a better mechanism! */
4751 +static void UpdateModel(unsigned long sym)
4753 + unsigned long c, ch_i, ch_sym;
4755 + if (sym_cum[0] >= MAX_CUM) {
4757 + for (i = N_CHAR; i > 0; i--) {
4759 + c += (sym_freq[i] = (sym_freq[i] + 1) >> 1);
4763 + for (i = sym; sym_freq[i] == sym_freq[i - 1]; i--) ;
4765 + ch_i = sym_to_char[i]; ch_sym = sym_to_char[sym];
4766 + sym_to_char[i] = ch_sym; sym_to_char[sym] = ch_i;
4767 + char_to_sym[ch_i] = sym; char_to_sym[ch_sym] = i;
4770 + while (--i > 0) sym_cum[i]++;
4774 +static unsigned long BinarySearchSym(unsigned long x)
4775 + /* 1 if x >= sym_cum[1],
4776 + N_CHAR if sym_cum[N_CHAR] > x,
4777 + i such that sym_cum[i - 1] > x >= sym_cum[i] otherwise */
4779 + unsigned long i, j, k;
4781 + i = 1; j = N_CHAR;
4784 + if (sym_cum[k] > x) i = k + 1; else j = k;
4789 +unsigned long BinarySearchPos(unsigned long x)
4790 + /* 0 if x >= position_cum[1],
4791 + N - 1 if position_cum[N] > x,
4792 + i such that position_cum[i] > x >= position_cum[i + 1] otherwise */
4794 + unsigned long i, j, k;
4799 + if (position_cum[k] > x) i = k + 1; else j = k;
4804 +/* modified for block compression */
4805 +/* on return, srclen will contain the number of successfully compressed bytes
4806 + and dstlen will contain completed compressed bytes */
4808 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
4809 + unsigned long *dstlen)
4811 + unsigned long c, i, len, r, s, last_match_length, sym, range;
4812 + unsigned long low = 0;
4813 + unsigned long high = Q4;
4814 + unsigned long shifts = 0; /* counts for magnifying low and high around Q2 */
4815 + unsigned char *ip, *op;
4816 + unsigned long written = 0;
4817 + unsigned long read = 0;
4818 + unsigned char buffer = 0;
4819 + unsigned char mask = 128;
4820 + unsigned char *srcend = srcbuf + *srclen;
4821 + unsigned char *dstend = dstbuf + *dstlen;
4825 + InitTree(); /* initialize trees */
4827 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
4828 + any character that will appear often. */
4829 + for (len = 0; (len < F) && (ip < srcend); len++)
4830 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
4833 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
4834 + each of which begins with one or more 'space' characters. Note
4835 + the order in which these strings are inserted. This way,
4836 + degenerate trees will be less likely to occur. */
4837 + InsertNode(r); /* Finally, insert the whole string just read. The
4838 + global variables match_length and match_position are set. */
4840 + if (match_length > len) match_length = len; /* match_length
4841 + may be spuriously long near the end of text. */
4842 + if (match_length <= THRESHOLD) {
4843 + match_length = 1; /* Not long enough match. Send one byte. */
4844 + sym = char_to_sym[text_buf[r]];
4845 + range = high - low;
4846 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
4847 + low += (range * sym_cum[sym ]) / sym_cum[0];
4850 + if ((mask >>= 1) == 0) {
4851 + if (op >= dstend) {
4852 + *dstlen = written;
4861 + for ( ; shifts > 0; shifts--) {
4863 + if ((mask >>= 1) == 0) {
4864 + if (op >= dstend) {
4865 + *dstlen = written;
4875 + } else if (low >= Q2) {
4877 + if ((mask >>= 1) == 0) {
4878 + if (op >= dstend) {
4879 + *dstlen = written;
4888 + for ( ; shifts > 0; shifts--) {
4889 + if ((mask >>= 1) == 0) {
4890 + if (op >= dstend) {
4891 + *dstlen = written;
4903 + } else if (low >= Q1 && high <= Q3) {
4908 + low += low; high += high;
4912 + sym = char_to_sym[255 - THRESHOLD + match_length];
4913 + range = high - low;
4914 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
4915 + low += (range * sym_cum[sym ]) / sym_cum[0];
4918 + if ((mask >>= 1) == 0) {
4919 + if (op >= dstend) {
4920 + *dstlen = written;
4929 + for ( ; shifts > 0; shifts--) {
4931 + if ((mask >>= 1) == 0) {
4932 + if (op >= dstend) {
4933 + *dstlen = written;
4943 + } else if (low >= Q2) {
4945 + if ((mask >>= 1) == 0) {
4946 + if (op >= dstend) {
4947 + *dstlen = written;
4956 + for ( ; shifts > 0; shifts--) {
4957 + if ((mask >>= 1) == 0) {
4958 + if (op >= dstend) {
4959 + *dstlen = written;
4971 + } else if (low >= Q1 && high <= Q3) {
4976 + low += low; high += high;
4979 + range = high - low;
4980 + high = low + (range * position_cum[match_position - 1]) / position_cum[0];
4981 + low += (range * position_cum[match_position ]) / position_cum[0];
4984 + if ((mask >>= 1) == 0) {
4985 + if (op >= dstend) {
4986 + *dstlen = written;
4995 + for ( ; shifts > 0; shifts--) {
4997 + if ((mask >>= 1) == 0) {
4998 + if (op >= dstend) {
4999 + *dstlen = written;
5012 + if ((mask >>= 1) == 0) {
5013 + if (op >= dstend) {
5014 + *dstlen = written;
5023 + for ( ; shifts > 0; shifts--) {
5024 + if ((mask >>= 1) == 0) {
5025 + if (op >= dstend) {
5026 + *dstlen = written;
5039 + if ((low >= Q1) && (high <= Q3)) {
5052 + last_match_length = match_length;
5053 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
5058 + text_buf[s + N] = c;
5059 + s = (s + 1) & (N - 1);
5060 + r = (r + 1) & (N - 1);
5064 + while (i++ < last_match_length) {
5066 + s = (s + 1) & (N - 1);
5067 + r = (r + 1) & (N - 1);
5068 + if (--len) InsertNode(r);
5070 + } while (len > 0);
5073 + if ((mask >>= 1) == 0) {
5074 + if (op >= dstend) {
5075 + *dstlen = written;
5084 + for ( ; shifts > 0; shifts--) {
5086 + if ((mask >>= 1) == 0) {
5087 + if (op >= dstend) {
5088 + *dstlen = written;
5100 + if ((mask >>= 1) == 0) {
5101 + if (op >= dstend) {
5102 + *dstlen = written;
5111 + for ( ; shifts > 0; shifts--) {
5112 + if ((mask >>= 1) == 0) {
5113 + if (op >= dstend) {
5114 + *dstlen = written;
5125 + for (i = 0; i < 7; i++) {
5126 + if ((mask >>= 1) == 0) {
5127 + if (op >= dstend) {
5128 + *dstlen = written;
5138 + *dstlen = written;
5142 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
5143 + unsigned long dstlen) /* Just the reverse of Encode(). */
5145 + unsigned long i, r, j, k, c, range, sym;
5146 + unsigned char *ip, *op;
5147 + unsigned char *srcend = srcbuf + srclen;
5148 + unsigned char *dstend = dstbuf + dstlen;
5149 + unsigned char buffer = 0;
5150 + unsigned char mask = 0;
5151 + unsigned long low = 0;
5152 + unsigned long high = Q4;
5153 + unsigned long value = 0;
5156 + for (i = 0; i < M + 2; i++) {
5158 + if ((mask >>= 1) == 0) {
5159 + buffer = (ip >= srcend) ? 0 : *(ip++);
5162 + value += ((buffer & mask) != 0);
5165 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
5167 + while (op < dstend) {
5168 + range = high - low;
5169 + sym = BinarySearchSym((unsigned long)
5170 + (((value - low + 1) * sym_cum[0] - 1) / range));
5171 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
5172 + low += (range * sym_cum[sym ]) / sym_cum[0];
5175 + value -= Q2; low -= Q2; high -= Q2;
5176 + } else if (low >= Q1 && high <= Q3) {
5177 + value -= Q1; low -= Q1; high -= Q1;
5178 + } else if (high > Q2) break;
5179 + low += low; high += high;
5181 + if ((mask >>= 1) == 0) {
5182 + buffer = (ip >= srcend) ? 0 : *(ip++);
5185 + value += ((buffer & mask) != 0);
5187 + c = sym_to_char[sym];
5190 + if (op >= dstend) return -1;
5192 + text_buf[r++] = c;
5195 + j = c - 255 + THRESHOLD;
5196 + range = high - low;
5197 + i = BinarySearchPos((unsigned long)
5198 + (((value - low + 1) * position_cum[0] - 1) / range));
5199 + high = low + (range * position_cum[i ]) / position_cum[0];
5200 + low += (range * position_cum[i + 1]) / position_cum[0];
5203 + value -= Q2; low -= Q2; high -= Q2;
5204 + } else if (low >= Q1 && high <= Q3) {
5205 + value -= Q1; low -= Q1; high -= Q1;
5206 + } else if (high > Q2) break;
5207 + low += low; high += high;
5209 + if ((mask >>= 1) == 0) {
5210 + buffer = (ip >= srcend) ? 0 : *(ip++);
5213 + value += ((buffer & mask) != 0);
5215 + i = (r - i - 1) & (N - 1);
5216 + for (k = 0; k < j; k++) {
5217 + c = text_buf[(i + k) & (N - 1)];
5218 + if (op >= dstend) return -1;
5220 + text_buf[r++] = c;
5228 +/* interface to jffs2 bbc follows */
5230 +#include "jffs2_bbc_framework.h"
5232 +#define JFFS2_BBC_LZARI_BLOCK_SIGN {0x73, 0x9a, 0x1c, 0x4d}
5235 +jffs2_bbc_lzari_compressor_init (void);
5238 +jffs2_bbc_lzari_compressor_deinit (void);
5241 +jffs2_bbc_lzari_compress (void *model, unsigned char *input,
5242 + unsigned char *output, unsigned long *sourcelen,
5243 + unsigned long *dstlen);
5246 +jffs2_bbc_lzari_estimate (void *model, unsigned char *input,
5247 + unsigned long sourcelen, unsigned long *dstlen,
5248 + unsigned long *readtime, unsigned long *writetime);
5251 +jffs2_bbc_lzari_decompress (void *model, unsigned char *input,
5252 + unsigned char *output, unsigned long sourcelen,
5253 + unsigned long dstlen);
5256 +jffs2_bbc_lzari_proc_info (void);
5259 +jffs2_bbc_lzari_proc_command (char *command);
5261 +struct jffs2_bbc_compressor_type jffs2_bbc_lzari = {
5264 + JFFS2_BBC_LZARI_BLOCK_SIGN,
5265 + jffs2_bbc_lzari_compressor_init,
5268 + jffs2_bbc_lzari_compressor_deinit,
5269 + jffs2_bbc_lzari_compress,
5270 + jffs2_bbc_lzari_estimate,
5271 + jffs2_bbc_lzari_decompress,
5272 + jffs2_bbc_lzari_proc_info,
5273 + jffs2_bbc_lzari_proc_command
5277 +jffs2_bbc_lzari_compressor_init (void)
5283 +jffs2_bbc_lzari_compressor_deinit (void)
5288 +jffs2_bbc_lzari_compress (void *model, unsigned char *input,
5289 + unsigned char *output, unsigned long *sourcelen,
5290 + unsigned long *dstlen)
5293 + unsigned long dst = *dstlen;
5294 + *(output++) = jffs2_bbc_lzari.block_sign[0];
5295 + *(output++) = jffs2_bbc_lzari.block_sign[1];
5297 + retval = Encode(input, output, sourcelen, &dst);
5304 +jffs2_bbc_lzari_estimate (void *model, unsigned char *input,
5305 + unsigned long sourcelen, unsigned long *dstlen,
5306 + unsigned long *readtime, unsigned long *writetime)
5308 + *dstlen = sourcelen / 2;
5309 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 15;
5310 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 7;
5315 +jffs2_bbc_lzari_decompress (void *model, unsigned char *input,
5316 + unsigned char *output, unsigned long sourcelen,
5317 + unsigned long dstlen)
5319 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzari.block_sign[0] ) ||
5320 + ( *(input++) != (unsigned char)jffs2_bbc_lzari.block_sign[1] )
5324 + return Decode(input, output, sourcelen - 2, dstlen);
5329 +jffs2_bbc_lzari_proc_info (void)
5331 + return "Lempel-Ziv-Arithmetic coding compression module";
5335 +jffs2_bbc_lzari_proc_command (char *command)
5340 +struct jffs2_bbc_compressor_type *
5341 +jffs2_bbc_lzari_init (int mode)
5343 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzari) == 0)
5345 + return &jffs2_bbc_lzari;
5354 +jffs2_bbc_lzari_deinit (void)
5356 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzari);
5359 +++ b/fs/jffs2/jffs2_bbc_lzhd_comp.c
5361 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
5364 + jffs2_bbc_lzhd_comp.c -- Lempel-Ziv-(dynamic) Huffman compression module for jffs2
5365 + Copyright (C) 2004 Patrik Kluba
5366 + Based on the LZHUF source included in LDS (lossless datacompression sources)
5367 + Block-compression and bitstream modifications by Patrik Kluba
5368 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
5372 +Original copyright follows:
5374 +**************************************************************
5376 + written by Haruyasu Yoshizaki 11/20/1988
5377 + some minor changes 4/6/1989
5378 + comments translated by Haruhiko Okumura 4/7/1989
5379 +**************************************************************
5381 +LZHUF.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
5382 +All rights reserved. Permission granted for non-commercial use.
5388 + 2004-02-18 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
5389 + Replaced name lzh-d with lzhd
5390 + Fixed no return value
5392 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
5397 +/* required because of memmove */
5399 + #include <string.h>
5401 + #include <linux/string.h>
5406 +#define N 4096 /* size of ring buffer */
5407 +#define F 60 /* upper limit for match_length */
5408 +#define THRESHOLD 2 /* encode string into position and length
5409 + if match_length is greater than this */
5410 +#define NIL N /* index for root of binary search trees */
5412 +static unsigned char
5413 + text_buf[N + F - 1]; /* ring buffer of size N,
5414 + with extra F-1 bytes to facilitate string comparison */
5415 +static unsigned long match_position, match_length, /* of longest match. These are
5416 + set by the InsertNode() procedure. */
5417 + lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
5418 + parents -- These constitute binary search trees. */
5420 +static void InitTree(void) /* initialize trees */
5424 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
5425 + left children of node i. These nodes need not be initialized.
5426 + Also, dad[i] is the parent of node i. These are initialized to
5427 + NIL (= N), which stands for 'not used.'
5428 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
5429 + for strings that begin with character i. These are initialized
5430 + to NIL. Note there are 256 trees. */
5432 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
5433 + for (i = 0; i < N; i++) dad[i] = NIL;
5436 +static void InsertNode(unsigned long r)
5437 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
5438 + trees (text_buf[r]'th tree) and returns the longest-match position
5439 + and length via the global variables match_position and match_length.
5440 + If match_length = F, then removes the old node in favor of the new
5441 + one, because the old one will be deleted sooner.
5442 + Note r plays double role, as tree node and position in buffer. */
5444 + unsigned long i, p, c;
5446 + unsigned char *key;
5448 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
5449 + rson[r] = lson[r] = NIL; match_length = 0;
5452 + if (rson[p] != NIL) p = rson[p];
5453 + else { rson[p] = r; dad[r] = p; return; }
5455 + if (lson[p] != NIL) p = lson[p];
5456 + else { lson[p] = r; dad[r] = p; return; }
5458 + for (i = 1; i < F; i++)
5459 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
5460 + if (i > THRESHOLD) {
5461 + if (i > match_length) {
5462 + match_position = ((r - p) & (N - 1)) - 1;
5463 + if ((match_length = i) >= F) break;
5465 + if (i == match_length) {
5466 + if ((c = ((r - p) & (N - 1)) - 1) < match_position) {
5467 + match_position = c;
5472 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
5473 + dad[lson[p]] = r; dad[rson[p]] = r;
5474 + if (rson[dad[p]] == p) rson[dad[p]] = r;
5475 + else lson[dad[p]] = r;
5476 + dad[p] = NIL; /* remove p */
5479 +static void DeleteNode(unsigned long p) /* deletes node p from tree */
5483 + if (dad[p] == NIL) return; /* not in tree */
5484 + if (rson[p] == NIL) q = lson[p];
5485 + else if (lson[p] == NIL) q = rson[p];
5488 + if (rson[q] != NIL) {
5489 + do { q = rson[q]; } while (rson[q] != NIL);
5490 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
5491 + lson[q] = lson[p]; dad[lson[p]] = q;
5493 + rson[q] = rson[p]; dad[rson[p]] = q;
5496 + if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
5500 +/* Huffman coding */
5502 +#define N_CHAR (256 - THRESHOLD + F)
5503 + /* kinds of characters (character code = 0..N_CHAR-1) */
5504 +#define T (N_CHAR * 2 - 1) /* size of table */
5505 +#define R (T - 1) /* position of root */
5506 +#define MAX_FREQ 0x8000 /* updates tree when the */
5507 + /* root frequency comes to this value. */
5509 +typedef unsigned long uchar; // much-much faster
5511 +/* table for encoding and decoding the upper 6 bits of position */
5514 +static uchar p_len[64] = {
5515 + 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
5516 + 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06,
5517 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5518 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5519 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5520 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5521 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5522 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
5525 +static uchar p_code[64] = {
5526 + 0x00, 0x20, 0x30, 0x40, 0x50, 0x58, 0x60, 0x68,
5527 + 0x70, 0x78, 0x80, 0x88, 0x90, 0x94, 0x98, 0x9C,
5528 + 0xA0, 0xA4, 0xA8, 0xAC, 0xB0, 0xB4, 0xB8, 0xBC,
5529 + 0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE,
5530 + 0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE,
5531 + 0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEC, 0xEE,
5532 + 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
5533 + 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
5537 +static uchar d_code[256] = {
5538 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5539 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5540 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5541 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5542 + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
5543 + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
5544 + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
5545 + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
5546 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5547 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5548 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5549 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5550 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5551 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5552 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5553 + 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
5554 + 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
5555 + 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
5556 + 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
5557 + 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
5558 + 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
5559 + 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
5560 + 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
5561 + 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
5562 + 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
5563 + 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
5564 + 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
5565 + 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
5566 + 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
5567 + 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
5568 + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
5569 + 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
5572 +static uchar d_len[256] = {
5573 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5574 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5575 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5576 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5577 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5578 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5579 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5580 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5581 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5582 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5583 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5584 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5585 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5586 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5587 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5588 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5589 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5590 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5591 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5592 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5593 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5594 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5595 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5596 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5597 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5598 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5599 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5600 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5601 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5602 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5603 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5604 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5607 +static unsigned long freq[T + 1]; /* frequency table */
5609 +static unsigned long prnt[T + N_CHAR]; /* pointers to parent nodes, except for the */
5610 + /* elements [T..T + N_CHAR - 1] which are used to get */
5611 + /* the positions of leaves corresponding to the codes. */
5613 +static unsigned long son[T]; /* pointers to child nodes (son[], son[] + 1) */
5615 +/* initialization of tree */
5617 +static void StartHuff(void)
5619 + unsigned long i, j;
5621 + for (i = 0; i < N_CHAR; i++) {
5626 + i = 0; j = N_CHAR;
5628 + freq[j] = freq[i] + freq[i + 1];
5630 + prnt[i] = prnt[i + 1] = j;
5637 +/* reconstruction of tree */
5639 +static void reconst(void)
5641 + unsigned long f, l, i, j, k;
5643 + /* collect leaf nodes in the first half of the table */
5644 + /* and replace the freq by (freq + 1) / 2. */
5646 + for (i = 0; i < T; i++) {
5647 + if (son[i] >= T) {
5648 + freq[j] = (freq[i] + 1) / 2;
5653 + /* begin constructing tree by connecting sons */
5654 + for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
5656 + f = freq[j] = freq[i] + freq[k];
5657 + for (k = j - 1; f < freq[k]; k--);
5660 + memmove(&freq[k + 1], &freq[k], l*sizeof(unsigned long));
5662 + memmove(&son[k + 1], &son[k], l*sizeof(unsigned long));
5665 + /* connect prnt */
5666 + for (i = 0; i < T; i++) {
5667 + if ((k = son[i]) >= T) {
5670 + prnt[k] = prnt[k + 1] = i;
5675 +/* increment frequency of given code by one, and update tree */
5677 +static void update(unsigned long c)
5679 + unsigned long i, j, k, l;
5681 + if (freq[R] == MAX_FREQ) {
5688 + /* if the order is disturbed, exchange nodes */
5689 + if (k > freq[l = c + 1]) {
5690 + while (k > freq[++l]);
5692 + freq[c] = freq[l];
5697 + if (i < T) prnt[i + 1] = l;
5703 + if (j < T) prnt[j + 1] = c;
5708 + } while (c = prnt[c]); /* repeat up to root */
5711 +/* modified for block compression */
5712 +/* on return, srclen will contain the number of successfully compressed bytes
5713 + and dstlen will contain completed compressed bytes */
5715 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
5716 + unsigned long *dstlen)
5718 + unsigned long c, i, j, k, len, r, s, last_match_length, code_buf_ptr;
5719 + unsigned char code_buf[17], mask;
5720 + unsigned char *ip, *op;
5721 + unsigned long written = 0;
5722 + unsigned long read = 0;
5723 + unsigned short putbuf = 0;
5725 + unsigned char *srcend = srcbuf + *srclen;
5726 + unsigned char *dstend = dstbuf + *dstlen;
5730 + InitTree(); /* initialize trees */
5731 + code_buf[0] = 0; /* code_buf[1..16] saves eight units of code, and
5732 + code_buf[0] works as eight flags, "1" representing that the unit
5733 + is an unencoded letter (1 byte), "0" a position-and-length pair
5734 + (2 bytes). Thus, eight units require at most 16 bytes of code. */
5735 + code_buf_ptr = mask = 1;
5737 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
5738 + any character that will appear often. */
5739 + for (len = 0; (len < F) && (ip < srcend); len++)
5740 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
5743 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
5744 + each of which begins with one or more 'space' characters. Note
5745 + the order in which these strings are inserted. This way,
5746 + degenerate trees will be less likely to occur. */
5747 + InsertNode(r); /* Finally, insert the whole string just read. The
5748 + global variables match_length and match_position are set. */
5750 + if (match_length > len) match_length = len; /* match_length
5751 + may be spuriously long near the end of text. */
5752 + if (match_length <= THRESHOLD) {
5753 + match_length = 1; /* Not long enough match. Send one byte. */
5755 + i = 0; j = 0; k = prnt[c + T];
5758 + /* if node's address is odd-numbered, choose bigger brother node */
5759 + if (k & 1) i |= 0x8000;
5761 + } while ((k = prnt[k]) != R);
5762 + putbuf |= i >> putlen;
5763 + if ((putlen += j) >= 8) {
5764 + if (op >= dstend) {
5765 + *dstlen = written;
5768 + *(op++) = putbuf >> 8;
5769 + if ((putlen -= 8) >= 8) {
5770 + if (op >= dstend) {
5771 + *dstlen = written;
5777 + putbuf = i << (j - putlen); /**warm**/
5786 + c = 255 - THRESHOLD + match_length;
5787 + i = 0; j = 0; k = prnt[c + T];
5790 + /* if node's address is odd-numbered, choose bigger brother node */
5791 + if (k & 1) i |= 0x8000;
5793 + } while ((k = prnt[k]) != R);
5794 + putbuf |= i >> putlen;
5795 + if ((putlen += j) >= 8) {
5796 + if (op >= dstend) {
5797 + *dstlen = written;
5800 + *(op++) = putbuf >> 8;
5801 + if ((putlen -= 8) >= 8) {
5802 + if (op >= dstend) {
5803 + *dstlen = written;
5809 + putbuf = i << (j - putlen); /**warm**/
5817 + j = p_len[match_position >> 6];
5818 + i = p_code[match_position >> 6] << 8;
5819 + putbuf |= i >> putlen;
5820 + if ((putlen += j) >= 8) {
5821 + if (op >= dstend) {
5822 + *dstlen = written;
5825 + *(op++) = putbuf >> 8;
5826 + if ((putlen -= 8) >= 8) {
5827 + if (op >= dstend) {
5828 + *dstlen = written;
5834 + putbuf = i << (j - putlen); /**hot**/
5842 + i = (match_position & 0x3f) << 10;
5843 + putbuf |= i >> putlen;
5844 + if ((putlen += j) >= 8) {
5845 + if (op >= dstend) {
5846 + *dstlen = written;
5849 + *(op++) = putbuf >> 8;
5850 + if ((putlen -= 8) >= 8) {
5851 + if (op >= dstend) {
5852 + *dstlen = written;
5858 + putbuf = i << (j - putlen); /**hot**/
5866 + last_match_length = match_length;
5867 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
5872 + text_buf[s + N] = c;
5873 + s = (s + 1) & (N - 1);
5874 + r = (r + 1) & (N - 1);
5878 + while (i++ < last_match_length) {
5880 + s = (s + 1) & (N - 1);
5881 + r = (r + 1) & (N - 1);
5882 + if (--len) InsertNode(r);
5884 + } while (len > 0);
5886 + if (op >= dstend) {
5887 + *dstlen = written;
5890 + *(op++) = putbuf >> 8;
5894 + *dstlen = written;
5898 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
5899 + unsigned long dstlen) /* Just the reverse of Encode(). */
5901 + unsigned long i, r, j, k, c;
5902 + unsigned char *ip, *op;
5903 + unsigned char *srcend = srcbuf + srclen;
5904 + unsigned char *dstend = dstbuf + dstlen;
5905 + unsigned short getbuf = 0;
5910 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
5912 + while (op < dstend) {
5914 + /* travel from root to leaf, */
5915 + /* choosing the smaller child node (son[]) if the read bit is 0, */
5916 + /* the bigger (son[]+1} if 1 */
5918 + while (getlen <= 8) {
5920 + t = (ip >= srcend) ? 0 : *(ip++);
5921 + getbuf |= t << (8 - getlen);
5924 + c += ((signed short)getbuf < 0);
5932 + if (op >= dstend) return -1;
5934 + text_buf[r++] = c;
5937 + j = c - 255 + THRESHOLD;
5938 + while (getlen <= 8) {
5940 + t = (ip >= srcend) ? 0 : *(ip++);
5941 + getbuf |= t << (8 - getlen);
5947 + c = d_code[i] << 6;
5949 + /* read lower 6 bits verbatim */
5952 + while (getlen <= 8) {
5954 + t = (ip >= srcend) ? 0 : *(ip++);
5955 + getbuf |= t << (8 - getlen);
5958 + i = (i << 1) + ((signed short)getbuf < 0);
5962 + i = c | (i & 0x3F);
5965 + for (k = 0; k < j; k++) {
5966 + c = text_buf[(i + k) & (N - 1)];
5967 + if (op >= dstend) return -1;
5969 + text_buf[r++] = c;
5977 +/* interface to jffs2 bbc follows */
5979 +#include "jffs2_bbc_framework.h"
5982 +#define JFFS2_BBC_LZHD_BLOCK_SIGN {0x3a, 0x98, 0xf7, 0xda}
5985 +jffs2_bbc_lzhd_compressor_init (void);
5988 +jffs2_bbc_lzhd_compressor_deinit (void);
5991 +jffs2_bbc_lzhd_compress (void *model, unsigned char *input,
5992 + unsigned char *output, unsigned long *sourcelen,
5993 + unsigned long *dstlen);
5996 +jffs2_bbc_lzhd_estimate (void *model, unsigned char *input,
5997 + unsigned long sourcelen, unsigned long *dstlen,
5998 + unsigned long *readtime, unsigned long *writetime);
6001 +jffs2_bbc_lzhd_decompress (void *model, unsigned char *input,
6002 + unsigned char *output, unsigned long sourcelen,
6003 + unsigned long dstlen);
6006 +jffs2_bbc_lzhd_proc_info (void);
6009 +jffs2_bbc_lzhd_proc_command (char *command);
6011 +struct jffs2_bbc_compressor_type jffs2_bbc_lzhd = {
6014 + JFFS2_BBC_LZHD_BLOCK_SIGN,
6015 + jffs2_bbc_lzhd_compressor_init,
6018 + jffs2_bbc_lzhd_compressor_deinit,
6019 + jffs2_bbc_lzhd_compress,
6020 + jffs2_bbc_lzhd_estimate,
6021 + jffs2_bbc_lzhd_decompress,
6022 + jffs2_bbc_lzhd_proc_info,
6023 + jffs2_bbc_lzhd_proc_command
6027 +jffs2_bbc_lzhd_compressor_init (void)
6033 +jffs2_bbc_lzhd_compressor_deinit (void)
6038 +jffs2_bbc_lzhd_compress (void *model, unsigned char *input,
6039 + unsigned char *output, unsigned long *sourcelen,
6040 + unsigned long *dstlen)
6043 + unsigned long dst = *dstlen;
6044 + *(output++) = jffs2_bbc_lzhd.block_sign[0];
6045 + *(output++) = jffs2_bbc_lzhd.block_sign[1];
6047 + retval = Encode(input, output, sourcelen, &dst);
6054 +jffs2_bbc_lzhd_estimate (void *model, unsigned char *input,
6055 + unsigned long sourcelen, unsigned long *dstlen,
6056 + unsigned long *readtime, unsigned long *writetime)
6058 + *dstlen = sourcelen * 55 / 100;
6059 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 8;
6060 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 65 / 10;
6065 +jffs2_bbc_lzhd_decompress (void *model, unsigned char *input,
6066 + unsigned char *output, unsigned long sourcelen,
6067 + unsigned long dstlen)
6069 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzhd.block_sign[0] ) ||
6070 + ( *(input++) != (unsigned char)jffs2_bbc_lzhd.block_sign[1] )
6074 + return Decode(input, output, sourcelen - 2, dstlen);
6079 +jffs2_bbc_lzhd_proc_info (void)
6081 + return "Lempel-Ziv-(dynamic) Huffman compression module";
6085 +jffs2_bbc_lzhd_proc_command (char *command)
6090 +struct jffs2_bbc_compressor_type *
6091 +jffs2_bbc_lzhd_init (int mode)
6093 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzhd) == 0)
6095 + return &jffs2_bbc_lzhd;
6104 +jffs2_bbc_lzhd_deinit (void)
6106 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzhd);
6109 +++ b/fs/jffs2/jffs2_bbc_lzo_comp.c
6111 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
6114 + jffs2_bbc_lzo_comp.c -- LZO1X-1 (and -999) compression module for jffs2
6115 + Copyright (C) 2004 Patrik Kluba
6116 + Based on the original LZO sources
6117 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
6121 + Original copyright notice follows:
6123 + lzo1x_9x.c -- implementation of the LZO1X-999 compression algorithm
6124 + lzo_ptr.h -- low-level pointer constructs
6125 + lzo_swd.ch -- sliding window dictionary
6126 + lzoconf.h -- configuration for the LZO real-time data compression library
6127 + lzo_mchw.ch -- matching functions using a window
6128 + minilzo.c -- mini subset of the LZO real-time data compression library
6129 + config1x.h -- configuration for the LZO1X algorithm
6130 + lzo1x.h -- public interface of the LZO1X compression algorithm
6132 + These files are part of the LZO real-time data compression library.
6134 + Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
6135 + All Rights Reserved.
6137 + The LZO library is free software; you can redistribute it and/or
6138 + modify it under the terms of the GNU General Public License as
6139 + published by the Free Software Foundation; either version 2 of
6140 + the License, or (at your option) any later version.
6142 + The LZO library is distributed in the hope that it will be useful,
6143 + but WITHOUT ANY WARRANTY; without even the implied warranty of
6144 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6145 + GNU General Public License for more details.
6147 + You should have received a copy of the GNU General Public License
6148 + along with the LZO library; see the file COPYING.
6149 + If not, write to the Free Software Foundation, Inc.,
6150 + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
6152 + Markus F.X.J. Oberhumer
6153 + <markus@oberhumer.com>
6158 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
6160 + -removed all 16 bit code
6161 + -all sensitive data will be on 4 byte boundary
6162 + -removed check parts for library use
6163 + -removed all but LZO1X-* compression
6168 + #include <sys/types.h>
6169 + #include <stddef.h>
6170 + #include <string.h>
6171 + #include <limits.h>
6173 + #include <linux/kernel.h>
6174 + #include <linux/types.h>
6175 + #include <linux/stddef.h>
6176 + #include <linux/string.h>
6177 + #define USHRT_MAX 65535
6178 + /* #define UINT_MAX 4294967295U */
6181 +/* data type definitions */
6182 +#define U32 unsigned long
6183 +#define S32 signed long
6185 +#define U16 unsigned short
6186 +#define S16 signed short
6188 +#define U8 unsigned char
6189 +#define S8 signed char
6192 +/*************************************/
6198 +#define SWD_THRESHOLD THRESHOLD
6200 +/* shortest unsigned int that 2 * SWD_F + SWD_N (currently 53248) fits in */
6201 +typedef unsigned short swd_uint;
6202 +/* upper limit of that data type */
6203 +#define SWD_UINT_MAX USHRT_MAX
6207 +#define LZO_VERSION_DATE "Jul 12 2002"
6208 +#define LZO_VERSION_STRING "1.08"
6209 +#define LZO_VERSION 0x1080
6213 +/* Integral types that have *exactly* the same number of bits as a lzo_voidp */
6214 +typedef unsigned long lzo_ptr_t;
6215 +typedef long lzo_sptr_t;
6218 +/*************************************/
6222 +#define M1_MAX_OFFSET 0x0400
6223 +#define M2_MAX_OFFSET 0x0800
6224 +#define M3_MAX_OFFSET 0x4000
6225 +#define M4_MAX_OFFSET 0xbfff
6227 +#define MX_MAX_OFFSET (M1_MAX_OFFSET + M2_MAX_OFFSET)
6229 +#define M1_MIN_LEN 2
6230 +#define M1_MAX_LEN 2
6231 +#define M2_MIN_LEN 3
6232 +#define M2_MAX_LEN 8
6233 +#define M3_MIN_LEN 3
6234 +#define M3_MAX_LEN 33
6235 +#define M4_MIN_LEN 3
6236 +#define M4_MAX_LEN 9
6238 +#define M1_MARKER 0
6239 +#define M2_MARKER 64
6240 +#define M3_MARKER 32
6241 +#define M4_MARKER 16
6243 +#define MIN_LOOKAHEAD (M2_MAX_LEN + 1)
6247 +#define LZO_BYTE(x) ((unsigned char) ((x) & 0xff))
6249 +#define LZO_MAX(a,b) ((a) >= (b) ? (a) : (b))
6250 +#define LZO_MIN(a,b) ((a) <= (b) ? (a) : (b))
6251 +#define LZO_MAX3(a,b,c) ((a) >= (b) ? LZO_MAX(a,c) : LZO_MAX(b,c))
6252 +#define LZO_MIN3(a,b,c) ((a) <= (b) ? LZO_MIN(a,c) : LZO_MIN(b,c))
6254 +#define lzo_sizeof(type) ((lzo_uint) (sizeof(type)))
6256 +#define LZO_HIGH(array) ((lzo_uint) (sizeof(array)/sizeof(*(array))))
6258 +#define LZO_SIZE(bits) (1u << (bits))
6259 +#define LZO_MASK(bits) (LZO_SIZE(bits) - 1)
6261 +#define LZO_LSIZE(bits) (1ul << (bits))
6262 +#define LZO_LMASK(bits) (LZO_LSIZE(bits) - 1)
6264 +#define LZO_USIZE(bits) ((lzo_uint) 1 << (bits))
6265 +#define LZO_UMASK(bits) (LZO_USIZE(bits) - 1)
6267 +#define LZO_STYPE_MAX(b) (((1l << (8*(b)-2)) - 1l) + (1l << (8*(b)-2)))
6268 +#define LZO_UTYPE_MAX(b) (((1ul << (8*(b)-1)) - 1ul) + (1ul << (8*(b)-1)))
6270 +#define _LZO_STRINGIZE(x) #x
6271 +#define _LZO_MEXPAND(x) _LZO_STRINGIZE(x)
6273 +#define _LZO_CONCAT2(a,b) a ## b
6274 +#define _LZO_CONCAT3(a,b,c) a ## b ## c
6275 +#define _LZO_CONCAT4(a,b,c,d) a ## b ## c ## d
6276 +#define _LZO_CONCAT5(a,b,c,d,e) a ## b ## c ## d ## e
6278 +#define _LZO_ECONCAT2(a,b) _LZO_CONCAT2(a,b)
6279 +#define _LZO_ECONCAT3(a,b,c) _LZO_CONCAT3(a,b,c)
6280 +#define _LZO_ECONCAT4(a,b,c,d) _LZO_CONCAT4(a,b,c,d)
6281 +#define _LZO_ECONCAT5(a,b,c,d,e) _LZO_CONCAT5(a,b,c,d,e)
6283 +#define lzo_dict_t const lzo_bytep
6284 +#define lzo_dict_p lzo_dict_t *
6285 +#define lzo_moff_t lzo_uint
6287 +#define MEMCPY8_DS(dest,src,len) \
6288 + memcpy(dest,src,len); \
6292 +#define MEMCPY_DS(dest,src,len) \
6293 + do *dest++ = *src++; \
6296 +#define MEMMOVE_DS(dest,src,len) \
6297 + do *dest++ = *src++; \
6300 +#define BZERO8_PTR(s,l,n) memset((s),0,(lzo_uint)(l)*(n))
6302 +#define LZO_BASE 65521u
6303 +#define LZO_NMAX 5552
6305 +#define LZO_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
6306 +#define LZO_DO2(buf,i) LZO_DO1(buf,i); LZO_DO1(buf,i+1);
6307 +#define LZO_DO4(buf,i) LZO_DO2(buf,i); LZO_DO2(buf,i+2);
6308 +#define LZO_DO8(buf,i) LZO_DO4(buf,i); LZO_DO4(buf,i+4);
6309 +#define LZO_DO16(buf,i) LZO_DO8(buf,i); LZO_DO8(buf,i+8);
6311 +#define IS_SIGNED(type) (((type) (-1)) < ((type) 0))
6312 +#define IS_UNSIGNED(type) (((type) (-1)) > ((type) 0))
6314 +#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
6317 +#define D_INDEX1(d,p) d = DM((0x21*DX3(p,5,5,6)) >> 5)
6318 +#define D_INDEX2(d,p) d = (d & (D_MASK & 0x7ff)) ^ (D_HIGH | 0x1f)
6320 +#define LZO_HASH LZO_HASH_LZO_INCREMENTAL_B
6322 +#define DL_MIN_LEN M2_MIN_LEN
6324 +#define D_SIZE LZO_SIZE(D_BITS)
6325 +#define D_MASK LZO_MASK(D_BITS)
6327 +#define D_HIGH ((D_MASK >> 1) + 1)
6329 +#define DINDEX1 D_INDEX1
6330 +#define DINDEX2 D_INDEX2
6332 +#define DX2(p,s1,s2) \
6333 + (((((lzo_uint32)((p)[2]) << (s2)) ^ (p)[1]) << (s1)) ^ (p)[0])
6335 +#define DX3(p,s1,s2,s3) ((DX2((p)+1,s2,s3) << (s1)) ^ (p)[0])
6336 +#define DMS(v,s) ((lzo_uint) (((v) & (D_MASK >> (s))) << (s)))
6337 +#define DM(v) DMS(v,0)
6339 +#define DENTRY(p,in) (p)
6340 +#define GINDEX(m_pos,m_off,dict,dindex,in) m_pos = dict[dindex]
6342 +#define LZO_CHECK_MPOS_DET(m_pos,m_off,in,ip,max_offset) \
6343 + (m_pos == NULL || (m_off = (lzo_moff_t) (ip - m_pos)) > max_offset)
6345 +#define LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
6346 + (BOUNDS_CHECKING_OFF_IN_EXPR( \
6347 + (PTR_LT(m_pos,in) || \
6348 + (m_off = (lzo_moff_t) PTR_DIFF(ip,m_pos)) <= 0 || \
6349 + m_off > max_offset) ))
6351 +#define BOUNDS_CHECKING_OFF_IN_EXPR(expr) (expr)
6354 +#define DD_SIZE LZO_SIZE(DD_BITS)
6355 +#define DD_MASK LZO_MASK(DD_BITS)
6357 +#define DL_BITS (D_BITS - DD_BITS)
6358 +#define DL_SIZE LZO_SIZE(DL_BITS)
6359 +#define DL_MASK LZO_MASK(DL_BITS)
6361 +#define UPDATE_D(dict,drun,dv,p,in) dict[ DINDEX(dv,p) ] = DENTRY(p,in)
6362 +#define UPDATE_I(dict,drun,index,p,in) dict[index] = DENTRY(p,in)
6363 +#define UPDATE_P(ptr,drun,p,in) (ptr)[0] = DENTRY(p,in)
6365 +#define __COPY4(dst,src) * (lzo_uint32p)(dst) = * (const lzo_uint32p)(src)
6366 +#define COPY4(dst,src) __COPY4((lzo_ptr_t)(dst),(lzo_ptr_t)(src))
6368 +#define TEST_IP (ip < ip_end)
6369 +#define TEST_OP (op <= op_end)
6371 +#define NEED_IP(x) \
6372 + if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x)) goto input_overrun
6373 +#define NEED_OP(x) \
6374 + if ((lzo_uint)(op_end - op) < (lzo_uint)(x)) goto output_overrun
6375 +#define TEST_LOOKBEHIND(m_pos,out) if (m_pos < out) goto lookbehind_overrun
6379 +#define LZO_UINT_MAX UINT_MAX
6380 +#define N M4_MAX_OFFSET
6381 +#define THRESHOLD 1
6384 +#define SWD_BEST_OFF (LZO_MAX3( M2_MAX_LEN, M3_MAX_LEN, M4_MAX_LEN ) + 1)
6386 +/* ../include/lzoconf.h */
6388 +typedef U32 lzo_uint32;
6389 +typedef I32 lzo_int32;
6390 +typedef U32 lzo_uint;
6391 +typedef I32 lzo_int;
6392 +typedef int lzo_bool;
6394 +#define lzo_byte U8
6395 +#define lzo_bytep U8 *
6396 +#define lzo_charp char *
6397 +#define lzo_voidp void *
6398 +#define lzo_shortp short *
6399 +#define lzo_ushortp unsigned short *
6400 +#define lzo_uint32p lzo_uint32 *
6401 +#define lzo_int32p lzo_int32 *
6402 +#define lzo_uintp lzo_uint *
6403 +#define lzo_intp lzo_int *
6404 +#define lzo_voidpp lzo_voidp *
6405 +#define lzo_bytepp lzo_bytep *
6406 +#define lzo_sizeof_dict_t sizeof(lzo_bytep)
6409 +#define LZO_E_ERROR (-1)
6410 +#define LZO_E_OUT_OF_MEMORY (-2) /* not used right now */
6411 +#define LZO_E_NOT_COMPRESSIBLE (-3) /* not used right now */
6412 +#define LZO_E_INPUT_OVERRUN (-4)
6413 +#define LZO_E_OUTPUT_OVERRUN (-5)
6414 +#define LZO_E_LOOKBEHIND_OVERRUN (-6)
6415 +#define LZO_E_EOF_NOT_FOUND (-7)
6416 +#define LZO_E_INPUT_NOT_CONSUMED (-8)
6418 +#define LZO_PTR_ALIGN_UP(_ptr,_size) \
6419 + ((_ptr) + (lzo_uint) __lzo_align_gap((const lzo_voidp)(_ptr),(lzo_uint)(_size)))
6420 +#define LZO_ALIGN(_ptr,_size) LZO_PTR_ALIGN_UP(_ptr,_size)
6423 + (*lzo_compress_t) (const lzo_byte * src, lzo_uint src_len,
6424 + lzo_byte * dst, lzo_uintp dst_len,
6425 + lzo_voidp wrkmem);
6428 + (*lzo_decompress_t) (const lzo_byte * src, lzo_uint src_len,
6429 + lzo_byte * dst, lzo_uintp dst_len,
6430 + lzo_voidp wrkmem);
6433 + (*lzo_optimize_t) (lzo_byte * src, lzo_uint src_len,
6434 + lzo_byte * dst, lzo_uintp dst_len,
6435 + lzo_voidp wrkmem);
6438 + (*lzo_compress_dict_t) (const lzo_byte * src, lzo_uint src_len,
6439 + lzo_byte * dst, lzo_uintp dst_len,
6441 + const lzo_byte * dict, lzo_uint dict_len);
6444 + (*lzo_decompress_dict_t) (const lzo_byte * src, lzo_uint src_len,
6445 + lzo_byte * dst, lzo_uintp dst_len,
6447 + const lzo_byte * dict, lzo_uint dict_len);
6450 + (*lzo_compress_asm_t) (const lzo_byte * src, lzo_uint src_len,
6451 + lzo_byte * dst, lzo_uintp dst_len,
6452 + lzo_voidp wrkmem);
6455 + (*lzo_decompress_asm_t) (const lzo_byte * src, lzo_uint src_len,
6456 + lzo_byte * dst, lzo_uintp dst_len,
6457 + lzo_voidp wrkmem);
6459 +typedef void (*lzo_progress_callback_t) (lzo_uint, lzo_uint);
6481 +#define LZO1X_1_MEM_COMPRESS ((lzo_uint32) (16384L * lzo_sizeof_dict_t))
6482 +#define LZO1X_999_MEM_COMPRESS ((lzo_uint32) (14 * 16384L * sizeof(short)))
6486 +#define PTR(a) ((lzo_ptr_t) (a))
6487 +#define PTR_LINEAR(a) PTR(a)
6488 +#define PTR_ALIGNED_4(a) ((PTR_LINEAR(a) & 3) == 0)
6489 +#define PTR_ALIGNED_8(a) ((PTR_LINEAR(a) & 7) == 0)
6490 +#define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
6491 +#define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
6492 +#define PTR_LT(a,b) (PTR(a) < PTR(b))
6493 +#define PTR_GE(a,b) (PTR(a) >= PTR(b))
6494 +#define PTR_DIFF(a,b) ((lzo_ptrdiff_t) (PTR(a) - PTR(b)))
6495 +#define pd(a,b) ((lzo_uint) ((a)-(b)))
6497 +typedef ptrdiff_t lzo_ptrdiff_t;
6502 + unsigned char a_uchar;
6504 + unsigned short a_ushort;
6506 + unsigned int a_uint;
6508 + unsigned long a_ulong;
6509 + lzo_int a_lzo_int;
6510 + lzo_uint a_lzo_uint;
6511 + lzo_int32 a_lzo_int32;
6512 + lzo_uint32 a_lzo_uint32;
6513 + ptrdiff_t a_ptrdiff_t;
6514 + lzo_ptrdiff_t a_lzo_ptrdiff_t;
6515 + lzo_ptr_t a_lzo_ptr_t;
6516 + lzo_voidp a_lzo_voidp;
6518 + lzo_bytep a_lzo_bytep;
6519 + lzo_bytepp a_lzo_bytepp;
6520 + lzo_uintp a_lzo_uintp;
6521 + lzo_uint *a_lzo_uint_p;
6522 + lzo_uint32p a_lzo_uint32p;
6523 + lzo_uint32 *a_lzo_uint32_p;
6524 + unsigned char *a_uchar_p;
6540 + lzo_uint last_m_len;
6541 + lzo_uint last_m_off;
6543 + const lzo_byte *bp;
6544 + const lzo_byte *ip;
6545 + const lzo_byte *in;
6546 + const lzo_byte *in_end;
6549 + lzo_progress_callback_t cb;
6551 + lzo_uint textsize;
6552 + lzo_uint codesize;
6553 + lzo_uint printcount;
6555 + unsigned long lit_bytes;
6556 + unsigned long match_bytes;
6557 + unsigned long rep_bytes;
6558 + unsigned long lazy;
6561 + lzo_uint r1_m_len;
6563 + unsigned long m1a_m, m1b_m, m2_m, m3_m, m4_m;
6564 + unsigned long lit1_r, lit2_r, lit3_r;
6568 +#define getbyte(c) ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
6572 +#define SWD_UINT(x) ((swd_uint)(x))
6573 +#define SWD_HSIZE 16384
6574 +#define SWD_MAX_CHAIN 2048
6575 +#define HEAD3(b,p) \
6576 + (((0x9f5f*(((((lzo_uint32)b[p]<<5)^b[p+1])<<5)^b[p+2]))>>5) & (SWD_HSIZE-1))
6577 +#define HEAD2(b,p) (b[p] ^ ((unsigned)b[p+1]<<8))
6578 +#define NIL2 SWD_UINT_MAX
6584 + lzo_uint threshold;
6586 + lzo_uint max_chain;
6587 + lzo_uint nice_length;
6588 + lzo_bool use_best_off;
6589 + lzo_uint lazy_insert;
6596 + lzo_uint best_off[SWD_BEST_OFF];
6601 + lzo_uint best_pos[SWD_BEST_OFF];
6603 + const lzo_byte *dict;
6604 + const lzo_byte *dict_end;
6605 + lzo_uint dict_len;
6612 + unsigned char *b_wrap;
6614 + lzo_uint node_count;
6615 + lzo_uint first_rp;
6617 + unsigned char b[SWD_N + SWD_F + SWD_F];
6618 + swd_uint head3[SWD_HSIZE];
6619 + swd_uint succ3[SWD_N + SWD_F];
6620 + swd_uint best3[SWD_N + SWD_F];
6621 + swd_uint llen3[SWD_HSIZE];
6623 + swd_uint head2[65536L];
6627 +#define s_head3(s,key) s->head3[key]
6628 +#define swd_pos2off(s,pos) \
6629 + (s->bp > (pos) ? s->bp - (pos) : s->b_size - ((pos) - s->bp))
6631 +static __inline__ void
6632 +swd_getbyte (lzo1x_999_swd_t * s)
6636 + if ((c = getbyte (*(s->c))) < 0)
6643 + s->b[s->ip] = LZO_BYTE (c);
6645 + s->b_wrap[s->ip] = LZO_BYTE (c);
6647 + if (++s->ip == s->b_size)
6649 + if (++s->bp == s->b_size)
6651 + if (++s->rp == s->b_size)
6656 +swd_initdict (lzo1x_999_swd_t * s, const lzo_byte * dict, lzo_uint dict_len)
6658 + s->dict = s->dict_end = NULL;
6661 + if (!dict || dict_len <= 0)
6663 + if (dict_len > s->n)
6665 + dict += dict_len - s->n;
6670 + s->dict_len = dict_len;
6671 + s->dict_end = dict + dict_len;
6672 + memcpy (s->b, dict, dict_len);
6677 +swd_insertdict (lzo1x_999_swd_t * s, lzo_uint node, lzo_uint len)
6681 + s->node_count = s->n - len;
6682 + s->first_rp = node;
6686 + key = HEAD3 (s->b, node);
6687 + s->succ3[node] = s_head3 (s, key);
6688 + s->head3[key] = SWD_UINT (node);
6689 + s->best3[node] = SWD_UINT (s->f + 1);
6692 + key = HEAD2 (s->b, node);
6693 + s->head2[key] = SWD_UINT (node);
6700 +swd_init (lzo1x_999_swd_t * s, const lzo_byte * dict, lzo_uint dict_len)
6705 + s->threshold = SWD_THRESHOLD;
6709 + s->max_chain = SWD_MAX_CHAIN;
6710 + s->nice_length = SWD_F;
6711 + s->use_best_off = 0;
6712 + s->lazy_insert = 0;
6714 + s->b_size = s->n + s->f;
6715 + if (2 * s->f >= s->n || s->b_size + s->f >= NIL2)
6716 + return LZO_E_ERROR;
6717 + s->b_wrap = s->b + s->b_size;
6718 + s->node_count = s->n;
6720 + memset (s->llen3, 0, sizeof (s->llen3[0]) * SWD_HSIZE);
6721 + memset (s->head2, 0xff, sizeof (s->head2[0]) * 65536L);
6724 + swd_initdict (s, dict, dict_len);
6726 + s->first_rp = s->ip;
6728 + s->look = (lzo_uint) (s->c->in_end - s->c->ip);
6731 + if (s->look > s->f)
6733 + memcpy (&s->b[s->ip], s->c->ip, s->look);
6734 + s->c->ip += s->look;
6738 + if (s->ip == s->b_size)
6741 + if (s->look >= 2 && s->dict_len > 0)
6742 + swd_insertdict (s, 0, s->dict_len);
6744 + s->rp = s->first_rp;
6745 + if (s->rp >= s->node_count)
6746 + s->rp -= s->node_count;
6748 + s->rp += s->b_size - s->node_count;
6753 +static __inline__ void
6754 +swd_remove_node (lzo1x_999_swd_t * s, lzo_uint node)
6756 + if (s->node_count == 0)
6760 + key = HEAD3 (s->b, node);
6764 + key = HEAD2 (s->b, node);
6766 + if ((lzo_uint) s->head2[key] == node)
6767 + s->head2[key] = NIL2;
6774 +swd_accept (lzo1x_999_swd_t * s, lzo_uint n)
6781 + swd_remove_node (s, s->rp);
6783 + key = HEAD3 (s->b, s->bp);
6784 + s->succ3[s->bp] = s_head3 (s, key);
6785 + s->head3[key] = SWD_UINT (s->bp);
6786 + s->best3[s->bp] = SWD_UINT (s->f + 1);
6789 + key = HEAD2 (s->b, s->bp);
6790 + s->head2[key] = SWD_UINT (s->bp);;
6797 +swd_search (lzo1x_999_swd_t * s, lzo_uint node, lzo_uint cnt)
6799 + const unsigned char *p1;
6800 + const unsigned char *p2;
6801 + const unsigned char *px;
6803 + lzo_uint m_len = s->m_len;
6804 + const unsigned char *b = s->b;
6805 + const unsigned char *bp = s->b + s->bp;
6806 + const unsigned char *bx = s->b + s->bp + s->look;
6807 + unsigned char scan_end1;
6809 + scan_end1 = bp[m_len - 1];
6810 + for (; cnt-- > 0; node = s->succ3[node])
6816 + if (p2[m_len - 1] == scan_end1 &&
6817 + p2[m_len] == p1[m_len] &&
6818 + p2[0] == p1[0] && p2[1] == p1[1])
6827 + while (++p1 < px && *p1 == *++p2);
6831 + if (i < SWD_BEST_OFF)
6833 + if (s->best_pos[i] == 0)
6834 + s->best_pos[i] = node + 1;
6839 + s->m_len = m_len = i;
6841 + if (m_len == s->look)
6843 + if (m_len >= s->nice_length)
6845 + if (m_len > (lzo_uint) s->best3[node])
6847 + scan_end1 = bp[m_len - 1];
6854 +swd_search2 (lzo1x_999_swd_t * s)
6858 + key = s->head2[HEAD2 (s->b, s->bp)];
6862 + if (s->best_pos[2] == 0)
6863 + s->best_pos[2] = key + 1;
6874 +swd_findbest (lzo1x_999_swd_t * s)
6877 + lzo_uint cnt, node;
6880 + key = HEAD3 (s->b, s->bp);
6881 + node = s->succ3[s->bp] = s_head3 (s, key);
6882 + cnt = s->llen3[key]++;
6884 + if (cnt > s->max_chain && s->max_chain > 0)
6885 + cnt = s->max_chain;
6886 + s->head3[key] = SWD_UINT (s->bp);
6888 + s->b_char = s->b[s->bp];
6890 + if (s->m_len >= s->look)
6895 + s->best3[s->bp] = SWD_UINT (s->f + 1);
6900 + if (swd_search2 (s))
6903 + swd_search (s, node, cnt);
6904 + if (s->m_len > len)
6905 + s->m_off = swd_pos2off (s, s->m_pos);
6906 + s->best3[s->bp] = SWD_UINT (s->m_len);
6908 + if (s->use_best_off)
6911 + for (i = 2; i < SWD_BEST_OFF; i++)
6912 + if (s->best_pos[i] > 0)
6918 + s->best_off[i] = 0;
6923 + swd_remove_node (s, s->rp);
6925 + key = HEAD2 (s->b, s->bp);
6926 + s->head2[key] = SWD_UINT (s->bp);
6933 +init_match (lzo1x_999_t * c, lzo1x_999_swd_t * s,
6934 + const lzo_byte * dict, lzo_uint dict_len, lzo_uint32 flags)
6942 + c->last_m_len = c->last_m_off = 0;
6944 + c->textsize = c->codesize = c->printcount = 0;
6945 + c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
6948 + r = swd_init (s, dict, dict_len);
6952 + s->use_best_off = (flags & 1) ? 1 : 0;
6957 +find_match (lzo1x_999_t * c, lzo1x_999_swd_t * s,
6958 + lzo_uint this_len, lzo_uint skip)
6962 + swd_accept (s, this_len - skip);
6963 + c->textsize += this_len - skip + 1;
6967 + c->textsize += this_len - skip;
6973 + if (s->use_best_off)
6974 + memset (s->best_pos, 0, sizeof (s->best_pos));
6977 + c->m_len = s->m_len;
6978 + c->m_off = s->m_off;
6982 + if (s->b_char < 0)
6989 + c->look = s->look + 1;
6991 + c->bp = c->ip - c->look;
6993 + if (c->cb && c->textsize > c->printcount)
6995 + (*c->cb) (c->textsize, c->codesize);
6996 + c->printcount += 1024;
7005 +code_match (lzo1x_999_t * c, lzo_byte * op, lzo_uint m_len, lzo_uint m_off)
7007 + lzo_uint x_len = m_len;
7008 + lzo_uint x_off = m_off;
7010 + c->match_bytes += m_len;
7016 + *op++ = LZO_BYTE (M1_MARKER | ((m_off & 3) << 2));
7017 + *op++ = LZO_BYTE (m_off >> 2);
7022 + else if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
7027 + *op++ = LZO_BYTE (((m_len - 1) << 5) | ((m_off & 7) << 2));
7028 + *op++ = LZO_BYTE (m_off >> 3);
7031 + else if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET
7032 + && c->r1_lit >= 4)
7034 + m_off -= 1 + M2_MAX_OFFSET;
7036 + *op++ = LZO_BYTE (M1_MARKER | ((m_off & 3) << 2));
7037 + *op++ = LZO_BYTE (m_off >> 2);
7041 + else if (m_off <= M3_MAX_OFFSET)
7044 + if (m_len <= M3_MAX_LEN)
7045 + *op++ = LZO_BYTE (M3_MARKER | (m_len - 2));
7048 + m_len -= M3_MAX_LEN;
7049 + *op++ = M3_MARKER | 0;
7050 + while (m_len > 255)
7055 + *op++ = LZO_BYTE (m_len);
7058 + *op++ = LZO_BYTE (m_off << 2);
7059 + *op++ = LZO_BYTE (m_off >> 6);
7068 + k = (m_off & 0x4000) >> 11;
7069 + if (m_len <= M4_MAX_LEN)
7070 + *op++ = LZO_BYTE (M4_MARKER | k | (m_len - 2));
7073 + m_len -= M4_MAX_LEN;
7074 + *op++ = LZO_BYTE (M4_MARKER | k | 0);
7075 + while (m_len > 255)
7080 + *op++ = LZO_BYTE (m_len);
7083 + *op++ = LZO_BYTE (m_off << 2);
7084 + *op++ = LZO_BYTE (m_off >> 6);
7089 + c->last_m_len = x_len;
7090 + c->last_m_off = x_off;
7095 +STORE_RUN (lzo1x_999_t * c, lzo_byte * op, const lzo_byte * ii, lzo_uint t)
7097 + c->lit_bytes += t;
7099 + if (op == c->out && t <= 238)
7101 + *op++ = LZO_BYTE (17 + t);
7105 + op[-2] |= LZO_BYTE (t);
7111 + *op++ = LZO_BYTE (t - 3);
7116 + lzo_uint tt = t - 18;
7124 + *op++ = LZO_BYTE (tt);
7135 +code_run (lzo1x_999_t * c, lzo_byte * op, const lzo_byte * ii,
7136 + lzo_uint lit, lzo_uint m_len)
7140 + op = STORE_RUN (c, op, ii, lit);
7141 + c->r1_m_len = m_len;
7154 +len_of_coded_match (lzo_uint m_len, lzo_uint m_off, lzo_uint lit)
7161 + return (m_off <= M1_MAX_OFFSET && lit > 0
7162 + && lit < 4) ? 2 : -1;
7163 + if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
7165 + if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4)
7167 + if (m_off <= M3_MAX_OFFSET)
7169 + if (m_len <= M3_MAX_LEN)
7171 + m_len -= M3_MAX_LEN;
7172 + while (m_len > 255)
7179 + if (m_off <= M4_MAX_OFFSET)
7181 + if (m_len <= M4_MAX_LEN)
7183 + m_len -= M4_MAX_LEN;
7184 + while (m_len > 255)
7195 +min_gain (lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, int l1, int l2,
7198 + lzo_int lazy_match_min_gain = 0;
7200 + lazy_match_min_gain += ahead;
7203 + lazy_match_min_gain += (lit2 <= 3) ? 0 : 2;
7204 + else if (lit1 <= 18)
7205 + lazy_match_min_gain += (lit2 <= 18) ? 0 : 1;
7207 + lazy_match_min_gain += (l2 - l1) * 2;
7209 + lazy_match_min_gain -= (ahead - l3) * 2;
7211 + if (lazy_match_min_gain < 0)
7212 + lazy_match_min_gain = 0;
7214 + return lazy_match_min_gain;
7218 +better_match (const lzo1x_999_swd_t * swd, lzo_uint * m_len, lzo_uint * m_off)
7220 + if (*m_len <= M2_MIN_LEN)
7223 + if (*m_off <= M2_MAX_OFFSET)
7226 + if (*m_off > M2_MAX_OFFSET &&
7227 + *m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
7228 + swd->best_off[*m_len - 1]
7229 + && swd->best_off[*m_len - 1] <= M2_MAX_OFFSET)
7231 + *m_len = *m_len - 1;
7232 + *m_off = swd->best_off[*m_len];
7236 + if (*m_off > M3_MAX_OFFSET &&
7237 + *m_len >= M4_MAX_LEN + 1 && *m_len <= M2_MAX_LEN + 2 &&
7238 + swd->best_off[*m_len - 2]
7239 + && swd->best_off[*m_len - 2] <= M2_MAX_OFFSET)
7241 + *m_len = *m_len - 2;
7242 + *m_off = swd->best_off[*m_len];
7246 + if (*m_off > M3_MAX_OFFSET &&
7247 + *m_len >= M4_MAX_LEN + 1 && *m_len <= M3_MAX_LEN + 1 &&
7248 + swd->best_off[*m_len - 1]
7249 + && swd->best_off[*m_len - 1] <= M3_MAX_OFFSET)
7251 + *m_len = *m_len - 1;
7252 + *m_off = swd->best_off[*m_len];
7260 +lzo_assert (int expr)
7262 + return (expr) ? 1 : 0;
7268 +lzo1x_999_compress_internal (const lzo_byte * in, lzo_uint in_len,
7269 + lzo_byte * out, lzo_uintp out_len,
7271 + const lzo_byte * dict, lzo_uint dict_len,
7272 + lzo_progress_callback_t cb,
7274 + lzo_uint good_length,
7275 + lzo_uint max_lazy,
7276 + lzo_uint nice_length,
7277 + lzo_uint max_chain, lzo_uint32 flags)
7280 + const lzo_byte *ii;
7282 + lzo_uint m_len, m_off;
7284 + lzo1x_999_t *const c = &cc;
7285 + lzo1x_999_swd_t *const swd = (lzo1x_999_swd_t *) wrkmem;
7289 + (LZO1X_999_MEM_COMPRESS >= lzo_sizeof (lzo1x_999_swd_t)))
7290 + return LZO_E_ERROR;
7295 + if (good_length <= 0)
7298 + if (max_lazy <= 0)
7301 + if (nice_length <= 0)
7304 + if (max_chain <= 0)
7305 + max_chain = SWD_MAX_CHAIN;
7308 + c->ip = c->in = in;
7309 + c->in_end = in + in_len;
7312 + c->m1a_m = c->m1b_m = c->m2_m = c->m3_m = c->m4_m = 0;
7313 + c->lit1_r = c->lit2_r = c->lit3_r = 0;
7318 + c->r1_lit = c->r1_m_len = 0;
7320 + r = init_match (c, swd, dict, dict_len, flags);
7323 + if (max_chain > 0)
7324 + swd->max_chain = max_chain;
7325 + if (nice_length > 0)
7326 + swd->nice_length = nice_length;
7328 + r = find_match (c, swd, 0, 0);
7331 + while (c->look > 0)
7334 + lzo_uint max_ahead;
7337 + c->codesize = op - out;
7347 + && (m_off > M1_MAX_OFFSET || lit == 0 || lit >= 4))
7348 + || (m_len == 2 && op == out) || (op == out && lit == 0))
7353 + else if (m_len == M2_MIN_LEN)
7356 + if (m_off > MX_MAX_OFFSET && lit >= 4)
7364 + swd->max_chain = max_chain;
7365 + r = find_match (c, swd, 1, 0);
7369 + if (swd->use_best_off)
7370 + better_match (swd, &m_len, &m_off);
7373 + if (try_lazy <= 0 || m_len >= max_lazy)
7382 + l1 = len_of_coded_match (m_len, m_off, lit);
7384 + max_ahead = LZO_MIN (try_lazy, l1 - 1);
7388 + while (ahead < max_ahead && c->look > m_len)
7390 + lzo_int lazy_match_min_gain;
7392 + if (m_len >= good_length)
7393 + swd->max_chain = max_chain >> 2;
7395 + swd->max_chain = max_chain;
7396 + r = find_match (c, swd, 1, 0);
7399 + if (c->m_len < m_len)
7402 + if (c->m_len == m_len && c->m_off >= m_off)
7405 + if (swd->use_best_off)
7406 + better_match (swd, &c->m_len, &c->m_off);
7408 + l2 = len_of_coded_match (c->m_len, c->m_off,
7413 + l3 = (op == out) ? -1 : len_of_coded_match (ahead,
7417 + lazy_match_min_gain =
7418 + min_gain (ahead, lit, lit + ahead, l1, l2,
7420 + if (c->m_len >= m_len + lazy_match_min_gain)
7427 + op = code_run (c, op, ii, lit, ahead);
7430 + op = code_match (c, op, ahead, m_off);
7436 + goto lazy_match_done;
7440 + op = code_run (c, op, ii, lit, m_len);
7443 + op = code_match (c, op, m_len, m_off);
7444 + swd->max_chain = max_chain;
7445 + r = find_match (c, swd, m_len, 1 + ahead);
7451 + op = STORE_RUN (c, op, ii, lit);
7453 + *op++ = M4_MARKER | 1;
7457 + c->codesize = op - out;
7459 + *out_len = op - out;
7462 + (*c->cb) (c->textsize, c->codesize);
7468 +lzo1x_999_compress_level (const lzo_byte * in, lzo_uint in_len,
7469 + lzo_byte * out, lzo_uintp out_len,
7471 + const lzo_byte * dict, lzo_uint dict_len,
7472 + lzo_progress_callback_t cb, int compression_level)
7474 + static const struct
7477 + lzo_uint good_length;
7478 + lzo_uint max_lazy;
7479 + lzo_uint nice_length;
7480 + lzo_uint max_chain;
7485 + 0, 0, 0, 8, 4, 0},
7487 + 0, 0, 0, 16, 8, 0},
7489 + 0, 0, 0, 32, 16, 0},
7491 + 1, 4, 4, 16, 16, 0},
7493 + 1, 8, 16, 32, 32, 0},
7495 + 1, 8, 16, 128, 128, 0},
7497 + 2, 8, 32, 128, 256, 0},
7499 + 2, 32, 128, F, 2048, 1},
7501 + 2, F, F, F, 4096, 1}
7504 + if (compression_level < 1 || compression_level > 9)
7505 + return LZO_E_ERROR;
7507 + compression_level -= 1;
7508 + return lzo1x_999_compress_internal (in, in_len, out, out_len, wrkmem,
7509 + dict, dict_len, cb,
7510 + c[compression_level].try_lazy,
7511 + c[compression_level].good_length,
7512 + c[compression_level].max_lazy,
7514 + c[compression_level].max_chain,
7515 + c[compression_level].flags);
7519 +lzo1x_999_compress (const lzo_byte * in, lzo_uint in_len,
7520 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7522 + return lzo1x_999_compress_level (in, in_len, out, out_len, wrkmem,
7528 +static const lzo_byte __lzo_copyright[] = LZO_VERSION_STRING;
7531 +_lzo1x_1_do_compress (const lzo_byte * in, lzo_uint in_len,
7532 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7535 + register const lzo_byte *ip;
7538 + const lzo_byte *const in_end = in + in_len;
7539 + const lzo_byte *const ip_end = in + in_len - 8 - 5;
7540 + const lzo_byte *ii;
7541 + lzo_dict_p const dict = (lzo_dict_p) wrkmem;
7550 + register const lzo_byte *m_pos;
7556 + DINDEX1 (dindex, ip);
7557 + GINDEX (m_pos, m_off, dict, dindex, in);
7558 + if (LZO_CHECK_MPOS_NON_DET
7559 + (m_pos, m_off, in, ip, M4_MAX_OFFSET))
7562 + if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
7564 + DINDEX2 (dindex, ip);
7565 + GINDEX (m_pos, m_off, dict, dindex, in);
7567 + if (LZO_CHECK_MPOS_NON_DET
7568 + (m_pos, m_off, in, ip, M4_MAX_OFFSET))
7570 + if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
7575 + if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
7580 + if (m_pos[2] == ip[2])
7590 + UPDATE_I (dict, 0, dindex, ip, in);
7597 + UPDATE_I (dict, 0, dindex, ip, in);
7599 + if (pd (ip, ii) > 0)
7601 + register lzo_uint t = pd (ip, ii);
7605 + op[-2] |= LZO_BYTE (t);
7608 + *op++ = LZO_BYTE (t - 3);
7611 + register lzo_uint tt = t - 18;
7619 + *op++ = LZO_BYTE (tt);;
7627 + if (m_pos[3] != *ip++ || m_pos[4] != *ip++
7628 + || m_pos[5] != *ip++ || m_pos[6] != *ip++
7629 + || m_pos[7] != *ip++ || m_pos[8] != *ip++)
7634 + if (m_off <= M2_MAX_OFFSET)
7638 + *op++ = LZO_BYTE (((m_len -
7639 + 1) << 5) | ((m_off & 7) <<
7641 + *op++ = LZO_BYTE (m_off >> 3);
7643 + else if (m_off <= M3_MAX_OFFSET)
7646 + *op++ = LZO_BYTE (M3_MARKER | (m_len - 2));
7647 + goto m3_m4_offset;
7654 + *op++ = LZO_BYTE (M4_MARKER |
7655 + ((m_off & 0x4000) >> 11) |
7657 + goto m3_m4_offset;
7663 + const lzo_byte *end = in_end;
7664 + const lzo_byte *m = m_pos + M2_MAX_LEN + 1;
7665 + while (ip < end && *m == *ip)
7667 + m_len = (ip - ii);
7671 + if (m_off <= M3_MAX_OFFSET)
7675 + *op++ = LZO_BYTE (M3_MARKER |
7680 + *op++ = M3_MARKER | 0;
7688 + if (m_len <= M4_MAX_LEN)
7689 + *op++ = LZO_BYTE (M4_MARKER |
7690 + ((m_off & 0x4000) >>
7691 + 11) | (m_len - 2));
7695 + m_len -= M4_MAX_LEN;
7696 + *op++ = LZO_BYTE (M4_MARKER |
7697 + ((m_off & 0x4000) >>
7700 + while (m_len > 255)
7706 + *op++ = LZO_BYTE (m_len);
7711 + *op++ = LZO_BYTE ((m_off & 63) << 2);
7712 + *op++ = LZO_BYTE (m_off >> 6);
7719 + *out_len = op - out;
7720 + return pd (in_end, ii);
7724 +lzo1x_1_compress (const lzo_byte * in, lzo_uint in_len,
7725 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7727 + lzo_byte *op = out;
7730 + if (in_len <= M2_MAX_LEN + 5)
7734 + t = _lzo1x_1_do_compress (in, in_len, op, out_len, wrkmem);
7740 + const lzo_byte *ii = in + in_len - t;
7742 + if (op == out && t <= 238)
7743 + *op++ = LZO_BYTE (17 + t);
7745 + op[-2] |= LZO_BYTE (t);
7747 + *op++ = LZO_BYTE (t - 3);
7750 + lzo_uint tt = t - 18;
7759 + *op++ = LZO_BYTE (tt);
7766 + *op++ = M4_MARKER | 1;
7770 + *out_len = op - out;
7775 +lzo1x_decompress (const lzo_byte * in, lzo_uint in_len,
7776 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7778 + register lzo_byte *op;
7779 + register const lzo_byte *ip;
7780 + register lzo_uint t;
7782 + register const lzo_byte *m_pos;
7784 + const lzo_byte *const ip_end = in + in_len;
7785 + lzo_byte *const op_end = out + *out_len;
7802 + goto first_literal_run;
7805 + while (TEST_IP && TEST_OP)
7823 + if (PTR_ALIGNED2_4 (op, ip))
7861 + first_literal_run:
7867 + m_pos = op - (1 + M2_MAX_OFFSET);
7869 + m_pos -= *ip++ << 2;
7870 + TEST_LOOKBEHIND (m_pos, out);
7878 + while (TEST_IP && TEST_OP)
7884 + m_pos -= (t >> 2) & 7;
7885 + m_pos -= *ip++ << 3;
7887 + TEST_LOOKBEHIND (m_pos, out);
7888 + NEED_OP (t + 3 - 1);
7908 + m_pos -= (ip[0] >> 2) + (ip[1] << 6);
7915 + m_pos -= (t & 8) << 11;
7930 + m_pos -= (ip[0] >> 2) + (ip[1] << 6);
7942 + m_pos -= *ip++ << 2;
7943 + TEST_LOOKBEHIND (m_pos, out);
7951 + TEST_LOOKBEHIND (m_pos, out);
7952 + NEED_OP (t + 3 - 1);
7953 + if (t >= 2 * 4 - (3 - 1)
7954 + && PTR_ALIGNED2_4 (op, m_pos))
7956 + COPY4 (op, m_pos);
7962 + COPY4 (op, m_pos);
7999 + *out_len = op - out;
8000 + return LZO_E_EOF_NOT_FOUND;
8003 + *out_len = op - out;
8004 + return (ip == ip_end ? LZO_E_OK :
8006 + ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
8009 + *out_len = op - out;
8010 + return LZO_E_INPUT_OVERRUN;
8013 + *out_len = op - out;
8014 + return LZO_E_OUTPUT_OVERRUN;
8016 + lookbehind_overrun:
8017 + *out_len = op - out;
8018 + return LZO_E_LOOKBEHIND_OVERRUN;
8023 +#define NO_LIT LZO_UINT_MAX
8026 +copy2 (lzo_byte * ip, const lzo_byte * m_pos, lzo_ptrdiff_t off)
8036 +copy3 (lzo_byte * ip, const lzo_byte * m_pos, lzo_ptrdiff_t off)
8041 + ip[2] = ip[1] = m_pos[0];
8043 + else if (off == 2)
8056 +lzo1x_optimize (lzo_byte * in, lzo_uint in_len,
8057 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
8059 + register lzo_byte *op;
8060 + register lzo_byte *ip;
8061 + register lzo_uint t;
8062 + register lzo_byte *m_pos;
8064 + const lzo_byte *const ip_end = in + in_len;
8065 + const lzo_byte *const op_end = out + *out_len;
8066 + lzo_byte *litp = NULL;
8068 + lzo_uint next_lit = NO_LIT;
8069 + long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
8081 + goto first_literal_run;
8084 + while (TEST_IP && TEST_OP)
8102 + first_literal_run:
8111 + m_pos = op - 1 - 0x800;
8113 + m_pos -= *ip++ << 2;
8120 + while (TEST_IP && TEST_OP)
8126 + m_pos -= *ip++ << 2;
8132 + if (nl == 0 && lit == 1 && ip[0] >= 16)
8136 + *litp = LZO_BYTE ((*litp & ~3) | lit);
8137 + copy2 (ip - 2, m_pos, op - m_pos);
8140 + else if (nl == 0 && ip[0] < 16 && ip[0] != 0
8141 + && (lit + 2 + ip[0] < 16))
8145 + copy2 (ip - 3 + 1, m_pos, op - m_pos);
8148 + memmove (litp + 1, litp, lit);
8150 + *litp = LZO_BYTE (lit - 3);
8155 + goto copy_literal_run;
8167 + m_pos -= (t >> 2) & 7;
8168 + m_pos -= *ip++ << 3;
8174 + if (t == 1 && lit > 3 && nl == 0 &&
8175 + ip[0] < 16 && ip[0] != 0
8176 + && (lit + 3 + ip[0] < 16))
8179 + copy3 (ip - 1 - 2, m_pos,
8182 + *litp = LZO_BYTE (lit - 3);
8187 + goto copy_literal_run;
8204 + m_pos -= *ip++ >> 2;
8205 + m_pos -= *ip++ << 6;
8210 + m_pos -= (t & 8) << 11;
8220 + m_pos -= *ip++ >> 2;
8221 + m_pos -= *ip++ << 6;
8230 + if (t == 1 && lit == 0 && nl == 0
8235 + *litp = LZO_BYTE ((*litp & ~3)
8237 + copy3 (ip - 3, m_pos,
8241 + else if (t == 1 && lit <= 3 && nl == 0
8242 + && ip[0] < 16 && ip[0] != 0
8243 + && (lit + 3 + ip[0] < 16))
8247 + copy3 (ip - 4 + 1, m_pos,
8251 + memmove (litp + 1,
8254 + *litp = LZO_BYTE (lit - 3);
8260 + goto copy_literal_run;
8272 + if (next_lit == NO_LIT)
8280 + next_lit = NO_LIT;
8291 + *out_len = op - out;
8292 + return LZO_E_EOF_NOT_FOUND;
8295 + *out_len = op - out;
8296 + return (ip == ip_end ? LZO_E_OK :
8298 + ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
8301 +/* interface to jffs2 bbc follows */
8303 +#include "jffs2_bbc_framework.h"
8305 +#define BLOCKSIZE 4096
8306 +#define OUTBLOCKSIZE (BLOCKSIZE + BLOCKSIZE / 64 + 16 + 3)
8308 +#define JFFS2_BBC_LZO_BLOCK_SIGN {0x3f, 0x47, 0x5a, 0x18}
8311 +jffs2_bbc_lzo_compressor_init (void);
8314 +jffs2_bbc_lzo_compressor_deinit (void);
8317 +jffs2_bbc_lzo_compress (void *model, unsigned char *input,
8318 + unsigned char *output, unsigned long *sourcelen,
8319 + unsigned long *dstlen);
8322 +jffs2_bbc_lzo_estimate (void *model, unsigned char *input,
8323 + unsigned long sourcelen, unsigned long *dstlen,
8324 + unsigned long *readtime, unsigned long *writetime);
8327 +jffs2_bbc_lzo_decompress (void *model, unsigned char *input,
8328 + unsigned char *output, unsigned long sourcelen,
8329 + unsigned long dstlen);
8332 +jffs2_bbc_lzo_proc_info (void);
8335 +jffs2_bbc_lzo_proc_command (char *command);
8337 +struct jffs2_bbc_compressor_type jffs2_bbc_lzo = {
8340 + JFFS2_BBC_LZO_BLOCK_SIGN,
8341 + jffs2_bbc_lzo_compressor_init,
8344 + jffs2_bbc_lzo_compressor_deinit,
8345 + jffs2_bbc_lzo_compress,
8346 + jffs2_bbc_lzo_estimate,
8347 + jffs2_bbc_lzo_decompress,
8348 + jffs2_bbc_lzo_proc_info,
8349 + jffs2_bbc_lzo_proc_command
8353 +no_lzo1x_optimize (lzo_byte * src, lzo_uint src_len,
8354 + lzo_byte * dst, lzo_uintp dst_len, lzo_voidp wrkmem)
8360 +static lzo_compress_t lzo1x_compressor = lzo1x_1_compress;
8361 +static lzo_optimize_t lzo1x_optimizer = no_lzo1x_optimize;
8362 +static int lzo1x_compressor_type = 1;
8363 +static int lzo1x_optimize_type = 0;
8364 +static unsigned long lzo1x_compressor_memsize = LZO1X_1_MEM_COMPRESS;
8366 +static lzo_compress_t lzo1x_compressor = lzo1x_999_compress;
8367 +static lzo_optimize_t lzo1x_optimizer = lzo1x_optimize;
8368 +static int lzo1x_compressor_type = 999;
8369 +static int lzo1x_optimize_type = 1;
8370 +static unsigned long lzo1x_compressor_memsize = LZO1X_999_MEM_COMPRESS;
8373 +static lzo_bytep wrkmem = NULL; /* temporary buffer for compression, used by lzo */
8374 +static lzo_bytep cmprssmem = NULL; /* temporary buffer for compression, used by interface */
8377 +jffs2_bbc_lzo_compressor_init (void)
8379 + wrkmem = (lzo_bytep) jffs2_bbc_malloc (lzo1x_compressor_memsize);
8380 + cmprssmem = (lzo_bytep) jffs2_bbc_malloc (OUTBLOCKSIZE);
8381 + return !(wrkmem && cmprssmem);
8385 +jffs2_bbc_lzo_compressor_deinit (void)
8387 + jffs2_bbc_free (wrkmem);
8388 + jffs2_bbc_free (cmprssmem);
8392 +jffs2_bbc_lzo_compress (void *model, unsigned char *input,
8393 + unsigned char *output, unsigned long *sourcelen,
8394 + unsigned long *dstlen)
8396 + lzo_uint csize = OUTBLOCKSIZE;
8397 + lzo_uint isize = *sourcelen;
8400 + lzo1x_compressor (input, *sourcelen, cmprssmem, &csize,
8401 + wrkmem)) != LZO_E_OK)
8403 + *sourcelen = *dstlen = 0;
8408 + retval = lzo1x_optimizer (cmprssmem, csize, input, &isize,
8411 + if (csize <= *dstlen) {
8413 + *(output++) = jffs2_bbc_lzo.block_sign[0];
8414 + *(output++) = jffs2_bbc_lzo.block_sign[1];
8415 + memcpy (output, cmprssmem, csize - 2);
8418 + *sourcelen = *dstlen = 0;
8425 +jffs2_bbc_lzo_estimate (void *model, unsigned char *input,
8426 + unsigned long sourcelen, unsigned long *dstlen,
8427 + unsigned long *readtime, unsigned long *writetime)
8429 + *dstlen = sourcelen * 55 / 100;
8430 + *readtime = JFFS2_BBC_ZLIB_READ_TIME / 2;
8431 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 8 / 10; /* LZO1X-1 is much-much faster,
8432 + but LZO1X-999 is slow. The default mode for inside kernel compression is LZO1X-1
8433 + This should be *0.4 really */
8438 +jffs2_bbc_lzo_decompress (void *model, unsigned char *input,
8439 + unsigned char *output, unsigned long sourcelen,
8440 + unsigned long dstlen)
8442 + lzo_uint outlen = dstlen;
8443 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzo.block_sign[0] ) ||
8444 + ( *(input++) != (unsigned char)jffs2_bbc_lzo.block_sign[1] )
8448 + return lzo1x_decompress (input, sourcelen - 2, output, &outlen, NULL);
8453 +jffs2_bbc_lzo_proc_info (void)
8455 + if (lzo1x_compressor_type == 1)
8457 + if (lzo1x_optimize_type == 1)
8459 + return "LZO1X-1 compression with optimization";
8463 + return "LZO1X-1 compression without optimization";
8466 + else if (lzo1x_compressor_type == 999)
8468 + if (lzo1x_optimize_type == 1)
8470 + return "LZO1X-999 compression with optimization";
8474 + return "LZO1X-999 compression without optimization";
8479 + return "Unknown configuration!";
8484 +jffs2_bbc_lzo_proc_command (char *command)
8489 + /* switch optimization off */
8490 + lzo1x_optimizer = no_lzo1x_optimize;
8491 + lzo1x_optimize_type = 0;
8492 + jffs2_bbc_print1 ("Compression optimization switched off.\n");
8495 + /* switch optimization on */
8496 + lzo1x_optimizer = lzo1x_optimize;
8497 + lzo1x_optimize_type = 1;
8498 + jffs2_bbc_print1 ("Compression optimization switched on.\n");
8501 + /* switch compression to LZO1X-1 */
8502 + jffs2_bbc_free (wrkmem);
8503 + lzo1x_compressor_type = 1;
8504 + lzo1x_compressor = lzo1x_1_compress;
8505 + lzo1x_compressor_memsize = LZO1X_1_MEM_COMPRESS;
8506 + wrkmem = (lzo_bytep)
8507 + jffs2_bbc_malloc (lzo1x_compressor_memsize);
8508 + jffs2_bbc_print1 ("Compression type switched to LZO1X-1.\n");
8511 + /* switch compression to LZO1X-999 */
8512 + jffs2_bbc_free (wrkmem);
8513 + lzo1x_compressor_type = 999;
8514 + lzo1x_compressor = lzo1x_999_compress;
8515 + lzo1x_compressor_memsize = LZO1X_999_MEM_COMPRESS;
8516 + wrkmem = (lzo_bytep)
8517 + jffs2_bbc_malloc (lzo1x_compressor_memsize);
8519 + ("Compression type switched to LZO1X-999.\n");
8522 + jffs2_bbc_print1 ("Unknown command!\n");
8528 +struct jffs2_bbc_compressor_type *
8529 +jffs2_bbc_lzo_init (int mode)
8531 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzo) == 0)
8533 + return &jffs2_bbc_lzo;
8542 +jffs2_bbc_lzo_deinit (void)
8544 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzo);
8547 +++ b/fs/jffs2/jffs2_bbc_lzss_comp.c
8549 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
8552 + jffs2_bbc_lzss_comp.c -- Lempel-Ziv-Storer-Szymanski compression module for jffs2
8553 + Copyright (C) 2004 Patrik Kluba
8554 + Based on the LZSS source included in LDS (lossless datacompression sources)
8555 + Block-compression modifications by Patrik Kluba
8556 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
8560 +Original copyright follows:
8562 +**************************************************************
8563 + LZSS.C -- A Data Compression Program
8564 +**************************************************************
8565 + 4/6/1989 Haruhiko Okumura
8566 + Use, distribute, and modify this program freely.
8567 + Please send me your improved versions.
8569 + NIFTY-Serve PAF01022
8570 + CompuServe 74050,1022
8571 +**************************************************************
8577 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
8584 +#define N 4096 /* size of ring buffer */
8585 +#define F 18 /* upper limit for match_length */
8586 +#define THRESHOLD 2 /* encode string into position and length
8587 + if match_length is greater than this */
8588 +#define NIL N /* index for root of binary search trees */
8590 +static unsigned char
8591 + text_buf[N + F - 1]; /* ring buffer of size N,
8592 + with extra F-1 bytes to facilitate string comparison */
8593 +static unsigned long match_position, match_length; /* of longest match. These are
8594 + set by the InsertNode() procedure. */
8595 +static unsigned long lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
8596 + parents -- These constitute binary search trees. */
8598 +static void InitTree(void) /* initialize trees */
8602 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
8603 + left children of node i. These nodes need not be initialized.
8604 + Also, dad[i] is the parent of node i. These are initialized to
8605 + NIL (= N), which stands for 'not used.'
8606 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
8607 + for strings that begin with character i. These are initialized
8608 + to NIL. Note there are 256 trees. */
8610 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
8611 + for (i = 0; i < N; i++) dad[i] = NIL;
8614 +static void InsertNode(unsigned long r)
8615 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
8616 + trees (text_buf[r]'th tree) and returns the longest-match position
8617 + and length via the global variables match_position and match_length.
8618 + If match_length = F, then removes the old node in favor of the new
8619 + one, because the old one will be deleted sooner.
8620 + Note r plays double role, as tree node and position in buffer. */
8622 + unsigned long i, p;
8623 + unsigned char *key;
8626 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
8627 + rson[r] = lson[r] = NIL; match_length = 0;
8630 + if (rson[p] != NIL) p = rson[p];
8631 + else { rson[p] = r; dad[r] = p; return; }
8633 + if (lson[p] != NIL) p = lson[p];
8634 + else { lson[p] = r; dad[r] = p; return; }
8636 + for (i = 1; i < F; i++)
8637 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
8638 + if (i > match_length) {
8639 + match_position = p;
8640 + if ((match_length = i) >= F) break;
8643 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
8644 + dad[lson[p]] = r; dad[rson[p]] = r;
8645 + if (rson[dad[p]] == p) rson[dad[p]] = r;
8646 + else lson[dad[p]] = r;
8647 + dad[p] = NIL; /* remove p */
8650 +static void DeleteNode(unsigned long p) /* deletes node p from tree */
8654 + if (dad[p] == NIL) return; /* not in tree */
8655 + if (rson[p] == NIL) q = lson[p];
8656 + else if (lson[p] == NIL) q = rson[p];
8659 + if (rson[q] != NIL) {
8660 + do { q = rson[q]; } while (rson[q] != NIL);
8661 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
8662 + lson[q] = lson[p]; dad[lson[p]] = q;
8664 + rson[q] = rson[p]; dad[rson[p]] = q;
8667 + if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
8671 +/* modified for block compression */
8672 +/* on return, srclen will contain the number of successfully compressed bytes
8673 + and dstlen will contain completed compressed bytes */
8675 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
8676 + unsigned long *dstlen)
8678 + unsigned long i, len, r, c, s, last_match_length, code_buf_ptr;
8679 + unsigned char code_buf[17], mask;
8680 + unsigned char *ip, *op;
8681 + unsigned long written = 0;
8682 + unsigned long read = 0;
8683 + unsigned char *srcend = srcbuf + *srclen;
8684 + unsigned char *dstend = dstbuf + *dstlen;
8687 + InitTree(); /* initialize trees */
8688 + code_buf[0] = 0; /* code_buf[1..16] saves eight units of code, and
8689 + code_buf[0] works as eight flags, "1" representing that the unit
8690 + is an unencoded letter (1 byte), "0" a position-and-length pair
8691 + (2 bytes). Thus, eight units require at most 16 bytes of code. */
8692 + code_buf_ptr = mask = 1;
8694 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
8695 + any character that will appear often. */
8696 + for (len = 0; (len < F) && (ip < srcend); len++)
8697 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
8700 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
8701 + each of which begins with one or more 'space' characters. Note
8702 + the order in which these strings are inserted. This way,
8703 + degenerate trees will be less likely to occur. */
8704 + InsertNode(r); /* Finally, insert the whole string just read. The
8705 + global variables match_length and match_position are set. */
8707 + if (match_length > len) match_length = len; /* match_length
8708 + may be spuriously long near the end of text. */
8709 + if (match_length <= THRESHOLD) {
8710 + match_length = 1; /* Not long enough match. Send one byte. */
8711 + code_buf[0] |= mask; /* 'send one byte' flag */
8712 + code_buf[code_buf_ptr++] = text_buf[r]; /* Send uncoded. */
8714 + code_buf[code_buf_ptr++] = match_position;
8715 + code_buf[code_buf_ptr++] = (((match_position >> 4) & 0xf0)
8716 + | (match_length - (THRESHOLD + 1))); /* Send position and
8717 + length pair. Note match_length > THRESHOLD. */
8719 + if ((mask <<= 1) == 0) { /* Shift mask left one bit. */
8720 + if ((op + code_buf_ptr) > dstend) {
8721 + *dstlen = written; /* written contains bytes of complete compressed
8725 + for (i = 0; i < code_buf_ptr; *(op++) = code_buf[i++]); /* Send at most 8 units of */
8726 + /* code together */
8727 + written += code_buf_ptr;
8728 + *srclen = read; /* this many bytes have been successfully compressed */
8729 + code_buf[0] = 0; code_buf_ptr = mask = 1;
8731 + last_match_length = match_length;
8732 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
8734 + DeleteNode(s); /* Delete old strings and */
8735 + text_buf[s] = c; /* read new bytes */
8736 + if (s < F - 1) text_buf[s + N] = c; /* If the position is
8737 + near the end of buffer, extend the buffer to make
8738 + string comparison easier. */
8739 + s = (s + 1) & (N - 1); r = (r + 1) & (N - 1);
8740 + /* Since this is a ring buffer, increment the position
8742 + InsertNode(r); /* Register the string in text_buf[r..r+F-1] */
8745 + while (i++ < last_match_length) { /* After the end of text, */
8746 + DeleteNode(s); /* no need to read, but */
8747 + s = (s + 1) & (N - 1); r = (r + 1) & (N - 1);
8748 + if (--len) InsertNode(r); /* buffer may not be empty. */
8750 + } while (len > 0); /* until length of string to be processed is zero */
8751 + if (code_buf_ptr > 1) { /* Send remaining code. */
8752 + if ((op + code_buf_ptr) > dstend) {
8753 + *dstlen = written;
8756 + for (i = 0; i < code_buf_ptr; *(op++) = code_buf[i++]);
8757 + written += code_buf_ptr;
8760 + *dstlen = written;
8764 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
8765 + unsigned long dstlen) /* Just the reverse of Encode(). */
8767 + unsigned long i, r, c, j, k, flags;
8768 + unsigned char *ip, *op;
8769 + unsigned long written;
8770 + unsigned long read;
8771 + unsigned char *srcend = srcbuf + srclen;
8772 + unsigned char *dstend = dstbuf + dstlen;
8773 + read = written = 0;
8776 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
8777 + r = N - F; flags = 0;
8779 + if (((flags >>= 1) & 256) == 0) {
8780 + if (ip >= srcend) return 0;
8782 + flags = c | 0xff00; /* uses higher byte cleverly */
8783 + } /* to count eight */
8785 + if (ip >= srcend) return 0;
8787 + if (op >= dstend) return -1;
8788 + *(op++) = text_buf[r++] = c; r &= (N - 1);
8790 + if ((ip + 2) > srcend) return 0;
8793 + i |= ((j & 0xf0) << 4); j = (j & 0x0f) + THRESHOLD;
8794 + if ((op + j + 1) > dstend) return -1;
8795 + for (k = 0; k <= j; k++) {
8796 + c = text_buf[(i + k) & (N - 1)];
8797 + *(op++) = text_buf[r++] = c; r &= (N - 1);
8803 +/* interface to jffs2 bbc follows */
8805 +#include "jffs2_bbc_framework.h"
8808 +#define JFFS2_BBC_LZSS_BLOCK_SIGN {0x27, 0x6f, 0x12, 0xc4}
8811 +jffs2_bbc_lzss_compressor_init (void);
8814 +jffs2_bbc_lzss_compressor_deinit (void);
8817 +jffs2_bbc_lzss_compress (void *model, unsigned char *input,
8818 + unsigned char *output, unsigned long *sourcelen,
8819 + unsigned long *dstlen);
8822 +jffs2_bbc_lzss_estimate (void *model, unsigned char *input,
8823 + unsigned long sourcelen, unsigned long *dstlen,
8824 + unsigned long *readtime, unsigned long *writetime);
8827 +jffs2_bbc_lzss_decompress (void *model, unsigned char *input,
8828 + unsigned char *output, unsigned long sourcelen,
8829 + unsigned long dstlen);
8832 +jffs2_bbc_lzss_proc_info (void);
8835 +jffs2_bbc_lzss_proc_command (char *command);
8837 +struct jffs2_bbc_compressor_type jffs2_bbc_lzss = {
8840 + JFFS2_BBC_LZSS_BLOCK_SIGN,
8841 + jffs2_bbc_lzss_compressor_init,
8844 + jffs2_bbc_lzss_compressor_deinit,
8845 + jffs2_bbc_lzss_compress,
8846 + jffs2_bbc_lzss_estimate,
8847 + jffs2_bbc_lzss_decompress,
8848 + jffs2_bbc_lzss_proc_info,
8849 + jffs2_bbc_lzss_proc_command
8853 +jffs2_bbc_lzss_compressor_init (void)
8859 +jffs2_bbc_lzss_compressor_deinit (void)
8864 +jffs2_bbc_lzss_compress (void *model, unsigned char *input,
8865 + unsigned char *output, unsigned long *sourcelen,
8866 + unsigned long *dstlen)
8869 + unsigned long dst = *dstlen;
8870 + *(output++) = jffs2_bbc_lzss.block_sign[0];
8871 + *(output++) = jffs2_bbc_lzss.block_sign[1];
8873 + retval = Encode(input, output, sourcelen, &dst);
8880 +jffs2_bbc_lzss_estimate (void *model, unsigned char *input,
8881 + unsigned long sourcelen, unsigned long *dstlen,
8882 + unsigned long *readtime, unsigned long *writetime)
8884 + *dstlen = sourcelen * 60 / 100;
8885 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 12 / 10;
8886 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 3;
8891 +jffs2_bbc_lzss_decompress (void *model, unsigned char *input,
8892 + unsigned char *output, unsigned long sourcelen,
8893 + unsigned long dstlen)
8895 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzss.block_sign[0] ) ||
8896 + ( *(input++) != (unsigned char)jffs2_bbc_lzss.block_sign[1] )
8900 + return Decode(input, output, sourcelen - 2, dstlen);
8905 +jffs2_bbc_lzss_proc_info (void)
8907 + return "Lempel-Ziv-Storer-Szymanski compression module";
8911 +jffs2_bbc_lzss_proc_command (char *command)
8916 +struct jffs2_bbc_compressor_type *
8917 +jffs2_bbc_lzss_init (int mode)
8919 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzss) == 0)
8921 + return &jffs2_bbc_lzss;
8930 +jffs2_bbc_lzss_deinit (void)
8932 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzss);
8935 +++ b/fs/jffs2/linux-2.4.25.hpatch
8944 ++ $(JFFS2_BBC_KERNEL_OBJS) \
8948 +-tristate 'Compressed ROM file system support' CONFIG_CRAMFS
8952 +F../../Documentation/Configure.help
8954 +-JFFS stats available
8955 +iConfigure.help.bbc.inc
8959 +=(de)compress->(de)compress2
8960 +-int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
8961 ++int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out,
8962 +-void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
8963 ++void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out,
8964 +?inflateEnd(&strm);
8967 ++extern int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 * sourcelen, __u32 * dstlen);
8968 ++extern void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
8970 ++int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
8971 ++ __u32 *sourcelen, __u32 *dstlen)
8973 ++ return jffs2_zlib_compress(data_in,cpage_out,sourcelen,dstlen);
8976 ++void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
8977 ++ __u32 srclen, __u32 destlen)
8979 ++ jffs2_zlib_decompress(data_in,cpage_out,srclen,destlen);
8984 +=set_act_sb before write
8986 ++#include "jffs2_bbc_framework.h" /**BBC**/
8988 +?int jffs2_commit_write
8990 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
8994 +=set_act_sb before write
8996 ++#include "jffs2_bbc_framework.h" /**BBC**/
8998 +?int jffs2_garbage_collect_dnode(
9000 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9004 +=set_act_sb before read
9006 ++#include "jffs2_bbc_framework.h" /**BBC**/
9008 +?int jffs2_read_dnode(
9010 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9016 ++#include "jffs2_bbc_fs.h" /**BBC**/
9018 +?struct super_block *jffs2_read_super(
9020 ++ jffs2_bbc_load_model(sb); /**BBC**/
9022 +?void jffs2_put_super
9024 ++ jffs2_bbc_unload_model(sb); /**BBC**/
9025 +?init_jffs2_fs(void)
9028 ++ jffs2_bbc_proc_init(); /**BBC**/
9030 +?exit_jffs2_fs(void)
9032 ++ jffs2_bbc_proc_deinit(); /**BBC**/
9034 --- a/fs/jffs2/read.c
9035 +++ b/fs/jffs2/read.c
9040 +#include "jffs2_bbc_framework.h" /**BBC**/
9041 #include <linux/kernel.h>
9042 #include <linux/slab.h>
9043 #include <linux/jffs2.h>
9044 @@ -140,6 +141,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
9045 D2(printk(KERN_DEBUG "Data CRC matches calculated CRC %08x\n", crc));
9046 if (ri->compr != JFFS2_COMPR_NONE) {
9047 D2(printk(KERN_DEBUG "Decompress %d bytes from %p to %d bytes at %p\n", ri->csize, readbuf, ri->dsize, decomprbuf));
9048 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
9049 ret = jffs2_decompress(ri->compr, readbuf, decomprbuf, ri->csize, ri->dsize);
9051 printk(KERN_WARNING "Error: jffs2_decompress returned %d\n", ret);
9052 --- a/fs/jffs2/super.c
9053 +++ b/fs/jffs2/super.c
9058 +#include "jffs2_bbc_fs.h" /**BBC**/
9059 #include <linux/config.h>
9060 #include <linux/kernel.h>
9061 #include <linux/module.h>
9062 @@ -272,6 +273,7 @@ static struct super_block *jffs2_read_su
9063 sb->s_magic = JFFS2_SUPER_MAGIC;
9064 if (!(sb->s_flags & MS_RDONLY))
9065 jffs2_start_garbage_collect_thread(c);
9066 + jffs2_bbc_load_model(sb); /**BBC**/
9070 @@ -288,6 +290,7 @@ static struct super_block *jffs2_read_su
9071 void jffs2_put_super (struct super_block *sb)
9073 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
9074 + jffs2_bbc_unload_model(sb); /**BBC**/
9076 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
9078 @@ -344,6 +347,9 @@ static int __init init_jffs2_fs(void)
9082 + jffs2_bbc_proc_init(); /**BBC**/
9085 printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
9087 #ifdef JFFS2_OUT_OF_KERNEL
9088 @@ -388,6 +394,8 @@ static int __init init_jffs2_fs(void)
9090 static void __exit exit_jffs2_fs(void)
9092 + jffs2_bbc_proc_deinit(); /**BBC**/
9094 jffs2_destroy_slab_caches();
9096 unregister_filesystem(&jffs2_fs_type);