1 Index: linux-2.4.35.4/Documentation/Configure.help
2 ===================================================================
3 --- linux-2.4.35.4.orig/Documentation/Configure.help
4 +++ linux-2.4.35.4/Documentation/Configure.help
5 @@ -17580,6 +17580,32 @@ CONFIG_JFFS2_FS_DEBUG
6 If reporting bugs, please try to have available a full dump of the
7 messages at debug level 1 while the misbehaviour was occurring.
9 +ARMLIB compression support for BBC (EXPERIMENTAL)
10 +CONFIG_JFFS2_BBC_ARMLIB
11 + This enables ARMLIB support for BBC.
13 +LZO1X-* compression support for BBC (EXPERIMENTAL)
15 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
16 + compressor, beats ZLIB in everything)
18 +LZARI compression support for BBC (EXPERIMENTAL)
19 +CONFIG_JFFS2_BBC_LZARI
20 + This enables LempelZiv-Storer-Szymanski compression for BBC with
21 + additional arithmetic coding (damn slow, but best compresor).
23 +LZHD compression support for BBC (EXPERIMENTAL)
24 +CONFIG_JFFS2_BBC_LZHD
25 + This enables LempelZiv-Storer-Szymanski compression for BBC with
26 + additional dynamic Huffman coding (a little faster than LZARI, and
27 + it's compression ratio is a little worse than LZARI's)
29 +LZSS compression support for BBC (EXPERIMENTAL)
30 +CONFIG_JFFS2_BBC_LZSS
31 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
32 + (faster than LZHD, and, and has a not-so-good compression ratio,
33 + was included just for testing)
35 JFFS stats available in /proc filesystem
37 Enabling this option will cause statistics from mounted JFFS file systems
38 Index: linux-2.4.35.4/fs/Config.in
39 ===================================================================
40 --- linux-2.4.35.4.orig/fs/Config.in
41 +++ linux-2.4.35.4/fs/Config.in
42 @@ -50,6 +50,12 @@ dep_tristate 'Journalling Flash File Sys
43 if [ "$CONFIG_JFFS2_FS" = "y" -o "$CONFIG_JFFS2_FS" = "m" ] ; then
44 int 'JFFS2 debugging verbosity (0 = quiet, 2 = noisy)' CONFIG_JFFS2_FS_DEBUG 0
46 +dep_mbool ' ARMLIB compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_ARMLIB $CONFIG_JFFS2_FS
47 +dep_mbool ' LZO1X-* compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZO $CONFIG_JFFS2_FS
48 +dep_mbool ' LZARI compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZARI $CONFIG_JFFS2_FS
49 +dep_mbool ' LZHD compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZHD $CONFIG_JFFS2_FS
50 +dep_mbool ' LZSS compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZSS $CONFIG_JFFS2_FS
52 tristate 'Compressed ROM file system support' CONFIG_CRAMFS
53 tristate 'Squashed file system support' CONFIG_SQUASHFS
54 if [ "$CONFIG_SQUASHFS" = "y" -o "$CONFIG_SQUASHFS" = "m" ] ; then
55 Index: linux-2.4.35.4/fs/jffs2/Config.in.bbc.inc
56 ===================================================================
58 +++ linux-2.4.35.4/fs/jffs2/Config.in.bbc.inc
60 +dep_mbool ' ARMLIB compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_ARMLIB $CONFIG_JFFS2_FS
61 +dep_mbool ' LZO1X-* compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZO $CONFIG_JFFS2_FS
62 +dep_mbool ' LZARI compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZARI $CONFIG_JFFS2_FS
63 +dep_mbool ' LZHD compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZHD $CONFIG_JFFS2_FS
64 +dep_mbool ' LZSS compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZSS $CONFIG_JFFS2_FS
65 Index: linux-2.4.35.4/fs/jffs2/Configure.help.bbc.inc
66 ===================================================================
68 +++ linux-2.4.35.4/fs/jffs2/Configure.help.bbc.inc
70 +ARMLIB compression support for BBC (EXPERIMENTAL)
71 +CONFIG_JFFS2_BBC_ARMLIB
72 + This enables ARMLIB support for BBC.
74 +LZO1X-* compression support for BBC (EXPERIMENTAL)
76 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
77 + compressor, beats ZLIB in everything)
79 +LZARI compression support for BBC (EXPERIMENTAL)
80 +CONFIG_JFFS2_BBC_LZARI
81 + This enables LempelZiv-Storer-Szymanski compression for BBC with
82 + additional arithmetic coding (damn slow, but best compresor).
84 +LZHD compression support for BBC (EXPERIMENTAL)
85 +CONFIG_JFFS2_BBC_LZHD
86 + This enables LempelZiv-Storer-Szymanski compression for BBC with
87 + additional dynamic Huffman coding (a little faster than LZARI, and
88 + it's compression ratio is a little worse than LZARI's)
90 +LZSS compression support for BBC (EXPERIMENTAL)
91 +CONFIG_JFFS2_BBC_LZSS
92 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
93 + (faster than LZHD, and, and has a not-so-good compression ratio,
94 + was included just for testing)
95 Index: linux-2.4.35.4/fs/jffs2/Kconfig.bbc.inc
96 ===================================================================
98 +++ linux-2.4.35.4/fs/jffs2/Kconfig.bbc.inc
100 +config JFFS2_BBC_ARMLIB
101 + bool "ARMLIB compression support for BBC (EXPERIMENTAL)"
102 + depends on JFFS2_FS && EXPERIMENTAL
105 + This enables ARMLIB support for BBC.
107 +config JFFS2_BBC_LZO
108 + bool "LZO1X-* compression support for BBC (EXPERIMENTAL)"
109 + depends on JFFS2_FS && EXPERIMENTAL
112 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
115 +config JFFS2_BBC_LZARI
116 + bool "LZARI compression support for BBC (EXPERIMENTAL)"
117 + depends on JFFS2_FS && EXPERIMENTAL
120 + This enables LempelZiv-Storer-Szymanski compression for BBC with
121 + additional arithmetic coding (damn slow, but best compresor).
123 +config JFFS2_BBC_LZHD
124 + bool "LZHD compression support for BBC (EXPERIMENTAL)"
125 + depends on JFFS2_FS && EXPERIMENTAL
128 + This enables LempelZiv-Storer-Szymanski compression for BBC with
129 + additional dynamic Huffman coding (a little faster than LZARI, and
130 + it's compression ratio is a little worse than LZARI's)
132 +config JFFS2_BBC_LZSS
133 + bool "LZSS compression support for BBC (EXPERIMENTAL)"
134 + depends on JFFS2_FS && EXPERIMENTAL
137 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
138 + (faster than LZHD, and, and has a not-so-good compression ratio,
139 + was included just for testing)
140 Index: linux-2.4.35.4/fs/jffs2/Makefile
141 ===================================================================
142 --- linux-2.4.35.4.orig/fs/jffs2/Makefile
143 +++ linux-2.4.35.4/fs/jffs2/Makefile
145 # Note 2! The CFLAGS definitions are now in the main makefile...
148 +JFFS2_BBC_KERNEL_OBJS-y = jffs2_bbc_framework.o jffs2_bbc_fs.o
150 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_ARMLIB) += jffs2_bbc_armlib_comp.o
151 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZO) += jffs2_bbc_lzo_comp.o
152 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZSS) += jffs2_bbc_lzss_comp.o
153 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZARI) += jffs2_bbc_lzari_comp.o
154 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZHD) += jffs2_bbc_lzhd_comp.o
156 +JFFS2_BBC_KERNEL_OBJS := $(JFFS2_BBC_KERNEL_OBJS-y)
158 +JFFS2_BBC_MKFS_OBJS = jffs2_bbc_mkfs.o jffs2_bbc_framework.o jffs2_bbc_armlib_comp.o jffs2_bbc_lzo_comp.o\
159 + jffs2_bbc_lzss_comp.o jffs2_bbc_lzari_comp.o jffs2_bbc_lzhd_comp.o
161 COMPR_OBJS := compr.o compr_rubin.o compr_rtime.o pushpull.o \
163 JFFS2_OBJS := dir.o file.o ioctl.o nodelist.o malloc.o \
164 + $(JFFS2_BBC_KERNEL_OBJS) \
165 read.o nodemgmt.o readinode.o super.o write.o scan.o gc.o \
166 symlink.o build.o erase.o background.o
168 Index: linux-2.4.35.4/fs/jffs2/Makefile.bbc.inc
169 ===================================================================
171 +++ linux-2.4.35.4/fs/jffs2/Makefile.bbc.inc
173 +JFFS2_BBC_KERNEL_OBJS-y = jffs2_bbc_framework.o jffs2_bbc_fs.o
175 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_ARMLIB) += jffs2_bbc_armlib_comp.o
176 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZO) += jffs2_bbc_lzo_comp.o
177 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZSS) += jffs2_bbc_lzss_comp.o
178 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZARI) += jffs2_bbc_lzari_comp.o
179 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZHD) += jffs2_bbc_lzhd_comp.o
181 +JFFS2_BBC_KERNEL_OBJS := $(JFFS2_BBC_KERNEL_OBJS-y)
183 +JFFS2_BBC_MKFS_OBJS = jffs2_bbc_mkfs.o jffs2_bbc_framework.o jffs2_bbc_armlib_comp.o jffs2_bbc_lzo_comp.o\
184 + jffs2_bbc_lzss_comp.o jffs2_bbc_lzari_comp.o jffs2_bbc_lzhd_comp.o
185 Index: linux-2.4.35.4/fs/jffs2/compr_zlib.c
186 ===================================================================
187 --- linux-2.4.35.4.orig/fs/jffs2/compr_zlib.c
188 +++ linux-2.4.35.4/fs/jffs2/compr_zlib.c
189 @@ -85,7 +85,7 @@ void jffs2_zlib_exit(void)
190 vfree(inflate_workspace);
193 -int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
194 +int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out,
195 __u32 *sourcelen, __u32 *dstlen)
198 @@ -145,7 +145,7 @@ int zlib_compress(unsigned char *data_in
202 -void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
203 +void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out,
204 __u32 srclen, __u32 destlen)
207 @@ -175,3 +175,19 @@ void zlib_decompress(unsigned char *data
208 zlib_inflateEnd(&strm);
212 +extern int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 * sourcelen, __u32 * dstlen);
213 +extern void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
215 +int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
216 + __u32 *sourcelen, __u32 *dstlen)
218 + return jffs2_zlib_compress(data_in,cpage_out,sourcelen,dstlen);
221 +void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
222 + __u32 srclen, __u32 destlen)
224 + jffs2_zlib_decompress(data_in,cpage_out,srclen,destlen);
227 Index: linux-2.4.35.4/fs/jffs2/file.c
228 ===================================================================
229 --- linux-2.4.35.4.orig/fs/jffs2/file.c
230 +++ linux-2.4.35.4/fs/jffs2/file.c
235 +#include "jffs2_bbc_framework.h" /**BBC**/
236 #include <linux/kernel.h>
237 #include <linux/mtd/compatmac.h> /* for min() */
238 #include <linux/slab.h>
239 @@ -459,6 +460,7 @@ int jffs2_commit_write (struct file *fil
241 comprbuf = kmalloc(cdatalen, GFP_KERNEL);
243 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
244 comprtype = jffs2_compress(page_address(pg)+ (file_ofs & (PAGE_CACHE_SIZE-1)), comprbuf, &datalen, &cdatalen);
246 if (comprtype == JFFS2_COMPR_NONE) {
247 Index: linux-2.4.35.4/fs/jffs2/gc.c
248 ===================================================================
249 --- linux-2.4.35.4.orig/fs/jffs2/gc.c
250 +++ linux-2.4.35.4/fs/jffs2/gc.c
255 +#include "jffs2_bbc_framework.h" /**BBC**/
256 #include <linux/kernel.h>
257 #include <linux/mtd/mtd.h>
258 #include <linux/slab.h>
259 @@ -651,6 +652,7 @@ static int jffs2_garbage_collect_dnode(s
260 writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
263 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
264 comprtype = jffs2_compress(writebuf, comprbuf, &datalen, &cdatalen);
267 Index: linux-2.4.35.4/fs/jffs2/hpatch
268 ===================================================================
270 +++ linux-2.4.35.4/fs/jffs2/hpatch
273 +# A patch-like utility
274 +# Designed for patching different version of jffs2 with the same hpatch file
276 +# Copyright (C) 2004, Ferenc Havasi
278 +# This program is free software; you can redistribute it and/or
279 +# modify it under the terms of the GNU General Public License
280 +# as published by the Free Software Foundation; either version 2
281 +# of the License, or (at your option) any later version.
283 +# This program is distributed in the hope that it will be useful,
284 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
285 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
286 +# GNU General Public License for more details.
288 +# You should have received a copy of the GNU General Public License
289 +# along with this program; if not, write to the Free Software
290 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
292 +$filename_tmp1="file1.tmp";
293 +$filename_tmp2="file2.tmp";
296 +$filename_out=$filename_tmp1;
303 +# 3: expecting F (first state)
309 + if (($mode!=2)&&($modified==1)) {
315 + if ($cmd_name ne "") { $rules{"$cmd_name"}=1; }
316 + $filename_result=$filename_out;
317 + if ($filename_result ne $filename_in_save) {
318 + open(RES,"<$filename_result") or die "Cannot open $filename_result.\n";
319 + open(DST,">$filename_in_save") or die "Cannot open $filename_in_save.\n";
326 + unlink($filename_tmp1) && unlink($filename_tmp2);
331 + $filename_result=$filename_in;
332 + if ($filename_result ne $filename_in_save) {
333 + open(RES,"<$filename_result") or die "Cannot open $filename_result.\n";
334 + open(DST,">$filename_in_save") or die "Cannot open $filename_in_save.\n";
341 + unlink($filename_tmp1);
344 + foreach $rulename (keys %rules) {
345 + if ($rules{"$rulename"}==0) { print(STDERR "On $filename_in_save error applying rule $rulename.\n"); }
351 + print ("usage: hpatch hpatch_file\n");
355 +open(CMD,"<$ARGV[0]") or die "Cannot open $ARGV[0].\n";
358 +while (chomp($cmd_line=<CMD>)) {
360 + if ($cmd_line eq "") {next;}
361 + #$cmd_line =~ s/\#.*//;
362 + $cmd_line =~ s/\ *$//;
363 + if ($cmd_line eq "") {next;}
364 + if ($cmd_line =~ /^F(.*)/) {
365 + $tmp_filename_in=$1;
369 + $filename_in=$tmp_filename_in;
370 + $filename_in_save=$filename_in;
371 + open(SRC,"<$filename_in") or die "Cannot open $filename_in.\n";
372 + open(DST,">$filename_out") or die "Cannot open $filename_out.\n";;
377 + if ($mode==3) {die "error: F expression expected in line $cmd_linenum\n";}
378 + if ($cmd_line =~ /^=(.*)/) {
380 + if (($mode!=2)&&($modified==1)) {
386 + if (($cmd_name ne "")) {$rules{"$cmd_name"}=1;};
388 + if ($filename_cnt%2==1) {
389 + $filename_in=$filename_tmp1;
390 + $filename_out=$filename_tmp2;
393 + $filename_in=$filename_tmp2;
394 + $filename_out=$filename_tmp1;
402 + $cmd_name=$tmp_cmd_name;
403 + if (($cmd_name ne "")) {
404 + if ($rules{"$cmd_name"}==1) {
408 + $rules{"$cmd_name"}=0;
411 + open(SRC,"<$filename_in") or die "Cannot open $filename_in.\n";
412 + open(DST,">$filename_out") or die "Cannot open $filename_out.\n";
416 + if ($mode == 0) {die "error: = expression expected in line $cmd_linenum\n";}
417 + if ($mode == 2) {next;}
418 + if ($cmd_line =~ /^!(.*)/) {
423 + if ($cmd_line =~ /^\?(.*)/) {
428 + if (index($_,$search_str)>=0) {$found=1; last;}
430 + if ($found==0) { $mode=2; }
433 + if ($cmd_line =~ /^\+(.*)/) {
438 + if ($cmd_line =~ /^\-(.*)/) {
442 + if (index($_,$search_str)>=0) {$saved_line=$_; $found=1; $modified=1; last;}
445 + if ($found==0) { $mode=2; }
448 + if ($cmd_line =~ /^i(.*)/) {
450 + open(INCSRC,"<$filename_inc") or die "Cannot open $filename_inc.\n";
456 + if ($cmd_line =~ /^I/) {
457 + print DST $saved_line;
463 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_armlib_comp.c
464 ===================================================================
466 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_armlib_comp.c
469 + * JFFS2-BBC: armlib compressor plugin
471 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
473 + * Copyright (C) 2004, Ferenc Havasi & Tamas Gergely
475 + * This program is free software; you can redistribute it and/or
476 + * modify it under the terms of the GNU General Public License
477 + * as published by the Free Software Foundation; either version 2
478 + * of the License, or (at your option) any later version.
480 + * This program is distributed in the hope that it will be useful,
481 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
482 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
483 + * GNU General Public License for more details.
485 + * You should have received a copy of the GNU General Public License
486 + * along with this program; if not, write to the Free Software
487 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
491 +#include "jffs2_bbc_framework.h"
494 +#include <linux/string.h>
499 +//ORIGIN: include/DataStructures/TypeDefs.h
501 +/*******************************************************************************
503 +* AUTHOR: Tamás Gergely
504 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
505 +*******************************************************************************/
519 +#define u8 unsigned char
522 +#define u16 unsigned short
525 +#define u32 unsigned long
528 +#define s8 signed char
531 +#define s16 signed short
534 +#define s32 signed long
545 +#define VECTOR_P_END(vct) ((void*)(((char*)((vct)->ptr)) + (vct)->size))
546 +#define VECTOR_S_END(vct) ((void*)(((char*)((vct).ptr)) + (vct).size))
548 +static void vector_clear(vector *);
549 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
550 +static void vector_reset(vector *);
551 +static void vector_clr_ptr(vector *);
552 +static void vector_add_u8(vector *, u8);
553 +static void vector_add_u16(vector *, u16);
554 +static void vector_add_u32(vector *, u32);
555 +static void vector_add_s8(vector *, s8);
556 +static void vector_add_s16(vector *, s16);
557 +static void vector_add_s32(vector *, s32);
558 +static void vector_add_ptr(vector *, void *);
559 +static void vector_concat(vector *, vector *);
564 +//ORIGIN: include/DataStructures/DataTypes.h
566 +/*******************************************************************************
568 +* AUTHOR: Tamás Gergely
569 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
570 +*******************************************************************************/
575 +//#include "DataStructures/TypeDefs.h"
577 +typedef u16 THUMB_DataType;
578 +typedef u32 ARM_DataType;
579 +typedef u8 TokenType;
580 +typedef u8 PredictorType;
581 +typedef u8 *ProbDist;
583 +typedef vector RawData;
584 +typedef vector RawBlocks;
585 +typedef vector TokenStream;
586 +typedef vector TokenBlocks;
587 +typedef vector LatType;
589 +#define THUMB_DATA_LENGTH 16
590 +#define ARM_DATA_LENGTH 32
591 +#define TOKEN_LENGTH 8
592 +#define TOKEN_MAXVALUE 0xff
593 +#define PREDICTOR_LENGTH 8
594 +#define PREDICTOR_MAXVALUE 0xff
598 +//ORIGIN: include/DataStructures/BitVector.h
600 +/*******************************************************************************
602 +* AUTHOR: Tamás Gergely
603 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
604 +*******************************************************************************/
609 +//#include "DataStructures/TypeDefs.h"
611 +typedef vector BitBlocks;
624 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
625 +static void bitblocks_clear(BitBlocks *);
626 +static void bitvector_clear(BitVector *);
627 +static void bitvector_W_reset(BitVector *);
628 +static void bitvector_W_add0(BitVector *);
629 +static void bitvector_W_add1(BitVector *);
630 +static void bitvector_W_concat_b(BitVector *, BitVector *);
631 +static void bitvector_W_concat_v(BitVector *, vector *);
632 +static void bitvector_W_flush(BitVector *);
633 +static void bitvector_R_reset(BitVector *);
634 +static u8 bitvector_R_get1(BitVector *);
635 +static u8 bitvector_R_get8(BitVector *);
638 +#define BITVECTOR_P_END(bv) ((void*)(((bv)->base)+((bv)->size)))
639 +#define BITVECTOR_S_END(bv) ((void*)( ((bv).base)+ ((bv).size)))
640 +#define BITVECTOR_SKIP(bv,num) ((bv)->ptr) += (num)
644 +//ORIGIN: include/DataStructures/DecisionTree.h
646 +/*******************************************************************************
647 +* FILE: DecisionTree.h
648 +* AUTHOR: Tamás Gergely
649 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
650 +*******************************************************************************/
652 +#ifndef DECISIONTREE_H
653 +#define DECISIONTREE_H
655 +//#include "DataStructures/DataTypes.h"
659 +#define TREENODETYPE_NULLNODE 0
660 +#define TREENODETYPE_NODE_BINARY_EQ 1
661 +#define TREENODETYPE_LEAF_P 2
662 +#define TREENODETYPE_LEAF_C 3
663 +#define TREENODETYPE_NODE_BINARY_LT 5
664 +#define TREENODETYPE_IS_NODE(n) (((n) == TREENODETYPE_NODE_BINARY_EQ) || \
665 + ((n) == TREENODETYPE_NODE_BINARY_LT))
666 +#define TREENODETYPE_IS_NODE_BINARY(n) (((n) == TREENODETYPE_NODE_BINARY_EQ) || \
667 + ((n) == TREENODETYPE_NODE_BINARY_LT))
669 +#define TREENODETYPE_IS_LEAF(n) (((n) == TREENODETYPE_LEAF_P) || \
670 + ((n) == TREENODETYPE_LEAF_C))
673 +#define TREE_SUBTREE_RELATION_LEFT_EQ !=
674 +#define TREE_SUBTREE_RELATION_RIGHT_EQ ==
675 +#define TREE_SUBTREE_RELATION_LEFT_LT <
676 +#define TREE_SUBTREE_RELATION_RIGHT_LT >=
678 +#define GET_NODE_PTR_TYPE(n) (((TreeNodeDummy*)(n))->type)
687 + u8 type; // [TREENODETYPE_NODE_BINARY]
689 + PredictorType value;
696 + u8 type; // [TREENODETYPE_LEAF_P]
698 + PredictorType *probabilities;
703 + u8 type; // [TREENODETYPE_LEAF_C]
704 + PredictorType predicted_class;
718 + u16 number_of_classes;
719 + u16 number_of_predictors;
720 + PredictorType *predictor_max_values;
723 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
724 +static void decisiontree_delete(DecisionTree *);
725 +static void decisiontree_get_probability_for_token(void *, PredictorType *, TokenType, ProbabilityType *);
726 +static TokenType decisiontree_get_token_for_range(void *, PredictorType *, u32, u32, ProbabilityType *);
731 +//ORIGIN: include/DataStructures/PredictorTable.h
733 +/*******************************************************************************
734 +* FILE: PredictorTable.h
735 +* AUTHOR: Tamás Gergely
736 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
737 +*******************************************************************************/
739 +#ifndef PREDICTORTABLE_H
740 +#define PREDICTORTABLE_H
742 +//#include "DataStructures/TypeDefs.h"
743 +//#include "DataStructures/DataTypes.h"
744 +////#include "DataStructures/Filter.h"
745 +////#include "DataStructures/Converter.h"
746 +////#include "DataStructures/Manipulator.h"
748 +#define NUMBER_OF_PREDICTORS_ARM 17
751 +#define NUMBER_OF_PREDICTORS_TXT 2
754 +#endif // __KERNEL__
757 +#define NUMBER_OF_PREDICTORS NUMBER_OF_PREDICTORS_TXT
758 +#define predictortable_reset predictortable_resetTXT
759 +#define predictortable_update predictortable_updateTXT
760 +#define predictortable_minvalue predictortable_minvalueTXT
761 +#define predictortable_maxvalue predictortable_maxvalueTXT
763 +#define NUMBER_OF_PREDICTORS NUMBER_OF_PREDICTORS_ARM
764 +#define predictortable_reset predictortable_resetARM
765 +#define predictortable_update predictortable_updateARM
766 +#define predictortable_minvalue predictortable_minvalueARM
767 +#define predictortable_maxvalue predictortable_maxvalueARM
775 + PredictorType *predictors;
778 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
779 +static void predictortable_clear(PredictorTable *);
780 +static void predictortable_free(PredictorTable *);
781 +static void predictortable_resetARM(PredictorTable *);
782 +static void predictortable_updateARM(PredictorTable *, TokenType);
783 +static PredictorType predictortable_minvalueARM(PredictorTable *, u32);
784 +static PredictorType predictortable_maxvalueARM(PredictorTable *, u32);
789 +static void predictortable_resetTXT(PredictorTable *);
790 +static void predictortable_updateTXT(PredictorTable *, TokenType);
791 +static PredictorType predictortable_minvalueTXT(PredictorTable *, u32);
792 +static PredictorType predictortable_maxvalueTXT(PredictorTable *, u32);
794 +#endif // __KERNEL__
798 +//ORIGIN: include/DataStructures/ipack_model.h
800 +/*******************************************************************************
801 +* FILE: ipack_model.h
802 +* AUTHOR: Tamás Gergely
803 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
804 +*******************************************************************************/
806 +#ifndef IPACK_MODEL_H
807 +#define IPACK_MODEL_H
809 +//#include "DataStructures/DataTypes.h"
810 +//#include "DataStructures/DecisionTree.h"
811 +//#include "DataStructures/PredictorTable.h"
813 +#define PROBABILITY_SHIFT 12
814 +#define PROBABILITY_MAX 0x00001000l
816 +#define NUMBER_OF_TOKENS_ARM 16
817 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION_ARM 8
820 +#define NUMBER_OF_TOKENS_TXT 256
821 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION_TXT 4
824 +#endif // __KERNEL__
827 +#define NUMBER_OF_TOKENS NUMBER_OF_TOKENS_TXT
828 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION NUMBER_OF_TOKENS_PER_INSTRUCTION_TXT
830 +#define NUMBER_OF_TOKENS NUMBER_OF_TOKENS_ARM
831 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION NUMBER_OF_TOKENS_PER_INSTRUCTION_ARM
837 + Data structure of an internal node of the tree
841 + PredictorType *attribute_ptr;
842 + u32 value; // PredictorType
843 + void *right_child_ptr;
844 +} ipack_treenodeBin;
846 + Data structure of a leaf with probabilities
850 + u16 probabilities[0]; // PredictorType[0]
853 + Data structure of a leaf with class prediction
857 + PredictorType predicted_class; // PredictorType
860 + Possible data structures of a tree node
864 + ipack_treenodeBin nodeBin;
865 + ipack_treeleafP leafP;
866 + ipack_treeleafC leafC;
874 + ipack_node_data data; // ipack_node_data
882 + u16 probabilities[NUMBER_OF_TOKENS];
885 + Model for ipack project
889 + char ID[4]; // char[4]
890 + char block_sign[4]; // only the first 2 are used!
891 + void *tree_root_ptr; // void*
892 + void *tree_code; // generated ARM code
893 + PredictorType *predictors_ptr; // PredictorType*
894 + ipack_nullnode nullnode;
901 +} ipack_probability_type;
904 +static void ipack_model_get_probability_for_token(ipack_nodetype *, TokenType, ipack_probability_type *);
905 +static TokenType ipack_model_get_token_for_range(ipack_nodetype *, u32, u32, ipack_probability_type *);
906 +/*void ipack_model_predictortable_reset (PredictorType*);
907 +void ipack_model_predictortable_update (PredictorType*, TokenType);*/
910 +/*static void ipack_model_printinfo(ipack_model_type *);
911 +static void ipack_dumpmodel(void *);*/
916 +//ORIGIN: include/Builders/PredictorGenerator.h
918 +/*******************************************************************************
919 +* FILE: PredictorGenerator.h
920 +* AUTHOR: Tamás Gergely
921 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
922 +*******************************************************************************/
924 +#ifndef PREDICTORGENERATOR_H
925 +#define PREDICTORGENERATOR_H
927 +//#include "DataStructures.h"
929 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
930 +static PredictorTable *predictorgenerator_generate(void);
935 +//ORIGIN: include/Builders/Coder.h
937 +/*******************************************************************************
939 +* AUTHOR: Tamás Gergely
940 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
941 +*******************************************************************************/
946 +#define CODER_VALUEBITS 16
947 +#define CODER_VALUEMAX 0x00010000l
948 +#define CODER_VALUE3RD 0x0000c000l
949 +#define CODER_VALUEHLF 0x00008000l
950 +#define CODER_VALUE1ST 0x00004000l
954 +//ORIGIN: DataStructures/src/TypeDefs.c
956 +/*******************************************************************************
958 +* AUTHOR: Tamás Gergely
959 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
960 +*******************************************************************************/
962 +//#include "ipack_common.h"
963 +//#include "DataStructures/TypeDefs.h"
968 +#define VECTOR_ALLOC_SIZE 0x00001000
970 +static void vector_clear(vector * vct)
973 + jffs2_bbc_free(vct->ptr);
979 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
980 +static void vector_extend(vector * vct)
983 + vct->capacity += vct->alloc_size;
984 + tmp = jffs2_bbc_malloc(vct->capacity);
986 + memcpy(tmp, vct->ptr, vct->size);
987 + jffs2_bbc_free(vct->ptr);
992 +static void vector_reset(vector * vct)
996 + vct->alloc_size = VECTOR_ALLOC_SIZE;
1000 +static void vector_clr_ptr(vector * vct)
1004 + for (it = vct->ptr, end_it = (((char *) (vct->ptr)) + vct->size); it != end_it; it++) {
1005 + vector_clear(*it);
1006 + jffs2_bbc_free(*it);
1009 + jffs2_bbc_free(vct->ptr);
1010 + vct->capacity = 0;
1015 +static void vector_add_u8(vector * vct, u8 val)
1017 + if ((vct->size) + sizeof(u8) > (vct->capacity)) {
1018 + vector_extend(vct);
1020 + *(u8 *) ((char *) (vct->ptr) + (vct->size)) = val;
1021 + vct->size += sizeof(u8);
1024 +static void vector_add_u16(vector * vct, u16 val)
1026 + if ((vct->size) + sizeof(u16) > (vct->capacity)) {
1027 + vector_extend(vct);
1029 + *(u16 *) ((char *) (vct->ptr) + (vct->size)) = val;
1030 + vct->size += sizeof(u16);
1033 +static void vector_add_u32(vector * vct, u32 val)
1035 + if ((vct->size) + sizeof(u32) > (vct->capacity)) {
1036 + vector_extend(vct);
1038 + *(u32 *) ((char *) (vct->ptr) + (vct->size)) = val;
1039 + vct->size += sizeof(u32);
1042 +static void vector_add_s8(vector * vct, s8 val)
1044 + if ((vct->size) + sizeof(s8) > (vct->capacity)) {
1045 + vector_extend(vct);
1047 + *(s8 *) ((char *) (vct->ptr) + (vct->size)) = val;
1048 + vct->size += sizeof(s8);
1051 +static void vector_add_s16(vector * vct, s16 val)
1053 + if ((vct->size) + sizeof(s16) > (vct->capacity)) {
1054 + vector_extend(vct);
1056 + *(s16 *) ((char *) (vct->ptr) + (vct->size)) = val;
1057 + vct->size += sizeof(s16);
1060 +static void vector_add_s32(vector * vct, s32 val)
1062 + if ((vct->size) + sizeof(s32) > (vct->capacity)) {
1063 + vector_extend(vct);
1065 + *(s32 *) ((char *) (vct->ptr) + (vct->size)) = val;
1066 + vct->size += sizeof(s32);
1069 +static void vector_add_ptr(vector * vct, void *ptr)
1071 + if ((vct->size) + sizeof(void *) > (vct->capacity)) {
1072 + vector_extend(vct);
1074 + *(void **) ((char *) (vct->ptr) + (vct->size)) = ptr;
1075 + vct->size += sizeof(void *);
1078 +static void vector_concat(vector * lhs, vector * rhs)
1081 + if (!(rhs->size)) {
1085 + lhs->capacity = (lhs->size) + (rhs->size);
1086 + lhs->ptr = jffs2_bbc_malloc(lhs->capacity);
1088 + memcpy(lhs->ptr, tmp, lhs->size);
1089 + jffs2_bbc_free(tmp);
1091 + memcpy((((u8 *) lhs->ptr) + lhs->size), rhs->ptr, rhs->size);
1092 + lhs->size += rhs->size;
1097 +//ORIGIN: DataStructures/src/BitVector.c
1099 +/*******************************************************************************
1100 +* FILE: BitVector.c
1101 +* AUTHOR: Tamás Gergely
1102 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1103 +*******************************************************************************/
1105 +//#include "ipack_common.h"
1106 +//#include "DataStructures/BitVector.h"
1108 +#include <memory.h>
1111 +#define VECTOR_ALLOC_SIZE 0x00001000
1113 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1115 +static void bitblocks_clear(BitBlocks * this)
1119 + for (it = this->ptr, end_it = VECTOR_P_END(this); it != end_it; it++) {
1120 + bitvector_clear(*it);
1121 + jffs2_bbc_free(*it);
1123 + jffs2_bbc_free(this->ptr);
1127 +static void bitvector_clear(BitVector * this)
1130 + jffs2_bbc_free(this->base);
1132 + this->freebits = 0;
1133 + this->capacity = 0;
1139 +static void bitvector_W_reset(BitVector * this)
1141 + this->freebits = 0;
1142 + this->capacity = 0;
1148 +static void bitvector_W_add0(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) = 0x00;
1168 + (*(this->ptr)) <<= 1;
1172 +static void bitvector_W_add1(BitVector * this)
1174 + if (!(this->freebits)) {
1175 + if (this->size == this->capacity) {
1176 + void *tmp = this->base;
1177 + this->capacity += VECTOR_ALLOC_SIZE;
1178 + this->base = jffs2_bbc_malloc(this->capacity);
1179 + this->ptr = ((u8 *) (this->base)) + this->size;
1180 + memcpy(this->base, tmp, this->size);
1181 + jffs2_bbc_free(tmp);
1187 + this->freebits = 7;
1188 + *(this->ptr) = 0x01;
1192 + (*(this->ptr)) <<= 1;
1193 + (*(this->ptr)) |= 0x01;
1197 +static void bitvector_W_concat_b(BitVector * lhs, BitVector * rhs)
1200 + if (!(rhs->size)) {
1204 + lhs->capacity = ((((lhs->size) + (rhs->size) - 1) / VECTOR_ALLOC_SIZE) + 1) * VECTOR_ALLOC_SIZE;
1205 + lhs->base = jffs2_bbc_malloc(lhs->capacity);
1207 + memcpy(lhs->base, tmp, lhs->size);
1208 + jffs2_bbc_free(tmp);
1210 + memcpy((((u8 *) (lhs->base)) + lhs->size), rhs->base, rhs->size);
1211 + lhs->freebits = 0;
1212 + lhs->size += rhs->size;
1213 + lhs->ptr = ((u8 *) (lhs->base)) + lhs->size;
1216 +static void bitvector_W_concat_v(BitVector * lhs, vector * rhs)
1219 + if (!(rhs->size)) {
1223 + lhs->capacity = ((((lhs->size) + (rhs->size) - 1) / VECTOR_ALLOC_SIZE) + 1) * VECTOR_ALLOC_SIZE;
1224 + lhs->base = jffs2_bbc_malloc(lhs->capacity);
1226 + memcpy(lhs->base, tmp, lhs->size);
1227 + jffs2_bbc_free(tmp);
1229 + memcpy((((u8 *) (lhs->base)) + lhs->size), rhs->ptr, rhs->size);
1230 + lhs->freebits = 0;
1231 + lhs->size += rhs->size;
1232 + lhs->ptr = ((u8 *) (lhs->base)) + lhs->size;
1235 +static void bitvector_W_flush(BitVector * this)
1237 + (*(this->ptr)) <<= this->freebits;
1238 + this->freebits = 0;
1241 +static void bitvector_R_reset(BitVector * this)
1243 + this->freebits = 7;
1244 + this->ptr = this->base;
1247 +static u8 bitvector_R_get1(BitVector * this)
1249 + u8 tmp = ((*(this->ptr)) >> this->freebits) & 0x01;
1250 + if (!(this->freebits)) {
1251 + this->freebits = 7;
1260 +static u8 bitvector_R_get8(BitVector * this)
1262 + u8 tmp = (*(this->ptr));
1269 +//ORIGIN: DataStructures/src/DecisionTree.c
1271 +/*******************************************************************************
1272 +* FILE: DecisionTree.c
1273 +* AUTHOR: Tamás Gergely
1274 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1275 +*******************************************************************************/
1277 +//#include "ipack_common.h"
1278 +//#include "DataStructures/DecisionTree.h"
1280 +static void decisiontree_delete_node(void *root)
1282 + u8 tmp = GET_NODE_PTR_TYPE(root);
1283 + if (TREENODETYPE_IS_NODE_BINARY(tmp)) {
1284 + decisiontree_delete_node(((TreeNodeBinary *) root)->left);
1285 + decisiontree_delete_node(((TreeNodeBinary *) root)->right);
1287 + else if ((tmp) == TREENODETYPE_LEAF_P) {
1288 + if (((TreeLeafP *) root)->probabilities) {
1289 + jffs2_bbc_free(((TreeLeafP *) root)->probabilities);
1292 + else if ((tmp) == TREENODETYPE_LEAF_C) {
1294 + jffs2_bbc_free(root);
1297 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1299 +static void decisiontree_delete(DecisionTree * dt)
1301 + decisiontree_delete_node(dt->root);
1302 + jffs2_bbc_free(dt->predictor_max_values);
1305 +static void decisiontree_get_probability_for_token(void *root, PredictorType * preds, TokenType token, ProbabilityType * prob)
1308 + while (TREENODETYPE_IS_NODE(((TreeNodeBinary *) tmp)->type)) {
1309 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_EQ) {
1310 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_EQ((TreeNodeBinary *) tmp)->value) {
1311 + tmp = ((TreeNodeBinary *) tmp)->left;
1314 + tmp = ((TreeNodeBinary *) tmp)->right;
1317 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_LT) {
1318 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_LT((TreeNodeBinary *) tmp)->value) {
1319 + tmp = ((TreeNodeBinary *) tmp)->left;
1322 + tmp = ((TreeNodeBinary *) tmp)->right;
1329 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_P) {
1331 + u32 lngth = ((TreeLeafP *) tmp)->pairs << 1;
1332 + for (i = 0; i < lngth;) {
1333 + TokenType at = ((TreeLeafP *) tmp)->probabilities[i++];
1334 + TokenType av = ((TreeLeafP *) tmp)->probabilities[i++];
1342 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_C) {
1343 + if (((TreeLeafC *) tmp)->predicted_class == token) {
1344 + prob->high = TOKEN_MAXVALUE;
1345 + prob->max = TOKEN_MAXVALUE;
1350 +static TokenType decisiontree_get_token_for_range(void *root, PredictorType * preds, u32 value, u32 range, ProbabilityType * prob)
1353 + TokenType token = 0;
1354 + while (TREENODETYPE_IS_NODE(((TreeNodeBinary *) tmp)->type)) {
1355 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_EQ) {
1356 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_EQ((TreeNodeBinary *) tmp)->value) {
1357 + tmp = ((TreeNodeBinary *) tmp)->left;
1360 + tmp = ((TreeNodeBinary *) tmp)->right;
1363 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_LT) {
1364 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_LT((TreeNodeBinary *) tmp)->value) {
1365 + tmp = ((TreeNodeBinary *) tmp)->left;
1368 + tmp = ((TreeNodeBinary *) tmp)->right;
1375 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_P) {
1380 + u32 lngth = ((TreeLeafP *) tmp)->pairs << 1;
1381 + for (i = 0; i < lngth;) {
1383 + prob->max += ((TreeLeafP *) tmp)->probabilities[i++];
1385 + norm = (value * prob->max - 1) / range;
1386 + for (i = 0; prob->high <= norm;) {
1387 + at = ((TreeLeafP *) tmp)->probabilities[i++];
1388 + av = ((TreeLeafP *) tmp)->probabilities[i++];
1390 + if (prob->high <= norm)
1395 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_C) {
1396 + token = ((TreeLeafC *) tmp)->predicted_class;
1397 + prob->high = TOKEN_MAXVALUE;
1398 + prob->max = TOKEN_MAXVALUE;
1404 +//ORIGIN: DataStructures/src/PredictorTable.c
1406 +/*******************************************************************************
1407 +* FILE: PredictorTable.c
1408 +* AUTHOR: Tamás Gergely
1409 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1410 +*******************************************************************************/
1412 +//#include "ipack_common.h"
1413 +//#include "DataStructures/PredictorTable.h"
1415 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1417 +static void predictortable_clear(PredictorTable * table)
1419 + table->predictors = 0;
1422 +static void predictortable_free(PredictorTable * table)
1424 + if (table->predictors) {
1425 + jffs2_bbc_free(table->predictors);
1426 + table->predictors = 0;
1430 +static void predictortable_resetARM(PredictorTable * table)
1432 + register PredictorType *ptr = table->predictors;
1433 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS_ARM;
1434 + while (ptr < end) {
1439 +static void predictortable_updateARM(PredictorTable * table, TokenType token)
1441 + register PredictorType *ptr = table->predictors;
1442 + register u32 ndx = ptr[0] + 1;
1443 + ptr[ndx + 8] = ptr[ndx];
1453 +static PredictorType predictortable_minvalueARM(PredictorTable * table, u32 index)
1458 +static PredictorType predictortable_maxvalueARM(PredictorTable * table, u32 index)
1472 +/*static void predictortable_resetTXT(PredictorTable * table)
1474 + register PredictorType *ptr = table->predictors;
1475 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS_TXT;
1476 + while (ptr < end) {
1481 +static void predictortable_updateTXT(PredictorTable * table, TokenType token)
1483 + register PredictorType *ptr = table->predictors;
1484 +// register u32 ndx;
1486 + if ((('a' <= token) && (token <= 'z')) || (('A' <= token) && (token <= 'Z'))) {
1494 +static PredictorType predictortable_minvalueTXT(PredictorTable * table, u32 index)
1499 +static PredictorType predictortable_maxvalueTXT(PredictorTable * table, u32 index)
1504 +#endif // __KERNEL__
1506 +//ORIGIN: DataStructures/src/ipack_model.c
1508 +/*******************************************************************************
1509 +* FILE: ipack_model.c
1510 +* AUTHOR: Tamás Gergely
1511 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1512 +*******************************************************************************/
1514 +//#include "DataStructures/ipack_model.h"
1515 +//#include "measuredef.h"
1516 +//#include "ipack_common.h"
1518 +#ifdef __MEASURE_TIME_MODEL_GETPROB
1519 +#define __MT_P_MAX 256
1520 +#define __MT_P_DIV 128
1521 +#define __MT_P_MIN 0
1524 +static void ipack_model_get_probability_for_token(ipack_nodetype * tmp, TokenType token, ipack_probability_type * prob)
1526 +// register ipack_nodetype* tmp = model->tree_root_ptr;
1527 +// register ipack_nodetype* tmp = root;
1528 + while (TREENODETYPE_IS_NODE(tmp->type)) {
1529 + if (tmp->type == TREENODETYPE_NODE_BINARY_EQ) {
1530 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_EQ tmp->data.nodeBin.value) {
1531 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1534 + tmp = tmp->data.nodeBin.right_child_ptr;
1537 + else if (tmp->type == TREENODETYPE_NODE_BINARY_LT) {
1538 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_LT tmp->data.nodeBin.value) {
1539 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1542 + tmp = tmp->data.nodeBin.right_child_ptr;
1549 + if (tmp->type == TREENODETYPE_LEAF_P) {
1551 + prob->low = tmp->data.leafP.probabilities[token - 1];
1553 + prob->high = tmp->data.leafP.probabilities[token];
1554 +// prob->max = tmp->data.leafP.probabilities[15];
1556 + else if (tmp->type == TREENODETYPE_LEAF_C) {
1557 + if (tmp->data.leafC.predicted_class == token) {
1558 + prob->high = TOKEN_MAXVALUE;
1559 +// prob->max = TOKEN_MAXVALUE;
1564 +#ifndef IPACK_ARM_ASM
1566 +//return ipack_model_get_token_for_range2(tmp,value,range,prob);
1568 +static TokenType ipack_model_get_token_for_range(ipack_nodetype * tmp, u32 value, u32 range, ipack_probability_type * prob)
1570 +// register ipack_nodetype* tmp = model->tree_root_ptr;
1571 +// register ipack_nodetype* tmp = root;
1572 + register TokenType token = 0;
1573 + while (TREENODETYPE_IS_NODE(tmp->type)) {
1574 + if (tmp->type == TREENODETYPE_NODE_BINARY_EQ) {
1575 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_EQ tmp->data.nodeBin.value) {
1576 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1579 + tmp = tmp->data.nodeBin.right_child_ptr;
1582 + else if (tmp->type == TREENODETYPE_NODE_BINARY_LT) {
1583 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_LT tmp->data.nodeBin.value) {
1584 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1587 + tmp = tmp->data.nodeBin.right_child_ptr;
1594 + if (tmp->type == TREENODETYPE_LEAF_P) {
1597 +// prob->max = tmp->data.leafP.probabilities[15];
1598 +/* norm = (value * prob->max -1)/range;
1599 + for(i = 0; i < 15; ++i) {
1600 + if(tmp->data.leafP.probabilities[i] > norm) {
1604 + norm = ((value << PROBABILITY_SHIFT) - 1);
1605 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1606 + if (range * tmp->data.leafP.probabilities[i] > norm) {
1610 + token = (TokenType) i;
1611 + prob->high = tmp->data.leafP.probabilities[i];
1613 + prob->low = tmp->data.leafP.probabilities[token - 1];
1616 + else if (tmp->type == TREENODETYPE_LEAF_C) {
1617 + token = tmp->data.leafC.predicted_class;
1618 + prob->high = PROBABILITY_MAX;
1619 +// prob->max = PROBABILITY_MAX;
1625 +void ipack_model_predictortable_reset(PredictorType* ptr)
1627 +// register PredictorType* ptr = model->predictors_ptr;
1628 +// register PredictorType* ptr = preds;
1629 + register PredictorType* end = ptr + NUMBER_OF_PREDICTORS;
1630 + while(ptr < end) {
1635 +void ipack_model_predictortable_update(PredictorType* ptr, TokenType token)
1637 +// register PredictorType* ptr = model->predictors_ptr;
1638 +// register PredictorType* ptr = preds;
1639 + register u32 ndx = ptr[0] + 1;
1640 + ptr[ndx + 8] = ptr[ndx];
1648 +/****************************************************************************/
1651 +static void ipack_model_countpreds(void *ptr, ipack_nodetype * node, double *table, double val)
1653 + if ((node->type == TREENODETYPE_NODE_BINARY_EQ) || (node->type == TREENODETYPE_NODE_BINARY_LT)) {
1654 + table[(u32) (node->data.nodeBin.attribute_ptr) - (u32) (ptr)] += val;
1655 + ipack_model_countpreds(ptr, (void *) (((u8 *) (node)) + sizeof(node->type) + sizeof(ipack_treenodeBin)), table, val / 2);
1656 + ipack_model_countpreds(ptr, node->data.nodeBin.right_child_ptr, table, val / 2);
1662 +/*static void ipack_model_printinfo(ipack_model_type * model)
1664 + double *prcnt = jffs2_bbc_malloc(sizeof(double) * NUMBER_OF_PREDICTORS);
1666 + for (i = 0; i < NUMBER_OF_PREDICTORS; i++) {
1669 + ipack_model_countpreds(model->predictors_ptr, model->tree_root_ptr, prcnt, 100);
1670 + for (i = 0; i < NUMBER_OF_PREDICTORS; i++) {
1671 + jffs2_bbc_print3(" p[%3d] = %10.6lf\n", (int) i, prcnt[i]);
1673 + jffs2_bbc_free(prcnt);
1676 +static void ipack_dumpnode(unsigned char **ptr, FILE * file, char *prefs)
1678 + switch (*((*ptr)++)) {
1683 + case TREENODETYPE_NODE_BINARY_EQ:
1686 + fprintf(file, "%s+->\tBinary node: P[%u] equals %u\n", prefs, (unsigned int)x, (unsigned int)y);
1687 + for (j = 0; j < 4096 && prefs[j]; ++j);
1690 + ipack_dumpnode(ptr, file, prefs);
1692 + ipack_dumpnode(ptr, file, prefs);
1695 + case TREENODETYPE_NODE_BINARY_LT:
1698 + fprintf(file, "%s+->\tBinary node: P[%u] greater than %u\n", prefs, (unsigned int)x, (unsigned int)y);
1699 + for (j = 0; j < 4096 && prefs[j]; ++j);
1702 + ipack_dumpnode(ptr, file, prefs);
1704 + ipack_dumpnode(ptr, file, prefs);
1707 + case TREENODETYPE_LEAF_P:
1709 + fprintf(file, "%s+->\tLeaf: %u pairs\n", prefs, (unsigned int)x);
1710 + (*ptr) += (x << 1);
1712 + case TREENODETYPE_LEAF_C:
1714 + fprintf(file, "%s+->\tLeaf: class %u\n", prefs, (unsigned int)x);
1717 + fprintf(file, "%s+->\tLeaf: nullnode\n", prefs);
1721 +/*static void ipack_dumpmodel(void *model)
1723 + unsigned char *tmp_ptr = model;
1726 + if ((file = fopen("DUMPED_MODEL", "wa"))) {
1728 + for (i = 0; i < 4096; C[i++] = 0);
1730 + tmp_ptr += sizeof(u32);
1731 + ipack_dumpnode(&tmp_ptr, file, C);
1738 +//ORIGIN: Builders/src/PredictorGenerator.c
1740 +/*******************************************************************************
1741 +* FILE: PredictorGenerator.c
1742 +* AUTHOR: Tamás Gergely
1743 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1744 +*******************************************************************************/
1746 +//#include "ipack_common.h"
1747 +//#include "Builders/PredictorGenerator.h"
1749 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1750 +static PredictorTable *predictorgenerator_generate( /*PredictorGeneratorSettings* settings */ )
1752 + PredictorTable *ptr = jffs2_bbc_malloc(sizeof(PredictorTable));
1753 + predictortable_clear(ptr);
1754 + ptr->predictors = jffs2_bbc_malloc(NUMBER_OF_PREDICTORS * sizeof(PredictorType));
1759 +//ORIGIN: Builders/src/ipack_armlib_compressor.c
1761 +/*******************************************************************************
1762 +* FILE: ipack_armlim_compressor.c
1763 +* AUTHOR: Tamás Gergely
1764 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1765 +*******************************************************************************/
1767 +//#include "ipack_common.h"
1768 +//#include "DataStructures.h"
1769 +//#include "Builders/PredictorGenerator.h"
1770 +//#include "Builders/Tokenizer.h"
1771 +//#include "Builders/Coder.h"
1773 +#define EC_NO_ERROR 0
1774 +#define EC_NOT_IPMF_FILE -1
1775 +#define EC_NOT_IPMF_MODEL -2
1776 +#define EC_NOT_HG_BLOCK -3
1777 +#define EC_WRONG_INPUT_LENGTH -501
1778 +#define EC_CODER_WRONG_PROBABILITY 1
1779 +#define EC_CODER_WRONG_RANGE 2
1780 +#define EC_BUFFER_OVERFLOW 501
1781 +#define EC_BUFFER_UNDERFLOW 502
1782 +#define EC_UNKNOWN_TOKEN_TYPE 1001
1783 +#define EC_UNKNOWN_FILTER 1002
1784 +#define EC_UNKNOWN_CONVERTER 1003
1785 +#define EC_UNKNOWN_MANIPULATOR 1004
1787 +/*******************************************************************************
1789 + COMPRESSOR INIT FUNCTIONS
1791 +*******************************************************************************/
1793 +#define ROUND_UP_TO_DWORD(val) ( ( (val) + 3 ) & 0xfffffffc )
1796 +int ipack_glb_endian_X;
1799 +static int ipack_compressor_init_tree(unsigned char **ptr, ipack_model_type * model, ipack_nodetype * node, void *nullnode)
1802 + node->type = *((*ptr)++);
1803 + switch (node->type) {
1810 + case TREENODETYPE_NODE_BINARY_EQ:
1811 + case TREENODETYPE_NODE_BINARY_LT:
1812 + node->data.nodeBin.attribute_ptr = (model->predictors_ptr) + (*((*ptr)++));
1813 + node->data.nodeBin.value = *((*ptr)++);
1814 + retval = sizeof(node->data.nodeBin);
1815 + retval += ipack_compressor_init_tree(ptr, model, (void *) ROUND_UP_TO_DWORD(((u32) node) + sizeof(node->type) + sizeof(node->data.nodeBin)), nullnode);
1816 + node->data.nodeBin.right_child_ptr = (void *) ROUND_UP_TO_DWORD(((u32) node) + retval + sizeof(node->type));
1817 + retval += ipack_compressor_init_tree(ptr, model, node->data.nodeBin.right_child_ptr, nullnode);
1819 + case TREENODETYPE_LEAF_P:
1820 + lngth = *((*ptr)++);
1822 + for (i = 0, j = 0; i < lngth; ++i) {
1825 + node->data.leafP.probabilities[j++] = av;
1827 + av += *((*ptr)++);
1829 + while (j < NUMBER_OF_TOKENS) {
1830 + node->data.leafP.probabilities[j++] = av;
1832 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1833 + node->data.leafP.probabilities[i] = ((node->data.leafP.probabilities[i] << PROBABILITY_SHIFT) / node->data.leafP.probabilities[NUMBER_OF_TOKENS - 1]);
1835 + retval = ROUND_UP_TO_DWORD(NUMBER_OF_TOKENS * sizeof(u16));
1837 + case TREENODETYPE_LEAF_C:
1838 + node->data.leafC.predicted_class = *((*ptr)++);
1839 + retval = sizeof(node->data.leafC);
1840 + retval = ROUND_UP_TO_DWORD(retval);
1845 + return retval + sizeof(node->type);
1848 +#define IPACK_TREE_CONVERT_REPLACE 0
1849 +#define IPACK_TREE_CONVERT_KEEP 1
1851 +static void *ipack_tree_to_code(ipack_model_type * model, int *code_size);
1853 +static int ipack_armlib_convert_tree_to_code(ipack_model_type * model_img, int mode)
1855 +#ifdef IPACK_TREE_TO_CODE
1858 + model_img->tree_code = ipack_tree_to_code(model_img, &tree_size);
1859 + jffs2_bbc_print2("Convertation done. Code size=%d\n", tree_size);
1860 + if (mode == IPACK_TREE_CONVERT_REPLACE) {
1861 + jffs2_bbc_print1("Freeing original tree.\n");
1862 + jffs2_bbc_free(model_img->tree_root_ptr);
1863 + model_img->tree_root_ptr = NULL;
1870 +static int ipack_armlib_compressor_init(void **model)
1872 + int retval = EC_NO_ERROR;
1873 + unsigned char *tmp_ptr = *model;
1875 + ipack_model_type *model_img;
1878 + if (*(tmp_ptr++) != 'i') {
1879 + return EC_NOT_IPMF_FILE;
1881 + else if (*(tmp_ptr++) != 'P') {
1882 + return EC_NOT_IPMF_FILE;
1884 + else if (*(tmp_ptr++) != 'M') {
1885 + return EC_NOT_IPMF_FILE;
1887 + else if (*(tmp_ptr++) != 'F') {
1888 + return EC_NOT_IPMF_FILE;
1890 + tmp_c[0] = *(tmp_ptr++);
1891 + tmp_c[1] = *(tmp_ptr++);
1894 + //model_img = jffs2_bbc_malloc(*((u32*)tmp_ptr));
1895 + model_img = jffs2_bbc_malloc(sizeof(ipack_model_type) + ROUND_UP_TO_DWORD(NUMBER_OF_PREDICTORS));
1896 + model_img->tree_root_ptr = jffs2_bbc_malloc(*((u32 *) tmp_ptr)); //it is smaller a little but, but...
1898 + tmp_ptr += sizeof(u32);
1900 + model_img->ID[0] = 'i';
1901 + model_img->ID[1] = 'P';
1902 + model_img->ID[2] = 'M';
1903 + model_img->ID[3] = 'F';
1905 + model_img->block_sign[0] = tmp_c[0];
1906 + model_img->block_sign[1] = tmp_c[1];
1908 + model_img->nullnode.type = TREENODETYPE_LEAF_P;
1909 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1910 + model_img->nullnode.probabilities[i] = 0;
1912 + model_img->predictors_ptr = (void *) (((u32) model_img) + sizeof(ipack_model_type));
1913 + //model_img->tree_root_ptr = (void*)ROUND_UP_TO_DWORD(((u32)(model_img->predictors_ptr)) + NUMBER_OF_PREDICTORS);//ALIGN
1915 + ipack_compressor_init_tree(&tmp_ptr, model_img, model_img->tree_root_ptr, &(model_img->nullnode));
1917 +#ifdef IPACK_TREE_TO_CODE
1918 +#ifdef IPACK_AUTO_TREE_TO_CODE
1919 + jffs2_bbc_print1("Automatically converting tree to ARM code...\n");
1920 + ipack_armlib_convert_tree_to_code(model_img, IPACK_TREE_CONVERT_REPLACE);
1922 + model_img->tree_code = NULL;
1925 + model_img->tree_code = NULL;
1928 + jffs2_bbc_free(*model);
1929 + *model = model_img;
1933 +/*******************************************************************************
1935 + COMPRESSOR DEINIT FUNCTIONS
1937 +*******************************************************************************/
1940 +/* Descructor of compressor (model will be freed with jffs2_bbc_free() after it)*/
1941 +static void ipack_armlib_compressor_deinit(void)
1945 +/*******************************************************************************
1947 + COMPRESS FUNCTIONS
1949 +*******************************************************************************/
1951 +static int writebits0(unsigned char **dest, u8 * freebits, u32 * opposite, unsigned char *end)
1953 + if (!(*freebits)) {
1962 + if ((*dest == end) && !(*freebits)) {
1963 + return EC_BUFFER_OVERFLOW;
1965 + while (*opposite) {
1967 + if (!(*freebits)) {
1977 + if ((*dest == end) && !(*freebits)) {
1978 + return EC_BUFFER_OVERFLOW;
1984 +static int writebits1(unsigned char **dest, u8 * freebits, u32 * opposite, unsigned char *end)
1986 + if (!(*freebits)) {
1996 + if ((*dest == end) && !(*freebits)) {
1997 + return EC_BUFFER_OVERFLOW;
1999 + while (*opposite) {
2001 + if (!(*freebits)) {
2010 + if ((*dest == end) && !(*freebits)) {
2011 + return EC_BUFFER_OVERFLOW;
2021 + * *dstlen bytes are allocated.
2022 + * if it is not enough write *sourcelen over to the processed amount of data
2023 + * returns non zero if fails
2025 +static int ipack_armlib_compress(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen)
2027 + register u32 coder_high = CODER_VALUEMAX - 1;
2028 + register u32 coder_low = 0;
2029 + u32 coder_opbits = 0;
2030 + u8 bitvector_freebits = 8;
2031 + unsigned char *bitvector_ptr = output;
2032 + unsigned char *bitvector_end = output + (*dstlen - 1);
2033 + ARM_DataType *tmpp;
2038 + ipack_nodetype *treeroot = ((ipack_model_type *) model)->tree_root_ptr;
2039 + PredictorType *predctrs = ((ipack_model_type *) model)->predictors_ptr;
2041 +#ifdef IPACK_TREE_TO_CODE
2042 + void (*treefunc) (ipack_nodetype *, TokenType, ipack_probability_type *);
2044 + treefunc = ((ipack_model_type *) model)->tree_code;
2045 + if (treefunc != NULL)
2049 + if ((*sourcelen % 4) != 0) {
2050 + return EC_WRONG_INPUT_LENGTH;
2052 + if (*dstlen <= 4) {
2053 + return EC_BUFFER_OVERFLOW;
2056 + if (((ipack_model_type *) model)->ID[0] != 'i') {
2057 + return EC_NOT_IPMF_MODEL;
2059 + else if (((ipack_model_type *) model)->ID[1] != 'P') {
2060 + return EC_NOT_IPMF_MODEL;
2062 + else if (((ipack_model_type *) model)->ID[2] != 'M') {
2063 + return EC_NOT_IPMF_MODEL;
2065 + else if (((ipack_model_type *) model)->ID[3] != 'F') {
2066 + return EC_NOT_IPMF_MODEL;
2069 + tmpv.capacity = (*sourcelen);
2071 + tmpv.capacity = (*sourcelen) << 1;
2073 + tmpv.size = tmpv.capacity;
2074 + tmpv.ptr = jffs2_bbc_malloc(tmpv.size);
2078 + if (ipack_glb_endian_X) {
2079 + for (tmpp = (void *) input; (u32) tmpp < (u32) (input + *sourcelen); ++tmpp) {
2081 + *(it++) = (u8) ((*tmpp & 0xff000000) >> 24);
2082 + *(it++) = (u8) ((*tmpp & 0x00ff0000) >> 16);
2083 + *(it++) = (u8) ((*tmpp & 0x0000ff00) >> 8);
2084 + *(it++) = (u8) ((*tmpp & 0x000000ff));
2086 + *(it++) = (u8) ((*tmpp & 0x0000f000) >> 12);
2087 + *(it++) = (u8) ((*tmpp & 0x0000000f));
2088 + *(it++) = (u8) ((*tmpp & 0xf0000000) >> 28);
2089 + *(it++) = (u8) ((*tmpp & 0x000f0000) >> 16);
2090 + *(it++) = (u8) ((*tmpp & 0x00000f00) >> 8);
2091 + *(it++) = (u8) ((*tmpp & 0x00f00000) >> 20);
2092 + *(it++) = (u8) ((*tmpp & 0x0f000000) >> 24);
2093 + *(it++) = (u8) ((*tmpp & 0x000000f0) >> 4);
2094 +#endif //TXT_TOKENS
2099 + for (tmpp = (void *) input; (u32) tmpp < (u32) (input + *sourcelen); ++tmpp) {
2101 + *(it++) = (u8) ((*tmpp & 0x000000ff));
2102 + *(it++) = (u8) ((*tmpp & 0x0000ff00) >> 8);
2103 + *(it++) = (u8) ((*tmpp & 0x00ff0000) >> 16);
2104 + *(it++) = (u8) ((*tmpp & 0xff000000) >> 24);
2106 + *(it++) = (u8) ((*tmpp & 0x00f00000) >> 20);
2107 + *(it++) = (u8) ((*tmpp & 0x0f000000) >> 24);
2108 + *(it++) = (u8) ((*tmpp & 0x000000f0) >> 4);
2109 + *(it++) = (u8) ((*tmpp & 0x00000f00) >> 8);
2110 + *(it++) = (u8) ((*tmpp & 0x000f0000) >> 16);
2111 + *(it++) = (u8) ((*tmpp & 0x0000f000) >> 12);
2112 + *(it++) = (u8) ((*tmpp & 0x0000000f));
2113 + *(it++) = (u8) ((*tmpp & 0xf0000000) >> 28);
2114 +#endif //TXT_TOKENS
2122 + { //predictor reset
2123 + register PredictorType *ptr = predctrs;
2124 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS;
2125 + while (ptr < end) {
2130 + //*(bitvector_ptr++) = 'H';
2131 + //*(bitvector_ptr++) = 'G';
2132 + *(bitvector_ptr++) = ((ipack_model_type *) model)->block_sign[0];
2133 + *(bitvector_ptr++) = ((ipack_model_type *) model)->block_sign[1];
2135 + *(bitvector_ptr++) = (unsigned char) (((*sourcelen) >> 8) & 0xff);
2136 + *(bitvector_ptr++) = (unsigned char) ((*sourcelen) & 0xff);
2137 + for (it = tmpv.ptr, end_it = VECTOR_S_END(tmpv); it != end_it; ++it) {
2138 + ipack_probability_type prob;
2141 +#ifdef IPACK_TREE_TO_CODE
2142 + if (treefunc != NULL)
2143 + (*treefunc) (treeroot, *it, &prob);
2145 + ipack_model_get_probability_for_token(treeroot, *it, &prob);
2147 + ipack_model_get_probability_for_token(treeroot, *it, &prob);
2150 + if (prob.high == prob.low) {
2151 + vector_clear(&tmpv);
2152 + return EC_CODER_WRONG_PROBABILITY;
2154 + range = coder_high - coder_low + 1;
2155 + coder_high = coder_low + ((range * prob.high) >> PROBABILITY_SHIFT) - 1;
2156 + coder_low += ((range * prob.low) >> PROBABILITY_SHIFT);
2158 + if (coder_high < CODER_VALUEHLF) {
2159 + if (writebits0(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2160 + vector_clear(&tmpv);
2161 + return EC_BUFFER_OVERFLOW;
2164 + else if (coder_low >= CODER_VALUEHLF) {
2165 + if (writebits1(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2166 + vector_clear(&tmpv);
2167 + return EC_BUFFER_OVERFLOW;
2169 + coder_high -= CODER_VALUEHLF;
2170 + coder_low -= CODER_VALUEHLF;
2172 + else if ((CODER_VALUE1ST <= coder_low) && (coder_high < CODER_VALUE3RD)) {
2174 + coder_high -= CODER_VALUE1ST;
2175 + coder_low -= CODER_VALUE1ST;
2183 + if (coder_high < coder_low) {
2184 + vector_clear(&tmpv);
2185 + return EC_CODER_WRONG_RANGE;
2190 +// register u32 ndx;
2191 + predctrs[0] = *it;
2192 + if ((('a' <= *it) && (*it <= 'z')) || (('A' <= *it) && (*it <= 'Z'))) {
2199 + register u32 ndx = predctrs[0] + 1;
2200 + predctrs[ndx + 8] = predctrs[ndx];
2201 + predctrs[ndx] = *it;
2212 + vector_clear(&tmpv);
2214 + if (coder_low < CODER_VALUE1ST) {
2215 + if (writebits0(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2216 + return EC_BUFFER_OVERFLOW;
2220 + if (writebits1(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2221 + return EC_BUFFER_OVERFLOW;
2224 + (*(bitvector_ptr)) <<= bitvector_freebits;
2225 + *dstlen = ((u32) bitvector_ptr - (u32) output + 1);
2226 + return EC_NO_ERROR;
2229 +/*******************************************************************************
2231 + DECOMPRESS FUNCTIONS
2233 +*******************************************************************************/
2241 +} ipack_decompressor_values;
2246 + unsigned char *ptr;
2247 + unsigned char *end;
2248 +} ipack_decompressor_bitvector;
2250 +static u8 ipack_bitvector_R_get1(ipack_decompressor_bitvector * bv)
2253 + if (bv->ptr == bv->end) {
2257 + tmp = (*(bv->ptr) >> bv->freebits) & 0x01;
2258 + if (!(bv->freebits)) {
2268 +/* Decompress block
2269 + * returns non zero if fails
2271 +static int ipack_armlib_decompress(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen)
2273 + ARM_DataType *data;
2274 + register u32 coder_high = CODER_VALUEMAX - 1;
2275 + register u32 coder_low = 0;
2276 + register u32 coder_value = 0;
2277 + u32 coder_overread = 0;
2278 + ipack_decompressor_bitvector bitvector;
2282 + TokenType tkns[8];
2285 + ipack_nodetype *treeroot = ((ipack_model_type *) model)->tree_root_ptr;
2286 + PredictorType *predctrs = ((ipack_model_type *) model)->predictors_ptr;
2288 +#ifdef IPACK_TREE_TO_CODE
2289 + TokenType(*treefunc) (ipack_nodetype *, u32, u32, ipack_probability_type *);
2291 + treefunc = ((ipack_model_type *) model)->tree_code;
2295 + if (((ipack_model_type *) model)->ID[0] != 'i') {
2296 + return EC_NOT_IPMF_MODEL;
2298 + else if (((ipack_model_type *) model)->ID[1] != 'P') {
2299 + return EC_NOT_IPMF_MODEL;
2301 + else if (((ipack_model_type *) model)->ID[2] != 'M') {
2302 + return EC_NOT_IPMF_MODEL;
2304 + else if (((ipack_model_type *) model)->ID[3] != 'F') {
2305 + return EC_NOT_IPMF_MODEL;
2308 + bitvector.freebits = 7;
2309 + bitvector.ptr = input;
2310 + bitvector.end = input + sourcelen;
2312 + /*if(*(bitvector.ptr++) != 'H') {
2313 + return EC_NOT_HG_BLOCK;
2314 + } else if(*(bitvector.ptr++) != 'G') {
2315 + return EC_NOT_HG_BLOCK;
2320 + data = (void *) output;
2321 + cntbytes = *(bitvector.ptr++);
2323 + cntbytes += *(bitvector.ptr++);
2325 + { //predictor reset
2326 + register PredictorType *ptr = predctrs;
2327 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS;
2328 + while (ptr < end) {
2332 + for (i = 0; i < CODER_VALUEBITS; ++i) {
2333 + coder_value <<= 1;
2334 + coder_value += ipack_bitvector_R_get1(&bitvector);
2336 + lngth = dstlen >> 2;
2337 + if (lngth > (cntbytes >> 2)) {
2338 + lngth = cntbytes >> 2;
2340 + for (i = 0; (i < lngth); ++i) {
2344 + for (j = 0; j < NUMBER_OF_TOKENS_PER_INSTRUCTION; ++j) {
2345 + ipack_probability_type prob;
2346 + u32 range = coder_high - coder_low + 1;
2348 +#ifdef IPACK_TREE_TO_CODE
2349 + if (treefunc != NULL)
2350 + itoken = (*treefunc) (treeroot, coder_value - coder_low + 1, range, &prob);
2353 + itoken = ipack_model_get_token_for_range(treeroot, coder_value - coder_low + 1, range, &prob);
2356 + if (prob.high == prob.low) {
2357 + return EC_CODER_WRONG_PROBABILITY;
2359 + coder_high = coder_low + ((range * prob.high) >> PROBABILITY_SHIFT) - 1;
2360 + coder_low += ((range * prob.low) >> PROBABILITY_SHIFT);
2362 + if (coder_high < CODER_VALUEHLF) {
2364 + else if (CODER_VALUEHLF <= coder_low) {
2365 + coder_value -= CODER_VALUEHLF;
2366 + coder_high -= CODER_VALUEHLF;
2367 + coder_low -= CODER_VALUEHLF;
2369 + else if ((CODER_VALUE1ST <= coder_low) && (coder_high < CODER_VALUE3RD)) {
2370 + coder_value -= CODER_VALUE1ST;
2371 + coder_high -= CODER_VALUE1ST;
2372 + coder_low -= CODER_VALUE1ST;
2380 + coder_value <<= 1;
2381 + if (bitvector.ptr == bitvector.end) {
2382 + bitvector.freebits = 0;
2384 + coder_value += ((*(bitvector.ptr) >> bitvector.freebits) & 0x01);
2385 + if (bitvector.freebits) {
2386 + --bitvector.freebits;
2389 + bitvector.freebits = 7;
2392 + if (coder_high < coder_low) {
2393 + return EC_CODER_WRONG_RANGE;
2395 + if ((bitvector.ptr == bitvector.end) && !(bitvector.freebits)) {
2396 + if ((coder_overread++) > CODER_VALUEBITS) {
2397 + return EC_BUFFER_UNDERFLOW;
2403 +// register u32 ndx;
2404 + predctrs[0] = itoken;
2405 + if ((('a' <= itoken) && (itoken <= 'z')) || (('A' <= itoken) && (itoken <= 'Z'))) {
2413 + register u32 ndx = predctrs[0] + 1;
2414 + predctrs[ndx + 8] = predctrs[ndx];
2415 + predctrs[ndx] = itoken;
2425 + (*(tptr++)) = itoken;
2429 + if (ipack_glb_endian_X) {
2431 + (*data) = ((*tptr) << 24);
2433 + (*data) |= ((*tptr) << 16);
2435 + (*data) |= ((*tptr) << 8);
2437 + (*data) |= (*tptr);
2440 + (*data) = (((*tptr) & 0xf) << 12);
2442 + (*data) |= ((*tptr) & 0xf);
2444 + (*data) |= (((*tptr) & 0xf) << 28);
2446 + (*data) |= (((*tptr) & 0xf) << 16);
2448 + (*data) |= (((*tptr) & 0xf) << 8);
2450 + (*data) |= (((*tptr) & 0xf) << 20);
2452 + (*data) |= (((*tptr) & 0xf) << 24);
2454 + (*data) |= (((*tptr) & 0xf) << 4);
2461 + (*data) = (*tptr);
2463 + (*data) |= ((*tptr) << 8);
2465 + (*data) |= ((*tptr) << 16);
2467 + (*data) |= ((*tptr) << 24);
2470 + (*data) = (((*tptr) & 0xf) << 20);
2472 + (*data) |= (((*tptr) & 0xf) << 24);
2474 + (*data) |= (((*tptr) & 0xf) << 4);
2476 + (*data) |= (((*tptr) & 0xf) << 8);
2478 + (*data) |= (((*tptr) & 0xf) << 16);
2480 + (*data) |= (((*tptr) & 0xf) << 12);
2482 + (*data) |= ((*tptr) & 0xf);
2484 + (*data) |= (((*tptr) & 0xf) << 28);
2491 + return EC_NO_ERROR;
2494 +static int ipack_armlib_estimate(void *model, unsigned char *input, unsigned long sourcelen, unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime)
2496 + int i, tmp, tmp2, max, maxi;
2497 + int cnt_cond[] = { 0, 0, 0, 0 };
2498 + int cnt_inst[] = { 0, 0, 0, 0 };
2500 + // TODO: make a more precise estimation!!!
2501 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 6;
2502 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 2;
2504 + if (sourcelen % 4 != 0) {
2505 + *dstlen = sourcelen;
2508 + for (i = 0; i < sourcelen; i++, input++) {
2509 + tmp2 = tmp = *input;
2510 + tmp = ((tmp) & 0xf0) >> 4;
2511 + tmp2 = tmp2 & 0xf;
2513 + cnt_cond[i % 4]++;
2514 + if ((tmp2 == 2) || (tmp2 == 3))
2515 + cnt_inst[i % 4]++;
2519 + for (i = 0; i < 4; i++)
2520 + if (max < cnt_cond[i]) {
2521 + max = cnt_cond[i];
2524 + /*jffs2_bbc_print("armlib_EST: %d/%d : %d/%d %d/%d %d/%d %d/%d",
2525 + cnt_cond[maxi],cnt_inst[maxi],
2526 + cnt_cond[0],cnt_inst[0],
2527 + cnt_cond[1],cnt_inst[1],
2528 + cnt_cond[2],cnt_inst[2],
2529 + cnt_cond[3],cnt_inst[3]); */
2531 + if (cnt_cond[maxi] < (sourcelen >> 4)) {
2532 + *dstlen = sourcelen;
2535 + *dstlen = sourcelen / 3;
2541 +static char *ipack_armlib_proc_info(void);
2542 +static int ipack_armlib_proc_command(char *command);
2543 +static void ipack_armlib_destroy_model(void **model);
2545 +struct jffs2_bbc_compressor_type jffs2_bbc_armlib = {
2550 + ipack_armlib_compressor_init, // init_model
2551 + ipack_armlib_destroy_model, // destroy_model
2552 + ipack_armlib_compressor_deinit, // deinit
2553 + ipack_armlib_compress,
2554 + ipack_armlib_estimate,
2555 + ipack_armlib_decompress,
2556 + ipack_armlib_proc_info,
2557 + ipack_armlib_proc_command
2561 +static char *ipack_armlib_proc_info()
2563 +#ifdef IPACK_TREE_TO_CODE
2564 +#ifdef IPACK_AUTO_TREE_TO_CODE
2565 + return "automatic tree to code conversion";
2567 + return "manual tree to code conversion possibility";
2570 + return "tree in memory version";
2574 +static int ipack_armlib_proc_command(char *command)
2576 + struct jffs2_bbc_model_list_node *model;
2577 + ipack_model_type *armlib_model;
2579 + if ((*command == 'g') || (*command == 'G')) {
2580 + jffs2_bbc_print1("Converting tree(s) to ARM code... (keeping original)\n");
2581 + model = jffs2_bbc_armlib.models;
2582 + if (model == NULL)
2583 + jffs2_bbc_print1("no model found!\n");
2584 + while (model != NULL) {
2585 + armlib_model = model->model;
2586 + if (armlib_model == NULL) {
2587 + jffs2_bbc_print1("Error: NULL model!\n");
2590 + ipack_armlib_convert_tree_to_code(armlib_model, IPACK_TREE_CONVERT_KEEP);
2592 + model = model->next_compr_model;
2595 + else if ((*command == 'r') || (*command == 'R')) {
2596 + jffs2_bbc_print1("Converting tree(s) to ARM code... (deleting original)\n");
2597 + model = jffs2_bbc_armlib.models;
2598 + if (model == NULL)
2599 + jffs2_bbc_print1("no model found!\n");
2600 + while (model != NULL) {
2601 + armlib_model = model->model;
2602 + if (armlib_model == NULL) {
2603 + jffs2_bbc_print1("Error: NULL model!\n");
2606 + //armlib_model->tree_code = ipack_tree_to_code(armlib_model, &tree_size);
2607 + //jffs2_bbc_print("Convertation done. Code size=%d\n",tree_size);
2608 + ipack_armlib_convert_tree_to_code(armlib_model, IPACK_TREE_CONVERT_REPLACE);
2610 + model = model->next_compr_model;
2613 + else if ((*command == 'c') || (*command == 'C')) {
2614 + jffs2_bbc_print1("Deleting ARM representation of the tree(s)...\n");
2615 + model = jffs2_bbc_armlib.models;
2616 + if (model == NULL)
2617 + jffs2_bbc_print1("no model found!\n");
2618 + while (model != NULL) {
2619 + armlib_model = model->model;
2620 + if (armlib_model == NULL) {
2621 + jffs2_bbc_print1("Error: NULL model!\n");
2624 + if (armlib_model->tree_code == NULL) {
2625 + jffs2_bbc_print1("already deleted.\n");
2628 + if (armlib_model->tree_root_ptr == NULL) {
2629 + jffs2_bbc_print1("cannot delete this ARM tree - original tree has deleted\n");
2632 + jffs2_bbc_print1("deleting...");
2633 + jffs2_bbc_free(armlib_model->tree_code);
2634 + armlib_model->tree_code = NULL;
2635 + jffs2_bbc_print1("done.\n");
2639 + model = model->next_compr_model;
2642 + else if (*command == '?') {
2643 + jffs2_bbc_print1("ARMLIB commands:\n");
2644 + jffs2_bbc_print1(" g: convert TREEs to ARM code and keep the original\n");
2645 + jffs2_bbc_print1(" r: convert TREEs to ARM code and remove the original\n");
2646 + jffs2_bbc_print1(" c: delete the original TREEs - if there is any\n");
2649 + jffs2_bbc_print1("Unknown command.\n");
2654 +static void ipack_armlib_destroy_model(void **model)
2656 + ipack_model_type *model_img;
2658 + model_img = *model;
2659 + if (model_img == NULL) {
2660 + jffs2_bbc_print1("jffs2.bbc: armlib: NULL model at destoying model!\n");
2663 + if (model_img->tree_code != NULL) {
2664 + //jffs2_bbc_print1("jffs2.bbc: armlib: debug: freeing code...\n");
2665 + jffs2_bbc_free(model_img->tree_code);
2666 + model_img->tree_code = NULL;
2668 + if (model_img->tree_root_ptr != NULL) {
2669 + //jffs2_bbc_print1("jffs2.bbc: armlib: debug: freeing tree...\n");
2670 + jffs2_bbc_free(model_img->tree_root_ptr);
2671 + model_img->tree_root_ptr = NULL;
2674 + jffs2_bbc_free(model_img);
2678 +struct jffs2_bbc_compressor_type *jffs2_bbc_armlib_init(int mode)
2680 + if (jffs2_bbc_register_compressor(&jffs2_bbc_armlib) == 0)
2681 + return &jffs2_bbc_armlib;
2686 +void jffs2_bbc_armlib_deinit(void)
2688 + jffs2_bbc_unregister_compressor(&jffs2_bbc_armlib);
2692 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_framework.c
2693 ===================================================================
2695 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_framework.c
2698 + * JFFS2-BBC: Compression Framework
2700 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
2702 + * Copyright (C) 2004, Ferenc Havasi
2704 + * This program is free software; you can redistribute it and/or
2705 + * modify it under the terms of the GNU General Public License
2706 + * as published by the Free Software Foundation; either version 2
2707 + * of the License, or (at your option) any later version.
2709 + * This program is distributed in the hope that it will be useful,
2710 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2711 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2712 + * GNU General Public License for more details.
2714 + * You should have received a copy of the GNU General Public License
2715 + * along with this program; if not, write to the Free Software
2716 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2720 +/* USE JFFS2_BBC_STANDALONE define if you don't want to compile without JFFS2 */
2722 +//#define DEBUG_COMPRESSORS
2723 +//#define DEBUG_SHOW_BLOCK_SIZES
2725 +#define JFFS2_BBC_STAT_BUFF_SIZE 8000
2730 +#include <malloc.h>
2731 +typedef unsigned long uint32_t;
2735 +#include <linux/kernel.h>
2736 +#include <linux/slab.h>
2737 +#include <linux/vmalloc.h>
2741 +#define JFFS2_BBC_ZLIB_BLOCK_SIGN_0 (120)
2742 +#define JFFS2_BBC_ZLIB_BLOCK_SIGN_1 (94)
2744 +#define JFFS2_BBC_DUMMY_BLOCKSIGN_0 (0x54)
2745 +#define JFFS2_BBC_DUMMY_BLOCKSIGN_1 (0x01)
2748 +#define NULL ((void*)(0))
2751 +#include "jffs2_bbc_framework.h"
2753 +/*********************************************************************
2755 + *********************************************************************/
2757 +static int jffs2_bbc_compression_mode = JFFS2_BBC_ZLIB_MODE;
2758 +static struct jffs2_bbc_compressor_type *jffs2_bbc_manual_compressor = NULL;
2759 +static struct jffs2_bbc_compressor_type *jffs2_bbc_compressors = NULL;
2760 +static struct jffs2_bbc_model_list_node *jffs2_bbc_model_list = NULL;
2761 +static void *last_sb = NULL; /* previously activated sb */
2763 +/*********************************************************************
2764 + * Compressor initialization *
2765 + *********************************************************************/
2767 +#ifndef JFFS2_BBC_STANDALONE
2769 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2770 +struct jffs2_bbc_compressor_type *jffs2_bbc_armlib_init(void);
2771 +void jffs2_bbc_armlib_deinit(void);
2774 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2775 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzo_init(void);
2776 +void jffs2_bbc_lzo_deinit(void);
2779 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2780 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzss_init(void);
2781 +void jffs2_bbc_lzss_deinit(void);
2784 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2785 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzari_init(void);
2786 +void jffs2_bbc_lzari_deinit(void);
2789 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2790 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzhd_init(void);
2791 +void jffs2_bbc_lzhd_deinit(void);
2794 +void jffs2_bbc_compressor_init()
2796 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2797 + jffs2_bbc_armlib_init();
2799 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2800 + jffs2_bbc_lzo_init();
2802 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2803 + jffs2_bbc_lzss_init();
2805 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2806 + jffs2_bbc_lzari_init();
2808 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2809 + jffs2_bbc_lzhd_init();
2813 +void jffs2_bbc_compressor_deinit()
2815 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2816 + jffs2_bbc_lzhd_deinit();
2818 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2819 + jffs2_bbc_lzari_deinit();
2821 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2822 + jffs2_bbc_lzss_deinit();
2824 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2825 + jffs2_bbc_lzo_deinit();
2827 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2828 + jffs2_bbc_armlib_deinit();
2834 +#ifndef JFFS2_BBC_STANDALONE
2836 +/*********************************************************************
2837 + * ZLIB COMPRESSOR *
2838 + *********************************************************************/
2840 +extern int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out, uint32_t * sourcelen, uint32_t * dstlen);
2841 +extern void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen);
2843 +static int jffs2_bbc_zlib_compress(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen)
2845 + return jffs2_zlib_compress2(input, output, sourcelen, dstlen);
2848 +static int jffs2_bbc_zlib_decompress(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen)
2850 + jffs2_zlib_decompress2(input, output, sourcelen, dstlen);
2854 +static int jffs2_bbc_zlib_estimate(void *model, unsigned char *input, unsigned long sourcelen, unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime)
2856 + *dstlen = sourcelen * 65 / 100;
2857 + *readtime = JFFS2_BBC_ZLIB_READ_TIME;
2858 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME;
2862 +static struct jffs2_bbc_compressor_type jffs2_bbc_zlib = {
2865 + {JFFS2_BBC_ZLIB_BLOCK_SIGN_0, JFFS2_BBC_ZLIB_BLOCK_SIGN_1, 0, 0},
2870 + jffs2_bbc_zlib_compress,
2871 + jffs2_bbc_zlib_estimate,
2872 + jffs2_bbc_zlib_decompress,
2878 +static struct jffs2_bbc_compressor_type *jffs2_bbc_original_compressor = &jffs2_bbc_zlib;
2882 +/*********************************************************************
2883 + * Compression mode handling *
2884 + *********************************************************************/
2886 +int jffs2_bbc_get_compression_mode(void)
2888 + return jffs2_bbc_compression_mode;
2891 +void jffs2_bbc_set_compression_mode(int mode)
2893 + jffs2_bbc_compression_mode = mode;
2896 +void jffs2_bbc_set_manual_compressor(struct jffs2_bbc_compressor_type *c)
2898 + jffs2_bbc_manual_compressor = c;
2899 + jffs2_bbc_set_compression_mode(JFFS2_BBC_MANUAL_MODE);
2902 +int jffs2_bbc_set_manual_compressor_by_name(char *name)
2904 + struct jffs2_bbc_compressor_type *l;
2907 + l = jffs2_bbc_compressors;
2908 + while (l != NULL) {
2909 + for (i = 0; i < 1000; i++) {
2910 + if (l->name[i] == 0) {
2911 + jffs2_bbc_set_manual_compressor(l);
2914 + else if (name[i] == 0)
2916 + else if (name[i] != l->name[i])
2921 + jffs2_bbc_set_manual_compressor(NULL);
2925 +static struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_by_name(char *name)
2927 + struct jffs2_bbc_compressor_type *l;
2930 +#ifndef JFFS2_BBC_STANDALONE
2931 + l = jffs2_bbc_original_compressor;
2932 + for (i = 0; i < 1000; i++) {
2933 + if (l->name[i] == 0) {
2936 + else if (name[i] == 0)
2938 + else if (name[i] != l->name[i])
2943 + l = jffs2_bbc_compressors;
2944 + while (l != NULL) {
2945 + for (i = 0; i < 1000; i++) {
2946 + if (l->name[i] == 0) {
2949 + else if (name[i] == 0)
2951 + else if (name[i] != l->name[i])
2960 +int jffs2_bbc_disable_compressor_by_name(char *name)
2962 + struct jffs2_bbc_compressor_type *l;
2964 + l = jffs2_bbc_get_compressor_by_name(name);
2965 + if (l == NULL) return 1;
2970 +int jffs2_bbc_enable_compressor_by_name(char *name)
2972 + struct jffs2_bbc_compressor_type *l;
2974 + l = jffs2_bbc_get_compressor_by_name(name);
2975 + if (l == NULL) return 1;
2980 +void jffs2_bbc_compressor_command_by_name(char *name_and_command)
2982 + struct jffs2_bbc_compressor_type *l;
2985 + l = jffs2_bbc_compressors;
2986 + while (l != NULL) {
2987 + for (i = 0; i < 1000; i++) {
2988 + if (l->name[i] == 0) {
2989 + if (name_and_command[i] != ':') {
2990 + jffs2_bbc_print1("jffs2.bbc: ':' missing after compressor name\n");
2993 + if (l->proc_command != NULL)
2994 + l->proc_command(name_and_command + i + 1);
2999 + else if (name_and_command[i] == 0) {
3002 + else if (name_and_command[i] != l->name[i]) {
3010 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_manual_compressor(void)
3012 + if (jffs2_bbc_get_compression_mode() != JFFS2_BBC_MANUAL_MODE) {
3013 + jffs2_bbc_manual_compressor = NULL;
3015 + return jffs2_bbc_manual_compressor;
3018 +/*********************************************************************
3019 + * Compressor handling *
3020 + *********************************************************************/
3022 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_list(void)
3024 + return jffs2_bbc_compressors;
3027 +struct jffs2_bbc_model_list_node *jffs2_bbc_get_model_list(void)
3029 + return jffs2_bbc_model_list;
3032 +int jffs2_bbc_register_compressor(struct jffs2_bbc_compressor_type *c)
3034 + struct jffs2_bbc_compressor_type *l;
3035 + struct jffs2_bbc_model_list_node *l2;
3036 + int model_found = 0;
3038 + l = jffs2_bbc_compressors;
3039 + /* Check for confilcts */
3040 + while (l != NULL) {
3042 + /*if (strcmp(c->name,l->name)==0) {
3043 + jffs2_bbc_print1("jffs2.bbc: compressor is already loaded.");
3046 + if ((l->model_file_sign == c->model_file_sign) && (c->model_file_sign != 0)) {
3047 + jffs2_bbc_print1("jffs2.bbc: already used model file sign. fail.");
3052 + /* Search and initialize model */
3055 + if (c->init != NULL) {
3056 + if (c->init() != 0) {
3057 + jffs2_bbc_print2("jffs2.bbc: cannot initialize compressor %s.\n", c->name);
3061 + if (c->model_file_sign != 0) {
3062 + l2 = jffs2_bbc_model_list;
3066 + if (c->model_file_sign == l2->sign) {
3067 + if (l2->compressor != NULL) {
3068 + jffs2_bbc_print2("jffs2.bbc: register for %s: BUG, model file already reserved!!!!\n", c->name);
3071 + if (c->init_model(&(l2->model)) != 0) {
3072 + jffs2_bbc_print2("jffs2.bbc: cannot initialize compressor %s for a model", c->name);
3075 + l2->compressor = c;
3076 + l2->next_compr_model = c->models;
3083 + l2 = l2->next_model;
3085 + /*if (model_found==0) {
3086 + jffs2_bbc_print2("jffs2.bbc: no macthing model file found for %s at this time (maybe later)\n",c->name);
3089 + /* Insert to the end of the compressor list */
3092 + c->buffer_size = 0;
3093 + c->stat_compr_orig = c->stat_compr_new = c->stat_decompr = 0;
3095 + if (jffs2_bbc_compressors == NULL) {
3096 + jffs2_bbc_compressors = c;
3099 + l = jffs2_bbc_compressors;
3100 + while (l->next != NULL)
3107 +int jffs2_bbc_unregister_compressor(struct jffs2_bbc_compressor_type *c)
3109 + struct jffs2_bbc_compressor_type *l;
3110 + struct jffs2_bbc_model_list_node *l2;
3112 + if (c->mounted != 0) {
3113 + jffs2_bbc_print1("jffs2.bbc: Compressor is in use. Sorry.");
3116 + if (jffs2_bbc_compressors == NULL) {
3117 + jffs2_bbc_print1("jffs2.bbc: unregister: empty list.");
3120 + else if (jffs2_bbc_compressors == c) {
3121 + if (c->deinit != NULL)
3123 + jffs2_bbc_compressors = c->next;
3126 + l = jffs2_bbc_compressors;
3127 + while (l->next != c) {
3128 + if (l->next == NULL) {
3129 + jffs2_bbc_print2("jffs2.bbc: unregister: cannot find compressor %s in the list.", c->name);
3134 + if (c->deinit != NULL)
3136 + l->next = c->next;
3138 + if (c->buffer != NULL) {
3139 + jffs2_bbc_free(c->buffer);
3141 + c->buffer_size = 0;
3144 + l2 = jffs2_bbc_model_list;
3145 + while (l2 != NULL) {
3146 + if (l2->compressor == c) {
3147 + jffs2_bbc_print1("jffs2.bbc: unregister: BUG: model found!!!");
3148 + l2->compressor = NULL;
3149 + l2->next_compr_model = NULL;
3151 + l2 = l2->next_model;
3157 +int jffs2_bbc_model_new(void *sb, int i_num, void *model)
3159 + struct jffs2_bbc_model_list_node *node;
3160 + struct jffs2_bbc_compressor_type *l;
3161 + char block_sign[2];
3165 + /* check for conflicts... */
3166 + sign = *((int *) model);
3167 + block_sign[0] = *(((char *) model) + 4);
3168 + block_sign[1] = *(((char *) model) + 5);
3169 + node = jffs2_bbc_model_list;
3170 + while (node != NULL) {
3171 + if ((node->block_sign[0] == block_sign[0]) && (node->block_sign[1] == block_sign[1]) && (node->sb == sb)) {
3172 + //jffs2_bbc_print2("jffs2.bbc: model_new: model conflict (inode=%d)!\n",i_num);
3175 + node = node->next_model;
3179 + node = jffs2_bbc_malloc_small((long)sizeof(struct jffs2_bbc_model_list_node));
3181 + node->model = model;
3182 + node->sign = *((int *) model);
3183 + node->block_sign[0] = *(((char *) model) + 4);
3184 + node->block_sign[1] = *(((char *) model) + 5);
3185 + node->inode = i_num;
3186 + node->next_model = jffs2_bbc_model_list;
3187 + node->compressor = NULL;
3188 + node->stat_decompr = 0;
3189 + node->next_compr_model = NULL;
3190 + jffs2_bbc_model_list = node;
3192 + /* search for matching compressor */
3193 + l = jffs2_bbc_compressors;
3194 + while (l != NULL) {
3195 + if (l->model_file_sign == sign) {
3196 + //jffs2_bbc_print2("jffs2.bbc: compressor for model found: %s ",l->name);
3197 + if (l->init_model(&(node->model)) != 0) {
3198 + jffs2_bbc_print1("jffs2.bbc: cannot initialize compressor for a model");
3202 + node->compressor = l;
3203 + node->next_compr_model = l->models;
3213 +static void jffs2_bbc_model_del_from_compressor(struct jffs2_bbc_model_list_node *node)
3215 + struct jffs2_bbc_model_list_node *l;
3217 + if (node->model != NULL) {
3218 + if (node->compressor != NULL) {
3219 + if (node->compressor->destroy_model == NULL) {
3220 + jffs2_bbc_free(node->model);
3221 + node->model = NULL;
3224 + node->compressor->destroy_model(&(node->model));
3225 + if (node->model != NULL)
3226 + jffs2_bbc_print1("jffs2.bbc: warning: not NULL model after destroying!\n");
3231 + if (node->compressor == NULL) {
3232 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor: no compressor!\n");
3235 + l = node->compressor->models;
3237 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor error, models=NULL!\n");
3241 + node->compressor->models = node->next_compr_model;
3242 + node->compressor->mounted--;
3246 + if (l->next_compr_model == node) {
3247 + l->next_compr_model = node->next_compr_model;
3248 + node->compressor->mounted--;
3251 + l = l->next_compr_model;
3253 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor: not found\n");
3259 +void jffs2_bbc_model_del(void *sb)
3261 + struct jffs2_bbc_model_list_node *l, *l2;
3263 + l = jffs2_bbc_model_list;
3266 + if (l->sb == sb) {
3267 + jffs2_bbc_model_list = l->next_model;
3268 + jffs2_bbc_model_del_from_compressor(l);
3269 + jffs2_bbc_free_small(l);
3270 + jffs2_bbc_model_del(sb);
3274 + if (l->next_model == NULL) {
3277 + if (l->next_model->sb == sb) {
3278 + l2 = l->next_model;
3279 + l->next_model = l->next_model->next_model;
3280 + jffs2_bbc_model_del_from_compressor(l2);
3281 + jffs2_bbc_free_small(l2);
3282 + jffs2_bbc_model_del(sb);
3285 + l = l->next_model;
3290 +void jffs2_bbc_model_set_act_sb(void *sb)
3295 +void *jffs2_bbc_model_get_act_sb(void)
3300 +void *jffs2_bbc_model_get_newest(struct jffs2_bbc_compressor_type *compressor)
3302 + struct jffs2_bbc_model_list_node *m, *best_m;
3303 + int max_sign, sign;
3305 + if (compressor == NULL) {
3306 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_get: NULL!!\n");
3312 + m = compressor->models;
3313 + while (m != NULL) {
3314 + if (m->sb == last_sb) {
3315 + sign = (int) (m->block_sign[0]) * 256 + (int) (m->block_sign[1]);
3316 + if (sign > max_sign) {
3321 + m = m->next_compr_model;
3323 + if (best_m != NULL)
3324 + return best_m->model;
3329 +/*********************************************************************
3331 + *********************************************************************/
3333 +static char *jffs2_bbc_stat_buff = NULL;
3335 +char *jffs2_bbc_get_model_stats(void)
3338 + struct jffs2_bbc_model_list_node *m;
3339 + struct jffs2_bbc_compressor_type *c;
3341 + if (jffs2_bbc_stat_buff == NULL)
3342 + jffs2_bbc_stat_buff = jffs2_bbc_malloc(8000);
3344 + b = jffs2_bbc_stat_buff;
3346 + b += sprintf(b, "Loaded compressors:");
3347 + c = jffs2_bbc_compressors;
3348 + while (c != NULL) {
3349 + b += sprintf(b, "\n %s (%d) ", c->name, c->enabled);
3350 + if (c->model_file_sign != 0) {
3351 + b += sprintf(b, "m_sign=%d ", c->model_file_sign);
3352 + b += sprintf(b, "models=");
3354 + while (m != NULL) {
3355 + b += sprintf(b, "(inode=%d)", m->inode);
3356 + m = m->next_compr_model;
3360 + b += sprintf(b, "b_sign=(%d,%d) nomodel", (int) (c->block_sign[0]), (int) (c->block_sign[1]));
3362 + if (c->proc_info != NULL) {
3363 + b += sprintf(b, "\n %s", c->proc_info());
3368 + m = jffs2_bbc_model_list;
3371 + b += sprintf(b, "\nPresent models: NONE\n");
3374 + b += sprintf(b, "\nPresent models:\n");
3375 + while (m != NULL) {
3376 + 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);
3377 + if (m->compressor == NULL)
3378 + b += sprintf(b, ",compressor=NULL\n");
3380 + b += sprintf(b, ",compressor=%s\n", m->compressor->name);
3381 + m = m->next_model;
3385 + return jffs2_bbc_stat_buff;
3388 +/*********************************************************************
3389 + * Memory handling, debug *
3390 + *********************************************************************/
3392 +static int jffs2_bbc_mem_counter = 0;
3396 +void *jffs2_bbc_malloc(long size)
3398 + void *addr = vmalloc(size);
3400 + jffs2_bbc_mem_counter++;
3402 + jffs2_bbc_print2("DEBUG: not enough memory (%ld)\n", size);
3407 +void jffs2_bbc_free(void *addr)
3409 + jffs2_bbc_mem_counter--;
3413 +void *jffs2_bbc_malloc_small(long size)
3416 + addr = kmalloc(size, 0);
3418 + jffs2_bbc_mem_counter++;
3422 +void jffs2_bbc_free_small(void *addr)
3424 + jffs2_bbc_mem_counter--;
3430 +void *jffs2_bbc_malloc(long size)
3432 + void *addr = malloc(size);
3434 + jffs2_bbc_mem_counter++;
3438 +void jffs2_bbc_free(void *addr)
3440 + jffs2_bbc_mem_counter--;
3444 +void *jffs2_bbc_malloc_small(long size)
3446 + return jffs2_bbc_malloc(size);
3449 +void jffs2_bbc_free_small(void *addr)
3451 + jffs2_bbc_free(addr);
3456 +int jffs2_bbc_test_memory_counter(int verbose)
3458 + if (verbose > 0) {
3459 + jffs2_bbc_print2("jffs2.bbc: mem_counter=%d!\n", jffs2_bbc_mem_counter);
3461 + return jffs2_bbc_mem_counter;
3464 +int jffs2_bbc_get_memory_counter(void)
3466 + return jffs2_bbc_mem_counter;
3469 +static char mem_stat[200];
3471 +char *jffs2_bbc_get_mem_stats(void)
3473 + sprintf(mem_stat, "Memcounter=%d\n", jffs2_bbc_mem_counter);
3477 +void jffs2_bbc_print_flush(void)
3487 +/*********************************************************************
3488 + * FRAMEWORK - ZLIB REPLACEMENT *
3489 + *********************************************************************/
3491 +#ifndef JFFS2_BBC_STANDALONE
3493 +/* Temorary buffers */
3494 +static char stat_str[JFFS2_BBC_STAT_BUFF_SIZE];
3495 +static int tmp_buffer_size = 0;
3496 +static char *tmp_buffer = NULL;
3498 +/* Statistic - used by /proc/jffs2_bbc and mkfs.jffs2 */
3499 +char *jffs2_bbc_get_compr_stats(void)
3501 + struct jffs2_bbc_compressor_type *l;
3502 + char *s = stat_str;
3504 + s += sprintf(s, "Compression statistics:\n");
3505 + l = jffs2_bbc_original_compressor;
3506 + //s += sprintf(s, " zlib: compr=%d/%d decompr=%d\n", stat_zlib_compr_new, stat_zlib_compr_orig, stat_zlib_decompr);
3507 + s += sprintf(s, " %s: compr=%d/%d decompr=%d\n", l->name, l->stat_compr_new, l->stat_compr_orig, l->stat_decompr);
3508 + l = jffs2_bbc_get_compressor_list();
3509 + while (l != NULL) {
3510 + s += sprintf(s, " %s: compr=%d/%d decompr=%d\n", l->name, l->stat_compr_new, l->stat_compr_orig, l->stat_decompr);
3516 +static void jffs2_bbc_buffer_fill(unsigned char *buff, int size)
3518 + for (; size > 0; size--, buff++)
3523 +static int jffs2_bbc_update_compr_buf(unsigned long size)
3525 + struct jffs2_bbc_compressor_type *l;
3529 + if (tmp_buffer == NULL) {
3530 + tmp_buffer = jffs2_bbc_malloc(size);
3531 + jffs2_bbc_buffer_fill(tmp_buffer, size);
3532 + tmp_buffer_size = size;
3534 + else if (tmp_buffer_size < size) {
3535 + jffs2_bbc_free(tmp_buffer);
3536 + tmp_buffer = jffs2_bbc_malloc(size);
3537 + jffs2_bbc_buffer_fill(tmp_buffer, size);
3538 + tmp_buffer_size = size;
3540 + l = jffs2_bbc_get_compressor_list();
3541 + while (l != NULL) {
3542 + if (l->buffer == NULL) {
3543 + l->buffer_size = size;
3544 + l->buffer = jffs2_bbc_malloc(size);
3545 + jffs2_bbc_buffer_fill(l->buffer, size);
3547 + else if (l->buffer_size < size) {
3548 + jffs2_bbc_free(l->buffer);
3549 + l->buffer_size = size;
3550 + l->buffer = jffs2_bbc_malloc(size);
3551 + jffs2_bbc_buffer_fill(l->buffer, size);
3558 +#ifdef DEBUG_COMPRESSORS
3560 +static unsigned char *debug_tmp_buff = NULL;
3561 +static long debug_orig_srclen = -1;
3562 +static long debug_orig_dstlen = -1;
3563 +static int debug_mem_counter = -1;
3566 +void debug_before_compress(struct jffs2_bbc_compressor_type *c, void *model, unsigned char *input, unsigned char *output, long *sourcelen, long *dstlen)
3569 + debug_orig_srclen = *sourcelen; // for buffer overflow test
3570 + debug_orig_dstlen = *dstlen; // for buffer overflow test
3571 + output[debug_orig_dstlen + 1] = 255;
3573 + debug_mem_counter = jffs2_bbc_get_memory_counter(); // for memory guard
3576 +void debug_after_compress(struct jffs2_bbc_compressor_type *c, int back, void *model, unsigned char *input, unsigned char *output, long *sourcelen, long *dstlen)
3578 + long dst_len = *dstlen;
3579 + long src_len = *sourcelen;
3583 + if (debug_mem_counter != jffs2_bbc_get_memory_counter()) {
3584 + jffs2_bbc_print4("!!!!!!!! %s error: possible COMPRESSOR MEMORY LEAK: %d->%d\n", c->name, debug_mem_counter, jffs2_bbc_get_memory_counter());
3585 + debug_mem_counter = jffs2_bbc_get_memory_counter();
3588 + // Buffer overflow test
3589 + if (output[debug_orig_dstlen + 1] != 255) {
3590 + 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);
3593 + // Decompression check
3595 + if (debug_tmp_buff == NULL)
3596 + debug_tmp_buff = jffs2_bbc_malloc(17000);
3597 + for (i = 0; i < src_len; i++) debug_tmp_buff[i] = 0xf6;
3598 + c->decompress(model, output, debug_tmp_buff, dst_len, src_len);
3599 + // Memory guard for decompressor
3600 + if (debug_mem_counter != jffs2_bbc_get_memory_counter()) {
3601 + jffs2_bbc_print4("!!!!!!!! %s error: possible DECOMPRESSOR MEMORY LEAK: %d->%d\n", c->name, debug_mem_counter, jffs2_bbc_get_memory_counter());
3602 + debug_mem_counter = jffs2_bbc_get_memory_counter();
3605 + for (i = 0; i < src_len; i++)
3606 + if (input[i] != debug_tmp_buff[i]) {
3607 + 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);
3613 + // Return value test
3614 + //jffs2_bbc_print3("!!!!!!!! %s error: %d !!!!!!!!!!!!\n", c->name, back);
3619 +int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, uint32_t * sourcelen, uint32_t * dstlen)
3621 + struct jffs2_bbc_compressor_type *c;
3622 + int back, back_zlib, mode, min, i, i2;
3623 + long tmp = 0, tmp_read_time = 1000, tmp_write_time = 1000, orig_src, orig_dest, src, dest;
3624 + struct jffs2_bbc_model_list_node *m;
3626 + unsigned char *tmp_p = NULL;
3628 + sb = jffs2_bbc_model_get_act_sb();
3630 + orig_src = *sourcelen;
3631 + orig_dest = *dstlen;
3633 + mode = jffs2_bbc_get_compression_mode();
3635 + if (mode == JFFS2_BBC_DUMMY_MODE) {
3638 + cpage_out[i++]=JFFS2_BBC_DUMMY_BLOCKSIGN_0;
3639 + cpage_out[i++]=JFFS2_BBC_DUMMY_BLOCKSIGN_1;
3642 + for (;((i < *dstlen) && (i < (*sourcelen)+i2));i++) {
3643 + cpage_out[i] = data_in[i-i2];
3650 + if (mode == JFFS2_BBC_ZLIB_MODE) {
3651 + /*if (!jffs2_bbc_original_compressor->enabled) {
3652 + jffs2_bbc_print2("jffs2.bbc: WARNING: ZLIB mode but %s disabled! Enabling for this procedure...\n",jffs2_bbc_original_compressor->name);
3654 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3655 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3656 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3660 + jffs2_bbc_update_compr_buf(orig_dest);
3662 + if (mode == JFFS2_BBC_SIZE_MODE) {
3663 + // Testing all compressors
3664 + if (!jffs2_bbc_original_compressor->enabled) {
3668 + back_zlib = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3671 + c = jffs2_bbc_get_compressor_list();
3672 + while (c != NULL) {
3673 + c->buffer_cnt = -1;
3674 + if (c->enabled == 0) {
3678 + if (c->model_file_sign == 0) {
3681 +#ifdef DEBUG_COMPRESSORS
3682 + debug_before_compress(c, NULL, data_in, c->buffer, &src, &dest);
3684 + back = c->compress(NULL, data_in, c->buffer, &src, &dest);
3685 +#ifdef DEBUG_COMPRESSORS
3686 + debug_after_compress(c, back, NULL, data_in, c->buffer, &src, &dest);
3689 + c->buffer_cnt = dest;
3690 + if ((min < 0) || (min > dest))
3696 + while (m != NULL) {
3699 + if (m->sb == sb) {
3700 + if (c->buffer_cnt == -1) {
3701 +#ifdef DEBUG_COMPRESSORS
3702 + debug_before_compress(c, m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3704 + back = c->compress(m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3705 +#ifdef DEBUG_COMPRESSORS
3706 + debug_after_compress(c, back, m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3709 + c->buffer_cnt = dest;
3710 + if ((min < 0) || (min > dest))
3715 +#ifdef DEBUG_COMPRESSORS
3716 + debug_before_compress(c, m->model, data_in, tmp_buffer, &src, &dest);
3718 + back = c->compress(m->model, data_in, tmp_buffer, &src, &dest);
3719 +#ifdef DEBUG_COMPRESSORS
3720 + debug_after_compress(c, back, m->model, data_in, tmp_buffer, &src, &dest);
3723 + if (c->buffer_cnt > dest) {
3724 + c->buffer_cnt = dest;
3725 + tmp_p = c->buffer;
3726 + c->buffer = tmp_buffer;
3727 + tmp_buffer = tmp_p;
3728 + if ((min < 0) || (min > dest))
3734 + m = m->next_compr_model;
3739 + //Finding the best and copy its result
3741 +#ifdef DEBUG_SHOW_BLOCK_SIZES
3742 + jffs2_bbc_print1("\n");
3743 + if (jffs2_bbc_original_compressor->enabled) {
3744 + if (min == *dstlen) {
3745 + jffs2_bbc_print3("%s:%d* ", jffs2_bbc_original_compressor->name, (int) (*dstlen));
3748 + jffs2_bbc_print3("%s:%d ", jffs2_bbc_original_compressor->name, (int) (*dstlen));
3751 + c = jffs2_bbc_get_compressor_list();
3752 + while (c != NULL) {
3753 + if (c->enabled == 0) {
3757 + if (c->buffer_cnt == min)
3758 + jffs2_bbc_print3("%s:%d* ", c->name, c->buffer_cnt);
3760 + jffs2_bbc_print3("%s:%d ", c->name, c->buffer_cnt);
3766 + return -1; // none of compressors work (maybe too short output buffer)
3769 + if (jffs2_bbc_original_compressor->enabled) {
3770 + if (min == *dstlen) {
3771 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3772 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3777 + c = jffs2_bbc_get_compressor_list();
3778 + while (c != NULL) {
3779 + if (c->enabled == 0) {
3783 + if (c->buffer_cnt == min) {
3784 + *dstlen = c->buffer_cnt;
3785 + *sourcelen = orig_src;
3786 + for (i = 0; i < *dstlen; i++) {
3787 + cpage_out[i] = c->buffer[i];
3789 + c->stat_compr_orig += *sourcelen;
3790 + c->stat_compr_new += *dstlen;
3795 + jffs2_bbc_print1("jffs2.bbc: compr (full): BUG!!!\n");
3799 + if ((mode == JFFS2_BBC_FASTR_MODE)||(mode == JFFS2_BBC_FASTW_MODE)||(mode == JFFS2_BBC_FASTS_MODE)) {
3800 + // Estimating all compressors
3801 + if (jffs2_bbc_original_compressor->enabled) {
3802 + back = jffs2_bbc_original_compressor->estimate(NULL, data_in, *sourcelen, &tmp, &tmp_read_time, &tmp_write_time);
3806 + tmp_read_time = -1;
3807 + tmp_write_time = -1;
3809 + if (mode == JFFS2_BBC_FASTR_MODE) tmp = tmp_read_time;
3810 + if (mode == JFFS2_BBC_FASTW_MODE) tmp = tmp_write_time;
3812 + c = jffs2_bbc_get_compressor_list();
3813 + while (c != NULL) {
3816 + c->buffer_cnt = -1;
3817 + if (c->enabled == 0) {
3821 + if ((c->model_file_sign == 0) || (jffs2_bbc_model_get_newest(c) != NULL)) {
3822 + back = c->estimate(jffs2_bbc_model_get_newest(c), data_in, src, &dest, &tmp_read_time, &tmp_write_time);
3823 + if (mode == JFFS2_BBC_FASTR_MODE) dest = tmp_read_time;
3824 + if (mode == JFFS2_BBC_FASTW_MODE) dest = tmp_write_time;
3826 + c->buffer_cnt = dest;
3827 + if ((min < 0) || (min > dest))
3831 + c->buffer_cnt = -1;
3836 + // Finding the best and compress with it
3840 + if (jffs2_bbc_original_compressor->enabled) {
3842 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3843 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3844 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3848 + c = jffs2_bbc_get_compressor_list();
3849 + while (c != NULL) {
3850 + if (c->enabled == 0) {
3854 + if (c->buffer_cnt == min) {
3855 + back = c->compress(jffs2_bbc_model_get_newest(c), data_in, cpage_out, (unsigned long*)sourcelen, (unsigned long*)dstlen);
3856 + if ((back == 0) && (*dstlen < orig_dest) && (*dstlen > 4)) {
3857 + c->stat_compr_orig += *sourcelen;
3858 + c->stat_compr_new += *dstlen;
3860 + else { // fallback will always be available
3861 + *sourcelen = orig_src;
3862 + *dstlen = orig_dest;
3863 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3864 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3865 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3872 + jffs2_bbc_print1("jffs2.bbc: compress (fastX mode): BUG!!!\n");
3876 + if (mode == JFFS2_BBC_MANUAL_MODE) {
3877 + c = jffs2_bbc_get_manual_compressor();
3879 + if (c->model_file_sign == 0) {
3882 + back = c->compress(NULL, data_in, cpage_out, &src, &dest);
3886 + c->stat_compr_orig += *sourcelen;
3887 + c->stat_compr_new += *dstlen;
3892 + c->buffer_cnt = -1;
3895 + while (m != NULL) {
3898 + if (m->sb == sb) {
3900 + back = c->compress(m->model, data_in, cpage_out, (unsigned long*)sourcelen, (unsigned long*)dstlen);
3901 + if ((back == 0) && (*dstlen < orig_dest) && (*dstlen > 4)) {
3903 + tmp_p = cpage_out;
3907 + back = c->compress(m->model, data_in, tmp_buffer, &src, &dest);
3908 + if ((back == 0) && (dest < orig_dest) && (dest > 4)) {
3909 + if (c->buffer_cnt > dest) {
3912 + tmp_p = tmp_buffer;
3918 + m = m->next_compr_model;
3921 + if (tmp_p != cpage_out) {
3922 + for (i = 0; i < min; i++)
3923 + cpage_out[i] = tmp_p[i];
3924 + *sourcelen = orig_src;
3927 + c->stat_compr_orig += *sourcelen;
3928 + c->stat_compr_new += *dstlen;
3934 + jffs2_bbc_print1("iPack: manual mode without selected compressor!\n");
3937 + /*if (!jffs2_bbc_original_compressor->enabled) {
3938 + jffs2_bbc_print2("jffs2.bbc: WARNING: %s must be enabled! Enabling for this procedure...\n",jffs2_bbc_original_compressor->name);
3940 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3941 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3942 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3948 + jffs2_bbc_print1("jffs2.bbc: compress: unimlemented compress mode!!!\n");
3952 +void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen)
3954 + struct jffs2_bbc_model_list_node *m;
3955 + struct jffs2_bbc_compressor_type *c;
3960 + /* If the input too small... */
3962 + cpage_out[0]=data_in[0];
3963 + if (destlen==2) cpage_out[1]=data_in[1];
3967 + sb = jffs2_bbc_model_get_act_sb();
3968 + d[0] = *(data_in);
3969 + d[1] = *(data_in + 1);
3971 + d[0] &= 0x7f; // Variants support...
3973 + /* Search for model based decompressors... */
3974 + m = jffs2_bbc_get_model_list();
3975 + while (m != NULL) {
3976 + if ((d[0] == m->block_sign[0]) && (d[1] == m->block_sign[1]) && (sb == m->sb)) {
3977 + if (m->compressor == NULL) {
3978 + jffs2_bbc_print3("jffs2.bbc: decompressor for block_sign (%d,%d) not loaded!\n", (int) (d[0]), (int) (d[1]));
3981 + m->compressor->decompress(m->model, data_in, cpage_out, srclen, destlen);
3982 + m->compressor->stat_decompr++;
3983 + m->stat_decompr++;
3987 + m = m->next_model;
3990 + if ((((int) d[0]) == (int)(jffs2_bbc_original_compressor->block_sign[0])) && (((int) d[1]) == (int)(jffs2_bbc_original_compressor->block_sign[1]))) {
3991 + jffs2_bbc_original_compressor->decompress(NULL, data_in, cpage_out, srclen, destlen);
3992 + jffs2_bbc_original_compressor->stat_decompr++;
3995 + /* Search for non model based decompressors... */
3996 + c = jffs2_bbc_get_compressor_list();
3997 + while (c != NULL) {
3998 + if (c->model_file_sign == 0) {
3999 + if (((int) (d[0]) == (int) (c->block_sign[0])) && ((int) (d[1]) == (int) (c->block_sign[1]))) {
4000 + c->decompress(NULL, data_in, cpage_out, srclen, destlen);
4001 + c->stat_decompr++;
4007 + /* Is it DUMMY? */
4008 + if ((((int) d[0]) == JFFS2_BBC_DUMMY_BLOCKSIGN_0) && (((int) d[1]) == JFFS2_BBC_DUMMY_BLOCKSIGN_1)) {
4009 + for (i=0;i<destlen;i++) {
4010 + cpage_out[i]=data_in[i+2];
4014 + /* No matching decompressor found... */
4015 + 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);
4016 + jffs2_bbc_original_compressor->decompress(NULL, data_in, cpage_out, srclen, destlen);
4017 + jffs2_bbc_original_compressor->stat_decompr++;
4021 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_framework.h
4022 ===================================================================
4024 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_framework.h
4027 + * JFFS2-BBC: Compression Framework - headers
4029 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4031 + * Copyright (C) 2004, Ferenc Havasi
4033 + * This program is free software; you can redistribute it and/or
4034 + * modify it under the terms of the GNU General Public License
4035 + * as published by the Free Software Foundation; either version 2
4036 + * of the License, or (at your option) any later version.
4038 + * This program is distributed in the hope that it will be useful,
4039 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4040 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4041 + * GNU General Public License for more details.
4043 + * You should have received a copy of the GNU General Public License
4044 + * along with this program; if not, write to the Free Software
4045 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4049 +#ifndef __JFFS2_BBC_FRAMEWORK_H__
4051 +#define __JFFS2_BBC_FRAMEWORK_H__
4053 +#define JFFS2_BBC_VERSION "0.54.3"
4055 +#define JFFS2_BBC_CONFIG_FILE "bbc.conf"
4057 +/*********************************************************************
4058 + * Compression mode handling *
4059 + *********************************************************************/
4061 +#define JFFS2_BBC_ZLIB_MODE 1
4062 +#define JFFS2_BBC_SIZE_MODE 2
4063 +#define JFFS2_BBC_FASTR_MODE 3
4064 +#define JFFS2_BBC_FASTW_MODE 4
4065 +#define JFFS2_BBC_FASTS_MODE 5
4066 +#define JFFS2_BBC_MANUAL_MODE 6
4067 +#define JFFS2_BBC_DUMMY_MODE 7
4069 +int jffs2_bbc_get_compression_mode(void);
4070 +void jffs2_bbc_set_compression_mode(int mode);
4072 +/*********************************************************************
4073 + * Read/write speed unit *
4074 + * everything is relative to the speed of zlib *
4075 + * bigger number means slower speed! *
4076 + *********************************************************************/
4078 +#define JFFS2_BBC_ZLIB_READ_TIME 10000
4079 +#define JFFS2_BBC_ZLIB_WRITE_TIME 10000
4081 +/*********************************************************************
4082 + * Compressor handling *
4083 + *********************************************************************/
4085 +struct jffs2_bbc_compressor_type
4088 + int model_file_sign; /* 0 for no model file needed */
4089 + char block_sign[4]; /* only nomodel compressors, and only the first 2 _bytes are used! */
4090 + int (*init)(void);
4091 + int (*init_model)(void **model);
4092 + void (*destroy_model)(void **model);
4093 + void (*deinit)(void);
4095 + * *dstlen bytes are allocated.
4096 + * if it is not enough write *sourcelen over to the processed amount of data
4097 + * returns non zero if fails
4099 + int (*compress)(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen);
4100 + int (*estimate)(void *model, unsigned char *input, unsigned long sourcelen,
4101 + unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime);
4102 + /* Decompress block
4103 + * returns non zero if fails
4105 + int (*decompress)(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen);
4106 + char *(*proc_info)(void);
4107 + int (*proc_command)(char *command);
4108 + int enabled; /* filled by BBC */
4109 + int mounted; /* filled by BBC */
4110 + void *models; /* filled by BBC */
4111 + char *buffer; /* filled by BBC */
4112 + int buffer_size; /* filled by BBC */
4113 + int buffer_cnt; /* filled by BBC */
4114 + int buffer_tmp; /* filled by BBC */
4115 + int stat_compr_orig; /* filled by BBC */
4116 + int stat_compr_new; /* filled by BBC */
4117 + int stat_decompr; /* filled by BBC */
4118 + struct jffs2_bbc_compressor_type *next; /* filled by BBC */
4121 +/* It sets the compression mode to JFFS2_BBC_MANUAL_MODE */
4123 +void jffs2_bbc_set_manual_compressor(struct jffs2_bbc_compressor_type *c); /* NULL = ZLIB */
4124 +int jffs2_bbc_set_manual_compressor_by_name(char *name);
4125 +int jffs2_bbc_disable_compressor_by_name(char *name);
4126 +int jffs2_bbc_enable_compressor_by_name(char *name);
4127 +void jffs2_bbc_compressor_command_by_name(char *name_and_command);
4129 +/* If the compression mode is JFFS2_BCC_MANUAL_MODE the manually setted
4130 + compressor can be get using it. Otherwise it returns with NULL. */
4132 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_manual_compressor(void);
4134 +struct jffs2_bbc_model_list_node
4136 + void *sb; /* FS idendifier (JFFS2_SB_INFO(sb) at this moment) */
4137 + void *model; /* model data */
4138 + int sign; /* sign of the model (first 4 bytes) */
4139 + char block_sign[4]; /* block sign - only the first 2 bytes are used! */
4140 + int inode; /* inode number of the model file */
4142 + struct jffs2_bbc_compressor_type *compressor;
4143 + struct jffs2_bbc_model_list_node *next_model;
4144 + struct jffs2_bbc_model_list_node *next_compr_model;
4147 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_list(void);
4148 +struct jffs2_bbc_model_list_node *jffs2_bbc_get_model_list(void);
4150 +int jffs2_bbc_register_compressor(struct jffs2_bbc_compressor_type *c);
4151 +int jffs2_bbc_unregister_compressor(struct jffs2_bbc_compressor_type *c);
4153 +int jffs2_bbc_model_new(void *sb, int i_num, void *model);
4154 +void jffs2_bbc_model_del(void *sb);
4155 +void jffs2_bbc_model_set_act_sb(void *sb);
4156 +void *jffs2_bbc_model_get_act_sb(void);
4157 +void *jffs2_bbc_model_get_newest(struct jffs2_bbc_compressor_type *compressor);
4159 +/*********************************************************************
4160 + * Compressor init function *
4161 + *********************************************************************/
4163 +void jffs2_bbc_compressor_init(void);
4164 +void jffs2_bbc_compressor_deinit(void);
4166 +/*********************************************************************
4168 + *********************************************************************/
4170 +char *jffs2_bbc_get_compr_stats(void);
4171 +char *jffs2_bbc_get_model_stats(void);
4173 +/*********************************************************************
4175 + *********************************************************************/
4178 +void jffs2_bbc_print_flush(void);
4181 +#include <linux/kernel.h>
4182 +#define jffs2_bbc_print1(a) printk(a)
4183 +#define jffs2_bbc_print2(a,b) printk(a,b)
4184 +#define jffs2_bbc_print3(a,b,c) printk(a,b,c)
4185 +#define jffs2_bbc_print4(a,b,c,d) printk(a,b,c,d)
4186 +#define jffs2_bbc_print5(a,b,c,d,e) printk(a,b,c,d,e)
4187 +#define jffs2_bbc_print6(a,b,c,d,e,f) printk(a,b,c,d,e,f)
4188 +#define jffs2_bbc_print7(a,b,c,d,e,f,g) printk(a,b,c,d,e,f,g)
4189 +#define jffs2_bbc_print8(a,b,c,d,e,f,g,h) printk(a,b,c,d,e,f,g,h)
4190 +#define jffs2_bbc_print9(a,b,c,d,e,f,g,h,i) printk(a,b,c,d,e,f,g,h,i)
4193 +#define jffs2_bbc_print1(a) fprintf(stderr,a)
4194 +#define jffs2_bbc_print2(a,b) fprintf(stderr,a,b)
4195 +#define jffs2_bbc_print3(a,b,c) fprintf(stderr,a,b,c)
4196 +#define jffs2_bbc_print4(a,b,c,d) fprintf(stderr,a,b,c,d)
4197 +#define jffs2_bbc_print5(a,b,c,d,e) fprintf(stderr,a,b,c,d,e)
4198 +#define jffs2_bbc_print6(a,b,c,d,e,f) fprintf(stderr,a,b,c,d,e,f)
4199 +#define jffs2_bbc_print7(a,b,c,d,e,f,g) fprintf(stderr,a,b,c,d,e,f,g)
4200 +#define jffs2_bbc_print8(a,b,c,d,e,f,g,h) fprintf(stderr,a,b,c,d,e,f,g,h)
4201 +#define jffs2_bbc_print9(a,b,c,d,e,f,g,h,i) fprintf(stderr,a,b,c,d,e,f,g,h,i)
4204 +/* Handle endianness */
4207 +#define ENDIAN_HOST_AND_TARGET_SAME 0
4208 +#define ENDIAN_HOST_AND_TARGET_DIFFERENT 1
4210 +extern int jffs2_bbc_glb_endian_X;
4214 +/* Allocating more than one page (tip. 4096 byte) */
4215 +void *jffs2_bbc_malloc(long size);
4216 +void jffs2_bbc_free(void *addr);
4218 +/* Allocating less than one page (tip. 4096 byte) */
4219 +void *jffs2_bbc_malloc_small(long size);
4220 +void jffs2_bbc_free_small(void *addr);
4222 +/* Memory guarding */
4223 +int jffs2_bbc_test_memory_counter(int verbose);
4224 +char *jffs2_bbc_get_mem_stats(void);
4225 +int jffs2_bbc_get_memory_counter(void);
4228 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_fs.c
4229 ===================================================================
4231 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_fs.c
4234 + * JFFS2-BBC: File System Extension for Linux Kernel
4236 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4238 + * Copyright (C) 2004, Ferenc Havasi
4240 + * This program is free software; you can redistribute it and/or
4241 + * modify it under the terms of the GNU General Public License
4242 + * as published by the Free Software Foundation; either version 2
4243 + * of the License, or (at your option) any later version.
4245 + * This program is distributed in the hope that it will be useful,
4246 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4247 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4248 + * GNU General Public License for more details.
4250 + * You should have received a copy of the GNU General Public License
4251 + * along with this program; if not, write to the Free Software
4252 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4256 +#include <linux/kernel.h>
4257 +#include <linux/fs.h>
4258 +#include <linux/jffs2.h>
4259 +#include <linux/proc_fs.h>
4260 +#include <linux/version.h>
4262 +#include "nodelist.h"
4264 +#include "jffs2_bbc_framework.h"
4266 +struct jffs2_bbc_fs_sb_list {
4267 + struct super_block *sb;
4268 + struct jffs2_bbc_fs_sb_list *next;
4271 +static struct jffs2_bbc_fs_sb_list *sb_list = NULL;
4273 +void jffs2_bbc_proc_init(void);
4274 +void jffs2_bbc_proc_deinit(void);
4276 +void jffs2_bbc_load_model(void *sb_par) {
4277 + struct jffs2_sb_info *c;
4278 + //struct jffs2_inode_info *f;
4279 + struct dentry *config_dentry,*model_dentry;
4280 + struct qstr config_name,model_name;
4281 + struct file *config_file,*model_file;
4282 + char *buff=NULL,*model_buff;
4283 + int config_size,model_size;
4285 + struct super_block *sb;
4286 + struct jffs2_bbc_fs_sb_list *sb_l;
4289 + sb_l = jffs2_bbc_malloc_small(sizeof(struct jffs2_bbc_fs_sb_list));
4291 + sb_l->next = sb_list;
4293 + config_name.name = JFFS2_BBC_CONFIG_FILE;
4294 + config_name.len = strlen(config_name.name);
4295 + config_name.hash = full_name_hash(config_name.name,config_name.len);
4296 + config_dentry = d_alloc(sb->s_root,&config_name);
4297 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4298 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,config_dentry);
4300 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,config_dentry,NULL);
4303 + if (config_dentry->d_inode != NULL) {
4304 + config_size = config_dentry->d_inode->i_size;
4305 + //printk("config_file_size=%d\n",config_size);
4306 + if (config_size > 0) {
4307 + buff = jffs2_bbc_malloc(config_size+1);
4308 + config_file = dentry_open(config_dentry,NULL,O_RDONLY);
4309 + kernel_read(config_file,0,buff,config_size);
4310 + buff[config_size] = 0;
4311 + for (prev_i = i = 0 ; i < config_size+1 ; i++) {
4312 + if (buff[i] == '\n') buff[i]=0;
4313 + if (buff[i] == 0) {
4314 + if (prev_i != i) {
4315 + if ((buff[prev_i] == '-') && (buff[prev_i+1] == 0)) break;
4316 + printk("reading model file %s... ",buff+prev_i);
4317 + model_name.name = buff+prev_i;
4318 + model_name.len = strlen(buff+prev_i);
4319 + model_name.hash = full_name_hash(model_name.name,model_name.len);
4320 + model_dentry = d_alloc(sb->s_root,&model_name);
4321 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4322 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,model_dentry);
4324 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,model_dentry,NULL);
4326 + if (model_dentry->d_inode != NULL) {
4327 + c = JFFS2_SB_INFO(model_dentry->d_inode->i_sb);
4328 + //f = JFFS2_INODE_INFO(model_dentry->d_inode);
4329 + model_size = model_dentry->d_inode->i_size;
4330 + model_buff = jffs2_bbc_malloc(model_size);
4331 + model_file = dentry_open(model_dentry,NULL,O_RDONLY);
4332 + kernel_read(model_file,0,model_buff,model_size);
4333 + if (jffs2_bbc_model_new(c,model_dentry->d_inode->i_ino,model_buff) != 0) {
4334 + printk("already loaded.\n");
4335 + jffs2_bbc_free(model_buff);
4338 + printk("done (%d bytes readed from inode %d).\n",model_size,(int)(model_dentry->d_inode->i_ino));
4342 + printk("not found.\n");
4344 + dput(model_dentry);
4351 + dput(config_dentry);
4352 + if (buff != NULL) jffs2_bbc_free(buff);
4355 +void jffs2_bbc_unload_model(void *sb_par)
4357 + struct jffs2_sb_info *c;
4358 + struct super_block *sb = sb_par;
4359 + struct jffs2_bbc_fs_sb_list *sb_l,*sb_l2;
4362 + c = JFFS2_SB_INFO(sb);
4363 + jffs2_bbc_model_del(c);
4364 + if (sb_list == NULL) printk("jffs2.bbc: error! NULL sb list!\n");
4366 + if (sb_list->sb == sb) {
4367 + jffs2_bbc_free_small(sb_list);
4373 + while (sb_l->next != NULL) {
4374 + if (sb_l->next->sb == sb) {
4375 + sb_l2 = sb_l->next->next;
4376 + jffs2_bbc_free_small(sb_l->next);
4377 + sb_l->next = sb_l2;
4380 + sb_l = sb_l->next;
4385 + printk("jffs2.bbc: cannot delete sb from sblist!\n");
4390 +static int jffs2_bbc_get_mounted(void) {
4391 + struct jffs2_bbc_fs_sb_list *sb_l;
4395 + while (sb_l != NULL) {
4397 + sb_l = sb_l->next;
4403 +int jffs2_bbc_proc_read(char *buf, char **start, off_t offset, int count, int *eof, void *data)
4405 + int len = 0, mode;
4407 + mode = jffs2_bbc_get_compression_mode();
4408 + len += sprintf(buf + len, "BBC version: %s\n", JFFS2_BBC_VERSION);
4409 + len += sprintf(buf+len,"Mounted jffs2 filesystems: %d\n",jffs2_bbc_get_mounted());
4410 + //len += sprintf(buf+len,"actual model file inode: %d\n",jffs2_bbc_model_get_inum());
4411 + len += sprintf(buf + len, "Compression mode: ");
4412 + if (mode == JFFS2_BBC_ZLIB_MODE)
4413 + len += sprintf(buf + len, "ZLIB mode");
4414 + else if (mode == JFFS2_BBC_SIZE_MODE)
4415 + len += sprintf(buf + len, "SIZE mode");
4416 + else if (mode == JFFS2_BBC_FASTR_MODE)
4417 + len += sprintf(buf + len, "FASTR mode");
4418 + else if (mode == JFFS2_BBC_FASTW_MODE)
4419 + len += sprintf(buf + len, "FASTW mode");
4420 + else if (mode == JFFS2_BBC_FASTS_MODE)
4421 + len += sprintf(buf + len, "FASTS mode");
4422 + else if (mode == JFFS2_BBC_DUMMY_MODE)
4423 + len += sprintf(buf + len, "DUMMY mode");
4424 + else if (mode == JFFS2_BBC_MANUAL_MODE) {
4425 + len += sprintf(buf + len, "MANUAL mode (");
4426 + if (jffs2_bbc_get_manual_compressor() != NULL)
4427 + len += sprintf(buf + len, "%s)", jffs2_bbc_get_manual_compressor()->name);
4429 + len += sprintf(buf + len, "ZLIB)");
4432 + len += sprintf(buf + len, "unknown mode");
4433 + len += sprintf(buf + len, "\n");
4434 + len += sprintf(buf + len, "%s", jffs2_bbc_get_compr_stats());
4435 + len += sprintf(buf + len, "%s", jffs2_bbc_get_model_stats());
4436 + len += sprintf(buf + len, "%s", jffs2_bbc_get_mem_stats());
4441 +int jffs2_bbc_proc_write(struct file *file, const char *buffer_orig, unsigned long count, void *data)
4445 + struct jffs2_bbc_fs_sb_list *sb_l;
4447 + if (buffer_orig == NULL) return 0;
4449 + buffer = jffs2_bbc_malloc(count+2);
4450 + for (i=0;i<count;i++) buffer[i]=buffer_orig[i];
4451 + buffer[count] = 0;
4452 + if ((*buffer == 'z') || (*buffer == 'Z')) {
4453 + jffs2_bbc_set_compression_mode(JFFS2_BBC_ZLIB_MODE);
4454 + jffs2_bbc_print1("jffs2.bbc: ZLIB compression mode activated.\n");
4455 + jffs2_bbc_free(buffer);
4458 + else if ((*buffer == 's') || (*buffer == 'S')) {
4459 + jffs2_bbc_set_compression_mode(JFFS2_BBC_SIZE_MODE);
4460 + jffs2_bbc_print1("jffs2.bbc: SIZE compression mode activated.\n");
4461 + jffs2_bbc_free(buffer);
4464 + else if ((*buffer == 'd') || (*buffer == 'D')) {
4465 + jffs2_bbc_set_compression_mode(JFFS2_BBC_DUMMY_MODE);
4466 + jffs2_bbc_print1("jffs2.bbc: DUMMY compression mode activated.\n");
4467 + jffs2_bbc_free(buffer);
4470 + else if (((*buffer == 'm') || (*buffer == 'M')) && (count >= 3) && (buffer[1] == ':')) {
4471 + jffs2_bbc_print1("jffs2.bbc: activating MANUAL mode.\n");
4472 + jffs2_bbc_set_manual_compressor_by_name(buffer + 2);
4473 + jffs2_bbc_free(buffer);
4476 + else if (((*buffer == '0')) && (count >= 3) && (buffer[1] == ':')) {
4477 + jffs2_bbc_print1("jffs2.bbc: disabling a compressor... ");
4478 + if (jffs2_bbc_disable_compressor_by_name(buffer + 2) == 0) {
4479 + jffs2_bbc_print1("done.\n");
4482 + jffs2_bbc_print1("not found.\n");
4484 + jffs2_bbc_free(buffer);
4487 + else if (((*buffer == '1')) && (count >= 3) && (buffer[1] == ':')) {
4488 + jffs2_bbc_print1("jffs2.bbc: enabling a compressor... ");
4489 + if (jffs2_bbc_enable_compressor_by_name(buffer + 2) == 0) {
4490 + jffs2_bbc_print1("done.\n");
4493 + jffs2_bbc_print1("not found.\n");
4495 + jffs2_bbc_free(buffer);
4498 + else if (((*buffer == 'c') || (*buffer == 'C')) && (count >= 3) && (buffer[1] == ':')) {
4499 + jffs2_bbc_compressor_command_by_name(buffer + 2);
4500 + jffs2_bbc_free(buffer);
4503 + else if ((*buffer == 'r') || (*buffer == 'R')) {
4504 + jffs2_bbc_print1("jffs2.bbc: reloading model files:\n");
4506 + while (sb_l != NULL) {
4507 + jffs2_bbc_unload_model(sb_l->sb);
4508 + jffs2_bbc_load_model(sb_l->sb);
4509 + sb_l = sb_l->next;
4511 + jffs2_bbc_free(buffer);
4514 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 'r') || (buffer[1] == 'R'))) {
4515 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTR_MODE);
4516 + jffs2_bbc_print1("jffs2.bbc: FASTR compression mode activated.\n");
4517 + jffs2_bbc_free(buffer);
4520 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 'w') || (buffer[1] == 'W'))) {
4521 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTW_MODE);
4522 + jffs2_bbc_print1("jffs2.bbc: FASTW compression mode activated.\n");
4523 + jffs2_bbc_free(buffer);
4526 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 's') || (buffer[1] == 'S'))) {
4527 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTS_MODE);
4528 + jffs2_bbc_print1("jffs2.bbc: FASTS compression mode activated.\n");
4529 + jffs2_bbc_free(buffer);
4533 + jffs2_bbc_print1("jffs2.bbc: unkown command. Valid commands are:\n"
4534 + " z = switch to ZLIB compression mode\n"
4535 + " s = switch to SIZE compression mode\n"
4536 + " d = switch to DUMMY compression mode\n"
4537 + " fr = switch to FASTR compression mode\n"
4538 + " fw = switch to FASTW compression mode\n"
4539 + " fs = switch to FASTS compression mode\n"
4540 + " r = reread model files from actual file system\n"
4541 + " m:compressor_name = switch to MANUAL compression mode\n"
4542 + " 0:compressor_name = disable a compressor\n"
4543 + " 1:compressor_name = enable a compressor\n"
4544 + " c:compressor_name:command = enable a compressor\n");
4545 + jffs2_bbc_free(buffer);
4549 +void jffs2_bbc_proc_init()
4551 + struct proc_dir_entry *res = create_proc_entry("jffs2_bbc", 0, NULL);
4552 + jffs2_bbc_compressor_init();
4554 + res->read_proc = jffs2_bbc_proc_read;
4555 + res->write_proc = jffs2_bbc_proc_write;
4559 +void jffs2_bbc_proc_deinit()
4561 + jffs2_bbc_compressor_deinit();
4562 + remove_proc_entry("jffs2_bbc", NULL);
4564 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_fs.h
4565 ===================================================================
4567 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_fs.h
4570 + * JFFS2 BBC: File System Extension for Linux Kernel - headers
4572 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4574 + * Copyright (C) 2004, Ferenc Havasi
4576 + * This program is free software; you can redistribute it and/or
4577 + * modify it under the terms of the GNU General Public License
4578 + * as published by the Free Software Foundation; either version 2
4579 + * of the License, or (at your option) any later version.
4581 + * This program is distributed in the hope that it will be useful,
4582 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4583 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4584 + * GNU General Public License for more details.
4586 + * You should have received a copy of the GNU General Public License
4587 + * along with this program; if not, write to the Free Software
4588 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4592 +extern int jffs2_bbc_inode_not_found;
4594 +void jffs2_bbc_load_model(void *sb);
4595 +void jffs2_bbc_unload_model(void *sb);
4597 +void jffs2_bbc_proc_init(void);
4598 +void jffs2_bbc_proc_deinit(void);
4599 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzari_comp.c
4600 ===================================================================
4602 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzari_comp.c
4604 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4607 + jffs2_bbc_lzari_comp.c -- Lempel-Ziv-Arithmetic coding compression module for jffs2
4608 + Copyright (C) 2004 Patrik Kluba
4609 + Based on the LZARI source included in LDS (lossless datacompression sources)
4610 + Block-compression and bitstream modifications by Patrik Kluba
4611 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4615 +Original copyright follows:
4617 +**************************************************************
4618 + LZARI.C -- A Data Compression Program
4620 +**************************************************************
4621 + 4/7/1989 Haruhiko Okumura
4622 + Use, distribute, and modify this program freely.
4623 + Please send me your improved versions.
4625 + NIFTY-Serve PAF01022
4626 + CompuServe 74050,1022
4627 +**************************************************************
4629 +LZARI.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
4630 +All rights reserved. Permission granted for non-commercial use.
4636 + 2004-02-18 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
4637 + Removed unused variables and fixed no return value
4639 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
4646 +#define N 4096 /* size of ring buffer */
4647 +#define F 60 /* upper limit for match_length */
4648 +#define THRESHOLD 2 /* encode string into position and length
4649 + if match_length is greater than this */
4650 +#define NIL N /* index for root of binary search trees */
4652 +static unsigned char
4653 + text_buf[N + F - 1]; /* ring buffer of size N,
4654 + with extra F-1 bytes to facilitate string comparison */
4655 +static unsigned long match_position, match_length, /* of longest match. These are
4656 + set by the InsertNode() procedure. */
4657 + lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
4658 + parents -- These constitute binary search trees. */
4660 +static void InitTree(void) /* Initialize trees */
4664 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
4665 + left children of node i. These nodes need not be initialized.
4666 + Also, dad[i] is the parent of node i. These are initialized to
4667 + NIL (= N), which stands for 'not used.'
4668 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
4669 + for strings that begin with character i. These are initialized
4670 + to NIL. Note there are 256 trees. */
4672 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL; /* root */
4673 + for (i = 0; i < N; i++) dad[i] = NIL; /* node */
4676 +static void InsertNode(unsigned long r)
4677 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
4678 + trees (text_buf[r]'th tree) and returns the longest-match position
4679 + and length via the global variables match_position and match_length.
4680 + If match_length = F, then removes the old node in favor of the new
4681 + one, because the old one will be deleted sooner.
4682 + Note r plays double role, as tree node and position in buffer. */
4684 + unsigned long i, p, temp;
4685 + unsigned char *key;
4688 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
4689 + rson[r] = lson[r] = NIL; match_length = 0;
4692 + if (rson[p] != NIL) p = rson[p];
4693 + else { rson[p] = r; dad[r] = p; return; }
4695 + if (lson[p] != NIL) p = lson[p];
4696 + else { lson[p] = r; dad[r] = p; return; }
4698 + for (i = 1; i < F; i++)
4699 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
4700 + if (i > THRESHOLD) {
4701 + if (i > match_length) {
4702 + match_position = (r - p) & (N - 1);
4703 + if ((match_length = i) >= F) break;
4704 + } else if (i == match_length) {
4705 + if ((temp = (r - p) & (N - 1)) < match_position)
4706 + match_position = temp;
4710 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
4711 + dad[lson[p]] = r; dad[rson[p]] = r;
4712 + if (rson[dad[p]] == p) rson[dad[p]] = r;
4713 + else lson[dad[p]] = r;
4714 + dad[p] = NIL; /* remove p */
4717 +static void DeleteNode(unsigned long p) /* Delete node p from tree */
4721 + if (dad[p] == NIL) return; /* not in tree */
4722 + if (rson[p] == NIL) q = lson[p];
4723 + else if (lson[p] == NIL) q = rson[p];
4726 + if (rson[q] != NIL) {
4727 + do { q = rson[q]; } while (rson[q] != NIL);
4728 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
4729 + lson[q] = lson[p]; dad[lson[p]] = q;
4731 + rson[q] = rson[p]; dad[rson[p]] = q;
4734 + if (rson[dad[p]] == p) rson[dad[p]] = q;
4735 + else lson[dad[p]] = q;
4739 +/********** Arithmetic Compression **********/
4741 +/* If you are not familiar with arithmetic compression, you should read
4742 + I. E. Witten, R. M. Neal, and J. G. Cleary,
4743 + Communications of the ACM, Vol. 30, pp. 520-540 (1987),
4744 + from which much have been borrowed. */
4748 +/* Q1 (= 2 to the M) must be sufficiently large, but not so
4749 + large as the unsigned long 4 * Q1 * (Q1 - 1) overflows. */
4751 +#define Q1 (1UL << M)
4752 +#define Q2 (2 * Q1)
4753 +#define Q3 (3 * Q1)
4754 +#define Q4 (4 * Q1)
4755 +#define MAX_CUM (Q1 - 1)
4757 +#define N_CHAR (256 - THRESHOLD + F)
4758 + /* character code = 0, 1, ..., N_CHAR - 1 */
4760 +static unsigned long char_to_sym[N_CHAR], sym_to_char[N_CHAR + 1];
4761 +static unsigned long
4762 + sym_freq[N_CHAR + 1], /* frequency for symbols */
4763 + sym_cum[N_CHAR + 1], /* cumulative freq for symbols */
4764 + position_cum[N + 1]; /* cumulative freq for positions */
4766 +static void StartModel(void) /* Initialize model */
4768 + unsigned long ch, sym, i;
4770 + sym_cum[N_CHAR] = 0;
4771 + for (sym = N_CHAR; sym >= 1; sym--) {
4773 + char_to_sym[ch] = sym; sym_to_char[sym] = ch;
4774 + sym_freq[sym] = 1;
4775 + sym_cum[sym - 1] = sym_cum[sym] + sym_freq[sym];
4777 + sym_freq[0] = 0; /* sentinel (!= sym_freq[1]) */
4778 + position_cum[N] = 0;
4779 + for (i = N; i >= 1; i--)
4780 + position_cum[i - 1] = position_cum[i] + 10000 / (i + 200);
4781 + /* empirical distribution function (quite tentative) */
4782 + /* Please devise a better mechanism! */
4785 +static void UpdateModel(unsigned long sym)
4787 + unsigned long c, ch_i, ch_sym;
4789 + if (sym_cum[0] >= MAX_CUM) {
4791 + for (i = N_CHAR; i > 0; i--) {
4793 + c += (sym_freq[i] = (sym_freq[i] + 1) >> 1);
4797 + for (i = sym; sym_freq[i] == sym_freq[i - 1]; i--) ;
4799 + ch_i = sym_to_char[i]; ch_sym = sym_to_char[sym];
4800 + sym_to_char[i] = ch_sym; sym_to_char[sym] = ch_i;
4801 + char_to_sym[ch_i] = sym; char_to_sym[ch_sym] = i;
4804 + while (--i > 0) sym_cum[i]++;
4808 +static unsigned long BinarySearchSym(unsigned long x)
4809 + /* 1 if x >= sym_cum[1],
4810 + N_CHAR if sym_cum[N_CHAR] > x,
4811 + i such that sym_cum[i - 1] > x >= sym_cum[i] otherwise */
4813 + unsigned long i, j, k;
4815 + i = 1; j = N_CHAR;
4818 + if (sym_cum[k] > x) i = k + 1; else j = k;
4823 +unsigned long BinarySearchPos(unsigned long x)
4824 + /* 0 if x >= position_cum[1],
4825 + N - 1 if position_cum[N] > x,
4826 + i such that position_cum[i] > x >= position_cum[i + 1] otherwise */
4828 + unsigned long i, j, k;
4833 + if (position_cum[k] > x) i = k + 1; else j = k;
4838 +/* modified for block compression */
4839 +/* on return, srclen will contain the number of successfully compressed bytes
4840 + and dstlen will contain completed compressed bytes */
4842 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
4843 + unsigned long *dstlen)
4845 + unsigned long c, i, len, r, s, last_match_length, sym, range;
4846 + unsigned long low = 0;
4847 + unsigned long high = Q4;
4848 + unsigned long shifts = 0; /* counts for magnifying low and high around Q2 */
4849 + unsigned char *ip, *op;
4850 + unsigned long written = 0;
4851 + unsigned long read = 0;
4852 + unsigned char buffer = 0;
4853 + unsigned char mask = 128;
4854 + unsigned char *srcend = srcbuf + *srclen;
4855 + unsigned char *dstend = dstbuf + *dstlen;
4859 + InitTree(); /* initialize trees */
4861 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
4862 + any character that will appear often. */
4863 + for (len = 0; (len < F) && (ip < srcend); len++)
4864 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
4867 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
4868 + each of which begins with one or more 'space' characters. Note
4869 + the order in which these strings are inserted. This way,
4870 + degenerate trees will be less likely to occur. */
4871 + InsertNode(r); /* Finally, insert the whole string just read. The
4872 + global variables match_length and match_position are set. */
4874 + if (match_length > len) match_length = len; /* match_length
4875 + may be spuriously long near the end of text. */
4876 + if (match_length <= THRESHOLD) {
4877 + match_length = 1; /* Not long enough match. Send one byte. */
4878 + sym = char_to_sym[text_buf[r]];
4879 + range = high - low;
4880 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
4881 + low += (range * sym_cum[sym ]) / sym_cum[0];
4884 + if ((mask >>= 1) == 0) {
4885 + if (op >= dstend) {
4886 + *dstlen = written;
4895 + for ( ; shifts > 0; shifts--) {
4897 + if ((mask >>= 1) == 0) {
4898 + if (op >= dstend) {
4899 + *dstlen = written;
4909 + } else if (low >= Q2) {
4911 + if ((mask >>= 1) == 0) {
4912 + if (op >= dstend) {
4913 + *dstlen = written;
4922 + for ( ; shifts > 0; shifts--) {
4923 + if ((mask >>= 1) == 0) {
4924 + if (op >= dstend) {
4925 + *dstlen = written;
4937 + } else if (low >= Q1 && high <= Q3) {
4942 + low += low; high += high;
4946 + sym = char_to_sym[255 - THRESHOLD + match_length];
4947 + range = high - low;
4948 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
4949 + low += (range * sym_cum[sym ]) / sym_cum[0];
4952 + if ((mask >>= 1) == 0) {
4953 + if (op >= dstend) {
4954 + *dstlen = written;
4963 + for ( ; shifts > 0; shifts--) {
4965 + if ((mask >>= 1) == 0) {
4966 + if (op >= dstend) {
4967 + *dstlen = written;
4977 + } else if (low >= Q2) {
4979 + if ((mask >>= 1) == 0) {
4980 + if (op >= dstend) {
4981 + *dstlen = written;
4990 + for ( ; shifts > 0; shifts--) {
4991 + if ((mask >>= 1) == 0) {
4992 + if (op >= dstend) {
4993 + *dstlen = written;
5005 + } else if (low >= Q1 && high <= Q3) {
5010 + low += low; high += high;
5013 + range = high - low;
5014 + high = low + (range * position_cum[match_position - 1]) / position_cum[0];
5015 + low += (range * position_cum[match_position ]) / position_cum[0];
5018 + if ((mask >>= 1) == 0) {
5019 + if (op >= dstend) {
5020 + *dstlen = written;
5029 + for ( ; shifts > 0; shifts--) {
5031 + if ((mask >>= 1) == 0) {
5032 + if (op >= dstend) {
5033 + *dstlen = written;
5046 + if ((mask >>= 1) == 0) {
5047 + if (op >= dstend) {
5048 + *dstlen = written;
5057 + for ( ; shifts > 0; shifts--) {
5058 + if ((mask >>= 1) == 0) {
5059 + if (op >= dstend) {
5060 + *dstlen = written;
5073 + if ((low >= Q1) && (high <= Q3)) {
5086 + last_match_length = match_length;
5087 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
5092 + text_buf[s + N] = c;
5093 + s = (s + 1) & (N - 1);
5094 + r = (r + 1) & (N - 1);
5098 + while (i++ < last_match_length) {
5100 + s = (s + 1) & (N - 1);
5101 + r = (r + 1) & (N - 1);
5102 + if (--len) InsertNode(r);
5104 + } while (len > 0);
5107 + if ((mask >>= 1) == 0) {
5108 + if (op >= dstend) {
5109 + *dstlen = written;
5118 + for ( ; shifts > 0; shifts--) {
5120 + if ((mask >>= 1) == 0) {
5121 + if (op >= dstend) {
5122 + *dstlen = written;
5134 + if ((mask >>= 1) == 0) {
5135 + if (op >= dstend) {
5136 + *dstlen = written;
5145 + for ( ; shifts > 0; shifts--) {
5146 + if ((mask >>= 1) == 0) {
5147 + if (op >= dstend) {
5148 + *dstlen = written;
5159 + for (i = 0; i < 7; i++) {
5160 + if ((mask >>= 1) == 0) {
5161 + if (op >= dstend) {
5162 + *dstlen = written;
5172 + *dstlen = written;
5176 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
5177 + unsigned long dstlen) /* Just the reverse of Encode(). */
5179 + unsigned long i, r, j, k, c, range, sym;
5180 + unsigned char *ip, *op;
5181 + unsigned char *srcend = srcbuf + srclen;
5182 + unsigned char *dstend = dstbuf + dstlen;
5183 + unsigned char buffer = 0;
5184 + unsigned char mask = 0;
5185 + unsigned long low = 0;
5186 + unsigned long high = Q4;
5187 + unsigned long value = 0;
5190 + for (i = 0; i < M + 2; i++) {
5192 + if ((mask >>= 1) == 0) {
5193 + buffer = (ip >= srcend) ? 0 : *(ip++);
5196 + value += ((buffer & mask) != 0);
5199 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
5201 + while (op < dstend) {
5202 + range = high - low;
5203 + sym = BinarySearchSym((unsigned long)
5204 + (((value - low + 1) * sym_cum[0] - 1) / range));
5205 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
5206 + low += (range * sym_cum[sym ]) / sym_cum[0];
5209 + value -= Q2; low -= Q2; high -= Q2;
5210 + } else if (low >= Q1 && high <= Q3) {
5211 + value -= Q1; low -= Q1; high -= Q1;
5212 + } else if (high > Q2) break;
5213 + low += low; high += high;
5215 + if ((mask >>= 1) == 0) {
5216 + buffer = (ip >= srcend) ? 0 : *(ip++);
5219 + value += ((buffer & mask) != 0);
5221 + c = sym_to_char[sym];
5224 + if (op >= dstend) return -1;
5226 + text_buf[r++] = c;
5229 + j = c - 255 + THRESHOLD;
5230 + range = high - low;
5231 + i = BinarySearchPos((unsigned long)
5232 + (((value - low + 1) * position_cum[0] - 1) / range));
5233 + high = low + (range * position_cum[i ]) / position_cum[0];
5234 + low += (range * position_cum[i + 1]) / position_cum[0];
5237 + value -= Q2; low -= Q2; high -= Q2;
5238 + } else if (low >= Q1 && high <= Q3) {
5239 + value -= Q1; low -= Q1; high -= Q1;
5240 + } else if (high > Q2) break;
5241 + low += low; high += high;
5243 + if ((mask >>= 1) == 0) {
5244 + buffer = (ip >= srcend) ? 0 : *(ip++);
5247 + value += ((buffer & mask) != 0);
5249 + i = (r - i - 1) & (N - 1);
5250 + for (k = 0; k < j; k++) {
5251 + c = text_buf[(i + k) & (N - 1)];
5252 + if (op >= dstend) return -1;
5254 + text_buf[r++] = c;
5262 +/* interface to jffs2 bbc follows */
5264 +#include "jffs2_bbc_framework.h"
5266 +#define JFFS2_BBC_LZARI_BLOCK_SIGN {0x73, 0x9a, 0x1c, 0x4d}
5269 +jffs2_bbc_lzari_compressor_init (void);
5272 +jffs2_bbc_lzari_compressor_deinit (void);
5275 +jffs2_bbc_lzari_compress (void *model, unsigned char *input,
5276 + unsigned char *output, unsigned long *sourcelen,
5277 + unsigned long *dstlen);
5280 +jffs2_bbc_lzari_estimate (void *model, unsigned char *input,
5281 + unsigned long sourcelen, unsigned long *dstlen,
5282 + unsigned long *readtime, unsigned long *writetime);
5285 +jffs2_bbc_lzari_decompress (void *model, unsigned char *input,
5286 + unsigned char *output, unsigned long sourcelen,
5287 + unsigned long dstlen);
5290 +jffs2_bbc_lzari_proc_info (void);
5293 +jffs2_bbc_lzari_proc_command (char *command);
5295 +struct jffs2_bbc_compressor_type jffs2_bbc_lzari = {
5298 + JFFS2_BBC_LZARI_BLOCK_SIGN,
5299 + jffs2_bbc_lzari_compressor_init,
5302 + jffs2_bbc_lzari_compressor_deinit,
5303 + jffs2_bbc_lzari_compress,
5304 + jffs2_bbc_lzari_estimate,
5305 + jffs2_bbc_lzari_decompress,
5306 + jffs2_bbc_lzari_proc_info,
5307 + jffs2_bbc_lzari_proc_command
5311 +jffs2_bbc_lzari_compressor_init (void)
5317 +jffs2_bbc_lzari_compressor_deinit (void)
5322 +jffs2_bbc_lzari_compress (void *model, unsigned char *input,
5323 + unsigned char *output, unsigned long *sourcelen,
5324 + unsigned long *dstlen)
5327 + unsigned long dst = *dstlen;
5328 + *(output++) = jffs2_bbc_lzari.block_sign[0];
5329 + *(output++) = jffs2_bbc_lzari.block_sign[1];
5331 + retval = Encode(input, output, sourcelen, &dst);
5338 +jffs2_bbc_lzari_estimate (void *model, unsigned char *input,
5339 + unsigned long sourcelen, unsigned long *dstlen,
5340 + unsigned long *readtime, unsigned long *writetime)
5342 + *dstlen = sourcelen / 2;
5343 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 15;
5344 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 7;
5349 +jffs2_bbc_lzari_decompress (void *model, unsigned char *input,
5350 + unsigned char *output, unsigned long sourcelen,
5351 + unsigned long dstlen)
5353 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzari.block_sign[0] ) ||
5354 + ( *(input++) != (unsigned char)jffs2_bbc_lzari.block_sign[1] )
5358 + return Decode(input, output, sourcelen - 2, dstlen);
5363 +jffs2_bbc_lzari_proc_info (void)
5365 + return "Lempel-Ziv-Arithmetic coding compression module";
5369 +jffs2_bbc_lzari_proc_command (char *command)
5374 +struct jffs2_bbc_compressor_type *
5375 +jffs2_bbc_lzari_init (int mode)
5377 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzari) == 0)
5379 + return &jffs2_bbc_lzari;
5388 +jffs2_bbc_lzari_deinit (void)
5390 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzari);
5392 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzhd_comp.c
5393 ===================================================================
5395 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzhd_comp.c
5397 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
5400 + jffs2_bbc_lzhd_comp.c -- Lempel-Ziv-(dynamic) Huffman compression module for jffs2
5401 + Copyright (C) 2004 Patrik Kluba
5402 + Based on the LZHUF source included in LDS (lossless datacompression sources)
5403 + Block-compression and bitstream modifications by Patrik Kluba
5404 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
5408 +Original copyright follows:
5410 +**************************************************************
5412 + written by Haruyasu Yoshizaki 11/20/1988
5413 + some minor changes 4/6/1989
5414 + comments translated by Haruhiko Okumura 4/7/1989
5415 +**************************************************************
5417 +LZHUF.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
5418 +All rights reserved. Permission granted for non-commercial use.
5424 + 2004-02-18 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
5425 + Replaced name lzh-d with lzhd
5426 + Fixed no return value
5428 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
5433 +/* required because of memmove */
5435 + #include <string.h>
5437 + #include <linux/string.h>
5442 +#define N 4096 /* size of ring buffer */
5443 +#define F 60 /* upper limit for match_length */
5444 +#define THRESHOLD 2 /* encode string into position and length
5445 + if match_length is greater than this */
5446 +#define NIL N /* index for root of binary search trees */
5448 +static unsigned char
5449 + text_buf[N + F - 1]; /* ring buffer of size N,
5450 + with extra F-1 bytes to facilitate string comparison */
5451 +static unsigned long match_position, match_length, /* of longest match. These are
5452 + set by the InsertNode() procedure. */
5453 + lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
5454 + parents -- These constitute binary search trees. */
5456 +static void InitTree(void) /* initialize trees */
5460 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
5461 + left children of node i. These nodes need not be initialized.
5462 + Also, dad[i] is the parent of node i. These are initialized to
5463 + NIL (= N), which stands for 'not used.'
5464 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
5465 + for strings that begin with character i. These are initialized
5466 + to NIL. Note there are 256 trees. */
5468 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
5469 + for (i = 0; i < N; i++) dad[i] = NIL;
5472 +static void InsertNode(unsigned long r)
5473 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
5474 + trees (text_buf[r]'th tree) and returns the longest-match position
5475 + and length via the global variables match_position and match_length.
5476 + If match_length = F, then removes the old node in favor of the new
5477 + one, because the old one will be deleted sooner.
5478 + Note r plays double role, as tree node and position in buffer. */
5480 + unsigned long i, p, c;
5482 + unsigned char *key;
5484 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
5485 + rson[r] = lson[r] = NIL; match_length = 0;
5488 + if (rson[p] != NIL) p = rson[p];
5489 + else { rson[p] = r; dad[r] = p; return; }
5491 + if (lson[p] != NIL) p = lson[p];
5492 + else { lson[p] = r; dad[r] = p; return; }
5494 + for (i = 1; i < F; i++)
5495 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
5496 + if (i > THRESHOLD) {
5497 + if (i > match_length) {
5498 + match_position = ((r - p) & (N - 1)) - 1;
5499 + if ((match_length = i) >= F) break;
5501 + if (i == match_length) {
5502 + if ((c = ((r - p) & (N - 1)) - 1) < match_position) {
5503 + match_position = c;
5508 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
5509 + dad[lson[p]] = r; dad[rson[p]] = r;
5510 + if (rson[dad[p]] == p) rson[dad[p]] = r;
5511 + else lson[dad[p]] = r;
5512 + dad[p] = NIL; /* remove p */
5515 +static void DeleteNode(unsigned long p) /* deletes node p from tree */
5519 + if (dad[p] == NIL) return; /* not in tree */
5520 + if (rson[p] == NIL) q = lson[p];
5521 + else if (lson[p] == NIL) q = rson[p];
5524 + if (rson[q] != NIL) {
5525 + do { q = rson[q]; } while (rson[q] != NIL);
5526 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
5527 + lson[q] = lson[p]; dad[lson[p]] = q;
5529 + rson[q] = rson[p]; dad[rson[p]] = q;
5532 + if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
5536 +/* Huffman coding */
5538 +#define N_CHAR (256 - THRESHOLD + F)
5539 + /* kinds of characters (character code = 0..N_CHAR-1) */
5540 +#define T (N_CHAR * 2 - 1) /* size of table */
5541 +#define R (T - 1) /* position of root */
5542 +#define MAX_FREQ 0x8000 /* updates tree when the */
5543 + /* root frequency comes to this value. */
5545 +typedef unsigned long uchar; // much-much faster
5547 +/* table for encoding and decoding the upper 6 bits of position */
5550 +static uchar p_len[64] = {
5551 + 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
5552 + 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06,
5553 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5554 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5555 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5556 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5557 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5558 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
5561 +static uchar p_code[64] = {
5562 + 0x00, 0x20, 0x30, 0x40, 0x50, 0x58, 0x60, 0x68,
5563 + 0x70, 0x78, 0x80, 0x88, 0x90, 0x94, 0x98, 0x9C,
5564 + 0xA0, 0xA4, 0xA8, 0xAC, 0xB0, 0xB4, 0xB8, 0xBC,
5565 + 0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE,
5566 + 0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE,
5567 + 0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEC, 0xEE,
5568 + 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
5569 + 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
5573 +static uchar d_code[256] = {
5574 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5575 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5576 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5577 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5578 + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
5579 + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
5580 + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
5581 + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
5582 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5583 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5584 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5585 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5586 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5587 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5588 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5589 + 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
5590 + 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
5591 + 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
5592 + 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
5593 + 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
5594 + 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
5595 + 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
5596 + 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
5597 + 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
5598 + 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
5599 + 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
5600 + 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
5601 + 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
5602 + 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
5603 + 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
5604 + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
5605 + 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
5608 +static uchar d_len[256] = {
5609 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5610 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5611 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5612 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5613 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5614 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5615 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5616 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5617 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5618 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5619 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5620 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5621 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5622 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5623 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5624 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5625 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5626 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5627 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5628 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5629 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5630 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5631 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5632 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5633 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5634 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5635 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5636 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5637 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5638 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5639 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5640 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5643 +static unsigned long freq[T + 1]; /* frequency table */
5645 +static unsigned long prnt[T + N_CHAR]; /* pointers to parent nodes, except for the */
5646 + /* elements [T..T + N_CHAR - 1] which are used to get */
5647 + /* the positions of leaves corresponding to the codes. */
5649 +static unsigned long son[T]; /* pointers to child nodes (son[], son[] + 1) */
5651 +/* initialization of tree */
5653 +static void StartHuff(void)
5655 + unsigned long i, j;
5657 + for (i = 0; i < N_CHAR; i++) {
5662 + i = 0; j = N_CHAR;
5664 + freq[j] = freq[i] + freq[i + 1];
5666 + prnt[i] = prnt[i + 1] = j;
5673 +/* reconstruction of tree */
5675 +static void reconst(void)
5677 + unsigned long f, l, i, j, k;
5679 + /* collect leaf nodes in the first half of the table */
5680 + /* and replace the freq by (freq + 1) / 2. */
5682 + for (i = 0; i < T; i++) {
5683 + if (son[i] >= T) {
5684 + freq[j] = (freq[i] + 1) / 2;
5689 + /* begin constructing tree by connecting sons */
5690 + for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
5692 + f = freq[j] = freq[i] + freq[k];
5693 + for (k = j - 1; f < freq[k]; k--);
5696 + memmove(&freq[k + 1], &freq[k], l*sizeof(unsigned long));
5698 + memmove(&son[k + 1], &son[k], l*sizeof(unsigned long));
5701 + /* connect prnt */
5702 + for (i = 0; i < T; i++) {
5703 + if ((k = son[i]) >= T) {
5706 + prnt[k] = prnt[k + 1] = i;
5711 +/* increment frequency of given code by one, and update tree */
5713 +static void update(unsigned long c)
5715 + unsigned long i, j, k, l;
5717 + if (freq[R] == MAX_FREQ) {
5724 + /* if the order is disturbed, exchange nodes */
5725 + if (k > freq[l = c + 1]) {
5726 + while (k > freq[++l]);
5728 + freq[c] = freq[l];
5733 + if (i < T) prnt[i + 1] = l;
5739 + if (j < T) prnt[j + 1] = c;
5744 + } while (c = prnt[c]); /* repeat up to root */
5747 +/* modified for block compression */
5748 +/* on return, srclen will contain the number of successfully compressed bytes
5749 + and dstlen will contain completed compressed bytes */
5751 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
5752 + unsigned long *dstlen)
5754 + unsigned long c, i, j, k, len, r, s, last_match_length, code_buf_ptr;
5755 + unsigned char code_buf[17], mask;
5756 + unsigned char *ip, *op;
5757 + unsigned long written = 0;
5758 + unsigned long read = 0;
5759 + unsigned short putbuf = 0;
5761 + unsigned char *srcend = srcbuf + *srclen;
5762 + unsigned char *dstend = dstbuf + *dstlen;
5766 + InitTree(); /* initialize trees */
5767 + code_buf[0] = 0; /* code_buf[1..16] saves eight units of code, and
5768 + code_buf[0] works as eight flags, "1" representing that the unit
5769 + is an unencoded letter (1 byte), "0" a position-and-length pair
5770 + (2 bytes). Thus, eight units require at most 16 bytes of code. */
5771 + code_buf_ptr = mask = 1;
5773 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
5774 + any character that will appear often. */
5775 + for (len = 0; (len < F) && (ip < srcend); len++)
5776 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
5779 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
5780 + each of which begins with one or more 'space' characters. Note
5781 + the order in which these strings are inserted. This way,
5782 + degenerate trees will be less likely to occur. */
5783 + InsertNode(r); /* Finally, insert the whole string just read. The
5784 + global variables match_length and match_position are set. */
5786 + if (match_length > len) match_length = len; /* match_length
5787 + may be spuriously long near the end of text. */
5788 + if (match_length <= THRESHOLD) {
5789 + match_length = 1; /* Not long enough match. Send one byte. */
5791 + i = 0; j = 0; k = prnt[c + T];
5794 + /* if node's address is odd-numbered, choose bigger brother node */
5795 + if (k & 1) i |= 0x8000;
5797 + } while ((k = prnt[k]) != R);
5798 + putbuf |= i >> putlen;
5799 + if ((putlen += j) >= 8) {
5800 + if (op >= dstend) {
5801 + *dstlen = written;
5804 + *(op++) = putbuf >> 8;
5805 + if ((putlen -= 8) >= 8) {
5806 + if (op >= dstend) {
5807 + *dstlen = written;
5813 + putbuf = i << (j - putlen); /**warm**/
5822 + c = 255 - THRESHOLD + match_length;
5823 + i = 0; j = 0; k = prnt[c + T];
5826 + /* if node's address is odd-numbered, choose bigger brother node */
5827 + if (k & 1) i |= 0x8000;
5829 + } while ((k = prnt[k]) != R);
5830 + putbuf |= i >> putlen;
5831 + if ((putlen += j) >= 8) {
5832 + if (op >= dstend) {
5833 + *dstlen = written;
5836 + *(op++) = putbuf >> 8;
5837 + if ((putlen -= 8) >= 8) {
5838 + if (op >= dstend) {
5839 + *dstlen = written;
5845 + putbuf = i << (j - putlen); /**warm**/
5853 + j = p_len[match_position >> 6];
5854 + i = p_code[match_position >> 6] << 8;
5855 + putbuf |= i >> putlen;
5856 + if ((putlen += j) >= 8) {
5857 + if (op >= dstend) {
5858 + *dstlen = written;
5861 + *(op++) = putbuf >> 8;
5862 + if ((putlen -= 8) >= 8) {
5863 + if (op >= dstend) {
5864 + *dstlen = written;
5870 + putbuf = i << (j - putlen); /**hot**/
5878 + i = (match_position & 0x3f) << 10;
5879 + putbuf |= i >> putlen;
5880 + if ((putlen += j) >= 8) {
5881 + if (op >= dstend) {
5882 + *dstlen = written;
5885 + *(op++) = putbuf >> 8;
5886 + if ((putlen -= 8) >= 8) {
5887 + if (op >= dstend) {
5888 + *dstlen = written;
5894 + putbuf = i << (j - putlen); /**hot**/
5902 + last_match_length = match_length;
5903 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
5908 + text_buf[s + N] = c;
5909 + s = (s + 1) & (N - 1);
5910 + r = (r + 1) & (N - 1);
5914 + while (i++ < last_match_length) {
5916 + s = (s + 1) & (N - 1);
5917 + r = (r + 1) & (N - 1);
5918 + if (--len) InsertNode(r);
5920 + } while (len > 0);
5922 + if (op >= dstend) {
5923 + *dstlen = written;
5926 + *(op++) = putbuf >> 8;
5930 + *dstlen = written;
5934 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
5935 + unsigned long dstlen) /* Just the reverse of Encode(). */
5937 + unsigned long i, r, j, k, c;
5938 + unsigned char *ip, *op;
5939 + unsigned char *srcend = srcbuf + srclen;
5940 + unsigned char *dstend = dstbuf + dstlen;
5941 + unsigned short getbuf = 0;
5946 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
5948 + while (op < dstend) {
5950 + /* travel from root to leaf, */
5951 + /* choosing the smaller child node (son[]) if the read bit is 0, */
5952 + /* the bigger (son[]+1} if 1 */
5954 + while (getlen <= 8) {
5956 + t = (ip >= srcend) ? 0 : *(ip++);
5957 + getbuf |= t << (8 - getlen);
5960 + c += ((signed short)getbuf < 0);
5968 + if (op >= dstend) return -1;
5970 + text_buf[r++] = c;
5973 + j = c - 255 + THRESHOLD;
5974 + while (getlen <= 8) {
5976 + t = (ip >= srcend) ? 0 : *(ip++);
5977 + getbuf |= t << (8 - getlen);
5983 + c = d_code[i] << 6;
5985 + /* read lower 6 bits verbatim */
5988 + while (getlen <= 8) {
5990 + t = (ip >= srcend) ? 0 : *(ip++);
5991 + getbuf |= t << (8 - getlen);
5994 + i = (i << 1) + ((signed short)getbuf < 0);
5998 + i = c | (i & 0x3F);
6001 + for (k = 0; k < j; k++) {
6002 + c = text_buf[(i + k) & (N - 1)];
6003 + if (op >= dstend) return -1;
6005 + text_buf[r++] = c;
6013 +/* interface to jffs2 bbc follows */
6015 +#include "jffs2_bbc_framework.h"
6018 +#define JFFS2_BBC_LZHD_BLOCK_SIGN {0x3a, 0x98, 0xf7, 0xda}
6021 +jffs2_bbc_lzhd_compressor_init (void);
6024 +jffs2_bbc_lzhd_compressor_deinit (void);
6027 +jffs2_bbc_lzhd_compress (void *model, unsigned char *input,
6028 + unsigned char *output, unsigned long *sourcelen,
6029 + unsigned long *dstlen);
6032 +jffs2_bbc_lzhd_estimate (void *model, unsigned char *input,
6033 + unsigned long sourcelen, unsigned long *dstlen,
6034 + unsigned long *readtime, unsigned long *writetime);
6037 +jffs2_bbc_lzhd_decompress (void *model, unsigned char *input,
6038 + unsigned char *output, unsigned long sourcelen,
6039 + unsigned long dstlen);
6042 +jffs2_bbc_lzhd_proc_info (void);
6045 +jffs2_bbc_lzhd_proc_command (char *command);
6047 +struct jffs2_bbc_compressor_type jffs2_bbc_lzhd = {
6050 + JFFS2_BBC_LZHD_BLOCK_SIGN,
6051 + jffs2_bbc_lzhd_compressor_init,
6054 + jffs2_bbc_lzhd_compressor_deinit,
6055 + jffs2_bbc_lzhd_compress,
6056 + jffs2_bbc_lzhd_estimate,
6057 + jffs2_bbc_lzhd_decompress,
6058 + jffs2_bbc_lzhd_proc_info,
6059 + jffs2_bbc_lzhd_proc_command
6063 +jffs2_bbc_lzhd_compressor_init (void)
6069 +jffs2_bbc_lzhd_compressor_deinit (void)
6074 +jffs2_bbc_lzhd_compress (void *model, unsigned char *input,
6075 + unsigned char *output, unsigned long *sourcelen,
6076 + unsigned long *dstlen)
6079 + unsigned long dst = *dstlen;
6080 + *(output++) = jffs2_bbc_lzhd.block_sign[0];
6081 + *(output++) = jffs2_bbc_lzhd.block_sign[1];
6083 + retval = Encode(input, output, sourcelen, &dst);
6090 +jffs2_bbc_lzhd_estimate (void *model, unsigned char *input,
6091 + unsigned long sourcelen, unsigned long *dstlen,
6092 + unsigned long *readtime, unsigned long *writetime)
6094 + *dstlen = sourcelen * 55 / 100;
6095 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 8;
6096 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 65 / 10;
6101 +jffs2_bbc_lzhd_decompress (void *model, unsigned char *input,
6102 + unsigned char *output, unsigned long sourcelen,
6103 + unsigned long dstlen)
6105 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzhd.block_sign[0] ) ||
6106 + ( *(input++) != (unsigned char)jffs2_bbc_lzhd.block_sign[1] )
6110 + return Decode(input, output, sourcelen - 2, dstlen);
6115 +jffs2_bbc_lzhd_proc_info (void)
6117 + return "Lempel-Ziv-(dynamic) Huffman compression module";
6121 +jffs2_bbc_lzhd_proc_command (char *command)
6126 +struct jffs2_bbc_compressor_type *
6127 +jffs2_bbc_lzhd_init (int mode)
6129 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzhd) == 0)
6131 + return &jffs2_bbc_lzhd;
6140 +jffs2_bbc_lzhd_deinit (void)
6142 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzhd);
6144 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzo_comp.c
6145 ===================================================================
6147 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzo_comp.c
6149 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
6152 + jffs2_bbc_lzo_comp.c -- LZO1X-1 (and -999) compression module for jffs2
6153 + Copyright (C) 2004 Patrik Kluba
6154 + Based on the original LZO sources
6155 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
6159 + Original copyright notice follows:
6161 + lzo1x_9x.c -- implementation of the LZO1X-999 compression algorithm
6162 + lzo_ptr.h -- low-level pointer constructs
6163 + lzo_swd.ch -- sliding window dictionary
6164 + lzoconf.h -- configuration for the LZO real-time data compression library
6165 + lzo_mchw.ch -- matching functions using a window
6166 + minilzo.c -- mini subset of the LZO real-time data compression library
6167 + config1x.h -- configuration for the LZO1X algorithm
6168 + lzo1x.h -- public interface of the LZO1X compression algorithm
6170 + These files are part of the LZO real-time data compression library.
6172 + Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
6173 + All Rights Reserved.
6175 + The LZO library is free software; you can redistribute it and/or
6176 + modify it under the terms of the GNU General Public License as
6177 + published by the Free Software Foundation; either version 2 of
6178 + the License, or (at your option) any later version.
6180 + The LZO library is distributed in the hope that it will be useful,
6181 + but WITHOUT ANY WARRANTY; without even the implied warranty of
6182 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6183 + GNU General Public License for more details.
6185 + You should have received a copy of the GNU General Public License
6186 + along with the LZO library; see the file COPYING.
6187 + If not, write to the Free Software Foundation, Inc.,
6188 + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
6190 + Markus F.X.J. Oberhumer
6191 + <markus@oberhumer.com>
6196 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
6198 + -removed all 16 bit code
6199 + -all sensitive data will be on 4 byte boundary
6200 + -removed check parts for library use
6201 + -removed all but LZO1X-* compression
6206 + #include <sys/types.h>
6207 + #include <stddef.h>
6208 + #include <string.h>
6209 + #include <limits.h>
6211 + #include <linux/kernel.h>
6212 + #include <linux/types.h>
6213 + #include <linux/stddef.h>
6214 + #include <linux/string.h>
6215 + #define USHRT_MAX 65535
6216 + /* #define UINT_MAX 4294967295U */
6219 +/* data type definitions */
6220 +#define U32 unsigned long
6221 +#define S32 signed long
6223 +#define U16 unsigned short
6224 +#define S16 signed short
6226 +#define U8 unsigned char
6227 +#define S8 signed char
6230 +/*************************************/
6236 +#define SWD_THRESHOLD THRESHOLD
6238 +/* shortest unsigned int that 2 * SWD_F + SWD_N (currently 53248) fits in */
6239 +typedef unsigned short swd_uint;
6240 +/* upper limit of that data type */
6241 +#define SWD_UINT_MAX USHRT_MAX
6245 +#define LZO_VERSION_DATE "Jul 12 2002"
6246 +#define LZO_VERSION_STRING "1.08"
6247 +#define LZO_VERSION 0x1080
6251 +/* Integral types that have *exactly* the same number of bits as a lzo_voidp */
6252 +typedef unsigned long lzo_ptr_t;
6253 +typedef long lzo_sptr_t;
6256 +/*************************************/
6260 +#define M1_MAX_OFFSET 0x0400
6261 +#define M2_MAX_OFFSET 0x0800
6262 +#define M3_MAX_OFFSET 0x4000
6263 +#define M4_MAX_OFFSET 0xbfff
6265 +#define MX_MAX_OFFSET (M1_MAX_OFFSET + M2_MAX_OFFSET)
6267 +#define M1_MIN_LEN 2
6268 +#define M1_MAX_LEN 2
6269 +#define M2_MIN_LEN 3
6270 +#define M2_MAX_LEN 8
6271 +#define M3_MIN_LEN 3
6272 +#define M3_MAX_LEN 33
6273 +#define M4_MIN_LEN 3
6274 +#define M4_MAX_LEN 9
6276 +#define M1_MARKER 0
6277 +#define M2_MARKER 64
6278 +#define M3_MARKER 32
6279 +#define M4_MARKER 16
6281 +#define MIN_LOOKAHEAD (M2_MAX_LEN + 1)
6285 +#define LZO_BYTE(x) ((unsigned char) ((x) & 0xff))
6287 +#define LZO_MAX(a,b) ((a) >= (b) ? (a) : (b))
6288 +#define LZO_MIN(a,b) ((a) <= (b) ? (a) : (b))
6289 +#define LZO_MAX3(a,b,c) ((a) >= (b) ? LZO_MAX(a,c) : LZO_MAX(b,c))
6290 +#define LZO_MIN3(a,b,c) ((a) <= (b) ? LZO_MIN(a,c) : LZO_MIN(b,c))
6292 +#define lzo_sizeof(type) ((lzo_uint) (sizeof(type)))
6294 +#define LZO_HIGH(array) ((lzo_uint) (sizeof(array)/sizeof(*(array))))
6296 +#define LZO_SIZE(bits) (1u << (bits))
6297 +#define LZO_MASK(bits) (LZO_SIZE(bits) - 1)
6299 +#define LZO_LSIZE(bits) (1ul << (bits))
6300 +#define LZO_LMASK(bits) (LZO_LSIZE(bits) - 1)
6302 +#define LZO_USIZE(bits) ((lzo_uint) 1 << (bits))
6303 +#define LZO_UMASK(bits) (LZO_USIZE(bits) - 1)
6305 +#define LZO_STYPE_MAX(b) (((1l << (8*(b)-2)) - 1l) + (1l << (8*(b)-2)))
6306 +#define LZO_UTYPE_MAX(b) (((1ul << (8*(b)-1)) - 1ul) + (1ul << (8*(b)-1)))
6308 +#define _LZO_STRINGIZE(x) #x
6309 +#define _LZO_MEXPAND(x) _LZO_STRINGIZE(x)
6311 +#define _LZO_CONCAT2(a,b) a ## b
6312 +#define _LZO_CONCAT3(a,b,c) a ## b ## c
6313 +#define _LZO_CONCAT4(a,b,c,d) a ## b ## c ## d
6314 +#define _LZO_CONCAT5(a,b,c,d,e) a ## b ## c ## d ## e
6316 +#define _LZO_ECONCAT2(a,b) _LZO_CONCAT2(a,b)
6317 +#define _LZO_ECONCAT3(a,b,c) _LZO_CONCAT3(a,b,c)
6318 +#define _LZO_ECONCAT4(a,b,c,d) _LZO_CONCAT4(a,b,c,d)
6319 +#define _LZO_ECONCAT5(a,b,c,d,e) _LZO_CONCAT5(a,b,c,d,e)
6321 +#define lzo_dict_t const lzo_bytep
6322 +#define lzo_dict_p lzo_dict_t *
6323 +#define lzo_moff_t lzo_uint
6325 +#define MEMCPY8_DS(dest,src,len) \
6326 + memcpy(dest,src,len); \
6330 +#define MEMCPY_DS(dest,src,len) \
6331 + do *dest++ = *src++; \
6334 +#define MEMMOVE_DS(dest,src,len) \
6335 + do *dest++ = *src++; \
6338 +#define BZERO8_PTR(s,l,n) memset((s),0,(lzo_uint)(l)*(n))
6340 +#define LZO_BASE 65521u
6341 +#define LZO_NMAX 5552
6343 +#define LZO_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
6344 +#define LZO_DO2(buf,i) LZO_DO1(buf,i); LZO_DO1(buf,i+1);
6345 +#define LZO_DO4(buf,i) LZO_DO2(buf,i); LZO_DO2(buf,i+2);
6346 +#define LZO_DO8(buf,i) LZO_DO4(buf,i); LZO_DO4(buf,i+4);
6347 +#define LZO_DO16(buf,i) LZO_DO8(buf,i); LZO_DO8(buf,i+8);
6349 +#define IS_SIGNED(type) (((type) (-1)) < ((type) 0))
6350 +#define IS_UNSIGNED(type) (((type) (-1)) > ((type) 0))
6352 +#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
6355 +#define D_INDEX1(d,p) d = DM((0x21*DX3(p,5,5,6)) >> 5)
6356 +#define D_INDEX2(d,p) d = (d & (D_MASK & 0x7ff)) ^ (D_HIGH | 0x1f)
6358 +#define LZO_HASH LZO_HASH_LZO_INCREMENTAL_B
6360 +#define DL_MIN_LEN M2_MIN_LEN
6362 +#define D_SIZE LZO_SIZE(D_BITS)
6363 +#define D_MASK LZO_MASK(D_BITS)
6365 +#define D_HIGH ((D_MASK >> 1) + 1)
6367 +#define DINDEX1 D_INDEX1
6368 +#define DINDEX2 D_INDEX2
6370 +#define DX2(p,s1,s2) \
6371 + (((((lzo_uint32)((p)[2]) << (s2)) ^ (p)[1]) << (s1)) ^ (p)[0])
6373 +#define DX3(p,s1,s2,s3) ((DX2((p)+1,s2,s3) << (s1)) ^ (p)[0])
6374 +#define DMS(v,s) ((lzo_uint) (((v) & (D_MASK >> (s))) << (s)))
6375 +#define DM(v) DMS(v,0)
6377 +#define DENTRY(p,in) (p)
6378 +#define GINDEX(m_pos,m_off,dict,dindex,in) m_pos = dict[dindex]
6380 +#define LZO_CHECK_MPOS_DET(m_pos,m_off,in,ip,max_offset) \
6381 + (m_pos == NULL || (m_off = (lzo_moff_t) (ip - m_pos)) > max_offset)
6383 +#define LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
6384 + (BOUNDS_CHECKING_OFF_IN_EXPR( \
6385 + (PTR_LT(m_pos,in) || \
6386 + (m_off = (lzo_moff_t) PTR_DIFF(ip,m_pos)) <= 0 || \
6387 + m_off > max_offset) ))
6389 +#define BOUNDS_CHECKING_OFF_IN_EXPR(expr) (expr)
6392 +#define DD_SIZE LZO_SIZE(DD_BITS)
6393 +#define DD_MASK LZO_MASK(DD_BITS)
6395 +#define DL_BITS (D_BITS - DD_BITS)
6396 +#define DL_SIZE LZO_SIZE(DL_BITS)
6397 +#define DL_MASK LZO_MASK(DL_BITS)
6399 +#define UPDATE_D(dict,drun,dv,p,in) dict[ DINDEX(dv,p) ] = DENTRY(p,in)
6400 +#define UPDATE_I(dict,drun,index,p,in) dict[index] = DENTRY(p,in)
6401 +#define UPDATE_P(ptr,drun,p,in) (ptr)[0] = DENTRY(p,in)
6403 +#define __COPY4(dst,src) * (lzo_uint32p)(dst) = * (const lzo_uint32p)(src)
6404 +#define COPY4(dst,src) __COPY4((lzo_ptr_t)(dst),(lzo_ptr_t)(src))
6406 +#define TEST_IP (ip < ip_end)
6407 +#define TEST_OP (op <= op_end)
6409 +#define NEED_IP(x) \
6410 + if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x)) goto input_overrun
6411 +#define NEED_OP(x) \
6412 + if ((lzo_uint)(op_end - op) < (lzo_uint)(x)) goto output_overrun
6413 +#define TEST_LOOKBEHIND(m_pos,out) if (m_pos < out) goto lookbehind_overrun
6417 +#define LZO_UINT_MAX UINT_MAX
6418 +#define N M4_MAX_OFFSET
6419 +#define THRESHOLD 1
6422 +#define SWD_BEST_OFF (LZO_MAX3( M2_MAX_LEN, M3_MAX_LEN, M4_MAX_LEN ) + 1)
6424 +/* ../include/lzoconf.h */
6426 +typedef U32 lzo_uint32;
6427 +typedef I32 lzo_int32;
6428 +typedef U32 lzo_uint;
6429 +typedef I32 lzo_int;
6430 +typedef int lzo_bool;
6432 +#define lzo_byte U8
6433 +#define lzo_bytep U8 *
6434 +#define lzo_charp char *
6435 +#define lzo_voidp void *
6436 +#define lzo_shortp short *
6437 +#define lzo_ushortp unsigned short *
6438 +#define lzo_uint32p lzo_uint32 *
6439 +#define lzo_int32p lzo_int32 *
6440 +#define lzo_uintp lzo_uint *
6441 +#define lzo_intp lzo_int *
6442 +#define lzo_voidpp lzo_voidp *
6443 +#define lzo_bytepp lzo_bytep *
6444 +#define lzo_sizeof_dict_t sizeof(lzo_bytep)
6447 +#define LZO_E_ERROR (-1)
6448 +#define LZO_E_OUT_OF_MEMORY (-2) /* not used right now */
6449 +#define LZO_E_NOT_COMPRESSIBLE (-3) /* not used right now */
6450 +#define LZO_E_INPUT_OVERRUN (-4)
6451 +#define LZO_E_OUTPUT_OVERRUN (-5)
6452 +#define LZO_E_LOOKBEHIND_OVERRUN (-6)
6453 +#define LZO_E_EOF_NOT_FOUND (-7)
6454 +#define LZO_E_INPUT_NOT_CONSUMED (-8)
6456 +#define LZO_PTR_ALIGN_UP(_ptr,_size) \
6457 + ((_ptr) + (lzo_uint) __lzo_align_gap((const lzo_voidp)(_ptr),(lzo_uint)(_size)))
6458 +#define LZO_ALIGN(_ptr,_size) LZO_PTR_ALIGN_UP(_ptr,_size)
6461 + (*lzo_compress_t) (const lzo_byte * src, lzo_uint src_len,
6462 + lzo_byte * dst, lzo_uintp dst_len,
6463 + lzo_voidp wrkmem);
6466 + (*lzo_decompress_t) (const lzo_byte * src, lzo_uint src_len,
6467 + lzo_byte * dst, lzo_uintp dst_len,
6468 + lzo_voidp wrkmem);
6471 + (*lzo_optimize_t) (lzo_byte * src, lzo_uint src_len,
6472 + lzo_byte * dst, lzo_uintp dst_len,
6473 + lzo_voidp wrkmem);
6476 + (*lzo_compress_dict_t) (const lzo_byte * src, lzo_uint src_len,
6477 + lzo_byte * dst, lzo_uintp dst_len,
6479 + const lzo_byte * dict, lzo_uint dict_len);
6482 + (*lzo_decompress_dict_t) (const lzo_byte * src, lzo_uint src_len,
6483 + lzo_byte * dst, lzo_uintp dst_len,
6485 + const lzo_byte * dict, lzo_uint dict_len);
6488 + (*lzo_compress_asm_t) (const lzo_byte * src, lzo_uint src_len,
6489 + lzo_byte * dst, lzo_uintp dst_len,
6490 + lzo_voidp wrkmem);
6493 + (*lzo_decompress_asm_t) (const lzo_byte * src, lzo_uint src_len,
6494 + lzo_byte * dst, lzo_uintp dst_len,
6495 + lzo_voidp wrkmem);
6497 +typedef void (*lzo_progress_callback_t) (lzo_uint, lzo_uint);
6519 +#define LZO1X_1_MEM_COMPRESS ((lzo_uint32) (16384L * lzo_sizeof_dict_t))
6520 +#define LZO1X_999_MEM_COMPRESS ((lzo_uint32) (14 * 16384L * sizeof(short)))
6524 +#define PTR(a) ((lzo_ptr_t) (a))
6525 +#define PTR_LINEAR(a) PTR(a)
6526 +#define PTR_ALIGNED_4(a) ((PTR_LINEAR(a) & 3) == 0)
6527 +#define PTR_ALIGNED_8(a) ((PTR_LINEAR(a) & 7) == 0)
6528 +#define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
6529 +#define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
6530 +#define PTR_LT(a,b) (PTR(a) < PTR(b))
6531 +#define PTR_GE(a,b) (PTR(a) >= PTR(b))
6532 +#define PTR_DIFF(a,b) ((lzo_ptrdiff_t) (PTR(a) - PTR(b)))
6533 +#define pd(a,b) ((lzo_uint) ((a)-(b)))
6535 +typedef ptrdiff_t lzo_ptrdiff_t;
6540 + unsigned char a_uchar;
6542 + unsigned short a_ushort;
6544 + unsigned int a_uint;
6546 + unsigned long a_ulong;
6547 + lzo_int a_lzo_int;
6548 + lzo_uint a_lzo_uint;
6549 + lzo_int32 a_lzo_int32;
6550 + lzo_uint32 a_lzo_uint32;
6551 + ptrdiff_t a_ptrdiff_t;
6552 + lzo_ptrdiff_t a_lzo_ptrdiff_t;
6553 + lzo_ptr_t a_lzo_ptr_t;
6554 + lzo_voidp a_lzo_voidp;
6556 + lzo_bytep a_lzo_bytep;
6557 + lzo_bytepp a_lzo_bytepp;
6558 + lzo_uintp a_lzo_uintp;
6559 + lzo_uint *a_lzo_uint_p;
6560 + lzo_uint32p a_lzo_uint32p;
6561 + lzo_uint32 *a_lzo_uint32_p;
6562 + unsigned char *a_uchar_p;
6578 + lzo_uint last_m_len;
6579 + lzo_uint last_m_off;
6581 + const lzo_byte *bp;
6582 + const lzo_byte *ip;
6583 + const lzo_byte *in;
6584 + const lzo_byte *in_end;
6587 + lzo_progress_callback_t cb;
6589 + lzo_uint textsize;
6590 + lzo_uint codesize;
6591 + lzo_uint printcount;
6593 + unsigned long lit_bytes;
6594 + unsigned long match_bytes;
6595 + unsigned long rep_bytes;
6596 + unsigned long lazy;
6599 + lzo_uint r1_m_len;
6601 + unsigned long m1a_m, m1b_m, m2_m, m3_m, m4_m;
6602 + unsigned long lit1_r, lit2_r, lit3_r;
6606 +#define getbyte(c) ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
6610 +#define SWD_UINT(x) ((swd_uint)(x))
6611 +#define SWD_HSIZE 16384
6612 +#define SWD_MAX_CHAIN 2048
6613 +#define HEAD3(b,p) \
6614 + (((0x9f5f*(((((lzo_uint32)b[p]<<5)^b[p+1])<<5)^b[p+2]))>>5) & (SWD_HSIZE-1))
6615 +#define HEAD2(b,p) (b[p] ^ ((unsigned)b[p+1]<<8))
6616 +#define NIL2 SWD_UINT_MAX
6622 + lzo_uint threshold;
6624 + lzo_uint max_chain;
6625 + lzo_uint nice_length;
6626 + lzo_bool use_best_off;
6627 + lzo_uint lazy_insert;
6634 + lzo_uint best_off[SWD_BEST_OFF];
6639 + lzo_uint best_pos[SWD_BEST_OFF];
6641 + const lzo_byte *dict;
6642 + const lzo_byte *dict_end;
6643 + lzo_uint dict_len;
6650 + unsigned char *b_wrap;
6652 + lzo_uint node_count;
6653 + lzo_uint first_rp;
6655 + unsigned char b[SWD_N + SWD_F + SWD_F];
6656 + swd_uint head3[SWD_HSIZE];
6657 + swd_uint succ3[SWD_N + SWD_F];
6658 + swd_uint best3[SWD_N + SWD_F];
6659 + swd_uint llen3[SWD_HSIZE];
6661 + swd_uint head2[65536L];
6665 +#define s_head3(s,key) s->head3[key]
6666 +#define swd_pos2off(s,pos) \
6667 + (s->bp > (pos) ? s->bp - (pos) : s->b_size - ((pos) - s->bp))
6669 +static __inline__ void
6670 +swd_getbyte (lzo1x_999_swd_t * s)
6674 + if ((c = getbyte (*(s->c))) < 0)
6681 + s->b[s->ip] = LZO_BYTE (c);
6683 + s->b_wrap[s->ip] = LZO_BYTE (c);
6685 + if (++s->ip == s->b_size)
6687 + if (++s->bp == s->b_size)
6689 + if (++s->rp == s->b_size)
6694 +swd_initdict (lzo1x_999_swd_t * s, const lzo_byte * dict, lzo_uint dict_len)
6696 + s->dict = s->dict_end = NULL;
6699 + if (!dict || dict_len <= 0)
6701 + if (dict_len > s->n)
6703 + dict += dict_len - s->n;
6708 + s->dict_len = dict_len;
6709 + s->dict_end = dict + dict_len;
6710 + memcpy (s->b, dict, dict_len);
6715 +swd_insertdict (lzo1x_999_swd_t * s, lzo_uint node, lzo_uint len)
6719 + s->node_count = s->n - len;
6720 + s->first_rp = node;
6724 + key = HEAD3 (s->b, node);
6725 + s->succ3[node] = s_head3 (s, key);
6726 + s->head3[key] = SWD_UINT (node);
6727 + s->best3[node] = SWD_UINT (s->f + 1);
6730 + key = HEAD2 (s->b, node);
6731 + s->head2[key] = SWD_UINT (node);
6738 +swd_init (lzo1x_999_swd_t * s, const lzo_byte * dict, lzo_uint dict_len)
6743 + s->threshold = SWD_THRESHOLD;
6747 + s->max_chain = SWD_MAX_CHAIN;
6748 + s->nice_length = SWD_F;
6749 + s->use_best_off = 0;
6750 + s->lazy_insert = 0;
6752 + s->b_size = s->n + s->f;
6753 + if (2 * s->f >= s->n || s->b_size + s->f >= NIL2)
6754 + return LZO_E_ERROR;
6755 + s->b_wrap = s->b + s->b_size;
6756 + s->node_count = s->n;
6758 + memset (s->llen3, 0, sizeof (s->llen3[0]) * SWD_HSIZE);
6759 + memset (s->head2, 0xff, sizeof (s->head2[0]) * 65536L);
6762 + swd_initdict (s, dict, dict_len);
6764 + s->first_rp = s->ip;
6766 + s->look = (lzo_uint) (s->c->in_end - s->c->ip);
6769 + if (s->look > s->f)
6771 + memcpy (&s->b[s->ip], s->c->ip, s->look);
6772 + s->c->ip += s->look;
6776 + if (s->ip == s->b_size)
6779 + if (s->look >= 2 && s->dict_len > 0)
6780 + swd_insertdict (s, 0, s->dict_len);
6782 + s->rp = s->first_rp;
6783 + if (s->rp >= s->node_count)
6784 + s->rp -= s->node_count;
6786 + s->rp += s->b_size - s->node_count;
6791 +static __inline__ void
6792 +swd_remove_node (lzo1x_999_swd_t * s, lzo_uint node)
6794 + if (s->node_count == 0)
6798 + key = HEAD3 (s->b, node);
6802 + key = HEAD2 (s->b, node);
6804 + if ((lzo_uint) s->head2[key] == node)
6805 + s->head2[key] = NIL2;
6812 +swd_accept (lzo1x_999_swd_t * s, lzo_uint n)
6819 + swd_remove_node (s, s->rp);
6821 + key = HEAD3 (s->b, s->bp);
6822 + s->succ3[s->bp] = s_head3 (s, key);
6823 + s->head3[key] = SWD_UINT (s->bp);
6824 + s->best3[s->bp] = SWD_UINT (s->f + 1);
6827 + key = HEAD2 (s->b, s->bp);
6828 + s->head2[key] = SWD_UINT (s->bp);;
6835 +swd_search (lzo1x_999_swd_t * s, lzo_uint node, lzo_uint cnt)
6837 + const unsigned char *p1;
6838 + const unsigned char *p2;
6839 + const unsigned char *px;
6841 + lzo_uint m_len = s->m_len;
6842 + const unsigned char *b = s->b;
6843 + const unsigned char *bp = s->b + s->bp;
6844 + const unsigned char *bx = s->b + s->bp + s->look;
6845 + unsigned char scan_end1;
6847 + scan_end1 = bp[m_len - 1];
6848 + for (; cnt-- > 0; node = s->succ3[node])
6854 + if (p2[m_len - 1] == scan_end1 &&
6855 + p2[m_len] == p1[m_len] &&
6856 + p2[0] == p1[0] && p2[1] == p1[1])
6865 + while (++p1 < px && *p1 == *++p2);
6869 + if (i < SWD_BEST_OFF)
6871 + if (s->best_pos[i] == 0)
6872 + s->best_pos[i] = node + 1;
6877 + s->m_len = m_len = i;
6879 + if (m_len == s->look)
6881 + if (m_len >= s->nice_length)
6883 + if (m_len > (lzo_uint) s->best3[node])
6885 + scan_end1 = bp[m_len - 1];
6892 +swd_search2 (lzo1x_999_swd_t * s)
6896 + key = s->head2[HEAD2 (s->b, s->bp)];
6900 + if (s->best_pos[2] == 0)
6901 + s->best_pos[2] = key + 1;
6912 +swd_findbest (lzo1x_999_swd_t * s)
6915 + lzo_uint cnt, node;
6918 + key = HEAD3 (s->b, s->bp);
6919 + node = s->succ3[s->bp] = s_head3 (s, key);
6920 + cnt = s->llen3[key]++;
6922 + if (cnt > s->max_chain && s->max_chain > 0)
6923 + cnt = s->max_chain;
6924 + s->head3[key] = SWD_UINT (s->bp);
6926 + s->b_char = s->b[s->bp];
6928 + if (s->m_len >= s->look)
6933 + s->best3[s->bp] = SWD_UINT (s->f + 1);
6938 + if (swd_search2 (s))
6941 + swd_search (s, node, cnt);
6942 + if (s->m_len > len)
6943 + s->m_off = swd_pos2off (s, s->m_pos);
6944 + s->best3[s->bp] = SWD_UINT (s->m_len);
6946 + if (s->use_best_off)
6949 + for (i = 2; i < SWD_BEST_OFF; i++)
6950 + if (s->best_pos[i] > 0)
6956 + s->best_off[i] = 0;
6961 + swd_remove_node (s, s->rp);
6963 + key = HEAD2 (s->b, s->bp);
6964 + s->head2[key] = SWD_UINT (s->bp);
6971 +init_match (lzo1x_999_t * c, lzo1x_999_swd_t * s,
6972 + const lzo_byte * dict, lzo_uint dict_len, lzo_uint32 flags)
6980 + c->last_m_len = c->last_m_off = 0;
6982 + c->textsize = c->codesize = c->printcount = 0;
6983 + c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
6986 + r = swd_init (s, dict, dict_len);
6990 + s->use_best_off = (flags & 1) ? 1 : 0;
6995 +find_match (lzo1x_999_t * c, lzo1x_999_swd_t * s,
6996 + lzo_uint this_len, lzo_uint skip)
7000 + swd_accept (s, this_len - skip);
7001 + c->textsize += this_len - skip + 1;
7005 + c->textsize += this_len - skip;
7011 + if (s->use_best_off)
7012 + memset (s->best_pos, 0, sizeof (s->best_pos));
7015 + c->m_len = s->m_len;
7016 + c->m_off = s->m_off;
7020 + if (s->b_char < 0)
7027 + c->look = s->look + 1;
7029 + c->bp = c->ip - c->look;
7031 + if (c->cb && c->textsize > c->printcount)
7033 + (*c->cb) (c->textsize, c->codesize);
7034 + c->printcount += 1024;
7043 +code_match (lzo1x_999_t * c, lzo_byte * op, lzo_uint m_len, lzo_uint m_off)
7045 + lzo_uint x_len = m_len;
7046 + lzo_uint x_off = m_off;
7048 + c->match_bytes += m_len;
7054 + *op++ = LZO_BYTE (M1_MARKER | ((m_off & 3) << 2));
7055 + *op++ = LZO_BYTE (m_off >> 2);
7060 + else if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
7065 + *op++ = LZO_BYTE (((m_len - 1) << 5) | ((m_off & 7) << 2));
7066 + *op++ = LZO_BYTE (m_off >> 3);
7069 + else if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET
7070 + && c->r1_lit >= 4)
7072 + m_off -= 1 + M2_MAX_OFFSET;
7074 + *op++ = LZO_BYTE (M1_MARKER | ((m_off & 3) << 2));
7075 + *op++ = LZO_BYTE (m_off >> 2);
7079 + else if (m_off <= M3_MAX_OFFSET)
7082 + if (m_len <= M3_MAX_LEN)
7083 + *op++ = LZO_BYTE (M3_MARKER | (m_len - 2));
7086 + m_len -= M3_MAX_LEN;
7087 + *op++ = M3_MARKER | 0;
7088 + while (m_len > 255)
7093 + *op++ = LZO_BYTE (m_len);
7096 + *op++ = LZO_BYTE (m_off << 2);
7097 + *op++ = LZO_BYTE (m_off >> 6);
7106 + k = (m_off & 0x4000) >> 11;
7107 + if (m_len <= M4_MAX_LEN)
7108 + *op++ = LZO_BYTE (M4_MARKER | k | (m_len - 2));
7111 + m_len -= M4_MAX_LEN;
7112 + *op++ = LZO_BYTE (M4_MARKER | k | 0);
7113 + while (m_len > 255)
7118 + *op++ = LZO_BYTE (m_len);
7121 + *op++ = LZO_BYTE (m_off << 2);
7122 + *op++ = LZO_BYTE (m_off >> 6);
7127 + c->last_m_len = x_len;
7128 + c->last_m_off = x_off;
7133 +STORE_RUN (lzo1x_999_t * c, lzo_byte * op, const lzo_byte * ii, lzo_uint t)
7135 + c->lit_bytes += t;
7137 + if (op == c->out && t <= 238)
7139 + *op++ = LZO_BYTE (17 + t);
7143 + op[-2] |= LZO_BYTE (t);
7149 + *op++ = LZO_BYTE (t - 3);
7154 + lzo_uint tt = t - 18;
7162 + *op++ = LZO_BYTE (tt);
7173 +code_run (lzo1x_999_t * c, lzo_byte * op, const lzo_byte * ii,
7174 + lzo_uint lit, lzo_uint m_len)
7178 + op = STORE_RUN (c, op, ii, lit);
7179 + c->r1_m_len = m_len;
7192 +len_of_coded_match (lzo_uint m_len, lzo_uint m_off, lzo_uint lit)
7199 + return (m_off <= M1_MAX_OFFSET && lit > 0
7200 + && lit < 4) ? 2 : -1;
7201 + if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
7203 + if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4)
7205 + if (m_off <= M3_MAX_OFFSET)
7207 + if (m_len <= M3_MAX_LEN)
7209 + m_len -= M3_MAX_LEN;
7210 + while (m_len > 255)
7217 + if (m_off <= M4_MAX_OFFSET)
7219 + if (m_len <= M4_MAX_LEN)
7221 + m_len -= M4_MAX_LEN;
7222 + while (m_len > 255)
7233 +min_gain (lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, int l1, int l2,
7236 + lzo_int lazy_match_min_gain = 0;
7238 + lazy_match_min_gain += ahead;
7241 + lazy_match_min_gain += (lit2 <= 3) ? 0 : 2;
7242 + else if (lit1 <= 18)
7243 + lazy_match_min_gain += (lit2 <= 18) ? 0 : 1;
7245 + lazy_match_min_gain += (l2 - l1) * 2;
7247 + lazy_match_min_gain -= (ahead - l3) * 2;
7249 + if (lazy_match_min_gain < 0)
7250 + lazy_match_min_gain = 0;
7252 + return lazy_match_min_gain;
7256 +better_match (const lzo1x_999_swd_t * swd, lzo_uint * m_len, lzo_uint * m_off)
7258 + if (*m_len <= M2_MIN_LEN)
7261 + if (*m_off <= M2_MAX_OFFSET)
7264 + if (*m_off > M2_MAX_OFFSET &&
7265 + *m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
7266 + swd->best_off[*m_len - 1]
7267 + && swd->best_off[*m_len - 1] <= M2_MAX_OFFSET)
7269 + *m_len = *m_len - 1;
7270 + *m_off = swd->best_off[*m_len];
7274 + if (*m_off > M3_MAX_OFFSET &&
7275 + *m_len >= M4_MAX_LEN + 1 && *m_len <= M2_MAX_LEN + 2 &&
7276 + swd->best_off[*m_len - 2]
7277 + && swd->best_off[*m_len - 2] <= M2_MAX_OFFSET)
7279 + *m_len = *m_len - 2;
7280 + *m_off = swd->best_off[*m_len];
7284 + if (*m_off > M3_MAX_OFFSET &&
7285 + *m_len >= M4_MAX_LEN + 1 && *m_len <= M3_MAX_LEN + 1 &&
7286 + swd->best_off[*m_len - 1]
7287 + && swd->best_off[*m_len - 1] <= M3_MAX_OFFSET)
7289 + *m_len = *m_len - 1;
7290 + *m_off = swd->best_off[*m_len];
7298 +lzo_assert (int expr)
7300 + return (expr) ? 1 : 0;
7306 +lzo1x_999_compress_internal (const lzo_byte * in, lzo_uint in_len,
7307 + lzo_byte * out, lzo_uintp out_len,
7309 + const lzo_byte * dict, lzo_uint dict_len,
7310 + lzo_progress_callback_t cb,
7312 + lzo_uint good_length,
7313 + lzo_uint max_lazy,
7314 + lzo_uint nice_length,
7315 + lzo_uint max_chain, lzo_uint32 flags)
7318 + const lzo_byte *ii;
7320 + lzo_uint m_len, m_off;
7322 + lzo1x_999_t *const c = &cc;
7323 + lzo1x_999_swd_t *const swd = (lzo1x_999_swd_t *) wrkmem;
7327 + (LZO1X_999_MEM_COMPRESS >= lzo_sizeof (lzo1x_999_swd_t)))
7328 + return LZO_E_ERROR;
7333 + if (good_length <= 0)
7336 + if (max_lazy <= 0)
7339 + if (nice_length <= 0)
7342 + if (max_chain <= 0)
7343 + max_chain = SWD_MAX_CHAIN;
7346 + c->ip = c->in = in;
7347 + c->in_end = in + in_len;
7350 + c->m1a_m = c->m1b_m = c->m2_m = c->m3_m = c->m4_m = 0;
7351 + c->lit1_r = c->lit2_r = c->lit3_r = 0;
7356 + c->r1_lit = c->r1_m_len = 0;
7358 + r = init_match (c, swd, dict, dict_len, flags);
7361 + if (max_chain > 0)
7362 + swd->max_chain = max_chain;
7363 + if (nice_length > 0)
7364 + swd->nice_length = nice_length;
7366 + r = find_match (c, swd, 0, 0);
7369 + while (c->look > 0)
7372 + lzo_uint max_ahead;
7375 + c->codesize = op - out;
7385 + && (m_off > M1_MAX_OFFSET || lit == 0 || lit >= 4))
7386 + || (m_len == 2 && op == out) || (op == out && lit == 0))
7391 + else if (m_len == M2_MIN_LEN)
7394 + if (m_off > MX_MAX_OFFSET && lit >= 4)
7402 + swd->max_chain = max_chain;
7403 + r = find_match (c, swd, 1, 0);
7407 + if (swd->use_best_off)
7408 + better_match (swd, &m_len, &m_off);
7411 + if (try_lazy <= 0 || m_len >= max_lazy)
7420 + l1 = len_of_coded_match (m_len, m_off, lit);
7422 + max_ahead = LZO_MIN (try_lazy, l1 - 1);
7426 + while (ahead < max_ahead && c->look > m_len)
7428 + lzo_int lazy_match_min_gain;
7430 + if (m_len >= good_length)
7431 + swd->max_chain = max_chain >> 2;
7433 + swd->max_chain = max_chain;
7434 + r = find_match (c, swd, 1, 0);
7437 + if (c->m_len < m_len)
7440 + if (c->m_len == m_len && c->m_off >= m_off)
7443 + if (swd->use_best_off)
7444 + better_match (swd, &c->m_len, &c->m_off);
7446 + l2 = len_of_coded_match (c->m_len, c->m_off,
7451 + l3 = (op == out) ? -1 : len_of_coded_match (ahead,
7455 + lazy_match_min_gain =
7456 + min_gain (ahead, lit, lit + ahead, l1, l2,
7458 + if (c->m_len >= m_len + lazy_match_min_gain)
7465 + op = code_run (c, op, ii, lit, ahead);
7468 + op = code_match (c, op, ahead, m_off);
7474 + goto lazy_match_done;
7478 + op = code_run (c, op, ii, lit, m_len);
7481 + op = code_match (c, op, m_len, m_off);
7482 + swd->max_chain = max_chain;
7483 + r = find_match (c, swd, m_len, 1 + ahead);
7489 + op = STORE_RUN (c, op, ii, lit);
7491 + *op++ = M4_MARKER | 1;
7495 + c->codesize = op - out;
7497 + *out_len = op - out;
7500 + (*c->cb) (c->textsize, c->codesize);
7506 +lzo1x_999_compress_level (const lzo_byte * in, lzo_uint in_len,
7507 + lzo_byte * out, lzo_uintp out_len,
7509 + const lzo_byte * dict, lzo_uint dict_len,
7510 + lzo_progress_callback_t cb, int compression_level)
7512 + static const struct
7515 + lzo_uint good_length;
7516 + lzo_uint max_lazy;
7517 + lzo_uint nice_length;
7518 + lzo_uint max_chain;
7523 + 0, 0, 0, 8, 4, 0},
7525 + 0, 0, 0, 16, 8, 0},
7527 + 0, 0, 0, 32, 16, 0},
7529 + 1, 4, 4, 16, 16, 0},
7531 + 1, 8, 16, 32, 32, 0},
7533 + 1, 8, 16, 128, 128, 0},
7535 + 2, 8, 32, 128, 256, 0},
7537 + 2, 32, 128, F, 2048, 1},
7539 + 2, F, F, F, 4096, 1}
7542 + if (compression_level < 1 || compression_level > 9)
7543 + return LZO_E_ERROR;
7545 + compression_level -= 1;
7546 + return lzo1x_999_compress_internal (in, in_len, out, out_len, wrkmem,
7547 + dict, dict_len, cb,
7548 + c[compression_level].try_lazy,
7549 + c[compression_level].good_length,
7550 + c[compression_level].max_lazy,
7552 + c[compression_level].max_chain,
7553 + c[compression_level].flags);
7557 +lzo1x_999_compress (const lzo_byte * in, lzo_uint in_len,
7558 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7560 + return lzo1x_999_compress_level (in, in_len, out, out_len, wrkmem,
7566 +static const lzo_byte __lzo_copyright[] = LZO_VERSION_STRING;
7569 +_lzo1x_1_do_compress (const lzo_byte * in, lzo_uint in_len,
7570 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7573 + register const lzo_byte *ip;
7576 + const lzo_byte *const in_end = in + in_len;
7577 + const lzo_byte *const ip_end = in + in_len - 8 - 5;
7578 + const lzo_byte *ii;
7579 + lzo_dict_p const dict = (lzo_dict_p) wrkmem;
7588 + register const lzo_byte *m_pos;
7594 + DINDEX1 (dindex, ip);
7595 + GINDEX (m_pos, m_off, dict, dindex, in);
7596 + if (LZO_CHECK_MPOS_NON_DET
7597 + (m_pos, m_off, in, ip, M4_MAX_OFFSET))
7600 + if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
7602 + DINDEX2 (dindex, ip);
7603 + GINDEX (m_pos, m_off, dict, dindex, in);
7605 + if (LZO_CHECK_MPOS_NON_DET
7606 + (m_pos, m_off, in, ip, M4_MAX_OFFSET))
7608 + if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
7613 + if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
7618 + if (m_pos[2] == ip[2])
7628 + UPDATE_I (dict, 0, dindex, ip, in);
7635 + UPDATE_I (dict, 0, dindex, ip, in);
7637 + if (pd (ip, ii) > 0)
7639 + register lzo_uint t = pd (ip, ii);
7643 + op[-2] |= LZO_BYTE (t);
7646 + *op++ = LZO_BYTE (t - 3);
7649 + register lzo_uint tt = t - 18;
7657 + *op++ = LZO_BYTE (tt);;
7665 + if (m_pos[3] != *ip++ || m_pos[4] != *ip++
7666 + || m_pos[5] != *ip++ || m_pos[6] != *ip++
7667 + || m_pos[7] != *ip++ || m_pos[8] != *ip++)
7672 + if (m_off <= M2_MAX_OFFSET)
7676 + *op++ = LZO_BYTE (((m_len -
7677 + 1) << 5) | ((m_off & 7) <<
7679 + *op++ = LZO_BYTE (m_off >> 3);
7681 + else if (m_off <= M3_MAX_OFFSET)
7684 + *op++ = LZO_BYTE (M3_MARKER | (m_len - 2));
7685 + goto m3_m4_offset;
7692 + *op++ = LZO_BYTE (M4_MARKER |
7693 + ((m_off & 0x4000) >> 11) |
7695 + goto m3_m4_offset;
7701 + const lzo_byte *end = in_end;
7702 + const lzo_byte *m = m_pos + M2_MAX_LEN + 1;
7703 + while (ip < end && *m == *ip)
7705 + m_len = (ip - ii);
7709 + if (m_off <= M3_MAX_OFFSET)
7713 + *op++ = LZO_BYTE (M3_MARKER |
7718 + *op++ = M3_MARKER | 0;
7726 + if (m_len <= M4_MAX_LEN)
7727 + *op++ = LZO_BYTE (M4_MARKER |
7728 + ((m_off & 0x4000) >>
7729 + 11) | (m_len - 2));
7733 + m_len -= M4_MAX_LEN;
7734 + *op++ = LZO_BYTE (M4_MARKER |
7735 + ((m_off & 0x4000) >>
7738 + while (m_len > 255)
7744 + *op++ = LZO_BYTE (m_len);
7749 + *op++ = LZO_BYTE ((m_off & 63) << 2);
7750 + *op++ = LZO_BYTE (m_off >> 6);
7757 + *out_len = op - out;
7758 + return pd (in_end, ii);
7762 +lzo1x_1_compress (const lzo_byte * in, lzo_uint in_len,
7763 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7765 + lzo_byte *op = out;
7768 + if (in_len <= M2_MAX_LEN + 5)
7772 + t = _lzo1x_1_do_compress (in, in_len, op, out_len, wrkmem);
7778 + const lzo_byte *ii = in + in_len - t;
7780 + if (op == out && t <= 238)
7781 + *op++ = LZO_BYTE (17 + t);
7783 + op[-2] |= LZO_BYTE (t);
7785 + *op++ = LZO_BYTE (t - 3);
7788 + lzo_uint tt = t - 18;
7797 + *op++ = LZO_BYTE (tt);
7804 + *op++ = M4_MARKER | 1;
7808 + *out_len = op - out;
7813 +lzo1x_decompress (const lzo_byte * in, lzo_uint in_len,
7814 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7816 + register lzo_byte *op;
7817 + register const lzo_byte *ip;
7818 + register lzo_uint t;
7820 + register const lzo_byte *m_pos;
7822 + const lzo_byte *const ip_end = in + in_len;
7823 + lzo_byte *const op_end = out + *out_len;
7840 + goto first_literal_run;
7843 + while (TEST_IP && TEST_OP)
7861 + if (PTR_ALIGNED2_4 (op, ip))
7899 + first_literal_run:
7905 + m_pos = op - (1 + M2_MAX_OFFSET);
7907 + m_pos -= *ip++ << 2;
7908 + TEST_LOOKBEHIND (m_pos, out);
7916 + while (TEST_IP && TEST_OP)
7922 + m_pos -= (t >> 2) & 7;
7923 + m_pos -= *ip++ << 3;
7925 + TEST_LOOKBEHIND (m_pos, out);
7926 + NEED_OP (t + 3 - 1);
7946 + m_pos -= (ip[0] >> 2) + (ip[1] << 6);
7953 + m_pos -= (t & 8) << 11;
7968 + m_pos -= (ip[0] >> 2) + (ip[1] << 6);
7980 + m_pos -= *ip++ << 2;
7981 + TEST_LOOKBEHIND (m_pos, out);
7989 + TEST_LOOKBEHIND (m_pos, out);
7990 + NEED_OP (t + 3 - 1);
7991 + if (t >= 2 * 4 - (3 - 1)
7992 + && PTR_ALIGNED2_4 (op, m_pos))
7994 + COPY4 (op, m_pos);
8000 + COPY4 (op, m_pos);
8037 + *out_len = op - out;
8038 + return LZO_E_EOF_NOT_FOUND;
8041 + *out_len = op - out;
8042 + return (ip == ip_end ? LZO_E_OK :
8044 + ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
8047 + *out_len = op - out;
8048 + return LZO_E_INPUT_OVERRUN;
8051 + *out_len = op - out;
8052 + return LZO_E_OUTPUT_OVERRUN;
8054 + lookbehind_overrun:
8055 + *out_len = op - out;
8056 + return LZO_E_LOOKBEHIND_OVERRUN;
8061 +#define NO_LIT LZO_UINT_MAX
8064 +copy2 (lzo_byte * ip, const lzo_byte * m_pos, lzo_ptrdiff_t off)
8074 +copy3 (lzo_byte * ip, const lzo_byte * m_pos, lzo_ptrdiff_t off)
8079 + ip[2] = ip[1] = m_pos[0];
8081 + else if (off == 2)
8094 +lzo1x_optimize (lzo_byte * in, lzo_uint in_len,
8095 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
8097 + register lzo_byte *op;
8098 + register lzo_byte *ip;
8099 + register lzo_uint t;
8100 + register lzo_byte *m_pos;
8102 + const lzo_byte *const ip_end = in + in_len;
8103 + const lzo_byte *const op_end = out + *out_len;
8104 + lzo_byte *litp = NULL;
8106 + lzo_uint next_lit = NO_LIT;
8107 + long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
8119 + goto first_literal_run;
8122 + while (TEST_IP && TEST_OP)
8140 + first_literal_run:
8149 + m_pos = op - 1 - 0x800;
8151 + m_pos -= *ip++ << 2;
8158 + while (TEST_IP && TEST_OP)
8164 + m_pos -= *ip++ << 2;
8170 + if (nl == 0 && lit == 1 && ip[0] >= 16)
8174 + *litp = LZO_BYTE ((*litp & ~3) | lit);
8175 + copy2 (ip - 2, m_pos, op - m_pos);
8178 + else if (nl == 0 && ip[0] < 16 && ip[0] != 0
8179 + && (lit + 2 + ip[0] < 16))
8183 + copy2 (ip - 3 + 1, m_pos, op - m_pos);
8186 + memmove (litp + 1, litp, lit);
8188 + *litp = LZO_BYTE (lit - 3);
8193 + goto copy_literal_run;
8205 + m_pos -= (t >> 2) & 7;
8206 + m_pos -= *ip++ << 3;
8212 + if (t == 1 && lit > 3 && nl == 0 &&
8213 + ip[0] < 16 && ip[0] != 0
8214 + && (lit + 3 + ip[0] < 16))
8217 + copy3 (ip - 1 - 2, m_pos,
8220 + *litp = LZO_BYTE (lit - 3);
8225 + goto copy_literal_run;
8242 + m_pos -= *ip++ >> 2;
8243 + m_pos -= *ip++ << 6;
8248 + m_pos -= (t & 8) << 11;
8258 + m_pos -= *ip++ >> 2;
8259 + m_pos -= *ip++ << 6;
8268 + if (t == 1 && lit == 0 && nl == 0
8273 + *litp = LZO_BYTE ((*litp & ~3)
8275 + copy3 (ip - 3, m_pos,
8279 + else if (t == 1 && lit <= 3 && nl == 0
8280 + && ip[0] < 16 && ip[0] != 0
8281 + && (lit + 3 + ip[0] < 16))
8285 + copy3 (ip - 4 + 1, m_pos,
8289 + memmove (litp + 1,
8292 + *litp = LZO_BYTE (lit - 3);
8298 + goto copy_literal_run;
8310 + if (next_lit == NO_LIT)
8318 + next_lit = NO_LIT;
8329 + *out_len = op - out;
8330 + return LZO_E_EOF_NOT_FOUND;
8333 + *out_len = op - out;
8334 + return (ip == ip_end ? LZO_E_OK :
8336 + ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
8339 +/* interface to jffs2 bbc follows */
8341 +#include "jffs2_bbc_framework.h"
8343 +#define BLOCKSIZE 4096
8344 +#define OUTBLOCKSIZE (BLOCKSIZE + BLOCKSIZE / 64 + 16 + 3)
8346 +#define JFFS2_BBC_LZO_BLOCK_SIGN {0x3f, 0x47, 0x5a, 0x18}
8349 +jffs2_bbc_lzo_compressor_init (void);
8352 +jffs2_bbc_lzo_compressor_deinit (void);
8355 +jffs2_bbc_lzo_compress (void *model, unsigned char *input,
8356 + unsigned char *output, unsigned long *sourcelen,
8357 + unsigned long *dstlen);
8360 +jffs2_bbc_lzo_estimate (void *model, unsigned char *input,
8361 + unsigned long sourcelen, unsigned long *dstlen,
8362 + unsigned long *readtime, unsigned long *writetime);
8365 +jffs2_bbc_lzo_decompress (void *model, unsigned char *input,
8366 + unsigned char *output, unsigned long sourcelen,
8367 + unsigned long dstlen);
8370 +jffs2_bbc_lzo_proc_info (void);
8373 +jffs2_bbc_lzo_proc_command (char *command);
8375 +struct jffs2_bbc_compressor_type jffs2_bbc_lzo = {
8378 + JFFS2_BBC_LZO_BLOCK_SIGN,
8379 + jffs2_bbc_lzo_compressor_init,
8382 + jffs2_bbc_lzo_compressor_deinit,
8383 + jffs2_bbc_lzo_compress,
8384 + jffs2_bbc_lzo_estimate,
8385 + jffs2_bbc_lzo_decompress,
8386 + jffs2_bbc_lzo_proc_info,
8387 + jffs2_bbc_lzo_proc_command
8391 +no_lzo1x_optimize (lzo_byte * src, lzo_uint src_len,
8392 + lzo_byte * dst, lzo_uintp dst_len, lzo_voidp wrkmem)
8398 +static lzo_compress_t lzo1x_compressor = lzo1x_1_compress;
8399 +static lzo_optimize_t lzo1x_optimizer = no_lzo1x_optimize;
8400 +static int lzo1x_compressor_type = 1;
8401 +static int lzo1x_optimize_type = 0;
8402 +static unsigned long lzo1x_compressor_memsize = LZO1X_1_MEM_COMPRESS;
8404 +static lzo_compress_t lzo1x_compressor = lzo1x_999_compress;
8405 +static lzo_optimize_t lzo1x_optimizer = lzo1x_optimize;
8406 +static int lzo1x_compressor_type = 999;
8407 +static int lzo1x_optimize_type = 1;
8408 +static unsigned long lzo1x_compressor_memsize = LZO1X_999_MEM_COMPRESS;
8411 +static lzo_bytep wrkmem = NULL; /* temporary buffer for compression, used by lzo */
8412 +static lzo_bytep cmprssmem = NULL; /* temporary buffer for compression, used by interface */
8415 +jffs2_bbc_lzo_compressor_init (void)
8417 + wrkmem = (lzo_bytep) jffs2_bbc_malloc (lzo1x_compressor_memsize);
8418 + cmprssmem = (lzo_bytep) jffs2_bbc_malloc (OUTBLOCKSIZE);
8419 + return !(wrkmem && cmprssmem);
8423 +jffs2_bbc_lzo_compressor_deinit (void)
8425 + jffs2_bbc_free (wrkmem);
8426 + jffs2_bbc_free (cmprssmem);
8430 +jffs2_bbc_lzo_compress (void *model, unsigned char *input,
8431 + unsigned char *output, unsigned long *sourcelen,
8432 + unsigned long *dstlen)
8434 + lzo_uint csize = OUTBLOCKSIZE;
8435 + lzo_uint isize = *sourcelen;
8438 + lzo1x_compressor (input, *sourcelen, cmprssmem, &csize,
8439 + wrkmem)) != LZO_E_OK)
8441 + *sourcelen = *dstlen = 0;
8446 + retval = lzo1x_optimizer (cmprssmem, csize, input, &isize,
8449 + if (csize <= *dstlen) {
8451 + *(output++) = jffs2_bbc_lzo.block_sign[0];
8452 + *(output++) = jffs2_bbc_lzo.block_sign[1];
8453 + memcpy (output, cmprssmem, csize - 2);
8456 + *sourcelen = *dstlen = 0;
8463 +jffs2_bbc_lzo_estimate (void *model, unsigned char *input,
8464 + unsigned long sourcelen, unsigned long *dstlen,
8465 + unsigned long *readtime, unsigned long *writetime)
8467 + *dstlen = sourcelen * 55 / 100;
8468 + *readtime = JFFS2_BBC_ZLIB_READ_TIME / 2;
8469 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 8 / 10; /* LZO1X-1 is much-much faster,
8470 + but LZO1X-999 is slow. The default mode for inside kernel compression is LZO1X-1
8471 + This should be *0.4 really */
8476 +jffs2_bbc_lzo_decompress (void *model, unsigned char *input,
8477 + unsigned char *output, unsigned long sourcelen,
8478 + unsigned long dstlen)
8480 + lzo_uint outlen = dstlen;
8481 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzo.block_sign[0] ) ||
8482 + ( *(input++) != (unsigned char)jffs2_bbc_lzo.block_sign[1] )
8486 + return lzo1x_decompress (input, sourcelen - 2, output, &outlen, NULL);
8491 +jffs2_bbc_lzo_proc_info (void)
8493 + if (lzo1x_compressor_type == 1)
8495 + if (lzo1x_optimize_type == 1)
8497 + return "LZO1X-1 compression with optimization";
8501 + return "LZO1X-1 compression without optimization";
8504 + else if (lzo1x_compressor_type == 999)
8506 + if (lzo1x_optimize_type == 1)
8508 + return "LZO1X-999 compression with optimization";
8512 + return "LZO1X-999 compression without optimization";
8517 + return "Unknown configuration!";
8522 +jffs2_bbc_lzo_proc_command (char *command)
8527 + /* switch optimization off */
8528 + lzo1x_optimizer = no_lzo1x_optimize;
8529 + lzo1x_optimize_type = 0;
8530 + jffs2_bbc_print1 ("Compression optimization switched off.\n");
8533 + /* switch optimization on */
8534 + lzo1x_optimizer = lzo1x_optimize;
8535 + lzo1x_optimize_type = 1;
8536 + jffs2_bbc_print1 ("Compression optimization switched on.\n");
8539 + /* switch compression to LZO1X-1 */
8540 + jffs2_bbc_free (wrkmem);
8541 + lzo1x_compressor_type = 1;
8542 + lzo1x_compressor = lzo1x_1_compress;
8543 + lzo1x_compressor_memsize = LZO1X_1_MEM_COMPRESS;
8544 + wrkmem = (lzo_bytep)
8545 + jffs2_bbc_malloc (lzo1x_compressor_memsize);
8546 + jffs2_bbc_print1 ("Compression type switched to LZO1X-1.\n");
8549 + /* switch compression to LZO1X-999 */
8550 + jffs2_bbc_free (wrkmem);
8551 + lzo1x_compressor_type = 999;
8552 + lzo1x_compressor = lzo1x_999_compress;
8553 + lzo1x_compressor_memsize = LZO1X_999_MEM_COMPRESS;
8554 + wrkmem = (lzo_bytep)
8555 + jffs2_bbc_malloc (lzo1x_compressor_memsize);
8557 + ("Compression type switched to LZO1X-999.\n");
8560 + jffs2_bbc_print1 ("Unknown command!\n");
8566 +struct jffs2_bbc_compressor_type *
8567 +jffs2_bbc_lzo_init (int mode)
8569 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzo) == 0)
8571 + return &jffs2_bbc_lzo;
8580 +jffs2_bbc_lzo_deinit (void)
8582 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzo);
8584 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzss_comp.c
8585 ===================================================================
8587 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzss_comp.c
8589 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
8592 + jffs2_bbc_lzss_comp.c -- Lempel-Ziv-Storer-Szymanski compression module for jffs2
8593 + Copyright (C) 2004 Patrik Kluba
8594 + Based on the LZSS source included in LDS (lossless datacompression sources)
8595 + Block-compression modifications by Patrik Kluba
8596 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
8600 +Original copyright follows:
8602 +**************************************************************
8603 + LZSS.C -- A Data Compression Program
8604 +**************************************************************
8605 + 4/6/1989 Haruhiko Okumura
8606 + Use, distribute, and modify this program freely.
8607 + Please send me your improved versions.
8609 + NIFTY-Serve PAF01022
8610 + CompuServe 74050,1022
8611 +**************************************************************
8617 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
8624 +#define N 4096 /* size of ring buffer */
8625 +#define F 18 /* upper limit for match_length */
8626 +#define THRESHOLD 2 /* encode string into position and length
8627 + if match_length is greater than this */
8628 +#define NIL N /* index for root of binary search trees */
8630 +static unsigned char
8631 + text_buf[N + F - 1]; /* ring buffer of size N,
8632 + with extra F-1 bytes to facilitate string comparison */
8633 +static unsigned long match_position, match_length; /* of longest match. These are
8634 + set by the InsertNode() procedure. */
8635 +static unsigned long lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
8636 + parents -- These constitute binary search trees. */
8638 +static void InitTree(void) /* initialize trees */
8642 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
8643 + left children of node i. These nodes need not be initialized.
8644 + Also, dad[i] is the parent of node i. These are initialized to
8645 + NIL (= N), which stands for 'not used.'
8646 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
8647 + for strings that begin with character i. These are initialized
8648 + to NIL. Note there are 256 trees. */
8650 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
8651 + for (i = 0; i < N; i++) dad[i] = NIL;
8654 +static void InsertNode(unsigned long r)
8655 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
8656 + trees (text_buf[r]'th tree) and returns the longest-match position
8657 + and length via the global variables match_position and match_length.
8658 + If match_length = F, then removes the old node in favor of the new
8659 + one, because the old one will be deleted sooner.
8660 + Note r plays double role, as tree node and position in buffer. */
8662 + unsigned long i, p;
8663 + unsigned char *key;
8666 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
8667 + rson[r] = lson[r] = NIL; match_length = 0;
8670 + if (rson[p] != NIL) p = rson[p];
8671 + else { rson[p] = r; dad[r] = p; return; }
8673 + if (lson[p] != NIL) p = lson[p];
8674 + else { lson[p] = r; dad[r] = p; return; }
8676 + for (i = 1; i < F; i++)
8677 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
8678 + if (i > match_length) {
8679 + match_position = p;
8680 + if ((match_length = i) >= F) break;
8683 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
8684 + dad[lson[p]] = r; dad[rson[p]] = r;
8685 + if (rson[dad[p]] == p) rson[dad[p]] = r;
8686 + else lson[dad[p]] = r;
8687 + dad[p] = NIL; /* remove p */
8690 +static void DeleteNode(unsigned long p) /* deletes node p from tree */
8694 + if (dad[p] == NIL) return; /* not in tree */
8695 + if (rson[p] == NIL) q = lson[p];
8696 + else if (lson[p] == NIL) q = rson[p];
8699 + if (rson[q] != NIL) {
8700 + do { q = rson[q]; } while (rson[q] != NIL);
8701 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
8702 + lson[q] = lson[p]; dad[lson[p]] = q;
8704 + rson[q] = rson[p]; dad[rson[p]] = q;
8707 + if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
8711 +/* modified for block compression */
8712 +/* on return, srclen will contain the number of successfully compressed bytes
8713 + and dstlen will contain completed compressed bytes */
8715 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
8716 + unsigned long *dstlen)
8718 + unsigned long i, len, r, c, s, last_match_length, code_buf_ptr;
8719 + unsigned char code_buf[17], mask;
8720 + unsigned char *ip, *op;
8721 + unsigned long written = 0;
8722 + unsigned long read = 0;
8723 + unsigned char *srcend = srcbuf + *srclen;
8724 + unsigned char *dstend = dstbuf + *dstlen;
8727 + InitTree(); /* initialize trees */
8728 + code_buf[0] = 0; /* code_buf[1..16] saves eight units of code, and
8729 + code_buf[0] works as eight flags, "1" representing that the unit
8730 + is an unencoded letter (1 byte), "0" a position-and-length pair
8731 + (2 bytes). Thus, eight units require at most 16 bytes of code. */
8732 + code_buf_ptr = mask = 1;
8734 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
8735 + any character that will appear often. */
8736 + for (len = 0; (len < F) && (ip < srcend); len++)
8737 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
8740 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
8741 + each of which begins with one or more 'space' characters. Note
8742 + the order in which these strings are inserted. This way,
8743 + degenerate trees will be less likely to occur. */
8744 + InsertNode(r); /* Finally, insert the whole string just read. The
8745 + global variables match_length and match_position are set. */
8747 + if (match_length > len) match_length = len; /* match_length
8748 + may be spuriously long near the end of text. */
8749 + if (match_length <= THRESHOLD) {
8750 + match_length = 1; /* Not long enough match. Send one byte. */
8751 + code_buf[0] |= mask; /* 'send one byte' flag */
8752 + code_buf[code_buf_ptr++] = text_buf[r]; /* Send uncoded. */
8754 + code_buf[code_buf_ptr++] = match_position;
8755 + code_buf[code_buf_ptr++] = (((match_position >> 4) & 0xf0)
8756 + | (match_length - (THRESHOLD + 1))); /* Send position and
8757 + length pair. Note match_length > THRESHOLD. */
8759 + if ((mask <<= 1) == 0) { /* Shift mask left one bit. */
8760 + if ((op + code_buf_ptr) > dstend) {
8761 + *dstlen = written; /* written contains bytes of complete compressed
8765 + for (i = 0; i < code_buf_ptr; *(op++) = code_buf[i++]); /* Send at most 8 units of */
8766 + /* code together */
8767 + written += code_buf_ptr;
8768 + *srclen = read; /* this many bytes have been successfully compressed */
8769 + code_buf[0] = 0; code_buf_ptr = mask = 1;
8771 + last_match_length = match_length;
8772 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
8774 + DeleteNode(s); /* Delete old strings and */
8775 + text_buf[s] = c; /* read new bytes */
8776 + if (s < F - 1) text_buf[s + N] = c; /* If the position is
8777 + near the end of buffer, extend the buffer to make
8778 + string comparison easier. */
8779 + s = (s + 1) & (N - 1); r = (r + 1) & (N - 1);
8780 + /* Since this is a ring buffer, increment the position
8782 + InsertNode(r); /* Register the string in text_buf[r..r+F-1] */
8785 + while (i++ < last_match_length) { /* After the end of text, */
8786 + DeleteNode(s); /* no need to read, but */
8787 + s = (s + 1) & (N - 1); r = (r + 1) & (N - 1);
8788 + if (--len) InsertNode(r); /* buffer may not be empty. */
8790 + } while (len > 0); /* until length of string to be processed is zero */
8791 + if (code_buf_ptr > 1) { /* Send remaining code. */
8792 + if ((op + code_buf_ptr) > dstend) {
8793 + *dstlen = written;
8796 + for (i = 0; i < code_buf_ptr; *(op++) = code_buf[i++]);
8797 + written += code_buf_ptr;
8800 + *dstlen = written;
8804 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
8805 + unsigned long dstlen) /* Just the reverse of Encode(). */
8807 + unsigned long i, r, c, j, k, flags;
8808 + unsigned char *ip, *op;
8809 + unsigned long written;
8810 + unsigned long read;
8811 + unsigned char *srcend = srcbuf + srclen;
8812 + unsigned char *dstend = dstbuf + dstlen;
8813 + read = written = 0;
8816 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
8817 + r = N - F; flags = 0;
8819 + if (((flags >>= 1) & 256) == 0) {
8820 + if (ip >= srcend) return 0;
8822 + flags = c | 0xff00; /* uses higher byte cleverly */
8823 + } /* to count eight */
8825 + if (ip >= srcend) return 0;
8827 + if (op >= dstend) return -1;
8828 + *(op++) = text_buf[r++] = c; r &= (N - 1);
8830 + if ((ip + 2) > srcend) return 0;
8833 + i |= ((j & 0xf0) << 4); j = (j & 0x0f) + THRESHOLD;
8834 + if ((op + j + 1) > dstend) return -1;
8835 + for (k = 0; k <= j; k++) {
8836 + c = text_buf[(i + k) & (N - 1)];
8837 + *(op++) = text_buf[r++] = c; r &= (N - 1);
8843 +/* interface to jffs2 bbc follows */
8845 +#include "jffs2_bbc_framework.h"
8848 +#define JFFS2_BBC_LZSS_BLOCK_SIGN {0x27, 0x6f, 0x12, 0xc4}
8851 +jffs2_bbc_lzss_compressor_init (void);
8854 +jffs2_bbc_lzss_compressor_deinit (void);
8857 +jffs2_bbc_lzss_compress (void *model, unsigned char *input,
8858 + unsigned char *output, unsigned long *sourcelen,
8859 + unsigned long *dstlen);
8862 +jffs2_bbc_lzss_estimate (void *model, unsigned char *input,
8863 + unsigned long sourcelen, unsigned long *dstlen,
8864 + unsigned long *readtime, unsigned long *writetime);
8867 +jffs2_bbc_lzss_decompress (void *model, unsigned char *input,
8868 + unsigned char *output, unsigned long sourcelen,
8869 + unsigned long dstlen);
8872 +jffs2_bbc_lzss_proc_info (void);
8875 +jffs2_bbc_lzss_proc_command (char *command);
8877 +struct jffs2_bbc_compressor_type jffs2_bbc_lzss = {
8880 + JFFS2_BBC_LZSS_BLOCK_SIGN,
8881 + jffs2_bbc_lzss_compressor_init,
8884 + jffs2_bbc_lzss_compressor_deinit,
8885 + jffs2_bbc_lzss_compress,
8886 + jffs2_bbc_lzss_estimate,
8887 + jffs2_bbc_lzss_decompress,
8888 + jffs2_bbc_lzss_proc_info,
8889 + jffs2_bbc_lzss_proc_command
8893 +jffs2_bbc_lzss_compressor_init (void)
8899 +jffs2_bbc_lzss_compressor_deinit (void)
8904 +jffs2_bbc_lzss_compress (void *model, unsigned char *input,
8905 + unsigned char *output, unsigned long *sourcelen,
8906 + unsigned long *dstlen)
8909 + unsigned long dst = *dstlen;
8910 + *(output++) = jffs2_bbc_lzss.block_sign[0];
8911 + *(output++) = jffs2_bbc_lzss.block_sign[1];
8913 + retval = Encode(input, output, sourcelen, &dst);
8920 +jffs2_bbc_lzss_estimate (void *model, unsigned char *input,
8921 + unsigned long sourcelen, unsigned long *dstlen,
8922 + unsigned long *readtime, unsigned long *writetime)
8924 + *dstlen = sourcelen * 60 / 100;
8925 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 12 / 10;
8926 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 3;
8931 +jffs2_bbc_lzss_decompress (void *model, unsigned char *input,
8932 + unsigned char *output, unsigned long sourcelen,
8933 + unsigned long dstlen)
8935 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzss.block_sign[0] ) ||
8936 + ( *(input++) != (unsigned char)jffs2_bbc_lzss.block_sign[1] )
8940 + return Decode(input, output, sourcelen - 2, dstlen);
8945 +jffs2_bbc_lzss_proc_info (void)
8947 + return "Lempel-Ziv-Storer-Szymanski compression module";
8951 +jffs2_bbc_lzss_proc_command (char *command)
8956 +struct jffs2_bbc_compressor_type *
8957 +jffs2_bbc_lzss_init (int mode)
8959 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzss) == 0)
8961 + return &jffs2_bbc_lzss;
8970 +jffs2_bbc_lzss_deinit (void)
8972 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzss);
8974 Index: linux-2.4.35.4/fs/jffs2/linux-2.4.25.hpatch
8975 ===================================================================
8977 +++ linux-2.4.35.4/fs/jffs2/linux-2.4.25.hpatch
8986 ++ $(JFFS2_BBC_KERNEL_OBJS) \
8990 +-tristate 'Compressed ROM file system support' CONFIG_CRAMFS
8994 +F../../Documentation/Configure.help
8996 +-JFFS stats available
8997 +iConfigure.help.bbc.inc
9001 +=(de)compress->(de)compress2
9002 +-int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
9003 ++int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out,
9004 +-void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
9005 ++void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out,
9006 +?inflateEnd(&strm);
9009 ++extern int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 * sourcelen, __u32 * dstlen);
9010 ++extern void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
9012 ++int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
9013 ++ __u32 *sourcelen, __u32 *dstlen)
9015 ++ return jffs2_zlib_compress(data_in,cpage_out,sourcelen,dstlen);
9018 ++void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
9019 ++ __u32 srclen, __u32 destlen)
9021 ++ jffs2_zlib_decompress(data_in,cpage_out,srclen,destlen);
9026 +=set_act_sb before write
9028 ++#include "jffs2_bbc_framework.h" /**BBC**/
9030 +?int jffs2_commit_write
9032 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9036 +=set_act_sb before write
9038 ++#include "jffs2_bbc_framework.h" /**BBC**/
9040 +?int jffs2_garbage_collect_dnode(
9042 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9046 +=set_act_sb before read
9048 ++#include "jffs2_bbc_framework.h" /**BBC**/
9050 +?int jffs2_read_dnode(
9052 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9058 ++#include "jffs2_bbc_fs.h" /**BBC**/
9060 +?struct super_block *jffs2_read_super(
9062 ++ jffs2_bbc_load_model(sb); /**BBC**/
9064 +?void jffs2_put_super
9066 ++ jffs2_bbc_unload_model(sb); /**BBC**/
9067 +?init_jffs2_fs(void)
9070 ++ jffs2_bbc_proc_init(); /**BBC**/
9072 +?exit_jffs2_fs(void)
9074 ++ jffs2_bbc_proc_deinit(); /**BBC**/
9076 Index: linux-2.4.35.4/fs/jffs2/read.c
9077 ===================================================================
9078 --- linux-2.4.35.4.orig/fs/jffs2/read.c
9079 +++ linux-2.4.35.4/fs/jffs2/read.c
9084 +#include "jffs2_bbc_framework.h" /**BBC**/
9085 #include <linux/kernel.h>
9086 #include <linux/slab.h>
9087 #include <linux/jffs2.h>
9088 @@ -140,6 +141,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
9089 D2(printk(KERN_DEBUG "Data CRC matches calculated CRC %08x\n", crc));
9090 if (ri->compr != JFFS2_COMPR_NONE) {
9091 D2(printk(KERN_DEBUG "Decompress %d bytes from %p to %d bytes at %p\n", ri->csize, readbuf, ri->dsize, decomprbuf));
9092 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
9093 ret = jffs2_decompress(ri->compr, readbuf, decomprbuf, ri->csize, ri->dsize);
9095 printk(KERN_WARNING "Error: jffs2_decompress returned %d\n", ret);
9096 Index: linux-2.4.35.4/fs/jffs2/super.c
9097 ===================================================================
9098 --- linux-2.4.35.4.orig/fs/jffs2/super.c
9099 +++ linux-2.4.35.4/fs/jffs2/super.c
9104 +#include "jffs2_bbc_fs.h" /**BBC**/
9105 #include <linux/config.h>
9106 #include <linux/kernel.h>
9107 #include <linux/module.h>
9108 @@ -272,6 +273,7 @@ static struct super_block *jffs2_read_su
9109 sb->s_magic = JFFS2_SUPER_MAGIC;
9110 if (!(sb->s_flags & MS_RDONLY))
9111 jffs2_start_garbage_collect_thread(c);
9112 + jffs2_bbc_load_model(sb); /**BBC**/
9116 @@ -288,6 +290,7 @@ static struct super_block *jffs2_read_su
9117 void jffs2_put_super (struct super_block *sb)
9119 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
9120 + jffs2_bbc_unload_model(sb); /**BBC**/
9122 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
9124 @@ -344,6 +347,9 @@ static int __init init_jffs2_fs(void)
9128 + jffs2_bbc_proc_init(); /**BBC**/
9131 printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
9133 #ifdef JFFS2_OUT_OF_KERNEL
9134 @@ -388,6 +394,8 @@ static int __init init_jffs2_fs(void)
9136 static void __exit exit_jffs2_fs(void)
9138 + jffs2_bbc_proc_deinit(); /**BBC**/
9140 jffs2_destroy_slab_caches();
9142 unregister_filesystem(&jffs2_fs_type);