decompress_unlzo.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * LZO decompressor for the Linux kernel. Code borrowed from the lzo
  3. * implementation by Markus Franz Xaver Johannes Oberhumer.
  4. *
  5. * Linux kernel adaptation:
  6. * Copyright (C) 2009
  7. * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
  8. *
  9. * Original code:
  10. * Copyright (C) 1996-2005 Markus Franz Xaver Johannes Oberhumer
  11. * All Rights Reserved.
  12. *
  13. * lzop and the LZO library are free software; you can redistribute them
  14. * and/or modify them under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; see the file COPYING.
  25. * If not, write to the Free Software Foundation, Inc.,
  26. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. *
  28. * Markus F.X.J. Oberhumer
  29. * <markus@oberhumer.com>
  30. * http://www.oberhumer.com/opensource/lzop/
  31. */
  32. #ifdef STATIC
  33. #include "lzo/lzo1x_decompress.c"
  34. #else
  35. #include <linux/slab.h>
  36. #include <linux/decompress/unlzo.h>
  37. #endif
  38. #include <linux/types.h>
  39. #include <linux/lzo.h>
  40. #include <linux/decompress/mm.h>
  41. #include <linux/compiler.h>
  42. #include <asm/unaligned.h>
  43. static const unsigned char lzop_magic[] = {
  44. 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
  45. #define LZO_BLOCK_SIZE (256*1024l)
  46. #define HEADER_HAS_FILTER 0x00000800L
  47. STATIC inline int INIT parse_header(u8 *input, u8 *skip)
  48. {
  49. int l;
  50. u8 *parse = input;
  51. u8 level = 0;
  52. u16 version;
  53. /* read magic: 9 first bits */
  54. for (l = 0; l < 9; l++) {
  55. if (*parse++ != lzop_magic[l])
  56. return 0;
  57. }
  58. /* get version (2bytes), skip library version (2),
  59. * 'need to be extracted' version (2) and
  60. * method (1) */
  61. version = get_unaligned_be16(parse);
  62. parse += 7;
  63. if (version >= 0x0940)
  64. level = *parse++;
  65. if (get_unaligned_be32(parse) & HEADER_HAS_FILTER)
  66. parse += 8; /* flags + filter info */
  67. else
  68. parse += 4; /* flags */
  69. /* skip mode and mtime_low */
  70. parse += 8;
  71. if (version >= 0x0940)
  72. parse += 4; /* skip mtime_high */
  73. l = *parse++;
  74. /* don't care about the file name, and skip checksum */
  75. parse += l + 4;
  76. *skip = parse - input;
  77. return 1;
  78. }
  79. STATIC inline int INIT unlzo(u8 *input, int in_len,
  80. int (*fill) (void *, unsigned int),
  81. int (*flush) (void *, unsigned int),
  82. u8 *output, int *posp,
  83. void (*error_fn) (char *x))
  84. {
  85. u8 skip = 0, r = 0;
  86. u32 src_len, dst_len;
  87. size_t tmp;
  88. u8 *in_buf, *in_buf_save, *out_buf;
  89. int ret = -1;
  90. set_error_fn(error_fn);
  91. if (output) {
  92. out_buf = output;
  93. } else if (!flush) {
  94. error("NULL output pointer and no flush function provided");
  95. goto exit;
  96. } else {
  97. out_buf = malloc(LZO_BLOCK_SIZE);
  98. if (!out_buf) {
  99. error("Could not allocate output buffer");
  100. goto exit;
  101. }
  102. }
  103. if (input && fill) {
  104. error("Both input pointer and fill function provided, don't know what to do");
  105. goto exit_1;
  106. } else if (input) {
  107. in_buf = input;
  108. } else if (!fill || !posp) {
  109. error("NULL input pointer and missing position pointer or fill function");
  110. goto exit_1;
  111. } else {
  112. in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
  113. if (!in_buf) {
  114. error("Could not allocate input buffer");
  115. goto exit_1;
  116. }
  117. }
  118. in_buf_save = in_buf;
  119. if (posp)
  120. *posp = 0;
  121. if (fill)
  122. fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
  123. if (!parse_header(input, &skip)) {
  124. error("invalid header");
  125. goto exit_2;
  126. }
  127. in_buf += skip;
  128. if (posp)
  129. *posp = skip;
  130. for (;;) {
  131. /* read uncompressed block size */
  132. dst_len = get_unaligned_be32(in_buf);
  133. in_buf += 4;
  134. /* exit if last block */
  135. if (dst_len == 0) {
  136. if (posp)
  137. *posp += 4;
  138. break;
  139. }
  140. if (dst_len > LZO_BLOCK_SIZE) {
  141. error("dest len longer than block size");
  142. goto exit_2;
  143. }
  144. /* read compressed block size, and skip block checksum info */
  145. src_len = get_unaligned_be32(in_buf);
  146. in_buf += 8;
  147. if (src_len <= 0 || src_len > dst_len) {
  148. error("file corrupted");
  149. goto exit_2;
  150. }
  151. /* decompress */
  152. tmp = dst_len;
  153. /* When the input data is not compressed at all,
  154. * lzo1x_decompress_safe will fail, so call memcpy()
  155. * instead */
  156. if (unlikely(dst_len == src_len))
  157. memcpy(out_buf, in_buf, src_len);
  158. else {
  159. r = lzo1x_decompress_safe((u8 *) in_buf, src_len,
  160. out_buf, &tmp);
  161. if (r != LZO_E_OK || dst_len != tmp) {
  162. error("Compressed data violation");
  163. goto exit_2;
  164. }
  165. }
  166. if (flush)
  167. flush(out_buf, dst_len);
  168. if (output)
  169. out_buf += dst_len;
  170. if (posp)
  171. *posp += src_len + 12;
  172. if (fill) {
  173. in_buf = in_buf_save;
  174. fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
  175. } else
  176. in_buf += src_len;
  177. }
  178. ret = 0;
  179. exit_2:
  180. if (!input)
  181. free(in_buf);
  182. exit_1:
  183. if (!output)
  184. free(out_buf);
  185. exit:
  186. return ret;
  187. }
  188. #define decompress unlzo