decompress_unlzo.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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) (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. if (output) {
  91. out_buf = output;
  92. } else if (!flush) {
  93. error("NULL output pointer and no flush function provided");
  94. goto exit;
  95. } else {
  96. out_buf = malloc(LZO_BLOCK_SIZE);
  97. if (!out_buf) {
  98. error("Could not allocate output buffer");
  99. goto exit;
  100. }
  101. }
  102. if (input && fill) {
  103. error("Both input pointer and fill function provided, don't know what to do");
  104. goto exit_1;
  105. } else if (input) {
  106. in_buf = input;
  107. } else if (!fill || !posp) {
  108. error("NULL input pointer and missing position pointer or fill function");
  109. goto exit_1;
  110. } else {
  111. in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
  112. if (!in_buf) {
  113. error("Could not allocate input buffer");
  114. goto exit_1;
  115. }
  116. }
  117. in_buf_save = in_buf;
  118. if (posp)
  119. *posp = 0;
  120. if (fill)
  121. fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
  122. if (!parse_header(input, &skip)) {
  123. error("invalid header");
  124. goto exit_2;
  125. }
  126. in_buf += skip;
  127. if (posp)
  128. *posp = skip;
  129. for (;;) {
  130. /* read uncompressed block size */
  131. dst_len = get_unaligned_be32(in_buf);
  132. in_buf += 4;
  133. /* exit if last block */
  134. if (dst_len == 0) {
  135. if (posp)
  136. *posp += 4;
  137. break;
  138. }
  139. if (dst_len > LZO_BLOCK_SIZE) {
  140. error("dest len longer than block size");
  141. goto exit_2;
  142. }
  143. /* read compressed block size, and skip block checksum info */
  144. src_len = get_unaligned_be32(in_buf);
  145. in_buf += 8;
  146. if (src_len <= 0 || src_len > dst_len) {
  147. error("file corrupted");
  148. goto exit_2;
  149. }
  150. /* decompress */
  151. tmp = dst_len;
  152. /* When the input data is not compressed at all,
  153. * lzo1x_decompress_safe will fail, so call memcpy()
  154. * instead */
  155. if (unlikely(dst_len == src_len))
  156. memcpy(out_buf, in_buf, src_len);
  157. else {
  158. r = lzo1x_decompress_safe((u8 *) in_buf, src_len,
  159. out_buf, &tmp);
  160. if (r != LZO_E_OK || dst_len != tmp) {
  161. error("Compressed data violation");
  162. goto exit_2;
  163. }
  164. }
  165. if (flush)
  166. flush(out_buf, dst_len);
  167. if (output)
  168. out_buf += dst_len;
  169. if (posp)
  170. *posp += src_len + 12;
  171. if (fill) {
  172. in_buf = in_buf_save;
  173. fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
  174. } else
  175. in_buf += src_len;
  176. }
  177. ret = 0;
  178. exit_2:
  179. if (!input)
  180. free(in_buf);
  181. exit_1:
  182. if (!output)
  183. free(out_buf);
  184. exit:
  185. return ret;
  186. }
  187. #define decompress unlzo