source: rtems/cpukit/libfs/src/jffs2/src/compr_rtime.c @ c77cd426

5
Last change on this file since c77cd426 was 6ac6a5c8, checked in by Sebastian Huber <sebastian.huber@…>, on 03/27/18 at 11:01:56

jffs2: Do not use command line defines

Update #3375.

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