This series of patches closes the support gap on one of the explicitly
[openwrt/svn-archive/archive.git] / target / linux / generic-2.4 / patches / 003-jffs2_compression.patch
1 Index: linux-2.4.35.4/Documentation/Configure.help
2 ===================================================================
3 --- linux-2.4.35.4.orig/Documentation/Configure.help
4 +++ linux-2.4.35.4/Documentation/Configure.help
5 @@ -17580,6 +17580,32 @@ CONFIG_JFFS2_FS_DEBUG
6 If reporting bugs, please try to have available a full dump of the
7 messages at debug level 1 while the misbehaviour was occurring.
8
9 +ARMLIB compression support for BBC (EXPERIMENTAL)
10 +CONFIG_JFFS2_BBC_ARMLIB
11 + This enables ARMLIB support for BBC.
12 +
13 +LZO1X-* compression support for BBC (EXPERIMENTAL)
14 +CONFIG_JFFS2_BBC_LZO
15 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
16 + compressor, beats ZLIB in everything)
17 +
18 +LZARI compression support for BBC (EXPERIMENTAL)
19 +CONFIG_JFFS2_BBC_LZARI
20 + This enables LempelZiv-Storer-Szymanski compression for BBC with
21 + additional arithmetic coding (damn slow, but best compresor).
22 +
23 +LZHD compression support for BBC (EXPERIMENTAL)
24 +CONFIG_JFFS2_BBC_LZHD
25 + This enables LempelZiv-Storer-Szymanski compression for BBC with
26 + additional dynamic Huffman coding (a little faster than LZARI, and
27 + it's compression ratio is a little worse than LZARI's)
28 +
29 +LZSS compression support for BBC (EXPERIMENTAL)
30 +CONFIG_JFFS2_BBC_LZSS
31 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
32 + (faster than LZHD, and, and has a not-so-good compression ratio,
33 + was included just for testing)
34 +
35 JFFS stats available in /proc filesystem
36 CONFIG_JFFS_PROC_FS
37 Enabling this option will cause statistics from mounted JFFS file systems
38 Index: linux-2.4.35.4/fs/Config.in
39 ===================================================================
40 --- linux-2.4.35.4.orig/fs/Config.in
41 +++ linux-2.4.35.4/fs/Config.in
42 @@ -50,6 +50,12 @@ dep_tristate 'Journalling Flash File Sys
43 if [ "$CONFIG_JFFS2_FS" = "y" -o "$CONFIG_JFFS2_FS" = "m" ] ; then
44 int 'JFFS2 debugging verbosity (0 = quiet, 2 = noisy)' CONFIG_JFFS2_FS_DEBUG 0
45 fi
46 +dep_mbool ' ARMLIB compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_ARMLIB $CONFIG_JFFS2_FS
47 +dep_mbool ' LZO1X-* compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZO $CONFIG_JFFS2_FS
48 +dep_mbool ' LZARI compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZARI $CONFIG_JFFS2_FS
49 +dep_mbool ' LZHD compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZHD $CONFIG_JFFS2_FS
50 +dep_mbool ' LZSS compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZSS $CONFIG_JFFS2_FS
51 +
52 tristate 'Compressed ROM file system support' CONFIG_CRAMFS
53 tristate 'Squashed file system support' CONFIG_SQUASHFS
54 if [ "$CONFIG_SQUASHFS" = "y" -o "$CONFIG_SQUASHFS" = "m" ] ; then
55 Index: linux-2.4.35.4/fs/jffs2/Config.in.bbc.inc
56 ===================================================================
57 --- /dev/null
58 +++ linux-2.4.35.4/fs/jffs2/Config.in.bbc.inc
59 @@ -0,0 +1,5 @@
60 +dep_mbool ' ARMLIB compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_ARMLIB $CONFIG_JFFS2_FS
61 +dep_mbool ' LZO1X-* compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZO $CONFIG_JFFS2_FS
62 +dep_mbool ' LZARI compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZARI $CONFIG_JFFS2_FS
63 +dep_mbool ' LZHD compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZHD $CONFIG_JFFS2_FS
64 +dep_mbool ' LZSS compression support for BBC (EXPERIMENTAL)' CONFIG_JFFS2_BBC_LZSS $CONFIG_JFFS2_FS
65 Index: linux-2.4.35.4/fs/jffs2/Configure.help.bbc.inc
66 ===================================================================
67 --- /dev/null
68 +++ linux-2.4.35.4/fs/jffs2/Configure.help.bbc.inc
69 @@ -0,0 +1,25 @@
70 +ARMLIB compression support for BBC (EXPERIMENTAL)
71 +CONFIG_JFFS2_BBC_ARMLIB
72 + This enables ARMLIB support for BBC.
73 +
74 +LZO1X-* compression support for BBC (EXPERIMENTAL)
75 +CONFIG_JFFS2_BBC_LZO
76 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
77 + compressor, beats ZLIB in everything)
78 +
79 +LZARI compression support for BBC (EXPERIMENTAL)
80 +CONFIG_JFFS2_BBC_LZARI
81 + This enables LempelZiv-Storer-Szymanski compression for BBC with
82 + additional arithmetic coding (damn slow, but best compresor).
83 +
84 +LZHD compression support for BBC (EXPERIMENTAL)
85 +CONFIG_JFFS2_BBC_LZHD
86 + This enables LempelZiv-Storer-Szymanski compression for BBC with
87 + additional dynamic Huffman coding (a little faster than LZARI, and
88 + it's compression ratio is a little worse than LZARI's)
89 +
90 +LZSS compression support for BBC (EXPERIMENTAL)
91 +CONFIG_JFFS2_BBC_LZSS
92 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
93 + (faster than LZHD, and, and has a not-so-good compression ratio,
94 + was included just for testing)
95 Index: linux-2.4.35.4/fs/jffs2/Kconfig.bbc.inc
96 ===================================================================
97 --- /dev/null
98 +++ linux-2.4.35.4/fs/jffs2/Kconfig.bbc.inc
99 @@ -0,0 +1,40 @@
100 +config JFFS2_BBC_ARMLIB
101 + bool "ARMLIB compression support for BBC (EXPERIMENTAL)"
102 + depends on JFFS2_FS && EXPERIMENTAL
103 + default y
104 + help
105 + This enables ARMLIB support for BBC.
106 +
107 +config JFFS2_BBC_LZO
108 + bool "LZO1X-* compression support for BBC (EXPERIMENTAL)"
109 + depends on JFFS2_FS && EXPERIMENTAL
110 + default y
111 + help
112 + This enables LZO1X-1 and LZO1X-999 support for BBC. (fast & good
113 + compressor)
114 +
115 +config JFFS2_BBC_LZARI
116 + bool "LZARI compression support for BBC (EXPERIMENTAL)"
117 + depends on JFFS2_FS && EXPERIMENTAL
118 + default y
119 + help
120 + This enables LempelZiv-Storer-Szymanski compression for BBC with
121 + additional arithmetic coding (damn slow, but best compresor).
122 +
123 +config JFFS2_BBC_LZHD
124 + bool "LZHD compression support for BBC (EXPERIMENTAL)"
125 + depends on JFFS2_FS && EXPERIMENTAL
126 + default y
127 + help
128 + This enables LempelZiv-Storer-Szymanski compression for BBC with
129 + additional dynamic Huffman coding (a little faster than LZARI, and
130 + it's compression ratio is a little worse than LZARI's)
131 +
132 +config JFFS2_BBC_LZSS
133 + bool "LZSS compression support for BBC (EXPERIMENTAL)"
134 + depends on JFFS2_FS && EXPERIMENTAL
135 + default y
136 + help
137 + This enables simple LempelZiv-Storer-Szymanski compression for BBC
138 + (faster than LZHD, and, and has a not-so-good compression ratio,
139 + was included just for testing)
140 Index: linux-2.4.35.4/fs/jffs2/Makefile
141 ===================================================================
142 --- linux-2.4.35.4.orig/fs/jffs2/Makefile
143 +++ linux-2.4.35.4/fs/jffs2/Makefile
144 @@ -10,9 +10,23 @@
145 # Note 2! The CFLAGS definitions are now in the main makefile...
146
147
148 +JFFS2_BBC_KERNEL_OBJS-y = jffs2_bbc_framework.o jffs2_bbc_fs.o
149 +
150 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_ARMLIB) += jffs2_bbc_armlib_comp.o
151 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZO) += jffs2_bbc_lzo_comp.o
152 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZSS) += jffs2_bbc_lzss_comp.o
153 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZARI) += jffs2_bbc_lzari_comp.o
154 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZHD) += jffs2_bbc_lzhd_comp.o
155 +
156 +JFFS2_BBC_KERNEL_OBJS := $(JFFS2_BBC_KERNEL_OBJS-y)
157 +
158 +JFFS2_BBC_MKFS_OBJS = jffs2_bbc_mkfs.o jffs2_bbc_framework.o jffs2_bbc_armlib_comp.o jffs2_bbc_lzo_comp.o\
159 + jffs2_bbc_lzss_comp.o jffs2_bbc_lzari_comp.o jffs2_bbc_lzhd_comp.o
160 +
161 COMPR_OBJS := compr.o compr_rubin.o compr_rtime.o pushpull.o \
162 compr_zlib.o
163 JFFS2_OBJS := dir.o file.o ioctl.o nodelist.o malloc.o \
164 + $(JFFS2_BBC_KERNEL_OBJS) \
165 read.o nodemgmt.o readinode.o super.o write.o scan.o gc.o \
166 symlink.o build.o erase.o background.o
167
168 Index: linux-2.4.35.4/fs/jffs2/Makefile.bbc.inc
169 ===================================================================
170 --- /dev/null
171 +++ linux-2.4.35.4/fs/jffs2/Makefile.bbc.inc
172 @@ -0,0 +1,12 @@
173 +JFFS2_BBC_KERNEL_OBJS-y = jffs2_bbc_framework.o jffs2_bbc_fs.o
174 +
175 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_ARMLIB) += jffs2_bbc_armlib_comp.o
176 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZO) += jffs2_bbc_lzo_comp.o
177 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZSS) += jffs2_bbc_lzss_comp.o
178 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZARI) += jffs2_bbc_lzari_comp.o
179 +JFFS2_BBC_KERNEL_OBJS-$(CONFIG_JFFS2_BBC_LZHD) += jffs2_bbc_lzhd_comp.o
180 +
181 +JFFS2_BBC_KERNEL_OBJS := $(JFFS2_BBC_KERNEL_OBJS-y)
182 +
183 +JFFS2_BBC_MKFS_OBJS = jffs2_bbc_mkfs.o jffs2_bbc_framework.o jffs2_bbc_armlib_comp.o jffs2_bbc_lzo_comp.o\
184 + jffs2_bbc_lzss_comp.o jffs2_bbc_lzari_comp.o jffs2_bbc_lzhd_comp.o
185 Index: linux-2.4.35.4/fs/jffs2/compr_zlib.c
186 ===================================================================
187 --- linux-2.4.35.4.orig/fs/jffs2/compr_zlib.c
188 +++ linux-2.4.35.4/fs/jffs2/compr_zlib.c
189 @@ -85,7 +85,7 @@ void jffs2_zlib_exit(void)
190 vfree(inflate_workspace);
191 }
192
193 -int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
194 +int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out,
195 __u32 *sourcelen, __u32 *dstlen)
196 {
197 z_stream strm;
198 @@ -145,7 +145,7 @@ int zlib_compress(unsigned char *data_in
199 return 0;
200 }
201
202 -void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
203 +void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out,
204 __u32 srclen, __u32 destlen)
205 {
206 z_stream strm;
207 @@ -175,3 +175,19 @@ void zlib_decompress(unsigned char *data
208 zlib_inflateEnd(&strm);
209 up(&inflate_sem);
210 }
211 +
212 +extern int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 * sourcelen, __u32 * dstlen);
213 +extern void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
214 +
215 +int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
216 + __u32 *sourcelen, __u32 *dstlen)
217 +{
218 + return jffs2_zlib_compress(data_in,cpage_out,sourcelen,dstlen);
219 +}
220 +
221 +void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
222 + __u32 srclen, __u32 destlen)
223 +{
224 + jffs2_zlib_decompress(data_in,cpage_out,srclen,destlen);
225 +}
226 +
227 Index: linux-2.4.35.4/fs/jffs2/file.c
228 ===================================================================
229 --- linux-2.4.35.4.orig/fs/jffs2/file.c
230 +++ linux-2.4.35.4/fs/jffs2/file.c
231 @@ -35,6 +35,7 @@
232 *
233 */
234
235 +#include "jffs2_bbc_framework.h" /**BBC**/
236 #include <linux/kernel.h>
237 #include <linux/mtd/compatmac.h> /* for min() */
238 #include <linux/slab.h>
239 @@ -459,6 +460,7 @@ int jffs2_commit_write (struct file *fil
240
241 comprbuf = kmalloc(cdatalen, GFP_KERNEL);
242 if (comprbuf) {
243 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
244 comprtype = jffs2_compress(page_address(pg)+ (file_ofs & (PAGE_CACHE_SIZE-1)), comprbuf, &datalen, &cdatalen);
245 }
246 if (comprtype == JFFS2_COMPR_NONE) {
247 Index: linux-2.4.35.4/fs/jffs2/gc.c
248 ===================================================================
249 --- linux-2.4.35.4.orig/fs/jffs2/gc.c
250 +++ linux-2.4.35.4/fs/jffs2/gc.c
251 @@ -35,6 +35,7 @@
252 *
253 */
254
255 +#include "jffs2_bbc_framework.h" /**BBC**/
256 #include <linux/kernel.h>
257 #include <linux/mtd/mtd.h>
258 #include <linux/slab.h>
259 @@ -651,6 +652,7 @@ static int jffs2_garbage_collect_dnode(s
260 writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
261
262 if (comprbuf) {
263 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
264 comprtype = jffs2_compress(writebuf, comprbuf, &datalen, &cdatalen);
265 }
266 if (comprtype) {
267 Index: linux-2.4.35.4/fs/jffs2/hpatch
268 ===================================================================
269 --- /dev/null
270 +++ linux-2.4.35.4/fs/jffs2/hpatch
271 @@ -0,0 +1,191 @@
272 +#!/usr/bin/perl
273 +# A patch-like utility
274 +# Designed for patching different version of jffs2 with the same hpatch file
275 +#
276 +# Copyright (C) 2004, Ferenc Havasi
277 +#
278 +# This program is free software; you can redistribute it and/or
279 +# modify it under the terms of the GNU General Public License
280 +# as published by the Free Software Foundation; either version 2
281 +# of the License, or (at your option) any later version.
282 +#
283 +# This program is distributed in the hope that it will be useful,
284 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
285 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
286 +# GNU General Public License for more details.
287 +#
288 +# You should have received a copy of the GNU General Public License
289 +# along with this program; if not, write to the Free Software
290 +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
291 +
292 +$filename_tmp1="file1.tmp";
293 +$filename_tmp2="file2.tmp";
294 +
295 +$filename_in="";
296 +$filename_out=$filename_tmp1;
297 +$filename_cnt=0;
298 +
299 +# Modes:
300 +# 0: expecting =
301 +# 1: normal_cmd
302 +# 2: skip until =
303 +# 3: expecting F (first state)
304 +$mode=3;
305 +
306 +%rules = ();
307 +
308 +sub file_end {
309 + if (($mode!=2)&&($modified==1)) {
310 + while (<SRC>) {
311 + print DST $_;
312 + }
313 + close(SRC);
314 + close(DST);
315 + if ($cmd_name ne "") { $rules{"$cmd_name"}=1; }
316 + $filename_result=$filename_out;
317 + if ($filename_result ne $filename_in_save) {
318 + open(RES,"<$filename_result") or die "Cannot open $filename_result.\n";
319 + open(DST,">$filename_in_save") or die "Cannot open $filename_in_save.\n";
320 + while (<RES>) {
321 + print DST $_;
322 + }
323 + close(DST);
324 + close(RES);
325 + }
326 + unlink($filename_tmp1) && unlink($filename_tmp2);
327 + }
328 + else {
329 + close(SRC);
330 + close(DST);
331 + $filename_result=$filename_in;
332 + if ($filename_result ne $filename_in_save) {
333 + open(RES,"<$filename_result") or die "Cannot open $filename_result.\n";
334 + open(DST,">$filename_in_save") or die "Cannot open $filename_in_save.\n";
335 + while (<RES>) {
336 + print DST $_;
337 + }
338 + close(DST);
339 + close(RES);
340 + }
341 + unlink($filename_tmp1);
342 + }
343 + $modified=0;
344 + foreach $rulename (keys %rules) {
345 + if ($rules{"$rulename"}==0) { print(STDERR "On $filename_in_save error applying rule $rulename.\n"); }
346 + }
347 + %rules = ();
348 +}
349 +
350 +if ($#ARGV<0) {
351 + print ("usage: hpatch hpatch_file\n");
352 + exit;
353 +}
354 +
355 +open(CMD,"<$ARGV[0]") or die "Cannot open $ARGV[0].\n";
356 +$cmd_linenum=0;
357 +
358 +while (chomp($cmd_line=<CMD>)) {
359 + $cmd_linenum++;
360 + if ($cmd_line eq "") {next;}
361 + #$cmd_line =~ s/\#.*//;
362 + $cmd_line =~ s/\ *$//;
363 + if ($cmd_line eq "") {next;}
364 + if ($cmd_line =~ /^F(.*)/) {
365 + $tmp_filename_in=$1;
366 + if ($mode!=3) {
367 + file_end();
368 + }
369 + $filename_in=$tmp_filename_in;
370 + $filename_in_save=$filename_in;
371 + open(SRC,"<$filename_in") or die "Cannot open $filename_in.\n";
372 + open(DST,">$filename_out") or die "Cannot open $filename_out.\n";;
373 + $modified=0;
374 + $mode=0;
375 + next;
376 + }
377 + if ($mode==3) {die "error: F expression expected in line $cmd_linenum\n";}
378 + if ($cmd_line =~ /^=(.*)/) {
379 + $tmp_cmd_name=$1;
380 + if (($mode!=2)&&($modified==1)) {
381 + while (<SRC>) {
382 + print DST $_;
383 + }
384 + close(SRC);
385 + close(DST);
386 + if (($cmd_name ne "")) {$rules{"$cmd_name"}=1;};
387 + $filename_cnt++;
388 + if ($filename_cnt%2==1) {
389 + $filename_in=$filename_tmp1;
390 + $filename_out=$filename_tmp2;
391 + }
392 + else {
393 + $filename_in=$filename_tmp2;
394 + $filename_out=$filename_tmp1;
395 + }
396 + }
397 + else {
398 + close(SRC);
399 + close(DST);
400 + }
401 + $mode=1;
402 + $cmd_name=$tmp_cmd_name;
403 + if (($cmd_name ne "")) {
404 + if ($rules{"$cmd_name"}==1) {
405 + $mode=2;
406 + }
407 + else {
408 + $rules{"$cmd_name"}=0;
409 + }
410 + }
411 + open(SRC,"<$filename_in") or die "Cannot open $filename_in.\n";
412 + open(DST,">$filename_out") or die "Cannot open $filename_out.\n";
413 + $modified=0;
414 + next;
415 + }
416 + if ($mode == 0) {die "error: = expression expected in line $cmd_linenum\n";}
417 + if ($mode == 2) {next;}
418 + if ($cmd_line =~ /^!(.*)/) {
419 + print "$1\n";
420 + $modified=1;
421 + next;
422 + }
423 + if ($cmd_line =~ /^\?(.*)/) {
424 + $search_str=$1;
425 + $found=0;
426 + while (<SRC>) {
427 + print DST $_;
428 + if (index($_,$search_str)>=0) {$found=1; last;}
429 + }
430 + if ($found==0) { $mode=2; }
431 + next;
432 + }
433 + if ($cmd_line =~ /^\+(.*)/) {
434 + print DST "$1\n";
435 + $modified=1;
436 + next;
437 + }
438 + if ($cmd_line =~ /^\-(.*)/) {
439 + $search_str=$1;
440 + $found=0;
441 + while (<SRC>) {
442 + if (index($_,$search_str)>=0) {$saved_line=$_; $found=1; $modified=1; last;}
443 + print DST $_;
444 + }
445 + if ($found==0) { $mode=2; }
446 + next;
447 + }
448 + if ($cmd_line =~ /^i(.*)/) {
449 + $filename_inc=$1;
450 + open(INCSRC,"<$filename_inc") or die "Cannot open $filename_inc.\n";
451 + while (<INCSRC>) {
452 + print DST $_;
453 + }
454 + next;
455 + }
456 + if ($cmd_line =~ /^I/) {
457 + print DST $saved_line;
458 + next;
459 + }
460 +}
461 +file_end();
462 +close(CMD);
463 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_armlib_comp.c
464 ===================================================================
465 --- /dev/null
466 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_armlib_comp.c
467 @@ -0,0 +1,2224 @@
468 +/*
469 + * JFFS2-BBC: armlib compressor plugin
470 + *
471 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
472 + *
473 + * Copyright (C) 2004, Ferenc Havasi & Tamas Gergely
474 + *
475 + * This program is free software; you can redistribute it and/or
476 + * modify it under the terms of the GNU General Public License
477 + * as published by the Free Software Foundation; either version 2
478 + * of the License, or (at your option) any later version.
479 + *
480 + * This program is distributed in the hope that it will be useful,
481 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
482 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
483 + * GNU General Public License for more details.
484 + *
485 + * You should have received a copy of the GNU General Public License
486 + * along with this program; if not, write to the Free Software
487 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
488 + *
489 + */
490 +
491 +#include "jffs2_bbc_framework.h"
492 +
493 +#ifdef __KERNEL__
494 +#include <linux/string.h>
495 +#else
496 +#include <string.h>
497 +#endif
498 +
499 +//ORIGIN: include/DataStructures/TypeDefs.h
500 +
501 +/*******************************************************************************
502 +* FILE: TypeDefs.h
503 +* AUTHOR: Tamás Gergely
504 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
505 +*******************************************************************************/
506 +
507 +#ifndef TYPEDEFS_H
508 +#define TYPEDEFS_H
509 +
510 +#pragma pack(4)
511 +
512 +#ifndef bool
513 +#define bool char
514 +#define true 1
515 +#define false 0
516 +#endif
517 +
518 +#ifndef u8
519 +#define u8 unsigned char
520 +#endif
521 +#ifndef u16
522 +#define u16 unsigned short
523 +#endif
524 +#ifndef u32
525 +#define u32 unsigned long
526 +#endif
527 +#ifndef s8
528 +#define s8 signed char
529 +#endif
530 +#ifndef s16
531 +#define s16 signed short
532 +#endif
533 +#ifndef s32
534 +#define s32 signed long
535 +#endif
536 +
537 +typedef struct
538 +{
539 + u32 capacity;
540 + u32 size;
541 + u32 alloc_size;
542 + void *ptr;
543 +} vector;
544 +
545 +#define VECTOR_P_END(vct) ((void*)(((char*)((vct)->ptr)) + (vct)->size))
546 +#define VECTOR_S_END(vct) ((void*)(((char*)((vct).ptr)) + (vct).size))
547 +
548 +static void vector_clear(vector *);
549 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
550 +static void vector_reset(vector *);
551 +static void vector_clr_ptr(vector *);
552 +static void vector_add_u8(vector *, u8);
553 +static void vector_add_u16(vector *, u16);
554 +static void vector_add_u32(vector *, u32);
555 +static void vector_add_s8(vector *, s8);
556 +static void vector_add_s16(vector *, s16);
557 +static void vector_add_s32(vector *, s32);
558 +static void vector_add_ptr(vector *, void *);
559 +static void vector_concat(vector *, vector *);
560 +#endif
561 +
562 +#endif
563 +
564 +//ORIGIN: include/DataStructures/DataTypes.h
565 +
566 +/*******************************************************************************
567 +* FILE: DataTypes.h
568 +* AUTHOR: Tamás Gergely
569 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
570 +*******************************************************************************/
571 +
572 +#ifndef DATATYPES_H
573 +#define DATATYPES_H
574 +
575 +//#include "DataStructures/TypeDefs.h"
576 +
577 +typedef u16 THUMB_DataType;
578 +typedef u32 ARM_DataType;
579 +typedef u8 TokenType;
580 +typedef u8 PredictorType;
581 +typedef u8 *ProbDist;
582 +
583 +typedef vector RawData;
584 +typedef vector RawBlocks;
585 +typedef vector TokenStream;
586 +typedef vector TokenBlocks;
587 +typedef vector LatType;
588 +
589 +#define THUMB_DATA_LENGTH 16
590 +#define ARM_DATA_LENGTH 32
591 +#define TOKEN_LENGTH 8
592 +#define TOKEN_MAXVALUE 0xff
593 +#define PREDICTOR_LENGTH 8
594 +#define PREDICTOR_MAXVALUE 0xff
595 +
596 +#endif
597 +
598 +//ORIGIN: include/DataStructures/BitVector.h
599 +
600 +/*******************************************************************************
601 +* FILE: BitVector.h
602 +* AUTHOR: Tamás Gergely
603 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
604 +*******************************************************************************/
605 +
606 +#ifndef BITVECTOR_H
607 +#define BITVECTOR_H
608 +
609 +//#include "DataStructures/TypeDefs.h"
610 +
611 +typedef vector BitBlocks;
612 +
613 +#pragma pack(4)
614 +
615 +typedef struct
616 +{
617 + u32 freebits;
618 + u32 capacity;
619 + u32 size;
620 + u8 *base;
621 + u8 *ptr;
622 +} BitVector;
623 +
624 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
625 +static void bitblocks_clear(BitBlocks *);
626 +static void bitvector_clear(BitVector *);
627 +static void bitvector_W_reset(BitVector *);
628 +static void bitvector_W_add0(BitVector *);
629 +static void bitvector_W_add1(BitVector *);
630 +static void bitvector_W_concat_b(BitVector *, BitVector *);
631 +static void bitvector_W_concat_v(BitVector *, vector *);
632 +static void bitvector_W_flush(BitVector *);
633 +static void bitvector_R_reset(BitVector *);
634 +static u8 bitvector_R_get1(BitVector *);
635 +static u8 bitvector_R_get8(BitVector *);
636 +#endif
637 +
638 +#define BITVECTOR_P_END(bv) ((void*)(((bv)->base)+((bv)->size)))
639 +#define BITVECTOR_S_END(bv) ((void*)( ((bv).base)+ ((bv).size)))
640 +#define BITVECTOR_SKIP(bv,num) ((bv)->ptr) += (num)
641 +
642 +#endif
643 +
644 +//ORIGIN: include/DataStructures/DecisionTree.h
645 +
646 +/*******************************************************************************
647 +* FILE: DecisionTree.h
648 +* AUTHOR: Tamás Gergely
649 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
650 +*******************************************************************************/
651 +
652 +#ifndef DECISIONTREE_H
653 +#define DECISIONTREE_H
654 +
655 +//#include "DataStructures/DataTypes.h"
656 +
657 +#pragma pack(4)
658 +
659 +#define TREENODETYPE_NULLNODE 0
660 +#define TREENODETYPE_NODE_BINARY_EQ 1
661 +#define TREENODETYPE_LEAF_P 2
662 +#define TREENODETYPE_LEAF_C 3
663 +#define TREENODETYPE_NODE_BINARY_LT 5
664 +#define TREENODETYPE_IS_NODE(n) (((n) == TREENODETYPE_NODE_BINARY_EQ) || \
665 + ((n) == TREENODETYPE_NODE_BINARY_LT))
666 +#define TREENODETYPE_IS_NODE_BINARY(n) (((n) == TREENODETYPE_NODE_BINARY_EQ) || \
667 + ((n) == TREENODETYPE_NODE_BINARY_LT))
668 +
669 +#define TREENODETYPE_IS_LEAF(n) (((n) == TREENODETYPE_LEAF_P) || \
670 + ((n) == TREENODETYPE_LEAF_C))
671 +
672 +
673 +#define TREE_SUBTREE_RELATION_LEFT_EQ !=
674 +#define TREE_SUBTREE_RELATION_RIGHT_EQ ==
675 +#define TREE_SUBTREE_RELATION_LEFT_LT <
676 +#define TREE_SUBTREE_RELATION_RIGHT_LT >=
677 +
678 +#define GET_NODE_PTR_TYPE(n) (((TreeNodeDummy*)(n))->type)
679 +
680 +typedef struct
681 +{
682 + u8 type;
683 +} TreeNodeDummy;
684 +
685 +typedef struct
686 +{
687 + u8 type; // [TREENODETYPE_NODE_BINARY]
688 + u8 attribute;
689 + PredictorType value;
690 + void *left;
691 + void *right;
692 +} TreeNodeBinary;
693 +
694 +typedef struct
695 +{
696 + u8 type; // [TREENODETYPE_LEAF_P]
697 + u16 pairs;
698 + PredictorType *probabilities;
699 +} TreeLeafP;
700 +
701 +typedef struct
702 +{
703 + u8 type; // [TREENODETYPE_LEAF_C]
704 + PredictorType predicted_class;
705 +} TreeLeafC;
706 +
707 +typedef struct
708 +{
709 + u32 high;
710 + u32 low;
711 + u32 max;
712 +} ProbabilityType;
713 +
714 +
715 +typedef struct
716 +{
717 + void *root;
718 + u16 number_of_classes;
719 + u16 number_of_predictors;
720 + PredictorType *predictor_max_values;
721 +} DecisionTree;
722 +
723 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
724 +static void decisiontree_delete(DecisionTree *);
725 +static void decisiontree_get_probability_for_token(void *, PredictorType *, TokenType, ProbabilityType *);
726 +static TokenType decisiontree_get_token_for_range(void *, PredictorType *, u32, u32, ProbabilityType *);
727 +#endif
728 +
729 +#endif
730 +
731 +//ORIGIN: include/DataStructures/PredictorTable.h
732 +
733 +/*******************************************************************************
734 +* FILE: PredictorTable.h
735 +* AUTHOR: Tamás Gergely
736 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
737 +*******************************************************************************/
738 +
739 +#ifndef PREDICTORTABLE_H
740 +#define PREDICTORTABLE_H
741 +
742 +//#include "DataStructures/TypeDefs.h"
743 +//#include "DataStructures/DataTypes.h"
744 +////#include "DataStructures/Filter.h"
745 +////#include "DataStructures/Converter.h"
746 +////#include "DataStructures/Manipulator.h"
747 +
748 +#define NUMBER_OF_PREDICTORS_ARM 17
749 +
750 +#ifndef __KERNEL__
751 +#define NUMBER_OF_PREDICTORS_TXT 2
752 +#else
753 +#undef TXT_TOKENS
754 +#endif // __KERNEL__
755 +
756 +#ifdef TXT_TOKENS
757 +#define NUMBER_OF_PREDICTORS NUMBER_OF_PREDICTORS_TXT
758 +#define predictortable_reset predictortable_resetTXT
759 +#define predictortable_update predictortable_updateTXT
760 +#define predictortable_minvalue predictortable_minvalueTXT
761 +#define predictortable_maxvalue predictortable_maxvalueTXT
762 +#else
763 +#define NUMBER_OF_PREDICTORS NUMBER_OF_PREDICTORS_ARM
764 +#define predictortable_reset predictortable_resetARM
765 +#define predictortable_update predictortable_updateARM
766 +#define predictortable_minvalue predictortable_minvalueARM
767 +#define predictortable_maxvalue predictortable_maxvalueARM
768 +#endif
769 +
770 +
771 +#pragma pack(4)
772 +
773 +typedef struct
774 +{
775 + PredictorType *predictors;
776 +} PredictorTable;
777 +
778 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
779 +static void predictortable_clear(PredictorTable *);
780 +static void predictortable_free(PredictorTable *);
781 +static void predictortable_resetARM(PredictorTable *);
782 +static void predictortable_updateARM(PredictorTable *, TokenType);
783 +static PredictorType predictortable_minvalueARM(PredictorTable *, u32);
784 +static PredictorType predictortable_maxvalueARM(PredictorTable *, u32);
785 +#endif
786 +
787 +#ifndef __KERNEL__
788 +/*
789 +static void predictortable_resetTXT(PredictorTable *);
790 +static void predictortable_updateTXT(PredictorTable *, TokenType);
791 +static PredictorType predictortable_minvalueTXT(PredictorTable *, u32);
792 +static PredictorType predictortable_maxvalueTXT(PredictorTable *, u32);
793 +*/
794 +#endif // __KERNEL__
795 +
796 +#endif
797 +
798 +//ORIGIN: include/DataStructures/ipack_model.h
799 +
800 +/*******************************************************************************
801 +* FILE: ipack_model.h
802 +* AUTHOR: Tamás Gergely
803 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
804 +*******************************************************************************/
805 +
806 +#ifndef IPACK_MODEL_H
807 +#define IPACK_MODEL_H
808 +
809 +//#include "DataStructures/DataTypes.h"
810 +//#include "DataStructures/DecisionTree.h"
811 +//#include "DataStructures/PredictorTable.h"
812 +
813 +#define PROBABILITY_SHIFT 12
814 +#define PROBABILITY_MAX 0x00001000l
815 +
816 +#define NUMBER_OF_TOKENS_ARM 16
817 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION_ARM 8
818 +
819 +#ifndef __KERNEL__
820 +#define NUMBER_OF_TOKENS_TXT 256
821 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION_TXT 4
822 +#else
823 +#undef TXT_TOKENS
824 +#endif // __KERNEL__
825 +
826 +#ifdef TXT_TOKENS
827 +#define NUMBER_OF_TOKENS NUMBER_OF_TOKENS_TXT
828 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION NUMBER_OF_TOKENS_PER_INSTRUCTION_TXT
829 +#else
830 +#define NUMBER_OF_TOKENS NUMBER_OF_TOKENS_ARM
831 +#define NUMBER_OF_TOKENS_PER_INSTRUCTION NUMBER_OF_TOKENS_PER_INSTRUCTION_ARM
832 +#endif
833 +
834 +#pragma pack(4)
835 +
836 +/*
837 + Data structure of an internal node of the tree
838 +*/
839 +typedef struct
840 +{
841 + PredictorType *attribute_ptr;
842 + u32 value; // PredictorType
843 + void *right_child_ptr;
844 +} ipack_treenodeBin;
845 +/*
846 + Data structure of a leaf with probabilities
847 +*/
848 +typedef struct
849 +{
850 + u16 probabilities[0]; // PredictorType[0]
851 +} ipack_treeleafP;
852 +/*
853 + Data structure of a leaf with class prediction
854 +*/
855 +typedef struct
856 +{
857 + PredictorType predicted_class; // PredictorType
858 +} ipack_treeleafC;
859 +/*
860 + Possible data structures of a tree node
861 +*/
862 +typedef union
863 +{
864 + ipack_treenodeBin nodeBin;
865 + ipack_treeleafP leafP;
866 + ipack_treeleafC leafC;
867 +} ipack_node_data;
868 +/*
869 + Tree node
870 +*/
871 +typedef struct
872 +{
873 + u32 type; // u8
874 + ipack_node_data data; // ipack_node_data
875 +} ipack_nodetype;
876 +/*
877 + Nullnode
878 +*/
879 +typedef struct
880 +{
881 + u32 type;
882 + u16 probabilities[NUMBER_OF_TOKENS];
883 +} ipack_nullnode;
884 +/*
885 + Model for ipack project
886 +*/
887 +typedef struct
888 +{
889 + char ID[4]; // char[4]
890 + char block_sign[4]; // only the first 2 are used!
891 + void *tree_root_ptr; // void*
892 + void *tree_code; // generated ARM code
893 + PredictorType *predictors_ptr; // PredictorType*
894 + ipack_nullnode nullnode;
895 +} ipack_model_type;
896 +
897 +typedef struct
898 +{
899 + u32 high;
900 + u32 low;
901 +} ipack_probability_type;
902 +
903 +
904 +static void ipack_model_get_probability_for_token(ipack_nodetype *, TokenType, ipack_probability_type *);
905 +static TokenType ipack_model_get_token_for_range(ipack_nodetype *, u32, u32, ipack_probability_type *);
906 +/*void ipack_model_predictortable_reset (PredictorType*);
907 +void ipack_model_predictortable_update (PredictorType*, TokenType);*/
908 +
909 +#ifndef __KERNEL__
910 +/*static void ipack_model_printinfo(ipack_model_type *);
911 +static void ipack_dumpmodel(void *);*/
912 +#endif
913 +
914 +#endif
915 +
916 +//ORIGIN: include/Builders/PredictorGenerator.h
917 +
918 +/*******************************************************************************
919 +* FILE: PredictorGenerator.h
920 +* AUTHOR: Tamás Gergely
921 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
922 +*******************************************************************************/
923 +
924 +#ifndef PREDICTORGENERATOR_H
925 +#define PREDICTORGENERATOR_H
926 +
927 +//#include "DataStructures.h"
928 +
929 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
930 +static PredictorTable *predictorgenerator_generate(void);
931 +#endif
932 +
933 +#endif
934 +
935 +//ORIGIN: include/Builders/Coder.h
936 +
937 +/*******************************************************************************
938 +* FILE: Coder.h
939 +* AUTHOR: Tamás Gergely
940 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
941 +*******************************************************************************/
942 +
943 +#ifndef CODER_H
944 +#define CODER_H
945 +
946 +#define CODER_VALUEBITS 16
947 +#define CODER_VALUEMAX 0x00010000l
948 +#define CODER_VALUE3RD 0x0000c000l
949 +#define CODER_VALUEHLF 0x00008000l
950 +#define CODER_VALUE1ST 0x00004000l
951 +
952 +#endif
953 +
954 +//ORIGIN: DataStructures/src/TypeDefs.c
955 +
956 +/*******************************************************************************
957 +* FILE: TypeDefs.c
958 +* AUTHOR: Tamás Gergely
959 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
960 +*******************************************************************************/
961 +
962 +//#include "ipack_common.h"
963 +//#include "DataStructures/TypeDefs.h"
964 +#ifndef __KERNEL__
965 +#include <memory.h>
966 +#endif
967 +
968 +#define VECTOR_ALLOC_SIZE 0x00001000
969 +
970 +static void vector_clear(vector * vct)
971 +{
972 + if (vct->ptr)
973 + jffs2_bbc_free(vct->ptr);
974 + vct->capacity = 0;
975 + vct->size = 0;
976 + vct->ptr = 0;
977 +}
978 +
979 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
980 +static void vector_extend(vector * vct)
981 +{
982 + void *tmp;
983 + vct->capacity += vct->alloc_size;
984 + tmp = jffs2_bbc_malloc(vct->capacity);
985 + if (vct->ptr) {
986 + memcpy(tmp, vct->ptr, vct->size);
987 + jffs2_bbc_free(vct->ptr);
988 + }
989 + vct->ptr = tmp;
990 +}
991 +
992 +static void vector_reset(vector * vct)
993 +{
994 + vct->capacity = 0;
995 + vct->size = 0;
996 + vct->alloc_size = VECTOR_ALLOC_SIZE;
997 + vct->ptr = 0;
998 +}
999 +
1000 +static void vector_clr_ptr(vector * vct)
1001 +{
1002 + void **it;
1003 + void *end_it;
1004 + for (it = vct->ptr, end_it = (((char *) (vct->ptr)) + vct->size); it != end_it; it++) {
1005 + vector_clear(*it);
1006 + jffs2_bbc_free(*it);
1007 + }
1008 + if (vct->ptr)
1009 + jffs2_bbc_free(vct->ptr);
1010 + vct->capacity = 0;
1011 + vct->size = 0;
1012 + vct->ptr = 0;
1013 +}
1014 +
1015 +static void vector_add_u8(vector * vct, u8 val)
1016 +{
1017 + if ((vct->size) + sizeof(u8) > (vct->capacity)) {
1018 + vector_extend(vct);
1019 + }
1020 + *(u8 *) ((char *) (vct->ptr) + (vct->size)) = val;
1021 + vct->size += sizeof(u8);
1022 +};
1023 +
1024 +static void vector_add_u16(vector * vct, u16 val)
1025 +{
1026 + if ((vct->size) + sizeof(u16) > (vct->capacity)) {
1027 + vector_extend(vct);
1028 + }
1029 + *(u16 *) ((char *) (vct->ptr) + (vct->size)) = val;
1030 + vct->size += sizeof(u16);
1031 +};
1032 +
1033 +static void vector_add_u32(vector * vct, u32 val)
1034 +{
1035 + if ((vct->size) + sizeof(u32) > (vct->capacity)) {
1036 + vector_extend(vct);
1037 + }
1038 + *(u32 *) ((char *) (vct->ptr) + (vct->size)) = val;
1039 + vct->size += sizeof(u32);
1040 +};
1041 +
1042 +static void vector_add_s8(vector * vct, s8 val)
1043 +{
1044 + if ((vct->size) + sizeof(s8) > (vct->capacity)) {
1045 + vector_extend(vct);
1046 + }
1047 + *(s8 *) ((char *) (vct->ptr) + (vct->size)) = val;
1048 + vct->size += sizeof(s8);
1049 +};
1050 +
1051 +static void vector_add_s16(vector * vct, s16 val)
1052 +{
1053 + if ((vct->size) + sizeof(s16) > (vct->capacity)) {
1054 + vector_extend(vct);
1055 + }
1056 + *(s16 *) ((char *) (vct->ptr) + (vct->size)) = val;
1057 + vct->size += sizeof(s16);
1058 +};
1059 +
1060 +static void vector_add_s32(vector * vct, s32 val)
1061 +{
1062 + if ((vct->size) + sizeof(s32) > (vct->capacity)) {
1063 + vector_extend(vct);
1064 + }
1065 + *(s32 *) ((char *) (vct->ptr) + (vct->size)) = val;
1066 + vct->size += sizeof(s32);
1067 +};
1068 +
1069 +static void vector_add_ptr(vector * vct, void *ptr)
1070 +{
1071 + if ((vct->size) + sizeof(void *) > (vct->capacity)) {
1072 + vector_extend(vct);
1073 + }
1074 + *(void **) ((char *) (vct->ptr) + (vct->size)) = ptr;
1075 + vct->size += sizeof(void *);
1076 +}
1077 +
1078 +static void vector_concat(vector * lhs, vector * rhs)
1079 +{
1080 + void *tmp;
1081 + if (!(rhs->size)) {
1082 + return;
1083 + }
1084 + tmp = lhs->ptr;
1085 + lhs->capacity = (lhs->size) + (rhs->size);
1086 + lhs->ptr = jffs2_bbc_malloc(lhs->capacity);
1087 + if (tmp) {
1088 + memcpy(lhs->ptr, tmp, lhs->size);
1089 + jffs2_bbc_free(tmp);
1090 + }
1091 + memcpy((((u8 *) lhs->ptr) + lhs->size), rhs->ptr, rhs->size);
1092 + lhs->size += rhs->size;
1093 +}
1094 +
1095 +#endif
1096 +
1097 +//ORIGIN: DataStructures/src/BitVector.c
1098 +
1099 +/*******************************************************************************
1100 +* FILE: BitVector.c
1101 +* AUTHOR: Tamás Gergely
1102 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1103 +*******************************************************************************/
1104 +
1105 +//#include "ipack_common.h"
1106 +//#include "DataStructures/BitVector.h"
1107 +#ifndef __KERNEL__
1108 +#include <memory.h>
1109 +#endif
1110 +
1111 +#define VECTOR_ALLOC_SIZE 0x00001000
1112 +
1113 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1114 +
1115 +static void bitblocks_clear(BitBlocks * this)
1116 +{
1117 + BitVector **it;
1118 + void *end_it;
1119 + for (it = this->ptr, end_it = VECTOR_P_END(this); it != end_it; it++) {
1120 + bitvector_clear(*it);
1121 + jffs2_bbc_free(*it);
1122 + }
1123 + jffs2_bbc_free(this->ptr);
1124 + this->ptr = 0;
1125 +}
1126 +
1127 +static void bitvector_clear(BitVector * this)
1128 +{
1129 + if (this->base) {
1130 + jffs2_bbc_free(this->base);
1131 + }
1132 + this->freebits = 0;
1133 + this->capacity = 0;
1134 + this->size = 0;
1135 + this->base = 0;
1136 + this->ptr = 0;
1137 +}
1138 +
1139 +static void bitvector_W_reset(BitVector * this)
1140 +{
1141 + this->freebits = 0;
1142 + this->capacity = 0;
1143 + this->size = 0;
1144 + this->base = 0;
1145 + this->ptr = 0;
1146 +}
1147 +
1148 +static void bitvector_W_add0(BitVector * this)
1149 +{
1150 + if (!(this->freebits)) {
1151 + if (this->size == this->capacity) {
1152 + void *tmp = this->base;
1153 + this->capacity += VECTOR_ALLOC_SIZE;
1154 + this->base = jffs2_bbc_malloc(this->capacity);
1155 + this->ptr = ((u8 *) (this->base)) + this->size;
1156 + memcpy(this->base, tmp, this->size);
1157 + jffs2_bbc_free(tmp);
1158 + }
1159 + else {
1160 + this->ptr++;
1161 + }
1162 + this->size++;
1163 + this->freebits = 7;
1164 + *(this->ptr) = 0x00;
1165 + }
1166 + else {
1167 + this->freebits--;
1168 + (*(this->ptr)) <<= 1;
1169 + }
1170 +}
1171 +
1172 +static void bitvector_W_add1(BitVector * this)
1173 +{
1174 + if (!(this->freebits)) {
1175 + if (this->size == this->capacity) {
1176 + void *tmp = this->base;
1177 + this->capacity += VECTOR_ALLOC_SIZE;
1178 + this->base = jffs2_bbc_malloc(this->capacity);
1179 + this->ptr = ((u8 *) (this->base)) + this->size;
1180 + memcpy(this->base, tmp, this->size);
1181 + jffs2_bbc_free(tmp);
1182 + }
1183 + else {
1184 + this->ptr++;
1185 + }
1186 + this->size++;
1187 + this->freebits = 7;
1188 + *(this->ptr) = 0x01;
1189 + }
1190 + else {
1191 + this->freebits--;
1192 + (*(this->ptr)) <<= 1;
1193 + (*(this->ptr)) |= 0x01;
1194 + }
1195 +}
1196 +
1197 +static void bitvector_W_concat_b(BitVector * lhs, BitVector * rhs)
1198 +{
1199 + void *tmp;
1200 + if (!(rhs->size)) {
1201 + return;
1202 + }
1203 + tmp = lhs->base;
1204 + lhs->capacity = ((((lhs->size) + (rhs->size) - 1) / VECTOR_ALLOC_SIZE) + 1) * VECTOR_ALLOC_SIZE;
1205 + lhs->base = jffs2_bbc_malloc(lhs->capacity);
1206 + if (tmp) {
1207 + memcpy(lhs->base, tmp, lhs->size);
1208 + jffs2_bbc_free(tmp);
1209 + }
1210 + memcpy((((u8 *) (lhs->base)) + lhs->size), rhs->base, rhs->size);
1211 + lhs->freebits = 0;
1212 + lhs->size += rhs->size;
1213 + lhs->ptr = ((u8 *) (lhs->base)) + lhs->size;
1214 +}
1215 +
1216 +static void bitvector_W_concat_v(BitVector * lhs, vector * rhs)
1217 +{
1218 + void *tmp;
1219 + if (!(rhs->size)) {
1220 + return;
1221 + }
1222 + tmp = lhs->base;
1223 + lhs->capacity = ((((lhs->size) + (rhs->size) - 1) / VECTOR_ALLOC_SIZE) + 1) * VECTOR_ALLOC_SIZE;
1224 + lhs->base = jffs2_bbc_malloc(lhs->capacity);
1225 + if (tmp) {
1226 + memcpy(lhs->base, tmp, lhs->size);
1227 + jffs2_bbc_free(tmp);
1228 + }
1229 + memcpy((((u8 *) (lhs->base)) + lhs->size), rhs->ptr, rhs->size);
1230 + lhs->freebits = 0;
1231 + lhs->size += rhs->size;
1232 + lhs->ptr = ((u8 *) (lhs->base)) + lhs->size;
1233 +}
1234 +
1235 +static void bitvector_W_flush(BitVector * this)
1236 +{
1237 + (*(this->ptr)) <<= this->freebits;
1238 + this->freebits = 0;
1239 +}
1240 +
1241 +static void bitvector_R_reset(BitVector * this)
1242 +{
1243 + this->freebits = 7;
1244 + this->ptr = this->base;
1245 +}
1246 +
1247 +static u8 bitvector_R_get1(BitVector * this)
1248 +{
1249 + u8 tmp = ((*(this->ptr)) >> this->freebits) & 0x01;
1250 + if (!(this->freebits)) {
1251 + this->freebits = 7;
1252 + this->ptr++;
1253 + }
1254 + else {
1255 + this->freebits--;
1256 + }
1257 + return tmp;
1258 +}
1259 +
1260 +static u8 bitvector_R_get8(BitVector * this)
1261 +{
1262 + u8 tmp = (*(this->ptr));
1263 + this->ptr++;
1264 + return tmp;
1265 +}
1266 +
1267 +#endif
1268 +
1269 +//ORIGIN: DataStructures/src/DecisionTree.c
1270 +
1271 +/*******************************************************************************
1272 +* FILE: DecisionTree.c
1273 +* AUTHOR: Tamás Gergely
1274 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1275 +*******************************************************************************/
1276 +
1277 +//#include "ipack_common.h"
1278 +//#include "DataStructures/DecisionTree.h"
1279 +
1280 +static void decisiontree_delete_node(void *root)
1281 +{
1282 + u8 tmp = GET_NODE_PTR_TYPE(root);
1283 + if (TREENODETYPE_IS_NODE_BINARY(tmp)) {
1284 + decisiontree_delete_node(((TreeNodeBinary *) root)->left);
1285 + decisiontree_delete_node(((TreeNodeBinary *) root)->right);
1286 + }
1287 + else if ((tmp) == TREENODETYPE_LEAF_P) {
1288 + if (((TreeLeafP *) root)->probabilities) {
1289 + jffs2_bbc_free(((TreeLeafP *) root)->probabilities);
1290 + }
1291 + }
1292 + else if ((tmp) == TREENODETYPE_LEAF_C) {
1293 + }
1294 + jffs2_bbc_free(root);
1295 +}
1296 +
1297 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1298 +
1299 +static void decisiontree_delete(DecisionTree * dt)
1300 +{
1301 + decisiontree_delete_node(dt->root);
1302 + jffs2_bbc_free(dt->predictor_max_values);
1303 +}
1304 +
1305 +static void decisiontree_get_probability_for_token(void *root, PredictorType * preds, TokenType token, ProbabilityType * prob)
1306 +{
1307 + void *tmp = root;
1308 + while (TREENODETYPE_IS_NODE(((TreeNodeBinary *) tmp)->type)) {
1309 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_EQ) {
1310 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_EQ((TreeNodeBinary *) tmp)->value) {
1311 + tmp = ((TreeNodeBinary *) tmp)->left;
1312 + }
1313 + else {
1314 + tmp = ((TreeNodeBinary *) tmp)->right;
1315 + }
1316 + }
1317 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_LT) {
1318 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_LT((TreeNodeBinary *) tmp)->value) {
1319 + tmp = ((TreeNodeBinary *) tmp)->left;
1320 + }
1321 + else {
1322 + tmp = ((TreeNodeBinary *) tmp)->right;
1323 + }
1324 + }
1325 + }
1326 + prob->high = 0;
1327 + prob->low = 0;
1328 + prob->max = 0;
1329 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_P) {
1330 + u32 i;
1331 + u32 lngth = ((TreeLeafP *) tmp)->pairs << 1;
1332 + for (i = 0; i < lngth;) {
1333 + TokenType at = ((TreeLeafP *) tmp)->probabilities[i++];
1334 + TokenType av = ((TreeLeafP *) tmp)->probabilities[i++];
1335 + if (token > at)
1336 + prob->low += av;
1337 + if (token >= at)
1338 + prob->high += av;
1339 + prob->max += av;
1340 + }
1341 + }
1342 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_C) {
1343 + if (((TreeLeafC *) tmp)->predicted_class == token) {
1344 + prob->high = TOKEN_MAXVALUE;
1345 + prob->max = TOKEN_MAXVALUE;
1346 + }
1347 + }
1348 +}
1349 +
1350 +static TokenType decisiontree_get_token_for_range(void *root, PredictorType * preds, u32 value, u32 range, ProbabilityType * prob)
1351 +{
1352 + void *tmp = root;
1353 + TokenType token = 0;
1354 + while (TREENODETYPE_IS_NODE(((TreeNodeBinary *) tmp)->type)) {
1355 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_EQ) {
1356 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_EQ((TreeNodeBinary *) tmp)->value) {
1357 + tmp = ((TreeNodeBinary *) tmp)->left;
1358 + }
1359 + else {
1360 + tmp = ((TreeNodeBinary *) tmp)->right;
1361 + }
1362 + }
1363 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_NODE_BINARY_LT) {
1364 + if (preds[((TreeNodeBinary *) tmp)->attribute] TREE_SUBTREE_RELATION_LEFT_LT((TreeNodeBinary *) tmp)->value) {
1365 + tmp = ((TreeNodeBinary *) tmp)->left;
1366 + }
1367 + else {
1368 + tmp = ((TreeNodeBinary *) tmp)->right;
1369 + }
1370 + }
1371 + }
1372 + prob->high = 0;
1373 + prob->low = 0;
1374 + prob->max = 0;
1375 + if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_P) {
1376 + u32 i;
1377 + u32 norm;
1378 + TokenType at = 0;
1379 + TokenType av;
1380 + u32 lngth = ((TreeLeafP *) tmp)->pairs << 1;
1381 + for (i = 0; i < lngth;) {
1382 + i++;
1383 + prob->max += ((TreeLeafP *) tmp)->probabilities[i++];
1384 + }
1385 + norm = (value * prob->max - 1) / range;
1386 + for (i = 0; prob->high <= norm;) {
1387 + at = ((TreeLeafP *) tmp)->probabilities[i++];
1388 + av = ((TreeLeafP *) tmp)->probabilities[i++];
1389 + prob->high += av;
1390 + if (prob->high <= norm)
1391 + prob->low += av;
1392 + }
1393 + token = at;
1394 + }
1395 + else if (((TreeNodeBinary *) tmp)->type == TREENODETYPE_LEAF_C) {
1396 + token = ((TreeLeafC *) tmp)->predicted_class;
1397 + prob->high = TOKEN_MAXVALUE;
1398 + prob->max = TOKEN_MAXVALUE;
1399 + }
1400 + return token;
1401 +}
1402 +#endif
1403 +
1404 +//ORIGIN: DataStructures/src/PredictorTable.c
1405 +
1406 +/*******************************************************************************
1407 +* FILE: PredictorTable.c
1408 +* AUTHOR: Tamás Gergely
1409 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1410 +*******************************************************************************/
1411 +
1412 +//#include "ipack_common.h"
1413 +//#include "DataStructures/PredictorTable.h"
1414 +
1415 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1416 +
1417 +static void predictortable_clear(PredictorTable * table)
1418 +{
1419 + table->predictors = 0;
1420 +}
1421 +
1422 +static void predictortable_free(PredictorTable * table)
1423 +{
1424 + if (table->predictors) {
1425 + jffs2_bbc_free(table->predictors);
1426 + table->predictors = 0;
1427 + }
1428 +}
1429 +
1430 +static void predictortable_resetARM(PredictorTable * table)
1431 +{
1432 + register PredictorType *ptr = table->predictors;
1433 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS_ARM;
1434 + while (ptr < end) {
1435 + *(ptr++) = 0;
1436 + }
1437 +}
1438 +
1439 +static void predictortable_updateARM(PredictorTable * table, TokenType token)
1440 +{
1441 + register PredictorType *ptr = table->predictors;
1442 + register u32 ndx = ptr[0] + 1;
1443 + ptr[ndx + 8] = ptr[ndx];
1444 + ptr[ndx] = token;
1445 + if (ndx == 8) {
1446 + ptr[0] = 0;
1447 + }
1448 + else {
1449 + ++ptr[0];
1450 + }
1451 +}
1452 +
1453 +static PredictorType predictortable_minvalueARM(PredictorTable * table, u32 index)
1454 +{
1455 + return 0;
1456 +}
1457 +
1458 +static PredictorType predictortable_maxvalueARM(PredictorTable * table, u32 index)
1459 +{
1460 + if (index == 0) {
1461 + return 7;
1462 + }
1463 + else {
1464 + return 15;
1465 + }
1466 +}
1467 +
1468 +#endif
1469 +
1470 +#ifndef __KERNEL__
1471 +
1472 +/*static void predictortable_resetTXT(PredictorTable * table)
1473 +{
1474 + register PredictorType *ptr = table->predictors;
1475 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS_TXT;
1476 + while (ptr < end) {
1477 + *(ptr++) = 0;
1478 + }
1479 +}
1480 +
1481 +static void predictortable_updateTXT(PredictorTable * table, TokenType token)
1482 +{ //TODO: modify
1483 + register PredictorType *ptr = table->predictors;
1484 +// register u32 ndx;
1485 + ptr[0] = token;
1486 + if ((('a' <= token) && (token <= 'z')) || (('A' <= token) && (token <= 'Z'))) {
1487 + ++(ptr[1]);
1488 + }
1489 + else {
1490 + ptr[1] = 0;
1491 + }
1492 +}
1493 +
1494 +static PredictorType predictortable_minvalueTXT(PredictorTable * table, u32 index)
1495 +{
1496 + return 0;
1497 +}
1498 +
1499 +static PredictorType predictortable_maxvalueTXT(PredictorTable * table, u32 index)
1500 +{ //TODO: modify
1501 + return 254;
1502 +}*/
1503 +
1504 +#endif // __KERNEL__
1505 +
1506 +//ORIGIN: DataStructures/src/ipack_model.c
1507 +
1508 +/*******************************************************************************
1509 +* FILE: ipack_model.c
1510 +* AUTHOR: Tamás Gergely
1511 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1512 +*******************************************************************************/
1513 +
1514 +//#include "DataStructures/ipack_model.h"
1515 +//#include "measuredef.h"
1516 +//#include "ipack_common.h"
1517 +
1518 +#ifdef __MEASURE_TIME_MODEL_GETPROB
1519 +#define __MT_P_MAX 256
1520 +#define __MT_P_DIV 128
1521 +#define __MT_P_MIN 0
1522 +#endif
1523 +
1524 +static void ipack_model_get_probability_for_token(ipack_nodetype * tmp, TokenType token, ipack_probability_type * prob)
1525 +{
1526 +// register ipack_nodetype* tmp = model->tree_root_ptr;
1527 +// register ipack_nodetype* tmp = root;
1528 + while (TREENODETYPE_IS_NODE(tmp->type)) {
1529 + if (tmp->type == TREENODETYPE_NODE_BINARY_EQ) {
1530 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_EQ tmp->data.nodeBin.value) {
1531 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1532 + }
1533 + else {
1534 + tmp = tmp->data.nodeBin.right_child_ptr;
1535 + }
1536 + }
1537 + else if (tmp->type == TREENODETYPE_NODE_BINARY_LT) {
1538 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_LT tmp->data.nodeBin.value) {
1539 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1540 + }
1541 + else {
1542 + tmp = tmp->data.nodeBin.right_child_ptr;
1543 + }
1544 + }
1545 + }
1546 + prob->high = 0;
1547 + prob->low = 0;
1548 +// prob->max = 0;
1549 + if (tmp->type == TREENODETYPE_LEAF_P) {
1550 + if (token) {
1551 + prob->low = tmp->data.leafP.probabilities[token - 1];
1552 + }
1553 + prob->high = tmp->data.leafP.probabilities[token];
1554 +// prob->max = tmp->data.leafP.probabilities[15];
1555 + }
1556 + else if (tmp->type == TREENODETYPE_LEAF_C) {
1557 + if (tmp->data.leafC.predicted_class == token) {
1558 + prob->high = TOKEN_MAXVALUE;
1559 +// prob->max = TOKEN_MAXVALUE;
1560 + }
1561 + }
1562 +}
1563 +
1564 +#ifndef IPACK_ARM_ASM
1565 +
1566 +//return ipack_model_get_token_for_range2(tmp,value,range,prob);
1567 +
1568 +static TokenType ipack_model_get_token_for_range(ipack_nodetype * tmp, u32 value, u32 range, ipack_probability_type * prob)
1569 +{
1570 +// register ipack_nodetype* tmp = model->tree_root_ptr;
1571 +// register ipack_nodetype* tmp = root;
1572 + register TokenType token = 0;
1573 + while (TREENODETYPE_IS_NODE(tmp->type)) {
1574 + if (tmp->type == TREENODETYPE_NODE_BINARY_EQ) {
1575 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_EQ tmp->data.nodeBin.value) {
1576 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1577 + }
1578 + else {
1579 + tmp = tmp->data.nodeBin.right_child_ptr;
1580 + }
1581 + }
1582 + else if (tmp->type == TREENODETYPE_NODE_BINARY_LT) {
1583 + if (*(tmp->data.nodeBin.attribute_ptr) TREE_SUBTREE_RELATION_LEFT_LT tmp->data.nodeBin.value) {
1584 + ((char *) tmp) += sizeof(tmp->type) + sizeof(ipack_treenodeBin);
1585 + }
1586 + else {
1587 + tmp = tmp->data.nodeBin.right_child_ptr;
1588 + }
1589 + }
1590 + }
1591 + prob->high = 0;
1592 + prob->low = 0;
1593 +// prob->max = 0;
1594 + if (tmp->type == TREENODETYPE_LEAF_P) {
1595 + u32 i;
1596 + u32 norm;
1597 +// prob->max = tmp->data.leafP.probabilities[15];
1598 +/* norm = (value * prob->max -1)/range;
1599 + for(i = 0; i < 15; ++i) {
1600 + if(tmp->data.leafP.probabilities[i] > norm) {
1601 + break;
1602 + }
1603 + }*/
1604 + norm = ((value << PROBABILITY_SHIFT) - 1);
1605 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1606 + if (range * tmp->data.leafP.probabilities[i] > norm) {
1607 + break;
1608 + }
1609 + }
1610 + token = (TokenType) i;
1611 + prob->high = tmp->data.leafP.probabilities[i];
1612 + if (token) {
1613 + prob->low = tmp->data.leafP.probabilities[token - 1];
1614 + }
1615 + }
1616 + else if (tmp->type == TREENODETYPE_LEAF_C) {
1617 + token = tmp->data.leafC.predicted_class;
1618 + prob->high = PROBABILITY_MAX;
1619 +// prob->max = PROBABILITY_MAX;
1620 + }
1621 + return token;
1622 +}
1623 +#endif
1624 +/*
1625 +void ipack_model_predictortable_reset(PredictorType* ptr)
1626 +{
1627 +// register PredictorType* ptr = model->predictors_ptr;
1628 +// register PredictorType* ptr = preds;
1629 + register PredictorType* end = ptr + NUMBER_OF_PREDICTORS;
1630 + while(ptr < end) {
1631 + *(ptr++) = 0;
1632 + }
1633 +}
1634 +
1635 +void ipack_model_predictortable_update(PredictorType* ptr, TokenType token)
1636 +{
1637 +// register PredictorType* ptr = model->predictors_ptr;
1638 +// register PredictorType* ptr = preds;
1639 + register u32 ndx = ptr[0] + 1;
1640 + ptr[ndx + 8] = ptr[ndx];
1641 + ptr[ndx] = token;
1642 + if(ndx == 8) {
1643 + ptr[0] = 0;
1644 + } else {
1645 + ++ ptr[0];
1646 + }
1647 +}*/
1648 +/****************************************************************************/
1649 +
1650 +#ifndef __KERNEL__
1651 +static void ipack_model_countpreds(void *ptr, ipack_nodetype * node, double *table, double val)
1652 +{
1653 + if ((node->type == TREENODETYPE_NODE_BINARY_EQ) || (node->type == TREENODETYPE_NODE_BINARY_LT)) {
1654 + table[(u32) (node->data.nodeBin.attribute_ptr) - (u32) (ptr)] += val;
1655 + ipack_model_countpreds(ptr, (void *) (((u8 *) (node)) + sizeof(node->type) + sizeof(ipack_treenodeBin)), table, val / 2);
1656 + ipack_model_countpreds(ptr, node->data.nodeBin.right_child_ptr, table, val / 2);
1657 + }
1658 + else {
1659 + }
1660 +}
1661 +
1662 +/*static void ipack_model_printinfo(ipack_model_type * model)
1663 +{
1664 + double *prcnt = jffs2_bbc_malloc(sizeof(double) * NUMBER_OF_PREDICTORS);
1665 + u32 i;
1666 + for (i = 0; i < NUMBER_OF_PREDICTORS; i++) {
1667 + prcnt[i] = 0.0;
1668 + }
1669 + ipack_model_countpreds(model->predictors_ptr, model->tree_root_ptr, prcnt, 100);
1670 + for (i = 0; i < NUMBER_OF_PREDICTORS; i++) {
1671 + jffs2_bbc_print3(" p[%3d] = %10.6lf\n", (int) i, prcnt[i]);
1672 + }
1673 + jffs2_bbc_free(prcnt);
1674 +}*/
1675 +
1676 +static void ipack_dumpnode(unsigned char **ptr, FILE * file, char *prefs)
1677 +{
1678 + switch (*((*ptr)++)) {
1679 + u32 i;
1680 + u32 j;
1681 + u32 x;
1682 + u32 y;
1683 + case TREENODETYPE_NODE_BINARY_EQ:
1684 + x = *((*ptr)++);
1685 + y = *((*ptr)++);
1686 + fprintf(file, "%s+->\tBinary node: P[%u] equals %u\n", prefs, (unsigned int)x, (unsigned int)y);
1687 + for (j = 0; j < 4096 && prefs[j]; ++j);
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_NODE_BINARY_LT:
1696 + x = *((*ptr)++);
1697 + y = *((*ptr)++);
1698 + fprintf(file, "%s+->\tBinary node: P[%u] greater than %u\n", prefs, (unsigned int)x, (unsigned int)y);
1699 + for (j = 0; j < 4096 && prefs[j]; ++j);
1700 + prefs[j] = '\t';
1701 + prefs[++j] = '|';
1702 + ipack_dumpnode(ptr, file, prefs);
1703 + prefs[j--] = 0;
1704 + ipack_dumpnode(ptr, file, prefs);
1705 + prefs[j] = 0;
1706 + break;
1707 + case TREENODETYPE_LEAF_P:
1708 + x = *((*ptr)++);
1709 + fprintf(file, "%s+->\tLeaf: %u pairs\n", prefs, (unsigned int)x);
1710 + (*ptr) += (x << 1);
1711 + break;
1712 + case TREENODETYPE_LEAF_C:
1713 + x = *((*ptr)++);
1714 + fprintf(file, "%s+->\tLeaf: class %u\n", prefs, (unsigned int)x);
1715 + break;
1716 + default:
1717 + fprintf(file, "%s+->\tLeaf: nullnode\n", prefs);
1718 + }
1719 +}
1720 +
1721 +/*static void ipack_dumpmodel(void *model)
1722 +{
1723 + unsigned char *tmp_ptr = model;
1724 + FILE *file;
1725 + char C[4096];
1726 + if ((file = fopen("DUMPED_MODEL", "wa"))) {
1727 + int i;
1728 + for (i = 0; i < 4096; C[i++] = 0);
1729 + tmp_ptr += 8;
1730 + tmp_ptr += sizeof(u32);
1731 + ipack_dumpnode(&tmp_ptr, file, C);
1732 + fclose(file);
1733 + }
1734 +}*/
1735 +
1736 +#endif
1737 +
1738 +//ORIGIN: Builders/src/PredictorGenerator.c
1739 +
1740 +/*******************************************************************************
1741 +* FILE: PredictorGenerator.c
1742 +* AUTHOR: Tamás Gergely
1743 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1744 +*******************************************************************************/
1745 +
1746 +//#include "ipack_common.h"
1747 +//#include "Builders/PredictorGenerator.h"
1748 +
1749 +#ifdef JFFS2_BBC_ARMLIB_MODELGEN
1750 +static PredictorTable *predictorgenerator_generate( /*PredictorGeneratorSettings* settings */ )
1751 +{
1752 + PredictorTable *ptr = jffs2_bbc_malloc(sizeof(PredictorTable));
1753 + predictortable_clear(ptr);
1754 + ptr->predictors = jffs2_bbc_malloc(NUMBER_OF_PREDICTORS * sizeof(PredictorType));
1755 + return ptr;
1756 +}
1757 +#endif
1758 +
1759 +//ORIGIN: Builders/src/ipack_armlib_compressor.c
1760 +
1761 +/*******************************************************************************
1762 +* FILE: ipack_armlim_compressor.c
1763 +* AUTHOR: Tamás Gergely
1764 +* MODIFIED: $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
1765 +*******************************************************************************/
1766 +
1767 +//#include "ipack_common.h"
1768 +//#include "DataStructures.h"
1769 +//#include "Builders/PredictorGenerator.h"
1770 +//#include "Builders/Tokenizer.h"
1771 +//#include "Builders/Coder.h"
1772 +
1773 +#define EC_NO_ERROR 0
1774 +#define EC_NOT_IPMF_FILE -1
1775 +#define EC_NOT_IPMF_MODEL -2
1776 +#define EC_NOT_HG_BLOCK -3
1777 +#define EC_WRONG_INPUT_LENGTH -501
1778 +#define EC_CODER_WRONG_PROBABILITY 1
1779 +#define EC_CODER_WRONG_RANGE 2
1780 +#define EC_BUFFER_OVERFLOW 501
1781 +#define EC_BUFFER_UNDERFLOW 502
1782 +#define EC_UNKNOWN_TOKEN_TYPE 1001
1783 +#define EC_UNKNOWN_FILTER 1002
1784 +#define EC_UNKNOWN_CONVERTER 1003
1785 +#define EC_UNKNOWN_MANIPULATOR 1004
1786 +
1787 +/*******************************************************************************
1788 +
1789 + COMPRESSOR INIT FUNCTIONS
1790 +
1791 +*******************************************************************************/
1792 +
1793 +#define ROUND_UP_TO_DWORD(val) ( ( (val) + 3 ) & 0xfffffffc )
1794 +
1795 +#ifndef __KERNEL__
1796 +int ipack_glb_endian_X;
1797 +#endif
1798 +
1799 +static int ipack_compressor_init_tree(unsigned char **ptr, ipack_model_type * model, ipack_nodetype * node, void *nullnode)
1800 +{
1801 + int retval = 0;
1802 + node->type = *((*ptr)++);
1803 + switch (node->type) {
1804 + u32 i;
1805 + u32 j;
1806 + u32 lngth;
1807 + u32 tmpret;
1808 + TokenType at;
1809 + u16 av;
1810 + case TREENODETYPE_NODE_BINARY_EQ:
1811 + case TREENODETYPE_NODE_BINARY_LT:
1812 + node->data.nodeBin.attribute_ptr = (model->predictors_ptr) + (*((*ptr)++));
1813 + node->data.nodeBin.value = *((*ptr)++);
1814 + retval = sizeof(node->data.nodeBin);
1815 + retval += ipack_compressor_init_tree(ptr, model, (void *) ROUND_UP_TO_DWORD(((u32) node) + sizeof(node->type) + sizeof(node->data.nodeBin)), nullnode);
1816 + node->data.nodeBin.right_child_ptr = (void *) ROUND_UP_TO_DWORD(((u32) node) + retval + sizeof(node->type));
1817 + retval += ipack_compressor_init_tree(ptr, model, node->data.nodeBin.right_child_ptr, nullnode);
1818 + break;
1819 + case TREENODETYPE_LEAF_P:
1820 + lngth = *((*ptr)++);
1821 + av = 0;
1822 + for (i = 0, j = 0; i < lngth; ++i) {
1823 + at = *((*ptr)++);
1824 + while (j < at) {
1825 + node->data.leafP.probabilities[j++] = av;
1826 + }
1827 + av += *((*ptr)++);
1828 + }
1829 + while (j < NUMBER_OF_TOKENS) {
1830 + node->data.leafP.probabilities[j++] = av;
1831 + }
1832 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1833 + node->data.leafP.probabilities[i] = ((node->data.leafP.probabilities[i] << PROBABILITY_SHIFT) / node->data.leafP.probabilities[NUMBER_OF_TOKENS - 1]);
1834 + }
1835 + retval = ROUND_UP_TO_DWORD(NUMBER_OF_TOKENS * sizeof(u16));
1836 + break;
1837 + case TREENODETYPE_LEAF_C:
1838 + node->data.leafC.predicted_class = *((*ptr)++);
1839 + retval = sizeof(node->data.leafC);
1840 + retval = ROUND_UP_TO_DWORD(retval);
1841 + break;
1842 + default:
1843 + return 0;
1844 + }
1845 + return retval + sizeof(node->type);
1846 +}
1847 +
1848 +#define IPACK_TREE_CONVERT_REPLACE 0
1849 +#define IPACK_TREE_CONVERT_KEEP 1
1850 +
1851 +static void *ipack_tree_to_code(ipack_model_type * model, int *code_size);
1852 +
1853 +static int ipack_armlib_convert_tree_to_code(ipack_model_type * model_img, int mode)
1854 +{
1855 +#ifdef IPACK_TREE_TO_CODE
1856 + int tree_size;
1857 +
1858 + model_img->tree_code = ipack_tree_to_code(model_img, &tree_size);
1859 + jffs2_bbc_print2("Convertation done. Code size=%d\n", tree_size);
1860 + if (mode == IPACK_TREE_CONVERT_REPLACE) {
1861 + jffs2_bbc_print1("Freeing original tree.\n");
1862 + jffs2_bbc_free(model_img->tree_root_ptr);
1863 + model_img->tree_root_ptr = NULL;
1864 + }
1865 +#endif
1866 + return 0;
1867 +}
1868 +
1869 +
1870 +static int ipack_armlib_compressor_init(void **model)
1871 +{
1872 + int retval = EC_NO_ERROR;
1873 + unsigned char *tmp_ptr = *model;
1874 + u32 i;
1875 + ipack_model_type *model_img;
1876 + char tmp_c[2];
1877 +
1878 + if (*(tmp_ptr++) != 'i') {
1879 + return EC_NOT_IPMF_FILE;
1880 + }
1881 + else if (*(tmp_ptr++) != 'P') {
1882 + return EC_NOT_IPMF_FILE;
1883 + }
1884 + else if (*(tmp_ptr++) != 'M') {
1885 + return EC_NOT_IPMF_FILE;
1886 + }
1887 + else if (*(tmp_ptr++) != 'F') {
1888 + return EC_NOT_IPMF_FILE;
1889 + }
1890 + tmp_c[0] = *(tmp_ptr++);
1891 + tmp_c[1] = *(tmp_ptr++);
1892 + tmp_ptr += 2;
1893 +
1894 + //model_img = jffs2_bbc_malloc(*((u32*)tmp_ptr));
1895 + model_img = jffs2_bbc_malloc(sizeof(ipack_model_type) + ROUND_UP_TO_DWORD(NUMBER_OF_PREDICTORS));
1896 + model_img->tree_root_ptr = jffs2_bbc_malloc(*((u32 *) tmp_ptr)); //it is smaller a little but, but...
1897 +
1898 + tmp_ptr += sizeof(u32);
1899 +
1900 + model_img->ID[0] = 'i';
1901 + model_img->ID[1] = 'P';
1902 + model_img->ID[2] = 'M';
1903 + model_img->ID[3] = 'F';
1904 +
1905 + model_img->block_sign[0] = tmp_c[0];
1906 + model_img->block_sign[1] = tmp_c[1];
1907 +
1908 + model_img->nullnode.type = TREENODETYPE_LEAF_P;
1909 + for (i = 0; i < NUMBER_OF_TOKENS; ++i) {
1910 + model_img->nullnode.probabilities[i] = 0;
1911 + }
1912 + model_img->predictors_ptr = (void *) (((u32) model_img) + sizeof(ipack_model_type));
1913 + //model_img->tree_root_ptr = (void*)ROUND_UP_TO_DWORD(((u32)(model_img->predictors_ptr)) + NUMBER_OF_PREDICTORS);//ALIGN
1914 +
1915 + ipack_compressor_init_tree(&tmp_ptr, model_img, model_img->tree_root_ptr, &(model_img->nullnode));
1916 +
1917 +#ifdef IPACK_TREE_TO_CODE
1918 +#ifdef IPACK_AUTO_TREE_TO_CODE
1919 + jffs2_bbc_print1("Automatically converting tree to ARM code...\n");
1920 + ipack_armlib_convert_tree_to_code(model_img, IPACK_TREE_CONVERT_REPLACE);
1921 +#else
1922 + model_img->tree_code = NULL;
1923 +#endif
1924 +#else
1925 + model_img->tree_code = NULL;
1926 +#endif
1927 +
1928 + jffs2_bbc_free(*model);
1929 + *model = model_img;
1930 + return retval;
1931 +}
1932 +
1933 +/*******************************************************************************
1934 +
1935 + COMPRESSOR DEINIT FUNCTIONS
1936 +
1937 +*******************************************************************************/
1938 +
1939 +
1940 +/* Descructor of compressor (model will be freed with jffs2_bbc_free() after it)*/
1941 +static void ipack_armlib_compressor_deinit(void)
1942 +{
1943 +}
1944 +
1945 +/*******************************************************************************
1946 +
1947 + COMPRESS FUNCTIONS
1948 +
1949 +*******************************************************************************/
1950 +
1951 +static int writebits0(unsigned char **dest, u8 * freebits, u32 * opposite, unsigned char *end)
1952 +{
1953 + if (!(*freebits)) {
1954 + ++(*dest);
1955 + *freebits = 7;
1956 + **dest = 0x00;
1957 + }
1958 + else {
1959 + --(*freebits);
1960 + (**dest) <<= 1;
1961 + }
1962 + if ((*dest == end) && !(*freebits)) {
1963 + return EC_BUFFER_OVERFLOW;
1964 + }
1965 + while (*opposite) {
1966 + --(*opposite);
1967 + if (!(*freebits)) {
1968 + ++(*dest);
1969 + *freebits = 7;
1970 + **dest = 0x01;
1971 + }
1972 + else {
1973 + --(*freebits);
1974 + (**dest) <<= 1;
1975 + (**dest) |= 0x01;
1976 + }
1977 + if ((*dest == end) && !(*freebits)) {
1978 + return EC_BUFFER_OVERFLOW;
1979 + }
1980 + }
1981 + return 0;
1982 +}
1983 +
1984 +static int writebits1(unsigned char **dest, u8 * freebits, u32 * opposite, unsigned char *end)
1985 +{
1986 + if (!(*freebits)) {
1987 + ++(*dest);
1988 + *freebits = 7;
1989 + **dest = 0x01;
1990 + }
1991 + else {
1992 + --(*freebits);
1993 + (**dest) <<= 1;
1994 + (**dest) |= 0x01;
1995 + }
1996 + if ((*dest == end) && !(*freebits)) {
1997 + return EC_BUFFER_OVERFLOW;
1998 + }
1999 + while (*opposite) {
2000 + --(*opposite);
2001 + if (!(*freebits)) {
2002 + ++(*dest);
2003 + *freebits = 7;
2004 + **dest = 0x00;
2005 + }
2006 + else {
2007 + --(*freebits);
2008 + (**dest) <<= 1;
2009 + }
2010 + if ((*dest == end) && !(*freebits)) {
2011 + return EC_BUFFER_OVERFLOW;
2012 + }
2013 + }
2014 + return 0;
2015 +}
2016 +
2017 +
2018 +
2019 +
2020 +/* Compress block
2021 + * *dstlen bytes are allocated.
2022 + * if it is not enough write *sourcelen over to the processed amount of data
2023 + * returns non zero if fails
2024 + */
2025 +static int ipack_armlib_compress(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen)
2026 +{
2027 + register u32 coder_high = CODER_VALUEMAX - 1;
2028 + register u32 coder_low = 0;
2029 + u32 coder_opbits = 0;
2030 + u8 bitvector_freebits = 8;
2031 + unsigned char *bitvector_ptr = output;
2032 + unsigned char *bitvector_end = output + (*dstlen - 1);
2033 + ARM_DataType *tmpp;
2034 + TokenStream tmpv;
2035 + TokenType *it;
2036 + void *end_it;
2037 +
2038 + ipack_nodetype *treeroot = ((ipack_model_type *) model)->tree_root_ptr;
2039 + PredictorType *predctrs = ((ipack_model_type *) model)->predictors_ptr;
2040 +
2041 +#ifdef IPACK_TREE_TO_CODE
2042 + void (*treefunc) (ipack_nodetype *, TokenType, ipack_probability_type *);
2043 +
2044 + treefunc = ((ipack_model_type *) model)->tree_code;
2045 + if (treefunc != NULL)
2046 + treefunc += 4;
2047 +#endif
2048 +
2049 + if ((*sourcelen % 4) != 0) {
2050 + return EC_WRONG_INPUT_LENGTH;
2051 + }
2052 + if (*dstlen <= 4) {
2053 + return EC_BUFFER_OVERFLOW;
2054 + }
2055 +
2056 + if (((ipack_model_type *) model)->ID[0] != 'i') {
2057 + return EC_NOT_IPMF_MODEL;
2058 + }
2059 + else if (((ipack_model_type *) model)->ID[1] != 'P') {
2060 + return EC_NOT_IPMF_MODEL;
2061 + }
2062 + else if (((ipack_model_type *) model)->ID[2] != 'M') {
2063 + return EC_NOT_IPMF_MODEL;
2064 + }
2065 + else if (((ipack_model_type *) model)->ID[3] != 'F') {
2066 + return EC_NOT_IPMF_MODEL;
2067 + }
2068 +#ifdef TXT_TOKENS
2069 + tmpv.capacity = (*sourcelen);
2070 +#else
2071 + tmpv.capacity = (*sourcelen) << 1;
2072 +#endif
2073 + tmpv.size = tmpv.capacity;
2074 + tmpv.ptr = jffs2_bbc_malloc(tmpv.size);
2075 + it = tmpv.ptr;
2076 +
2077 +#ifndef __KERNEL__
2078 + if (ipack_glb_endian_X) {
2079 + for (tmpp = (void *) input; (u32) tmpp < (u32) (input + *sourcelen); ++tmpp) {
2080 +#ifdef TXT_TOKENS
2081 + *(it++) = (u8) ((*tmpp & 0xff000000) >> 24);
2082 + *(it++) = (u8) ((*tmpp & 0x00ff0000) >> 16);
2083 + *(it++) = (u8) ((*tmpp & 0x0000ff00) >> 8);
2084 + *(it++) = (u8) ((*tmpp & 0x000000ff));
2085 +#else
2086 + *(it++) = (u8) ((*tmpp & 0x0000f000) >> 12);
2087 + *(it++) = (u8) ((*tmpp & 0x0000000f));
2088 + *(it++) = (u8) ((*tmpp & 0xf0000000) >> 28);
2089 + *(it++) = (u8) ((*tmpp & 0x000f0000) >> 16);
2090 + *(it++) = (u8) ((*tmpp & 0x00000f00) >> 8);
2091 + *(it++) = (u8) ((*tmpp & 0x00f00000) >> 20);
2092 + *(it++) = (u8) ((*tmpp & 0x0f000000) >> 24);
2093 + *(it++) = (u8) ((*tmpp & 0x000000f0) >> 4);
2094 +#endif //TXT_TOKENS
2095 + }
2096 + }
2097 + else {
2098 +#endif
2099 + for (tmpp = (void *) input; (u32) tmpp < (u32) (input + *sourcelen); ++tmpp) {
2100 +#ifdef TXT_TOKENS
2101 + *(it++) = (u8) ((*tmpp & 0x000000ff));
2102 + *(it++) = (u8) ((*tmpp & 0x0000ff00) >> 8);
2103 + *(it++) = (u8) ((*tmpp & 0x00ff0000) >> 16);
2104 + *(it++) = (u8) ((*tmpp & 0xff000000) >> 24);
2105 +#else
2106 + *(it++) = (u8) ((*tmpp & 0x00f00000) >> 20);
2107 + *(it++) = (u8) ((*tmpp & 0x0f000000) >> 24);
2108 + *(it++) = (u8) ((*tmpp & 0x000000f0) >> 4);
2109 + *(it++) = (u8) ((*tmpp & 0x00000f00) >> 8);
2110 + *(it++) = (u8) ((*tmpp & 0x000f0000) >> 16);
2111 + *(it++) = (u8) ((*tmpp & 0x0000f000) >> 12);
2112 + *(it++) = (u8) ((*tmpp & 0x0000000f));
2113 + *(it++) = (u8) ((*tmpp & 0xf0000000) >> 28);
2114 +#endif //TXT_TOKENS
2115 + }
2116 +#ifndef __KERNEL__
2117 + }
2118 +#endif
2119 +/*
2120 + ENCODE
2121 +*/
2122 + { //predictor reset
2123 + register PredictorType *ptr = predctrs;
2124 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS;
2125 + while (ptr < end) {
2126 + *(ptr++) = 0;
2127 + }
2128 + }
2129 +
2130 + //*(bitvector_ptr++) = 'H';
2131 + //*(bitvector_ptr++) = 'G';
2132 + *(bitvector_ptr++) = ((ipack_model_type *) model)->block_sign[0];
2133 + *(bitvector_ptr++) = ((ipack_model_type *) model)->block_sign[1];
2134 +
2135 + *(bitvector_ptr++) = (unsigned char) (((*sourcelen) >> 8) & 0xff);
2136 + *(bitvector_ptr++) = (unsigned char) ((*sourcelen) & 0xff);
2137 + for (it = tmpv.ptr, end_it = VECTOR_S_END(tmpv); it != end_it; ++it) {
2138 + ipack_probability_type prob;
2139 + u32 range;
2140 +
2141 +#ifdef IPACK_TREE_TO_CODE
2142 + if (treefunc != NULL)
2143 + (*treefunc) (treeroot, *it, &prob);
2144 + else
2145 + ipack_model_get_probability_for_token(treeroot, *it, &prob);
2146 +#else
2147 + ipack_model_get_probability_for_token(treeroot, *it, &prob);
2148 +#endif
2149 +
2150 + if (prob.high == prob.low) {
2151 + vector_clear(&tmpv);
2152 + return EC_CODER_WRONG_PROBABILITY;
2153 + }
2154 + range = coder_high - coder_low + 1;
2155 + coder_high = coder_low + ((range * prob.high) >> PROBABILITY_SHIFT) - 1;
2156 + coder_low += ((range * prob.low) >> PROBABILITY_SHIFT);
2157 + for (;;) {
2158 + if (coder_high < CODER_VALUEHLF) {
2159 + if (writebits0(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2160 + vector_clear(&tmpv);
2161 + return EC_BUFFER_OVERFLOW;
2162 + }
2163 + }
2164 + else if (coder_low >= CODER_VALUEHLF) {
2165 + if (writebits1(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2166 + vector_clear(&tmpv);
2167 + return EC_BUFFER_OVERFLOW;
2168 + }
2169 + coder_high -= CODER_VALUEHLF;
2170 + coder_low -= CODER_VALUEHLF;
2171 + }
2172 + else if ((CODER_VALUE1ST <= coder_low) && (coder_high < CODER_VALUE3RD)) {
2173 + ++coder_opbits;
2174 + coder_high -= CODER_VALUE1ST;
2175 + coder_low -= CODER_VALUE1ST;
2176 + }
2177 + else {
2178 + break;
2179 + }
2180 + coder_high <<= 1;
2181 + ++coder_high;
2182 + coder_low <<= 1;
2183 + if (coder_high < coder_low) {
2184 + vector_clear(&tmpv);
2185 + return EC_CODER_WRONG_RANGE;
2186 + }
2187 + }
2188 + {
2189 +#ifdef TXT_TOKENS
2190 +// register u32 ndx;
2191 + predctrs[0] = *it;
2192 + if ((('a' <= *it) && (*it <= 'z')) || (('A' <= *it) && (*it <= 'Z'))) {
2193 + ++(predctrs[1]);
2194 + }
2195 + else {
2196 + predctrs[1] = 0;
2197 + }
2198 +#else
2199 + register u32 ndx = predctrs[0] + 1;
2200 + predctrs[ndx + 8] = predctrs[ndx];
2201 + predctrs[ndx] = *it;
2202 + if (ndx == 8) {
2203 + predctrs[0] = 0;
2204 + }
2205 + else {
2206 + ++predctrs[0];
2207 + }
2208 +#endif
2209 + }
2210 +
2211 + }
2212 + vector_clear(&tmpv);
2213 + ++coder_opbits;
2214 + if (coder_low < CODER_VALUE1ST) {
2215 + if (writebits0(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2216 + return EC_BUFFER_OVERFLOW;
2217 + }
2218 + }
2219 + else {
2220 + if (writebits1(&bitvector_ptr, &bitvector_freebits, &coder_opbits, bitvector_end)) {
2221 + return EC_BUFFER_OVERFLOW;
2222 + }
2223 + }
2224 + (*(bitvector_ptr)) <<= bitvector_freebits;
2225 + *dstlen = ((u32) bitvector_ptr - (u32) output + 1);
2226 + return EC_NO_ERROR;
2227 +}
2228 +
2229 +/*******************************************************************************
2230 +
2231 + DECOMPRESS FUNCTIONS
2232 +
2233 +*******************************************************************************/
2234 +
2235 +typedef struct
2236 +{
2237 + u32 high;
2238 + u32 low;
2239 + u32 value;
2240 + u32 overread;
2241 +} ipack_decompressor_values;
2242 +
2243 +typedef struct
2244 +{
2245 + u8 freebits;
2246 + unsigned char *ptr;
2247 + unsigned char *end;
2248 +} ipack_decompressor_bitvector;
2249 +
2250 +static u8 ipack_bitvector_R_get1(ipack_decompressor_bitvector * bv)
2251 +{
2252 + u8 tmp;
2253 + if (bv->ptr == bv->end) {
2254 + bv->freebits = 0;
2255 + return 0;
2256 + }
2257 + tmp = (*(bv->ptr) >> bv->freebits) & 0x01;
2258 + if (!(bv->freebits)) {
2259 + bv->freebits = 7;
2260 + ++(bv->ptr);
2261 + }
2262 + else {
2263 + --(bv->freebits);
2264 + }
2265 + return tmp;
2266 +}
2267 +
2268 +/* Decompress block
2269 + * returns non zero if fails
2270 + */
2271 +static int ipack_armlib_decompress(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen)
2272 +{
2273 + ARM_DataType *data;
2274 + register u32 coder_high = CODER_VALUEMAX - 1;
2275 + register u32 coder_low = 0;
2276 + register u32 coder_value = 0;
2277 + u32 coder_overread = 0;
2278 + ipack_decompressor_bitvector bitvector;
2279 + u32 lngth;
2280 + u32 i;
2281 + u32 cntbytes;
2282 + TokenType tkns[8];
2283 + TokenType *tptr;
2284 +
2285 + ipack_nodetype *treeroot = ((ipack_model_type *) model)->tree_root_ptr;
2286 + PredictorType *predctrs = ((ipack_model_type *) model)->predictors_ptr;
2287 +
2288 +#ifdef IPACK_TREE_TO_CODE
2289 + TokenType(*treefunc) (ipack_nodetype *, u32, u32, ipack_probability_type *);
2290 +
2291 + treefunc = ((ipack_model_type *) model)->tree_code;
2292 +#endif
2293 +
2294 +
2295 + if (((ipack_model_type *) model)->ID[0] != 'i') {
2296 + return EC_NOT_IPMF_MODEL;
2297 + }
2298 + else if (((ipack_model_type *) model)->ID[1] != 'P') {
2299 + return EC_NOT_IPMF_MODEL;
2300 + }
2301 + else if (((ipack_model_type *) model)->ID[2] != 'M') {
2302 + return EC_NOT_IPMF_MODEL;
2303 + }
2304 + else if (((ipack_model_type *) model)->ID[3] != 'F') {
2305 + return EC_NOT_IPMF_MODEL;
2306 + }
2307 +
2308 + bitvector.freebits = 7;
2309 + bitvector.ptr = input;
2310 + bitvector.end = input + sourcelen;
2311 +
2312 + /*if(*(bitvector.ptr++) != 'H') {
2313 + return EC_NOT_HG_BLOCK;
2314 + } else if(*(bitvector.ptr++) != 'G') {
2315 + return EC_NOT_HG_BLOCK;
2316 + } */
2317 + bitvector.ptr++;
2318 + bitvector.ptr++;
2319 +
2320 + data = (void *) output;
2321 + cntbytes = *(bitvector.ptr++);
2322 + cntbytes <<= 8;
2323 + cntbytes += *(bitvector.ptr++);
2324 +
2325 + { //predictor reset
2326 + register PredictorType *ptr = predctrs;
2327 + register PredictorType *end = ptr + NUMBER_OF_PREDICTORS;
2328 + while (ptr < end) {
2329 + *(ptr++) = 0;
2330 + }
2331 + }
2332 + for (i = 0; i < CODER_VALUEBITS; ++i) {
2333 + coder_value <<= 1;
2334 + coder_value += ipack_bitvector_R_get1(&bitvector);
2335 + }
2336 + lngth = dstlen >> 2;
2337 + if (lngth > (cntbytes >> 2)) {
2338 + lngth = cntbytes >> 2;
2339 + }
2340 + for (i = 0; (i < lngth); ++i) {
2341 + TokenType itoken;
2342 + u32 j;
2343 + tptr = tkns;
2344 + for (j = 0; j < NUMBER_OF_TOKENS_PER_INSTRUCTION; ++j) {
2345 + ipack_probability_type prob;
2346 + u32 range = coder_high - coder_low + 1;
2347 +
2348 +#ifdef IPACK_TREE_TO_CODE
2349 + if (treefunc != NULL)
2350 + itoken = (*treefunc) (treeroot, coder_value - coder_low + 1, range, &prob);
2351 + else
2352 +#endif
2353 + itoken = ipack_model_get_token_for_range(treeroot, coder_value - coder_low + 1, range, &prob);
2354 +
2355 +
2356 + if (prob.high == prob.low) {
2357 + return EC_CODER_WRONG_PROBABILITY;
2358 + }
2359 + coder_high = coder_low + ((range * prob.high) >> PROBABILITY_SHIFT) - 1;
2360 + coder_low += ((range * prob.low) >> PROBABILITY_SHIFT);
2361 + for (;;) {
2362 + if (coder_high < CODER_VALUEHLF) {
2363 + }
2364 + else if (CODER_VALUEHLF <= coder_low) {
2365 + coder_value -= CODER_VALUEHLF;
2366 + coder_high -= CODER_VALUEHLF;
2367 + coder_low -= CODER_VALUEHLF;
2368 + }
2369 + else if ((CODER_VALUE1ST <= coder_low) && (coder_high < CODER_VALUE3RD)) {
2370 + coder_value -= CODER_VALUE1ST;
2371 + coder_high -= CODER_VALUE1ST;
2372 + coder_low -= CODER_VALUE1ST;
2373 + }
2374 + else {
2375 + break;
2376 + }
2377 + coder_low <<= 1;
2378 + coder_high <<= 1;
2379 + ++(coder_high);
2380 + coder_value <<= 1;
2381 + if (bitvector.ptr == bitvector.end) {
2382 + bitvector.freebits = 0;
2383 + }
2384 + coder_value += ((*(bitvector.ptr) >> bitvector.freebits) & 0x01);
2385 + if (bitvector.freebits) {
2386 + --bitvector.freebits;
2387 + }
2388 + else {
2389 + bitvector.freebits = 7;
2390 + ++bitvector.ptr;
2391 + }
2392 + if (coder_high < coder_low) {
2393 + return EC_CODER_WRONG_RANGE;
2394 + }
2395 + if ((bitvector.ptr == bitvector.end) && !(bitvector.freebits)) {
2396 + if ((coder_overread++) > CODER_VALUEBITS) {
2397 + return EC_BUFFER_UNDERFLOW;
2398 + }
2399 + }
2400 + }
2401 + {
2402 +#ifdef TXT_TOKENS
2403 +// register u32 ndx;
2404 + predctrs[0] = itoken;
2405 + if ((('a' <= itoken) && (itoken <= 'z')) || (('A' <= itoken) && (itoken <= 'Z'))) {
2406 + ++(predctrs[1]);
2407 + }
2408 + else {
2409 + predctrs[1] = 0;
2410 + }
2411 +
2412 +#else
2413 + register u32 ndx = predctrs[0] + 1;
2414 + predctrs[ndx + 8] = predctrs[ndx];
2415 + predctrs[ndx] = itoken;
2416 + if (ndx == 8) {
2417 + predctrs[0] = 0;
2418 + }
2419 + else {
2420 + ++predctrs[0];
2421 + }
2422 +#endif
2423 + }
2424 +
2425 + (*(tptr++)) = itoken;
2426 + }
2427 + tptr = tkns;
2428 +#ifndef __KERNEL__
2429 + if (ipack_glb_endian_X) {
2430 +#ifdef TXT_TOKENS
2431 + (*data) = ((*tptr) << 24);
2432 + ++tptr;
2433 + (*data) |= ((*tptr) << 16);
2434 + ++tptr;
2435 + (*data) |= ((*tptr) << 8);
2436 + ++tptr;
2437 + (*data) |= (*tptr);
2438 + ++data;
2439 +#else
2440 + (*data) = (((*tptr) & 0xf) << 12);
2441 + ++tptr;
2442 + (*data) |= ((*tptr) & 0xf);
2443 + ++tptr;
2444 + (*data) |= (((*tptr) & 0xf) << 28);
2445 + ++tptr;
2446 + (*data) |= (((*tptr) & 0xf) << 16);
2447 + ++tptr;
2448 + (*data) |= (((*tptr) & 0xf) << 8);
2449 + ++tptr;
2450 + (*data) |= (((*tptr) & 0xf) << 20);
2451 + ++tptr;
2452 + (*data) |= (((*tptr) & 0xf) << 24);
2453 + ++tptr;
2454 + (*data) |= (((*tptr) & 0xf) << 4);
2455 + ++data;
2456 +#endif
2457 + }
2458 + else {
2459 +#endif
2460 +#ifdef TXT_TOKENS
2461 + (*data) = (*tptr);
2462 + ++tptr;
2463 + (*data) |= ((*tptr) << 8);
2464 + ++tptr;
2465 + (*data) |= ((*tptr) << 16);
2466 + ++tptr;
2467 + (*data) |= ((*tptr) << 24);
2468 + ++data;
2469 +#else
2470 + (*data) = (((*tptr) & 0xf) << 20);
2471 + ++tptr;
2472 + (*data) |= (((*tptr) & 0xf) << 24);
2473 + ++tptr;
2474 + (*data) |= (((*tptr) & 0xf) << 4);
2475 + ++tptr;
2476 + (*data) |= (((*tptr) & 0xf) << 8);
2477 + ++tptr;
2478 + (*data) |= (((*tptr) & 0xf) << 16);
2479 + ++tptr;
2480 + (*data) |= (((*tptr) & 0xf) << 12);
2481 + ++tptr;
2482 + (*data) |= ((*tptr) & 0xf);
2483 + ++tptr;
2484 + (*data) |= (((*tptr) & 0xf) << 28);
2485 + ++data;
2486 +#endif
2487 +#ifndef __KERNEL__
2488 + }
2489 +#endif
2490 + }
2491 + return EC_NO_ERROR;
2492 +}
2493 +
2494 +static int ipack_armlib_estimate(void *model, unsigned char *input, unsigned long sourcelen, unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime)
2495 +{
2496 + int i, tmp, tmp2, max, maxi;
2497 + int cnt_cond[] = { 0, 0, 0, 0 };
2498 + int cnt_inst[] = { 0, 0, 0, 0 };
2499 +
2500 + // TODO: make a more precise estimation!!!
2501 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 6;
2502 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 2;
2503 +
2504 + if (sourcelen % 4 != 0) {
2505 + *dstlen = sourcelen;
2506 + return 0;
2507 + }
2508 + for (i = 0; i < sourcelen; i++, input++) {
2509 + tmp2 = tmp = *input;
2510 + tmp = ((tmp) & 0xf0) >> 4;
2511 + tmp2 = tmp2 & 0xf;
2512 + if (tmp == 14)
2513 + cnt_cond[i % 4]++;
2514 + if ((tmp2 == 2) || (tmp2 == 3))
2515 + cnt_inst[i % 4]++;
2516 + }
2517 + maxi = -1;
2518 + max = -1;
2519 + for (i = 0; i < 4; i++)
2520 + if (max < cnt_cond[i]) {
2521 + max = cnt_cond[i];
2522 + maxi = i;
2523 + }
2524 + /*jffs2_bbc_print("armlib_EST: %d/%d : %d/%d %d/%d %d/%d %d/%d",
2525 + cnt_cond[maxi],cnt_inst[maxi],
2526 + cnt_cond[0],cnt_inst[0],
2527 + cnt_cond[1],cnt_inst[1],
2528 + cnt_cond[2],cnt_inst[2],
2529 + cnt_cond[3],cnt_inst[3]); */
2530 +
2531 + if (cnt_cond[maxi] < (sourcelen >> 4)) {
2532 + *dstlen = sourcelen;
2533 + }
2534 + else {
2535 + *dstlen = sourcelen / 3;
2536 + }
2537 +
2538 + return 0;
2539 +}
2540 +
2541 +static char *ipack_armlib_proc_info(void);
2542 +static int ipack_armlib_proc_command(char *command);
2543 +static void ipack_armlib_destroy_model(void **model);
2544 +
2545 +struct jffs2_bbc_compressor_type jffs2_bbc_armlib = {
2546 + "armlib",
2547 + 0x464d5069,
2548 + {0, 0, 0, 0},
2549 + NULL, // init
2550 + ipack_armlib_compressor_init, // init_model
2551 + ipack_armlib_destroy_model, // destroy_model
2552 + ipack_armlib_compressor_deinit, // deinit
2553 + ipack_armlib_compress,
2554 + ipack_armlib_estimate,
2555 + ipack_armlib_decompress,
2556 + ipack_armlib_proc_info,
2557 + ipack_armlib_proc_command
2558 +};
2559 +
2560 +
2561 +static char *ipack_armlib_proc_info()
2562 +{
2563 +#ifdef IPACK_TREE_TO_CODE
2564 +#ifdef IPACK_AUTO_TREE_TO_CODE
2565 + return "automatic tree to code conversion";
2566 +#else
2567 + return "manual tree to code conversion possibility";
2568 +#endif
2569 +#else
2570 + return "tree in memory version";
2571 +#endif
2572 +}
2573 +
2574 +static int ipack_armlib_proc_command(char *command)
2575 +{
2576 + struct jffs2_bbc_model_list_node *model;
2577 + ipack_model_type *armlib_model;
2578 +
2579 + if ((*command == 'g') || (*command == 'G')) {
2580 + jffs2_bbc_print1("Converting tree(s) to ARM code... (keeping original)\n");
2581 + model = jffs2_bbc_armlib.models;
2582 + if (model == NULL)
2583 + jffs2_bbc_print1("no model found!\n");
2584 + while (model != NULL) {
2585 + armlib_model = model->model;
2586 + if (armlib_model == NULL) {
2587 + jffs2_bbc_print1("Error: NULL model!\n");
2588 + }
2589 + else {
2590 + ipack_armlib_convert_tree_to_code(armlib_model, IPACK_TREE_CONVERT_KEEP);
2591 + }
2592 + model = model->next_compr_model;
2593 + }
2594 + }
2595 + else if ((*command == 'r') || (*command == 'R')) {
2596 + jffs2_bbc_print1("Converting tree(s) to ARM code... (deleting original)\n");
2597 + model = jffs2_bbc_armlib.models;
2598 + if (model == NULL)
2599 + jffs2_bbc_print1("no model found!\n");
2600 + while (model != NULL) {
2601 + armlib_model = model->model;
2602 + if (armlib_model == NULL) {
2603 + jffs2_bbc_print1("Error: NULL model!\n");
2604 + }
2605 + else {
2606 + //armlib_model->tree_code = ipack_tree_to_code(armlib_model, &tree_size);
2607 + //jffs2_bbc_print("Convertation done. Code size=%d\n",tree_size);
2608 + ipack_armlib_convert_tree_to_code(armlib_model, IPACK_TREE_CONVERT_REPLACE);
2609 + }
2610 + model = model->next_compr_model;
2611 + }
2612 + }
2613 + else if ((*command == 'c') || (*command == 'C')) {
2614 + jffs2_bbc_print1("Deleting ARM representation of the tree(s)...\n");
2615 + model = jffs2_bbc_armlib.models;
2616 + if (model == NULL)
2617 + jffs2_bbc_print1("no model found!\n");
2618 + while (model != NULL) {
2619 + armlib_model = model->model;
2620 + if (armlib_model == NULL) {
2621 + jffs2_bbc_print1("Error: NULL model!\n");
2622 + }
2623 + else {
2624 + if (armlib_model->tree_code == NULL) {
2625 + jffs2_bbc_print1("already deleted.\n");
2626 + }
2627 + else {
2628 + if (armlib_model->tree_root_ptr == NULL) {
2629 + jffs2_bbc_print1("cannot delete this ARM tree - original tree has deleted\n");
2630 + }
2631 + else {
2632 + jffs2_bbc_print1("deleting...");
2633 + jffs2_bbc_free(armlib_model->tree_code);
2634 + armlib_model->tree_code = NULL;
2635 + jffs2_bbc_print1("done.\n");
2636 + }
2637 + }
2638 + }
2639 + model = model->next_compr_model;
2640 + }
2641 + }
2642 + else if (*command == '?') {
2643 + jffs2_bbc_print1("ARMLIB commands:\n");
2644 + jffs2_bbc_print1(" g: convert TREEs to ARM code and keep the original\n");
2645 + jffs2_bbc_print1(" r: convert TREEs to ARM code and remove the original\n");
2646 + jffs2_bbc_print1(" c: delete the original TREEs - if there is any\n");
2647 + }
2648 + else {
2649 + jffs2_bbc_print1("Unknown command.\n");
2650 + }
2651 + return 0;
2652 +}
2653 +
2654 +static void ipack_armlib_destroy_model(void **model)
2655 +{
2656 + ipack_model_type *model_img;
2657 +
2658 + model_img = *model;
2659 + if (model_img == NULL) {
2660 + jffs2_bbc_print1("jffs2.bbc: armlib: NULL model at destoying model!\n");
2661 + return;
2662 + }
2663 + if (model_img->tree_code != NULL) {
2664 + //jffs2_bbc_print1("jffs2.bbc: armlib: debug: freeing code...\n");
2665 + jffs2_bbc_free(model_img->tree_code);
2666 + model_img->tree_code = NULL;
2667 + }
2668 + if (model_img->tree_root_ptr != NULL) {
2669 + //jffs2_bbc_print1("jffs2.bbc: armlib: debug: freeing tree...\n");
2670 + jffs2_bbc_free(model_img->tree_root_ptr);
2671 + model_img->tree_root_ptr = NULL;
2672 + }
2673 +
2674 + jffs2_bbc_free(model_img);
2675 + *model = NULL;
2676 +}
2677 +
2678 +struct jffs2_bbc_compressor_type *jffs2_bbc_armlib_init(int mode)
2679 +{
2680 + if (jffs2_bbc_register_compressor(&jffs2_bbc_armlib) == 0)
2681 + return &jffs2_bbc_armlib;
2682 + else
2683 + return NULL;
2684 +}
2685 +
2686 +void jffs2_bbc_armlib_deinit(void)
2687 +{
2688 + jffs2_bbc_unregister_compressor(&jffs2_bbc_armlib);
2689 +}
2690 +
2691 +/*END OF ARMLIB*/
2692 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_framework.c
2693 ===================================================================
2694 --- /dev/null
2695 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_framework.c
2696 @@ -0,0 +1,1324 @@
2697 +/*
2698 + * JFFS2-BBC: Compression Framework
2699 + *
2700 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
2701 + *
2702 + * Copyright (C) 2004, Ferenc Havasi
2703 + *
2704 + * This program is free software; you can redistribute it and/or
2705 + * modify it under the terms of the GNU General Public License
2706 + * as published by the Free Software Foundation; either version 2
2707 + * of the License, or (at your option) any later version.
2708 + *
2709 + * This program is distributed in the hope that it will be useful,
2710 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2711 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2712 + * GNU General Public License for more details.
2713 + *
2714 + * You should have received a copy of the GNU General Public License
2715 + * along with this program; if not, write to the Free Software
2716 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2717 + *
2718 + */
2719 +
2720 +/* USE JFFS2_BBC_STANDALONE define if you don't want to compile without JFFS2 */
2721 +
2722 +//#define DEBUG_COMPRESSORS
2723 +//#define DEBUG_SHOW_BLOCK_SIZES
2724 +
2725 +#define JFFS2_BBC_STAT_BUFF_SIZE 8000
2726 +
2727 +#ifndef __KERNEL__
2728 +
2729 +#include <stdio.h>
2730 +#include <malloc.h>
2731 +typedef unsigned long uint32_t;
2732 +
2733 +#else
2734 +
2735 +#include <linux/kernel.h>
2736 +#include <linux/slab.h>
2737 +#include <linux/vmalloc.h>
2738 +
2739 +#endif
2740 +
2741 +#define JFFS2_BBC_ZLIB_BLOCK_SIGN_0 (120)
2742 +#define JFFS2_BBC_ZLIB_BLOCK_SIGN_1 (94)
2743 +
2744 +#define JFFS2_BBC_DUMMY_BLOCKSIGN_0 (0x54)
2745 +#define JFFS2_BBC_DUMMY_BLOCKSIGN_1 (0x01)
2746 +
2747 +#ifndef NULL
2748 +#define NULL ((void*)(0))
2749 +#endif
2750 +
2751 +#include "jffs2_bbc_framework.h"
2752 +
2753 +/*********************************************************************
2754 + * Global data *
2755 + *********************************************************************/
2756 +
2757 +static int jffs2_bbc_compression_mode = JFFS2_BBC_ZLIB_MODE;
2758 +static struct jffs2_bbc_compressor_type *jffs2_bbc_manual_compressor = NULL;
2759 +static struct jffs2_bbc_compressor_type *jffs2_bbc_compressors = NULL;
2760 +static struct jffs2_bbc_model_list_node *jffs2_bbc_model_list = NULL;
2761 +static void *last_sb = NULL; /* previously activated sb */
2762 +
2763 +/*********************************************************************
2764 + * Compressor initialization *
2765 + *********************************************************************/
2766 +
2767 +#ifndef JFFS2_BBC_STANDALONE
2768 +
2769 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2770 +struct jffs2_bbc_compressor_type *jffs2_bbc_armlib_init(void);
2771 +void jffs2_bbc_armlib_deinit(void);
2772 +#endif
2773 +
2774 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2775 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzo_init(void);
2776 +void jffs2_bbc_lzo_deinit(void);
2777 +#endif
2778 +
2779 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2780 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzss_init(void);
2781 +void jffs2_bbc_lzss_deinit(void);
2782 +#endif
2783 +
2784 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2785 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzari_init(void);
2786 +void jffs2_bbc_lzari_deinit(void);
2787 +#endif
2788 +
2789 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2790 +struct jffs2_bbc_compressor_type *jffs2_bbc_lzhd_init(void);
2791 +void jffs2_bbc_lzhd_deinit(void);
2792 +#endif
2793 +
2794 +void jffs2_bbc_compressor_init()
2795 +{
2796 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2797 + jffs2_bbc_armlib_init();
2798 +#endif
2799 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2800 + jffs2_bbc_lzo_init();
2801 +#endif
2802 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2803 + jffs2_bbc_lzss_init();
2804 +#endif
2805 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2806 + jffs2_bbc_lzari_init();
2807 +#endif
2808 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2809 + jffs2_bbc_lzhd_init();
2810 +#endif
2811 +}
2812 +
2813 +void jffs2_bbc_compressor_deinit()
2814 +{
2815 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZHD)
2816 + jffs2_bbc_lzhd_deinit();
2817 +#endif
2818 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZARI)
2819 + jffs2_bbc_lzari_deinit();
2820 +#endif
2821 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZSS)
2822 + jffs2_bbc_lzss_deinit();
2823 +#endif
2824 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_LZO)
2825 + jffs2_bbc_lzo_deinit();
2826 +#endif
2827 +#if !defined(__KERNEL__) || defined(CONFIG_JFFS2_BBC_ARMLIB)
2828 + jffs2_bbc_armlib_deinit();
2829 +#endif
2830 +}
2831 +
2832 +#endif
2833 +
2834 +#ifndef JFFS2_BBC_STANDALONE
2835 +
2836 +/*********************************************************************
2837 + * ZLIB COMPRESSOR *
2838 + *********************************************************************/
2839 +
2840 +extern int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out, uint32_t * sourcelen, uint32_t * dstlen);
2841 +extern void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen);
2842 +
2843 +static int jffs2_bbc_zlib_compress(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen)
2844 +{
2845 + return jffs2_zlib_compress2(input, output, sourcelen, dstlen);
2846 +}
2847 +
2848 +static int jffs2_bbc_zlib_decompress(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen)
2849 +{
2850 + jffs2_zlib_decompress2(input, output, sourcelen, dstlen);
2851 + return 0;
2852 +}
2853 +
2854 +static int jffs2_bbc_zlib_estimate(void *model, unsigned char *input, unsigned long sourcelen, unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime)
2855 +{
2856 + *dstlen = sourcelen * 65 / 100;
2857 + *readtime = JFFS2_BBC_ZLIB_READ_TIME;
2858 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME;
2859 + return 0;
2860 +}
2861 +
2862 +static struct jffs2_bbc_compressor_type jffs2_bbc_zlib = {
2863 + "zlib",
2864 + 0,
2865 + {JFFS2_BBC_ZLIB_BLOCK_SIGN_0, JFFS2_BBC_ZLIB_BLOCK_SIGN_1, 0, 0},
2866 + NULL,
2867 + NULL,
2868 + NULL,
2869 + NULL,
2870 + jffs2_bbc_zlib_compress,
2871 + jffs2_bbc_zlib_estimate,
2872 + jffs2_bbc_zlib_decompress,
2873 + NULL,
2874 + NULL,
2875 + 1
2876 +};
2877 +
2878 +static struct jffs2_bbc_compressor_type *jffs2_bbc_original_compressor = &jffs2_bbc_zlib;
2879 +
2880 +#endif
2881 +
2882 +/*********************************************************************
2883 + * Compression mode handling *
2884 + *********************************************************************/
2885 +
2886 +int jffs2_bbc_get_compression_mode(void)
2887 +{
2888 + return jffs2_bbc_compression_mode;
2889 +}
2890 +
2891 +void jffs2_bbc_set_compression_mode(int mode)
2892 +{
2893 + jffs2_bbc_compression_mode = mode;
2894 +}
2895 +
2896 +void jffs2_bbc_set_manual_compressor(struct jffs2_bbc_compressor_type *c)
2897 +{
2898 + jffs2_bbc_manual_compressor = c;
2899 + jffs2_bbc_set_compression_mode(JFFS2_BBC_MANUAL_MODE);
2900 +}
2901 +
2902 +int jffs2_bbc_set_manual_compressor_by_name(char *name)
2903 +{
2904 + struct jffs2_bbc_compressor_type *l;
2905 + int i;
2906 +
2907 + l = jffs2_bbc_compressors;
2908 + while (l != NULL) {
2909 + for (i = 0; i < 1000; i++) {
2910 + if (l->name[i] == 0) {
2911 + jffs2_bbc_set_manual_compressor(l);
2912 + return 0;
2913 + }
2914 + else if (name[i] == 0)
2915 + i = 1000;
2916 + else if (name[i] != l->name[i])
2917 + i = 1000;
2918 + }
2919 + l = l->next;
2920 + }
2921 + jffs2_bbc_set_manual_compressor(NULL);
2922 + return 1;
2923 +}
2924 +
2925 +static struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_by_name(char *name)
2926 +{
2927 + struct jffs2_bbc_compressor_type *l;
2928 + int i;
2929 +
2930 +#ifndef JFFS2_BBC_STANDALONE
2931 + l = jffs2_bbc_original_compressor;
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 +#endif
2942 +
2943 + l = jffs2_bbc_compressors;
2944 + while (l != NULL) {
2945 + for (i = 0; i < 1000; i++) {
2946 + if (l->name[i] == 0) {
2947 + return l;
2948 + }
2949 + else if (name[i] == 0)
2950 + i = 1000;
2951 + else if (name[i] != l->name[i])
2952 + i = 1000;
2953 + }
2954 + l = l->next;
2955 + }
2956 +
2957 + return NULL;
2958 +}
2959 +
2960 +int jffs2_bbc_disable_compressor_by_name(char *name)
2961 +{
2962 + struct jffs2_bbc_compressor_type *l;
2963 +
2964 + l = jffs2_bbc_get_compressor_by_name(name);
2965 + if (l == NULL) return 1;
2966 + l->enabled = 0;
2967 + return 0;
2968 +}
2969 +
2970 +int jffs2_bbc_enable_compressor_by_name(char *name)
2971 +{
2972 + struct jffs2_bbc_compressor_type *l;
2973 +
2974 + l = jffs2_bbc_get_compressor_by_name(name);
2975 + if (l == NULL) return 1;
2976 + l->enabled = 1;
2977 + return 0;
2978 +}
2979 +
2980 +void jffs2_bbc_compressor_command_by_name(char *name_and_command)
2981 +{
2982 + struct jffs2_bbc_compressor_type *l;
2983 + int i;
2984 +
2985 + l = jffs2_bbc_compressors;
2986 + while (l != NULL) {
2987 + for (i = 0; i < 1000; i++) {
2988 + if (l->name[i] == 0) {
2989 + if (name_and_command[i] != ':') {
2990 + jffs2_bbc_print1("jffs2.bbc: ':' missing after compressor name\n");
2991 + }
2992 + else {
2993 + if (l->proc_command != NULL)
2994 + l->proc_command(name_and_command + i + 1);
2995 + }
2996 + i = 1000;
2997 + return;
2998 + }
2999 + else if (name_and_command[i] == 0) {
3000 + i = 1000;
3001 + }
3002 + else if (name_and_command[i] != l->name[i]) {
3003 + i = 1000;
3004 + }
3005 + }
3006 + l = l->next;
3007 + }
3008 +}
3009 +
3010 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_manual_compressor(void)
3011 +{
3012 + if (jffs2_bbc_get_compression_mode() != JFFS2_BBC_MANUAL_MODE) {
3013 + jffs2_bbc_manual_compressor = NULL;
3014 + }
3015 + return jffs2_bbc_manual_compressor;
3016 +}
3017 +
3018 +/*********************************************************************
3019 + * Compressor handling *
3020 + *********************************************************************/
3021 +
3022 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_list(void)
3023 +{
3024 + return jffs2_bbc_compressors;
3025 +}
3026 +
3027 +struct jffs2_bbc_model_list_node *jffs2_bbc_get_model_list(void)
3028 +{
3029 + return jffs2_bbc_model_list;
3030 +}
3031 +
3032 +int jffs2_bbc_register_compressor(struct jffs2_bbc_compressor_type *c)
3033 +{
3034 + struct jffs2_bbc_compressor_type *l;
3035 + struct jffs2_bbc_model_list_node *l2;
3036 + int model_found = 0;
3037 +
3038 + l = jffs2_bbc_compressors;
3039 + /* Check for confilcts */
3040 + while (l != NULL) {
3041 + c->name[15] = 0;
3042 + /*if (strcmp(c->name,l->name)==0) {
3043 + jffs2_bbc_print1("jffs2.bbc: compressor is already loaded.");
3044 + return -1;
3045 + } */
3046 + if ((l->model_file_sign == c->model_file_sign) && (c->model_file_sign != 0)) {
3047 + jffs2_bbc_print1("jffs2.bbc: already used model file sign. fail.");
3048 + return -1;
3049 + }
3050 + l = l->next;
3051 + }
3052 + /* Search and initialize model */
3053 + c->models = NULL;
3054 + c->mounted = 0;
3055 + if (c->init != NULL) {
3056 + if (c->init() != 0) {
3057 + jffs2_bbc_print2("jffs2.bbc: cannot initialize compressor %s.\n", c->name);
3058 + return -1;
3059 + }
3060 + }
3061 + if (c->model_file_sign != 0) {
3062 + l2 = jffs2_bbc_model_list;
3063 + while (1) {
3064 + if (l2 == NULL)
3065 + break;
3066 + if (c->model_file_sign == l2->sign) {
3067 + if (l2->compressor != NULL) {
3068 + jffs2_bbc_print2("jffs2.bbc: register for %s: BUG, model file already reserved!!!!\n", c->name);
3069 + }
3070 + else {
3071 + if (c->init_model(&(l2->model)) != 0) {
3072 + jffs2_bbc_print2("jffs2.bbc: cannot initialize compressor %s for a model", c->name);
3073 + }
3074 + else {
3075 + l2->compressor = c;
3076 + l2->next_compr_model = c->models;
3077 + c->models = l2;
3078 + c->mounted++;
3079 + model_found++;
3080 + }
3081 + }
3082 + }
3083 + l2 = l2->next_model;
3084 + }
3085 + /*if (model_found==0) {
3086 + jffs2_bbc_print2("jffs2.bbc: no macthing model file found for %s at this time (maybe later)\n",c->name);
3087 + } */
3088 + }
3089 + /* Insert to the end of the compressor list */
3090 + c->enabled = 1;
3091 + c->buffer = NULL;
3092 + c->buffer_size = 0;
3093 + c->stat_compr_orig = c->stat_compr_new = c->stat_decompr = 0;
3094 + c->next = NULL;
3095 + if (jffs2_bbc_compressors == NULL) {
3096 + jffs2_bbc_compressors = c;
3097 + }
3098 + else {
3099 + l = jffs2_bbc_compressors;
3100 + while (l->next != NULL)
3101 + l = l->next;
3102 + l->next = c;
3103 + }
3104 + return 0;
3105 +}
3106 +
3107 +int jffs2_bbc_unregister_compressor(struct jffs2_bbc_compressor_type *c)
3108 +{
3109 + struct jffs2_bbc_compressor_type *l;
3110 + struct jffs2_bbc_model_list_node *l2;
3111 +
3112 + if (c->mounted != 0) {
3113 + jffs2_bbc_print1("jffs2.bbc: Compressor is in use. Sorry.");
3114 + return -1;
3115 + }
3116 + if (jffs2_bbc_compressors == NULL) {
3117 + jffs2_bbc_print1("jffs2.bbc: unregister: empty list.");
3118 + return -1;
3119 + }
3120 + else if (jffs2_bbc_compressors == c) {
3121 + if (c->deinit != NULL)
3122 + c->deinit();
3123 + jffs2_bbc_compressors = c->next;
3124 + }
3125 + else {
3126 + l = jffs2_bbc_compressors;
3127 + while (l->next != c) {
3128 + if (l->next == NULL) {
3129 + jffs2_bbc_print2("jffs2.bbc: unregister: cannot find compressor %s in the list.", c->name);
3130 + return -1;
3131 + }
3132 + l = l->next;
3133 + }
3134 + if (c->deinit != NULL)
3135 + c->deinit();
3136 + l->next = c->next;
3137 + }
3138 + if (c->buffer != NULL) {
3139 + jffs2_bbc_free(c->buffer);
3140 + c->buffer = NULL;
3141 + c->buffer_size = 0;
3142 + }
3143 +
3144 + l2 = jffs2_bbc_model_list;
3145 + while (l2 != NULL) {
3146 + if (l2->compressor == c) {
3147 + jffs2_bbc_print1("jffs2.bbc: unregister: BUG: model found!!!");
3148 + l2->compressor = NULL;
3149 + l2->next_compr_model = NULL;
3150 + }
3151 + l2 = l2->next_model;
3152 + }
3153 +
3154 + return 0;
3155 +}
3156 +
3157 +int jffs2_bbc_model_new(void *sb, int i_num, void *model)
3158 +{
3159 + struct jffs2_bbc_model_list_node *node;
3160 + struct jffs2_bbc_compressor_type *l;
3161 + char block_sign[2];
3162 +
3163 + int sign;
3164 +
3165 + /* check for conflicts... */
3166 + sign = *((int *) model);
3167 + block_sign[0] = *(((char *) model) + 4);
3168 + block_sign[1] = *(((char *) model) + 5);
3169 + node = jffs2_bbc_model_list;
3170 + while (node != NULL) {
3171 + if ((node->block_sign[0] == block_sign[0]) && (node->block_sign[1] == block_sign[1]) && (node->sb == sb)) {
3172 + //jffs2_bbc_print2("jffs2.bbc: model_new: model conflict (inode=%d)!\n",i_num);
3173 + return -1;
3174 + }
3175 + node = node->next_model;
3176 + }
3177 +
3178 + /* insertion */
3179 + node = jffs2_bbc_malloc_small((long)sizeof(struct jffs2_bbc_model_list_node));
3180 + node->sb = sb;
3181 + node->model = model;
3182 + node->sign = *((int *) model);
3183 + node->block_sign[0] = *(((char *) model) + 4);
3184 + node->block_sign[1] = *(((char *) model) + 5);
3185 + node->inode = i_num;
3186 + node->next_model = jffs2_bbc_model_list;
3187 + node->compressor = NULL;
3188 + node->stat_decompr = 0;
3189 + node->next_compr_model = NULL;
3190 + jffs2_bbc_model_list = node;
3191 +
3192 + /* search for matching compressor */
3193 + l = jffs2_bbc_compressors;
3194 + while (l != NULL) {
3195 + if (l->model_file_sign == sign) {
3196 + //jffs2_bbc_print2("jffs2.bbc: compressor for model found: %s ",l->name);
3197 + if (l->init_model(&(node->model)) != 0) {
3198 + jffs2_bbc_print1("jffs2.bbc: cannot initialize compressor for a model");
3199 + }
3200 + else {
3201 + l->mounted++;
3202 + node->compressor = l;
3203 + node->next_compr_model = l->models;
3204 + l->models = node;
3205 + }
3206 + break;
3207 + }
3208 + l = l->next;
3209 + }
3210 + return 0;
3211 +}
3212 +
3213 +static void jffs2_bbc_model_del_from_compressor(struct jffs2_bbc_model_list_node *node)
3214 +{
3215 + struct jffs2_bbc_model_list_node *l;
3216 +
3217 + if (node->model != NULL) {
3218 + if (node->compressor != NULL) {
3219 + if (node->compressor->destroy_model == NULL) {
3220 + jffs2_bbc_free(node->model);
3221 + node->model = NULL;
3222 + }
3223 + else {
3224 + node->compressor->destroy_model(&(node->model));
3225 + if (node->model != NULL)
3226 + jffs2_bbc_print1("jffs2.bbc: warning: not NULL model after destroying!\n");
3227 + }
3228 + }
3229 + }
3230 +
3231 + if (node->compressor == NULL) {
3232 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor: no compressor!\n");
3233 + return;
3234 + }
3235 + l = node->compressor->models;
3236 + if (l == NULL) {
3237 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor error, models=NULL!\n");
3238 + return;
3239 + }
3240 + if (l == node) {
3241 + node->compressor->models = node->next_compr_model;
3242 + node->compressor->mounted--;
3243 + return;
3244 + }
3245 + while (1) {
3246 + if (l->next_compr_model == node) {
3247 + l->next_compr_model = node->next_compr_model;
3248 + node->compressor->mounted--;
3249 + return;
3250 + }
3251 + l = l->next_compr_model;
3252 + if (l == NULL) {
3253 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_del_from_compressor: not found\n");
3254 + return;
3255 + }
3256 + }
3257 +}
3258 +
3259 +void jffs2_bbc_model_del(void *sb)
3260 +{
3261 + struct jffs2_bbc_model_list_node *l, *l2;
3262 +
3263 + l = jffs2_bbc_model_list;
3264 + if (l == NULL)
3265 + return;
3266 + if (l->sb == sb) {
3267 + jffs2_bbc_model_list = l->next_model;
3268 + jffs2_bbc_model_del_from_compressor(l);
3269 + jffs2_bbc_free_small(l);
3270 + jffs2_bbc_model_del(sb);
3271 + return;
3272 + }
3273 + while (1) {
3274 + if (l->next_model == NULL) {
3275 + break;
3276 + }
3277 + if (l->next_model->sb == sb) {
3278 + l2 = l->next_model;
3279 + l->next_model = l->next_model->next_model;
3280 + jffs2_bbc_model_del_from_compressor(l2);
3281 + jffs2_bbc_free_small(l2);
3282 + jffs2_bbc_model_del(sb);
3283 + return;
3284 + }
3285 + l = l->next_model;
3286 + }
3287 + last_sb = NULL;
3288 +}
3289 +
3290 +void jffs2_bbc_model_set_act_sb(void *sb)
3291 +{
3292 + last_sb = sb;
3293 +}
3294 +
3295 +void *jffs2_bbc_model_get_act_sb(void)
3296 +{
3297 + return last_sb;
3298 +}
3299 +
3300 +void *jffs2_bbc_model_get_newest(struct jffs2_bbc_compressor_type *compressor)
3301 +{
3302 + struct jffs2_bbc_model_list_node *m, *best_m;
3303 + int max_sign, sign;
3304 +
3305 + if (compressor == NULL) {
3306 + jffs2_bbc_print1("jffs2.bbc: jffs2_bbc_model_get: NULL!!\n");
3307 + return NULL;
3308 + }
3309 +
3310 + best_m = NULL;
3311 + max_sign = -1;
3312 + m = compressor->models;
3313 + while (m != NULL) {
3314 + if (m->sb == last_sb) {
3315 + sign = (int) (m->block_sign[0]) * 256 + (int) (m->block_sign[1]);
3316 + if (sign > max_sign) {
3317 + max_sign = sign;
3318 + best_m = m;
3319 + }
3320 + }
3321 + m = m->next_compr_model;
3322 + }
3323 + if (best_m != NULL)
3324 + return best_m->model;
3325 + else
3326 + return NULL;
3327 +}
3328 +
3329 +/*********************************************************************
3330 + * Statistics *
3331 + *********************************************************************/
3332 +
3333 +static char *jffs2_bbc_stat_buff = NULL;
3334 +
3335 +char *jffs2_bbc_get_model_stats(void)
3336 +{
3337 + char *b;
3338 + struct jffs2_bbc_model_list_node *m;
3339 + struct jffs2_bbc_compressor_type *c;
3340 +
3341 + if (jffs2_bbc_stat_buff == NULL)
3342 + jffs2_bbc_stat_buff = jffs2_bbc_malloc(8000);
3343 +
3344 + b = jffs2_bbc_stat_buff;
3345 +
3346 + b += sprintf(b, "Loaded compressors:");
3347 + c = jffs2_bbc_compressors;
3348 + while (c != NULL) {
3349 + b += sprintf(b, "\n %s (%d) ", c->name, c->enabled);
3350 + if (c->model_file_sign != 0) {
3351 + b += sprintf(b, "m_sign=%d ", c->model_file_sign);
3352 + b += sprintf(b, "models=");
3353 + m = c->models;
3354 + while (m != NULL) {
3355 + b += sprintf(b, "(inode=%d)", m->inode);
3356 + m = m->next_compr_model;
3357 + }
3358 + }
3359 + else {
3360 + b += sprintf(b, "b_sign=(%d,%d) nomodel", (int) (c->block_sign[0]), (int) (c->block_sign[1]));
3361 + }
3362 + if (c->proc_info != NULL) {
3363 + b += sprintf(b, "\n %s", c->proc_info());
3364 + }
3365 + c = c->next;
3366 + }
3367 +
3368 + m = jffs2_bbc_model_list;
3369 +
3370 + if (m == NULL) {
3371 + b += sprintf(b, "\nPresent models: NONE\n");
3372 + }
3373 + else {
3374 + b += sprintf(b, "\nPresent models:\n");
3375 + while (m != NULL) {
3376 + b += sprintf(b, " b_sign=(%d,%d),inode=%d,decompr=%d", (int) (m->block_sign[0]), (int) (m->block_sign[1]), m->inode, m->stat_decompr);
3377 + if (m->compressor == NULL)
3378 + b += sprintf(b, ",compressor=NULL\n");
3379 + else
3380 + b += sprintf(b, ",compressor=%s\n", m->compressor->name);
3381 + m = m->next_model;
3382 + }
3383 + }
3384 +
3385 + return jffs2_bbc_stat_buff;
3386 +}
3387 +
3388 +/*********************************************************************
3389 + * Memory handling, debug *
3390 + *********************************************************************/
3391 +
3392 +static int jffs2_bbc_mem_counter = 0;
3393 +
3394 +#ifdef __KERNEL__
3395 +
3396 +void *jffs2_bbc_malloc(long size)
3397 +{
3398 + void *addr = vmalloc(size);
3399 + if (addr != NULL)
3400 + jffs2_bbc_mem_counter++;
3401 + else {
3402 + jffs2_bbc_print2("DEBUG: not enough memory (%ld)\n", size);
3403 + }
3404 + return addr;
3405 +}
3406 +
3407 +void jffs2_bbc_free(void *addr)
3408 +{
3409 + jffs2_bbc_mem_counter--;
3410 + vfree(addr);
3411 +}
3412 +
3413 +void *jffs2_bbc_malloc_small(long size)
3414 +{
3415 + void *addr;
3416 + addr = kmalloc(size, 0);
3417 + if (addr != NULL)
3418 + jffs2_bbc_mem_counter++;
3419 + return addr;
3420 +}
3421 +
3422 +void jffs2_bbc_free_small(void *addr)
3423 +{
3424 + jffs2_bbc_mem_counter--;
3425 + kfree(addr);
3426 +}
3427 +
3428 +#else
3429 +
3430 +void *jffs2_bbc_malloc(long size)
3431 +{
3432 + void *addr = malloc(size);
3433 + if (addr != NULL)
3434 + jffs2_bbc_mem_counter++;
3435 + return addr;
3436 +}
3437 +
3438 +void jffs2_bbc_free(void *addr)
3439 +{
3440 + jffs2_bbc_mem_counter--;
3441 + free(addr);
3442 +}
3443 +
3444 +void *jffs2_bbc_malloc_small(long size)
3445 +{
3446 + return jffs2_bbc_malloc(size);
3447 +}
3448 +
3449 +void jffs2_bbc_free_small(void *addr)
3450 +{
3451 + jffs2_bbc_free(addr);
3452 +}
3453 +
3454 +#endif
3455 +
3456 +int jffs2_bbc_test_memory_counter(int verbose)
3457 +{
3458 + if (verbose > 0) {
3459 + jffs2_bbc_print2("jffs2.bbc: mem_counter=%d!\n", jffs2_bbc_mem_counter);
3460 + }
3461 + return jffs2_bbc_mem_counter;
3462 +}
3463 +
3464 +int jffs2_bbc_get_memory_counter(void)
3465 +{
3466 + return jffs2_bbc_mem_counter;
3467 +}
3468 +
3469 +static char mem_stat[200];
3470 +
3471 +char *jffs2_bbc_get_mem_stats(void)
3472 +{
3473 + sprintf(mem_stat, "Memcounter=%d\n", jffs2_bbc_mem_counter);
3474 + return mem_stat;
3475 +}
3476 +
3477 +void jffs2_bbc_print_flush(void)
3478 +{
3479 +#ifdef __KERNEL__
3480 + return;
3481 +#else
3482 + fflush(stdout);
3483 + fflush(stderr);
3484 +#endif
3485 +}
3486 +
3487 +/*********************************************************************
3488 + * FRAMEWORK - ZLIB REPLACEMENT *
3489 + *********************************************************************/
3490 +
3491 +#ifndef JFFS2_BBC_STANDALONE
3492 +
3493 +/* Temorary buffers */
3494 +static char stat_str[JFFS2_BBC_STAT_BUFF_SIZE];
3495 +static int tmp_buffer_size = 0;
3496 +static char *tmp_buffer = NULL;
3497 +
3498 +/* Statistic - used by /proc/jffs2_bbc and mkfs.jffs2 */
3499 +char *jffs2_bbc_get_compr_stats(void)
3500 +{
3501 + struct jffs2_bbc_compressor_type *l;
3502 + char *s = stat_str;
3503 +
3504 + s += sprintf(s, "Compression statistics:\n");
3505 + l = jffs2_bbc_original_compressor;
3506 + //s += sprintf(s, " zlib: compr=%d/%d decompr=%d\n", stat_zlib_compr_new, stat_zlib_compr_orig, stat_zlib_decompr);
3507 + s += sprintf(s, " %s: compr=%d/%d decompr=%d\n", l->name, l->stat_compr_new, l->stat_compr_orig, l->stat_decompr);
3508 + l = jffs2_bbc_get_compressor_list();
3509 + while (l != NULL) {
3510 + s += sprintf(s, " %s: compr=%d/%d decompr=%d\n", l->name, l->stat_compr_new, l->stat_compr_orig, l->stat_decompr);
3511 + l = l->next;
3512 + }
3513 + return stat_str;
3514 +}
3515 +
3516 +static void jffs2_bbc_buffer_fill(unsigned char *buff, int size)
3517 +{
3518 + for (; size > 0; size--, buff++)
3519 + *buff = 255;
3520 +}
3521 +
3522 +
3523 +static int jffs2_bbc_update_compr_buf(unsigned long size)
3524 +{
3525 + struct jffs2_bbc_compressor_type *l;
3526 +
3527 + if (size < 5000)
3528 + size = 5000;
3529 + if (tmp_buffer == NULL) {
3530 + tmp_buffer = jffs2_bbc_malloc(size);
3531 + jffs2_bbc_buffer_fill(tmp_buffer, size);
3532 + tmp_buffer_size = size;
3533 + }
3534 + else if (tmp_buffer_size < size) {
3535 + jffs2_bbc_free(tmp_buffer);
3536 + tmp_buffer = jffs2_bbc_malloc(size);
3537 + jffs2_bbc_buffer_fill(tmp_buffer, size);
3538 + tmp_buffer_size = size;
3539 + }
3540 + l = jffs2_bbc_get_compressor_list();
3541 + while (l != NULL) {
3542 + if (l->buffer == NULL) {
3543 + l->buffer_size = size;
3544 + l->buffer = jffs2_bbc_malloc(size);
3545 + jffs2_bbc_buffer_fill(l->buffer, size);
3546 + }
3547 + else if (l->buffer_size < size) {
3548 + jffs2_bbc_free(l->buffer);
3549 + l->buffer_size = size;
3550 + l->buffer = jffs2_bbc_malloc(size);
3551 + jffs2_bbc_buffer_fill(l->buffer, size);
3552 + }
3553 + l = l->next;
3554 + }
3555 + return 0;
3556 +}
3557 +
3558 +#ifdef DEBUG_COMPRESSORS
3559 +
3560 +static unsigned char *debug_tmp_buff = NULL;
3561 +static long debug_orig_srclen = -1;
3562 +static long debug_orig_dstlen = -1;
3563 +static int debug_mem_counter = -1;
3564 +
3565 +
3566 +void debug_before_compress(struct jffs2_bbc_compressor_type *c, void *model, unsigned char *input, unsigned char *output, long *sourcelen, long *dstlen)
3567 +{
3568 +
3569 + debug_orig_srclen = *sourcelen; // for buffer overflow test
3570 + debug_orig_dstlen = *dstlen; // for buffer overflow test
3571 + output[debug_orig_dstlen + 1] = 255;
3572 +
3573 + debug_mem_counter = jffs2_bbc_get_memory_counter(); // for memory guard
3574 +}
3575 +
3576 +void debug_after_compress(struct jffs2_bbc_compressor_type *c, int back, void *model, unsigned char *input, unsigned char *output, long *sourcelen, long *dstlen)
3577 +{
3578 + long dst_len = *dstlen;
3579 + long src_len = *sourcelen;
3580 + int i;
3581 +
3582 + // Memory guard
3583 + if (debug_mem_counter != jffs2_bbc_get_memory_counter()) {
3584 + jffs2_bbc_print4("!!!!!!!! %s error: possible COMPRESSOR MEMORY LEAK: %d->%d\n", c->name, debug_mem_counter, jffs2_bbc_get_memory_counter());
3585 + debug_mem_counter = jffs2_bbc_get_memory_counter();
3586 + }
3587 +
3588 + // Buffer overflow test
3589 + if (output[debug_orig_dstlen + 1] != 255) {
3590 + jffs2_bbc_print7("!!!!!!!! %s error: BUFFER OVERFLOW !!!!!!!!!!!! b[%d]=%d (srclen=%d dstlen=%d, back=%d)\n", c->name, (int) (debug_orig_dstlen + 1), (int) (output[debug_orig_dstlen + 1]), (int) (debug_orig_srclen), (int) (*dstlen), back);
3591 + }
3592 +
3593 + // Decompression check
3594 + if (back == 0) {
3595 + if (debug_tmp_buff == NULL)
3596 + debug_tmp_buff = jffs2_bbc_malloc(17000);
3597 + for (i = 0; i < src_len; i++) debug_tmp_buff[i] = 0xf6;
3598 + c->decompress(model, output, debug_tmp_buff, dst_len, src_len);
3599 + // Memory guard for decompressor
3600 + if (debug_mem_counter != jffs2_bbc_get_memory_counter()) {
3601 + jffs2_bbc_print4("!!!!!!!! %s error: possible DECOMPRESSOR MEMORY LEAK: %d->%d\n", c->name, debug_mem_counter, jffs2_bbc_get_memory_counter());
3602 + debug_mem_counter = jffs2_bbc_get_memory_counter();
3603 + }
3604 +
3605 + for (i = 0; i < src_len; i++)
3606 + if (input[i] != debug_tmp_buff[i]) {
3607 + jffs2_bbc_print7("!!!!!!!! %s error: BLOCK DECOMPRESSED BADLY (first bad: %d in %d: %d!=%d (compressed size=%d)) !!!!!!!!!!!!\n", c->name, i, src_len, (int)input[i], (int)debug_tmp_buff[i], dst_len);
3608 + break;
3609 + }
3610 + return;
3611 + }
3612 +
3613 + // Return value test
3614 + //jffs2_bbc_print3("!!!!!!!! %s error: %d !!!!!!!!!!!!\n", c->name, back);
3615 +}
3616 +
3617 +#endif
3618 +
3619 +int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, uint32_t * sourcelen, uint32_t * dstlen)
3620 +{
3621 + struct jffs2_bbc_compressor_type *c;
3622 + int back, back_zlib, mode, min, i, i2;
3623 + long tmp = 0, tmp_read_time = 1000, tmp_write_time = 1000, orig_src, orig_dest, src, dest;
3624 + struct jffs2_bbc_model_list_node *m;
3625 + void *sb;
3626 + unsigned char *tmp_p = NULL;
3627 +
3628 + sb = jffs2_bbc_model_get_act_sb();
3629 +
3630 + orig_src = *sourcelen;
3631 + orig_dest = *dstlen;
3632 +
3633 + mode = jffs2_bbc_get_compression_mode();
3634 +
3635 + if (mode == JFFS2_BBC_DUMMY_MODE) {
3636 + i=0; i2=0;
3637 + if (*dstlen>2) {
3638 + cpage_out[i++]=JFFS2_BBC_DUMMY_BLOCKSIGN_0;
3639 + cpage_out[i++]=JFFS2_BBC_DUMMY_BLOCKSIGN_1;
3640 + i2=i;
3641 + }
3642 + for (;((i < *dstlen) && (i < (*sourcelen)+i2));i++) {
3643 + cpage_out[i] = data_in[i-i2];
3644 + }
3645 + *sourcelen=i-i2;
3646 + *dstlen=i;
3647 + return 0;
3648 + }
3649 +
3650 + if (mode == JFFS2_BBC_ZLIB_MODE) {
3651 + /*if (!jffs2_bbc_original_compressor->enabled) {
3652 + jffs2_bbc_print2("jffs2.bbc: WARNING: ZLIB mode but %s disabled! Enabling for this procedure...\n",jffs2_bbc_original_compressor->name);
3653 + }*/
3654 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3655 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3656 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3657 + return back;
3658 + }
3659 +
3660 + jffs2_bbc_update_compr_buf(orig_dest);
3661 +
3662 + if (mode == JFFS2_BBC_SIZE_MODE) {
3663 + // Testing all compressors
3664 + if (!jffs2_bbc_original_compressor->enabled) {
3665 + min = -1;
3666 + }
3667 + else {
3668 + back_zlib = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3669 + min = *dstlen;
3670 + }
3671 + c = jffs2_bbc_get_compressor_list();
3672 + while (c != NULL) {
3673 + c->buffer_cnt = -1;
3674 + if (c->enabled == 0) {
3675 + c = c->next;
3676 + continue;
3677 + }
3678 + if (c->model_file_sign == 0) {
3679 + src = orig_src;
3680 + dest = orig_dest;
3681 +#ifdef DEBUG_COMPRESSORS
3682 + debug_before_compress(c, NULL, data_in, c->buffer, &src, &dest);
3683 +#endif
3684 + back = c->compress(NULL, data_in, c->buffer, &src, &dest);
3685 +#ifdef DEBUG_COMPRESSORS
3686 + debug_after_compress(c, back, NULL, data_in, c->buffer, &src, &dest);
3687 +#endif
3688 + if (back == 0) {
3689 + c->buffer_cnt = dest;
3690 + if ((min < 0) || (min > dest))
3691 + min = dest;
3692 + }
3693 + }
3694 + else {
3695 + m = c->models;
3696 + while (m != NULL) {
3697 + src = orig_src;
3698 + dest = orig_dest;
3699 + if (m->sb == sb) {
3700 + if (c->buffer_cnt == -1) {
3701 +#ifdef DEBUG_COMPRESSORS
3702 + debug_before_compress(c, m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3703 +#endif
3704 + back = c->compress(m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3705 +#ifdef DEBUG_COMPRESSORS
3706 + debug_after_compress(c, back, m->model, data_in, c->buffer, (long *) (&src), (long *) (&dest));
3707 +#endif
3708 + if (back == 0) {
3709 + c->buffer_cnt = dest;
3710 + if ((min < 0) || (min > dest))
3711 + min = dest;
3712 + }
3713 + }
3714 + else {
3715 +#ifdef DEBUG_COMPRESSORS
3716 + debug_before_compress(c, m->model, data_in, tmp_buffer, &src, &dest);
3717 +#endif
3718 + back = c->compress(m->model, data_in, tmp_buffer, &src, &dest);
3719 +#ifdef DEBUG_COMPRESSORS
3720 + debug_after_compress(c, back, m->model, data_in, tmp_buffer, &src, &dest);
3721 +#endif
3722 + if (back == 0) {
3723 + if (c->buffer_cnt > dest) {
3724 + c->buffer_cnt = dest;
3725 + tmp_p = c->buffer;
3726 + c->buffer = tmp_buffer;
3727 + tmp_buffer = tmp_p;
3728 + if ((min < 0) || (min > dest))
3729 + min = dest;
3730 + }
3731 + }
3732 + }
3733 + }
3734 + m = m->next_compr_model;
3735 + }
3736 + }
3737 + c = c->next;
3738 + }
3739 + //Finding the best and copy its result
3740 +
3741 +#ifdef DEBUG_SHOW_BLOCK_SIZES
3742 + jffs2_bbc_print1("\n");
3743 + if (jffs2_bbc_original_compressor->enabled) {
3744 + if (min == *dstlen) {
3745 + jffs2_bbc_print3("%s:%d* ", jffs2_bbc_original_compressor->name, (int) (*dstlen));
3746 + }
3747 + else {
3748 + jffs2_bbc_print3("%s:%d ", jffs2_bbc_original_compressor->name, (int) (*dstlen));
3749 + }
3750 + }
3751 + c = jffs2_bbc_get_compressor_list();
3752 + while (c != NULL) {
3753 + if (c->enabled == 0) {
3754 + c = c->next;
3755 + continue;
3756 + }
3757 + if (c->buffer_cnt == min)
3758 + jffs2_bbc_print3("%s:%d* ", c->name, c->buffer_cnt);
3759 + else
3760 + jffs2_bbc_print3("%s:%d ", c->name, c->buffer_cnt);
3761 + c = c->next;
3762 + }
3763 +#endif
3764 +
3765 + if (min == -1) {
3766 + return -1; // none of compressors work (maybe too short output buffer)
3767 + }
3768 +
3769 + if (jffs2_bbc_original_compressor->enabled) {
3770 + if (min == *dstlen) {
3771 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3772 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3773 + return back_zlib;
3774 + }
3775 + }
3776 +
3777 + c = jffs2_bbc_get_compressor_list();
3778 + while (c != NULL) {
3779 + if (c->enabled == 0) {
3780 + c = c->next;
3781 + continue;
3782 + }
3783 + if (c->buffer_cnt == min) {
3784 + *dstlen = c->buffer_cnt;
3785 + *sourcelen = orig_src;
3786 + for (i = 0; i < *dstlen; i++) {
3787 + cpage_out[i] = c->buffer[i];
3788 + }
3789 + c->stat_compr_orig += *sourcelen;
3790 + c->stat_compr_new += *dstlen;
3791 + return 0;
3792 + }
3793 + c = c->next;
3794 + }
3795 + jffs2_bbc_print1("jffs2.bbc: compr (full): BUG!!!\n");
3796 + return 0;
3797 + }
3798 +
3799 + if ((mode == JFFS2_BBC_FASTR_MODE)||(mode == JFFS2_BBC_FASTW_MODE)||(mode == JFFS2_BBC_FASTS_MODE)) {
3800 + // Estimating all compressors
3801 + if (jffs2_bbc_original_compressor->enabled) {
3802 + back = jffs2_bbc_original_compressor->estimate(NULL, data_in, *sourcelen, &tmp, &tmp_read_time, &tmp_write_time);
3803 + }
3804 + else {
3805 + tmp = -1;
3806 + tmp_read_time = -1;
3807 + tmp_write_time = -1;
3808 + }
3809 + if (mode == JFFS2_BBC_FASTR_MODE) tmp = tmp_read_time;
3810 + if (mode == JFFS2_BBC_FASTW_MODE) tmp = tmp_write_time;
3811 + min = tmp;
3812 + c = jffs2_bbc_get_compressor_list();
3813 + while (c != NULL) {
3814 + src = orig_src;
3815 + dest = orig_dest;
3816 + c->buffer_cnt = -1;
3817 + if (c->enabled == 0) {
3818 + c = c->next;
3819 + continue;
3820 + }
3821 + if ((c->model_file_sign == 0) || (jffs2_bbc_model_get_newest(c) != NULL)) {
3822 + back = c->estimate(jffs2_bbc_model_get_newest(c), data_in, src, &dest, &tmp_read_time, &tmp_write_time);
3823 + if (mode == JFFS2_BBC_FASTR_MODE) dest = tmp_read_time;
3824 + if (mode == JFFS2_BBC_FASTW_MODE) dest = tmp_write_time;
3825 + if (back == 0) {
3826 + c->buffer_cnt = dest;
3827 + if ((min < 0) || (min > dest))
3828 + min = dest;
3829 + }
3830 + else {
3831 + c->buffer_cnt = -1;
3832 + }
3833 + }
3834 + c = c->next;
3835 + }
3836 + // Finding the best and compress with it
3837 + if (min == -1) {
3838 + return -1;
3839 + }
3840 + if (jffs2_bbc_original_compressor->enabled) {
3841 + if (min == tmp) {
3842 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3843 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3844 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3845 + return back;
3846 + }
3847 + }
3848 + c = jffs2_bbc_get_compressor_list();
3849 + while (c != NULL) {
3850 + if (c->enabled == 0) {
3851 + c = c->next;
3852 + continue;
3853 + }
3854 + if (c->buffer_cnt == min) {
3855 + back = c->compress(jffs2_bbc_model_get_newest(c), data_in, cpage_out, (unsigned long*)sourcelen, (unsigned long*)dstlen);
3856 + if ((back == 0) && (*dstlen < orig_dest) && (*dstlen > 4)) {
3857 + c->stat_compr_orig += *sourcelen;
3858 + c->stat_compr_new += *dstlen;
3859 + }
3860 + else { // fallback will always be available
3861 + *sourcelen = orig_src;
3862 + *dstlen = orig_dest;
3863 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3864 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3865 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3866 + return back;
3867 + }
3868 + return 0;
3869 + }
3870 + c = c->next;
3871 + }
3872 + jffs2_bbc_print1("jffs2.bbc: compress (fastX mode): BUG!!!\n");
3873 + return 0;
3874 + }
3875 +
3876 + if (mode == JFFS2_BBC_MANUAL_MODE) {
3877 + c = jffs2_bbc_get_manual_compressor();
3878 + if (c != NULL) {
3879 + if (c->model_file_sign == 0) {
3880 + src = orig_src;
3881 + dest = orig_dest;
3882 + back = c->compress(NULL, data_in, cpage_out, &src, &dest);
3883 + if (back == 0) {
3884 + *dstlen = dest;
3885 + *sourcelen = src;
3886 + c->stat_compr_orig += *sourcelen;
3887 + c->stat_compr_new += *dstlen;
3888 + return 0;
3889 + }
3890 + }
3891 + else {
3892 + c->buffer_cnt = -1;
3893 + m = c->models;
3894 + min = -1;
3895 + while (m != NULL) {
3896 + src = orig_src;
3897 + dest = orig_dest;
3898 + if (m->sb == sb) {
3899 + if (min == -1) {
3900 + back = c->compress(m->model, data_in, cpage_out, (unsigned long*)sourcelen, (unsigned long*)dstlen);
3901 + if ((back == 0) && (*dstlen < orig_dest) && (*dstlen > 4)) {
3902 + min = dest;
3903 + tmp_p = cpage_out;
3904 + }
3905 + }
3906 + else {
3907 + back = c->compress(m->model, data_in, tmp_buffer, &src, &dest);
3908 + if ((back == 0) && (dest < orig_dest) && (dest > 4)) {
3909 + if (c->buffer_cnt > dest) {
3910 + if (min > dest) {
3911 + min = dest;
3912 + tmp_p = tmp_buffer;
3913 + }
3914 + }
3915 + }
3916 + }
3917 + }
3918 + m = m->next_compr_model;
3919 + }
3920 + if (min != -1) {
3921 + if (tmp_p != cpage_out) {
3922 + for (i = 0; i < min; i++)
3923 + cpage_out[i] = tmp_p[i];
3924 + *sourcelen = orig_src;
3925 + *dstlen = min;
3926 + }
3927 + c->stat_compr_orig += *sourcelen;
3928 + c->stat_compr_new += *dstlen;
3929 + return 0;
3930 + }
3931 + }
3932 + }
3933 + /*else {
3934 + jffs2_bbc_print1("iPack: manual mode without selected compressor!\n");
3935 + } */
3936 +
3937 + /*if (!jffs2_bbc_original_compressor->enabled) {
3938 + jffs2_bbc_print2("jffs2.bbc: WARNING: %s must be enabled! Enabling for this procedure...\n",jffs2_bbc_original_compressor->name);
3939 + }*/
3940 + back = jffs2_bbc_original_compressor->compress(NULL, data_in, cpage_out, sourcelen, dstlen);
3941 + jffs2_bbc_original_compressor->stat_compr_orig += *sourcelen;
3942 + jffs2_bbc_original_compressor->stat_compr_new += *dstlen;
3943 + return back;
3944 +
3945 +
3946 + }
3947 +
3948 + jffs2_bbc_print1("jffs2.bbc: compress: unimlemented compress mode!!!\n");
3949 + return 0;
3950 +}
3951 +
3952 +void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen)
3953 +{
3954 + struct jffs2_bbc_model_list_node *m;
3955 + struct jffs2_bbc_compressor_type *c;
3956 + char d[2];
3957 + void *sb;
3958 + int i;
3959 +
3960 + /* If the input too small... */
3961 + if (destlen<=2) {
3962 + cpage_out[0]=data_in[0];
3963 + if (destlen==2) cpage_out[1]=data_in[1];
3964 + return;
3965 + }
3966 +
3967 + sb = jffs2_bbc_model_get_act_sb();
3968 + d[0] = *(data_in);
3969 + d[1] = *(data_in + 1);
3970 +
3971 + d[0] &= 0x7f; // Variants support...
3972 +
3973 + /* Search for model based decompressors... */
3974 + m = jffs2_bbc_get_model_list();
3975 + while (m != NULL) {
3976 + if ((d[0] == m->block_sign[0]) && (d[1] == m->block_sign[1]) && (sb == m->sb)) {
3977 + if (m->compressor == NULL) {
3978 + jffs2_bbc_print3("jffs2.bbc: decompressor for block_sign (%d,%d) not loaded!\n", (int) (d[0]), (int) (d[1]));
3979 + }
3980 + else {
3981 + m->compressor->decompress(m->model, data_in, cpage_out, srclen, destlen);
3982 + m->compressor->stat_decompr++;
3983 + m->stat_decompr++;
3984 + }
3985 + return;
3986 + }
3987 + m = m->next_model;
3988 + }
3989 + /* Is it ZLIB? */
3990 + if ((((int) d[0]) == (int)(jffs2_bbc_original_compressor->block_sign[0])) && (((int) d[1]) == (int)(jffs2_bbc_original_compressor->block_sign[1]))) {
3991 + jffs2_bbc_original_compressor->decompress(NULL, data_in, cpage_out, srclen, destlen);
3992 + jffs2_bbc_original_compressor->stat_decompr++;
3993 + return;
3994 + }
3995 + /* Search for non model based decompressors... */
3996 + c = jffs2_bbc_get_compressor_list();
3997 + while (c != NULL) {
3998 + if (c->model_file_sign == 0) {
3999 + if (((int) (d[0]) == (int) (c->block_sign[0])) && ((int) (d[1]) == (int) (c->block_sign[1]))) {
4000 + c->decompress(NULL, data_in, cpage_out, srclen, destlen);
4001 + c->stat_decompr++;
4002 + return;
4003 + }
4004 + }
4005 + c = c->next;
4006 + }
4007 + /* Is it DUMMY? */
4008 + if ((((int) d[0]) == JFFS2_BBC_DUMMY_BLOCKSIGN_0) && (((int) d[1]) == JFFS2_BBC_DUMMY_BLOCKSIGN_1)) {
4009 + for (i=0;i<destlen;i++) {
4010 + cpage_out[i]=data_in[i+2];
4011 + }
4012 + return;
4013 + }
4014 + /* No matching decompressor found... */
4015 + jffs2_bbc_print4("jffs2.bbc: cannot find model for decompress: bsign=(%d,%d),sb=%d. Using zlib.\n", (int) d[0], (int) d[1], (int) sb);
4016 + jffs2_bbc_original_compressor->decompress(NULL, data_in, cpage_out, srclen, destlen);
4017 + jffs2_bbc_original_compressor->stat_decompr++;
4018 +}
4019 +
4020 +#endif
4021 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_framework.h
4022 ===================================================================
4023 --- /dev/null
4024 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_framework.h
4025 @@ -0,0 +1,202 @@
4026 +/*
4027 + * JFFS2-BBC: Compression Framework - headers
4028 + *
4029 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4030 + *
4031 + * Copyright (C) 2004, Ferenc Havasi
4032 + *
4033 + * This program is free software; you can redistribute it and/or
4034 + * modify it under the terms of the GNU General Public License
4035 + * as published by the Free Software Foundation; either version 2
4036 + * of the License, or (at your option) any later version.
4037 + *
4038 + * This program is distributed in the hope that it will be useful,
4039 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4040 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4041 + * GNU General Public License for more details.
4042 + *
4043 + * You should have received a copy of the GNU General Public License
4044 + * along with this program; if not, write to the Free Software
4045 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4046 + *
4047 + */
4048 +
4049 +#ifndef __JFFS2_BBC_FRAMEWORK_H__
4050 +
4051 +#define __JFFS2_BBC_FRAMEWORK_H__
4052 +
4053 +#define JFFS2_BBC_VERSION "0.54.3"
4054 +
4055 +#define JFFS2_BBC_CONFIG_FILE "bbc.conf"
4056 +
4057 +/*********************************************************************
4058 + * Compression mode handling *
4059 + *********************************************************************/
4060 +
4061 +#define JFFS2_BBC_ZLIB_MODE 1
4062 +#define JFFS2_BBC_SIZE_MODE 2
4063 +#define JFFS2_BBC_FASTR_MODE 3
4064 +#define JFFS2_BBC_FASTW_MODE 4
4065 +#define JFFS2_BBC_FASTS_MODE 5
4066 +#define JFFS2_BBC_MANUAL_MODE 6
4067 +#define JFFS2_BBC_DUMMY_MODE 7
4068 +
4069 +int jffs2_bbc_get_compression_mode(void);
4070 +void jffs2_bbc_set_compression_mode(int mode);
4071 +
4072 +/*********************************************************************
4073 + * Read/write speed unit *
4074 + * everything is relative to the speed of zlib *
4075 + * bigger number means slower speed! *
4076 + *********************************************************************/
4077 +
4078 +#define JFFS2_BBC_ZLIB_READ_TIME 10000
4079 +#define JFFS2_BBC_ZLIB_WRITE_TIME 10000
4080 +
4081 +/*********************************************************************
4082 + * Compressor handling *
4083 + *********************************************************************/
4084 +
4085 +struct jffs2_bbc_compressor_type
4086 +{
4087 + char name[16];
4088 + int model_file_sign; /* 0 for no model file needed */
4089 + char block_sign[4]; /* only nomodel compressors, and only the first 2 _bytes are used! */
4090 + int (*init)(void);
4091 + int (*init_model)(void **model);
4092 + void (*destroy_model)(void **model);
4093 + void (*deinit)(void);
4094 + /* Compress block
4095 + * *dstlen bytes are allocated.
4096 + * if it is not enough write *sourcelen over to the processed amount of data
4097 + * returns non zero if fails
4098 + */
4099 + int (*compress)(void *model, unsigned char *input, unsigned char *output, unsigned long *sourcelen, unsigned long *dstlen);
4100 + int (*estimate)(void *model, unsigned char *input, unsigned long sourcelen,
4101 + unsigned long *dstlen, unsigned long *readtime, unsigned long *writetime);
4102 + /* Decompress block
4103 + * returns non zero if fails
4104 + */
4105 + int (*decompress)(void *model, unsigned char *input, unsigned char *output, unsigned long sourcelen, unsigned long dstlen);
4106 + char *(*proc_info)(void);
4107 + int (*proc_command)(char *command);
4108 + int enabled; /* filled by BBC */
4109 + int mounted; /* filled by BBC */
4110 + void *models; /* filled by BBC */
4111 + char *buffer; /* filled by BBC */
4112 + int buffer_size; /* filled by BBC */
4113 + int buffer_cnt; /* filled by BBC */
4114 + int buffer_tmp; /* filled by BBC */
4115 + int stat_compr_orig; /* filled by BBC */
4116 + int stat_compr_new; /* filled by BBC */
4117 + int stat_decompr; /* filled by BBC */
4118 + struct jffs2_bbc_compressor_type *next; /* filled by BBC */
4119 +};
4120 +
4121 +/* It sets the compression mode to JFFS2_BBC_MANUAL_MODE */
4122 +
4123 +void jffs2_bbc_set_manual_compressor(struct jffs2_bbc_compressor_type *c); /* NULL = ZLIB */
4124 +int jffs2_bbc_set_manual_compressor_by_name(char *name);
4125 +int jffs2_bbc_disable_compressor_by_name(char *name);
4126 +int jffs2_bbc_enable_compressor_by_name(char *name);
4127 +void jffs2_bbc_compressor_command_by_name(char *name_and_command);
4128 +
4129 +/* If the compression mode is JFFS2_BCC_MANUAL_MODE the manually setted
4130 + compressor can be get using it. Otherwise it returns with NULL. */
4131 +
4132 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_manual_compressor(void);
4133 +
4134 +struct jffs2_bbc_model_list_node
4135 +{
4136 + void *sb; /* FS idendifier (JFFS2_SB_INFO(sb) at this moment) */
4137 + void *model; /* model data */
4138 + int sign; /* sign of the model (first 4 bytes) */
4139 + char block_sign[4]; /* block sign - only the first 2 bytes are used! */
4140 + int inode; /* inode number of the model file */
4141 + int stat_decompr;
4142 + struct jffs2_bbc_compressor_type *compressor;
4143 + struct jffs2_bbc_model_list_node *next_model;
4144 + struct jffs2_bbc_model_list_node *next_compr_model;
4145 +};
4146 +
4147 +struct jffs2_bbc_compressor_type *jffs2_bbc_get_compressor_list(void);
4148 +struct jffs2_bbc_model_list_node *jffs2_bbc_get_model_list(void);
4149 +
4150 +int jffs2_bbc_register_compressor(struct jffs2_bbc_compressor_type *c);
4151 +int jffs2_bbc_unregister_compressor(struct jffs2_bbc_compressor_type *c);
4152 +
4153 +int jffs2_bbc_model_new(void *sb, int i_num, void *model);
4154 +void jffs2_bbc_model_del(void *sb);
4155 +void jffs2_bbc_model_set_act_sb(void *sb);
4156 +void *jffs2_bbc_model_get_act_sb(void);
4157 +void *jffs2_bbc_model_get_newest(struct jffs2_bbc_compressor_type *compressor);
4158 +
4159 +/*********************************************************************
4160 + * Compressor init function *
4161 + *********************************************************************/
4162 +
4163 +void jffs2_bbc_compressor_init(void);
4164 +void jffs2_bbc_compressor_deinit(void);
4165 +
4166 +/*********************************************************************
4167 + * Statistics *
4168 + *********************************************************************/
4169 +
4170 +char *jffs2_bbc_get_compr_stats(void);
4171 +char *jffs2_bbc_get_model_stats(void);
4172 +
4173 +/*********************************************************************
4174 + * Other *
4175 + *********************************************************************/
4176 +
4177 +
4178 +void jffs2_bbc_print_flush(void);
4179 +
4180 +#ifdef __KERNEL__
4181 +#include <linux/kernel.h>
4182 +#define jffs2_bbc_print1(a) printk(a)
4183 +#define jffs2_bbc_print2(a,b) printk(a,b)
4184 +#define jffs2_bbc_print3(a,b,c) printk(a,b,c)
4185 +#define jffs2_bbc_print4(a,b,c,d) printk(a,b,c,d)
4186 +#define jffs2_bbc_print5(a,b,c,d,e) printk(a,b,c,d,e)
4187 +#define jffs2_bbc_print6(a,b,c,d,e,f) printk(a,b,c,d,e,f)
4188 +#define jffs2_bbc_print7(a,b,c,d,e,f,g) printk(a,b,c,d,e,f,g)
4189 +#define jffs2_bbc_print8(a,b,c,d,e,f,g,h) printk(a,b,c,d,e,f,g,h)
4190 +#define jffs2_bbc_print9(a,b,c,d,e,f,g,h,i) printk(a,b,c,d,e,f,g,h,i)
4191 +#else
4192 +#include <stdio.h>
4193 +#define jffs2_bbc_print1(a) fprintf(stderr,a)
4194 +#define jffs2_bbc_print2(a,b) fprintf(stderr,a,b)
4195 +#define jffs2_bbc_print3(a,b,c) fprintf(stderr,a,b,c)
4196 +#define jffs2_bbc_print4(a,b,c,d) fprintf(stderr,a,b,c,d)
4197 +#define jffs2_bbc_print5(a,b,c,d,e) fprintf(stderr,a,b,c,d,e)
4198 +#define jffs2_bbc_print6(a,b,c,d,e,f) fprintf(stderr,a,b,c,d,e,f)
4199 +#define jffs2_bbc_print7(a,b,c,d,e,f,g) fprintf(stderr,a,b,c,d,e,f,g)
4200 +#define jffs2_bbc_print8(a,b,c,d,e,f,g,h) fprintf(stderr,a,b,c,d,e,f,g,h)
4201 +#define jffs2_bbc_print9(a,b,c,d,e,f,g,h,i) fprintf(stderr,a,b,c,d,e,f,g,h,i)
4202 +#endif
4203 +
4204 +/* Handle endianness */
4205 +#ifndef __KERNEL__
4206 +
4207 +#define ENDIAN_HOST_AND_TARGET_SAME 0
4208 +#define ENDIAN_HOST_AND_TARGET_DIFFERENT 1
4209 +
4210 +extern int jffs2_bbc_glb_endian_X;
4211 +
4212 +#endif
4213 +
4214 +/* Allocating more than one page (tip. 4096 byte) */
4215 +void *jffs2_bbc_malloc(long size);
4216 +void jffs2_bbc_free(void *addr);
4217 +
4218 +/* Allocating less than one page (tip. 4096 byte) */
4219 +void *jffs2_bbc_malloc_small(long size);
4220 +void jffs2_bbc_free_small(void *addr);
4221 +
4222 +/* Memory guarding */
4223 +int jffs2_bbc_test_memory_counter(int verbose);
4224 +char *jffs2_bbc_get_mem_stats(void);
4225 +int jffs2_bbc_get_memory_counter(void);
4226 +
4227 +#endif
4228 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_fs.c
4229 ===================================================================
4230 --- /dev/null
4231 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_fs.c
4232 @@ -0,0 +1,331 @@
4233 +/*
4234 + * JFFS2-BBC: File System Extension for Linux Kernel
4235 + *
4236 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4237 + *
4238 + * Copyright (C) 2004, Ferenc Havasi
4239 + *
4240 + * This program is free software; you can redistribute it and/or
4241 + * modify it under the terms of the GNU General Public License
4242 + * as published by the Free Software Foundation; either version 2
4243 + * of the License, or (at your option) any later version.
4244 + *
4245 + * This program is distributed in the hope that it will be useful,
4246 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4247 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4248 + * GNU General Public License for more details.
4249 + *
4250 + * You should have received a copy of the GNU General Public License
4251 + * along with this program; if not, write to the Free Software
4252 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4253 + *
4254 + */
4255 +
4256 +#include <linux/kernel.h>
4257 +#include <linux/fs.h>
4258 +#include <linux/jffs2.h>
4259 +#include <linux/proc_fs.h>
4260 +#include <linux/version.h>
4261 +
4262 +#include "nodelist.h"
4263 +
4264 +#include "jffs2_bbc_framework.h"
4265 +
4266 +struct jffs2_bbc_fs_sb_list {
4267 + struct super_block *sb;
4268 + struct jffs2_bbc_fs_sb_list *next;
4269 +};
4270 +
4271 +static struct jffs2_bbc_fs_sb_list *sb_list = NULL;
4272 +
4273 +void jffs2_bbc_proc_init(void);
4274 +void jffs2_bbc_proc_deinit(void);
4275 +
4276 +void jffs2_bbc_load_model(void *sb_par) {
4277 + struct jffs2_sb_info *c;
4278 + //struct jffs2_inode_info *f;
4279 + struct dentry *config_dentry,*model_dentry;
4280 + struct qstr config_name,model_name;
4281 + struct file *config_file,*model_file;
4282 + char *buff=NULL,*model_buff;
4283 + int config_size,model_size;
4284 + int i,prev_i;
4285 + struct super_block *sb;
4286 + struct jffs2_bbc_fs_sb_list *sb_l;
4287 +
4288 + sb = sb_par;
4289 + sb_l = jffs2_bbc_malloc_small(sizeof(struct jffs2_bbc_fs_sb_list));
4290 + sb_l->sb = sb;
4291 + sb_l->next = sb_list;
4292 + sb_list = sb_l;
4293 + config_name.name = JFFS2_BBC_CONFIG_FILE;
4294 + config_name.len = strlen(config_name.name);
4295 + config_name.hash = full_name_hash(config_name.name,config_name.len);
4296 + config_dentry = d_alloc(sb->s_root,&config_name);
4297 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4298 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,config_dentry);
4299 +#else
4300 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,config_dentry,NULL);
4301 +#endif
4302 +
4303 + if (config_dentry->d_inode != NULL) {
4304 + config_size = config_dentry->d_inode->i_size;
4305 + //printk("config_file_size=%d\n",config_size);
4306 + if (config_size > 0) {
4307 + buff = jffs2_bbc_malloc(config_size+1);
4308 + config_file = dentry_open(config_dentry,NULL,O_RDONLY);
4309 + kernel_read(config_file,0,buff,config_size);
4310 + buff[config_size] = 0;
4311 + for (prev_i = i = 0 ; i < config_size+1 ; i++) {
4312 + if (buff[i] == '\n') buff[i]=0;
4313 + if (buff[i] == 0) {
4314 + if (prev_i != i) {
4315 + if ((buff[prev_i] == '-') && (buff[prev_i+1] == 0)) break;
4316 + printk("reading model file %s... ",buff+prev_i);
4317 + model_name.name = buff+prev_i;
4318 + model_name.len = strlen(buff+prev_i);
4319 + model_name.hash = full_name_hash(model_name.name,model_name.len);
4320 + model_dentry = d_alloc(sb->s_root,&model_name);
4321 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4322 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,model_dentry);
4323 +#else
4324 + sb->s_root->d_inode->i_op->lookup(sb->s_root->d_inode,model_dentry,NULL);
4325 +#endif
4326 + if (model_dentry->d_inode != NULL) {
4327 + c = JFFS2_SB_INFO(model_dentry->d_inode->i_sb);
4328 + //f = JFFS2_INODE_INFO(model_dentry->d_inode);
4329 + model_size = model_dentry->d_inode->i_size;
4330 + model_buff = jffs2_bbc_malloc(model_size);
4331 + model_file = dentry_open(model_dentry,NULL,O_RDONLY);
4332 + kernel_read(model_file,0,model_buff,model_size);
4333 + if (jffs2_bbc_model_new(c,model_dentry->d_inode->i_ino,model_buff) != 0) {
4334 + printk("already loaded.\n");
4335 + jffs2_bbc_free(model_buff);
4336 + }
4337 + else {
4338 + printk("done (%d bytes readed from inode %d).\n",model_size,(int)(model_dentry->d_inode->i_ino));
4339 + }
4340 + }
4341 + else {
4342 + printk("not found.\n");
4343 + }
4344 + dput(model_dentry);
4345 + }
4346 + prev_i = i+1;
4347 + }
4348 + }
4349 + }
4350 + }
4351 + dput(config_dentry);
4352 + if (buff != NULL) jffs2_bbc_free(buff);
4353 +}
4354 +
4355 +void jffs2_bbc_unload_model(void *sb_par)
4356 +{
4357 + struct jffs2_sb_info *c;
4358 + struct super_block *sb = sb_par;
4359 + struct jffs2_bbc_fs_sb_list *sb_l,*sb_l2;
4360 + int done = 0;
4361 +
4362 + c = JFFS2_SB_INFO(sb);
4363 + jffs2_bbc_model_del(c);
4364 + if (sb_list == NULL) printk("jffs2.bbc: error! NULL sb list!\n");
4365 + else {
4366 + if (sb_list->sb == sb) {
4367 + jffs2_bbc_free_small(sb_list);
4368 + sb_list = NULL;
4369 + done = 1;
4370 + }
4371 + else {
4372 + sb_l = sb_list;
4373 + while (sb_l->next != NULL) {
4374 + if (sb_l->next->sb == sb) {
4375 + sb_l2 = sb_l->next->next;
4376 + jffs2_bbc_free_small(sb_l->next);
4377 + sb_l->next = sb_l2;
4378 + done = 1;
4379 + }
4380 + sb_l = sb_l->next;
4381 + }
4382 +
4383 + }
4384 + if (done == 0) {
4385 + printk("jffs2.bbc: cannot delete sb from sblist!\n");
4386 + }
4387 + }
4388 +}
4389 +
4390 +static int jffs2_bbc_get_mounted(void) {
4391 + struct jffs2_bbc_fs_sb_list *sb_l;
4392 + int num = 0;
4393 +
4394 + sb_l = sb_list;
4395 + while (sb_l != NULL) {
4396 + num++;
4397 + sb_l = sb_l->next;
4398 + }
4399 + return num;
4400 +
4401 +}
4402 +
4403 +int jffs2_bbc_proc_read(char *buf, char **start, off_t offset, int count, int *eof, void *data)
4404 +{
4405 + int len = 0, mode;
4406 +
4407 + mode = jffs2_bbc_get_compression_mode();
4408 + len += sprintf(buf + len, "BBC version: %s\n", JFFS2_BBC_VERSION);
4409 + len += sprintf(buf+len,"Mounted jffs2 filesystems: %d\n",jffs2_bbc_get_mounted());
4410 + //len += sprintf(buf+len,"actual model file inode: %d\n",jffs2_bbc_model_get_inum());
4411 + len += sprintf(buf + len, "Compression mode: ");
4412 + if (mode == JFFS2_BBC_ZLIB_MODE)
4413 + len += sprintf(buf + len, "ZLIB mode");
4414 + else if (mode == JFFS2_BBC_SIZE_MODE)
4415 + len += sprintf(buf + len, "SIZE mode");
4416 + else if (mode == JFFS2_BBC_FASTR_MODE)
4417 + len += sprintf(buf + len, "FASTR mode");
4418 + else if (mode == JFFS2_BBC_FASTW_MODE)
4419 + len += sprintf(buf + len, "FASTW mode");
4420 + else if (mode == JFFS2_BBC_FASTS_MODE)
4421 + len += sprintf(buf + len, "FASTS mode");
4422 + else if (mode == JFFS2_BBC_DUMMY_MODE)
4423 + len += sprintf(buf + len, "DUMMY mode");
4424 + else if (mode == JFFS2_BBC_MANUAL_MODE) {
4425 + len += sprintf(buf + len, "MANUAL mode (");
4426 + if (jffs2_bbc_get_manual_compressor() != NULL)
4427 + len += sprintf(buf + len, "%s)", jffs2_bbc_get_manual_compressor()->name);
4428 + else
4429 + len += sprintf(buf + len, "ZLIB)");
4430 + }
4431 + else
4432 + len += sprintf(buf + len, "unknown mode");
4433 + len += sprintf(buf + len, "\n");
4434 + len += sprintf(buf + len, "%s", jffs2_bbc_get_compr_stats());
4435 + len += sprintf(buf + len, "%s", jffs2_bbc_get_model_stats());
4436 + len += sprintf(buf + len, "%s", jffs2_bbc_get_mem_stats());
4437 + *eof = 1;
4438 + return len;
4439 +}
4440 +
4441 +int jffs2_bbc_proc_write(struct file *file, const char *buffer_orig, unsigned long count, void *data)
4442 +{
4443 + char *buffer;
4444 + int i;
4445 + struct jffs2_bbc_fs_sb_list *sb_l;
4446 +
4447 + if (buffer_orig == NULL) return 0;
4448 +
4449 + buffer = jffs2_bbc_malloc(count+2);
4450 + for (i=0;i<count;i++) buffer[i]=buffer_orig[i];
4451 + buffer[count] = 0;
4452 + if ((*buffer == 'z') || (*buffer == 'Z')) {
4453 + jffs2_bbc_set_compression_mode(JFFS2_BBC_ZLIB_MODE);
4454 + jffs2_bbc_print1("jffs2.bbc: ZLIB compression mode activated.\n");
4455 + jffs2_bbc_free(buffer);
4456 + return count;
4457 + }
4458 + else if ((*buffer == 's') || (*buffer == 'S')) {
4459 + jffs2_bbc_set_compression_mode(JFFS2_BBC_SIZE_MODE);
4460 + jffs2_bbc_print1("jffs2.bbc: SIZE compression mode activated.\n");
4461 + jffs2_bbc_free(buffer);
4462 + return count;
4463 + }
4464 + else if ((*buffer == 'd') || (*buffer == 'D')) {
4465 + jffs2_bbc_set_compression_mode(JFFS2_BBC_DUMMY_MODE);
4466 + jffs2_bbc_print1("jffs2.bbc: DUMMY compression mode activated.\n");
4467 + jffs2_bbc_free(buffer);
4468 + return count;
4469 + }
4470 + else if (((*buffer == 'm') || (*buffer == 'M')) && (count >= 3) && (buffer[1] == ':')) {
4471 + jffs2_bbc_print1("jffs2.bbc: activating MANUAL mode.\n");
4472 + jffs2_bbc_set_manual_compressor_by_name(buffer + 2);
4473 + jffs2_bbc_free(buffer);
4474 + return count;
4475 + }
4476 + else if (((*buffer == '0')) && (count >= 3) && (buffer[1] == ':')) {
4477 + jffs2_bbc_print1("jffs2.bbc: disabling a compressor... ");
4478 + if (jffs2_bbc_disable_compressor_by_name(buffer + 2) == 0) {
4479 + jffs2_bbc_print1("done.\n");
4480 + }
4481 + else {
4482 + jffs2_bbc_print1("not found.\n");
4483 + }
4484 + jffs2_bbc_free(buffer);
4485 + return count;
4486 + }
4487 + else if (((*buffer == '1')) && (count >= 3) && (buffer[1] == ':')) {
4488 + jffs2_bbc_print1("jffs2.bbc: enabling a compressor... ");
4489 + if (jffs2_bbc_enable_compressor_by_name(buffer + 2) == 0) {
4490 + jffs2_bbc_print1("done.\n");
4491 + }
4492 + else {
4493 + jffs2_bbc_print1("not found.\n");
4494 + }
4495 + jffs2_bbc_free(buffer);
4496 + return count;
4497 + }
4498 + else if (((*buffer == 'c') || (*buffer == 'C')) && (count >= 3) && (buffer[1] == ':')) {
4499 + jffs2_bbc_compressor_command_by_name(buffer + 2);
4500 + jffs2_bbc_free(buffer);
4501 + return count;
4502 + }
4503 + else if ((*buffer == 'r') || (*buffer == 'R')) {
4504 + jffs2_bbc_print1("jffs2.bbc: reloading model files:\n");
4505 + sb_l = sb_list;
4506 + while (sb_l != NULL) {
4507 + jffs2_bbc_unload_model(sb_l->sb);
4508 + jffs2_bbc_load_model(sb_l->sb);
4509 + sb_l = sb_l->next;
4510 + }
4511 + jffs2_bbc_free(buffer);
4512 + return count;
4513 + }
4514 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 'r') || (buffer[1] == 'R'))) {
4515 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTR_MODE);
4516 + jffs2_bbc_print1("jffs2.bbc: FASTR compression mode activated.\n");
4517 + jffs2_bbc_free(buffer);
4518 + return count;
4519 + }
4520 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 'w') || (buffer[1] == 'W'))) {
4521 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTW_MODE);
4522 + jffs2_bbc_print1("jffs2.bbc: FASTW compression mode activated.\n");
4523 + jffs2_bbc_free(buffer);
4524 + return count;
4525 + }
4526 + else if (((buffer[0] == 'f') || (buffer[0] == 'F'))&&((buffer[1] == 's') || (buffer[1] == 'S'))) {
4527 + jffs2_bbc_set_compression_mode(JFFS2_BBC_FASTS_MODE);
4528 + jffs2_bbc_print1("jffs2.bbc: FASTS compression mode activated.\n");
4529 + jffs2_bbc_free(buffer);
4530 + return count;
4531 + }
4532 +
4533 + jffs2_bbc_print1("jffs2.bbc: unkown command. Valid commands are:\n"
4534 + " z = switch to ZLIB compression mode\n"
4535 + " s = switch to SIZE compression mode\n"
4536 + " d = switch to DUMMY compression mode\n"
4537 + " fr = switch to FASTR compression mode\n"
4538 + " fw = switch to FASTW compression mode\n"
4539 + " fs = switch to FASTS compression mode\n"
4540 + " r = reread model files from actual file system\n"
4541 + " m:compressor_name = switch to MANUAL compression mode\n"
4542 + " 0:compressor_name = disable a compressor\n"
4543 + " 1:compressor_name = enable a compressor\n"
4544 + " c:compressor_name:command = enable a compressor\n");
4545 + jffs2_bbc_free(buffer);
4546 + return count;
4547 +}
4548 +
4549 +void jffs2_bbc_proc_init()
4550 +{
4551 + struct proc_dir_entry *res = create_proc_entry("jffs2_bbc", 0, NULL);
4552 + jffs2_bbc_compressor_init();
4553 + if (res) {
4554 + res->read_proc = jffs2_bbc_proc_read;
4555 + res->write_proc = jffs2_bbc_proc_write;
4556 + }
4557 +}
4558 +
4559 +void jffs2_bbc_proc_deinit()
4560 +{
4561 + jffs2_bbc_compressor_deinit();
4562 + remove_proc_entry("jffs2_bbc", NULL);
4563 +}
4564 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_fs.h
4565 ===================================================================
4566 --- /dev/null
4567 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_fs.h
4568 @@ -0,0 +1,30 @@
4569 +/*
4570 + * JFFS2 BBC: File System Extension for Linux Kernel - headers
4571 + *
4572 + * $Id: 301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4573 + *
4574 + * Copyright (C) 2004, Ferenc Havasi
4575 + *
4576 + * This program is free software; you can redistribute it and/or
4577 + * modify it under the terms of the GNU General Public License
4578 + * as published by the Free Software Foundation; either version 2
4579 + * of the License, or (at your option) any later version.
4580 + *
4581 + * This program is distributed in the hope that it will be useful,
4582 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4583 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4584 + * GNU General Public License for more details.
4585 + *
4586 + * You should have received a copy of the GNU General Public License
4587 + * along with this program; if not, write to the Free Software
4588 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4589 + *
4590 + */
4591 +
4592 +extern int jffs2_bbc_inode_not_found;
4593 +
4594 +void jffs2_bbc_load_model(void *sb);
4595 +void jffs2_bbc_unload_model(void *sb);
4596 +
4597 +void jffs2_bbc_proc_init(void);
4598 +void jffs2_bbc_proc_deinit(void);
4599 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzari_comp.c
4600 ===================================================================
4601 --- /dev/null
4602 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzari_comp.c
4603 @@ -0,0 +1,788 @@
4604 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4605 +
4606 +/*
4607 + jffs2_bbc_lzari_comp.c -- Lempel-Ziv-Arithmetic coding compression module for jffs2
4608 + Copyright (C) 2004 Patrik Kluba
4609 + Based on the LZARI source included in LDS (lossless datacompression sources)
4610 + Block-compression and bitstream modifications by Patrik Kluba
4611 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
4612 +*/
4613 +
4614 +/*
4615 +Original copyright follows:
4616 +
4617 +**************************************************************
4618 + LZARI.C -- A Data Compression Program
4619 + (tab = 4 spaces)
4620 +**************************************************************
4621 + 4/7/1989 Haruhiko Okumura
4622 + Use, distribute, and modify this program freely.
4623 + Please send me your improved versions.
4624 + PC-VAN SCIENCE
4625 + NIFTY-Serve PAF01022
4626 + CompuServe 74050,1022
4627 +**************************************************************
4628 +
4629 +LZARI.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
4630 +All rights reserved. Permission granted for non-commercial use.
4631 +
4632 +*/
4633 +
4634 +/*
4635 +
4636 + 2004-02-18 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
4637 + Removed unused variables and fixed no return value
4638 +
4639 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
4640 + Initial release
4641 +
4642 +*/
4643 +
4644 +/* lzari.c */
4645 +
4646 +#define N 4096 /* size of ring buffer */
4647 +#define F 60 /* upper limit for match_length */
4648 +#define THRESHOLD 2 /* encode string into position and length
4649 + if match_length is greater than this */
4650 +#define NIL N /* index for root of binary search trees */
4651 +
4652 +static unsigned char
4653 + text_buf[N + F - 1]; /* ring buffer of size N,
4654 + with extra F-1 bytes to facilitate string comparison */
4655 +static unsigned long match_position, match_length, /* of longest match. These are
4656 + set by the InsertNode() procedure. */
4657 + lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
4658 + parents -- These constitute binary search trees. */
4659 +
4660 +static void InitTree(void) /* Initialize trees */
4661 +{
4662 + unsigned long i;
4663 +
4664 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
4665 + left children of node i. These nodes need not be initialized.
4666 + Also, dad[i] is the parent of node i. These are initialized to
4667 + NIL (= N), which stands for 'not used.'
4668 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
4669 + for strings that begin with character i. These are initialized
4670 + to NIL. Note there are 256 trees. */
4671 +
4672 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL; /* root */
4673 + for (i = 0; i < N; i++) dad[i] = NIL; /* node */
4674 +}
4675 +
4676 +static void InsertNode(unsigned long r)
4677 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
4678 + trees (text_buf[r]'th tree) and returns the longest-match position
4679 + and length via the global variables match_position and match_length.
4680 + If match_length = F, then removes the old node in favor of the new
4681 + one, because the old one will be deleted sooner.
4682 + Note r plays double role, as tree node and position in buffer. */
4683 +{
4684 + unsigned long i, p, temp;
4685 + unsigned char *key;
4686 + signed long cmp;
4687 +
4688 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
4689 + rson[r] = lson[r] = NIL; match_length = 0;
4690 + for ( ; ; ) {
4691 + if (cmp >= 0) {
4692 + if (rson[p] != NIL) p = rson[p];
4693 + else { rson[p] = r; dad[r] = p; return; }
4694 + } else {
4695 + if (lson[p] != NIL) p = lson[p];
4696 + else { lson[p] = r; dad[r] = p; return; }
4697 + }
4698 + for (i = 1; i < F; i++)
4699 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
4700 + if (i > THRESHOLD) {
4701 + if (i > match_length) {
4702 + match_position = (r - p) & (N - 1);
4703 + if ((match_length = i) >= F) break;
4704 + } else if (i == match_length) {
4705 + if ((temp = (r - p) & (N - 1)) < match_position)
4706 + match_position = temp;
4707 + }
4708 + }
4709 + }
4710 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
4711 + dad[lson[p]] = r; dad[rson[p]] = r;
4712 + if (rson[dad[p]] == p) rson[dad[p]] = r;
4713 + else lson[dad[p]] = r;
4714 + dad[p] = NIL; /* remove p */
4715 +}
4716 +
4717 +static void DeleteNode(unsigned long p) /* Delete node p from tree */
4718 +{
4719 + unsigned long q;
4720 +
4721 + if (dad[p] == NIL) return; /* not in tree */
4722 + if (rson[p] == NIL) q = lson[p];
4723 + else if (lson[p] == NIL) q = rson[p];
4724 + else {
4725 + q = lson[p];
4726 + if (rson[q] != NIL) {
4727 + do { q = rson[q]; } while (rson[q] != NIL);
4728 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
4729 + lson[q] = lson[p]; dad[lson[p]] = q;
4730 + }
4731 + rson[q] = rson[p]; dad[rson[p]] = q;
4732 + }
4733 + dad[q] = dad[p];
4734 + if (rson[dad[p]] == p) rson[dad[p]] = q;
4735 + else lson[dad[p]] = q;
4736 + dad[p] = NIL;
4737 +}
4738 +
4739 +/********** Arithmetic Compression **********/
4740 +
4741 +/* If you are not familiar with arithmetic compression, you should read
4742 + I. E. Witten, R. M. Neal, and J. G. Cleary,
4743 + Communications of the ACM, Vol. 30, pp. 520-540 (1987),
4744 + from which much have been borrowed. */
4745 +
4746 +#define M 15
4747 +
4748 +/* Q1 (= 2 to the M) must be sufficiently large, but not so
4749 + large as the unsigned long 4 * Q1 * (Q1 - 1) overflows. */
4750 +
4751 +#define Q1 (1UL << M)
4752 +#define Q2 (2 * Q1)
4753 +#define Q3 (3 * Q1)
4754 +#define Q4 (4 * Q1)
4755 +#define MAX_CUM (Q1 - 1)
4756 +
4757 +#define N_CHAR (256 - THRESHOLD + F)
4758 + /* character code = 0, 1, ..., N_CHAR - 1 */
4759 +
4760 +static unsigned long char_to_sym[N_CHAR], sym_to_char[N_CHAR + 1];
4761 +static unsigned long
4762 + sym_freq[N_CHAR + 1], /* frequency for symbols */
4763 + sym_cum[N_CHAR + 1], /* cumulative freq for symbols */
4764 + position_cum[N + 1]; /* cumulative freq for positions */
4765 +
4766 +static void StartModel(void) /* Initialize model */
4767 +{
4768 + unsigned long ch, sym, i;
4769 +
4770 + sym_cum[N_CHAR] = 0;
4771 + for (sym = N_CHAR; sym >= 1; sym--) {
4772 + ch = sym - 1;
4773 + char_to_sym[ch] = sym; sym_to_char[sym] = ch;
4774 + sym_freq[sym] = 1;
4775 + sym_cum[sym - 1] = sym_cum[sym] + sym_freq[sym];
4776 + }
4777 + sym_freq[0] = 0; /* sentinel (!= sym_freq[1]) */
4778 + position_cum[N] = 0;
4779 + for (i = N; i >= 1; i--)
4780 + position_cum[i - 1] = position_cum[i] + 10000 / (i + 200);
4781 + /* empirical distribution function (quite tentative) */
4782 + /* Please devise a better mechanism! */
4783 +}
4784 +
4785 +static void UpdateModel(unsigned long sym)
4786 +{
4787 + unsigned long c, ch_i, ch_sym;
4788 + unsigned long i;
4789 + if (sym_cum[0] >= MAX_CUM) {
4790 + c = 0;
4791 + for (i = N_CHAR; i > 0; i--) {
4792 + sym_cum[i] = c;
4793 + c += (sym_freq[i] = (sym_freq[i] + 1) >> 1);
4794 + }
4795 + sym_cum[0] = c;
4796 + }
4797 + for (i = sym; sym_freq[i] == sym_freq[i - 1]; i--) ;
4798 + if (i < sym) {
4799 + ch_i = sym_to_char[i]; ch_sym = sym_to_char[sym];
4800 + sym_to_char[i] = ch_sym; sym_to_char[sym] = ch_i;
4801 + char_to_sym[ch_i] = sym; char_to_sym[ch_sym] = i;
4802 + }
4803 + sym_freq[i]++;
4804 + while (--i > 0) sym_cum[i]++;
4805 + sym_cum[0]++;
4806 +}
4807 +
4808 +static unsigned long BinarySearchSym(unsigned long x)
4809 + /* 1 if x >= sym_cum[1],
4810 + N_CHAR if sym_cum[N_CHAR] > x,
4811 + i such that sym_cum[i - 1] > x >= sym_cum[i] otherwise */
4812 +{
4813 + unsigned long i, j, k;
4814 +
4815 + i = 1; j = N_CHAR;
4816 + while (i < j) {
4817 + k = (i + j) / 2;
4818 + if (sym_cum[k] > x) i = k + 1; else j = k;
4819 + }
4820 + return i;
4821 +}
4822 +
4823 +unsigned long BinarySearchPos(unsigned long x)
4824 + /* 0 if x >= position_cum[1],
4825 + N - 1 if position_cum[N] > x,
4826 + i such that position_cum[i] > x >= position_cum[i + 1] otherwise */
4827 +{
4828 + unsigned long i, j, k;
4829 +
4830 + i = 1; j = N;
4831 + while (i < j) {
4832 + k = (i + j) / 2;
4833 + if (position_cum[k] > x) i = k + 1; else j = k;
4834 + }
4835 + return i - 1;
4836 +}
4837 +
4838 +/* modified for block compression */
4839 +/* on return, srclen will contain the number of successfully compressed bytes
4840 + and dstlen will contain completed compressed bytes */
4841 +
4842 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
4843 + unsigned long *dstlen)
4844 +{
4845 + unsigned long c, i, len, r, s, last_match_length, sym, range;
4846 + unsigned long low = 0;
4847 + unsigned long high = Q4;
4848 + unsigned long shifts = 0; /* counts for magnifying low and high around Q2 */
4849 + unsigned char *ip, *op;
4850 + unsigned long written = 0;
4851 + unsigned long read = 0;
4852 + unsigned char buffer = 0;
4853 + unsigned char mask = 128;
4854 + unsigned char *srcend = srcbuf + *srclen;
4855 + unsigned char *dstend = dstbuf + *dstlen;
4856 + ip = srcbuf;
4857 + op = dstbuf;
4858 + StartModel();
4859 + InitTree(); /* initialize trees */
4860 + s = 0; r = N - F;
4861 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
4862 + any character that will appear often. */
4863 + for (len = 0; (len < F) && (ip < srcend); len++)
4864 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
4865 + the buffer */
4866 + read = len;
4867 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
4868 + each of which begins with one or more 'space' characters. Note
4869 + the order in which these strings are inserted. This way,
4870 + degenerate trees will be less likely to occur. */
4871 + InsertNode(r); /* Finally, insert the whole string just read. The
4872 + global variables match_length and match_position are set. */
4873 + do {
4874 + if (match_length > len) match_length = len; /* match_length
4875 + may be spuriously long near the end of text. */
4876 + if (match_length <= THRESHOLD) {
4877 + match_length = 1; /* Not long enough match. Send one byte. */
4878 + sym = char_to_sym[text_buf[r]];
4879 + range = high - low;
4880 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
4881 + low += (range * sym_cum[sym ]) / sym_cum[0];
4882 + for ( ; ; ) {
4883 + if (high <= Q2) {
4884 + if ((mask >>= 1) == 0) {
4885 + if (op >= dstend) {
4886 + *dstlen = written;
4887 + return -1;
4888 + }
4889 + *(op++) = buffer;
4890 + buffer = 0;
4891 + mask = 128;
4892 + written++;
4893 + *srclen = read;
4894 + }
4895 + for ( ; shifts > 0; shifts--) {
4896 + buffer |= mask;
4897 + if ((mask >>= 1) == 0) {
4898 + if (op >= dstend) {
4899 + *dstlen = written;
4900 + return -1;
4901 + }
4902 + *(op++) = buffer;
4903 + buffer = 0;
4904 + mask = 128;
4905 + written++;
4906 + *srclen = read;
4907 + }
4908 + }
4909 + } else if (low >= Q2) {
4910 + buffer |= mask;
4911 + if ((mask >>= 1) == 0) {
4912 + if (op >= dstend) {
4913 + *dstlen = written;
4914 + return -1;
4915 + }
4916 + *(op++) = buffer;
4917 + buffer = 0;
4918 + mask = 128;
4919 + written++;
4920 + *srclen = read;
4921 + }
4922 + for ( ; shifts > 0; shifts--) {
4923 + if ((mask >>= 1) == 0) {
4924 + if (op >= dstend) {
4925 + *dstlen = written;
4926 + return -1;
4927 + }
4928 + *(op++) = buffer;
4929 + buffer = 0;
4930 + mask = 128;
4931 + written++;
4932 + *srclen = read;
4933 + }
4934 + }
4935 + low -= Q2;
4936 + high -= Q2;
4937 + } else if (low >= Q1 && high <= Q3) {
4938 + shifts++;
4939 + low -= Q1;
4940 + high -= Q1;
4941 + } else break;
4942 + low += low; high += high;
4943 + }
4944 + UpdateModel(sym);
4945 + } else {
4946 + sym = char_to_sym[255 - THRESHOLD + match_length];
4947 + range = high - low;
4948 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
4949 + low += (range * sym_cum[sym ]) / sym_cum[0];
4950 + for ( ; ; ) {
4951 + if (high <= Q2) {
4952 + if ((mask >>= 1) == 0) {
4953 + if (op >= dstend) {
4954 + *dstlen = written;
4955 + return -1;
4956 + }
4957 + *(op++) = buffer;
4958 + buffer = 0;
4959 + mask = 128;
4960 + written++;
4961 + *srclen = read;
4962 + }
4963 + for ( ; shifts > 0; shifts--) {
4964 + buffer |= mask;
4965 + if ((mask >>= 1) == 0) {
4966 + if (op >= dstend) {
4967 + *dstlen = written;
4968 + return -1;
4969 + }
4970 + *(op++) = buffer;
4971 + buffer = 0;
4972 + mask = 128;
4973 + written++;
4974 + *srclen = read;
4975 + }
4976 + }
4977 + } else if (low >= Q2) {
4978 + buffer |= mask;
4979 + if ((mask >>= 1) == 0) {
4980 + if (op >= dstend) {
4981 + *dstlen = written;
4982 + return -1;
4983 + }
4984 + *(op++) = buffer;
4985 + buffer = 0;
4986 + mask = 128;
4987 + written++;
4988 + *srclen = read;
4989 + }
4990 + for ( ; shifts > 0; shifts--) {
4991 + if ((mask >>= 1) == 0) {
4992 + if (op >= dstend) {
4993 + *dstlen = written;
4994 + return -1;
4995 + }
4996 + *(op++) = buffer;
4997 + buffer = 0;
4998 + mask = 128;
4999 + written++;
5000 + *srclen = read;
5001 + }
5002 + }
5003 + low -= Q2;
5004 + high -= Q2;
5005 + } else if (low >= Q1 && high <= Q3) {
5006 + shifts++;
5007 + low -= Q1;
5008 + high -= Q1;
5009 + } else break;
5010 + low += low; high += high;
5011 + }
5012 + UpdateModel(sym);
5013 + range = high - low;
5014 + high = low + (range * position_cum[match_position - 1]) / position_cum[0];
5015 + low += (range * position_cum[match_position ]) / position_cum[0];
5016 + for ( ; ; ) {
5017 + if (high <= Q2) {
5018 + if ((mask >>= 1) == 0) {
5019 + if (op >= dstend) {
5020 + *dstlen = written;
5021 + return -1;
5022 + }
5023 + *(op++) = buffer;
5024 + buffer = 0;
5025 + mask = 128;
5026 + written++;
5027 + *srclen = read;
5028 + }
5029 + for ( ; shifts > 0; shifts--) {
5030 + buffer |= mask;
5031 + if ((mask >>= 1) == 0) {
5032 + if (op >= dstend) {
5033 + *dstlen = written;
5034 + return -1;
5035 + }
5036 + *(op++) = buffer;
5037 + buffer = 0;
5038 + mask = 128;
5039 + written++;
5040 + *srclen = read;
5041 + }
5042 + }
5043 + } else {
5044 + if (low >= Q2) {
5045 + buffer |= mask;
5046 + if ((mask >>= 1) == 0) {
5047 + if (op >= dstend) {
5048 + *dstlen = written;
5049 + return -1;
5050 + }
5051 + *(op++) = buffer;
5052 + buffer = 0;
5053 + mask = 128;
5054 + written++;
5055 + *srclen = read;
5056 + }
5057 + for ( ; shifts > 0; shifts--) {
5058 + if ((mask >>= 1) == 0) {
5059 + if (op >= dstend) {
5060 + *dstlen = written;
5061 + return -1;
5062 + }
5063 + *(op++) = buffer;
5064 + buffer = 0;
5065 + mask = 128;
5066 + written++;
5067 + *srclen = read;
5068 + }
5069 + }
5070 + low -= Q2;
5071 + high -= Q2;
5072 + } else {
5073 + if ((low >= Q1) && (high <= Q3)) {
5074 + shifts++;
5075 + low -= Q1;
5076 + high -= Q1;
5077 + } else {
5078 + break;
5079 + }
5080 + }
5081 + }
5082 + low += low;
5083 + high += high;
5084 + }
5085 + }
5086 + last_match_length = match_length;
5087 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
5088 + c = *(ip++);
5089 + DeleteNode(s);
5090 + text_buf[s] = c;
5091 + if (s < F - 1)
5092 + text_buf[s + N] = c;
5093 + s = (s + 1) & (N - 1);
5094 + r = (r + 1) & (N - 1);
5095 + InsertNode(r);
5096 + }
5097 + read += i;
5098 + while (i++ < last_match_length) {
5099 + DeleteNode(s);
5100 + s = (s + 1) & (N - 1);
5101 + r = (r + 1) & (N - 1);
5102 + if (--len) InsertNode(r);
5103 + }
5104 + } while (len > 0);
5105 + shifts++;
5106 + if (low < Q1) {
5107 + if ((mask >>= 1) == 0) {
5108 + if (op >= dstend) {
5109 + *dstlen = written;
5110 + return -1;
5111 + }
5112 + *(op++) = buffer;
5113 + buffer = 0;
5114 + mask = 128;
5115 + written++;
5116 + *srclen = read;
5117 + }
5118 + for ( ; shifts > 0; shifts--) {
5119 + buffer |= mask;
5120 + if ((mask >>= 1) == 0) {
5121 + if (op >= dstend) {
5122 + *dstlen = written;
5123 + return -1;
5124 + }
5125 + *(op++) = buffer;
5126 + buffer = 0;
5127 + mask = 128;
5128 + written++;
5129 + *srclen = read;
5130 + }
5131 + }
5132 + } else {
5133 + buffer |= mask;
5134 + if ((mask >>= 1) == 0) {
5135 + if (op >= dstend) {
5136 + *dstlen = written;
5137 + return -1;
5138 + }
5139 + *(op++) = buffer;
5140 + buffer = 0;
5141 + mask = 128;
5142 + written++;
5143 + *srclen = read;
5144 + }
5145 + for ( ; shifts > 0; shifts--) {
5146 + if ((mask >>= 1) == 0) {
5147 + if (op >= dstend) {
5148 + *dstlen = written;
5149 + return -1;
5150 + }
5151 + *(op++) = buffer;
5152 + buffer = 0;
5153 + mask = 128;
5154 + written++;
5155 + *srclen = read;
5156 + }
5157 + }
5158 + }
5159 + for (i = 0; i < 7; i++) {
5160 + if ((mask >>= 1) == 0) {
5161 + if (op >= dstend) {
5162 + *dstlen = written;
5163 + return -1;
5164 + }
5165 + *(op++) = buffer;
5166 + buffer = 0;
5167 + mask = 128;
5168 + written++;
5169 + *srclen = read;
5170 + }
5171 + }
5172 + *dstlen = written;
5173 + return 0;
5174 +}
5175 +
5176 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
5177 + unsigned long dstlen) /* Just the reverse of Encode(). */
5178 +{
5179 + unsigned long i, r, j, k, c, range, sym;
5180 + unsigned char *ip, *op;
5181 + unsigned char *srcend = srcbuf + srclen;
5182 + unsigned char *dstend = dstbuf + dstlen;
5183 + unsigned char buffer = 0;
5184 + unsigned char mask = 0;
5185 + unsigned long low = 0;
5186 + unsigned long high = Q4;
5187 + unsigned long value = 0;
5188 + ip = srcbuf;
5189 + op = dstbuf;
5190 + for (i = 0; i < M + 2; i++) {
5191 + value *= 2;
5192 + if ((mask >>= 1) == 0) {
5193 + buffer = (ip >= srcend) ? 0 : *(ip++);
5194 + mask = 128;
5195 + }
5196 + value += ((buffer & mask) != 0);
5197 + }
5198 + StartModel();
5199 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
5200 + r = N - F;
5201 + while (op < dstend) {
5202 + range = high - low;
5203 + sym = BinarySearchSym((unsigned long)
5204 + (((value - low + 1) * sym_cum[0] - 1) / range));
5205 + high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
5206 + low += (range * sym_cum[sym ]) / sym_cum[0];
5207 + for ( ; ; ) {
5208 + if (low >= Q2) {
5209 + value -= Q2; low -= Q2; high -= Q2;
5210 + } else if (low >= Q1 && high <= Q3) {
5211 + value -= Q1; low -= Q1; high -= Q1;
5212 + } else if (high > Q2) break;
5213 + low += low; high += high;
5214 + value *= 2;
5215 + if ((mask >>= 1) == 0) {
5216 + buffer = (ip >= srcend) ? 0 : *(ip++);
5217 + mask = 128;
5218 + }
5219 + value += ((buffer & mask) != 0);
5220 + }
5221 + c = sym_to_char[sym];
5222 + UpdateModel(sym);
5223 + if (c < 256) {
5224 + if (op >= dstend) return -1;
5225 + *(op++) = c;
5226 + text_buf[r++] = c;
5227 + r &= (N - 1);
5228 + } else {
5229 + j = c - 255 + THRESHOLD;
5230 + range = high - low;
5231 + i = BinarySearchPos((unsigned long)
5232 + (((value - low + 1) * position_cum[0] - 1) / range));
5233 + high = low + (range * position_cum[i ]) / position_cum[0];
5234 + low += (range * position_cum[i + 1]) / position_cum[0];
5235 + for ( ; ; ) {
5236 + if (low >= Q2) {
5237 + value -= Q2; low -= Q2; high -= Q2;
5238 + } else if (low >= Q1 && high <= Q3) {
5239 + value -= Q1; low -= Q1; high -= Q1;
5240 + } else if (high > Q2) break;
5241 + low += low; high += high;
5242 + value *= 2;
5243 + if ((mask >>= 1) == 0) {
5244 + buffer = (ip >= srcend) ? 0 : *(ip++);
5245 + mask = 128;
5246 + }
5247 + value += ((buffer & mask) != 0);
5248 + }
5249 + i = (r - i - 1) & (N - 1);
5250 + for (k = 0; k < j; k++) {
5251 + c = text_buf[(i + k) & (N - 1)];
5252 + if (op >= dstend) return -1;
5253 + *(op++) = c;
5254 + text_buf[r++] = c;
5255 + r &= (N - 1);
5256 + }
5257 + }
5258 + }
5259 + return 0;
5260 +}
5261 +
5262 +/* interface to jffs2 bbc follows */
5263 +
5264 +#include "jffs2_bbc_framework.h"
5265 +
5266 +#define JFFS2_BBC_LZARI_BLOCK_SIGN {0x73, 0x9a, 0x1c, 0x4d}
5267 +
5268 +static int
5269 +jffs2_bbc_lzari_compressor_init (void);
5270 +
5271 +static void
5272 +jffs2_bbc_lzari_compressor_deinit (void);
5273 +
5274 +static int
5275 +jffs2_bbc_lzari_compress (void *model, unsigned char *input,
5276 + unsigned char *output, unsigned long *sourcelen,
5277 + unsigned long *dstlen);
5278 +
5279 +static int
5280 +jffs2_bbc_lzari_estimate (void *model, unsigned char *input,
5281 + unsigned long sourcelen, unsigned long *dstlen,
5282 + unsigned long *readtime, unsigned long *writetime);
5283 +
5284 +static int
5285 +jffs2_bbc_lzari_decompress (void *model, unsigned char *input,
5286 + unsigned char *output, unsigned long sourcelen,
5287 + unsigned long dstlen);
5288 +
5289 +static char *
5290 +jffs2_bbc_lzari_proc_info (void);
5291 +
5292 +static int
5293 +jffs2_bbc_lzari_proc_command (char *command);
5294 +
5295 +struct jffs2_bbc_compressor_type jffs2_bbc_lzari = {
5296 + "lzari",
5297 + 0,
5298 + JFFS2_BBC_LZARI_BLOCK_SIGN,
5299 + jffs2_bbc_lzari_compressor_init,
5300 + NULL,
5301 + NULL,
5302 + jffs2_bbc_lzari_compressor_deinit,
5303 + jffs2_bbc_lzari_compress,
5304 + jffs2_bbc_lzari_estimate,
5305 + jffs2_bbc_lzari_decompress,
5306 + jffs2_bbc_lzari_proc_info,
5307 + jffs2_bbc_lzari_proc_command
5308 +};
5309 +
5310 +static int
5311 +jffs2_bbc_lzari_compressor_init (void)
5312 +{
5313 + return 0;
5314 +}
5315 +
5316 +static void
5317 +jffs2_bbc_lzari_compressor_deinit (void)
5318 +{
5319 +}
5320 +
5321 +static int
5322 +jffs2_bbc_lzari_compress (void *model, unsigned char *input,
5323 + unsigned char *output, unsigned long *sourcelen,
5324 + unsigned long *dstlen)
5325 +{
5326 + int retval;
5327 + unsigned long dst = *dstlen;
5328 + *(output++) = jffs2_bbc_lzari.block_sign[0];
5329 + *(output++) = jffs2_bbc_lzari.block_sign[1];
5330 + dst -= 2;
5331 + retval = Encode(input, output, sourcelen, &dst);
5332 + dst += 2;
5333 + *dstlen = dst;
5334 + return retval;
5335 +}
5336 +
5337 +static int
5338 +jffs2_bbc_lzari_estimate (void *model, unsigned char *input,
5339 + unsigned long sourcelen, unsigned long *dstlen,
5340 + unsigned long *readtime, unsigned long *writetime)
5341 +{
5342 + *dstlen = sourcelen / 2;
5343 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 15;
5344 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 7;
5345 + return 0;
5346 +}
5347 +
5348 +static int
5349 +jffs2_bbc_lzari_decompress (void *model, unsigned char *input,
5350 + unsigned char *output, unsigned long sourcelen,
5351 + unsigned long dstlen)
5352 +{
5353 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzari.block_sign[0] ) ||
5354 + ( *(input++) != (unsigned char)jffs2_bbc_lzari.block_sign[1] )
5355 + ) {
5356 + return -1;
5357 + } else {
5358 + return Decode(input, output, sourcelen - 2, dstlen);
5359 + }
5360 +}
5361 +
5362 +static char *
5363 +jffs2_bbc_lzari_proc_info (void)
5364 +{
5365 + return "Lempel-Ziv-Arithmetic coding compression module";
5366 +}
5367 +
5368 +static int
5369 +jffs2_bbc_lzari_proc_command (char *command)
5370 +{
5371 + return 0;
5372 +}
5373 +
5374 +struct jffs2_bbc_compressor_type *
5375 +jffs2_bbc_lzari_init (int mode)
5376 +{
5377 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzari) == 0)
5378 + {
5379 + return &jffs2_bbc_lzari;
5380 + }
5381 + else
5382 + {
5383 + return NULL;
5384 + }
5385 +}
5386 +
5387 +void
5388 +jffs2_bbc_lzari_deinit (void)
5389 +{
5390 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzari);
5391 +}
5392 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzhd_comp.c
5393 ===================================================================
5394 --- /dev/null
5395 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzhd_comp.c
5396 @@ -0,0 +1,747 @@
5397 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
5398 +
5399 +/*
5400 + jffs2_bbc_lzhd_comp.c -- Lempel-Ziv-(dynamic) Huffman compression module for jffs2
5401 + Copyright (C) 2004 Patrik Kluba
5402 + Based on the LZHUF source included in LDS (lossless datacompression sources)
5403 + Block-compression and bitstream modifications by Patrik Kluba
5404 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
5405 +*/
5406 +
5407 +/*
5408 +Original copyright follows:
5409 +
5410 +**************************************************************
5411 + lzhuf.c
5412 + written by Haruyasu Yoshizaki 11/20/1988
5413 + some minor changes 4/6/1989
5414 + comments translated by Haruhiko Okumura 4/7/1989
5415 +**************************************************************
5416 +
5417 +LZHUF.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
5418 +All rights reserved. Permission granted for non-commercial use.
5419 +
5420 +*/
5421 +
5422 +/*
5423 +
5424 + 2004-02-18 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
5425 + Replaced name lzh-d with lzhd
5426 + Fixed no return value
5427 +
5428 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
5429 + Initial release
5430 +
5431 +*/
5432 +
5433 +/* required because of memmove */
5434 +#ifndef __KERNEL__
5435 + #include <string.h>
5436 +#else
5437 + #include <linux/string.h>
5438 +#endif
5439 +
5440 +/* lzhuf.c */
5441 +
5442 +#define N 4096 /* size of ring buffer */
5443 +#define F 60 /* upper limit for match_length */
5444 +#define THRESHOLD 2 /* encode string into position and length
5445 + if match_length is greater than this */
5446 +#define NIL N /* index for root of binary search trees */
5447 +
5448 +static unsigned char
5449 + text_buf[N + F - 1]; /* ring buffer of size N,
5450 + with extra F-1 bytes to facilitate string comparison */
5451 +static unsigned long match_position, match_length, /* of longest match. These are
5452 + set by the InsertNode() procedure. */
5453 + lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
5454 + parents -- These constitute binary search trees. */
5455 +
5456 +static void InitTree(void) /* initialize trees */
5457 +{
5458 + unsigned long i;
5459 +
5460 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
5461 + left children of node i. These nodes need not be initialized.
5462 + Also, dad[i] is the parent of node i. These are initialized to
5463 + NIL (= N), which stands for 'not used.'
5464 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
5465 + for strings that begin with character i. These are initialized
5466 + to NIL. Note there are 256 trees. */
5467 +
5468 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
5469 + for (i = 0; i < N; i++) dad[i] = NIL;
5470 +}
5471 +
5472 +static void InsertNode(unsigned long r)
5473 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
5474 + trees (text_buf[r]'th tree) and returns the longest-match position
5475 + and length via the global variables match_position and match_length.
5476 + If match_length = F, then removes the old node in favor of the new
5477 + one, because the old one will be deleted sooner.
5478 + Note r plays double role, as tree node and position in buffer. */
5479 +{
5480 + unsigned long i, p, c;
5481 + signed long cmp;
5482 + unsigned char *key;
5483 +
5484 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
5485 + rson[r] = lson[r] = NIL; match_length = 0;
5486 + for ( ; ; ) {
5487 + if (cmp >= 0) {
5488 + if (rson[p] != NIL) p = rson[p];
5489 + else { rson[p] = r; dad[r] = p; return; }
5490 + } else {
5491 + if (lson[p] != NIL) p = lson[p];
5492 + else { lson[p] = r; dad[r] = p; return; }
5493 + }
5494 + for (i = 1; i < F; i++)
5495 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
5496 + if (i > THRESHOLD) {
5497 + if (i > match_length) {
5498 + match_position = ((r - p) & (N - 1)) - 1;
5499 + if ((match_length = i) >= F) break;
5500 + }
5501 + if (i == match_length) {
5502 + if ((c = ((r - p) & (N - 1)) - 1) < match_position) {
5503 + match_position = c;
5504 + }
5505 + }
5506 + }
5507 + }
5508 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
5509 + dad[lson[p]] = r; dad[rson[p]] = r;
5510 + if (rson[dad[p]] == p) rson[dad[p]] = r;
5511 + else lson[dad[p]] = r;
5512 + dad[p] = NIL; /* remove p */
5513 +}
5514 +
5515 +static void DeleteNode(unsigned long p) /* deletes node p from tree */
5516 +{
5517 + unsigned long q;
5518 +
5519 + if (dad[p] == NIL) return; /* not in tree */
5520 + if (rson[p] == NIL) q = lson[p];
5521 + else if (lson[p] == NIL) q = rson[p];
5522 + else {
5523 + q = lson[p];
5524 + if (rson[q] != NIL) {
5525 + do { q = rson[q]; } while (rson[q] != NIL);
5526 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
5527 + lson[q] = lson[p]; dad[lson[p]] = q;
5528 + }
5529 + rson[q] = rson[p]; dad[rson[p]] = q;
5530 + }
5531 + dad[q] = dad[p];
5532 + if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
5533 + dad[p] = NIL;
5534 +}
5535 +
5536 +/* Huffman coding */
5537 +
5538 +#define N_CHAR (256 - THRESHOLD + F)
5539 + /* kinds of characters (character code = 0..N_CHAR-1) */
5540 +#define T (N_CHAR * 2 - 1) /* size of table */
5541 +#define R (T - 1) /* position of root */
5542 +#define MAX_FREQ 0x8000 /* updates tree when the */
5543 + /* root frequency comes to this value. */
5544 +
5545 +typedef unsigned long uchar; // much-much faster
5546 +
5547 +/* table for encoding and decoding the upper 6 bits of position */
5548 +
5549 +/* for encoding */
5550 +static uchar p_len[64] = {
5551 + 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
5552 + 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06,
5553 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5554 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5555 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5556 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5557 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5558 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
5559 +};
5560 +
5561 +static uchar p_code[64] = {
5562 + 0x00, 0x20, 0x30, 0x40, 0x50, 0x58, 0x60, 0x68,
5563 + 0x70, 0x78, 0x80, 0x88, 0x90, 0x94, 0x98, 0x9C,
5564 + 0xA0, 0xA4, 0xA8, 0xAC, 0xB0, 0xB4, 0xB8, 0xBC,
5565 + 0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE,
5566 + 0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE,
5567 + 0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEC, 0xEE,
5568 + 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
5569 + 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
5570 +};
5571 +
5572 +/* for decoding */
5573 +static uchar d_code[256] = {
5574 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5575 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5576 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5577 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5578 + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
5579 + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
5580 + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
5581 + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
5582 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5583 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5584 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5585 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5586 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5587 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5588 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5589 + 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
5590 + 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
5591 + 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
5592 + 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
5593 + 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
5594 + 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
5595 + 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
5596 + 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
5597 + 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
5598 + 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
5599 + 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
5600 + 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
5601 + 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
5602 + 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
5603 + 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
5604 + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
5605 + 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
5606 +};
5607 +
5608 +static uchar d_len[256] = {
5609 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5610 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5611 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5612 + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
5613 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5614 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5615 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5616 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5617 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5618 + 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
5619 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5620 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5621 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5622 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5623 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5624 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5625 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5626 + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
5627 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5628 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5629 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5630 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5631 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5632 + 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
5633 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5634 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5635 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5636 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5637 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5638 + 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
5639 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5640 + 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
5641 +};
5642 +
5643 +static unsigned long freq[T + 1]; /* frequency table */
5644 +
5645 +static unsigned long prnt[T + N_CHAR]; /* pointers to parent nodes, except for the */
5646 + /* elements [T..T + N_CHAR - 1] which are used to get */
5647 + /* the positions of leaves corresponding to the codes. */
5648 +
5649 +static unsigned long son[T]; /* pointers to child nodes (son[], son[] + 1) */
5650 +
5651 +/* initialization of tree */
5652 +
5653 +static void StartHuff(void)
5654 +{
5655 + unsigned long i, j;
5656 +
5657 + for (i = 0; i < N_CHAR; i++) {
5658 + freq[i] = 1;
5659 + son[i] = i + T;
5660 + prnt[i + T] = i;
5661 + }
5662 + i = 0; j = N_CHAR;
5663 + while (j <= R) {
5664 + freq[j] = freq[i] + freq[i + 1];
5665 + son[j] = i;
5666 + prnt[i] = prnt[i + 1] = j;
5667 + i += 2; j++;
5668 + }
5669 + freq[T] = 0xffff;
5670 + prnt[R] = 0;
5671 +}
5672 +
5673 +/* reconstruction of tree */
5674 +
5675 +static void reconst(void)
5676 +{
5677 + unsigned long f, l, i, j, k;
5678 +
5679 + /* collect leaf nodes in the first half of the table */
5680 + /* and replace the freq by (freq + 1) / 2. */
5681 + j = 0;
5682 + for (i = 0; i < T; i++) {
5683 + if (son[i] >= T) {
5684 + freq[j] = (freq[i] + 1) / 2;
5685 + son[j] = son[i];
5686 + j++;
5687 + }
5688 + }
5689 + /* begin constructing tree by connecting sons */
5690 + for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
5691 + k = i + 1;
5692 + f = freq[j] = freq[i] + freq[k];
5693 + for (k = j - 1; f < freq[k]; k--);
5694 + k++;
5695 + l = (j - k) * 2;
5696 + memmove(&freq[k + 1], &freq[k], l*sizeof(unsigned long));
5697 + freq[k] = f;
5698 + memmove(&son[k + 1], &son[k], l*sizeof(unsigned long));
5699 + son[k] = i;
5700 + }
5701 + /* connect prnt */
5702 + for (i = 0; i < T; i++) {
5703 + if ((k = son[i]) >= T) {
5704 + prnt[k] = i;
5705 + } else {
5706 + prnt[k] = prnt[k + 1] = i;
5707 + }
5708 + }
5709 +}
5710 +
5711 +/* increment frequency of given code by one, and update tree */
5712 +
5713 +static void update(unsigned long c)
5714 +{
5715 + unsigned long i, j, k, l;
5716 +
5717 + if (freq[R] == MAX_FREQ) {
5718 + reconst();
5719 + }
5720 + c = prnt[c + T];
5721 + do {
5722 + k = ++freq[c];
5723 +
5724 + /* if the order is disturbed, exchange nodes */
5725 + if (k > freq[l = c + 1]) {
5726 + while (k > freq[++l]);
5727 + l--;
5728 + freq[c] = freq[l];
5729 + freq[l] = k;
5730 +
5731 + i = son[c];
5732 + prnt[i] = l;
5733 + if (i < T) prnt[i + 1] = l;
5734 +
5735 + j = son[l];
5736 + son[l] = i;
5737 +
5738 + prnt[j] = c;
5739 + if (j < T) prnt[j + 1] = c;
5740 + son[c] = j;
5741 +
5742 + c = l;
5743 + }
5744 + } while (c = prnt[c]); /* repeat up to root */
5745 +}
5746 +
5747 +/* modified for block compression */
5748 +/* on return, srclen will contain the number of successfully compressed bytes
5749 + and dstlen will contain completed compressed bytes */
5750 +
5751 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
5752 + unsigned long *dstlen)
5753 +{
5754 + unsigned long c, i, j, k, len, r, s, last_match_length, code_buf_ptr;
5755 + unsigned char code_buf[17], mask;
5756 + unsigned char *ip, *op;
5757 + unsigned long written = 0;
5758 + unsigned long read = 0;
5759 + unsigned short putbuf = 0;
5760 + uchar putlen = 0;
5761 + unsigned char *srcend = srcbuf + *srclen;
5762 + unsigned char *dstend = dstbuf + *dstlen;
5763 + ip = srcbuf;
5764 + op = dstbuf;
5765 + StartHuff();
5766 + InitTree(); /* initialize trees */
5767 + code_buf[0] = 0; /* code_buf[1..16] saves eight units of code, and
5768 + code_buf[0] works as eight flags, "1" representing that the unit
5769 + is an unencoded letter (1 byte), "0" a position-and-length pair
5770 + (2 bytes). Thus, eight units require at most 16 bytes of code. */
5771 + code_buf_ptr = mask = 1;
5772 + s = 0; r = N - F;
5773 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
5774 + any character that will appear often. */
5775 + for (len = 0; (len < F) && (ip < srcend); len++)
5776 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
5777 + the buffer */
5778 + read = len;
5779 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
5780 + each of which begins with one or more 'space' characters. Note
5781 + the order in which these strings are inserted. This way,
5782 + degenerate trees will be less likely to occur. */
5783 + InsertNode(r); /* Finally, insert the whole string just read. The
5784 + global variables match_length and match_position are set. */
5785 + do {
5786 + if (match_length > len) match_length = len; /* match_length
5787 + may be spuriously long near the end of text. */
5788 + if (match_length <= THRESHOLD) {
5789 + match_length = 1; /* Not long enough match. Send one byte. */
5790 + c = text_buf[r];
5791 + i = 0; j = 0; k = prnt[c + T];
5792 + do {
5793 + i >>= 1;
5794 + /* if node's address is odd-numbered, choose bigger brother node */
5795 + if (k & 1) i |= 0x8000;
5796 + j++;
5797 + } while ((k = prnt[k]) != R);
5798 + putbuf |= i >> putlen;
5799 + if ((putlen += j) >= 8) {
5800 + if (op >= dstend) {
5801 + *dstlen = written;
5802 + return -1;
5803 + }
5804 + *(op++) = putbuf >> 8;
5805 + if ((putlen -= 8) >= 8) {
5806 + if (op >= dstend) {
5807 + *dstlen = written;
5808 + return -1;
5809 + }
5810 + *(op++) = putbuf;
5811 + written += 2;
5812 + putlen -= 8;
5813 + putbuf = i << (j - putlen); /**warm**/
5814 + } else {
5815 + putbuf <<= 8;
5816 + written++;
5817 + }
5818 + *srclen = read;
5819 + }
5820 + update(c);
5821 + } else {
5822 + c = 255 - THRESHOLD + match_length;
5823 + i = 0; j = 0; k = prnt[c + T];
5824 + do {
5825 + i >>= 1;
5826 + /* if node's address is odd-numbered, choose bigger brother node */
5827 + if (k & 1) i |= 0x8000;
5828 + j++;
5829 + } while ((k = prnt[k]) != R);
5830 + putbuf |= i >> putlen;
5831 + if ((putlen += j) >= 8) {
5832 + if (op >= dstend) {
5833 + *dstlen = written;
5834 + return -1;
5835 + }
5836 + *(op++) = putbuf >> 8;
5837 + if ((putlen -= 8) >= 8) {
5838 + if (op >= dstend) {
5839 + *dstlen = written;
5840 + return -1;
5841 + }
5842 + *(op++) = putbuf;
5843 + written += 2;
5844 + putlen -= 8;
5845 + putbuf = i << (j - putlen); /**warm**/
5846 + } else {
5847 + putbuf <<= 8;
5848 + written++;
5849 + }
5850 + *srclen = read;
5851 + }
5852 + update(c);
5853 + j = p_len[match_position >> 6];
5854 + i = p_code[match_position >> 6] << 8;
5855 + putbuf |= i >> putlen;
5856 + if ((putlen += j) >= 8) {
5857 + if (op >= dstend) {
5858 + *dstlen = written;
5859 + return -1;
5860 + }
5861 + *(op++) = putbuf >> 8;
5862 + if ((putlen -= 8) >= 8) {
5863 + if (op >= dstend) {
5864 + *dstlen = written;
5865 + return -1;
5866 + }
5867 + *(op++) = putbuf;
5868 + written += 2;
5869 + putlen -= 8;
5870 + putbuf = i << (j - putlen); /**hot**/
5871 + } else {
5872 + putbuf <<= 8;
5873 + written++;
5874 + }
5875 + *srclen = read;
5876 + }
5877 + j = 6;
5878 + i = (match_position & 0x3f) << 10;
5879 + putbuf |= i >> putlen;
5880 + if ((putlen += j) >= 8) {
5881 + if (op >= dstend) {
5882 + *dstlen = written;
5883 + return -1;
5884 + }
5885 + *(op++) = putbuf >> 8;
5886 + if ((putlen -= 8) >= 8) {
5887 + if (op >= dstend) {
5888 + *dstlen = written;
5889 + return -1;
5890 + }
5891 + *(op++) = putbuf;
5892 + written += 2;
5893 + putlen -= 8;
5894 + putbuf = i << (j - putlen); /**hot**/
5895 + } else {
5896 + putbuf <<= 8;
5897 + written++;
5898 + }
5899 + *srclen = read;
5900 + }
5901 + }
5902 + last_match_length = match_length;
5903 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
5904 + c = *(ip++);
5905 + DeleteNode(s);
5906 + text_buf[s] = c;
5907 + if (s < F - 1)
5908 + text_buf[s + N] = c;
5909 + s = (s + 1) & (N - 1);
5910 + r = (r + 1) & (N - 1);
5911 + InsertNode(r);
5912 + }
5913 + read += i;
5914 + while (i++ < last_match_length) {
5915 + DeleteNode(s);
5916 + s = (s + 1) & (N - 1);
5917 + r = (r + 1) & (N - 1);
5918 + if (--len) InsertNode(r);
5919 + }
5920 + } while (len > 0);
5921 + if (putlen) {
5922 + if (op >= dstend) {
5923 + *dstlen = written;
5924 + return -1;
5925 + }
5926 + *(op++) = putbuf >> 8;
5927 + written++;
5928 + *srclen = read;
5929 + }
5930 + *dstlen = written;
5931 + return 0;
5932 +}
5933 +
5934 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
5935 + unsigned long dstlen) /* Just the reverse of Encode(). */
5936 +{
5937 + unsigned long i, r, j, k, c;
5938 + unsigned char *ip, *op;
5939 + unsigned char *srcend = srcbuf + srclen;
5940 + unsigned char *dstend = dstbuf + dstlen;
5941 + unsigned short getbuf = 0;
5942 + uchar getlen = 0;
5943 + ip = srcbuf;
5944 + op = dstbuf;
5945 + StartHuff();
5946 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
5947 + r = N - F;
5948 + while (op < dstend) {
5949 + c = son[R];
5950 + /* travel from root to leaf, */
5951 + /* choosing the smaller child node (son[]) if the read bit is 0, */
5952 + /* the bigger (son[]+1} if 1 */
5953 + while (c < T) {
5954 + while (getlen <= 8) {
5955 + unsigned short t;
5956 + t = (ip >= srcend) ? 0 : *(ip++);
5957 + getbuf |= t << (8 - getlen);
5958 + getlen += 8;
5959 + }
5960 + c += ((signed short)getbuf < 0);
5961 + getbuf <<= 1;
5962 + getlen--;
5963 + c = son[c];
5964 + }
5965 + c -= T;
5966 + update(c);
5967 + if (c < 256) {
5968 + if (op >= dstend) return -1;
5969 + *(op++) = c;
5970 + text_buf[r++] = c;
5971 + r &= (N - 1);
5972 + } else {
5973 + j = c - 255 + THRESHOLD;
5974 + while (getlen <= 8) {
5975 + unsigned short t;
5976 + t = (ip >= srcend) ? 0 : *(ip++);
5977 + getbuf |= t << (8 - getlen);
5978 + getlen += 8;
5979 + }
5980 + i = getbuf >> 8;
5981 + getbuf <<= 8;
5982 + getlen -= 8;
5983 + c = d_code[i] << 6;
5984 + k = d_len[i];
5985 + /* read lower 6 bits verbatim */
5986 + k -= 2;
5987 + while (k--) {
5988 + while (getlen <= 8) {
5989 + unsigned short t;
5990 + t = (ip >= srcend) ? 0 : *(ip++);
5991 + getbuf |= t << (8 - getlen);
5992 + getlen += 8;
5993 + }
5994 + i = (i << 1) + ((signed short)getbuf < 0);
5995 + getbuf <<= 1;
5996 + getlen--;
5997 + }
5998 + i = c | (i & 0x3F);
5999 + i = r - i - 1;
6000 + i &= (N - 1);
6001 + for (k = 0; k < j; k++) {
6002 + c = text_buf[(i + k) & (N - 1)];
6003 + if (op >= dstend) return -1;
6004 + *(op++) = c;
6005 + text_buf[r++] = c;
6006 + r &= (N - 1);
6007 + }
6008 + }
6009 + }
6010 + return 0;
6011 +}
6012 +
6013 +/* interface to jffs2 bbc follows */
6014 +
6015 +#include "jffs2_bbc_framework.h"
6016 +
6017 +
6018 +#define JFFS2_BBC_LZHD_BLOCK_SIGN {0x3a, 0x98, 0xf7, 0xda}
6019 +
6020 +static int
6021 +jffs2_bbc_lzhd_compressor_init (void);
6022 +
6023 +static void
6024 +jffs2_bbc_lzhd_compressor_deinit (void);
6025 +
6026 +static int
6027 +jffs2_bbc_lzhd_compress (void *model, unsigned char *input,
6028 + unsigned char *output, unsigned long *sourcelen,
6029 + unsigned long *dstlen);
6030 +
6031 +static int
6032 +jffs2_bbc_lzhd_estimate (void *model, unsigned char *input,
6033 + unsigned long sourcelen, unsigned long *dstlen,
6034 + unsigned long *readtime, unsigned long *writetime);
6035 +
6036 +static int
6037 +jffs2_bbc_lzhd_decompress (void *model, unsigned char *input,
6038 + unsigned char *output, unsigned long sourcelen,
6039 + unsigned long dstlen);
6040 +
6041 +static char *
6042 +jffs2_bbc_lzhd_proc_info (void);
6043 +
6044 +static int
6045 +jffs2_bbc_lzhd_proc_command (char *command);
6046 +
6047 +struct jffs2_bbc_compressor_type jffs2_bbc_lzhd = {
6048 + "lzhd",
6049 + 0,
6050 + JFFS2_BBC_LZHD_BLOCK_SIGN,
6051 + jffs2_bbc_lzhd_compressor_init,
6052 + NULL,
6053 + NULL,
6054 + jffs2_bbc_lzhd_compressor_deinit,
6055 + jffs2_bbc_lzhd_compress,
6056 + jffs2_bbc_lzhd_estimate,
6057 + jffs2_bbc_lzhd_decompress,
6058 + jffs2_bbc_lzhd_proc_info,
6059 + jffs2_bbc_lzhd_proc_command
6060 +};
6061 +
6062 +static int
6063 +jffs2_bbc_lzhd_compressor_init (void)
6064 +{
6065 + return 0;
6066 +}
6067 +
6068 +static void
6069 +jffs2_bbc_lzhd_compressor_deinit (void)
6070 +{
6071 +}
6072 +
6073 +static int
6074 +jffs2_bbc_lzhd_compress (void *model, unsigned char *input,
6075 + unsigned char *output, unsigned long *sourcelen,
6076 + unsigned long *dstlen)
6077 +{
6078 + int retval;
6079 + unsigned long dst = *dstlen;
6080 + *(output++) = jffs2_bbc_lzhd.block_sign[0];
6081 + *(output++) = jffs2_bbc_lzhd.block_sign[1];
6082 + dst -= 2;
6083 + retval = Encode(input, output, sourcelen, &dst);
6084 + dst += 2;
6085 + *dstlen = dst;
6086 + return retval;
6087 +}
6088 +
6089 +static int
6090 +jffs2_bbc_lzhd_estimate (void *model, unsigned char *input,
6091 + unsigned long sourcelen, unsigned long *dstlen,
6092 + unsigned long *readtime, unsigned long *writetime)
6093 +{
6094 + *dstlen = sourcelen * 55 / 100;
6095 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 8;
6096 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 65 / 10;
6097 + return 0;
6098 +}
6099 +
6100 +static int
6101 +jffs2_bbc_lzhd_decompress (void *model, unsigned char *input,
6102 + unsigned char *output, unsigned long sourcelen,
6103 + unsigned long dstlen)
6104 +{
6105 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzhd.block_sign[0] ) ||
6106 + ( *(input++) != (unsigned char)jffs2_bbc_lzhd.block_sign[1] )
6107 + ) {
6108 + return -1;
6109 + } else {
6110 + return Decode(input, output, sourcelen - 2, dstlen);
6111 + }
6112 +}
6113 +
6114 +static char *
6115 +jffs2_bbc_lzhd_proc_info (void)
6116 +{
6117 + return "Lempel-Ziv-(dynamic) Huffman compression module";
6118 +}
6119 +
6120 +static int
6121 +jffs2_bbc_lzhd_proc_command (char *command)
6122 +{
6123 + return 0;
6124 +}
6125 +
6126 +struct jffs2_bbc_compressor_type *
6127 +jffs2_bbc_lzhd_init (int mode)
6128 +{
6129 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzhd) == 0)
6130 + {
6131 + return &jffs2_bbc_lzhd;
6132 + }
6133 + else
6134 + {
6135 + return NULL;
6136 + }
6137 +}
6138 +
6139 +void
6140 +jffs2_bbc_lzhd_deinit (void)
6141 +{
6142 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzhd);
6143 +}
6144 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzo_comp.c
6145 ===================================================================
6146 --- /dev/null
6147 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzo_comp.c
6148 @@ -0,0 +1,2435 @@
6149 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
6150 +
6151 +/*
6152 + jffs2_bbc_lzo_comp.c -- LZO1X-1 (and -999) compression module for jffs2
6153 + Copyright (C) 2004 Patrik Kluba
6154 + Based on the original LZO sources
6155 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
6156 +*/
6157 +
6158 +/*
6159 + Original copyright notice follows:
6160 +
6161 + lzo1x_9x.c -- implementation of the LZO1X-999 compression algorithm
6162 + lzo_ptr.h -- low-level pointer constructs
6163 + lzo_swd.ch -- sliding window dictionary
6164 + lzoconf.h -- configuration for the LZO real-time data compression library
6165 + lzo_mchw.ch -- matching functions using a window
6166 + minilzo.c -- mini subset of the LZO real-time data compression library
6167 + config1x.h -- configuration for the LZO1X algorithm
6168 + lzo1x.h -- public interface of the LZO1X compression algorithm
6169 +
6170 + These files are part of the LZO real-time data compression library.
6171 +
6172 + Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
6173 + All Rights Reserved.
6174 +
6175 + The LZO library is free software; you can redistribute it and/or
6176 + modify it under the terms of the GNU General Public License as
6177 + published by the Free Software Foundation; either version 2 of
6178 + the License, or (at your option) any later version.
6179 +
6180 + The LZO library is distributed in the hope that it will be useful,
6181 + but WITHOUT ANY WARRANTY; without even the implied warranty of
6182 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6183 + GNU General Public License for more details.
6184 +
6185 + You should have received a copy of the GNU General Public License
6186 + along with the LZO library; see the file COPYING.
6187 + If not, write to the Free Software Foundation, Inc.,
6188 + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
6189 +
6190 + Markus F.X.J. Oberhumer
6191 + <markus@oberhumer.com>
6192 +*/
6193 +
6194 +/*
6195 +
6196 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
6197 + Initial release
6198 + -removed all 16 bit code
6199 + -all sensitive data will be on 4 byte boundary
6200 + -removed check parts for library use
6201 + -removed all but LZO1X-* compression
6202 +
6203 +*/
6204 +
6205 +#ifndef __KERNEL__
6206 + #include <sys/types.h>
6207 + #include <stddef.h>
6208 + #include <string.h>
6209 + #include <limits.h>
6210 +#else
6211 + #include <linux/kernel.h>
6212 + #include <linux/types.h>
6213 + #include <linux/stddef.h>
6214 + #include <linux/string.h>
6215 + #define USHRT_MAX 65535
6216 + /* #define UINT_MAX 4294967295U */
6217 +#endif
6218 +
6219 +/* data type definitions */
6220 +#define U32 unsigned long
6221 +#define S32 signed long
6222 +#define I32 long
6223 +#define U16 unsigned short
6224 +#define S16 signed short
6225 +#define I16 short
6226 +#define U8 unsigned char
6227 +#define S8 signed char
6228 +#define I8 char
6229 +
6230 +/*************************************/
6231 +
6232 +/* lzo_swd.ch */
6233 +
6234 +#define SWD_N N
6235 +#define SWD_F F
6236 +#define SWD_THRESHOLD THRESHOLD
6237 +
6238 +/* shortest unsigned int that 2 * SWD_F + SWD_N (currently 53248) fits in */
6239 +typedef unsigned short swd_uint;
6240 +/* upper limit of that data type */
6241 +#define SWD_UINT_MAX USHRT_MAX
6242 +
6243 +/* minilzo.c */
6244 +
6245 +#define LZO_VERSION_DATE "Jul 12 2002"
6246 +#define LZO_VERSION_STRING "1.08"
6247 +#define LZO_VERSION 0x1080
6248 +
6249 +/* lzo_ptr.h */
6250 +
6251 +/* Integral types that have *exactly* the same number of bits as a lzo_voidp */
6252 +typedef unsigned long lzo_ptr_t;
6253 +typedef long lzo_sptr_t;
6254 +
6255 +
6256 +/*************************************/
6257 +
6258 +/* config1x.h */
6259 +
6260 +#define M1_MAX_OFFSET 0x0400
6261 +#define M2_MAX_OFFSET 0x0800
6262 +#define M3_MAX_OFFSET 0x4000
6263 +#define M4_MAX_OFFSET 0xbfff
6264 +
6265 +#define MX_MAX_OFFSET (M1_MAX_OFFSET + M2_MAX_OFFSET)
6266 +
6267 +#define M1_MIN_LEN 2
6268 +#define M1_MAX_LEN 2
6269 +#define M2_MIN_LEN 3
6270 +#define M2_MAX_LEN 8
6271 +#define M3_MIN_LEN 3
6272 +#define M3_MAX_LEN 33
6273 +#define M4_MIN_LEN 3
6274 +#define M4_MAX_LEN 9
6275 +
6276 +#define M1_MARKER 0
6277 +#define M2_MARKER 64
6278 +#define M3_MARKER 32
6279 +#define M4_MARKER 16
6280 +
6281 +#define MIN_LOOKAHEAD (M2_MAX_LEN + 1)
6282 +
6283 +/* minilzo.c */
6284 +
6285 +#define LZO_BYTE(x) ((unsigned char) ((x) & 0xff))
6286 +
6287 +#define LZO_MAX(a,b) ((a) >= (b) ? (a) : (b))
6288 +#define LZO_MIN(a,b) ((a) <= (b) ? (a) : (b))
6289 +#define LZO_MAX3(a,b,c) ((a) >= (b) ? LZO_MAX(a,c) : LZO_MAX(b,c))
6290 +#define LZO_MIN3(a,b,c) ((a) <= (b) ? LZO_MIN(a,c) : LZO_MIN(b,c))
6291 +
6292 +#define lzo_sizeof(type) ((lzo_uint) (sizeof(type)))
6293 +
6294 +#define LZO_HIGH(array) ((lzo_uint) (sizeof(array)/sizeof(*(array))))
6295 +
6296 +#define LZO_SIZE(bits) (1u << (bits))
6297 +#define LZO_MASK(bits) (LZO_SIZE(bits) - 1)
6298 +
6299 +#define LZO_LSIZE(bits) (1ul << (bits))
6300 +#define LZO_LMASK(bits) (LZO_LSIZE(bits) - 1)
6301 +
6302 +#define LZO_USIZE(bits) ((lzo_uint) 1 << (bits))
6303 +#define LZO_UMASK(bits) (LZO_USIZE(bits) - 1)
6304 +
6305 +#define LZO_STYPE_MAX(b) (((1l << (8*(b)-2)) - 1l) + (1l << (8*(b)-2)))
6306 +#define LZO_UTYPE_MAX(b) (((1ul << (8*(b)-1)) - 1ul) + (1ul << (8*(b)-1)))
6307 +
6308 +#define _LZO_STRINGIZE(x) #x
6309 +#define _LZO_MEXPAND(x) _LZO_STRINGIZE(x)
6310 +
6311 +#define _LZO_CONCAT2(a,b) a ## b
6312 +#define _LZO_CONCAT3(a,b,c) a ## b ## c
6313 +#define _LZO_CONCAT4(a,b,c,d) a ## b ## c ## d
6314 +#define _LZO_CONCAT5(a,b,c,d,e) a ## b ## c ## d ## e
6315 +
6316 +#define _LZO_ECONCAT2(a,b) _LZO_CONCAT2(a,b)
6317 +#define _LZO_ECONCAT3(a,b,c) _LZO_CONCAT3(a,b,c)
6318 +#define _LZO_ECONCAT4(a,b,c,d) _LZO_CONCAT4(a,b,c,d)
6319 +#define _LZO_ECONCAT5(a,b,c,d,e) _LZO_CONCAT5(a,b,c,d,e)
6320 +
6321 +#define lzo_dict_t const lzo_bytep
6322 +#define lzo_dict_p lzo_dict_t *
6323 +#define lzo_moff_t lzo_uint
6324 +
6325 +#define MEMCPY8_DS(dest,src,len) \
6326 + memcpy(dest,src,len); \
6327 + dest += len; \
6328 + src += len
6329 +
6330 +#define MEMCPY_DS(dest,src,len) \
6331 + do *dest++ = *src++; \
6332 + while (--len > 0)
6333 +
6334 +#define MEMMOVE_DS(dest,src,len) \
6335 + do *dest++ = *src++; \
6336 + while (--len > 0)
6337 +
6338 +#define BZERO8_PTR(s,l,n) memset((s),0,(lzo_uint)(l)*(n))
6339 +
6340 +#define LZO_BASE 65521u
6341 +#define LZO_NMAX 5552
6342 +
6343 +#define LZO_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
6344 +#define LZO_DO2(buf,i) LZO_DO1(buf,i); LZO_DO1(buf,i+1);
6345 +#define LZO_DO4(buf,i) LZO_DO2(buf,i); LZO_DO2(buf,i+2);
6346 +#define LZO_DO8(buf,i) LZO_DO4(buf,i); LZO_DO4(buf,i+4);
6347 +#define LZO_DO16(buf,i) LZO_DO8(buf,i); LZO_DO8(buf,i+8);
6348 +
6349 +#define IS_SIGNED(type) (((type) (-1)) < ((type) 0))
6350 +#define IS_UNSIGNED(type) (((type) (-1)) > ((type) 0))
6351 +
6352 +#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
6353 +
6354 +#define D_BITS 14
6355 +#define D_INDEX1(d,p) d = DM((0x21*DX3(p,5,5,6)) >> 5)
6356 +#define D_INDEX2(d,p) d = (d & (D_MASK & 0x7ff)) ^ (D_HIGH | 0x1f)
6357 +
6358 +#define LZO_HASH LZO_HASH_LZO_INCREMENTAL_B
6359 +
6360 +#define DL_MIN_LEN M2_MIN_LEN
6361 +
6362 +#define D_SIZE LZO_SIZE(D_BITS)
6363 +#define D_MASK LZO_MASK(D_BITS)
6364 +
6365 +#define D_HIGH ((D_MASK >> 1) + 1)
6366 +
6367 +#define DINDEX1 D_INDEX1
6368 +#define DINDEX2 D_INDEX2
6369 +
6370 +#define DX2(p,s1,s2) \
6371 + (((((lzo_uint32)((p)[2]) << (s2)) ^ (p)[1]) << (s1)) ^ (p)[0])
6372 +
6373 +#define DX3(p,s1,s2,s3) ((DX2((p)+1,s2,s3) << (s1)) ^ (p)[0])
6374 +#define DMS(v,s) ((lzo_uint) (((v) & (D_MASK >> (s))) << (s)))
6375 +#define DM(v) DMS(v,0)
6376 +
6377 +#define DENTRY(p,in) (p)
6378 +#define GINDEX(m_pos,m_off,dict,dindex,in) m_pos = dict[dindex]
6379 +
6380 +#define LZO_CHECK_MPOS_DET(m_pos,m_off,in,ip,max_offset) \
6381 + (m_pos == NULL || (m_off = (lzo_moff_t) (ip - m_pos)) > max_offset)
6382 +
6383 +#define LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
6384 + (BOUNDS_CHECKING_OFF_IN_EXPR( \
6385 + (PTR_LT(m_pos,in) || \
6386 + (m_off = (lzo_moff_t) PTR_DIFF(ip,m_pos)) <= 0 || \
6387 + m_off > max_offset) ))
6388 +
6389 +#define BOUNDS_CHECKING_OFF_IN_EXPR(expr) (expr)
6390 +
6391 +#define DD_BITS 0
6392 +#define DD_SIZE LZO_SIZE(DD_BITS)
6393 +#define DD_MASK LZO_MASK(DD_BITS)
6394 +
6395 +#define DL_BITS (D_BITS - DD_BITS)
6396 +#define DL_SIZE LZO_SIZE(DL_BITS)
6397 +#define DL_MASK LZO_MASK(DL_BITS)
6398 +
6399 +#define UPDATE_D(dict,drun,dv,p,in) dict[ DINDEX(dv,p) ] = DENTRY(p,in)
6400 +#define UPDATE_I(dict,drun,index,p,in) dict[index] = DENTRY(p,in)
6401 +#define UPDATE_P(ptr,drun,p,in) (ptr)[0] = DENTRY(p,in)
6402 +
6403 +#define __COPY4(dst,src) * (lzo_uint32p)(dst) = * (const lzo_uint32p)(src)
6404 +#define COPY4(dst,src) __COPY4((lzo_ptr_t)(dst),(lzo_ptr_t)(src))
6405 +
6406 +#define TEST_IP (ip < ip_end)
6407 +#define TEST_OP (op <= op_end)
6408 +
6409 +#define NEED_IP(x) \
6410 + if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x)) goto input_overrun
6411 +#define NEED_OP(x) \
6412 + if ((lzo_uint)(op_end - op) < (lzo_uint)(x)) goto output_overrun
6413 +#define TEST_LOOKBEHIND(m_pos,out) if (m_pos < out) goto lookbehind_overrun
6414 +
6415 +/* lzo1x_9x.c */
6416 +
6417 +#define LZO_UINT_MAX UINT_MAX
6418 +#define N M4_MAX_OFFSET
6419 +#define THRESHOLD 1
6420 +#define F 2048
6421 +
6422 +#define SWD_BEST_OFF (LZO_MAX3( M2_MAX_LEN, M3_MAX_LEN, M4_MAX_LEN ) + 1)
6423 +
6424 +/* ../include/lzoconf.h */
6425 +
6426 +typedef U32 lzo_uint32;
6427 +typedef I32 lzo_int32;
6428 +typedef U32 lzo_uint;
6429 +typedef I32 lzo_int;
6430 +typedef int lzo_bool;
6431 +
6432 +#define lzo_byte U8
6433 +#define lzo_bytep U8 *
6434 +#define lzo_charp char *
6435 +#define lzo_voidp void *
6436 +#define lzo_shortp short *
6437 +#define lzo_ushortp unsigned short *
6438 +#define lzo_uint32p lzo_uint32 *
6439 +#define lzo_int32p lzo_int32 *
6440 +#define lzo_uintp lzo_uint *
6441 +#define lzo_intp lzo_int *
6442 +#define lzo_voidpp lzo_voidp *
6443 +#define lzo_bytepp lzo_bytep *
6444 +#define lzo_sizeof_dict_t sizeof(lzo_bytep)
6445 +
6446 +#define LZO_E_OK 0
6447 +#define LZO_E_ERROR (-1)
6448 +#define LZO_E_OUT_OF_MEMORY (-2) /* not used right now */
6449 +#define LZO_E_NOT_COMPRESSIBLE (-3) /* not used right now */
6450 +#define LZO_E_INPUT_OVERRUN (-4)
6451 +#define LZO_E_OUTPUT_OVERRUN (-5)
6452 +#define LZO_E_LOOKBEHIND_OVERRUN (-6)
6453 +#define LZO_E_EOF_NOT_FOUND (-7)
6454 +#define LZO_E_INPUT_NOT_CONSUMED (-8)
6455 +
6456 +#define LZO_PTR_ALIGN_UP(_ptr,_size) \
6457 + ((_ptr) + (lzo_uint) __lzo_align_gap((const lzo_voidp)(_ptr),(lzo_uint)(_size)))
6458 +#define LZO_ALIGN(_ptr,_size) LZO_PTR_ALIGN_UP(_ptr,_size)
6459 +
6460 +typedef int
6461 + (*lzo_compress_t) (const lzo_byte * src, lzo_uint src_len,
6462 + lzo_byte * dst, lzo_uintp dst_len,
6463 + lzo_voidp wrkmem);
6464 +
6465 +typedef int
6466 + (*lzo_decompress_t) (const lzo_byte * src, lzo_uint src_len,
6467 + lzo_byte * dst, lzo_uintp dst_len,
6468 + lzo_voidp wrkmem);
6469 +
6470 +typedef int
6471 + (*lzo_optimize_t) (lzo_byte * src, lzo_uint src_len,
6472 + lzo_byte * dst, lzo_uintp dst_len,
6473 + lzo_voidp wrkmem);
6474 +
6475 +typedef int
6476 + (*lzo_compress_dict_t) (const lzo_byte * src, lzo_uint src_len,
6477 + lzo_byte * dst, lzo_uintp dst_len,
6478 + lzo_voidp wrkmem,
6479 + const lzo_byte * dict, lzo_uint dict_len);
6480 +
6481 +typedef int
6482 + (*lzo_decompress_dict_t) (const lzo_byte * src, lzo_uint src_len,
6483 + lzo_byte * dst, lzo_uintp dst_len,
6484 + lzo_voidp wrkmem,
6485 + const lzo_byte * dict, lzo_uint dict_len);
6486 +
6487 +typedef int
6488 + (*lzo_compress_asm_t) (const lzo_byte * src, lzo_uint src_len,
6489 + lzo_byte * dst, lzo_uintp dst_len,
6490 + lzo_voidp wrkmem);
6491 +
6492 +typedef int
6493 + (*lzo_decompress_asm_t) (const lzo_byte * src, lzo_uint src_len,
6494 + lzo_byte * dst, lzo_uintp dst_len,
6495 + lzo_voidp wrkmem);
6496 +
6497 +typedef void (*lzo_progress_callback_t) (lzo_uint, lzo_uint);
6498 +
6499 +typedef union
6500 +{
6501 + lzo_bytep p;
6502 + lzo_uint u;
6503 +} __lzo_pu_u;
6504 +typedef union
6505 +{
6506 + lzo_bytep p;
6507 + lzo_uint32 u32;
6508 +} __lzo_pu32_u;
6509 +typedef union
6510 +{
6511 + void *vp;
6512 + lzo_bytep bp;
6513 + lzo_uint32 u32;
6514 + long l;
6515 +} lzo_align_t;
6516 +
6517 +/* lzo1x.h */
6518 +
6519 +#define LZO1X_1_MEM_COMPRESS ((lzo_uint32) (16384L * lzo_sizeof_dict_t))
6520 +#define LZO1X_999_MEM_COMPRESS ((lzo_uint32) (14 * 16384L * sizeof(short)))
6521 +
6522 +/* lzo_ptr.h */
6523 +
6524 +#define PTR(a) ((lzo_ptr_t) (a))
6525 +#define PTR_LINEAR(a) PTR(a)
6526 +#define PTR_ALIGNED_4(a) ((PTR_LINEAR(a) & 3) == 0)
6527 +#define PTR_ALIGNED_8(a) ((PTR_LINEAR(a) & 7) == 0)
6528 +#define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
6529 +#define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
6530 +#define PTR_LT(a,b) (PTR(a) < PTR(b))
6531 +#define PTR_GE(a,b) (PTR(a) >= PTR(b))
6532 +#define PTR_DIFF(a,b) ((lzo_ptrdiff_t) (PTR(a) - PTR(b)))
6533 +#define pd(a,b) ((lzo_uint) ((a)-(b)))
6534 +
6535 +typedef ptrdiff_t lzo_ptrdiff_t;
6536 +
6537 +typedef union
6538 +{
6539 + char a_char;
6540 + unsigned char a_uchar;
6541 + short a_short;
6542 + unsigned short a_ushort;
6543 + int a_int;
6544 + unsigned int a_uint;
6545 + long a_long;
6546 + unsigned long a_ulong;
6547 + lzo_int a_lzo_int;
6548 + lzo_uint a_lzo_uint;
6549 + lzo_int32 a_lzo_int32;
6550 + lzo_uint32 a_lzo_uint32;
6551 + ptrdiff_t a_ptrdiff_t;
6552 + lzo_ptrdiff_t a_lzo_ptrdiff_t;
6553 + lzo_ptr_t a_lzo_ptr_t;
6554 + lzo_voidp a_lzo_voidp;
6555 + void *a_void_p;
6556 + lzo_bytep a_lzo_bytep;
6557 + lzo_bytepp a_lzo_bytepp;
6558 + lzo_uintp a_lzo_uintp;
6559 + lzo_uint *a_lzo_uint_p;
6560 + lzo_uint32p a_lzo_uint32p;
6561 + lzo_uint32 *a_lzo_uint32_p;
6562 + unsigned char *a_uchar_p;
6563 + char *a_char_p;
6564 +}
6565 +lzo_full_align_t;
6566 +
6567 +/* lzo_mchw.ch */
6568 +
6569 +typedef struct
6570 +{
6571 + int init;
6572 +
6573 + lzo_uint look;
6574 +
6575 + lzo_uint m_len;
6576 + lzo_uint m_off;
6577 +
6578 + lzo_uint last_m_len;
6579 + lzo_uint last_m_off;
6580 +
6581 + const lzo_byte *bp;
6582 + const lzo_byte *ip;
6583 + const lzo_byte *in;
6584 + const lzo_byte *in_end;
6585 + lzo_byte *out;
6586 +
6587 + lzo_progress_callback_t cb;
6588 +
6589 + lzo_uint textsize;
6590 + lzo_uint codesize;
6591 + lzo_uint printcount;
6592 +
6593 + unsigned long lit_bytes;
6594 + unsigned long match_bytes;
6595 + unsigned long rep_bytes;
6596 + unsigned long lazy;
6597 +
6598 + lzo_uint r1_lit;
6599 + lzo_uint r1_m_len;
6600 +
6601 + unsigned long m1a_m, m1b_m, m2_m, m3_m, m4_m;
6602 + unsigned long lit1_r, lit2_r, lit3_r;
6603 +}
6604 +lzo1x_999_t;
6605 +
6606 +#define getbyte(c) ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
6607 +
6608 +/* lzo_swd.ch */
6609 +
6610 +#define SWD_UINT(x) ((swd_uint)(x))
6611 +#define SWD_HSIZE 16384
6612 +#define SWD_MAX_CHAIN 2048
6613 +#define HEAD3(b,p) \
6614 + (((0x9f5f*(((((lzo_uint32)b[p]<<5)^b[p+1])<<5)^b[p+2]))>>5) & (SWD_HSIZE-1))
6615 +#define HEAD2(b,p) (b[p] ^ ((unsigned)b[p+1]<<8))
6616 +#define NIL2 SWD_UINT_MAX
6617 +
6618 +typedef struct
6619 +{
6620 + lzo_uint n;
6621 + lzo_uint f;
6622 + lzo_uint threshold;
6623 +
6624 + lzo_uint max_chain;
6625 + lzo_uint nice_length;
6626 + lzo_bool use_best_off;
6627 + lzo_uint lazy_insert;
6628 +
6629 + lzo_uint m_len;
6630 + lzo_uint m_off;
6631 + lzo_uint look;
6632 + int b_char;
6633 +
6634 + lzo_uint best_off[SWD_BEST_OFF];
6635 +
6636 + lzo1x_999_t *c;
6637 + lzo_uint m_pos;
6638 +
6639 + lzo_uint best_pos[SWD_BEST_OFF];
6640 +
6641 + const lzo_byte *dict;
6642 + const lzo_byte *dict_end;
6643 + lzo_uint dict_len;
6644 +
6645 + lzo_uint ip;
6646 + lzo_uint bp;
6647 + lzo_uint rp;
6648 + lzo_uint b_size;
6649 +
6650 + unsigned char *b_wrap;
6651 +
6652 + lzo_uint node_count;
6653 + lzo_uint first_rp;
6654 +
6655 + unsigned char b[SWD_N + SWD_F + SWD_F];
6656 + swd_uint head3[SWD_HSIZE];
6657 + swd_uint succ3[SWD_N + SWD_F];
6658 + swd_uint best3[SWD_N + SWD_F];
6659 + swd_uint llen3[SWD_HSIZE];
6660 +
6661 + swd_uint head2[65536L];
6662 +}
6663 +lzo1x_999_swd_t;
6664 +
6665 +#define s_head3(s,key) s->head3[key]
6666 +#define swd_pos2off(s,pos) \
6667 + (s->bp > (pos) ? s->bp - (pos) : s->b_size - ((pos) - s->bp))
6668 +
6669 +static __inline__ void
6670 +swd_getbyte (lzo1x_999_swd_t * s)
6671 +{
6672 + int c;
6673 +
6674 + if ((c = getbyte (*(s->c))) < 0)
6675 + {
6676 + if (s->look > 0)
6677 + --s->look;
6678 + }
6679 + else
6680 + {
6681 + s->b[s->ip] = LZO_BYTE (c);
6682 + if (s->ip < s->f)
6683 + s->b_wrap[s->ip] = LZO_BYTE (c);
6684 + }
6685 + if (++s->ip == s->b_size)
6686 + s->ip = 0;
6687 + if (++s->bp == s->b_size)
6688 + s->bp = 0;
6689 + if (++s->rp == s->b_size)
6690 + s->rp = 0;
6691 +}
6692 +
6693 +static void
6694 +swd_initdict (lzo1x_999_swd_t * s, const lzo_byte * dict, lzo_uint dict_len)
6695 +{
6696 + s->dict = s->dict_end = NULL;
6697 + s->dict_len = 0;
6698 +
6699 + if (!dict || dict_len <= 0)
6700 + return;
6701 + if (dict_len > s->n)
6702 + {
6703 + dict += dict_len - s->n;
6704 + dict_len = s->n;
6705 + }
6706 +
6707 + s->dict = dict;
6708 + s->dict_len = dict_len;
6709 + s->dict_end = dict + dict_len;
6710 + memcpy (s->b, dict, dict_len);
6711 + s->ip = dict_len;
6712 +}
6713 +
6714 +static void
6715 +swd_insertdict (lzo1x_999_swd_t * s, lzo_uint node, lzo_uint len)
6716 +{
6717 + lzo_uint key;
6718 +
6719 + s->node_count = s->n - len;
6720 + s->first_rp = node;
6721 +
6722 + while (len-- > 0)
6723 + {
6724 + key = HEAD3 (s->b, node);
6725 + s->succ3[node] = s_head3 (s, key);
6726 + s->head3[key] = SWD_UINT (node);
6727 + s->best3[node] = SWD_UINT (s->f + 1);
6728 + s->llen3[key]++;
6729 +
6730 + key = HEAD2 (s->b, node);
6731 + s->head2[key] = SWD_UINT (node);
6732 +
6733 + node++;
6734 + }
6735 +}
6736 +
6737 +static int
6738 +swd_init (lzo1x_999_swd_t * s, const lzo_byte * dict, lzo_uint dict_len)
6739 +{
6740 +
6741 + s->n = SWD_N;
6742 + s->f = SWD_F;
6743 + s->threshold = SWD_THRESHOLD;
6744 +
6745 +
6746 +
6747 + s->max_chain = SWD_MAX_CHAIN;
6748 + s->nice_length = SWD_F;
6749 + s->use_best_off = 0;
6750 + s->lazy_insert = 0;
6751 +
6752 + s->b_size = s->n + s->f;
6753 + if (2 * s->f >= s->n || s->b_size + s->f >= NIL2)
6754 + return LZO_E_ERROR;
6755 + s->b_wrap = s->b + s->b_size;
6756 + s->node_count = s->n;
6757 +
6758 + memset (s->llen3, 0, sizeof (s->llen3[0]) * SWD_HSIZE);
6759 + memset (s->head2, 0xff, sizeof (s->head2[0]) * 65536L);
6760 +
6761 + s->ip = 0;
6762 + swd_initdict (s, dict, dict_len);
6763 + s->bp = s->ip;
6764 + s->first_rp = s->ip;
6765 +
6766 + s->look = (lzo_uint) (s->c->in_end - s->c->ip);
6767 + if (s->look > 0)
6768 + {
6769 + if (s->look > s->f)
6770 + s->look = s->f;
6771 + memcpy (&s->b[s->ip], s->c->ip, s->look);
6772 + s->c->ip += s->look;
6773 + s->ip += s->look;
6774 + }
6775 +
6776 + if (s->ip == s->b_size)
6777 + s->ip = 0;
6778 +
6779 + if (s->look >= 2 && s->dict_len > 0)
6780 + swd_insertdict (s, 0, s->dict_len);
6781 +
6782 + s->rp = s->first_rp;
6783 + if (s->rp >= s->node_count)
6784 + s->rp -= s->node_count;
6785 + else
6786 + s->rp += s->b_size - s->node_count;
6787 +
6788 + return LZO_E_OK;
6789 +}
6790 +
6791 +static __inline__ void
6792 +swd_remove_node (lzo1x_999_swd_t * s, lzo_uint node)
6793 +{
6794 + if (s->node_count == 0)
6795 + {
6796 + lzo_uint key;
6797 +
6798 + key = HEAD3 (s->b, node);
6799 +
6800 + --s->llen3[key];
6801 +
6802 + key = HEAD2 (s->b, node);
6803 +
6804 + if ((lzo_uint) s->head2[key] == node)
6805 + s->head2[key] = NIL2;
6806 + }
6807 + else
6808 + --s->node_count;
6809 +}
6810 +
6811 +static void
6812 +swd_accept (lzo1x_999_swd_t * s, lzo_uint n)
6813 +{
6814 +
6815 + while (n--)
6816 + {
6817 + lzo_uint key;
6818 +
6819 + swd_remove_node (s, s->rp);
6820 +
6821 + key = HEAD3 (s->b, s->bp);
6822 + s->succ3[s->bp] = s_head3 (s, key);
6823 + s->head3[key] = SWD_UINT (s->bp);
6824 + s->best3[s->bp] = SWD_UINT (s->f + 1);
6825 + s->llen3[key]++;
6826 +
6827 + key = HEAD2 (s->b, s->bp);
6828 + s->head2[key] = SWD_UINT (s->bp);;
6829 +
6830 + swd_getbyte (s);
6831 + }
6832 +}
6833 +
6834 +static void
6835 +swd_search (lzo1x_999_swd_t * s, lzo_uint node, lzo_uint cnt)
6836 +{
6837 + const unsigned char *p1;
6838 + const unsigned char *p2;
6839 + const unsigned char *px;
6840 +
6841 + lzo_uint m_len = s->m_len;
6842 + const unsigned char *b = s->b;
6843 + const unsigned char *bp = s->b + s->bp;
6844 + const unsigned char *bx = s->b + s->bp + s->look;
6845 + unsigned char scan_end1;
6846 +
6847 + scan_end1 = bp[m_len - 1];
6848 + for (; cnt-- > 0; node = s->succ3[node])
6849 + {
6850 + p1 = bp;
6851 + p2 = b + node;
6852 + px = bx;
6853 +
6854 + if (p2[m_len - 1] == scan_end1 &&
6855 + p2[m_len] == p1[m_len] &&
6856 + p2[0] == p1[0] && p2[1] == p1[1])
6857 + {
6858 + lzo_uint i;
6859 +
6860 + p1 += 2;
6861 + p2 += 2;
6862 + do
6863 + {
6864 + }
6865 + while (++p1 < px && *p1 == *++p2);
6866 +
6867 + i = p1 - bp;
6868 +
6869 + if (i < SWD_BEST_OFF)
6870 + {
6871 + if (s->best_pos[i] == 0)
6872 + s->best_pos[i] = node + 1;
6873 + }
6874 +
6875 + if (i > m_len)
6876 + {
6877 + s->m_len = m_len = i;
6878 + s->m_pos = node;
6879 + if (m_len == s->look)
6880 + return;
6881 + if (m_len >= s->nice_length)
6882 + return;
6883 + if (m_len > (lzo_uint) s->best3[node])
6884 + return;
6885 + scan_end1 = bp[m_len - 1];
6886 + }
6887 + }
6888 + }
6889 +}
6890 +
6891 +static lzo_bool
6892 +swd_search2 (lzo1x_999_swd_t * s)
6893 +{
6894 + lzo_uint key;
6895 +
6896 + key = s->head2[HEAD2 (s->b, s->bp)];
6897 + if (key == NIL2)
6898 + return 0;
6899 +
6900 + if (s->best_pos[2] == 0)
6901 + s->best_pos[2] = key + 1;
6902 +
6903 + if (s->m_len < 2)
6904 + {
6905 + s->m_len = 2;
6906 + s->m_pos = key;
6907 + }
6908 + return 1;
6909 +}
6910 +
6911 +static void
6912 +swd_findbest (lzo1x_999_swd_t * s)
6913 +{
6914 + lzo_uint key;
6915 + lzo_uint cnt, node;
6916 + lzo_uint len;
6917 +
6918 + key = HEAD3 (s->b, s->bp);
6919 + node = s->succ3[s->bp] = s_head3 (s, key);
6920 + cnt = s->llen3[key]++;
6921 +
6922 + if (cnt > s->max_chain && s->max_chain > 0)
6923 + cnt = s->max_chain;
6924 + s->head3[key] = SWD_UINT (s->bp);
6925 +
6926 + s->b_char = s->b[s->bp];
6927 + len = s->m_len;
6928 + if (s->m_len >= s->look)
6929 + {
6930 + if (s->look == 0)
6931 + s->b_char = -1;
6932 + s->m_off = 0;
6933 + s->best3[s->bp] = SWD_UINT (s->f + 1);
6934 + }
6935 + else
6936 + {
6937 +
6938 + if (swd_search2 (s))
6939 +
6940 + if (s->look >= 3)
6941 + swd_search (s, node, cnt);
6942 + if (s->m_len > len)
6943 + s->m_off = swd_pos2off (s, s->m_pos);
6944 + s->best3[s->bp] = SWD_UINT (s->m_len);
6945 +
6946 + if (s->use_best_off)
6947 + {
6948 + int i;
6949 + for (i = 2; i < SWD_BEST_OFF; i++)
6950 + if (s->best_pos[i] > 0)
6951 + s->best_off[i] =
6952 + swd_pos2off (s,
6953 + s->best_pos[i] -
6954 + 1);
6955 + else
6956 + s->best_off[i] = 0;
6957 + }
6958 +
6959 + }
6960 +
6961 + swd_remove_node (s, s->rp);
6962 +
6963 + key = HEAD2 (s->b, s->bp);
6964 + s->head2[key] = SWD_UINT (s->bp);
6965 +
6966 +}
6967 +
6968 +/* lzo_mchw.ch */
6969 +
6970 +static int
6971 +init_match (lzo1x_999_t * c, lzo1x_999_swd_t * s,
6972 + const lzo_byte * dict, lzo_uint dict_len, lzo_uint32 flags)
6973 +{
6974 + int r;
6975 +
6976 + c->init = 1;
6977 +
6978 + s->c = c;
6979 +
6980 + c->last_m_len = c->last_m_off = 0;
6981 +
6982 + c->textsize = c->codesize = c->printcount = 0;
6983 + c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
6984 + c->lazy = 0;
6985 +
6986 + r = swd_init (s, dict, dict_len);
6987 + if (r != 0)
6988 + return r;
6989 +
6990 + s->use_best_off = (flags & 1) ? 1 : 0;
6991 + return r;
6992 +}
6993 +
6994 +static int
6995 +find_match (lzo1x_999_t * c, lzo1x_999_swd_t * s,
6996 + lzo_uint this_len, lzo_uint skip)
6997 +{
6998 + if (skip > 0)
6999 + {
7000 + swd_accept (s, this_len - skip);
7001 + c->textsize += this_len - skip + 1;
7002 + }
7003 + else
7004 + {
7005 + c->textsize += this_len - skip;
7006 + }
7007 +
7008 + s->m_len = 1;
7009 + s->m_len = 1;
7010 +
7011 + if (s->use_best_off)
7012 + memset (s->best_pos, 0, sizeof (s->best_pos));
7013 +
7014 + swd_findbest (s);
7015 + c->m_len = s->m_len;
7016 + c->m_off = s->m_off;
7017 +
7018 + swd_getbyte (s);
7019 +
7020 + if (s->b_char < 0)
7021 + {
7022 + c->look = 0;
7023 + c->m_len = 0;
7024 + }
7025 + else
7026 + {
7027 + c->look = s->look + 1;
7028 + }
7029 + c->bp = c->ip - c->look;
7030 +
7031 + if (c->cb && c->textsize > c->printcount)
7032 + {
7033 + (*c->cb) (c->textsize, c->codesize);
7034 + c->printcount += 1024;
7035 + }
7036 +
7037 + return LZO_E_OK;
7038 +}
7039 +
7040 +/* lzo1x_9x.c */
7041 +
7042 +static lzo_byte *
7043 +code_match (lzo1x_999_t * c, lzo_byte * op, lzo_uint m_len, lzo_uint m_off)
7044 +{
7045 + lzo_uint x_len = m_len;
7046 + lzo_uint x_off = m_off;
7047 +
7048 + c->match_bytes += m_len;
7049 +
7050 + if (m_len == 2)
7051 + {
7052 + m_off -= 1;
7053 +
7054 + *op++ = LZO_BYTE (M1_MARKER | ((m_off & 3) << 2));
7055 + *op++ = LZO_BYTE (m_off >> 2);
7056 +
7057 + c->m1a_m++;
7058 + }
7059 +
7060 + else if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
7061 +
7062 + {
7063 +
7064 + m_off -= 1;
7065 + *op++ = LZO_BYTE (((m_len - 1) << 5) | ((m_off & 7) << 2));
7066 + *op++ = LZO_BYTE (m_off >> 3);
7067 + c->m2_m++;
7068 + }
7069 + else if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET
7070 + && c->r1_lit >= 4)
7071 + {
7072 + m_off -= 1 + M2_MAX_OFFSET;
7073 +
7074 + *op++ = LZO_BYTE (M1_MARKER | ((m_off & 3) << 2));
7075 + *op++ = LZO_BYTE (m_off >> 2);
7076 +
7077 + c->m1b_m++;
7078 + }
7079 + else if (m_off <= M3_MAX_OFFSET)
7080 + {
7081 + m_off -= 1;
7082 + if (m_len <= M3_MAX_LEN)
7083 + *op++ = LZO_BYTE (M3_MARKER | (m_len - 2));
7084 + else
7085 + {
7086 + m_len -= M3_MAX_LEN;
7087 + *op++ = M3_MARKER | 0;
7088 + while (m_len > 255)
7089 + {
7090 + m_len -= 255;
7091 + *op++ = 0;
7092 + }
7093 + *op++ = LZO_BYTE (m_len);
7094 + }
7095 +
7096 + *op++ = LZO_BYTE (m_off << 2);
7097 + *op++ = LZO_BYTE (m_off >> 6);
7098 +
7099 + c->m3_m++;
7100 + }
7101 + else
7102 + {
7103 + lzo_uint k;
7104 +
7105 + m_off -= 0x4000;
7106 + k = (m_off & 0x4000) >> 11;
7107 + if (m_len <= M4_MAX_LEN)
7108 + *op++ = LZO_BYTE (M4_MARKER | k | (m_len - 2));
7109 + else
7110 + {
7111 + m_len -= M4_MAX_LEN;
7112 + *op++ = LZO_BYTE (M4_MARKER | k | 0);
7113 + while (m_len > 255)
7114 + {
7115 + m_len -= 255;
7116 + *op++ = 0;
7117 + }
7118 + *op++ = LZO_BYTE (m_len);
7119 + }
7120 +
7121 + *op++ = LZO_BYTE (m_off << 2);
7122 + *op++ = LZO_BYTE (m_off >> 6);
7123 +
7124 + c->m4_m++;
7125 + }
7126 +
7127 + c->last_m_len = x_len;
7128 + c->last_m_off = x_off;
7129 + return op;
7130 +}
7131 +
7132 +static lzo_byte *
7133 +STORE_RUN (lzo1x_999_t * c, lzo_byte * op, const lzo_byte * ii, lzo_uint t)
7134 +{
7135 + c->lit_bytes += t;
7136 +
7137 + if (op == c->out && t <= 238)
7138 + {
7139 + *op++ = LZO_BYTE (17 + t);
7140 + }
7141 + else if (t <= 3)
7142 + {
7143 + op[-2] |= LZO_BYTE (t);
7144 +
7145 + c->lit1_r++;
7146 + }
7147 + else if (t <= 18)
7148 + {
7149 + *op++ = LZO_BYTE (t - 3);
7150 + c->lit2_r++;
7151 + }
7152 + else
7153 + {
7154 + lzo_uint tt = t - 18;
7155 +
7156 + *op++ = 0;
7157 + while (tt > 255)
7158 + {
7159 + tt -= 255;
7160 + *op++ = 0;
7161 + }
7162 + *op++ = LZO_BYTE (tt);
7163 + c->lit3_r++;
7164 + }
7165 + do
7166 + *op++ = *ii++;
7167 + while (--t > 0);
7168 +
7169 + return op;
7170 +}
7171 +
7172 +static lzo_byte *
7173 +code_run (lzo1x_999_t * c, lzo_byte * op, const lzo_byte * ii,
7174 + lzo_uint lit, lzo_uint m_len)
7175 +{
7176 + if (lit > 0)
7177 + {
7178 + op = STORE_RUN (c, op, ii, lit);
7179 + c->r1_m_len = m_len;
7180 + c->r1_lit = lit;
7181 + }
7182 + else
7183 + {
7184 + c->r1_m_len = 0;
7185 + c->r1_lit = 0;
7186 + }
7187 +
7188 + return op;
7189 +}
7190 +
7191 +static int
7192 +len_of_coded_match (lzo_uint m_len, lzo_uint m_off, lzo_uint lit)
7193 +{
7194 + int n = 4;
7195 +
7196 + if (m_len < 2)
7197 + return -1;
7198 + if (m_len == 2)
7199 + return (m_off <= M1_MAX_OFFSET && lit > 0
7200 + && lit < 4) ? 2 : -1;
7201 + if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
7202 + return 2;
7203 + if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4)
7204 + return 2;
7205 + if (m_off <= M3_MAX_OFFSET)
7206 + {
7207 + if (m_len <= M3_MAX_LEN)
7208 + return 3;
7209 + m_len -= M3_MAX_LEN;
7210 + while (m_len > 255)
7211 + {
7212 + m_len -= 255;
7213 + n++;
7214 + }
7215 + return n;
7216 + }
7217 + if (m_off <= M4_MAX_OFFSET)
7218 + {
7219 + if (m_len <= M4_MAX_LEN)
7220 + return 3;
7221 + m_len -= M4_MAX_LEN;
7222 + while (m_len > 255)
7223 + {
7224 + m_len -= 255;
7225 + n++;
7226 + }
7227 + return n;
7228 + }
7229 + return -1;
7230 +}
7231 +
7232 +static lzo_int
7233 +min_gain (lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, int l1, int l2,
7234 + int l3)
7235 +{
7236 + lzo_int lazy_match_min_gain = 0;
7237 +
7238 + lazy_match_min_gain += ahead;
7239 +
7240 + if (lit1 <= 3)
7241 + lazy_match_min_gain += (lit2 <= 3) ? 0 : 2;
7242 + else if (lit1 <= 18)
7243 + lazy_match_min_gain += (lit2 <= 18) ? 0 : 1;
7244 +
7245 + lazy_match_min_gain += (l2 - l1) * 2;
7246 + if (l3 > 0)
7247 + lazy_match_min_gain -= (ahead - l3) * 2;
7248 +
7249 + if (lazy_match_min_gain < 0)
7250 + lazy_match_min_gain = 0;
7251 +
7252 + return lazy_match_min_gain;
7253 +}
7254 +
7255 +static void
7256 +better_match (const lzo1x_999_swd_t * swd, lzo_uint * m_len, lzo_uint * m_off)
7257 +{
7258 + if (*m_len <= M2_MIN_LEN)
7259 + return;
7260 +
7261 + if (*m_off <= M2_MAX_OFFSET)
7262 + return;
7263 +
7264 + if (*m_off > M2_MAX_OFFSET &&
7265 + *m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
7266 + swd->best_off[*m_len - 1]
7267 + && swd->best_off[*m_len - 1] <= M2_MAX_OFFSET)
7268 + {
7269 + *m_len = *m_len - 1;
7270 + *m_off = swd->best_off[*m_len];
7271 + return;
7272 + }
7273 +
7274 + if (*m_off > M3_MAX_OFFSET &&
7275 + *m_len >= M4_MAX_LEN + 1 && *m_len <= M2_MAX_LEN + 2 &&
7276 + swd->best_off[*m_len - 2]
7277 + && swd->best_off[*m_len - 2] <= M2_MAX_OFFSET)
7278 + {
7279 + *m_len = *m_len - 2;
7280 + *m_off = swd->best_off[*m_len];
7281 + return;
7282 + }
7283 +
7284 + if (*m_off > M3_MAX_OFFSET &&
7285 + *m_len >= M4_MAX_LEN + 1 && *m_len <= M3_MAX_LEN + 1 &&
7286 + swd->best_off[*m_len - 1]
7287 + && swd->best_off[*m_len - 1] <= M3_MAX_OFFSET)
7288 + {
7289 + *m_len = *m_len - 1;
7290 + *m_off = swd->best_off[*m_len];
7291 + }
7292 +
7293 +}
7294 +
7295 +/* minilzo.c */
7296 +
7297 +static lzo_bool
7298 +lzo_assert (int expr)
7299 +{
7300 + return (expr) ? 1 : 0;
7301 +}
7302 +
7303 +/* lzo1x_9x.c */
7304 +
7305 +static int
7306 +lzo1x_999_compress_internal (const lzo_byte * in, lzo_uint in_len,
7307 + lzo_byte * out, lzo_uintp out_len,
7308 + lzo_voidp wrkmem,
7309 + const lzo_byte * dict, lzo_uint dict_len,
7310 + lzo_progress_callback_t cb,
7311 + int try_lazy,
7312 + lzo_uint good_length,
7313 + lzo_uint max_lazy,
7314 + lzo_uint nice_length,
7315 + lzo_uint max_chain, lzo_uint32 flags)
7316 +{
7317 + lzo_byte *op;
7318 + const lzo_byte *ii;
7319 + lzo_uint lit;
7320 + lzo_uint m_len, m_off;
7321 + lzo1x_999_t cc;
7322 + lzo1x_999_t *const c = &cc;
7323 + lzo1x_999_swd_t *const swd = (lzo1x_999_swd_t *) wrkmem;
7324 + int r;
7325 +
7326 + if (!lzo_assert
7327 + (LZO1X_999_MEM_COMPRESS >= lzo_sizeof (lzo1x_999_swd_t)))
7328 + return LZO_E_ERROR;
7329 +
7330 + if (try_lazy < 0)
7331 + try_lazy = 1;
7332 +
7333 + if (good_length <= 0)
7334 + good_length = 32;
7335 +
7336 + if (max_lazy <= 0)
7337 + max_lazy = 32;
7338 +
7339 + if (nice_length <= 0)
7340 + nice_length = 0;
7341 +
7342 + if (max_chain <= 0)
7343 + max_chain = SWD_MAX_CHAIN;
7344 +
7345 + c->init = 0;
7346 + c->ip = c->in = in;
7347 + c->in_end = in + in_len;
7348 + c->out = out;
7349 + c->cb = cb;
7350 + c->m1a_m = c->m1b_m = c->m2_m = c->m3_m = c->m4_m = 0;
7351 + c->lit1_r = c->lit2_r = c->lit3_r = 0;
7352 +
7353 + op = out;
7354 + ii = c->ip;
7355 + lit = 0;
7356 + c->r1_lit = c->r1_m_len = 0;
7357 +
7358 + r = init_match (c, swd, dict, dict_len, flags);
7359 + if (r != 0)
7360 + return r;
7361 + if (max_chain > 0)
7362 + swd->max_chain = max_chain;
7363 + if (nice_length > 0)
7364 + swd->nice_length = nice_length;
7365 +
7366 + r = find_match (c, swd, 0, 0);
7367 + if (r != 0)
7368 + return r;
7369 + while (c->look > 0)
7370 + {
7371 + lzo_uint ahead;
7372 + lzo_uint max_ahead;
7373 + int l1, l2, l3;
7374 +
7375 + c->codesize = op - out;
7376 +
7377 + m_len = c->m_len;
7378 + m_off = c->m_off;
7379 +
7380 + if (lit == 0)
7381 + ii = c->bp;
7382 +
7383 + if (m_len < 2 ||
7384 + (m_len == 2
7385 + && (m_off > M1_MAX_OFFSET || lit == 0 || lit >= 4))
7386 + || (m_len == 2 && op == out) || (op == out && lit == 0))
7387 + {
7388 +
7389 + m_len = 0;
7390 + }
7391 + else if (m_len == M2_MIN_LEN)
7392 + {
7393 +
7394 + if (m_off > MX_MAX_OFFSET && lit >= 4)
7395 + m_len = 0;
7396 + }
7397 +
7398 + if (m_len == 0)
7399 + {
7400 +
7401 + lit++;
7402 + swd->max_chain = max_chain;
7403 + r = find_match (c, swd, 1, 0);
7404 + continue;
7405 + }
7406 +
7407 + if (swd->use_best_off)
7408 + better_match (swd, &m_len, &m_off);
7409 +
7410 + ahead = 0;
7411 + if (try_lazy <= 0 || m_len >= max_lazy)
7412 + {
7413 +
7414 + l1 = 0;
7415 + max_ahead = 0;
7416 + }
7417 + else
7418 + {
7419 +
7420 + l1 = len_of_coded_match (m_len, m_off, lit);
7421 +
7422 + max_ahead = LZO_MIN (try_lazy, l1 - 1);
7423 +
7424 + }
7425 +
7426 + while (ahead < max_ahead && c->look > m_len)
7427 + {
7428 + lzo_int lazy_match_min_gain;
7429 +
7430 + if (m_len >= good_length)
7431 + swd->max_chain = max_chain >> 2;
7432 + else
7433 + swd->max_chain = max_chain;
7434 + r = find_match (c, swd, 1, 0);
7435 + ahead++;
7436 +
7437 + if (c->m_len < m_len)
7438 + continue;
7439 +
7440 + if (c->m_len == m_len && c->m_off >= m_off)
7441 + continue;
7442 +
7443 + if (swd->use_best_off)
7444 + better_match (swd, &c->m_len, &c->m_off);
7445 +
7446 + l2 = len_of_coded_match (c->m_len, c->m_off,
7447 + lit + ahead);
7448 + if (l2 < 0)
7449 + continue;
7450 +
7451 + l3 = (op == out) ? -1 : len_of_coded_match (ahead,
7452 + m_off,
7453 + lit);
7454 +
7455 + lazy_match_min_gain =
7456 + min_gain (ahead, lit, lit + ahead, l1, l2,
7457 + l3);
7458 + if (c->m_len >= m_len + lazy_match_min_gain)
7459 + {
7460 + c->lazy++;
7461 +
7462 + if (l3 > 0)
7463 + {
7464 +
7465 + op = code_run (c, op, ii, lit, ahead);
7466 + lit = 0;
7467 +
7468 + op = code_match (c, op, ahead, m_off);
7469 + }
7470 + else
7471 + {
7472 + lit += ahead;
7473 + }
7474 + goto lazy_match_done;
7475 + }
7476 + }
7477 +
7478 + op = code_run (c, op, ii, lit, m_len);
7479 + lit = 0;
7480 +
7481 + op = code_match (c, op, m_len, m_off);
7482 + swd->max_chain = max_chain;
7483 + r = find_match (c, swd, m_len, 1 + ahead);
7484 +
7485 + lazy_match_done:;
7486 + }
7487 +
7488 + if (lit > 0)
7489 + op = STORE_RUN (c, op, ii, lit);
7490 +
7491 + *op++ = M4_MARKER | 1;
7492 + *op++ = 0;
7493 + *op++ = 0;
7494 +
7495 + c->codesize = op - out;
7496 +
7497 + *out_len = op - out;
7498 +
7499 + if (c->cb)
7500 + (*c->cb) (c->textsize, c->codesize);
7501 +
7502 + return LZO_E_OK;
7503 +}
7504 +
7505 +static int
7506 +lzo1x_999_compress_level (const lzo_byte * in, lzo_uint in_len,
7507 + lzo_byte * out, lzo_uintp out_len,
7508 + lzo_voidp wrkmem,
7509 + const lzo_byte * dict, lzo_uint dict_len,
7510 + lzo_progress_callback_t cb, int compression_level)
7511 +{
7512 + static const struct
7513 + {
7514 + int try_lazy;
7515 + lzo_uint good_length;
7516 + lzo_uint max_lazy;
7517 + lzo_uint nice_length;
7518 + lzo_uint max_chain;
7519 + lzo_uint32 flags;
7520 + } c[9] =
7521 + {
7522 + {
7523 + 0, 0, 0, 8, 4, 0},
7524 + {
7525 + 0, 0, 0, 16, 8, 0},
7526 + {
7527 + 0, 0, 0, 32, 16, 0},
7528 + {
7529 + 1, 4, 4, 16, 16, 0},
7530 + {
7531 + 1, 8, 16, 32, 32, 0},
7532 + {
7533 + 1, 8, 16, 128, 128, 0},
7534 + {
7535 + 2, 8, 32, 128, 256, 0},
7536 + {
7537 + 2, 32, 128, F, 2048, 1},
7538 + {
7539 + 2, F, F, F, 4096, 1}
7540 + };
7541 +
7542 + if (compression_level < 1 || compression_level > 9)
7543 + return LZO_E_ERROR;
7544 +
7545 + compression_level -= 1;
7546 + return lzo1x_999_compress_internal (in, in_len, out, out_len, wrkmem,
7547 + dict, dict_len, cb,
7548 + c[compression_level].try_lazy,
7549 + c[compression_level].good_length,
7550 + c[compression_level].max_lazy,
7551 + 0,
7552 + c[compression_level].max_chain,
7553 + c[compression_level].flags);
7554 +}
7555 +
7556 +static int
7557 +lzo1x_999_compress (const lzo_byte * in, lzo_uint in_len,
7558 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7559 +{
7560 + return lzo1x_999_compress_level (in, in_len, out, out_len, wrkmem,
7561 + NULL, 0, 0, 8);
7562 +}
7563 +
7564 +/* minilzo.c */
7565 +
7566 +static const lzo_byte __lzo_copyright[] = LZO_VERSION_STRING;
7567 +
7568 +static lzo_uint
7569 +_lzo1x_1_do_compress (const lzo_byte * in, lzo_uint in_len,
7570 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7571 +{
7572 +
7573 + register const lzo_byte *ip;
7574 +
7575 + lzo_byte *op;
7576 + const lzo_byte *const in_end = in + in_len;
7577 + const lzo_byte *const ip_end = in + in_len - 8 - 5;
7578 + const lzo_byte *ii;
7579 + lzo_dict_p const dict = (lzo_dict_p) wrkmem;
7580 +
7581 + op = out;
7582 + ip = in;
7583 + ii = ip;
7584 +
7585 + ip += 4;
7586 + for (;;)
7587 + {
7588 + register const lzo_byte *m_pos;
7589 +
7590 + lzo_uint m_off;
7591 + lzo_uint m_len;
7592 + lzo_uint dindex;
7593 +
7594 + DINDEX1 (dindex, ip);
7595 + GINDEX (m_pos, m_off, dict, dindex, in);
7596 + if (LZO_CHECK_MPOS_NON_DET
7597 + (m_pos, m_off, in, ip, M4_MAX_OFFSET))
7598 + goto literal;
7599 +
7600 + if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
7601 + goto try_match;
7602 + DINDEX2 (dindex, ip);
7603 + GINDEX (m_pos, m_off, dict, dindex, in);
7604 +
7605 + if (LZO_CHECK_MPOS_NON_DET
7606 + (m_pos, m_off, in, ip, M4_MAX_OFFSET))
7607 + goto literal;
7608 + if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
7609 + goto try_match;
7610 + goto literal;
7611 +
7612 + try_match:
7613 + if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
7614 + {
7615 + }
7616 + else
7617 + {
7618 + if (m_pos[2] == ip[2])
7619 + {
7620 + goto match;
7621 + }
7622 + else
7623 + {
7624 + }
7625 + }
7626 +
7627 + literal:
7628 + UPDATE_I (dict, 0, dindex, ip, in);
7629 + ++ip;
7630 + if (ip >= ip_end)
7631 + break;
7632 + continue;
7633 +
7634 + match:
7635 + UPDATE_I (dict, 0, dindex, ip, in);
7636 +
7637 + if (pd (ip, ii) > 0)
7638 + {
7639 + register lzo_uint t = pd (ip, ii);
7640 +
7641 + if (t <= 3)
7642 + {
7643 + op[-2] |= LZO_BYTE (t);
7644 + }
7645 + else if (t <= 18)
7646 + *op++ = LZO_BYTE (t - 3);
7647 + else
7648 + {
7649 + register lzo_uint tt = t - 18;
7650 +
7651 + *op++ = 0;
7652 + while (tt > 255)
7653 + {
7654 + tt -= 255;
7655 + *op++ = 0;
7656 + }
7657 + *op++ = LZO_BYTE (tt);;
7658 + }
7659 + do
7660 + *op++ = *ii++;
7661 + while (--t > 0);
7662 + }
7663 +
7664 + ip += 3;
7665 + if (m_pos[3] != *ip++ || m_pos[4] != *ip++
7666 + || m_pos[5] != *ip++ || m_pos[6] != *ip++
7667 + || m_pos[7] != *ip++ || m_pos[8] != *ip++)
7668 + {
7669 + --ip;
7670 + m_len = ip - ii;
7671 +
7672 + if (m_off <= M2_MAX_OFFSET)
7673 + {
7674 + m_off -= 1;
7675 +
7676 + *op++ = LZO_BYTE (((m_len -
7677 + 1) << 5) | ((m_off & 7) <<
7678 + 2));
7679 + *op++ = LZO_BYTE (m_off >> 3);
7680 + }
7681 + else if (m_off <= M3_MAX_OFFSET)
7682 + {
7683 + m_off -= 1;
7684 + *op++ = LZO_BYTE (M3_MARKER | (m_len - 2));
7685 + goto m3_m4_offset;
7686 + }
7687 + else
7688 +
7689 + {
7690 + m_off -= 0x4000;
7691 +
7692 + *op++ = LZO_BYTE (M4_MARKER |
7693 + ((m_off & 0x4000) >> 11) |
7694 + (m_len - 2));
7695 + goto m3_m4_offset;
7696 + }
7697 + }
7698 + else
7699 + {
7700 + {
7701 + const lzo_byte *end = in_end;
7702 + const lzo_byte *m = m_pos + M2_MAX_LEN + 1;
7703 + while (ip < end && *m == *ip)
7704 + m++, ip++;
7705 + m_len = (ip - ii);
7706 + }
7707 +
7708 +
7709 + if (m_off <= M3_MAX_OFFSET)
7710 + {
7711 + m_off -= 1;
7712 + if (m_len <= 33)
7713 + *op++ = LZO_BYTE (M3_MARKER |
7714 + (m_len - 2));
7715 + else
7716 + {
7717 + m_len -= 33;
7718 + *op++ = M3_MARKER | 0;
7719 + goto m3_m4_len;
7720 + }
7721 + }
7722 + else
7723 + {
7724 + m_off -= 0x4000;
7725 +
7726 + if (m_len <= M4_MAX_LEN)
7727 + *op++ = LZO_BYTE (M4_MARKER |
7728 + ((m_off & 0x4000) >>
7729 + 11) | (m_len - 2));
7730 +
7731 + else
7732 + {
7733 + m_len -= M4_MAX_LEN;
7734 + *op++ = LZO_BYTE (M4_MARKER |
7735 + ((m_off & 0x4000) >>
7736 + 11));
7737 + m3_m4_len:
7738 + while (m_len > 255)
7739 + {
7740 + m_len -= 255;
7741 + *op++ = 0;
7742 + }
7743 +
7744 + *op++ = LZO_BYTE (m_len);
7745 + }
7746 + }
7747 +
7748 + m3_m4_offset:
7749 + *op++ = LZO_BYTE ((m_off & 63) << 2);
7750 + *op++ = LZO_BYTE (m_off >> 6);
7751 + }
7752 + ii = ip;
7753 + if (ip >= ip_end)
7754 + break;
7755 + }
7756 +
7757 + *out_len = op - out;
7758 + return pd (in_end, ii);
7759 +}
7760 +
7761 +static int
7762 +lzo1x_1_compress (const lzo_byte * in, lzo_uint in_len,
7763 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7764 +{
7765 + lzo_byte *op = out;
7766 + lzo_uint t;
7767 +
7768 + if (in_len <= M2_MAX_LEN + 5)
7769 + t = in_len;
7770 + else
7771 + {
7772 + t = _lzo1x_1_do_compress (in, in_len, op, out_len, wrkmem);
7773 + op += *out_len;
7774 + }
7775 +
7776 + if (t > 0)
7777 + {
7778 + const lzo_byte *ii = in + in_len - t;
7779 +
7780 + if (op == out && t <= 238)
7781 + *op++ = LZO_BYTE (17 + t);
7782 + else if (t <= 3)
7783 + op[-2] |= LZO_BYTE (t);
7784 + else if (t <= 18)
7785 + *op++ = LZO_BYTE (t - 3);
7786 + else
7787 + {
7788 + lzo_uint tt = t - 18;
7789 +
7790 + *op++ = 0;
7791 + while (tt > 255)
7792 + {
7793 + tt -= 255;
7794 + *op++ = 0;
7795 + }
7796 +
7797 + *op++ = LZO_BYTE (tt);
7798 + }
7799 + do
7800 + *op++ = *ii++;
7801 + while (--t > 0);
7802 + }
7803 +
7804 + *op++ = M4_MARKER | 1;
7805 + *op++ = 0;
7806 + *op++ = 0;
7807 +
7808 + *out_len = op - out;
7809 + return 0;
7810 +}
7811 +
7812 +static int
7813 +lzo1x_decompress (const lzo_byte * in, lzo_uint in_len,
7814 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
7815 +{
7816 + register lzo_byte *op;
7817 + register const lzo_byte *ip;
7818 + register lzo_uint t;
7819 +
7820 + register const lzo_byte *m_pos;
7821 +
7822 + const lzo_byte *const ip_end = in + in_len;
7823 + lzo_byte *const op_end = out + *out_len;
7824 +
7825 + *out_len = 0;
7826 +
7827 + op = out;
7828 + ip = in;
7829 +
7830 + if (*ip > 17)
7831 + {
7832 + t = *ip++ - 17;
7833 + if (t < 4)
7834 + goto match_next;
7835 + NEED_OP (t);
7836 + NEED_IP (t + 1);
7837 + do
7838 + *op++ = *ip++;
7839 + while (--t > 0);
7840 + goto first_literal_run;
7841 + }
7842 +
7843 + while (TEST_IP && TEST_OP)
7844 + {
7845 + t = *ip++;
7846 + if (t >= 16)
7847 + goto match;
7848 + if (t == 0)
7849 + {
7850 + NEED_IP (1);
7851 + while (*ip == 0)
7852 + {
7853 + t += 255;
7854 + ip++;
7855 + NEED_IP (1);
7856 + }
7857 + t += 15 + *ip++;
7858 + }
7859 + NEED_OP (t + 3);
7860 + NEED_IP (t + 4);
7861 + if (PTR_ALIGNED2_4 (op, ip))
7862 + {
7863 + COPY4 (op, ip);
7864 +
7865 + op += 4;
7866 + ip += 4;
7867 + if (--t > 0)
7868 + {
7869 + if (t >= 4)
7870 + {
7871 + do
7872 + {
7873 + COPY4 (op, ip);
7874 + op += 4;
7875 + ip += 4;
7876 + t -= 4;
7877 + }
7878 + while (t >= 4);
7879 + if (t > 0)
7880 + do
7881 + *op++ = *ip++;
7882 + while (--t > 0);
7883 + }
7884 + else
7885 + do
7886 + *op++ = *ip++;
7887 + while (--t > 0);
7888 + }
7889 + }
7890 + else
7891 + {
7892 + *op++ = *ip++;
7893 + *op++ = *ip++;
7894 + *op++ = *ip++;
7895 + do
7896 + *op++ = *ip++;
7897 + while (--t > 0);
7898 + }
7899 + first_literal_run:
7900 +
7901 + t = *ip++;
7902 + if (t >= 16)
7903 + goto match;
7904 +
7905 + m_pos = op - (1 + M2_MAX_OFFSET);
7906 + m_pos -= t >> 2;
7907 + m_pos -= *ip++ << 2;
7908 + TEST_LOOKBEHIND (m_pos, out);
7909 + NEED_OP (3);
7910 + *op++ = *m_pos++;
7911 + *op++ = *m_pos++;
7912 + *op++ = *m_pos;
7913 +
7914 + goto match_done;
7915 +
7916 + while (TEST_IP && TEST_OP)
7917 + {
7918 + match:
7919 + if (t >= 64)
7920 + {
7921 + m_pos = op - 1;
7922 + m_pos -= (t >> 2) & 7;
7923 + m_pos -= *ip++ << 3;
7924 + t = (t >> 5) - 1;
7925 + TEST_LOOKBEHIND (m_pos, out);
7926 + NEED_OP (t + 3 - 1);
7927 + goto copy_match;
7928 +
7929 + }
7930 + else if (t >= 32)
7931 + {
7932 + t &= 31;
7933 + if (t == 0)
7934 + {
7935 + NEED_IP (1);
7936 + while (*ip == 0)
7937 + {
7938 + t += 255;
7939 + ip++;
7940 + NEED_IP (1);
7941 + }
7942 + t += 31 + *ip++;
7943 + }
7944 +
7945 + m_pos = op - 1;
7946 + m_pos -= (ip[0] >> 2) + (ip[1] << 6);
7947 +
7948 + ip += 2;
7949 + }
7950 + else if (t >= 16)
7951 + {
7952 + m_pos = op;
7953 + m_pos -= (t & 8) << 11;
7954 +
7955 + t &= 7;
7956 + if (t == 0)
7957 + {
7958 + NEED_IP (1);
7959 + while (*ip == 0)
7960 + {
7961 + t += 255;
7962 + ip++;
7963 + NEED_IP (1);
7964 + }
7965 + t += 7 + *ip++;
7966 + }
7967 +
7968 + m_pos -= (ip[0] >> 2) + (ip[1] << 6);
7969 +
7970 + ip += 2;
7971 + if (m_pos == op)
7972 + goto eof_found;
7973 + m_pos -= 0x4000;
7974 + }
7975 + else
7976 + {
7977 +
7978 + m_pos = op - 1;
7979 + m_pos -= t >> 2;
7980 + m_pos -= *ip++ << 2;
7981 + TEST_LOOKBEHIND (m_pos, out);
7982 + NEED_OP (2);
7983 + *op++ = *m_pos++;
7984 + *op++ = *m_pos;
7985 +
7986 + goto match_done;
7987 + }
7988 +
7989 + TEST_LOOKBEHIND (m_pos, out);
7990 + NEED_OP (t + 3 - 1);
7991 + if (t >= 2 * 4 - (3 - 1)
7992 + && PTR_ALIGNED2_4 (op, m_pos))
7993 + {
7994 + COPY4 (op, m_pos);
7995 + op += 4;
7996 + m_pos += 4;
7997 + t -= 4 - (3 - 1);
7998 + do
7999 + {
8000 + COPY4 (op, m_pos);
8001 + op += 4;
8002 + m_pos += 4;
8003 + t -= 4;
8004 + }
8005 + while (t >= 4);
8006 + if (t > 0)
8007 + do
8008 + *op++ = *m_pos++;
8009 + while (--t > 0);
8010 + }
8011 + else
8012 +
8013 + {
8014 + copy_match:
8015 + *op++ = *m_pos++;
8016 + *op++ = *m_pos++;
8017 + do
8018 + *op++ = *m_pos++;
8019 + while (--t > 0);
8020 + }
8021 +
8022 + match_done:
8023 + t = ip[-2] & 3;
8024 +
8025 + if (t == 0)
8026 + break;
8027 +
8028 + match_next:
8029 + NEED_OP (t);
8030 + NEED_IP (t + 1);
8031 + do
8032 + *op++ = *ip++;
8033 + while (--t > 0);
8034 + t = *ip++;
8035 + }
8036 + }
8037 + *out_len = op - out;
8038 + return LZO_E_EOF_NOT_FOUND;
8039 +
8040 + eof_found:
8041 + *out_len = op - out;
8042 + return (ip == ip_end ? LZO_E_OK :
8043 + (ip <
8044 + ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
8045 +
8046 + input_overrun:
8047 + *out_len = op - out;
8048 + return LZO_E_INPUT_OVERRUN;
8049 +
8050 + output_overrun:
8051 + *out_len = op - out;
8052 + return LZO_E_OUTPUT_OVERRUN;
8053 +
8054 + lookbehind_overrun:
8055 + *out_len = op - out;
8056 + return LZO_E_LOOKBEHIND_OVERRUN;
8057 +}
8058 +
8059 +/* lzo1x_oo.ch */
8060 +
8061 +#define NO_LIT LZO_UINT_MAX
8062 +
8063 +static void
8064 +copy2 (lzo_byte * ip, const lzo_byte * m_pos, lzo_ptrdiff_t off)
8065 +{
8066 + ip[0] = m_pos[0];
8067 + if (off == 1)
8068 + ip[1] = m_pos[0];
8069 + else
8070 + ip[1] = m_pos[1];
8071 +}
8072 +
8073 +static void
8074 +copy3 (lzo_byte * ip, const lzo_byte * m_pos, lzo_ptrdiff_t off)
8075 +{
8076 + ip[0] = m_pos[0];
8077 + if (off == 1)
8078 + {
8079 + ip[2] = ip[1] = m_pos[0];
8080 + }
8081 + else if (off == 2)
8082 + {
8083 + ip[1] = m_pos[1];
8084 + ip[2] = m_pos[0];
8085 + }
8086 + else
8087 + {
8088 + ip[1] = m_pos[1];
8089 + ip[2] = m_pos[2];
8090 + }
8091 +}
8092 +
8093 +static int
8094 +lzo1x_optimize (lzo_byte * in, lzo_uint in_len,
8095 + lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
8096 +{
8097 + register lzo_byte *op;
8098 + register lzo_byte *ip;
8099 + register lzo_uint t;
8100 + register lzo_byte *m_pos;
8101 + lzo_uint nl;
8102 + const lzo_byte *const ip_end = in + in_len;
8103 + const lzo_byte *const op_end = out + *out_len;
8104 + lzo_byte *litp = NULL;
8105 + lzo_uint lit = 0;
8106 + lzo_uint next_lit = NO_LIT;
8107 + long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
8108 +
8109 + *out_len = 0;
8110 +
8111 + op = out;
8112 + ip = in;
8113 +
8114 + if (*ip > 17)
8115 + {
8116 + t = *ip++ - 17;
8117 + if (t < 4)
8118 + goto match_next;
8119 + goto first_literal_run;
8120 + }
8121 +
8122 + while (TEST_IP && TEST_OP)
8123 + {
8124 + t = *ip++;
8125 + if (t >= 16)
8126 + goto match;
8127 + litp = ip - 1;
8128 + if (t == 0)
8129 + {
8130 + t = 15;
8131 + while (*ip == 0)
8132 + t += 255, ip++;
8133 + t += *ip++;
8134 + }
8135 + lit = t + 3;
8136 + copy_literal_run:
8137 + *op++ = *ip++;
8138 + *op++ = *ip++;
8139 + *op++ = *ip++;
8140 + first_literal_run:
8141 + do
8142 + *op++ = *ip++;
8143 + while (--t > 0);
8144 +
8145 + t = *ip++;
8146 +
8147 + if (t >= 16)
8148 + goto match;
8149 + m_pos = op - 1 - 0x800;
8150 + m_pos -= t >> 2;
8151 + m_pos -= *ip++ << 2;
8152 + *op++ = *m_pos++;
8153 + *op++ = *m_pos++;
8154 + *op++ = *m_pos++;
8155 + lit = 0;
8156 + goto match_done;
8157 +
8158 + while (TEST_IP && TEST_OP)
8159 + {
8160 + if (t < 16)
8161 + {
8162 + m_pos = op - 1;
8163 + m_pos -= t >> 2;
8164 + m_pos -= *ip++ << 2;
8165 +
8166 + if (litp == NULL)
8167 + goto copy_m1;
8168 +
8169 + nl = ip[-2] & 3;
8170 + if (nl == 0 && lit == 1 && ip[0] >= 16)
8171 + {
8172 + next_lit = nl;
8173 + lit += 2;
8174 + *litp = LZO_BYTE ((*litp & ~3) | lit);
8175 + copy2 (ip - 2, m_pos, op - m_pos);
8176 + o_m1_a++;
8177 + }
8178 + else if (nl == 0 && ip[0] < 16 && ip[0] != 0
8179 + && (lit + 2 + ip[0] < 16))
8180 + {
8181 + t = *ip++;
8182 + *litp &= ~3;
8183 + copy2 (ip - 3 + 1, m_pos, op - m_pos);
8184 + litp += 2;
8185 + if (lit > 0)
8186 + memmove (litp + 1, litp, lit);
8187 + lit += 2 + t + 3;
8188 + *litp = LZO_BYTE (lit - 3);
8189 +
8190 + o_m1_b++;
8191 + *op++ = *m_pos++;
8192 + *op++ = *m_pos++;
8193 + goto copy_literal_run;
8194 + }
8195 + copy_m1:
8196 + *op++ = *m_pos++;
8197 + *op++ = *m_pos++;
8198 + }
8199 + else
8200 + {
8201 + match:
8202 + if (t >= 64)
8203 + {
8204 + m_pos = op - 1;
8205 + m_pos -= (t >> 2) & 7;
8206 + m_pos -= *ip++ << 3;
8207 + t = (t >> 5) - 1;
8208 + if (litp == NULL)
8209 + goto copy_m;
8210 +
8211 + nl = ip[-2] & 3;
8212 + if (t == 1 && lit > 3 && nl == 0 &&
8213 + ip[0] < 16 && ip[0] != 0
8214 + && (lit + 3 + ip[0] < 16))
8215 + {
8216 + t = *ip++;
8217 + copy3 (ip - 1 - 2, m_pos,
8218 + op - m_pos);
8219 + lit += 3 + t + 3;
8220 + *litp = LZO_BYTE (lit - 3);
8221 + o_m2++;
8222 + *op++ = *m_pos++;
8223 + *op++ = *m_pos++;
8224 + *op++ = *m_pos++;
8225 + goto copy_literal_run;
8226 + }
8227 + }
8228 + else
8229 + {
8230 + if (t >= 32)
8231 + {
8232 + t &= 31;
8233 + if (t == 0)
8234 + {
8235 + t = 31;
8236 + while (*ip == 0)
8237 + t += 255,
8238 + ip++;
8239 + t += *ip++;
8240 + }
8241 + m_pos = op - 1;
8242 + m_pos -= *ip++ >> 2;
8243 + m_pos -= *ip++ << 6;
8244 + }
8245 + else
8246 + {
8247 + m_pos = op;
8248 + m_pos -= (t & 8) << 11;
8249 + t &= 7;
8250 + if (t == 0)
8251 + {
8252 + t = 7;
8253 + while (*ip == 0)
8254 + t += 255,
8255 + ip++;
8256 + t += *ip++;
8257 + }
8258 + m_pos -= *ip++ >> 2;
8259 + m_pos -= *ip++ << 6;
8260 + if (m_pos == op)
8261 + goto eof_found;
8262 + m_pos -= 0x4000;
8263 + }
8264 + if (litp == NULL)
8265 + goto copy_m;
8266 +
8267 + nl = ip[-2] & 3;
8268 + if (t == 1 && lit == 0 && nl == 0
8269 + && ip[0] >= 16)
8270 + {
8271 + next_lit = nl;
8272 + lit += 3;
8273 + *litp = LZO_BYTE ((*litp & ~3)
8274 + | lit);
8275 + copy3 (ip - 3, m_pos,
8276 + op - m_pos);
8277 + o_m3_a++;
8278 + }
8279 + else if (t == 1 && lit <= 3 && nl == 0
8280 + && ip[0] < 16 && ip[0] != 0
8281 + && (lit + 3 + ip[0] < 16))
8282 + {
8283 + t = *ip++;
8284 + *litp &= ~3;
8285 + copy3 (ip - 4 + 1, m_pos,
8286 + op - m_pos);
8287 + litp += 2;
8288 + if (lit > 0)
8289 + memmove (litp + 1,
8290 + litp, lit);
8291 + lit += 3 + t + 3;
8292 + *litp = LZO_BYTE (lit - 3);
8293 +
8294 + o_m3_b++;
8295 + *op++ = *m_pos++;
8296 + *op++ = *m_pos++;
8297 + *op++ = *m_pos++;
8298 + goto copy_literal_run;
8299 + }
8300 + }
8301 + copy_m:
8302 + *op++ = *m_pos++;
8303 + *op++ = *m_pos++;
8304 + do
8305 + *op++ = *m_pos++;
8306 + while (--t > 0);
8307 + }
8308 +
8309 + match_done:
8310 + if (next_lit == NO_LIT)
8311 + {
8312 + t = ip[-2] & 3;
8313 + lit = t;
8314 + litp = ip - 2;
8315 + }
8316 + else
8317 + t = next_lit;
8318 + next_lit = NO_LIT;
8319 + if (t == 0)
8320 + break;
8321 + match_next:
8322 + do
8323 + *op++ = *ip++;
8324 + while (--t > 0);
8325 + t = *ip++;
8326 + }
8327 + }
8328 +
8329 + *out_len = op - out;
8330 + return LZO_E_EOF_NOT_FOUND;
8331 +
8332 + eof_found:
8333 + *out_len = op - out;
8334 + return (ip == ip_end ? LZO_E_OK :
8335 + (ip <
8336 + ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
8337 +}
8338 +
8339 +/* interface to jffs2 bbc follows */
8340 +
8341 +#include "jffs2_bbc_framework.h"
8342 +
8343 +#define BLOCKSIZE 4096
8344 +#define OUTBLOCKSIZE (BLOCKSIZE + BLOCKSIZE / 64 + 16 + 3)
8345 +
8346 +#define JFFS2_BBC_LZO_BLOCK_SIGN {0x3f, 0x47, 0x5a, 0x18}
8347 +
8348 +static int
8349 +jffs2_bbc_lzo_compressor_init (void);
8350 +
8351 +static void
8352 +jffs2_bbc_lzo_compressor_deinit (void);
8353 +
8354 +static int
8355 +jffs2_bbc_lzo_compress (void *model, unsigned char *input,
8356 + unsigned char *output, unsigned long *sourcelen,
8357 + unsigned long *dstlen);
8358 +
8359 +static int
8360 +jffs2_bbc_lzo_estimate (void *model, unsigned char *input,
8361 + unsigned long sourcelen, unsigned long *dstlen,
8362 + unsigned long *readtime, unsigned long *writetime);
8363 +
8364 +static int
8365 +jffs2_bbc_lzo_decompress (void *model, unsigned char *input,
8366 + unsigned char *output, unsigned long sourcelen,
8367 + unsigned long dstlen);
8368 +
8369 +static char *
8370 +jffs2_bbc_lzo_proc_info (void);
8371 +
8372 +static int
8373 +jffs2_bbc_lzo_proc_command (char *command);
8374 +
8375 +struct jffs2_bbc_compressor_type jffs2_bbc_lzo = {
8376 + "lzo",
8377 + 0,
8378 + JFFS2_BBC_LZO_BLOCK_SIGN,
8379 + jffs2_bbc_lzo_compressor_init,
8380 + NULL,
8381 + NULL,
8382 + jffs2_bbc_lzo_compressor_deinit,
8383 + jffs2_bbc_lzo_compress,
8384 + jffs2_bbc_lzo_estimate,
8385 + jffs2_bbc_lzo_decompress,
8386 + jffs2_bbc_lzo_proc_info,
8387 + jffs2_bbc_lzo_proc_command
8388 +};
8389 +
8390 +static int
8391 +no_lzo1x_optimize (lzo_byte * src, lzo_uint src_len,
8392 + lzo_byte * dst, lzo_uintp dst_len, lzo_voidp wrkmem)
8393 +{
8394 + return 0;
8395 +}
8396 +
8397 +#ifdef __KERNEL__
8398 +static lzo_compress_t lzo1x_compressor = lzo1x_1_compress;
8399 +static lzo_optimize_t lzo1x_optimizer = no_lzo1x_optimize;
8400 +static int lzo1x_compressor_type = 1;
8401 +static int lzo1x_optimize_type = 0;
8402 +static unsigned long lzo1x_compressor_memsize = LZO1X_1_MEM_COMPRESS;
8403 +#else
8404 +static lzo_compress_t lzo1x_compressor = lzo1x_999_compress;
8405 +static lzo_optimize_t lzo1x_optimizer = lzo1x_optimize;
8406 +static int lzo1x_compressor_type = 999;
8407 +static int lzo1x_optimize_type = 1;
8408 +static unsigned long lzo1x_compressor_memsize = LZO1X_999_MEM_COMPRESS;
8409 +#endif
8410 +
8411 +static lzo_bytep wrkmem = NULL; /* temporary buffer for compression, used by lzo */
8412 +static lzo_bytep cmprssmem = NULL; /* temporary buffer for compression, used by interface */
8413 +
8414 +static int
8415 +jffs2_bbc_lzo_compressor_init (void)
8416 +{
8417 + wrkmem = (lzo_bytep) jffs2_bbc_malloc (lzo1x_compressor_memsize);
8418 + cmprssmem = (lzo_bytep) jffs2_bbc_malloc (OUTBLOCKSIZE);
8419 + return !(wrkmem && cmprssmem);
8420 +}
8421 +
8422 +static void
8423 +jffs2_bbc_lzo_compressor_deinit (void)
8424 +{
8425 + jffs2_bbc_free (wrkmem);
8426 + jffs2_bbc_free (cmprssmem);
8427 +}
8428 +
8429 +static int
8430 +jffs2_bbc_lzo_compress (void *model, unsigned char *input,
8431 + unsigned char *output, unsigned long *sourcelen,
8432 + unsigned long *dstlen)
8433 +{
8434 + lzo_uint csize = OUTBLOCKSIZE;
8435 + lzo_uint isize = *sourcelen;
8436 + int retval;
8437 + if ((retval =
8438 + lzo1x_compressor (input, *sourcelen, cmprssmem, &csize,
8439 + wrkmem)) != LZO_E_OK)
8440 + {
8441 + *sourcelen = *dstlen = 0;
8442 + return retval;
8443 + }
8444 + else
8445 + {
8446 + retval = lzo1x_optimizer (cmprssmem, csize, input, &isize,
8447 + NULL);
8448 + csize += 2;
8449 + if (csize <= *dstlen) {
8450 + *dstlen = csize;
8451 + *(output++) = jffs2_bbc_lzo.block_sign[0];
8452 + *(output++) = jffs2_bbc_lzo.block_sign[1];
8453 + memcpy (output, cmprssmem, csize - 2);
8454 + return retval;
8455 + } else {
8456 + *sourcelen = *dstlen = 0;
8457 + return -1;
8458 + }
8459 + }
8460 +}
8461 +
8462 +static int
8463 +jffs2_bbc_lzo_estimate (void *model, unsigned char *input,
8464 + unsigned long sourcelen, unsigned long *dstlen,
8465 + unsigned long *readtime, unsigned long *writetime)
8466 +{
8467 + *dstlen = sourcelen * 55 / 100;
8468 + *readtime = JFFS2_BBC_ZLIB_READ_TIME / 2;
8469 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 8 / 10; /* LZO1X-1 is much-much faster,
8470 + but LZO1X-999 is slow. The default mode for inside kernel compression is LZO1X-1
8471 + This should be *0.4 really */
8472 + return 0;
8473 +}
8474 +
8475 +static int
8476 +jffs2_bbc_lzo_decompress (void *model, unsigned char *input,
8477 + unsigned char *output, unsigned long sourcelen,
8478 + unsigned long dstlen)
8479 +{
8480 + lzo_uint outlen = dstlen;
8481 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzo.block_sign[0] ) ||
8482 + ( *(input++) != (unsigned char)jffs2_bbc_lzo.block_sign[1] )
8483 + ) {
8484 + return -1;
8485 + } else {
8486 + return lzo1x_decompress (input, sourcelen - 2, output, &outlen, NULL);
8487 + }
8488 +}
8489 +
8490 +static char *
8491 +jffs2_bbc_lzo_proc_info (void)
8492 +{
8493 + if (lzo1x_compressor_type == 1)
8494 + {
8495 + if (lzo1x_optimize_type == 1)
8496 + {
8497 + return "LZO1X-1 compression with optimization";
8498 + }
8499 + else
8500 + {
8501 + return "LZO1X-1 compression without optimization";
8502 + }
8503 + }
8504 + else if (lzo1x_compressor_type == 999)
8505 + {
8506 + if (lzo1x_optimize_type == 1)
8507 + {
8508 + return "LZO1X-999 compression with optimization";
8509 + }
8510 + else
8511 + {
8512 + return "LZO1X-999 compression without optimization";
8513 + }
8514 + }
8515 + else
8516 + {
8517 + return "Unknown configuration!";
8518 + }
8519 +}
8520 +
8521 +static int
8522 +jffs2_bbc_lzo_proc_command (char *command)
8523 +{
8524 + switch (*command)
8525 + {
8526 + case 'o':
8527 + /* switch optimization off */
8528 + lzo1x_optimizer = no_lzo1x_optimize;
8529 + lzo1x_optimize_type = 0;
8530 + jffs2_bbc_print1 ("Compression optimization switched off.\n");
8531 + return 0;
8532 + case 'O':
8533 + /* switch optimization on */
8534 + lzo1x_optimizer = lzo1x_optimize;
8535 + lzo1x_optimize_type = 1;
8536 + jffs2_bbc_print1 ("Compression optimization switched on.\n");
8537 + return 0;
8538 + case '1':
8539 + /* switch compression to LZO1X-1 */
8540 + jffs2_bbc_free (wrkmem);
8541 + lzo1x_compressor_type = 1;
8542 + lzo1x_compressor = lzo1x_1_compress;
8543 + lzo1x_compressor_memsize = LZO1X_1_MEM_COMPRESS;
8544 + wrkmem = (lzo_bytep)
8545 + jffs2_bbc_malloc (lzo1x_compressor_memsize);
8546 + jffs2_bbc_print1 ("Compression type switched to LZO1X-1.\n");
8547 + return 0;
8548 + case '9':
8549 + /* switch compression to LZO1X-999 */
8550 + jffs2_bbc_free (wrkmem);
8551 + lzo1x_compressor_type = 999;
8552 + lzo1x_compressor = lzo1x_999_compress;
8553 + lzo1x_compressor_memsize = LZO1X_999_MEM_COMPRESS;
8554 + wrkmem = (lzo_bytep)
8555 + jffs2_bbc_malloc (lzo1x_compressor_memsize);
8556 + jffs2_bbc_print1
8557 + ("Compression type switched to LZO1X-999.\n");
8558 + return 0;
8559 + default:
8560 + jffs2_bbc_print1 ("Unknown command!\n");
8561 + return 0;
8562 + }
8563 +}
8564 +
8565 +
8566 +struct jffs2_bbc_compressor_type *
8567 +jffs2_bbc_lzo_init (int mode)
8568 +{
8569 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzo) == 0)
8570 + {
8571 + return &jffs2_bbc_lzo;
8572 + }
8573 + else
8574 + {
8575 + return NULL;
8576 + }
8577 +}
8578 +
8579 +void
8580 +jffs2_bbc_lzo_deinit (void)
8581 +{
8582 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzo);
8583 +}
8584 Index: linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzss_comp.c
8585 ===================================================================
8586 --- /dev/null
8587 +++ linux-2.4.35.4/fs/jffs2/jffs2_bbc_lzss_comp.c
8588 @@ -0,0 +1,385 @@
8589 +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
8590 +
8591 +/*
8592 + jffs2_bbc_lzss_comp.c -- Lempel-Ziv-Storer-Szymanski compression module for jffs2
8593 + Copyright (C) 2004 Patrik Kluba
8594 + Based on the LZSS source included in LDS (lossless datacompression sources)
8595 + Block-compression modifications by Patrik Kluba
8596 + $Header: /openwrt/openwrt/package/linux/kernel-patches/301-jffs-compression,v 1.1 2005/03/26 10:33:31 wbx Exp $
8597 +*/
8598 +
8599 +/*
8600 +Original copyright follows:
8601 +
8602 +**************************************************************
8603 + LZSS.C -- A Data Compression Program
8604 +**************************************************************
8605 + 4/6/1989 Haruhiko Okumura
8606 + Use, distribute, and modify this program freely.
8607 + Please send me your improved versions.
8608 + PC-VAN SCIENCE
8609 + NIFTY-Serve PAF01022
8610 + CompuServe 74050,1022
8611 +**************************************************************
8612 +
8613 +*/
8614 +
8615 +/*
8616 +
8617 + 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
8618 + Initial release
8619 +
8620 +*/
8621 +
8622 +/* lzss.c */
8623 +
8624 +#define N 4096 /* size of ring buffer */
8625 +#define F 18 /* upper limit for match_length */
8626 +#define THRESHOLD 2 /* encode string into position and length
8627 + if match_length is greater than this */
8628 +#define NIL N /* index for root of binary search trees */
8629 +
8630 +static unsigned char
8631 + text_buf[N + F - 1]; /* ring buffer of size N,
8632 + with extra F-1 bytes to facilitate string comparison */
8633 +static unsigned long match_position, match_length; /* of longest match. These are
8634 + set by the InsertNode() procedure. */
8635 +static unsigned long lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
8636 + parents -- These constitute binary search trees. */
8637 +
8638 +static void InitTree(void) /* initialize trees */
8639 +{
8640 + unsigned long i;
8641 +
8642 + /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
8643 + left children of node i. These nodes need not be initialized.
8644 + Also, dad[i] is the parent of node i. These are initialized to
8645 + NIL (= N), which stands for 'not used.'
8646 + For i = 0 to 255, rson[N + i + 1] is the root of the tree
8647 + for strings that begin with character i. These are initialized
8648 + to NIL. Note there are 256 trees. */
8649 +
8650 + for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
8651 + for (i = 0; i < N; i++) dad[i] = NIL;
8652 +}
8653 +
8654 +static void InsertNode(unsigned long r)
8655 + /* Inserts string of length F, text_buf[r..r+F-1], into one of the
8656 + trees (text_buf[r]'th tree) and returns the longest-match position
8657 + and length via the global variables match_position and match_length.
8658 + If match_length = F, then removes the old node in favor of the new
8659 + one, because the old one will be deleted sooner.
8660 + Note r plays double role, as tree node and position in buffer. */
8661 +{
8662 + unsigned long i, p;
8663 + unsigned char *key;
8664 + signed long cmp;
8665 +
8666 + cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
8667 + rson[r] = lson[r] = NIL; match_length = 0;
8668 + for ( ; ; ) {
8669 + if (cmp >= 0) {
8670 + if (rson[p] != NIL) p = rson[p];
8671 + else { rson[p] = r; dad[r] = p; return; }
8672 + } else {
8673 + if (lson[p] != NIL) p = lson[p];
8674 + else { lson[p] = r; dad[r] = p; return; }
8675 + }
8676 + for (i = 1; i < F; i++)
8677 + if ((cmp = key[i] - text_buf[p + i]) != 0) break;
8678 + if (i > match_length) {
8679 + match_position = p;
8680 + if ((match_length = i) >= F) break;
8681 + }
8682 + }
8683 + dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
8684 + dad[lson[p]] = r; dad[rson[p]] = r;
8685 + if (rson[dad[p]] == p) rson[dad[p]] = r;
8686 + else lson[dad[p]] = r;
8687 + dad[p] = NIL; /* remove p */
8688 +}
8689 +
8690 +static void DeleteNode(unsigned long p) /* deletes node p from tree */
8691 +{
8692 + unsigned long q;
8693 +
8694 + if (dad[p] == NIL) return; /* not in tree */
8695 + if (rson[p] == NIL) q = lson[p];
8696 + else if (lson[p] == NIL) q = rson[p];
8697 + else {
8698 + q = lson[p];
8699 + if (rson[q] != NIL) {
8700 + do { q = rson[q]; } while (rson[q] != NIL);
8701 + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
8702 + lson[q] = lson[p]; dad[lson[p]] = q;
8703 + }
8704 + rson[q] = rson[p]; dad[rson[p]] = q;
8705 + }
8706 + dad[q] = dad[p];
8707 + if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
8708 + dad[p] = NIL;
8709 +}
8710 +
8711 +/* modified for block compression */
8712 +/* on return, srclen will contain the number of successfully compressed bytes
8713 + and dstlen will contain completed compressed bytes */
8714 +
8715 +static int Encode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long *srclen,
8716 + unsigned long *dstlen)
8717 +{
8718 + unsigned long i, len, r, c, s, last_match_length, code_buf_ptr;
8719 + unsigned char code_buf[17], mask;
8720 + unsigned char *ip, *op;
8721 + unsigned long written = 0;
8722 + unsigned long read = 0;
8723 + unsigned char *srcend = srcbuf + *srclen;
8724 + unsigned char *dstend = dstbuf + *dstlen;
8725 + ip = srcbuf;
8726 + op = dstbuf;
8727 + InitTree(); /* initialize trees */
8728 + code_buf[0] = 0; /* code_buf[1..16] saves eight units of code, and
8729 + code_buf[0] works as eight flags, "1" representing that the unit
8730 + is an unencoded letter (1 byte), "0" a position-and-length pair
8731 + (2 bytes). Thus, eight units require at most 16 bytes of code. */
8732 + code_buf_ptr = mask = 1;
8733 + s = 0; r = N - F;
8734 + for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
8735 + any character that will appear often. */
8736 + for (len = 0; (len < F) && (ip < srcend); len++)
8737 + text_buf[r + len] = *(ip++); /* Read F bytes into the last F bytes of
8738 + the buffer */
8739 + read = len;
8740 + for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
8741 + each of which begins with one or more 'space' characters. Note
8742 + the order in which these strings are inserted. This way,
8743 + degenerate trees will be less likely to occur. */
8744 + InsertNode(r); /* Finally, insert the whole string just read. The
8745 + global variables match_length and match_position are set. */
8746 + do {
8747 + if (match_length > len) match_length = len; /* match_length
8748 + may be spuriously long near the end of text. */
8749 + if (match_length <= THRESHOLD) {
8750 + match_length = 1; /* Not long enough match. Send one byte. */
8751 + code_buf[0] |= mask; /* 'send one byte' flag */
8752 + code_buf[code_buf_ptr++] = text_buf[r]; /* Send uncoded. */
8753 + } else {
8754 + code_buf[code_buf_ptr++] = match_position;
8755 + code_buf[code_buf_ptr++] = (((match_position >> 4) & 0xf0)
8756 + | (match_length - (THRESHOLD + 1))); /* Send position and
8757 + length pair. Note match_length > THRESHOLD. */
8758 + }
8759 + if ((mask <<= 1) == 0) { /* Shift mask left one bit. */
8760 + if ((op + code_buf_ptr) > dstend) {
8761 + *dstlen = written; /* written contains bytes of complete compressed
8762 + code */
8763 + return -1;
8764 + };
8765 + for (i = 0; i < code_buf_ptr; *(op++) = code_buf[i++]); /* Send at most 8 units of */
8766 + /* code together */
8767 + written += code_buf_ptr;
8768 + *srclen = read; /* this many bytes have been successfully compressed */
8769 + code_buf[0] = 0; code_buf_ptr = mask = 1;
8770 + }
8771 + last_match_length = match_length;
8772 + for (i = 0; (i < last_match_length) && (ip < srcend); i++) {
8773 + c = *(ip++);
8774 + DeleteNode(s); /* Delete old strings and */
8775 + text_buf[s] = c; /* read new bytes */
8776 + if (s < F - 1) text_buf[s + N] = c; /* If the position is
8777 + near the end of buffer, extend the buffer to make
8778 + string comparison easier. */
8779 + s = (s + 1) & (N - 1); r = (r + 1) & (N - 1);
8780 + /* Since this is a ring buffer, increment the position
8781 + modulo N. */
8782 + InsertNode(r); /* Register the string in text_buf[r..r+F-1] */
8783 + }
8784 + read += i;
8785 + while (i++ < last_match_length) { /* After the end of text, */
8786 + DeleteNode(s); /* no need to read, but */
8787 + s = (s + 1) & (N - 1); r = (r + 1) & (N - 1);
8788 + if (--len) InsertNode(r); /* buffer may not be empty. */
8789 + }
8790 + } while (len > 0); /* until length of string to be processed is zero */
8791 + if (code_buf_ptr > 1) { /* Send remaining code. */
8792 + if ((op + code_buf_ptr) > dstend) {
8793 + *dstlen = written;
8794 + return -1;
8795 + }
8796 + for (i = 0; i < code_buf_ptr; *(op++) = code_buf[i++]);
8797 + written += code_buf_ptr;
8798 + *srclen = read;
8799 + }
8800 + *dstlen = written;
8801 + return 0;
8802 +}
8803 +
8804 +static int Decode(unsigned char *srcbuf, unsigned char *dstbuf, unsigned long srclen,
8805 + unsigned long dstlen) /* Just the reverse of Encode(). */
8806 +{
8807 + unsigned long i, r, c, j, k, flags;
8808 + unsigned char *ip, *op;
8809 + unsigned long written;
8810 + unsigned long read;
8811 + unsigned char *srcend = srcbuf + srclen;
8812 + unsigned char *dstend = dstbuf + dstlen;
8813 + read = written = 0;
8814 + ip = srcbuf;
8815 + op = dstbuf;
8816 + for (i = 0; i < N - F; i++) text_buf[i] = ' ';
8817 + r = N - F; flags = 0;
8818 + for ( ; ; ) {
8819 + if (((flags >>= 1) & 256) == 0) {
8820 + if (ip >= srcend) return 0;
8821 + c = *(ip++);
8822 + flags = c | 0xff00; /* uses higher byte cleverly */
8823 + } /* to count eight */
8824 + if (flags & 1) {
8825 + if (ip >= srcend) return 0;
8826 + c = *(ip++);
8827 + if (op >= dstend) return -1;
8828 + *(op++) = text_buf[r++] = c; r &= (N - 1);
8829 + } else {
8830 + if ((ip + 2) > srcend) return 0;
8831 + i = *(ip++);
8832 + j = *(ip++);
8833 + i |= ((j & 0xf0) << 4); j = (j & 0x0f) + THRESHOLD;
8834 + if ((op + j + 1) > dstend) return -1;
8835 + for (k = 0; k <= j; k++) {
8836 + c = text_buf[(i + k) & (N - 1)];
8837 + *(op++) = text_buf[r++] = c; r &= (N - 1);
8838 + }
8839 + }
8840 + }
8841 +}
8842 +
8843 +/* interface to jffs2 bbc follows */
8844 +
8845 +#include "jffs2_bbc_framework.h"
8846 +
8847 +
8848 +#define JFFS2_BBC_LZSS_BLOCK_SIGN {0x27, 0x6f, 0x12, 0xc4}
8849 +
8850 +static int
8851 +jffs2_bbc_lzss_compressor_init (void);
8852 +
8853 +static void
8854 +jffs2_bbc_lzss_compressor_deinit (void);
8855 +
8856 +static int
8857 +jffs2_bbc_lzss_compress (void *model, unsigned char *input,
8858 + unsigned char *output, unsigned long *sourcelen,
8859 + unsigned long *dstlen);
8860 +
8861 +static int
8862 +jffs2_bbc_lzss_estimate (void *model, unsigned char *input,
8863 + unsigned long sourcelen, unsigned long *dstlen,
8864 + unsigned long *readtime, unsigned long *writetime);
8865 +
8866 +static int
8867 +jffs2_bbc_lzss_decompress (void *model, unsigned char *input,
8868 + unsigned char *output, unsigned long sourcelen,
8869 + unsigned long dstlen);
8870 +
8871 +static char *
8872 +jffs2_bbc_lzss_proc_info (void);
8873 +
8874 +static int
8875 +jffs2_bbc_lzss_proc_command (char *command);
8876 +
8877 +struct jffs2_bbc_compressor_type jffs2_bbc_lzss = {
8878 + "lzss",
8879 + 0,
8880 + JFFS2_BBC_LZSS_BLOCK_SIGN,
8881 + jffs2_bbc_lzss_compressor_init,
8882 + NULL,
8883 + NULL,
8884 + jffs2_bbc_lzss_compressor_deinit,
8885 + jffs2_bbc_lzss_compress,
8886 + jffs2_bbc_lzss_estimate,
8887 + jffs2_bbc_lzss_decompress,
8888 + jffs2_bbc_lzss_proc_info,
8889 + jffs2_bbc_lzss_proc_command
8890 +};
8891 +
8892 +static int
8893 +jffs2_bbc_lzss_compressor_init (void)
8894 +{
8895 + return 0;
8896 +}
8897 +
8898 +static void
8899 +jffs2_bbc_lzss_compressor_deinit (void)
8900 +{
8901 +}
8902 +
8903 +static int
8904 +jffs2_bbc_lzss_compress (void *model, unsigned char *input,
8905 + unsigned char *output, unsigned long *sourcelen,
8906 + unsigned long *dstlen)
8907 +{
8908 + int retval;
8909 + unsigned long dst = *dstlen;
8910 + *(output++) = jffs2_bbc_lzss.block_sign[0];
8911 + *(output++) = jffs2_bbc_lzss.block_sign[1];
8912 + dst -= 2;
8913 + retval = Encode(input, output, sourcelen, &dst);
8914 + dst += 2;
8915 + *dstlen = dst;
8916 + return retval;
8917 +}
8918 +
8919 +static int
8920 +jffs2_bbc_lzss_estimate (void *model, unsigned char *input,
8921 + unsigned long sourcelen, unsigned long *dstlen,
8922 + unsigned long *readtime, unsigned long *writetime)
8923 +{
8924 + *dstlen = sourcelen * 60 / 100;
8925 + *readtime = JFFS2_BBC_ZLIB_READ_TIME * 12 / 10;
8926 + *writetime = JFFS2_BBC_ZLIB_WRITE_TIME * 3;
8927 + return 0;
8928 +}
8929 +
8930 +static int
8931 +jffs2_bbc_lzss_decompress (void *model, unsigned char *input,
8932 + unsigned char *output, unsigned long sourcelen,
8933 + unsigned long dstlen)
8934 +{
8935 + if ( ( *(input++) != (unsigned char)jffs2_bbc_lzss.block_sign[0] ) ||
8936 + ( *(input++) != (unsigned char)jffs2_bbc_lzss.block_sign[1] )
8937 + ) {
8938 + return -1;
8939 + } else {
8940 + return Decode(input, output, sourcelen - 2, dstlen);
8941 + }
8942 +}
8943 +
8944 +static char *
8945 +jffs2_bbc_lzss_proc_info (void)
8946 +{
8947 + return "Lempel-Ziv-Storer-Szymanski compression module";
8948 +}
8949 +
8950 +static int
8951 +jffs2_bbc_lzss_proc_command (char *command)
8952 +{
8953 + return 0;
8954 +}
8955 +
8956 +struct jffs2_bbc_compressor_type *
8957 +jffs2_bbc_lzss_init (int mode)
8958 +{
8959 + if (jffs2_bbc_register_compressor (&jffs2_bbc_lzss) == 0)
8960 + {
8961 + return &jffs2_bbc_lzss;
8962 + }
8963 + else
8964 + {
8965 + return NULL;
8966 + }
8967 +}
8968 +
8969 +void
8970 +jffs2_bbc_lzss_deinit (void)
8971 +{
8972 + jffs2_bbc_unregister_compressor (&jffs2_bbc_lzss);
8973 +}
8974 Index: linux-2.4.35.4/fs/jffs2/linux-2.4.25.hpatch
8975 ===================================================================
8976 --- /dev/null
8977 +++ linux-2.4.35.4/fs/jffs2/linux-2.4.25.hpatch
8978 @@ -0,0 +1,97 @@
8979 +FMakefile
8980 +=BBC insertion
8981 +-COMPR_OBJS
8982 +iMakefile.bbc.inc
8983 ++
8984 +I
8985 +?JFFS2_OBJS
8986 ++ $(JFFS2_BBC_KERNEL_OBJS) \
8987 +
8988 +F../Config.in
8989 +=BBC insertion
8990 +-tristate 'Compressed ROM file system support' CONFIG_CRAMFS
8991 +iConfig.in.bbc.inc
8992 ++
8993 +I
8994 +F../../Documentation/Configure.help
8995 +=BBC insertion
8996 +-JFFS stats available
8997 +iConfigure.help.bbc.inc
8998 ++
8999 +I
9000 +Fcompr_zlib.c
9001 +=(de)compress->(de)compress2
9002 +-int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
9003 ++int jffs2_zlib_compress2(unsigned char *data_in, unsigned char *cpage_out,
9004 +-void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
9005 ++void jffs2_zlib_decompress2(unsigned char *data_in, unsigned char *cpage_out,
9006 +?inflateEnd(&strm);
9007 +?}
9008 ++
9009 ++extern int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 * sourcelen, __u32 * dstlen);
9010 ++extern void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
9011 ++
9012 ++int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
9013 ++ __u32 *sourcelen, __u32 *dstlen)
9014 ++{
9015 ++ return jffs2_zlib_compress(data_in,cpage_out,sourcelen,dstlen);
9016 ++}
9017 ++
9018 ++void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
9019 ++ __u32 srclen, __u32 destlen)
9020 ++{
9021 ++ jffs2_zlib_decompress(data_in,cpage_out,srclen,destlen);
9022 ++}
9023 ++
9024 +
9025 +Ffile.c
9026 +=set_act_sb before write
9027 +-#include
9028 ++#include "jffs2_bbc_framework.h" /**BBC**/
9029 +I
9030 +?int jffs2_commit_write
9031 +-jffs2_compress(
9032 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9033 +I
9034 +
9035 +Fgc.c
9036 +=set_act_sb before write
9037 +-#include
9038 ++#include "jffs2_bbc_framework.h" /**BBC**/
9039 +I
9040 +?int jffs2_garbage_collect_dnode(
9041 +-jffs2_compress(
9042 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9043 +I
9044 +
9045 +Fread.c
9046 +=set_act_sb before read
9047 +-#include
9048 ++#include "jffs2_bbc_framework.h" /**BBC**/
9049 +I
9050 +?int jffs2_read_dnode(
9051 +-jffs2_decompress(
9052 ++ jffs2_bbc_model_set_act_sb(c); /**BBC**/
9053 +I
9054 +
9055 +Fsuper.c
9056 +=init, load_model
9057 +-#include
9058 ++#include "jffs2_bbc_fs.h" /**BBC**/
9059 +I
9060 +?struct super_block *jffs2_read_super(
9061 +-return sb;
9062 ++ jffs2_bbc_load_model(sb); /**BBC**/
9063 +I
9064 +?void jffs2_put_super
9065 +?c = JFFS2_SB_INFO
9066 ++ jffs2_bbc_unload_model(sb); /**BBC**/
9067 +?init_jffs2_fs(void)
9068 +?int ret;
9069 ++
9070 ++ jffs2_bbc_proc_init(); /**BBC**/
9071 ++
9072 +?exit_jffs2_fs(void)
9073 +?{
9074 ++ jffs2_bbc_proc_deinit(); /**BBC**/
9075 ++
9076 Index: linux-2.4.35.4/fs/jffs2/read.c
9077 ===================================================================
9078 --- linux-2.4.35.4.orig/fs/jffs2/read.c
9079 +++ linux-2.4.35.4/fs/jffs2/read.c
9080 @@ -35,6 +35,7 @@
9081 *
9082 */
9083
9084 +#include "jffs2_bbc_framework.h" /**BBC**/
9085 #include <linux/kernel.h>
9086 #include <linux/slab.h>
9087 #include <linux/jffs2.h>
9088 @@ -140,6 +141,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
9089 D2(printk(KERN_DEBUG "Data CRC matches calculated CRC %08x\n", crc));
9090 if (ri->compr != JFFS2_COMPR_NONE) {
9091 D2(printk(KERN_DEBUG "Decompress %d bytes from %p to %d bytes at %p\n", ri->csize, readbuf, ri->dsize, decomprbuf));
9092 + jffs2_bbc_model_set_act_sb(c); /**BBC**/
9093 ret = jffs2_decompress(ri->compr, readbuf, decomprbuf, ri->csize, ri->dsize);
9094 if (ret) {
9095 printk(KERN_WARNING "Error: jffs2_decompress returned %d\n", ret);
9096 Index: linux-2.4.35.4/fs/jffs2/super.c
9097 ===================================================================
9098 --- linux-2.4.35.4.orig/fs/jffs2/super.c
9099 +++ linux-2.4.35.4/fs/jffs2/super.c
9100 @@ -35,6 +35,7 @@
9101 *
9102 */
9103
9104 +#include "jffs2_bbc_fs.h" /**BBC**/
9105 #include <linux/config.h>
9106 #include <linux/kernel.h>
9107 #include <linux/module.h>
9108 @@ -272,6 +273,7 @@ static struct super_block *jffs2_read_su
9109 sb->s_magic = JFFS2_SUPER_MAGIC;
9110 if (!(sb->s_flags & MS_RDONLY))
9111 jffs2_start_garbage_collect_thread(c);
9112 + jffs2_bbc_load_model(sb); /**BBC**/
9113 return sb;
9114
9115 out_root_i:
9116 @@ -288,6 +290,7 @@ static struct super_block *jffs2_read_su
9117 void jffs2_put_super (struct super_block *sb)
9118 {
9119 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
9120 + jffs2_bbc_unload_model(sb); /**BBC**/
9121
9122 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
9123
9124 @@ -344,6 +347,9 @@ static int __init init_jffs2_fs(void)
9125 {
9126 int ret;
9127
9128 + jffs2_bbc_proc_init(); /**BBC**/
9129 +
9130 +
9131 printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
9132
9133 #ifdef JFFS2_OUT_OF_KERNEL
9134 @@ -388,6 +394,8 @@ static int __init init_jffs2_fs(void)
9135
9136 static void __exit exit_jffs2_fs(void)
9137 {
9138 + jffs2_bbc_proc_deinit(); /**BBC**/
9139 +
9140 jffs2_destroy_slab_caches();
9141 jffs2_zlib_exit();
9142 unregister_filesystem(&jffs2_fs_type);