VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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.14 2004/06/23 16:34:40 havasi 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 #include <linux/jffs2.h> 
29 #include "compr.h"
30
31 /* _compress returns the compressed size, -1 if bigger */
32 int jffs2_rtime_compress(unsigned char *data_in, unsigned char *cpage_out, 
33                    uint32_t *sourcelen, uint32_t *dstlen, void *model)
34 {
35         short positions[256];
36         int outpos = 0;
37         int pos=0;
38
39         memset(positions,0,sizeof(positions)); 
40         
41         while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
42                 int backpos, runlen=0;
43                 unsigned char value;
44                 
45                 value = data_in[pos];
46
47                 cpage_out[outpos++] = data_in[pos++];
48                 
49                 backpos = positions[value];
50                 positions[value]=pos;
51                 
52                 while ((backpos < pos) && (pos < (*sourcelen)) &&
53                        (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
54                         pos++;
55                         runlen++;
56                 }
57                 cpage_out[outpos++] = runlen;
58         }
59
60         if (outpos >= pos) {
61                 /* We failed */
62                 return -1;
63         }
64         
65         /* Tell the caller how much we managed to compress, and how much space it took */
66         *sourcelen = pos;
67         *dstlen = outpos;
68         return 0;
69 }                  
70
71
72 int jffs2_rtime_decompress(unsigned char *data_in, unsigned char *cpage_out,
73                       uint32_t srclen, uint32_t destlen, void *model)
74 {
75         short positions[256];
76         int outpos = 0;
77         int pos=0;
78         
79         memset(positions,0,sizeof(positions)); 
80         
81         while (outpos<destlen) {
82                 unsigned char value;
83                 int backoffs;
84                 int repeat;
85                 
86                 value = data_in[pos++];
87                 cpage_out[outpos++] = value; /* first the verbatim copied byte */
88                 repeat = data_in[pos++];
89                 backoffs = positions[value];
90                 
91                 positions[value]=outpos;
92                 if (repeat) {
93                         if (backoffs + repeat >= outpos) {
94                                 while(repeat) {
95                                         cpage_out[outpos++] = cpage_out[backoffs++];
96                                         repeat--;
97                                 }
98                         } else {
99                                 memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
100                                 outpos+=repeat;         
101                         }
102                 }
103         }
104         return 0;
105 }                  
106
107 static struct jffs2_compressor jffs2_rtime_comp = {
108     .priority = JFFS2_RTIME_PRIORITY,
109     .name = "rtime",
110     .compr = JFFS2_COMPR_RTIME,
111     .compress = &jffs2_rtime_compress,
112     .decompress = &jffs2_rtime_decompress,
113 #ifdef JFFS2_RTIME_DISABLED
114     .disabled = 1,
115 #else
116     .disabled = 0,
117 #endif
118 };
119
120 int jffs2_rtime_init(void)
121 {
122     return jffs2_register_compressor(&jffs2_rtime_comp);
123 }
124
125 void jffs2_rtime_exit(void)
126 {
127     jffs2_unregister_compressor(&jffs2_rtime_comp);
128 }