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 © 2001-2007 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 * 11 * 10 * 12 * 11 * 13 * Very simple lz77-ish encoder. 12 * Very simple lz77-ish encoder. 14 * 13 * 15 * Theory of operation: Both encoder and decod 14 * Theory of operation: Both encoder and decoder have a list of "last 16 * occurrences" for every possible source-valu 15 * occurrences" for every possible source-value; after sending the 17 * first source-byte, the second byte indicate 16 * first source-byte, the second byte indicated the "run" length of 18 * matches 17 * matches 19 * 18 * 20 * The algorithm is intended to only send "who 19 * The algorithm is intended to only send "whole bytes", no bit-messing. 21 * 20 * 22 */ 21 */ 23 22 24 #include <linux/kernel.h> 23 #include <linux/kernel.h> 25 #include <linux/types.h> 24 #include <linux/types.h> 26 #include <linux/errno.h> 25 #include <linux/errno.h> 27 #include <linux/string.h> 26 #include <linux/string.h> 28 #include <linux/jffs2.h> 27 #include <linux/jffs2.h> 29 #include "compr.h" 28 #include "compr.h" 30 29 31 /* _compress returns the compressed size, -1 i 30 /* _compress returns the compressed size, -1 if bigger */ 32 static int jffs2_rtime_compress(unsigned char 31 static int jffs2_rtime_compress(unsigned char *data_in, 33 unsigned char 32 unsigned char *cpage_out, 34 uint32_t *sour !! 33 uint32_t *sourcelen, uint32_t *dstlen, >> 34 void *model) 35 { 35 { 36 unsigned short positions[256]; !! 36 short positions[256]; 37 int outpos = 0; 37 int outpos = 0; 38 int pos=0; 38 int pos=0; 39 39 40 if (*dstlen <= 3) << 41 return -1; << 42 << 43 memset(positions,0,sizeof(positions)); 40 memset(positions,0,sizeof(positions)); 44 41 45 while (pos < (*sourcelen) && outpos <= 42 while (pos < (*sourcelen) && outpos <= (*dstlen)-2) { 46 int backpos, runlen=0; 43 int backpos, runlen=0; 47 unsigned char value; 44 unsigned char value; 48 45 49 value = data_in[pos]; 46 value = data_in[pos]; 50 47 51 cpage_out[outpos++] = data_in[ 48 cpage_out[outpos++] = data_in[pos++]; 52 49 53 backpos = positions[value]; 50 backpos = positions[value]; 54 positions[value]=pos; 51 positions[value]=pos; 55 52 56 while ((backpos < pos) && (pos 53 while ((backpos < pos) && (pos < (*sourcelen)) && 57 (data_in[pos]==data_in[ 54 (data_in[pos]==data_in[backpos++]) && (runlen<255)) { 58 pos++; 55 pos++; 59 runlen++; 56 runlen++; 60 } 57 } 61 cpage_out[outpos++] = runlen; 58 cpage_out[outpos++] = runlen; 62 } 59 } 63 60 64 if (outpos >= pos) { 61 if (outpos >= pos) { 65 /* We failed */ 62 /* We failed */ 66 return -1; 63 return -1; 67 } 64 } 68 65 69 /* Tell the caller how much we managed 66 /* Tell the caller how much we managed to compress, and how much space it took */ 70 *sourcelen = pos; 67 *sourcelen = pos; 71 *dstlen = outpos; 68 *dstlen = outpos; 72 return 0; 69 return 0; 73 } 70 } 74 71 75 72 76 static int jffs2_rtime_decompress(unsigned cha 73 static int jffs2_rtime_decompress(unsigned char *data_in, 77 unsigned cha 74 unsigned char *cpage_out, 78 uint32_t src !! 75 uint32_t srclen, uint32_t destlen, >> 76 void *model) 79 { 77 { 80 unsigned short positions[256]; !! 78 short positions[256]; 81 int outpos = 0; 79 int outpos = 0; 82 int pos=0; 80 int pos=0; 83 81 84 memset(positions,0,sizeof(positions)); 82 memset(positions,0,sizeof(positions)); 85 83 86 while (outpos<destlen) { 84 while (outpos<destlen) { 87 unsigned char value; 85 unsigned char value; 88 int backoffs; 86 int backoffs; 89 int repeat; 87 int repeat; 90 88 91 value = data_in[pos++]; 89 value = data_in[pos++]; 92 cpage_out[outpos++] = value; / 90 cpage_out[outpos++] = value; /* first the verbatim copied byte */ 93 repeat = data_in[pos++]; 91 repeat = data_in[pos++]; 94 backoffs = positions[value]; 92 backoffs = positions[value]; 95 93 96 positions[value]=outpos; 94 positions[value]=outpos; 97 if (repeat) { 95 if (repeat) { 98 if (backoffs + repeat 96 if (backoffs + repeat >= outpos) { 99 while(repeat) 97 while(repeat) { 100 cpage_ 98 cpage_out[outpos++] = cpage_out[backoffs++]; 101 repeat 99 repeat--; 102 } 100 } 103 } else { 101 } else { 104 memcpy(&cpage_ 102 memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat); 105 outpos+=repeat 103 outpos+=repeat; 106 } 104 } 107 } 105 } 108 } 106 } 109 return 0; 107 return 0; 110 } 108 } 111 109 112 static struct jffs2_compressor jffs2_rtime_com 110 static struct jffs2_compressor jffs2_rtime_comp = { 113 .priority = JFFS2_RTIME_PRIORITY, 111 .priority = JFFS2_RTIME_PRIORITY, 114 .name = "rtime", 112 .name = "rtime", 115 .compr = JFFS2_COMPR_RTIME, 113 .compr = JFFS2_COMPR_RTIME, 116 .compress = &jffs2_rtime_compress, 114 .compress = &jffs2_rtime_compress, 117 .decompress = &jffs2_rtime_decompress, 115 .decompress = &jffs2_rtime_decompress, 118 #ifdef JFFS2_RTIME_DISABLED 116 #ifdef JFFS2_RTIME_DISABLED 119 .disabled = 1, 117 .disabled = 1, 120 #else 118 #else 121 .disabled = 0, 119 .disabled = 0, 122 #endif 120 #endif 123 }; 121 }; 124 122 125 int jffs2_rtime_init(void) 123 int jffs2_rtime_init(void) 126 { 124 { 127 return jffs2_register_compressor(&jffs2_rt 125 return jffs2_register_compressor(&jffs2_rtime_comp); 128 } 126 } 129 127 130 void jffs2_rtime_exit(void) 128 void jffs2_rtime_exit(void) 131 { 129 { 132 jffs2_unregister_compressor(&jffs2_rtime_c 130 jffs2_unregister_compressor(&jffs2_rtime_comp); 133 } 131 } 134 132
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.