annotate lzo.c @ 4426:65ef5fd314ad libavcodec

LZO optimization: check input buffer bounds less frequently if padded
author reimar
date Sun, 28 Jan 2007 16:49:51 +0000
parents 4ade01ded107
children b80ad08b0f68
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
1 /*
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
2 * LZO 1x decompression
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
3 * Copyright (c) 2006 Reimar Doeffinger
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3060
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3060
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3060
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3060
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3060
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
15 * Lesser General Public License for more details.
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
16 *
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3060
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 3034
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
20 */
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
21 #include "common.h"
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
22 //! avoid e.g. MPlayers fast_memcpy, it slows things down here
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
23 #undef memcpy
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
24 #include <string.h>
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
25 #include "lzo.h"
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
26
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
27 //! define if we may write up to 12 bytes beyond the output buffer
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
28 #define OUTBUF_PADDED 1
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
29 //! define if we may read up to 8 bytes beyond the input buffer
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
30 #define INBUF_PADDED 1
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
31 typedef struct LZOContext {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
32 uint8_t *in, *in_end;
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
33 uint8_t *out_start, *out, *out_end;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
34 int error;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
35 } LZOContext;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
36
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
37 /**
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
38 * \brief read one byte from input buffer, avoiding overrun
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
39 * \return byte read
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
40 */
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
41 static inline int get_byte(LZOContext *c) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
42 if (c->in < c->in_end)
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
43 return *c->in++;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
44 c->error |= LZO_INPUT_DEPLETED;
3049
9f85c9cf6034 10l, get_byte returning 0 on error can cause a hang. So let's try with 1 instead...
reimar
parents: 3042
diff changeset
45 return 1;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
46 }
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
47
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
48 #ifdef INBUF_PADDED
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
49 #define GETB(c) (*(c).in++)
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
50 #else
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
51 #define GETB(c) get_byte(&(c))
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
52 #endif
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
53
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
54 /**
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
55 * \brief decode a length value in the coding used by lzo
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
56 * \param x previous byte value
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
57 * \param mask bits used from x
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
58 * \return decoded length value
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
59 */
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
60 static inline int get_len(LZOContext *c, int x, int mask) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
61 int cnt = x & mask;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
62 if (!cnt) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
63 while (!(x = get_byte(c))) cnt += 255;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
64 cnt += mask + x;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
65 }
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
66 return cnt;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
67 }
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
68
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
69 /**
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
70 * \brief copy bytes from input to output buffer with checking
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
71 * \param cnt number of bytes to copy, must be > 0
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
72 */
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
73 static inline void copy(LZOContext *c, int cnt) {
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
74 register uint8_t *src = c->in;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
75 register uint8_t *dst = c->out;
4419
4ade01ded107 Fix buffer end checks in lzo copy code to work in all cases.
reimar
parents: 4417
diff changeset
76 if (src + cnt > c->in_end || src + cnt < src) {
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
77 cnt = c->in_end - src;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
78 c->error |= LZO_INPUT_DEPLETED;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
79 }
4419
4ade01ded107 Fix buffer end checks in lzo copy code to work in all cases.
reimar
parents: 4417
diff changeset
80 if (dst + cnt > c->out_end || dst + cnt < dst) {
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
81 cnt = c->out_end - dst;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
82 c->error |= LZO_OUTPUT_FULL;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
83 }
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
84 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
85 dst[0] = src[0];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
86 dst[1] = src[1];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
87 dst[2] = src[2];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
88 dst[3] = src[3];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
89 src += 4;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
90 dst += 4;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
91 cnt -= 4;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
92 if (cnt > 0)
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
93 #endif
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
94 memcpy(dst, src, cnt);
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
95 c->in = src + cnt;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
96 c->out = dst + cnt;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
97 }
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
98
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
99 /**
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
100 * \brief copy previously decoded bytes to current position
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
101 * \param back how many bytes back we start
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
102 * \param cnt number of bytes to copy, must be > 0
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
103 *
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
104 * cnt > back is valid, this will copy the bytes we just copied,
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
105 * thus creating a repeating pattern with a period length of back.
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
106 */
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
107 static inline void copy_backptr(LZOContext *c, int back, int cnt) {
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
108 register uint8_t *src = &c->out[-back];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
109 register uint8_t *dst = c->out;
4419
4ade01ded107 Fix buffer end checks in lzo copy code to work in all cases.
reimar
parents: 4417
diff changeset
110 if (src < c->out_start || src > dst) {
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
111 c->error |= LZO_INVALID_BACKPTR;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
112 return;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
113 }
4419
4ade01ded107 Fix buffer end checks in lzo copy code to work in all cases.
reimar
parents: 4417
diff changeset
114 if (dst + cnt > c->out_end || dst + cnt < dst) {
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
115 cnt = c->out_end - dst;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
116 c->error |= LZO_OUTPUT_FULL;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
117 }
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
118 if (back == 1) {
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
119 memset(dst, *src, cnt);
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
120 dst += cnt;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
121 } else {
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
122 #ifdef OUTBUF_PADDED
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
123 dst[0] = src[0];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
124 dst[1] = src[1];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
125 dst[2] = src[2];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
126 dst[3] = src[3];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
127 src += 4;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
128 dst += 4;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
129 cnt -= 4;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
130 if (cnt > 0) {
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
131 dst[0] = src[0];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
132 dst[1] = src[1];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
133 dst[2] = src[2];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
134 dst[3] = src[3];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
135 dst[4] = src[4];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
136 dst[5] = src[5];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
137 dst[6] = src[6];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
138 dst[7] = src[7];
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
139 src += 8;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
140 dst += 8;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
141 cnt -= 8;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
142 }
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
143 #endif
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
144 if (cnt > 0) {
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
145 int blocklen = back;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
146 while (cnt > blocklen) {
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
147 memcpy(dst, src, blocklen);
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
148 dst += blocklen;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
149 cnt -= blocklen;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
150 blocklen <<= 1;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
151 }
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
152 memcpy(dst, src, cnt);
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
153 }
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
154 dst += cnt;
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
155 }
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
156 c->out = dst;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
157 }
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
158
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
159 /**
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
160 * \brief decode LZO 1x compressed data
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
161 * \param out output buffer
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
162 * \param outlen size of output buffer, number of bytes left are returned here
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
163 * \param in input buffer
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
164 * \param inlen size of input buffer, number of bytes left are returned here
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
165 * \return 0 on success, otherwise error flags, see lzo.h
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
166 *
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
167 * make sure all buffers are appropriately padded, in must provide
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
168 * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
169 */
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
170 int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
171 enum {COPY, BACKPTR} state = COPY;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
172 int x;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
173 LZOContext c;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
174 c.in = in;
3958
f48a01a0d3dc Avoid void *-arithmetic
reimar
parents: 3947
diff changeset
175 c.in_end = (uint8_t *)in + *inlen;
3060
a2f611d6c34d faster copy functions for lzo decoder that also need padding
reimar
parents: 3049
diff changeset
176 c.out = c.out_start = out;
3958
f48a01a0d3dc Avoid void *-arithmetic
reimar
parents: 3947
diff changeset
177 c.out_end = (uint8_t *)out + * outlen;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
178 c.error = 0;
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
179 x = GETB(c);
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
180 if (x > 17) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
181 copy(&c, x - 17);
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
182 x = GETB(c);
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
183 if (x < 16) c.error |= LZO_ERROR;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
184 }
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
185 while (!c.error) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
186 int cnt, back;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
187 if (x >> 4) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
188 if (x >> 6) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
189 cnt = (x >> 5) - 1;
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
190 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
191 } else if (x >> 5) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
192 cnt = get_len(&c, x, 31);
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
193 x = GETB(c);
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
194 back = (GETB(c) << 6) + (x >> 2) + 1;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
195 } else {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
196 cnt = get_len(&c, x, 7);
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
197 back = (1 << 14) + ((x & 8) << 11);
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
198 x = GETB(c);
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
199 back += (GETB(c) << 6) + (x >> 2);
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
200 if (back == (1 << 14)) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
201 if (cnt != 1)
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
202 c.error |= LZO_ERROR;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
203 break;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
204 }
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
205 }
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
206 } else
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
207 switch (state) {
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
208 case COPY:
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
209 cnt = get_len(&c, x, 15);
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
210 copy(&c, cnt + 3);
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
211 x = GETB(c);
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
212 if (x >> 4)
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
213 continue;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
214 cnt = 1;
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
215 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
216 break;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
217 case BACKPTR:
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
218 cnt = 0;
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
219 back = (GETB(c) << 2) + (x >> 2) + 1;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
220 break;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
221 }
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
222 copy_backptr(&c, back, cnt + 2);
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
223 cnt = x & 3;
3041
241b3a9bbd0c Wrong state handling causing decompression errors in some cases
reimar
parents: 3036
diff changeset
224 state = cnt ? BACKPTR : COPY;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
225 if (cnt)
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
226 copy(&c, cnt);
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
227 x = GETB(c);
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
228 if (c.in > c.in_end)
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
229 c.error |= LZO_INPUT_DEPLETED;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
230 }
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
231 *inlen = c.in_end - c.in;
4426
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
232 if (c.in > c.in_end)
65ef5fd314ad LZO optimization: check input buffer bounds less frequently if padded
reimar
parents: 4419
diff changeset
233 *inlen = 0;
3034
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
234 *outlen = c.out_end - c.out;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
235 return c.error;
d37065d8aeff Our own LZO (1X) implementation, under LGPL and optimized for readability.
reimar
parents:
diff changeset
236 }
4417
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
237
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
238 #ifdef TEST
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
239 #include <stdio.h>
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
240 #include <lzo/lzo1x.h>
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
241 #include "log.h"
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
242 #define MAXSZ (10*1024*1024)
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
243 int main(int argc, char *argv[]) {
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
244 FILE *in = fopen(argv[1], "rb");
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
245 uint8_t *orig = av_malloc(MAXSZ + 16);
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
246 uint8_t *comp = av_malloc(2*MAXSZ + 16);
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
247 uint8_t *decomp = av_malloc(MAXSZ + 16);
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
248 size_t s = fread(orig, 1, MAXSZ, in);
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
249 lzo_uint clen = 0;
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
250 long tmp[LZO1X_MEM_COMPRESS];
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
251 int inlen, outlen;
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
252 int i;
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
253 av_log_level = AV_LOG_DEBUG;
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
254 lzo1x_999_compress(orig, s, comp, &clen, tmp);
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
255 for (i = 0; i < 300; i++) {
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
256 START_TIMER
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
257 inlen = clen; outlen = MAXSZ;
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
258 if (lzo1x_decode(decomp, &outlen, comp, &inlen))
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
259 av_log(NULL, AV_LOG_ERROR, "decompression error\n");
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
260 STOP_TIMER("lzod")
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
261 }
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
262 if (memcmp(orig, decomp, s))
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
263 av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
264 else
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
265 av_log(NULL, AV_LOG_ERROR, "decompression ok\n");
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
266 return 0;
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
267 }
426ccc1cd1ae Add lzo test code
reimar
parents: 3958
diff changeset
268 #endif