lzo.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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))) {
  61. if (cnt >= INT_MAX - 1000) {
  62. c->error |= AV_LZO_ERROR;
  63. break;
  64. }
  65. cnt += 255;
  66. }
  67. cnt += mask + x;
  68. }
  69. return cnt;
  70. }
  71. //#define UNALIGNED_LOADSTORE
  72. #define BUILTIN_MEMCPY
  73. #ifdef UNALIGNED_LOADSTORE
  74. #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s);
  75. #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s);
  76. #elif defined(BUILTIN_MEMCPY)
  77. #define COPY2(d, s) memcpy(d, s, 2);
  78. #define COPY4(d, s) memcpy(d, s, 4);
  79. #else
  80. #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
  81. #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
  82. #endif
  83. /**
  84. * \brief Copies bytes from input to output buffer with checking.
  85. * \param cnt number of bytes to copy, must be >= 0
  86. */
  87. static inline void copy(LZOContext *c, int cnt) {
  88. register const uint8_t *src = c->in;
  89. register uint8_t *dst = c->out;
  90. if (cnt > c->in_end - src) {
  91. cnt = FFMAX(c->in_end - src, 0);
  92. c->error |= AV_LZO_INPUT_DEPLETED;
  93. }
  94. if (cnt > c->out_end - dst) {
  95. cnt = FFMAX(c->out_end - dst, 0);
  96. c->error |= AV_LZO_OUTPUT_FULL;
  97. }
  98. #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
  99. COPY4(dst, src);
  100. src += 4;
  101. dst += 4;
  102. cnt -= 4;
  103. if (cnt > 0)
  104. #endif
  105. memcpy(dst, src, cnt);
  106. c->in = src + cnt;
  107. c->out = dst + cnt;
  108. }
  109. static inline void memcpy_backptr(uint8_t *dst, int back, int cnt);
  110. /**
  111. * \brief Copies previously decoded bytes to current position.
  112. * \param back how many bytes back we start
  113. * \param cnt number of bytes to copy, must be >= 0
  114. *
  115. * cnt > back is valid, this will copy the bytes we just copied,
  116. * thus creating a repeating pattern with a period length of back.
  117. */
  118. static inline void copy_backptr(LZOContext *c, int back, int cnt)
  119. {
  120. register uint8_t *dst = c->out;
  121. if (dst - c->out_start < back) {
  122. c->error |= AV_LZO_INVALID_BACKPTR;
  123. return;
  124. }
  125. if (cnt > c->out_end - dst) {
  126. cnt = FFMAX(c->out_end - dst, 0);
  127. c->error |= AV_LZO_OUTPUT_FULL;
  128. }
  129. memcpy_backptr(dst, back, cnt);
  130. c->out = dst + cnt;
  131. }
  132. static inline void memcpy_backptr(uint8_t *dst, int back, int cnt) {
  133. const uint8_t *src = &dst[-back];
  134. if (back == 1) {
  135. memset(dst, *src, cnt);
  136. } else {
  137. #ifdef OUTBUF_PADDED
  138. COPY2(dst, src);
  139. COPY2(dst + 2, src + 2);
  140. src += 4;
  141. dst += 4;
  142. cnt -= 4;
  143. if (cnt > 0) {
  144. COPY2(dst, src);
  145. COPY2(dst + 2, src + 2);
  146. COPY2(dst + 4, src + 4);
  147. COPY2(dst + 6, src + 6);
  148. src += 8;
  149. dst += 8;
  150. cnt -= 8;
  151. }
  152. #endif
  153. if (cnt > 0) {
  154. int blocklen = back;
  155. while (cnt > blocklen) {
  156. memcpy(dst, src, blocklen);
  157. dst += blocklen;
  158. cnt -= blocklen;
  159. blocklen <<= 1;
  160. }
  161. memcpy(dst, src, cnt);
  162. }
  163. }
  164. }
  165. void av_memcpy_backptr(uint8_t *dst, int back, int cnt) {
  166. memcpy_backptr(dst, back, cnt);
  167. }
  168. int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
  169. int state= 0;
  170. int x;
  171. LZOContext c;
  172. c.in = in;
  173. c.in_end = (const uint8_t *)in + *inlen;
  174. c.out = c.out_start = out;
  175. c.out_end = (uint8_t *)out + * outlen;
  176. c.error = 0;
  177. x = GETB(c);
  178. if (x > 17) {
  179. copy(&c, x - 17);
  180. x = GETB(c);
  181. if (x < 16) c.error |= AV_LZO_ERROR;
  182. }
  183. if (c.in > c.in_end)
  184. c.error |= AV_LZO_INPUT_DEPLETED;
  185. while (!c.error) {
  186. int cnt, back;
  187. if (x > 15) {
  188. if (x > 63) {
  189. cnt = (x >> 5) - 1;
  190. back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
  191. } else if (x > 31) {
  192. cnt = get_len(&c, x, 31);
  193. x = GETB(c);
  194. back = (GETB(c) << 6) + (x >> 2) + 1;
  195. } else {
  196. cnt = get_len(&c, x, 7);
  197. back = (1 << 14) + ((x & 8) << 11);
  198. x = GETB(c);
  199. back += (GETB(c) << 6) + (x >> 2);
  200. if (back == (1 << 14)) {
  201. if (cnt != 1)
  202. c.error |= AV_LZO_ERROR;
  203. break;
  204. }
  205. }
  206. } else if(!state){
  207. cnt = get_len(&c, x, 15);
  208. copy(&c, cnt + 3);
  209. x = GETB(c);
  210. if (x > 15)
  211. continue;
  212. cnt = 1;
  213. back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
  214. } else {
  215. cnt = 0;
  216. back = (GETB(c) << 2) + (x >> 2) + 1;
  217. }
  218. copy_backptr(&c, back, cnt + 2);
  219. state=
  220. cnt = x & 3;
  221. copy(&c, cnt);
  222. x = GETB(c);
  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. #if LIBAVUTIL_VERSION_MAJOR < 50
  231. int lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
  232. return av_lzo1x_decode(out, outlen, in, inlen);
  233. }
  234. #endif
  235. #ifdef TEST
  236. #include <stdio.h>
  237. #include <lzo/lzo1x.h>
  238. #include "log.h"
  239. #define MAXSZ (10*1024*1024)
  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_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. #ifdef LIBLZO
  256. if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
  257. #elif defined(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