00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #define __LZO_IN_MINILZO
00041 #define LZO_BUILD
00042
00043 #ifdef MINILZO_HAVE_CONFIG_H
00044 # include <config.h>
00045 #endif
00046
00047 #undef LZO_HAVE_CONFIG_H
00048 #include "minilzo.h"
00049
00050 #if !defined(MINILZO_VERSION) || (MINILZO_VERSION != 0x1080)
00051 # error "version mismatch in miniLZO source files"
00052 #endif
00053
00054 #ifdef MINILZO_HAVE_CONFIG_H
00055 # define LZO_HAVE_CONFIG_H
00056 #endif
00057
00058 #if !defined(LZO_NO_SYS_TYPES_H)
00059 # include <sys/types.h>
00060 #endif
00061 #include <stdio.h>
00062
00063 #ifndef __LZO_CONF_H
00064 #define __LZO_CONF_H
00065
00066 #if !defined(__LZO_IN_MINILZO)
00067 # ifndef __LZOCONF_H
00068 # include <lzoconf.h>
00069 # endif
00070 #endif
00071
00072 #if defined(__BOUNDS_CHECKING_ON)
00073 # include <unchecked.h>
00074 #else
00075 # define BOUNDS_CHECKING_OFF_DURING(stmt) stmt
00076 # define BOUNDS_CHECKING_OFF_IN_EXPR(expr) (expr)
00077 #endif
00078
00079 #if !defined(LZO_HAVE_CONFIG_H)
00080 # include <stddef.h>
00081 # include <string.h>
00082 # if !defined(NO_STDLIB_H)
00083 # include <stdlib.h>
00084 # endif
00085 # define HAVE_MEMCMP
00086 # define HAVE_MEMCPY
00087 # define HAVE_MEMMOVE
00088 # define HAVE_MEMSET
00089 #else
00090 # include <sys/types.h>
00091 # if defined(HAVE_STDDEF_H)
00092 # include <stddef.h>
00093 # endif
00094 # if defined(STDC_HEADERS)
00095 # include <string.h>
00096 # include <stdlib.h>
00097 # endif
00098 #endif
00099
00100 #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
00101 # define HAVE_MALLOC_H
00102 # define HAVE_HALLOC
00103 #endif
00104
00105 #undef NDEBUG
00106 #if !defined(LZO_DEBUG)
00107 # define NDEBUG
00108 #endif
00109 #if defined(LZO_DEBUG) || !defined(NDEBUG)
00110 # if !defined(NO_STDIO_H)
00111 # include <stdio.h>
00112 # endif
00113 #endif
00114 #include <assert.h>
00115
00116 #if !defined(LZO_COMPILE_TIME_ASSERT)
00117 # define LZO_COMPILE_TIME_ASSERT(expr) \
00118 { typedef int __lzo_compile_time_assert_fail[1 - 2 * !(expr)]; }
00119 #endif
00120
00121 #if !defined(LZO_UNUSED)
00122 # if 1
00123 # define LZO_UNUSED(var) ((void)&var)
00124 # elif 0
00125 # define LZO_UNUSED(var) { typedef int __lzo_unused[sizeof(var) ? 2 : 1]; }
00126 # else
00127 # define LZO_UNUSED(parm) (parm = parm)
00128 # endif
00129 #endif
00130
00131 #if !defined(__inline__) && !defined(__GNUC__)
00132 # if defined(__cplusplus)
00133 # define __inline__ inline
00134 # else
00135 # define __inline__
00136 # endif
00137 #endif
00138
00139 #if defined(NO_MEMCMP)
00140 # undef HAVE_MEMCMP
00141 #endif
00142
00143 #if !defined(HAVE_MEMCMP)
00144 # undef memcmp
00145 # define memcmp lzo_memcmp
00146 #endif
00147 #if !defined(HAVE_MEMCPY)
00148 # undef memcpy
00149 # define memcpy lzo_memcpy
00150 #endif
00151 #if !defined(HAVE_MEMMOVE)
00152 # undef memmove
00153 # define memmove lzo_memmove
00154 #endif
00155 #if !defined(HAVE_MEMSET)
00156 # undef memset
00157 # define memset lzo_memset
00158 #endif
00159
00160 #if 0
00161 # define LZO_BYTE(x) ((unsigned char) (x))
00162 #else
00163 # define LZO_BYTE(x) ((unsigned char) ((x) & 0xff))
00164 #endif
00165
00166 #define LZO_MAX(a,b) ((a) >= (b) ? (a) : (b))
00167 #define LZO_MIN(a,b) ((a) <= (b) ? (a) : (b))
00168 #define LZO_MAX3(a,b,c) ((a) >= (b) ? LZO_MAX(a,c) : LZO_MAX(b,c))
00169 #define LZO_MIN3(a,b,c) ((a) <= (b) ? LZO_MIN(a,c) : LZO_MIN(b,c))
00170
00171 #define lzo_sizeof(type) ((lzo_uint) (sizeof(type)))
00172
00173 #define LZO_HIGH(array) ((lzo_uint) (sizeof(array)/sizeof(*(array))))
00174
00175 #define LZO_SIZE(bits) (1u << (bits))
00176 #define LZO_MASK(bits) (LZO_SIZE(bits) - 1)
00177
00178 #define LZO_LSIZE(bits) (1ul << (bits))
00179 #define LZO_LMASK(bits) (LZO_LSIZE(bits) - 1)
00180
00181 #define LZO_USIZE(bits) ((lzo_uint) 1 << (bits))
00182 #define LZO_UMASK(bits) (LZO_USIZE(bits) - 1)
00183
00184 #define LZO_STYPE_MAX(b) (((1l << (8*(b)-2)) - 1l) + (1l << (8*(b)-2)))
00185 #define LZO_UTYPE_MAX(b) (((1ul << (8*(b)-1)) - 1ul) + (1ul << (8*(b)-1)))
00186
00187 #if !defined(SIZEOF_UNSIGNED)
00188 # if (UINT_MAX == 0xffff)
00189 # define SIZEOF_UNSIGNED 2
00190 # elif (UINT_MAX == LZO_0xffffffffL)
00191 # define SIZEOF_UNSIGNED 4
00192 # elif (UINT_MAX >= LZO_0xffffffffL)
00193 # define SIZEOF_UNSIGNED 8
00194 # else
00195 # error "SIZEOF_UNSIGNED"
00196 # endif
00197 #endif
00198
00199 #if !defined(SIZEOF_UNSIGNED_LONG)
00200 # if (ULONG_MAX == LZO_0xffffffffL)
00201 # define SIZEOF_UNSIGNED_LONG 4
00202 # elif (ULONG_MAX >= LZO_0xffffffffL)
00203 # define SIZEOF_UNSIGNED_LONG 8
00204 # else
00205 # error "SIZEOF_UNSIGNED_LONG"
00206 # endif
00207 #endif
00208
00209 #if !defined(SIZEOF_SIZE_T)
00210 # define SIZEOF_SIZE_T SIZEOF_UNSIGNED
00211 #endif
00212 #if !defined(SIZE_T_MAX)
00213 # define SIZE_T_MAX LZO_UTYPE_MAX(SIZEOF_SIZE_T)
00214 #endif
00215
00216 #if 1 && defined(__LZO_i386) && (UINT_MAX == LZO_0xffffffffL)
00217 # if !defined(LZO_UNALIGNED_OK_2) && (USHRT_MAX == 0xffff)
00218 # define LZO_UNALIGNED_OK_2
00219 # endif
00220 # if !defined(LZO_UNALIGNED_OK_4) && (LZO_UINT32_MAX == LZO_0xffffffffL)
00221 # define LZO_UNALIGNED_OK_4
00222 # endif
00223 #endif
00224
00225 #if defined(LZO_UNALIGNED_OK_2) || defined(LZO_UNALIGNED_OK_4)
00226 # if !defined(LZO_UNALIGNED_OK)
00227 # define LZO_UNALIGNED_OK
00228 # endif
00229 #endif
00230
00231 #if defined(__LZO_NO_UNALIGNED)
00232 # undef LZO_UNALIGNED_OK
00233 # undef LZO_UNALIGNED_OK_2
00234 # undef LZO_UNALIGNED_OK_4
00235 #endif
00236
00237 #if defined(LZO_UNALIGNED_OK_2) && (USHRT_MAX != 0xffff)
00238 # error "LZO_UNALIGNED_OK_2 must not be defined on this system"
00239 #endif
00240 #if defined(LZO_UNALIGNED_OK_4) && (LZO_UINT32_MAX != LZO_0xffffffffL)
00241 # error "LZO_UNALIGNED_OK_4 must not be defined on this system"
00242 #endif
00243
00244 #if defined(__LZO_NO_ALIGNED)
00245 # undef LZO_ALIGNED_OK_4
00246 #endif
00247
00248 #if defined(LZO_ALIGNED_OK_4) && (LZO_UINT32_MAX != LZO_0xffffffffL)
00249 # error "LZO_ALIGNED_OK_4 must not be defined on this system"
00250 #endif
00251
00252 #define LZO_LITTLE_ENDIAN 1234
00253 #define LZO_BIG_ENDIAN 4321
00254 #define LZO_PDP_ENDIAN 3412
00255
00256 #if !defined(LZO_BYTE_ORDER)
00257 # if defined(MFX_BYTE_ORDER)
00258 # define LZO_BYTE_ORDER MFX_BYTE_ORDER
00259 # elif defined(__LZO_i386)
00260 # define LZO_BYTE_ORDER LZO_LITTLE_ENDIAN
00261 # elif defined(BYTE_ORDER)
00262 # define LZO_BYTE_ORDER BYTE_ORDER
00263 # elif defined(__BYTE_ORDER)
00264 # define LZO_BYTE_ORDER __BYTE_ORDER
00265 # endif
00266 #endif
00267
00268 #if defined(LZO_BYTE_ORDER)
00269 # if (LZO_BYTE_ORDER != LZO_LITTLE_ENDIAN) && \
00270 (LZO_BYTE_ORDER != LZO_BIG_ENDIAN)
00271 # error "invalid LZO_BYTE_ORDER"
00272 # endif
00273 #endif
00274
00275 #if defined(LZO_UNALIGNED_OK) && !defined(LZO_BYTE_ORDER)
00276 # error "LZO_BYTE_ORDER is not defined"
00277 #endif
00278
00279 #define LZO_OPTIMIZE_GNUC_i386_IS_BUGGY
00280
00281 #if defined(NDEBUG) && !defined(LZO_DEBUG) && !defined(__LZO_CHECKER)
00282 # if defined(__GNUC__) && defined(__i386__)
00283 # if !defined(LZO_OPTIMIZE_GNUC_i386_IS_BUGGY)
00284 # define LZO_OPTIMIZE_GNUC_i386
00285 # endif
00286 # endif
00287 #endif
00288
00289 __LZO_EXTERN_C int __lzo_init_done;
00290 __LZO_EXTERN_C const lzo_byte __lzo_copyright[];
00291 LZO_EXTERN(const lzo_byte *) lzo_copyright(void);
00292 __LZO_EXTERN_C const lzo_uint32 _lzo_crc32_table[256];
00293
00294 #define _LZO_STRINGIZE(x) #x
00295 #define _LZO_MEXPAND(x) _LZO_STRINGIZE(x)
00296
00297 #define _LZO_CONCAT2(a,b) a ## b
00298 #define _LZO_CONCAT3(a,b,c) a ## b ## c
00299 #define _LZO_CONCAT4(a,b,c,d) a ## b ## c ## d
00300 #define _LZO_CONCAT5(a,b,c,d,e) a ## b ## c ## d ## e
00301
00302 #define _LZO_ECONCAT2(a,b) _LZO_CONCAT2(a,b)
00303 #define _LZO_ECONCAT3(a,b,c) _LZO_CONCAT3(a,b,c)
00304 #define _LZO_ECONCAT4(a,b,c,d) _LZO_CONCAT4(a,b,c,d)
00305 #define _LZO_ECONCAT5(a,b,c,d,e) _LZO_CONCAT5(a,b,c,d,e)
00306
00307 #if 0
00308
00309 #define __LZO_IS_COMPRESS_QUERY(i,il,o,ol,w) ((lzo_voidp)(o) == (w))
00310 #define __LZO_QUERY_COMPRESS(i,il,o,ol,w,n,s) \
00311 (*ol = (n)*(s), LZO_E_OK)
00312
00313 #define __LZO_IS_DECOMPRESS_QUERY(i,il,o,ol,w) ((lzo_voidp)(o) == (w))
00314 #define __LZO_QUERY_DECOMPRESS(i,il,o,ol,w,n,s) \
00315 (*ol = (n)*(s), LZO_E_OK)
00316
00317 #define __LZO_IS_OPTIMIZE_QUERY(i,il,o,ol,w) ((lzo_voidp)(o) == (w))
00318 #define __LZO_QUERY_OPTIMIZE(i,il,o,ol,w,n,s) \
00319 (*ol = (n)*(s), LZO_E_OK)
00320
00321 #endif
00322
00323 #ifndef __LZO_PTR_H
00324 #define __LZO_PTR_H
00325
00326 #ifdef __cplusplus
00327 extern "C" {
00328 #endif
00329
00330 #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
00331 # include <dos.h>
00332 # if 1 && defined(__WATCOMC__)
00333 # include <i86.h>
00334 __LZO_EXTERN_C unsigned char _HShift;
00335 # define __LZO_HShift _HShift
00336 # elif 1 && defined(_MSC_VER)
00337 __LZO_EXTERN_C unsigned short __near _AHSHIFT;
00338 # define __LZO_HShift ((unsigned) &_AHSHIFT)
00339 # elif defined(__LZO_WIN16)
00340 # define __LZO_HShift 3
00341 # else
00342 # define __LZO_HShift 12
00343 # endif
00344 # if !defined(_FP_SEG) && defined(FP_SEG)
00345 # define _FP_SEG FP_SEG
00346 # endif
00347 # if !defined(_FP_OFF) && defined(FP_OFF)
00348 # define _FP_OFF FP_OFF
00349 # endif
00350 #endif
00351
00352 #if !defined(lzo_ptrdiff_t)
00353 # if (UINT_MAX >= LZO_0xffffffffL)
00354 typedef ptrdiff_t lzo_ptrdiff_t;
00355 # else
00356 typedef long lzo_ptrdiff_t;
00357 # endif
00358 #endif
00359
00360 #if !defined(__LZO_HAVE_PTR_T)
00361 # if defined(lzo_ptr_t)
00362 # define __LZO_HAVE_PTR_T
00363 # endif
00364 #endif
00365 #if !defined(__LZO_HAVE_PTR_T)
00366 # if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_LONG)
00367 # if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_LONG)
00368 typedef unsigned long lzo_ptr_t;
00369 typedef long lzo_sptr_t;
00370 # define __LZO_HAVE_PTR_T
00371 # endif
00372 # endif
00373 #endif
00374 #if !defined(__LZO_HAVE_PTR_T)
00375 # if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED)
00376 # if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED)
00377 typedef unsigned int lzo_ptr_t;
00378 typedef int lzo_sptr_t;
00379 # define __LZO_HAVE_PTR_T
00380 # endif
00381 # endif
00382 #endif
00383 #if !defined(__LZO_HAVE_PTR_T)
00384 # if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_SHORT)
00385 # if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_SHORT)
00386 typedef unsigned short lzo_ptr_t;
00387 typedef short lzo_sptr_t;
00388 # define __LZO_HAVE_PTR_T
00389 # endif
00390 # endif
00391 #endif
00392 #if !defined(__LZO_HAVE_PTR_T)
00393 # if defined(LZO_HAVE_CONFIG_H) || defined(SIZEOF_CHAR_P)
00394 # error "no suitable type for lzo_ptr_t"
00395 # else
00396 typedef unsigned long lzo_ptr_t;
00397 typedef long lzo_sptr_t;
00398 # define __LZO_HAVE_PTR_T
00399 # endif
00400 #endif
00401
00402 #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
00403 #define PTR(a) ((lzo_bytep) (a))
00404 #define PTR_ALIGNED_4(a) ((_FP_OFF(a) & 3) == 0)
00405 #define PTR_ALIGNED2_4(a,b) (((_FP_OFF(a) | _FP_OFF(b)) & 3) == 0)
00406 #else
00407 #define PTR(a) ((lzo_ptr_t) (a))
00408 #define PTR_LINEAR(a) PTR(a)
00409 #define PTR_ALIGNED_4(a) ((PTR_LINEAR(a) & 3) == 0)
00410 #define PTR_ALIGNED_8(a) ((PTR_LINEAR(a) & 7) == 0)
00411 #define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
00412 #define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
00413 #endif
00414
00415 #define PTR_LT(a,b) (PTR(a) < PTR(b))
00416 #define PTR_GE(a,b) (PTR(a) >= PTR(b))
00417 #define PTR_DIFF(a,b) ((lzo_ptrdiff_t) (PTR(a) - PTR(b)))
00418 #define pd(a,b) ((lzo_uint) ((a)-(b)))
00419
00420 LZO_EXTERN(lzo_ptr_t)
00421 __lzo_ptr_linear(const lzo_voidp ptr);
00422
00423 typedef union
00424 {
00425 char a_char;
00426 unsigned char a_uchar;
00427 short a_short;
00428 unsigned short a_ushort;
00429 int a_int;
00430 unsigned int a_uint;
00431 long a_long;
00432 unsigned long a_ulong;
00433 lzo_int a_lzo_int;
00434 lzo_uint a_lzo_uint;
00435 lzo_int32 a_lzo_int32;
00436 lzo_uint32 a_lzo_uint32;
00437 ptrdiff_t a_ptrdiff_t;
00438 lzo_ptrdiff_t a_lzo_ptrdiff_t;
00439 lzo_ptr_t a_lzo_ptr_t;
00440 lzo_voidp a_lzo_voidp;
00441 void * a_void_p;
00442 lzo_bytep a_lzo_bytep;
00443 lzo_bytepp a_lzo_bytepp;
00444 lzo_uintp a_lzo_uintp;
00445 lzo_uint * a_lzo_uint_p;
00446 lzo_uint32p a_lzo_uint32p;
00447 lzo_uint32 * a_lzo_uint32_p;
00448 unsigned char * a_uchar_p;
00449 char * a_char_p;
00450 }
00451 lzo_full_align_t;
00452
00453 #ifdef __cplusplus
00454 }
00455 #endif
00456
00457 #endif
00458
00459 #define LZO_DETERMINISTIC
00460
00461 #define LZO_DICT_USE_PTR
00462 #if defined(__LZO_DOS16) || defined(__LZO_WIN16) || defined(__LZO_STRICT_16BIT)
00463 # undef LZO_DICT_USE_PTR
00464 #endif
00465
00466 #if defined(LZO_DICT_USE_PTR)
00467 # define lzo_dict_t const lzo_bytep
00468 # define lzo_dict_p lzo_dict_t __LZO_MMODEL *
00469 #else
00470 # define lzo_dict_t lzo_uint
00471 # define lzo_dict_p lzo_dict_t __LZO_MMODEL *
00472 #endif
00473
00474 #if !defined(lzo_moff_t)
00475 #define lzo_moff_t lzo_uint
00476 #endif
00477
00478 #endif
00479
00480 LZO_PUBLIC(lzo_ptr_t)
00481 __lzo_ptr_linear(const lzo_voidp ptr)
00482 {
00483 lzo_ptr_t p;
00484
00485 #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
00486 p = (((lzo_ptr_t)(_FP_SEG(ptr))) << (16 - __LZO_HShift)) + (_FP_OFF(ptr));
00487 #else
00488 p = PTR_LINEAR(ptr);
00489 #endif
00490
00491 return p;
00492 }
00493
00494 LZO_PUBLIC(unsigned)
00495 __lzo_align_gap(const lzo_voidp ptr, lzo_uint size)
00496 {
00497 lzo_ptr_t p, s, n;
00498
00499 assert(size > 0);
00500
00501 p = __lzo_ptr_linear(ptr);
00502 s = (lzo_ptr_t) (size - 1);
00503 #if 0
00504 assert((size & (size - 1)) == 0);
00505 n = ((p + s) & ~s) - p;
00506 #else
00507 n = (((p + s) / size) * size) - p;
00508 #endif
00509
00510 assert((long)n >= 0);
00511 assert(n <= s);
00512
00513 return (unsigned)n;
00514 }
00515
00516 #ifndef __LZO_UTIL_H
00517 #define __LZO_UTIL_H
00518
00519 #ifndef __LZO_CONF_H
00520 #endif
00521
00522 #ifdef __cplusplus
00523 extern "C" {
00524 #endif
00525
00526 #if 1 && defined(HAVE_MEMCPY)
00527 #if !defined(__LZO_DOS16) && !defined(__LZO_WIN16)
00528
00529 #define MEMCPY8_DS(dest,src,len) \
00530 memcpy(dest,src,len); \
00531 dest += len; \
00532 src += len
00533
00534 #endif
00535 #endif
00536
00537 #if 0 && !defined(MEMCPY8_DS)
00538
00539 #define MEMCPY8_DS(dest,src,len) \
00540 { do { \
00541 *dest++ = *src++; \
00542 *dest++ = *src++; \
00543 *dest++ = *src++; \
00544 *dest++ = *src++; \
00545 *dest++ = *src++; \
00546 *dest++ = *src++; \
00547 *dest++ = *src++; \
00548 *dest++ = *src++; \
00549 len -= 8; \
00550 } while (len > 0); }
00551
00552 #endif
00553
00554 #if !defined(MEMCPY8_DS)
00555
00556 #define MEMCPY8_DS(dest,src,len) \
00557 { register lzo_uint __l = (len) / 8; \
00558 do { \
00559 *dest++ = *src++; \
00560 *dest++ = *src++; \
00561 *dest++ = *src++; \
00562 *dest++ = *src++; \
00563 *dest++ = *src++; \
00564 *dest++ = *src++; \
00565 *dest++ = *src++; \
00566 *dest++ = *src++; \
00567 } while (--__l > 0); }
00568
00569 #endif
00570
00571 #define MEMCPY_DS(dest,src,len) \
00572 do *dest++ = *src++; \
00573 while (--len > 0)
00574
00575 #define MEMMOVE_DS(dest,src,len) \
00576 do *dest++ = *src++; \
00577 while (--len > 0)
00578
00579 #if 0 && defined(LZO_OPTIMIZE_GNUC_i386)
00580
00581 #define BZERO8_PTR(s,l,n) \
00582 __asm__ __volatile__( \
00583 "movl %0,%%eax \n" \
00584 "movl %1,%%edi \n" \
00585 "movl %2,%%ecx \n" \
00586 "cld \n" \
00587 "rep \n" \
00588 "stosl %%eax,(%%edi) \n" \
00589 : \
00590 :"g" (0),"g" (s),"g" (n) \
00591 :"eax","edi","ecx", "memory", "cc" \
00592 )
00593
00594 #elif (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET)
00595
00596 #if 1
00597 #define BZERO8_PTR(s,l,n) memset((s),0,(lzo_uint)(l)*(n))
00598 #else
00599 #define BZERO8_PTR(s,l,n) memset((lzo_voidp)(s),0,(lzo_uint)(l)*(n))
00600 #endif
00601
00602 #else
00603
00604 #define BZERO8_PTR(s,l,n) \
00605 lzo_memset((lzo_voidp)(s),0,(lzo_uint)(l)*(n))
00606
00607 #endif
00608
00609 #if 0
00610 #if defined(__GNUC__) && defined(__i386__)
00611
00612 unsigned char lzo_rotr8(unsigned char value, int shift);
00613 extern __inline__ unsigned char lzo_rotr8(unsigned char value, int shift)
00614 {
00615 unsigned char result;
00616
00617 __asm__ __volatile__ ("movb %b1, %b0; rorb %b2, %b0"
00618 : "=a"(result) : "g"(value), "c"(shift));
00619 return result;
00620 }
00621
00622 unsigned short lzo_rotr16(unsigned short value, int shift);
00623 extern __inline__ unsigned short lzo_rotr16(unsigned short value, int shift)
00624 {
00625 unsigned short result;
00626
00627 __asm__ __volatile__ ("movw %b1, %b0; rorw %b2, %b0"
00628 : "=a"(result) : "g"(value), "c"(shift));
00629 return result;
00630 }
00631
00632 #endif
00633 #endif
00634
00635 #ifdef __cplusplus
00636 }
00637 #endif
00638
00639 #endif
00640
00641 LZO_PUBLIC(lzo_bool)
00642 lzo_assert(int expr)
00643 {
00644 return (expr) ? 1 : 0;
00645 }
00646
00647
00648
00649
00650
00651 const lzo_byte __lzo_copyright[] =
00652 #if !defined(__LZO_IN_MINLZO)
00653 LZO_VERSION_STRING;
00654 #else
00655 "\n\n\n"
00656 "LZO real-time data compression library.\n"
00657 "Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 Markus Franz Xaver Johannes Oberhumer\n"
00658 "<markus.oberhumer@jk.uni-linz.ac.at>\n"
00659 "http://www.oberhumer.com/opensource/lzo/\n"
00660 "\n"
00661 "LZO version: v" LZO_VERSION_STRING ", " LZO_VERSION_DATE "\n"
00662 "LZO build date: " __DATE__ " " __TIME__ "\n\n"
00663 "LZO special compilation options:\n"
00664 #ifdef __cplusplus
00665 " __cplusplus\n"
00666 #endif
00667 #if defined(__PIC__)
00668 " __PIC__\n"
00669 #elif defined(__pic__)
00670 " __pic__\n"
00671 #endif
00672 #if (UINT_MAX < LZO_0xffffffffL)
00673 " 16BIT\n"
00674 #endif
00675 #if defined(__LZO_STRICT_16BIT)
00676 " __LZO_STRICT_16BIT\n"
00677 #endif
00678 #if (UINT_MAX > LZO_0xffffffffL)
00679 " UINT_MAX=" _LZO_MEXPAND(UINT_MAX) "\n"
00680 #endif
00681 #if (ULONG_MAX > LZO_0xffffffffL)
00682 " ULONG_MAX=" _LZO_MEXPAND(ULONG_MAX) "\n"
00683 #endif
00684 #if defined(LZO_BYTE_ORDER)
00685 " LZO_BYTE_ORDER=" _LZO_MEXPAND(LZO_BYTE_ORDER) "\n"
00686 #endif
00687 #if defined(LZO_UNALIGNED_OK_2)
00688 " LZO_UNALIGNED_OK_2\n"
00689 #endif
00690 #if defined(LZO_UNALIGNED_OK_4)
00691 " LZO_UNALIGNED_OK_4\n"
00692 #endif
00693 #if defined(LZO_ALIGNED_OK_4)
00694 " LZO_ALIGNED_OK_4\n"
00695 #endif
00696 #if defined(LZO_DICT_USE_PTR)
00697 " LZO_DICT_USE_PTR\n"
00698 #endif
00699 #if defined(__LZO_QUERY_COMPRESS)
00700 " __LZO_QUERY_COMPRESS\n"
00701 #endif
00702 #if defined(__LZO_QUERY_DECOMPRESS)
00703 " __LZO_QUERY_DECOMPRESS\n"
00704 #endif
00705 #if defined(__LZO_IN_MINILZO)
00706 " __LZO_IN_MINILZO\n"
00707 #endif
00708 "\n\n"
00709 "$Id: LZO " LZO_VERSION_STRING " built " __DATE__ " " __TIME__
00710 #if defined(__GNUC__) && defined(__VERSION__)
00711 " by gcc " __VERSION__
00712 #elif defined(__BORLANDC__)
00713 " by Borland C " _LZO_MEXPAND(__BORLANDC__)
00714 #elif defined(_MSC_VER)
00715 " by Microsoft C " _LZO_MEXPAND(_MSC_VER)
00716 #elif defined(__PUREC__)
00717 " by Pure C " _LZO_MEXPAND(__PUREC__)
00718 #elif defined(__SC__)
00719 " by Symantec C " _LZO_MEXPAND(__SC__)
00720 #elif defined(__TURBOC__)
00721 " by Turbo C " _LZO_MEXPAND(__TURBOC__)
00722 #elif defined(__WATCOMC__)
00723 " by Watcom C " _LZO_MEXPAND(__WATCOMC__)
00724 #endif
00725 " $\n"
00726 "$Copyright: LZO (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 Markus Franz Xaver Johannes Oberhumer $\n";
00727 #endif
00728
00729 LZO_PUBLIC(const lzo_byte *)
00730 lzo_copyright(void)
00731 {
00732 return __lzo_copyright;
00733 }
00734
00735 LZO_PUBLIC(unsigned)
00736 lzo_version(void)
00737 {
00738 return LZO_VERSION;
00739 }
00740
00741 LZO_PUBLIC(const char *)
00742 lzo_version_string(void)
00743 {
00744 return LZO_VERSION_STRING;
00745 }
00746
00747 LZO_PUBLIC(const char *)
00748 lzo_version_date(void)
00749 {
00750 return LZO_VERSION_DATE;
00751 }
00752
00753 LZO_PUBLIC(const lzo_charp)
00754 _lzo_version_string(void)
00755 {
00756 return LZO_VERSION_STRING;
00757 }
00758
00759 LZO_PUBLIC(const lzo_charp)
00760 _lzo_version_date(void)
00761 {
00762 return LZO_VERSION_DATE;
00763 }
00764
00765 #define LZO_BASE 65521u
00766 #define LZO_NMAX 5552
00767
00768 #define LZO_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
00769 #define LZO_DO2(buf,i) LZO_DO1(buf,i); LZO_DO1(buf,i+1);
00770 #define LZO_DO4(buf,i) LZO_DO2(buf,i); LZO_DO2(buf,i+2);
00771 #define LZO_DO8(buf,i) LZO_DO4(buf,i); LZO_DO4(buf,i+4);
00772 #define LZO_DO16(buf,i) LZO_DO8(buf,i); LZO_DO8(buf,i+8);
00773
00774 LZO_PUBLIC(lzo_uint32)
00775 lzo_adler32(lzo_uint32 adler, const lzo_byte *buf, lzo_uint len)
00776 {
00777 lzo_uint32 s1 = adler & 0xffff;
00778 lzo_uint32 s2 = (adler >> 16) & 0xffff;
00779 int k;
00780
00781 if (buf == NULL)
00782 return 1;
00783
00784 while (len > 0)
00785 {
00786 k = len < LZO_NMAX ? (int) len : LZO_NMAX;
00787 len -= k;
00788 if (k >= 16) do
00789 {
00790 LZO_DO16(buf,0);
00791 buf += 16;
00792 k -= 16;
00793 } while (k >= 16);
00794 if (k != 0) do
00795 {
00796 s1 += *buf++;
00797 s2 += s1;
00798 } while (--k > 0);
00799 s1 %= LZO_BASE;
00800 s2 %= LZO_BASE;
00801 }
00802 return (s2 << 16) | s1;
00803 }
00804
00805 LZO_PUBLIC(int)
00806 lzo_memcmp(const lzo_voidp s1, const lzo_voidp s2, lzo_uint len)
00807 {
00808 #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCMP)
00809 return memcmp(s1,s2,len);
00810 #else
00811 const lzo_byte *p1 = (const lzo_byte *) s1;
00812 const lzo_byte *p2 = (const lzo_byte *) s2;
00813 int d;
00814
00815 if (len > 0) do
00816 {
00817 d = *p1 - *p2;
00818 if (d != 0)
00819 return d;
00820 p1++;
00821 p2++;
00822 }
00823 while (--len > 0);
00824 return 0;
00825 #endif
00826 }
00827
00828 LZO_PUBLIC(lzo_voidp)
00829 lzo_memcpy(lzo_voidp dest, const lzo_voidp src, lzo_uint len)
00830 {
00831 #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCPY)
00832 return memcpy(dest,src,len);
00833 #else
00834 lzo_byte *p1 = (lzo_byte *) dest;
00835 const lzo_byte *p2 = (const lzo_byte *) src;
00836
00837 if (len <= 0 || p1 == p2)
00838 return dest;
00839 do
00840 *p1++ = *p2++;
00841 while (--len > 0);
00842 return dest;
00843 #endif
00844 }
00845
00846 LZO_PUBLIC(lzo_voidp)
00847 lzo_memmove(lzo_voidp dest, const lzo_voidp src, lzo_uint len)
00848 {
00849 #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMMOVE)
00850 return memmove(dest,src,len);
00851 #else
00852 lzo_byte *p1 = (lzo_byte *) dest;
00853 const lzo_byte *p2 = (const lzo_byte *) src;
00854
00855 if (len <= 0 || p1 == p2)
00856 return dest;
00857
00858 if (p1 < p2)
00859 {
00860 do
00861 *p1++ = *p2++;
00862 while (--len > 0);
00863 }
00864 else
00865 {
00866 p1 += len;
00867 p2 += len;
00868 do
00869 *--p1 = *--p2;
00870 while (--len > 0);
00871 }
00872 return dest;
00873 #endif
00874 }
00875
00876 LZO_PUBLIC(lzo_voidp)
00877 lzo_memset(lzo_voidp s, int c, lzo_uint len)
00878 {
00879 #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET)
00880 return memset(s,c,len);
00881 #else
00882 lzo_byte *p = (lzo_byte *) s;
00883
00884 if (len > 0) do
00885 *p++ = LZO_BYTE(c);
00886 while (--len > 0);
00887 return s;
00888 #endif
00889 }
00890
00891 #if 0
00892 # define IS_SIGNED(type) (((type) (1ul << (8 * sizeof(type) - 1))) < 0)
00893 # define IS_UNSIGNED(type) (((type) (1ul << (8 * sizeof(type) - 1))) > 0)
00894 #else
00895 # define IS_SIGNED(type) (((type) (-1)) < ((type) 0))
00896 # define IS_UNSIGNED(type) (((type) (-1)) > ((type) 0))
00897 #endif
00898
00899 #define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
00900
00901 static lzo_bool schedule_insns_bug(void);
00902 static lzo_bool strength_reduce_bug(int *);
00903
00904 #if 0 || defined(LZO_DEBUG)
00905 #include <stdio.h>
00906 static lzo_bool __lzo_assert_fail(const char *s, unsigned line)
00907 {
00908 #if defined(__palmos__)
00909 printf("LZO assertion failed in line %u: '%s'\n",line,s);
00910 #else
00911 fprintf(stderr,"LZO assertion failed in line %u: '%s'\n",line,s);
00912 #endif
00913 return 0;
00914 }
00915 # define __lzo_assert(x) ((x) ? 1 : __lzo_assert_fail(#x,__LINE__))
00916 #else
00917 # define __lzo_assert(x) ((x) ? 1 : 0)
00918 #endif
00919
00920 #undef COMPILE_TIME_ASSERT
00921 #if 0
00922 # define COMPILE_TIME_ASSERT(expr) r &= __lzo_assert(expr)
00923 #else
00924 # define COMPILE_TIME_ASSERT(expr) LZO_COMPILE_TIME_ASSERT(expr)
00925 #endif
00926
00927 static lzo_bool basic_integral_check(void)
00928 {
00929 lzo_bool r = 1;
00930
00931 COMPILE_TIME_ASSERT(CHAR_BIT == 8);
00932 COMPILE_TIME_ASSERT(sizeof(char) == 1);
00933 COMPILE_TIME_ASSERT(sizeof(short) >= 2);
00934 COMPILE_TIME_ASSERT(sizeof(long) >= 4);
00935 COMPILE_TIME_ASSERT(sizeof(int) >= sizeof(short));
00936 COMPILE_TIME_ASSERT(sizeof(long) >= sizeof(int));
00937
00938 COMPILE_TIME_ASSERT(sizeof(lzo_uint) == sizeof(lzo_int));
00939 COMPILE_TIME_ASSERT(sizeof(lzo_uint32) == sizeof(lzo_int32));
00940
00941 COMPILE_TIME_ASSERT(sizeof(lzo_uint32) >= 4);
00942 COMPILE_TIME_ASSERT(sizeof(lzo_uint32) >= sizeof(unsigned));
00943 #if defined(__LZO_STRICT_16BIT)
00944 COMPILE_TIME_ASSERT(sizeof(lzo_uint) == 2);
00945 #else
00946 COMPILE_TIME_ASSERT(sizeof(lzo_uint) >= 4);
00947 COMPILE_TIME_ASSERT(sizeof(lzo_uint) >= sizeof(unsigned));
00948 #endif
00949
00950 #if (USHRT_MAX == 65535u)
00951 COMPILE_TIME_ASSERT(sizeof(short) == 2);
00952 #elif (USHRT_MAX == LZO_0xffffffffL)
00953 COMPILE_TIME_ASSERT(sizeof(short) == 4);
00954 #elif (USHRT_MAX >= LZO_0xffffffffL)
00955 COMPILE_TIME_ASSERT(sizeof(short) > 4);
00956 #endif
00957 #if (UINT_MAX == 65535u)
00958 COMPILE_TIME_ASSERT(sizeof(int) == 2);
00959 #elif (UINT_MAX == LZO_0xffffffffL)
00960 COMPILE_TIME_ASSERT(sizeof(int) == 4);
00961 #elif (UINT_MAX >= LZO_0xffffffffL)
00962 COMPILE_TIME_ASSERT(sizeof(int) > 4);
00963 #endif
00964 #if (ULONG_MAX == 65535ul)
00965 COMPILE_TIME_ASSERT(sizeof(long) == 2);
00966 #elif (ULONG_MAX == LZO_0xffffffffL)
00967 COMPILE_TIME_ASSERT(sizeof(long) == 4);
00968 #elif (ULONG_MAX >= LZO_0xffffffffL)
00969 COMPILE_TIME_ASSERT(sizeof(long) > 4);
00970 #endif
00971
00972 #if defined(SIZEOF_UNSIGNED)
00973 COMPILE_TIME_ASSERT(SIZEOF_UNSIGNED == sizeof(unsigned));
00974 #endif
00975 #if defined(SIZEOF_UNSIGNED_LONG)
00976 COMPILE_TIME_ASSERT(SIZEOF_UNSIGNED_LONG == sizeof(unsigned long));
00977 #endif
00978 #if defined(SIZEOF_UNSIGNED_SHORT)
00979 COMPILE_TIME_ASSERT(SIZEOF_UNSIGNED_SHORT == sizeof(unsigned short));
00980 #endif
00981 #if !defined(__LZO_IN_MINILZO)
00982 #if defined(SIZEOF_SIZE_T)
00983 COMPILE_TIME_ASSERT(SIZEOF_SIZE_T == sizeof(size_t));
00984 #endif
00985 #endif
00986
00987 COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned char));
00988 COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned short));
00989 COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned));
00990 COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned long));
00991 COMPILE_TIME_ASSERT(IS_SIGNED(short));
00992 COMPILE_TIME_ASSERT(IS_SIGNED(int));
00993 COMPILE_TIME_ASSERT(IS_SIGNED(long));
00994
00995 COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_uint32));
00996 COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_uint));
00997 COMPILE_TIME_ASSERT(IS_SIGNED(lzo_int32));
00998 COMPILE_TIME_ASSERT(IS_SIGNED(lzo_int));
00999
01000 COMPILE_TIME_ASSERT(INT_MAX == LZO_STYPE_MAX(sizeof(int)));
01001 COMPILE_TIME_ASSERT(UINT_MAX == LZO_UTYPE_MAX(sizeof(unsigned)));
01002 COMPILE_TIME_ASSERT(LONG_MAX == LZO_STYPE_MAX(sizeof(long)));
01003 COMPILE_TIME_ASSERT(ULONG_MAX == LZO_UTYPE_MAX(sizeof(unsigned long)));
01004 COMPILE_TIME_ASSERT(SHRT_MAX == LZO_STYPE_MAX(sizeof(short)));
01005 COMPILE_TIME_ASSERT(USHRT_MAX == LZO_UTYPE_MAX(sizeof(unsigned short)));
01006 COMPILE_TIME_ASSERT(LZO_UINT32_MAX == LZO_UTYPE_MAX(sizeof(lzo_uint32)));
01007 COMPILE_TIME_ASSERT(LZO_UINT_MAX == LZO_UTYPE_MAX(sizeof(lzo_uint)));
01008 #if !defined(__LZO_IN_MINILZO)
01009 COMPILE_TIME_ASSERT(SIZE_T_MAX == LZO_UTYPE_MAX(sizeof(size_t)));
01010 #endif
01011
01012 r &= __lzo_assert(LZO_BYTE(257) == 1);
01013
01014 return r;
01015 }
01016
01017 static lzo_bool basic_ptr_check(void)
01018 {
01019 lzo_bool r = 1;
01020
01021 COMPILE_TIME_ASSERT(sizeof(char *) >= sizeof(int));
01022 COMPILE_TIME_ASSERT(sizeof(lzo_byte *) >= sizeof(char *));
01023
01024 COMPILE_TIME_ASSERT(sizeof(lzo_voidp) == sizeof(lzo_byte *));
01025 COMPILE_TIME_ASSERT(sizeof(lzo_voidp) == sizeof(lzo_voidpp));
01026 COMPILE_TIME_ASSERT(sizeof(lzo_voidp) == sizeof(lzo_bytepp));
01027 COMPILE_TIME_ASSERT(sizeof(lzo_voidp) >= sizeof(lzo_uint));
01028
01029 COMPILE_TIME_ASSERT(sizeof(lzo_ptr_t) == sizeof(lzo_voidp));
01030 COMPILE_TIME_ASSERT(sizeof(lzo_ptr_t) == sizeof(lzo_sptr_t));
01031 COMPILE_TIME_ASSERT(sizeof(lzo_ptr_t) >= sizeof(lzo_uint));
01032
01033 COMPILE_TIME_ASSERT(sizeof(lzo_ptrdiff_t) >= 4);
01034 COMPILE_TIME_ASSERT(sizeof(lzo_ptrdiff_t) >= sizeof(ptrdiff_t));
01035
01036 COMPILE_TIME_ASSERT(sizeof(ptrdiff_t) >= sizeof(size_t));
01037 COMPILE_TIME_ASSERT(sizeof(lzo_ptrdiff_t) >= sizeof(lzo_uint));
01038
01039 #if defined(SIZEOF_CHAR_P)
01040 COMPILE_TIME_ASSERT(SIZEOF_CHAR_P == sizeof(char *));
01041 #endif
01042 #if defined(SIZEOF_PTRDIFF_T)
01043 COMPILE_TIME_ASSERT(SIZEOF_PTRDIFF_T == sizeof(ptrdiff_t));
01044 #endif
01045
01046 COMPILE_TIME_ASSERT(IS_SIGNED(ptrdiff_t));
01047 COMPILE_TIME_ASSERT(IS_UNSIGNED(size_t));
01048 COMPILE_TIME_ASSERT(IS_SIGNED(lzo_ptrdiff_t));
01049 COMPILE_TIME_ASSERT(IS_SIGNED(lzo_sptr_t));
01050 COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_ptr_t));
01051 COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_moff_t));
01052
01053 return r;
01054 }
01055
01056 static lzo_bool ptr_check(void)
01057 {
01058 lzo_bool r = 1;
01059 int i;
01060 char _wrkmem[10 * sizeof(lzo_byte *) + sizeof(lzo_full_align_t)];
01061 lzo_bytep wrkmem;
01062 lzo_bytepp dict;
01063 unsigned char x[4 * sizeof(lzo_full_align_t)];
01064 long d;
01065 lzo_full_align_t a;
01066 lzo_full_align_t u;
01067
01068 for (i = 0; i < (int) sizeof(x); i++)
01069 x[i] = LZO_BYTE(i);
01070
01071 wrkmem = LZO_PTR_ALIGN_UP((lzo_byte *)_wrkmem,sizeof(lzo_full_align_t));
01072
01073 #if 0
01074 dict = (lzo_bytepp) wrkmem;
01075 #else
01076
01077 u.a_lzo_bytep = wrkmem; dict = u.a_lzo_bytepp;
01078 #endif
01079
01080 d = (long) ((const lzo_bytep) dict - (const lzo_bytep) _wrkmem);
01081 r &= __lzo_assert(d >= 0);
01082 r &= __lzo_assert(d < (long) sizeof(lzo_full_align_t));
01083
01084 memset(&a,0,sizeof(a));
01085 r &= __lzo_assert(a.a_lzo_voidp == NULL);
01086
01087 memset(&a,0xff,sizeof(a));
01088 r &= __lzo_assert(a.a_ushort == USHRT_MAX);
01089 r &= __lzo_assert(a.a_uint == UINT_MAX);
01090 r &= __lzo_assert(a.a_ulong == ULONG_MAX);
01091 r &= __lzo_assert(a.a_lzo_uint == LZO_UINT_MAX);
01092 r &= __lzo_assert(a.a_lzo_uint32 == LZO_UINT32_MAX);
01093
01094 if (r == 1)
01095 {
01096 for (i = 0; i < 8; i++)
01097 r &= __lzo_assert((const lzo_voidp) (&dict[i]) == (const lzo_voidp) (&wrkmem[i * sizeof(lzo_byte *)]));
01098 }
01099
01100 memset(&a,0,sizeof(a));
01101 r &= __lzo_assert(a.a_char_p == NULL);
01102 r &= __lzo_assert(a.a_lzo_bytep == NULL);
01103 r &= __lzo_assert(NULL == (void *)0);
01104 if (r == 1)
01105 {
01106 for (i = 0; i < 10; i++)
01107 dict[i] = wrkmem;
01108 BZERO8_PTR(dict+1,sizeof(dict[0]),8);
01109 r &= __lzo_assert(dict[0] == wrkmem);
01110 for (i = 1; i < 9; i++)
01111 r &= __lzo_assert(dict[i] == NULL);
01112 r &= __lzo_assert(dict[9] == wrkmem);
01113 }
01114
01115 if (r == 1)
01116 {
01117 unsigned k = 1;
01118 const unsigned n = (unsigned) sizeof(lzo_uint32);
01119 lzo_byte *p0;
01120 lzo_byte *p1;
01121
01122 k += __lzo_align_gap(&x[k],n);
01123 p0 = (lzo_bytep) &x[k];
01124 #if defined(PTR_LINEAR)
01125 r &= __lzo_assert((PTR_LINEAR(p0) & (n-1)) == 0);
01126 #else
01127 r &= __lzo_assert(n == 4);
01128 r &= __lzo_assert(PTR_ALIGNED_4(p0));
01129 #endif
01130
01131 r &= __lzo_assert(k >= 1);
01132 p1 = (lzo_bytep) &x[1];
01133 r &= __lzo_assert(PTR_GE(p0,p1));
01134
01135 r &= __lzo_assert(k < 1+n);
01136 p1 = (lzo_bytep) &x[1+n];
01137 r &= __lzo_assert(PTR_LT(p0,p1));
01138
01139 if (r == 1)
01140 {
01141 lzo_uint32 v0, v1;
01142 #if 0
01143 v0 = * (lzo_uint32 *) &x[k];
01144 v1 = * (lzo_uint32 *) &x[k+n];
01145 #else
01146
01147 u.a_uchar_p = &x[k];
01148 v0 = *u.a_lzo_uint32_p;
01149 u.a_uchar_p = &x[k+n];
01150 v1 = *u.a_lzo_uint32_p;
01151 #endif
01152 r &= __lzo_assert(v0 > 0);
01153 r &= __lzo_assert(v1 > 0);
01154 }
01155 }
01156
01157 return r;
01158 }
01159
01160 LZO_PUBLIC(int)
01161 _lzo_config_check(void)
01162 {
01163 lzo_bool r = 1;
01164 int i;
01165 union {
01166 lzo_uint32 a;
01167 unsigned short b;
01168 lzo_uint32 aa[4];
01169 unsigned char x[4*sizeof(lzo_full_align_t)];
01170 } u;
01171
01172 COMPILE_TIME_ASSERT( (int) ((unsigned char) ((signed char) -1)) == 255);
01173 COMPILE_TIME_ASSERT( (((unsigned char)128) << (int)(8*sizeof(int)-8)) < 0);
01174
01175 #if 0
01176 r &= __lzo_assert((const void *)&u == (const void *)&u.a);
01177 r &= __lzo_assert((const void *)&u == (const void *)&u.b);
01178 r &= __lzo_assert((const void *)&u == (const void *)&u.x[0]);
01179 r &= __lzo_assert((const void *)&u == (const void *)&u.aa[0]);
01180 #endif
01181
01182 r &= basic_integral_check();
01183 r &= basic_ptr_check();
01184 if (r != 1)
01185 return LZO_E_ERROR;
01186
01187 u.a = 0; u.b = 0;
01188 for (i = 0; i < (int) sizeof(u.x); i++)
01189 u.x[i] = LZO_BYTE(i);
01190
01191 #if defined(LZO_BYTE_ORDER)
01192 if (r == 1)
01193 {
01194 # if (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
01195 lzo_uint32 a = (lzo_uint32) (u.a & LZO_0xffffffffL);
01196 unsigned short b = (unsigned short) (u.b & 0xffff);
01197 r &= __lzo_assert(a == 0x03020100L);
01198 r &= __lzo_assert(b == 0x0100);
01199 # elif (LZO_BYTE_ORDER == LZO_BIG_ENDIAN)
01200 lzo_uint32 a = u.a >> (8 * sizeof(u.a) - 32);
01201 unsigned short b = u.b >> (8 * sizeof(u.b) - 16);
01202 r &= __lzo_assert(a == 0x00010203L);
01203 r &= __lzo_assert(b == 0x0001);
01204 # else
01205 # error "invalid LZO_BYTE_ORDER"
01206 # endif
01207 }
01208 #endif
01209
01210 #if defined(LZO_UNALIGNED_OK_2)
01211 COMPILE_TIME_ASSERT(sizeof(short) == 2);
01212 if (r == 1)
01213 {
01214 unsigned short b[4];
01215
01216 for (i = 0; i < 4; i++)
01217 b[i] = * (const unsigned short *) &u.x[i];
01218
01219 # if (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
01220 r &= __lzo_assert(b[0] == 0x0100);
01221 r &= __lzo_assert(b[1] == 0x0201);
01222 r &= __lzo_assert(b[2] == 0x0302);
01223 r &= __lzo_assert(b[3] == 0x0403);
01224 # elif (LZO_BYTE_ORDER == LZO_BIG_ENDIAN)
01225 r &= __lzo_assert(b[0] == 0x0001);
01226 r &= __lzo_assert(b[1] == 0x0102);
01227 r &= __lzo_assert(b[2] == 0x0203);
01228 r &= __lzo_assert(b[3] == 0x0304);
01229 # endif
01230 }
01231 #endif
01232
01233 #if defined(LZO_UNALIGNED_OK_4)
01234 COMPILE_TIME_ASSERT(sizeof(lzo_uint32) == 4);
01235 if (r == 1)
01236 {
01237 lzo_uint32 a[4];
01238
01239 for (i = 0; i < 4; i++)
01240 a[i] = * (const lzo_uint32 *) &u.x[i];
01241
01242 # if (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
01243 r &= __lzo_assert(a[0] == 0x03020100L);
01244 r &= __lzo_assert(a[1] == 0x04030201L);
01245 r &= __lzo_assert(a[2] == 0x05040302L);
01246 r &= __lzo_assert(a[3] == 0x06050403L);
01247 # elif (LZO_BYTE_ORDER == LZO_BIG_ENDIAN)
01248 r &= __lzo_assert(a[0] == 0x00010203L);
01249 r &= __lzo_assert(a[1] == 0x01020304L);
01250 r &= __lzo_assert(a[2] == 0x02030405L);
01251 r &= __lzo_assert(a[3] == 0x03040506L);
01252 # endif
01253 }
01254 #endif
01255
01256 #if defined(LZO_ALIGNED_OK_4)
01257 COMPILE_TIME_ASSERT(sizeof(lzo_uint32) == 4);
01258 #endif
01259
01260 COMPILE_TIME_ASSERT(lzo_sizeof_dict_t == sizeof(lzo_dict_t));
01261
01262 #if defined(__LZO_IN_MINLZO)
01263 if (r == 1)
01264 {
01265 lzo_uint32 adler;
01266 adler = lzo_adler32(0, NULL, 0);
01267 adler = lzo_adler32(adler, lzo_copyright(), 200);
01268 r &= __lzo_assert(adler == 0xc76f1751L);
01269 }
01270 #endif
01271
01272 if (r == 1)
01273 {
01274 r &= __lzo_assert(!schedule_insns_bug());
01275 }
01276
01277 if (r == 1)
01278 {
01279 static int x[3];
01280 static unsigned xn = 3;
01281 register unsigned j;
01282
01283 for (j = 0; j < xn; j++)
01284 x[j] = (int)j - 3;
01285 r &= __lzo_assert(!strength_reduce_bug(x));
01286 }
01287
01288 if (r == 1)
01289 {
01290 r &= ptr_check();
01291 }
01292
01293 return r == 1 ? LZO_E_OK : LZO_E_ERROR;
01294 }
01295
01296 static lzo_bool schedule_insns_bug(void)
01297 {
01298 #if defined(__LZO_CHECKER)
01299 return 0;
01300 #else
01301 const int clone[] = {1, 2, 0};
01302 const int *q;
01303 q = clone;
01304 return (*q) ? 0 : 1;
01305 #endif
01306 }
01307
01308 static lzo_bool strength_reduce_bug(int *x)
01309 {
01310 return x[0] != -3 || x[1] != -2 || x[2] != -1;
01311 }
01312
01313 #undef COMPILE_TIME_ASSERT
01314
01315 int __lzo_init_done = 0;
01316
01317 LZO_PUBLIC(int)
01318 __lzo_init2(unsigned v, int s1, int s2, int s3, int s4, int s5,
01319 int s6, int s7, int s8, int s9)
01320 {
01321 int r;
01322
01323 __lzo_init_done = 1;
01324
01325 if (v == 0)
01326 return LZO_E_ERROR;
01327
01328 r = (s1 == -1 || s1 == (int) sizeof(short)) &&
01329 (s2 == -1 || s2 == (int) sizeof(int)) &&
01330 (s3 == -1 || s3 == (int) sizeof(long)) &&
01331 (s4 == -1 || s4 == (int) sizeof(lzo_uint32)) &&
01332 (s5 == -1 || s5 == (int) sizeof(lzo_uint)) &&
01333 (s6 == -1 || s6 == (int) lzo_sizeof_dict_t) &&
01334 (s7 == -1 || s7 == (int) sizeof(char *)) &&
01335 (s8 == -1 || s8 == (int) sizeof(lzo_voidp)) &&
01336 (s9 == -1 || s9 == (int) sizeof(lzo_compress_t));
01337 if (!r)
01338 return LZO_E_ERROR;
01339
01340 r = _lzo_config_check();
01341 if (r != LZO_E_OK)
01342 return r;
01343
01344 return r;
01345 }
01346
01347 #if !defined(__LZO_IN_MINILZO)
01348
01349 LZO_EXTERN(int)
01350 __lzo_init(unsigned v,int s1,int s2,int s3,int s4,int s5,int s6,int s7);
01351
01352 LZO_PUBLIC(int)
01353 __lzo_init(unsigned v,int s1,int s2,int s3,int s4,int s5,int s6,int s7)
01354 {
01355 if (v == 0 || v > 0x1010)
01356 return LZO_E_ERROR;
01357 return __lzo_init2(v,s1,s2,s3,s4,s5,-1,-1,s6,s7);
01358 }
01359
01360 #endif
01361
01362 #define do_compress _lzo1x_1_do_compress
01363
01364 #define LZO_NEED_DICT_H
01365 #define D_BITS 14
01366 #define D_INDEX1(d,p) d = DM((0x21*DX3(p,5,5,6)) >> 5)
01367 #define D_INDEX2(d,p) d = (d & (D_MASK & 0x7ff)) ^ (D_HIGH | 0x1f)
01368
01369 #ifndef __LZO_CONFIG1X_H
01370 #define __LZO_CONFIG1X_H
01371
01372 #if !defined(LZO1X) && !defined(LZO1Y) && !defined(LZO1Z)
01373 # define LZO1X
01374 #endif
01375
01376 #if !defined(__LZO_IN_MINILZO)
01377 #include <lzo1x.h>
01378 #endif
01379
01380 #define LZO_EOF_CODE
01381 #undef LZO_DETERMINISTIC
01382
01383 #define M1_MAX_OFFSET 0x0400
01384 #ifndef M2_MAX_OFFSET
01385 #define M2_MAX_OFFSET 0x0800
01386 #endif
01387 #define M3_MAX_OFFSET 0x4000
01388 #define M4_MAX_OFFSET 0xbfff
01389
01390 #define MX_MAX_OFFSET (M1_MAX_OFFSET + M2_MAX_OFFSET)
01391
01392 #define M1_MIN_LEN 2
01393 #define M1_MAX_LEN 2
01394 #define M2_MIN_LEN 3
01395 #ifndef M2_MAX_LEN
01396 #define M2_MAX_LEN 8
01397 #endif
01398 #define M3_MIN_LEN 3
01399 #define M3_MAX_LEN 33
01400 #define M4_MIN_LEN 3
01401 #define M4_MAX_LEN 9
01402
01403 #define M1_MARKER 0
01404 #define M2_MARKER 64
01405 #define M3_MARKER 32
01406 #define M4_MARKER 16
01407
01408 #ifndef MIN_LOOKAHEAD
01409 #define MIN_LOOKAHEAD (M2_MAX_LEN + 1)
01410 #endif
01411
01412 #if defined(LZO_NEED_DICT_H)
01413
01414 #ifndef LZO_HASH
01415 #define LZO_HASH LZO_HASH_LZO_INCREMENTAL_B
01416 #endif
01417 #define DL_MIN_LEN M2_MIN_LEN
01418
01419 #ifndef __LZO_DICT_H
01420 #define __LZO_DICT_H
01421
01422 #ifdef __cplusplus
01423 extern "C" {
01424 #endif
01425
01426 #if !defined(D_BITS) && defined(DBITS)
01427 # define D_BITS DBITS
01428 #endif
01429 #if !defined(D_BITS)
01430 # error "D_BITS is not defined"
01431 #endif
01432 #if (D_BITS < 16)
01433 # define D_SIZE LZO_SIZE(D_BITS)
01434 # define D_MASK LZO_MASK(D_BITS)
01435 #else
01436 # define D_SIZE LZO_USIZE(D_BITS)
01437 # define D_MASK LZO_UMASK(D_BITS)
01438 #endif
01439 #define D_HIGH ((D_MASK >> 1) + 1)
01440
01441 #if !defined(DD_BITS)
01442 # define DD_BITS 0
01443 #endif
01444 #define DD_SIZE LZO_SIZE(DD_BITS)
01445 #define DD_MASK LZO_MASK(DD_BITS)
01446
01447 #if !defined(DL_BITS)
01448 # define DL_BITS (D_BITS - DD_BITS)
01449 #endif
01450 #if (DL_BITS < 16)
01451 # define DL_SIZE LZO_SIZE(DL_BITS)
01452 # define DL_MASK LZO_MASK(DL_BITS)
01453 #else
01454 # define DL_SIZE LZO_USIZE(DL_BITS)
01455 # define DL_MASK LZO_UMASK(DL_BITS)
01456 #endif
01457
01458 #if (D_BITS != DL_BITS + DD_BITS)
01459 # error "D_BITS does not match"
01460 #endif
01461 #if (D_BITS < 8 || D_BITS > 18)
01462 # error "invalid D_BITS"
01463 #endif
01464 #if (DL_BITS < 8 || DL_BITS > 20)
01465 # error "invalid DL_BITS"
01466 #endif
01467 #if (DD_BITS < 0 || DD_BITS > 6)
01468 # error "invalid DD_BITS"
01469 #endif
01470
01471 #if !defined(DL_MIN_LEN)
01472 # define DL_MIN_LEN 3
01473 #endif
01474 #if !defined(DL_SHIFT)
01475 # define DL_SHIFT ((DL_BITS + (DL_MIN_LEN - 1)) / DL_MIN_LEN)
01476 #endif
01477
01478 #define LZO_HASH_GZIP 1
01479 #define LZO_HASH_GZIP_INCREMENTAL 2
01480 #define LZO_HASH_LZO_INCREMENTAL_A 3
01481 #define LZO_HASH_LZO_INCREMENTAL_B 4
01482
01483 #if !defined(LZO_HASH)
01484 # error "choose a hashing strategy"
01485 #endif
01486
01487 #if (DL_MIN_LEN == 3)
01488 # define _DV2_A(p,shift1,shift2) \
01489 (((( (lzo_uint32)((p)[0]) << shift1) ^ (p)[1]) << shift2) ^ (p)[2])
01490 # define _DV2_B(p,shift1,shift2) \
01491 (((( (lzo_uint32)((p)[2]) << shift1) ^ (p)[1]) << shift2) ^ (p)[0])
01492 # define _DV3_B(p,shift1,shift2,shift3) \
01493 ((_DV2_B((p)+1,shift1,shift2) << (shift3)) ^ (p)[0])
01494 #elif (DL_MIN_LEN == 2)
01495 # define _DV2_A(p,shift1,shift2) \
01496 (( (lzo_uint32)(p[0]) << shift1) ^ p[1])
01497 # define _DV2_B(p,shift1,shift2) \
01498 (( (lzo_uint32)(p[1]) << shift1) ^ p[2])
01499 #else
01500 # error "invalid DL_MIN_LEN"
01501 #endif
01502 #define _DV_A(p,shift) _DV2_A(p,shift,shift)
01503 #define _DV_B(p,shift) _DV2_B(p,shift,shift)
01504 #define DA2(p,s1,s2) \
01505 (((((lzo_uint32)((p)[2]) << (s2)) + (p)[1]) << (s1)) + (p)[0])
01506 #define DS2(p,s1,s2) \
01507 (((((lzo_uint32)((p)[2]) << (s2)) - (p)[1]) << (s1)) - (p)[0])
01508 #define DX2(p,s1,s2) \
01509 (((((lzo_uint32)((p)[2]) << (s2)) ^ (p)[1]) << (s1)) ^ (p)[0])
01510 #define DA3(p,s1,s2,s3) ((DA2((p)+1,s2,s3) << (s1)) + (p)[0])
01511 #define DS3(p,s1,s2,s3) ((DS2((p)+1,s2,s3) << (s1)) - (p)[0])
01512 #define DX3(p,s1,s2,s3) ((DX2((p)+1,s2,s3) << (s1)) ^ (p)[0])
01513 #define DMS(v,s) ((lzo_uint) (((v) & (D_MASK >> (s))) << (s)))
01514 #define DM(v) DMS(v,0)
01515
01516 #if (LZO_HASH == LZO_HASH_GZIP)
01517 # define _DINDEX(dv,p) (_DV_A((p),DL_SHIFT))
01518
01519 #elif (LZO_HASH == LZO_HASH_GZIP_INCREMENTAL)
01520 # define __LZO_HASH_INCREMENTAL
01521 # define DVAL_FIRST(dv,p) dv = _DV_A((p),DL_SHIFT)
01522 # define DVAL_NEXT(dv,p) dv = (((dv) << DL_SHIFT) ^ p[2])
01523 # define _DINDEX(dv,p) (dv)
01524 # define DVAL_LOOKAHEAD DL_MIN_LEN
01525
01526 #elif (LZO_HASH == LZO_HASH_LZO_INCREMENTAL_A)
01527 # define __LZO_HASH_INCREMENTAL
01528 # define DVAL_FIRST(dv,p) dv = _DV_A((p),5)
01529 # define DVAL_NEXT(dv,p) \
01530 dv ^= (lzo_uint32)(p[-1]) << (2*5); dv = (((dv) << 5) ^ p[2])
01531 # define _DINDEX(dv,p) ((0x9f5f * (dv)) >> 5)
01532 # define DVAL_LOOKAHEAD DL_MIN_LEN
01533
01534 #elif (LZO_HASH == LZO_HASH_LZO_INCREMENTAL_B)
01535 # define __LZO_HASH_INCREMENTAL
01536 # define DVAL_FIRST(dv,p) dv = _DV_B((p),5)
01537 # define DVAL_NEXT(dv,p) \
01538 dv ^= p[-1]; dv = (((dv) >> 5) ^ ((lzo_uint32)(p[2]) << (2*5)))
01539 # define _DINDEX(dv,p) ((0x9f5f * (dv)) >> 5)
01540 # define DVAL_LOOKAHEAD DL_MIN_LEN
01541
01542 #else
01543 # error "choose a hashing strategy"
01544 #endif
01545
01546 #ifndef DINDEX
01547 #define DINDEX(dv,p) ((lzo_uint)((_DINDEX(dv,p)) & DL_MASK) << DD_BITS)
01548 #endif
01549 #if !defined(DINDEX1) && defined(D_INDEX1)
01550 #define DINDEX1 D_INDEX1
01551 #endif
01552 #if !defined(DINDEX2) && defined(D_INDEX2)
01553 #define DINDEX2 D_INDEX2
01554 #endif
01555
01556 #if !defined(__LZO_HASH_INCREMENTAL)
01557 # define DVAL_FIRST(dv,p) ((void) 0)
01558 # define DVAL_NEXT(dv,p) ((void) 0)
01559 # define DVAL_LOOKAHEAD 0
01560 #endif
01561
01562 #if !defined(DVAL_ASSERT)
01563 #if defined(__LZO_HASH_INCREMENTAL) && !defined(NDEBUG)
01564 static void DVAL_ASSERT(lzo_uint32 dv, const lzo_byte *p)
01565 {
01566 lzo_uint32 df;
01567 DVAL_FIRST(df,(p));
01568 assert(DINDEX(dv,p) == DINDEX(df,p));
01569 }
01570 #else
01571 # define DVAL_ASSERT(dv,p) ((void) 0)
01572 #endif
01573 #endif
01574
01575 #if defined(LZO_DICT_USE_PTR)
01576 # define DENTRY(p,in) (p)
01577 # define GINDEX(m_pos,m_off,dict,dindex,in) m_pos = dict[dindex]
01578 #else
01579 # define DENTRY(p,in) ((lzo_uint) ((p)-(in)))
01580 # define GINDEX(m_pos,m_off,dict,dindex,in) m_off = dict[dindex]
01581 #endif
01582
01583 #if (DD_BITS == 0)
01584
01585 # define UPDATE_D(dict,drun,dv,p,in) dict[ DINDEX(dv,p) ] = DENTRY(p,in)
01586 # define UPDATE_I(dict,drun,index,p,in) dict[index] = DENTRY(p,in)
01587 # define UPDATE_P(ptr,drun,p,in) (ptr)[0] = DENTRY(p,in)
01588
01589 #else
01590
01591 # define UPDATE_D(dict,drun,dv,p,in) \
01592 dict[ DINDEX(dv,p) + drun++ ] = DENTRY(p,in); drun &= DD_MASK
01593 # define UPDATE_I(dict,drun,index,p,in) \
01594 dict[ (index) + drun++ ] = DENTRY(p,in); drun &= DD_MASK
01595 # define UPDATE_P(ptr,drun,p,in) \
01596 (ptr) [ drun++ ] = DENTRY(p,in); drun &= DD_MASK
01597
01598 #endif
01599
01600 #if defined(LZO_DICT_USE_PTR)
01601
01602 #define LZO_CHECK_MPOS_DET(m_pos,m_off,in,ip,max_offset) \
01603 (m_pos == NULL || (m_off = (lzo_moff_t) (ip - m_pos)) > max_offset)
01604
01605 #define LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
01606 (BOUNDS_CHECKING_OFF_IN_EXPR( \
01607 (PTR_LT(m_pos,in) || \
01608 (m_off = (lzo_moff_t) PTR_DIFF(ip,m_pos)) <= 0 || \
01609 m_off > max_offset) ))
01610
01611 #else
01612
01613 #define LZO_CHECK_MPOS_DET(m_pos,m_off,in,ip,max_offset) \
01614 (m_off == 0 || \
01615 ((m_off = (lzo_moff_t) ((ip)-(in)) - m_off) > max_offset) || \
01616 (m_pos = (ip) - (m_off), 0) )
01617
01618 #define LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
01619 ((lzo_moff_t) ((ip)-(in)) <= m_off || \
01620 ((m_off = (lzo_moff_t) ((ip)-(in)) - m_off) > max_offset) || \
01621 (m_pos = (ip) - (m_off), 0) )
01622
01623 #endif
01624
01625 #if defined(LZO_DETERMINISTIC)
01626 # define LZO_CHECK_MPOS LZO_CHECK_MPOS_DET
01627 #else
01628 # define LZO_CHECK_MPOS LZO_CHECK_MPOS_NON_DET
01629 #endif
01630
01631 #ifdef __cplusplus
01632 }
01633 #endif
01634
01635 #endif
01636
01637 #endif
01638
01639 #endif
01640
01641 #define DO_COMPRESS lzo1x_1_compress
01642
01643 static
01644 lzo_uint do_compress ( const lzo_byte *in , lzo_uint in_len,
01645 lzo_byte *out, lzo_uintp out_len,
01646 lzo_voidp wrkmem )
01647 {
01648 #if 0 && defined(__GNUC__) && defined(__i386__)
01649 register const lzo_byte *ip __asm__("%esi");
01650 #else
01651 register const lzo_byte *ip;
01652 #endif
01653 lzo_byte *op;
01654 const lzo_byte * const in_end = in + in_len;
01655 const lzo_byte * const ip_end = in + in_len - M2_MAX_LEN - 5;
01656 const lzo_byte *ii;
01657 lzo_dict_p const dict = (lzo_dict_p) wrkmem;
01658
01659 op = out;
01660 ip = in;
01661 ii = ip;
01662
01663 ip += 4;
01664 for (;;)
01665 {
01666 #if 0 && defined(__GNUC__) && defined(__i386__)
01667 register const lzo_byte *m_pos __asm__("%edi");
01668 #else
01669 register const lzo_byte *m_pos;
01670 #endif
01671 lzo_moff_t m_off;
01672 lzo_uint m_len;
01673 lzo_uint dindex;
01674
01675 DINDEX1(dindex,ip);
01676 GINDEX(m_pos,m_off,dict,dindex,in);
01677 if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
01678 goto literal;
01679 #if 1
01680 if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
01681 goto try_match;
01682 DINDEX2(dindex,ip);
01683 #endif
01684 GINDEX(m_pos,m_off,dict,dindex,in);
01685 if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
01686 goto literal;
01687 if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
01688 goto try_match;
01689 goto literal;
01690
01691 try_match:
01692 #if 1 && defined(LZO_UNALIGNED_OK_2)
01693 if (* (const lzo_ushortp) m_pos != * (const lzo_ushortp) ip)
01694 #else
01695 if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
01696 #endif
01697 {
01698 }
01699 else
01700 {
01701 if (m_pos[2] == ip[2])
01702 {
01703 #if 0
01704 if (m_off <= M2_MAX_OFFSET)
01705 goto match;
01706 if (lit <= 3)
01707 goto match;
01708 if (lit == 3)
01709 {
01710 assert(op - 2 > out); op[-2] |= LZO_BYTE(3);
01711 *op++ = *ii++; *op++ = *ii++; *op++ = *ii++;
01712 goto code_match;
01713 }
01714 if (m_pos[3] == ip[3])
01715 #endif
01716 goto match;
01717 }
01718 else
01719 {
01720 #if 0
01721 #if 0
01722 if (m_off <= M1_MAX_OFFSET && lit > 0 && lit <= 3)
01723 #else
01724 if (m_off <= M1_MAX_OFFSET && lit == 3)
01725 #endif
01726 {
01727 register lzo_uint t;
01728
01729 t = lit;
01730 assert(op - 2 > out); op[-2] |= LZO_BYTE(t);
01731 do *op++ = *ii++; while (--t > 0);
01732 assert(ii == ip);
01733 m_off -= 1;
01734 *op++ = LZO_BYTE(M1_MARKER | ((m_off & 3) << 2));
01735 *op++ = LZO_BYTE(m_off >> 2);
01736 ip += 2;
01737 goto match_done;
01738 }
01739 #endif
01740 }
01741 }
01742
01743 literal:
01744 UPDATE_I(dict,0,dindex,ip,in);
01745 ++ip;
01746 if (ip >= ip_end)
01747 break;
01748 continue;
01749
01750 match:
01751 UPDATE_I(dict,0,dindex,ip,in);
01752 if (pd(ip,ii) > 0)
01753 {
01754 register lzo_uint t = pd(ip,ii);
01755
01756 if (t <= 3)
01757 {
01758 assert(op - 2 > out);
01759 op[-2] |= LZO_BYTE(t);
01760 }
01761 else if (t <= 18)
01762 *op++ = LZO_BYTE(t - 3);
01763 else
01764 {
01765 register lzo_uint tt = t - 18;
01766
01767 *op++ = 0;
01768 while (tt > 255)
01769 {
01770 tt -= 255;
01771 *op++ = 0;
01772 }
01773 assert(tt > 0);
01774 *op++ = LZO_BYTE(tt);
01775 }
01776 do *op++ = *ii++; while (--t > 0);
01777 }
01778
01779 assert(ii == ip);
01780 ip += 3;
01781 if (m_pos[3] != *ip++ || m_pos[4] != *ip++ || m_pos[5] != *ip++ ||
01782 m_pos[6] != *ip++ || m_pos[7] != *ip++ || m_pos[8] != *ip++
01783 #ifdef LZO1Y
01784 || m_pos[ 9] != *ip++ || m_pos[10] != *ip++ || m_pos[11] != *ip++
01785 || m_pos[12] != *ip++ || m_pos[13] != *ip++ || m_pos[14] != *ip++
01786 #endif
01787 )
01788 {
01789 --ip;
01790 m_len = ip - ii;
01791 assert(m_len >= 3); assert(m_len <= M2_MAX_LEN);
01792
01793 if (m_off <= M2_MAX_OFFSET)
01794 {
01795 m_off -= 1;
01796 #if defined(LZO1X)
01797 *op++ = LZO_BYTE(((m_len - 1) << 5) | ((m_off & 7) << 2));
01798 *op++ = LZO_BYTE(m_off >> 3);
01799 #elif defined(LZO1Y)
01800 *op++ = LZO_BYTE(((m_len + 1) << 4) | ((m_off & 3) << 2));
01801 *op++ = LZO_BYTE(m_off >> 2);
01802 #endif
01803 }
01804 else if (m_off <= M3_MAX_OFFSET)
01805 {
01806 m_off -= 1;
01807 *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
01808 goto m3_m4_offset;
01809 }
01810 else
01811 #if defined(LZO1X)
01812 {
01813 m_off -= 0x4000;
01814 assert(m_off > 0); assert(m_off <= 0x7fff);
01815 *op++ = LZO_BYTE(M4_MARKER |
01816 ((m_off & 0x4000) >> 11) | (m_len - 2));
01817 goto m3_m4_offset;
01818 }
01819 #elif defined(LZO1Y)
01820 goto m4_match;
01821 #endif
01822 }
01823 else
01824 {
01825 {
01826 const lzo_byte *end = in_end;
01827 const lzo_byte *m = m_pos + M2_MAX_LEN + 1;
01828 while (ip < end && *m == *ip)
01829 m++, ip++;
01830 m_len = (ip - ii);
01831 }
01832 assert(m_len > M2_MAX_LEN);
01833
01834 if (m_off <= M3_MAX_OFFSET)
01835 {
01836 m_off -= 1;
01837 if (m_len <= 33)
01838 *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
01839 else
01840 {
01841 m_len -= 33;
01842 *op++ = M3_MARKER | 0;
01843 goto m3_m4_len;
01844 }
01845 }
01846 else
01847 {
01848 #if defined(LZO1Y)
01849 m4_match:
01850 #endif
01851 m_off -= 0x4000;
01852 assert(m_off > 0); assert(m_off <= 0x7fff);
01853 if (m_len <= M4_MAX_LEN)
01854 *op++ = LZO_BYTE(M4_MARKER |
01855 ((m_off & 0x4000) >> 11) | (m_len - 2));
01856 else
01857 {
01858 m_len -= M4_MAX_LEN;
01859 *op++ = LZO_BYTE(M4_MARKER | ((m_off & 0x4000) >> 11));
01860 m3_m4_len:
01861 while (m_len > 255)
01862 {
01863 m_len -= 255;
01864 *op++ = 0;
01865 }
01866 assert(m_len > 0);
01867 *op++ = LZO_BYTE(m_len);
01868 }
01869 }
01870
01871 m3_m4_offset:
01872 *op++ = LZO_BYTE((m_off & 63) << 2);
01873 *op++ = LZO_BYTE(m_off >> 6);
01874 }
01875
01876 #if 0
01877 match_done:
01878 #endif
01879 ii = ip;
01880 if (ip >= ip_end)
01881 break;
01882 }
01883
01884 *out_len = op - out;
01885 return pd(in_end,ii);
01886 }
01887
01888 LZO_PUBLIC(int)
01889 DO_COMPRESS ( const lzo_byte *in , lzo_uint in_len,
01890 lzo_byte *out, lzo_uintp out_len,
01891 lzo_voidp wrkmem )
01892 {
01893 lzo_byte *op = out;
01894 lzo_uint t;
01895
01896 #if defined(__LZO_QUERY_COMPRESS)
01897 if (__LZO_IS_COMPRESS_QUERY(in,in_len,out,out_len,wrkmem))
01898 return __LZO_QUERY_COMPRESS(in,in_len,out,out_len,wrkmem,D_SIZE,lzo_sizeof(lzo_dict_t));
01899 #endif
01900
01901 if (in_len <= M2_MAX_LEN + 5)
01902 t = in_len;
01903 else
01904 {
01905 t = do_compress(in,in_len,op,out_len,wrkmem);
01906 op += *out_len;
01907 }
01908
01909 if (t > 0)
01910 {
01911 const lzo_byte *ii = in + in_len - t;
01912
01913 if (op == out && t <= 238)
01914 *op++ = LZO_BYTE(17 + t);
01915 else if (t <= 3)
01916 op[-2] |= LZO_BYTE(t);
01917 else if (t <= 18)
01918 *op++ = LZO_BYTE(t - 3);
01919 else
01920 {
01921 lzo_uint tt = t - 18;
01922
01923 *op++ = 0;
01924 while (tt > 255)
01925 {
01926 tt -= 255;
01927 *op++ = 0;
01928 }
01929 assert(tt > 0);
01930 *op++ = LZO_BYTE(tt);
01931 }
01932 do *op++ = *ii++; while (--t > 0);
01933 }
01934
01935 *op++ = M4_MARKER | 1;
01936 *op++ = 0;
01937 *op++ = 0;
01938
01939 *out_len = op - out;
01940 return LZO_E_OK;
01941 }
01942
01943 #undef do_compress
01944 #undef DO_COMPRESS
01945 #undef LZO_HASH
01946
01947 #undef LZO_TEST_DECOMPRESS_OVERRUN
01948 #undef LZO_TEST_DECOMPRESS_OVERRUN_INPUT
01949 #undef LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT
01950 #undef LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND
01951 #undef DO_DECOMPRESS
01952 #define DO_DECOMPRESS lzo1x_decompress
01953
01954 #if defined(LZO_TEST_DECOMPRESS_OVERRUN)
01955 # if !defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
01956 # define LZO_TEST_DECOMPRESS_OVERRUN_INPUT 2
01957 # endif
01958 # if !defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
01959 # define LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT 2
01960 # endif
01961 # if !defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
01962 # define LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND
01963 # endif
01964 #endif
01965
01966 #undef TEST_IP
01967 #undef TEST_OP
01968 #undef TEST_LOOKBEHIND
01969 #undef NEED_IP
01970 #undef NEED_OP
01971 #undef HAVE_TEST_IP
01972 #undef HAVE_TEST_OP
01973 #undef HAVE_NEED_IP
01974 #undef HAVE_NEED_OP
01975 #undef HAVE_ANY_IP
01976 #undef HAVE_ANY_OP
01977
01978 #if defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
01979 # if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 1)
01980 # define TEST_IP (ip < ip_end)
01981 # endif
01982 # if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 2)
01983 # define NEED_IP(x) \
01984 if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x)) goto input_overrun
01985 # endif
01986 #endif
01987
01988 #if defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
01989 # if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 1)
01990 # define TEST_OP (op <= op_end)
01991 # endif
01992 # if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 2)
01993 # undef TEST_OP
01994 # define NEED_OP(x) \
01995 if ((lzo_uint)(op_end - op) < (lzo_uint)(x)) goto output_overrun
01996 # endif
01997 #endif
01998
01999 #if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
02000 # define TEST_LOOKBEHIND(m_pos,out) if (m_pos < out) goto lookbehind_overrun
02001 #else
02002 # define TEST_LOOKBEHIND(m_pos,op) ((void) 0)
02003 #endif
02004
02005 #if !defined(LZO_EOF_CODE) && !defined(TEST_IP)
02006 # define TEST_IP (ip < ip_end)
02007 #endif
02008
02009 #if defined(TEST_IP)
02010 # define HAVE_TEST_IP
02011 #else
02012 # define TEST_IP 1
02013 #endif
02014 #if defined(TEST_OP)
02015 # define HAVE_TEST_OP
02016 #else
02017 # define TEST_OP 1
02018 #endif
02019
02020 #if defined(NEED_IP)
02021 # define HAVE_NEED_IP
02022 #else
02023 # define NEED_IP(x) ((void) 0)
02024 #endif
02025 #if defined(NEED_OP)
02026 # define HAVE_NEED_OP
02027 #else
02028 # define NEED_OP(x) ((void) 0)
02029 #endif
02030
02031 #if defined(HAVE_TEST_IP) || defined(HAVE_NEED_IP)
02032 # define HAVE_ANY_IP
02033 #endif
02034 #if defined(HAVE_TEST_OP) || defined(HAVE_NEED_OP)
02035 # define HAVE_ANY_OP
02036 #endif
02037
02038 #undef __COPY4
02039 #define __COPY4(dst,src) * (lzo_uint32p)(dst) = * (const lzo_uint32p)(src)
02040
02041 #undef COPY4
02042 #if defined(LZO_UNALIGNED_OK_4)
02043 # define COPY4(dst,src) __COPY4(dst,src)
02044 #elif defined(LZO_ALIGNED_OK_4)
02045 # define COPY4(dst,src) __COPY4((lzo_ptr_t)(dst),(lzo_ptr_t)(src))
02046 #endif
02047
02048 #if defined(DO_DECOMPRESS)
02049 LZO_PUBLIC(int)
02050 DO_DECOMPRESS ( const lzo_byte *in , lzo_uint in_len,
02051 lzo_byte *out, lzo_uintp out_len,
02052 lzo_voidp wrkmem )
02053 #endif
02054 {
02055 register lzo_byte *op;
02056 register const lzo_byte *ip;
02057 register lzo_uint t;
02058 #if defined(COPY_DICT)
02059 lzo_uint m_off;
02060 const lzo_byte *dict_end;
02061 #else
02062 register const lzo_byte *m_pos;
02063 #endif
02064
02065 const lzo_byte * const ip_end = in + in_len;
02066 #if defined(HAVE_ANY_OP)
02067 lzo_byte * const op_end = out + *out_len;
02068 #endif
02069 #if defined(LZO1Z)
02070 lzo_uint last_m_off = 0;
02071 #endif
02072
02073 LZO_UNUSED(wrkmem);
02074
02075 #if defined(__LZO_QUERY_DECOMPRESS)
02076 if (__LZO_IS_DECOMPRESS_QUERY(in,in_len,out,out_len,wrkmem))
02077 return __LZO_QUERY_DECOMPRESS(in,in_len,out,out_len,wrkmem,0,0);
02078 #endif
02079
02080 #if defined(COPY_DICT)
02081 if (dict)
02082 {
02083 if (dict_len > M4_MAX_OFFSET)
02084 {
02085 dict += dict_len - M4_MAX_OFFSET;
02086 dict_len = M4_MAX_OFFSET;
02087 }
02088 dict_end = dict + dict_len;
02089 }
02090 else
02091 {
02092 dict_len = 0;
02093 dict_end = NULL;
02094 }
02095 #endif
02096
02097 *out_len = 0;
02098
02099 op = out;
02100 ip = in;
02101
02102 if (*ip > 17)
02103 {
02104 t = *ip++ - 17;
02105 if (t < 4)
02106 goto match_next;
02107 assert(t > 0); NEED_OP(t); NEED_IP(t+1);
02108 do *op++ = *ip++; while (--t > 0);
02109 goto first_literal_run;
02110 }
02111
02112 while (TEST_IP && TEST_OP)
02113 {
02114 t = *ip++;
02115 if (t >= 16)
02116 goto match;
02117 if (t == 0)
02118 {
02119 NEED_IP(1);
02120 while (*ip == 0)
02121 {
02122 t += 255;
02123 ip++;
02124 NEED_IP(1);
02125 }
02126 t += 15 + *ip++;
02127 }
02128 assert(t > 0); NEED_OP(t+3); NEED_IP(t+4);
02129 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
02130 #if !defined(LZO_UNALIGNED_OK_4)
02131 if (PTR_ALIGNED2_4(op,ip))
02132 {
02133 #endif
02134 COPY4(op,ip);
02135 op += 4; ip += 4;
02136 if (--t > 0)
02137 {
02138 if (t >= 4)
02139 {
02140 do {
02141 COPY4(op,ip);
02142 op += 4; ip += 4; t -= 4;
02143 } while (t >= 4);
02144 if (t > 0) do *op++ = *ip++; while (--t > 0);
02145 }
02146 else
02147 do *op++ = *ip++; while (--t > 0);
02148 }
02149 #if !defined(LZO_UNALIGNED_OK_4)
02150 }
02151 else
02152 #endif
02153 #endif
02154 #if !defined(LZO_UNALIGNED_OK_4)
02155 {
02156 *op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
02157 do *op++ = *ip++; while (--t > 0);
02158 }
02159 #endif
02160
02161 first_literal_run:
02162
02163 t = *ip++;
02164 if (t >= 16)
02165 goto match;
02166 #if defined(COPY_DICT)
02167 #if defined(LZO1Z)
02168 m_off = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
02169 last_m_off = m_off;
02170 #else
02171 m_off = (1 + M2_MAX_OFFSET) + (t >> 2) + (*ip++ << 2);
02172 #endif
02173 NEED_OP(3);
02174 t = 3; COPY_DICT(t,m_off)
02175 #else
02176 #if defined(LZO1Z)
02177 t = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
02178 m_pos = op - t;
02179 last_m_off = t;
02180 #else
02181 m_pos = op - (1 + M2_MAX_OFFSET);
02182 m_pos -= t >> 2;
02183 m_pos -= *ip++ << 2;
02184 #endif
02185 TEST_LOOKBEHIND(m_pos,out); NEED_OP(3);
02186 *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos;
02187 #endif
02188 goto match_done;
02189
02190 while (TEST_IP && TEST_OP)
02191 {
02192 match:
02193 if (t >= 64)
02194 {
02195 #if defined(COPY_DICT)
02196 #if defined(LZO1X)
02197 m_off = 1 + ((t >> 2) & 7) + (*ip++ << 3);
02198 t = (t >> 5) - 1;
02199 #elif defined(LZO1Y)
02200 m_off = 1 + ((t >> 2) & 3) + (*ip++ << 2);
02201 t = (t >> 4) - 3;
02202 #elif defined(LZO1Z)
02203 m_off = t & 0x1f;
02204 if (m_off >= 0x1c)
02205 m_off = last_m_off;
02206 else
02207 {
02208 m_off = 1 + (m_off << 6) + (*ip++ >> 2);
02209 last_m_off = m_off;
02210 }
02211 t = (t >> 5) - 1;
02212 #endif
02213 #else
02214 #if defined(LZO1X)
02215 m_pos = op - 1;
02216 m_pos -= (t >> 2) & 7;
02217 m_pos -= *ip++ << 3;
02218 t = (t >> 5) - 1;
02219 #elif defined(LZO1Y)
02220 m_pos = op - 1;
02221 m_pos -= (t >> 2) & 3;
02222 m_pos -= *ip++ << 2;
02223 t = (t >> 4) - 3;
02224 #elif defined(LZO1Z)
02225 {
02226 lzo_uint off = t & 0x1f;
02227 m_pos = op;
02228 if (off >= 0x1c)
02229 {
02230 assert(last_m_off > 0);
02231 m_pos -= last_m_off;
02232 }
02233 else
02234 {
02235 off = 1 + (off << 6) + (*ip++ >> 2);
02236 m_pos -= off;
02237 last_m_off = off;
02238 }
02239 }
02240 t = (t >> 5) - 1;
02241 #endif
02242 TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t+3-1);
02243 goto copy_match;
02244 #endif
02245 }
02246 else if (t >= 32)
02247 {
02248 t &= 31;
02249 if (t == 0)
02250 {
02251 NEED_IP(1);
02252 while (*ip == 0)
02253 {
02254 t += 255;
02255 ip++;
02256 NEED_IP(1);
02257 }
02258 t += 31 + *ip++;
02259 }
02260 #if defined(COPY_DICT)
02261 #if defined(LZO1Z)
02262 m_off = 1 + (ip[0] << 6) + (ip[1] >> 2);
02263 last_m_off = m_off;
02264 #else
02265 m_off = 1 + (ip[0] >> 2) + (ip[1] << 6);
02266 #endif
02267 #else
02268 #if defined(LZO1Z)
02269 {
02270 lzo_uint off = 1 + (ip[0] << 6) + (ip[1] >> 2);
02271 m_pos = op - off;
02272 last_m_off = off;
02273 }
02274 #elif defined(LZO_UNALIGNED_OK_2) && (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
02275 m_pos = op - 1;
02276 m_pos -= (* (const lzo_ushortp) ip) >> 2;
02277 #else
02278 m_pos = op - 1;
02279 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
02280 #endif
02281 #endif
02282 ip += 2;
02283 }
02284 else if (t >= 16)
02285 {
02286 #if defined(COPY_DICT)
02287 m_off = (t & 8) << 11;
02288 #else
02289 m_pos = op;
02290 m_pos -= (t & 8) << 11;
02291 #endif
02292 t &= 7;
02293 if (t == 0)
02294 {
02295 NEED_IP(1);
02296 while (*ip == 0)
02297 {
02298 t += 255;
02299 ip++;
02300 NEED_IP(1);
02301 }
02302 t += 7 + *ip++;
02303 }
02304 #if defined(COPY_DICT)
02305 #if defined(LZO1Z)
02306 m_off += (ip[0] << 6) + (ip[1] >> 2);
02307 #else
02308 m_off += (ip[0] >> 2) + (ip[1] << 6);
02309 #endif
02310 ip += 2;
02311 if (m_off == 0)
02312 goto eof_found;
02313 m_off += 0x4000;
02314 #if defined(LZO1Z)
02315 last_m_off = m_off;
02316 #endif
02317 #else
02318 #if defined(LZO1Z)
02319 m_pos -= (ip[0] << 6) + (ip[1] >> 2);
02320 #elif defined(LZO_UNALIGNED_OK_2) && (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
02321 m_pos -= (* (const lzo_ushortp) ip) >> 2;
02322 #else
02323 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
02324 #endif
02325 ip += 2;
02326 if (m_pos == op)
02327 goto eof_found;
02328 m_pos -= 0x4000;
02329 #if defined(LZO1Z)
02330 last_m_off = op - m_pos;
02331 #endif
02332 #endif
02333 }
02334 else
02335 {
02336 #if defined(COPY_DICT)
02337 #if defined(LZO1Z)
02338 m_off = 1 + (t << 6) + (*ip++ >> 2);
02339 last_m_off = m_off;
02340 #else
02341 m_off = 1 + (t >> 2) + (*ip++ << 2);
02342 #endif
02343 NEED_OP(2);
02344 t = 2; COPY_DICT(t,m_off)
02345 #else
02346 #if defined(LZO1Z)
02347 t = 1 + (t << 6) + (*ip++ >> 2);
02348 m_pos = op - t;
02349 last_m_off = t;
02350 #else
02351 m_pos = op - 1;
02352 m_pos -= t >> 2;
02353 m_pos -= *ip++ << 2;
02354 #endif
02355 TEST_LOOKBEHIND(m_pos,out); NEED_OP(2);
02356 *op++ = *m_pos++; *op++ = *m_pos;
02357 #endif
02358 goto match_done;
02359 }
02360
02361 #if defined(COPY_DICT)
02362
02363 NEED_OP(t+3-1);
02364 t += 3-1; COPY_DICT(t,m_off)
02365
02366 #else
02367
02368 TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t+3-1);
02369 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
02370 #if !defined(LZO_UNALIGNED_OK_4)
02371 if (t >= 2 * 4 - (3 - 1) && PTR_ALIGNED2_4(op,m_pos))
02372 {
02373 assert((op - m_pos) >= 4);
02374 #else
02375 if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
02376 {
02377 #endif
02378 COPY4(op,m_pos);
02379 op += 4; m_pos += 4; t -= 4 - (3 - 1);
02380 do {
02381 COPY4(op,m_pos);
02382 op += 4; m_pos += 4; t -= 4;
02383 } while (t >= 4);
02384 if (t > 0) do *op++ = *m_pos++; while (--t > 0);
02385 }
02386 else
02387 #endif
02388 {
02389 copy_match:
02390 *op++ = *m_pos++; *op++ = *m_pos++;
02391 do *op++ = *m_pos++; while (--t > 0);
02392 }
02393
02394 #endif
02395
02396 match_done:
02397 #if defined(LZO1Z)
02398 t = ip[-1] & 3;
02399 #else
02400 t = ip[-2] & 3;
02401 #endif
02402 if (t == 0)
02403 break;
02404
02405 match_next:
02406 assert(t > 0); NEED_OP(t); NEED_IP(t+1);
02407 do *op++ = *ip++; while (--t > 0);
02408 t = *ip++;
02409 }
02410 }
02411
02412 #if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
02413 *out_len = op - out;
02414 return LZO_E_EOF_NOT_FOUND;
02415 #endif
02416
02417 eof_found:
02418 assert(t == 1);
02419 *out_len = op - out;
02420 return (ip == ip_end ? LZO_E_OK :
02421 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
02422
02423 #if defined(HAVE_NEED_IP)
02424 input_overrun:
02425 *out_len = op - out;
02426 return LZO_E_INPUT_OVERRUN;
02427 #endif
02428
02429 #if defined(HAVE_NEED_OP)
02430 output_overrun:
02431 *out_len = op - out;
02432 return LZO_E_OUTPUT_OVERRUN;
02433 #endif
02434
02435 #if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
02436 lookbehind_overrun:
02437 *out_len = op - out;
02438 return LZO_E_LOOKBEHIND_OVERRUN;
02439 #endif
02440 }
02441
02442 #define LZO_TEST_DECOMPRESS_OVERRUN
02443 #undef DO_DECOMPRESS
02444 #define DO_DECOMPRESS lzo1x_decompress_safe
02445
02446 #if defined(LZO_TEST_DECOMPRESS_OVERRUN)
02447 # if !defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
02448 # define LZO_TEST_DECOMPRESS_OVERRUN_INPUT 2
02449 # endif
02450 # if !defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
02451 # define LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT 2
02452 # endif
02453 # if !defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
02454 # define LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND
02455 # endif
02456 #endif
02457
02458 #undef TEST_IP
02459 #undef TEST_OP
02460 #undef TEST_LOOKBEHIND
02461 #undef NEED_IP
02462 #undef NEED_OP
02463 #undef HAVE_TEST_IP
02464 #undef HAVE_TEST_OP
02465 #undef HAVE_NEED_IP
02466 #undef HAVE_NEED_OP
02467 #undef HAVE_ANY_IP
02468 #undef HAVE_ANY_OP
02469
02470 #if defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
02471 # if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 1)
02472 # define TEST_IP (ip < ip_end)
02473 # endif
02474 # if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 2)
02475 # define NEED_IP(x) \
02476 if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x)) goto input_overrun
02477 # endif
02478 #endif
02479
02480 #if defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
02481 # if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 1)
02482 # define TEST_OP (op <= op_end)
02483 # endif
02484 # if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 2)
02485 # undef TEST_OP
02486 # define NEED_OP(x) \
02487 if ((lzo_uint)(op_end - op) < (lzo_uint)(x)) goto output_overrun
02488 # endif
02489 #endif
02490
02491 #if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
02492 # define TEST_LOOKBEHIND(m_pos,out) if (m_pos < out) goto lookbehind_overrun
02493 #else
02494 # define TEST_LOOKBEHIND(m_pos,op) ((void) 0)
02495 #endif
02496
02497 #if !defined(LZO_EOF_CODE) && !defined(TEST_IP)
02498 # define TEST_IP (ip < ip_end)
02499 #endif
02500
02501 #if defined(TEST_IP)
02502 # define HAVE_TEST_IP
02503 #else
02504 # define TEST_IP 1
02505 #endif
02506 #if defined(TEST_OP)
02507 # define HAVE_TEST_OP
02508 #else
02509 # define TEST_OP 1
02510 #endif
02511
02512 #if defined(NEED_IP)
02513 # define HAVE_NEED_IP
02514 #else
02515 # define NEED_IP(x) ((void) 0)
02516 #endif
02517 #if defined(NEED_OP)
02518 # define HAVE_NEED_OP
02519 #else
02520 # define NEED_OP(x) ((void) 0)
02521 #endif
02522
02523 #if defined(HAVE_TEST_IP) || defined(HAVE_NEED_IP)
02524 # define HAVE_ANY_IP
02525 #endif
02526 #if defined(HAVE_TEST_OP) || defined(HAVE_NEED_OP)
02527 # define HAVE_ANY_OP
02528 #endif
02529
02530 #undef __COPY4
02531 #define __COPY4(dst,src) * (lzo_uint32p)(dst) = * (const lzo_uint32p)(src)
02532
02533 #undef COPY4
02534 #if defined(LZO_UNALIGNED_OK_4)
02535 # define COPY4(dst,src) __COPY4(dst,src)
02536 #elif defined(LZO_ALIGNED_OK_4)
02537 # define COPY4(dst,src) __COPY4((lzo_ptr_t)(dst),(lzo_ptr_t)(src))
02538 #endif
02539
02540 #if defined(DO_DECOMPRESS)
02541 LZO_PUBLIC(int)
02542 DO_DECOMPRESS ( const lzo_byte *in , lzo_uint in_len,
02543 lzo_byte *out, lzo_uintp out_len,
02544 lzo_voidp wrkmem )
02545 #endif
02546 {
02547 register lzo_byte *op;
02548 register const lzo_byte *ip;
02549 register lzo_uint t;
02550 #if defined(COPY_DICT)
02551 lzo_uint m_off;
02552 const lzo_byte *dict_end;
02553 #else
02554 register const lzo_byte *m_pos;
02555 #endif
02556
02557 const lzo_byte * const ip_end = in + in_len;
02558 #if defined(HAVE_ANY_OP)
02559 lzo_byte * const op_end = out + *out_len;
02560 #endif
02561 #if defined(LZO1Z)
02562 lzo_uint last_m_off = 0;
02563 #endif
02564
02565 LZO_UNUSED(wrkmem);
02566
02567 #if defined(__LZO_QUERY_DECOMPRESS)
02568 if (__LZO_IS_DECOMPRESS_QUERY(in,in_len,out,out_len,wrkmem))
02569 return __LZO_QUERY_DECOMPRESS(in,in_len,out,out_len,wrkmem,0,0);
02570 #endif
02571
02572 #if defined(COPY_DICT)
02573 if (dict)
02574 {
02575 if (dict_len > M4_MAX_OFFSET)
02576 {
02577 dict += dict_len - M4_MAX_OFFSET;
02578 dict_len = M4_MAX_OFFSET;
02579 }
02580 dict_end = dict + dict_len;
02581 }
02582 else
02583 {
02584 dict_len = 0;
02585 dict_end = NULL;
02586 }
02587 #endif
02588
02589 *out_len = 0;
02590
02591 op = out;
02592 ip = in;
02593
02594 if (*ip > 17)
02595 {
02596 t = *ip++ - 17;
02597 if (t < 4)
02598 goto match_next;
02599 assert(t > 0); NEED_OP(t); NEED_IP(t+1);
02600 do *op++ = *ip++; while (--t > 0);
02601 goto first_literal_run;
02602 }
02603
02604 while (TEST_IP && TEST_OP)
02605 {
02606 t = *ip++;
02607 if (t >= 16)
02608 goto match;
02609 if (t == 0)
02610 {
02611 NEED_IP(1);
02612 while (*ip == 0)
02613 {
02614 t += 255;
02615 ip++;
02616 NEED_IP(1);
02617 }
02618 t += 15 + *ip++;
02619 }
02620 assert(t > 0); NEED_OP(t+3); NEED_IP(t+4);
02621 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
02622 #if !defined(LZO_UNALIGNED_OK_4)
02623 if (PTR_ALIGNED2_4(op,ip))
02624 {
02625 #endif
02626 COPY4(op,ip);
02627 op += 4; ip += 4;
02628 if (--t > 0)
02629 {
02630 if (t >= 4)
02631 {
02632 do {
02633 COPY4(op,ip);
02634 op += 4; ip += 4; t -= 4;
02635 } while (t >= 4);
02636 if (t > 0) do *op++ = *ip++; while (--t > 0);
02637 }
02638 else
02639 do *op++ = *ip++; while (--t > 0);
02640 }
02641 #if !defined(LZO_UNALIGNED_OK_4)
02642 }
02643 else
02644 #endif
02645 #endif
02646 #if !defined(LZO_UNALIGNED_OK_4)
02647 {
02648 *op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
02649 do *op++ = *ip++; while (--t > 0);
02650 }
02651 #endif
02652
02653 first_literal_run:
02654
02655 t = *ip++;
02656 if (t >= 16)
02657 goto match;
02658 #if defined(COPY_DICT)
02659 #if defined(LZO1Z)
02660 m_off = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
02661 last_m_off = m_off;
02662 #else
02663 m_off = (1 + M2_MAX_OFFSET) + (t >> 2) + (*ip++ << 2);
02664 #endif
02665 NEED_OP(3);
02666 t = 3; COPY_DICT(t,m_off)
02667 #else
02668 #if defined(LZO1Z)
02669 t = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
02670 m_pos = op - t;
02671 last_m_off = t;
02672 #else
02673 m_pos = op - (1 + M2_MAX_OFFSET);
02674 m_pos -= t >> 2;
02675 m_pos -= *ip++ << 2;
02676 #endif
02677 TEST_LOOKBEHIND(m_pos,out); NEED_OP(3);
02678 *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos;
02679 #endif
02680 goto match_done;
02681
02682 while (TEST_IP && TEST_OP)
02683 {
02684 match:
02685 if (t >= 64)
02686 {
02687 #if defined(COPY_DICT)
02688 #if defined(LZO1X)
02689 m_off = 1 + ((t >> 2) & 7) + (*ip++ << 3);
02690 t = (t >> 5) - 1;
02691 #elif defined(LZO1Y)
02692 m_off = 1 + ((t >> 2) & 3) + (*ip++ << 2);
02693 t = (t >> 4) - 3;
02694 #elif defined(LZO1Z)
02695 m_off = t & 0x1f;
02696 if (m_off >= 0x1c)
02697 m_off = last_m_off;
02698 else
02699 {
02700 m_off = 1 + (m_off << 6) + (*ip++ >> 2);
02701 last_m_off = m_off;
02702 }
02703 t = (t >> 5) - 1;
02704 #endif
02705 #else
02706 #if defined(LZO1X)
02707 m_pos = op - 1;
02708 m_pos -= (t >> 2) & 7;
02709 m_pos -= *ip++ << 3;
02710 t = (t >> 5) - 1;
02711 #elif defined(LZO1Y)
02712 m_pos = op - 1;
02713 m_pos -= (t >> 2) & 3;
02714 m_pos -= *ip++ << 2;
02715 t = (t >> 4) - 3;
02716 #elif defined(LZO1Z)
02717 {
02718 lzo_uint off = t & 0x1f;
02719 m_pos = op;
02720 if (off >= 0x1c)
02721 {
02722 assert(last_m_off > 0);
02723 m_pos -= last_m_off;
02724 }
02725 else
02726 {
02727 off = 1 + (off << 6) + (*ip++ >> 2);
02728 m_pos -= off;
02729 last_m_off = off;
02730 }
02731 }
02732 t = (t >> 5) - 1;
02733 #endif
02734 TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t+3-1);
02735 goto copy_match;
02736 #endif
02737 }
02738 else if (t >= 32)
02739 {
02740 t &= 31;
02741 if (t == 0)
02742 {
02743 NEED_IP(1);
02744 while (*ip == 0)
02745 {
02746 t += 255;
02747 ip++;
02748 NEED_IP(1);
02749 }
02750 t += 31 + *ip++;
02751 }
02752 #if defined(COPY_DICT)
02753 #if defined(LZO1Z)
02754 m_off = 1 + (ip[0] << 6) + (ip[1] >> 2);
02755 last_m_off = m_off;
02756 #else
02757 m_off = 1 + (ip[0] >> 2) + (ip[1] << 6);
02758 #endif
02759 #else
02760 #if defined(LZO1Z)
02761 {
02762 lzo_uint off = 1 + (ip[0] << 6) + (ip[1] >> 2);
02763 m_pos = op - off;
02764 last_m_off = off;
02765 }
02766 #elif defined(LZO_UNALIGNED_OK_2) && (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
02767 m_pos = op - 1;
02768 m_pos -= (* (const lzo_ushortp) ip) >> 2;
02769 #else
02770 m_pos = op - 1;
02771 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
02772 #endif
02773 #endif
02774 ip += 2;
02775 }
02776 else if (t >= 16)
02777 {
02778 #if defined(COPY_DICT)
02779 m_off = (t & 8) << 11;
02780 #else
02781 m_pos = op;
02782 m_pos -= (t & 8) << 11;
02783 #endif
02784 t &= 7;
02785 if (t == 0)
02786 {
02787 NEED_IP(1);
02788 while (*ip == 0)
02789 {
02790 t += 255;
02791 ip++;
02792 NEED_IP(1);
02793 }
02794 t += 7 + *ip++;
02795 }
02796 #if defined(COPY_DICT)
02797 #if defined(LZO1Z)
02798 m_off += (ip[0] << 6) + (ip[1] >> 2);
02799 #else
02800 m_off += (ip[0] >> 2) + (ip[1] << 6);
02801 #endif
02802 ip += 2;
02803 if (m_off == 0)
02804 goto eof_found;
02805 m_off += 0x4000;
02806 #if defined(LZO1Z)
02807 last_m_off = m_off;
02808 #endif
02809 #else
02810 #if defined(LZO1Z)
02811 m_pos -= (ip[0] << 6) + (ip[1] >> 2);
02812 #elif defined(LZO_UNALIGNED_OK_2) && (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
02813 m_pos -= (* (const lzo_ushortp) ip) >> 2;
02814 #else
02815 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
02816 #endif
02817 ip += 2;
02818 if (m_pos == op)
02819 goto eof_found;
02820 m_pos -= 0x4000;
02821 #if defined(LZO1Z)
02822 last_m_off = op - m_pos;
02823 #endif
02824 #endif
02825 }
02826 else
02827 {
02828 #if defined(COPY_DICT)
02829 #if defined(LZO1Z)
02830 m_off = 1 + (t << 6) + (*ip++ >> 2);
02831 last_m_off = m_off;
02832 #else
02833 m_off = 1 + (t >> 2) + (*ip++ << 2);
02834 #endif
02835 NEED_OP(2);
02836 t = 2; COPY_DICT(t,m_off)
02837 #else
02838 #if defined(LZO1Z)
02839 t = 1 + (t << 6) + (*ip++ >> 2);
02840 m_pos = op - t;
02841 last_m_off = t;
02842 #else
02843 m_pos = op - 1;
02844 m_pos -= t >> 2;
02845 m_pos -= *ip++ << 2;
02846 #endif
02847 TEST_LOOKBEHIND(m_pos,out); NEED_OP(2);
02848 *op++ = *m_pos++; *op++ = *m_pos;
02849 #endif
02850 goto match_done;
02851 }
02852
02853 #if defined(COPY_DICT)
02854
02855 NEED_OP(t+3-1);
02856 t += 3-1; COPY_DICT(t,m_off)
02857
02858 #else
02859
02860 TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t+3-1);
02861 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
02862 #if !defined(LZO_UNALIGNED_OK_4)
02863 if (t >= 2 * 4 - (3 - 1) && PTR_ALIGNED2_4(op,m_pos))
02864 {
02865 assert((op - m_pos) >= 4);
02866 #else
02867 if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
02868 {
02869 #endif
02870 COPY4(op,m_pos);
02871 op += 4; m_pos += 4; t -= 4 - (3 - 1);
02872 do {
02873 COPY4(op,m_pos);
02874 op += 4; m_pos += 4; t -= 4;
02875 } while (t >= 4);
02876 if (t > 0) do *op++ = *m_pos++; while (--t > 0);
02877 }
02878 else
02879 #endif
02880 {
02881 copy_match:
02882 *op++ = *m_pos++; *op++ = *m_pos++;
02883 do *op++ = *m_pos++; while (--t > 0);
02884 }
02885
02886 #endif
02887
02888 match_done:
02889 #if defined(LZO1Z)
02890 t = ip[-1] & 3;
02891 #else
02892 t = ip[-2] & 3;
02893 #endif
02894 if (t == 0)
02895 break;
02896
02897 match_next:
02898 assert(t > 0); NEED_OP(t); NEED_IP(t+1);
02899 do *op++ = *ip++; while (--t > 0);
02900 t = *ip++;
02901 }
02902 }
02903
02904 #if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
02905 *out_len = op - out;
02906 return LZO_E_EOF_NOT_FOUND;
02907 #endif
02908
02909 eof_found:
02910 assert(t == 1);
02911 *out_len = op - out;
02912 return (ip == ip_end ? LZO_E_OK :
02913 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
02914
02915 #if defined(HAVE_NEED_IP)
02916 input_overrun:
02917 *out_len = op - out;
02918 return LZO_E_INPUT_OVERRUN;
02919 #endif
02920
02921 #if defined(HAVE_NEED_OP)
02922 output_overrun:
02923 *out_len = op - out;
02924 return LZO_E_OUTPUT_OVERRUN;
02925 #endif
02926
02927 #if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
02928 lookbehind_overrun:
02929 *out_len = op - out;
02930 return LZO_E_LOOKBEHIND_OVERRUN;
02931 #endif
02932 }
02933
02934
02935