You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.8KB

  1. /*
  2. * LZO 1x decompression
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "common.h"
  20. #include "lzo.h"
  21. typedef struct LZOContext {
  22. uint8_t *in, *in_end;
  23. uint8_t *out, *out_end;
  24. int out_size;
  25. int error;
  26. } LZOContext;
  27. /**
  28. * \brief read one byte from input buffer, avoiding overrun
  29. * \return byte read
  30. */
  31. static inline int get_byte(LZOContext *c) {
  32. if (c->in < c->in_end)
  33. return *c->in++;
  34. c->error |= LZO_INPUT_DEPLETED;
  35. return -1;
  36. }
  37. /**
  38. * \brief decode a length value in the coding used by lzo
  39. * \param x previous byte value
  40. * \param mask bits used from x
  41. * \return decoded length value
  42. */
  43. static inline int get_len(LZOContext *c, int x, int mask) {
  44. int cnt = x & mask;
  45. if (!cnt) {
  46. while (!(x = get_byte(c))) cnt += 255;
  47. cnt += mask + x;
  48. }
  49. return cnt;
  50. }
  51. /**
  52. * \brief copy bytes from input to output buffer with checking
  53. * \param cnt number of bytes to copy, must be > 0
  54. */
  55. static inline void copy(LZOContext *c, int cnt) {
  56. if (c->in + cnt > c->in_end) {
  57. cnt = c->in_end - c->in;
  58. c->error |= LZO_INPUT_DEPLETED;
  59. }
  60. if (c->out + cnt > c->out_end) {
  61. cnt = c->out_end - c->out;
  62. c->error |= LZO_OUTPUT_FULL;
  63. }
  64. do {
  65. *c->out++ = *c->in++;
  66. } while (--cnt);
  67. }
  68. /**
  69. * \brief copy previously decoded bytes to current position
  70. * \param back how many bytes back we start
  71. * \param cnt number of bytes to copy, must be > 0
  72. *
  73. * cnt > back is valid, this will copy the bytes we just copied.
  74. */
  75. static inline void copy_backptr(LZOContext *c, int back, int cnt) {
  76. if (c->out - back < c->out_end - c->out_size) {
  77. c->error |= LZO_INVALID_BACKPTR;
  78. return;
  79. }
  80. if (c->out + cnt > c->out_end) {
  81. cnt = c->out_end - c->out;
  82. c->error |= LZO_OUTPUT_FULL;
  83. }
  84. do {
  85. *c->out++ = c->out[-back];
  86. } while (--cnt);
  87. }
  88. /**
  89. * \brief decode LZO 1x compressed data
  90. * \param out output buffer
  91. * \param outlen size of output buffer, number of bytes left are returned here
  92. * \param in input buffer
  93. * \param inlen size of input buffer, number of bytes left are returned here
  94. * \return 0 on success, otherwise error flags, see lzo.h
  95. */
  96. int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) {
  97. enum {COPY, BACKPTR} state = COPY;
  98. int x;
  99. LZOContext c;
  100. c.in = in;
  101. c.in_end = in + *inlen;
  102. c.out = out;
  103. c.out_end = out + * outlen;
  104. c.out_size = *outlen;
  105. c.error = 0;
  106. x = get_byte(&c);
  107. if (x > 17) {
  108. copy(&c, x - 17);
  109. x = get_byte(&c);
  110. if (x < 16) c.error |= LZO_ERROR;
  111. }
  112. while (!c.error) {
  113. int cnt, back;
  114. if (x >> 4) {
  115. state = BACKPTR;
  116. if (x >> 6) {
  117. cnt = (x >> 5) - 1;
  118. back = (get_byte(&c) << 3) + ((x >> 2) & 7) + 1;
  119. } else if (x >> 5) {
  120. cnt = get_len(&c, x, 31);
  121. x = get_byte(&c);
  122. back = (get_byte(&c) << 6) + (x >> 2) + 1;
  123. } else {
  124. cnt = get_len(&c, x, 7);
  125. back = (1 << 14) + ((x & 8) << 11);
  126. x = get_byte(&c);
  127. back += (get_byte(&c) << 6) + (x >> 2);
  128. if (back == (1 << 14)) {
  129. if (cnt != 1)
  130. c.error |= LZO_ERROR;
  131. break;
  132. }
  133. }
  134. } else
  135. switch (state) {
  136. case COPY:
  137. cnt = get_len(&c, x, 15);
  138. copy(&c, cnt + 3);
  139. x = get_byte(&c);
  140. if (x >> 4)
  141. continue;
  142. cnt = 1;
  143. back = (1 << 11) + (get_byte(&c) << 2) + (x >> 2) + 1;
  144. break;
  145. case BACKPTR:
  146. cnt = 0;
  147. back = (get_byte(&c) << 2) + (x >> 2) + 1;
  148. break;
  149. }
  150. copy_backptr(&c, back, cnt + 2);
  151. cnt = x & 3;
  152. if (cnt)
  153. copy(&c, cnt);
  154. else
  155. state = (state == COPY) ? BACKPTR : COPY;
  156. x = get_byte(&c);
  157. }
  158. *inlen = c.in_end - c.in;
  159. *outlen = c.out_end - c.out;
  160. return c.error;
  161. }