1 diff -Nur linux-mips-cvs/Documentation/Configure.help linux-mips/Documentation/Configure.help
2 --- linux-mips-cvs/Documentation/Configure.help 2005-01-20 03:19:21.000000000 +0100
3 +++ linux-mips/Documentation/Configure.help 2005-02-07 05:08:35.000000000 +0100
4 @@ -17528,6 +17528,32 @@
5 If reporting bugs, please try to have available a full dump of the
6 messages at debug level 1 while the misbehaviour was occurring.
8 +ARMLIB compression support for BBC (EXPERIMENTAL)
9 +CONFIG_JFFS2_BBC_ARMLIB
10 + This enables ARMLIB support for BBC.
12 +LZO1X-* compression support for BBC (EXPERIMENTAL)
14 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
15 + compressor, beats ZLIB in everything)
17 +LZARI compression support for BBC (EXPERIMENTAL)
18 +CONFIG_JFFS2_BBC_LZARI
19 + This enables LempelZiv-Storer-Szymanski compression for BBC with
20 + additional arithmetic coding (damn slow, but best compresor).
22 +LZHD compression support for BBC (EXPERIMENTAL)
23 +CONFIG_JFFS2_BBC_LZHD
24 + This enables LempelZiv-Storer-Szymanski compression for BBC with
25 + additional dynamic Huffman coding (a little faster than LZARI, and
26 + it's compression ratio is a little worse than LZARI's)
28 +LZSS compression support for BBC (EXPERIMENTAL)
29 +CONFIG_JFFS2_BBC_LZSS
30 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
31 + (faster than LZHD, and, and has a not-so-good compression ratio,
32 + was included just for testing)
34 JFFS stats available in /proc filesystem
36 Enabling this option will cause statistics from mounted JFFS file systems
37 diff -Nur linux-mips-cvs/fs/Config.in linux-mips/fs/Config.in
38 --- linux-mips-cvs/fs/Config.in 2004-11-19 01:28:47.000000000 +0100
39 +++ linux-mips/fs/Config.in 2005-02-07 05:08:34.000000000 +0100
41 if [ "$CONFIG_JFFS2_FS" = "y" -o "$CONFIG_JFFS2_FS" = "m" ] ; then
42 int 'JFFS2 debugging verbosity (0 = quiet, 2 = noisy)' CONFIG_JFFS2_FS_DEBUG 0
44 +dep_mbool ' ARMLIB compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_ARMLIB $CONFIG_JFFS2_FS
45 +dep_mbool ' LZO1X-* compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZO $CONFIG_JFFS2_FS
46 +dep_mbool ' LZARI compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZARI $CONFIG_JFFS2_FS
47 +dep_mbool ' LZHD compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZHD $CONFIG_JFFS2_FS
48 +dep_mbool ' LZSS compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZSS $CONFIG_JFFS2_FS
50 tristate 'Compressed ROM file system support' CONFIG_CRAMFS
51 bool 'Virtual memory file system support (former shm fs)' CONFIG_TMPFS
52 define_bool CONFIG_RAMFS y
53 diff -Nur linux-mips-cvs/fs/jffs2/Config.in.bbc.inc linux-mips/fs/jffs2/Config.in.bbc.inc
54 --- linux-mips-cvs/fs/jffs2/Config.in.bbc.inc 1970-01-01 01:00:00.000000000 +0100
55 +++ linux-mips/fs/jffs2/Config.in.bbc.inc 2005-02-07 05:08:34.000000000 +0100
57 +dep_mbool ' ARMLIB compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_ARMLIB $CONFIG_JFFS2_FS
58 +dep_mbool ' LZO1X-* compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZO $CONFIG_JFFS2_FS
59 +dep_mbool ' LZARI compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZARI $CONFIG_JFFS2_FS
60 +dep_mbool ' LZHD compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZHD $CONFIG_JFFS2_FS
61 +dep_mbool ' LZSS compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZSS $CONFIG_JFFS2_FS
62 diff -Nur linux-mips-cvs/fs/jffs2/Configure.help.bbc.inc linux-mips/fs/jffs2/Configure.help.bbc.inc
63 --- linux-mips-cvs/fs/jffs2/Configure.help.bbc.inc 1970-01-01 01:00:00.000000000 +0100
64 +++ linux-mips/fs/jffs2/Configure.help.bbc.inc 2005-02-07 05:08:34.000000000 +0100
66 +ARMLIB compression support for BBC (EXPERIMENTAL)
67 +CONFIG_JFFS2_BBC_ARMLIB
68 + This enables ARMLIB support for BBC.
70 +LZO1X-* compression support for BBC (EXPERIMENTAL)
72 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
73 + compressor, beats ZLIB in everything)
75 +LZARI compression support for BBC (EXPERIMENTAL)
76 +CONFIG_JFFS2_BBC_LZARI
77 + This enables LempelZiv-Storer-Szymanski compression for BBC with
78 + additional arithmetic coding (damn slow, but best compresor).
80 +LZHD compression support for BBC (EXPERIMENTAL)
81 +CONFIG_JFFS2_BBC_LZHD
82 + This enables LempelZiv-Storer-Szymanski compression for BBC with
83 + additional dynamic Huffman coding (a little faster than LZARI, and
84 + it's compression ratio is a little worse than LZARI's)
86 +LZSS compression support for BBC (EXPERIMENTAL)
87 +CONFIG_JFFS2_BBC_LZSS
88 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
89 + (faster than LZHD, and, and has a not-so-good compression ratio,
90 + was included just for testing)
91 diff -Nur linux-mips-cvs/fs/jffs2/Kconfig.bbc.inc linux-mips/fs/jffs2/Kconfig.bbc.inc
92 --- linux-mips-cvs/fs/jffs2/Kconfig.bbc.inc 1970-01-01 01:00:00.000000000 +0100
93 +++ linux-mips/fs/jffs2/Kconfig.bbc.inc 2005-02-07 05:08:34.000000000 +0100
95 +config JFFS2_BBC_ARMLIB
96 + bool "ARMLIB compression support for BBC (EXPERIMENTAL)"
97 + depends on JFFS2_FS && EXPERIMENTAL
100 + This enables ARMLIB support for BBC.
102 +config JFFS2_BBC_LZO
103 + bool "LZO1X-* compression support for BBC (EXPERIMENTAL)"
104 + depends on JFFS2_FS && EXPERIMENTAL
107 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
110 +config JFFS2_BBC_LZARI
111 + bool "LZARI compression support for BBC (EXPERIMENTAL)"
112 + depends on JFFS2_FS && EXPERIMENTAL
115 + This enables LempelZiv-Storer-Szymanski compression for BBC with
116 + additional arithmetic coding (damn slow, but best compresor).
118 +config JFFS2_BBC_LZHD
119 + bool "LZHD compression support for BBC (EXPERIMENTAL)"
120 + depends on JFFS2_FS && EXPERIMENTAL
123 + This enables LempelZiv-Storer-Szymanski compression for BBC with
124 + additional dynamic Huffman coding (a little faster than LZARI, and
125 + it's compression ratio is a little worse than LZARI's)
127 +config JFFS2_BBC_LZSS
128 + bool "LZSS compression support for BBC (EXPERIMENTAL)"
129 + depends on JFFS2_FS && EXPERIMENTAL
132 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
133 + (faster than LZHD, and, and has a not-so-good compression ratio,
134 + was included just for testing)
135 diff -Nur linux-mips-cvs/fs/jffs2/Makefile linux-mips/fs/jffs2/Makefile
136 --- linux-mips-cvs/fs/jffs2/Makefile 2003-08-13 19:19:25.000000000 +0200
137 +++ linux-mips/fs/jffs2/Makefile 2005-02-07 05:08:34.000000000 +0100
139 # Note 2! The CFLAGS definitions are now in the main makefile...
142 +JFFS2_BBC_KERNEL_OBJS-y = jffs2_bbc_framework.o jffs2_bbc_fs.o
144 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_ARMLIB) += jffs2_bbc_armlib_comp.o
145 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZO) += jffs2_bbc_lzo_comp.o
146 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZSS) += jffs2_bbc_lzss_comp.o
147 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZARI) += jffs2_bbc_lzari_comp.o
148 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZHD) += jffs2_bbc_lzhd_comp.o
150 +JFFS2_BBC_KERNEL_OBJS := $(JFFS2_BBC_KERNEL_OBJS-y)
152 +JFFS2_BBC_MKFS_OBJS = jffs2_bbc_mkfs.o jffs2_bbc_framework.o jffs2_bbc_armlib_comp.o jffs2_bbc_lzo_comp.o\
153 + jffs2_bbc_lzss_comp.o jffs2_bbc_lzari_comp.o jffs2_bbc_lzhd_comp.o
155 COMPR_OBJS := compr.o compr_rubin.o compr_rtime.o pushpull.o \
157 JFFS2_OBJS := dir.o file.o ioctl.o nodelist.o malloc.o \
158 + $(JFFS2_BBC_KERNEL_OBJS) \
159 read.o nodemgmt.o readinode.o super.o write.o scan.o gc.o \
160 symlink.o build.o erase.o background.o
162 diff -Nur linux-mips-cvs/fs/jffs2/Makefile.bbc.inc linux-mips/fs/jffs2/Makefile.bbc.inc
163 --- linux-mips-cvs/fs/jffs2/Makefile.bbc.inc 1970-01-01 01:00:00.000000000 +0100
164 +++ linux-mips/fs/jffs2/Makefile.bbc.inc 2005-02-07 05:08:34.000000000 +0100
166 +JFFS2_BBC_KERNEL_OBJS-y = jffs2_bbc_framework.o jffs2_bbc_fs.o
168 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_ARMLIB) += jffs2_bbc_armlib_comp.o
169 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZO) += jffs2_bbc_lzo_comp.o
170 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZSS) += jffs2_bbc_lzss_comp.o
171 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZARI) += jffs2_bbc_lzari_comp.o
172 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZHD) += jffs2_bbc_lzhd_comp.o
174 +JFFS2_BBC_KERNEL_OBJS := $(JFFS2_BBC_KERNEL_OBJS-y)
176 +JFFS2_BBC_MKFS_OBJS = jffs2_bbc_mkfs.o jffs2_bbc_framework.o jffs2_bbc_armlib_comp.o jffs2_bbc_lzo_comp.o\
177 + jffs2_bbc_lzss_comp.o jffs2_bbc_lzari_comp.o jffs2_bbc_lzhd_comp.o
178 diff -Nur linux-mips-cvs/fs/jffs2/compr_zlib.c linux-mips/fs/jffs2/compr_zlib.c
179 --- linux-mips-cvs/fs/jffs2/compr_zlib.c 2003-01-11 18:53:17.000000000 +0100
180 +++ linux-mips/fs/jffs2/compr_zlib.c 2005-02-07 05:08:35.000000000 +0100
182 vfree(inflate_workspace);
185 -int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
186 +int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out,
187 __u32 *sourcelen, __u32 *dstlen)
194 -void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
195 +void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out,
196 __u32 srclen, __u32 destlen)
200 zlib_inflateEnd(&strm);
204 +extern int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 * sourcelen, __u32 * dstlen);
205 +extern void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
207 +int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
208 + __u32 *sourcelen, __u32 *dstlen)
210 + return jffs2_zlib_compress(data_in,cpage_out,sourcelen,dstlen);
213 +void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
214 + __u32 srclen, __u32 destlen)
216 + jffs2_zlib_decompress(data_in,cpage_out,srclen,destlen);
219 diff -Nur linux-mips-cvs/fs/jffs2/file.c linux-mips/fs/jffs2/file.c
220 --- linux-mips-cvs/fs/jffs2/file.c 2003-11-17 02:07:44.000000000 +0100
221 +++ linux-mips/fs/jffs2/file.c 2005-02-07 05:08:35.000000000 +0100
226 +#include "jffs2_bbc_framework.h" /**BBC**/
227 #include <linux/kernel.h>
228 #include <linux/mtd/compatmac.h> /* for min() */
229 #include <linux/slab.h>
232 comprbuf = kmalloc(cdatalen, GFP_KERNEL);
234 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
235 comprtype = jffs2_compress(page_address(pg)+ (file_ofs & (PAGE_CACHE_SIZE-1)), comprbuf, &datalen, &cdatalen);
237 if (comprtype == JFFS2_COMPR_NONE) {
238 diff -Nur linux-mips-cvs/fs/jffs2/gc.c linux-mips/fs/jffs2/gc.c
239 --- linux-mips-cvs/fs/jffs2/gc.c 2003-11-17 02:07:44.000000000 +0100
240 +++ linux-mips/fs/jffs2/gc.c 2005-02-07 05:08:35.000000000 +0100
245 +#include "jffs2_bbc_framework.h" /**BBC**/
246 #include <linux/kernel.h>
247 #include <linux/mtd/mtd.h>
248 #include <linux/slab.h>
250 writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
253 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
254 comprtype = jffs2_compress(writebuf, comprbuf, &datalen, &cdatalen);
257 diff -Nur linux-mips-cvs/fs/jffs2/hpatch linux-mips/fs/jffs2/hpatch
258 --- linux-mips-cvs/fs/jffs2/hpatch 1970-01-01 01:00:00.000000000 +0100
259 +++ linux-mips/fs/jffs2/hpatch 2005-02-07 05:08:34.000000000 +0100
262 +# A patch-like utility
263 +# Designed for patching different version of jffs2 with the same hpatch file
265 +# Copyright (C) 2004, Ferenc Havasi
267 +# This program is free software; you can redistribute it and/or
268 +# modify it under the terms of the GNU General Public License
269 +# as published by the Free Software Foundation; either version 2
270 +# of the License, or (at your option) any later version.
272 +# This program is distributed in the hope that it will be useful,
273 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
274 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
275 +# GNU General Public License for more details.
277 +# You should have received a copy of the GNU General Public License
278 +# along with this program; if not, write to the Free Software
279 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
281 +$filename_tmp1="file1.tmp";
282 +$filename_tmp2="file2.tmp";
285 +$filename_out=$filename_tmp1;
292 +# 3: expecting F (first state)
298 + if (($mode!=2)&&($modified==1)) {
304 + if ($cmd_name ne "") { $rules{"$cmd_name"}=1; }
305 + $filename_result=$filename_out;
306 + if ($filename_result ne $filename_in_save) {
307 + open(RES,"<$filename_result") or die "Cannot open $filename_result.\n";
308 + open(DST,">$filename_in_save") or die "Cannot open $filename_in_save.\n";
315 + unlink($filename_tmp1) && unlink($filename_tmp2);
320 + $filename_result=$filename_in;
321 + if ($filename_result ne $filename_in_save) {
322 + open(RES,"<$filename_result") or die "Cannot open $filename_result.\n";
323 + open(DST,">$filename_in_save") or die "Cannot open $filename_in_save.\n";
330 + unlink($filename_tmp1);
333 + foreach $rulename (keys %rules) {
334 + if ($rules{"$rulename"}==0) { print(STDERR "On $filename_in_save error applying rule $rulename.\n"); }
340 + print ("usage: hpatch hpatch_file\n");
344 +open(CMD,"<$ARGV[0]") or die "Cannot open $ARGV[0].\n";
347 +while (chomp($cmd_line=<CMD>)) {
349 + if ($cmd_line eq "") {next;}
350 + #$cmd_line =~ s/\#.*//;
351 + $cmd_line =~ s/\ *$//;
352 + if ($cmd_line eq "") {next;}
353 + if ($cmd_line =~ /^F(.*)/) {
354 + $tmp_filename_in=$1;
358 + $filename_in=$tmp_filename_in;
359 + $filename_in_save=$filename_in;
360 + open(SRC,"<$filename_in") or die "Cannot open $filename_in.\n";
361 + open(DST,">$filename_out") or die "Cannot open $filename_out.\n";;
366 + if ($mode==3) {die "error: F expression expected in line $cmd_linenum\n";}
367 + if ($cmd_line =~ /^=(.*)/) {
369 + if (($mode!=2)&&($modified==1)) {
375 + if (($cmd_name ne "")) {$rules{"$cmd_name"}=1;};
377 + if ($filename_cnt%2==1) {
378 + $filename_in=$filename_tmp1;
379 + $filename_out=$filename_tmp2;
382 + $filename_in=$filename_tmp2;
383 + $filename_out=$filename_tmp1;
391 + $cmd_name=$tmp_cmd_name;
392 + if (($cmd_name ne "")) {
393 + if ($rules{"$cmd_name"}==1) {
397 + $rules{"$cmd_name"}=0;
400 + open(SRC,"<$filename_in") or die "Cannot open $filename_in.\n";
401 + open(DST,">$filename_out") or die "Cannot open $filename_out.\n";
405 + if ($mode == 0) {die "error: = expression expected in line $cmd_linenum\n";}
406 + if ($mode == 2) {next;}
407 + if ($cmd_line =~ /^!(.*)/) {
412 + if ($cmd_line =~ /^\?(.*)/) {
417 + if (index($_,$search_str)>=0) {$found=1; last;}
419 + if ($found==0) { $mode=2; }
422 + if ($cmd_line =~ /^\+(.*)/) {
427 + if ($cmd_line =~ /^\-(.*)/) {
431 + if (index($_,$search_str)>=0) {$saved_line=$_; $found=1; $modified=1; last;}
434 + if ($found==0) { $mode=2; }
437 + if ($cmd_line =~ /^i(.*)/) {
439 + open(INCSRC,"<$filename_inc") or die "Cannot open $filename_inc.\n";
445 + if ($cmd_line =~ /^I/) {
446 + print DST $saved_line;
452 diff -Nur linux-mips-cvs/fs/jffs2/jffs2_bbc_armlib_comp.c linux-mips/fs/jffs2/jffs2_bbc_armlib_comp.c
453 --- linux-mips-cvs/fs/jffs2/jffs2_bbc_armlib_comp.c 1970-01-01 01:00:00.000000000 +0100
454 +++ linux-mips/fs/jffs2/jffs2_bbc_armlib_comp.c 2005-02-07 05:08:34.000000000 +0100
457 + * JFFS2-BBC: armlib compressor plugin
461 + * Copyright (C) 2004, Ferenc Havasi & Tamas Gergely
463 + * This program is free software; you can redistribute it and/or
464 + * modify it under the terms of the GNU General Public License
465 + * as published by the Free Software Foundation; either version 2
466 + * of the License, or (at your option) any later version.
468 + * This program is distributed in the hope that it will be useful,
469 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
470 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
471 + * GNU General Public License for more details.
473 + * You should have received a copy of the GNU General Public License
474 + * along with this program; if not, write to the Free Software
475 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
479 +#include "jffs2_bbc_framework.h"
482 +#include <linux/string.h>
487 +//ORIGIN: include/DataStructures/TypeDefs.h
489 +/*******************************************************************************
491 +* AUTHOR: Tamás Gergely
493 +*******************************************************************************/
507 +#define u8 unsigned char
510 +#define u16 unsigned short
513 +#define u32 unsigned long
516 +#define s8 signed char
519 +#define s16 signed short
522 +#define s32 signed long
533 +#define VECTOR_P_END(vct) ((void*)(((char*)((vct)->ptr)) + (vct)->size))
534 +#define VECTOR_S_END(vct) ((void*)(((char*)((vct).ptr)) + (vct).size))
536 +static void vector_clear(vector *);
537 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
538 +static void vector_reset(vector *);
539 +static void vector_clr_ptr(vector *);
540 +static void vector_add_u8(vector *, u8);
541 +static void vector_add_u16(vector *, u16);
542 +static void vector_add_u32(vector *, u32);
543 +static void vector_add_s8(vector *, s8);
544 +static void vector_add_s16(vector *, s16);
545 +static void vector_add_s32(vector *, s32);
546 +static void vector_add_ptr(vector *, void *);
547 +static void vector_concat(vector *, vector *);
552 +//ORIGIN: include/DataStructures/DataTypes.h
554 +/*******************************************************************************
556 +* AUTHOR: Tamás Gergely
558 +*******************************************************************************/
563 +//#include "DataStructures/TypeDefs.h"
565 +typedef u16 THUMB_DataType;
566 +typedef u32 ARM_DataType;
567 +typedef u8 TokenType;
568 +typedef u8 PredictorType;
569 +typedef u8 *ProbDist;
571 +typedef vector RawData;
572 +typedef vector RawBlocks;
573 +typedef vector TokenStream;
574 +typedef vector TokenBlocks;
575 +typedef vector LatType;
577 +#define THUMB_DATA_LENGTH 16
578 +#define ARM_DATA_LENGTH 32
579 +#define TOKEN_LENGTH 8
580 +#define TOKEN_MAXVALUE 0xff
581 +#define PREDICTOR_LENGTH 8
582 +#define PREDICTOR_MAXVALUE 0xff
586 +//ORIGIN: include/DataStructures/BitVector.h
588 +/*******************************************************************************
590 +* AUTHOR: Tamás Gergely
592 +*******************************************************************************/
597 +//#include "DataStructures/TypeDefs.h"
599 +typedef vector BitBlocks;
612 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
613 +static void bitblocks_clear(BitBlocks *);
614 +static void bitvector_clear(BitVector *);
615 +static void bitvector_W_reset(BitVector *);
616 +static void bitvector_W_add0(BitVector *);
617 +static void bitvector_W_add1(BitVector *);
618 +static void bitvector_W_concat_b(BitVector *, BitVector *);
619 +static void bitvector_W_concat_v(BitVector *, vector *);
620 +static void bitvector_W_flush(BitVector *);
621 +static void bitvector_R_reset(BitVector *);
622 +static u8 bitvector_R_get1(BitVector *);
623 +static u8 bitvector_R_get8(BitVector *);
626 +#define BITVECTOR_P_END(bv) ((void*)(((bv)->base)+((bv)->size)))
627 +#define BITVECTOR_S_END(bv) ((void*)( ((bv).base)+ ((bv).size)))
628 +#define BITVECTOR_SKIP(bv,num) ((bv)->ptr) += (num)
632 +//ORIGIN: include/DataStructures/DecisionTree.h
634 +/*******************************************************************************
635 +* FILE: DecisionTree.h
636 +* AUTHOR: Tamás Gergely
638 +*******************************************************************************/
640 +#ifndef DECISIONTREE_H
641 +#define DECISIONTREE_H
643 +//#include "DataStructures/DataTypes.h"
647 +#define TREENODETYPE_NULLNODE 0
648 +#define TREENODETYPE_NODE_BINARY_EQ 1
649 +#define TREENODETYPE_LEAF_P 2
650 +#define TREENODETYPE_LEAF_C 3
651 +#define TREENODETYPE_NODE_BINARY_LT 5
652 +#define TREENODETYPE_IS_NODE(n) (((n) == TREENODETYPE_NODE_BINARY_EQ) || \
653 + ((n) == TREENODETYPE_NODE_BINARY_LT))
654 +#define TREENODETYPE_IS_NODE_BINARY(n) (((n) == TREENODETYPE_NODE_BINARY_EQ) || \
655 + ((n) == TREENODETYPE_NODE_BINARY_LT))
657 +#define TREENODETYPE_IS_LEAF(n) (((n) == TREENODETYPE_LEAF_P) || \
658 + ((n) == TREENODETYPE_LEAF_C))
661 +#define TREE_SUBTREE_RELATION_LEFT_EQ !=
662 +#define TREE_SUBTREE_RELATION_RIGHT_EQ ==
663 +#define TREE_SUBTREE_RELATION_LEFT_LT <
664 +#define TREE_SUBTREE_RELATION_RIGHT_LT >=
666 +#define GET_NODE_PTR_TYPE(n) (((TreeNodeDummy*)(n))->type)
675 + u8 type; // [TREENODETYPE_NODE_BINARY]
677 + PredictorType value;
684 + u8 type; // [TREENODETYPE_LEAF_P]
686 + PredictorType *probabilities;
691 + u8 type; // [TREENODETYPE_LEAF_C]
692 + PredictorType predicted_class;
706 + u16 number_of_classes;
707 + u16 number_of_predictors;
708 + PredictorType *predictor_max_values;
711 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
712 +static void decisiontree_delete(DecisionTree *);
713 +static void decisiontree_get_probability_for_token(void *, PredictorType *, TokenType, ProbabilityType *);
714 +static TokenType decisiontree_get_token_for_range(void *, PredictorType *, u32, u32, ProbabilityType *);
719 +//ORIGIN: include/DataStructures/PredictorTable.h
721 +/*******************************************************************************
722 +* FILE: PredictorTable.h
723 +* AUTHOR: Tamás Gergely
725 +*******************************************************************************/
727 +#ifndef PREDICTORTABLE_H
728 +#define PREDICTORTABLE_H
730 +//#include "DataStructures/TypeDefs.h"
731 +//#include "DataStructures/DataTypes.h"
732 +////#include "DataStructures/Filter.h"
733 +////#include "DataStructures/Converter.h"
734 +////#include "DataStructures/Manipulator.h"
736 +#define NUMBER_OF_PREDICTORS_ARM 17
739 +#define NUMBER_OF_PREDICTORS_TXT 2
742 +#endif // __KERNEL__
745 +#define NUMBER_OF_PREDICTORS NUMBER_OF_PREDICTORS_TXT
746 +#define predictortable_reset predictortable_resetTXT
747 +#define predictortable_update predictortable_updateTXT
748 +#define predictortable_minvalue predictortable_minvalueTXT
749 +#define predictortable_maxvalue predictortable_maxvalueTXT
751 +#define NUMBER_OF_PREDICTORS NUMBER_OF_PREDICTORS_ARM
752 +#define predictortable_reset predictortable_resetARM
753 +#define predictortable_update predictortable_updateARM
754 +#define predictortable_minvalue predictortable_minvalueARM
755 +#define predictortable_maxvalue predictortable_maxvalueARM
763 + PredictorType *predictors;
766 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
767 +static void predictortable_clear(PredictorTable *);
768 +static void predictortable_free(PredictorTable *);
769 +static void predictortable_resetARM(PredictorTable *);
770 +static void predictortable_updateARM(PredictorTable *, TokenType);
771 +static PredictorType predictortable_minvalueARM(PredictorTable *, u32);
772 +static PredictorType predictortable_maxvalueARM(PredictorTable *, u32);
777 +static void predictortable_resetTXT(PredictorTable *);
778 +static void predictortable_updateTXT(PredictorTable *, TokenType);
779 +static PredictorType predictortable_minvalueTXT(PredictorTable *, u32);
780 +static PredictorType predictortable_maxvalueTXT(PredictorTable *, u32);
782 +#endif // __KERNEL__
786 +//ORIGIN: include/DataStructures/ipack_model.h
788 +/*******************************************************************************
789 +* FILE: ipack_model.h
790 +* AUTHOR: Tamás Gergely
792 +*******************************************************************************/
794 +#ifndef IPACK_MODEL_H
795 +#define IPACK_MODEL_H
797 +//#include "DataStructures/DataTypes.h"
798 +//#include "DataStructures/DecisionTree.h"
799 +//#include "DataStructures/PredictorTable.h"
801 +#define PROBABILITY_SHIFT 12
802 +#define PROBABILITY_MAX 0x00001000l
804 +#define NUMBER_OF_TOKENS_ARM 16
805 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION_ARM 8
808 +#define NUMBER_OF_TOKENS_TXT 256
809 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION_TXT 4
812 +#endif // __KERNEL__
815 +#define NUMBER_OF_TOKENS NUMBER_OF_TOKENS_TXT
816 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION NUMBER_OF_TOKENS_PER_INSTRUCTION_TXT
818 +#define NUMBER_OF_TOKENS NUMBER_OF_TOKENS_ARM
819 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION NUMBER_OF_TOKENS_PER_INSTRUCTION_ARM
825 + Data structure of an internal node of the tree
829 + PredictorType *attribute_ptr;
830 + u32 value; // PredictorType
831 + void *right_child_ptr;
832 +} ipack_treenodeBin;
834 + Data structure of a leaf with probabilities
838 + u16 probabilities[0]; // PredictorType[0]
841 + Data structure of a leaf with class prediction
845 + PredictorType predicted_class; // PredictorType
848 + Possible data structures of a tree node
852 + ipack_treenodeBin nodeBin;
853 + ipack_treeleafP leafP;
854 + ipack_treeleafC leafC;
862 + ipack_node_data data; // ipack_node_data
870 + u16 probabilities[NUMBER_OF_TOKENS];
873 + Model for ipack project
877 + char ID[4]; // char[4]
878 + char block_sign[4]; // only the first 2 are used!
879 + void *tree_root_ptr; // void*
880 + void *tree_code; // generated ARM code
881 + PredictorType *predictors_ptr; // PredictorType*
882 + ipack_nullnode nullnode;
889 +} ipack_probability_type;
892 +static void ipack_model_get_probability_for_token(ipack_nodetype *, TokenType, ipack_probability_type *);
893 +static TokenType ipack_model_get_token_for_range(ipack_nodetype *, u32, u32, ipack_probability_type *);
894 +/*void ipack_model_predictortable_reset (PredictorType*);
895 +void ipack_model_predictortable_update (PredictorType*, TokenType);*/
898 +/*static void ipack_model_printinfo(ipack_model_type *);
899 +static void ipack_dumpmodel(void *);*/
904 +//ORIGIN: include/Builders/PredictorGenerator.h
906 +/*******************************************************************************
907 +* FILE: PredictorGenerator.h
908 +* AUTHOR: Tamás Gergely
910 +*******************************************************************************/
912 +#ifndef PREDICTORGENERATOR_H
913 +#define PREDICTORGENERATOR_H
915 +//#include "DataStructures.h"
917 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
918 +static PredictorTable *predictorgenerator_generate(void);
923 +//ORIGIN: include/Builders/Coder.h
925 +/*******************************************************************************
927 +* AUTHOR: Tamás Gergely
929 +*******************************************************************************/
934 +#define CODER_VALUEBITS 16
935 +#define CODER_VALUEMAX 0x00010000l
936 +#define CODER_VALUE3RD 0x0000c000l
937 +#define CODER_VALUEHLF 0x00008000l
938 +#define CODER_VALUE1ST 0x00004000l
942 +//ORIGIN: DataStructures/src/TypeDefs.c
944 +/*******************************************************************************
946 +* AUTHOR: Tamás Gergely
948 +*******************************************************************************/
950 +//#include "ipack_common.h"
951 +//#include "DataStructures/TypeDefs.h"
956 +#define VECTOR_ALLOC_SIZE 0x00001000
958 +static void vector_clear(vector * vct)
961 + jffs2_bbc_free(vct->ptr);
967 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
968 +static void vector_extend(vector * vct)
971 + vct->capacity += vct->alloc_size;
972 + tmp = jffs2_bbc_malloc(vct->capacity);
974 + memcpy(tmp, vct->ptr, vct->size);
975 + jffs2_bbc_free(vct->ptr);
980 +static void vector_reset(vector * vct)
984 + vct->alloc_size = VECTOR_ALLOC_SIZE;
988 +static void vector_clr_ptr(vector * vct)
992 + for (it = vct->ptr, end_it = (((char *) (vct->ptr)) + vct->size); it != end_it; it++) {
994 + jffs2_bbc_free(*it);
997 + jffs2_bbc_free(vct->ptr);
1003 +static void vector_add_u8(vector * vct, u8 val)
1005 + if ((vct->size) + sizeof(u8) > (vct->capacity)) {
1006 + vector_extend(vct);
1008 + *(u8 *) ((char *) (vct->ptr) + (vct->size)) = val;
1009 + vct->size += sizeof(u8);
1012 +static void vector_add_u16(vector * vct, u16 val)
1014 + if ((vct->size) + sizeof(u16) > (vct->capacity)) {
1015 + vector_extend(vct);
1017 + *(u16 *) ((char *) (vct->ptr) + (vct->size)) = val;
1018 + vct->size += sizeof(u16);
1021 +static void vector_add_u32(vector * vct, u32 val)
1023 + if ((vct->size) + sizeof(u32) > (vct->capacity)) {
1024 + vector_extend(vct);
1026 + *(u32 *) ((char *) (vct->ptr) + (vct->size)) = val;
1027 + vct->size += sizeof(u32);
1030 +static void vector_add_s8(vector * vct, s8 val)
1032 + if ((vct->size) + sizeof(s8) > (vct->capacity)) {
1033 + vector_extend(vct);
1035 + *(s8 *) ((char *) (vct->ptr) + (vct->size)) = val;
1036 + vct->size += sizeof(s8);
1039 +static void vector_add_s16(vector * vct, s16 val)
1041 + if ((vct->size) + sizeof(s16) > (vct->capacity)) {
1042 + vector_extend(vct);
1044 + *(s16 *) ((char *) (vct->ptr) + (vct->size)) = val;
1045 + vct->size += sizeof(s16);
1048 +static void vector_add_s32(vector * vct, s32 val)
1050 + if ((vct->size) + sizeof(s32) > (vct->capacity)) {
1051 + vector_extend(vct);
1053 + *(s32 *) ((char *) (vct->ptr) + (vct->size)) = val;
1054 + vct->size += sizeof(s32);
1057 +static void vector_add_ptr(vector * vct, void *ptr)
1059 + if ((vct->size) + sizeof(void *) > (vct->capacity)) {
1060 + vector_extend(vct);
1062 + *(void **) ((char *) (vct->ptr) + (vct->size)) = ptr;
1063 + vct->size += sizeof(void *);
1066 +static void vector_concat(vector * lhs, vector * rhs)
1069 + if (!(rhs->size)) {
1073 + lhs->capacity = (lhs->size) + (rhs->size);
1074 + lhs->ptr = jffs2_bbc_malloc(lhs->capacity);
1076 + memcpy(lhs->ptr, tmp, lhs->size);
1077 + jffs2_bbc_free(tmp);
1079 + memcpy((((u8 *) lhs->ptr) + lhs->size), rhs->ptr, rhs->size);
1080 + lhs->size += rhs->size;
1085 +//ORIGIN: DataStructures/src/BitVector.c
1087 +/*******************************************************************************
1088 +* FILE: BitVector.c
1089 +* AUTHOR: Tamás Gergely
1091 +*******************************************************************************/
1093 +//#include "ipack_common.h"
1094 +//#include "DataStructures/BitVector.h"
1096 +#include <memory.h>
1099 +#define VECTOR_ALLOC_SIZE 0x00001000
1101 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1103 +static void bitblocks_clear(BitBlocks * this)
1107 + for (it = this->ptr, end_it = VECTOR_P_END(this); it != end_it; it++) {
1108 + bitvector_clear(*it);
1109 + jffs2_bbc_free(*it);
1111 + jffs2_bbc_free(this->ptr);
1115 +static void bitvector_clear(BitVector * this)
1118 + jffs2_bbc_free(this->base);
1120 + this->freebits = 0;
1121 + this->capacity = 0;
1127 +static void bitvector_W_reset(BitVector * this)
1129 + this->freebits = 0;
1130 + this->capacity = 0;
1136 +static void bitvector_W_add0(BitVector * this)
1138 + if (!(this->freebits)) {
1139 + if (this->size == this->capacity) {
1140 + void *tmp = this->base;
1141 + this->capacity += VECTOR_ALLOC_SIZE;
1142 + this->base = jffs2_bbc_malloc(this->capacity);
1143 + this->ptr = ((u8 *) (this->base)) + this->size;
1144 + memcpy(this->base, tmp, this->size);
1145 + jffs2_bbc_free(tmp);
1151 + this->freebits = 7;
1152 + *(this->ptr) = 0x00;
1156 + (*(this->ptr)) <<= 1;
1160 +static void bitvector_W_add1(BitVector * this)
1162 + if (!(this->freebits)) {
1163 + if (this->size == this->capacity) {
1164 + void *tmp = this->base;
1165 + this->capacity += VECTOR_ALLOC_SIZE;
1166 + this->base = jffs2_bbc_malloc(this->capacity);
1167 + this->ptr = ((u8 *) (this->base)) + this->size;
1168 + memcpy(this->base, tmp, this->size);
1169 + jffs2_bbc_free(tmp);
1175 + this->freebits = 7;
1176 + *(this->ptr) = 0x01;
1180 + (*(this->ptr)) <<= 1;
1181 + (*(this->ptr)) |= 0x01;
1185 +static void bitvector_W_concat_b(BitVector * lhs, BitVector * rhs)
1188 + if (!(rhs->size)) {
1192 + lhs->capacity = ((((lhs->size) + (rhs->size) - 1) / VECTOR_ALLOC_SIZE) + 1) * VECTOR_ALLOC_SIZE;
1193 + lhs->base = jffs2_bbc_malloc(lhs->capacity);
1195 + memcpy(lhs->base, tmp, lhs->size);
1196 + jffs2_bbc_free(tmp);
1198 + memcpy((((u8 *) (lhs->base)) + lhs->size), rhs->base, rhs->size);
1199 + lhs->freebits = 0;
1200 + lhs->size += rhs->size;
1201 + lhs->ptr = ((u8 *) (lhs->base)) + lhs->size;
1204 +static void bitvector_W_concat_v(BitVector * lhs, vector * rhs)
1207 + if (!(rhs->size)) {
1211 + lhs->capacity = ((((lhs->size) + (rhs->size) - 1) / VECTOR_ALLOC_SIZE) + 1) * VECTOR_ALLOC_SIZE;
1212 + lhs->base = jffs2_bbc_malloc(lhs->capacity);
1214 + memcpy(lhs->base, tmp, lhs->size);
1215 + jffs2_bbc_free(tmp);
1217 + memcpy((((u8 *) (lhs->base)) + lhs->size), rhs->ptr, rhs->size);
1218 + lhs->freebits = 0;
1219 + lhs->size += rhs->size;
1220 + lhs->ptr = ((u8 *) (lhs->base)) + lhs->size;
1223 +static void bitvector_W_flush(BitVector * this)
1225 + (*(this->ptr)) <<= this->freebits;
1226 + this->freebits = 0;
1229 +static void bitvector_R_reset(BitVector * this)
1231 + this->freebits = 7;
1232 + this->ptr = this->base;
1235 +static u8 bitvector_R_get1(BitVector * this)
1237 + u8 tmp = ((*(this->ptr)) >> this->freebits) & 0x01;
1238 + if (!(this->freebits)) {
1239 + this->freebits = 7;
1248 +static u8 bitvector_R_get8(BitVector * this)
1250 + u8 tmp = (*(this->ptr));
1257 +//ORIGIN: DataStructures/src/DecisionTree.c
1259 +/*******************************************************************************
1260 +* FILE: DecisionTree.c
1261 +* AUTHOR: Tamás Gergely
1263 +*******************************************************************************/
1265 +//#include "ipack_common.h"
1266 +//#include "DataStructures/DecisionTree.h"
1268 +static void decisiontree_delete_node(void *root)
1270 + u8 tmp = GET_NODE_PTR_TYPE(root);
1271 + if (TREENODETYPE_IS_NODE_BINARY(tmp)) {
1272 + decisiontree_delete_node(((TreeNodeBinary *) root)->left);
1273 + decisiontree_delete_node(((TreeNodeBinary *) root)->right);
1275 + else if ((tmp) == TREENODETYPE_LEAF_P) {
1276 + if (((TreeLeafP *) root)->probabilities) {
1277 + jffs2_bbc_free(((TreeLeafP *) root)->probabilities);
1280 + else if ((tmp) == TREENODETYPE_LEAF_C) {
1282 + jffs2_bbc_free(root);
1285 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1287 +static void decisiontree_delete(DecisionTree * dt)
1289 + decisiontree_delete_node(dt->root);
1290 + jffs2_bbc_free(dt->predictor_max_values);
1293 +static void decisiontree_get_probability_for_token(void *root, PredictorType * preds, TokenType token, ProbabilityType * prob)
1296 + while (TREENODETYPE_IS_NODE(((TreeNodeBinary *) tmp)->type)) {
1297 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_EQ) {
1298 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_EQ((TreeNodeBinary *) tmp)->value) {
1299 + tmp = ((TreeNodeBinary *) tmp)->left;
1302 + tmp = ((TreeNodeBinary *) tmp)->right;
1305 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_LT) {
1306 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_LT((TreeNodeBinary *) tmp)->value) {
1307 + tmp = ((TreeNodeBinary *) tmp)->left;
1310 + tmp = ((TreeNodeBinary *) tmp)->right;
1317 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_P) {
1319 + u32 lngth = ((TreeLeafP *) tmp)->pairs << 1;
1320 + for (i = 0; i < lngth;) {
1321 + TokenType at = ((TreeLeafP *) tmp)->probabilities[i++];
1322 + TokenType av = ((TreeLeafP *) tmp)->probabilities[i++];
1330 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_C) {
1331 + if (((TreeLeafC *) tmp)->predicted_class == token) {
1332 + prob->high = TOKEN_MAXVALUE;
1333 + prob->max = TOKEN_MAXVALUE;
1338 +static TokenType decisiontree_get_token_for_range(void *root, PredictorType * preds, u32 value, u32 range, ProbabilityType * prob)
1341 + TokenType token = 0;
1342 + while (TREENODETYPE_IS_NODE(((TreeNodeBinary *) tmp)->type)) {
1343 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_EQ) {
1344 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_EQ((TreeNodeBinary *) tmp)->value) {
1345 + tmp = ((TreeNodeBinary *) tmp)->left;
1348 + tmp = ((TreeNodeBinary *) tmp)->right;
1351 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_LT) {
1352 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_LT((TreeNodeBinary *) tmp)->value) {
1353 + tmp = ((TreeNodeBinary *) tmp)->left;
1356 + tmp = ((TreeNodeBinary *) tmp)->right;
1363 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_P) {
1368 + u32 lngth = ((TreeLeafP *) tmp)->pairs << 1;
1369 + for (i = 0; i < lngth;) {
1371 + prob->max += ((TreeLeafP *) tmp)->probabilities[i++];
1373 + norm = (value * prob->max - 1) / range;
1374 + for (i = 0; prob->high <= norm;) {
1375 + at = ((TreeLeafP *) tmp)->probabilities[i++];
1376 + av = ((TreeLeafP *) tmp)->probabilities[i++];
1378 + if (prob->high <= norm)
1383 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_C) {
1384 + token = ((TreeLeafC *) tmp)->predicted_class;
1385 + prob->high = TOKEN_MAXVALUE;
1386 + prob->max = TOKEN_MAXVALUE;
1392 +//ORIGIN: DataStructures/src/PredictorTable.c
1394 +/*******************************************************************************
1395 +* FILE: PredictorTable.c
1396 +* AUTHOR: Tamás Gergely
1398 +*******************************************************************************/
1400 +//#include "ipack_common.h"
1401 +//#include "DataStructures/PredictorTable.h"
1403 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1405 +static void predictortable_clear(PredictorTable * table)
1407 + table->predictors = 0;
1410 +static void predictortable_free(PredictorTable * table)
1412 + if (table->predictors) {
1413 + jffs2_bbc_free(table->predictors);
1414 + table->predictors = 0;
1418 +static void predictortable_resetARM(PredictorTable * table)
1420 + register PredictorType *ptr = table->predictors;
1421 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS_ARM;
1422 + while (ptr < end) {
1427 +static void predictortable_updateARM(PredictorTable * table, TokenType token)
1429 + register PredictorType *ptr = table->predictors;
1430 + register u32 ndx = ptr[0] + 1;
1431 + ptr[ndx + 8] = ptr[ndx];
1441 +static PredictorType predictortable_minvalueARM(PredictorTable * table, u32 index)
1446 +static PredictorType predictortable_maxvalueARM(PredictorTable * table, u32 index)
1460 +/*static void predictortable_resetTXT(PredictorTable * table)
1462 + register PredictorType *ptr = table->predictors;
1463 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS_TXT;
1464 + while (ptr < end) {
1469 +static void predictortable_updateTXT(PredictorTable * table, TokenType token)
1471 + register PredictorType *ptr = table->predictors;
1472 +// register u32 ndx;
1474 + if ((('a' <= token) && (token <= 'z')) || (('A' <= token) && (token <= 'Z'))) {
1482 +static PredictorType predictortable_minvalueTXT(PredictorTable * table, u32 index)
1487 +static PredictorType predictortable_maxvalueTXT(PredictorTable * table, u32 index)
1492 +#endif // __KERNEL__
1494 +//ORIGIN: DataStructures/src/ipack_model.c
1496 +/*******************************************************************************
1497 +* FILE: ipack_model.c
1498 +* AUTHOR: Tamás Gergely
1500 +*******************************************************************************/
1502 +//#include "DataStructures/ipack_model.h"
1503 +//#include "measuredef.h"
1504 +//#include "ipack_common.h"
1506 +#ifdef __MEASURE_TIME_MODEL_GETPROB
1507 +#define __MT_P_MAX 256
1508 +#define __MT_P_DIV 128
1509 +#define __MT_P_MIN 0
1512 +static void ipack_model_get_probability_for_token(ipack_nodetype * tmp, TokenType token, ipack_probability_type * prob)
1514 +// register ipack_nodetype* tmp = model->tree_root_ptr;
1515 +// register ipack_nodetype* tmp = root;
1516 + while (TREENODETYPE_IS_NODE(tmp->type)) {
1517 + if (tmp->type == TREENODETYPE_NODE_BINARY_EQ) {
1518 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_EQ tmp->data.nodeBin.value) {
1519 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1522 + tmp = tmp->data.nodeBin.right_child_ptr;
1525 + else if (tmp->type == TREENODETYPE_NODE_BINARY_LT) {
1526 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_LT tmp->data.nodeBin.value) {
1527 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1530 + tmp = tmp->data.nodeBin.right_child_ptr;
1537 + if (tmp->type == TREENODETYPE_LEAF_P) {
1539 + prob->low = tmp->data.leafP.probabilities[token - 1];
1541 + prob->high = tmp->data.leafP.probabilities[token];
1542 +// prob->max = tmp->data.leafP.probabilities[15];
1544 + else if (tmp->type == TREENODETYPE_LEAF_C) {
1545 + if (tmp->data.leafC.predicted_class == token) {
1546 + prob->high = TOKEN_MAXVALUE;
1547 +// prob->max = TOKEN_MAXVALUE;
1552 +#ifndef IPACK_ARM_ASM
1554 +//return ipack_model_get_token_for_range2(tmp,value,range,prob);
1556 +static TokenType ipack_model_get_token_for_range(ipack_nodetype * tmp, u32 value, u32 range, ipack_probability_type * prob)
1558 +// register ipack_nodetype* tmp = model->tree_root_ptr;
1559 +// register ipack_nodetype* tmp = root;
1560 + register TokenType token = 0;
1561 + while (TREENODETYPE_IS_NODE(tmp->type)) {
1562 + if (tmp->type == TREENODETYPE_NODE_BINARY_EQ) {
1563 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_EQ tmp->data.nodeBin.value) {
1564 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1567 + tmp = tmp->data.nodeBin.right_child_ptr;
1570 + else if (tmp->type == TREENODETYPE_NODE_BINARY_LT) {
1571 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_LT tmp->data.nodeBin.value) {
1572 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1575 + tmp = tmp->data.nodeBin.right_child_ptr;
1582 + if (tmp->type == TREENODETYPE_LEAF_P) {
1585 +// prob->max = tmp->data.leafP.probabilities[15];
1586 +/* norm = (value * prob->max -1)/range;
1587 + for(i = 0; i < 15; ++i) {
1588 + if(tmp->data.leafP.probabilities[i] > norm) {
1592 + norm = ((value << PROBABILITY_SHIFT) - 1);
1593 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1594 + if (range * tmp->data.leafP.probabilities[i] > norm) {
1598 + token = (TokenType) i;
1599 + prob->high = tmp->data.leafP.probabilities[i];
1601 + prob->low = tmp->data.leafP.probabilities[token - 1];
1604 + else if (tmp->type == TREENODETYPE_LEAF_C) {
1605 + token = tmp->data.leafC.predicted_class;
1606 + prob->high = PROBABILITY_MAX;
1607 +// prob->max = PROBABILITY_MAX;
1613 +void ipack_model_predictortable_reset(PredictorType* ptr)
1615 +// register PredictorType* ptr = model->predictors_ptr;
1616 +// register PredictorType* ptr = preds;
1617 + register PredictorType* end = ptr + NUMBER_OF_PREDICTORS;
1618 + while(ptr < end) {
1623 +void ipack_model_predictortable_update(PredictorType* ptr, TokenType token)
1625 +// register PredictorType* ptr = model->predictors_ptr;
1626 +// register PredictorType* ptr = preds;
1627 + register u32 ndx = ptr[0] + 1;
1628 + ptr[ndx + 8] = ptr[ndx];
1636 +/****************************************************************************/
1639 +static void ipack_model_countpreds(void *ptr, ipack_nodetype * node, double *table, double val)
1641 + if ((node->type == TREENODETYPE_NODE_BINARY_EQ) || (node->type == TREENODETYPE_NODE_BINARY_LT)) {
1642 + table[(u32) (node->data.nodeBin.attribute_ptr) - (u32) (ptr)] += val;
1643 + ipack_model_countpreds(ptr, (void *) (((u8 *) (node)) + sizeof(node->type) + sizeof(ipack_treenodeBin)), table, val / 2);
1644 + ipack_model_countpreds(ptr, node->data.nodeBin.right_child_ptr, table, val / 2);
1650 +/*static void ipack_model_printinfo(ipack_model_type * model)
1652 + double *prcnt = jffs2_bbc_malloc(sizeof(double) * NUMBER_OF_PREDICTORS);
1654 + for (i = 0; i < NUMBER_OF_PREDICTORS; i++) {
1657 + ipack_model_countpreds(model->predictors_ptr, model->tree_root_ptr, prcnt, 100);
1658 + for (i = 0; i < NUMBER_OF_PREDICTORS; i++) {
1659 + jffs2_bbc_print3(" p[%3d] = %10.6lf\n", (int) i, prcnt[i]);
1661 + jffs2_bbc_free(prcnt);
1664 +static void ipack_dumpnode(unsigned char **ptr, FILE * file, char *prefs)
1666 + switch (*((*ptr)++)) {
1671 + case TREENODETYPE_NODE_BINARY_EQ:
1674 + fprintf(file, "%s+->\tBinary node: P[%u] equals %u\n", prefs, (unsigned int)x, (unsigned int)y);
1675 + for (j = 0; j < 4096 && prefs[j]; ++j);
1678 + ipack_dumpnode(ptr, file, prefs);
1680 + ipack_dumpnode(ptr, file, prefs);
1683 + case TREENODETYPE_NODE_BINARY_LT:
1686 + fprintf(file, "%s+->\tBinary node: P[%u] greater than %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_LEAF_P:
1697 + fprintf(file, "%s+->\tLeaf: %u pairs\n", prefs, (unsigned int)x);
1698 + (*ptr) += (x << 1);
1700 + case TREENODETYPE_LEAF_C:
1702 + fprintf(file, "%s+->\tLeaf: class %u\n", prefs, (unsigned int)x);
1705 + fprintf(file, "%s+->\tLeaf: nullnode\n", prefs);
1709 +/*static void ipack_dumpmodel(void *model)
1711 + unsigned char *tmp_ptr = model;
1714 + if ((file = fopen("DUMPED_MODEL", "wa"))) {
1716 + for (i = 0; i < 4096; C[i++] = 0);
1718 + tmp_ptr += sizeof(u32);
1719 + ipack_dumpnode(&tmp_ptr, file, C);
1726 +//ORIGIN: Builders/src/PredictorGenerator.c
1728 +/*******************************************************************************
1729 +* FILE: PredictorGenerator.c
1730 +* AUTHOR: Tamás Gergely
1732 +*******************************************************************************/
1734 +//#include "ipack_common.h"
1735 +//#include "Builders/PredictorGenerator.h"
1737 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1738 +static PredictorTable *predictorgenerator_generate( /*PredictorGeneratorSettings* settings */ )
1740 + PredictorTable *ptr = jffs2_bbc_malloc(sizeof(PredictorTable));
1741 + predictortable_clear(ptr);
1742 + ptr->predictors = jffs2_bbc_malloc(NUMBER_OF_PREDICTORS * sizeof(PredictorType));
1747 +//ORIGIN: Builders/src/ipack_armlib_compressor.c
1749 +/*******************************************************************************
1750 +* FILE: ipack_armlim_compressor.c
1751 +* AUTHOR: Tamás Gergely
1753 +*******************************************************************************/
1755 +//#include "ipack_common.h"
1756 +//#include "DataStructures.h"
1757 +//#include "Builders/PredictorGenerator.h"
1758 +//#include "Builders/Tokenizer.h"
1759 +//#include "Builders/Coder.h"
1761 +#define EC_NO_ERROR 0
1762 +#define EC_NOT_IPMF_FILE -1
1763 +#define EC_NOT_IPMF_MODEL -2
1764 +#define EC_NOT_HG_BLOCK -3
1765 +#define EC_WRONG_INPUT_LENGTH -501
1766 +#define EC_CODER_WRONG_PROBABILITY 1
1767 +#define EC_CODER_WRONG_RANGE 2
1768 +#define EC_BUFFER_OVERFLOW 501
1769 +#define EC_BUFFER_UNDERFLOW 502
1770 +#define EC_UNKNOWN_TOKEN_TYPE 1001
1771 +#define EC_UNKNOWN_FILTER 1002
1772 +#define EC_UNKNOWN_CONVERTER 1003
1773 +#define EC_UNKNOWN_MANIPULATOR 1004
1775 +/*******************************************************************************
1777 + COMPRESSOR INIT FUNCTIONS
1779 +*******************************************************************************/
1781 +#define ROUND_UP_TO_DWORD(val) ( ( (val) + 3 ) & 0xfffffffc )
1784 +int ipack_glb_endian_X;
1787 +static int ipack_compressor_init_tree(unsigned char **ptr, ipack_model_type * model, ipack_nodetype * node, void *nullnode)
1790 + node->type = *((*ptr)++);
1791 + switch (node->type) {
1798 + case TREENODETYPE_NODE_BINARY_EQ:
1799 + case TREENODETYPE_NODE_BINARY_LT:
1800 + node->data.nodeBin.attribute_ptr = (model->predictors_ptr) + (*((*ptr)++));
1801 + node->data.nodeBin.value = *((*ptr)++);
1802 + retval = sizeof(node->data.nodeBin);
1803 + retval += ipack_compressor_init_tree(ptr, model, (void *) ROUND_UP_TO_DWORD(((u32) node) + sizeof(node->type) + sizeof(node->data.nodeBin)), nullnode);
1804 + node->data.nodeBin.right_child_ptr = (void *) ROUND_UP_TO_DWORD(((u32) node) + retval + sizeof(node->type));
1805 + retval += ipack_compressor_init_tree(ptr, model, node->data.nodeBin.right_child_ptr, nullnode);
1807 + case TREENODETYPE_LEAF_P:
1808 + lngth = *((*ptr)++);
1810 + for (i = 0, j = 0; i < lngth; ++i) {
1813 + node->data.leafP.probabilities[j++] = av;
1815 + av += *((*ptr)++);
1817 + while (j < NUMBER_OF_TOKENS) {
1818 + node->data.leafP.probabilities[j++] = av;
1820 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1821 + node->data.leafP.probabilities[i] = ((node->data.leafP.probabilities[i] << PROBABILITY_SHIFT) / node->data.leafP.probabilities[NUMBER_OF_TOKENS - 1]);
1823 + retval = ROUND_UP_TO_DWORD(NUMBER_OF_TOKENS * sizeof(u16));
1825 + case TREENODETYPE_LEAF_C:
1826 + node->data.leafC.predicted_class = *((*ptr)++);
1827 + retval = sizeof(node->data.leafC);
1828 + retval = ROUND_UP_TO_DWORD(retval);
1833 + return retval + sizeof(node->type);
1836 +#define IPACK_TREE_CONVERT_REPLACE 0
1837 +#define IPACK_TREE_CONVERT_KEEP 1
1839 +static void *ipack_tree_to_code(ipack_model_type * model, int *code_size);
1841 +static int ipack_armlib_convert_tree_to_code(ipack_model_type * model_img, int mode)
1843 +#ifdef IPACK_TREE_TO_CODE
1846 + model_img->tree_code = ipack_tree_to_code(model_img, &tree_size);
1847 + jffs2_bbc_print2("Convertation done. Code size=%d\n", tree_size);
1848 + if (mode == IPACK_TREE_CONVERT_REPLACE) {
1849 + jffs2_bbc_print1("Freeing original tree.\n");
1850 + jffs2_bbc_free(model_img->tree_root_ptr);
1851 + model_img->tree_root_ptr = NULL;
1858 +static int ipack_armlib_compressor_init(void **model)
1860 + int retval = EC_NO_ERROR;
1861 + unsigned char *tmp_ptr = *model;
1863 + ipack_model_type *model_img;
1866 + if (*(tmp_ptr++) != 'i') {
1867 + return EC_NOT_IPMF_FILE;
1869 + else if (*(tmp_ptr++) != 'P') {
1870 + return EC_NOT_IPMF_FILE;
1872 + else if (*(tmp_ptr++) != 'M') {
1873 + return EC_NOT_IPMF_FILE;
1875 + else if (*(tmp_ptr++) != 'F') {
1876 + return EC_NOT_IPMF_FILE;
1878 + tmp_c[0] = *(tmp_ptr++);
1879 + tmp_c[1] = *(tmp_ptr++);
1882 + //model_img = jffs2_bbc_malloc(*((u32*)tmp_ptr));
1883 + model_img = jffs2_bbc_malloc(sizeof(ipack_model_type) + ROUND_UP_TO_DWORD(NUMBER_OF_PREDICTORS));
1884 + model_img->tree_root_ptr = jffs2_bbc_malloc(*((u32 *) tmp_ptr)); //it is smaller a little but, but...
1886 + tmp_ptr += sizeof(u32);
1888 + model_img->ID[0] = 'i';
1889 + model_img->ID[1] = 'P';
1890 + model_img->ID[2] = 'M';
1891 + model_img->ID[3] = 'F';
1893 + model_img->block_sign[0] = tmp_c[0];
1894 + model_img->block_sign[1] = tmp_c[1];
1896 + model_img->nullnode.type = TREENODETYPE_LEAF_P;
1897 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1898 + model_img->nullnode.probabilities[i] = 0;
1900 + model_img->predictors_ptr = (void *) (((u32) model_img) + sizeof(ipack_model_type));
1901 + //model_img->tree_root_ptr = (void*)ROUND_UP_TO_DWORD(((u32)(model_img->predictors_ptr)) + NUMBER_OF_PREDICTORS);//ALIGN
1903 + ipack_compressor_init_tree(&tmp_ptr, model_img, model_img->tree_root_ptr, &(model_img->nullnode));
1905 +#ifdef IPACK_TREE_TO_CODE
1906 +#ifdef IPACK_AUTO_TREE_TO_CODE
1907 + jffs2_bbc_print1("Automatically converting tree to ARM code...\n");
1908 + ipack_armlib_convert_tree_to_code(model_img, IPACK_TREE_CONVERT_REPLACE);
1910 + model_img->tree_code = NULL;
1913 + model_img->tree_code = NULL;
1916 + jffs2_bbc_free(*model);
1917 + *model = model_img;
1921 +/*******************************************************************************
1923 + COMPRESSOR DEINIT FUNCTIONS
1925 +*******************************************************************************/
1928 +/* Descructor of compressor (model will be freed with jffs2_bbc_free() after it)*/
1929 +static void ipack_armlib_compressor_deinit(void)
1933 +/*******************************************************************************
1935 + COMPRESS FUNCTIONS
1937 +*******************************************************************************/
1939 +static int writebits0(unsigned char **dest, u8 * freebits, u32 * opposite, unsigned char *end)
1941 + if (!(*freebits)) {
1950 + if ((*dest == end) && !(*freebits)) {
1951 + return EC_BUFFER_OVERFLOW;
1953 + while (*opposite) {
1955 + if (!(*freebits)) {
1965 + if ((*dest == end) && !(*freebits)) {
1966 + return EC_BUFFER_OVERFLOW;
1972 +static int writebits1(unsigned char **dest, u8 * freebits, u32 * opposite, unsigned char *end)
1974 + if (!(*freebits)) {
1984 + if ((*dest == end) && !(*freebits)) {
1985 + return EC_BUFFER_OVERFLOW;
1987 + while (*opposite) {
1989 + if (!(*freebits)) {
1998 + if ((*dest == end) && !(*freebits)) {
1999 + return EC_BUFFER_OVERFLOW;
2009 + * *dstlen bytes are allocated.
2010 + * if it is not enough write *sourcelen over to the processed amount of data
2011 + * returns non zero if fails
2013 +static int ipack_armlib_compress(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen)
2015 + register u32 coder_high = CODER_VALUEMAX - 1;
2016 + register u32 coder_low = 0;
2017 + u32 coder_opbits = 0;
2018 + u8 bitvector_freebits = 8;
2019 + unsigned char *bitvector_ptr = output;
2020 + unsigned char *bitvector_end = output + (*dstlen - 1);
2021 + ARM_DataType *tmpp;
2026 + ipack_nodetype *treeroot = ((ipack_model_type *) model)->tree_root_ptr;
2027 + PredictorType *predctrs = ((ipack_model_type *) model)->predictors_ptr;
2029 +#ifdef IPACK_TREE_TO_CODE
2030 + void (*treefunc) (ipack_nodetype *, TokenType, ipack_probability_type *);
2032 + treefunc = ((ipack_model_type *) model)->tree_code;
2033 + if (treefunc != NULL)
2037 + if ((*sourcelen % 4) != 0) {
2038 + return EC_WRONG_INPUT_LENGTH;
2040 + if (*dstlen <= 4) {
2041 + return EC_BUFFER_OVERFLOW;
2044 + if (((ipack_model_type *) model)->ID[0] != 'i') {
2045 + return EC_NOT_IPMF_MODEL;
2047 + else if (((ipack_model_type *) model)->ID[1] != 'P') {
2048 + return EC_NOT_IPMF_MODEL;
2050 + else if (((ipack_model_type *) model)->ID[2] != 'M') {
2051 + return EC_NOT_IPMF_MODEL;
2053 + else if (((ipack_model_type *) model)->ID[3] != 'F') {
2054 + return EC_NOT_IPMF_MODEL;
2057 + tmpv.capacity = (*sourcelen);
2059 + tmpv.capacity = (*sourcelen) << 1;
2061 + tmpv.size = tmpv.capacity;
2062 + tmpv.ptr = jffs2_bbc_malloc(tmpv.size);
2066 + if (ipack_glb_endian_X) {
2067 + for (tmpp = (void *) input; (u32) tmpp < (u32) (input + *sourcelen); ++tmpp) {
2069 + *(it++) = (u8) ((*tmpp & 0xff000000) >> 24);
2070 + *(it++) = (u8) ((*tmpp & 0x00ff0000) >> 16);
2071 + *(it++) = (u8) ((*tmpp & 0x0000ff00) >> 8);
2072 + *(it++) = (u8) ((*tmpp & 0x000000ff));
2074 + *(it++) = (u8) ((*tmpp & 0x0000f000) >> 12);
2075 + *(it++) = (u8) ((*tmpp & 0x0000000f));
2076 + *(it++) = (u8) ((*tmpp & 0xf0000000) >> 28);
2077 + *(it++) = (u8) ((*tmpp & 0x000f0000) >> 16);
2078 + *(it++) = (u8) ((*tmpp & 0x00000f00) >> 8);
2079 + *(it++) = (u8) ((*tmpp & 0x00f00000) >> 20);
2080 + *(it++) = (u8) ((*tmpp & 0x0f000000) >> 24);
2081 + *(it++) = (u8) ((*tmpp & 0x000000f0) >> 4);
2082 +#endif //TXT_TOKENS
2087 + for (tmpp = (void *) input; (u32) tmpp < (u32) (input + *sourcelen); ++tmpp) {
2089 + *(it++) = (u8) ((*tmpp & 0x000000ff));
2090 + *(it++) = (u8) ((*tmpp & 0x0000ff00) >> 8);
2091 + *(it++) = (u8) ((*tmpp & 0x00ff0000) >> 16);
2092 + *(it++) = (u8) ((*tmpp & 0xff000000) >> 24);
2094 + *(it++) = (u8) ((*tmpp & 0x00f00000) >> 20);
2095 + *(it++) = (u8) ((*tmpp & 0x0f000000) >> 24);
2096 + *(it++) = (u8) ((*tmpp & 0x000000f0) >> 4);
2097 + *(it++) = (u8) ((*tmpp & 0x00000f00) >> 8);
2098 + *(it++) = (u8) ((*tmpp & 0x000f0000) >> 16);
2099 + *(it++) = (u8) ((*tmpp & 0x0000f000) >> 12);
2100 + *(it++) = (u8) ((*tmpp & 0x0000000f));
2101 + *(it++) = (u8) ((*tmpp & 0xf0000000) >> 28);
2102 +#endif //TXT_TOKENS
2110 + { //predictor reset
2111 + register PredictorType *ptr = predctrs;
2112 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS;
2113 + while (ptr < end) {
2118 + //*(bitvector_ptr++) = 'H';
2119 + //*(bitvector_ptr++) = 'G';
2120 + *(bitvector_ptr++) = ((ipack_model_type *) model)->block_sign[0];
2121 + *(bitvector_ptr++) = ((ipack_model_type *) model)->block_sign[1];
2123 + *(bitvector_ptr++) = (unsigned char) (((*sourcelen) >> 8) & 0xff);
2124 + *(bitvector_ptr++) = (unsigned char) ((*sourcelen) & 0xff);
2125 + for (it = tmpv.ptr, end_it = VECTOR_S_END(tmpv); it != end_it; ++it) {
2126 + ipack_probability_type prob;
2129 +#ifdef IPACK_TREE_TO_CODE
2130 + if (treefunc != NULL)
2131 + (*treefunc) (treeroot, *it, &prob);
2133 + ipack_model_get_probability_for_token(treeroot, *it, &prob);
2135 + ipack_model_get_probability_for_token(treeroot, *it, &prob);
2138 + if (prob.high == prob.low) {
2139 + vector_clear(&tmpv);
2140 + return EC_CODER_WRONG_PROBABILITY;
2142 + range = coder_high - coder_low + 1;
2143 + coder_high = coder_low + ((range * prob.high) >> PROBABILITY_SHIFT) - 1;
2144 + coder_low += ((range * prob.low) >> PROBABILITY_SHIFT);
2146 + if (coder_high < CODER_VALUEHLF) {
2147 + if (writebits0(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2148 + vector_clear(&tmpv);
2149 + return EC_BUFFER_OVERFLOW;
2152 + else if (coder_low >= CODER_VALUEHLF) {
2153 + if (writebits1(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2154 + vector_clear(&tmpv);
2155 + return EC_BUFFER_OVERFLOW;
2157 + coder_high -= CODER_VALUEHLF;
2158 + coder_low -= CODER_VALUEHLF;
2160 + else if ((CODER_VALUE1ST <= coder_low) && (coder_high < CODER_VALUE3RD)) {
2162 + coder_high -= CODER_VALUE1ST;
2163 + coder_low -= CODER_VALUE1ST;
2171 + if (coder_high < coder_low) {
2172 + vector_clear(&tmpv);
2173 + return EC_CODER_WRONG_RANGE;
2178 +// register u32 ndx;
2179 + predctrs[0] = *it;
2180 + if ((('a' <= *it) && (*it <= 'z')) || (('A' <= *it) && (*it <= 'Z'))) {
2187 + register u32 ndx = predctrs[0] + 1;
2188 + predctrs[ndx + 8] = predctrs[ndx];
2189 + predctrs[ndx] = *it;
2200 + vector_clear(&tmpv);
2202 + if (coder_low < CODER_VALUE1ST) {
2203 + if (writebits0(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2204 + return EC_BUFFER_OVERFLOW;
2208 + if (writebits1(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2209 + return EC_BUFFER_OVERFLOW;
2212 + (*(bitvector_ptr)) <<= bitvector_freebits;
2213 + *dstlen = ((u32) bitvector_ptr - (u32) output + 1);
2214 + return EC_NO_ERROR;
2217 +/*******************************************************************************
2219 + DECOMPRESS FUNCTIONS
2221 +*******************************************************************************/
2229 +} ipack_decompressor_values;
2234 + unsigned char *ptr;
2235 + unsigned char *end;
2236 +} ipack_decompressor_bitvector;
2238 +static u8 ipack_bitvector_R_get1(ipack_decompressor_bitvector * bv)
2241 + if (bv->ptr == bv->end) {
2245 + tmp = (*(bv->ptr) >> bv->freebits) & 0x01;
2246 + if (!(bv->freebits)) {
2256 +/* Decompress block
2257 + * returns non zero if fails
2259 +static int ipack_armlib_decompress(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen)
2261 + ARM_DataType *data;
2262 + register u32 coder_high = CODER_VALUEMAX - 1;
2263 + register u32 coder_low = 0;
2264 + register u32 coder_value = 0;
2265 + u32 coder_overread = 0;
2266 + ipack_decompressor_bitvector bitvector;
2270 + TokenType tkns[8];
2273 + ipack_nodetype *treeroot = ((ipack_model_type *) model)->tree_root_ptr;
2274 + PredictorType *predctrs = ((ipack_model_type *) model)->predictors_ptr;
2276 +#ifdef IPACK_TREE_TO_CODE
2277 + TokenType(*treefunc) (ipack_nodetype *, u32, u32, ipack_probability_type *);
2279 + treefunc = ((ipack_model_type *) model)->tree_code;
2283 + if (((ipack_model_type *) model)->ID[0] != 'i') {
2284 + return EC_NOT_IPMF_MODEL;
2286 + else if (((ipack_model_type *) model)->ID[1] != 'P') {
2287 + return EC_NOT_IPMF_MODEL;
2289 + else if (((ipack_model_type *) model)->ID[2] != 'M') {
2290 + return EC_NOT_IPMF_MODEL;
2292 + else if (((ipack_model_type *) model)->ID[3] != 'F') {
2293 + return EC_NOT_IPMF_MODEL;
2296 + bitvector.freebits = 7;
2297 + bitvector.ptr = input;
2298 + bitvector.end = input + sourcelen;
2300 + /*if(*(bitvector.ptr++) != 'H') {
2301 + return EC_NOT_HG_BLOCK;
2302 + } else if(*(bitvector.ptr++) != 'G') {
2303 + return EC_NOT_HG_BLOCK;
2308 + data = (void *) output;
2309 + cntbytes = *(bitvector.ptr++);
2311 + cntbytes += *(bitvector.ptr++);
2313 + { //predictor reset
2314 + register PredictorType *ptr = predctrs;
2315 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS;
2316 + while (ptr < end) {
2320 + for (i = 0; i < CODER_VALUEBITS; ++i) {
2321 + coder_value <<= 1;
2322 + coder_value += ipack_bitvector_R_get1(&bitvector);
2324 + lngth = dstlen >> 2;
2325 + if (lngth > (cntbytes >> 2)) {
2326 + lngth = cntbytes >> 2;
2328 + for (i = 0; (i < lngth); ++i) {
2332 + for (j = 0; j < NUMBER_OF_TOKENS_PER_INSTRUCTION; ++j) {
2333 + ipack_probability_type prob;
2334 + u32 range = coder_high - coder_low + 1;
2336 +#ifdef IPACK_TREE_TO_CODE
2337 + if (treefunc != NULL)
2338 + itoken = (*treefunc) (treeroot, coder_value - coder_low + 1, range, &prob);
2341 + itoken = ipack_model_get_token_for_range(treeroot, coder_value - coder_low + 1, range, &prob);
2344 + if (prob.high == prob.low) {
2345 + return EC_CODER_WRONG_PROBABILITY;
2347 + coder_high = coder_low + ((range * prob.high) >> PROBABILITY_SHIFT) - 1;
2348 + coder_low += ((range * prob.low) >> PROBABILITY_SHIFT);
2350 + if (coder_high < CODER_VALUEHLF) {
2352 + else if (CODER_VALUEHLF <= coder_low) {
2353 + coder_value -= CODER_VALUEHLF;
2354 + coder_high -= CODER_VALUEHLF;
2355 + coder_low -= CODER_VALUEHLF;
2357 + else if ((CODER_VALUE1ST <= coder_low) && (coder_high < CODER_VALUE3RD)) {
2358 + coder_value -= CODER_VALUE1ST;
2359 + coder_high -= CODER_VALUE1ST;
2360 + coder_low -= CODER_VALUE1ST;
2368 + coder_value <<= 1;
2369 + if (bitvector.ptr == bitvector.end) {
2370 + bitvector.freebits = 0;
2372 + coder_value += ((*(bitvector.ptr) >> bitvector.freebits) & 0x01);
2373 + if (bitvector.freebits) {
2374 + --bitvector.freebits;
2377 + bitvector.freebits = 7;
2380 + if (coder_high < coder_low) {
2381 + return EC_CODER_WRONG_RANGE;
2383 + if ((bitvector.ptr == bitvector.end) && !(bitvector.freebits)) {
2384 + if ((coder_overread++) > CODER_VALUEBITS) {
2385 + return EC_BUFFER_UNDERFLOW;
2391 +// register u32 ndx;
2392 + predctrs[0] = itoken;
2393 + if ((('a' <= itoken) && (itoken <= 'z')) || (('A' <= itoken) && (itoken <= 'Z'))) {
2401 + register u32 ndx = predctrs[0] + 1;
2402 + predctrs[ndx + 8] = predctrs[ndx];
2403 + predctrs[ndx] = itoken;
2413 + (*(tptr++)) = itoken;
2417 + if (ipack_glb_endian_X) {
2419 + (*data) = ((*tptr) << 24);
2421 + (*data) |= ((*tptr) << 16);
2423 + (*data) |= ((*tptr) << 8);
2425 + (*data) |= (*tptr);
2428 + (*data) = (((*tptr) & 0xf) << 12);
2430 + (*data) |= ((*tptr) & 0xf);
2432 + (*data) |= (((*tptr) & 0xf) << 28);
2434 + (*data) |= (((*tptr) & 0xf) << 16);
2436 + (*data) |= (((*tptr) & 0xf) << 8);
2438 + (*data) |= (((*tptr) & 0xf) << 20);
2440 + (*data) |= (((*tptr) & 0xf) << 24);
2442 + (*data) |= (((*tptr) & 0xf) << 4);
2449 + (*data) = (*tptr);
2451 + (*data) |= ((*tptr) << 8);
2453 + (*data) |= ((*tptr) << 16);
2455 + (*data) |= ((*tptr) << 24);
2458 + (*data) = (((*tptr) & 0xf) << 20);
2460 + (*data) |= (((*tptr) & 0xf) << 24);
2462 + (*data) |= (((*tptr) & 0xf) << 4);
2464 + (*data) |= (((*tptr) & 0xf) << 8);
2466 + (*data) |= (((*tptr) & 0xf) << 16);
2468 + (*data) |= (((*tptr) & 0xf) << 12);
2470 + (*data) |= ((*tptr) & 0xf);
2472 + (*data) |= (((*tptr) & 0xf) << 28);
2479 + return EC_NO_ERROR;
2482 +static int ipack_armlib_estimate(void *model, unsigned char *input, unsigned long sourcelen, unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime)
2484 + int i, tmp, tmp2, max, maxi;
2485 + int cnt_cond[] = { 0, 0, 0, 0 };
2486 + int cnt_inst[] = { 0, 0, 0, 0 };
2488 + // TODO: make a more precise estimation!!!
2489 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 6;
2490 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 2;
2492 + if (sourcelen % 4 != 0) {
2493 + *dstlen = sourcelen;
2496 + for (i = 0; i < sourcelen; i++, input++) {
2497 + tmp2 = tmp = *input;
2498 + tmp = ((tmp) & 0xf0) >> 4;
2499 + tmp2 = tmp2 & 0xf;
2501 + cnt_cond[i % 4]++;
2502 + if ((tmp2 == 2) || (tmp2 == 3))
2503 + cnt_inst[i % 4]++;
2507 + for (i = 0; i < 4; i++)
2508 + if (max < cnt_cond[i]) {
2509 + max = cnt_cond[i];
2512 + /*jffs2_bbc_print("armlib_EST: %d/%d : %d/%d %d/%d %d/%d %d/%d",
2513 + cnt_cond[maxi],cnt_inst[maxi],
2514 + cnt_cond[0],cnt_inst[0],
2515 + cnt_cond[1],cnt_inst[1],
2516 + cnt_cond[2],cnt_inst[2],
2517 + cnt_cond[3],cnt_inst[3]); */
2519 + if (cnt_cond[maxi] < (sourcelen >> 4)) {
2520 + *dstlen = sourcelen;
2523 + *dstlen = sourcelen / 3;
2529 +static char *ipack_armlib_proc_info(void);
2530 +static int ipack_armlib_proc_command(char *command);
2531 +static void ipack_armlib_destroy_model(void **model);
2533 +struct jffs2_bbc_compressor_type jffs2_bbc_armlib = {
2538 + ipack_armlib_compressor_init, // init_model
2539 + ipack_armlib_destroy_model, // destroy_model
2540 + ipack_armlib_compressor_deinit, // deinit
2541 + ipack_armlib_compress,
2542 + ipack_armlib_estimate,
2543 + ipack_armlib_decompress,
2544 + ipack_armlib_proc_info,
2545 + ipack_armlib_proc_command
2549 +static char *ipack_armlib_proc_info()
2551 +#ifdef IPACK_TREE_TO_CODE
2552 +#ifdef IPACK_AUTO_TREE_TO_CODE
2553 + return "automatic tree to code conversion";
2555 + return "manual tree to code conversion possibility";
2558 + return "tree in memory version";
2562 +static int ipack_armlib_proc_command(char *command)
2564 + struct jffs2_bbc_model_list_node *model;
2565 + ipack_model_type *armlib_model;
2567 + if ((*command == 'g') || (*command == 'G')) {
2568 + jffs2_bbc_print1("Converting tree(s) to ARM code... (keeping original)\n");
2569 + model = jffs2_bbc_armlib.models;
2570 + if (model == NULL)
2571 + jffs2_bbc_print1("no model found!\n");
2572 + while (model != NULL) {
2573 + armlib_model = model->model;
2574 + if (armlib_model == NULL) {
2575 + jffs2_bbc_print1("Error: NULL model!\n");
2578 + ipack_armlib_convert_tree_to_code(armlib_model, IPACK_TREE_CONVERT_KEEP);
2580 + model = model->next_compr_model;
2583 + else if ((*command == 'r') || (*command == 'R')) {
2584 + jffs2_bbc_print1("Converting tree(s) to ARM code... (deleting original)\n");
2585 + model = jffs2_bbc_armlib.models;
2586 + if (model == NULL)
2587 + jffs2_bbc_print1("no model found!\n");
2588 + while (model != NULL) {
2589 + armlib_model = model->model;
2590 + if (armlib_model == NULL) {
2591 + jffs2_bbc_print1("Error: NULL model!\n");
2594 + //armlib_model->tree_code = ipack_tree_to_code(armlib_model, &tree_size);
2595 + //jffs2_bbc_print("Convertation done. Code size=%d\n",tree_size);
2596 + ipack_armlib_convert_tree_to_code(armlib_model, IPACK_TREE_CONVERT_REPLACE);
2598 + model = model->next_compr_model;
2601 + else if ((*command == 'c') || (*command == 'C')) {
2602 + jffs2_bbc_print1("Deleting ARM representation of the tree(s)...\n");
2603 + model = jffs2_bbc_armlib.models;
2604 + if (model == NULL)
2605 + jffs2_bbc_print1("no model found!\n");
2606 + while (model != NULL) {
2607 + armlib_model = model->model;
2608 + if (armlib_model == NULL) {
2609 + jffs2_bbc_print1("Error: NULL model!\n");
2612 + if (armlib_model->tree_code == NULL) {
2613 + jffs2_bbc_print1("already deleted.\n");
2616 + if (armlib_model->tree_root_ptr == NULL) {
2617 + jffs2_bbc_print1("cannot delete this ARM tree - original tree has deleted\n");
2620 + jffs2_bbc_print1("deleting...");
2621 + jffs2_bbc_free(armlib_model->tree_code);
2622 + armlib_model->tree_code = NULL;
2623 + jffs2_bbc_print1("done.\n");
2627 + model = model->next_compr_model;
2630 + else if (*command == '?') {
2631 + jffs2_bbc_print1("ARMLIB commands:\n");
2632 + jffs2_bbc_print1(" g: convert TREEs to ARM code and keep the original\n");
2633 + jffs2_bbc_print1(" r: convert TREEs to ARM code and remove the original\n");
2634 + jffs2_bbc_print1(" c: delete the original TREEs - if there is any\n");
2637 + jffs2_bbc_print1("Unknown command.\n");
2642 +static void ipack_armlib_destroy_model(void **model)
2644 + ipack_model_type *model_img;
2646 + model_img = *model;
2647 + if (model_img == NULL) {
2648 + jffs2_bbc_print1("jffs2.bbc: armlib: NULL model at destoying model!\n");
2651 + if (model_img->tree_code != NULL) {
2652 + //jffs2_bbc_print1("jffs2.bbc: armlib: debug: freeing code...\n");
2653 + jffs2_bbc_free(model_img->tree_code);
2654 + model_img->tree_code = NULL;
2656 + if (model_img->tree_root_ptr != NULL) {
2657 + //jffs2_bbc_print1("jffs2.bbc: armlib: debug: freeing tree...\n");
2658 + jffs2_bbc_free(model_img->tree_root_ptr);
2659 + model_img->tree_root_ptr = NULL;
2662 + jffs2_bbc_free(model_img);
2666 +struct jffs2_bbc_compressor_type *jffs2_bbc_armlib_init(int mode)
2668 + if (jffs2_bbc_register_compressor(&jffs2_bbc_armlib) == 0)
2669 + return &jffs2_bbc_armlib;
2674 +void jffs2_bbc_armlib_deinit(void)
2676 + jffs2_bbc_unregister_compressor(&jffs2_bbc_armlib);
2680 diff -Nur linux-mips-cvs/fs/jffs2/jffs2_bbc_framework.c linux-mips/fs/jffs2/jffs2_bbc_framework.c
2681 --- linux-mips-cvs/fs/jffs2/jffs2_bbc_framework.c 1970-01-01 01:00:00.000000000 +0100
2682 +++ linux-mips/fs/jffs2/jffs2_bbc_framework.c 2005-02-07 05:08:34.000000000 +0100
2685 + * JFFS2-BBC: Compression Framework
2689 + * Copyright (C) 2004, Ferenc Havasi
2691 + * This program is free software; you can redistribute it and/or
2692 + * modify it under the terms of the GNU General Public License
2693 + * as published by the Free Software Foundation; either version 2
2694 + * of the License, or (at your option) any later version.
2696 + * This program is distributed in the hope that it will be useful,
2697 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2698 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2699 + * GNU General Public License for more details.
2701 + * You should have received a copy of the GNU General Public License
2702 + * along with this program; if not, write to the Free Software
2703 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2707 +/* USE JFFS2_BBC_STANDALONE define if you don't want to compile without JFFS2 */
2709 +//#define DEBUG_COMPRESSORS
2710 +//#define DEBUG_SHOW_BLOCK_SIZES
2712 +#define JFFS2_BBC_STAT_BUFF_SIZE 8000
2717 +#include <malloc.h>
2718 +typedef unsigned long uint32_t;
2722 +#include <linux/kernel.h>
2723 +#include <linux/slab.h>
2724 +#include <linux/vmalloc.h>
2728 +#define JFFS2_BBC_ZLIB_BLOCK_SIGN_0 (120)
2729 +#define JFFS2_BBC_ZLIB_BLOCK_SIGN_1 (94)
2731 +#define JFFS2_BBC_DUMMY_BLOCKSIGN_0 (0x54)
2732 +#define JFFS2_BBC_DUMMY_BLOCKSIGN_1 (0x01)
2735 +#define NULL ((void*)(0))
2738 +#include "jffs2_bbc_framework.h"
2740 +/*********************************************************************
2742 + *********************************************************************/
2744 +static int jffs2_bbc_compression_mode = JFFS2_BBC_ZLIB_MODE;
2745 +static struct jffs2_bbc_compressor_type *jffs2_bbc_manual_compressor = NULL;
2746 +static struct jffs2_bbc_compressor_type *jffs2_bbc_compressors = NULL;
2747 +static struct jffs2_bbc_model_list_node *jffs2_bbc_model_list = NULL;
2748 +static void *last_sb = NULL; /* previously activated sb */
2750 +/*********************************************************************
2751 + * Compressor initialization *
2752 + *********************************************************************/
2754 +#ifndef JFFS2_BBC_STANDALONE
2756 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2757 +struct jffs2_bbc_compressor_type *jffs2_bbc_armlib_init(void);
2758 +void jffs2_bbc_armlib_deinit(void);
2761 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2762 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzo_init(void);
2763 +void jffs2_bbc_lzo_deinit(void);
2766 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2767 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzss_init(void);
2768 +void jffs2_bbc_lzss_deinit(void);
2771 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2772 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzari_init(void);
2773 +void jffs2_bbc_lzari_deinit(void);
2776 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2777 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzhd_init(void);
2778 +void jffs2_bbc_lzhd_deinit(void);
2781 +void jffs2_bbc_compressor_init()
2783 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2784 + jffs2_bbc_armlib_init();
2786 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2787 + jffs2_bbc_lzo_init();
2789 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2790 + jffs2_bbc_lzss_init();
2792 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2793 + jffs2_bbc_lzari_init();
2795 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2796 + jffs2_bbc_lzhd_init();
2800 +void jffs2_bbc_compressor_deinit()
2802 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2803 + jffs2_bbc_lzhd_deinit();
2805 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2806 + jffs2_bbc_lzari_deinit();
2808 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2809 + jffs2_bbc_lzss_deinit();
2811 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2812 + jffs2_bbc_lzo_deinit();
2814 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2815 + jffs2_bbc_armlib_deinit();
2821 +#ifndef JFFS2_BBC_STANDALONE
2823 +/*********************************************************************
2824 + * ZLIB COMPRESSOR *
2825 + *********************************************************************/
2827 +extern int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out, uint32_t * sourcelen, uint32_t * dstlen);
2828 +extern void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen);
2830 +static int jffs2_bbc_zlib_compress(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen)
2832 + return jffs2_zlib_compress2(input, output, sourcelen, dstlen);
2835 +static int jffs2_bbc_zlib_decompress(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen)
2837 + jffs2_zlib_decompress2(input, output, sourcelen, dstlen);
2841 +static int jffs2_bbc_zlib_estimate(void *model, unsigned char *input, unsigned long sourcelen, unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime)
2843 + *dstlen = sourcelen * 65 / 100;
2844 + *readtime = JFFS2_BBC_ZLIB_READ_TIME;
2845 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME;
2849 +static struct jffs2_bbc_compressor_type jffs2_bbc_zlib = {
2852 + {JFFS2_BBC_ZLIB_BLOCK_SIGN_0, JFFS2_BBC_ZLIB_BLOCK_SIGN_1, 0, 0},
2857 + jffs2_bbc_zlib_compress,
2858 + jffs2_bbc_zlib_estimate,
2859 + jffs2_bbc_zlib_decompress,
2865 +static struct jffs2_bbc_compressor_type *jffs2_bbc_original_compressor = &jffs2_bbc_zlib;
2869 +/*********************************************************************
2870 + * Compression mode handling *
2871 + *********************************************************************/
2873 +int jffs2_bbc_get_compression_mode(void)
2875 + return jffs2_bbc_compression_mode;
2878 +void jffs2_bbc_set_compression_mode(int mode)
2880 + jffs2_bbc_compression_mode = mode;
2883 +void jffs2_bbc_set_manual_compressor(struct jffs2_bbc_compressor_type *c)
2885 + jffs2_bbc_manual_compressor = c;
2886 + jffs2_bbc_set_compression_mode(JFFS2_BBC_MANUAL_MODE);
2889 +int jffs2_bbc_set_manual_compressor_by_name(char *name)
2891 + struct jffs2_bbc_compressor_type *l;
2894 + l = jffs2_bbc_compressors;
2895 + while (l != NULL) {
2896 + for (i = 0; i < 1000; i++) {
2897 + if (l->name[i] == 0) {
2898 + jffs2_bbc_set_manual_compressor(l);
2901 + else if (name[i] == 0)
2903 + else if (name[i] != l->name[i])
2908 + jffs2_bbc_set_manual_compressor(NULL);
2912 +static struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_by_name(char *name)
2914 + struct jffs2_bbc_compressor_type *l;
2917 +#ifndef JFFS2_BBC_STANDALONE
2918 + l = jffs2_bbc_original_compressor;
2919 + for (i = 0; i < 1000; i++) {
2920 + if (l->name[i] == 0) {
2923 + else if (name[i] == 0)
2925 + else if (name[i] != l->name[i])
2930 + l = jffs2_bbc_compressors;
2931 + while (l != NULL) {
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])
2947 +int jffs2_bbc_disable_compressor_by_name(char *name)
2949 + struct jffs2_bbc_compressor_type *l;
2951 + l = jffs2_bbc_get_compressor_by_name(name);
2952 + if (l == NULL) return 1;
2957 +int jffs2_bbc_enable_compressor_by_name(char *name)
2959 + struct jffs2_bbc_compressor_type *l;
2961 + l = jffs2_bbc_get_compressor_by_name(name);
2962 + if (l == NULL) return 1;
2967 +void jffs2_bbc_compressor_command_by_name(char *name_and_command)
2969 + struct jffs2_bbc_compressor_type *l;
2972 + l = jffs2_bbc_compressors;
2973 + while (l != NULL) {
2974 + for (i = 0; i < 1000; i++) {
2975 + if (l->name[i] == 0) {
2976 + if (name_and_command[i] != ':') {
2977 + jffs2_bbc_print1("jffs2.bbc: ':' missing after compressor name\n");
2980 + if (l->proc_command != NULL)
2981 + l->proc_command(name_and_command + i + 1);
2986 + else if (name_and_command[i] == 0) {
2989 + else if (name_and_command[i] != l->name[i]) {
2997 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_manual_compressor(void)
2999 + if (jffs2_bbc_get_compression_mode() != JFFS2_BBC_MANUAL_MODE) {
3000 + jffs2_bbc_manual_compressor = NULL;
3002 + return jffs2_bbc_manual_compressor;
3005 +/*********************************************************************
3006 + * Compressor handling *
3007 + *********************************************************************/
3009 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_list(void)
3011 + return jffs2_bbc_compressors;
3014 +struct jffs2_bbc_model_list_node *jffs2_bbc_get_model_list(void)
3016 + return jffs2_bbc_model_list;
3019 +int jffs2_bbc_register_compressor(struct jffs2_bbc_compressor_type *c)
3021 + struct jffs2_bbc_compressor_type *l;
3022 + struct jffs2_bbc_model_list_node *l2;
3023 + int model_found = 0;
3025 + l = jffs2_bbc_compressors;
3026 + /* Check for confilcts */
3027 + while (l != NULL) {
3029 + /*if (strcmp(c->name,l->name)==0) {
3030 + jffs2_bbc_print1("jffs2.bbc: compressor is already loaded.");
3033 + if ((l->model_file_sign == c->model_file_sign) && (c->model_file_sign != 0)) {
3034 + jffs2_bbc_print1("jffs2.bbc: already used model file sign. fail.");
3039 + /* Search and initialize model */
3042 + if (c->init != NULL) {
3043 + if (c->init() != 0) {
3044 + jffs2_bbc_print2("jffs2.bbc: cannot initialize compressor %s.\n", c->name);
3048 + if (c->model_file_sign != 0) {
3049 + l2 = jffs2_bbc_model_list;
3053 + if (c->model_file_sign == l2->sign) {
3054 + if (l2->compressor != NULL) {
3055 + jffs2_bbc_print2("jffs2.bbc: register for %s: BUG, model file already reserved!!!!\n", c->name);
3058 + if (c->init_model(&(l2->model)) != 0) {
3059 + jffs2_bbc_print2("jffs2.bbc: cannot initialize compressor %s for a model", c->name);
3062 + l2->compressor = c;
3063 + l2->next_compr_model = c->models;
3070 + l2 = l2->next_model;
3072 + /*if (model_found==0) {
3073 + jffs2_bbc_print2("jffs2.bbc: no macthing model file found for %s at this time (maybe later)\n",c->name);
3076 + /* Insert to the end of the compressor list */
3079 + c->buffer_size = 0;
3080 + c->stat_compr_orig = c->stat_compr_new = c->stat_decompr = 0;
3082 + if (jffs2_bbc_compressors == NULL) {
3083 + jffs2_bbc_compressors = c;
3086 + l = jffs2_bbc_compressors;
3087 + while (l->next != NULL)
3094 +int jffs2_bbc_unregister_compressor(struct jffs2_bbc_compressor_type *c)
3096 + struct jffs2_bbc_compressor_type *l;
3097 + struct jffs2_bbc_model_list_node *l2;
3099 + if (c->mounted != 0) {
3100 + jffs2_bbc_print1("jffs2.bbc: Compressor is in use. Sorry.");
3103 + if (jffs2_bbc_compressors == NULL) {
3104 + jffs2_bbc_print1("jffs2.bbc: unregister: empty list.");
3107 + else if (jffs2_bbc_compressors == c) {
3108 + if (c->deinit != NULL)
3110 + jffs2_bbc_compressors = c->next;
3113 + l = jffs2_bbc_compressors;
3114 + while (l->next != c) {
3115 + if (l->next == NULL) {
3116 + jffs2_bbc_print2("jffs2.bbc: unregister: cannot find compressor %s in the list.", c->name);
3121 + if (c->deinit != NULL)
3123 + l->next = c->next;
3125 + if (c->buffer != NULL) {
3126 + jffs2_bbc_free(c->buffer);
3128 + c->buffer_size = 0;
3131 + l2 = jffs2_bbc_model_list;
3132 + while (l2 != NULL) {
3133 + if (l2->compressor == c) {
3134 + jffs2_bbc_print1("jffs2.bbc: unregister: BUG: model found!!!");
3135 + l2->compressor = NULL;
3136 + l2->next_compr_model = NULL;
3138 + l2 = l2->next_model;
3144 +int jffs2_bbc_model_new(void *sb, int i_num, void *model)
3146 + struct jffs2_bbc_model_list_node *node;
3147 + struct jffs2_bbc_compressor_type *l;
3148 + char block_sign[2];
3152 + /* check for conflicts... */
3153 + sign = *((int *) model);
3154 + block_sign[0] = *(((char *) model) + 4);
3155 + block_sign[1] = *(((char *) model) + 5);
3156 + node = jffs2_bbc_model_list;
3157 + while (node != NULL) {
3158 + if ((node->block_sign[0] == block_sign[0]) && (node->block_sign[1] == block_sign[1]) && (node->sb == sb)) {
3159 + //jffs2_bbc_print2("jffs2.bbc: model_new: model conflict (inode=%d)!\n",i_num);
3162 + node = node->next_model;
3166 + node = jffs2_bbc_malloc_small((long)sizeof(struct jffs2_bbc_model_list_node));
3168 + node->model = model;
3169 + node->sign = *((int *) model);
3170 + node->block_sign[0] = *(((char *) model) + 4);
3171 + node->block_sign[1] = *(((char *) model) + 5);
3172 + node->inode = i_num;
3173 + node->next_model = jffs2_bbc_model_list;
3174 + node->compressor = NULL;
3175 + node->stat_decompr = 0;
3176 + node->next_compr_model = NULL;
3177 + jffs2_bbc_model_list = node;
3179 + /* search for matching compressor */
3180 + l = jffs2_bbc_compressors;
3181 + while (l != NULL) {
3182 + if (l->model_file_sign == sign) {
3183 + //jffs2_bbc_print2("jffs2.bbc: compressor for model found: %s ",l->name);
3184 + if (l->init_model(&(node->model)) != 0) {
3185 + jffs2_bbc_print1("jffs2.bbc: cannot initialize compressor for a model");
3189 + node->compressor = l;
3190 + node->next_compr_model = l->models;
3200 +static void jffs2_bbc_model_del_from_compressor(struct jffs2_bbc_model_list_node *node)
3202 + struct jffs2_bbc_model_list_node *l;
3204 + if (node->model != NULL) {
3205 + if (node->compressor != NULL) {
3206 + if (node->compressor->destroy_model == NULL) {
3207 + jffs2_bbc_free(node->model);
3208 + node->model = NULL;
3211 + node->compressor->destroy_model(&(node->model));
3212 + if (node->model != NULL)
3213 + jffs2_bbc_print1("jffs2.bbc: warning: not NULL model after destroying!\n");
3218 + if (node->compressor == NULL) {
3219 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor: no compressor!\n");
3222 + l = node->compressor->models;
3224 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor error, models=NULL!\n");
3228 + node->compressor->models = node->next_compr_model;
3229 + node->compressor->mounted--;
3233 + if (l->next_compr_model == node) {
3234 + l->next_compr_model = node->next_compr_model;
3235 + node->compressor->mounted--;
3238 + l = l->next_compr_model;
3240 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor: not found\n");
3246 +void jffs2_bbc_model_del(void *sb)
3248 + struct jffs2_bbc_model_list_node *l, *l2;
3250 + l = jffs2_bbc_model_list;
3253 + if (l->sb == sb) {
3254 + jffs2_bbc_model_list = l->next_model;
3255 + jffs2_bbc_model_del_from_compressor(l);
3256 + jffs2_bbc_free_small(l);
3257 + jffs2_bbc_model_del(sb);
3261 + if (l->next_model == NULL) {
3264 + if (l->next_model->sb == sb) {
3265 + l2 = l->next_model;
3266 + l->next_model = l->next_model->next_model;
3267 + jffs2_bbc_model_del_from_compressor(l2);
3268 + jffs2_bbc_free_small(l2);
3269 + jffs2_bbc_model_del(sb);
3272 + l = l->next_model;
3277 +void jffs2_bbc_model_set_act_sb(void *sb)
3282 +void *jffs2_bbc_model_get_act_sb(void)
3287 +void *jffs2_bbc_model_get_newest(struct jffs2_bbc_compressor_type *compressor)
3289 + struct jffs2_bbc_model_list_node *m, *best_m;
3290 + int max_sign, sign;
3292 + if (compressor == NULL) {
3293 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_get: NULL!!\n");
3299 + m = compressor->models;
3300 + while (m != NULL) {
3301 + if (m->sb == last_sb) {
3302 + sign = (int) (m->block_sign[0]) * 256 + (int) (m->block_sign[1]);
3303 + if (sign > max_sign) {
3308 + m = m->next_compr_model;
3310 + if (best_m != NULL)
3311 + return best_m->model;
3316 +/*********************************************************************
3318 + *********************************************************************/
3320 +static char *jffs2_bbc_stat_buff = NULL;
3322 +char *jffs2_bbc_get_model_stats(void)
3325 + struct jffs2_bbc_model_list_node *m;
3326 + struct jffs2_bbc_compressor_type *c;
3328 + if (jffs2_bbc_stat_buff == NULL)
3329 + jffs2_bbc_stat_buff = jffs2_bbc_malloc(8000);
3331 + b = jffs2_bbc_stat_buff;
3333 + b += sprintf(b, "Loaded compressors:");
3334 + c = jffs2_bbc_compressors;
3335 + while (c != NULL) {
3336 + b += sprintf(b, "\n %s (%d) ", c->name, c->enabled);
3337 + if (c->model_file_sign != 0) {
3338 + b += sprintf(b, "m_sign=%d ", c->model_file_sign);
3339 + b += sprintf(b, "models=");
3341 + while (m != NULL) {
3342 + b += sprintf(b, "(inode=%d)", m->inode);
3343 + m = m->next_compr_model;
3347 + b += sprintf(b, "b_sign=(%d,%d) nomodel", (int) (c->block_sign[0]), (int) (c->block_sign[1]));
3349 + if (c->proc_info != NULL) {
3350 + b += sprintf(b, "\n %s", c->proc_info());
3355 + m = jffs2_bbc_model_list;
3358 + b += sprintf(b, "\nPresent models: NONE\n");
3361 + b += sprintf(b, "\nPresent models:\n");
3362 + while (m != NULL) {
3363 + 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);
3364 + if (m->compressor == NULL)
3365 + b += sprintf(b, ",compressor=NULL\n");
3367 + b += sprintf(b, ",compressor=%s\n", m->compressor->name);
3368 + m = m->next_model;
3372 + return jffs2_bbc_stat_buff;
3375 +/*********************************************************************
3376 + * Memory handling, debug *
3377 + *********************************************************************/
3379 +static int jffs2_bbc_mem_counter = 0;
3383 +void *jffs2_bbc_malloc(long size)
3385 + void *addr = vmalloc(size);
3387 + jffs2_bbc_mem_counter++;
3389 + jffs2_bbc_print2("DEBUG: not enough memory (%ld)\n", size);
3394 +void jffs2_bbc_free(void *addr)
3396 + jffs2_bbc_mem_counter--;
3400 +void *jffs2_bbc_malloc_small(long size)
3403 + addr = kmalloc(size, 0);
3405 + jffs2_bbc_mem_counter++;
3409 +void jffs2_bbc_free_small(void *addr)
3411 + jffs2_bbc_mem_counter--;
3417 +void *jffs2_bbc_malloc(long size)
3419 + void *addr = malloc(size);
3421 + jffs2_bbc_mem_counter++;
3425 +void jffs2_bbc_free(void *addr)
3427 + jffs2_bbc_mem_counter--;
3431 +void *jffs2_bbc_malloc_small(long size)
3433 + return jffs2_bbc_malloc(size);
3436 +void jffs2_bbc_free_small(void *addr)
3438 + jffs2_bbc_free(addr);
3443 +int jffs2_bbc_test_memory_counter(int verbose)
3445 + if (verbose > 0) {
3446 + jffs2_bbc_print2("jffs2.bbc: mem_counter=%d!\n", jffs2_bbc_mem_counter);
3448 + return jffs2_bbc_mem_counter;
3451 +int jffs2_bbc_get_memory_counter(void)
3453 + return jffs2_bbc_mem_counter;
3456 +static char mem_stat[200];
3458 +char *jffs2_bbc_get_mem_stats(void)
3460 + sprintf(mem_stat, "Memcounter=%d\n", jffs2_bbc_mem_counter);
3464 +void jffs2_bbc_print_flush(void)
3474 +/*********************************************************************
3475 + * FRAMEWORK - ZLIB REPLACEMENT *
3476 + *********************************************************************/
3478 +#ifndef JFFS2_BBC_STANDALONE
3480 +/* Temorary buffers */
3481 +static char stat_str[JFFS2_BBC_STAT_BUFF_SIZE];
3482 +static int tmp_buffer_size = 0;
3483 +static char *tmp_buffer = NULL;
3485 +/* Statistic - used by /proc/jffs2_bbc and mkfs.jffs2 */
3486 +char *jffs2_bbc_get_compr_stats(void)
3488 + struct jffs2_bbc_compressor_type *l;
3489 + char *s = stat_str;
3491 + s += sprintf(s, "Compression statistics:\n");
3492 + l = jffs2_bbc_original_compressor;
3493 + //s += sprintf(s, " zlib: compr=%d/%d decompr=%d\n", stat_zlib_compr_new, stat_zlib_compr_orig, stat_zlib_decompr);
3494 + s += sprintf(s, " %s: compr=%d/%d decompr=%d\n", l->name, l->stat_compr_new, l->stat_compr_orig, l->stat_decompr);
3495 + l = jffs2_bbc_get_compressor_list();
3496 + while (l != NULL) {
3497 + s += sprintf(s, " %s: compr=%d/%d decompr=%d\n", l->name, l->stat_compr_new, l->stat_compr_orig, l->stat_decompr);
3503 +static void jffs2_bbc_buffer_fill(unsigned char *buff, int size)
3505 + for (; size > 0; size--, buff++)
3510 +static int jffs2_bbc_update_compr_buf(unsigned long size)
3512 + struct jffs2_bbc_compressor_type *l;
3516 + if (tmp_buffer == NULL) {
3517 + tmp_buffer = jffs2_bbc_malloc(size);
3518 + jffs2_bbc_buffer_fill(tmp_buffer, size);
3519 + tmp_buffer_size = size;
3521 + else if (tmp_buffer_size < size) {
3522 + jffs2_bbc_free(tmp_buffer);
3523 + tmp_buffer = jffs2_bbc_malloc(size);
3524 + jffs2_bbc_buffer_fill(tmp_buffer, size);
3525 + tmp_buffer_size = size;
3527 + l = jffs2_bbc_get_compressor_list();
3528 + while (l != NULL) {
3529 + if (l->buffer == NULL) {
3530 + l->buffer_size = size;
3531 + l->buffer = jffs2_bbc_malloc(size);
3532 + jffs2_bbc_buffer_fill(l->buffer, size);
3534 + else if (l->buffer_size < size) {
3535 + jffs2_bbc_free(l->buffer);
3536 + l->buffer_size = size;
3537 + l->buffer = jffs2_bbc_malloc(size);
3538 + jffs2_bbc_buffer_fill(l->buffer, size);
3545 +#ifdef DEBUG_COMPRESSORS
3547 +static unsigned char *debug_tmp_buff = NULL;
3548 +static long debug_orig_srclen = -1;
3549 +static long debug_orig_dstlen = -1;
3550 +static int debug_mem_counter = -1;
3553 +void debug_before_compress(struct jffs2_bbc_compressor_type *c, void *model, unsigned char *input, unsigned char *output, long *sourcelen, long *dstlen)
3556 + debug_orig_srclen = *sourcelen; // for buffer overflow test
3557 + debug_orig_dstlen = *dstlen; // for buffer overflow test
3558 + output[debug_orig_dstlen + 1] = 255;
3560 + debug_mem_counter = jffs2_bbc_get_memory_counter(); // for memory guard
3563 +void debug_after_compress(struct jffs2_bbc_compressor_type *c, int back, void *model, unsigned char *input, unsigned char *output, long *sourcelen, long *dstlen)
3565 + long dst_len = *dstlen;
3566 + long src_len = *sourcelen;
3570 + if (debug_mem_counter != jffs2_bbc_get_memory_counter()) {
3571 + jffs2_bbc_print4("!!!!!!!! %s error: possible COMPRESSOR MEMORY LEAK: %d->%d\n", c->name, debug_mem_counter, jffs2_bbc_get_memory_counter());
3572 + debug_mem_counter = jffs2_bbc_get_memory_counter();
3575 + // Buffer overflow test
3576 + if (output[debug_orig_dstlen + 1] != 255) {
3577 + 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);
3580 + // Decompression check
3582 + if (debug_tmp_buff == NULL)
3583 + debug_tmp_buff = jffs2_bbc_malloc(17000);
3584 + for (i = 0; i < src_len; i++) debug_tmp_buff[i] = 0xf6;
3585 + c->decompress(model, output, debug_tmp_buff, dst_len, src_len);
3586 + // Memory guard for decompressor
3587 + if (debug_mem_counter != jffs2_bbc_get_memory_counter()) {
3588 + jffs2_bbc_print4("!!!!!!!! %s error: possible DECOMPRESSOR MEMORY LEAK: %d->%d\n", c->name, debug_mem_counter, jffs2_bbc_get_memory_counter());
3589 + debug_mem_counter = jffs2_bbc_get_memory_counter();
3592 + for (i = 0; i < src_len; i++)
3593 + if (input[i] != debug_tmp_buff[i]) {
3594 + 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);
3600 + // Return value test
3601 + //jffs2_bbc_print3("!!!!!!!! %s error: %d !!!!!!!!!!!!\n", c->name, back);
3606 +int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, uint32_t * sourcelen, uint32_t * dstlen)
3608 + struct jffs2_bbc_compressor_type *c;
3609 + int back, back_zlib, mode, min, i, i2;
3610 + long tmp = 0, tmp_read_time = 1000, tmp_write_time = 1000, orig_src, orig_dest, src, dest;
3611 + struct jffs2_bbc_model_list_node *m;
3613 + unsigned char *tmp_p = NULL;
3615 + sb = jffs2_bbc_model_get_act_sb();
3617 + orig_src = *sourcelen;
3618 + orig_dest = *dstlen;
3620 + mode = jffs2_bbc_get_compression_mode();
3622 + if (mode == JFFS2_BBC_DUMMY_MODE) {
3625 + cpage_out[i++]=JFFS2_BBC_DUMMY_BLOCKSIGN_0;
3626 + cpage_out[i++]=JFFS2_BBC_DUMMY_BLOCKSIGN_1;
3629 + for (;((i < *dstlen) && (i < (*sourcelen)+i2));i++) {
3630 + cpage_out[i] = data_in[i-i2];
3637 + if (mode == JFFS2_BBC_ZLIB_MODE) {
3638 + /*if (!jffs2_bbc_original_compressor->enabled) {
3639 + jffs2_bbc_print2("jffs2.bbc: WARNING: ZLIB mode but %s disabled! Enabling for this procedure...\n",jffs2_bbc_original_compressor->name);
3641 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3642 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3643 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3647 + jffs2_bbc_update_compr_buf(orig_dest);
3649 + if (mode == JFFS2_BBC_SIZE_MODE) {
3650 + // Testing all compressors
3651 + if (!jffs2_bbc_original_compressor->enabled) {
3655 + back_zlib = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3658 + c = jffs2_bbc_get_compressor_list();
3659 + while (c != NULL) {
3660 + c->buffer_cnt = -1;
3661 + if (c->enabled == 0) {
3665 + if (c->model_file_sign == 0) {
3668 +#ifdef DEBUG_COMPRESSORS
3669 + debug_before_compress(c, NULL, data_in, c->buffer, &src, &dest);
3671 + back = c->compress(NULL, data_in, c->buffer, &src, &dest);
3672 +#ifdef DEBUG_COMPRESSORS
3673 + debug_after_compress(c, back, NULL, data_in, c->buffer, &src, &dest);
3676 + c->buffer_cnt = dest;
3677 + if ((min < 0) || (min > dest))
3683 + while (m != NULL) {
3686 + if (m->sb == sb) {
3687 + if (c->buffer_cnt == -1) {
3688 +#ifdef DEBUG_COMPRESSORS
3689 + debug_before_compress(c, m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3691 + back = c->compress(m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3692 +#ifdef DEBUG_COMPRESSORS
3693 + debug_after_compress(c, back, m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3696 + c->buffer_cnt = dest;
3697 + if ((min < 0) || (min > dest))
3702 +#ifdef DEBUG_COMPRESSORS
3703 + debug_before_compress(c, m->model, data_in, tmp_buffer, &src, &dest);
3705 + back = c->compress(m->model, data_in, tmp_buffer, &src, &dest);
3706 +#ifdef DEBUG_COMPRESSORS
3707 + debug_after_compress(c, back, m->model, data_in, tmp_buffer, &src, &dest);
3710 + if (c->buffer_cnt > dest) {
3711 + c->buffer_cnt = dest;
3712 + tmp_p = c->buffer;
3713 + c->buffer = tmp_buffer;
3714 + tmp_buffer = tmp_p;
3715 + if ((min < 0) || (min > dest))
3721 + m = m->next_compr_model;
3726 + //Finding the best and copy its result
3728 +#ifdef DEBUG_SHOW_BLOCK_SIZES
3729 + jffs2_bbc_print1("\n");
3730 + if (jffs2_bbc_original_compressor->enabled) {
3731 + if (min == *dstlen) {
3732 + jffs2_bbc_print3("%s:%d* ", jffs2_bbc_original_compressor->name, (int) (*dstlen));
3735 + jffs2_bbc_print3("%s:%d ", jffs2_bbc_original_compressor->name, (int) (*dstlen));
3738 + c = jffs2_bbc_get_compressor_list();
3739 + while (c != NULL) {
3740 + if (c->enabled == 0) {
3744 + if (c->buffer_cnt == min)
3745 + jffs2_bbc_print3("%s:%d* ", c->name, c->buffer_cnt);
3747 + jffs2_bbc_print3("%s:%d ", c->name, c->buffer_cnt);
3753 + return -1; // none of compressors work (maybe too short output buffer)
3756 + if (jffs2_bbc_original_compressor->enabled) {
3757 + if (min == *dstlen) {
3758 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3759 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3764 + c = jffs2_bbc_get_compressor_list();
3765 + while (c != NULL) {
3766 + if (c->enabled == 0) {
3770 + if (c->buffer_cnt == min) {
3771 + *dstlen = c->buffer_cnt;
3772 + *sourcelen = orig_src;
3773 + for (i = 0; i < *dstlen; i++) {
3774 + cpage_out[i] = c->buffer[i];
3776 + c->stat_compr_orig += *sourcelen;
3777 + c->stat_compr_new += *dstlen;
3782 + jffs2_bbc_print1("jffs2.bbc: compr (full): BUG!!!\n");
3786 + if ((mode == JFFS2_BBC_FASTR_MODE)||(mode == JFFS2_BBC_FASTW_MODE)||(mode == JFFS2_BBC_FASTS_MODE)) {
3787 + // Estimating all compressors
3788 + if (jffs2_bbc_original_compressor->enabled) {
3789 + back = jffs2_bbc_original_compressor->estimate(NULL, data_in, *sourcelen, &tmp, &tmp_read_time, &tmp_write_time);
3793 + tmp_read_time = -1;
3794 + tmp_write_time = -1;
3796 + if (mode == JFFS2_BBC_FASTR_MODE) tmp = tmp_read_time;
3797 + if (mode == JFFS2_BBC_FASTW_MODE) tmp = tmp_write_time;
3799 + c = jffs2_bbc_get_compressor_list();
3800 + while (c != NULL) {
3803 + c->buffer_cnt = -1;
3804 + if (c->enabled == 0) {
3808 + if ((c->model_file_sign == 0) || (jffs2_bbc_model_get_newest(c) != NULL)) {
3809 + back = c->estimate(jffs2_bbc_model_get_newest(c), data_in, src, &dest, &tmp_read_time, &tmp_write_time);
3810 + if (mode == JFFS2_BBC_FASTR_MODE) dest = tmp_read_time;
3811 + if (mode == JFFS2_BBC_FASTW_MODE) dest = tmp_write_time;
3813 + c->buffer_cnt = dest;
3814 + if ((min < 0) || (min > dest))
3818 + c->buffer_cnt = -1;
3823 + // Finding the best and compress with it
3827 + if (jffs2_bbc_original_compressor->enabled) {
3829 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3830 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3831 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3835 + c = jffs2_bbc_get_compressor_list();
3836 + while (c != NULL) {
3837 + if (c->enabled == 0) {
3841 + if (c->buffer_cnt == min) {
3842 + back = c->compress(jffs2_bbc_model_get_newest(c), data_in, cpage_out, (unsigned long*)sourcelen, (unsigned long*)dstlen);
3843 + if ((back == 0) && (*dstlen < orig_dest) && (*dstlen > 4)) {
3844 + c->stat_compr_orig += *sourcelen;
3845 + c->stat_compr_new += *dstlen;
3847 + else { // fallback will always be available
3848 + *sourcelen = orig_src;
3849 + *dstlen = orig_dest;
3850 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3851 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3852 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3859 + jffs2_bbc_print1("jffs2.bbc: compress (fastX mode): BUG!!!\n");
3863 + if (mode == JFFS2_BBC_MANUAL_MODE) {
3864 + c = jffs2_bbc_get_manual_compressor();
3866 + if (c->model_file_sign == 0) {
3869 + back = c->compress(NULL, data_in, cpage_out, &src, &dest);
3873 + c->stat_compr_orig += *sourcelen;
3874 + c->stat_compr_new += *dstlen;
3879 + c->buffer_cnt = -1;
3882 + while (m != NULL) {
3885 + if (m->sb == sb) {
3887 + back = c->compress(m->model, data_in, cpage_out, (unsigned long*)sourcelen, (unsigned long*)dstlen);
3888 + if ((back == 0) && (*dstlen < orig_dest) && (*dstlen > 4)) {
3890 + tmp_p = cpage_out;
3894 + back = c->compress(m->model, data_in, tmp_buffer, &src, &dest);
3895 + if ((back == 0) && (dest < orig_dest) && (dest > 4)) {
3896 + if (c->buffer_cnt > dest) {
3899 + tmp_p = tmp_buffer;
3905 + m = m->next_compr_model;
3908 + if (tmp_p != cpage_out) {
3909 + for (i = 0; i < min; i++)
3910 + cpage_out[i] = tmp_p[i];
3911 + *sourcelen = orig_src;
3914 + c->stat_compr_orig += *sourcelen;
3915 + c->stat_compr_new += *dstlen;
3921 + jffs2_bbc_print1("iPack: manual mode without selected compressor!\n");
3924 + /*if (!jffs2_bbc_original_compressor->enabled) {
3925 + jffs2_bbc_print2("jffs2.bbc: WARNING: %s must be enabled! Enabling for this procedure...\n",jffs2_bbc_original_compressor->name);
3927 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3928 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3929 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3935 + jffs2_bbc_print1("jffs2.bbc: compress: unimlemented compress mode!!!\n");
3939 +void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen)
3941 + struct jffs2_bbc_model_list_node *m;
3942 + struct jffs2_bbc_compressor_type *c;
3947 + /* If the input too small... */
3949 + cpage_out[0]=data_in[0];
3950 + if (destlen==2) cpage_out[1]=data_in[1];
3954 + sb = jffs2_bbc_model_get_act_sb();
3955 + d[0] = *(data_in);
3956 + d[1] = *(data_in + 1);
3958 + d[0] &= 0x7f; // Variants support...
3960 + /* Search for model based decompressors... */
3961 + m = jffs2_bbc_get_model_list();
3962 + while (m != NULL) {
3963 + if ((d[0] == m->block_sign[0]) && (d[1] == m->block_sign[1]) && (sb == m->sb)) {
3964 + if (m->compressor == NULL) {
3965 + jffs2_bbc_print3("jffs2.bbc: decompressor for block_sign (%d,%d) not loaded!\n", (int) (d[0]), (int) (d[1]));
3968 + m->compressor->decompress(m->model, data_in, cpage_out, srclen, destlen);
3969 + m->compressor->stat_decompr++;
3970 + m->stat_decompr++;
3974 + m = m->next_model;
3977 + if ((((int) d[0]) == (int)(jffs2_bbc_original_compressor->block_sign[0])) && (((int) d[1]) == (int)(jffs2_bbc_original_compressor->block_sign[1]))) {
3978 + jffs2_bbc_original_compressor->decompress(NULL, data_in, cpage_out, srclen, destlen);
3979 + jffs2_bbc_original_compressor->stat_decompr++;
3982 + /* Search for non model based decompressors... */
3983 + c = jffs2_bbc_get_compressor_list();
3984 + while (c != NULL) {
3985 + if (c->model_file_sign == 0) {
3986 + if (((int) (d[0]) == (int) (c->block_sign[0])) && ((int) (d[1]) == (int) (c->block_sign[1]))) {
3987 + c->decompress(NULL, data_in, cpage_out, srclen, destlen);
3988 + c->stat_decompr++;
3994 + /* Is it DUMMY? */
3995 + if ((((int) d[0]) == JFFS2_BBC_DUMMY_BLOCKSIGN_0) && (((int) d[1]) == JFFS2_BBC_DUMMY_BLOCKSIGN_1)) {
3996 + for (i=0;i<destlen;i++) {
3997 + cpage_out[i]=data_in[i+2];
4001 + /* No matching decompressor found... */
4002 + 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);
4003 + jffs2_bbc_original_compressor->decompress(NULL, data_in, cpage_out, srclen, destlen);
4004 + jffs2_bbc_original_compressor->stat_decompr++;
4008 diff -Nur linux-mips-cvs/fs/jffs2/jffs2_bbc_framework.h linux-mips/fs/jffs2/jffs2_bbc_framework.h
4009 --- linux-mips-cvs/fs/jffs2/jffs2_bbc_framework.h 1970-01-01 01:00:00.000000000 +0100
4010 +++ linux-mips/fs/jffs2/jffs2_bbc_framework.h 2005-02-07 05:08:34.000000000 +0100
4013 + * JFFS2-BBC: Compression Framework - headers
4017 + * Copyright (C) 2004, Ferenc Havasi
4019 + * This program is free software; you can redistribute it and/or
4020 + * modify it under the terms of the GNU General Public License
4021 + * as published by the Free Software Foundation; either version 2
4022 + * of the License, or (at your option) any later version.
4024 + * This program is distributed in the hope that it will be useful,
4025 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4026 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4027 + * GNU General Public License for more details.
4029 + * You should have received a copy of the GNU General Public License
4030 + * along with this program; if not, write to the Free Software
4031 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4035 +#ifndef __JFFS2_BBC_FRAMEWORK_H__
4037 +#define __JFFS2_BBC_FRAMEWORK_H__
4039 +#define JFFS2_BBC_VERSION "0.54.3"
4041 +#define JFFS2_BBC_CONFIG_FILE "bbc.conf"
4043 +/*********************************************************************
4044 + * Compression mode handling *
4045 + *********************************************************************/
4047 +#define JFFS2_BBC_ZLIB_MODE 1
4048 +#define JFFS2_BBC_SIZE_MODE 2
4049 +#define JFFS2_BBC_FASTR_MODE 3
4050 +#define JFFS2_BBC_FASTW_MODE 4
4051 +#define JFFS2_BBC_FASTS_MODE 5
4052 +#define JFFS2_BBC_MANUAL_MODE 6
4053 +#define JFFS2_BBC_DUMMY_MODE 7
4055 +int jffs2_bbc_get_compression_mode(void);
4056 +void jffs2_bbc_set_compression_mode(int mode);
4058 +/*********************************************************************
4059 + * Read/write speed unit *
4060 + * everything is relative to the speed of zlib *
4061 + * bigger number means slower speed! *
4062 + *********************************************************************/
4064 +#define JFFS2_BBC_ZLIB_READ_TIME 10000
4065 +#define JFFS2_BBC_ZLIB_WRITE_TIME 10000
4067 +/*********************************************************************
4068 + * Compressor handling *
4069 + *********************************************************************/
4071 +struct jffs2_bbc_compressor_type
4074 + int model_file_sign; /* 0 for no model file needed */
4075 + char block_sign[4]; /* only nomodel compressors, and only the first 2 _bytes are used! */
4076 + int (*init)(void);
4077 + int (*init_model)(void **model);
4078 + void (*destroy_model)(void **model);
4079 + void (*deinit)(void);
4081 + * *dstlen bytes are allocated.
4082 + * if it is not enough write *sourcelen over to the processed amount of data
4083 + * returns non zero if fails
4085 + int (*compress)(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen);
4086 + int (*estimate)(void *model, unsigned char *input, unsigned long sourcelen,
4087 + unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime);
4088 + /* Decompress block
4089 + * returns non zero if fails
4091 + int (*decompress)(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen);
4092 + char *(*proc_info)(void);
4093 + int (*proc_command)(char *command);
4094 + int enabled; /* filled by BBC */
4095 + int mounted; /* filled by BBC */
4096 + void *models; /* filled by BBC */
4097 + char *buffer; /* filled by BBC */
4098 + int buffer_size; /* filled by BBC */
4099 + int buffer_cnt; /* filled by BBC */
4100 + int buffer_tmp; /* filled by BBC */
4101 + int stat_compr_orig; /* filled by BBC */
4102 + int stat_compr_new; /* filled by BBC */
4103 + int stat_decompr; /* filled by BBC */
4104 + struct jffs2_bbc_compressor_type *next; /* filled by BBC */
4107 +/* It sets the compression mode to JFFS2_BBC_MANUAL_MODE */
4109 +void jffs2_bbc_set_manual_compressor(struct jffs2_bbc_compressor_type *c); /* NULL = ZLIB */
4110 +int jffs2_bbc_set_manual_compressor_by_name(char *name);
4111 +int jffs2_bbc_disable_compressor_by_name(char *name);
4112 +int jffs2_bbc_enable_compressor_by_name(char *name);
4113 +void jffs2_bbc_compressor_command_by_name(char *name_and_command);
4115 +/* If the compression mode is JFFS2_BCC_MANUAL_MODE the manually setted
4116 + compressor can be get using it. Otherwise it returns with NULL. */
4118 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_manual_compressor(void);
4120 +struct jffs2_bbc_model_list_node
4122 + void *sb; /* FS idendifier (JFFS2_SB_INFO(sb) at this moment) */
4123 + void *model; /* model data */
4124 + int sign; /* sign of the model (first 4 bytes) */
4125 + char block_sign[4]; /* block sign - only the first 2 bytes are used! */
4126 + int inode; /* inode number of the model file */
4128 + struct jffs2_bbc_compressor_type *compressor;
4129 + struct jffs2_bbc_model_list_node *next_model;
4130 + struct jffs2_bbc_model_list_node *next_compr_model;
4133 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_list(void);
4134 +struct jffs2_bbc_model_list_node *jffs2_bbc_get_model_list(void);
4136 +int jffs2_bbc_register_compressor(struct jffs2_bbc_compressor_type *c);
4137 +int jffs2_bbc_unregister_compressor(struct jffs2_bbc_compressor_type *c);
4139 +int jffs2_bbc_model_new(void *sb, int i_num, void *model);
4140 +void jffs2_bbc_model_del(void *sb);
4141 +void jffs2_bbc_model_set_act_sb(void *sb);
4142 +void *jffs2_bbc_model_get_act_sb(void);
4143 +void *jffs2_bbc_model_get_newest(struct jffs2_bbc_compressor_type *compressor);
4145 +/*********************************************************************
4146 + * Compressor init function *
4147 + *********************************************************************/
4149 +void jffs2_bbc_compressor_init(void);
4150 +void jffs2_bbc_compressor_deinit(void);
4152 +/*********************************************************************
4154 + *********************************************************************/
4156 +char *jffs2_bbc_get_compr_stats(void);
4157 +char *jffs2_bbc_get_model_stats(void);
4159 +/*********************************************************************
4161 + *********************************************************************/
4164 +void jffs2_bbc_print_flush(void);
4167 +#include <linux/kernel.h>
4168 +#define jffs2_bbc_print1(a) printk(a)
4169 +#define jffs2_bbc_print2(a,b) printk(a,b)
4170 +#define jffs2_bbc_print3(a,b,c) printk(a,b,c)
4171 +#define jffs2_bbc_print4(a,b,c,d) printk(a,b,c,d)
4172 +#define jffs2_bbc_print5(a,b,c,d,e) printk(a,b,c,d,e)
4173 +#define jffs2_bbc_print6(a,b,c,d,e,f) printk(a,b,c,d,e,f)
4174 +#define jffs2_bbc_print7(a,b,c,d,e,f,g) printk(a,b,c,d,e,f,g)
4175 +#define jffs2_bbc_print8(a,b,c,d,e,f,g,h) printk(a,b,c,d,e,f,g,h)
4176 +#define jffs2_bbc_print9(a,b,c,d,e,f,g,h,i) printk(a,b,c,d,e,f,g,h,i)
4179 +#define jffs2_bbc_print1(a) fprintf(stderr,a)
4180 +#define jffs2_bbc_print2(a,b) fprintf(stderr,a,b)
4181 +#define jffs2_bbc_print3(a,b,c) fprintf(stderr,a,b,c)
4182 +#define jffs2_bbc_print4(a,b,c,d) fprintf(stderr,a,b,c,d)
4183 +#define jffs2_bbc_print5(a,b,c,d,e) fprintf(stderr,a,b,c,d,e)
4184 +#define jffs2_bbc_print6(a,b,c,d,e,f) fprintf(stderr,a,b,c,d,e,f)
4185 +#define jffs2_bbc_print7(a,b,c,d,e,f,g) fprintf(stderr,a,b,c,d,e,f,g)
4186 +#define jffs2_bbc_print8(a,b,c,d,e,f,g,h) fprintf(stderr,a,b,c,d,e,f,g,h)
4187 +#define jffs2_bbc_print9(a,b,c,d,e,f,g,h,i) fprintf(stderr,a,b,c,d,e,f,g,h,i)
4190 +/* Handle endianness */
4193 +#define ENDIAN_HOST_AND_TARGET_SAME 0
4194 +#define ENDIAN_HOST_AND_TARGET_DIFFERENT 1
4196 +extern int jffs2_bbc_glb_endian_X;
4200 +/* Allocating more than one page (tip. 4096 byte) */
4201 +void *jffs2_bbc_malloc(long size);
4202 +void jffs2_bbc_free(void *addr);
4204 +/* Allocating less than one page (tip. 4096 byte) */
4205 +void *jffs2_bbc_malloc_small(long size);
4206 +void jffs2_bbc_free_small(void *addr);
4208 +/* Memory guarding */
4209 +int jffs2_bbc_test_memory_counter(int verbose);
4210 +char *jffs2_bbc_get_mem_stats(void);
4211 +int jffs2_bbc_get_memory_counter(void);
4214 diff -Nur linux-mips-cvs/fs/jffs2/jffs2_bbc_fs.c linux-mips/fs/jffs2/jffs2_bbc_fs.c
4215 --- linux-mips-cvs/fs/jffs2/jffs2_bbc_fs.c 1970-01-01 01:00:00.000000000 +0100
4216 +++ linux-mips/fs/jffs2/jffs2_bbc_fs.c 2005-02-07 05:08:34.000000000 +0100
4219 + * JFFS2-BBC: File System Extension for Linux Kernel
4223 + * Copyright (C) 2004, Ferenc Havasi
4225 + * This program is free software; you can redistribute it and/or
4226 + * modify it under the terms of the GNU General Public License
4227 + * as published by the Free Software Foundation; either version 2
4228 + * of the License, or (at your option) any later version.
4230 + * This program is distributed in the hope that it will be useful,
4231 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4232 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4233 + * GNU General Public License for more details.
4235 + * You should have received a copy of the GNU General Public License
4236 + * along with this program; if not, write to the Free Software
4237 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4241 +#include <linux/kernel.h>
4242 +#include <linux/fs.h>
4243 +#include <linux/jffs2.h>
4244 +#include <linux/proc_fs.h>
4245 +#include <linux/version.h>
4247 +#include "nodelist.h"
4249 +#include "jffs2_bbc_framework.h"
4251 +struct jffs2_bbc_fs_sb_list {
4252 + struct super_block *sb;
4253 + struct jffs2_bbc_fs_sb_list *next;
4256 +static struct jffs2_bbc_fs_sb_list *sb_list = NULL;
4258 +void jffs2_bbc_proc_init(void);
4259 +void jffs2_bbc_proc_deinit(void);
4261 +void jffs2_bbc_load_model(void *sb_par) {
4262 + struct jffs2_sb_info *c;
4263 + //struct jffs2_inode_info *f;
4264 + struct dentry *config_dentry,*model_dentry;
4265 + struct qstr config_name,model_name;
4266 + struct file *config_file,*model_file;
4267 + char *buff=NULL,*model_buff;
4268 + int config_size,model_size;
4270 + struct super_block *sb;
4271 + struct jffs2_bbc_fs_sb_list *sb_l;
4274 + sb_l = jffs2_bbc_malloc_small(sizeof(struct jffs2_bbc_fs_sb_list));
4276 + sb_l->next = sb_list;
4278 + config_name.name = JFFS2_BBC_CONFIG_FILE;
4279 + config_name.len = strlen(config_name.name);
4280 + config_name.hash = full_name_hash(config_name.name,config_name.len);
4281 + config_dentry = d_alloc(sb->s_root,&config_name);
4282 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4283 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,config_dentry);
4285 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,config_dentry,NULL);
4288 + if (config_dentry->d_inode != NULL) {
4289 + config_size = config_dentry->d_inode->i_size;
4290 + //printk("config_file_size=%d\n",config_size);
4291 + if (config_size > 0) {
4292 + buff = jffs2_bbc_malloc(config_size+1);
4293 + config_file = dentry_open(config_dentry,NULL,O_RDONLY);
4294 + kernel_read(config_file,0,buff,config_size);
4295 + buff[config_size] = 0;
4296 + for (prev_i = i = 0 ; i < config_size+1 ; i++) {
4297 + if (buff[i] == '\n') buff[i]=0;
4298 + if (buff[i] == 0) {
4299 + if (prev_i != i) {
4300 + if ((buff[prev_i] == '-') && (buff[prev_i+1] == 0)) break;
4301 + printk("reading model file %s... ",buff+prev_i);
4302 + model_name.name = buff+prev_i;
4303 + model_name.len = strlen(buff+prev_i);
4304 + model_name.hash = full_name_hash(model_name.name,model_name.len);
4305 + model_dentry = d_alloc(sb->s_root,&model_name);
4306 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4307 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,model_dentry);
4309 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,model_dentry,NULL);
4311 + if (model_dentry->d_inode != NULL) {
4312 + c = JFFS2_SB_INFO(model_dentry->d_inode->i_sb);
4313 + //f = JFFS2_INODE_INFO(model_dentry->d_inode);
4314 + model_size = model_dentry->d_inode->i_size;
4315 + model_buff = jffs2_bbc_malloc(model_size);
4316 + model_file = dentry_open(model_dentry,NULL,O_RDONLY);
4317 + kernel_read(model_file,0,model_buff,model_size);
4318 + if (jffs2_bbc_model_new(c,model_dentry->d_inode->i_ino,model_buff) != 0) {
4319 + printk("already loaded.\n");
4320 + jffs2_bbc_free(model_buff);
4323 + printk("done (%d bytes readed from inode %d).\n",model_size,(int)(model_dentry->d_inode->i_ino));
4327 + printk("not found.\n");
4329 + dput(model_dentry);
4336 + dput(config_dentry);
4337 + if (buff != NULL) jffs2_bbc_free(buff);
4340 +void jffs2_bbc_unload_model(void *sb_par)
4342 + struct jffs2_sb_info *c;
4343 + struct super_block *sb = sb_par;
4344 + struct jffs2_bbc_fs_sb_list *sb_l,*sb_l2;
4347 + c = JFFS2_SB_INFO(sb);
4348 + jffs2_bbc_model_del(c);
4349 + if (sb_list == NULL) printk("jffs2.bbc: error! NULL sb list!\n");
4351 + if (sb_list->sb == sb) {
4352 + jffs2_bbc_free_small(sb_list);
4358 + while (sb_l->next != NULL) {
4359 + if (sb_l->next->sb == sb) {
4360 + sb_l2 = sb_l->next->next;
4361 + jffs2_bbc_free_small(sb_l->next);
4362 + sb_l->next = sb_l2;
4365 + sb_l = sb_l->next;
4370 + printk("jffs2.bbc: cannot delete sb from sblist!\n");
4375 +static int jffs2_bbc_get_mounted(void) {
4376 + struct jffs2_bbc_fs_sb_list *sb_l;
4380 + while (sb_l != NULL) {
4382 + sb_l = sb_l->next;
4388 +int jffs2_bbc_proc_read(char *buf, char **start, off_t offset, int count, int *eof, void *data)
4390 + int len = 0, mode;
4392 + mode = jffs2_bbc_get_compression_mode();
4393 + len += sprintf(buf + len, "BBC version: %s\n", JFFS2_BBC_VERSION);
4394 + len += sprintf(buf+len,"Mounted jffs2 filesystems: %d\n",jffs2_bbc_get_mounted());
4395 + //len += sprintf(buf+len,"actual model file inode: %d\n",jffs2_bbc_model_get_inum());
4396 + len += sprintf(buf + len, "Compression mode: ");
4397 + if (mode == JFFS2_BBC_ZLIB_MODE)
4398 + len += sprintf(buf + len, "ZLIB mode");
4399 + else if (mode == JFFS2_BBC_SIZE_MODE)
4400 + len += sprintf(buf + len, "SIZE mode");
4401 + else if (mode == JFFS2_BBC_FASTR_MODE)
4402 + len += sprintf(buf + len, "FASTR mode");
4403 + else if (mode == JFFS2_BBC_FASTW_MODE)
4404 + len += sprintf(buf + len, "FASTW mode");
4405 + else if (mode == JFFS2_BBC_FASTS_MODE)
4406 + len += sprintf(buf + len, "FASTS mode");
4407 + else if (mode == JFFS2_BBC_DUMMY_MODE)
4408 + len += sprintf(buf + len, "DUMMY mode");
4409 + else if (mode == JFFS2_BBC_MANUAL_MODE) {
4410 + len += sprintf(buf + len, "MANUAL mode (");
4411 + if (jffs2_bbc_get_manual_compressor() != NULL)
4412 + len += sprintf(buf + len, "%s)", jffs2_bbc_get_manual_compressor()->name);
4414 + len += sprintf(buf + len, "ZLIB)");
4417 + len += sprintf(buf + len, "unknown mode");
4418 + len += sprintf(buf + len, "\n");
4419 + len += sprintf(buf + len, "%s", jffs2_bbc_get_compr_stats());
4420 + len += sprintf(buf + len, "%s", jffs2_bbc_get_model_stats());
4421 + len += sprintf(buf + len, "%s", jffs2_bbc_get_mem_stats());
4426 +int jffs2_bbc_proc_write(struct file *file, const char *buffer_orig, unsigned long count, void *data)
4430 + struct jffs2_bbc_fs_sb_list *sb_l;
4432 + if (buffer_orig == NULL) return 0;
4434 + buffer = jffs2_bbc_malloc(count+2);
4435 + for (i=0;i<count;i++) buffer[i]=buffer_orig[i];
4436 + buffer[count] = 0;
4437 + if ((*buffer == 'z') || (*buffer == 'Z')) {
4438 + jffs2_bbc_set_compression_mode(JFFS2_BBC_ZLIB_MODE);
4439 + jffs2_bbc_print1("jffs2.bbc: ZLIB compression mode activated.\n");
4440 + jffs2_bbc_free(buffer);
4443 + else if ((*buffer == 's') || (*buffer == 'S')) {
4444 + jffs2_bbc_set_compression_mode(JFFS2_BBC_SIZE_MODE);
4445 + jffs2_bbc_print1("jffs2.bbc: SIZE compression mode activated.\n");
4446 + jffs2_bbc_free(buffer);
4449 + else if ((*buffer == 'd') || (*buffer == 'D')) {
4450 + jffs2_bbc_set_compression_mode(JFFS2_BBC_DUMMY_MODE);
4451 + jffs2_bbc_print1("jffs2.bbc: DUMMY compression mode activated.\n");
4452 + jffs2_bbc_free(buffer);
4455 + else if (((*buffer == 'm') || (*buffer == 'M')) && (count >= 3) && (buffer[1] == ':')) {
4456 + jffs2_bbc_print1("jffs2.bbc: activating MANUAL mode.\n");
4457 + jffs2_bbc_set_manual_compressor_by_name(buffer + 2);
4458 + jffs2_bbc_free(buffer);
4461 + else if (((*buffer == '0')) && (count >= 3) && (buffer[1] == ':')) {
4462 + jffs2_bbc_print1("jffs2.bbc: disabling a compressor... ");
4463 + if (jffs2_bbc_disable_compressor_by_name(buffer + 2) == 0) {
4464 + jffs2_bbc_print1("done.\n");
4467 + jffs2_bbc_print1("not found.\n");
4469 + jffs2_bbc_free(buffer);
4472 + else if (((*buffer == '1')) && (count >= 3) && (buffer[1] == ':')) {
4473 + jffs2_bbc_print1("jffs2.bbc: enabling a compressor... ");
4474 + if (jffs2_bbc_enable_compressor_by_name(buffer + 2) == 0) {
4475 + jffs2_bbc_print1("done.\n");
4478 + jffs2_bbc_print1("not found.\n");
4480 + jffs2_bbc_free(buffer);
4483 + else if (((*buffer == 'c') || (*buffer == 'C')) && (count >= 3) && (buffer[1] == ':')) {
4484 + jffs2_bbc_compressor_command_by_name(buffer + 2);
4485 + jffs2_bbc_free(buffer);
4488 + else if ((*buffer == 'r') || (*buffer == 'R')) {
4489 + jffs2_bbc_print1("jffs2.bbc: reloading model files:\n");
4491 + while (sb_l != NULL) {
4492 + jffs2_bbc_unload_model(sb_l->sb);
4493 + jffs2_bbc_load_model(sb_l->sb);
4494 + sb_l = sb_l->next;
4496 + jffs2_bbc_free(buffer);
4499 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 'r') || (buffer[1] == 'R'))) {
4500 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTR_MODE);
4501 + jffs2_bbc_print1("jffs2.bbc: FASTR compression mode activated.\n");
4502 + jffs2_bbc_free(buffer);
4505 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 'w') || (buffer[1] == 'W'))) {
4506 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTW_MODE);
4507 + jffs2_bbc_print1("jffs2.bbc: FASTW compression mode activated.\n");
4508 + jffs2_bbc_free(buffer);
4511 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 's') || (buffer[1] == 'S'))) {
4512 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTS_MODE);
4513 + jffs2_bbc_print1("jffs2.bbc: FASTS compression mode activated.\n");
4514 + jffs2_bbc_free(buffer);
4518 + jffs2_bbc_print1("jffs2.bbc: unkown command. Valid commands are:\n"
4519 + " z = switch to ZLIB compression mode\n"
4520 + " s = switch to SIZE compression mode\n"
4521 + " d = switch to DUMMY compression mode\n"
4522 + " fr = switch to FASTR compression mode\n"
4523 + " fw = switch to FASTW compression mode\n"
4524 + " fs = switch to FASTS compression mode\n"
4525 + " r = reread model files from actual file system\n"
4526 + " m:compressor_name = switch to MANUAL compression mode\n"
4527 + " 0:compressor_name = disable a compressor\n"
4528 + " 1:compressor_name = enable a compressor\n"
4529 + " c:compressor_name:command = enable a compressor\n");
4530 + jffs2_bbc_free(buffer);
4534 +void jffs2_bbc_proc_init()
4536 + struct proc_dir_entry *res = create_proc_entry("jffs2_bbc", 0, NULL);
4537 + jffs2_bbc_compressor_init();
4539 + res->read_proc = jffs2_bbc_proc_read;
4540 + res->write_proc = jffs2_bbc_proc_write;
4544 +void jffs2_bbc_proc_deinit()
4546 + jffs2_bbc_compressor_deinit();
4547 + remove_proc_entry("jffs2_bbc", NULL);
4549 diff -Nur linux-mips-cvs/fs/jffs2/jffs2_bbc_fs.h linux-mips/fs/jffs2/jffs2_bbc_fs.h
4550 --- linux-mips-cvs/fs/jffs2/jffs2_bbc_fs.h 1970-01-01 01:00:00.000000000 +0100
4551 +++ linux-mips/fs/jffs2/jffs2_bbc_fs.h 2005-02-07 05:08:34.000000000 +0100
4554 + * JFFS2 BBC: File System Extension for Linux Kernel - headers
4558 + * Copyright (C) 2004, Ferenc Havasi
4560 + * This program is free software; you can redistribute it and/or
4561 + * modify it under the terms of the GNU General Public License
4562 + * as published by the Free Software Foundation; either version 2
4563 + * of the License, or (at your option) any later version.
4565 + * This program is distributed in the hope that it will be useful,
4566 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4567 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4568 + * GNU General Public License for more details.
4570 + * You should have received a copy of the GNU General Public License
4571 + * along with this program; if not, write to the Free Software
4572 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4576 +extern int jffs2_bbc_inode_not_found;
4578 +void jffs2_bbc_load_model(void *sb);
4579 +void jffs2_bbc_unload_model(void *sb);
4581 +void jffs2_bbc_proc_init(void);
4582 +void jffs2_bbc_proc_deinit(void);
4583 diff -Nur linux-mips-cvs/fs/jffs2/jffs2_bbc_lzari_comp.c linux-mips/fs/jffs2/jffs2_bbc_lzari_comp.c
4584 --- linux-mips-cvs/fs/jffs2/jffs2_bbc_lzari_comp.c 1970-01-01 01:00:00.000000000 +0100
4585 +++ linux-mips/fs/jffs2/jffs2_bbc_lzari_comp.c 2005-02-07 05:08:34.000000000 +0100
4587 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4590 + jffs2_bbc_lzari_comp.c -- Lempel-Ziv-Arithmetic coding compression module for jffs2
4591 + Copyright (C) 2004 Patrik Kluba
4592 + Based on the LZARI source included in LDS (lossless datacompression sources)
4593 + Block-compression and bitstream modifications by Patrik Kluba
4598 +Original copyright follows:
4600 +**************************************************************
4601 + LZARI.C -- A Data Compression Program
4603 +**************************************************************
4604 + 4/7/1989 Haruhiko Okumura
4605 + Use, distribute, and modify this program freely.
4606 + Please send me your improved versions.
4608 + NIFTY-Serve PAF01022
4609 + CompuServe 74050,1022
4610 +**************************************************************
4612 +LZARI.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
4613 +All rights reserved. Permission granted for non-commercial use.
4619 + 2004-02-18 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
4620 + Removed unused variables and fixed no return value
4622 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
4629 +#define N 4096 /* size of ring buffer */
4630 +#define F 60 /* upper limit for match_length */
4631 +#define THRESHOLD 2 /* encode string into position and length
4632 + if match_length is greater than this */
4633 +#define NIL N /* index for root of binary search trees */
4635 +static unsigned char
4636 + text_buf[N + F - 1]; /* ring buffer of size N,
4637 + with extra F-1 bytes to facilitate string comparison */
4638 +static unsigned long match_position, match_length, /* of longest match. These are
4639 + set by the InsertNode() procedure. */
4640 + lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
4641 + parents -- These constitute binary search trees. */
4643 +static void InitTree(void) /* Initialize trees */
4647 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
4648 + left children of node i. These nodes need not be initialized.
4649 + Also, dad[i] is the parent of node i. These are initialized to
4650 + NIL (= N), which stands for 'not used.'
4651 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
4652 + for strings that begin with character i. These are initialized
4653 + to NIL. Note there are 256 trees. */
4655 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL; /* root */
4656 + for (i = 0; i < N; i++) dad[i] = NIL; /* node */
4659 +static void InsertNode(unsigned long r)
4660 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
4661 + trees (text_buf[r]'th tree) and returns the longest-match position
4662 + and length via the global variables match_position and match_length.
4663 + If match_length = F, then removes the old node in favor of the new
4664 + one, because the old one will be deleted sooner.
4665 + Note r plays double role, as tree node and position in buffer. */
4667 + unsigned long i, p, temp;
4668 + unsigned char *key;
4671 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
4672 + rson[r] = lson[r] = NIL; match_length = 0;
4675 + if (rson[p] != NIL) p = rson[p];
4676 + else { rson[p] = r; dad[r] = p; return; }
4678 + if (lson[p] != NIL) p = lson[p];
4679 + else { lson[p] = r; dad[r] = p; return; }
4681 + for (i = 1; i < F; i++)
4682 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
4683 + if (i > THRESHOLD) {
4684 + if (i > match_length) {
4685 + match_position = (r - p) & (N - 1);
4686 + if ((match_length = i) >= F) break;
4687 + } else if (i == match_length) {
4688 + if ((temp = (r - p) & (N - 1)) < match_position)
4689 + match_position = temp;
4693 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
4694 + dad[lson[p]] = r; dad[rson[p]] = r;
4695 + if (rson[dad[p]] == p) rson[dad[p]] = r;
4696 + else lson[dad[p]] = r;
4697 + dad[p] = NIL; /* remove p */
4700 +static void DeleteNode(unsigned long p) /* Delete node p from tree */
4704 + if (dad[p] == NIL) return; /* not in tree */
4705 + if (rson[p] == NIL) q = lson[p];
4706 + else if (lson[p] == NIL) q = rson[p];
4709 + if (rson[q] != NIL) {
4710 + do { q = rson[q]; } while (rson[q] != NIL);
4711 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
4712 + lson[q] = lson[p]; dad[lson[p]] = q;
4714 + rson[q] = rson[p]; dad[rson[p]] = q;
4717 + if (rson[dad[p]] == p) rson[dad[p]] = q;
4718 + else lson[dad[p]] = q;
4722 +/********** Arithmetic Compression **********/
4724 +/* If you are not familiar with arithmetic compression, you should read
4725 + I. E. Witten, R. M. Neal, and J. G. Cleary,
4726 + Communications of the ACM, Vol. 30, pp. 520-540 (1987),
4727 + from which much have been borrowed. */
4731 +/* Q1 (= 2 to the M) must be sufficiently large, but not so
4732 + large as the unsigned long 4 * Q1 * (Q1 - 1) overflows. */
4734 +#define Q1 (1UL << M)
4735 +#define Q2 (2 * Q1)
4736 +#define Q3 (3 * Q1)
4737 +#define Q4 (4 * Q1)
4738 +#define MAX_CUM (Q1 - 1)
4740 +#define N_CHAR (256 - THRESHOLD + F)
4741 + /* character code = 0, 1, ..., N_CHAR - 1 */
4743 +static unsigned long char_to_sym[N_CHAR], sym_to_char[N_CHAR + 1];
4744 +static unsigned long
4745 + sym_freq[N_CHAR + 1], /* frequency for symbols */
4746 + sym_cum[N_CHAR + 1], /* cumulative freq for symbols */
4747 + position_cum[N + 1]; /* cumulative freq for positions */
4749 +static void StartModel(void) /* Initialize model */
4751 + unsigned long ch, sym, i;
4753 + sym_cum[N_CHAR] = 0;
4754 + for (sym = N_CHAR; sym >= 1; sym--) {
4756 + char_to_sym[ch] = sym; sym_to_char[sym] = ch;
4757 + sym_freq[sym] = 1;
4758 + sym_cum[sym - 1] = sym_cum[sym] + sym_freq[sym];
4760 + sym_freq[0] = 0; /* sentinel (!= sym_freq[1]) */
4761 + position_cum[N] = 0;
4762 + for (i = N; i >= 1; i--)
4763 + position_cum[i - 1] = position_cum[i] + 10000 / (i + 200);
4764 + /* empirical distribution function (quite tentative) */
4765 + /* Please devise a better mechanism! */
4768 +static void UpdateModel(unsigned long sym)
4770 + unsigned long c, ch_i, ch_sym;
4772 + if (sym_cum[0] >= MAX_CUM) {
4774 + for (i = N_CHAR; i > 0; i--) {
4776 + c += (sym_freq[i] = (sym_freq[i] + 1) >> 1);
4780 + for (i = sym; sym_freq[i] == sym_freq[i - 1]; i--) ;
4782 + ch_i = sym_to_char[i]; ch_sym = sym_to_char[sym];
4783 + sym_to_char[i] = ch_sym; sym_to_char[sym] = ch_i;
4784 + char_to_sym[ch_i] = sym; char_to_sym[ch_sym] = i;
4787 + while (--i > 0) sym_cum[i]++;
4791 +static unsigned long BinarySearchSym(unsigned long x)
4792 + /* 1 if x >= sym_cum[1],
4793 + N_CHAR if sym_cum[N_CHAR] > x,
4794 + i such that sym_cum[i - 1] > x >= sym_cum[i] otherwise */
4796 + unsigned long i, j, k;
4798 + i = 1; j = N_CHAR;
4801 + if (sym_cum[k] > x) i = k + 1; else j = k;
4806 +unsigned long BinarySearchPos(unsigned long x)
4807 + /* 0 if x >= position_cum[1],
4808 + N - 1 if position_cum[N] > x,
4809 + i such that position_cum[i] > x >= position_cum[i + 1] otherwise */
4811 + unsigned long i, j, k;
4816 + if (position_cum[k] > x) i = k + 1; else j = k;
4821 +/* modified for block compression */
4822 +/* on return, srclen will contain the number of successfully compressed bytes
4823 + and dstlen will contain completed compressed bytes */
4825 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
4826 + unsigned long *dstlen)
4828 + unsigned long c, i, len, r, s, last_match_length, sym, range;
4829 + unsigned long low = 0;
4830 + unsigned long high = Q4;
4831 + unsigned long shifts = 0; /* counts for magnifying low and high around Q2 */
4832 + unsigned char *ip, *op;
4833 + unsigned long written = 0;
4834 + unsigned long read = 0;
4835 + unsigned char buffer = 0;
4836 + unsigned char mask = 128;
4837 + unsigned char *srcend = srcbuf + *srclen;
4838 + unsigned char *dstend = dstbuf + *dstlen;
4842 + InitTree(); /* initialize trees */
4844 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
4845 + any character that will appear often. */
4846 + for (len = 0; (len < F) && (ip < srcend); len++)
4847 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
4850 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
4851 + each of which begins with one or more 'space' characters. Note
4852 + the order in which these strings are inserted. This way,
4853 + degenerate trees will be less likely to occur. */
4854 + InsertNode(r); /* Finally, insert the whole string just read. The
4855 + global variables match_length and match_position are set. */
4857 + if (match_length > len) match_length = len; /* match_length
4858 + may be spuriously long near the end of text. */
4859 + if (match_length <= THRESHOLD) {
4860 + match_length = 1; /* Not long enough match. Send one byte. */
4861 + sym = char_to_sym[text_buf[r]];
4862 + range = high - low;
4863 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
4864 + low += (range * sym_cum[sym ]) / sym_cum[0];
4867 + if ((mask >>= 1) == 0) {
4868 + if (op >= dstend) {
4869 + *dstlen = written;
4878 + for ( ; shifts > 0; shifts--) {
4880 + if ((mask >>= 1) == 0) {
4881 + if (op >= dstend) {
4882 + *dstlen = written;
4892 + } else if (low >= Q2) {
4894 + if ((mask >>= 1) == 0) {
4895 + if (op >= dstend) {
4896 + *dstlen = written;
4905 + for ( ; shifts > 0; shifts--) {
4906 + if ((mask >>= 1) == 0) {
4907 + if (op >= dstend) {
4908 + *dstlen = written;
4920 + } else if (low >= Q1 && high <= Q3) {
4925 + low += low; high += high;
4929 + sym = char_to_sym[255 - THRESHOLD + match_length];
4930 + range = high - low;
4931 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
4932 + low += (range * sym_cum[sym ]) / sym_cum[0];
4935 + if ((mask >>= 1) == 0) {
4936 + if (op >= dstend) {
4937 + *dstlen = written;
4946 + for ( ; shifts > 0; shifts--) {
4948 + if ((mask >>= 1) == 0) {
4949 + if (op >= dstend) {
4950 + *dstlen = written;
4960 + } else if (low >= Q2) {
4962 + if ((mask >>= 1) == 0) {
4963 + if (op >= dstend) {
4964 + *dstlen = written;
4973 + for ( ; shifts > 0; shifts--) {
4974 + if ((mask >>= 1) == 0) {
4975 + if (op >= dstend) {
4976 + *dstlen = written;
4988 + } else if (low >= Q1 && high <= Q3) {
4993 + low += low; high += high;
4996 + range = high - low;
4997 + high = low + (range * position_cum[match_position - 1]) / position_cum[0];
4998 + low += (range * position_cum[match_position ]) / position_cum[0];
5001 + if ((mask >>= 1) == 0) {
5002 + if (op >= dstend) {
5003 + *dstlen = written;
5012 + for ( ; shifts > 0; shifts--) {
5014 + if ((mask >>= 1) == 0) {
5015 + if (op >= dstend) {
5016 + *dstlen = written;
5029 + if ((mask >>= 1) == 0) {
5030 + if (op >= dstend) {
5031 + *dstlen = written;
5040 + for ( ; shifts > 0; shifts--) {
5041 + if ((mask >>= 1) == 0) {
5042 + if (op >= dstend) {
5043 + *dstlen = written;
5056 + if ((low >= Q1) && (high <= Q3)) {
5069 + last_match_length = match_length;
5070 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
5075 + text_buf[s + N] = c;
5076 + s = (s + 1) & (N - 1);
5077 + r = (r + 1) & (N - 1);
5081 + while (i++ < last_match_length) {
5083 + s = (s + 1) & (N - 1);
5084 + r = (r + 1) & (N - 1);
5085 + if (--len) InsertNode(r);
5087 + } while (len > 0);
5090 + if ((mask >>= 1) == 0) {
5091 + if (op >= dstend) {
5092 + *dstlen = written;
5101 + for ( ; shifts > 0; shifts--) {
5103 + if ((mask >>= 1) == 0) {
5104 + if (op >= dstend) {
5105 + *dstlen = written;
5117 + if ((mask >>= 1) == 0) {
5118 + if (op >= dstend) {
5119 + *dstlen = written;
5128 + for ( ; shifts > 0; shifts--) {
5129 + if ((mask >>= 1) == 0) {
5130 + if (op >= dstend) {
5131 + *dstlen = written;
5142 + for (i = 0; i < 7; i++) {
5143 + if ((mask >>= 1) == 0) {
5144 + if (op >= dstend) {
5145 + *dstlen = written;
5155 + *dstlen = written;
5159 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
5160 + unsigned long dstlen) /* Just the reverse of Encode(). */
5162 + unsigned long i, r, j, k, c, range, sym;
5163 + unsigned char *ip, *op;
5164 + unsigned char *srcend = srcbuf + srclen;
5165 + unsigned char *dstend = dstbuf + dstlen;
5166 + unsigned char buffer = 0;
5167 + unsigned char mask = 0;
5168 + unsigned long low = 0;
5169 + unsigned long high = Q4;
5170 + unsigned long value = 0;
5173 + for (i = 0; i < M + 2; i++) {
5175 + if ((mask >>= 1) == 0) {
5176 + buffer = (ip >= srcend) ? 0 : *(ip++);
5179 + value += ((buffer & mask) != 0);
5182 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
5184 + while (op < dstend) {
5185 + range = high - low;
5186 + sym = BinarySearchSym((unsigned long)
5187 + (((value - low + 1) * sym_cum[0] - 1) / range));
5188 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
5189 + low += (range * sym_cum[sym ]) / sym_cum[0];
5192 + value -= Q2; low -= Q2; high -= Q2;
5193 + } else if (low >= Q1 && high <= Q3) {
5194 + value -= Q1; low -= Q1; high -= Q1;
5195 + } else if (high > Q2) break;
5196 + low += low; high += high;
5198 + if ((mask >>= 1) == 0) {
5199 + buffer = (ip >= srcend) ? 0 : *(ip++);
5202 + value += ((buffer & mask) != 0);
5204 + c = sym_to_char[sym];
5207 + if (op >= dstend) return -1;
5209 + text_buf[r++] = c;
5212 + j = c - 255 + THRESHOLD;
5213 + range = high - low;
5214 + i = BinarySearchPos((unsigned long)
5215 + (((value - low + 1) * position_cum[0] - 1) / range));
5216 + high = low + (range * position_cum[i ]) / position_cum[0];
5217 + low += (range * position_cum[i + 1]) / position_cum[0];
5220 + value -= Q2; low -= Q2; high -= Q2;
5221 + } else if (low >= Q1 && high <= Q3) {
5222 + value -= Q1; low -= Q1; high -= Q1;
5223 + } else if (high > Q2) break;
5224 + low += low; high += high;
5226 + if ((mask >>= 1) == 0) {
5227 + buffer = (ip >= srcend) ? 0 : *(ip++);
5230 + value += ((buffer & mask) != 0);
5232 + i = (r - i - 1) & (N - 1);
5233 + for (k = 0; k < j; k++) {
5234 + c = text_buf[(i + k) & (N - 1)];
5235 + if (op >= dstend) return -1;
5237 + text_buf[r++] = c;
5245 +/* interface to jffs2 bbc follows */
5247 +#include "jffs2_bbc_framework.h"
5249 +#define JFFS2_BBC_LZARI_BLOCK_SIGN {0x73, 0x9a, 0x1c, 0x4d}
5252 +jffs2_bbc_lzari_compressor_init (void);
5255 +jffs2_bbc_lzari_compressor_deinit (void);
5258 +jffs2_bbc_lzari_compress (void *model, unsigned char *input,
5259 + unsigned char *output, unsigned long *sourcelen,
5260 + unsigned long *dstlen);
5263 +jffs2_bbc_lzari_estimate (void *model, unsigned char *input,
5264 + unsigned long sourcelen, unsigned long *dstlen,
5265 + unsigned long *readtime, unsigned long *writetime);
5268 +jffs2_bbc_lzari_decompress (void *model, unsigned char *input,
5269 + unsigned char *output, unsigned long sourcelen,
5270 + unsigned long dstlen);
5273 +jffs2_bbc_lzari_proc_info (void);
5276 +jffs2_bbc_lzari_proc_command (char *command);
5278 +struct jffs2_bbc_compressor_type jffs2_bbc_lzari = {
5281 + JFFS2_BBC_LZARI_BLOCK_SIGN,
5282 + jffs2_bbc_lzari_compressor_init,
5285 + jffs2_bbc_lzari_compressor_deinit,
5286 + jffs2_bbc_lzari_compress,
5287 + jffs2_bbc_lzari_estimate,
5288 + jffs2_bbc_lzari_decompress,
5289 + jffs2_bbc_lzari_proc_info,
5290 + jffs2_bbc_lzari_proc_command
5294 +jffs2_bbc_lzari_compressor_init (void)
5300 +jffs2_bbc_lzari_compressor_deinit (void)
5305 +jffs2_bbc_lzari_compress (void *model, unsigned char *input,
5306 + unsigned char *output, unsigned long *sourcelen,
5307 + unsigned long *dstlen)
5310 + unsigned long dst = *dstlen;
5311 + *(output++) = jffs2_bbc_lzari.block_sign[0];
5312 + *(output++) = jffs2_bbc_lzari.block_sign[1];
5314 + retval = Encode(input, output, sourcelen, &dst);
5321 +jffs2_bbc_lzari_estimate (void *model, unsigned char *input,
5322 + unsigned long sourcelen, unsigned long *dstlen,
5323 + unsigned long *readtime, unsigned long *writetime)
5325 + *dstlen = sourcelen / 2;
5326 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 15;
5327 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 7;
5332 +jffs2_bbc_lzari_decompress (void *model, unsigned char *input,
5333 + unsigned char *output, unsigned long sourcelen,
5334 + unsigned long dstlen)
5336 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzari.block_sign[0] ) ||
5337 + ( *(input++) != (unsigned char)jffs2_bbc_lzari.block_sign[1] )
5341 + return Decode(input, output, sourcelen - 2, dstlen);
5346 +jffs2_bbc_lzari_proc_info (void)
5348 + return "Lempel-Ziv-Arithmetic coding compression module";
5352 +jffs2_bbc_lzari_proc_command (char *command)
5357 +struct jffs2_bbc_compressor_type *
5358 +jffs2_bbc_lzari_init (int mode)
5360 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzari) == 0)
5362 + return &jffs2_bbc_lzari;
5371 +jffs2_bbc_lzari_deinit (void)
5373 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzari);
5375 diff -Nur linux-mips-cvs/fs/jffs2/jffs2_bbc_lzhd_comp.c linux-mips/fs/jffs2/jffs2_bbc_lzhd_comp.c
5376 --- linux-mips-cvs/fs/jffs2/jffs2_bbc_lzhd_comp.c 1970-01-01 01:00:00.000000000 +0100
5377 +++ linux-mips/fs/jffs2/jffs2_bbc_lzhd_comp.c 2005-02-07 05:08:34.000000000 +0100
5379 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
5382 + jffs2_bbc_lzhd_comp.c -- Lempel-Ziv-(dynamic) Huffman compression module for jffs2
5383 + Copyright (C) 2004 Patrik Kluba
5384 + Based on the LZHUF source included in LDS (lossless datacompression sources)
5385 + Block-compression and bitstream modifications by Patrik Kluba
5390 +Original copyright follows:
5392 +**************************************************************
5394 + written by Haruyasu Yoshizaki 11/20/1988
5395 + some minor changes 4/6/1989
5396 + comments translated by Haruhiko Okumura 4/7/1989
5397 +**************************************************************
5399 +LZHUF.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
5400 +All rights reserved. Permission granted for non-commercial use.
5406 + 2004-02-18 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
5407 + Replaced name lzh-d with lzhd
5408 + Fixed no return value
5410 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
5415 +/* required because of memmove */
5417 + #include <string.h>
5419 + #include <linux/string.h>
5424 +#define N 4096 /* size of ring buffer */
5425 +#define F 60 /* upper limit for match_length */
5426 +#define THRESHOLD 2 /* encode string into position and length
5427 + if match_length is greater than this */
5428 +#define NIL N /* index for root of binary search trees */
5430 +static unsigned char
5431 + text_buf[N + F - 1]; /* ring buffer of size N,
5432 + with extra F-1 bytes to facilitate string comparison */
5433 +static unsigned long match_position, match_length, /* of longest match. These are
5434 + set by the InsertNode() procedure. */
5435 + lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
5436 + parents -- These constitute binary search trees. */
5438 +static void InitTree(void) /* initialize trees */
5442 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
5443 + left children of node i. These nodes need not be initialized.
5444 + Also, dad[i] is the parent of node i. These are initialized to
5445 + NIL (= N), which stands for 'not used.'
5446 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
5447 + for strings that begin with character i. These are initialized
5448 + to NIL. Note there are 256 trees. */
5450 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
5451 + for (i = 0; i < N; i++) dad[i] = NIL;
5454 +static void InsertNode(unsigned long r)
5455 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
5456 + trees (text_buf[r]'th tree) and returns the longest-match position
5457 + and length via the global variables match_position and match_length.
5458 + If match_length = F, then removes the old node in favor of the new
5459 + one, because the old one will be deleted sooner.
5460 + Note r plays double role, as tree node and position in buffer. */
5462 + unsigned long i, p, c;
5464 + unsigned char *key;
5466 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
5467 + rson[r] = lson[r] = NIL; match_length = 0;
5470 + if (rson[p] != NIL) p = rson[p];
5471 + else { rson[p] = r; dad[r] = p; return; }
5473 + if (lson[p] != NIL) p = lson[p];
5474 + else { lson[p] = r; dad[r] = p; return; }
5476 + for (i = 1; i < F; i++)
5477 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
5478 + if (i > THRESHOLD) {
5479 + if (i > match_length) {
5480 + match_position = ((r - p) & (N - 1)) - 1;
5481 + if ((match_length = i) >= F) break;
5483 + if (i == match_length) {
5484 + if ((c = ((r - p) & (N - 1)) - 1) < match_position) {
5485 + match_position = c;
5490 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
5491 + dad[lson[p]] = r; dad[rson[p]] = r;
5492 + if (rson[dad[p]] == p) rson[dad[p]] = r;
5493 + else lson[dad[p]] = r;
5494 + dad[p] = NIL; /* remove p */
5497 +static void DeleteNode(unsigned long p) /* deletes node p from tree */
5501 + if (dad[p] == NIL) return; /* not in tree */
5502 + if (rson[p] == NIL) q = lson[p];
5503 + else if (lson[p] == NIL) q = rson[p];
5506 + if (rson[q] != NIL) {
5507 + do { q = rson[q]; } while (rson[q] != NIL);
5508 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
5509 + lson[q] = lson[p]; dad[lson[p]] = q;
5511 + rson[q] = rson[p]; dad[rson[p]] = q;
5514 + if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
5518 +/* Huffman coding */
5520 +#define N_CHAR (256 - THRESHOLD + F)
5521 + /* kinds of characters (character code = 0..N_CHAR-1) */
5522 +#define T (N_CHAR * 2 - 1) /* size of table */
5523 +#define R (T - 1) /* position of root */
5524 +#define MAX_FREQ 0x8000 /* updates tree when the */
5525 + /* root frequency comes to this value. */
5527 +typedef unsigned long uchar; // much-much faster
5529 +/* table for encoding and decoding the upper 6 bits of position */
5532 +static uchar p_len[64] = {
5533 + 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
5534 + 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06,
5535 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5536 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5537 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5538 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5539 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5540 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
5543 +static uchar p_code[64] = {
5544 + 0x00, 0x20, 0x30, 0x40, 0x50, 0x58, 0x60, 0x68,
5545 + 0x70, 0x78, 0x80, 0x88, 0x90, 0x94, 0x98, 0x9C,
5546 + 0xA0, 0xA4, 0xA8, 0xAC, 0xB0, 0xB4, 0xB8, 0xBC,
5547 + 0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE,
5548 + 0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE,
5549 + 0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEC, 0xEE,
5550 + 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
5551 + 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
5555 +static uchar d_code[256] = {
5556 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5557 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5558 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5559 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5560 + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
5561 + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
5562 + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
5563 + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
5564 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5565 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5566 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5567 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5568 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5569 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5570 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5571 + 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
5572 + 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
5573 + 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
5574 + 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
5575 + 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
5576 + 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
5577 + 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
5578 + 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
5579 + 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
5580 + 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
5581 + 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
5582 + 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
5583 + 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
5584 + 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
5585 + 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
5586 + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
5587 + 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
5590 +static uchar d_len[256] = {
5591 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5592 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5593 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5594 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5595 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5596 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5597 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5598 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5599 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5600 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5601 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5602 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5603 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5604 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5605 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5606 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5607 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5608 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5609 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5610 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5611 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5612 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5613 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5614 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5615 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5616 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5617 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5618 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5619 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5620 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5621 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5622 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5625 +static unsigned long freq[T + 1]; /* frequency table */
5627 +static unsigned long prnt[T + N_CHAR]; /* pointers to parent nodes, except for the */
5628 + /* elements [T..T + N_CHAR - 1] which are used to get */
5629 + /* the positions of leaves corresponding to the codes. */
5631 +static unsigned long son[T]; /* pointers to child nodes (son[], son[] + 1) */
5633 +/* initialization of tree */
5635 +static void StartHuff(void)
5637 + unsigned long i, j;
5639 + for (i = 0; i < N_CHAR; i++) {
5644 + i = 0; j = N_CHAR;
5646 + freq[j] = freq[i] + freq[i + 1];
5648 + prnt[i] = prnt[i + 1] = j;
5655 +/* reconstruction of tree */
5657 +static void reconst(void)
5659 + unsigned long f, l, i, j, k;
5661 + /* collect leaf nodes in the first half of the table */
5662 + /* and replace the freq by (freq + 1) / 2. */
5664 + for (i = 0; i < T; i++) {
5665 + if (son[i] >= T) {
5666 + freq[j] = (freq[i] + 1) / 2;
5671 + /* begin constructing tree by connecting sons */
5672 + for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
5674 + f = freq[j] = freq[i] + freq[k];
5675 + for (k = j - 1; f < freq[k]; k--);
5678 + memmove(&freq[k + 1], &freq[k], l*sizeof(unsigned long));
5680 + memmove(&son[k + 1], &son[k], l*sizeof(unsigned long));
5683 + /* connect prnt */
5684 + for (i = 0; i < T; i++) {
5685 + if ((k = son[i]) >= T) {
5688 + prnt[k] = prnt[k + 1] = i;
5693 +/* increment frequency of given code by one, and update tree */
5695 +static void update(unsigned long c)
5697 + unsigned long i, j, k, l;
5699 + if (freq[R] == MAX_FREQ) {
5706 + /* if the order is disturbed, exchange nodes */
5707 + if (k > freq[l = c + 1]) {
5708 + while (k > freq[++l]);
5710 + freq[c] = freq[l];
5715 + if (i < T) prnt[i + 1] = l;
5721 + if (j < T) prnt[j + 1] = c;
5726 + } while (c = prnt[c]); /* repeat up to root */
5729 +/* modified for block compression */
5730 +/* on return, srclen will contain the number of successfully compressed bytes
5731 + and dstlen will contain completed compressed bytes */
5733 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
5734 + unsigned long *dstlen)
5736 + unsigned long c, i, j, k, len, r, s, last_match_length, code_buf_ptr;
5737 + unsigned char code_buf[17], mask;
5738 + unsigned char *ip, *op;
5739 + unsigned long written = 0;
5740 + unsigned long read = 0;
5741 + unsigned short putbuf = 0;
5743 + unsigned char *srcend = srcbuf + *srclen;
5744 + unsigned char *dstend = dstbuf + *dstlen;
5748 + InitTree(); /* initialize trees */
5749 + code_buf[0] = 0; /* code_buf[1..16] saves eight units of code, and
5750 + code_buf[0] works as eight flags, "1" representing that the unit
5751 + is an unencoded letter (1 byte), "0" a position-and-length pair
5752 + (2 bytes). Thus, eight units require at most 16 bytes of code. */
5753 + code_buf_ptr = mask = 1;
5755 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
5756 + any character that will appear often. */
5757 + for (len = 0; (len < F) && (ip < srcend); len++)
5758 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
5761 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
5762 + each of which begins with one or more 'space' characters. Note
5763 + the order in which these strings are inserted. This way,
5764 + degenerate trees will be less likely to occur. */
5765 + InsertNode(r); /* Finally, insert the whole string just read. The
5766 + global variables match_length and match_position are set. */
5768 + if (match_length > len) match_length = len; /* match_length
5769 + may be spuriously long near the end of text. */
5770 + if (match_length <= THRESHOLD) {
5771 + match_length = 1; /* Not long enough match. Send one byte. */
5773 + i = 0; j = 0; k = prnt[c + T];
5776 + /* if node's address is odd-numbered, choose bigger brother node */
5777 + if (k & 1) i |= 0x8000;
5779 + } while ((k = prnt[k]) != R);
5780 + putbuf |= i >> putlen;
5781 + if ((putlen += j) >= 8) {
5782 + if (op >= dstend) {
5783 + *dstlen = written;
5786 + *(op++) = putbuf >> 8;
5787 + if ((putlen -= 8) >= 8) {
5788 + if (op >= dstend) {
5789 + *dstlen = written;
5795 + putbuf = i << (j - putlen); /**warm**/
5804 + c = 255 - THRESHOLD + match_length;
5805 + i = 0; j = 0; k = prnt[c + T];
5808 + /* if node's address is odd-numbered, choose bigger brother node */
5809 + if (k & 1) i |= 0x8000;
5811 + } while ((k = prnt[k]) != R);
5812 + putbuf |= i >> putlen;
5813 + if ((putlen += j) >= 8) {
5814 + if (op >= dstend) {
5815 + *dstlen = written;
5818 + *(op++) = putbuf >> 8;
5819 + if ((putlen -= 8) >= 8) {
5820 + if (op >= dstend) {
5821 + *dstlen = written;
5827 + putbuf = i << (j - putlen); /**warm**/
5835 + j = p_len[match_position >> 6];
5836 + i = p_code[match_position >> 6] << 8;
5837 + putbuf |= i >> putlen;
5838 + if ((putlen += j) >= 8) {
5839 + if (op >= dstend) {
5840 + *dstlen = written;
5843 + *(op++) = putbuf >> 8;
5844 + if ((putlen -= 8) >= 8) {
5845 + if (op >= dstend) {
5846 + *dstlen = written;
5852 + putbuf = i << (j - putlen); /**hot**/
5860 + i = (match_position & 0x3f) << 10;
5861 + putbuf |= i >> putlen;
5862 + if ((putlen += j) >= 8) {
5863 + if (op >= dstend) {
5864 + *dstlen = written;
5867 + *(op++) = putbuf >> 8;
5868 + if ((putlen -= 8) >= 8) {
5869 + if (op >= dstend) {
5870 + *dstlen = written;
5876 + putbuf = i << (j - putlen); /**hot**/
5884 + last_match_length = match_length;
5885 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
5890 + text_buf[s + N] = c;
5891 + s = (s + 1) & (N - 1);
5892 + r = (r + 1) & (N - 1);
5896 + while (i++ < last_match_length) {
5898 + s = (s + 1) & (N - 1);
5899 + r = (r + 1) & (N - 1);
5900 + if (--len) InsertNode(r);
5902 + } while (len > 0);
5904 + if (op >= dstend) {
5905 + *dstlen = written;
5908 + *(op++) = putbuf >> 8;
5912 + *dstlen = written;
5916 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
5917 + unsigned long dstlen) /* Just the reverse of Encode(). */
5919 + unsigned long i, r, j, k, c;
5920 + unsigned char *ip, *op;
5921 + unsigned char *srcend = srcbuf + srclen;
5922 + unsigned char *dstend = dstbuf + dstlen;
5923 + unsigned short getbuf = 0;
5928 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
5930 + while (op < dstend) {
5932 + /* travel from root to leaf, */
5933 + /* choosing the smaller child node (son[]) if the read bit is 0, */
5934 + /* the bigger (son[]+1} if 1 */
5936 + while (getlen <= 8) {
5938 + t = (ip >= srcend) ? 0 : *(ip++);
5939 + getbuf |= t << (8 - getlen);
5942 + c += ((signed short)getbuf < 0);
5950 + if (op >= dstend) return -1;
5952 + text_buf[r++] = c;
5955 + j = c - 255 + THRESHOLD;
5956 + while (getlen <= 8) {
5958 + t = (ip >= srcend) ? 0 : *(ip++);
5959 + getbuf |= t << (8 - getlen);
5965 + c = d_code[i] << 6;
5967 + /* read lower 6 bits verbatim */
5970 + while (getlen <= 8) {
5972 + t = (ip >= srcend) ? 0 : *(ip++);
5973 + getbuf |= t << (8 - getlen);
5976 + i = (i << 1) + ((signed short)getbuf < 0);
5980 + i = c | (i & 0x3F);
5983 + for (k = 0; k < j; k++) {
5984 + c = text_buf[(i + k) & (N - 1)];
5985 + if (op >= dstend) return -1;
5987 + text_buf[r++] = c;
5995 +/* interface to jffs2 bbc follows */
5997 +#include "jffs2_bbc_framework.h"
6000 +#define JFFS2_BBC_LZHD_BLOCK_SIGN {0x3a, 0x98, 0xf7, 0xda}
6003 +jffs2_bbc_lzhd_compressor_init (void);
6006 +jffs2_bbc_lzhd_compressor_deinit (void);
6009 +jffs2_bbc_lzhd_compress (void *model, unsigned char *input,
6010 + unsigned char *output, unsigned long *sourcelen,
6011 + unsigned long *dstlen);
6014 +jffs2_bbc_lzhd_estimate (void *model, unsigned char *input,
6015 + unsigned long sourcelen, unsigned long *dstlen,
6016 + unsigned long *readtime, unsigned long *writetime);
6019 +jffs2_bbc_lzhd_decompress (void *model, unsigned char *input,
6020 + unsigned char *output, unsigned long sourcelen,
6021 + unsigned long dstlen);
6024 +jffs2_bbc_lzhd_proc_info (void);
6027 +jffs2_bbc_lzhd_proc_command (char *command);
6029 +struct jffs2_bbc_compressor_type jffs2_bbc_lzhd = {
6032 + JFFS2_BBC_LZHD_BLOCK_SIGN,
6033 + jffs2_bbc_lzhd_compressor_init,
6036 + jffs2_bbc_lzhd_compressor_deinit,
6037 + jffs2_bbc_lzhd_compress,
6038 + jffs2_bbc_lzhd_estimate,
6039 + jffs2_bbc_lzhd_decompress,
6040 + jffs2_bbc_lzhd_proc_info,
6041 + jffs2_bbc_lzhd_proc_command
6045 +jffs2_bbc_lzhd_compressor_init (void)
6051 +jffs2_bbc_lzhd_compressor_deinit (void)
6056 +jffs2_bbc_lzhd_compress (void *model, unsigned char *input,
6057 + unsigned char *output, unsigned long *sourcelen,
6058 + unsigned long *dstlen)
6061 + unsigned long dst = *dstlen;
6062 + *(output++) = jffs2_bbc_lzhd.block_sign[0];
6063 + *(output++) = jffs2_bbc_lzhd.block_sign[1];
6065 + retval = Encode(input, output, sourcelen, &dst);
6072 +jffs2_bbc_lzhd_estimate (void *model, unsigned char *input,
6073 + unsigned long sourcelen, unsigned long *dstlen,
6074 + unsigned long *readtime, unsigned long *writetime)
6076 + *dstlen = sourcelen * 55 / 100;
6077 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 8;
6078 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 65 / 10;
6083 +jffs2_bbc_lzhd_decompress (void *model, unsigned char *input,
6084 + unsigned char *output, unsigned long sourcelen,
6085 + unsigned long dstlen)
6087 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzhd.block_sign[0] ) ||
6088 + ( *(input++) != (unsigned char)jffs2_bbc_lzhd.block_sign[1] )
6092 + return Decode(input, output, sourcelen - 2, dstlen);
6097 +jffs2_bbc_lzhd_proc_info (void)
6099 + return "Lempel-Ziv-(dynamic) Huffman compression module";
6103 +jffs2_bbc_lzhd_proc_command (char *command)
6108 +struct jffs2_bbc_compressor_type *
6109 +jffs2_bbc_lzhd_init (int mode)
6111 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzhd) == 0)
6113 + return &jffs2_bbc_lzhd;
6122 +jffs2_bbc_lzhd_deinit (void)
6124 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzhd);
6126 diff -Nur linux-mips-cvs/fs/jffs2/jffs2_bbc_lzo_comp.c linux-mips/fs/jffs2/jffs2_bbc_lzo_comp.c
6127 --- linux-mips-cvs/fs/jffs2/jffs2_bbc_lzo_comp.c 1970-01-01 01:00:00.000000000 +0100
6128 +++ linux-mips/fs/jffs2/jffs2_bbc_lzo_comp.c 2005-02-07 05:08:34.000000000 +0100
6130 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
6133 + jffs2_bbc_lzo_comp.c -- LZO1X-1 (and -999) compression module for jffs2
6134 + Copyright (C) 2004 Patrik Kluba
6135 + Based on the original LZO sources
6140 + Original copyright notice follows:
6142 + lzo1x_9x.c -- implementation of the LZO1X-999 compression algorithm
6143 + lzo_ptr.h -- low-level pointer constructs
6144 + lzo_swd.ch -- sliding window dictionary
6145 + lzoconf.h -- configuration for the LZO real-time data compression library
6146 + lzo_mchw.ch -- matching functions using a window
6147 + minilzo.c -- mini subset of the LZO real-time data compression library
6148 + config1x.h -- configuration for the LZO1X algorithm
6149 + lzo1x.h -- public interface of the LZO1X compression algorithm
6151 + These files are part of the LZO real-time data compression library.
6153 + Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
6154 + All Rights Reserved.
6156 + The LZO library is free software; you can redistribute it and/or
6157 + modify it under the terms of the GNU General Public License as
6158 + published by the Free Software Foundation; either version 2 of
6159 + the License, or (at your option) any later version.
6161 + The LZO library is distributed in the hope that it will be useful,
6162 + but WITHOUT ANY WARRANTY; without even the implied warranty of
6163 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6164 + GNU General Public License for more details.
6166 + You should have received a copy of the GNU General Public License
6167 + along with the LZO library; see the file COPYING.
6168 + If not, write to the Free Software Foundation, Inc.,
6169 + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
6171 + Markus F.X.J. Oberhumer
6172 + <markus@oberhumer.com>
6177 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
6179 + -removed all 16 bit code
6180 + -all sensitive data will be on 4 byte boundary
6181 + -removed check parts for library use
6182 + -removed all but LZO1X-* compression
6187 + #include <sys/types.h>
6188 + #include <stddef.h>
6189 + #include <string.h>
6190 + #include <limits.h>
6192 + #include <linux/kernel.h>
6193 + #include <linux/types.h>
6194 + #include <linux/stddef.h>
6195 + #include <linux/string.h>
6196 + #define USHRT_MAX 65535
6197 + /* #define UINT_MAX 4294967295U */
6200 +/* data type definitions */
6201 +#define U32 unsigned long
6202 +#define S32 signed long
6204 +#define U16 unsigned short
6205 +#define S16 signed short
6207 +#define U8 unsigned char
6208 +#define S8 signed char
6211 +/*************************************/
6217 +#define SWD_THRESHOLD THRESHOLD
6219 +/* shortest unsigned int that 2 * SWD_F + SWD_N (currently 53248) fits in */
6220 +typedef unsigned short swd_uint;
6221 +/* upper limit of that data type */
6222 +#define SWD_UINT_MAX USHRT_MAX
6226 +#define LZO_VERSION_DATE "Jul 12 2002"
6227 +#define LZO_VERSION_STRING "1.08"
6228 +#define LZO_VERSION 0x1080
6232 +/* Integral types that have *exactly* the same number of bits as a lzo_voidp */
6233 +typedef unsigned long lzo_ptr_t;
6234 +typedef long lzo_sptr_t;
6237 +/*************************************/
6241 +#define M1_MAX_OFFSET 0x0400
6242 +#define M2_MAX_OFFSET 0x0800
6243 +#define M3_MAX_OFFSET 0x4000
6244 +#define M4_MAX_OFFSET 0xbfff
6246 +#define MX_MAX_OFFSET (M1_MAX_OFFSET + M2_MAX_OFFSET)
6248 +#define M1_MIN_LEN 2
6249 +#define M1_MAX_LEN 2
6250 +#define M2_MIN_LEN 3
6251 +#define M2_MAX_LEN 8
6252 +#define M3_MIN_LEN 3
6253 +#define M3_MAX_LEN 33
6254 +#define M4_MIN_LEN 3
6255 +#define M4_MAX_LEN 9
6257 +#define M1_MARKER 0
6258 +#define M2_MARKER 64
6259 +#define M3_MARKER 32
6260 +#define M4_MARKER 16
6262 +#define MIN_LOOKAHEAD (M2_MAX_LEN + 1)
6266 +#define LZO_BYTE(x) ((unsigned char) ((x) & 0xff))
6268 +#define LZO_MAX(a,b) ((a) >= (b) ? (a) : (b))
6269 +#define LZO_MIN(a,b) ((a) <= (b) ? (a) : (b))
6270 +#define LZO_MAX3(a,b,c) ((a) >= (b) ? LZO_MAX(a,c) : LZO_MAX(b,c))
6271 +#define LZO_MIN3(a,b,c) ((a) <= (b) ? LZO_MIN(a,c) : LZO_MIN(b,c))
6273 +#define lzo_sizeof(type) ((lzo_uint) (sizeof(type)))
6275 +#define LZO_HIGH(array) ((lzo_uint) (sizeof(array)/sizeof(*(array))))
6277 +#define LZO_SIZE(bits) (1u << (bits))
6278 +#define LZO_MASK(bits) (LZO_SIZE(bits) - 1)
6280 +#define LZO_LSIZE(bits) (1ul << (bits))
6281 +#define LZO_LMASK(bits) (LZO_LSIZE(bits) - 1)
6283 +#define LZO_USIZE(bits) ((lzo_uint) 1 << (bits))
6284 +#define LZO_UMASK(bits) (LZO_USIZE(bits) - 1)
6286 +#define LZO_STYPE_MAX(b) (((1l << (8*(b)-2)) - 1l) + (1l << (8*(b)-2)))
6287 +#define LZO_UTYPE_MAX(b) (((1ul << (8*(b)-1)) - 1ul) + (1ul << (8*(b)-1)))
6289 +#define _LZO_STRINGIZE(x) #x
6290 +#define _LZO_MEXPAND(x) _LZO_STRINGIZE(x)
6292 +#define _LZO_CONCAT2(a,b) a ## b
6293 +#define _LZO_CONCAT3(a,b,c) a ## b ## c
6294 +#define _LZO_CONCAT4(a,b,c,d) a ## b ## c ## d
6295 +#define _LZO_CONCAT5(a,b,c,d,e) a ## b ## c ## d ## e
6297 +#define _LZO_ECONCAT2(a,b) _LZO_CONCAT2(a,b)
6298 +#define _LZO_ECONCAT3(a,b,c) _LZO_CONCAT3(a,b,c)
6299 +#define _LZO_ECONCAT4(a,b,c,d) _LZO_CONCAT4(a,b,c,d)
6300 +#define _LZO_ECONCAT5(a,b,c,d,e) _LZO_CONCAT5(a,b,c,d,e)
6302 +#define lzo_dict_t const lzo_bytep
6303 +#define lzo_dict_p lzo_dict_t *
6304 +#define lzo_moff_t lzo_uint
6306 +#define MEMCPY8_DS(dest,src,len) \
6307 + memcpy(dest,src,len); \
6311 +#define MEMCPY_DS(dest,src,len) \
6312 + do *dest++ = *src++; \
6315 +#define MEMMOVE_DS(dest,src,len) \
6316 + do *dest++ = *src++; \
6319 +#define BZERO8_PTR(s,l,n) memset((s),0,(lzo_uint)(l)*(n))
6321 +#define LZO_BASE 65521u
6322 +#define LZO_NMAX 5552
6324 +#define LZO_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
6325 +#define LZO_DO2(buf,i) LZO_DO1(buf,i); LZO_DO1(buf,i+1);
6326 +#define LZO_DO4(buf,i) LZO_DO2(buf,i); LZO_DO2(buf,i+2);
6327 +#define LZO_DO8(buf,i) LZO_DO4(buf,i); LZO_DO4(buf,i+4);
6328 +#define LZO_DO16(buf,i) LZO_DO8(buf,i); LZO_DO8(buf,i+8);
6330 +#define IS_SIGNED(type) (((type) (-1)) < ((type) 0))
6331 +#define IS_UNSIGNED(type) (((type) (-1)) > ((type) 0))
6333 +#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
6336 +#define D_INDEX1(d,p) d = DM((0x21*DX3(p,5,5,6)) >> 5)
6337 +#define D_INDEX2(d,p) d = (d & (D_MASK & 0x7ff)) ^ (D_HIGH | 0x1f)
6339 +#define LZO_HASH LZO_HASH_LZO_INCREMENTAL_B
6341 +#define DL_MIN_LEN M2_MIN_LEN
6343 +#define D_SIZE LZO_SIZE(D_BITS)
6344 +#define D_MASK LZO_MASK(D_BITS)
6346 +#define D_HIGH ((D_MASK >> 1) + 1)
6348 +#define DINDEX1 D_INDEX1
6349 +#define DINDEX2 D_INDEX2
6351 +#define DX2(p,s1,s2) \
6352 + (((((lzo_uint32)((p)[2]) << (s2)) ^ (p)[1]) << (s1)) ^ (p)[0])
6354 +#define DX3(p,s1,s2,s3) ((DX2((p)+1,s2,s3) << (s1)) ^ (p)[0])
6355 +#define DMS(v,s) ((lzo_uint) (((v) & (D_MASK >> (s))) << (s)))
6356 +#define DM(v) DMS(v,0)
6358 +#define DENTRY(p,in) (p)
6359 +#define GINDEX(m_pos,m_off,dict,dindex,in) m_pos = dict[dindex]
6361 +#define LZO_CHECK_MPOS_DET(m_pos,m_off,in,ip,max_offset) \
6362 + (m_pos == NULL || (m_off = (lzo_moff_t) (ip - m_pos)) > max_offset)
6364 +#define LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
6365 + (BOUNDS_CHECKING_OFF_IN_EXPR( \
6366 + (PTR_LT(m_pos,in) || \
6367 + (m_off = (lzo_moff_t) PTR_DIFF(ip,m_pos)) <= 0 || \
6368 + m_off > max_offset) ))
6370 +#define BOUNDS_CHECKING_OFF_IN_EXPR(expr) (expr)
6373 +#define DD_SIZE LZO_SIZE(DD_BITS)
6374 +#define DD_MASK LZO_MASK(DD_BITS)
6376 +#define DL_BITS (D_BITS - DD_BITS)
6377 +#define DL_SIZE LZO_SIZE(DL_BITS)
6378 +#define DL_MASK LZO_MASK(DL_BITS)
6380 +#define UPDATE_D(dict,drun,dv,p,in) dict[ DINDEX(dv,p) ] = DENTRY(p,in)
6381 +#define UPDATE_I(dict,drun,index,p,in) dict[index] = DENTRY(p,in)
6382 +#define UPDATE_P(ptr,drun,p,in) (ptr)[0] = DENTRY(p,in)
6384 +#define __COPY4(dst,src) * (lzo_uint32p)(dst) = * (const lzo_uint32p)(src)
6385 +#define COPY4(dst,src) __COPY4((lzo_ptr_t)(dst),(lzo_ptr_t)(src))
6387 +#define TEST_IP (ip < ip_end)
6388 +#define TEST_OP (op <= op_end)
6390 +#define NEED_IP(x) \
6391 + if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x)) goto input_overrun
6392 +#define NEED_OP(x) \
6393 + if ((lzo_uint)(op_end - op) < (lzo_uint)(x)) goto output_overrun
6394 +#define TEST_LOOKBEHIND(m_pos,out) if (m_pos < out) goto lookbehind_overrun
6398 +#define LZO_UINT_MAX UINT_MAX
6399 +#define N M4_MAX_OFFSET
6400 +#define THRESHOLD 1
6403 +#define SWD_BEST_OFF (LZO_MAX3( M2_MAX_LEN, M3_MAX_LEN, M4_MAX_LEN ) + 1)
6405 +/* ../include/lzoconf.h */
6407 +typedef U32 lzo_uint32;
6408 +typedef I32 lzo_int32;
6409 +typedef U32 lzo_uint;
6410 +typedef I32 lzo_int;
6411 +typedef int lzo_bool;
6413 +#define lzo_byte U8
6414 +#define lzo_bytep U8 *
6415 +#define lzo_charp char *
6416 +#define lzo_voidp void *
6417 +#define lzo_shortp short *
6418 +#define lzo_ushortp unsigned short *
6419 +#define lzo_uint32p lzo_uint32 *
6420 +#define lzo_int32p lzo_int32 *
6421 +#define lzo_uintp lzo_uint *
6422 +#define lzo_intp lzo_int *
6423 +#define lzo_voidpp lzo_voidp *
6424 +#define lzo_bytepp lzo_bytep *
6425 +#define lzo_sizeof_dict_t sizeof(lzo_bytep)
6428 +#define LZO_E_ERROR (-1)
6429 +#define LZO_E_OUT_OF_MEMORY (-2) /* not used right now */
6430 +#define LZO_E_NOT_COMPRESSIBLE (-3) /* not used right now */
6431 +#define LZO_E_INPUT_OVERRUN (-4)
6432 +#define LZO_E_OUTPUT_OVERRUN (-5)
6433 +#define LZO_E_LOOKBEHIND_OVERRUN (-6)
6434 +#define LZO_E_EOF_NOT_FOUND (-7)
6435 +#define LZO_E_INPUT_NOT_CONSUMED (-8)
6437 +#define LZO_PTR_ALIGN_UP(_ptr,_size) \
6438 + ((_ptr) + (lzo_uint) __lzo_align_gap((const lzo_voidp)(_ptr),(lzo_uint)(_size)))
6439 +#define LZO_ALIGN(_ptr,_size) LZO_PTR_ALIGN_UP(_ptr,_size)
6442 + (*lzo_compress_t) (const lzo_byte * src, lzo_uint src_len,
6443 + lzo_byte * dst, lzo_uintp dst_len,
6444 + lzo_voidp wrkmem);
6447 + (*lzo_decompress_t) (const lzo_byte * src, lzo_uint src_len,
6448 + lzo_byte * dst, lzo_uintp dst_len,
6449 + lzo_voidp wrkmem);
6452 + (*lzo_optimize_t) (lzo_byte * src, lzo_uint src_len,
6453 + lzo_byte * dst, lzo_uintp dst_len,
6454 + lzo_voidp wrkmem);
6457 + (*lzo_compress_dict_t) (const lzo_byte * src, lzo_uint src_len,
6458 + lzo_byte * dst, lzo_uintp dst_len,
6460 + const lzo_byte * dict, lzo_uint dict_len);
6463 + (*lzo_decompress_dict_t) (const lzo_byte * src, lzo_uint src_len,
6464 + lzo_byte * dst, lzo_uintp dst_len,
6466 + const lzo_byte * dict, lzo_uint dict_len);
6469 + (*lzo_compress_asm_t) (const lzo_byte * src, lzo_uint src_len,
6470 + lzo_byte * dst, lzo_uintp dst_len,
6471 + lzo_voidp wrkmem);
6474 + (*lzo_decompress_asm_t) (const lzo_byte * src, lzo_uint src_len,
6475 + lzo_byte * dst, lzo_uintp dst_len,
6476 + lzo_voidp wrkmem);
6478 +typedef void (*lzo_progress_callback_t) (lzo_uint, lzo_uint);
6500 +#define LZO1X_1_MEM_COMPRESS ((lzo_uint32) (16384L * lzo_sizeof_dict_t))
6501 +#define LZO1X_999_MEM_COMPRESS ((lzo_uint32) (14 * 16384L * sizeof(short)))
6505 +#define PTR(a) ((lzo_ptr_t) (a))
6506 +#define PTR_LINEAR(a) PTR(a)
6507 +#define PTR_ALIGNED_4(a) ((PTR_LINEAR(a) & 3) == 0)
6508 +#define PTR_ALIGNED_8(a) ((PTR_LINEAR(a) & 7) == 0)
6509 +#define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
6510 +#define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
6511 +#define PTR_LT(a,b) (PTR(a) < PTR(b))
6512 +#define PTR_GE(a,b) (PTR(a) >= PTR(b))
6513 +#define PTR_DIFF(a,b) ((lzo_ptrdiff_t) (PTR(a) - PTR(b)))
6514 +#define pd(a,b) ((lzo_uint) ((a)-(b)))
6516 +typedef ptrdiff_t lzo_ptrdiff_t;
6521 + unsigned char a_uchar;
6523 + unsigned short a_ushort;
6525 + unsigned int a_uint;
6527 + unsigned long a_ulong;
6528 + lzo_int a_lzo_int;
6529 + lzo_uint a_lzo_uint;
6530 + lzo_int32 a_lzo_int32;
6531 + lzo_uint32 a_lzo_uint32;
6532 + ptrdiff_t a_ptrdiff_t;
6533 + lzo_ptrdiff_t a_lzo_ptrdiff_t;
6534 + lzo_ptr_t a_lzo_ptr_t;
6535 + lzo_voidp a_lzo_voidp;
6537 + lzo_bytep a_lzo_bytep;
6538 + lzo_bytepp a_lzo_bytepp;
6539 + lzo_uintp a_lzo_uintp;
6540 + lzo_uint *a_lzo_uint_p;
6541 + lzo_uint32p a_lzo_uint32p;
6542 + lzo_uint32 *a_lzo_uint32_p;
6543 + unsigned char *a_uchar_p;
6559 + lzo_uint last_m_len;
6560 + lzo_uint last_m_off;
6562 + const lzo_byte *bp;
6563 + const lzo_byte *ip;
6564 + const lzo_byte *in;
6565 + const lzo_byte *in_end;
6568 + lzo_progress_callback_t cb;
6570 + lzo_uint textsize;
6571 + lzo_uint codesize;
6572 + lzo_uint printcount;
6574 + unsigned long lit_bytes;
6575 + unsigned long match_bytes;
6576 + unsigned long rep_bytes;
6577 + unsigned long lazy;
6580 + lzo_uint r1_m_len;
6582 + unsigned long m1a_m, m1b_m, m2_m, m3_m, m4_m;
6583 + unsigned long lit1_r, lit2_r, lit3_r;
6587 +#define getbyte(c) ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
6591 +#define SWD_UINT(x) ((swd_uint)(x))
6592 +#define SWD_HSIZE 16384
6593 +#define SWD_MAX_CHAIN 2048
6594 +#define HEAD3(b,p) \
6595 + (((0x9f5f*(((((lzo_uint32)b[p]<<5)^b[p+1])<<5)^b[p+2]))>>5) & (SWD_HSIZE-1))
6596 +#define HEAD2(b,p) (b[p] ^ ((unsigned)b[p+1]<<8))
6597 +#define NIL2 SWD_UINT_MAX
6603 + lzo_uint threshold;
6605 + lzo_uint max_chain;
6606 + lzo_uint nice_length;
6607 + lzo_bool use_best_off;
6608 + lzo_uint lazy_insert;
6615 + lzo_uint best_off[SWD_BEST_OFF];
6620 + lzo_uint best_pos[SWD_BEST_OFF];
6622 + const lzo_byte *dict;
6623 + const lzo_byte *dict_end;
6624 + lzo_uint dict_len;
6631 + unsigned char *b_wrap;
6633 + lzo_uint node_count;
6634 + lzo_uint first_rp;
6636 + unsigned char b[SWD_N + SWD_F + SWD_F];
6637 + swd_uint head3[SWD_HSIZE];
6638 + swd_uint succ3[SWD_N + SWD_F];
6639 + swd_uint best3[SWD_N + SWD_F];
6640 + swd_uint llen3[SWD_HSIZE];
6642 + swd_uint head2[65536L];
6646 +#define s_head3(s,key) s->head3[key]
6647 +#define swd_pos2off(s,pos) \
6648 + (s->bp > (pos) ? s->bp - (pos) : s->b_size - ((pos) - s->bp))
6650 +static __inline__ void
6651 +swd_getbyte (lzo1x_999_swd_t * s)
6655 + if ((c = getbyte (*(s->c))) < 0)
6662 + s->b[s->ip] = LZO_BYTE (c);
6664 + s->b_wrap[s->ip] = LZO_BYTE (c);
6666 + if (++s->ip == s->b_size)
6668 + if (++s->bp == s->b_size)
6670 + if (++s->rp == s->b_size)
6675 +swd_initdict (lzo1x_999_swd_t * s, const lzo_byte * dict, lzo_uint dict_len)
6677 + s->dict = s->dict_end = NULL;
6680 + if (!dict || dict_len <= 0)
6682 + if (dict_len > s->n)
6684 + dict += dict_len - s->n;
6689 + s->dict_len = dict_len;
6690 + s->dict_end = dict + dict_len;
6691 + memcpy (s->b, dict, dict_len);
6696 +swd_insertdict (lzo1x_999_swd_t * s, lzo_uint node, lzo_uint len)
6700 + s->node_count = s->n - len;
6701 + s->first_rp = node;
6705 + key = HEAD3 (s->b, node);
6706 + s->succ3[node] = s_head3 (s, key);
6707 + s->head3[key] = SWD_UINT (node);
6708 + s->best3[node] = SWD_UINT (s->f + 1);
6711 + key = HEAD2 (s->b, node);
6712 + s->head2[key] = SWD_UINT (node);
6719 +swd_init (lzo1x_999_swd_t * s, const lzo_byte * dict, lzo_uint dict_len)
6724 + s->threshold = SWD_THRESHOLD;
6728 + s->max_chain = SWD_MAX_CHAIN;
6729 + s->nice_length = SWD_F;
6730 + s->use_best_off = 0;
6731 + s->lazy_insert = 0;
6733 + s->b_size = s->n + s->f;
6734 + if (2 * s->f >= s->n || s->b_size + s->f >= NIL2)
6735 + return LZO_E_ERROR;
6736 + s->b_wrap = s->b + s->b_size;
6737 + s->node_count = s->n;
6739 + memset (s->llen3, 0, sizeof (s->llen3[0]) * SWD_HSIZE);
6740 + memset (s->head2, 0xff, sizeof (s->head2[0]) * 65536L);
6743 + swd_initdict (s, dict, dict_len);
6745 + s->first_rp = s->ip;
6747 + s->look = (lzo_uint) (s->c->in_end - s->c->ip);
6750 + if (s->look > s->f)
6752 + memcpy (&s->b[s->ip], s->c->ip, s->look);
6753 + s->c->ip += s->look;
6757 + if (s->ip == s->b_size)
6760 + if (s->look >= 2 && s->dict_len > 0)
6761 + swd_insertdict (s, 0, s->dict_len);
6763 + s->rp = s->first_rp;
6764 + if (s->rp >= s->node_count)
6765 + s->rp -= s->node_count;
6767 + s->rp += s->b_size - s->node_count;
6772 +static __inline__ void
6773 +swd_remove_node (lzo1x_999_swd_t * s, lzo_uint node)
6775 + if (s->node_count == 0)
6779 + key = HEAD3 (s->b, node);
6783 + key = HEAD2 (s->b, node);
6785 + if ((lzo_uint) s->head2[key] == node)
6786 + s->head2[key] = NIL2;
6793 +swd_accept (lzo1x_999_swd_t * s, lzo_uint n)
6800 + swd_remove_node (s, s->rp);
6802 + key = HEAD3 (s->b, s->bp);
6803 + s->succ3[s->bp] = s_head3 (s, key);
6804 + s->head3[key] = SWD_UINT (s->bp);
6805 + s->best3[s->bp] = SWD_UINT (s->f + 1);
6808 + key = HEAD2 (s->b, s->bp);
6809 + s->head2[key] = SWD_UINT (s->bp);;
6816 +swd_search (lzo1x_999_swd_t * s, lzo_uint node, lzo_uint cnt)
6818 + const unsigned char *p1;
6819 + const unsigned char *p2;
6820 + const unsigned char *px;
6822 + lzo_uint m_len = s->m_len;
6823 + const unsigned char *b = s->b;
6824 + const unsigned char *bp = s->b + s->bp;
6825 + const unsigned char *bx = s->b + s->bp + s->look;
6826 + unsigned char scan_end1;
6828 + scan_end1 = bp[m_len - 1];
6829 + for (; cnt-- > 0; node = s->succ3[node])
6835 + if (p2[m_len - 1] == scan_end1 &&
6836 + p2[m_len] == p1[m_len] &&
6837 + p2[0] == p1[0] && p2[1] == p1[1])
6846 + while (++p1 < px && *p1 == *++p2);
6850 + if (i < SWD_BEST_OFF)
6852 + if (s->best_pos[i] == 0)
6853 + s->best_pos[i] = node + 1;
6858 + s->m_len = m_len = i;
6860 + if (m_len == s->look)
6862 + if (m_len >= s->nice_length)
6864 + if (m_len > (lzo_uint) s->best3[node])
6866 + scan_end1 = bp[m_len - 1];
6873 +swd_search2 (lzo1x_999_swd_t * s)
6877 + key = s->head2[HEAD2 (s->b, s->bp)];
6881 + if (s->best_pos[2] == 0)
6882 + s->best_pos[2] = key + 1;
6893 +swd_findbest (lzo1x_999_swd_t * s)
6896 + lzo_uint cnt, node;
6899 + key = HEAD3 (s->b, s->bp);
6900 + node = s->succ3[s->bp] = s_head3 (s, key);
6901 + cnt = s->llen3[key]++;
6903 + if (cnt > s->max_chain && s->max_chain > 0)
6904 + cnt = s->max_chain;
6905 + s->head3[key] = SWD_UINT (s->bp);
6907 + s->b_char = s->b[s->bp];
6909 + if (s->m_len >= s->look)
6914 + s->best3[s->bp] = SWD_UINT (s->f + 1);
6919 + if (swd_search2 (s))
6922 + swd_search (s, node, cnt);
6923 + if (s->m_len > len)
6924 + s->m_off = swd_pos2off (s, s->m_pos);
6925 + s->best3[s->bp] = SWD_UINT (s->m_len);
6927 + if (s->use_best_off)
6930 + for (i = 2; i < SWD_BEST_OFF; i++)
6931 + if (s->best_pos[i] > 0)
6937 + s->best_off[i] = 0;
6942 + swd_remove_node (s, s->rp);
6944 + key = HEAD2 (s->b, s->bp);
6945 + s->head2[key] = SWD_UINT (s->bp);
6952 +init_match (lzo1x_999_t * c, lzo1x_999_swd_t * s,
6953 + const lzo_byte * dict, lzo_uint dict_len, lzo_uint32 flags)
6961 + c->last_m_len = c->last_m_off = 0;
6963 + c->textsize = c->codesize = c->printcount = 0;
6964 + c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
6967 + r = swd_init (s, dict, dict_len);
6971 + s->use_best_off = (flags & 1) ? 1 : 0;
6976 +find_match (lzo1x_999_t * c, lzo1x_999_swd_t * s,
6977 + lzo_uint this_len, lzo_uint skip)
6981 + swd_accept (s, this_len - skip);
6982 + c->textsize += this_len - skip + 1;
6986 + c->textsize += this_len - skip;
6992 + if (s->use_best_off)
6993 + memset (s->best_pos, 0, sizeof (s->best_pos));
6996 + c->m_len = s->m_len;
6997 + c->m_off = s->m_off;
7001 + if (s->b_char < 0)
7008 + c->look = s->look + 1;
7010 + c->bp = c->ip - c->look;
7012 + if (c->cb && c->textsize > c->printcount)
7014 + (*c->cb) (c->textsize, c->codesize);
7015 + c->printcount += 1024;
7024 +code_match (lzo1x_999_t * c, lzo_byte * op, lzo_uint m_len, lzo_uint m_off)
7026 + lzo_uint x_len = m_len;
7027 + lzo_uint x_off = m_off;
7029 + c->match_bytes += m_len;
7035 + *op++ = LZO_BYTE (M1_MARKER | ((m_off & 3) << 2));
7036 + *op++ = LZO_BYTE (m_off >> 2);
7041 + else if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
7046 + *op++ = LZO_BYTE (((m_len - 1) << 5) | ((m_off & 7) << 2));
7047 + *op++ = LZO_BYTE (m_off >> 3);
7050 + else if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET
7051 + && c->r1_lit >= 4)
7053 + m_off -= 1 + M2_MAX_OFFSET;
7055 + *op++ = LZO_BYTE (M1_MARKER | ((m_off & 3) << 2));
7056 + *op++ = LZO_BYTE (m_off >> 2);
7060 + else if (m_off <= M3_MAX_OFFSET)
7063 + if (m_len <= M3_MAX_LEN)
7064 + *op++ = LZO_BYTE (M3_MARKER | (m_len - 2));
7067 + m_len -= M3_MAX_LEN;
7068 + *op++ = M3_MARKER | 0;
7069 + while (m_len > 255)
7074 + *op++ = LZO_BYTE (m_len);
7077 + *op++ = LZO_BYTE (m_off << 2);
7078 + *op++ = LZO_BYTE (m_off >> 6);
7087 + k = (m_off & 0x4000) >> 11;
7088 + if (m_len <= M4_MAX_LEN)
7089 + *op++ = LZO_BYTE (M4_MARKER | k | (m_len - 2));
7092 + m_len -= M4_MAX_LEN;
7093 + *op++ = LZO_BYTE (M4_MARKER | k | 0);
7094 + while (m_len > 255)
7099 + *op++ = LZO_BYTE (m_len);
7102 + *op++ = LZO_BYTE (m_off << 2);
7103 + *op++ = LZO_BYTE (m_off >> 6);
7108 + c->last_m_len = x_len;
7109 + c->last_m_off = x_off;
7114 +STORE_RUN (lzo1x_999_t * c, lzo_byte * op, const lzo_byte * ii, lzo_uint t)
7116 + c->lit_bytes += t;
7118 + if (op == c->out && t <= 238)
7120 + *op++ = LZO_BYTE (17 + t);
7124 + op[-2] |= LZO_BYTE (t);
7130 + *op++ = LZO_BYTE (t - 3);
7135 + lzo_uint tt = t - 18;
7143 + *op++ = LZO_BYTE (tt);
7154 +code_run (lzo1x_999_t * c, lzo_byte * op, const lzo_byte * ii,
7155 + lzo_uint lit, lzo_uint m_len)
7159 + op = STORE_RUN (c, op, ii, lit);
7160 + c->r1_m_len = m_len;
7173 +len_of_coded_match (lzo_uint m_len, lzo_uint m_off, lzo_uint lit)
7180 + return (m_off <= M1_MAX_OFFSET && lit > 0
7181 + && lit < 4) ? 2 : -1;
7182 + if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
7184 + if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4)
7186 + if (m_off <= M3_MAX_OFFSET)
7188 + if (m_len <= M3_MAX_LEN)
7190 + m_len -= M3_MAX_LEN;
7191 + while (m_len > 255)
7198 + if (m_off <= M4_MAX_OFFSET)
7200 + if (m_len <= M4_MAX_LEN)
7202 + m_len -= M4_MAX_LEN;
7203 + while (m_len > 255)
7214 +min_gain (lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, int l1, int l2,
7217 + lzo_int lazy_match_min_gain = 0;
7219 + lazy_match_min_gain += ahead;
7222 + lazy_match_min_gain += (lit2 <= 3) ? 0 : 2;
7223 + else if (lit1 <= 18)
7224 + lazy_match_min_gain += (lit2 <= 18) ? 0 : 1;
7226 + lazy_match_min_gain += (l2 - l1) * 2;
7228 + lazy_match_min_gain -= (ahead - l3) * 2;
7230 + if (lazy_match_min_gain < 0)
7231 + lazy_match_min_gain = 0;
7233 + return lazy_match_min_gain;
7237 +better_match (const lzo1x_999_swd_t * swd, lzo_uint * m_len, lzo_uint * m_off)
7239 + if (*m_len <= M2_MIN_LEN)
7242 + if (*m_off <= M2_MAX_OFFSET)
7245 + if (*m_off > M2_MAX_OFFSET &&
7246 + *m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
7247 + swd->best_off[*m_len - 1]
7248 + && swd->best_off[*m_len - 1] <= M2_MAX_OFFSET)
7250 + *m_len = *m_len - 1;
7251 + *m_off = swd->best_off[*m_len];
7255 + if (*m_off > M3_MAX_OFFSET &&
7256 + *m_len >= M4_MAX_LEN + 1 && *m_len <= M2_MAX_LEN + 2 &&
7257 + swd->best_off[*m_len - 2]
7258 + && swd->best_off[*m_len - 2] <= M2_MAX_OFFSET)
7260 + *m_len = *m_len - 2;
7261 + *m_off = swd->best_off[*m_len];
7265 + if (*m_off > M3_MAX_OFFSET &&
7266 + *m_len >= M4_MAX_LEN + 1 && *m_len <= M3_MAX_LEN + 1 &&
7267 + swd->best_off[*m_len - 1]
7268 + && swd->best_off[*m_len - 1] <= M3_MAX_OFFSET)
7270 + *m_len = *m_len - 1;
7271 + *m_off = swd->best_off[*m_len];
7279 +lzo_assert (int expr)
7281 + return (expr) ? 1 : 0;
7287 +lzo1x_999_compress_internal (const lzo_byte * in, lzo_uint in_len,
7288 + lzo_byte * out, lzo_uintp out_len,
7290 + const lzo_byte * dict, lzo_uint dict_len,
7291 + lzo_progress_callback_t cb,
7293 + lzo_uint good_length,
7294 + lzo_uint max_lazy,
7295 + lzo_uint nice_length,
7296 + lzo_uint max_chain, lzo_uint32 flags)
7299 + const lzo_byte *ii;
7301 + lzo_uint m_len, m_off;
7303 + lzo1x_999_t *const c = &cc;
7304 + lzo1x_999_swd_t *const swd = (lzo1x_999_swd_t *) wrkmem;
7308 + (LZO1X_999_MEM_COMPRESS >= lzo_sizeof (lzo1x_999_swd_t)))
7309 + return LZO_E_ERROR;
7314 + if (good_length <= 0)
7317 + if (max_lazy <= 0)
7320 + if (nice_length <= 0)
7323 + if (max_chain <= 0)
7324 + max_chain = SWD_MAX_CHAIN;
7327 + c->ip = c->in = in;
7328 + c->in_end = in + in_len;
7331 + c->m1a_m = c->m1b_m = c->m2_m = c->m3_m = c->m4_m = 0;
7332 + c->lit1_r = c->lit2_r = c->lit3_r = 0;
7337 + c->r1_lit = c->r1_m_len = 0;
7339 + r = init_match (c, swd, dict, dict_len, flags);
7342 + if (max_chain > 0)
7343 + swd->max_chain = max_chain;
7344 + if (nice_length > 0)
7345 + swd->nice_length = nice_length;
7347 + r = find_match (c, swd, 0, 0);
7350 + while (c->look > 0)
7353 + lzo_uint max_ahead;
7356 + c->codesize = op - out;
7366 + && (m_off > M1_MAX_OFFSET || lit == 0 || lit >= 4))
7367 + || (m_len == 2 && op == out) || (op == out && lit == 0))
7372 + else if (m_len == M2_MIN_LEN)
7375 + if (m_off > MX_MAX_OFFSET && lit >= 4)
7383 + swd->max_chain = max_chain;
7384 + r = find_match (c, swd, 1, 0);
7388 + if (swd->use_best_off)
7389 + better_match (swd, &m_len, &m_off);
7392 + if (try_lazy <= 0 || m_len >= max_lazy)
7401 + l1 = len_of_coded_match (m_len, m_off, lit);
7403 + max_ahead = LZO_MIN (try_lazy, l1 - 1);
7407 + while (ahead < max_ahead && c->look > m_len)
7409 + lzo_int lazy_match_min_gain;
7411 + if (m_len >= good_length)
7412 + swd->max_chain = max_chain >> 2;
7414 + swd->max_chain = max_chain;
7415 + r = find_match (c, swd, 1, 0);
7418 + if (c->m_len < m_len)
7421 + if (c->m_len == m_len && c->m_off >= m_off)
7424 + if (swd->use_best_off)
7425 + better_match (swd, &c->m_len, &c->m_off);
7427 + l2 = len_of_coded_match (c->m_len, c->m_off,
7432 + l3 = (op == out) ? -1 : len_of_coded_match (ahead,
7436 + lazy_match_min_gain =
7437 + min_gain (ahead, lit, lit + ahead, l1, l2,
7439 + if (c->m_len >= m_len + lazy_match_min_gain)
7446 + op = code_run (c, op, ii, lit, ahead);
7449 + op = code_match (c, op, ahead, m_off);
7455 + goto lazy_match_done;
7459 + op = code_run (c, op, ii, lit, m_len);
7462 + op = code_match (c, op, m_len, m_off);
7463 + swd->max_chain = max_chain;
7464 + r = find_match (c, swd, m_len, 1 + ahead);
7470 + op = STORE_RUN (c, op, ii, lit);
7472 + *op++ = M4_MARKER | 1;
7476 + c->codesize = op - out;
7478 + *out_len = op - out;
7481 + (*c->cb) (c->textsize, c->codesize);
7487 +lzo1x_999_compress_level (const lzo_byte * in, lzo_uint in_len,
7488 + lzo_byte * out, lzo_uintp out_len,
7490 + const lzo_byte * dict, lzo_uint dict_len,
7491 + lzo_progress_callback_t cb, int compression_level)
7493 + static const struct
7496 + lzo_uint good_length;
7497 + lzo_uint max_lazy;
7498 + lzo_uint nice_length;
7499 + lzo_uint max_chain;
7504 + 0, 0, 0, 8, 4, 0},
7506 + 0, 0, 0, 16, 8, 0},
7508 + 0, 0, 0, 32, 16, 0},
7510 + 1, 4, 4, 16, 16, 0},
7512 + 1, 8, 16, 32, 32, 0},
7514 + 1, 8, 16, 128, 128, 0},
7516 + 2, 8, 32, 128, 256, 0},
7518 + 2, 32, 128, F, 2048, 1},
7520 + 2, F, F, F, 4096, 1}
7523 + if (compression_level < 1 || compression_level > 9)
7524 + return LZO_E_ERROR;
7526 + compression_level -= 1;
7527 + return lzo1x_999_compress_internal (in, in_len, out, out_len, wrkmem,
7528 + dict, dict_len, cb,
7529 + c[compression_level].try_lazy,
7530 + c[compression_level].good_length,
7531 + c[compression_level].max_lazy,
7533 + c[compression_level].max_chain,
7534 + c[compression_level].flags);
7538 +lzo1x_999_compress (const lzo_byte * in, lzo_uint in_len,
7539 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7541 + return lzo1x_999_compress_level (in, in_len, out, out_len, wrkmem,
7547 +static const lzo_byte __lzo_copyright[] = LZO_VERSION_STRING;
7550 +_lzo1x_1_do_compress (const lzo_byte * in, lzo_uint in_len,
7551 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7554 + register const lzo_byte *ip;
7557 + const lzo_byte *const in_end = in + in_len;
7558 + const lzo_byte *const ip_end = in + in_len - 8 - 5;
7559 + const lzo_byte *ii;
7560 + lzo_dict_p const dict = (lzo_dict_p) wrkmem;
7569 + register const lzo_byte *m_pos;
7575 + DINDEX1 (dindex, ip);
7576 + GINDEX (m_pos, m_off, dict, dindex, in);
7577 + if (LZO_CHECK_MPOS_NON_DET
7578 + (m_pos, m_off, in, ip, M4_MAX_OFFSET))
7581 + if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
7583 + DINDEX2 (dindex, ip);
7584 + GINDEX (m_pos, m_off, dict, dindex, in);
7586 + if (LZO_CHECK_MPOS_NON_DET
7587 + (m_pos, m_off, in, ip, M4_MAX_OFFSET))
7589 + if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
7594 + if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
7599 + if (m_pos[2] == ip[2])
7609 + UPDATE_I (dict, 0, dindex, ip, in);
7616 + UPDATE_I (dict, 0, dindex, ip, in);
7618 + if (pd (ip, ii) > 0)
7620 + register lzo_uint t = pd (ip, ii);
7624 + op[-2] |= LZO_BYTE (t);
7627 + *op++ = LZO_BYTE (t - 3);
7630 + register lzo_uint tt = t - 18;
7638 + *op++ = LZO_BYTE (tt);;
7646 + if (m_pos[3] != *ip++ || m_pos[4] != *ip++
7647 + || m_pos[5] != *ip++ || m_pos[6] != *ip++
7648 + || m_pos[7] != *ip++ || m_pos[8] != *ip++)
7653 + if (m_off <= M2_MAX_OFFSET)
7657 + *op++ = LZO_BYTE (((m_len -
7658 + 1) << 5) | ((m_off & 7) <<
7660 + *op++ = LZO_BYTE (m_off >> 3);
7662 + else if (m_off <= M3_MAX_OFFSET)
7665 + *op++ = LZO_BYTE (M3_MARKER | (m_len - 2));
7666 + goto m3_m4_offset;
7673 + *op++ = LZO_BYTE (M4_MARKER |
7674 + ((m_off & 0x4000) >> 11) |
7676 + goto m3_m4_offset;
7682 + const lzo_byte *end = in_end;
7683 + const lzo_byte *m = m_pos + M2_MAX_LEN + 1;
7684 + while (ip < end && *m == *ip)
7686 + m_len = (ip - ii);
7690 + if (m_off <= M3_MAX_OFFSET)
7694 + *op++ = LZO_BYTE (M3_MARKER |
7699 + *op++ = M3_MARKER | 0;
7707 + if (m_len <= M4_MAX_LEN)
7708 + *op++ = LZO_BYTE (M4_MARKER |
7709 + ((m_off & 0x4000) >>
7710 + 11) | (m_len - 2));
7714 + m_len -= M4_MAX_LEN;
7715 + *op++ = LZO_BYTE (M4_MARKER |
7716 + ((m_off & 0x4000) >>
7719 + while (m_len > 255)
7725 + *op++ = LZO_BYTE (m_len);
7730 + *op++ = LZO_BYTE ((m_off & 63) << 2);
7731 + *op++ = LZO_BYTE (m_off >> 6);
7738 + *out_len = op - out;
7739 + return pd (in_end, ii);
7743 +lzo1x_1_compress (const lzo_byte * in, lzo_uint in_len,
7744 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7746 + lzo_byte *op = out;
7749 + if (in_len <= M2_MAX_LEN + 5)
7753 + t = _lzo1x_1_do_compress (in, in_len, op, out_len, wrkmem);
7759 + const lzo_byte *ii = in + in_len - t;
7761 + if (op == out && t <= 238)
7762 + *op++ = LZO_BYTE (17 + t);
7764 + op[-2] |= LZO_BYTE (t);
7766 + *op++ = LZO_BYTE (t - 3);
7769 + lzo_uint tt = t - 18;
7778 + *op++ = LZO_BYTE (tt);
7785 + *op++ = M4_MARKER | 1;
7789 + *out_len = op - out;
7794 +lzo1x_decompress (const lzo_byte * in, lzo_uint in_len,
7795 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7797 + register lzo_byte *op;
7798 + register const lzo_byte *ip;
7799 + register lzo_uint t;
7801 + register const lzo_byte *m_pos;
7803 + const lzo_byte *const ip_end = in + in_len;
7804 + lzo_byte *const op_end = out + *out_len;
7821 + goto first_literal_run;
7824 + while (TEST_IP && TEST_OP)
7842 + if (PTR_ALIGNED2_4 (op, ip))
7880 + first_literal_run:
7886 + m_pos = op - (1 + M2_MAX_OFFSET);
7888 + m_pos -= *ip++ << 2;
7889 + TEST_LOOKBEHIND (m_pos, out);
7897 + while (TEST_IP && TEST_OP)
7903 + m_pos -= (t >> 2) & 7;
7904 + m_pos -= *ip++ << 3;
7906 + TEST_LOOKBEHIND (m_pos, out);
7907 + NEED_OP (t + 3 - 1);
7927 + m_pos -= (ip[0] >> 2) + (ip[1] << 6);
7934 + m_pos -= (t & 8) << 11;
7949 + m_pos -= (ip[0] >> 2) + (ip[1] << 6);
7961 + m_pos -= *ip++ << 2;
7962 + TEST_LOOKBEHIND (m_pos, out);
7970 + TEST_LOOKBEHIND (m_pos, out);
7971 + NEED_OP (t + 3 - 1);
7972 + if (t >= 2 * 4 - (3 - 1)
7973 + && PTR_ALIGNED2_4 (op, m_pos))
7975 + COPY4 (op, m_pos);
7981 + COPY4 (op, m_pos);
8018 + *out_len = op - out;
8019 + return LZO_E_EOF_NOT_FOUND;
8022 + *out_len = op - out;
8023 + return (ip == ip_end ? LZO_E_OK :
8025 + ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
8028 + *out_len = op - out;
8029 + return LZO_E_INPUT_OVERRUN;
8032 + *out_len = op - out;
8033 + return LZO_E_OUTPUT_OVERRUN;
8035 + lookbehind_overrun:
8036 + *out_len = op - out;
8037 + return LZO_E_LOOKBEHIND_OVERRUN;
8042 +#define NO_LIT LZO_UINT_MAX
8045 +copy2 (lzo_byte * ip, const lzo_byte * m_pos, lzo_ptrdiff_t off)
8055 +copy3 (lzo_byte * ip, const lzo_byte * m_pos, lzo_ptrdiff_t off)
8060 + ip[2] = ip[1] = m_pos[0];
8062 + else if (off == 2)
8075 +lzo1x_optimize (lzo_byte * in, lzo_uint in_len,
8076 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
8078 + register lzo_byte *op;
8079 + register lzo_byte *ip;
8080 + register lzo_uint t;
8081 + register lzo_byte *m_pos;
8083 + const lzo_byte *const ip_end = in + in_len;
8084 + const lzo_byte *const op_end = out + *out_len;
8085 + lzo_byte *litp = NULL;
8087 + lzo_uint next_lit = NO_LIT;
8088 + long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
8100 + goto first_literal_run;
8103 + while (TEST_IP && TEST_OP)
8121 + first_literal_run:
8130 + m_pos = op - 1 - 0x800;
8132 + m_pos -= *ip++ << 2;
8139 + while (TEST_IP && TEST_OP)
8145 + m_pos -= *ip++ << 2;
8151 + if (nl == 0 && lit == 1 && ip[0] >= 16)
8155 + *litp = LZO_BYTE ((*litp & ~3) | lit);
8156 + copy2 (ip - 2, m_pos, op - m_pos);
8159 + else if (nl == 0 && ip[0] < 16 && ip[0] != 0
8160 + && (lit + 2 + ip[0] < 16))
8164 + copy2 (ip - 3 + 1, m_pos, op - m_pos);
8167 + memmove (litp + 1, litp, lit);
8169 + *litp = LZO_BYTE (lit - 3);
8174 + goto copy_literal_run;
8186 + m_pos -= (t >> 2) & 7;
8187 + m_pos -= *ip++ << 3;
8193 + if (t == 1 && lit > 3 && nl == 0 &&
8194 + ip[0] < 16 && ip[0] != 0
8195 + && (lit + 3 + ip[0] < 16))
8198 + copy3 (ip - 1 - 2, m_pos,
8201 + *litp = LZO_BYTE (lit - 3);
8206 + goto copy_literal_run;
8223 + m_pos -= *ip++ >> 2;
8224 + m_pos -= *ip++ << 6;
8229 + m_pos -= (t & 8) << 11;
8239 + m_pos -= *ip++ >> 2;
8240 + m_pos -= *ip++ << 6;
8249 + if (t == 1 && lit == 0 && nl == 0
8254 + *litp = LZO_BYTE ((*litp & ~3)
8256 + copy3 (ip - 3, m_pos,
8260 + else if (t == 1 && lit <= 3 && nl == 0
8261 + && ip[0] < 16 && ip[0] != 0
8262 + && (lit + 3 + ip[0] < 16))
8266 + copy3 (ip - 4 + 1, m_pos,
8270 + memmove (litp + 1,
8273 + *litp = LZO_BYTE (lit - 3);
8279 + goto copy_literal_run;
8291 + if (next_lit == NO_LIT)
8299 + next_lit = NO_LIT;
8310 + *out_len = op - out;
8311 + return LZO_E_EOF_NOT_FOUND;
8314 + *out_len = op - out;
8315 + return (ip == ip_end ? LZO_E_OK :
8317 + ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
8320 +/* interface to jffs2 bbc follows */
8322 +#include "jffs2_bbc_framework.h"
8324 +#define BLOCKSIZE 4096
8325 +#define OUTBLOCKSIZE (BLOCKSIZE + BLOCKSIZE / 64 + 16 + 3)
8327 +#define JFFS2_BBC_LZO_BLOCK_SIGN {0x3f, 0x47, 0x5a, 0x18}
8330 +jffs2_bbc_lzo_compressor_init (void);
8333 +jffs2_bbc_lzo_compressor_deinit (void);
8336 +jffs2_bbc_lzo_compress (void *model, unsigned char *input,
8337 + unsigned char *output, unsigned long *sourcelen,
8338 + unsigned long *dstlen);
8341 +jffs2_bbc_lzo_estimate (void *model, unsigned char *input,
8342 + unsigned long sourcelen, unsigned long *dstlen,
8343 + unsigned long *readtime, unsigned long *writetime);
8346 +jffs2_bbc_lzo_decompress (void *model, unsigned char *input,
8347 + unsigned char *output, unsigned long sourcelen,
8348 + unsigned long dstlen);
8351 +jffs2_bbc_lzo_proc_info (void);
8354 +jffs2_bbc_lzo_proc_command (char *command);
8356 +struct jffs2_bbc_compressor_type jffs2_bbc_lzo = {
8359 + JFFS2_BBC_LZO_BLOCK_SIGN,
8360 + jffs2_bbc_lzo_compressor_init,
8363 + jffs2_bbc_lzo_compressor_deinit,
8364 + jffs2_bbc_lzo_compress,
8365 + jffs2_bbc_lzo_estimate,
8366 + jffs2_bbc_lzo_decompress,
8367 + jffs2_bbc_lzo_proc_info,
8368 + jffs2_bbc_lzo_proc_command
8372 +no_lzo1x_optimize (lzo_byte * src, lzo_uint src_len,
8373 + lzo_byte * dst, lzo_uintp dst_len, lzo_voidp wrkmem)
8379 +static lzo_compress_t lzo1x_compressor = lzo1x_1_compress;
8380 +static lzo_optimize_t lzo1x_optimizer = no_lzo1x_optimize;
8381 +static int lzo1x_compressor_type = 1;
8382 +static int lzo1x_optimize_type = 0;
8383 +static unsigned long lzo1x_compressor_memsize = LZO1X_1_MEM_COMPRESS;
8385 +static lzo_compress_t lzo1x_compressor = lzo1x_999_compress;
8386 +static lzo_optimize_t lzo1x_optimizer = lzo1x_optimize;
8387 +static int lzo1x_compressor_type = 999;
8388 +static int lzo1x_optimize_type = 1;
8389 +static unsigned long lzo1x_compressor_memsize = LZO1X_999_MEM_COMPRESS;
8392 +static lzo_bytep wrkmem = NULL; /* temporary buffer for compression, used by lzo */
8393 +static lzo_bytep cmprssmem = NULL; /* temporary buffer for compression, used by interface */
8396 +jffs2_bbc_lzo_compressor_init (void)
8398 + wrkmem = (lzo_bytep) jffs2_bbc_malloc (lzo1x_compressor_memsize);
8399 + cmprssmem = (lzo_bytep) jffs2_bbc_malloc (OUTBLOCKSIZE);
8400 + return !(wrkmem && cmprssmem);
8404 +jffs2_bbc_lzo_compressor_deinit (void)
8406 + jffs2_bbc_free (wrkmem);
8407 + jffs2_bbc_free (cmprssmem);
8411 +jffs2_bbc_lzo_compress (void *model, unsigned char *input,
8412 + unsigned char *output, unsigned long *sourcelen,
8413 + unsigned long *dstlen)
8415 + lzo_uint csize = OUTBLOCKSIZE;
8416 + lzo_uint isize = *sourcelen;
8419 + lzo1x_compressor (input, *sourcelen, cmprssmem, &csize,
8420 + wrkmem)) != LZO_E_OK)
8422 + *sourcelen = *dstlen = 0;
8427 + retval = lzo1x_optimizer (cmprssmem, csize, input, &isize,
8430 + if (csize <= *dstlen) {
8432 + *(output++) = jffs2_bbc_lzo.block_sign[0];
8433 + *(output++) = jffs2_bbc_lzo.block_sign[1];
8434 + memcpy (output, cmprssmem, csize - 2);
8437 + *sourcelen = *dstlen = 0;
8444 +jffs2_bbc_lzo_estimate (void *model, unsigned char *input,
8445 + unsigned long sourcelen, unsigned long *dstlen,
8446 + unsigned long *readtime, unsigned long *writetime)
8448 + *dstlen = sourcelen * 55 / 100;
8449 + *readtime = JFFS2_BBC_ZLIB_READ_TIME / 2;
8450 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 8 / 10; /* LZO1X-1 is much-much faster,
8451 + but LZO1X-999 is slow. The default mode for inside kernel compression is LZO1X-1
8452 + This should be *0.4 really */
8457 +jffs2_bbc_lzo_decompress (void *model, unsigned char *input,
8458 + unsigned char *output, unsigned long sourcelen,
8459 + unsigned long dstlen)
8461 + lzo_uint outlen = dstlen;
8462 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzo.block_sign[0] ) ||
8463 + ( *(input++) != (unsigned char)jffs2_bbc_lzo.block_sign[1] )
8467 + return lzo1x_decompress (input, sourcelen - 2, output, &outlen, NULL);
8472 +jffs2_bbc_lzo_proc_info (void)
8474 + if (lzo1x_compressor_type == 1)
8476 + if (lzo1x_optimize_type == 1)
8478 + return "LZO1X-1 compression with optimization";
8482 + return "LZO1X-1 compression without optimization";
8485 + else if (lzo1x_compressor_type == 999)
8487 + if (lzo1x_optimize_type == 1)
8489 + return "LZO1X-999 compression with optimization";
8493 + return "LZO1X-999 compression without optimization";
8498 + return "Unknown configuration!";
8503 +jffs2_bbc_lzo_proc_command (char *command)
8508 + /* switch optimization off */
8509 + lzo1x_optimizer = no_lzo1x_optimize;
8510 + lzo1x_optimize_type = 0;
8511 + jffs2_bbc_print1 ("Compression optimization switched off.\n");
8514 + /* switch optimization on */
8515 + lzo1x_optimizer = lzo1x_optimize;
8516 + lzo1x_optimize_type = 1;
8517 + jffs2_bbc_print1 ("Compression optimization switched on.\n");
8520 + /* switch compression to LZO1X-1 */
8521 + jffs2_bbc_free (wrkmem);
8522 + lzo1x_compressor_type = 1;
8523 + lzo1x_compressor = lzo1x_1_compress;
8524 + lzo1x_compressor_memsize = LZO1X_1_MEM_COMPRESS;
8525 + wrkmem = (lzo_bytep)
8526 + jffs2_bbc_malloc (lzo1x_compressor_memsize);
8527 + jffs2_bbc_print1 ("Compression type switched to LZO1X-1.\n");
8530 + /* switch compression to LZO1X-999 */
8531 + jffs2_bbc_free (wrkmem);
8532 + lzo1x_compressor_type = 999;
8533 + lzo1x_compressor = lzo1x_999_compress;
8534 + lzo1x_compressor_memsize = LZO1X_999_MEM_COMPRESS;
8535 + wrkmem = (lzo_bytep)
8536 + jffs2_bbc_malloc (lzo1x_compressor_memsize);
8538 + ("Compression type switched to LZO1X-999.\n");
8541 + jffs2_bbc_print1 ("Unknown command!\n");
8547 +struct jffs2_bbc_compressor_type *
8548 +jffs2_bbc_lzo_init (int mode)
8550 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzo) == 0)
8552 + return &jffs2_bbc_lzo;
8561 +jffs2_bbc_lzo_deinit (void)
8563 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzo);
8565 diff -Nur linux-mips-cvs/fs/jffs2/jffs2_bbc_lzss_comp.c linux-mips/fs/jffs2/jffs2_bbc_lzss_comp.c
8566 --- linux-mips-cvs/fs/jffs2/jffs2_bbc_lzss_comp.c 1970-01-01 01:00:00.000000000 +0100
8567 +++ linux-mips/fs/jffs2/jffs2_bbc_lzss_comp.c 2005-02-07 05:08:34.000000000 +0100
8569 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
8572 + jffs2_bbc_lzss_comp.c -- Lempel-Ziv-Storer-Szymanski compression module for jffs2
8573 + Copyright (C) 2004 Patrik Kluba
8574 + Based on the LZSS source included in LDS (lossless datacompression sources)
8575 + Block-compression modifications by Patrik Kluba
8580 +Original copyright follows:
8582 +**************************************************************
8583 + LZSS.C -- A Data Compression Program
8584 +**************************************************************
8585 + 4/6/1989 Haruhiko Okumura
8586 + Use, distribute, and modify this program freely.
8587 + Please send me your improved versions.
8589 + NIFTY-Serve PAF01022
8590 + CompuServe 74050,1022
8591 +**************************************************************
8597 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
8604 +#define N 4096 /* size of ring buffer */
8605 +#define F 18 /* upper limit for match_length */
8606 +#define THRESHOLD 2 /* encode string into position and length
8607 + if match_length is greater than this */
8608 +#define NIL N /* index for root of binary search trees */
8610 +static unsigned char
8611 + text_buf[N + F - 1]; /* ring buffer of size N,
8612 + with extra F-1 bytes to facilitate string comparison */
8613 +static unsigned long match_position, match_length; /* of longest match. These are
8614 + set by the InsertNode() procedure. */
8615 +static unsigned long lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
8616 + parents -- These constitute binary search trees. */
8618 +static void InitTree(void) /* initialize trees */
8622 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
8623 + left children of node i. These nodes need not be initialized.
8624 + Also, dad[i] is the parent of node i. These are initialized to
8625 + NIL (= N), which stands for 'not used.'
8626 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
8627 + for strings that begin with character i. These are initialized
8628 + to NIL. Note there are 256 trees. */
8630 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
8631 + for (i = 0; i < N; i++) dad[i] = NIL;
8634 +static void InsertNode(unsigned long r)
8635 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
8636 + trees (text_buf[r]'th tree) and returns the longest-match position
8637 + and length via the global variables match_position and match_length.
8638 + If match_length = F, then removes the old node in favor of the new
8639 + one, because the old one will be deleted sooner.
8640 + Note r plays double role, as tree node and position in buffer. */
8642 + unsigned long i, p;
8643 + unsigned char *key;
8646 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
8647 + rson[r] = lson[r] = NIL; match_length = 0;
8650 + if (rson[p] != NIL) p = rson[p];
8651 + else { rson[p] = r; dad[r] = p; return; }
8653 + if (lson[p] != NIL) p = lson[p];
8654 + else { lson[p] = r; dad[r] = p; return; }
8656 + for (i = 1; i < F; i++)
8657 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
8658 + if (i > match_length) {
8659 + match_position = p;
8660 + if ((match_length = i) >= F) break;
8663 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
8664 + dad[lson[p]] = r; dad[rson[p]] = r;
8665 + if (rson[dad[p]] == p) rson[dad[p]] = r;
8666 + else lson[dad[p]] = r;
8667 + dad[p] = NIL; /* remove p */
8670 +static void DeleteNode(unsigned long p) /* deletes node p from tree */
8674 + if (dad[p] == NIL) return; /* not in tree */
8675 + if (rson[p] == NIL) q = lson[p];
8676 + else if (lson[p] == NIL) q = rson[p];
8679 + if (rson[q] != NIL) {
8680 + do { q = rson[q]; } while (rson[q] != NIL);
8681 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
8682 + lson[q] = lson[p]; dad[lson[p]] = q;
8684 + rson[q] = rson[p]; dad[rson[p]] = q;
8687 + if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
8691 +/* modified for block compression */
8692 +/* on return, srclen will contain the number of successfully compressed bytes
8693 + and dstlen will contain completed compressed bytes */
8695 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
8696 + unsigned long *dstlen)
8698 + unsigned long i, len, r, c, s, last_match_length, code_buf_ptr;
8699 + unsigned char code_buf[17], mask;
8700 + unsigned char *ip, *op;
8701 + unsigned long written = 0;
8702 + unsigned long read = 0;
8703 + unsigned char *srcend = srcbuf + *srclen;
8704 + unsigned char *dstend = dstbuf + *dstlen;
8707 + InitTree(); /* initialize trees */
8708 + code_buf[0] = 0; /* code_buf[1..16] saves eight units of code, and
8709 + code_buf[0] works as eight flags, "1" representing that the unit
8710 + is an unencoded letter (1 byte), "0" a position-and-length pair
8711 + (2 bytes). Thus, eight units require at most 16 bytes of code. */
8712 + code_buf_ptr = mask = 1;
8714 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
8715 + any character that will appear often. */
8716 + for (len = 0; (len < F) && (ip < srcend); len++)
8717 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
8720 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
8721 + each of which begins with one or more 'space' characters. Note
8722 + the order in which these strings are inserted. This way,
8723 + degenerate trees will be less likely to occur. */
8724 + InsertNode(r); /* Finally, insert the whole string just read. The
8725 + global variables match_length and match_position are set. */
8727 + if (match_length > len) match_length = len; /* match_length
8728 + may be spuriously long near the end of text. */
8729 + if (match_length <= THRESHOLD) {
8730 + match_length = 1; /* Not long enough match. Send one byte. */
8731 + code_buf[0] |= mask; /* 'send one byte' flag */
8732 + code_buf[code_buf_ptr++] = text_buf[r]; /* Send uncoded. */
8734 + code_buf[code_buf_ptr++] = match_position;
8735 + code_buf[code_buf_ptr++] = (((match_position >> 4) & 0xf0)
8736 + | (match_length - (THRESHOLD + 1))); /* Send position and
8737 + length pair. Note match_length > THRESHOLD. */
8739 + if ((mask <<= 1) == 0) { /* Shift mask left one bit. */
8740 + if ((op + code_buf_ptr) > dstend) {
8741 + *dstlen = written; /* written contains bytes of complete compressed
8745 + for (i = 0; i < code_buf_ptr; *(op++) = code_buf[i++]); /* Send at most 8 units of */
8746 + /* code together */
8747 + written += code_buf_ptr;
8748 + *srclen = read; /* this many bytes have been successfully compressed */
8749 + code_buf[0] = 0; code_buf_ptr = mask = 1;
8751 + last_match_length = match_length;
8752 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
8754 + DeleteNode(s); /* Delete old strings and */
8755 + text_buf[s] = c; /* read new bytes */
8756 + if (s < F - 1) text_buf[s + N] = c; /* If the position is
8757 + near the end of buffer, extend the buffer to make
8758 + string comparison easier. */
8759 + s = (s + 1) & (N - 1); r = (r + 1) & (N - 1);
8760 + /* Since this is a ring buffer, increment the position
8762 + InsertNode(r); /* Register the string in text_buf[r..r+F-1] */
8765 + while (i++ < last_match_length) { /* After the end of text, */
8766 + DeleteNode(s); /* no need to read, but */
8767 + s = (s + 1) & (N - 1); r = (r + 1) & (N - 1);
8768 + if (--len) InsertNode(r); /* buffer may not be empty. */
8770 + } while (len > 0); /* until length of string to be processed is zero */
8771 + if (code_buf_ptr > 1) { /* Send remaining code. */
8772 + if ((op + code_buf_ptr) > dstend) {
8773 + *dstlen = written;
8776 + for (i = 0; i < code_buf_ptr; *(op++) = code_buf[i++]);
8777 + written += code_buf_ptr;
8780 + *dstlen = written;
8784 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
8785 + unsigned long dstlen) /* Just the reverse of Encode(). */
8787 + unsigned long i, r, c, j, k, flags;
8788 + unsigned char *ip, *op;
8789 + unsigned long written;
8790 + unsigned long read;
8791 + unsigned char *srcend = srcbuf + srclen;
8792 + unsigned char *dstend = dstbuf + dstlen;
8793 + read = written = 0;
8796 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
8797 + r = N - F; flags = 0;
8799 + if (((flags >>= 1) & 256) == 0) {
8800 + if (ip >= srcend) return 0;
8802 + flags = c | 0xff00; /* uses higher byte cleverly */
8803 + } /* to count eight */
8805 + if (ip >= srcend) return 0;
8807 + if (op >= dstend) return -1;
8808 + *(op++) = text_buf[r++] = c; r &= (N - 1);
8810 + if ((ip + 2) > srcend) return 0;
8813 + i |= ((j & 0xf0) << 4); j = (j & 0x0f) + THRESHOLD;
8814 + if ((op + j + 1) > dstend) return -1;
8815 + for (k = 0; k <= j; k++) {
8816 + c = text_buf[(i + k) & (N - 1)];
8817 + *(op++) = text_buf[r++] = c; r &= (N - 1);
8823 +/* interface to jffs2 bbc follows */
8825 +#include "jffs2_bbc_framework.h"
8828 +#define JFFS2_BBC_LZSS_BLOCK_SIGN {0x27, 0x6f, 0x12, 0xc4}
8831 +jffs2_bbc_lzss_compressor_init (void);
8834 +jffs2_bbc_lzss_compressor_deinit (void);
8837 +jffs2_bbc_lzss_compress (void *model, unsigned char *input,
8838 + unsigned char *output, unsigned long *sourcelen,
8839 + unsigned long *dstlen);
8842 +jffs2_bbc_lzss_estimate (void *model, unsigned char *input,
8843 + unsigned long sourcelen, unsigned long *dstlen,
8844 + unsigned long *readtime, unsigned long *writetime);
8847 +jffs2_bbc_lzss_decompress (void *model, unsigned char *input,
8848 + unsigned char *output, unsigned long sourcelen,
8849 + unsigned long dstlen);
8852 +jffs2_bbc_lzss_proc_info (void);
8855 +jffs2_bbc_lzss_proc_command (char *command);
8857 +struct jffs2_bbc_compressor_type jffs2_bbc_lzss = {
8860 + JFFS2_BBC_LZSS_BLOCK_SIGN,
8861 + jffs2_bbc_lzss_compressor_init,
8864 + jffs2_bbc_lzss_compressor_deinit,
8865 + jffs2_bbc_lzss_compress,
8866 + jffs2_bbc_lzss_estimate,
8867 + jffs2_bbc_lzss_decompress,
8868 + jffs2_bbc_lzss_proc_info,
8869 + jffs2_bbc_lzss_proc_command
8873 +jffs2_bbc_lzss_compressor_init (void)
8879 +jffs2_bbc_lzss_compressor_deinit (void)
8884 +jffs2_bbc_lzss_compress (void *model, unsigned char *input,
8885 + unsigned char *output, unsigned long *sourcelen,
8886 + unsigned long *dstlen)
8889 + unsigned long dst = *dstlen;
8890 + *(output++) = jffs2_bbc_lzss.block_sign[0];
8891 + *(output++) = jffs2_bbc_lzss.block_sign[1];
8893 + retval = Encode(input, output, sourcelen, &dst);
8900 +jffs2_bbc_lzss_estimate (void *model, unsigned char *input,
8901 + unsigned long sourcelen, unsigned long *dstlen,
8902 + unsigned long *readtime, unsigned long *writetime)
8904 + *dstlen = sourcelen * 60 / 100;
8905 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 12 / 10;
8906 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 3;
8911 +jffs2_bbc_lzss_decompress (void *model, unsigned char *input,
8912 + unsigned char *output, unsigned long sourcelen,
8913 + unsigned long dstlen)
8915 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzss.block_sign[0] ) ||
8916 + ( *(input++) != (unsigned char)jffs2_bbc_lzss.block_sign[1] )
8920 + return Decode(input, output, sourcelen - 2, dstlen);
8925 +jffs2_bbc_lzss_proc_info (void)
8927 + return "Lempel-Ziv-Storer-Szymanski compression module";
8931 +jffs2_bbc_lzss_proc_command (char *command)
8936 +struct jffs2_bbc_compressor_type *
8937 +jffs2_bbc_lzss_init (int mode)
8939 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzss) == 0)
8941 + return &jffs2_bbc_lzss;
8950 +jffs2_bbc_lzss_deinit (void)
8952 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzss);
8954 diff -Nur linux-mips-cvs/fs/jffs2/linux-2.4.25.hpatch linux-mips/fs/jffs2/linux-2.4.25.hpatch
8955 --- linux-mips-cvs/fs/jffs2/linux-2.4.25.hpatch 1970-01-01 01:00:00.000000000 +0100
8956 +++ linux-mips/fs/jffs2/linux-2.4.25.hpatch 2005-02-07 05:08:34.000000000 +0100
8965 ++ $(JFFS2_BBC_KERNEL_OBJS) \
8969 +-tristate 'Compressed ROM file system support' CONFIG_CRAMFS
8973 +F../../Documentation/Configure.help
8975 +-JFFS stats available
8976 +iConfigure.help.bbc.inc
8980 +=(de)compress->(de)compress2
8981 +-int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
8982 ++int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out,
8983 +-void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
8984 ++void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out,
8985 +?inflateEnd(&strm);
8988 ++extern int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 * sourcelen, __u32 * dstlen);
8989 ++extern void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
8991 ++int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
8992 ++ __u32 *sourcelen, __u32 *dstlen)
8994 ++ return jffs2_zlib_compress(data_in,cpage_out,sourcelen,dstlen);
8997 ++void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
8998 ++ __u32 srclen, __u32 destlen)
9000 ++ jffs2_zlib_decompress(data_in,cpage_out,srclen,destlen);
9005 +=set_act_sb before write
9007 ++#include "jffs2_bbc_framework.h" /**BBC**/
9009 +?int jffs2_commit_write
9011 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9015 +=set_act_sb before write
9017 ++#include "jffs2_bbc_framework.h" /**BBC**/
9019 +?int jffs2_garbage_collect_dnode(
9021 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9025 +=set_act_sb before read
9027 ++#include "jffs2_bbc_framework.h" /**BBC**/
9029 +?int jffs2_read_dnode(
9031 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9037 ++#include "jffs2_bbc_fs.h" /**BBC**/
9039 +?struct super_block *jffs2_read_super(
9041 ++ jffs2_bbc_load_model(sb); /**BBC**/
9043 +?void jffs2_put_super
9045 ++ jffs2_bbc_unload_model(sb); /**BBC**/
9046 +?init_jffs2_fs(void)
9049 ++ jffs2_bbc_proc_init(); /**BBC**/
9051 +?exit_jffs2_fs(void)
9053 ++ jffs2_bbc_proc_deinit(); /**BBC**/
9055 diff -Nur linux-mips-cvs/fs/jffs2/read.c linux-mips/fs/jffs2/read.c
9056 --- linux-mips-cvs/fs/jffs2/read.c 2003-11-17 02:07:44.000000000 +0100
9057 +++ linux-mips/fs/jffs2/read.c 2005-02-07 05:08:35.000000000 +0100
9062 +#include "jffs2_bbc_framework.h" /**BBC**/
9063 #include <linux/kernel.h>
9064 #include <linux/slab.h>
9065 #include <linux/jffs2.h>
9067 D2(printk(KERN_DEBUG "Data CRC matches calculated CRC %08x\n", crc));
9068 if (ri->compr != JFFS2_COMPR_NONE) {
9069 D2(printk(KERN_DEBUG "Decompress %d bytes from %p to %d bytes at %p\n", ri->csize, readbuf, ri->dsize, decomprbuf));
9070 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
9071 ret = jffs2_decompress(ri->compr, readbuf, decomprbuf, ri->csize, ri->dsize);
9073 printk(KERN_WARNING "Error: jffs2_decompress returned %d\n", ret);
9074 diff -Nur linux-mips-cvs/fs/jffs2/super.c linux-mips/fs/jffs2/super.c
9075 --- linux-mips-cvs/fs/jffs2/super.c 2003-01-11 18:53:17.000000000 +0100
9076 +++ linux-mips/fs/jffs2/super.c 2005-02-07 05:08:35.000000000 +0100
9081 +#include "jffs2_bbc_fs.h" /**BBC**/
9082 #include <linux/config.h>
9083 #include <linux/kernel.h>
9084 #include <linux/module.h>
9086 sb->s_magic = JFFS2_SUPER_MAGIC;
9087 if (!(sb->s_flags & MS_RDONLY))
9088 jffs2_start_garbage_collect_thread(c);
9089 + jffs2_bbc_load_model(sb); /**BBC**/
9094 void jffs2_put_super (struct super_block *sb)
9096 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
9097 + jffs2_bbc_unload_model(sb); /**BBC**/
9099 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
9105 + jffs2_bbc_proc_init(); /**BBC**/
9108 printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
9110 #ifdef JFFS2_OUT_OF_KERNEL
9113 static void __exit exit_jffs2_fs(void)
9115 + jffs2_bbc_proc_deinit(); /**BBC**/
9117 jffs2_destroy_slab_caches();
9119 unregister_filesystem(&jffs2_fs_type);