embox

Форк
0
/
compr_rtime.c 
127 строк · 2.9 Кб
1
/*
2
 * JFFS2 -- Journalling Flash File System, Version 2.
3
 *
4
 * Copyright (C) 2001-2003 Red Hat, Inc.
5
 *
6
 * Created by Arjan van de Ven <arjanv@redhat.com>
7
 *
8
 * For licensing information, see the file 'LICENCE' in this directory.
9
 *
10
 * $Id: compr_rtime.c,v 1.15 2005/03/17 20:23:06 gleixner Exp $
11
 *
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>
27
#include <linux/string.h>
28
#include <fs/jffs2.h>
29
#include "compr.h"
30

31
/* _compress returns the compressed size, -1 if bigger */
32
static int jffs2_rtime_compress(unsigned char *data_in,
33
				unsigned char *cpage_out,uint32_t *sourcelen,
34
								uint32_t *dstlen, void *model) {
35
	short positions[256];
36
	int outpos = 0;
37
	int pos=0;
38

39
	memset(positions,0,sizeof(positions));
40

41
	while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
42
		int backpos, runlen=0;
43
		unsigned char value;
44

45
		value = data_in[pos];
46

47
		cpage_out[outpos++] = data_in[pos++];
48

49
		backpos = positions[value];
50
		positions[value]=pos;
51

52
		while ((backpos < pos) && (pos < (*sourcelen)) &&
53
		       (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
54
			pos++;
55
			runlen++;
56
		}
57
		cpage_out[outpos++] = runlen;
58
	}
59

60
	if (outpos >= pos) {
61
		/* We failed */
62
		return -1;
63
	}
64

65
	/* Tell the caller how much we managed to compress, and how much space it took */
66
	*sourcelen = pos;
67
	*dstlen = outpos;
68
	return 0;
69
}
70

71

72
static int jffs2_rtime_decompress(unsigned char *data_in,
73
				  unsigned char *cpage_out,
74
				  uint32_t srclen, uint32_t destlen,
75
				  void *model) {
76
	short positions[256];
77
	int outpos = 0;
78
	int pos=0;
79

80
	memset(positions,0,sizeof(positions));
81

82
	while (outpos<destlen) {
83
		unsigned char value;
84
		int backoffs;
85
		int repeat;
86

87
		value = data_in[pos++];
88
		cpage_out[outpos++] = value; /* first the verbatim copied byte */
89
		repeat = data_in[pos++];
90
		backoffs = positions[value];
91

92
		positions[value]=outpos;
93
		if (repeat) {
94
			if (backoffs + repeat >= outpos) {
95
				while(repeat) {
96
					cpage_out[outpos++] = cpage_out[backoffs++];
97
					repeat--;
98
				}
99
			} else {
100
				memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
101
				outpos+=repeat;
102
			}
103
		}
104
	}
105
        return 0;
106
}
107

108
static struct jffs2_compressor jffs2_rtime_comp = {
109
    .priority = JFFS2_RTIME_PRIORITY,
110
    .name = "rtime",
111
    .compr = JFFS2_COMPR_RTIME,
112
    .compress = &jffs2_rtime_compress,
113
    .decompress = &jffs2_rtime_decompress,
114
#ifdef JFFS2_RTIME_DISABLED
115
    .disabled = 1,
116
#else
117
    .disabled = 0,
118
#endif
119
};
120

121
int jffs2_rtime_init(void) {
122
    return jffs2_register_compressor(&jffs2_rtime_comp);
123
}
124

125
void jffs2_rtime_exit(void) {
126
    jffs2_unregister_compressor(&jffs2_rtime_comp);
127
}
128

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.