ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / jffs2 / compr_rtime.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  *
6  * Created by Arjan van de Ven <arjanv@redhat.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: compr_rtime.c,v 1.11 2003/10/04 08:33:06 dwmw2 Exp $
11  *
12  *
13  * Very simple lz77-ish encoder.
14  *
15  * Theory of operation: Both encoder and decoder have a list of "last
16  * occurrences" for every possible source-value; after sending the
17  * first source-byte, the second byte indicated the "run" length of
18  * matches
19  *
20  * The algorithm is intended to only send "whole bytes", no bit-messing.
21  *
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/string.h> 
28
29 /* _compress returns the compressed size, -1 if bigger */
30 int jffs2_rtime_compress(unsigned char *data_in, unsigned char *cpage_out, 
31                    uint32_t *sourcelen, uint32_t *dstlen)
32 {
33         short positions[256];
34         int outpos = 0;
35         int pos=0;
36
37         memset(positions,0,sizeof(positions)); 
38         
39         while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
40                 int backpos, runlen=0;
41                 unsigned char value;
42                 
43                 value = data_in[pos];
44
45                 cpage_out[outpos++] = data_in[pos++];
46                 
47                 backpos = positions[value];
48                 positions[value]=pos;
49                 
50                 while ((backpos < pos) && (pos < (*sourcelen)) &&
51                        (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
52                         pos++;
53                         runlen++;
54                 }
55                 cpage_out[outpos++] = runlen;
56         }
57
58         if (outpos >= pos) {
59                 /* We failed */
60                 return -1;
61         }
62         
63         /* Tell the caller how much we managed to compress, and how much space it took */
64         *sourcelen = pos;
65         *dstlen = outpos;
66         return 0;
67 }                  
68
69
70 void jffs2_rtime_decompress(unsigned char *data_in, unsigned char *cpage_out,
71                       uint32_t srclen, uint32_t destlen)
72 {
73         short positions[256];
74         int outpos = 0;
75         int pos=0;
76         
77         memset(positions,0,sizeof(positions)); 
78         
79         while (outpos<destlen) {
80                 unsigned char value;
81                 int backoffs;
82                 int repeat;
83                 
84                 value = data_in[pos++];
85                 cpage_out[outpos++] = value; /* first the verbatim copied byte */
86                 repeat = data_in[pos++];
87                 backoffs = positions[value];
88                 
89                 positions[value]=outpos;
90                 if (repeat) {
91                         if (backoffs + repeat >= outpos) {
92                                 while(repeat) {
93                                         cpage_out[outpos++] = cpage_out[backoffs++];
94                                         repeat--;
95                                 }
96                         } else {
97                                 memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
98                                 outpos+=repeat;         
99                         }
100                 }
101         }               
102 }                  
103
104