You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.4KB

  1. /*
  2. * LZO 1x decompression
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "common.h"
  20. //! avoid e.g. MPlayers fast_memcpy, it slows things down here
  21. #undef memcpy
  22. #include <string.h>
  23. #include "lzo.h"
  24. //! define if we may write up to 12 bytes beyond the output buffer
  25. #define OUTBUF_PADDED 1
  26. //! define if we may read up to 4 bytes beyond the input buffer
  27. #define INBUF_PADDED 1
  28. typedef struct LZOContext {
  29. uint8_t *in, *in_end;
  30. uint8_t *out_start, *out, *out_end;
  31. int error;
  32. } LZOContext;
  33. /**
  34. * \brief read one byte from input buffer, avoiding overrun
  35. * \return byte read
  36. */
  37. static inline int get_byte(LZOContext *c) {
  38. if (c->in < c->in_end)
  39. return *c->in++;
  40. c->error |= LZO_INPUT_DEPLETED;
  41. return 1;
  42. }
  43. /**
  44. * \brief decode a length value in the coding used by lzo
  45. * \param x previous byte value
  46. * \param mask bits used from x
  47. * \return decoded length value
  48. */
  49. static inline int get_len(LZOContext *c, int x, int mask) {
  50. int cnt = x & mask;
  51. if (!cnt) {
  52. while (!(x = get_byte(c))) cnt += 255;
  53. cnt += mask + x;
  54. }
  55. return cnt;
  56. }
  57. /**
  58. * \brief copy bytes from input to output buffer with checking
  59. * \param cnt number of bytes to copy, must be > 0
  60. */
  61. static inline void copy(LZOContext *c, int cnt) {
  62. register uint8_t *src = c->in;
  63. register uint8_t *dst = c->out;
  64. if (src + cnt > c->in_end) {
  65. cnt = c->in_end - src;
  66. c->error |= LZO_INPUT_DEPLETED;
  67. }
  68. if (dst + cnt > c->out_end) {
  69. cnt = c->out_end - dst;
  70. c->error |= LZO_OUTPUT_FULL;
  71. }
  72. #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
  73. dst[0] = src[0];
  74. dst[1] = src[1];
  75. dst[2] = src[2];
  76. dst[3] = src[3];
  77. src += 4;
  78. dst += 4;
  79. cnt -= 4;
  80. if (cnt > 0)
  81. #endif
  82. memcpy(dst, src, cnt);
  83. c->in = src + cnt;
  84. c->out = dst + cnt;
  85. }
  86. /**
  87. * \brief copy previously decoded bytes to current position
  88. * \param back how many bytes back we start
  89. * \param cnt number of bytes to copy, must be > 0
  90. *
  91. * cnt > back is valid, this will copy the bytes we just copied,
  92. * thus creating a repeating pattern with a period length of back.
  93. */
  94. static inline void copy_backptr(LZOContext *c, int back, int cnt) {
  95. register uint8_t *src = &c->out[-back];
  96. register uint8_t *dst = c->out;
  97. if (src < c->out_start) {
  98. c->error |= LZO_INVALID_BACKPTR;
  99. return;
  100. }
  101. if (dst + cnt > c->out_end) {
  102. cnt = c->out_end - dst;
  103. c->error |= LZO_OUTPUT_FULL;
  104. }
  105. if (back == 1) {
  106. memset(dst, *src, cnt);
  107. dst += cnt;
  108. } else {
  109. #ifdef OUTBUF_PADDED
  110. dst[0] = src[0];
  111. dst[1] = src[1];
  112. dst[2] = src[2];
  113. dst[3] = src[3];
  114. src += 4;
  115. dst += 4;
  116. cnt -= 4;
  117. if (cnt > 0) {
  118. dst[0] = src[0];
  119. dst[1] = src[1];
  120. dst[2] = src[2];
  121. dst[3] = src[3];
  122. dst[4] = src[4];
  123. dst[5] = src[5];
  124. dst[6] = src[6];
  125. dst[7] = src[7];
  126. src += 8;
  127. dst += 8;
  128. cnt -= 8;
  129. }
  130. #endif
  131. if (cnt > 0) {
  132. int blocklen = back;
  133. while (cnt > blocklen) {
  134. memcpy(dst, src, blocklen);
  135. dst += blocklen;
  136. cnt -= blocklen;
  137. blocklen <<= 1;
  138. }
  139. memcpy(dst, src, cnt);
  140. }
  141. dst += cnt;
  142. }
  143. c->out = dst;
  144. }
  145. /**
  146. * \brief decode LZO 1x compressed data
  147. * \param out output buffer
  148. * \param outlen size of output buffer, number of bytes left are returned here
  149. * \param in input buffer
  150. * \param inlen size of input buffer, number of bytes left are returned here
  151. * \return 0 on success, otherwise error flags, see lzo.h
  152. *
  153. * make sure all buffers are appropriately padded, in must provide
  154. * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
  155. */
  156. int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) {
  157. enum {COPY, BACKPTR} state = COPY;
  158. int x;
  159. LZOContext c;
  160. c.in = in;
  161. c.in_end = in + *inlen;
  162. c.out = c.out_start = out;
  163. c.out_end = out + * outlen;
  164. c.error = 0;
  165. x = get_byte(&c);
  166. if (x > 17) {
  167. copy(&c, x - 17);
  168. x = get_byte(&c);
  169. if (x < 16) c.error |= LZO_ERROR;
  170. }
  171. while (!c.error) {
  172. int cnt, back;
  173. if (x >> 4) {
  174. if (x >> 6) {
  175. cnt = (x >> 5) - 1;
  176. back = (get_byte(&c) << 3) + ((x >> 2) & 7) + 1;
  177. } else if (x >> 5) {
  178. cnt = get_len(&c, x, 31);
  179. x = get_byte(&c);
  180. back = (get_byte(&c) << 6) + (x >> 2) + 1;
  181. } else {
  182. cnt = get_len(&c, x, 7);
  183. back = (1 << 14) + ((x & 8) << 11);
  184. x = get_byte(&c);
  185. back += (get_byte(&c) << 6) + (x >> 2);
  186. if (back == (1 << 14)) {
  187. if (cnt != 1)
  188. c.error |= LZO_ERROR;
  189. break;
  190. }
  191. }
  192. } else
  193. switch (state) {
  194. case COPY:
  195. cnt = get_len(&c, x, 15);
  196. copy(&c, cnt + 3);
  197. x = get_byte(&c);
  198. if (x >> 4)
  199. continue;
  200. cnt = 1;
  201. back = (1 << 11) + (get_byte(&c) << 2) + (x >> 2) + 1;
  202. break;
  203. case BACKPTR:
  204. cnt = 0;
  205. back = (get_byte(&c) << 2) + (x >> 2) + 1;
  206. break;
  207. }
  208. copy_backptr(&c, back, cnt + 2);
  209. cnt = x & 3;
  210. state = cnt ? BACKPTR : COPY;
  211. if (cnt)
  212. copy(&c, cnt);
  213. x = get_byte(&c);
  214. }
  215. *inlen = c.in_end - c.in;
  216. *outlen = c.out_end - c.out;
  217. return c.error;
  218. }