00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "common.h"
00023 #undef memcpy
00024 #include <string.h>
00025 #include "lzo.h"
00026
00028 #define OUTBUF_PADDED 1
00030 #define INBUF_PADDED 1
00031 typedef struct LZOContext {
00032 const uint8_t *in, *in_end;
00033 uint8_t *out_start, *out, *out_end;
00034 int error;
00035 } LZOContext;
00036
00041 static inline int get_byte(LZOContext *c) {
00042 if (c->in < c->in_end)
00043 return *c->in++;
00044 c->error |= LZO_INPUT_DEPLETED;
00045 return 1;
00046 }
00047
00048 #ifdef INBUF_PADDED
00049 #define GETB(c) (*(c).in++)
00050 #else
00051 #define GETB(c) get_byte(&(c))
00052 #endif
00053
00060 static inline int get_len(LZOContext *c, int x, int mask) {
00061 int cnt = x & mask;
00062 if (!cnt) {
00063 while (!(x = get_byte(c))) cnt += 255;
00064 cnt += mask + x;
00065 }
00066 return cnt;
00067 }
00068
00069
00070 #define BUILTIN_MEMCPY
00071 #ifdef UNALIGNED_LOADSTORE
00072 #define COPY2(d, s) *(uint16_t *)(d) = *(uint16_t *)(s);
00073 #define COPY4(d, s) *(uint32_t *)(d) = *(uint32_t *)(s);
00074 #elif defined(BUILTIN_MEMCPY)
00075 #define COPY2(d, s) memcpy(d, s, 2);
00076 #define COPY4(d, s) memcpy(d, s, 4);
00077 #else
00078 #define COPY2(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1];
00079 #define COPY4(d, s) (d)[0] = (s)[0]; (d)[1] = (s)[1]; (d)[2] = (s)[2]; (d)[3] = (s)[3];
00080 #endif
00081
00086 static inline void copy(LZOContext *c, int cnt) {
00087 register const uint8_t *src = c->in;
00088 register uint8_t *dst = c->out;
00089 if (cnt > c->in_end - src) {
00090 cnt = FFMAX(c->in_end - src, 0);
00091 c->error |= LZO_INPUT_DEPLETED;
00092 }
00093 if (cnt > c->out_end - dst) {
00094 cnt = FFMAX(c->out_end - dst, 0);
00095 c->error |= LZO_OUTPUT_FULL;
00096 }
00097 #if defined(INBUF_PADDED) && defined(OUTBUF_PADDED)
00098 COPY4(dst, src);
00099 src += 4;
00100 dst += 4;
00101 cnt -= 4;
00102 if (cnt > 0)
00103 #endif
00104 memcpy(dst, src, cnt);
00105 c->in = src + cnt;
00106 c->out = dst + cnt;
00107 }
00108
00117 static inline void copy_backptr(LZOContext *c, int back, int cnt) {
00118 register const uint8_t *src = &c->out[-back];
00119 register uint8_t *dst = c->out;
00120 if (src < c->out_start || src > dst) {
00121 c->error |= LZO_INVALID_BACKPTR;
00122 return;
00123 }
00124 if (cnt > c->out_end - dst) {
00125 cnt = FFMAX(c->out_end - dst, 0);
00126 c->error |= LZO_OUTPUT_FULL;
00127 }
00128 if (back == 1) {
00129 memset(dst, *src, cnt);
00130 dst += cnt;
00131 } else {
00132 #ifdef OUTBUF_PADDED
00133 COPY2(dst, src);
00134 COPY2(dst + 2, src + 2);
00135 src += 4;
00136 dst += 4;
00137 cnt -= 4;
00138 if (cnt > 0) {
00139 COPY2(dst, src);
00140 COPY2(dst + 2, src + 2);
00141 COPY2(dst + 4, src + 4);
00142 COPY2(dst + 6, src + 6);
00143 src += 8;
00144 dst += 8;
00145 cnt -= 8;
00146 }
00147 #endif
00148 if (cnt > 0) {
00149 int blocklen = back;
00150 while (cnt > blocklen) {
00151 memcpy(dst, src, blocklen);
00152 dst += blocklen;
00153 cnt -= blocklen;
00154 blocklen <<= 1;
00155 }
00156 memcpy(dst, src, cnt);
00157 }
00158 dst += cnt;
00159 }
00160 c->out = dst;
00161 }
00162
00174 int lzo1x_decode(void *out, int *outlen, const void *in, int *inlen) {
00175 int state= 0;
00176 int x;
00177 LZOContext c;
00178 c.in = in;
00179 c.in_end = (const uint8_t *)in + *inlen;
00180 c.out = c.out_start = out;
00181 c.out_end = (uint8_t *)out + * outlen;
00182 c.error = 0;
00183 x = GETB(c);
00184 if (x > 17) {
00185 copy(&c, x - 17);
00186 x = GETB(c);
00187 if (x < 16) c.error |= LZO_ERROR;
00188 }
00189 if (c.in > c.in_end)
00190 c.error |= LZO_INPUT_DEPLETED;
00191 while (!c.error) {
00192 int cnt, back;
00193 if (x > 15) {
00194 if (x > 63) {
00195 cnt = (x >> 5) - 1;
00196 back = (GETB(c) << 3) + ((x >> 2) & 7) + 1;
00197 } else if (x > 31) {
00198 cnt = get_len(&c, x, 31);
00199 x = GETB(c);
00200 back = (GETB(c) << 6) + (x >> 2) + 1;
00201 } else {
00202 cnt = get_len(&c, x, 7);
00203 back = (1 << 14) + ((x & 8) << 11);
00204 x = GETB(c);
00205 back += (GETB(c) << 6) + (x >> 2);
00206 if (back == (1 << 14)) {
00207 if (cnt != 1)
00208 c.error |= LZO_ERROR;
00209 break;
00210 }
00211 }
00212 } else if(!state){
00213 cnt = get_len(&c, x, 15);
00214 copy(&c, cnt + 3);
00215 x = GETB(c);
00216 if (x > 15)
00217 continue;
00218 cnt = 1;
00219 back = (1 << 11) + (GETB(c) << 2) + (x >> 2) + 1;
00220 } else {
00221 cnt = 0;
00222 back = (GETB(c) << 2) + (x >> 2) + 1;
00223 }
00224 copy_backptr(&c, back, cnt + 2);
00225 state=
00226 cnt = x & 3;
00227 copy(&c, cnt);
00228 x = GETB(c);
00229 }
00230 *inlen = c.in_end - c.in;
00231 if (c.in > c.in_end)
00232 *inlen = 0;
00233 *outlen = c.out_end - c.out;
00234 return c.error;
00235 }
00236
00237 #ifdef TEST
00238 #include <stdio.h>
00239 #include <lzo/lzo1x.h>
00240 #include "log.h"
00241 #define MAXSZ (10*1024*1024)
00242 int main(int argc, char *argv[]) {
00243 FILE *in = fopen(argv[1], "rb");
00244 uint8_t *orig = av_malloc(MAXSZ + 16);
00245 uint8_t *comp = av_malloc(2*MAXSZ + 16);
00246 uint8_t *decomp = av_malloc(MAXSZ + 16);
00247 size_t s = fread(orig, 1, MAXSZ, in);
00248 lzo_uint clen = 0;
00249 long tmp[LZO1X_MEM_COMPRESS];
00250 int inlen, outlen;
00251 int i;
00252 av_log_level = AV_LOG_DEBUG;
00253 lzo1x_999_compress(orig, s, comp, &clen, tmp);
00254 for (i = 0; i < 300; i++) {
00255 START_TIMER
00256 inlen = clen; outlen = MAXSZ;
00257 #ifdef LIBLZO
00258 if (lzo1x_decompress_safe(comp, inlen, decomp, &outlen, NULL))
00259 #elif defined(LIBLZO_UNSAFE)
00260 if (lzo1x_decompress(comp, inlen, decomp, &outlen, NULL))
00261 #else
00262 if (lzo1x_decode(decomp, &outlen, comp, &inlen))
00263 #endif
00264 av_log(NULL, AV_LOG_ERROR, "decompression error\n");
00265 STOP_TIMER("lzod")
00266 }
00267 if (memcmp(orig, decomp, s))
00268 av_log(NULL, AV_LOG_ERROR, "decompression incorrect\n");
00269 else
00270 av_log(NULL, AV_LOG_ERROR, "decompression ok\n");
00271 return 0;
00272 }
00273 #endif