decompress_unlzo.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/decompress/unlzo.h>
  36. #endif
  37. #include <linux/types.h>
  38. #include <linux/lzo.h>
  39. #include <linux/decompress/mm.h>
  40. #include <linux/compiler.h>
  41. #include <asm/unaligned.h>
  42. static const unsigned char lzop_magic[] = {
  43. 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };
  44. #define LZO_BLOCK_SIZE (256*1024l)
  45. #define HEADER_HAS_FILTER 0x00000800L
  46. STATIC inline int INIT parse_header(u8 *input, u8 *skip)
  47. {
  48. int l;
  49. u8 *parse = input;
  50. u8 level = 0;
  51. u16 version;
  52. /* read magic: 9 first bits */
  53. for (l = 0; l < 9; l++) {
  54. if (*parse++ != lzop_magic[l])
  55. return 0;
  56. }
  57. /* get version (2bytes), skip library version (2),
  58. * 'need to be extracted' version (2) and
  59. * method (1) */
  60. version = get_unaligned_be16(parse);
  61. parse += 7;
  62. if (version >= 0x0940)
  63. level = *parse++;
  64. if (get_unaligned_be32(parse) & HEADER_HAS_FILTER)
  65. parse += 8; /* flags + filter info */
  66. else
  67. parse += 4; /* flags */
  68. /* skip mode and mtime_low */
  69. parse += 8;
  70. if (version >= 0x0940)
  71. parse += 4; /* skip mtime_high */
  72. l = *parse++;
  73. /* don't care about the file name, and skip checksum */
  74. parse += l + 4;
  75. *skip = parse - input;
  76. return 1;
  77. }
  78. STATIC inline int INIT unlzo(u8 *input, int in_len,
  79. int (*fill) (void *, unsigned int),
  80. int (*flush) (void *, unsigned int),
  81. u8 *output, int *posp,
  82. void (*error) (char *x))
  83. {
  84. u8 skip = 0, r = 0;
  85. u32 src_len, dst_len;
  86. size_t tmp;
  87. u8 *in_buf, *in_buf_save, *out_buf;
  88. int ret = -1;
  89. if (output) {
  90. out_buf = output;
  91. } else if (!flush) {
  92. error("NULL output pointer and no flush function provided");
  93. goto exit;
  94. } else {
  95. out_buf = malloc(LZO_BLOCK_SIZE);
  96. if (!out_buf) {
  97. error("Could not allocate output buffer");
  98. goto exit;
  99. }
  100. }
  101. if (input && fill) {
  102. error("Both input pointer and fill function provided, don't know what to do");
  103. goto exit_1;
  104. } else if (input) {
  105. in_buf = input;
  106. } else if (!fill || !posp) {
  107. error("NULL input pointer and missing position pointer or fill function");
  108. goto exit_1;
  109. } else {
  110. in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));
  111. if (!in_buf) {
  112. error("Could not allocate input buffer");
  113. goto exit_1;
  114. }
  115. }
  116. in_buf_save = in_buf;
  117. if (posp)
  118. *posp = 0;
  119. if (fill)
  120. fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
  121. if (!parse_header(input, &skip)) {
  122. error("invalid header");
  123. goto exit_2;
  124. }
  125. in_buf += skip;
  126. if (posp)
  127. *posp = skip;
  128. for (;;) {
  129. /* read uncompressed block size */
  130. dst_len = get_unaligned_be32(in_buf);
  131. in_buf += 4;
  132. /* exit if last block */
  133. if (dst_len == 0) {
  134. if (posp)
  135. *posp += 4;
  136. break;
  137. }
  138. if (dst_len > LZO_BLOCK_SIZE) {
  139. error("dest len longer than block size");
  140. goto exit_2;
  141. }
  142. /* read compressed block size, and skip block checksum info */
  143. src_len = get_unaligned_be32(in_buf);
  144. in_buf += 8;
  145. if (src_len <= 0 || src_len > dst_len) {
  146. error("file corrupted");
  147. goto exit_2;
  148. }
  149. /* decompress */
  150. tmp = dst_len;
  151. /* When the input data is not compressed at all,
  152. * lzo1x_decompress_safe will fail, so call memcpy()
  153. * instead */
  154. if (unlikely(dst_len == src_len))
  155. memcpy(out_buf, in_buf, src_len);
  156. else {
  157. r = lzo1x_decompress_safe((u8 *) in_buf, src_len,
  158. out_buf, &tmp);
  159. if (r != LZO_E_OK || dst_len != tmp) {
  160. error("Compressed data violation");
  161. goto exit_2;
  162. }
  163. }
  164. if (flush && flush(out_buf, dst_len) != dst_len)
  165. goto exit_2;
  166. if (output)
  167. out_buf += dst_len;
  168. if (posp)
  169. *posp += src_len + 12;
  170. if (fill) {
  171. in_buf = in_buf_save;
  172. fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));
  173. } else
  174. in_buf += src_len;
  175. }
  176. ret = 0;
  177. exit_2:
  178. if (!input)
  179. free(in_buf);
  180. exit_1:
  181. if (!output)
  182. free(out_buf);
  183. exit:
  184. return ret;
  185. }
  186. #define decompress unlzo