1 /* 1 /* 2 * JFFS2 -- Journalling Flash File System, Ver 2 * JFFS2 -- Journalling Flash File System, Version 2. 3 * 3 * 4 * Copyright © 2001-2007 Red Hat, Inc. !! 4 * Copyright (C) 2001-2003 Red Hat, Inc. 5 * Copyright © 2004-2010 David Woodhouse <dwm << 6 * 5 * 7 * Created by Arjan van de Ven <arjanv@redhat. 6 * Created by Arjan van de Ven <arjanv@redhat.com> 8 * 7 * 9 * For licensing information, see the file 'LI 8 * For licensing information, see the file 'LICENCE' in this directory. 10 * 9 * >> 10 * $Id: compr_rtime.c,v 1.11 2003/10/04 08:33:06 dwmw2 Exp $ 11 * 11 * 12 * 12 * 13 * Very simple lz77-ish encoder. 13 * Very simple lz77-ish encoder. 14 * 14 * 15 * Theory of operation: Both encoder and decod 15 * Theory of operation: Both encoder and decoder have a list of "last 16 * occurrences" for every possible source-valu 16 * occurrences" for every possible source-value; after sending the 17 * first source-byte, the second byte indicate 17 * first source-byte, the second byte indicated the "run" length of 18 * matches 18 * matches 19 * 19 * 20 * The algorithm is intended to only send "who 20 * The algorithm is intended to only send "whole bytes", no bit-messing. 21 * 21 * 22 */ 22 */ 23 23 24 #include <linux/kernel.h> 24 #include <linux/kernel.h> 25 #include <linux/types.h> 25 #include <linux/types.h> 26 #include <linux/errno.h> 26 #include <linux/errno.h> 27 #include <linux/string.h> !! 27 #include <linux/string.h> 28 #include <linux/jffs2.h> << 29 #include "compr.h" << 30 28 31 /* _compress returns the compressed size, -1 i 29 /* _compress returns the compressed size, -1 if bigger */ 32 static int jffs2_rtime_compress(unsigned char !! 30 int jffs2_rtime_compress(unsigned char *data_in, unsigned char *cpage_out, 33 unsigned char !! 31 uint32_t *sourcelen, uint32_t *dstlen) 34 uint32_t *sour << 35 { 32 { 36 unsigned short positions[256]; !! 33 short positions[256]; 37 int outpos = 0; 34 int outpos = 0; 38 int pos=0; 35 int pos=0; 39 36 40 if (*dstlen <= 3) !! 37 memset(positions,0,sizeof(positions)); 41 return -1; !! 38 42 << 43 memset(positions,0,sizeof(positions)); << 44 << 45 while (pos < (*sourcelen) && outpos <= 39 while (pos < (*sourcelen) && outpos <= (*dstlen)-2) { 46 int backpos, runlen=0; 40 int backpos, runlen=0; 47 unsigned char value; 41 unsigned char value; 48 !! 42 49 value = data_in[pos]; 43 value = data_in[pos]; 50 44 51 cpage_out[outpos++] = data_in[ 45 cpage_out[outpos++] = data_in[pos++]; 52 !! 46 53 backpos = positions[value]; 47 backpos = positions[value]; 54 positions[value]=pos; 48 positions[value]=pos; 55 !! 49 56 while ((backpos < pos) && (pos 50 while ((backpos < pos) && (pos < (*sourcelen)) && 57 (data_in[pos]==data_in[ 51 (data_in[pos]==data_in[backpos++]) && (runlen<255)) { 58 pos++; 52 pos++; 59 runlen++; 53 runlen++; 60 } 54 } 61 cpage_out[outpos++] = runlen; 55 cpage_out[outpos++] = runlen; 62 } 56 } 63 57 64 if (outpos >= pos) { 58 if (outpos >= pos) { 65 /* We failed */ 59 /* We failed */ 66 return -1; 60 return -1; 67 } 61 } 68 !! 62 69 /* Tell the caller how much we managed 63 /* Tell the caller how much we managed to compress, and how much space it took */ 70 *sourcelen = pos; 64 *sourcelen = pos; 71 *dstlen = outpos; 65 *dstlen = outpos; 72 return 0; 66 return 0; 73 } !! 67 } 74 68 75 69 76 static int jffs2_rtime_decompress(unsigned cha !! 70 void jffs2_rtime_decompress(unsigned char *data_in, unsigned char *cpage_out, 77 unsigned cha !! 71 uint32_t srclen, uint32_t destlen) 78 uint32_t src << 79 { 72 { 80 unsigned short positions[256]; !! 73 short positions[256]; 81 int outpos = 0; 74 int outpos = 0; 82 int pos=0; 75 int pos=0; 83 !! 76 84 memset(positions,0,sizeof(positions)); !! 77 memset(positions,0,sizeof(positions)); 85 !! 78 86 while (outpos<destlen) { 79 while (outpos<destlen) { 87 unsigned char value; 80 unsigned char value; 88 int backoffs; 81 int backoffs; 89 int repeat; 82 int repeat; 90 !! 83 91 value = data_in[pos++]; 84 value = data_in[pos++]; 92 cpage_out[outpos++] = value; / 85 cpage_out[outpos++] = value; /* first the verbatim copied byte */ 93 repeat = data_in[pos++]; 86 repeat = data_in[pos++]; 94 backoffs = positions[value]; 87 backoffs = positions[value]; 95 !! 88 96 positions[value]=outpos; 89 positions[value]=outpos; 97 if (repeat) { 90 if (repeat) { 98 if (backoffs + repeat 91 if (backoffs + repeat >= outpos) { 99 while(repeat) 92 while(repeat) { 100 cpage_ 93 cpage_out[outpos++] = cpage_out[backoffs++]; 101 repeat 94 repeat--; 102 } 95 } 103 } else { 96 } else { 104 memcpy(&cpage_ 97 memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat); 105 outpos+=repeat !! 98 outpos+=repeat; 106 } 99 } 107 } 100 } 108 } !! 101 } 109 return 0; !! 102 } 110 } << 111 << 112 static struct jffs2_compressor jffs2_rtime_com << 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 103 125 int jffs2_rtime_init(void) << 126 { << 127 return jffs2_register_compressor(&jffs2_rt << 128 } << 129 104 130 void jffs2_rtime_exit(void) << 131 { << 132 jffs2_unregister_compressor(&jffs2_rtime_c << 133 } << 134 105
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.