2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
6 * Created by Arjan van de Ven <arjanv@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
12 * Very simple lz77-ish encoder.
14 * Theory of operation: Both encoder and decoder have a list of "last
15 * occurrences" for every possible source-value; after sending the
16 * first source-byte, the second byte indicated the "run" length of
19 * The algorithm is intended to only send "whole bytes", no bit-messing.
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/jffs2.h>
30 /* _compress returns the compressed size, -1 if bigger */
31 static int jffs2_rtime_compress(unsigned char *data_in,
32 unsigned char *cpage_out,
33 uint32_t *sourcelen, uint32_t *dstlen,
40 memset(positions,0,sizeof(positions));
42 while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
43 int backpos, runlen=0;
48 cpage_out[outpos++] = data_in[pos++];
50 backpos = positions[value];
53 while ((backpos < pos) && (pos < (*sourcelen)) &&
54 (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
58 cpage_out[outpos++] = runlen;
66 /* Tell the caller how much we managed to compress, and how much space it took */
73 static int jffs2_rtime_decompress(unsigned char *data_in,
74 unsigned char *cpage_out,
75 uint32_t srclen, uint32_t destlen,
82 memset(positions,0,sizeof(positions));
84 while (outpos<destlen) {
89 value = data_in[pos++];
90 cpage_out[outpos++] = value; /* first the verbatim copied byte */
91 repeat = data_in[pos++];
92 backoffs = positions[value];
94 positions[value]=outpos;
96 if (backoffs + repeat >= outpos) {
98 cpage_out[outpos++] = cpage_out[backoffs++];
102 memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
110 static struct jffs2_compressor jffs2_rtime_comp = {
111 .priority = JFFS2_RTIME_PRIORITY,
113 .compr = JFFS2_COMPR_RTIME,
114 .compress = &jffs2_rtime_compress,
115 .decompress = &jffs2_rtime_decompress,
116 #ifdef JFFS2_RTIME_DISABLED
123 int jffs2_rtime_init(void)
125 return jffs2_register_compressor(&jffs2_rtime_comp);
128 void jffs2_rtime_exit(void)
130 jffs2_unregister_compressor(&jffs2_rtime_comp);