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.

247 lines
6.8KB

  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 <string.h>
  22. #include "avutil.h"
  23. #include "common.h"
  24. #include "intreadwrite.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. const uint8_t *in, *in_end;
  32. uint8_t *out_start, *out, *out_end;
  33. int error;
  34. } LZOContext;
  35. /**
  36. * @brief Reads one byte from the input buffer, avoiding an overrun.
  37. * @return byte read
  38. */
  39. static inline int get_byte(LZOContext *c)
  40. {
  41. if (c->in < c->in_end)
  42. return *c->in++;
  43. c->error |= AV_LZO_INPUT_DEPLETED;
  44. return 1;
  45. }
  46. #ifdef INBUF_PADDED
  47. #define GETB(c) (*(c).in++)
  48. #else
  49. #define GETB(c) get_byte(&(c))
  50. #endif
  51. /**
  52. * @brief Decodes a length value in the coding used by lzo.
  53. * @param x previous byte value
  54. * @param mask bits used from x
  55. * @return decoded length value
  56. */
  57. static inline int get_len(LZOContext *c, int x, int mask)
  58. {
  59. int cnt = x & mask;
  60. if (!cnt) {
  61. while (!(x = get_byte(c))) {
  62. if (cnt >= INT_MAX - 1000) {
  63. c->error |= AV_LZO_ERROR;
  64. break;
  65. }
  66. cnt += 255;
  67. }
  68. cnt += mask + x;
  69. }
  70. return cnt;
  71. }
  72. /**
  73. * @brief Copies bytes from input to output buffer with checking.
  74. * @param cnt number of bytes to copy, must be >= 0
  75. */
  76. static inline void copy(LZOContext *c, int cnt)
  77. {
  78. register const uint8_t *src = c->in;
  79. register uint8_t *dst = c->out;
  80. if (cnt > c->in_end - src) {
  81. cnt = FFMAX(c->in_end - src, 0);
  82. c->error |= AV_LZO_INPUT_DEPLETED;
  83. }
  84. if (cnt > c->out_end - dst) {
  85. cnt = FFMAX(c->out_end - dst, 0);
  86. c->error |= AV_LZO_OUTPUT_FULL;
  87. }
  88. #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
  89. AV_COPY32U(dst, src);
  90. src += 4;
  91. dst += 4;
  92. cnt -= 4;
  93. if (cnt > 0)
  94. #endif
  95. memcpy(dst, src, cnt);
  96. c->in = src + cnt;
  97. c->out = dst + cnt;
  98. }
  99. /**
  100. * @brief Copies previously decoded bytes to current position.
  101. * @param back how many bytes back we start, must be > 0
  102. * @param cnt number of bytes to copy, must be >= 0
  103. *
  104. * cnt > back is valid, this will copy the bytes we just copied,
  105. * thus creating a repeating pattern with a period length of back.
  106. */
  107. static inline void copy_backptr(LZOContext *c, int back, int cnt)
  108. {
  109. register uint8_t *dst = c->out;
  110. if (dst - c->out_start < back) {
  111. c->error |= AV_LZO_INVALID_BACKPTR;
  112. return;
  113. }
  114. if (cnt > c->out_end - dst) {
  115. cnt = FFMAX(c->out_end - dst, 0);
  116. c->error |= AV_LZO_OUTPUT_FULL;
  117. }
  118. av_memcpy_backptr(dst, back, cnt);
  119. c->out = dst + cnt;
  120. }
  121. int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
  122. {
  123. int state = 0;
  124. int x;
  125. LZOContext c;
  126. if (*outlen <= 0 || *inlen <= 0) {
  127. int res = 0;
  128. if (*outlen <= 0)
  129. res |= AV_LZO_OUTPUT_FULL;
  130. if (*inlen <= 0)
  131. res |= AV_LZO_INPUT_DEPLETED;
  132. return res;
  133. }
  134. c.in = in;
  135. c.in_end = (const uint8_t *)in + *inlen;
  136. c.out = c.out_start = out;
  137. c.out_end = (uint8_t *)out + *outlen;
  138. c.error = 0;
  139. x = GETB(c);
  140. if (x > 17) {
  141. copy(&c, x - 17);
  142. x = GETB(c);
  143. if (x < 16)
  144. c.error |= AV_LZO_ERROR;
  145. }
  146. if (c.in > c.in_end)
  147. c.error |= AV_LZO_INPUT_DEPLETED;
  148. while (!c.error) {
  149. int cnt, back;
  150. if (x > 15) {
  151. if (x > 63) {
  152. cnt = (x >> 5) - 1;
  153. back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
  154. } else if (x > 31) {
  155. cnt = get_len(&c, x, 31);
  156. x = GETB(c);
  157. back = (GETB(c) << 6) + (x >> 2) + 1;
  158. } else {
  159. cnt = get_len(&c, x, 7);
  160. back = (1 << 14) + ((x & 8) << 11);
  161. x = GETB(c);
  162. back += (GETB(c) << 6) + (x >> 2);
  163. if (back == (1 << 14)) {
  164. if (cnt != 1)
  165. c.error |= AV_LZO_ERROR;
  166. break;
  167. }
  168. }
  169. } else if (!state) {
  170. cnt = get_len(&c, x, 15);
  171. copy(&c, cnt + 3);
  172. x = GETB(c);
  173. if (x > 15)
  174. continue;
  175. cnt = 1;
  176. back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
  177. } else {
  178. cnt = 0;
  179. back = (GETB(c) << 2) + (x >> 2) + 1;
  180. }
  181. copy_backptr(&c, back, cnt + 2);
  182. state =
  183. cnt = x & 3;
  184. copy(&c, cnt);
  185. x = GETB(c);
  186. }
  187. *inlen = c.in_end - c.in;
  188. if (c.in > c.in_end)
  189. *inlen = 0;
  190. *outlen = c.out_end - c.out;
  191. return c.error;
  192. }
  193. #ifdef TEST
  194. #include <stdio.h>
  195. #include <lzo/lzo1x.h>
  196. #include "log.h"
  197. #define MAXSZ (10*1024*1024)
  198. /* Define one of these to 1 if you wish to benchmark liblzo
  199. * instead of our native implementation. */
  200. #define BENCHMARK_LIBLZO_SAFE 0
  201. #define BENCHMARK_LIBLZO_UNSAFE 0
  202. int main(int argc, char *argv[]) {
  203. FILE *in = fopen(argv[1], "rb");
  204. uint8_t *orig = av_malloc(MAXSZ + 16);
  205. uint8_t *comp = av_malloc(2*MAXSZ + 16);
  206. uint8_t *decomp = av_malloc(MAXSZ + 16);
  207. size_t s = fread(orig, 1, MAXSZ, in);
  208. lzo_uint clen = 0;
  209. long tmp[LZO1X_MEM_COMPRESS];
  210. int inlen, outlen;
  211. int i;
  212. av_log_set_level(AV_LOG_DEBUG);
  213. lzo1x_999_compress(orig, s, comp, &clen, tmp);
  214. for (i = 0; i < 300; i++) {
  215. START_TIMER
  216. inlen = clen; outlen = MAXSZ;
  217. #if BENCHMARK_LIBLZO_SAFE
  218. if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
  219. #elif BENCHMARK_LIBLZO_UNSAFE
  220. if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
  221. #else
  222. if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
  223. #endif
  224. av_log(NULL, AV_LOG_ERROR, "decompression error\n");
  225. STOP_TIMER("lzod")
  226. }
  227. if (memcmp(orig, decomp, s))
  228. av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
  229. else
  230. av_log(NULL, AV_LOG_ERROR, "decompression OK\n");
  231. return 0;
  232. }
  233. #endif