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
22 typedef struct LZOContext {
24 uint8_t *out, *out_end;
30 * \brief read one byte from input buffer, avoiding overrun
33 static inline int get_byte(LZOContext *c) {
34 if (c->in < c->in_end)
36 c->error |= LZO_INPUT_DEPLETED;
41 * \brief decode a length value in the coding used by lzo
42 * \param x previous byte value
43 * \param mask bits used from x
44 * \return decoded length value
46 static inline int get_len(LZOContext *c, int x, int mask) {
49 while (!(x = get_byte(c))) cnt += 255;
56 * \brief copy bytes from input to output buffer with checking
57 * \param cnt number of bytes to copy, must be > 0
59 static inline void copy(LZOContext *c, int cnt) {
60 if (c->in + cnt > c->in_end) {
61 cnt = c->in_end - c->in;
62 c->error |= LZO_INPUT_DEPLETED;
64 if (c->out + cnt > c->out_end) {
65 cnt = c->out_end - c->out;
66 c->error |= LZO_OUTPUT_FULL;
74 * \brief copy previously decoded bytes to current position
75 * \param back how many bytes back we start
76 * \param cnt number of bytes to copy, must be > 0
78 * cnt > back is valid, this will copy the bytes we just copied.
80 static inline void copy_backptr(LZOContext *c, int back, int cnt) {
81 if (c->out - back < c->out_end - c->out_size) {
82 c->error |= LZO_INVALID_BACKPTR;
85 if (c->out + cnt > c->out_end) {
86 cnt = c->out_end - c->out;
87 c->error |= LZO_OUTPUT_FULL;
90 *c->out++ = c->out[-back];
95 * \brief decode LZO 1x compressed data
96 * \param out output buffer
97 * \param outlen size of output buffer, number of bytes left are returned here
98 * \param in input buffer
99 * \param inlen size of input buffer, number of bytes left are returned here
100 * \return 0 on success, otherwise error flags, see lzo.h
102 int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) {
103 enum {COPY, BACKPTR} state = COPY;
107 c.in_end = in + *inlen;
109 c.out_end = out + * outlen;
110 c.out_size = *outlen;
116 if (x < 16) c.error |= LZO_ERROR;
123 back = (get_byte(&c) << 3) + ((x >> 2) & 7) + 1;
125 cnt = get_len(&c, x, 31);
127 back = (get_byte(&c) << 6) + (x >> 2) + 1;
129 cnt = get_len(&c, x, 7);
130 back = (1 << 14) + ((x & 8) << 11);
132 back += (get_byte(&c) << 6) + (x >> 2);
133 if (back == (1 << 14)) {
135 c.error |= LZO_ERROR;
142 cnt = get_len(&c, x, 15);
148 back = (1 << 11) + (get_byte(&c) << 2) + (x >> 2) + 1;
152 back = (get_byte(&c) << 2) + (x >> 2) + 1;
155 copy_backptr(&c, back, cnt + 2);
157 state = cnt ? BACKPTR : COPY;
162 *inlen = c.in_end - c.in;
163 *outlen = c.out_end - c.out;