3 * Copyright (c) 2006 Reimar Doeffinger
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.
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.
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
20 //! avoid e.g. MPlayers fast_memcpy, it slows things down here
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 {
31 uint8_t *out_start, *out, *out_end;
36 * \brief read one byte from input buffer, avoiding overrun
39 static inline int get_byte(LZOContext *c) {
40 if (c->in < c->in_end)
42 c->error |= LZO_INPUT_DEPLETED;
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
52 static inline int get_len(LZOContext *c, int x, int mask) {
55 while (!(x = get_byte(c))) cnt += 255;
62 * \brief copy bytes from input to output buffer with checking
63 * \param cnt number of bytes to copy, must be > 0
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;
72 if (dst + cnt > c->out_end) {
73 cnt = c->out_end - dst;
74 c->error |= LZO_OUTPUT_FULL;
76 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
86 memcpy(dst, src, cnt);
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
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.
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;
106 if (dst + cnt > c->out_end) {
107 cnt = c->out_end - dst;
108 c->error |= LZO_OUTPUT_FULL;
111 memset(dst, *src, cnt);
138 while (cnt > blocklen) {
139 memcpy(dst, src, blocklen);
144 memcpy(dst, src, cnt);
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
159 * make sure all buffers are appropriately padded, in must provide
160 * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
162 int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) {
163 enum {COPY, BACKPTR} state = COPY;
167 c.in_end = in + *inlen;
168 c.out = c.out_start = out;
169 c.out_end = out + * outlen;
175 if (x < 16) c.error |= LZO_ERROR;
182 back = (get_byte(&c) << 3) + ((x >> 2) & 7) + 1;
184 cnt = get_len(&c, x, 31);
186 back = (get_byte(&c) << 6) + (x >> 2) + 1;
188 cnt = get_len(&c, x, 7);
189 back = (1 << 14) + ((x & 8) << 11);
191 back += (get_byte(&c) << 6) + (x >> 2);
192 if (back == (1 << 14)) {
194 c.error |= LZO_ERROR;
201 cnt = get_len(&c, x, 15);
207 back = (1 << 11) + (get_byte(&c) << 2) + (x >> 2) + 1;
211 back = (get_byte(&c) << 2) + (x >> 2) + 1;
214 copy_backptr(&c, back, cnt + 2);
216 state = cnt ? BACKPTR : COPY;
221 *inlen = c.in_end - c.in;
222 *outlen = c.out_end - c.out;