lzo.c 8.1 KB

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