blob: 79e771ab624f47d7bb20323941a6a22e9c643127 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
David Woodhouse6088c052010-08-08 14:15:22 +01005 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Created by Arjan van de Ven <arjanv@redhat.com>
8 *
9 * For licensing information, see the file 'LICENCE' in this directory.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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>
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000027#include <linux/string.h>
28#include <linux/jffs2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "compr.h"
30
31/* _compress returns the compressed size, -1 if bigger */
32static int jffs2_rtime_compress(unsigned char *data_in,
33 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -040034 uint32_t *sourcelen, uint32_t *dstlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Kamlakant Patel3367da52014-01-06 19:06:54 +053036 unsigned short positions[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 int outpos = 0;
38 int pos=0;
39
Yang Yang7d1044c2021-01-28 02:55:35 -080040 if (*dstlen <= 3)
41 return -1;
42
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000043 memset(positions,0,sizeof(positions));
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
46 int backpos, runlen=0;
47 unsigned char value;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 value = data_in[pos];
50
51 cpage_out[outpos++] = data_in[pos++];
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 backpos = positions[value];
54 positions[value]=pos;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 while ((backpos < pos) && (pos < (*sourcelen)) &&
57 (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
58 pos++;
59 runlen++;
60 }
61 cpage_out[outpos++] = runlen;
62 }
63
64 if (outpos >= pos) {
65 /* We failed */
66 return -1;
67 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 /* Tell the caller how much we managed to compress, and how much space it took */
70 *sourcelen = pos;
71 *dstlen = outpos;
72 return 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000073}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75
76static int jffs2_rtime_decompress(unsigned char *data_in,
77 unsigned char *cpage_out,
Mike Frysinger088bd452010-09-22 22:52:33 -040078 uint32_t srclen, uint32_t destlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Kamlakant Patel3367da52014-01-06 19:06:54 +053080 unsigned short positions[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int outpos = 0;
82 int pos=0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000083
84 memset(positions,0,sizeof(positions));
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 while (outpos<destlen) {
87 unsigned char value;
88 int backoffs;
89 int repeat;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 value = data_in[pos++];
92 cpage_out[outpos++] = value; /* first the verbatim copied byte */
93 repeat = data_in[pos++];
94 backoffs = positions[value];
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 positions[value]=outpos;
97 if (repeat) {
98 if (backoffs + repeat >= outpos) {
99 while(repeat) {
100 cpage_out[outpos++] = cpage_out[backoffs++];
101 repeat--;
102 }
103 } else {
104 memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000105 outpos+=repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107 }
108 }
David Woodhouseef53cb02007-07-10 10:01:22 +0100109 return 0;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112static struct jffs2_compressor jffs2_rtime_comp = {
113 .priority = JFFS2_RTIME_PRIORITY,
114 .name = "rtime",
115 .compr = JFFS2_COMPR_RTIME,
116 .compress = &jffs2_rtime_compress,
117 .decompress = &jffs2_rtime_decompress,
118#ifdef JFFS2_RTIME_DISABLED
119 .disabled = 1,
120#else
121 .disabled = 0,
122#endif
123};
124
125int jffs2_rtime_init(void)
126{
127 return jffs2_register_compressor(&jffs2_rtime_comp);
128}
129
130void jffs2_rtime_exit(void)
131{
132 jffs2_unregister_compressor(&jffs2_rtime_comp);
133}