lzo.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "avassert.h"
  24. #include "common.h"
  25. #include "intreadwrite.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. {
  42. if (c->in < c->in_end)
  43. return *c->in++;
  44. c->error |= AV_LZO_INPUT_DEPLETED;
  45. return 1;
  46. }
  47. #ifdef INBUF_PADDED
  48. #define GETB(c) (*(c).in++)
  49. #else
  50. #define GETB(c) get_byte(&(c))
  51. #endif
  52. /**
  53. * @brief Decodes a length value in the coding used by lzo.
  54. * @param x previous byte value
  55. * @param mask bits used from x
  56. * @return decoded length value
  57. */
  58. static inline int get_len(LZOContext *c, int x, int mask)
  59. {
  60. int cnt = x & mask;
  61. if (!cnt) {
  62. while (!(x = get_byte(c))) {
  63. if (cnt >= INT_MAX - 1000) {
  64. c->error |= AV_LZO_ERROR;
  65. break;
  66. }
  67. cnt += 255;
  68. }
  69. cnt += mask + x;
  70. }
  71. return cnt;
  72. }
  73. /**
  74. * @brief Copies bytes from input to output buffer with checking.
  75. * @param cnt number of bytes to copy, must be >= 0
  76. */
  77. static inline void copy(LZOContext *c, int cnt)
  78. {
  79. register const uint8_t *src = c->in;
  80. register uint8_t *dst = c->out;
  81. av_assert0(cnt >= 0);
  82. if (cnt > c->in_end - src) {
  83. cnt = FFMAX(c->in_end - src, 0);
  84. c->error |= AV_LZO_INPUT_DEPLETED;
  85. }
  86. if (cnt > c->out_end - dst) {
  87. cnt = FFMAX(c->out_end - dst, 0);
  88. c->error |= AV_LZO_OUTPUT_FULL;
  89. }
  90. #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
  91. AV_COPY32U(dst, src);
  92. src += 4;
  93. dst += 4;
  94. cnt -= 4;
  95. if (cnt > 0)
  96. #endif
  97. memcpy(dst, src, cnt);
  98. c->in = src + cnt;
  99. c->out = dst + cnt;
  100. }
  101. /**
  102. * @brief Copies previously decoded bytes to current position.
  103. * @param back how many bytes back we start, must be > 0
  104. * @param cnt number of bytes to copy, must be > 0
  105. *
  106. * cnt > back is valid, this will copy the bytes we just copied,
  107. * thus creating a repeating pattern with a period length of back.
  108. */
  109. static inline void copy_backptr(LZOContext *c, int back, int cnt)
  110. {
  111. register uint8_t *dst = c->out;
  112. av_assert0(cnt > 0);
  113. if (dst - c->out_start < back) {
  114. c->error |= AV_LZO_INVALID_BACKPTR;
  115. return;
  116. }
  117. if (cnt > c->out_end - dst) {
  118. cnt = FFMAX(c->out_end - dst, 0);
  119. c->error |= AV_LZO_OUTPUT_FULL;
  120. }
  121. av_memcpy_backptr(dst, back, cnt);
  122. c->out = dst + cnt;
  123. }
  124. int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
  125. {
  126. int state = 0;
  127. int x;
  128. LZOContext c;
  129. if (*outlen <= 0 || *inlen <= 0) {
  130. int res = 0;
  131. if (*outlen <= 0)
  132. res |= AV_LZO_OUTPUT_FULL;
  133. if (*inlen <= 0)
  134. res |= AV_LZO_INPUT_DEPLETED;
  135. return res;
  136. }
  137. c.in = in;
  138. c.in_end = (const uint8_t *)in + *inlen;
  139. c.out = c.out_start = out;
  140. c.out_end = (uint8_t *)out + *outlen;
  141. c.error = 0;
  142. x = GETB(c);
  143. if (x > 17) {
  144. copy(&c, x - 17);
  145. x = GETB(c);
  146. if (x < 16)
  147. c.error |= AV_LZO_ERROR;
  148. }
  149. if (c.in > c.in_end)
  150. c.error |= AV_LZO_INPUT_DEPLETED;
  151. while (!c.error) {
  152. int cnt, back;
  153. if (x > 15) {
  154. if (x > 63) {
  155. cnt = (x >> 5) - 1;
  156. back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
  157. } else if (x > 31) {
  158. cnt = get_len(&c, x, 31);
  159. x = GETB(c);
  160. back = (GETB(c) << 6) + (x >> 2) + 1;
  161. } else {
  162. cnt = get_len(&c, x, 7);
  163. back = (1 << 14) + ((x & 8) << 11);
  164. x = GETB(c);
  165. back += (GETB(c) << 6) + (x >> 2);
  166. if (back == (1 << 14)) {
  167. if (cnt != 1)
  168. c.error |= AV_LZO_ERROR;
  169. break;
  170. }
  171. }
  172. } else if (!state) {
  173. cnt = get_len(&c, x, 15);
  174. copy(&c, cnt + 3);
  175. x = GETB(c);
  176. if (x > 15)
  177. continue;
  178. cnt = 1;
  179. back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
  180. } else {
  181. cnt = 0;
  182. back = (GETB(c) << 2) + (x >> 2) + 1;
  183. }
  184. copy_backptr(&c, back, cnt + 2);
  185. state =
  186. cnt = x & 3;
  187. copy(&c, cnt);
  188. x = GETB(c);
  189. }
  190. *inlen = c.in_end - c.in;
  191. if (c.in > c.in_end)
  192. *inlen = 0;
  193. *outlen = c.out_end - c.out;
  194. return c.error;
  195. }