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 | //! avoid e.g. MPlayers fast_memcpy, it slows things down here
|
---|
21 | #undef memcpy
|
---|
22 | #include <string.h>
|
---|
23 | #include "lzo.h"
|
---|
24 |
|
---|
25 | //! define if we may write up to 12 bytes beyond the output buffer
|
---|
26 | #define OUTBUF_PADDED 1
|
---|
27 | //! define if we may read up to 4 bytes beyond the input buffer
|
---|
28 | #define INBUF_PADDED 1
|
---|
29 | typedef struct LZOContext {
|
---|
30 | uint8_t *in, *in_end;
|
---|
31 | uint8_t *out_start, *out, *out_end;
|
---|
32 | int error;
|
---|
33 | } LZOContext;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * \brief read one byte from input buffer, avoiding overrun
|
---|
37 | * \return byte read
|
---|
38 | */
|
---|
39 | static inline int get_byte(LZOContext *c) {
|
---|
40 | if (c->in < c->in_end)
|
---|
41 | return *c->in++;
|
---|
42 | c->error |= LZO_INPUT_DEPLETED;
|
---|
43 | return 1;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * \brief decode a length value in the coding used by lzo
|
---|
48 | * \param x previous byte value
|
---|
49 | * \param mask bits used from x
|
---|
50 | * \return decoded length value
|
---|
51 | */
|
---|
52 | static inline int get_len(LZOContext *c, int x, int mask) {
|
---|
53 | int cnt = x & mask;
|
---|
54 | if (!cnt) {
|
---|
55 | while (!(x = get_byte(c))) cnt += 255;
|
---|
56 | cnt += mask + x;
|
---|
57 | }
|
---|
58 | return cnt;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * \brief copy bytes from input to output buffer with checking
|
---|
63 | * \param cnt number of bytes to copy, must be > 0
|
---|
64 | */
|
---|
65 | static inline void copy(LZOContext *c, int cnt) {
|
---|
66 | register uint8_t *src = c->in;
|
---|
67 | register uint8_t *dst = c->out;
|
---|
68 | if (src + cnt > c->in_end) {
|
---|
69 | cnt = c->in_end - src;
|
---|
70 | c->error |= LZO_INPUT_DEPLETED;
|
---|
71 | }
|
---|
72 | if (dst + cnt > c->out_end) {
|
---|
73 | cnt = c->out_end - dst;
|
---|
74 | c->error |= LZO_OUTPUT_FULL;
|
---|
75 | }
|
---|
76 | #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
|
---|
77 | dst[0] = src[0];
|
---|
78 | dst[1] = src[1];
|
---|
79 | dst[2] = src[2];
|
---|
80 | dst[3] = src[3];
|
---|
81 | src += 4;
|
---|
82 | dst += 4;
|
---|
83 | cnt -= 4;
|
---|
84 | if (cnt > 0)
|
---|
85 | #endif
|
---|
86 | memcpy(dst, src, cnt);
|
---|
87 | c->in = src + cnt;
|
---|
88 | c->out = dst + cnt;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * \brief copy previously decoded bytes to current position
|
---|
93 | * \param back how many bytes back we start
|
---|
94 | * \param cnt number of bytes to copy, must be > 0
|
---|
95 | *
|
---|
96 | * cnt > back is valid, this will copy the bytes we just copied,
|
---|
97 | * thus creating a repeating pattern with a period length of back.
|
---|
98 | */
|
---|
99 | static inline void copy_backptr(LZOContext *c, int back, int cnt) {
|
---|
100 | register uint8_t *src = &c->out[-back];
|
---|
101 | register uint8_t *dst = c->out;
|
---|
102 | if (src < c->out_start) {
|
---|
103 | c->error |= LZO_INVALID_BACKPTR;
|
---|
104 | return;
|
---|
105 | }
|
---|
106 | if (dst + cnt > c->out_end) {
|
---|
107 | cnt = c->out_end - dst;
|
---|
108 | c->error |= LZO_OUTPUT_FULL;
|
---|
109 | }
|
---|
110 | if (back == 1) {
|
---|
111 | memset(dst, *src, cnt);
|
---|
112 | dst += cnt;
|
---|
113 | } else {
|
---|
114 | #ifdef OUTBUF_PADDED
|
---|
115 | dst[0] = src[0];
|
---|
116 | dst[1] = src[1];
|
---|
117 | dst[2] = src[2];
|
---|
118 | dst[3] = src[3];
|
---|
119 | src += 4;
|
---|
120 | dst += 4;
|
---|
121 | cnt -= 4;
|
---|
122 | if (cnt > 0) {
|
---|
123 | dst[0] = src[0];
|
---|
124 | dst[1] = src[1];
|
---|
125 | dst[2] = src[2];
|
---|
126 | dst[3] = src[3];
|
---|
127 | dst[4] = src[4];
|
---|
128 | dst[5] = src[5];
|
---|
129 | dst[6] = src[6];
|
---|
130 | dst[7] = src[7];
|
---|
131 | src += 8;
|
---|
132 | dst += 8;
|
---|
133 | cnt -= 8;
|
---|
134 | }
|
---|
135 | #endif
|
---|
136 | if (cnt > 0) {
|
---|
137 | int blocklen = back;
|
---|
138 | while (cnt > blocklen) {
|
---|
139 | memcpy(dst, src, blocklen);
|
---|
140 | dst += blocklen;
|
---|
141 | cnt -= blocklen;
|
---|
142 | blocklen <<= 1;
|
---|
143 | }
|
---|
144 | memcpy(dst, src, cnt);
|
---|
145 | }
|
---|
146 | dst += cnt;
|
---|
147 | }
|
---|
148 | c->out = dst;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * \brief decode LZO 1x compressed data
|
---|
153 | * \param out output buffer
|
---|
154 | * \param outlen size of output buffer, number of bytes left are returned here
|
---|
155 | * \param in input buffer
|
---|
156 | * \param inlen size of input buffer, number of bytes left are returned here
|
---|
157 | * \return 0 on success, otherwise error flags, see lzo.h
|
---|
158 | *
|
---|
159 | * make sure all buffers are appropriately padded, in must provide
|
---|
160 | * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
|
---|
161 | */
|
---|
162 | int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) {
|
---|
163 | enum {COPY, BACKPTR} state = COPY;
|
---|
164 | int x;
|
---|
165 | LZOContext c;
|
---|
166 | c.in = in;
|
---|
167 | c.in_end = in + *inlen;
|
---|
168 | c.out = c.out_start = out;
|
---|
169 | c.out_end = out + * outlen;
|
---|
170 | c.error = 0;
|
---|
171 | x = get_byte(&c);
|
---|
172 | if (x > 17) {
|
---|
173 | copy(&c, x - 17);
|
---|
174 | x = get_byte(&c);
|
---|
175 | if (x < 16) c.error |= LZO_ERROR;
|
---|
176 | }
|
---|
177 | while (!c.error) {
|
---|
178 | int cnt, back;
|
---|
179 | if (x >> 4) {
|
---|
180 | if (x >> 6) {
|
---|
181 | cnt = (x >> 5) - 1;
|
---|
182 | back = (get_byte(&c) << 3) + ((x >> 2) & 7) + 1;
|
---|
183 | } else if (x >> 5) {
|
---|
184 | cnt = get_len(&c, x, 31);
|
---|
185 | x = get_byte(&c);
|
---|
186 | back = (get_byte(&c) << 6) + (x >> 2) + 1;
|
---|
187 | } else {
|
---|
188 | cnt = get_len(&c, x, 7);
|
---|
189 | back = (1 << 14) + ((x & 8) << 11);
|
---|
190 | x = get_byte(&c);
|
---|
191 | back += (get_byte(&c) << 6) + (x >> 2);
|
---|
192 | if (back == (1 << 14)) {
|
---|
193 | if (cnt != 1)
|
---|
194 | c.error |= LZO_ERROR;
|
---|
195 | break;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | } else
|
---|
199 | switch (state) {
|
---|
200 | case COPY:
|
---|
201 | cnt = get_len(&c, x, 15);
|
---|
202 | copy(&c, cnt + 3);
|
---|
203 | x = get_byte(&c);
|
---|
204 | if (x >> 4)
|
---|
205 | continue;
|
---|
206 | cnt = 1;
|
---|
207 | back = (1 << 11) + (get_byte(&c) << 2) + (x >> 2) + 1;
|
---|
208 | break;
|
---|
209 | case BACKPTR:
|
---|
210 | cnt = 0;
|
---|
211 | back = (get_byte(&c) << 2) + (x >> 2) + 1;
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | copy_backptr(&c, back, cnt + 2);
|
---|
215 | cnt = x & 3;
|
---|
216 | state = cnt ? BACKPTR : COPY;
|
---|
217 | if (cnt)
|
---|
218 | copy(&c, cnt);
|
---|
219 | x = get_byte(&c);
|
---|
220 | }
|
---|
221 | *inlen = c.in_end - c.in;
|
---|
222 | *outlen = c.out_end - c.out;
|
---|
223 | return c.error;
|
---|
224 | }
|
---|