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