2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2004 Patrik Kluba,
5 * University of Szeged, Hungary
7 * For licensing information, see the file 'LICENCE' in the
10 * $Id: compr_lzo.c,v 1.3 2004/06/23 16:34:39 havasi Exp $
15 LZO1X-1 (and -999) compression module for jffs2
16 based on the original LZO sources
19 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
22 Original copyright notice follows:
24 lzo1x_9x.c -- implementation of the LZO1X-999 compression algorithm
25 lzo_ptr.h -- low-level pointer constructs
26 lzo_swd.ch -- sliding window dictionary
27 lzoconf.h -- configuration for the LZO real-time data compression library
28 lzo_mchw.ch -- matching functions using a window
29 minilzo.c -- mini subset of the LZO real-time data compression library
30 config1x.h -- configuration for the LZO1X algorithm
31 lzo1x.h -- public interface of the LZO1X compression algorithm
33 These files are part of the LZO real-time data compression library.
35 Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
38 The LZO library is free software; you can redistribute it and/or
39 modify it under the terms of the GNU General Public License as
40 published by the Free Software Foundation; either version 2 of
41 the License, or (at your option) any later version.
43 The LZO library is distributed in the hope that it will be useful,
44 but WITHOUT ANY WARRANTY; without even the implied warranty of
45 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 GNU General Public License for more details.
48 You should have received a copy of the GNU General Public License
49 along with the LZO library; see the file COPYING.
50 If not, write to the Free Software Foundation, Inc.,
51 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
53 Markus F.X.J. Oberhumer
54 <markus@oberhumer.com>
59 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
61 -removed all 16 bit code
62 -all sensitive data will be on 4 byte boundary
63 -removed check parts for library use
64 -removed all but LZO1X-* compression
70 #include <linux/stddef.h>
71 #include <jffs2/jffs2.h>
72 #include <jffs2/compr_rubin.h>
74 /* Integral types that have *exactly* the same number of bits as a lzo_voidp */
75 typedef unsigned long lzo_ptr_t;
76 typedef long lzo_sptr_t;
78 /* data type definitions */
79 #define U32 unsigned long
80 #define S32 signed long
82 #define U16 unsigned short
83 #define S16 signed short
85 #define U8 unsigned char
86 #define S8 signed char
89 #define M1_MAX_OFFSET 0x0400
90 #define M2_MAX_OFFSET 0x0800
91 #define M3_MAX_OFFSET 0x4000
92 #define M4_MAX_OFFSET 0xbfff
94 #define __COPY4(dst,src) * (lzo_uint32p)(dst) = * (const lzo_uint32p)(src)
95 #define COPY4(dst,src) __COPY4((lzo_ptr_t)(dst),(lzo_ptr_t)(src))
97 #define TEST_IP (ip < ip_end)
98 #define TEST_OP (op <= op_end)
101 if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x)) goto input_overrun
103 if ((lzo_uint)(op_end - op) < (lzo_uint)(x)) goto output_overrun
104 #define TEST_LOOKBEHIND(m_pos,out) if (m_pos < out) goto lookbehind_overrun
106 typedef U32 lzo_uint32;
107 typedef I32 lzo_int32;
108 typedef U32 lzo_uint;
110 typedef int lzo_bool;
113 #define lzo_bytep U8 *
114 #define lzo_charp char *
115 #define lzo_voidp void *
116 #define lzo_shortp short *
117 #define lzo_ushortp unsigned short *
118 #define lzo_uint32p lzo_uint32 *
119 #define lzo_int32p lzo_int32 *
120 #define lzo_uintp lzo_uint *
121 #define lzo_intp lzo_int *
122 #define lzo_voidpp lzo_voidp *
123 #define lzo_bytepp lzo_bytep *
124 #define lzo_sizeof_dict_t sizeof(lzo_bytep)
127 #define LZO_E_ERROR (-1)
128 #define LZO_E_OUT_OF_MEMORY (-2) /* not used right now */
129 #define LZO_E_NOT_COMPRESSIBLE (-3) /* not used right now */
130 #define LZO_E_INPUT_OVERRUN (-4)
131 #define LZO_E_OUTPUT_OVERRUN (-5)
132 #define LZO_E_LOOKBEHIND_OVERRUN (-6)
133 #define LZO_E_EOF_NOT_FOUND (-7)
134 #define LZO_E_INPUT_NOT_CONSUMED (-8)
136 #define PTR(a) ((lzo_ptr_t) (a))
137 #define PTR_LINEAR(a) PTR(a)
138 #define PTR_ALIGNED_4(a) ((PTR_LINEAR(a) & 3) == 0)
139 #define PTR_ALIGNED_8(a) ((PTR_LINEAR(a) & 7) == 0)
140 #define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
141 #define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
142 #define PTR_LT(a,b) (PTR(a) < PTR(b))
143 #define PTR_GE(a,b) (PTR(a) >= PTR(b))
144 #define PTR_DIFF(a,b) ((lzo_ptrdiff_t) (PTR(a) - PTR(b)))
145 #define pd(a,b) ((lzo_uint) ((a)-(b)))
147 typedef ptrdiff_t lzo_ptrdiff_t;
150 lzo1x_decompress (const lzo_byte * in, lzo_uint in_len,
151 lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
153 register lzo_byte *op;
154 register const lzo_byte *ip;
157 register const lzo_byte *m_pos;
159 const lzo_byte *const ip_end = in + in_len;
160 lzo_byte *const op_end = out + *out_len;
177 goto first_literal_run;
180 while (TEST_IP && TEST_OP)
198 if (PTR_ALIGNED2_4 (op, ip))
242 m_pos = op - (1 + M2_MAX_OFFSET);
245 TEST_LOOKBEHIND (m_pos, out);
253 while (TEST_IP && TEST_OP)
259 m_pos -= (t >> 2) & 7;
262 TEST_LOOKBEHIND (m_pos, out);
283 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
290 m_pos -= (t & 8) << 11;
305 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
318 TEST_LOOKBEHIND (m_pos, out);
326 TEST_LOOKBEHIND (m_pos, out);
328 if (t >= 2 * 4 - (3 - 1)
329 && PTR_ALIGNED2_4 (op, m_pos))
375 return LZO_E_EOF_NOT_FOUND;
379 return (ip == ip_end ? LZO_E_OK :
381 ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
385 return LZO_E_INPUT_OVERRUN;
389 return LZO_E_OUTPUT_OVERRUN;
393 return LZO_E_LOOKBEHIND_OVERRUN;
396 int lzo_decompress(unsigned char *data_in, unsigned char *cpage_out,
397 u32 srclen, u32 destlen)
399 lzo_uint outlen = destlen;
400 return lzo1x_decompress (data_in, srclen, cpage_out, &outlen, NULL);