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.

259 lines
7.4KB

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