2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
7 * Created by Arjan van de Ven <arjanv@redhat.com>
9 * For licensing information, see the file 'LICENCE' in this directory.
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)
36 unsigned short positions[256];
43 memset(positions,0,sizeof(positions));
45 while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
46 int backpos, runlen=0;
51 cpage_out[outpos++] = data_in[pos++];
53 backpos = positions[value];
56 while ((backpos < pos) && (pos < (*sourcelen)) &&
57 (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
61 cpage_out[outpos++] = runlen;
69 /* Tell the caller how much we managed to compress, and how much space it took */
76 static int jffs2_rtime_decompress(unsigned char *data_in,
77 unsigned char *cpage_out,
78 uint32_t srclen, uint32_t destlen)
80 unsigned short positions[256];
84 memset(positions,0,sizeof(positions));
86 while (outpos<destlen) {
91 value = data_in[pos++];
92 cpage_out[outpos++] = value; /* first the verbatim copied byte */
93 repeat = data_in[pos++];
94 backoffs = positions[value];
96 positions[value]=outpos;
98 if (backoffs + repeat >= outpos) {
100 cpage_out[outpos++] = cpage_out[backoffs++];
104 memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
112 static struct jffs2_compressor jffs2_rtime_comp = {
113 .priority = JFFS2_RTIME_PRIORITY,
115 .compr = JFFS2_COMPR_RTIME,
116 .compress = &jffs2_rtime_compress,
117 .decompress = &jffs2_rtime_decompress,
118 #ifdef JFFS2_RTIME_DISABLED
125 int jffs2_rtime_init(void)
127 return jffs2_register_compressor(&jffs2_rtime_comp);
130 void jffs2_rtime_exit(void)
132 jffs2_unregister_compressor(&jffs2_rtime_comp);