compr_lzo.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2004 Patrik Kluba,
  5. * University of Szeged, Hungary
  6. *
  7. * For licensing information, see the file 'LICENCE' in the
  8. * jffs2 directory.
  9. *
  10. * $Id: compr_lzo.c,v 1.3 2004/06/23 16:34:39 havasi Exp $
  11. *
  12. */
  13. /*
  14. LZO1X-1 (and -999) compression module for jffs2
  15. based on the original LZO sources
  16. */
  17. /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
  18. /*
  19. Original copyright notice follows:
  20. lzo1x_9x.c -- implementation of the LZO1X-999 compression algorithm
  21. lzo_ptr.h -- low-level pointer constructs
  22. lzo_swd.ch -- sliding window dictionary
  23. lzoconf.h -- configuration for the LZO real-time data compression library
  24. lzo_mchw.ch -- matching functions using a window
  25. minilzo.c -- mini subset of the LZO real-time data compression library
  26. config1x.h -- configuration for the LZO1X algorithm
  27. lzo1x.h -- public interface of the LZO1X compression algorithm
  28. These files are part of the LZO real-time data compression library.
  29. Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
  30. All Rights Reserved.
  31. The LZO library is free software; you can redistribute it and/or
  32. modify it under the terms of the GNU General Public License as
  33. published by the Free Software Foundation; either version 2 of
  34. the License, or (at your option) any later version.
  35. The LZO library is distributed in the hope that it will be useful,
  36. but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. GNU General Public License for more details.
  39. You should have received a copy of the GNU General Public License
  40. along with the LZO library; see the file COPYING.
  41. If not, write to the Free Software Foundation, Inc.,
  42. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  43. Markus F.X.J. Oberhumer
  44. <markus@oberhumer.com>
  45. */
  46. /*
  47. 2004-02-16 pajko <pajko(AT)halom(DOT)u-szeged(DOT)hu>
  48. Initial release
  49. -removed all 16 bit code
  50. -all sensitive data will be on 4 byte boundary
  51. -removed check parts for library use
  52. -removed all but LZO1X-* compression
  53. */
  54. #include <config.h>
  55. #if ((CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_LZO_LZARI))
  56. #include <linux/stddef.h>
  57. #include <jffs2/jffs2.h>
  58. #include <jffs2/compr_rubin.h>
  59. /* Integral types that have *exactly* the same number of bits as a lzo_voidp */
  60. typedef unsigned long lzo_ptr_t;
  61. typedef long lzo_sptr_t;
  62. /* data type definitions */
  63. #define U32 unsigned long
  64. #define S32 signed long
  65. #define I32 long
  66. #define U16 unsigned short
  67. #define S16 signed short
  68. #define I16 short
  69. #define U8 unsigned char
  70. #define S8 signed char
  71. #define I8 char
  72. #define M1_MAX_OFFSET 0x0400
  73. #define M2_MAX_OFFSET 0x0800
  74. #define M3_MAX_OFFSET 0x4000
  75. #define M4_MAX_OFFSET 0xbfff
  76. #define __COPY4(dst,src) * (lzo_uint32p)(dst) = * (const lzo_uint32p)(src)
  77. #define COPY4(dst,src) __COPY4((lzo_ptr_t)(dst),(lzo_ptr_t)(src))
  78. #define TEST_IP (ip < ip_end)
  79. #define TEST_OP (op <= op_end)
  80. #define NEED_IP(x) \
  81. if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x)) goto input_overrun
  82. #define NEED_OP(x) \
  83. if ((lzo_uint)(op_end - op) < (lzo_uint)(x)) goto output_overrun
  84. #define TEST_LOOKBEHIND(m_pos,out) if (m_pos < out) goto lookbehind_overrun
  85. typedef U32 lzo_uint32;
  86. typedef I32 lzo_int32;
  87. typedef U32 lzo_uint;
  88. typedef I32 lzo_int;
  89. typedef int lzo_bool;
  90. #define lzo_byte U8
  91. #define lzo_bytep U8 *
  92. #define lzo_charp char *
  93. #define lzo_voidp void *
  94. #define lzo_shortp short *
  95. #define lzo_ushortp unsigned short *
  96. #define lzo_uint32p lzo_uint32 *
  97. #define lzo_int32p lzo_int32 *
  98. #define lzo_uintp lzo_uint *
  99. #define lzo_intp lzo_int *
  100. #define lzo_voidpp lzo_voidp *
  101. #define lzo_bytepp lzo_bytep *
  102. #define lzo_sizeof_dict_t sizeof(lzo_bytep)
  103. #define LZO_E_OK 0
  104. #define LZO_E_ERROR (-1)
  105. #define LZO_E_OUT_OF_MEMORY (-2) /* not used right now */
  106. #define LZO_E_NOT_COMPRESSIBLE (-3) /* not used right now */
  107. #define LZO_E_INPUT_OVERRUN (-4)
  108. #define LZO_E_OUTPUT_OVERRUN (-5)
  109. #define LZO_E_LOOKBEHIND_OVERRUN (-6)
  110. #define LZO_E_EOF_NOT_FOUND (-7)
  111. #define LZO_E_INPUT_NOT_CONSUMED (-8)
  112. #define PTR(a) ((lzo_ptr_t) (a))
  113. #define PTR_LINEAR(a) PTR(a)
  114. #define PTR_ALIGNED_4(a) ((PTR_LINEAR(a) & 3) == 0)
  115. #define PTR_ALIGNED_8(a) ((PTR_LINEAR(a) & 7) == 0)
  116. #define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
  117. #define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
  118. #define PTR_LT(a,b) (PTR(a) < PTR(b))
  119. #define PTR_GE(a,b) (PTR(a) >= PTR(b))
  120. #define PTR_DIFF(a,b) ((lzo_ptrdiff_t) (PTR(a) - PTR(b)))
  121. #define pd(a,b) ((lzo_uint) ((a)-(b)))
  122. typedef ptrdiff_t lzo_ptrdiff_t;
  123. static int
  124. lzo1x_decompress (const lzo_byte * in, lzo_uint in_len,
  125. lzo_byte * out, lzo_uintp out_len, lzo_voidp wrkmem)
  126. {
  127. register lzo_byte *op;
  128. register const lzo_byte *ip;
  129. register lzo_uint t;
  130. register const lzo_byte *m_pos;
  131. const lzo_byte *const ip_end = in + in_len;
  132. lzo_byte *const op_end = out + *out_len;
  133. *out_len = 0;
  134. op = out;
  135. ip = in;
  136. if (*ip > 17)
  137. {
  138. t = *ip++ - 17;
  139. if (t < 4)
  140. goto match_next;
  141. NEED_OP (t);
  142. NEED_IP (t + 1);
  143. do
  144. *op++ = *ip++;
  145. while (--t > 0);
  146. goto first_literal_run;
  147. }
  148. while (TEST_IP && TEST_OP)
  149. {
  150. t = *ip++;
  151. if (t >= 16)
  152. goto match;
  153. if (t == 0)
  154. {
  155. NEED_IP (1);
  156. while (*ip == 0)
  157. {
  158. t += 255;
  159. ip++;
  160. NEED_IP (1);
  161. }
  162. t += 15 + *ip++;
  163. }
  164. NEED_OP (t + 3);
  165. NEED_IP (t + 4);
  166. if (PTR_ALIGNED2_4 (op, ip))
  167. {
  168. COPY4 (op, ip);
  169. op += 4;
  170. ip += 4;
  171. if (--t > 0)
  172. {
  173. if (t >= 4)
  174. {
  175. do
  176. {
  177. COPY4 (op, ip);
  178. op += 4;
  179. ip += 4;
  180. t -= 4;
  181. }
  182. while (t >= 4);
  183. if (t > 0)
  184. do
  185. *op++ = *ip++;
  186. while (--t > 0);
  187. }
  188. else
  189. do
  190. *op++ = *ip++;
  191. while (--t > 0);
  192. }
  193. }
  194. else
  195. {
  196. *op++ = *ip++;
  197. *op++ = *ip++;
  198. *op++ = *ip++;
  199. do
  200. *op++ = *ip++;
  201. while (--t > 0);
  202. }
  203. first_literal_run:
  204. t = *ip++;
  205. if (t >= 16)
  206. goto match;
  207. m_pos = op - (1 + M2_MAX_OFFSET);
  208. m_pos -= t >> 2;
  209. m_pos -= *ip++ << 2;
  210. TEST_LOOKBEHIND (m_pos, out);
  211. NEED_OP (3);
  212. *op++ = *m_pos++;
  213. *op++ = *m_pos++;
  214. *op++ = *m_pos;
  215. goto match_done;
  216. while (TEST_IP && TEST_OP)
  217. {
  218. match:
  219. if (t >= 64)
  220. {
  221. m_pos = op - 1;
  222. m_pos -= (t >> 2) & 7;
  223. m_pos -= *ip++ << 3;
  224. t = (t >> 5) - 1;
  225. TEST_LOOKBEHIND (m_pos, out);
  226. NEED_OP (t + 3 - 1);
  227. goto copy_match;
  228. }
  229. else if (t >= 32)
  230. {
  231. t &= 31;
  232. if (t == 0)
  233. {
  234. NEED_IP (1);
  235. while (*ip == 0)
  236. {
  237. t += 255;
  238. ip++;
  239. NEED_IP (1);
  240. }
  241. t += 31 + *ip++;
  242. }
  243. m_pos = op - 1;
  244. m_pos -= (ip[0] >> 2) + (ip[1] << 6);
  245. ip += 2;
  246. }
  247. else if (t >= 16)
  248. {
  249. m_pos = op;
  250. m_pos -= (t & 8) << 11;
  251. t &= 7;
  252. if (t == 0)
  253. {
  254. NEED_IP (1);
  255. while (*ip == 0)
  256. {
  257. t += 255;
  258. ip++;
  259. NEED_IP (1);
  260. }
  261. t += 7 + *ip++;
  262. }
  263. m_pos -= (ip[0] >> 2) + (ip[1] << 6);
  264. ip += 2;
  265. if (m_pos == op)
  266. goto eof_found;
  267. m_pos -= 0x4000;
  268. }
  269. else
  270. {
  271. m_pos = op - 1;
  272. m_pos -= t >> 2;
  273. m_pos -= *ip++ << 2;
  274. TEST_LOOKBEHIND (m_pos, out);
  275. NEED_OP (2);
  276. *op++ = *m_pos++;
  277. *op++ = *m_pos;
  278. goto match_done;
  279. }
  280. TEST_LOOKBEHIND (m_pos, out);
  281. NEED_OP (t + 3 - 1);
  282. if (t >= 2 * 4 - (3 - 1)
  283. && PTR_ALIGNED2_4 (op, m_pos))
  284. {
  285. COPY4 (op, m_pos);
  286. op += 4;
  287. m_pos += 4;
  288. t -= 4 - (3 - 1);
  289. do
  290. {
  291. COPY4 (op, m_pos);
  292. op += 4;
  293. m_pos += 4;
  294. t -= 4;
  295. }
  296. while (t >= 4);
  297. if (t > 0)
  298. do
  299. *op++ = *m_pos++;
  300. while (--t > 0);
  301. }
  302. else
  303. {
  304. copy_match:
  305. *op++ = *m_pos++;
  306. *op++ = *m_pos++;
  307. do
  308. *op++ = *m_pos++;
  309. while (--t > 0);
  310. }
  311. match_done:
  312. t = ip[-2] & 3;
  313. if (t == 0)
  314. break;
  315. match_next:
  316. NEED_OP (t);
  317. NEED_IP (t + 1);
  318. do
  319. *op++ = *ip++;
  320. while (--t > 0);
  321. t = *ip++;
  322. }
  323. }
  324. *out_len = op - out;
  325. return LZO_E_EOF_NOT_FOUND;
  326. eof_found:
  327. *out_len = op - out;
  328. return (ip == ip_end ? LZO_E_OK :
  329. (ip <
  330. ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
  331. input_overrun:
  332. *out_len = op - out;
  333. return LZO_E_INPUT_OVERRUN;
  334. output_overrun:
  335. *out_len = op - out;
  336. return LZO_E_OUTPUT_OVERRUN;
  337. lookbehind_overrun:
  338. *out_len = op - out;
  339. return LZO_E_LOOKBEHIND_OVERRUN;
  340. }
  341. int lzo_decompress(unsigned char *data_in, unsigned char *cpage_out,
  342. u32 srclen, u32 destlen)
  343. {
  344. lzo_uint outlen = destlen;
  345. return lzo1x_decompress (data_in, srclen, cpage_out, &outlen, NULL);
  346. }
  347. #endif /* ((CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_LZO_LZARI)) */