make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / fs / jffs2 / compr_rtime.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001 Red Hat, Inc.
5  *
6  * Created by Arjan van de Ven <arjanv@redhat.com>
7  *
8  * The original JFFS, from which the design for JFFS2 was derived,
9  * was designed and implemented by Axis Communications AB.
10  *
11  * The contents of this file are subject to the Red Hat eCos Public
12  * License Version 1.1 (the "Licence"); you may not use this file
13  * except in compliance with the Licence.  You may obtain a copy of
14  * the Licence at http://www.redhat.com/
15  *
16  * Software distributed under the Licence is distributed on an "AS IS"
17  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18  * See the Licence for the specific language governing rights and
19  * limitations under the Licence.
20  *
21  * The Original Code is JFFS2 - Journalling Flash File System, version 2
22  *
23  * Alternatively, the contents of this file may be used under the
24  * terms of the GNU General Public License version 2 (the "GPL"), in
25  * which case the provisions of the GPL are applicable instead of the
26  * above.  If you wish to allow the use of your version of this file
27  * only under the terms of the GPL and not to allow others to use your
28  * version of this file under the RHEPL, indicate your decision by
29  * deleting the provisions above and replace them with the notice and
30  * other provisions required by the GPL.  If you do not delete the
31  * provisions above, a recipient may use your version of this file
32  * under either the RHEPL or the GPL.
33  *
34  * $Id: compr_rtime.c,v 1.1.1.1 2005/04/11 02:50:46 jack Exp $
35  *
36  *
37  * Very simple lz77-ish encoder.
38  *
39  * Theory of operation: Both encoder and decoder have a list of "last
40  * occurances" for every possible source-value; after sending the
41  * first source-byte, the second byte indicated the "run" length of
42  * matches
43  *
44  * The algorithm is intended to only send "whole bytes", no bit-messing.
45  *
46  */
47
48 #include <linux/kernel.h>
49 #include <linux/types.h>
50 #include <linux/errno.h>
51 #include <linux/string.h> 
52
53 /* _compress returns the compressed size, -1 if bigger */
54 int rtime_compress(unsigned char *data_in, unsigned char *cpage_out, 
55                    __u32 *sourcelen, __u32 *dstlen)
56 {
57         int positions[256];
58         int outpos = 0;
59         int pos=0;
60
61         memset(positions,0,sizeof(positions)); 
62         
63         while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
64                 int backpos, runlen=0;
65                 unsigned char value;
66                 
67                 value = data_in[pos];
68
69                 cpage_out[outpos++] = data_in[pos++];
70                 
71                 backpos = positions[value];
72                 positions[value]=pos;
73                 
74                 while ((backpos < pos) && (pos < (*sourcelen)) &&
75                        (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
76                         pos++;
77                         runlen++;
78                 }
79                 cpage_out[outpos++] = runlen;
80         }
81
82         if (outpos >= pos) {
83                 /* We failed */
84                 return -1;
85         }
86         
87         /* Tell the caller how much we managed to compress, and how much space it took */
88         *sourcelen = pos;
89         *dstlen = outpos;
90         return 0;
91 }                  
92
93
94 void rtime_decompress(unsigned char *data_in, unsigned char *cpage_out,
95                       __u32 srclen, __u32 destlen)
96 {
97         int positions[256];
98         int outpos = 0;
99         int pos=0;
100         
101         memset(positions,0,sizeof(positions)); 
102         
103         while (outpos<destlen) {
104                 unsigned char value;
105                 int backoffs;
106                 int repeat;
107                 
108                 value = data_in[pos++];
109                 cpage_out[outpos++] = value; /* first the verbatim copied byte */
110                 repeat = data_in[pos++];
111                 backoffs = positions[value];
112                 
113                 positions[value]=outpos;
114                 if (repeat) {
115                         if (backoffs + repeat >= outpos) {
116                                 while(repeat) {
117                                         cpage_out[outpos++] = cpage_out[backoffs++];
118                                         repeat--;
119                                 }
120                         } else {
121                                 memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
122                                 outpos+=repeat;         
123                         }
124                 }
125         }               
126 }                  
127
128