2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by Arjan van de Ven <arjanv@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: compr_rtime.c,v 1.14 2004/06/23 16:34:40 havasi Exp $
13 * Very simple lz77-ish encoder.
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
20 * The algorithm is intended to only send "whole bytes", no bit-messing.
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>
31 /* _compress returns the compressed size, -1 if bigger */
32 static int jffs2_rtime_compress(unsigned char *data_in,
33 unsigned char *cpage_out,
34 uint32_t *sourcelen, uint32_t *dstlen,
41 memset(positions,0,sizeof(positions));
43 while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
44 int backpos, runlen=0;
49 cpage_out[outpos++] = data_in[pos++];
51 backpos = positions[value];
54 while ((backpos < pos) && (pos < (*sourcelen)) &&
55 (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
59 cpage_out[outpos++] = runlen;
67 /* Tell the caller how much we managed to compress, and how much space it took */
74 static int jffs2_rtime_decompress(unsigned char *data_in,
75 unsigned char *cpage_out,
76 uint32_t srclen, uint32_t destlen,
83 memset(positions,0,sizeof(positions));
85 while (outpos<destlen) {
90 value = data_in[pos++];
91 cpage_out[outpos++] = value; /* first the verbatim copied byte */
92 repeat = data_in[pos++];
93 backoffs = positions[value];
95 positions[value]=outpos;
97 if (backoffs + repeat >= outpos) {
99 cpage_out[outpos++] = cpage_out[backoffs++];
103 memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
111 static struct jffs2_compressor jffs2_rtime_comp = {
112 .priority = JFFS2_RTIME_PRIORITY,
114 .compr = JFFS2_COMPR_RTIME,
115 .compress = &jffs2_rtime_compress,
116 .decompress = &jffs2_rtime_decompress,
117 #ifdef JFFS2_RTIME_DISABLED
124 int jffs2_rtime_init(void)
126 return jffs2_register_compressor(&jffs2_rtime_comp);
129 void jffs2_rtime_exit(void)
131 jffs2_unregister_compressor(&jffs2_rtime_comp);