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.

285 lines
7.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. cnt += 255;
  63. cnt += mask + x;
  64. }
  65. return cnt;
  66. }
  67. /**
  68. * @brief Copies bytes from input to output buffer with checking.
  69. * @param cnt number of bytes to copy, must be >= 0
  70. */
  71. static inline void copy(LZOContext *c, int cnt)
  72. {
  73. register const uint8_t *src = c->in;
  74. register uint8_t *dst = c->out;
  75. if (cnt > c->in_end - src) {
  76. cnt = FFMAX(c->in_end - src, 0);
  77. c->error |= AV_LZO_INPUT_DEPLETED;
  78. }
  79. if (cnt > c->out_end - dst) {
  80. cnt = FFMAX(c->out_end - dst, 0);
  81. c->error |= AV_LZO_OUTPUT_FULL;
  82. }
  83. #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
  84. AV_COPY32U(dst, src);
  85. src += 4;
  86. dst += 4;
  87. cnt -= 4;
  88. if (cnt > 0)
  89. #endif
  90. memcpy(dst, src, cnt);
  91. c->in = src + cnt;
  92. c->out = dst + cnt;
  93. }
  94. static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
  95. /**
  96. * @brief Copies previously decoded bytes to current position.
  97. * @param back how many bytes back we start, must be > 0
  98. * @param cnt number of bytes to copy, must be >= 0
  99. *
  100. * cnt > back is valid, this will copy the bytes we just copied,
  101. * thus creating a repeating pattern with a period length of back.
  102. */
  103. static inline void copy_backptr(LZOContext *c, int back, int cnt)
  104. {
  105. register const uint8_t *src = &c->out[-back];
  106. register uint8_t *dst = c->out;
  107. if (src < c->out_start || src > dst) {
  108. c->error |= AV_LZO_INVALID_BACKPTR;
  109. return;
  110. }
  111. if (cnt > c->out_end - dst) {
  112. cnt = FFMAX(c->out_end - dst, 0);
  113. c->error |= AV_LZO_OUTPUT_FULL;
  114. }
  115. memcpy_backptr(dst, back, cnt);
  116. c->out = dst + cnt;
  117. }
  118. static inline void memcpy_backptr(uint8_t *dst, int back, int cnt)
  119. {
  120. const uint8_t *src = &dst[-back];
  121. if (back <= 1) {
  122. memset(dst, *src, cnt);
  123. } else {
  124. #ifdef OUTBUF_PADDED
  125. AV_COPY16U(dst, src);
  126. AV_COPY16U(dst + 2, src + 2);
  127. src += 4;
  128. dst += 4;
  129. cnt -= 4;
  130. if (cnt > 0) {
  131. AV_COPY16U(dst, src);
  132. AV_COPY16U(dst + 2, src + 2);
  133. AV_COPY16U(dst + 4, src + 4);
  134. AV_COPY16U(dst + 6, src + 6);
  135. src += 8;
  136. dst += 8;
  137. cnt -= 8;
  138. }
  139. #endif
  140. if (cnt > 0) {
  141. int blocklen = back;
  142. while (cnt > blocklen) {
  143. memcpy(dst, src, blocklen);
  144. dst += blocklen;
  145. cnt -= blocklen;
  146. blocklen <<= 1;
  147. }
  148. memcpy(dst, src, cnt);
  149. }
  150. }
  151. }
  152. void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
  153. {
  154. memcpy_backptr(dst, back, cnt);
  155. }
  156. int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
  157. {
  158. int state = 0;
  159. int x;
  160. LZOContext c;
  161. if (*outlen <= 0 || *inlen <= 0) {
  162. int res = 0;
  163. if (*outlen <= 0)
  164. res |= AV_LZO_OUTPUT_FULL;
  165. if (*inlen <= 0)
  166. res |= AV_LZO_INPUT_DEPLETED;
  167. return res;
  168. }
  169. c.in = in;
  170. c.in_end = (const uint8_t *)in + *inlen;
  171. c.out = c.out_start = out;
  172. c.out_end = (uint8_t *)out + *outlen;
  173. c.error = 0;
  174. x = GETB(c);
  175. if (x > 17) {
  176. copy(&c, x - 17);
  177. x = GETB(c);
  178. if (x < 16)
  179. c.error |= AV_LZO_ERROR;
  180. }
  181. if (c.in > c.in_end)
  182. c.error |= AV_LZO_INPUT_DEPLETED;
  183. while (!c.error) {
  184. int cnt, back;
  185. if (x > 15) {
  186. if (x > 63) {
  187. cnt = (x >> 5) - 1;
  188. back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
  189. } else if (x > 31) {
  190. cnt = get_len(&c, x, 31);
  191. x = GETB(c);
  192. back = (GETB(c) << 6) + (x >> 2) + 1;
  193. } else {
  194. cnt = get_len(&c, x, 7);
  195. back = (1 << 14) + ((x & 8) << 11);
  196. x = GETB(c);
  197. back += (GETB(c) << 6) + (x >> 2);
  198. if (back == (1 << 14)) {
  199. if (cnt != 1)
  200. c.error |= AV_LZO_ERROR;
  201. break;
  202. }
  203. }
  204. } else if (!state) {
  205. cnt = get_len(&c, x, 15);
  206. copy(&c, cnt + 3);
  207. x = GETB(c);
  208. if (x > 15)
  209. continue;
  210. cnt = 1;
  211. back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
  212. } else {
  213. cnt = 0;
  214. back = (GETB(c) << 2) + (x >> 2) + 1;
  215. }
  216. copy_backptr(&c, back, cnt + 2);
  217. state =
  218. cnt = x & 3;
  219. copy(&c, cnt);
  220. x = GETB(c);
  221. }
  222. *inlen = c.in_end - c.in;
  223. if (c.in > c.in_end)
  224. *inlen = 0;
  225. *outlen = c.out_end - c.out;
  226. return c.error;
  227. }
  228. #ifdef TEST
  229. #include <stdio.h>
  230. #include <lzo/lzo1x.h>
  231. #include "log.h"
  232. #define MAXSZ (10*1024*1024)
  233. /* Define one of these to 1 if you wish to benchmark liblzo
  234. * instead of our native implementation. */
  235. #define BENCHMARK_LIBLZO_SAFE 0
  236. #define BENCHMARK_LIBLZO_UNSAFE 0
  237. int main(int argc, char *argv[]) {
  238. FILE *in = fopen(argv[1], "rb");
  239. uint8_t *orig = av_malloc(MAXSZ + 16);
  240. uint8_t *comp = av_malloc(2*MAXSZ + 16);
  241. uint8_t *decomp = av_malloc(MAXSZ + 16);
  242. size_t s = fread(orig, 1, MAXSZ, in);
  243. lzo_uint clen = 0;
  244. long tmp[LZO1X_MEM_COMPRESS];
  245. int inlen, outlen;
  246. int i;
  247. av_log_set_level(AV_LOG_DEBUG);
  248. lzo1x_999_compress(orig, s, comp, &clen, tmp);
  249. for (i = 0; i < 300; i++) {
  250. START_TIMER
  251. inlen = clen; outlen = MAXSZ;
  252. #if BENCHMARK_LIBLZO_SAFE
  253. if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
  254. #elif BENCHMARK_LIBLZO_UNSAFE
  255. if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
  256. #else
  257. if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
  258. #endif
  259. av_log(NULL, AV_LOG_ERROR, "decompression error\n");
  260. STOP_TIMER("lzod")
  261. }
  262. if (memcmp(orig, decomp, s))
  263. av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
  264. else
  265. av_log(NULL, AV_LOG_ERROR, "decompression OK\n");
  266. return 0;
  267. }
  268. #endif