^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * JFFS2 -- Journalling Flash File System, Version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright © 2001-2007 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Created by Arjan van de Ven <arjanv@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * For licensing information, see the file 'LICENCE' in this directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Very simple lz77-ish encoder.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Theory of operation: Both encoder and decoder have a list of "last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * occurrences" for every possible source-value; after sending the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * first source-byte, the second byte indicated the "run" length of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * The algorithm is intended to only send "whole bytes", no bit-messing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/jffs2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "compr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* _compress returns the compressed size, -1 if bigger */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int jffs2_rtime_compress(unsigned char *data_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned char *cpage_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) uint32_t *sourcelen, uint32_t *dstlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned short positions[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int outpos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int pos=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (*dstlen <= 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) memset(positions,0,sizeof(positions));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int backpos, runlen=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned char value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) value = data_in[pos];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) cpage_out[outpos++] = data_in[pos++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) backpos = positions[value];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) positions[value]=pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) while ((backpos < pos) && (pos < (*sourcelen)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) runlen++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) cpage_out[outpos++] = runlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (outpos >= pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* We failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* Tell the caller how much we managed to compress, and how much space it took */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *sourcelen = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *dstlen = outpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int jffs2_rtime_decompress(unsigned char *data_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned char *cpage_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) uint32_t srclen, uint32_t destlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned short positions[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int outpos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int pos=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) memset(positions,0,sizeof(positions));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) while (outpos<destlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned char value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int backoffs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) value = data_in[pos++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) cpage_out[outpos++] = value; /* first the verbatim copied byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) repeat = data_in[pos++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) backoffs = positions[value];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) positions[value]=outpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (repeat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (backoffs + repeat >= outpos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) while(repeat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) cpage_out[outpos++] = cpage_out[backoffs++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) repeat--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) outpos+=repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static struct jffs2_compressor jffs2_rtime_comp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .priority = JFFS2_RTIME_PRIORITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .name = "rtime",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .compr = JFFS2_COMPR_RTIME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .compress = &jffs2_rtime_compress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .decompress = &jffs2_rtime_decompress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #ifdef JFFS2_RTIME_DISABLED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .disabled = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .disabled = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int jffs2_rtime_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return jffs2_register_compressor(&jffs2_rtime_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) void jffs2_rtime_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) jffs2_unregister_compressor(&jffs2_rtime_comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }