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.

297 lines
8.1KB

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