lzo.c 8.4 KB

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