xref: /OK3568_Linux_fs/u-boot/fs/jffs2/compr_rtime.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * JFFS2 -- Journalling Flash File System, Version 2.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2001 Red Hat, Inc.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Created by Arjan van de Ven <arjanv@redhat.com>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * The original JFFS, from which the design for JFFS2 was derived,
9*4882a593Smuzhiyun  * was designed and implemented by Axis Communications AB.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The contents of this file are subject to the Red Hat eCos Public
12*4882a593Smuzhiyun  * License Version 1.1 (the "Licence"); you may not use this file
13*4882a593Smuzhiyun  * except in compliance with the Licence.  You may obtain a copy of
14*4882a593Smuzhiyun  * the Licence at http://www.redhat.com/
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Software distributed under the Licence is distributed on an "AS IS"
17*4882a593Smuzhiyun  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18*4882a593Smuzhiyun  * See the Licence for the specific language governing rights and
19*4882a593Smuzhiyun  * limitations under the Licence.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * The Original Code is JFFS2 - Journalling Flash File System, version 2
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Alternatively, the contents of this file may be used under the
24*4882a593Smuzhiyun  * terms of the GNU General Public License version 2 (the "GPL"), in
25*4882a593Smuzhiyun  * which case the provisions of the GPL are applicable instead of the
26*4882a593Smuzhiyun  * above.  If you wish to allow the use of your version of this file
27*4882a593Smuzhiyun  * only under the terms of the GPL and not to allow others to use your
28*4882a593Smuzhiyun  * version of this file under the RHEPL, indicate your decision by
29*4882a593Smuzhiyun  * deleting the provisions above and replace them with the notice and
30*4882a593Smuzhiyun  * other provisions required by the GPL.  If you do not delete the
31*4882a593Smuzhiyun  * provisions above, a recipient may use your version of this file
32*4882a593Smuzhiyun  * under either the RHEPL or the GPL.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * $Id: compr_rtime.c,v 1.2 2002/01/24 22:58:42 rfeany Exp $
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * Very simple lz77-ish encoder.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * Theory of operation: Both encoder and decoder have a list of "last
40*4882a593Smuzhiyun  * occurances" for every possible source-value; after sending the
41*4882a593Smuzhiyun  * first source-byte, the second byte indicated the "run" length of
42*4882a593Smuzhiyun  * matches
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * The algorithm is intended to only send "whole bytes", no bit-messing.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <config.h>
49*4882a593Smuzhiyun #include <jffs2/jffs2.h>
50*4882a593Smuzhiyun 
rtime_decompress(unsigned char * data_in,unsigned char * cpage_out,u32 srclen,u32 destlen)51*4882a593Smuzhiyun void rtime_decompress(unsigned char *data_in, unsigned char *cpage_out,
52*4882a593Smuzhiyun 		      u32 srclen, u32 destlen)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	int positions[256];
55*4882a593Smuzhiyun 	int outpos;
56*4882a593Smuzhiyun 	int pos;
57*4882a593Smuzhiyun 	int i;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	outpos = pos = 0;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	for (i = 0; i < 256; positions[i++] = 0);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	while (outpos<destlen) {
64*4882a593Smuzhiyun 		unsigned char value;
65*4882a593Smuzhiyun 		int backoffs;
66*4882a593Smuzhiyun 		int repeat;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 		value = data_in[pos++];
69*4882a593Smuzhiyun 		cpage_out[outpos++] = value; /* first the verbatim copied byte */
70*4882a593Smuzhiyun 		repeat = data_in[pos++];
71*4882a593Smuzhiyun 		backoffs = positions[value];
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 		positions[value]=outpos;
74*4882a593Smuzhiyun 		if (repeat) {
75*4882a593Smuzhiyun 			if (backoffs + repeat >= outpos) {
76*4882a593Smuzhiyun 				while(repeat) {
77*4882a593Smuzhiyun 					cpage_out[outpos++] = cpage_out[backoffs++];
78*4882a593Smuzhiyun 					repeat--;
79*4882a593Smuzhiyun 				}
80*4882a593Smuzhiyun 			} else {
81*4882a593Smuzhiyun 				for (i = 0; i < repeat; i++)
82*4882a593Smuzhiyun 					*(cpage_out + outpos + i) = *(cpage_out + backoffs + i);
83*4882a593Smuzhiyun 				outpos+=repeat;
84*4882a593Smuzhiyun 			}
85*4882a593Smuzhiyun 		}
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun }
88