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.

269 lines
7.6KB

  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 8 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. #ifdef INBUF_PADDED
  46. #define GETB(c) (*(c).in++)
  47. #else
  48. #define GETB(c) get_byte(&(c))
  49. #endif
  50. /**
  51. * \brief decode a length value in the coding used by lzo
  52. * \param x previous byte value
  53. * \param mask bits used from x
  54. * \return decoded length value
  55. */
  56. static inline int get_len(LZOContext *c, int x, int mask) {
  57. int cnt = x & mask;
  58. if (!cnt) {
  59. while (!(x = get_byte(c))) cnt += 255;
  60. cnt += mask + x;
  61. }
  62. return cnt;
  63. }
  64. /**
  65. * \brief copy bytes from input to output buffer with checking
  66. * \param cnt number of bytes to copy, must be > 0
  67. */
  68. static inline void copy(LZOContext *c, int cnt) {
  69. register uint8_t *src = c->in;
  70. register uint8_t *dst = c->out;
  71. if (src + cnt > c->in_end || src + cnt < src) {
  72. cnt = c->in_end - src;
  73. c->error |= LZO_INPUT_DEPLETED;
  74. }
  75. if (dst + cnt > c->out_end || dst + cnt < dst) {
  76. cnt = c->out_end - dst;
  77. c->error |= LZO_OUTPUT_FULL;
  78. }
  79. #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
  80. dst[0] = src[0];
  81. dst[1] = src[1];
  82. dst[2] = src[2];
  83. dst[3] = src[3];
  84. src += 4;
  85. dst += 4;
  86. cnt -= 4;
  87. if (cnt > 0)
  88. #endif
  89. memcpy(dst, src, cnt);
  90. c->in = src + cnt;
  91. c->out = dst + cnt;
  92. }
  93. /**
  94. * \brief copy previously decoded bytes to current position
  95. * \param back how many bytes back we start
  96. * \param cnt number of bytes to copy, must be > 0
  97. *
  98. * cnt > back is valid, this will copy the bytes we just copied,
  99. * thus creating a repeating pattern with a period length of back.
  100. */
  101. static inline void copy_backptr(LZOContext *c, int back, int cnt) {
  102. register uint8_t *src = &c->out[-back];
  103. register uint8_t *dst = c->out;
  104. if (src < c->out_start || src > dst) {
  105. c->error |= LZO_INVALID_BACKPTR;
  106. return;
  107. }
  108. if (dst + cnt > c->out_end || dst + cnt < dst) {
  109. cnt = c->out_end - dst;
  110. c->error |= LZO_OUTPUT_FULL;
  111. }
  112. if (back == 1) {
  113. memset(dst, *src, cnt);
  114. dst += cnt;
  115. } else {
  116. #ifdef OUTBUF_PADDED
  117. dst[0] = src[0];
  118. dst[1] = src[1];
  119. dst[2] = src[2];
  120. dst[3] = src[3];
  121. src += 4;
  122. dst += 4;
  123. cnt -= 4;
  124. if (cnt > 0) {
  125. dst[0] = src[0];
  126. dst[1] = src[1];
  127. dst[2] = src[2];
  128. dst[3] = src[3];
  129. dst[4] = src[4];
  130. dst[5] = src[5];
  131. dst[6] = src[6];
  132. dst[7] = src[7];
  133. src += 8;
  134. dst += 8;
  135. cnt -= 8;
  136. }
  137. #endif
  138. if (cnt > 0) {
  139. int blocklen = back;
  140. while (cnt > blocklen) {
  141. memcpy(dst, src, blocklen);
  142. dst += blocklen;
  143. cnt -= blocklen;
  144. blocklen <<= 1;
  145. }
  146. memcpy(dst, src, cnt);
  147. }
  148. dst += cnt;
  149. }
  150. c->out = dst;
  151. }
  152. /**
  153. * \brief decode LZO 1x compressed data
  154. * \param out output buffer
  155. * \param outlen size of output buffer, number of bytes left are returned here
  156. * \param in input buffer
  157. * \param inlen size of input buffer, number of bytes left are returned here
  158. * \return 0 on success, otherwise error flags, see lzo.h
  159. *
  160. * make sure all buffers are appropriately padded, in must provide
  161. * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
  162. */
  163. int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) {
  164. enum {COPY, BACKPTR} state = COPY;
  165. int x;
  166. LZOContext c;
  167. c.in = in;
  168. c.in_end = (uint8_t *)in + *inlen;
  169. c.out = c.out_start = out;
  170. c.out_end = (uint8_t *)out + * outlen;
  171. c.error = 0;
  172. x = GETB(c);
  173. if (x > 17) {
  174. copy(&c, x - 17);
  175. x = GETB(c);
  176. if (x < 16) c.error |= LZO_ERROR;
  177. }
  178. while (!c.error) {
  179. int cnt, back;
  180. if (x >> 4) {
  181. if (x >> 6) {
  182. cnt = (x >> 5) - 1;
  183. back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
  184. } else if (x >> 5) {
  185. cnt = get_len(&c, x, 31);
  186. x = GETB(c);
  187. back = (GETB(c) << 6) + (x >> 2) + 1;
  188. } else {
  189. cnt = get_len(&c, x, 7);
  190. back = (1 << 14) + ((x & 8) << 11);
  191. x = GETB(c);
  192. back += (GETB(c) << 6) + (x >> 2);
  193. if (back == (1 << 14)) {
  194. if (cnt != 1)
  195. c.error |= LZO_ERROR;
  196. break;
  197. }
  198. }
  199. } else
  200. switch (state) {
  201. case COPY:
  202. cnt = get_len(&c, x, 15);
  203. copy(&c, cnt + 3);
  204. x = GETB(c);
  205. if (x >> 4)
  206. continue;
  207. cnt = 1;
  208. back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
  209. break;
  210. case BACKPTR:
  211. cnt = 0;
  212. back = (GETB(c) << 2) + (x >> 2) + 1;
  213. break;
  214. }
  215. copy_backptr(&c, back, cnt + 2);
  216. cnt = x & 3;
  217. state = cnt ? BACKPTR : COPY;
  218. if (cnt)
  219. copy(&c, cnt);
  220. x = GETB(c);
  221. if (c.in > c.in_end)
  222. c.error |= LZO_INPUT_DEPLETED;
  223. }
  224. *inlen = c.in_end - c.in;
  225. if (c.in > c.in_end)
  226. *inlen = 0;
  227. *outlen = c.out_end - c.out;
  228. return c.error;
  229. }
  230. #ifdef TEST
  231. #include <stdio.h>
  232. #include <lzo/lzo1x.h>
  233. #include "log.h"
  234. #define MAXSZ (10*1024*1024)
  235. int main(int argc, char *argv[]) {
  236. FILE *in = fopen(argv[1], "rb");
  237. uint8_t *orig = av_malloc(MAXSZ + 16);
  238. uint8_t *comp = av_malloc(2*MAXSZ + 16);
  239. uint8_t *decomp = av_malloc(MAXSZ + 16);
  240. size_t s = fread(orig, 1, MAXSZ, in);
  241. lzo_uint clen = 0;
  242. long tmp[LZO1X_MEM_COMPRESS];
  243. int inlen, outlen;
  244. int i;
  245. av_log_level = AV_LOG_DEBUG;
  246. lzo1x_999_compress(orig, s, comp, &clen, tmp);
  247. for (i = 0; i < 300; i++) {
  248. START_TIMER
  249. inlen = clen; outlen = MAXSZ;
  250. if (lzo1x_decode(decomp, &outlen, comp, &inlen))
  251. av_log(NULL, AV_LOG_ERROR, "decompression error\n");
  252. STOP_TIMER("lzod")
  253. }
  254. if (memcmp(orig, decomp, s))
  255. av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
  256. else
  257. av_log(NULL, AV_LOG_ERROR, "decompression ok\n");
  258. return 0;
  259. }
  260. #endif