finally move buildroot-ng to trunk
[openwrt/staging/mkresin.git] / target / linux / generic-2.4 / patches / 003-jffs2_compression.patch
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.
7
8 +ARMLIB compression support for BBC (EXPERIMENTAL)
9 +CONFIG_JFFS2_BBC_ARMLIB
10 + This enables ARMLIB support for BBC.
11 +
12 +LZO1X-* compression support for BBC (EXPERIMENTAL)
13 +CONFIG_JFFS2_BBC_LZO
14 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
15 + compressor, beats ZLIB in everything)
16 +
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).
21 +
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)
27 +
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)
33 +
34 JFFS stats available in /proc filesystem
35 CONFIG_JFFS_PROC_FS
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
40 @@ -50,6 +50,12 @@
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
43 fi
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
49 +
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
56 @@ -0,0 +1,5 @@
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
65 @@ -0,0 +1,25 @@
66 +ARMLIB compression support for BBC (EXPERIMENTAL)
67 +CONFIG_JFFS2_BBC_ARMLIB
68 + This enables ARMLIB support for BBC.
69 +
70 +LZO1X-* compression support for BBC (EXPERIMENTAL)
71 +CONFIG_JFFS2_BBC_LZO
72 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
73 + compressor, beats ZLIB in everything)
74 +
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).
79 +
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)
85 +
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
94 @@ -0,0 +1,40 @@
95 +config JFFS2_BBC_ARMLIB
96 + bool "ARMLIB compression support for BBC (EXPERIMENTAL)"
97 + depends on JFFS2_FS && EXPERIMENTAL
98 + default y
99 + help
100 + This enables ARMLIB support for BBC.
101 +
102 +config JFFS2_BBC_LZO
103 + bool "LZO1X-* compression support for BBC (EXPERIMENTAL)"
104 + depends on JFFS2_FS && EXPERIMENTAL
105 + default y
106 + help
107 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
108 + compressor)
109 +
110 +config JFFS2_BBC_LZARI
111 + bool "LZARI compression support for BBC (EXPERIMENTAL)"
112 + depends on JFFS2_FS && EXPERIMENTAL
113 + default y
114 + help
115 + This enables LempelZiv-Storer-Szymanski compression for BBC with
116 + additional arithmetic coding (damn slow, but best compresor).
117 +
118 +config JFFS2_BBC_LZHD
119 + bool "LZHD compression support for BBC (EXPERIMENTAL)"
120 + depends on JFFS2_FS && EXPERIMENTAL
121 + default y
122 + help
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)
126 +
127 +config JFFS2_BBC_LZSS
128 + bool "LZSS compression support for BBC (EXPERIMENTAL)"
129 + depends on JFFS2_FS && EXPERIMENTAL
130 + default y
131 + help
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
138 @@ -10,9 +10,23 @@
139 # Note 2! The CFLAGS definitions are now in the main makefile...
140
141
142 +JFFS2_BBC_KERNEL_OBJS-y = jffs2_bbc_framework.o jffs2_bbc_fs.o
143 +
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
149 +
150 +JFFS2_BBC_KERNEL_OBJS := $(JFFS2_BBC_KERNEL_OBJS-y)
151 +
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
154 +
155 COMPR_OBJS := compr.o compr_rubin.o compr_rtime.o pushpull.o \
156 compr_zlib.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
161
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
165 @@ -0,0 +1,12 @@
166 +JFFS2_BBC_KERNEL_OBJS-y = jffs2_bbc_framework.o jffs2_bbc_fs.o
167 +
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
173 +
174 +JFFS2_BBC_KERNEL_OBJS := $(JFFS2_BBC_KERNEL_OBJS-y)
175 +
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
181 @@ -85,7 +85,7 @@
182 vfree(inflate_workspace);
183 }
184
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)
188 {
189 z_stream strm;
190 @@ -145,7 +145,7 @@
191 return 0;
192 }
193
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)
197 {
198 z_stream strm;
199 @@ -175,3 +175,19 @@
200 zlib_inflateEnd(&strm);
201 up(&inflate_sem);
202 }
203 +
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);
206 +
207 +int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
208 + __u32 *sourcelen, __u32 *dstlen)
209 +{
210 + return jffs2_zlib_compress(data_in,cpage_out,sourcelen,dstlen);
211 +}
212 +
213 +void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
214 + __u32 srclen, __u32 destlen)
215 +{
216 + jffs2_zlib_decompress(data_in,cpage_out,srclen,destlen);
217 +}
218 +
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
222 @@ -35,6 +35,7 @@
223 *
224 */
225
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>
230 @@ -459,6 +460,7 @@
231
232 comprbuf = kmalloc(cdatalen, GFP_KERNEL);
233 if (comprbuf) {
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);
236 }
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
241 @@ -35,6 +35,7 @@
242 *
243 */
244
245 +#include "jffs2_bbc_framework.h" /**BBC**/
246 #include <linux/kernel.h>
247 #include <linux/mtd/mtd.h>
248 #include <linux/slab.h>
249 @@ -651,6 +652,7 @@
250 writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
251
252 if (comprbuf) {
253 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
254 comprtype = jffs2_compress(writebuf, comprbuf, &datalen, &cdatalen);
255 }
256 if (comprtype) {
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
260 @@ -0,0 +1,191 @@
261 +#!/usr/bin/perl
262 +# A patch-like utility
263 +# Designed for patching different version of jffs2 with the same hpatch file
264 +#
265 +# Copyright (C) 2004, Ferenc Havasi
266 +#
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.
271 +#
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.
276 +#
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.
280 +
281 +$filename_tmp1="file1.tmp";
282 +$filename_tmp2="file2.tmp";
283 +
284 +$filename_in="";
285 +$filename_out=$filename_tmp1;
286 +$filename_cnt=0;
287 +
288 +# Modes:
289 +# 0: expecting =
290 +# 1: normal_cmd
291 +# 2: skip until =
292 +# 3: expecting F (first state)
293 +$mode=3;
294 +
295 +%rules = ();
296 +
297 +sub file_end {
298 + if (($mode!=2)&&($modified==1)) {
299 + while (<SRC>) {
300 + print DST $_;
301 + }
302 + close(SRC);
303 + close(DST);
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";
309 + while (<RES>) {
310 + print DST $_;
311 + }
312 + close(DST);
313 + close(RES);
314 + }
315 + unlink($filename_tmp1) && unlink($filename_tmp2);
316 + }
317 + else {
318 + close(SRC);
319 + close(DST);
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";
324 + while (<RES>) {
325 + print DST $_;
326 + }
327 + close(DST);
328 + close(RES);
329 + }
330 + unlink($filename_tmp1);
331 + }
332 + $modified=0;
333 + foreach $rulename (keys %rules) {
334 + if ($rules{"$rulename"}==0) { print(STDERR "On $filename_in_save error applying rule $rulename.\n"); }
335 + }
336 + %rules = ();
337 +}
338 +
339 +if ($#ARGV<0) {
340 + print ("usage: hpatch hpatch_file\n");
341 + exit;
342 +}
343 +
344 +open(CMD,"<$ARGV[0]") or die "Cannot open $ARGV[0].\n";
345 +$cmd_linenum=0;
346 +
347 +while (chomp($cmd_line=<CMD>)) {
348 + $cmd_linenum++;
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;
355 + if ($mode!=3) {
356 + file_end();
357 + }
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";;
362 + $modified=0;
363 + $mode=0;
364 + next;
365 + }
366 + if ($mode==3) {die "error: F expression expected in line $cmd_linenum\n";}
367 + if ($cmd_line =~ /^=(.*)/) {
368 + $tmp_cmd_name=$1;
369 + if (($mode!=2)&&($modified==1)) {
370 + while (<SRC>) {
371 + print DST $_;
372 + }
373 + close(SRC);
374 + close(DST);
375 + if (($cmd_name ne "")) {$rules{"$cmd_name"}=1;};
376 + $filename_cnt++;
377 + if ($filename_cnt%2==1) {
378 + $filename_in=$filename_tmp1;
379 + $filename_out=$filename_tmp2;
380 + }
381 + else {
382 + $filename_in=$filename_tmp2;
383 + $filename_out=$filename_tmp1;
384 + }
385 + }
386 + else {
387 + close(SRC);
388 + close(DST);
389 + }
390 + $mode=1;
391 + $cmd_name=$tmp_cmd_name;
392 + if (($cmd_name ne "")) {
393 + if ($rules{"$cmd_name"}==1) {
394 + $mode=2;
395 + }
396 + else {
397 + $rules{"$cmd_name"}=0;
398 + }
399 + }
400 + open(SRC,"<$filename_in") or die "Cannot open $filename_in.\n";
401 + open(DST,">$filename_out") or die "Cannot open $filename_out.\n";
402 + $modified=0;
403 + next;
404 + }
405 + if ($mode == 0) {die "error: = expression expected in line $cmd_linenum\n";}
406 + if ($mode == 2) {next;}
407 + if ($cmd_line =~ /^!(.*)/) {
408 + print "$1\n";
409 + $modified=1;
410 + next;
411 + }
412 + if ($cmd_line =~ /^\?(.*)/) {
413 + $search_str=$1;
414 + $found=0;
415 + while (<SRC>) {
416 + print DST $_;
417 + if (index($_,$search_str)>=0) {$found=1; last;}
418 + }
419 + if ($found==0) { $mode=2; }
420 + next;
421 + }
422 + if ($cmd_line =~ /^\+(.*)/) {
423 + print DST "$1\n";
424 + $modified=1;
425 + next;
426 + }
427 + if ($cmd_line =~ /^\-(.*)/) {
428 + $search_str=$1;
429 + $found=0;
430 + while (<SRC>) {
431 + if (index($_,$search_str)>=0) {$saved_line=$_; $found=1; $modified=1; last;}
432 + print DST $_;
433 + }
434 + if ($found==0) { $mode=2; }
435 + next;
436 + }
437 + if ($cmd_line =~ /^i(.*)/) {
438 + $filename_inc=$1;
439 + open(INCSRC,"<$filename_inc") or die "Cannot open $filename_inc.\n";
440 + while (<INCSRC>) {
441 + print DST $_;
442 + }
443 + next;
444 + }
445 + if ($cmd_line =~ /^I/) {
446 + print DST $saved_line;
447 + next;
448 + }
449 +}
450 +file_end();
451 +close(CMD);
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
455 @@ -0,0 +1,2224 @@
456 +/*
457 + * JFFS2-BBC: armlib compressor plugin
458 + *
459 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
460 + *
461 + * Copyright (C) 2004, Ferenc Havasi & Tamas Gergely
462 + *
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.
467 + *
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.
472 + *
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.
476 + *
477 + */
478 +
479 +#include "jffs2_bbc_framework.h"
480 +
481 +#ifdef __KERNEL__
482 +#include <linux/string.h>
483 +#else
484 +#include <string.h>
485 +#endif
486 +
487 +//ORIGIN: include/DataStructures/TypeDefs.h
488 +
489 +/*******************************************************************************
490 +* FILE: TypeDefs.h
491 +* AUTHOR: Tamás Gergely
492 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
493 +*******************************************************************************/
494 +
495 +#ifndef TYPEDEFS_H
496 +#define TYPEDEFS_H
497 +
498 +#pragma pack(4)
499 +
500 +#ifndef bool
501 +#define bool char
502 +#define true 1
503 +#define false 0
504 +#endif
505 +
506 +#ifndef u8
507 +#define u8 unsigned char
508 +#endif
509 +#ifndef u16
510 +#define u16 unsigned short
511 +#endif
512 +#ifndef u32
513 +#define u32 unsigned long
514 +#endif
515 +#ifndef s8
516 +#define s8 signed char
517 +#endif
518 +#ifndef s16
519 +#define s16 signed short
520 +#endif
521 +#ifndef s32
522 +#define s32 signed long
523 +#endif
524 +
525 +typedef struct
526 +{
527 + u32 capacity;
528 + u32 size;
529 + u32 alloc_size;
530 + void *ptr;
531 +} vector;
532 +
533 +#define VECTOR_P_END(vct) ((void*)(((char*)((vct)->ptr)) + (vct)->size))
534 +#define VECTOR_S_END(vct) ((void*)(((char*)((vct).ptr)) + (vct).size))
535 +
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 *);
548 +#endif
549 +
550 +#endif
551 +
552 +//ORIGIN: include/DataStructures/DataTypes.h
553 +
554 +/*******************************************************************************
555 +* FILE: DataTypes.h
556 +* AUTHOR: Tamás Gergely
557 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
558 +*******************************************************************************/
559 +
560 +#ifndef DATATYPES_H
561 +#define DATATYPES_H
562 +
563 +//#include "DataStructures/TypeDefs.h"
564 +
565 +typedef u16 THUMB_DataType;
566 +typedef u32 ARM_DataType;
567 +typedef u8 TokenType;
568 +typedef u8 PredictorType;
569 +typedef u8 *ProbDist;
570 +
571 +typedef vector RawData;
572 +typedef vector RawBlocks;
573 +typedef vector TokenStream;
574 +typedef vector TokenBlocks;
575 +typedef vector LatType;
576 +
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
583 +
584 +#endif
585 +
586 +//ORIGIN: include/DataStructures/BitVector.h
587 +
588 +/*******************************************************************************
589 +* FILE: BitVector.h
590 +* AUTHOR: Tamás Gergely
591 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
592 +*******************************************************************************/
593 +
594 +#ifndef BITVECTOR_H
595 +#define BITVECTOR_H
596 +
597 +//#include "DataStructures/TypeDefs.h"
598 +
599 +typedef vector BitBlocks;
600 +
601 +#pragma pack(4)
602 +
603 +typedef struct
604 +{
605 + u32 freebits;
606 + u32 capacity;
607 + u32 size;
608 + u8 *base;
609 + u8 *ptr;
610 +} BitVector;
611 +
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 *);
624 +#endif
625 +
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)
629 +
630 +#endif
631 +
632 +//ORIGIN: include/DataStructures/DecisionTree.h
633 +
634 +/*******************************************************************************
635 +* FILE: DecisionTree.h
636 +* AUTHOR: Tamás Gergely
637 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
638 +*******************************************************************************/
639 +
640 +#ifndef DECISIONTREE_H
641 +#define DECISIONTREE_H
642 +
643 +//#include "DataStructures/DataTypes.h"
644 +
645 +#pragma pack(4)
646 +
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))
656 +
657 +#define TREENODETYPE_IS_LEAF(n) (((n) == TREENODETYPE_LEAF_P) || \
658 + ((n) == TREENODETYPE_LEAF_C))
659 +
660 +
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 >=
665 +
666 +#define GET_NODE_PTR_TYPE(n) (((TreeNodeDummy*)(n))->type)
667 +
668 +typedef struct
669 +{
670 + u8 type;
671 +} TreeNodeDummy;
672 +
673 +typedef struct
674 +{
675 + u8 type; // [TREENODETYPE_NODE_BINARY]
676 + u8 attribute;
677 + PredictorType value;
678 + void *left;
679 + void *right;
680 +} TreeNodeBinary;
681 +
682 +typedef struct
683 +{
684 + u8 type; // [TREENODETYPE_LEAF_P]
685 + u16 pairs;
686 + PredictorType *probabilities;
687 +} TreeLeafP;
688 +
689 +typedef struct
690 +{
691 + u8 type; // [TREENODETYPE_LEAF_C]
692 + PredictorType predicted_class;
693 +} TreeLeafC;
694 +
695 +typedef struct
696 +{
697 + u32 high;
698 + u32 low;
699 + u32 max;
700 +} ProbabilityType;
701 +
702 +
703 +typedef struct
704 +{
705 + void *root;
706 + u16 number_of_classes;
707 + u16 number_of_predictors;
708 + PredictorType *predictor_max_values;
709 +} DecisionTree;
710 +
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 *);
715 +#endif
716 +
717 +#endif
718 +
719 +//ORIGIN: include/DataStructures/PredictorTable.h
720 +
721 +/*******************************************************************************
722 +* FILE: PredictorTable.h
723 +* AUTHOR: Tamás Gergely
724 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
725 +*******************************************************************************/
726 +
727 +#ifndef PREDICTORTABLE_H
728 +#define PREDICTORTABLE_H
729 +
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"
735 +
736 +#define NUMBER_OF_PREDICTORS_ARM 17
737 +
738 +#ifndef __KERNEL__
739 +#define NUMBER_OF_PREDICTORS_TXT 2
740 +#else
741 +#undef TXT_TOKENS
742 +#endif // __KERNEL__
743 +
744 +#ifdef TXT_TOKENS
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
750 +#else
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
756 +#endif
757 +
758 +
759 +#pragma pack(4)
760 +
761 +typedef struct
762 +{
763 + PredictorType *predictors;
764 +} PredictorTable;
765 +
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);
773 +#endif
774 +
775 +#ifndef __KERNEL__
776 +/*
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);
781 +*/
782 +#endif // __KERNEL__
783 +
784 +#endif
785 +
786 +//ORIGIN: include/DataStructures/ipack_model.h
787 +
788 +/*******************************************************************************
789 +* FILE: ipack_model.h
790 +* AUTHOR: Tamás Gergely
791 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
792 +*******************************************************************************/
793 +
794 +#ifndef IPACK_MODEL_H
795 +#define IPACK_MODEL_H
796 +
797 +//#include "DataStructures/DataTypes.h"
798 +//#include "DataStructures/DecisionTree.h"
799 +//#include "DataStructures/PredictorTable.h"
800 +
801 +#define PROBABILITY_SHIFT 12
802 +#define PROBABILITY_MAX 0x00001000l
803 +
804 +#define NUMBER_OF_TOKENS_ARM 16
805 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION_ARM 8
806 +
807 +#ifndef __KERNEL__
808 +#define NUMBER_OF_TOKENS_TXT 256
809 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION_TXT 4
810 +#else
811 +#undef TXT_TOKENS
812 +#endif // __KERNEL__
813 +
814 +#ifdef TXT_TOKENS
815 +#define NUMBER_OF_TOKENS NUMBER_OF_TOKENS_TXT
816 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION NUMBER_OF_TOKENS_PER_INSTRUCTION_TXT
817 +#else
818 +#define NUMBER_OF_TOKENS NUMBER_OF_TOKENS_ARM
819 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION NUMBER_OF_TOKENS_PER_INSTRUCTION_ARM
820 +#endif
821 +
822 +#pragma pack(4)
823 +
824 +/*
825 + Data structure of an internal node of the tree
826 +*/
827 +typedef struct
828 +{
829 + PredictorType *attribute_ptr;
830 + u32 value; // PredictorType
831 + void *right_child_ptr;
832 +} ipack_treenodeBin;
833 +/*
834 + Data structure of a leaf with probabilities
835 +*/
836 +typedef struct
837 +{
838 + u16 probabilities[0]; // PredictorType[0]
839 +} ipack_treeleafP;
840 +/*
841 + Data structure of a leaf with class prediction
842 +*/
843 +typedef struct
844 +{
845 + PredictorType predicted_class; // PredictorType
846 +} ipack_treeleafC;
847 +/*
848 + Possible data structures of a tree node
849 +*/
850 +typedef union
851 +{
852 + ipack_treenodeBin nodeBin;
853 + ipack_treeleafP leafP;
854 + ipack_treeleafC leafC;
855 +} ipack_node_data;
856 +/*
857 + Tree node
858 +*/
859 +typedef struct
860 +{
861 + u32 type; // u8
862 + ipack_node_data data; // ipack_node_data
863 +} ipack_nodetype;
864 +/*
865 + Nullnode
866 +*/
867 +typedef struct
868 +{
869 + u32 type;
870 + u16 probabilities[NUMBER_OF_TOKENS];
871 +} ipack_nullnode;
872 +/*
873 + Model for ipack project
874 +*/
875 +typedef struct
876 +{
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;
883 +} ipack_model_type;
884 +
885 +typedef struct
886 +{
887 + u32 high;
888 + u32 low;
889 +} ipack_probability_type;
890 +
891 +
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);*/
896 +
897 +#ifndef __KERNEL__
898 +/*static void ipack_model_printinfo(ipack_model_type *);
899 +static void ipack_dumpmodel(void *);*/
900 +#endif
901 +
902 +#endif
903 +
904 +//ORIGIN: include/Builders/PredictorGenerator.h
905 +
906 +/*******************************************************************************
907 +* FILE: PredictorGenerator.h
908 +* AUTHOR: Tamás Gergely
909 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
910 +*******************************************************************************/
911 +
912 +#ifndef PREDICTORGENERATOR_H
913 +#define PREDICTORGENERATOR_H
914 +
915 +//#include "DataStructures.h"
916 +
917 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
918 +static PredictorTable *predictorgenerator_generate(void);
919 +#endif
920 +
921 +#endif
922 +
923 +//ORIGIN: include/Builders/Coder.h
924 +
925 +/*******************************************************************************
926 +* FILE: Coder.h
927 +* AUTHOR: Tamás Gergely
928 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
929 +*******************************************************************************/
930 +
931 +#ifndef CODER_H
932 +#define CODER_H
933 +
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
939 +
940 +#endif
941 +
942 +//ORIGIN: DataStructures/src/TypeDefs.c
943 +
944 +/*******************************************************************************
945 +* FILE: TypeDefs.c
946 +* AUTHOR: Tamás Gergely
947 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
948 +*******************************************************************************/
949 +
950 +//#include "ipack_common.h"
951 +//#include "DataStructures/TypeDefs.h"
952 +#ifndef __KERNEL__
953 +#include <memory.h>
954 +#endif
955 +
956 +#define VECTOR_ALLOC_SIZE 0x00001000
957 +
958 +static void vector_clear(vector * vct)
959 +{
960 + if (vct->ptr)
961 + jffs2_bbc_free(vct->ptr);
962 + vct->capacity = 0;
963 + vct->size = 0;
964 + vct->ptr = 0;
965 +}
966 +
967 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
968 +static void vector_extend(vector * vct)
969 +{
970 + void *tmp;
971 + vct->capacity += vct->alloc_size;
972 + tmp = jffs2_bbc_malloc(vct->capacity);
973 + if (vct->ptr) {
974 + memcpy(tmp, vct->ptr, vct->size);
975 + jffs2_bbc_free(vct->ptr);
976 + }
977 + vct->ptr = tmp;
978 +}
979 +
980 +static void vector_reset(vector * vct)
981 +{
982 + vct->capacity = 0;
983 + vct->size = 0;
984 + vct->alloc_size = VECTOR_ALLOC_SIZE;
985 + vct->ptr = 0;
986 +}
987 +
988 +static void vector_clr_ptr(vector * vct)
989 +{
990 + void **it;
991 + void *end_it;
992 + for (it = vct->ptr, end_it = (((char *) (vct->ptr)) + vct->size); it != end_it; it++) {
993 + vector_clear(*it);
994 + jffs2_bbc_free(*it);
995 + }
996 + if (vct->ptr)
997 + jffs2_bbc_free(vct->ptr);
998 + vct->capacity = 0;
999 + vct->size = 0;
1000 + vct->ptr = 0;
1001 +}
1002 +
1003 +static void vector_add_u8(vector * vct, u8 val)
1004 +{
1005 + if ((vct->size) + sizeof(u8) > (vct->capacity)) {
1006 + vector_extend(vct);
1007 + }
1008 + *(u8 *) ((char *) (vct->ptr) + (vct->size)) = val;
1009 + vct->size += sizeof(u8);
1010 +};
1011 +
1012 +static void vector_add_u16(vector * vct, u16 val)
1013 +{
1014 + if ((vct->size) + sizeof(u16) > (vct->capacity)) {
1015 + vector_extend(vct);
1016 + }
1017 + *(u16 *) ((char *) (vct->ptr) + (vct->size)) = val;
1018 + vct->size += sizeof(u16);
1019 +};
1020 +
1021 +static void vector_add_u32(vector * vct, u32 val)
1022 +{
1023 + if ((vct->size) + sizeof(u32) > (vct->capacity)) {
1024 + vector_extend(vct);
1025 + }
1026 + *(u32 *) ((char *) (vct->ptr) + (vct->size)) = val;
1027 + vct->size += sizeof(u32);
1028 +};
1029 +
1030 +static void vector_add_s8(vector * vct, s8 val)
1031 +{
1032 + if ((vct->size) + sizeof(s8) > (vct->capacity)) {
1033 + vector_extend(vct);
1034 + }
1035 + *(s8 *) ((char *) (vct->ptr) + (vct->size)) = val;
1036 + vct->size += sizeof(s8);
1037 +};
1038 +
1039 +static void vector_add_s16(vector * vct, s16 val)
1040 +{
1041 + if ((vct->size) + sizeof(s16) > (vct->capacity)) {
1042 + vector_extend(vct);
1043 + }
1044 + *(s16 *) ((char *) (vct->ptr) + (vct->size)) = val;
1045 + vct->size += sizeof(s16);
1046 +};
1047 +
1048 +static void vector_add_s32(vector * vct, s32 val)
1049 +{
1050 + if ((vct->size) + sizeof(s32) > (vct->capacity)) {
1051 + vector_extend(vct);
1052 + }
1053 + *(s32 *) ((char *) (vct->ptr) + (vct->size)) = val;
1054 + vct->size += sizeof(s32);
1055 +};
1056 +
1057 +static void vector_add_ptr(vector * vct, void *ptr)
1058 +{
1059 + if ((vct->size) + sizeof(void *) > (vct->capacity)) {
1060 + vector_extend(vct);
1061 + }
1062 + *(void **) ((char *) (vct->ptr) + (vct->size)) = ptr;
1063 + vct->size += sizeof(void *);
1064 +}
1065 +
1066 +static void vector_concat(vector * lhs, vector * rhs)
1067 +{
1068 + void *tmp;
1069 + if (!(rhs->size)) {
1070 + return;
1071 + }
1072 + tmp = lhs->ptr;
1073 + lhs->capacity = (lhs->size) + (rhs->size);
1074 + lhs->ptr = jffs2_bbc_malloc(lhs->capacity);
1075 + if (tmp) {
1076 + memcpy(lhs->ptr, tmp, lhs->size);
1077 + jffs2_bbc_free(tmp);
1078 + }
1079 + memcpy((((u8 *) lhs->ptr) + lhs->size), rhs->ptr, rhs->size);
1080 + lhs->size += rhs->size;
1081 +}
1082 +
1083 +#endif
1084 +
1085 +//ORIGIN: DataStructures/src/BitVector.c
1086 +
1087 +/*******************************************************************************
1088 +* FILE: BitVector.c
1089 +* AUTHOR: Tamás Gergely
1090 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1091 +*******************************************************************************/
1092 +
1093 +//#include "ipack_common.h"
1094 +//#include "DataStructures/BitVector.h"
1095 +#ifndef __KERNEL__
1096 +#include <memory.h>
1097 +#endif
1098 +
1099 +#define VECTOR_ALLOC_SIZE 0x00001000
1100 +
1101 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1102 +
1103 +static void bitblocks_clear(BitBlocks * this)
1104 +{
1105 + BitVector **it;
1106 + void *end_it;
1107 + for (it = this->ptr, end_it = VECTOR_P_END(this); it != end_it; it++) {
1108 + bitvector_clear(*it);
1109 + jffs2_bbc_free(*it);
1110 + }
1111 + jffs2_bbc_free(this->ptr);
1112 + this->ptr = 0;
1113 +}
1114 +
1115 +static void bitvector_clear(BitVector * this)
1116 +{
1117 + if (this->base) {
1118 + jffs2_bbc_free(this->base);
1119 + }
1120 + this->freebits = 0;
1121 + this->capacity = 0;
1122 + this->size = 0;
1123 + this->base = 0;
1124 + this->ptr = 0;
1125 +}
1126 +
1127 +static void bitvector_W_reset(BitVector * this)
1128 +{
1129 + this->freebits = 0;
1130 + this->capacity = 0;
1131 + this->size = 0;
1132 + this->base = 0;
1133 + this->ptr = 0;
1134 +}
1135 +
1136 +static void bitvector_W_add0(BitVector * this)
1137 +{
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);
1146 + }
1147 + else {
1148 + this->ptr++;
1149 + }
1150 + this->size++;
1151 + this->freebits = 7;
1152 + *(this->ptr) = 0x00;
1153 + }
1154 + else {
1155 + this->freebits--;
1156 + (*(this->ptr)) <<= 1;
1157 + }
1158 +}
1159 +
1160 +static void bitvector_W_add1(BitVector * this)
1161 +{
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);
1170 + }
1171 + else {
1172 + this->ptr++;
1173 + }
1174 + this->size++;
1175 + this->freebits = 7;
1176 + *(this->ptr) = 0x01;
1177 + }
1178 + else {
1179 + this->freebits--;
1180 + (*(this->ptr)) <<= 1;
1181 + (*(this->ptr)) |= 0x01;
1182 + }
1183 +}
1184 +
1185 +static void bitvector_W_concat_b(BitVector * lhs, BitVector * rhs)
1186 +{
1187 + void *tmp;
1188 + if (!(rhs->size)) {
1189 + return;
1190 + }
1191 + tmp = lhs->base;
1192 + lhs->capacity = ((((lhs->size) + (rhs->size) - 1) / VECTOR_ALLOC_SIZE) + 1) * VECTOR_ALLOC_SIZE;
1193 + lhs->base = jffs2_bbc_malloc(lhs->capacity);
1194 + if (tmp) {
1195 + memcpy(lhs->base, tmp, lhs->size);
1196 + jffs2_bbc_free(tmp);
1197 + }
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;
1202 +}
1203 +
1204 +static void bitvector_W_concat_v(BitVector * lhs, vector * rhs)
1205 +{
1206 + void *tmp;
1207 + if (!(rhs->size)) {
1208 + return;
1209 + }
1210 + tmp = lhs->base;
1211 + lhs->capacity = ((((lhs->size) + (rhs->size) - 1) / VECTOR_ALLOC_SIZE) + 1) * VECTOR_ALLOC_SIZE;
1212 + lhs->base = jffs2_bbc_malloc(lhs->capacity);
1213 + if (tmp) {
1214 + memcpy(lhs->base, tmp, lhs->size);
1215 + jffs2_bbc_free(tmp);
1216 + }
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;
1221 +}
1222 +
1223 +static void bitvector_W_flush(BitVector * this)
1224 +{
1225 + (*(this->ptr)) <<= this->freebits;
1226 + this->freebits = 0;
1227 +}
1228 +
1229 +static void bitvector_R_reset(BitVector * this)
1230 +{
1231 + this->freebits = 7;
1232 + this->ptr = this->base;
1233 +}
1234 +
1235 +static u8 bitvector_R_get1(BitVector * this)
1236 +{
1237 + u8 tmp = ((*(this->ptr)) >> this->freebits) & 0x01;
1238 + if (!(this->freebits)) {
1239 + this->freebits = 7;
1240 + this->ptr++;
1241 + }
1242 + else {
1243 + this->freebits--;
1244 + }
1245 + return tmp;
1246 +}
1247 +
1248 +static u8 bitvector_R_get8(BitVector * this)
1249 +{
1250 + u8 tmp = (*(this->ptr));
1251 + this->ptr++;
1252 + return tmp;
1253 +}
1254 +
1255 +#endif
1256 +
1257 +//ORIGIN: DataStructures/src/DecisionTree.c
1258 +
1259 +/*******************************************************************************
1260 +* FILE: DecisionTree.c
1261 +* AUTHOR: Tamás Gergely
1262 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1263 +*******************************************************************************/
1264 +
1265 +//#include "ipack_common.h"
1266 +//#include "DataStructures/DecisionTree.h"
1267 +
1268 +static void decisiontree_delete_node(void *root)
1269 +{
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);
1274 + }
1275 + else if ((tmp) == TREENODETYPE_LEAF_P) {
1276 + if (((TreeLeafP *) root)->probabilities) {
1277 + jffs2_bbc_free(((TreeLeafP *) root)->probabilities);
1278 + }
1279 + }
1280 + else if ((tmp) == TREENODETYPE_LEAF_C) {
1281 + }
1282 + jffs2_bbc_free(root);
1283 +}
1284 +
1285 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1286 +
1287 +static void decisiontree_delete(DecisionTree * dt)
1288 +{
1289 + decisiontree_delete_node(dt->root);
1290 + jffs2_bbc_free(dt->predictor_max_values);
1291 +}
1292 +
1293 +static void decisiontree_get_probability_for_token(void *root, PredictorType * preds, TokenType token, ProbabilityType * prob)
1294 +{
1295 + void *tmp = root;
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;
1300 + }
1301 + else {
1302 + tmp = ((TreeNodeBinary *) tmp)->right;
1303 + }
1304 + }
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;
1308 + }
1309 + else {
1310 + tmp = ((TreeNodeBinary *) tmp)->right;
1311 + }
1312 + }
1313 + }
1314 + prob->high = 0;
1315 + prob->low = 0;
1316 + prob->max = 0;
1317 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_P) {
1318 + u32 i;
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++];
1323 + if (token > at)
1324 + prob->low += av;
1325 + if (token >= at)
1326 + prob->high += av;
1327 + prob->max += av;
1328 + }
1329 + }
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;
1334 + }
1335 + }
1336 +}
1337 +
1338 +static TokenType decisiontree_get_token_for_range(void *root, PredictorType * preds, u32 value, u32 range, ProbabilityType * prob)
1339 +{
1340 + void *tmp = root;
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;
1346 + }
1347 + else {
1348 + tmp = ((TreeNodeBinary *) tmp)->right;
1349 + }
1350 + }
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;
1354 + }
1355 + else {
1356 + tmp = ((TreeNodeBinary *) tmp)->right;
1357 + }
1358 + }
1359 + }
1360 + prob->high = 0;
1361 + prob->low = 0;
1362 + prob->max = 0;
1363 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_P) {
1364 + u32 i;
1365 + u32 norm;
1366 + TokenType at = 0;
1367 + TokenType av;
1368 + u32 lngth = ((TreeLeafP *) tmp)->pairs << 1;
1369 + for (i = 0; i < lngth;) {
1370 + i++;
1371 + prob->max += ((TreeLeafP *) tmp)->probabilities[i++];
1372 + }
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++];
1377 + prob->high += av;
1378 + if (prob->high <= norm)
1379 + prob->low += av;
1380 + }
1381 + token = at;
1382 + }
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;
1387 + }
1388 + return token;
1389 +}
1390 +#endif
1391 +
1392 +//ORIGIN: DataStructures/src/PredictorTable.c
1393 +
1394 +/*******************************************************************************
1395 +* FILE: PredictorTable.c
1396 +* AUTHOR: Tamás Gergely
1397 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1398 +*******************************************************************************/
1399 +
1400 +//#include "ipack_common.h"
1401 +//#include "DataStructures/PredictorTable.h"
1402 +
1403 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1404 +
1405 +static void predictortable_clear(PredictorTable * table)
1406 +{
1407 + table->predictors = 0;
1408 +}
1409 +
1410 +static void predictortable_free(PredictorTable * table)
1411 +{
1412 + if (table->predictors) {
1413 + jffs2_bbc_free(table->predictors);
1414 + table->predictors = 0;
1415 + }
1416 +}
1417 +
1418 +static void predictortable_resetARM(PredictorTable * table)
1419 +{
1420 + register PredictorType *ptr = table->predictors;
1421 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS_ARM;
1422 + while (ptr < end) {
1423 + *(ptr++) = 0;
1424 + }
1425 +}
1426 +
1427 +static void predictortable_updateARM(PredictorTable * table, TokenType token)
1428 +{
1429 + register PredictorType *ptr = table->predictors;
1430 + register u32 ndx = ptr[0] + 1;
1431 + ptr[ndx + 8] = ptr[ndx];
1432 + ptr[ndx] = token;
1433 + if (ndx == 8) {
1434 + ptr[0] = 0;
1435 + }
1436 + else {
1437 + ++ptr[0];
1438 + }
1439 +}
1440 +
1441 +static PredictorType predictortable_minvalueARM(PredictorTable * table, u32 index)
1442 +{
1443 + return 0;
1444 +}
1445 +
1446 +static PredictorType predictortable_maxvalueARM(PredictorTable * table, u32 index)
1447 +{
1448 + if (index == 0) {
1449 + return 7;
1450 + }
1451 + else {
1452 + return 15;
1453 + }
1454 +}
1455 +
1456 +#endif
1457 +
1458 +#ifndef __KERNEL__
1459 +
1460 +/*static void predictortable_resetTXT(PredictorTable * table)
1461 +{
1462 + register PredictorType *ptr = table->predictors;
1463 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS_TXT;
1464 + while (ptr < end) {
1465 + *(ptr++) = 0;
1466 + }
1467 +}
1468 +
1469 +static void predictortable_updateTXT(PredictorTable * table, TokenType token)
1470 +{ //TODO: modify
1471 + register PredictorType *ptr = table->predictors;
1472 +// register u32 ndx;
1473 + ptr[0] = token;
1474 + if ((('a' <= token) && (token <= 'z')) || (('A' <= token) && (token <= 'Z'))) {
1475 + ++(ptr[1]);
1476 + }
1477 + else {
1478 + ptr[1] = 0;
1479 + }
1480 +}
1481 +
1482 +static PredictorType predictortable_minvalueTXT(PredictorTable * table, u32 index)
1483 +{
1484 + return 0;
1485 +}
1486 +
1487 +static PredictorType predictortable_maxvalueTXT(PredictorTable * table, u32 index)
1488 +{ //TODO: modify
1489 + return 254;
1490 +}*/
1491 +
1492 +#endif // __KERNEL__
1493 +
1494 +//ORIGIN: DataStructures/src/ipack_model.c
1495 +
1496 +/*******************************************************************************
1497 +* FILE: ipack_model.c
1498 +* AUTHOR: Tamás Gergely
1499 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1500 +*******************************************************************************/
1501 +
1502 +//#include "DataStructures/ipack_model.h"
1503 +//#include "measuredef.h"
1504 +//#include "ipack_common.h"
1505 +
1506 +#ifdef __MEASURE_TIME_MODEL_GETPROB
1507 +#define __MT_P_MAX 256
1508 +#define __MT_P_DIV 128
1509 +#define __MT_P_MIN 0
1510 +#endif
1511 +
1512 +static void ipack_model_get_probability_for_token(ipack_nodetype * tmp, TokenType token, ipack_probability_type * prob)
1513 +{
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);
1520 + }
1521 + else {
1522 + tmp = tmp->data.nodeBin.right_child_ptr;
1523 + }
1524 + }
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);
1528 + }
1529 + else {
1530 + tmp = tmp->data.nodeBin.right_child_ptr;
1531 + }
1532 + }
1533 + }
1534 + prob->high = 0;
1535 + prob->low = 0;
1536 +// prob->max = 0;
1537 + if (tmp->type == TREENODETYPE_LEAF_P) {
1538 + if (token) {
1539 + prob->low = tmp->data.leafP.probabilities[token - 1];
1540 + }
1541 + prob->high = tmp->data.leafP.probabilities[token];
1542 +// prob->max = tmp->data.leafP.probabilities[15];
1543 + }
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;
1548 + }
1549 + }
1550 +}
1551 +
1552 +#ifndef IPACK_ARM_ASM
1553 +
1554 +//return ipack_model_get_token_for_range2(tmp,value,range,prob);
1555 +
1556 +static TokenType ipack_model_get_token_for_range(ipack_nodetype * tmp, u32 value, u32 range, ipack_probability_type * prob)
1557 +{
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);
1565 + }
1566 + else {
1567 + tmp = tmp->data.nodeBin.right_child_ptr;
1568 + }
1569 + }
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);
1573 + }
1574 + else {
1575 + tmp = tmp->data.nodeBin.right_child_ptr;
1576 + }
1577 + }
1578 + }
1579 + prob->high = 0;
1580 + prob->low = 0;
1581 +// prob->max = 0;
1582 + if (tmp->type == TREENODETYPE_LEAF_P) {
1583 + u32 i;
1584 + u32 norm;
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) {
1589 + break;
1590 + }
1591 + }*/
1592 + norm = ((value << PROBABILITY_SHIFT) - 1);
1593 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1594 + if (range * tmp->data.leafP.probabilities[i] > norm) {
1595 + break;
1596 + }
1597 + }
1598 + token = (TokenType) i;
1599 + prob->high = tmp->data.leafP.probabilities[i];
1600 + if (token) {
1601 + prob->low = tmp->data.leafP.probabilities[token - 1];
1602 + }
1603 + }
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;
1608 + }
1609 + return token;
1610 +}
1611 +#endif
1612 +/*
1613 +void ipack_model_predictortable_reset(PredictorType* ptr)
1614 +{
1615 +// register PredictorType* ptr = model->predictors_ptr;
1616 +// register PredictorType* ptr = preds;
1617 + register PredictorType* end = ptr + NUMBER_OF_PREDICTORS;
1618 + while(ptr < end) {
1619 + *(ptr++) = 0;
1620 + }
1621 +}
1622 +
1623 +void ipack_model_predictortable_update(PredictorType* ptr, TokenType token)
1624 +{
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];
1629 + ptr[ndx] = token;
1630 + if(ndx == 8) {
1631 + ptr[0] = 0;
1632 + } else {
1633 + ++ ptr[0];
1634 + }
1635 +}*/
1636 +/****************************************************************************/
1637 +
1638 +#ifndef __KERNEL__
1639 +static void ipack_model_countpreds(void *ptr, ipack_nodetype * node, double *table, double val)
1640 +{
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);
1645 + }
1646 + else {
1647 + }
1648 +}
1649 +
1650 +/*static void ipack_model_printinfo(ipack_model_type * model)
1651 +{
1652 + double *prcnt = jffs2_bbc_malloc(sizeof(double) * NUMBER_OF_PREDICTORS);
1653 + u32 i;
1654 + for (i = 0; i < NUMBER_OF_PREDICTORS; i++) {
1655 + prcnt[i] = 0.0;
1656 + }
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]);
1660 + }
1661 + jffs2_bbc_free(prcnt);
1662 +}*/
1663 +
1664 +static void ipack_dumpnode(unsigned char **ptr, FILE * file, char *prefs)
1665 +{
1666 + switch (*((*ptr)++)) {
1667 + u32 i;
1668 + u32 j;
1669 + u32 x;
1670 + u32 y;
1671 + case TREENODETYPE_NODE_BINARY_EQ:
1672 + x = *((*ptr)++);
1673 + y = *((*ptr)++);
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);
1676 + prefs[j] = '\t';
1677 + prefs[++j] = '|';
1678 + ipack_dumpnode(ptr, file, prefs);
1679 + prefs[j--] = 0;
1680 + ipack_dumpnode(ptr, file, prefs);
1681 + prefs[j] = 0;
1682 + break;
1683 + case TREENODETYPE_NODE_BINARY_LT:
1684 + x = *((*ptr)++);
1685 + y = *((*ptr)++);
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);
1688 + prefs[j] = '\t';
1689 + prefs[++j] = '|';
1690 + ipack_dumpnode(ptr, file, prefs);
1691 + prefs[j--] = 0;
1692 + ipack_dumpnode(ptr, file, prefs);
1693 + prefs[j] = 0;
1694 + break;
1695 + case TREENODETYPE_LEAF_P:
1696 + x = *((*ptr)++);
1697 + fprintf(file, "%s+->\tLeaf: %u pairs\n", prefs, (unsigned int)x);
1698 + (*ptr) += (x << 1);
1699 + break;
1700 + case TREENODETYPE_LEAF_C:
1701 + x = *((*ptr)++);
1702 + fprintf(file, "%s+->\tLeaf: class %u\n", prefs, (unsigned int)x);
1703 + break;
1704 + default:
1705 + fprintf(file, "%s+->\tLeaf: nullnode\n", prefs);
1706 + }
1707 +}
1708 +
1709 +/*static void ipack_dumpmodel(void *model)
1710 +{
1711 + unsigned char *tmp_ptr = model;
1712 + FILE *file;
1713 + char C[4096];
1714 + if ((file = fopen("DUMPED_MODEL", "wa"))) {
1715 + int i;
1716 + for (i = 0; i < 4096; C[i++] = 0);
1717 + tmp_ptr += 8;
1718 + tmp_ptr += sizeof(u32);
1719 + ipack_dumpnode(&tmp_ptr, file, C);
1720 + fclose(file);
1721 + }
1722 +}*/
1723 +
1724 +#endif
1725 +
1726 +//ORIGIN: Builders/src/PredictorGenerator.c
1727 +
1728 +/*******************************************************************************
1729 +* FILE: PredictorGenerator.c
1730 +* AUTHOR: Tamás Gergely
1731 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1732 +*******************************************************************************/
1733 +
1734 +//#include "ipack_common.h"
1735 +//#include "Builders/PredictorGenerator.h"
1736 +
1737 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1738 +static PredictorTable *predictorgenerator_generate( /*PredictorGeneratorSettings* settings */ )
1739 +{
1740 + PredictorTable *ptr = jffs2_bbc_malloc(sizeof(PredictorTable));
1741 + predictortable_clear(ptr);
1742 + ptr->predictors = jffs2_bbc_malloc(NUMBER_OF_PREDICTORS * sizeof(PredictorType));
1743 + return ptr;
1744 +}
1745 +#endif
1746 +
1747 +//ORIGIN: Builders/src/ipack_armlib_compressor.c
1748 +
1749 +/*******************************************************************************
1750 +* FILE: ipack_armlim_compressor.c
1751 +* AUTHOR: Tamás Gergely
1752 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1753 +*******************************************************************************/
1754 +
1755 +//#include "ipack_common.h"
1756 +//#include "DataStructures.h"
1757 +//#include "Builders/PredictorGenerator.h"
1758 +//#include "Builders/Tokenizer.h"
1759 +//#include "Builders/Coder.h"
1760 +
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
1774 +
1775 +/*******************************************************************************
1776 +
1777 + COMPRESSOR INIT FUNCTIONS
1778 +
1779 +*******************************************************************************/
1780 +
1781 +#define ROUND_UP_TO_DWORD(val) ( ( (val) + 3 ) & 0xfffffffc )
1782 +
1783 +#ifndef __KERNEL__
1784 +int ipack_glb_endian_X;
1785 +#endif
1786 +
1787 +static int ipack_compressor_init_tree(unsigned char **ptr, ipack_model_type * model, ipack_nodetype * node, void *nullnode)
1788 +{
1789 + int retval = 0;
1790 + node->type = *((*ptr)++);
1791 + switch (node->type) {
1792 + u32 i;
1793 + u32 j;
1794 + u32 lngth;
1795 + u32 tmpret;
1796 + TokenType at;
1797 + u16 av;
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);
1806 + break;
1807 + case TREENODETYPE_LEAF_P:
1808 + lngth = *((*ptr)++);
1809 + av = 0;
1810 + for (i = 0, j = 0; i < lngth; ++i) {
1811 + at = *((*ptr)++);
1812 + while (j < at) {
1813 + node->data.leafP.probabilities[j++] = av;
1814 + }
1815 + av += *((*ptr)++);
1816 + }
1817 + while (j < NUMBER_OF_TOKENS) {
1818 + node->data.leafP.probabilities[j++] = av;
1819 + }
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]);
1822 + }
1823 + retval = ROUND_UP_TO_DWORD(NUMBER_OF_TOKENS * sizeof(u16));
1824 + break;
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);
1829 + break;
1830 + default:
1831 + return 0;
1832 + }
1833 + return retval + sizeof(node->type);
1834 +}
1835 +
1836 +#define IPACK_TREE_CONVERT_REPLACE 0
1837 +#define IPACK_TREE_CONVERT_KEEP 1
1838 +
1839 +static void *ipack_tree_to_code(ipack_model_type * model, int *code_size);
1840 +
1841 +static int ipack_armlib_convert_tree_to_code(ipack_model_type * model_img, int mode)
1842 +{
1843 +#ifdef IPACK_TREE_TO_CODE
1844 + int tree_size;
1845 +
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;
1852 + }
1853 +#endif
1854 + return 0;
1855 +}
1856 +
1857 +
1858 +static int ipack_armlib_compressor_init(void **model)
1859 +{
1860 + int retval = EC_NO_ERROR;
1861 + unsigned char *tmp_ptr = *model;
1862 + u32 i;
1863 + ipack_model_type *model_img;
1864 + char tmp_c[2];
1865 +
1866 + if (*(tmp_ptr++) != 'i') {
1867 + return EC_NOT_IPMF_FILE;
1868 + }
1869 + else if (*(tmp_ptr++) != 'P') {
1870 + return EC_NOT_IPMF_FILE;
1871 + }
1872 + else if (*(tmp_ptr++) != 'M') {
1873 + return EC_NOT_IPMF_FILE;
1874 + }
1875 + else if (*(tmp_ptr++) != 'F') {
1876 + return EC_NOT_IPMF_FILE;
1877 + }
1878 + tmp_c[0] = *(tmp_ptr++);
1879 + tmp_c[1] = *(tmp_ptr++);
1880 + tmp_ptr += 2;
1881 +
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...
1885 +
1886 + tmp_ptr += sizeof(u32);
1887 +
1888 + model_img->ID[0] = 'i';
1889 + model_img->ID[1] = 'P';
1890 + model_img->ID[2] = 'M';
1891 + model_img->ID[3] = 'F';
1892 +
1893 + model_img->block_sign[0] = tmp_c[0];
1894 + model_img->block_sign[1] = tmp_c[1];
1895 +
1896 + model_img->nullnode.type = TREENODETYPE_LEAF_P;
1897 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1898 + model_img->nullnode.probabilities[i] = 0;
1899 + }
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
1902 +
1903 + ipack_compressor_init_tree(&tmp_ptr, model_img, model_img->tree_root_ptr, &(model_img->nullnode));
1904 +
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);
1909 +#else
1910 + model_img->tree_code = NULL;
1911 +#endif
1912 +#else
1913 + model_img->tree_code = NULL;
1914 +#endif
1915 +
1916 + jffs2_bbc_free(*model);
1917 + *model = model_img;
1918 + return retval;
1919 +}
1920 +
1921 +/*******************************************************************************
1922 +
1923 + COMPRESSOR DEINIT FUNCTIONS
1924 +
1925 +*******************************************************************************/
1926 +
1927 +
1928 +/* Descructor of compressor (model will be freed with jffs2_bbc_free() after it)*/
1929 +static void ipack_armlib_compressor_deinit(void)
1930 +{
1931 +}
1932 +
1933 +/*******************************************************************************
1934 +
1935 + COMPRESS FUNCTIONS
1936 +
1937 +*******************************************************************************/
1938 +
1939 +static int writebits0(unsigned char **dest, u8 * freebits, u32 * opposite, unsigned char *end)
1940 +{
1941 + if (!(*freebits)) {
1942 + ++(*dest);
1943 + *freebits = 7;
1944 + **dest = 0x00;
1945 + }
1946 + else {
1947 + --(*freebits);
1948 + (**dest) <<= 1;
1949 + }
1950 + if ((*dest == end) && !(*freebits)) {
1951 + return EC_BUFFER_OVERFLOW;
1952 + }
1953 + while (*opposite) {
1954 + --(*opposite);
1955 + if (!(*freebits)) {
1956 + ++(*dest);
1957 + *freebits = 7;
1958 + **dest = 0x01;
1959 + }
1960 + else {
1961 + --(*freebits);
1962 + (**dest) <<= 1;
1963 + (**dest) |= 0x01;
1964 + }
1965 + if ((*dest == end) && !(*freebits)) {
1966 + return EC_BUFFER_OVERFLOW;
1967 + }
1968 + }
1969 + return 0;
1970 +}
1971 +
1972 +static int writebits1(unsigned char **dest, u8 * freebits, u32 * opposite, unsigned char *end)
1973 +{
1974 + if (!(*freebits)) {
1975 + ++(*dest);
1976 + *freebits = 7;
1977 + **dest = 0x01;
1978 + }
1979 + else {
1980 + --(*freebits);
1981 + (**dest) <<= 1;
1982 + (**dest) |= 0x01;
1983 + }
1984 + if ((*dest == end) && !(*freebits)) {
1985 + return EC_BUFFER_OVERFLOW;
1986 + }
1987 + while (*opposite) {
1988 + --(*opposite);
1989 + if (!(*freebits)) {
1990 + ++(*dest);
1991 + *freebits = 7;
1992 + **dest = 0x00;
1993 + }
1994 + else {
1995 + --(*freebits);
1996 + (**dest) <<= 1;
1997 + }
1998 + if ((*dest == end) && !(*freebits)) {
1999 + return EC_BUFFER_OVERFLOW;
2000 + }
2001 + }
2002 + return 0;
2003 +}
2004 +
2005 +
2006 +
2007 +
2008 +/* Compress block
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
2012 + */
2013 +static int ipack_armlib_compress(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen)
2014 +{
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;
2022 + TokenStream tmpv;
2023 + TokenType *it;
2024 + void *end_it;
2025 +
2026 + ipack_nodetype *treeroot = ((ipack_model_type *) model)->tree_root_ptr;
2027 + PredictorType *predctrs = ((ipack_model_type *) model)->predictors_ptr;
2028 +
2029 +#ifdef IPACK_TREE_TO_CODE
2030 + void (*treefunc) (ipack_nodetype *, TokenType, ipack_probability_type *);
2031 +
2032 + treefunc = ((ipack_model_type *) model)->tree_code;
2033 + if (treefunc != NULL)
2034 + treefunc += 4;
2035 +#endif
2036 +
2037 + if ((*sourcelen % 4) != 0) {
2038 + return EC_WRONG_INPUT_LENGTH;
2039 + }
2040 + if (*dstlen <= 4) {
2041 + return EC_BUFFER_OVERFLOW;
2042 + }
2043 +
2044 + if (((ipack_model_type *) model)->ID[0] != 'i') {
2045 + return EC_NOT_IPMF_MODEL;
2046 + }
2047 + else if (((ipack_model_type *) model)->ID[1] != 'P') {
2048 + return EC_NOT_IPMF_MODEL;
2049 + }
2050 + else if (((ipack_model_type *) model)->ID[2] != 'M') {
2051 + return EC_NOT_IPMF_MODEL;
2052 + }
2053 + else if (((ipack_model_type *) model)->ID[3] != 'F') {
2054 + return EC_NOT_IPMF_MODEL;
2055 + }
2056 +#ifdef TXT_TOKENS
2057 + tmpv.capacity = (*sourcelen);
2058 +#else
2059 + tmpv.capacity = (*sourcelen) << 1;
2060 +#endif
2061 + tmpv.size = tmpv.capacity;
2062 + tmpv.ptr = jffs2_bbc_malloc(tmpv.size);
2063 + it = tmpv.ptr;
2064 +
2065 +#ifndef __KERNEL__
2066 + if (ipack_glb_endian_X) {
2067 + for (tmpp = (void *) input; (u32) tmpp < (u32) (input + *sourcelen); ++tmpp) {
2068 +#ifdef TXT_TOKENS
2069 + *(it++) = (u8) ((*tmpp & 0xff000000) >> 24);
2070 + *(it++) = (u8) ((*tmpp & 0x00ff0000) >> 16);
2071 + *(it++) = (u8) ((*tmpp & 0x0000ff00) >> 8);
2072 + *(it++) = (u8) ((*tmpp & 0x000000ff));
2073 +#else
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
2083 + }
2084 + }
2085 + else {
2086 +#endif
2087 + for (tmpp = (void *) input; (u32) tmpp < (u32) (input + *sourcelen); ++tmpp) {
2088 +#ifdef TXT_TOKENS
2089 + *(it++) = (u8) ((*tmpp & 0x000000ff));
2090 + *(it++) = (u8) ((*tmpp & 0x0000ff00) >> 8);
2091 + *(it++) = (u8) ((*tmpp & 0x00ff0000) >> 16);
2092 + *(it++) = (u8) ((*tmpp & 0xff000000) >> 24);
2093 +#else
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
2103 + }
2104 +#ifndef __KERNEL__
2105 + }
2106 +#endif
2107 +/*
2108 + ENCODE
2109 +*/
2110 + { //predictor reset
2111 + register PredictorType *ptr = predctrs;
2112 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS;
2113 + while (ptr < end) {
2114 + *(ptr++) = 0;
2115 + }
2116 + }
2117 +
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];
2122 +
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;
2127 + u32 range;
2128 +
2129 +#ifdef IPACK_TREE_TO_CODE
2130 + if (treefunc != NULL)
2131 + (*treefunc) (treeroot, *it, &prob);
2132 + else
2133 + ipack_model_get_probability_for_token(treeroot, *it, &prob);
2134 +#else
2135 + ipack_model_get_probability_for_token(treeroot, *it, &prob);
2136 +#endif
2137 +
2138 + if (prob.high == prob.low) {
2139 + vector_clear(&tmpv);
2140 + return EC_CODER_WRONG_PROBABILITY;
2141 + }
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);
2145 + for (;;) {
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;
2150 + }
2151 + }
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;
2156 + }
2157 + coder_high -= CODER_VALUEHLF;
2158 + coder_low -= CODER_VALUEHLF;
2159 + }
2160 + else if ((CODER_VALUE1ST <= coder_low) && (coder_high < CODER_VALUE3RD)) {
2161 + ++coder_opbits;
2162 + coder_high -= CODER_VALUE1ST;
2163 + coder_low -= CODER_VALUE1ST;
2164 + }
2165 + else {
2166 + break;
2167 + }
2168 + coder_high <<= 1;
2169 + ++coder_high;
2170 + coder_low <<= 1;
2171 + if (coder_high < coder_low) {
2172 + vector_clear(&tmpv);
2173 + return EC_CODER_WRONG_RANGE;
2174 + }
2175 + }
2176 + {
2177 +#ifdef TXT_TOKENS
2178 +// register u32 ndx;
2179 + predctrs[0] = *it;
2180 + if ((('a' <= *it) && (*it <= 'z')) || (('A' <= *it) && (*it <= 'Z'))) {
2181 + ++(predctrs[1]);
2182 + }
2183 + else {
2184 + predctrs[1] = 0;
2185 + }
2186 +#else
2187 + register u32 ndx = predctrs[0] + 1;
2188 + predctrs[ndx + 8] = predctrs[ndx];
2189 + predctrs[ndx] = *it;
2190 + if (ndx == 8) {
2191 + predctrs[0] = 0;
2192 + }
2193 + else {
2194 + ++predctrs[0];
2195 + }
2196 +#endif
2197 + }
2198 +
2199 + }
2200 + vector_clear(&tmpv);
2201 + ++coder_opbits;
2202 + if (coder_low < CODER_VALUE1ST) {
2203 + if (writebits0(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2204 + return EC_BUFFER_OVERFLOW;
2205 + }
2206 + }
2207 + else {
2208 + if (writebits1(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2209 + return EC_BUFFER_OVERFLOW;
2210 + }
2211 + }
2212 + (*(bitvector_ptr)) <<= bitvector_freebits;
2213 + *dstlen = ((u32) bitvector_ptr - (u32) output + 1);
2214 + return EC_NO_ERROR;
2215 +}
2216 +
2217 +/*******************************************************************************
2218 +
2219 + DECOMPRESS FUNCTIONS
2220 +
2221 +*******************************************************************************/
2222 +
2223 +typedef struct
2224 +{
2225 + u32 high;
2226 + u32 low;
2227 + u32 value;
2228 + u32 overread;
2229 +} ipack_decompressor_values;
2230 +
2231 +typedef struct
2232 +{
2233 + u8 freebits;
2234 + unsigned char *ptr;
2235 + unsigned char *end;
2236 +} ipack_decompressor_bitvector;
2237 +
2238 +static u8 ipack_bitvector_R_get1(ipack_decompressor_bitvector * bv)
2239 +{
2240 + u8 tmp;
2241 + if (bv->ptr == bv->end) {
2242 + bv->freebits = 0;
2243 + return 0;
2244 + }
2245 + tmp = (*(bv->ptr) >> bv->freebits) & 0x01;
2246 + if (!(bv->freebits)) {
2247 + bv->freebits = 7;
2248 + ++(bv->ptr);
2249 + }
2250 + else {
2251 + --(bv->freebits);
2252 + }
2253 + return tmp;
2254 +}
2255 +
2256 +/* Decompress block
2257 + * returns non zero if fails
2258 + */
2259 +static int ipack_armlib_decompress(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen)
2260 +{
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;
2267 + u32 lngth;
2268 + u32 i;
2269 + u32 cntbytes;
2270 + TokenType tkns[8];
2271 + TokenType *tptr;
2272 +
2273 + ipack_nodetype *treeroot = ((ipack_model_type *) model)->tree_root_ptr;
2274 + PredictorType *predctrs = ((ipack_model_type *) model)->predictors_ptr;
2275 +
2276 +#ifdef IPACK_TREE_TO_CODE
2277 + TokenType(*treefunc) (ipack_nodetype *, u32, u32, ipack_probability_type *);
2278 +
2279 + treefunc = ((ipack_model_type *) model)->tree_code;
2280 +#endif
2281 +
2282 +
2283 + if (((ipack_model_type *) model)->ID[0] != 'i') {
2284 + return EC_NOT_IPMF_MODEL;
2285 + }
2286 + else if (((ipack_model_type *) model)->ID[1] != 'P') {
2287 + return EC_NOT_IPMF_MODEL;
2288 + }
2289 + else if (((ipack_model_type *) model)->ID[2] != 'M') {
2290 + return EC_NOT_IPMF_MODEL;
2291 + }
2292 + else if (((ipack_model_type *) model)->ID[3] != 'F') {
2293 + return EC_NOT_IPMF_MODEL;
2294 + }
2295 +
2296 + bitvector.freebits = 7;
2297 + bitvector.ptr = input;
2298 + bitvector.end = input + sourcelen;
2299 +
2300 + /*if(*(bitvector.ptr++) != 'H') {
2301 + return EC_NOT_HG_BLOCK;
2302 + } else if(*(bitvector.ptr++) != 'G') {
2303 + return EC_NOT_HG_BLOCK;
2304 + } */
2305 + bitvector.ptr++;
2306 + bitvector.ptr++;
2307 +
2308 + data = (void *) output;
2309 + cntbytes = *(bitvector.ptr++);
2310 + cntbytes <<= 8;
2311 + cntbytes += *(bitvector.ptr++);
2312 +
2313 + { //predictor reset
2314 + register PredictorType *ptr = predctrs;
2315 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS;
2316 + while (ptr < end) {
2317 + *(ptr++) = 0;
2318 + }
2319 + }
2320 + for (i = 0; i < CODER_VALUEBITS; ++i) {
2321 + coder_value <<= 1;
2322 + coder_value += ipack_bitvector_R_get1(&bitvector);
2323 + }
2324 + lngth = dstlen >> 2;
2325 + if (lngth > (cntbytes >> 2)) {
2326 + lngth = cntbytes >> 2;
2327 + }
2328 + for (i = 0; (i < lngth); ++i) {
2329 + TokenType itoken;
2330 + u32 j;
2331 + tptr = tkns;
2332 + for (j = 0; j < NUMBER_OF_TOKENS_PER_INSTRUCTION; ++j) {
2333 + ipack_probability_type prob;
2334 + u32 range = coder_high - coder_low + 1;
2335 +
2336 +#ifdef IPACK_TREE_TO_CODE
2337 + if (treefunc != NULL)
2338 + itoken = (*treefunc) (treeroot, coder_value - coder_low + 1, range, &prob);
2339 + else
2340 +#endif
2341 + itoken = ipack_model_get_token_for_range(treeroot, coder_value - coder_low + 1, range, &prob);
2342 +
2343 +
2344 + if (prob.high == prob.low) {
2345 + return EC_CODER_WRONG_PROBABILITY;
2346 + }
2347 + coder_high = coder_low + ((range * prob.high) >> PROBABILITY_SHIFT) - 1;
2348 + coder_low += ((range * prob.low) >> PROBABILITY_SHIFT);
2349 + for (;;) {
2350 + if (coder_high < CODER_VALUEHLF) {
2351 + }
2352 + else if (CODER_VALUEHLF <= coder_low) {
2353 + coder_value -= CODER_VALUEHLF;
2354 + coder_high -= CODER_VALUEHLF;
2355 + coder_low -= CODER_VALUEHLF;
2356 + }
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;
2361 + }
2362 + else {
2363 + break;
2364 + }
2365 + coder_low <<= 1;
2366 + coder_high <<= 1;
2367 + ++(coder_high);
2368 + coder_value <<= 1;
2369 + if (bitvector.ptr == bitvector.end) {
2370 + bitvector.freebits = 0;
2371 + }
2372 + coder_value += ((*(bitvector.ptr) >> bitvector.freebits) & 0x01);
2373 + if (bitvector.freebits) {
2374 + --bitvector.freebits;
2375 + }
2376 + else {
2377 + bitvector.freebits = 7;
2378 + ++bitvector.ptr;
2379 + }
2380 + if (coder_high < coder_low) {
2381 + return EC_CODER_WRONG_RANGE;
2382 + }
2383 + if ((bitvector.ptr == bitvector.end) && !(bitvector.freebits)) {
2384 + if ((coder_overread++) > CODER_VALUEBITS) {
2385 + return EC_BUFFER_UNDERFLOW;
2386 + }
2387 + }
2388 + }
2389 + {
2390 +#ifdef TXT_TOKENS
2391 +// register u32 ndx;
2392 + predctrs[0] = itoken;
2393 + if ((('a' <= itoken) && (itoken <= 'z')) || (('A' <= itoken) && (itoken <= 'Z'))) {
2394 + ++(predctrs[1]);
2395 + }
2396 + else {
2397 + predctrs[1] = 0;
2398 + }
2399 +
2400 +#else
2401 + register u32 ndx = predctrs[0] + 1;
2402 + predctrs[ndx + 8] = predctrs[ndx];
2403 + predctrs[ndx] = itoken;
2404 + if (ndx == 8) {
2405 + predctrs[0] = 0;
2406 + }
2407 + else {
2408 + ++predctrs[0];
2409 + }
2410 +#endif
2411 + }
2412 +
2413 + (*(tptr++)) = itoken;
2414 + }
2415 + tptr = tkns;
2416 +#ifndef __KERNEL__
2417 + if (ipack_glb_endian_X) {
2418 +#ifdef TXT_TOKENS
2419 + (*data) = ((*tptr) << 24);
2420 + ++tptr;
2421 + (*data) |= ((*tptr) << 16);
2422 + ++tptr;
2423 + (*data) |= ((*tptr) << 8);
2424 + ++tptr;
2425 + (*data) |= (*tptr);
2426 + ++data;
2427 +#else
2428 + (*data) = (((*tptr) & 0xf) << 12);
2429 + ++tptr;
2430 + (*data) |= ((*tptr) & 0xf);
2431 + ++tptr;
2432 + (*data) |= (((*tptr) & 0xf) << 28);
2433 + ++tptr;
2434 + (*data) |= (((*tptr) & 0xf) << 16);
2435 + ++tptr;
2436 + (*data) |= (((*tptr) & 0xf) << 8);
2437 + ++tptr;
2438 + (*data) |= (((*tptr) & 0xf) << 20);
2439 + ++tptr;
2440 + (*data) |= (((*tptr) & 0xf) << 24);
2441 + ++tptr;
2442 + (*data) |= (((*tptr) & 0xf) << 4);
2443 + ++data;
2444 +#endif
2445 + }
2446 + else {
2447 +#endif
2448 +#ifdef TXT_TOKENS
2449 + (*data) = (*tptr);
2450 + ++tptr;
2451 + (*data) |= ((*tptr) << 8);
2452 + ++tptr;
2453 + (*data) |= ((*tptr) << 16);
2454 + ++tptr;
2455 + (*data) |= ((*tptr) << 24);
2456 + ++data;
2457 +#else
2458 + (*data) = (((*tptr) & 0xf) << 20);
2459 + ++tptr;
2460 + (*data) |= (((*tptr) & 0xf) << 24);
2461 + ++tptr;
2462 + (*data) |= (((*tptr) & 0xf) << 4);
2463 + ++tptr;
2464 + (*data) |= (((*tptr) & 0xf) << 8);
2465 + ++tptr;
2466 + (*data) |= (((*tptr) & 0xf) << 16);
2467 + ++tptr;
2468 + (*data) |= (((*tptr) & 0xf) << 12);
2469 + ++tptr;
2470 + (*data) |= ((*tptr) & 0xf);
2471 + ++tptr;
2472 + (*data) |= (((*tptr) & 0xf) << 28);
2473 + ++data;
2474 +#endif
2475 +#ifndef __KERNEL__
2476 + }
2477 +#endif
2478 + }
2479 + return EC_NO_ERROR;
2480 +}
2481 +
2482 +static int ipack_armlib_estimate(void *model, unsigned char *input, unsigned long sourcelen, unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime)
2483 +{
2484 + int i, tmp, tmp2, max, maxi;
2485 + int cnt_cond[] = { 0, 0, 0, 0 };
2486 + int cnt_inst[] = { 0, 0, 0, 0 };
2487 +
2488 + // TODO: make a more precise estimation!!!
2489 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 6;
2490 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 2;
2491 +
2492 + if (sourcelen % 4 != 0) {
2493 + *dstlen = sourcelen;
2494 + return 0;
2495 + }
2496 + for (i = 0; i < sourcelen; i++, input++) {
2497 + tmp2 = tmp = *input;
2498 + tmp = ((tmp) & 0xf0) >> 4;
2499 + tmp2 = tmp2 & 0xf;
2500 + if (tmp == 14)
2501 + cnt_cond[i % 4]++;
2502 + if ((tmp2 == 2) || (tmp2 == 3))
2503 + cnt_inst[i % 4]++;
2504 + }
2505 + maxi = -1;
2506 + max = -1;
2507 + for (i = 0; i < 4; i++)
2508 + if (max < cnt_cond[i]) {
2509 + max = cnt_cond[i];
2510 + maxi = i;
2511 + }
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]); */
2518 +
2519 + if (cnt_cond[maxi] < (sourcelen >> 4)) {
2520 + *dstlen = sourcelen;
2521 + }
2522 + else {
2523 + *dstlen = sourcelen / 3;
2524 + }
2525 +
2526 + return 0;
2527 +}
2528 +
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);
2532 +
2533 +struct jffs2_bbc_compressor_type jffs2_bbc_armlib = {
2534 + "armlib",
2535 + 0x464d5069,
2536 + {0, 0, 0, 0},
2537 + NULL, // init
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
2546 +};
2547 +
2548 +
2549 +static char *ipack_armlib_proc_info()
2550 +{
2551 +#ifdef IPACK_TREE_TO_CODE
2552 +#ifdef IPACK_AUTO_TREE_TO_CODE
2553 + return "automatic tree to code conversion";
2554 +#else
2555 + return "manual tree to code conversion possibility";
2556 +#endif
2557 +#else
2558 + return "tree in memory version";
2559 +#endif
2560 +}
2561 +
2562 +static int ipack_armlib_proc_command(char *command)
2563 +{
2564 + struct jffs2_bbc_model_list_node *model;
2565 + ipack_model_type *armlib_model;
2566 +
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");
2576 + }
2577 + else {
2578 + ipack_armlib_convert_tree_to_code(armlib_model, IPACK_TREE_CONVERT_KEEP);
2579 + }
2580 + model = model->next_compr_model;
2581 + }
2582 + }
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");
2592 + }
2593 + else {
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);
2597 + }
2598 + model = model->next_compr_model;
2599 + }
2600 + }
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");
2610 + }
2611 + else {
2612 + if (armlib_model->tree_code == NULL) {
2613 + jffs2_bbc_print1("already deleted.\n");
2614 + }
2615 + else {
2616 + if (armlib_model->tree_root_ptr == NULL) {
2617 + jffs2_bbc_print1("cannot delete this ARM tree - original tree has deleted\n");
2618 + }
2619 + else {
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");
2624 + }
2625 + }
2626 + }
2627 + model = model->next_compr_model;
2628 + }
2629 + }
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");
2635 + }
2636 + else {
2637 + jffs2_bbc_print1("Unknown command.\n");
2638 + }
2639 + return 0;
2640 +}
2641 +
2642 +static void ipack_armlib_destroy_model(void **model)
2643 +{
2644 + ipack_model_type *model_img;
2645 +
2646 + model_img = *model;
2647 + if (model_img == NULL) {
2648 + jffs2_bbc_print1("jffs2.bbc: armlib: NULL model at destoying model!\n");
2649 + return;
2650 + }
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;
2655 + }
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;
2660 + }
2661 +
2662 + jffs2_bbc_free(model_img);
2663 + *model = NULL;
2664 +}
2665 +
2666 +struct jffs2_bbc_compressor_type *jffs2_bbc_armlib_init(int mode)
2667 +{
2668 + if (jffs2_bbc_register_compressor(&jffs2_bbc_armlib) == 0)
2669 + return &jffs2_bbc_armlib;
2670 + else
2671 + return NULL;
2672 +}
2673 +
2674 +void jffs2_bbc_armlib_deinit(void)
2675 +{
2676 + jffs2_bbc_unregister_compressor(&jffs2_bbc_armlib);
2677 +}
2678 +
2679 +/*END OF 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
2683 @@ -0,0 +1,1324 @@
2684 +/*
2685 + * JFFS2-BBC: Compression Framework
2686 + *
2687 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
2688 + *
2689 + * Copyright (C) 2004, Ferenc Havasi
2690 + *
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.
2695 + *
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.
2700 + *
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.
2704 + *
2705 + */
2706 +
2707 +/* USE JFFS2_BBC_STANDALONE define if you don't want to compile without JFFS2 */
2708 +
2709 +//#define DEBUG_COMPRESSORS
2710 +//#define DEBUG_SHOW_BLOCK_SIZES
2711 +
2712 +#define JFFS2_BBC_STAT_BUFF_SIZE 8000
2713 +
2714 +#ifndef __KERNEL__
2715 +
2716 +#include <stdio.h>
2717 +#include <malloc.h>
2718 +typedef unsigned long uint32_t;
2719 +
2720 +#else
2721 +
2722 +#include <linux/kernel.h>
2723 +#include <linux/slab.h>
2724 +#include <linux/vmalloc.h>
2725 +
2726 +#endif
2727 +
2728 +#define JFFS2_BBC_ZLIB_BLOCK_SIGN_0 (120)
2729 +#define JFFS2_BBC_ZLIB_BLOCK_SIGN_1 (94)
2730 +
2731 +#define JFFS2_BBC_DUMMY_BLOCKSIGN_0 (0x54)
2732 +#define JFFS2_BBC_DUMMY_BLOCKSIGN_1 (0x01)
2733 +
2734 +#ifndef NULL
2735 +#define NULL ((void*)(0))
2736 +#endif
2737 +
2738 +#include "jffs2_bbc_framework.h"
2739 +
2740 +/*********************************************************************
2741 + * Global data *
2742 + *********************************************************************/
2743 +
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 */
2749 +
2750 +/*********************************************************************
2751 + * Compressor initialization *
2752 + *********************************************************************/
2753 +
2754 +#ifndef JFFS2_BBC_STANDALONE
2755 +
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);
2759 +#endif
2760 +
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);
2764 +#endif
2765 +
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);
2769 +#endif
2770 +
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);
2774 +#endif
2775 +
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);
2779 +#endif
2780 +
2781 +void jffs2_bbc_compressor_init()
2782 +{
2783 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2784 + jffs2_bbc_armlib_init();
2785 +#endif
2786 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2787 + jffs2_bbc_lzo_init();
2788 +#endif
2789 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2790 + jffs2_bbc_lzss_init();
2791 +#endif
2792 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2793 + jffs2_bbc_lzari_init();
2794 +#endif
2795 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2796 + jffs2_bbc_lzhd_init();
2797 +#endif
2798 +}
2799 +
2800 +void jffs2_bbc_compressor_deinit()
2801 +{
2802 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2803 + jffs2_bbc_lzhd_deinit();
2804 +#endif
2805 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2806 + jffs2_bbc_lzari_deinit();
2807 +#endif
2808 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2809 + jffs2_bbc_lzss_deinit();
2810 +#endif
2811 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2812 + jffs2_bbc_lzo_deinit();
2813 +#endif
2814 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2815 + jffs2_bbc_armlib_deinit();
2816 +#endif
2817 +}
2818 +
2819 +#endif
2820 +
2821 +#ifndef JFFS2_BBC_STANDALONE
2822 +
2823 +/*********************************************************************
2824 + * ZLIB COMPRESSOR *
2825 + *********************************************************************/
2826 +
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);
2829 +
2830 +static int jffs2_bbc_zlib_compress(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen)
2831 +{
2832 + return jffs2_zlib_compress2(input, output, sourcelen, dstlen);
2833 +}
2834 +
2835 +static int jffs2_bbc_zlib_decompress(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen)
2836 +{
2837 + jffs2_zlib_decompress2(input, output, sourcelen, dstlen);
2838 + return 0;
2839 +}
2840 +
2841 +static int jffs2_bbc_zlib_estimate(void *model, unsigned char *input, unsigned long sourcelen, unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime)
2842 +{
2843 + *dstlen = sourcelen * 65 / 100;
2844 + *readtime = JFFS2_BBC_ZLIB_READ_TIME;
2845 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME;
2846 + return 0;
2847 +}
2848 +
2849 +static struct jffs2_bbc_compressor_type jffs2_bbc_zlib = {
2850 + "zlib",
2851 + 0,
2852 + {JFFS2_BBC_ZLIB_BLOCK_SIGN_0, JFFS2_BBC_ZLIB_BLOCK_SIGN_1, 0, 0},
2853 + NULL,
2854 + NULL,
2855 + NULL,
2856 + NULL,
2857 + jffs2_bbc_zlib_compress,
2858 + jffs2_bbc_zlib_estimate,
2859 + jffs2_bbc_zlib_decompress,
2860 + NULL,
2861 + NULL,
2862 + 1
2863 +};
2864 +
2865 +static struct jffs2_bbc_compressor_type *jffs2_bbc_original_compressor = &jffs2_bbc_zlib;
2866 +
2867 +#endif
2868 +
2869 +/*********************************************************************
2870 + * Compression mode handling *
2871 + *********************************************************************/
2872 +
2873 +int jffs2_bbc_get_compression_mode(void)
2874 +{
2875 + return jffs2_bbc_compression_mode;
2876 +}
2877 +
2878 +void jffs2_bbc_set_compression_mode(int mode)
2879 +{
2880 + jffs2_bbc_compression_mode = mode;
2881 +}
2882 +
2883 +void jffs2_bbc_set_manual_compressor(struct jffs2_bbc_compressor_type *c)
2884 +{
2885 + jffs2_bbc_manual_compressor = c;
2886 + jffs2_bbc_set_compression_mode(JFFS2_BBC_MANUAL_MODE);
2887 +}
2888 +
2889 +int jffs2_bbc_set_manual_compressor_by_name(char *name)
2890 +{
2891 + struct jffs2_bbc_compressor_type *l;
2892 + int i;
2893 +
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);
2899 + return 0;
2900 + }
2901 + else if (name[i] == 0)
2902 + i = 1000;
2903 + else if (name[i] != l->name[i])
2904 + i = 1000;
2905 + }
2906 + l = l->next;
2907 + }
2908 + jffs2_bbc_set_manual_compressor(NULL);
2909 + return 1;
2910 +}
2911 +
2912 +static struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_by_name(char *name)
2913 +{
2914 + struct jffs2_bbc_compressor_type *l;
2915 + int i;
2916 +
2917 +#ifndef JFFS2_BBC_STANDALONE
2918 + l = jffs2_bbc_original_compressor;
2919 + for (i = 0; i < 1000; i++) {
2920 + if (l->name[i] == 0) {
2921 + return l;
2922 + }
2923 + else if (name[i] == 0)
2924 + i = 1000;
2925 + else if (name[i] != l->name[i])
2926 + i = 1000;
2927 + }
2928 +#endif
2929 +
2930 + l = jffs2_bbc_compressors;
2931 + while (l != NULL) {
2932 + for (i = 0; i < 1000; i++) {
2933 + if (l->name[i] == 0) {
2934 + return l;
2935 + }
2936 + else if (name[i] == 0)
2937 + i = 1000;
2938 + else if (name[i] != l->name[i])
2939 + i = 1000;
2940 + }
2941 + l = l->next;
2942 + }
2943 +
2944 + return NULL;
2945 +}
2946 +
2947 +int jffs2_bbc_disable_compressor_by_name(char *name)
2948 +{
2949 + struct jffs2_bbc_compressor_type *l;
2950 +
2951 + l = jffs2_bbc_get_compressor_by_name(name);
2952 + if (l == NULL) return 1;
2953 + l->enabled = 0;
2954 + return 0;
2955 +}
2956 +
2957 +int jffs2_bbc_enable_compressor_by_name(char *name)
2958 +{
2959 + struct jffs2_bbc_compressor_type *l;
2960 +
2961 + l = jffs2_bbc_get_compressor_by_name(name);
2962 + if (l == NULL) return 1;
2963 + l->enabled = 1;
2964 + return 0;
2965 +}
2966 +
2967 +void jffs2_bbc_compressor_command_by_name(char *name_and_command)
2968 +{
2969 + struct jffs2_bbc_compressor_type *l;
2970 + int i;
2971 +
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");
2978 + }
2979 + else {
2980 + if (l->proc_command != NULL)
2981 + l->proc_command(name_and_command + i + 1);
2982 + }
2983 + i = 1000;
2984 + return;
2985 + }
2986 + else if (name_and_command[i] == 0) {
2987 + i = 1000;
2988 + }
2989 + else if (name_and_command[i] != l->name[i]) {
2990 + i = 1000;
2991 + }
2992 + }
2993 + l = l->next;
2994 + }
2995 +}
2996 +
2997 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_manual_compressor(void)
2998 +{
2999 + if (jffs2_bbc_get_compression_mode() != JFFS2_BBC_MANUAL_MODE) {
3000 + jffs2_bbc_manual_compressor = NULL;
3001 + }
3002 + return jffs2_bbc_manual_compressor;
3003 +}
3004 +
3005 +/*********************************************************************
3006 + * Compressor handling *
3007 + *********************************************************************/
3008 +
3009 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_list(void)
3010 +{
3011 + return jffs2_bbc_compressors;
3012 +}
3013 +
3014 +struct jffs2_bbc_model_list_node *jffs2_bbc_get_model_list(void)
3015 +{
3016 + return jffs2_bbc_model_list;
3017 +}
3018 +
3019 +int jffs2_bbc_register_compressor(struct jffs2_bbc_compressor_type *c)
3020 +{
3021 + struct jffs2_bbc_compressor_type *l;
3022 + struct jffs2_bbc_model_list_node *l2;
3023 + int model_found = 0;
3024 +
3025 + l = jffs2_bbc_compressors;
3026 + /* Check for confilcts */
3027 + while (l != NULL) {
3028 + c->name[15] = 0;
3029 + /*if (strcmp(c->name,l->name)==0) {
3030 + jffs2_bbc_print1("jffs2.bbc: compressor is already loaded.");
3031 + return -1;
3032 + } */
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.");
3035 + return -1;
3036 + }
3037 + l = l->next;
3038 + }
3039 + /* Search and initialize model */
3040 + c->models = NULL;
3041 + c->mounted = 0;
3042 + if (c->init != NULL) {
3043 + if (c->init() != 0) {
3044 + jffs2_bbc_print2("jffs2.bbc: cannot initialize compressor %s.\n", c->name);
3045 + return -1;
3046 + }
3047 + }
3048 + if (c->model_file_sign != 0) {
3049 + l2 = jffs2_bbc_model_list;
3050 + while (1) {
3051 + if (l2 == NULL)
3052 + break;
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);
3056 + }
3057 + else {
3058 + if (c->init_model(&(l2->model)) != 0) {
3059 + jffs2_bbc_print2("jffs2.bbc: cannot initialize compressor %s for a model", c->name);
3060 + }
3061 + else {
3062 + l2->compressor = c;
3063 + l2->next_compr_model = c->models;
3064 + c->models = l2;
3065 + c->mounted++;
3066 + model_found++;
3067 + }
3068 + }
3069 + }
3070 + l2 = l2->next_model;
3071 + }
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);
3074 + } */
3075 + }
3076 + /* Insert to the end of the compressor list */
3077 + c->enabled = 1;
3078 + c->buffer = NULL;
3079 + c->buffer_size = 0;
3080 + c->stat_compr_orig = c->stat_compr_new = c->stat_decompr = 0;
3081 + c->next = NULL;
3082 + if (jffs2_bbc_compressors == NULL) {
3083 + jffs2_bbc_compressors = c;
3084 + }
3085 + else {
3086 + l = jffs2_bbc_compressors;
3087 + while (l->next != NULL)
3088 + l = l->next;
3089 + l->next = c;
3090 + }
3091 + return 0;
3092 +}
3093 +
3094 +int jffs2_bbc_unregister_compressor(struct jffs2_bbc_compressor_type *c)
3095 +{
3096 + struct jffs2_bbc_compressor_type *l;
3097 + struct jffs2_bbc_model_list_node *l2;
3098 +
3099 + if (c->mounted != 0) {
3100 + jffs2_bbc_print1("jffs2.bbc: Compressor is in use. Sorry.");
3101 + return -1;
3102 + }
3103 + if (jffs2_bbc_compressors == NULL) {
3104 + jffs2_bbc_print1("jffs2.bbc: unregister: empty list.");
3105 + return -1;
3106 + }
3107 + else if (jffs2_bbc_compressors == c) {
3108 + if (c->deinit != NULL)
3109 + c->deinit();
3110 + jffs2_bbc_compressors = c->next;
3111 + }
3112 + else {
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);
3117 + return -1;
3118 + }
3119 + l = l->next;
3120 + }
3121 + if (c->deinit != NULL)
3122 + c->deinit();
3123 + l->next = c->next;
3124 + }
3125 + if (c->buffer != NULL) {
3126 + jffs2_bbc_free(c->buffer);
3127 + c->buffer = NULL;
3128 + c->buffer_size = 0;
3129 + }
3130 +
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;
3137 + }
3138 + l2 = l2->next_model;
3139 + }
3140 +
3141 + return 0;
3142 +}
3143 +
3144 +int jffs2_bbc_model_new(void *sb, int i_num, void *model)
3145 +{
3146 + struct jffs2_bbc_model_list_node *node;
3147 + struct jffs2_bbc_compressor_type *l;
3148 + char block_sign[2];
3149 +
3150 + int sign;
3151 +
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);
3160 + return -1;
3161 + }
3162 + node = node->next_model;
3163 + }
3164 +
3165 + /* insertion */
3166 + node = jffs2_bbc_malloc_small((long)sizeof(struct jffs2_bbc_model_list_node));
3167 + node->sb = sb;
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;
3178 +
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");
3186 + }
3187 + else {
3188 + l->mounted++;
3189 + node->compressor = l;
3190 + node->next_compr_model = l->models;
3191 + l->models = node;
3192 + }
3193 + break;
3194 + }
3195 + l = l->next;
3196 + }
3197 + return 0;
3198 +}
3199 +
3200 +static void jffs2_bbc_model_del_from_compressor(struct jffs2_bbc_model_list_node *node)
3201 +{
3202 + struct jffs2_bbc_model_list_node *l;
3203 +
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;
3209 + }
3210 + else {
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");
3214 + }
3215 + }
3216 + }
3217 +
3218 + if (node->compressor == NULL) {
3219 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor: no compressor!\n");
3220 + return;
3221 + }
3222 + l = node->compressor->models;
3223 + if (l == NULL) {
3224 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor error, models=NULL!\n");
3225 + return;
3226 + }
3227 + if (l == node) {
3228 + node->compressor->models = node->next_compr_model;
3229 + node->compressor->mounted--;
3230 + return;
3231 + }
3232 + while (1) {
3233 + if (l->next_compr_model == node) {
3234 + l->next_compr_model = node->next_compr_model;
3235 + node->compressor->mounted--;
3236 + return;
3237 + }
3238 + l = l->next_compr_model;
3239 + if (l == NULL) {
3240 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor: not found\n");
3241 + return;
3242 + }
3243 + }
3244 +}
3245 +
3246 +void jffs2_bbc_model_del(void *sb)
3247 +{
3248 + struct jffs2_bbc_model_list_node *l, *l2;
3249 +
3250 + l = jffs2_bbc_model_list;
3251 + if (l == NULL)
3252 + return;
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);
3258 + return;
3259 + }
3260 + while (1) {
3261 + if (l->next_model == NULL) {
3262 + break;
3263 + }
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);
3270 + return;
3271 + }
3272 + l = l->next_model;
3273 + }
3274 + last_sb = NULL;
3275 +}
3276 +
3277 +void jffs2_bbc_model_set_act_sb(void *sb)
3278 +{
3279 + last_sb = sb;
3280 +}
3281 +
3282 +void *jffs2_bbc_model_get_act_sb(void)
3283 +{
3284 + return last_sb;
3285 +}
3286 +
3287 +void *jffs2_bbc_model_get_newest(struct jffs2_bbc_compressor_type *compressor)
3288 +{
3289 + struct jffs2_bbc_model_list_node *m, *best_m;
3290 + int max_sign, sign;
3291 +
3292 + if (compressor == NULL) {
3293 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_get: NULL!!\n");
3294 + return NULL;
3295 + }
3296 +
3297 + best_m = NULL;
3298 + max_sign = -1;
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) {
3304 + max_sign = sign;
3305 + best_m = m;
3306 + }
3307 + }
3308 + m = m->next_compr_model;
3309 + }
3310 + if (best_m != NULL)
3311 + return best_m->model;
3312 + else
3313 + return NULL;
3314 +}
3315 +
3316 +/*********************************************************************
3317 + * Statistics *
3318 + *********************************************************************/
3319 +
3320 +static char *jffs2_bbc_stat_buff = NULL;
3321 +
3322 +char *jffs2_bbc_get_model_stats(void)
3323 +{
3324 + char *b;
3325 + struct jffs2_bbc_model_list_node *m;
3326 + struct jffs2_bbc_compressor_type *c;
3327 +
3328 + if (jffs2_bbc_stat_buff == NULL)
3329 + jffs2_bbc_stat_buff = jffs2_bbc_malloc(8000);
3330 +
3331 + b = jffs2_bbc_stat_buff;
3332 +
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=");
3340 + m = c->models;
3341 + while (m != NULL) {
3342 + b += sprintf(b, "(inode=%d)", m->inode);
3343 + m = m->next_compr_model;
3344 + }
3345 + }
3346 + else {
3347 + b += sprintf(b, "b_sign=(%d,%d) nomodel", (int) (c->block_sign[0]), (int) (c->block_sign[1]));
3348 + }
3349 + if (c->proc_info != NULL) {
3350 + b += sprintf(b, "\n %s", c->proc_info());
3351 + }
3352 + c = c->next;
3353 + }
3354 +
3355 + m = jffs2_bbc_model_list;
3356 +
3357 + if (m == NULL) {
3358 + b += sprintf(b, "\nPresent models: NONE\n");
3359 + }
3360 + else {
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");
3366 + else
3367 + b += sprintf(b, ",compressor=%s\n", m->compressor->name);
3368 + m = m->next_model;
3369 + }
3370 + }
3371 +
3372 + return jffs2_bbc_stat_buff;
3373 +}
3374 +
3375 +/*********************************************************************
3376 + * Memory handling, debug *
3377 + *********************************************************************/
3378 +
3379 +static int jffs2_bbc_mem_counter = 0;
3380 +
3381 +#ifdef __KERNEL__
3382 +
3383 +void *jffs2_bbc_malloc(long size)
3384 +{
3385 + void *addr = vmalloc(size);
3386 + if (addr != NULL)
3387 + jffs2_bbc_mem_counter++;
3388 + else {
3389 + jffs2_bbc_print2("DEBUG: not enough memory (%ld)\n", size);
3390 + }
3391 + return addr;
3392 +}
3393 +
3394 +void jffs2_bbc_free(void *addr)
3395 +{
3396 + jffs2_bbc_mem_counter--;
3397 + vfree(addr);
3398 +}
3399 +
3400 +void *jffs2_bbc_malloc_small(long size)
3401 +{
3402 + void *addr;
3403 + addr = kmalloc(size, 0);
3404 + if (addr != NULL)
3405 + jffs2_bbc_mem_counter++;
3406 + return addr;
3407 +}
3408 +
3409 +void jffs2_bbc_free_small(void *addr)
3410 +{
3411 + jffs2_bbc_mem_counter--;
3412 + kfree(addr);
3413 +}
3414 +
3415 +#else
3416 +
3417 +void *jffs2_bbc_malloc(long size)
3418 +{
3419 + void *addr = malloc(size);
3420 + if (addr != NULL)
3421 + jffs2_bbc_mem_counter++;
3422 + return addr;
3423 +}
3424 +
3425 +void jffs2_bbc_free(void *addr)
3426 +{
3427 + jffs2_bbc_mem_counter--;
3428 + free(addr);
3429 +}
3430 +
3431 +void *jffs2_bbc_malloc_small(long size)
3432 +{
3433 + return jffs2_bbc_malloc(size);
3434 +}
3435 +
3436 +void jffs2_bbc_free_small(void *addr)
3437 +{
3438 + jffs2_bbc_free(addr);
3439 +}
3440 +
3441 +#endif
3442 +
3443 +int jffs2_bbc_test_memory_counter(int verbose)
3444 +{
3445 + if (verbose > 0) {
3446 + jffs2_bbc_print2("jffs2.bbc: mem_counter=%d!\n", jffs2_bbc_mem_counter);
3447 + }
3448 + return jffs2_bbc_mem_counter;
3449 +}
3450 +
3451 +int jffs2_bbc_get_memory_counter(void)
3452 +{
3453 + return jffs2_bbc_mem_counter;
3454 +}
3455 +
3456 +static char mem_stat[200];
3457 +
3458 +char *jffs2_bbc_get_mem_stats(void)
3459 +{
3460 + sprintf(mem_stat, "Memcounter=%d\n", jffs2_bbc_mem_counter);
3461 + return mem_stat;
3462 +}
3463 +
3464 +void jffs2_bbc_print_flush(void)
3465 +{
3466 +#ifdef __KERNEL__
3467 + return;
3468 +#else
3469 + fflush(stdout);
3470 + fflush(stderr);
3471 +#endif
3472 +}
3473 +
3474 +/*********************************************************************
3475 + * FRAMEWORK - ZLIB REPLACEMENT *
3476 + *********************************************************************/
3477 +
3478 +#ifndef JFFS2_BBC_STANDALONE
3479 +
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;
3484 +
3485 +/* Statistic - used by /proc/jffs2_bbc and mkfs.jffs2 */
3486 +char *jffs2_bbc_get_compr_stats(void)
3487 +{
3488 + struct jffs2_bbc_compressor_type *l;
3489 + char *s = stat_str;
3490 +
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);
3498 + l = l->next;
3499 + }
3500 + return stat_str;
3501 +}
3502 +
3503 +static void jffs2_bbc_buffer_fill(unsigned char *buff, int size)
3504 +{
3505 + for (; size > 0; size--, buff++)
3506 + *buff = 255;
3507 +}
3508 +
3509 +
3510 +static int jffs2_bbc_update_compr_buf(unsigned long size)
3511 +{
3512 + struct jffs2_bbc_compressor_type *l;
3513 +
3514 + if (size < 5000)
3515 + size = 5000;
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;
3520 + }
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;
3526 + }
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);
3533 + }
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);
3539 + }
3540 + l = l->next;
3541 + }
3542 + return 0;
3543 +}
3544 +
3545 +#ifdef DEBUG_COMPRESSORS
3546 +
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;
3551 +
3552 +
3553 +void debug_before_compress(struct jffs2_bbc_compressor_type *c, void *model, unsigned char *input, unsigned char *output, long *sourcelen, long *dstlen)
3554 +{
3555 +
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;
3559 +
3560 + debug_mem_counter = jffs2_bbc_get_memory_counter(); // for memory guard
3561 +}
3562 +
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)
3564 +{
3565 + long dst_len = *dstlen;
3566 + long src_len = *sourcelen;
3567 + int i;
3568 +
3569 + // Memory guard
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();
3573 + }
3574 +
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);
3578 + }
3579 +
3580 + // Decompression check
3581 + if (back == 0) {
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();
3590 + }
3591 +
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);
3595 + break;
3596 + }
3597 + return;
3598 + }
3599 +
3600 + // Return value test
3601 + //jffs2_bbc_print3("!!!!!!!! %s error: %d !!!!!!!!!!!!\n", c->name, back);
3602 +}
3603 +
3604 +#endif
3605 +
3606 +int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, uint32_t * sourcelen, uint32_t * dstlen)
3607 +{
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;
3612 + void *sb;
3613 + unsigned char *tmp_p = NULL;
3614 +
3615 + sb = jffs2_bbc_model_get_act_sb();
3616 +
3617 + orig_src = *sourcelen;
3618 + orig_dest = *dstlen;
3619 +
3620 + mode = jffs2_bbc_get_compression_mode();
3621 +
3622 + if (mode == JFFS2_BBC_DUMMY_MODE) {
3623 + i=0; i2=0;
3624 + if (*dstlen>2) {
3625 + cpage_out[i++]=JFFS2_BBC_DUMMY_BLOCKSIGN_0;
3626 + cpage_out[i++]=JFFS2_BBC_DUMMY_BLOCKSIGN_1;
3627 + i2=i;
3628 + }
3629 + for (;((i < *dstlen) && (i < (*sourcelen)+i2));i++) {
3630 + cpage_out[i] = data_in[i-i2];
3631 + }
3632 + *sourcelen=i-i2;
3633 + *dstlen=i;
3634 + return 0;
3635 + }
3636 +
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);
3640 + }*/
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;
3644 + return back;
3645 + }
3646 +
3647 + jffs2_bbc_update_compr_buf(orig_dest);
3648 +
3649 + if (mode == JFFS2_BBC_SIZE_MODE) {
3650 + // Testing all compressors
3651 + if (!jffs2_bbc_original_compressor->enabled) {
3652 + min = -1;
3653 + }
3654 + else {
3655 + back_zlib = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3656 + min = *dstlen;
3657 + }
3658 + c = jffs2_bbc_get_compressor_list();
3659 + while (c != NULL) {
3660 + c->buffer_cnt = -1;
3661 + if (c->enabled == 0) {
3662 + c = c->next;
3663 + continue;
3664 + }
3665 + if (c->model_file_sign == 0) {
3666 + src = orig_src;
3667 + dest = orig_dest;
3668 +#ifdef DEBUG_COMPRESSORS
3669 + debug_before_compress(c, NULL, data_in, c->buffer, &src, &dest);
3670 +#endif
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);
3674 +#endif
3675 + if (back == 0) {
3676 + c->buffer_cnt = dest;
3677 + if ((min < 0) || (min > dest))
3678 + min = dest;
3679 + }
3680 + }
3681 + else {
3682 + m = c->models;
3683 + while (m != NULL) {
3684 + src = orig_src;
3685 + dest = orig_dest;
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));
3690 +#endif
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));
3694 +#endif
3695 + if (back == 0) {
3696 + c->buffer_cnt = dest;
3697 + if ((min < 0) || (min > dest))
3698 + min = dest;
3699 + }
3700 + }
3701 + else {
3702 +#ifdef DEBUG_COMPRESSORS
3703 + debug_before_compress(c, m->model, data_in, tmp_buffer, &src, &dest);
3704 +#endif
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);
3708 +#endif
3709 + if (back == 0) {
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))
3716 + min = dest;
3717 + }
3718 + }
3719 + }
3720 + }
3721 + m = m->next_compr_model;
3722 + }
3723 + }
3724 + c = c->next;
3725 + }
3726 + //Finding the best and copy its result
3727 +
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));
3733 + }
3734 + else {
3735 + jffs2_bbc_print3("%s:%d ", jffs2_bbc_original_compressor->name, (int) (*dstlen));
3736 + }
3737 + }
3738 + c = jffs2_bbc_get_compressor_list();
3739 + while (c != NULL) {
3740 + if (c->enabled == 0) {
3741 + c = c->next;
3742 + continue;
3743 + }
3744 + if (c->buffer_cnt == min)
3745 + jffs2_bbc_print3("%s:%d* ", c->name, c->buffer_cnt);
3746 + else
3747 + jffs2_bbc_print3("%s:%d ", c->name, c->buffer_cnt);
3748 + c = c->next;
3749 + }
3750 +#endif
3751 +
3752 + if (min == -1) {
3753 + return -1; // none of compressors work (maybe too short output buffer)
3754 + }
3755 +
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;
3760 + return back_zlib;
3761 + }
3762 + }
3763 +
3764 + c = jffs2_bbc_get_compressor_list();
3765 + while (c != NULL) {
3766 + if (c->enabled == 0) {
3767 + c = c->next;
3768 + continue;
3769 + }
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];
3775 + }
3776 + c->stat_compr_orig += *sourcelen;
3777 + c->stat_compr_new += *dstlen;
3778 + return 0;
3779 + }
3780 + c = c->next;
3781 + }
3782 + jffs2_bbc_print1("jffs2.bbc: compr (full): BUG!!!\n");
3783 + return 0;
3784 + }
3785 +
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);
3790 + }
3791 + else {
3792 + tmp = -1;
3793 + tmp_read_time = -1;
3794 + tmp_write_time = -1;
3795 + }
3796 + if (mode == JFFS2_BBC_FASTR_MODE) tmp = tmp_read_time;
3797 + if (mode == JFFS2_BBC_FASTW_MODE) tmp = tmp_write_time;
3798 + min = tmp;
3799 + c = jffs2_bbc_get_compressor_list();
3800 + while (c != NULL) {
3801 + src = orig_src;
3802 + dest = orig_dest;
3803 + c->buffer_cnt = -1;
3804 + if (c->enabled == 0) {
3805 + c = c->next;
3806 + continue;
3807 + }
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;
3812 + if (back == 0) {
3813 + c->buffer_cnt = dest;
3814 + if ((min < 0) || (min > dest))
3815 + min = dest;
3816 + }
3817 + else {
3818 + c->buffer_cnt = -1;
3819 + }
3820 + }
3821 + c = c->next;
3822 + }
3823 + // Finding the best and compress with it
3824 + if (min == -1) {
3825 + return -1;
3826 + }
3827 + if (jffs2_bbc_original_compressor->enabled) {
3828 + if (min == tmp) {
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;
3832 + return back;
3833 + }
3834 + }
3835 + c = jffs2_bbc_get_compressor_list();
3836 + while (c != NULL) {
3837 + if (c->enabled == 0) {
3838 + c = c->next;
3839 + continue;
3840 + }
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;
3846 + }
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;
3853 + return back;
3854 + }
3855 + return 0;
3856 + }
3857 + c = c->next;
3858 + }
3859 + jffs2_bbc_print1("jffs2.bbc: compress (fastX mode): BUG!!!\n");
3860 + return 0;
3861 + }
3862 +
3863 + if (mode == JFFS2_BBC_MANUAL_MODE) {
3864 + c = jffs2_bbc_get_manual_compressor();
3865 + if (c != NULL) {
3866 + if (c->model_file_sign == 0) {
3867 + src = orig_src;
3868 + dest = orig_dest;
3869 + back = c->compress(NULL, data_in, cpage_out, &src, &dest);
3870 + if (back == 0) {
3871 + *dstlen = dest;
3872 + *sourcelen = src;
3873 + c->stat_compr_orig += *sourcelen;
3874 + c->stat_compr_new += *dstlen;
3875 + return 0;
3876 + }
3877 + }
3878 + else {
3879 + c->buffer_cnt = -1;
3880 + m = c->models;
3881 + min = -1;
3882 + while (m != NULL) {
3883 + src = orig_src;
3884 + dest = orig_dest;
3885 + if (m->sb == sb) {
3886 + if (min == -1) {
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)) {
3889 + min = dest;
3890 + tmp_p = cpage_out;
3891 + }
3892 + }
3893 + else {
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) {
3897 + if (min > dest) {
3898 + min = dest;
3899 + tmp_p = tmp_buffer;
3900 + }
3901 + }
3902 + }
3903 + }
3904 + }
3905 + m = m->next_compr_model;
3906 + }
3907 + if (min != -1) {
3908 + if (tmp_p != cpage_out) {
3909 + for (i = 0; i < min; i++)
3910 + cpage_out[i] = tmp_p[i];
3911 + *sourcelen = orig_src;
3912 + *dstlen = min;
3913 + }
3914 + c->stat_compr_orig += *sourcelen;
3915 + c->stat_compr_new += *dstlen;
3916 + return 0;
3917 + }
3918 + }
3919 + }
3920 + /*else {
3921 + jffs2_bbc_print1("iPack: manual mode without selected compressor!\n");
3922 + } */
3923 +
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);
3926 + }*/
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;
3930 + return back;
3931 +
3932 +
3933 + }
3934 +
3935 + jffs2_bbc_print1("jffs2.bbc: compress: unimlemented compress mode!!!\n");
3936 + return 0;
3937 +}
3938 +
3939 +void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen)
3940 +{
3941 + struct jffs2_bbc_model_list_node *m;
3942 + struct jffs2_bbc_compressor_type *c;
3943 + char d[2];
3944 + void *sb;
3945 + int i;
3946 +
3947 + /* If the input too small... */
3948 + if (destlen<=2) {
3949 + cpage_out[0]=data_in[0];
3950 + if (destlen==2) cpage_out[1]=data_in[1];
3951 + return;
3952 + }
3953 +
3954 + sb = jffs2_bbc_model_get_act_sb();
3955 + d[0] = *(data_in);
3956 + d[1] = *(data_in + 1);
3957 +
3958 + d[0] &= 0x7f; // Variants support...
3959 +
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]));
3966 + }
3967 + else {
3968 + m->compressor->decompress(m->model, data_in, cpage_out, srclen, destlen);
3969 + m->compressor->stat_decompr++;
3970 + m->stat_decompr++;
3971 + }
3972 + return;
3973 + }
3974 + m = m->next_model;
3975 + }
3976 + /* Is it ZLIB? */
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++;
3980 + return;
3981 + }
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++;
3989 + return;
3990 + }
3991 + }
3992 + c = c->next;
3993 + }
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];
3998 + }
3999 + return;
4000 + }
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++;
4005 +}
4006 +
4007 +#endif
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
4011 @@ -0,0 +1,202 @@
4012 +/*
4013 + * JFFS2-BBC: Compression Framework - headers
4014 + *
4015 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4016 + *
4017 + * Copyright (C) 2004, Ferenc Havasi
4018 + *
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.
4023 + *
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.
4028 + *
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.
4032 + *
4033 + */
4034 +
4035 +#ifndef __JFFS2_BBC_FRAMEWORK_H__
4036 +
4037 +#define __JFFS2_BBC_FRAMEWORK_H__
4038 +
4039 +#define JFFS2_BBC_VERSION "0.54.3"
4040 +
4041 +#define JFFS2_BBC_CONFIG_FILE "bbc.conf"
4042 +
4043 +/*********************************************************************
4044 + * Compression mode handling *
4045 + *********************************************************************/
4046 +
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
4054 +
4055 +int jffs2_bbc_get_compression_mode(void);
4056 +void jffs2_bbc_set_compression_mode(int mode);
4057 +
4058 +/*********************************************************************
4059 + * Read/write speed unit *
4060 + * everything is relative to the speed of zlib *
4061 + * bigger number means slower speed! *
4062 + *********************************************************************/
4063 +
4064 +#define JFFS2_BBC_ZLIB_READ_TIME 10000
4065 +#define JFFS2_BBC_ZLIB_WRITE_TIME 10000
4066 +
4067 +/*********************************************************************
4068 + * Compressor handling *
4069 + *********************************************************************/
4070 +
4071 +struct jffs2_bbc_compressor_type
4072 +{
4073 + char name[16];
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);
4080 + /* Compress block
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
4084 + */
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
4090 + */
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 */
4105 +};
4106 +
4107 +/* It sets the compression mode to JFFS2_BBC_MANUAL_MODE */
4108 +
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);
4114 +
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. */
4117 +
4118 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_manual_compressor(void);
4119 +
4120 +struct jffs2_bbc_model_list_node
4121 +{
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 */
4127 + int stat_decompr;
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;
4131 +};
4132 +
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);
4135 +
4136 +int jffs2_bbc_register_compressor(struct jffs2_bbc_compressor_type *c);
4137 +int jffs2_bbc_unregister_compressor(struct jffs2_bbc_compressor_type *c);
4138 +
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);
4144 +
4145 +/*********************************************************************
4146 + * Compressor init function *
4147 + *********************************************************************/
4148 +
4149 +void jffs2_bbc_compressor_init(void);
4150 +void jffs2_bbc_compressor_deinit(void);
4151 +
4152 +/*********************************************************************
4153 + * Statistics *
4154 + *********************************************************************/
4155 +
4156 +char *jffs2_bbc_get_compr_stats(void);
4157 +char *jffs2_bbc_get_model_stats(void);
4158 +
4159 +/*********************************************************************
4160 + * Other *
4161 + *********************************************************************/
4162 +
4163 +
4164 +void jffs2_bbc_print_flush(void);
4165 +
4166 +#ifdef __KERNEL__
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)
4177 +#else
4178 +#include <stdio.h>
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)
4188 +#endif
4189 +
4190 +/* Handle endianness */
4191 +#ifndef __KERNEL__
4192 +
4193 +#define ENDIAN_HOST_AND_TARGET_SAME 0
4194 +#define ENDIAN_HOST_AND_TARGET_DIFFERENT 1
4195 +
4196 +extern int jffs2_bbc_glb_endian_X;
4197 +
4198 +#endif
4199 +
4200 +/* Allocating more than one page (tip. 4096 byte) */
4201 +void *jffs2_bbc_malloc(long size);
4202 +void jffs2_bbc_free(void *addr);
4203 +
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);
4207 +
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);
4212 +
4213 +#endif
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
4217 @@ -0,0 +1,331 @@
4218 +/*
4219 + * JFFS2-BBC: File System Extension for Linux Kernel
4220 + *
4221 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4222 + *
4223 + * Copyright (C) 2004, Ferenc Havasi
4224 + *
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.
4229 + *
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.
4234 + *
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.
4238 + *
4239 + */
4240 +
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>
4246 +
4247 +#include "nodelist.h"
4248 +
4249 +#include "jffs2_bbc_framework.h"
4250 +
4251 +struct jffs2_bbc_fs_sb_list {
4252 + struct super_block *sb;
4253 + struct jffs2_bbc_fs_sb_list *next;
4254 +};
4255 +
4256 +static struct jffs2_bbc_fs_sb_list *sb_list = NULL;
4257 +
4258 +void jffs2_bbc_proc_init(void);
4259 +void jffs2_bbc_proc_deinit(void);
4260 +
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;
4269 + int i,prev_i;
4270 + struct super_block *sb;
4271 + struct jffs2_bbc_fs_sb_list *sb_l;
4272 +
4273 + sb = sb_par;
4274 + sb_l = jffs2_bbc_malloc_small(sizeof(struct jffs2_bbc_fs_sb_list));
4275 + sb_l->sb = sb;
4276 + sb_l->next = sb_list;
4277 + sb_list = sb_l;
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);
4284 +#else
4285 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,config_dentry,NULL);
4286 +#endif
4287 +
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);
4308 +#else
4309 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,model_dentry,NULL);
4310 +#endif
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);
4321 + }
4322 + else {
4323 + printk("done (%d bytes readed from inode %d).\n",model_size,(int)(model_dentry->d_inode->i_ino));
4324 + }
4325 + }
4326 + else {
4327 + printk("not found.\n");
4328 + }
4329 + dput(model_dentry);
4330 + }
4331 + prev_i = i+1;
4332 + }
4333 + }
4334 + }
4335 + }
4336 + dput(config_dentry);
4337 + if (buff != NULL) jffs2_bbc_free(buff);
4338 +}
4339 +
4340 +void jffs2_bbc_unload_model(void *sb_par)
4341 +{
4342 + struct jffs2_sb_info *c;
4343 + struct super_block *sb = sb_par;
4344 + struct jffs2_bbc_fs_sb_list *sb_l,*sb_l2;
4345 + int done = 0;
4346 +
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");
4350 + else {
4351 + if (sb_list->sb == sb) {
4352 + jffs2_bbc_free_small(sb_list);
4353 + sb_list = NULL;
4354 + done = 1;
4355 + }
4356 + else {
4357 + sb_l = 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;
4363 + done = 1;
4364 + }
4365 + sb_l = sb_l->next;
4366 + }
4367 +
4368 + }
4369 + if (done == 0) {
4370 + printk("jffs2.bbc: cannot delete sb from sblist!\n");
4371 + }
4372 + }
4373 +}
4374 +
4375 +static int jffs2_bbc_get_mounted(void) {
4376 + struct jffs2_bbc_fs_sb_list *sb_l;
4377 + int num = 0;
4378 +
4379 + sb_l = sb_list;
4380 + while (sb_l != NULL) {
4381 + num++;
4382 + sb_l = sb_l->next;
4383 + }
4384 + return num;
4385 +
4386 +}
4387 +
4388 +int jffs2_bbc_proc_read(char *buf, char **start, off_t offset, int count, int *eof, void *data)
4389 +{
4390 + int len = 0, mode;
4391 +
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);
4413 + else
4414 + len += sprintf(buf + len, "ZLIB)");
4415 + }
4416 + else
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());
4422 + *eof = 1;
4423 + return len;
4424 +}
4425 +
4426 +int jffs2_bbc_proc_write(struct file *file, const char *buffer_orig, unsigned long count, void *data)
4427 +{
4428 + char *buffer;
4429 + int i;
4430 + struct jffs2_bbc_fs_sb_list *sb_l;
4431 +
4432 + if (buffer_orig == NULL) return 0;
4433 +
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);
4441 + return count;
4442 + }
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);
4447 + return count;
4448 + }
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);
4453 + return count;
4454 + }
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);
4459 + return count;
4460 + }
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");
4465 + }
4466 + else {
4467 + jffs2_bbc_print1("not found.\n");
4468 + }
4469 + jffs2_bbc_free(buffer);
4470 + return count;
4471 + }
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");
4476 + }
4477 + else {
4478 + jffs2_bbc_print1("not found.\n");
4479 + }
4480 + jffs2_bbc_free(buffer);
4481 + return count;
4482 + }
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);
4486 + return count;
4487 + }
4488 + else if ((*buffer == 'r') || (*buffer == 'R')) {
4489 + jffs2_bbc_print1("jffs2.bbc: reloading model files:\n");
4490 + sb_l = sb_list;
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;
4495 + }
4496 + jffs2_bbc_free(buffer);
4497 + return count;
4498 + }
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);
4503 + return count;
4504 + }
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);
4509 + return count;
4510 + }
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);
4515 + return count;
4516 + }
4517 +
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);
4531 + return count;
4532 +}
4533 +
4534 +void jffs2_bbc_proc_init()
4535 +{
4536 + struct proc_dir_entry *res = create_proc_entry("jffs2_bbc", 0, NULL);
4537 + jffs2_bbc_compressor_init();
4538 + if (res) {
4539 + res->read_proc = jffs2_bbc_proc_read;
4540 + res->write_proc = jffs2_bbc_proc_write;
4541 + }
4542 +}
4543 +
4544 +void jffs2_bbc_proc_deinit()
4545 +{
4546 + jffs2_bbc_compressor_deinit();
4547 + remove_proc_entry("jffs2_bbc", NULL);
4548 +}
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
4552 @@ -0,0 +1,30 @@
4553 +/*
4554 + * JFFS2 BBC: File System Extension for Linux Kernel - headers
4555 + *
4556 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4557 + *
4558 + * Copyright (C) 2004, Ferenc Havasi
4559 + *
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.
4564 + *
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.
4569 + *
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.
4573 + *
4574 + */
4575 +
4576 +extern int jffs2_bbc_inode_not_found;
4577 +
4578 +void jffs2_bbc_load_model(void *sb);
4579 +void jffs2_bbc_unload_model(void *sb);
4580 +
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
4586 @@ -0,0 +1,788 @@
4587 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4588 +
4589 +/*
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
4594 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4595 +*/
4596 +
4597 +/*
4598 +Original copyright follows:
4599 +
4600 +**************************************************************
4601 + LZARI.C -- A Data Compression Program
4602 + (tab = 4 spaces)
4603 +**************************************************************
4604 + 4/7/1989 Haruhiko Okumura
4605 + Use, distribute, and modify this program freely.
4606 + Please send me your improved versions.
4607 + PC-VAN SCIENCE
4608 + NIFTY-Serve PAF01022
4609 + CompuServe 74050,1022
4610 +**************************************************************
4611 +
4612 +LZARI.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
4613 +All rights reserved. Permission granted for non-commercial use.
4614 +
4615 +*/
4616 +
4617 +/*
4618 +
4619 + 2004-02-18 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
4620 + Removed unused variables and fixed no return value
4621 +
4622 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
4623 + Initial release
4624 +
4625 +*/
4626 +
4627 +/* lzari.c */
4628 +
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 */
4634 +
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. */
4642 +
4643 +static void InitTree(void) /* Initialize trees */
4644 +{
4645 + unsigned long i;
4646 +
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. */
4654 +
4655 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL; /* root */
4656 + for (i = 0; i < N; i++) dad[i] = NIL; /* node */
4657 +}
4658 +
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. */
4666 +{
4667 + unsigned long i, p, temp;
4668 + unsigned char *key;
4669 + signed long cmp;
4670 +
4671 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
4672 + rson[r] = lson[r] = NIL; match_length = 0;
4673 + for ( ; ; ) {
4674 + if (cmp >= 0) {
4675 + if (rson[p] != NIL) p = rson[p];
4676 + else { rson[p] = r; dad[r] = p; return; }
4677 + } else {
4678 + if (lson[p] != NIL) p = lson[p];
4679 + else { lson[p] = r; dad[r] = p; return; }
4680 + }
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;
4690 + }
4691 + }
4692 + }
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 */
4698 +}
4699 +
4700 +static void DeleteNode(unsigned long p) /* Delete node p from tree */
4701 +{
4702 + unsigned long q;
4703 +
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];
4707 + else {
4708 + q = lson[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;
4713 + }
4714 + rson[q] = rson[p]; dad[rson[p]] = q;
4715 + }
4716 + dad[q] = dad[p];
4717 + if (rson[dad[p]] == p) rson[dad[p]] = q;
4718 + else lson[dad[p]] = q;
4719 + dad[p] = NIL;
4720 +}
4721 +
4722 +/********** Arithmetic Compression **********/
4723 +
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. */
4728 +
4729 +#define M 15
4730 +
4731 +/* Q1 (= 2 to the M) must be sufficiently large, but not so
4732 + large as the unsigned long 4 * Q1 * (Q1 - 1) overflows. */
4733 +
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)
4739 +
4740 +#define N_CHAR (256 - THRESHOLD + F)
4741 + /* character code = 0, 1, ..., N_CHAR - 1 */
4742 +
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 */
4748 +
4749 +static void StartModel(void) /* Initialize model */
4750 +{
4751 + unsigned long ch, sym, i;
4752 +
4753 + sym_cum[N_CHAR] = 0;
4754 + for (sym = N_CHAR; sym >= 1; sym--) {
4755 + ch = sym - 1;
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];
4759 + }
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! */
4766 +}
4767 +
4768 +static void UpdateModel(unsigned long sym)
4769 +{
4770 + unsigned long c, ch_i, ch_sym;
4771 + unsigned long i;
4772 + if (sym_cum[0] >= MAX_CUM) {
4773 + c = 0;
4774 + for (i = N_CHAR; i > 0; i--) {
4775 + sym_cum[i] = c;
4776 + c += (sym_freq[i] = (sym_freq[i] + 1) >> 1);
4777 + }
4778 + sym_cum[0] = c;
4779 + }
4780 + for (i = sym; sym_freq[i] == sym_freq[i - 1]; i--) ;
4781 + if (i < sym) {
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;
4785 + }
4786 + sym_freq[i]++;
4787 + while (--i > 0) sym_cum[i]++;
4788 + sym_cum[0]++;
4789 +}
4790 +
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 */
4795 +{
4796 + unsigned long i, j, k;
4797 +
4798 + i = 1; j = N_CHAR;
4799 + while (i < j) {
4800 + k = (i + j) / 2;
4801 + if (sym_cum[k] > x) i = k + 1; else j = k;
4802 + }
4803 + return i;
4804 +}
4805 +
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 */
4810 +{
4811 + unsigned long i, j, k;
4812 +
4813 + i = 1; j = N;
4814 + while (i < j) {
4815 + k = (i + j) / 2;
4816 + if (position_cum[k] > x) i = k + 1; else j = k;
4817 + }
4818 + return i - 1;
4819 +}
4820 +
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 */
4824 +
4825 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
4826 + unsigned long *dstlen)
4827 +{
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;
4839 + ip = srcbuf;
4840 + op = dstbuf;
4841 + StartModel();
4842 + InitTree(); /* initialize trees */
4843 + s = 0; r = N - F;
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
4848 + the buffer */
4849 + read = len;
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. */
4856 + do {
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];
4865 + for ( ; ; ) {
4866 + if (high <= Q2) {
4867 + if ((mask >>= 1) == 0) {
4868 + if (op >= dstend) {
4869 + *dstlen = written;
4870 + return -1;
4871 + }
4872 + *(op++) = buffer;
4873 + buffer = 0;
4874 + mask = 128;
4875 + written++;
4876 + *srclen = read;
4877 + }
4878 + for ( ; shifts > 0; shifts--) {
4879 + buffer |= mask;
4880 + if ((mask >>= 1) == 0) {
4881 + if (op >= dstend) {
4882 + *dstlen = written;
4883 + return -1;
4884 + }
4885 + *(op++) = buffer;
4886 + buffer = 0;
4887 + mask = 128;
4888 + written++;
4889 + *srclen = read;
4890 + }
4891 + }
4892 + } else if (low >= Q2) {
4893 + buffer |= mask;
4894 + if ((mask >>= 1) == 0) {
4895 + if (op >= dstend) {
4896 + *dstlen = written;
4897 + return -1;
4898 + }
4899 + *(op++) = buffer;
4900 + buffer = 0;
4901 + mask = 128;
4902 + written++;
4903 + *srclen = read;
4904 + }
4905 + for ( ; shifts > 0; shifts--) {
4906 + if ((mask >>= 1) == 0) {
4907 + if (op >= dstend) {
4908 + *dstlen = written;
4909 + return -1;
4910 + }
4911 + *(op++) = buffer;
4912 + buffer = 0;
4913 + mask = 128;
4914 + written++;
4915 + *srclen = read;
4916 + }
4917 + }
4918 + low -= Q2;
4919 + high -= Q2;
4920 + } else if (low >= Q1 && high <= Q3) {
4921 + shifts++;
4922 + low -= Q1;
4923 + high -= Q1;
4924 + } else break;
4925 + low += low; high += high;
4926 + }
4927 + UpdateModel(sym);
4928 + } else {
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];
4933 + for ( ; ; ) {
4934 + if (high <= Q2) {
4935 + if ((mask >>= 1) == 0) {
4936 + if (op >= dstend) {
4937 + *dstlen = written;
4938 + return -1;
4939 + }
4940 + *(op++) = buffer;
4941 + buffer = 0;
4942 + mask = 128;
4943 + written++;
4944 + *srclen = read;
4945 + }
4946 + for ( ; shifts > 0; shifts--) {
4947 + buffer |= mask;
4948 + if ((mask >>= 1) == 0) {
4949 + if (op >= dstend) {
4950 + *dstlen = written;
4951 + return -1;
4952 + }
4953 + *(op++) = buffer;
4954 + buffer = 0;
4955 + mask = 128;
4956 + written++;
4957 + *srclen = read;
4958 + }
4959 + }
4960 + } else if (low >= Q2) {
4961 + buffer |= mask;
4962 + if ((mask >>= 1) == 0) {
4963 + if (op >= dstend) {
4964 + *dstlen = written;
4965 + return -1;
4966 + }
4967 + *(op++) = buffer;
4968 + buffer = 0;
4969 + mask = 128;
4970 + written++;
4971 + *srclen = read;
4972 + }
4973 + for ( ; shifts > 0; shifts--) {
4974 + if ((mask >>= 1) == 0) {
4975 + if (op >= dstend) {
4976 + *dstlen = written;
4977 + return -1;
4978 + }
4979 + *(op++) = buffer;
4980 + buffer = 0;
4981 + mask = 128;
4982 + written++;
4983 + *srclen = read;
4984 + }
4985 + }
4986 + low -= Q2;
4987 + high -= Q2;
4988 + } else if (low >= Q1 && high <= Q3) {
4989 + shifts++;
4990 + low -= Q1;
4991 + high -= Q1;
4992 + } else break;
4993 + low += low; high += high;
4994 + }
4995 + UpdateModel(sym);
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];
4999 + for ( ; ; ) {
5000 + if (high <= Q2) {
5001 + if ((mask >>= 1) == 0) {
5002 + if (op >= dstend) {
5003 + *dstlen = written;
5004 + return -1;
5005 + }
5006 + *(op++) = buffer;
5007 + buffer = 0;
5008 + mask = 128;
5009 + written++;
5010 + *srclen = read;
5011 + }
5012 + for ( ; shifts > 0; shifts--) {
5013 + buffer |= mask;
5014 + if ((mask >>= 1) == 0) {
5015 + if (op >= dstend) {
5016 + *dstlen = written;
5017 + return -1;
5018 + }
5019 + *(op++) = buffer;
5020 + buffer = 0;
5021 + mask = 128;
5022 + written++;
5023 + *srclen = read;
5024 + }
5025 + }
5026 + } else {
5027 + if (low >= Q2) {
5028 + buffer |= mask;
5029 + if ((mask >>= 1) == 0) {
5030 + if (op >= dstend) {
5031 + *dstlen = written;
5032 + return -1;
5033 + }
5034 + *(op++) = buffer;
5035 + buffer = 0;
5036 + mask = 128;
5037 + written++;
5038 + *srclen = read;
5039 + }
5040 + for ( ; shifts > 0; shifts--) {
5041 + if ((mask >>= 1) == 0) {
5042 + if (op >= dstend) {
5043 + *dstlen = written;
5044 + return -1;
5045 + }
5046 + *(op++) = buffer;
5047 + buffer = 0;
5048 + mask = 128;
5049 + written++;
5050 + *srclen = read;
5051 + }
5052 + }
5053 + low -= Q2;
5054 + high -= Q2;
5055 + } else {
5056 + if ((low >= Q1) && (high <= Q3)) {
5057 + shifts++;
5058 + low -= Q1;
5059 + high -= Q1;
5060 + } else {
5061 + break;
5062 + }
5063 + }
5064 + }
5065 + low += low;
5066 + high += high;
5067 + }
5068 + }
5069 + last_match_length = match_length;
5070 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
5071 + c = *(ip++);
5072 + DeleteNode(s);
5073 + text_buf[s] = c;
5074 + if (s < F - 1)
5075 + text_buf[s + N] = c;
5076 + s = (s + 1) & (N - 1);
5077 + r = (r + 1) & (N - 1);
5078 + InsertNode(r);
5079 + }
5080 + read += i;
5081 + while (i++ < last_match_length) {
5082 + DeleteNode(s);
5083 + s = (s + 1) & (N - 1);
5084 + r = (r + 1) & (N - 1);
5085 + if (--len) InsertNode(r);
5086 + }
5087 + } while (len > 0);
5088 + shifts++;
5089 + if (low < Q1) {
5090 + if ((mask >>= 1) == 0) {
5091 + if (op >= dstend) {
5092 + *dstlen = written;
5093 + return -1;
5094 + }
5095 + *(op++) = buffer;
5096 + buffer = 0;
5097 + mask = 128;
5098 + written++;
5099 + *srclen = read;
5100 + }
5101 + for ( ; shifts > 0; shifts--) {
5102 + buffer |= mask;
5103 + if ((mask >>= 1) == 0) {
5104 + if (op >= dstend) {
5105 + *dstlen = written;
5106 + return -1;
5107 + }
5108 + *(op++) = buffer;
5109 + buffer = 0;
5110 + mask = 128;
5111 + written++;
5112 + *srclen = read;
5113 + }
5114 + }
5115 + } else {
5116 + buffer |= mask;
5117 + if ((mask >>= 1) == 0) {
5118 + if (op >= dstend) {
5119 + *dstlen = written;
5120 + return -1;
5121 + }
5122 + *(op++) = buffer;
5123 + buffer = 0;
5124 + mask = 128;
5125 + written++;
5126 + *srclen = read;
5127 + }
5128 + for ( ; shifts > 0; shifts--) {
5129 + if ((mask >>= 1) == 0) {
5130 + if (op >= dstend) {
5131 + *dstlen = written;
5132 + return -1;
5133 + }
5134 + *(op++) = buffer;
5135 + buffer = 0;
5136 + mask = 128;
5137 + written++;
5138 + *srclen = read;
5139 + }
5140 + }
5141 + }
5142 + for (i = 0; i < 7; i++) {
5143 + if ((mask >>= 1) == 0) {
5144 + if (op >= dstend) {
5145 + *dstlen = written;
5146 + return -1;
5147 + }
5148 + *(op++) = buffer;
5149 + buffer = 0;
5150 + mask = 128;
5151 + written++;
5152 + *srclen = read;
5153 + }
5154 + }
5155 + *dstlen = written;
5156 + return 0;
5157 +}
5158 +
5159 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
5160 + unsigned long dstlen) /* Just the reverse of Encode(). */
5161 +{
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;
5171 + ip = srcbuf;
5172 + op = dstbuf;
5173 + for (i = 0; i < M + 2; i++) {
5174 + value *= 2;
5175 + if ((mask >>= 1) == 0) {
5176 + buffer = (ip >= srcend) ? 0 : *(ip++);
5177 + mask = 128;
5178 + }
5179 + value += ((buffer & mask) != 0);
5180 + }
5181 + StartModel();
5182 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
5183 + r = N - F;
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];
5190 + for ( ; ; ) {
5191 + if (low >= Q2) {
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;
5197 + value *= 2;
5198 + if ((mask >>= 1) == 0) {
5199 + buffer = (ip >= srcend) ? 0 : *(ip++);
5200 + mask = 128;
5201 + }
5202 + value += ((buffer & mask) != 0);
5203 + }
5204 + c = sym_to_char[sym];
5205 + UpdateModel(sym);
5206 + if (c < 256) {
5207 + if (op >= dstend) return -1;
5208 + *(op++) = c;
5209 + text_buf[r++] = c;
5210 + r &= (N - 1);
5211 + } else {
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];
5218 + for ( ; ; ) {
5219 + if (low >= Q2) {
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;
5225 + value *= 2;
5226 + if ((mask >>= 1) == 0) {
5227 + buffer = (ip >= srcend) ? 0 : *(ip++);
5228 + mask = 128;
5229 + }
5230 + value += ((buffer & mask) != 0);
5231 + }
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;
5236 + *(op++) = c;
5237 + text_buf[r++] = c;
5238 + r &= (N - 1);
5239 + }
5240 + }
5241 + }
5242 + return 0;
5243 +}
5244 +
5245 +/* interface to jffs2 bbc follows */
5246 +
5247 +#include "jffs2_bbc_framework.h"
5248 +
5249 +#define JFFS2_BBC_LZARI_BLOCK_SIGN {0x73, 0x9a, 0x1c, 0x4d}
5250 +
5251 +static int
5252 +jffs2_bbc_lzari_compressor_init (void);
5253 +
5254 +static void
5255 +jffs2_bbc_lzari_compressor_deinit (void);
5256 +
5257 +static int
5258 +jffs2_bbc_lzari_compress (void *model, unsigned char *input,
5259 + unsigned char *output, unsigned long *sourcelen,
5260 + unsigned long *dstlen);
5261 +
5262 +static int
5263 +jffs2_bbc_lzari_estimate (void *model, unsigned char *input,
5264 + unsigned long sourcelen, unsigned long *dstlen,
5265 + unsigned long *readtime, unsigned long *writetime);
5266 +
5267 +static int
5268 +jffs2_bbc_lzari_decompress (void *model, unsigned char *input,
5269 + unsigned char *output, unsigned long sourcelen,
5270 + unsigned long dstlen);
5271 +
5272 +static char *
5273 +jffs2_bbc_lzari_proc_info (void);
5274 +
5275 +static int
5276 +jffs2_bbc_lzari_proc_command (char *command);
5277 +
5278 +struct jffs2_bbc_compressor_type jffs2_bbc_lzari = {
5279 + "lzari",
5280 + 0,
5281 + JFFS2_BBC_LZARI_BLOCK_SIGN,
5282 + jffs2_bbc_lzari_compressor_init,
5283 + NULL,
5284 + NULL,
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
5291 +};
5292 +
5293 +static int
5294 +jffs2_bbc_lzari_compressor_init (void)
5295 +{
5296 + return 0;
5297 +}
5298 +
5299 +static void
5300 +jffs2_bbc_lzari_compressor_deinit (void)
5301 +{
5302 +}
5303 +
5304 +static int
5305 +jffs2_bbc_lzari_compress (void *model, unsigned char *input,
5306 + unsigned char *output, unsigned long *sourcelen,
5307 + unsigned long *dstlen)
5308 +{
5309 + int retval;
5310 + unsigned long dst = *dstlen;
5311 + *(output++) = jffs2_bbc_lzari.block_sign[0];
5312 + *(output++) = jffs2_bbc_lzari.block_sign[1];
5313 + dst -= 2;
5314 + retval = Encode(input, output, sourcelen, &dst);
5315 + dst += 2;
5316 + *dstlen = dst;
5317 + return retval;
5318 +}
5319 +
5320 +static int
5321 +jffs2_bbc_lzari_estimate (void *model, unsigned char *input,
5322 + unsigned long sourcelen, unsigned long *dstlen,
5323 + unsigned long *readtime, unsigned long *writetime)
5324 +{
5325 + *dstlen = sourcelen / 2;
5326 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 15;
5327 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 7;
5328 + return 0;
5329 +}
5330 +
5331 +static int
5332 +jffs2_bbc_lzari_decompress (void *model, unsigned char *input,
5333 + unsigned char *output, unsigned long sourcelen,
5334 + unsigned long dstlen)
5335 +{
5336 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzari.block_sign[0] ) ||
5337 + ( *(input++) != (unsigned char)jffs2_bbc_lzari.block_sign[1] )
5338 + ) {
5339 + return -1;
5340 + } else {
5341 + return Decode(input, output, sourcelen - 2, dstlen);
5342 + }
5343 +}
5344 +
5345 +static char *
5346 +jffs2_bbc_lzari_proc_info (void)
5347 +{
5348 + return "Lempel-Ziv-Arithmetic coding compression module";
5349 +}
5350 +
5351 +static int
5352 +jffs2_bbc_lzari_proc_command (char *command)
5353 +{
5354 + return 0;
5355 +}
5356 +
5357 +struct jffs2_bbc_compressor_type *
5358 +jffs2_bbc_lzari_init (int mode)
5359 +{
5360 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzari) == 0)
5361 + {
5362 + return &jffs2_bbc_lzari;
5363 + }
5364 + else
5365 + {
5366 + return NULL;
5367 + }
5368 +}
5369 +
5370 +void
5371 +jffs2_bbc_lzari_deinit (void)
5372 +{
5373 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzari);
5374 +}
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
5378 @@ -0,0 +1,747 @@
5379 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
5380 +
5381 +/*
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
5386 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
5387 +*/
5388 +
5389 +/*
5390 +Original copyright follows:
5391 +
5392 +**************************************************************
5393 + lzhuf.c
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 +**************************************************************
5398 +
5399 +LZHUF.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
5400 +All rights reserved. Permission granted for non-commercial use.
5401 +
5402 +*/
5403 +
5404 +/*
5405 +
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
5409 +
5410 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
5411 + Initial release
5412 +
5413 +*/
5414 +
5415 +/* required because of memmove */
5416 +#ifndef __KERNEL__
5417 + #include <string.h>
5418 +#else
5419 + #include <linux/string.h>
5420 +#endif
5421 +
5422 +/* lzhuf.c */
5423 +
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 */
5429 +
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. */
5437 +
5438 +static void InitTree(void) /* initialize trees */
5439 +{
5440 + unsigned long i;
5441 +
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. */
5449 +
5450 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
5451 + for (i = 0; i < N; i++) dad[i] = NIL;
5452 +}
5453 +
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. */
5461 +{
5462 + unsigned long i, p, c;
5463 + signed long cmp;
5464 + unsigned char *key;
5465 +
5466 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
5467 + rson[r] = lson[r] = NIL; match_length = 0;
5468 + for ( ; ; ) {
5469 + if (cmp >= 0) {
5470 + if (rson[p] != NIL) p = rson[p];
5471 + else { rson[p] = r; dad[r] = p; return; }
5472 + } else {
5473 + if (lson[p] != NIL) p = lson[p];
5474 + else { lson[p] = r; dad[r] = p; return; }
5475 + }
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;
5482 + }
5483 + if (i == match_length) {
5484 + if ((c = ((r - p) & (N - 1)) - 1) < match_position) {
5485 + match_position = c;
5486 + }
5487 + }
5488 + }
5489 + }
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 */
5495 +}
5496 +
5497 +static void DeleteNode(unsigned long p) /* deletes node p from tree */
5498 +{
5499 + unsigned long q;
5500 +
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];
5504 + else {
5505 + q = lson[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;
5510 + }
5511 + rson[q] = rson[p]; dad[rson[p]] = q;
5512 + }
5513 + dad[q] = dad[p];
5514 + if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
5515 + dad[p] = NIL;
5516 +}
5517 +
5518 +/* Huffman coding */
5519 +
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. */
5526 +
5527 +typedef unsigned long uchar; // much-much faster
5528 +
5529 +/* table for encoding and decoding the upper 6 bits of position */
5530 +
5531 +/* for encoding */
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
5541 +};
5542 +
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
5552 +};
5553 +
5554 +/* for decoding */
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,
5588 +};
5589 +
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,
5623 +};
5624 +
5625 +static unsigned long freq[T + 1]; /* frequency table */
5626 +
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. */
5630 +
5631 +static unsigned long son[T]; /* pointers to child nodes (son[], son[] + 1) */
5632 +
5633 +/* initialization of tree */
5634 +
5635 +static void StartHuff(void)
5636 +{
5637 + unsigned long i, j;
5638 +
5639 + for (i = 0; i < N_CHAR; i++) {
5640 + freq[i] = 1;
5641 + son[i] = i + T;
5642 + prnt[i + T] = i;
5643 + }
5644 + i = 0; j = N_CHAR;
5645 + while (j <= R) {
5646 + freq[j] = freq[i] + freq[i + 1];
5647 + son[j] = i;
5648 + prnt[i] = prnt[i + 1] = j;
5649 + i += 2; j++;
5650 + }
5651 + freq[T] = 0xffff;
5652 + prnt[R] = 0;
5653 +}
5654 +
5655 +/* reconstruction of tree */
5656 +
5657 +static void reconst(void)
5658 +{
5659 + unsigned long f, l, i, j, k;
5660 +
5661 + /* collect leaf nodes in the first half of the table */
5662 + /* and replace the freq by (freq + 1) / 2. */
5663 + j = 0;
5664 + for (i = 0; i < T; i++) {
5665 + if (son[i] >= T) {
5666 + freq[j] = (freq[i] + 1) / 2;
5667 + son[j] = son[i];
5668 + j++;
5669 + }
5670 + }
5671 + /* begin constructing tree by connecting sons */
5672 + for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
5673 + k = i + 1;
5674 + f = freq[j] = freq[i] + freq[k];
5675 + for (k = j - 1; f < freq[k]; k--);
5676 + k++;
5677 + l = (j - k) * 2;
5678 + memmove(&freq[k + 1], &freq[k], l*sizeof(unsigned long));
5679 + freq[k] = f;
5680 + memmove(&son[k + 1], &son[k], l*sizeof(unsigned long));
5681 + son[k] = i;
5682 + }
5683 + /* connect prnt */
5684 + for (i = 0; i < T; i++) {
5685 + if ((k = son[i]) >= T) {
5686 + prnt[k] = i;
5687 + } else {
5688 + prnt[k] = prnt[k + 1] = i;
5689 + }
5690 + }
5691 +}
5692 +
5693 +/* increment frequency of given code by one, and update tree */
5694 +
5695 +static void update(unsigned long c)
5696 +{
5697 + unsigned long i, j, k, l;
5698 +
5699 + if (freq[R] == MAX_FREQ) {
5700 + reconst();
5701 + }
5702 + c = prnt[c + T];
5703 + do {
5704 + k = ++freq[c];
5705 +
5706 + /* if the order is disturbed, exchange nodes */
5707 + if (k > freq[l = c + 1]) {
5708 + while (k > freq[++l]);
5709 + l--;
5710 + freq[c] = freq[l];
5711 + freq[l] = k;
5712 +
5713 + i = son[c];
5714 + prnt[i] = l;
5715 + if (i < T) prnt[i + 1] = l;
5716 +
5717 + j = son[l];
5718 + son[l] = i;
5719 +
5720 + prnt[j] = c;
5721 + if (j < T) prnt[j + 1] = c;
5722 + son[c] = j;
5723 +
5724 + c = l;
5725 + }
5726 + } while (c = prnt[c]); /* repeat up to root */
5727 +}
5728 +
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 */
5732 +
5733 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
5734 + unsigned long *dstlen)
5735 +{
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;
5742 + uchar putlen = 0;
5743 + unsigned char *srcend = srcbuf + *srclen;
5744 + unsigned char *dstend = dstbuf + *dstlen;
5745 + ip = srcbuf;
5746 + op = dstbuf;
5747 + StartHuff();
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;
5754 + s = 0; r = N - F;
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
5759 + the buffer */
5760 + read = len;
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. */
5767 + do {
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. */
5772 + c = text_buf[r];
5773 + i = 0; j = 0; k = prnt[c + T];
5774 + do {
5775 + i >>= 1;
5776 + /* if node's address is odd-numbered, choose bigger brother node */
5777 + if (k & 1) i |= 0x8000;
5778 + j++;
5779 + } while ((k = prnt[k]) != R);
5780 + putbuf |= i >> putlen;
5781 + if ((putlen += j) >= 8) {
5782 + if (op >= dstend) {
5783 + *dstlen = written;
5784 + return -1;
5785 + }
5786 + *(op++) = putbuf >> 8;
5787 + if ((putlen -= 8) >= 8) {
5788 + if (op >= dstend) {
5789 + *dstlen = written;
5790 + return -1;
5791 + }
5792 + *(op++) = putbuf;
5793 + written += 2;
5794 + putlen -= 8;
5795 + putbuf = i << (j - putlen); /**warm**/
5796 + } else {
5797 + putbuf <<= 8;
5798 + written++;
5799 + }
5800 + *srclen = read;
5801 + }
5802 + update(c);
5803 + } else {
5804 + c = 255 - THRESHOLD + match_length;
5805 + i = 0; j = 0; k = prnt[c + T];
5806 + do {
5807 + i >>= 1;
5808 + /* if node's address is odd-numbered, choose bigger brother node */
5809 + if (k & 1) i |= 0x8000;
5810 + j++;
5811 + } while ((k = prnt[k]) != R);
5812 + putbuf |= i >> putlen;
5813 + if ((putlen += j) >= 8) {
5814 + if (op >= dstend) {
5815 + *dstlen = written;
5816 + return -1;
5817 + }
5818 + *(op++) = putbuf >> 8;
5819 + if ((putlen -= 8) >= 8) {
5820 + if (op >= dstend) {
5821 + *dstlen = written;
5822 + return -1;
5823 + }
5824 + *(op++) = putbuf;
5825 + written += 2;
5826 + putlen -= 8;
5827 + putbuf = i << (j - putlen); /**warm**/
5828 + } else {
5829 + putbuf <<= 8;
5830 + written++;
5831 + }
5832 + *srclen = read;
5833 + }
5834 + update(c);
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;
5841 + return -1;
5842 + }
5843 + *(op++) = putbuf >> 8;
5844 + if ((putlen -= 8) >= 8) {
5845 + if (op >= dstend) {
5846 + *dstlen = written;
5847 + return -1;
5848 + }
5849 + *(op++) = putbuf;
5850 + written += 2;
5851 + putlen -= 8;
5852 + putbuf = i << (j - putlen); /**hot**/
5853 + } else {
5854 + putbuf <<= 8;
5855 + written++;
5856 + }
5857 + *srclen = read;
5858 + }
5859 + j = 6;
5860 + i = (match_position & 0x3f) << 10;
5861 + putbuf |= i >> putlen;
5862 + if ((putlen += j) >= 8) {
5863 + if (op >= dstend) {
5864 + *dstlen = written;
5865 + return -1;
5866 + }
5867 + *(op++) = putbuf >> 8;
5868 + if ((putlen -= 8) >= 8) {
5869 + if (op >= dstend) {
5870 + *dstlen = written;
5871 + return -1;
5872 + }
5873 + *(op++) = putbuf;
5874 + written += 2;
5875 + putlen -= 8;
5876 + putbuf = i << (j - putlen); /**hot**/
5877 + } else {
5878 + putbuf <<= 8;
5879 + written++;
5880 + }
5881 + *srclen = read;
5882 + }
5883 + }
5884 + last_match_length = match_length;
5885 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
5886 + c = *(ip++);
5887 + DeleteNode(s);
5888 + text_buf[s] = c;
5889 + if (s < F - 1)
5890 + text_buf[s + N] = c;
5891 + s = (s + 1) & (N - 1);
5892 + r = (r + 1) & (N - 1);
5893 + InsertNode(r);
5894 + }
5895 + read += i;
5896 + while (i++ < last_match_length) {
5897 + DeleteNode(s);
5898 + s = (s + 1) & (N - 1);
5899 + r = (r + 1) & (N - 1);
5900 + if (--len) InsertNode(r);
5901 + }
5902 + } while (len > 0);
5903 + if (putlen) {
5904 + if (op >= dstend) {
5905 + *dstlen = written;
5906 + return -1;
5907 + }
5908 + *(op++) = putbuf >> 8;
5909 + written++;
5910 + *srclen = read;
5911 + }
5912 + *dstlen = written;
5913 + return 0;
5914 +}
5915 +
5916 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
5917 + unsigned long dstlen) /* Just the reverse of Encode(). */
5918 +{
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;
5924 + uchar getlen = 0;
5925 + ip = srcbuf;
5926 + op = dstbuf;
5927 + StartHuff();
5928 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
5929 + r = N - F;
5930 + while (op < dstend) {
5931 + c = son[R];
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 */
5935 + while (c < T) {
5936 + while (getlen <= 8) {
5937 + unsigned short t;
5938 + t = (ip >= srcend) ? 0 : *(ip++);
5939 + getbuf |= t << (8 - getlen);
5940 + getlen += 8;
5941 + }
5942 + c += ((signed short)getbuf < 0);
5943 + getbuf <<= 1;
5944 + getlen--;
5945 + c = son[c];
5946 + }
5947 + c -= T;
5948 + update(c);
5949 + if (c < 256) {
5950 + if (op >= dstend) return -1;
5951 + *(op++) = c;
5952 + text_buf[r++] = c;
5953 + r &= (N - 1);
5954 + } else {
5955 + j = c - 255 + THRESHOLD;
5956 + while (getlen <= 8) {
5957 + unsigned short t;
5958 + t = (ip >= srcend) ? 0 : *(ip++);
5959 + getbuf |= t << (8 - getlen);
5960 + getlen += 8;
5961 + }
5962 + i = getbuf >> 8;
5963 + getbuf <<= 8;
5964 + getlen -= 8;
5965 + c = d_code[i] << 6;
5966 + k = d_len[i];
5967 + /* read lower 6 bits verbatim */
5968 + k -= 2;
5969 + while (k--) {
5970 + while (getlen <= 8) {
5971 + unsigned short t;
5972 + t = (ip >= srcend) ? 0 : *(ip++);
5973 + getbuf |= t << (8 - getlen);
5974 + getlen += 8;
5975 + }
5976 + i = (i << 1) + ((signed short)getbuf < 0);
5977 + getbuf <<= 1;
5978 + getlen--;
5979 + }
5980 + i = c | (i & 0x3F);
5981 + i = r - i - 1;
5982 + i &= (N - 1);
5983 + for (k = 0; k < j; k++) {
5984 + c = text_buf[(i + k) & (N - 1)];
5985 + if (op >= dstend) return -1;
5986 + *(op++) = c;
5987 + text_buf[r++] = c;
5988 + r &= (N - 1);
5989 + }
5990 + }
5991 + }
5992 + return 0;
5993 +}
5994 +
5995 +/* interface to jffs2 bbc follows */
5996 +
5997 +#include "jffs2_bbc_framework.h"
5998 +
5999 +
6000 +#define JFFS2_BBC_LZHD_BLOCK_SIGN {0x3a, 0x98, 0xf7, 0xda}
6001 +
6002 +static int
6003 +jffs2_bbc_lzhd_compressor_init (void);
6004 +
6005 +static void
6006 +jffs2_bbc_lzhd_compressor_deinit (void);
6007 +
6008 +static int
6009 +jffs2_bbc_lzhd_compress (void *model, unsigned char *input,
6010 + unsigned char *output, unsigned long *sourcelen,
6011 + unsigned long *dstlen);
6012 +
6013 +static int
6014 +jffs2_bbc_lzhd_estimate (void *model, unsigned char *input,
6015 + unsigned long sourcelen, unsigned long *dstlen,
6016 + unsigned long *readtime, unsigned long *writetime);
6017 +
6018 +static int
6019 +jffs2_bbc_lzhd_decompress (void *model, unsigned char *input,
6020 + unsigned char *output, unsigned long sourcelen,
6021 + unsigned long dstlen);
6022 +
6023 +static char *
6024 +jffs2_bbc_lzhd_proc_info (void);
6025 +
6026 +static int
6027 +jffs2_bbc_lzhd_proc_command (char *command);
6028 +
6029 +struct jffs2_bbc_compressor_type jffs2_bbc_lzhd = {
6030 + "lzhd",
6031 + 0,
6032 + JFFS2_BBC_LZHD_BLOCK_SIGN,
6033 + jffs2_bbc_lzhd_compressor_init,
6034 + NULL,
6035 + NULL,
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
6042 +};
6043 +
6044 +static int
6045 +jffs2_bbc_lzhd_compressor_init (void)
6046 +{
6047 + return 0;
6048 +}
6049 +
6050 +static void
6051 +jffs2_bbc_lzhd_compressor_deinit (void)
6052 +{
6053 +}
6054 +
6055 +static int
6056 +jffs2_bbc_lzhd_compress (void *model, unsigned char *input,
6057 + unsigned char *output, unsigned long *sourcelen,
6058 + unsigned long *dstlen)
6059 +{
6060 + int retval;
6061 + unsigned long dst = *dstlen;
6062 + *(output++) = jffs2_bbc_lzhd.block_sign[0];
6063 + *(output++) = jffs2_bbc_lzhd.block_sign[1];
6064 + dst -= 2;
6065 + retval = Encode(input, output, sourcelen, &dst);
6066 + dst += 2;
6067 + *dstlen = dst;
6068 + return retval;
6069 +}
6070 +
6071 +static int
6072 +jffs2_bbc_lzhd_estimate (void *model, unsigned char *input,
6073 + unsigned long sourcelen, unsigned long *dstlen,
6074 + unsigned long *readtime, unsigned long *writetime)
6075 +{
6076 + *dstlen = sourcelen * 55 / 100;
6077 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 8;
6078 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 65 / 10;
6079 + return 0;
6080 +}
6081 +
6082 +static int
6083 +jffs2_bbc_lzhd_decompress (void *model, unsigned char *input,
6084 + unsigned char *output, unsigned long sourcelen,
6085 + unsigned long dstlen)
6086 +{
6087 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzhd.block_sign[0] ) ||
6088 + ( *(input++) != (unsigned char)jffs2_bbc_lzhd.block_sign[1] )
6089 + ) {
6090 + return -1;
6091 + } else {
6092 + return Decode(input, output, sourcelen - 2, dstlen);
6093 + }
6094 +}
6095 +
6096 +static char *
6097 +jffs2_bbc_lzhd_proc_info (void)
6098 +{
6099 + return "Lempel-Ziv-(dynamic) Huffman compression module";
6100 +}
6101 +
6102 +static int
6103 +jffs2_bbc_lzhd_proc_command (char *command)
6104 +{
6105 + return 0;
6106 +}
6107 +
6108 +struct jffs2_bbc_compressor_type *
6109 +jffs2_bbc_lzhd_init (int mode)
6110 +{
6111 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzhd) == 0)
6112 + {
6113 + return &jffs2_bbc_lzhd;
6114 + }
6115 + else
6116 + {
6117 + return NULL;
6118 + }
6119 +}
6120 +
6121 +void
6122 +jffs2_bbc_lzhd_deinit (void)
6123 +{
6124 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzhd);
6125 +}
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
6129 @@ -0,0 +1,2435 @@
6130 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
6131 +
6132 +/*
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
6136 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
6137 +*/
6138 +
6139 +/*
6140 + Original copyright notice follows:
6141 +
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
6150 +
6151 + These files are part of the LZO real-time data compression library.
6152 +
6153 + Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
6154 + All Rights Reserved.
6155 +
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.
6160 +
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.
6165 +
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.
6170 +
6171 + Markus F.X.J. Oberhumer
6172 + <markus@oberhumer.com>
6173 +*/
6174 +
6175 +/*
6176 +
6177 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
6178 + Initial release
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
6183 +
6184 +*/
6185 +
6186 +#ifndef __KERNEL__
6187 + #include <sys/types.h>
6188 + #include <stddef.h>
6189 + #include <string.h>
6190 + #include <limits.h>
6191 +#else
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 */
6198 +#endif
6199 +
6200 +/* data type definitions */
6201 +#define U32 unsigned long
6202 +#define S32 signed long
6203 +#define I32 long
6204 +#define U16 unsigned short
6205 +#define S16 signed short
6206 +#define I16 short
6207 +#define U8 unsigned char
6208 +#define S8 signed char
6209 +#define I8 char
6210 +
6211 +/*************************************/
6212 +
6213 +/* lzo_swd.ch */
6214 +
6215 +#define SWD_N N
6216 +#define SWD_F F
6217 +#define SWD_THRESHOLD THRESHOLD
6218 +
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
6223 +
6224 +/* minilzo.c */
6225 +
6226 +#define LZO_VERSION_DATE "Jul 12 2002"
6227 +#define LZO_VERSION_STRING "1.08"
6228 +#define LZO_VERSION 0x1080
6229 +
6230 +/* lzo_ptr.h */
6231 +
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;
6235 +
6236 +
6237 +/*************************************/
6238 +
6239 +/* config1x.h */
6240 +
6241 +#define M1_MAX_OFFSET 0x0400
6242 +#define M2_MAX_OFFSET 0x0800
6243 +#define M3_MAX_OFFSET 0x4000
6244 +#define M4_MAX_OFFSET 0xbfff
6245 +
6246 +#define MX_MAX_OFFSET (M1_MAX_OFFSET + M2_MAX_OFFSET)
6247 +
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
6256 +
6257 +#define M1_MARKER 0
6258 +#define M2_MARKER 64
6259 +#define M3_MARKER 32
6260 +#define M4_MARKER 16
6261 +
6262 +#define MIN_LOOKAHEAD (M2_MAX_LEN + 1)
6263 +
6264 +/* minilzo.c */
6265 +
6266 +#define LZO_BYTE(x) ((unsigned char) ((x) & 0xff))
6267 +
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))
6272 +
6273 +#define lzo_sizeof(type) ((lzo_uint) (sizeof(type)))
6274 +
6275 +#define LZO_HIGH(array) ((lzo_uint) (sizeof(array)/sizeof(*(array))))
6276 +
6277 +#define LZO_SIZE(bits) (1u << (bits))
6278 +#define LZO_MASK(bits) (LZO_SIZE(bits) - 1)
6279 +
6280 +#define LZO_LSIZE(bits) (1ul << (bits))
6281 +#define LZO_LMASK(bits) (LZO_LSIZE(bits) - 1)
6282 +
6283 +#define LZO_USIZE(bits) ((lzo_uint) 1 << (bits))
6284 +#define LZO_UMASK(bits) (LZO_USIZE(bits) - 1)
6285 +
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)))
6288 +
6289 +#define _LZO_STRINGIZE(x) #x
6290 +#define _LZO_MEXPAND(x) _LZO_STRINGIZE(x)
6291 +
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
6296 +
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)
6301 +
6302 +#define lzo_dict_t const lzo_bytep
6303 +#define lzo_dict_p lzo_dict_t *
6304 +#define lzo_moff_t lzo_uint
6305 +
6306 +#define MEMCPY8_DS(dest,src,len) \
6307 + memcpy(dest,src,len); \
6308 + dest += len; \
6309 + src += len
6310 +
6311 +#define MEMCPY_DS(dest,src,len) \
6312 + do *dest++ = *src++; \
6313 + while (--len > 0)
6314 +
6315 +#define MEMMOVE_DS(dest,src,len) \
6316 + do *dest++ = *src++; \
6317 + while (--len > 0)
6318 +
6319 +#define BZERO8_PTR(s,l,n) memset((s),0,(lzo_uint)(l)*(n))
6320 +
6321 +#define LZO_BASE 65521u
6322 +#define LZO_NMAX 5552
6323 +
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);
6329 +
6330 +#define IS_SIGNED(type) (((type) (-1)) < ((type) 0))
6331 +#define IS_UNSIGNED(type) (((type) (-1)) > ((type) 0))
6332 +
6333 +#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
6334 +
6335 +#define D_BITS 14
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)
6338 +
6339 +#define LZO_HASH LZO_HASH_LZO_INCREMENTAL_B
6340 +
6341 +#define DL_MIN_LEN M2_MIN_LEN
6342 +
6343 +#define D_SIZE LZO_SIZE(D_BITS)
6344 +#define D_MASK LZO_MASK(D_BITS)
6345 +
6346 +#define D_HIGH ((D_MASK >> 1) + 1)
6347 +
6348 +#define DINDEX1 D_INDEX1
6349 +#define DINDEX2 D_INDEX2
6350 +
6351 +#define DX2(p,s1,s2) \
6352 + (((((lzo_uint32)((p)[2]) << (s2)) ^ (p)[1]) << (s1)) ^ (p)[0])
6353 +
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)
6357 +
6358 +#define DENTRY(p,in) (p)
6359 +#define GINDEX(m_pos,m_off,dict,dindex,in) m_pos = dict[dindex]
6360 +
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)
6363 +
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) ))
6369 +
6370 +#define BOUNDS_CHECKING_OFF_IN_EXPR(expr) (expr)
6371 +
6372 +#define DD_BITS 0
6373 +#define DD_SIZE LZO_SIZE(DD_BITS)
6374 +#define DD_MASK LZO_MASK(DD_BITS)
6375 +
6376 +#define DL_BITS (D_BITS - DD_BITS)
6377 +#define DL_SIZE LZO_SIZE(DL_BITS)
6378 +#define DL_MASK LZO_MASK(DL_BITS)
6379 +
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)
6383 +
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))
6386 +
6387 +#define TEST_IP (ip < ip_end)
6388 +#define TEST_OP (op <= op_end)
6389 +
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
6395 +
6396 +/* lzo1x_9x.c */
6397 +
6398 +#define LZO_UINT_MAX UINT_MAX
6399 +#define N M4_MAX_OFFSET
6400 +#define THRESHOLD 1
6401 +#define F 2048
6402 +
6403 +#define SWD_BEST_OFF (LZO_MAX3( M2_MAX_LEN, M3_MAX_LEN, M4_MAX_LEN ) + 1)
6404 +
6405 +/* ../include/lzoconf.h */
6406 +
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;
6412 +
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)
6426 +
6427 +#define LZO_E_OK 0
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)
6436 +
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)
6440 +
6441 +typedef int
6442 + (*lzo_compress_t) (const lzo_byte * src, lzo_uint src_len,
6443 + lzo_byte * dst, lzo_uintp dst_len,
6444 + lzo_voidp wrkmem);
6445 +
6446 +typedef int
6447 + (*lzo_decompress_t) (const lzo_byte * src, lzo_uint src_len,
6448 + lzo_byte * dst, lzo_uintp dst_len,
6449 + lzo_voidp wrkmem);
6450 +
6451 +typedef int
6452 + (*lzo_optimize_t) (lzo_byte * src, lzo_uint src_len,
6453 + lzo_byte * dst, lzo_uintp dst_len,
6454 + lzo_voidp wrkmem);
6455 +
6456 +typedef int
6457 + (*lzo_compress_dict_t) (const lzo_byte * src, lzo_uint src_len,
6458 + lzo_byte * dst, lzo_uintp dst_len,
6459 + lzo_voidp wrkmem,
6460 + const lzo_byte * dict, lzo_uint dict_len);
6461 +
6462 +typedef int
6463 + (*lzo_decompress_dict_t) (const lzo_byte * src, lzo_uint src_len,
6464 + lzo_byte * dst, lzo_uintp dst_len,
6465 + lzo_voidp wrkmem,
6466 + const lzo_byte * dict, lzo_uint dict_len);
6467 +
6468 +typedef int
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);
6472 +
6473 +typedef int
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);
6477 +
6478 +typedef void (*lzo_progress_callback_t) (lzo_uint, lzo_uint);
6479 +
6480 +typedef union
6481 +{
6482 + lzo_bytep p;
6483 + lzo_uint u;
6484 +} __lzo_pu_u;
6485 +typedef union
6486 +{
6487 + lzo_bytep p;
6488 + lzo_uint32 u32;
6489 +} __lzo_pu32_u;
6490 +typedef union
6491 +{
6492 + void *vp;
6493 + lzo_bytep bp;
6494 + lzo_uint32 u32;
6495 + long l;
6496 +} lzo_align_t;
6497 +
6498 +/* lzo1x.h */
6499 +
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)))
6502 +
6503 +/* lzo_ptr.h */
6504 +
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)))
6515 +
6516 +typedef ptrdiff_t lzo_ptrdiff_t;
6517 +
6518 +typedef union
6519 +{
6520 + char a_char;
6521 + unsigned char a_uchar;
6522 + short a_short;
6523 + unsigned short a_ushort;
6524 + int a_int;
6525 + unsigned int a_uint;
6526 + long a_long;
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;
6536 + void *a_void_p;
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;
6544 + char *a_char_p;
6545 +}
6546 +lzo_full_align_t;
6547 +
6548 +/* lzo_mchw.ch */
6549 +
6550 +typedef struct
6551 +{
6552 + int init;
6553 +
6554 + lzo_uint look;
6555 +
6556 + lzo_uint m_len;
6557 + lzo_uint m_off;
6558 +
6559 + lzo_uint last_m_len;
6560 + lzo_uint last_m_off;
6561 +
6562 + const lzo_byte *bp;
6563 + const lzo_byte *ip;
6564 + const lzo_byte *in;
6565 + const lzo_byte *in_end;
6566 + lzo_byte *out;
6567 +
6568 + lzo_progress_callback_t cb;
6569 +
6570 + lzo_uint textsize;
6571 + lzo_uint codesize;
6572 + lzo_uint printcount;
6573 +
6574 + unsigned long lit_bytes;
6575 + unsigned long match_bytes;
6576 + unsigned long rep_bytes;
6577 + unsigned long lazy;
6578 +
6579 + lzo_uint r1_lit;
6580 + lzo_uint r1_m_len;
6581 +
6582 + unsigned long m1a_m, m1b_m, m2_m, m3_m, m4_m;
6583 + unsigned long lit1_r, lit2_r, lit3_r;
6584 +}
6585 +lzo1x_999_t;
6586 +
6587 +#define getbyte(c) ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
6588 +
6589 +/* lzo_swd.ch */
6590 +
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
6598 +
6599 +typedef struct
6600 +{
6601 + lzo_uint n;
6602 + lzo_uint f;
6603 + lzo_uint threshold;
6604 +
6605 + lzo_uint max_chain;
6606 + lzo_uint nice_length;
6607 + lzo_bool use_best_off;
6608 + lzo_uint lazy_insert;
6609 +
6610 + lzo_uint m_len;
6611 + lzo_uint m_off;
6612 + lzo_uint look;
6613 + int b_char;
6614 +
6615 + lzo_uint best_off[SWD_BEST_OFF];
6616 +
6617 + lzo1x_999_t *c;
6618 + lzo_uint m_pos;
6619 +
6620 + lzo_uint best_pos[SWD_BEST_OFF];
6621 +
6622 + const lzo_byte *dict;
6623 + const lzo_byte *dict_end;
6624 + lzo_uint dict_len;
6625 +
6626 + lzo_uint ip;
6627 + lzo_uint bp;
6628 + lzo_uint rp;
6629 + lzo_uint b_size;
6630 +
6631 + unsigned char *b_wrap;
6632 +
6633 + lzo_uint node_count;
6634 + lzo_uint first_rp;
6635 +
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];
6641 +
6642 + swd_uint head2[65536L];
6643 +}
6644 +lzo1x_999_swd_t;
6645 +
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))
6649 +
6650 +static __inline__ void
6651 +swd_getbyte (lzo1x_999_swd_t * s)
6652 +{
6653 + int c;
6654 +
6655 + if ((c = getbyte (*(s->c))) < 0)
6656 + {
6657 + if (s->look > 0)
6658 + --s->look;
6659 + }
6660 + else
6661 + {
6662 + s->b[s->ip] = LZO_BYTE (c);
6663 + if (s->ip < s->f)
6664 + s->b_wrap[s->ip] = LZO_BYTE (c);
6665 + }
6666 + if (++s->ip == s->b_size)
6667 + s->ip = 0;
6668 + if (++s->bp == s->b_size)
6669 + s->bp = 0;
6670 + if (++s->rp == s->b_size)
6671 + s->rp = 0;
6672 +}
6673 +
6674 +static void
6675 +swd_initdict (lzo1x_999_swd_t * s, const lzo_byte * dict, lzo_uint dict_len)
6676 +{
6677 + s->dict = s->dict_end = NULL;
6678 + s->dict_len = 0;
6679 +
6680 + if (!dict || dict_len <= 0)
6681 + return;
6682 + if (dict_len > s->n)
6683 + {
6684 + dict += dict_len - s->n;
6685 + dict_len = s->n;
6686 + }
6687 +
6688 + s->dict = dict;
6689 + s->dict_len = dict_len;
6690 + s->dict_end = dict + dict_len;
6691 + memcpy (s->b, dict, dict_len);
6692 + s->ip = dict_len;
6693 +}
6694 +
6695 +static void
6696 +swd_insertdict (lzo1x_999_swd_t * s, lzo_uint node, lzo_uint len)
6697 +{
6698 + lzo_uint key;
6699 +
6700 + s->node_count = s->n - len;
6701 + s->first_rp = node;
6702 +
6703 + while (len-- > 0)
6704 + {
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);
6709 + s->llen3[key]++;
6710 +
6711 + key = HEAD2 (s->b, node);
6712 + s->head2[key] = SWD_UINT (node);
6713 +
6714 + node++;
6715 + }
6716 +}
6717 +
6718 +static int
6719 +swd_init (lzo1x_999_swd_t * s, const lzo_byte * dict, lzo_uint dict_len)
6720 +{
6721 +
6722 + s->n = SWD_N;
6723 + s->f = SWD_F;
6724 + s->threshold = SWD_THRESHOLD;
6725 +
6726 +
6727 +
6728 + s->max_chain = SWD_MAX_CHAIN;
6729 + s->nice_length = SWD_F;
6730 + s->use_best_off = 0;
6731 + s->lazy_insert = 0;
6732 +
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;
6738 +
6739 + memset (s->llen3, 0, sizeof (s->llen3[0]) * SWD_HSIZE);
6740 + memset (s->head2, 0xff, sizeof (s->head2[0]) * 65536L);
6741 +
6742 + s->ip = 0;
6743 + swd_initdict (s, dict, dict_len);
6744 + s->bp = s->ip;
6745 + s->first_rp = s->ip;
6746 +
6747 + s->look = (lzo_uint) (s->c->in_end - s->c->ip);
6748 + if (s->look > 0)
6749 + {
6750 + if (s->look > s->f)
6751 + s->look = s->f;
6752 + memcpy (&s->b[s->ip], s->c->ip, s->look);
6753 + s->c->ip += s->look;
6754 + s->ip += s->look;
6755 + }
6756 +
6757 + if (s->ip == s->b_size)
6758 + s->ip = 0;
6759 +
6760 + if (s->look >= 2 && s->dict_len > 0)
6761 + swd_insertdict (s, 0, s->dict_len);
6762 +
6763 + s->rp = s->first_rp;
6764 + if (s->rp >= s->node_count)
6765 + s->rp -= s->node_count;
6766 + else
6767 + s->rp += s->b_size - s->node_count;
6768 +
6769 + return LZO_E_OK;
6770 +}
6771 +
6772 +static __inline__ void
6773 +swd_remove_node (lzo1x_999_swd_t * s, lzo_uint node)
6774 +{
6775 + if (s->node_count == 0)
6776 + {
6777 + lzo_uint key;
6778 +
6779 + key = HEAD3 (s->b, node);
6780 +
6781 + --s->llen3[key];
6782 +
6783 + key = HEAD2 (s->b, node);
6784 +
6785 + if ((lzo_uint) s->head2[key] == node)
6786 + s->head2[key] = NIL2;
6787 + }
6788 + else
6789 + --s->node_count;
6790 +}
6791 +
6792 +static void
6793 +swd_accept (lzo1x_999_swd_t * s, lzo_uint n)
6794 +{
6795 +
6796 + while (n--)
6797 + {
6798 + lzo_uint key;
6799 +
6800 + swd_remove_node (s, s->rp);
6801 +
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);
6806 + s->llen3[key]++;
6807 +
6808 + key = HEAD2 (s->b, s->bp);
6809 + s->head2[key] = SWD_UINT (s->bp);;
6810 +
6811 + swd_getbyte (s);
6812 + }
6813 +}
6814 +
6815 +static void
6816 +swd_search (lzo1x_999_swd_t * s, lzo_uint node, lzo_uint cnt)
6817 +{
6818 + const unsigned char *p1;
6819 + const unsigned char *p2;
6820 + const unsigned char *px;
6821 +
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;
6827 +
6828 + scan_end1 = bp[m_len - 1];
6829 + for (; cnt-- > 0; node = s->succ3[node])
6830 + {
6831 + p1 = bp;
6832 + p2 = b + node;
6833 + px = bx;
6834 +
6835 + if (p2[m_len - 1] == scan_end1 &&
6836 + p2[m_len] == p1[m_len] &&
6837 + p2[0] == p1[0] && p2[1] == p1[1])
6838 + {
6839 + lzo_uint i;
6840 +
6841 + p1 += 2;
6842 + p2 += 2;
6843 + do
6844 + {
6845 + }
6846 + while (++p1 < px && *p1 == *++p2);
6847 +
6848 + i = p1 - bp;
6849 +
6850 + if (i < SWD_BEST_OFF)
6851 + {
6852 + if (s->best_pos[i] == 0)
6853 + s->best_pos[i] = node + 1;
6854 + }
6855 +
6856 + if (i > m_len)
6857 + {
6858 + s->m_len = m_len = i;
6859 + s->m_pos = node;
6860 + if (m_len == s->look)
6861 + return;
6862 + if (m_len >= s->nice_length)
6863 + return;
6864 + if (m_len > (lzo_uint) s->best3[node])
6865 + return;
6866 + scan_end1 = bp[m_len - 1];
6867 + }
6868 + }
6869 + }
6870 +}
6871 +
6872 +static lzo_bool
6873 +swd_search2 (lzo1x_999_swd_t * s)
6874 +{
6875 + lzo_uint key;
6876 +
6877 + key = s->head2[HEAD2 (s->b, s->bp)];
6878 + if (key == NIL2)
6879 + return 0;
6880 +
6881 + if (s->best_pos[2] == 0)
6882 + s->best_pos[2] = key + 1;
6883 +
6884 + if (s->m_len < 2)
6885 + {
6886 + s->m_len = 2;
6887 + s->m_pos = key;
6888 + }
6889 + return 1;
6890 +}
6891 +
6892 +static void
6893 +swd_findbest (lzo1x_999_swd_t * s)
6894 +{
6895 + lzo_uint key;
6896 + lzo_uint cnt, node;
6897 + lzo_uint len;
6898 +
6899 + key = HEAD3 (s->b, s->bp);
6900 + node = s->succ3[s->bp] = s_head3 (s, key);
6901 + cnt = s->llen3[key]++;
6902 +
6903 + if (cnt > s->max_chain && s->max_chain > 0)
6904 + cnt = s->max_chain;
6905 + s->head3[key] = SWD_UINT (s->bp);
6906 +
6907 + s->b_char = s->b[s->bp];
6908 + len = s->m_len;
6909 + if (s->m_len >= s->look)
6910 + {
6911 + if (s->look == 0)
6912 + s->b_char = -1;
6913 + s->m_off = 0;
6914 + s->best3[s->bp] = SWD_UINT (s->f + 1);
6915 + }
6916 + else
6917 + {
6918 +
6919 + if (swd_search2 (s))
6920 +
6921 + if (s->look >= 3)
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);
6926 +
6927 + if (s->use_best_off)
6928 + {
6929 + int i;
6930 + for (i = 2; i < SWD_BEST_OFF; i++)
6931 + if (s->best_pos[i] > 0)
6932 + s->best_off[i] =
6933 + swd_pos2off (s,
6934 + s->best_pos[i] -
6935 + 1);
6936 + else
6937 + s->best_off[i] = 0;
6938 + }
6939 +
6940 + }
6941 +
6942 + swd_remove_node (s, s->rp);
6943 +
6944 + key = HEAD2 (s->b, s->bp);
6945 + s->head2[key] = SWD_UINT (s->bp);
6946 +
6947 +}
6948 +
6949 +/* lzo_mchw.ch */
6950 +
6951 +static int
6952 +init_match (lzo1x_999_t * c, lzo1x_999_swd_t * s,
6953 + const lzo_byte * dict, lzo_uint dict_len, lzo_uint32 flags)
6954 +{
6955 + int r;
6956 +
6957 + c->init = 1;
6958 +
6959 + s->c = c;
6960 +
6961 + c->last_m_len = c->last_m_off = 0;
6962 +
6963 + c->textsize = c->codesize = c->printcount = 0;
6964 + c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
6965 + c->lazy = 0;
6966 +
6967 + r = swd_init (s, dict, dict_len);
6968 + if (r != 0)
6969 + return r;
6970 +
6971 + s->use_best_off = (flags & 1) ? 1 : 0;
6972 + return r;
6973 +}
6974 +
6975 +static int
6976 +find_match (lzo1x_999_t * c, lzo1x_999_swd_t * s,
6977 + lzo_uint this_len, lzo_uint skip)
6978 +{
6979 + if (skip > 0)
6980 + {
6981 + swd_accept (s, this_len - skip);
6982 + c->textsize += this_len - skip + 1;
6983 + }
6984 + else
6985 + {
6986 + c->textsize += this_len - skip;
6987 + }
6988 +
6989 + s->m_len = 1;
6990 + s->m_len = 1;
6991 +
6992 + if (s->use_best_off)
6993 + memset (s->best_pos, 0, sizeof (s->best_pos));
6994 +
6995 + swd_findbest (s);
6996 + c->m_len = s->m_len;
6997 + c->m_off = s->m_off;
6998 +
6999 + swd_getbyte (s);
7000 +
7001 + if (s->b_char < 0)
7002 + {
7003 + c->look = 0;
7004 + c->m_len = 0;
7005 + }
7006 + else
7007 + {
7008 + c->look = s->look + 1;
7009 + }
7010 + c->bp = c->ip - c->look;
7011 +
7012 + if (c->cb && c->textsize > c->printcount)
7013 + {
7014 + (*c->cb) (c->textsize, c->codesize);
7015 + c->printcount += 1024;
7016 + }
7017 +
7018 + return LZO_E_OK;
7019 +}
7020 +
7021 +/* lzo1x_9x.c */
7022 +
7023 +static lzo_byte *
7024 +code_match (lzo1x_999_t * c, lzo_byte * op, lzo_uint m_len, lzo_uint m_off)
7025 +{
7026 + lzo_uint x_len = m_len;
7027 + lzo_uint x_off = m_off;
7028 +
7029 + c->match_bytes += m_len;
7030 +
7031 + if (m_len == 2)
7032 + {
7033 + m_off -= 1;
7034 +
7035 + *op++ = LZO_BYTE (M1_MARKER | ((m_off & 3) << 2));
7036 + *op++ = LZO_BYTE (m_off >> 2);
7037 +
7038 + c->m1a_m++;
7039 + }
7040 +
7041 + else if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
7042 +
7043 + {
7044 +
7045 + m_off -= 1;
7046 + *op++ = LZO_BYTE (((m_len - 1) << 5) | ((m_off & 7) << 2));
7047 + *op++ = LZO_BYTE (m_off >> 3);
7048 + c->m2_m++;
7049 + }
7050 + else if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET
7051 + && c->r1_lit >= 4)
7052 + {
7053 + m_off -= 1 + M2_MAX_OFFSET;
7054 +
7055 + *op++ = LZO_BYTE (M1_MARKER | ((m_off & 3) << 2));
7056 + *op++ = LZO_BYTE (m_off >> 2);
7057 +
7058 + c->m1b_m++;
7059 + }
7060 + else if (m_off <= M3_MAX_OFFSET)
7061 + {
7062 + m_off -= 1;
7063 + if (m_len <= M3_MAX_LEN)
7064 + *op++ = LZO_BYTE (M3_MARKER | (m_len - 2));
7065 + else
7066 + {
7067 + m_len -= M3_MAX_LEN;
7068 + *op++ = M3_MARKER | 0;
7069 + while (m_len > 255)
7070 + {
7071 + m_len -= 255;
7072 + *op++ = 0;
7073 + }
7074 + *op++ = LZO_BYTE (m_len);
7075 + }
7076 +
7077 + *op++ = LZO_BYTE (m_off << 2);
7078 + *op++ = LZO_BYTE (m_off >> 6);
7079 +
7080 + c->m3_m++;
7081 + }
7082 + else
7083 + {
7084 + lzo_uint k;
7085 +
7086 + m_off -= 0x4000;
7087 + k = (m_off & 0x4000) >> 11;
7088 + if (m_len <= M4_MAX_LEN)
7089 + *op++ = LZO_BYTE (M4_MARKER | k | (m_len - 2));
7090 + else
7091 + {
7092 + m_len -= M4_MAX_LEN;
7093 + *op++ = LZO_BYTE (M4_MARKER | k | 0);
7094 + while (m_len > 255)
7095 + {
7096 + m_len -= 255;
7097 + *op++ = 0;
7098 + }
7099 + *op++ = LZO_BYTE (m_len);
7100 + }
7101 +
7102 + *op++ = LZO_BYTE (m_off << 2);
7103 + *op++ = LZO_BYTE (m_off >> 6);
7104 +
7105 + c->m4_m++;
7106 + }
7107 +
7108 + c->last_m_len = x_len;
7109 + c->last_m_off = x_off;
7110 + return op;
7111 +}
7112 +
7113 +static lzo_byte *
7114 +STORE_RUN (lzo1x_999_t * c, lzo_byte * op, const lzo_byte * ii, lzo_uint t)
7115 +{
7116 + c->lit_bytes += t;
7117 +
7118 + if (op == c->out && t <= 238)
7119 + {
7120 + *op++ = LZO_BYTE (17 + t);
7121 + }
7122 + else if (t <= 3)
7123 + {
7124 + op[-2] |= LZO_BYTE (t);
7125 +
7126 + c->lit1_r++;
7127 + }
7128 + else if (t <= 18)
7129 + {
7130 + *op++ = LZO_BYTE (t - 3);
7131 + c->lit2_r++;
7132 + }
7133 + else
7134 + {
7135 + lzo_uint tt = t - 18;
7136 +
7137 + *op++ = 0;
7138 + while (tt > 255)
7139 + {
7140 + tt -= 255;
7141 + *op++ = 0;
7142 + }
7143 + *op++ = LZO_BYTE (tt);
7144 + c->lit3_r++;
7145 + }
7146 + do
7147 + *op++ = *ii++;
7148 + while (--t > 0);
7149 +
7150 + return op;
7151 +}
7152 +
7153 +static lzo_byte *
7154 +code_run (lzo1x_999_t * c, lzo_byte * op, const lzo_byte * ii,
7155 + lzo_uint lit, lzo_uint m_len)
7156 +{
7157 + if (lit > 0)
7158 + {
7159 + op = STORE_RUN (c, op, ii, lit);
7160 + c->r1_m_len = m_len;
7161 + c->r1_lit = lit;
7162 + }
7163 + else
7164 + {
7165 + c->r1_m_len = 0;
7166 + c->r1_lit = 0;
7167 + }
7168 +
7169 + return op;
7170 +}
7171 +
7172 +static int
7173 +len_of_coded_match (lzo_uint m_len, lzo_uint m_off, lzo_uint lit)
7174 +{
7175 + int n = 4;
7176 +
7177 + if (m_len < 2)
7178 + return -1;
7179 + if (m_len == 2)
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)
7183 + return 2;
7184 + if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4)
7185 + return 2;
7186 + if (m_off <= M3_MAX_OFFSET)
7187 + {
7188 + if (m_len <= M3_MAX_LEN)
7189 + return 3;
7190 + m_len -= M3_MAX_LEN;
7191 + while (m_len > 255)
7192 + {
7193 + m_len -= 255;
7194 + n++;
7195 + }
7196 + return n;
7197 + }
7198 + if (m_off <= M4_MAX_OFFSET)
7199 + {
7200 + if (m_len <= M4_MAX_LEN)
7201 + return 3;
7202 + m_len -= M4_MAX_LEN;
7203 + while (m_len > 255)
7204 + {
7205 + m_len -= 255;
7206 + n++;
7207 + }
7208 + return n;
7209 + }
7210 + return -1;
7211 +}
7212 +
7213 +static lzo_int
7214 +min_gain (lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, int l1, int l2,
7215 + int l3)
7216 +{
7217 + lzo_int lazy_match_min_gain = 0;
7218 +
7219 + lazy_match_min_gain += ahead;
7220 +
7221 + if (lit1 <= 3)
7222 + lazy_match_min_gain += (lit2 <= 3) ? 0 : 2;
7223 + else if (lit1 <= 18)
7224 + lazy_match_min_gain += (lit2 <= 18) ? 0 : 1;
7225 +
7226 + lazy_match_min_gain += (l2 - l1) * 2;
7227 + if (l3 > 0)
7228 + lazy_match_min_gain -= (ahead - l3) * 2;
7229 +
7230 + if (lazy_match_min_gain < 0)
7231 + lazy_match_min_gain = 0;
7232 +
7233 + return lazy_match_min_gain;
7234 +}
7235 +
7236 +static void
7237 +better_match (const lzo1x_999_swd_t * swd, lzo_uint * m_len, lzo_uint * m_off)
7238 +{
7239 + if (*m_len <= M2_MIN_LEN)
7240 + return;
7241 +
7242 + if (*m_off <= M2_MAX_OFFSET)
7243 + return;
7244 +
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)
7249 + {
7250 + *m_len = *m_len - 1;
7251 + *m_off = swd->best_off[*m_len];
7252 + return;
7253 + }
7254 +
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)
7259 + {
7260 + *m_len = *m_len - 2;
7261 + *m_off = swd->best_off[*m_len];
7262 + return;
7263 + }
7264 +
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)
7269 + {
7270 + *m_len = *m_len - 1;
7271 + *m_off = swd->best_off[*m_len];
7272 + }
7273 +
7274 +}
7275 +
7276 +/* minilzo.c */
7277 +
7278 +static lzo_bool
7279 +lzo_assert (int expr)
7280 +{
7281 + return (expr) ? 1 : 0;
7282 +}
7283 +
7284 +/* lzo1x_9x.c */
7285 +
7286 +static int
7287 +lzo1x_999_compress_internal (const lzo_byte * in, lzo_uint in_len,
7288 + lzo_byte * out, lzo_uintp out_len,
7289 + lzo_voidp wrkmem,
7290 + const lzo_byte * dict, lzo_uint dict_len,
7291 + lzo_progress_callback_t cb,
7292 + int try_lazy,
7293 + lzo_uint good_length,
7294 + lzo_uint max_lazy,
7295 + lzo_uint nice_length,
7296 + lzo_uint max_chain, lzo_uint32 flags)
7297 +{
7298 + lzo_byte *op;
7299 + const lzo_byte *ii;
7300 + lzo_uint lit;
7301 + lzo_uint m_len, m_off;
7302 + lzo1x_999_t cc;
7303 + lzo1x_999_t *const c = &cc;
7304 + lzo1x_999_swd_t *const swd = (lzo1x_999_swd_t *) wrkmem;
7305 + int r;
7306 +
7307 + if (!lzo_assert
7308 + (LZO1X_999_MEM_COMPRESS >= lzo_sizeof (lzo1x_999_swd_t)))
7309 + return LZO_E_ERROR;
7310 +
7311 + if (try_lazy < 0)
7312 + try_lazy = 1;
7313 +
7314 + if (good_length <= 0)
7315 + good_length = 32;
7316 +
7317 + if (max_lazy <= 0)
7318 + max_lazy = 32;
7319 +
7320 + if (nice_length <= 0)
7321 + nice_length = 0;
7322 +
7323 + if (max_chain <= 0)
7324 + max_chain = SWD_MAX_CHAIN;
7325 +
7326 + c->init = 0;
7327 + c->ip = c->in = in;
7328 + c->in_end = in + in_len;
7329 + c->out = out;
7330 + c->cb = cb;
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;
7333 +
7334 + op = out;
7335 + ii = c->ip;
7336 + lit = 0;
7337 + c->r1_lit = c->r1_m_len = 0;
7338 +
7339 + r = init_match (c, swd, dict, dict_len, flags);
7340 + if (r != 0)
7341 + return r;
7342 + if (max_chain > 0)
7343 + swd->max_chain = max_chain;
7344 + if (nice_length > 0)
7345 + swd->nice_length = nice_length;
7346 +
7347 + r = find_match (c, swd, 0, 0);
7348 + if (r != 0)
7349 + return r;
7350 + while (c->look > 0)
7351 + {
7352 + lzo_uint ahead;
7353 + lzo_uint max_ahead;
7354 + int l1, l2, l3;
7355 +
7356 + c->codesize = op - out;
7357 +
7358 + m_len = c->m_len;
7359 + m_off = c->m_off;
7360 +
7361 + if (lit == 0)
7362 + ii = c->bp;
7363 +
7364 + if (m_len < 2 ||
7365 + (m_len == 2
7366 + && (m_off > M1_MAX_OFFSET || lit == 0 || lit >= 4))
7367 + || (m_len == 2 && op == out) || (op == out && lit == 0))
7368 + {
7369 +
7370 + m_len = 0;
7371 + }
7372 + else if (m_len == M2_MIN_LEN)
7373 + {
7374 +
7375 + if (m_off > MX_MAX_OFFSET && lit >= 4)
7376 + m_len = 0;
7377 + }
7378 +
7379 + if (m_len == 0)
7380 + {
7381 +
7382 + lit++;
7383 + swd->max_chain = max_chain;
7384 + r = find_match (c, swd, 1, 0);
7385 + continue;
7386 + }
7387 +
7388 + if (swd->use_best_off)
7389 + better_match (swd, &m_len, &m_off);
7390 +
7391 + ahead = 0;
7392 + if (try_lazy <= 0 || m_len >= max_lazy)
7393 + {
7394 +
7395 + l1 = 0;
7396 + max_ahead = 0;
7397 + }
7398 + else
7399 + {
7400 +
7401 + l1 = len_of_coded_match (m_len, m_off, lit);
7402 +
7403 + max_ahead = LZO_MIN (try_lazy, l1 - 1);
7404 +
7405 + }
7406 +
7407 + while (ahead < max_ahead && c->look > m_len)
7408 + {
7409 + lzo_int lazy_match_min_gain;
7410 +
7411 + if (m_len >= good_length)
7412 + swd->max_chain = max_chain >> 2;
7413 + else
7414 + swd->max_chain = max_chain;
7415 + r = find_match (c, swd, 1, 0);
7416 + ahead++;
7417 +
7418 + if (c->m_len < m_len)
7419 + continue;
7420 +
7421 + if (c->m_len == m_len && c->m_off >= m_off)
7422 + continue;
7423 +
7424 + if (swd->use_best_off)
7425 + better_match (swd, &c->m_len, &c->m_off);
7426 +
7427 + l2 = len_of_coded_match (c->m_len, c->m_off,
7428 + lit + ahead);
7429 + if (l2 < 0)
7430 + continue;
7431 +
7432 + l3 = (op == out) ? -1 : len_of_coded_match (ahead,
7433 + m_off,
7434 + lit);
7435 +
7436 + lazy_match_min_gain =
7437 + min_gain (ahead, lit, lit + ahead, l1, l2,
7438 + l3);
7439 + if (c->m_len >= m_len + lazy_match_min_gain)
7440 + {
7441 + c->lazy++;
7442 +
7443 + if (l3 > 0)
7444 + {
7445 +
7446 + op = code_run (c, op, ii, lit, ahead);
7447 + lit = 0;
7448 +
7449 + op = code_match (c, op, ahead, m_off);
7450 + }
7451 + else
7452 + {
7453 + lit += ahead;
7454 + }
7455 + goto lazy_match_done;
7456 + }
7457 + }
7458 +
7459 + op = code_run (c, op, ii, lit, m_len);
7460 + lit = 0;
7461 +
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);
7465 +
7466 + lazy_match_done:;
7467 + }
7468 +
7469 + if (lit > 0)
7470 + op = STORE_RUN (c, op, ii, lit);
7471 +
7472 + *op++ = M4_MARKER | 1;
7473 + *op++ = 0;
7474 + *op++ = 0;
7475 +
7476 + c->codesize = op - out;
7477 +
7478 + *out_len = op - out;
7479 +
7480 + if (c->cb)
7481 + (*c->cb) (c->textsize, c->codesize);
7482 +
7483 + return LZO_E_OK;
7484 +}
7485 +
7486 +static int
7487 +lzo1x_999_compress_level (const lzo_byte * in, lzo_uint in_len,
7488 + lzo_byte * out, lzo_uintp out_len,
7489 + lzo_voidp wrkmem,
7490 + const lzo_byte * dict, lzo_uint dict_len,
7491 + lzo_progress_callback_t cb, int compression_level)
7492 +{
7493 + static const struct
7494 + {
7495 + int try_lazy;
7496 + lzo_uint good_length;
7497 + lzo_uint max_lazy;
7498 + lzo_uint nice_length;
7499 + lzo_uint max_chain;
7500 + lzo_uint32 flags;
7501 + } c[9] =
7502 + {
7503 + {
7504 + 0, 0, 0, 8, 4, 0},
7505 + {
7506 + 0, 0, 0, 16, 8, 0},
7507 + {
7508 + 0, 0, 0, 32, 16, 0},
7509 + {
7510 + 1, 4, 4, 16, 16, 0},
7511 + {
7512 + 1, 8, 16, 32, 32, 0},
7513 + {
7514 + 1, 8, 16, 128, 128, 0},
7515 + {
7516 + 2, 8, 32, 128, 256, 0},
7517 + {
7518 + 2, 32, 128, F, 2048, 1},
7519 + {
7520 + 2, F, F, F, 4096, 1}
7521 + };
7522 +
7523 + if (compression_level < 1 || compression_level > 9)
7524 + return LZO_E_ERROR;
7525 +
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,
7532 + 0,
7533 + c[compression_level].max_chain,
7534 + c[compression_level].flags);
7535 +}
7536 +
7537 +static int
7538 +lzo1x_999_compress (const lzo_byte * in, lzo_uint in_len,
7539 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7540 +{
7541 + return lzo1x_999_compress_level (in, in_len, out, out_len, wrkmem,
7542 + NULL, 0, 0, 8);
7543 +}
7544 +
7545 +/* minilzo.c */
7546 +
7547 +static const lzo_byte __lzo_copyright[] = LZO_VERSION_STRING;
7548 +
7549 +static lzo_uint
7550 +_lzo1x_1_do_compress (const lzo_byte * in, lzo_uint in_len,
7551 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7552 +{
7553 +
7554 + register const lzo_byte *ip;
7555 +
7556 + lzo_byte *op;
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;
7561 +
7562 + op = out;
7563 + ip = in;
7564 + ii = ip;
7565 +
7566 + ip += 4;
7567 + for (;;)
7568 + {
7569 + register const lzo_byte *m_pos;
7570 +
7571 + lzo_uint m_off;
7572 + lzo_uint m_len;
7573 + lzo_uint dindex;
7574 +
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))
7579 + goto literal;
7580 +
7581 + if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
7582 + goto try_match;
7583 + DINDEX2 (dindex, ip);
7584 + GINDEX (m_pos, m_off, dict, dindex, in);
7585 +
7586 + if (LZO_CHECK_MPOS_NON_DET
7587 + (m_pos, m_off, in, ip, M4_MAX_OFFSET))
7588 + goto literal;
7589 + if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
7590 + goto try_match;
7591 + goto literal;
7592 +
7593 + try_match:
7594 + if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
7595 + {
7596 + }
7597 + else
7598 + {
7599 + if (m_pos[2] == ip[2])
7600 + {
7601 + goto match;
7602 + }
7603 + else
7604 + {
7605 + }
7606 + }
7607 +
7608 + literal:
7609 + UPDATE_I (dict, 0, dindex, ip, in);
7610 + ++ip;
7611 + if (ip >= ip_end)
7612 + break;
7613 + continue;
7614 +
7615 + match:
7616 + UPDATE_I (dict, 0, dindex, ip, in);
7617 +
7618 + if (pd (ip, ii) > 0)
7619 + {
7620 + register lzo_uint t = pd (ip, ii);
7621 +
7622 + if (t <= 3)
7623 + {
7624 + op[-2] |= LZO_BYTE (t);
7625 + }
7626 + else if (t <= 18)
7627 + *op++ = LZO_BYTE (t - 3);
7628 + else
7629 + {
7630 + register lzo_uint tt = t - 18;
7631 +
7632 + *op++ = 0;
7633 + while (tt > 255)
7634 + {
7635 + tt -= 255;
7636 + *op++ = 0;
7637 + }
7638 + *op++ = LZO_BYTE (tt);;
7639 + }
7640 + do
7641 + *op++ = *ii++;
7642 + while (--t > 0);
7643 + }
7644 +
7645 + ip += 3;
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++)
7649 + {
7650 + --ip;
7651 + m_len = ip - ii;
7652 +
7653 + if (m_off <= M2_MAX_OFFSET)
7654 + {
7655 + m_off -= 1;
7656 +
7657 + *op++ = LZO_BYTE (((m_len -
7658 + 1) << 5) | ((m_off & 7) <<
7659 + 2));
7660 + *op++ = LZO_BYTE (m_off >> 3);
7661 + }
7662 + else if (m_off <= M3_MAX_OFFSET)
7663 + {
7664 + m_off -= 1;
7665 + *op++ = LZO_BYTE (M3_MARKER | (m_len - 2));
7666 + goto m3_m4_offset;
7667 + }
7668 + else
7669 +
7670 + {
7671 + m_off -= 0x4000;
7672 +
7673 + *op++ = LZO_BYTE (M4_MARKER |
7674 + ((m_off & 0x4000) >> 11) |
7675 + (m_len - 2));
7676 + goto m3_m4_offset;
7677 + }
7678 + }
7679 + else
7680 + {
7681 + {
7682 + const lzo_byte *end = in_end;
7683 + const lzo_byte *m = m_pos + M2_MAX_LEN + 1;
7684 + while (ip < end && *m == *ip)
7685 + m++, ip++;
7686 + m_len = (ip - ii);
7687 + }
7688 +
7689 +
7690 + if (m_off <= M3_MAX_OFFSET)
7691 + {
7692 + m_off -= 1;
7693 + if (m_len <= 33)
7694 + *op++ = LZO_BYTE (M3_MARKER |
7695 + (m_len - 2));
7696 + else
7697 + {
7698 + m_len -= 33;
7699 + *op++ = M3_MARKER | 0;
7700 + goto m3_m4_len;
7701 + }
7702 + }
7703 + else
7704 + {
7705 + m_off -= 0x4000;
7706 +
7707 + if (m_len <= M4_MAX_LEN)
7708 + *op++ = LZO_BYTE (M4_MARKER |
7709 + ((m_off & 0x4000) >>
7710 + 11) | (m_len - 2));
7711 +
7712 + else
7713 + {
7714 + m_len -= M4_MAX_LEN;
7715 + *op++ = LZO_BYTE (M4_MARKER |
7716 + ((m_off & 0x4000) >>
7717 + 11));
7718 + m3_m4_len:
7719 + while (m_len > 255)
7720 + {
7721 + m_len -= 255;
7722 + *op++ = 0;
7723 + }
7724 +
7725 + *op++ = LZO_BYTE (m_len);
7726 + }
7727 + }
7728 +
7729 + m3_m4_offset:
7730 + *op++ = LZO_BYTE ((m_off & 63) << 2);
7731 + *op++ = LZO_BYTE (m_off >> 6);
7732 + }
7733 + ii = ip;
7734 + if (ip >= ip_end)
7735 + break;
7736 + }
7737 +
7738 + *out_len = op - out;
7739 + return pd (in_end, ii);
7740 +}
7741 +
7742 +static int
7743 +lzo1x_1_compress (const lzo_byte * in, lzo_uint in_len,
7744 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7745 +{
7746 + lzo_byte *op = out;
7747 + lzo_uint t;
7748 +
7749 + if (in_len <= M2_MAX_LEN + 5)
7750 + t = in_len;
7751 + else
7752 + {
7753 + t = _lzo1x_1_do_compress (in, in_len, op, out_len, wrkmem);
7754 + op += *out_len;
7755 + }
7756 +
7757 + if (t > 0)
7758 + {
7759 + const lzo_byte *ii = in + in_len - t;
7760 +
7761 + if (op == out && t <= 238)
7762 + *op++ = LZO_BYTE (17 + t);
7763 + else if (t <= 3)
7764 + op[-2] |= LZO_BYTE (t);
7765 + else if (t <= 18)
7766 + *op++ = LZO_BYTE (t - 3);
7767 + else
7768 + {
7769 + lzo_uint tt = t - 18;
7770 +
7771 + *op++ = 0;
7772 + while (tt > 255)
7773 + {
7774 + tt -= 255;
7775 + *op++ = 0;
7776 + }
7777 +
7778 + *op++ = LZO_BYTE (tt);
7779 + }
7780 + do
7781 + *op++ = *ii++;
7782 + while (--t > 0);
7783 + }
7784 +
7785 + *op++ = M4_MARKER | 1;
7786 + *op++ = 0;
7787 + *op++ = 0;
7788 +
7789 + *out_len = op - out;
7790 + return 0;
7791 +}
7792 +
7793 +static int
7794 +lzo1x_decompress (const lzo_byte * in, lzo_uint in_len,
7795 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7796 +{
7797 + register lzo_byte *op;
7798 + register const lzo_byte *ip;
7799 + register lzo_uint t;
7800 +
7801 + register const lzo_byte *m_pos;
7802 +
7803 + const lzo_byte *const ip_end = in + in_len;
7804 + lzo_byte *const op_end = out + *out_len;
7805 +
7806 + *out_len = 0;
7807 +
7808 + op = out;
7809 + ip = in;
7810 +
7811 + if (*ip > 17)
7812 + {
7813 + t = *ip++ - 17;
7814 + if (t < 4)
7815 + goto match_next;
7816 + NEED_OP (t);
7817 + NEED_IP (t + 1);
7818 + do
7819 + *op++ = *ip++;
7820 + while (--t > 0);
7821 + goto first_literal_run;
7822 + }
7823 +
7824 + while (TEST_IP && TEST_OP)
7825 + {
7826 + t = *ip++;
7827 + if (t >= 16)
7828 + goto match;
7829 + if (t == 0)
7830 + {
7831 + NEED_IP (1);
7832 + while (*ip == 0)
7833 + {
7834 + t += 255;
7835 + ip++;
7836 + NEED_IP (1);
7837 + }
7838 + t += 15 + *ip++;
7839 + }
7840 + NEED_OP (t + 3);
7841 + NEED_IP (t + 4);
7842 + if (PTR_ALIGNED2_4 (op, ip))
7843 + {
7844 + COPY4 (op, ip);
7845 +
7846 + op += 4;
7847 + ip += 4;
7848 + if (--t > 0)
7849 + {
7850 + if (t >= 4)
7851 + {
7852 + do
7853 + {
7854 + COPY4 (op, ip);
7855 + op += 4;
7856 + ip += 4;
7857 + t -= 4;
7858 + }
7859 + while (t >= 4);
7860 + if (t > 0)
7861 + do
7862 + *op++ = *ip++;
7863 + while (--t > 0);
7864 + }
7865 + else
7866 + do
7867 + *op++ = *ip++;
7868 + while (--t > 0);
7869 + }
7870 + }
7871 + else
7872 + {
7873 + *op++ = *ip++;
7874 + *op++ = *ip++;
7875 + *op++ = *ip++;
7876 + do
7877 + *op++ = *ip++;
7878 + while (--t > 0);
7879 + }
7880 + first_literal_run:
7881 +
7882 + t = *ip++;
7883 + if (t >= 16)
7884 + goto match;
7885 +
7886 + m_pos = op - (1 + M2_MAX_OFFSET);
7887 + m_pos -= t >> 2;
7888 + m_pos -= *ip++ << 2;
7889 + TEST_LOOKBEHIND (m_pos, out);
7890 + NEED_OP (3);
7891 + *op++ = *m_pos++;
7892 + *op++ = *m_pos++;
7893 + *op++ = *m_pos;
7894 +
7895 + goto match_done;
7896 +
7897 + while (TEST_IP && TEST_OP)
7898 + {
7899 + match:
7900 + if (t >= 64)
7901 + {
7902 + m_pos = op - 1;
7903 + m_pos -= (t >> 2) & 7;
7904 + m_pos -= *ip++ << 3;
7905 + t = (t >> 5) - 1;
7906 + TEST_LOOKBEHIND (m_pos, out);
7907 + NEED_OP (t + 3 - 1);
7908 + goto copy_match;
7909 +
7910 + }
7911 + else if (t >= 32)
7912 + {
7913 + t &= 31;
7914 + if (t == 0)
7915 + {
7916 + NEED_IP (1);
7917 + while (*ip == 0)
7918 + {
7919 + t += 255;
7920 + ip++;
7921 + NEED_IP (1);
7922 + }
7923 + t += 31 + *ip++;
7924 + }
7925 +
7926 + m_pos = op - 1;
7927 + m_pos -= (ip[0] >> 2) + (ip[1] << 6);
7928 +
7929 + ip += 2;
7930 + }
7931 + else if (t >= 16)
7932 + {
7933 + m_pos = op;
7934 + m_pos -= (t & 8) << 11;
7935 +
7936 + t &= 7;
7937 + if (t == 0)
7938 + {
7939 + NEED_IP (1);
7940 + while (*ip == 0)
7941 + {
7942 + t += 255;
7943 + ip++;
7944 + NEED_IP (1);
7945 + }
7946 + t += 7 + *ip++;
7947 + }
7948 +
7949 + m_pos -= (ip[0] >> 2) + (ip[1] << 6);
7950 +
7951 + ip += 2;
7952 + if (m_pos == op)
7953 + goto eof_found;
7954 + m_pos -= 0x4000;
7955 + }
7956 + else
7957 + {
7958 +
7959 + m_pos = op - 1;
7960 + m_pos -= t >> 2;
7961 + m_pos -= *ip++ << 2;
7962 + TEST_LOOKBEHIND (m_pos, out);
7963 + NEED_OP (2);
7964 + *op++ = *m_pos++;
7965 + *op++ = *m_pos;
7966 +
7967 + goto match_done;
7968 + }
7969 +
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))
7974 + {
7975 + COPY4 (op, m_pos);
7976 + op += 4;
7977 + m_pos += 4;
7978 + t -= 4 - (3 - 1);
7979 + do
7980 + {
7981 + COPY4 (op, m_pos);
7982 + op += 4;
7983 + m_pos += 4;
7984 + t -= 4;
7985 + }
7986 + while (t >= 4);
7987 + if (t > 0)
7988 + do
7989 + *op++ = *m_pos++;
7990 + while (--t > 0);
7991 + }
7992 + else
7993 +
7994 + {
7995 + copy_match:
7996 + *op++ = *m_pos++;
7997 + *op++ = *m_pos++;
7998 + do
7999 + *op++ = *m_pos++;
8000 + while (--t > 0);
8001 + }
8002 +
8003 + match_done:
8004 + t = ip[-2] & 3;
8005 +
8006 + if (t == 0)
8007 + break;
8008 +
8009 + match_next:
8010 + NEED_OP (t);
8011 + NEED_IP (t + 1);
8012 + do
8013 + *op++ = *ip++;
8014 + while (--t > 0);
8015 + t = *ip++;
8016 + }
8017 + }
8018 + *out_len = op - out;
8019 + return LZO_E_EOF_NOT_FOUND;
8020 +
8021 + eof_found:
8022 + *out_len = op - out;
8023 + return (ip == ip_end ? LZO_E_OK :
8024 + (ip <
8025 + ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
8026 +
8027 + input_overrun:
8028 + *out_len = op - out;
8029 + return LZO_E_INPUT_OVERRUN;
8030 +
8031 + output_overrun:
8032 + *out_len = op - out;
8033 + return LZO_E_OUTPUT_OVERRUN;
8034 +
8035 + lookbehind_overrun:
8036 + *out_len = op - out;
8037 + return LZO_E_LOOKBEHIND_OVERRUN;
8038 +}
8039 +
8040 +/* lzo1x_oo.ch */
8041 +
8042 +#define NO_LIT LZO_UINT_MAX
8043 +
8044 +static void
8045 +copy2 (lzo_byte * ip, const lzo_byte * m_pos, lzo_ptrdiff_t off)
8046 +{
8047 + ip[0] = m_pos[0];
8048 + if (off == 1)
8049 + ip[1] = m_pos[0];
8050 + else
8051 + ip[1] = m_pos[1];
8052 +}
8053 +
8054 +static void
8055 +copy3 (lzo_byte * ip, const lzo_byte * m_pos, lzo_ptrdiff_t off)
8056 +{
8057 + ip[0] = m_pos[0];
8058 + if (off == 1)
8059 + {
8060 + ip[2] = ip[1] = m_pos[0];
8061 + }
8062 + else if (off == 2)
8063 + {
8064 + ip[1] = m_pos[1];
8065 + ip[2] = m_pos[0];
8066 + }
8067 + else
8068 + {
8069 + ip[1] = m_pos[1];
8070 + ip[2] = m_pos[2];
8071 + }
8072 +}
8073 +
8074 +static int
8075 +lzo1x_optimize (lzo_byte * in, lzo_uint in_len,
8076 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
8077 +{
8078 + register lzo_byte *op;
8079 + register lzo_byte *ip;
8080 + register lzo_uint t;
8081 + register lzo_byte *m_pos;
8082 + lzo_uint nl;
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;
8086 + lzo_uint lit = 0;
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;
8089 +
8090 + *out_len = 0;
8091 +
8092 + op = out;
8093 + ip = in;
8094 +
8095 + if (*ip > 17)
8096 + {
8097 + t = *ip++ - 17;
8098 + if (t < 4)
8099 + goto match_next;
8100 + goto first_literal_run;
8101 + }
8102 +
8103 + while (TEST_IP && TEST_OP)
8104 + {
8105 + t = *ip++;
8106 + if (t >= 16)
8107 + goto match;
8108 + litp = ip - 1;
8109 + if (t == 0)
8110 + {
8111 + t = 15;
8112 + while (*ip == 0)
8113 + t += 255, ip++;
8114 + t += *ip++;
8115 + }
8116 + lit = t + 3;
8117 + copy_literal_run:
8118 + *op++ = *ip++;
8119 + *op++ = *ip++;
8120 + *op++ = *ip++;
8121 + first_literal_run:
8122 + do
8123 + *op++ = *ip++;
8124 + while (--t > 0);
8125 +
8126 + t = *ip++;
8127 +
8128 + if (t >= 16)
8129 + goto match;
8130 + m_pos = op - 1 - 0x800;
8131 + m_pos -= t >> 2;
8132 + m_pos -= *ip++ << 2;
8133 + *op++ = *m_pos++;
8134 + *op++ = *m_pos++;
8135 + *op++ = *m_pos++;
8136 + lit = 0;
8137 + goto match_done;
8138 +
8139 + while (TEST_IP && TEST_OP)
8140 + {
8141 + if (t < 16)
8142 + {
8143 + m_pos = op - 1;
8144 + m_pos -= t >> 2;
8145 + m_pos -= *ip++ << 2;
8146 +
8147 + if (litp == NULL)
8148 + goto copy_m1;
8149 +
8150 + nl = ip[-2] & 3;
8151 + if (nl == 0 && lit == 1 && ip[0] >= 16)
8152 + {
8153 + next_lit = nl;
8154 + lit += 2;
8155 + *litp = LZO_BYTE ((*litp & ~3) | lit);
8156 + copy2 (ip - 2, m_pos, op - m_pos);
8157 + o_m1_a++;
8158 + }
8159 + else if (nl == 0 && ip[0] < 16 && ip[0] != 0
8160 + && (lit + 2 + ip[0] < 16))
8161 + {
8162 + t = *ip++;
8163 + *litp &= ~3;
8164 + copy2 (ip - 3 + 1, m_pos, op - m_pos);
8165 + litp += 2;
8166 + if (lit > 0)
8167 + memmove (litp + 1, litp, lit);
8168 + lit += 2 + t + 3;
8169 + *litp = LZO_BYTE (lit - 3);
8170 +
8171 + o_m1_b++;
8172 + *op++ = *m_pos++;
8173 + *op++ = *m_pos++;
8174 + goto copy_literal_run;
8175 + }
8176 + copy_m1:
8177 + *op++ = *m_pos++;
8178 + *op++ = *m_pos++;
8179 + }
8180 + else
8181 + {
8182 + match:
8183 + if (t >= 64)
8184 + {
8185 + m_pos = op - 1;
8186 + m_pos -= (t >> 2) & 7;
8187 + m_pos -= *ip++ << 3;
8188 + t = (t >> 5) - 1;
8189 + if (litp == NULL)
8190 + goto copy_m;
8191 +
8192 + nl = ip[-2] & 3;
8193 + if (t == 1 && lit > 3 && nl == 0 &&
8194 + ip[0] < 16 && ip[0] != 0
8195 + && (lit + 3 + ip[0] < 16))
8196 + {
8197 + t = *ip++;
8198 + copy3 (ip - 1 - 2, m_pos,
8199 + op - m_pos);
8200 + lit += 3 + t + 3;
8201 + *litp = LZO_BYTE (lit - 3);
8202 + o_m2++;
8203 + *op++ = *m_pos++;
8204 + *op++ = *m_pos++;
8205 + *op++ = *m_pos++;
8206 + goto copy_literal_run;
8207 + }
8208 + }
8209 + else
8210 + {
8211 + if (t >= 32)
8212 + {
8213 + t &= 31;
8214 + if (t == 0)
8215 + {
8216 + t = 31;
8217 + while (*ip == 0)
8218 + t += 255,
8219 + ip++;
8220 + t += *ip++;
8221 + }
8222 + m_pos = op - 1;
8223 + m_pos -= *ip++ >> 2;
8224 + m_pos -= *ip++ << 6;
8225 + }
8226 + else
8227 + {
8228 + m_pos = op;
8229 + m_pos -= (t & 8) << 11;
8230 + t &= 7;
8231 + if (t == 0)
8232 + {
8233 + t = 7;
8234 + while (*ip == 0)
8235 + t += 255,
8236 + ip++;
8237 + t += *ip++;
8238 + }
8239 + m_pos -= *ip++ >> 2;
8240 + m_pos -= *ip++ << 6;
8241 + if (m_pos == op)
8242 + goto eof_found;
8243 + m_pos -= 0x4000;
8244 + }
8245 + if (litp == NULL)
8246 + goto copy_m;
8247 +
8248 + nl = ip[-2] & 3;
8249 + if (t == 1 && lit == 0 && nl == 0
8250 + && ip[0] >= 16)
8251 + {
8252 + next_lit = nl;
8253 + lit += 3;
8254 + *litp = LZO_BYTE ((*litp & ~3)
8255 + | lit);
8256 + copy3 (ip - 3, m_pos,
8257 + op - m_pos);
8258 + o_m3_a++;
8259 + }
8260 + else if (t == 1 && lit <= 3 && nl == 0
8261 + && ip[0] < 16 && ip[0] != 0
8262 + && (lit + 3 + ip[0] < 16))
8263 + {
8264 + t = *ip++;
8265 + *litp &= ~3;
8266 + copy3 (ip - 4 + 1, m_pos,
8267 + op - m_pos);
8268 + litp += 2;
8269 + if (lit > 0)
8270 + memmove (litp + 1,
8271 + litp, lit);
8272 + lit += 3 + t + 3;
8273 + *litp = LZO_BYTE (lit - 3);
8274 +
8275 + o_m3_b++;
8276 + *op++ = *m_pos++;
8277 + *op++ = *m_pos++;
8278 + *op++ = *m_pos++;
8279 + goto copy_literal_run;
8280 + }
8281 + }
8282 + copy_m:
8283 + *op++ = *m_pos++;
8284 + *op++ = *m_pos++;
8285 + do
8286 + *op++ = *m_pos++;
8287 + while (--t > 0);
8288 + }
8289 +
8290 + match_done:
8291 + if (next_lit == NO_LIT)
8292 + {
8293 + t = ip[-2] & 3;
8294 + lit = t;
8295 + litp = ip - 2;
8296 + }
8297 + else
8298 + t = next_lit;
8299 + next_lit = NO_LIT;
8300 + if (t == 0)
8301 + break;
8302 + match_next:
8303 + do
8304 + *op++ = *ip++;
8305 + while (--t > 0);
8306 + t = *ip++;
8307 + }
8308 + }
8309 +
8310 + *out_len = op - out;
8311 + return LZO_E_EOF_NOT_FOUND;
8312 +
8313 + eof_found:
8314 + *out_len = op - out;
8315 + return (ip == ip_end ? LZO_E_OK :
8316 + (ip <
8317 + ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
8318 +}
8319 +
8320 +/* interface to jffs2 bbc follows */
8321 +
8322 +#include "jffs2_bbc_framework.h"
8323 +
8324 +#define BLOCKSIZE 4096
8325 +#define OUTBLOCKSIZE (BLOCKSIZE + BLOCKSIZE / 64 + 16 + 3)
8326 +
8327 +#define JFFS2_BBC_LZO_BLOCK_SIGN {0x3f, 0x47, 0x5a, 0x18}
8328 +
8329 +static int
8330 +jffs2_bbc_lzo_compressor_init (void);
8331 +
8332 +static void
8333 +jffs2_bbc_lzo_compressor_deinit (void);
8334 +
8335 +static int
8336 +jffs2_bbc_lzo_compress (void *model, unsigned char *input,
8337 + unsigned char *output, unsigned long *sourcelen,
8338 + unsigned long *dstlen);
8339 +
8340 +static int
8341 +jffs2_bbc_lzo_estimate (void *model, unsigned char *input,
8342 + unsigned long sourcelen, unsigned long *dstlen,
8343 + unsigned long *readtime, unsigned long *writetime);
8344 +
8345 +static int
8346 +jffs2_bbc_lzo_decompress (void *model, unsigned char *input,
8347 + unsigned char *output, unsigned long sourcelen,
8348 + unsigned long dstlen);
8349 +
8350 +static char *
8351 +jffs2_bbc_lzo_proc_info (void);
8352 +
8353 +static int
8354 +jffs2_bbc_lzo_proc_command (char *command);
8355 +
8356 +struct jffs2_bbc_compressor_type jffs2_bbc_lzo = {
8357 + "lzo",
8358 + 0,
8359 + JFFS2_BBC_LZO_BLOCK_SIGN,
8360 + jffs2_bbc_lzo_compressor_init,
8361 + NULL,
8362 + NULL,
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
8369 +};
8370 +
8371 +static int
8372 +no_lzo1x_optimize (lzo_byte * src, lzo_uint src_len,
8373 + lzo_byte * dst, lzo_uintp dst_len, lzo_voidp wrkmem)
8374 +{
8375 + return 0;
8376 +}
8377 +
8378 +#ifdef __KERNEL__
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;
8384 +#else
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;
8390 +#endif
8391 +
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 */
8394 +
8395 +static int
8396 +jffs2_bbc_lzo_compressor_init (void)
8397 +{
8398 + wrkmem = (lzo_bytep) jffs2_bbc_malloc (lzo1x_compressor_memsize);
8399 + cmprssmem = (lzo_bytep) jffs2_bbc_malloc (OUTBLOCKSIZE);
8400 + return !(wrkmem && cmprssmem);
8401 +}
8402 +
8403 +static void
8404 +jffs2_bbc_lzo_compressor_deinit (void)
8405 +{
8406 + jffs2_bbc_free (wrkmem);
8407 + jffs2_bbc_free (cmprssmem);
8408 +}
8409 +
8410 +static int
8411 +jffs2_bbc_lzo_compress (void *model, unsigned char *input,
8412 + unsigned char *output, unsigned long *sourcelen,
8413 + unsigned long *dstlen)
8414 +{
8415 + lzo_uint csize = OUTBLOCKSIZE;
8416 + lzo_uint isize = *sourcelen;
8417 + int retval;
8418 + if ((retval =
8419 + lzo1x_compressor (input, *sourcelen, cmprssmem, &csize,
8420 + wrkmem)) != LZO_E_OK)
8421 + {
8422 + *sourcelen = *dstlen = 0;
8423 + return retval;
8424 + }
8425 + else
8426 + {
8427 + retval = lzo1x_optimizer (cmprssmem, csize, input, &isize,
8428 + NULL);
8429 + csize += 2;
8430 + if (csize <= *dstlen) {
8431 + *dstlen = csize;
8432 + *(output++) = jffs2_bbc_lzo.block_sign[0];
8433 + *(output++) = jffs2_bbc_lzo.block_sign[1];
8434 + memcpy (output, cmprssmem, csize - 2);
8435 + return retval;
8436 + } else {
8437 + *sourcelen = *dstlen = 0;
8438 + return -1;
8439 + }
8440 + }
8441 +}
8442 +
8443 +static int
8444 +jffs2_bbc_lzo_estimate (void *model, unsigned char *input,
8445 + unsigned long sourcelen, unsigned long *dstlen,
8446 + unsigned long *readtime, unsigned long *writetime)
8447 +{
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 */
8453 + return 0;
8454 +}
8455 +
8456 +static int
8457 +jffs2_bbc_lzo_decompress (void *model, unsigned char *input,
8458 + unsigned char *output, unsigned long sourcelen,
8459 + unsigned long dstlen)
8460 +{
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] )
8464 + ) {
8465 + return -1;
8466 + } else {
8467 + return lzo1x_decompress (input, sourcelen - 2, output, &outlen, NULL);
8468 + }
8469 +}
8470 +
8471 +static char *
8472 +jffs2_bbc_lzo_proc_info (void)
8473 +{
8474 + if (lzo1x_compressor_type == 1)
8475 + {
8476 + if (lzo1x_optimize_type == 1)
8477 + {
8478 + return "LZO1X-1 compression with optimization";
8479 + }
8480 + else
8481 + {
8482 + return "LZO1X-1 compression without optimization";
8483 + }
8484 + }
8485 + else if (lzo1x_compressor_type == 999)
8486 + {
8487 + if (lzo1x_optimize_type == 1)
8488 + {
8489 + return "LZO1X-999 compression with optimization";
8490 + }
8491 + else
8492 + {
8493 + return "LZO1X-999 compression without optimization";
8494 + }
8495 + }
8496 + else
8497 + {
8498 + return "Unknown configuration!";
8499 + }
8500 +}
8501 +
8502 +static int
8503 +jffs2_bbc_lzo_proc_command (char *command)
8504 +{
8505 + switch (*command)
8506 + {
8507 + case 'o':
8508 + /* switch optimization off */
8509 + lzo1x_optimizer = no_lzo1x_optimize;
8510 + lzo1x_optimize_type = 0;
8511 + jffs2_bbc_print1 ("Compression optimization switched off.\n");
8512 + return 0;
8513 + case 'O':
8514 + /* switch optimization on */
8515 + lzo1x_optimizer = lzo1x_optimize;
8516 + lzo1x_optimize_type = 1;
8517 + jffs2_bbc_print1 ("Compression optimization switched on.\n");
8518 + return 0;
8519 + case '1':
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");
8528 + return 0;
8529 + case '9':
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);
8537 + jffs2_bbc_print1
8538 + ("Compression type switched to LZO1X-999.\n");
8539 + return 0;
8540 + default:
8541 + jffs2_bbc_print1 ("Unknown command!\n");
8542 + return 0;
8543 + }
8544 +}
8545 +
8546 +
8547 +struct jffs2_bbc_compressor_type *
8548 +jffs2_bbc_lzo_init (int mode)
8549 +{
8550 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzo) == 0)
8551 + {
8552 + return &jffs2_bbc_lzo;
8553 + }
8554 + else
8555 + {
8556 + return NULL;
8557 + }
8558 +}
8559 +
8560 +void
8561 +jffs2_bbc_lzo_deinit (void)
8562 +{
8563 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzo);
8564 +}
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
8568 @@ -0,0 +1,385 @@
8569 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
8570 +
8571 +/*
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
8576 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
8577 +*/
8578 +
8579 +/*
8580 +Original copyright follows:
8581 +
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.
8588 + PC-VAN SCIENCE
8589 + NIFTY-Serve PAF01022
8590 + CompuServe 74050,1022
8591 +**************************************************************
8592 +
8593 +*/
8594 +
8595 +/*
8596 +
8597 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
8598 + Initial release
8599 +
8600 +*/
8601 +
8602 +/* lzss.c */
8603 +
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 */
8609 +
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. */
8617 +
8618 +static void InitTree(void) /* initialize trees */
8619 +{
8620 + unsigned long i;
8621 +
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. */
8629 +
8630 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
8631 + for (i = 0; i < N; i++) dad[i] = NIL;
8632 +}
8633 +
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. */
8641 +{
8642 + unsigned long i, p;
8643 + unsigned char *key;
8644 + signed long cmp;
8645 +
8646 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
8647 + rson[r] = lson[r] = NIL; match_length = 0;
8648 + for ( ; ; ) {
8649 + if (cmp >= 0) {
8650 + if (rson[p] != NIL) p = rson[p];
8651 + else { rson[p] = r; dad[r] = p; return; }
8652 + } else {
8653 + if (lson[p] != NIL) p = lson[p];
8654 + else { lson[p] = r; dad[r] = p; return; }
8655 + }
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;
8661 + }
8662 + }
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 */
8668 +}
8669 +
8670 +static void DeleteNode(unsigned long p) /* deletes node p from tree */
8671 +{
8672 + unsigned long q;
8673 +
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];
8677 + else {
8678 + q = lson[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;
8683 + }
8684 + rson[q] = rson[p]; dad[rson[p]] = q;
8685 + }
8686 + dad[q] = dad[p];
8687 + if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
8688 + dad[p] = NIL;
8689 +}
8690 +
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 */
8694 +
8695 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
8696 + unsigned long *dstlen)
8697 +{
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;
8705 + ip = srcbuf;
8706 + op = dstbuf;
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;
8713 + s = 0; r = N - F;
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
8718 + the buffer */
8719 + read = len;
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. */
8726 + do {
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. */
8733 + } else {
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. */
8738 + }
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
8742 + code */
8743 + return -1;
8744 + };
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;
8750 + }
8751 + last_match_length = match_length;
8752 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
8753 + c = *(ip++);
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
8761 + modulo N. */
8762 + InsertNode(r); /* Register the string in text_buf[r..r+F-1] */
8763 + }
8764 + read += i;
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. */
8769 + }
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;
8774 + return -1;
8775 + }
8776 + for (i = 0; i < code_buf_ptr; *(op++) = code_buf[i++]);
8777 + written += code_buf_ptr;
8778 + *srclen = read;
8779 + }
8780 + *dstlen = written;
8781 + return 0;
8782 +}
8783 +
8784 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
8785 + unsigned long dstlen) /* Just the reverse of Encode(). */
8786 +{
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;
8794 + ip = srcbuf;
8795 + op = dstbuf;
8796 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
8797 + r = N - F; flags = 0;
8798 + for ( ; ; ) {
8799 + if (((flags >>= 1) & 256) == 0) {
8800 + if (ip >= srcend) return 0;
8801 + c = *(ip++);
8802 + flags = c | 0xff00; /* uses higher byte cleverly */
8803 + } /* to count eight */
8804 + if (flags & 1) {
8805 + if (ip >= srcend) return 0;
8806 + c = *(ip++);
8807 + if (op >= dstend) return -1;
8808 + *(op++) = text_buf[r++] = c; r &= (N - 1);
8809 + } else {
8810 + if ((ip + 2) > srcend) return 0;
8811 + i = *(ip++);
8812 + j = *(ip++);
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);
8818 + }
8819 + }
8820 + }
8821 +}
8822 +
8823 +/* interface to jffs2 bbc follows */
8824 +
8825 +#include "jffs2_bbc_framework.h"
8826 +
8827 +
8828 +#define JFFS2_BBC_LZSS_BLOCK_SIGN {0x27, 0x6f, 0x12, 0xc4}
8829 +
8830 +static int
8831 +jffs2_bbc_lzss_compressor_init (void);
8832 +
8833 +static void
8834 +jffs2_bbc_lzss_compressor_deinit (void);
8835 +
8836 +static int
8837 +jffs2_bbc_lzss_compress (void *model, unsigned char *input,
8838 + unsigned char *output, unsigned long *sourcelen,
8839 + unsigned long *dstlen);
8840 +
8841 +static int
8842 +jffs2_bbc_lzss_estimate (void *model, unsigned char *input,
8843 + unsigned long sourcelen, unsigned long *dstlen,
8844 + unsigned long *readtime, unsigned long *writetime);
8845 +
8846 +static int
8847 +jffs2_bbc_lzss_decompress (void *model, unsigned char *input,
8848 + unsigned char *output, unsigned long sourcelen,
8849 + unsigned long dstlen);
8850 +
8851 +static char *
8852 +jffs2_bbc_lzss_proc_info (void);
8853 +
8854 +static int
8855 +jffs2_bbc_lzss_proc_command (char *command);
8856 +
8857 +struct jffs2_bbc_compressor_type jffs2_bbc_lzss = {
8858 + "lzss",
8859 + 0,
8860 + JFFS2_BBC_LZSS_BLOCK_SIGN,
8861 + jffs2_bbc_lzss_compressor_init,
8862 + NULL,
8863 + NULL,
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
8870 +};
8871 +
8872 +static int
8873 +jffs2_bbc_lzss_compressor_init (void)
8874 +{
8875 + return 0;
8876 +}
8877 +
8878 +static void
8879 +jffs2_bbc_lzss_compressor_deinit (void)
8880 +{
8881 +}
8882 +
8883 +static int
8884 +jffs2_bbc_lzss_compress (void *model, unsigned char *input,
8885 + unsigned char *output, unsigned long *sourcelen,
8886 + unsigned long *dstlen)
8887 +{
8888 + int retval;
8889 + unsigned long dst = *dstlen;
8890 + *(output++) = jffs2_bbc_lzss.block_sign[0];
8891 + *(output++) = jffs2_bbc_lzss.block_sign[1];
8892 + dst -= 2;
8893 + retval = Encode(input, output, sourcelen, &dst);
8894 + dst += 2;
8895 + *dstlen = dst;
8896 + return retval;
8897 +}
8898 +
8899 +static int
8900 +jffs2_bbc_lzss_estimate (void *model, unsigned char *input,
8901 + unsigned long sourcelen, unsigned long *dstlen,
8902 + unsigned long *readtime, unsigned long *writetime)
8903 +{
8904 + *dstlen = sourcelen * 60 / 100;
8905 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 12 / 10;
8906 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 3;
8907 + return 0;
8908 +}
8909 +
8910 +static int
8911 +jffs2_bbc_lzss_decompress (void *model, unsigned char *input,
8912 + unsigned char *output, unsigned long sourcelen,
8913 + unsigned long dstlen)
8914 +{
8915 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzss.block_sign[0] ) ||
8916 + ( *(input++) != (unsigned char)jffs2_bbc_lzss.block_sign[1] )
8917 + ) {
8918 + return -1;
8919 + } else {
8920 + return Decode(input, output, sourcelen - 2, dstlen);
8921 + }
8922 +}
8923 +
8924 +static char *
8925 +jffs2_bbc_lzss_proc_info (void)
8926 +{
8927 + return "Lempel-Ziv-Storer-Szymanski compression module";
8928 +}
8929 +
8930 +static int
8931 +jffs2_bbc_lzss_proc_command (char *command)
8932 +{
8933 + return 0;
8934 +}
8935 +
8936 +struct jffs2_bbc_compressor_type *
8937 +jffs2_bbc_lzss_init (int mode)
8938 +{
8939 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzss) == 0)
8940 + {
8941 + return &jffs2_bbc_lzss;
8942 + }
8943 + else
8944 + {
8945 + return NULL;
8946 + }
8947 +}
8948 +
8949 +void
8950 +jffs2_bbc_lzss_deinit (void)
8951 +{
8952 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzss);
8953 +}
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
8957 @@ -0,0 +1,97 @@
8958 +FMakefile
8959 +=BBC insertion
8960 +-COMPR_OBJS
8961 +iMakefile.bbc.inc
8962 ++
8963 +I
8964 +?JFFS2_OBJS
8965 ++ $(JFFS2_BBC_KERNEL_OBJS) \
8966 +
8967 +F../Config.in
8968 +=BBC insertion
8969 +-tristate 'Compressed ROM file system support' CONFIG_CRAMFS
8970 +iConfig.in.bbc.inc
8971 ++
8972 +I
8973 +F../../Documentation/Configure.help
8974 +=BBC insertion
8975 +-JFFS stats available
8976 +iConfigure.help.bbc.inc
8977 ++
8978 +I
8979 +Fcompr_zlib.c
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);
8986 +?}
8987 ++
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);
8990 ++
8991 ++int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
8992 ++ __u32 *sourcelen, __u32 *dstlen)
8993 ++{
8994 ++ return jffs2_zlib_compress(data_in,cpage_out,sourcelen,dstlen);
8995 ++}
8996 ++
8997 ++void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
8998 ++ __u32 srclen, __u32 destlen)
8999 ++{
9000 ++ jffs2_zlib_decompress(data_in,cpage_out,srclen,destlen);
9001 ++}
9002 ++
9003 +
9004 +Ffile.c
9005 +=set_act_sb before write
9006 +-#include
9007 ++#include "jffs2_bbc_framework.h" /**BBC**/
9008 +I
9009 +?int jffs2_commit_write
9010 +-jffs2_compress(
9011 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9012 +I
9013 +
9014 +Fgc.c
9015 +=set_act_sb before write
9016 +-#include
9017 ++#include "jffs2_bbc_framework.h" /**BBC**/
9018 +I
9019 +?int jffs2_garbage_collect_dnode(
9020 +-jffs2_compress(
9021 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9022 +I
9023 +
9024 +Fread.c
9025 +=set_act_sb before read
9026 +-#include
9027 ++#include "jffs2_bbc_framework.h" /**BBC**/
9028 +I
9029 +?int jffs2_read_dnode(
9030 +-jffs2_decompress(
9031 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9032 +I
9033 +
9034 +Fsuper.c
9035 +=init, load_model
9036 +-#include
9037 ++#include "jffs2_bbc_fs.h" /**BBC**/
9038 +I
9039 +?struct super_block *jffs2_read_super(
9040 +-return sb;
9041 ++ jffs2_bbc_load_model(sb); /**BBC**/
9042 +I
9043 +?void jffs2_put_super
9044 +?c = JFFS2_SB_INFO
9045 ++ jffs2_bbc_unload_model(sb); /**BBC**/
9046 +?init_jffs2_fs(void)
9047 +?int ret;
9048 ++
9049 ++ jffs2_bbc_proc_init(); /**BBC**/
9050 ++
9051 +?exit_jffs2_fs(void)
9052 +?{
9053 ++ jffs2_bbc_proc_deinit(); /**BBC**/
9054 ++
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
9058 @@ -35,6 +35,7 @@
9059 *
9060 */
9061
9062 +#include "jffs2_bbc_framework.h" /**BBC**/
9063 #include <linux/kernel.h>
9064 #include <linux/slab.h>
9065 #include <linux/jffs2.h>
9066 @@ -140,6 +141,7 @@
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);
9072 if (ret) {
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
9077 @@ -35,6 +35,7 @@
9078 *
9079 */
9080
9081 +#include "jffs2_bbc_fs.h" /**BBC**/
9082 #include <linux/config.h>
9083 #include <linux/kernel.h>
9084 #include <linux/module.h>
9085 @@ -272,6 +273,7 @@
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**/
9090 return sb;
9091
9092 out_root_i:
9093 @@ -288,6 +290,7 @@
9094 void jffs2_put_super (struct super_block *sb)
9095 {
9096 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
9097 + jffs2_bbc_unload_model(sb); /**BBC**/
9098
9099 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
9100
9101 @@ -344,6 +347,9 @@
9102 {
9103 int ret;
9104
9105 + jffs2_bbc_proc_init(); /**BBC**/
9106 +
9107 +
9108 printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
9109
9110 #ifdef JFFS2_OUT_OF_KERNEL
9111 @@ -388,6 +394,8 @@
9112
9113 static void __exit exit_jffs2_fs(void)
9114 {
9115 + jffs2_bbc_proc_deinit(); /**BBC**/
9116 +
9117 jffs2_destroy_slab_caches();
9118 jffs2_zlib_exit();
9119 unregister_filesystem(&jffs2_fs_type);