lzo.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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, must be > 0
  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. 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) c.error |= AV_LZO_ERROR;
  190. }
  191. if (c.in > c.in_end)
  192. c.error |= AV_LZO_INPUT_DEPLETED;
  193. while (!c.error) {
  194. int cnt, back;
  195. if (x > 15) {
  196. if (x > 63) {
  197. cnt = (x >> 5) - 1;
  198. back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
  199. } else if (x > 31) {
  200. cnt = get_len(&c, x, 31);
  201. x = GETB(c);
  202. back = (GETB(c) << 6) + (x >> 2) + 1;
  203. } else {
  204. cnt = get_len(&c, x, 7);
  205. back = (1 << 14) + ((x & 8) << 11);
  206. x = GETB(c);
  207. back += (GETB(c) << 6) + (x >> 2);
  208. if (back == (1 << 14)) {
  209. if (cnt != 1)
  210. c.error |= AV_LZO_ERROR;
  211. break;
  212. }
  213. }
  214. } else if(!state){
  215. cnt = get_len(&c, x, 15);
  216. copy(&c, cnt + 3);
  217. x = GETB(c);
  218. if (x > 15)
  219. continue;
  220. cnt = 1;
  221. back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
  222. } else {
  223. cnt = 0;
  224. back = (GETB(c) << 2) + (x >> 2) + 1;
  225. }
  226. copy_backptr(&c, back, cnt + 2);
  227. state=
  228. cnt = x & 3;
  229. copy(&c, cnt);
  230. x = GETB(c);
  231. }
  232. *inlen = c.in_end - c.in;
  233. if (c.in > c.in_end)
  234. *inlen = 0;
  235. *outlen = c.out_end - c.out;
  236. return c.error;
  237. }
  238. #ifdef TEST
  239. #include <stdio.h>
  240. #include <lzo/lzo1x.h>
  241. #include "log.h"
  242. #define MAXSZ (10*1024*1024)
  243. /* Define one of these to 1 if you wish to benchmark liblzo
  244. * instead of our native implementation. */
  245. #define BENCHMARK_LIBLZO_SAFE 0
  246. #define BENCHMARK_LIBLZO_UNSAFE 0
  247. int main(int argc, char *argv[]) {
  248. FILE *in = fopen(argv[1], "rb");
  249. uint8_t *orig = av_malloc(MAXSZ + 16);
  250. uint8_t *comp = av_malloc(2*MAXSZ + 16);
  251. uint8_t *decomp = av_malloc(MAXSZ + 16);
  252. size_t s = fread(orig, 1, MAXSZ, in);
  253. lzo_uint clen = 0;
  254. long tmp[LZO1X_MEM_COMPRESS];
  255. int inlen, outlen;
  256. int i;
  257. av_log_set_level(AV_LOG_DEBUG);
  258. lzo1x_999_compress(orig, s, comp, &clen, tmp);
  259. for (i = 0; i < 300; i++) {
  260. START_TIMER
  261. inlen = clen; outlen = MAXSZ;
  262. #if BENCHMARK_LIBLZO_SAFE
  263. if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
  264. #elif BENCHMARK_LIBLZO_UNSAFE
  265. if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
  266. #else
  267. if (av_lzo1x_decode(decomp, &outlen, comp, &inlen))
  268. #endif
  269. av_log(NULL, AV_LOG_ERROR, "decompression error\n");
  270. STOP_TIMER("lzod")
  271. }
  272. if (memcmp(orig, decomp, s))
  273. av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
  274. else
  275. av_log(NULL, AV_LOG_ERROR, "decompression OK\n");
  276. return 0;
  277. }
  278. #endif