Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 68.48 KB | None | 0 0
  1. /* minilzo.c -- mini subset of the LZO real-time data compression library
  2.  
  3. This file is part of the LZO real-time data compression library.
  4.  
  5. Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
  6. Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
  7. Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
  8. Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
  9. Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
  10. Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
  11. Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
  12. All Rights Reserved.
  13.  
  14. The LZO library is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation; either version 2 of
  17. the License, or (at your option) any later version.
  18.  
  19. The LZO library is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. GNU General Public License for more details.
  23.  
  24. You should have received a copy of the GNU General Public License
  25. along with the LZO library; see the file COPYING.
  26. If not, write to the Free Software Foundation, Inc.,
  27. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  28.  
  29. Markus F.X.J. Oberhumer
  30. <markus@oberhumer.com>
  31. http://www.oberhumer.com/opensource/lzo/
  32. */
  33.  
  34. /*
  35. * NOTE:
  36. *   the full LZO package can be found at
  37. *   http://www.oberhumer.com/opensource/lzo/
  38. */
  39.  
  40. #define __LZO_IN_MINILZO
  41. #define LZO_BUILD
  42.  
  43. #ifdef MINILZO_HAVE_CONFIG_H
  44. #  include <config.h>
  45. #endif
  46.  
  47. #undef LZO_HAVE_CONFIG_H
  48. #include "minilzo.h"
  49.  
  50. #if !defined(MINILZO_VERSION) || (MINILZO_VERSION != 0x1080)
  51. #  error "version mismatch in miniLZO source files"
  52. #endif
  53.  
  54. #ifdef MINILZO_HAVE_CONFIG_H
  55. #  define LZO_HAVE_CONFIG_H
  56. #endif
  57.  
  58. #if !defined(LZO_NO_SYS_TYPES_H)
  59. #  include <sys/types.h>
  60. #endif
  61. #include <stdio.h>
  62.  
  63. #ifndef __LZO_CONF_H
  64. #define __LZO_CONF_H
  65.  
  66. #if !defined(__LZO_IN_MINILZO)
  67. #  ifndef __LZOCONF_H
  68. #    include <lzo/lzoconf.h>
  69. #  endif
  70. #endif
  71.  
  72. #if defined(__BOUNDS_CHECKING_ON)
  73. #  include <unchecked.h>
  74. #else
  75. #  define BOUNDS_CHECKING_OFF_DURING(stmt)      stmt
  76. #  define BOUNDS_CHECKING_OFF_IN_EXPR(expr)     (expr)
  77. #endif
  78.  
  79. #if !defined(LZO_HAVE_CONFIG_H)
  80. #  include <stddef.h>
  81. #  include <string.h>
  82. #  if !defined(NO_STDLIB_H)
  83. #    include <stdlib.h>
  84. #  endif
  85. #  define HAVE_MEMCMP
  86. #  define HAVE_MEMCPY
  87. #  define HAVE_MEMMOVE
  88. #  define HAVE_MEMSET
  89. #else
  90. #  include <sys/types.h>
  91. #  if defined(HAVE_STDDEF_H)
  92. #    include <stddef.h>
  93. #  endif
  94. #  if defined(STDC_HEADERS)
  95. #    include <string.h>
  96. #    include <stdlib.h>
  97. #  endif
  98. #endif
  99.  
  100. #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
  101. #  define HAVE_MALLOC_H
  102. #  define HAVE_HALLOC
  103. #endif
  104.  
  105. #undef NDEBUG
  106. #if !defined(LZO_DEBUG)
  107. #  define NDEBUG
  108. #endif
  109. #if defined(LZO_DEBUG) || !defined(NDEBUG)
  110. #  if !defined(NO_STDIO_H)
  111. #    include <stdio.h>
  112. #  endif
  113. #endif
  114. #include <assert.h>
  115.  
  116. #if !defined(LZO_COMPILE_TIME_ASSERT)
  117. #  define LZO_COMPILE_TIME_ASSERT(expr) \
  118.     { typedef int __lzo_compile_time_assert_fail[1 - 2 * !(expr)]; }
  119. #endif
  120.  
  121. #if !defined(LZO_UNUSED)
  122. #  if 1
  123. #    define LZO_UNUSED(var)     ((void)&var)
  124. #  elif 0
  125. #    define LZO_UNUSED(var)     { typedef int __lzo_unused[sizeof(var) ? 2 : 1]; }
  126. #  else
  127. #    define LZO_UNUSED(parm)    (parm = parm)
  128. #  endif
  129. #endif
  130.  
  131. #if !defined(__inline__) && !defined(__GNUC__)
  132. #  if defined(__cplusplus)
  133. #    define __inline__      inline
  134. #  else
  135. #    define __inline__
  136. #  endif
  137. #endif
  138.  
  139. #if defined(NO_MEMCMP)
  140. #  undef HAVE_MEMCMP
  141. #endif
  142.  
  143. #if !defined(HAVE_MEMCMP)
  144. #  undef memcmp
  145. #  define memcmp    lzo_memcmp
  146. #endif
  147. #if !defined(HAVE_MEMCPY)
  148. #  undef memcpy
  149. #  define memcpy    lzo_memcpy
  150. #endif
  151. #if !defined(HAVE_MEMMOVE)
  152. #  undef memmove
  153. #  define memmove   lzo_memmove
  154. #endif
  155. #if !defined(HAVE_MEMSET)
  156. #  undef memset
  157. #  define memset    lzo_memset
  158. #endif
  159.  
  160. #if 0
  161. #  define LZO_BYTE(x)       ((unsigned char) (x))
  162. #else
  163. #  define LZO_BYTE(x)       ((unsigned char) ((x) & 0xff))
  164. #endif
  165.  
  166. #define LZO_MAX(a,b)        ((a) >= (b) ? (a) : (b))
  167. #define LZO_MIN(a,b)        ((a) <= (b) ? (a) : (b))
  168. #define LZO_MAX3(a,b,c)     ((a) >= (b) ? LZO_MAX(a,c) : LZO_MAX(b,c))
  169. #define LZO_MIN3(a,b,c)     ((a) <= (b) ? LZO_MIN(a,c) : LZO_MIN(b,c))
  170.  
  171. #define lzo_sizeof(type)    ((lzo_uint) (sizeof(type)))
  172.  
  173. #define LZO_HIGH(array)     ((lzo_uint) (sizeof(array)/sizeof(*(array))))
  174.  
  175. #define LZO_SIZE(bits)      (1u << (bits))
  176. #define LZO_MASK(bits)      (LZO_SIZE(bits) - 1)
  177.  
  178. #define LZO_LSIZE(bits)     (1ul << (bits))
  179. #define LZO_LMASK(bits)     (LZO_LSIZE(bits) - 1)
  180.  
  181. #define LZO_USIZE(bits)     ((lzo_uint) 1 << (bits))
  182. #define LZO_UMASK(bits)     (LZO_USIZE(bits) - 1)
  183.  
  184. #define LZO_STYPE_MAX(b)    (((1l  << (8*(b)-2)) - 1l)  + (1l  << (8*(b)-2)))
  185. #define LZO_UTYPE_MAX(b)    (((1ul << (8*(b)-1)) - 1ul) + (1ul << (8*(b)-1)))
  186.  
  187. #if !defined(SIZEOF_UNSIGNED)
  188. #  if (UINT_MAX == 0xffff)
  189. #    define SIZEOF_UNSIGNED         2
  190. #  elif (UINT_MAX == LZO_0xffffffffL)
  191. #    define SIZEOF_UNSIGNED         4
  192. #  elif (UINT_MAX >= LZO_0xffffffffL)
  193. #    define SIZEOF_UNSIGNED         8
  194. #  else
  195. #    error "SIZEOF_UNSIGNED"
  196. #  endif
  197. #endif
  198.  
  199. #if !defined(SIZEOF_UNSIGNED_LONG)
  200. #  if (ULONG_MAX == LZO_0xffffffffL)
  201. #    define SIZEOF_UNSIGNED_LONG    4
  202. #  elif (ULONG_MAX >= LZO_0xffffffffL)
  203. #    define SIZEOF_UNSIGNED_LONG    8
  204. #  else
  205. #    error "SIZEOF_UNSIGNED_LONG"
  206. #  endif
  207. #endif
  208.  
  209. #if !defined(SIZEOF_SIZE_T)
  210. #  define SIZEOF_SIZE_T             SIZEOF_UNSIGNED
  211. #endif
  212. #if !defined(SIZE_T_MAX)
  213. #  define SIZE_T_MAX                LZO_UTYPE_MAX(SIZEOF_SIZE_T)
  214. #endif
  215.  
  216. #if 1 && defined(__LZO_i386) && (UINT_MAX == LZO_0xffffffffL)
  217. #  if !defined(LZO_UNALIGNED_OK_2) && (USHRT_MAX == 0xffff)
  218. #    define LZO_UNALIGNED_OK_2
  219. #  endif
  220. #  if !defined(LZO_UNALIGNED_OK_4) && (LZO_UINT32_MAX == LZO_0xffffffffL)
  221. #    define LZO_UNALIGNED_OK_4
  222. #  endif
  223. #endif
  224.  
  225. #if defined(LZO_UNALIGNED_OK_2) || defined(LZO_UNALIGNED_OK_4)
  226. #  if !defined(LZO_UNALIGNED_OK)
  227. #    define LZO_UNALIGNED_OK
  228. #  endif
  229. #endif
  230.  
  231. #if defined(__LZO_NO_UNALIGNED)
  232. #  undef LZO_UNALIGNED_OK
  233. #  undef LZO_UNALIGNED_OK_2
  234. #  undef LZO_UNALIGNED_OK_4
  235. #endif
  236.  
  237. #if defined(LZO_UNALIGNED_OK_2) && (USHRT_MAX != 0xffff)
  238. #  error "LZO_UNALIGNED_OK_2 must not be defined on this system"
  239. #endif
  240. #if defined(LZO_UNALIGNED_OK_4) && (LZO_UINT32_MAX != LZO_0xffffffffL)
  241. #  error "LZO_UNALIGNED_OK_4 must not be defined on this system"
  242. #endif
  243.  
  244. #if defined(__LZO_NO_ALIGNED)
  245. #  undef LZO_ALIGNED_OK_4
  246. #endif
  247.  
  248. #if defined(LZO_ALIGNED_OK_4) && (LZO_UINT32_MAX != LZO_0xffffffffL)
  249. #  error "LZO_ALIGNED_OK_4 must not be defined on this system"
  250. #endif
  251.  
  252. #define LZO_LITTLE_ENDIAN       1234
  253. #define LZO_BIG_ENDIAN          4321
  254. #define LZO_PDP_ENDIAN          3412
  255.  
  256. #if !defined(LZO_BYTE_ORDER)
  257. #  if defined(MFX_BYTE_ORDER)
  258. #    define LZO_BYTE_ORDER      MFX_BYTE_ORDER
  259. #  elif defined(__LZO_i386)
  260. #    define LZO_BYTE_ORDER      LZO_LITTLE_ENDIAN
  261. #  elif defined(BYTE_ORDER)
  262. #    define LZO_BYTE_ORDER      BYTE_ORDER
  263. #  elif defined(__BYTE_ORDER)
  264. #    define LZO_BYTE_ORDER      __BYTE_ORDER
  265. #  endif
  266. #endif
  267.  
  268. #if defined(LZO_BYTE_ORDER)
  269. #  if (LZO_BYTE_ORDER != LZO_LITTLE_ENDIAN) && \
  270.     (LZO_BYTE_ORDER != LZO_BIG_ENDIAN)
  271. #    error "invalid LZO_BYTE_ORDER"
  272. #  endif
  273. #endif
  274.  
  275. #if defined(LZO_UNALIGNED_OK) && !defined(LZO_BYTE_ORDER)
  276. #  error "LZO_BYTE_ORDER is not defined"
  277. #endif
  278.  
  279. #define LZO_OPTIMIZE_GNUC_i386_IS_BUGGY
  280.  
  281. #if defined(NDEBUG) && !defined(LZO_DEBUG) && !defined(__LZO_CHECKER)
  282. #  if defined(__GNUC__) && defined(__i386__)
  283. #    if !defined(LZO_OPTIMIZE_GNUC_i386_IS_BUGGY)
  284. #      define LZO_OPTIMIZE_GNUC_i386
  285. #    endif
  286. #  endif
  287. #endif
  288.  
  289. __LZO_EXTERN_C int __lzo_init_done;
  290. __LZO_EXTERN_C const lzo_byte __lzo_copyright[];
  291. LZO_EXTERN(const lzo_byte *) lzo_copyright(void);
  292. __LZO_EXTERN_C const lzo_uint32 _lzo_crc32_table[256];
  293.  
  294. #define _LZO_STRINGIZE(x)           #x
  295. #define _LZO_MEXPAND(x)             _LZO_STRINGIZE(x)
  296.  
  297. #define _LZO_CONCAT2(a,b)           a ## b
  298. #define _LZO_CONCAT3(a,b,c)         a ## b ## c
  299. #define _LZO_CONCAT4(a,b,c,d)       a ## b ## c ## d
  300. #define _LZO_CONCAT5(a,b,c,d,e)     a ## b ## c ## d ## e
  301.  
  302. #define _LZO_ECONCAT2(a,b)          _LZO_CONCAT2(a,b)
  303. #define _LZO_ECONCAT3(a,b,c)        _LZO_CONCAT3(a,b,c)
  304. #define _LZO_ECONCAT4(a,b,c,d)      _LZO_CONCAT4(a,b,c,d)
  305. #define _LZO_ECONCAT5(a,b,c,d,e)    _LZO_CONCAT5(a,b,c,d,e)
  306.  
  307. #if 0
  308.  
  309. #define __LZO_IS_COMPRESS_QUERY(i,il,o,ol,w)    ((lzo_voidp)(o) == (w))
  310. #define __LZO_QUERY_COMPRESS(i,il,o,ol,w,n,s) \
  311.     (*ol = (n)*(s), LZO_E_OK)
  312.  
  313. #define __LZO_IS_DECOMPRESS_QUERY(i,il,o,ol,w)  ((lzo_voidp)(o) == (w))
  314. #define __LZO_QUERY_DECOMPRESS(i,il,o,ol,w,n,s) \
  315.     (*ol = (n)*(s), LZO_E_OK)
  316.  
  317. #define __LZO_IS_OPTIMIZE_QUERY(i,il,o,ol,w)    ((lzo_voidp)(o) == (w))
  318. #define __LZO_QUERY_OPTIMIZE(i,il,o,ol,w,n,s) \
  319.     (*ol = (n)*(s), LZO_E_OK)
  320.  
  321. #endif
  322.  
  323. #ifndef __LZO_PTR_H
  324. #define __LZO_PTR_H
  325.  
  326. #ifdef __cplusplus
  327. extern "C" {
  328. #endif
  329.  
  330. #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
  331. #  include <dos.h>
  332. #  if 1 && defined(__WATCOMC__)
  333. #    include <i86.h>
  334.     __LZO_EXTERN_C unsigned char _HShift;
  335. #    define __LZO_HShift    _HShift
  336. #  elif 1 && defined(_MSC_VER)
  337.     __LZO_EXTERN_C unsigned short __near _AHSHIFT;
  338. #    define __LZO_HShift    ((unsigned) &_AHSHIFT)
  339. #  elif defined(__LZO_WIN16)
  340. #    define __LZO_HShift    3
  341. #  else
  342. #    define __LZO_HShift    12
  343. #  endif
  344. #  if !defined(_FP_SEG) && defined(FP_SEG)
  345. #    define _FP_SEG         FP_SEG
  346. #  endif
  347. #  if !defined(_FP_OFF) && defined(FP_OFF)
  348. #    define _FP_OFF         FP_OFF
  349. #  endif
  350. #endif
  351.  
  352. #if !defined(lzo_ptrdiff_t)
  353. #  if (UINT_MAX >= LZO_0xffffffffL)
  354.     typedef ptrdiff_t          lzo_ptrdiff_t;
  355. #  else
  356.     typedef long               lzo_ptrdiff_t;
  357. #  endif
  358. #endif
  359.  
  360. #if !defined(__LZO_HAVE_PTR_T)
  361. #  if defined(lzo_ptr_t)
  362. #    define __LZO_HAVE_PTR_T
  363. #  endif
  364. #endif
  365. #if !defined(__LZO_HAVE_PTR_T)
  366. #  if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_LONG)
  367. #    if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_LONG)
  368.     typedef unsigned long    lzo_ptr_t;
  369.     typedef long             lzo_sptr_t;
  370. #      define __LZO_HAVE_PTR_T
  371. #    endif
  372. #  endif
  373. #endif
  374. #if !defined(__LZO_HAVE_PTR_T)
  375. #  if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED)
  376. #    if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED)
  377.     typedef unsigned int     lzo_ptr_t;
  378.     typedef int              lzo_sptr_t;
  379. #      define __LZO_HAVE_PTR_T
  380. #    endif
  381. #  endif
  382. #endif
  383. #if !defined(__LZO_HAVE_PTR_T)
  384. #  if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_SHORT)
  385. #    if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_SHORT)
  386.     typedef unsigned short   lzo_ptr_t;
  387.     typedef short            lzo_sptr_t;
  388. #      define __LZO_HAVE_PTR_T
  389. #    endif
  390. #  endif
  391. #endif
  392. #if !defined(__LZO_HAVE_PTR_T)
  393. #  if defined(LZO_HAVE_CONFIG_H) || defined(SIZEOF_CHAR_P)
  394. #    error "no suitable type for lzo_ptr_t"
  395. #  else
  396.     typedef unsigned long      lzo_ptr_t;
  397.     typedef long               lzo_sptr_t;
  398. #    define __LZO_HAVE_PTR_T
  399. #  endif
  400. #endif
  401.  
  402. #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
  403. #define PTR(a)              ((lzo_bytep) (a))
  404. #define PTR_ALIGNED_4(a)    ((_FP_OFF(a) & 3) == 0)
  405. #define PTR_ALIGNED2_4(a,b) (((_FP_OFF(a) | _FP_OFF(b)) & 3) == 0)
  406. #else
  407. #define PTR(a)              ((lzo_ptr_t) (a))
  408. #define PTR_LINEAR(a)       PTR(a)
  409. #define PTR_ALIGNED_4(a)    ((PTR_LINEAR(a) & 3) == 0)
  410. #define PTR_ALIGNED_8(a)    ((PTR_LINEAR(a) & 7) == 0)
  411. #define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
  412. #define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
  413. #endif
  414.  
  415. #define PTR_LT(a,b)         (PTR(a) < PTR(b))
  416. #define PTR_GE(a,b)         (PTR(a) >= PTR(b))
  417. #define PTR_DIFF(a,b)       ((lzo_ptrdiff_t) (PTR(a) - PTR(b)))
  418. #define pd(a,b)             ((lzo_uint) ((a)-(b)))
  419.  
  420.     LZO_EXTERN(lzo_ptr_t)
  421.         __lzo_ptr_linear(const lzo_voidp ptr);
  422.  
  423.     typedef union
  424.     {
  425.         char            a_char;
  426.         unsigned char   a_uchar;
  427.         short           a_short;
  428.         unsigned short  a_ushort;
  429.         int             a_int;
  430.         unsigned int    a_uint;
  431.         long            a_long;
  432.         unsigned long   a_ulong;
  433.         lzo_int         a_lzo_int;
  434.         lzo_uint        a_lzo_uint;
  435.         lzo_int32       a_lzo_int32;
  436.         lzo_uint32      a_lzo_uint32;
  437.         ptrdiff_t       a_ptrdiff_t;
  438.         lzo_ptrdiff_t   a_lzo_ptrdiff_t;
  439.         lzo_ptr_t       a_lzo_ptr_t;
  440.         lzo_voidp       a_lzo_voidp;
  441.         void *          a_void_p;
  442.         lzo_bytep       a_lzo_bytep;
  443.         lzo_bytepp      a_lzo_bytepp;
  444.         lzo_uintp       a_lzo_uintp;
  445.         lzo_uint *      a_lzo_uint_p;
  446.         lzo_uint32p     a_lzo_uint32p;
  447.         lzo_uint32 *    a_lzo_uint32_p;
  448.         unsigned char * a_uchar_p;
  449.         char *          a_char_p;
  450.     }
  451.     lzo_full_align_t;
  452.  
  453. #ifdef __cplusplus
  454. }
  455. #endif
  456.  
  457. #endif
  458.  
  459. #define LZO_DETERMINISTIC
  460.  
  461. #define LZO_DICT_USE_PTR
  462. #if defined(__LZO_DOS16) || defined(__LZO_WIN16) || defined(__LZO_STRICT_16BIT)
  463. #  undef LZO_DICT_USE_PTR
  464. #endif
  465.  
  466. #if defined(LZO_DICT_USE_PTR)
  467. #  define lzo_dict_t    const lzo_bytep
  468. #  define lzo_dict_p    lzo_dict_t __LZO_MMODEL *
  469. #else
  470. #  define lzo_dict_t    lzo_uint
  471. #  define lzo_dict_p    lzo_dict_t __LZO_MMODEL *
  472. #endif
  473.  
  474. #if !defined(lzo_moff_t)
  475. #define lzo_moff_t      lzo_uint
  476. #endif
  477.  
  478. #endif
  479.  
  480. LZO_PUBLIC(lzo_ptr_t)
  481. __lzo_ptr_linear(const lzo_voidp ptr)
  482. {
  483.     lzo_ptr_t p;
  484.  
  485. #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
  486.     p = (((lzo_ptr_t)(_FP_SEG(ptr))) << (16 - __LZO_HShift)) + (_FP_OFF(ptr));
  487. #else
  488.     p = PTR_LINEAR(ptr);
  489. #endif
  490.  
  491.     return p;
  492. }
  493.  
  494. LZO_PUBLIC(unsigned)
  495. __lzo_align_gap(const lzo_voidp ptr, lzo_uint size)
  496. {
  497.     lzo_ptr_t p, s, n;
  498.  
  499.     assert(size > 0);
  500.  
  501.     p = __lzo_ptr_linear(ptr);
  502.     s = (lzo_ptr_t) (size - 1);
  503. #if 0
  504.     assert((size & (size - 1)) == 0);
  505.     n = ((p + s) & ~s) - p;
  506. #else
  507.     n = (((p + s) / size) * size) - p;
  508. #endif
  509.  
  510.     assert((long)n >= 0);
  511.     assert(n <= s);
  512.  
  513.     return (unsigned)n;
  514. }
  515.  
  516. #ifndef __LZO_UTIL_H
  517. #define __LZO_UTIL_H
  518.  
  519. #ifndef __LZO_CONF_H
  520. #endif
  521.  
  522. #ifdef __cplusplus
  523. extern "C" {
  524. #endif
  525.  
  526. #if 1 && defined(HAVE_MEMCPY)
  527. #if !defined(__LZO_DOS16) && !defined(__LZO_WIN16)
  528.  
  529. #define MEMCPY8_DS(dest,src,len) \
  530.     memcpy(dest,src,len); \
  531.     dest += len; \
  532.     src += len
  533.  
  534. #endif
  535. #endif
  536.  
  537. #if 0 && !defined(MEMCPY8_DS)
  538.  
  539. #define MEMCPY8_DS(dest,src,len) \
  540.     { do { \
  541.     *dest++ = *src++; \
  542.     *dest++ = *src++; \
  543.     *dest++ = *src++; \
  544.     *dest++ = *src++; \
  545.     *dest++ = *src++; \
  546.     *dest++ = *src++; \
  547.     *dest++ = *src++; \
  548.     *dest++ = *src++; \
  549.     len -= 8; \
  550. } while (len > 0); }
  551.  
  552. #endif
  553.  
  554. #if !defined(MEMCPY8_DS)
  555.  
  556. #define MEMCPY8_DS(dest,src,len) \
  557.     { register lzo_uint __l = (len) / 8; \
  558.     do { \
  559.     *dest++ = *src++; \
  560.     *dest++ = *src++; \
  561.     *dest++ = *src++; \
  562.     *dest++ = *src++; \
  563.     *dest++ = *src++; \
  564.     *dest++ = *src++; \
  565.     *dest++ = *src++; \
  566.     *dest++ = *src++; \
  567.     } while (--__l > 0); }
  568.  
  569. #endif
  570.  
  571. #define MEMCPY_DS(dest,src,len) \
  572.     do *dest++ = *src++; \
  573.     while (--len > 0)
  574.  
  575. #define MEMMOVE_DS(dest,src,len) \
  576.     do *dest++ = *src++; \
  577.     while (--len > 0)
  578.  
  579. #if 0 && defined(LZO_OPTIMIZE_GNUC_i386)
  580.  
  581. #define BZERO8_PTR(s,l,n) \
  582.     __asm__ __volatile__( \
  583.     "movl  %0,%%eax \n"             \
  584.     "movl  %1,%%edi \n"             \
  585.     "movl  %2,%%ecx \n"             \
  586.     "cld \n"                        \
  587.     "rep \n"                        \
  588.     "stosl %%eax,(%%edi) \n"        \
  589.     :               \
  590.     :"g" (0),"g" (s),"g" (n)        \
  591.     :"eax","edi","ecx", "memory", "cc" \
  592.     )
  593.  
  594. #elif (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET)
  595.  
  596. #if 1
  597. #define BZERO8_PTR(s,l,n)   memset((s),0,(lzo_uint)(l)*(n))
  598. #else
  599. #define BZERO8_PTR(s,l,n)   memset((lzo_voidp)(s),0,(lzo_uint)(l)*(n))
  600. #endif
  601.  
  602. #else
  603.  
  604. #define BZERO8_PTR(s,l,n) \
  605.     lzo_memset((lzo_voidp)(s),0,(lzo_uint)(l)*(n))
  606.  
  607. #endif
  608.  
  609. #if 0
  610. #if defined(__GNUC__) && defined(__i386__)
  611.  
  612.     unsigned char lzo_rotr8(unsigned char value, int shift);
  613.     extern __inline__ unsigned char lzo_rotr8(unsigned char value, int shift)
  614.     {
  615.         unsigned char result;
  616.  
  617.         __asm__ __volatile__ ("movb %b1, %b0; rorb %b2, %b0"
  618.             : "=a"(result) : "g"(value), "c"(shift));
  619.         return result;
  620.     }
  621.  
  622.     unsigned short lzo_rotr16(unsigned short value, int shift);
  623.     extern __inline__ unsigned short lzo_rotr16(unsigned short value, int shift)
  624.     {
  625.         unsigned short result;
  626.  
  627.         __asm__ __volatile__ ("movw %b1, %b0; rorw %b2, %b0"
  628.             : "=a"(result) : "g"(value), "c"(shift));
  629.         return result;
  630.     }
  631.  
  632. #endif
  633. #endif
  634.  
  635. #ifdef __cplusplus
  636. }
  637. #endif
  638.  
  639. #endif
  640.  
  641. LZO_PUBLIC(lzo_bool)
  642. lzo_assert(int expr)
  643. {
  644.     return (expr) ? 1 : 0;
  645. }
  646.  
  647. /* If you use the LZO library in a product, you *must* keep this
  648. * copyright string in the executable of your product.
  649. */
  650.  
  651. const lzo_byte __lzo_copyright[] =
  652. #if !defined(__LZO_IN_MINLZO)
  653. LZO_VERSION_STRING;
  654. #else
  655. "\n\n\n"
  656. "LZO real-time data compression library.\n"
  657. "Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 Markus Franz Xaver Johannes Oberhumer\n"
  658. "<markus.oberhumer@jk.uni-linz.ac.at>\n"
  659. "http://www.oberhumer.com/opensource/lzo/\n"
  660. "\n"
  661. "LZO version: v" LZO_VERSION_STRING ", " LZO_VERSION_DATE "\n"
  662. "LZO build date: " __DATE__ " " __TIME__ "\n\n"
  663. "LZO special compilation options:\n"
  664. #ifdef __cplusplus
  665. " __cplusplus\n"
  666. #endif
  667. #if defined(__PIC__)
  668. " __PIC__\n"
  669. #elif defined(__pic__)
  670. " __pic__\n"
  671. #endif
  672. #if (UINT_MAX < LZO_0xffffffffL)
  673. " 16BIT\n"
  674. #endif
  675. #if defined(__LZO_STRICT_16BIT)
  676. " __LZO_STRICT_16BIT\n"
  677. #endif
  678. #if (UINT_MAX > LZO_0xffffffffL)
  679. " UINT_MAX=" _LZO_MEXPAND(UINT_MAX) "\n"
  680. #endif
  681. #if (ULONG_MAX > LZO_0xffffffffL)
  682. " ULONG_MAX=" _LZO_MEXPAND(ULONG_MAX) "\n"
  683. #endif
  684. #if defined(LZO_BYTE_ORDER)
  685. " LZO_BYTE_ORDER=" _LZO_MEXPAND(LZO_BYTE_ORDER) "\n"
  686. #endif
  687. #if defined(LZO_UNALIGNED_OK_2)
  688. " LZO_UNALIGNED_OK_2\n"
  689. #endif
  690. #if defined(LZO_UNALIGNED_OK_4)
  691. " LZO_UNALIGNED_OK_4\n"
  692. #endif
  693. #if defined(LZO_ALIGNED_OK_4)
  694. " LZO_ALIGNED_OK_4\n"
  695. #endif
  696. #if defined(LZO_DICT_USE_PTR)
  697. " LZO_DICT_USE_PTR\n"
  698. #endif
  699. #if defined(__LZO_QUERY_COMPRESS)
  700. " __LZO_QUERY_COMPRESS\n"
  701. #endif
  702. #if defined(__LZO_QUERY_DECOMPRESS)
  703. " __LZO_QUERY_DECOMPRESS\n"
  704. #endif
  705. #if defined(__LZO_IN_MINILZO)
  706. " __LZO_IN_MINILZO\n"
  707. #endif
  708. "\n\n"
  709. "$Id: LZO " LZO_VERSION_STRING " built " __DATE__ " " __TIME__
  710. #if defined(__GNUC__) && defined(__VERSION__)
  711. " by gcc " __VERSION__
  712. #elif defined(__BORLANDC__)
  713. " by Borland C " _LZO_MEXPAND(__BORLANDC__)
  714. #elif defined(_MSC_VER)
  715. " by Microsoft C " _LZO_MEXPAND(_MSC_VER)
  716. #elif defined(__PUREC__)
  717. " by Pure C " _LZO_MEXPAND(__PUREC__)
  718. #elif defined(__SC__)
  719. " by Symantec C " _LZO_MEXPAND(__SC__)
  720. #elif defined(__TURBOC__)
  721. " by Turbo C " _LZO_MEXPAND(__TURBOC__)
  722. #elif defined(__WATCOMC__)
  723. " by Watcom C " _LZO_MEXPAND(__WATCOMC__)
  724. #endif
  725. " $\n"
  726. "$Copyright: LZO (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 Markus Franz Xaver Johannes Oberhumer $\n";
  727. #endif
  728.  
  729. LZO_PUBLIC(const lzo_byte *)
  730. lzo_copyright(void)
  731. {
  732.     return __lzo_copyright;
  733. }
  734.  
  735. LZO_PUBLIC(unsigned)
  736. lzo_version(void)
  737. {
  738.     return LZO_VERSION;
  739. }
  740.  
  741. LZO_PUBLIC(const char *)
  742. lzo_version_string(void)
  743. {
  744.     return LZO_VERSION_STRING;
  745. }
  746.  
  747. LZO_PUBLIC(const char *)
  748. lzo_version_date(void)
  749. {
  750.     return LZO_VERSION_DATE;
  751. }
  752.  
  753. LZO_PUBLIC(const lzo_charp)
  754. _lzo_version_string(void)
  755. {
  756.     return LZO_VERSION_STRING;
  757. }
  758.  
  759. LZO_PUBLIC(const lzo_charp)
  760. _lzo_version_date(void)
  761. {
  762.     return LZO_VERSION_DATE;
  763. }
  764.  
  765. #define LZO_BASE 65521u
  766. #define LZO_NMAX 5552
  767.  
  768. #define LZO_DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
  769. #define LZO_DO2(buf,i)  LZO_DO1(buf,i); LZO_DO1(buf,i+1);
  770. #define LZO_DO4(buf,i)  LZO_DO2(buf,i); LZO_DO2(buf,i+2);
  771. #define LZO_DO8(buf,i)  LZO_DO4(buf,i); LZO_DO4(buf,i+4);
  772. #define LZO_DO16(buf,i) LZO_DO8(buf,i); LZO_DO8(buf,i+8);
  773.  
  774. LZO_PUBLIC(lzo_uint32)
  775. lzo_adler32(lzo_uint32 adler, const lzo_byte *buf, lzo_uint len)
  776. {
  777.     lzo_uint32 s1 = adler & 0xffff;
  778.     lzo_uint32 s2 = (adler >> 16) & 0xffff;
  779.     int k;
  780.  
  781.     if (buf == NULL)
  782.         return 1;
  783.  
  784.     while (len > 0)
  785.     {
  786.         k = len < LZO_NMAX ? (int) len : LZO_NMAX;
  787.         len -= k;
  788.         if (k >= 16) do
  789.         {
  790.             LZO_DO16(buf,0);
  791.             buf += 16;
  792.             k -= 16;
  793.         } while (k >= 16);
  794.         if (k != 0) do
  795.         {
  796.             s1 += *buf++;
  797.             s2 += s1;
  798.         } while (--k > 0);
  799.         s1 %= LZO_BASE;
  800.         s2 %= LZO_BASE;
  801.     }
  802.     return (s2 << 16) | s1;
  803. }
  804.  
  805. LZO_PUBLIC(int)
  806. lzo_memcmp(const lzo_voidp s1, const lzo_voidp s2, lzo_uint len)
  807. {
  808. #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCMP)
  809.     return memcmp(s1,s2,len);
  810. #else
  811.     const lzo_byte *p1 = (const lzo_byte *) s1;
  812.     const lzo_byte *p2 = (const lzo_byte *) s2;
  813.     int d;
  814.  
  815.     if (len > 0) do
  816.     {
  817.         d = *p1 - *p2;
  818.         if (d != 0)
  819.             return d;
  820.         p1++;
  821.         p2++;
  822.     }
  823.     while (--len > 0);
  824.     return 0;
  825. #endif
  826. }
  827.  
  828. LZO_PUBLIC(lzo_voidp)
  829. lzo_memcpy(lzo_voidp dest, const lzo_voidp src, lzo_uint len)
  830. {
  831. #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCPY)
  832.     return memcpy(dest,src,len);
  833. #else
  834.     lzo_byte *p1 = (lzo_byte *) dest;
  835.     const lzo_byte *p2 = (const lzo_byte *) src;
  836.  
  837.     if (len <= 0 || p1 == p2)
  838.         return dest;
  839.     do
  840.     *p1++ = *p2++;
  841.     while (--len > 0);
  842.     return dest;
  843. #endif
  844. }
  845.  
  846. LZO_PUBLIC(lzo_voidp)
  847. lzo_memmove(lzo_voidp dest, const lzo_voidp src, lzo_uint len)
  848. {
  849. #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMMOVE)
  850.     return memmove(dest,src,len);
  851. #else
  852.     lzo_byte *p1 = (lzo_byte *) dest;
  853.     const lzo_byte *p2 = (const lzo_byte *) src;
  854.  
  855.     if (len <= 0 || p1 == p2)
  856.         return dest;
  857.  
  858.     if (p1 < p2)
  859.     {
  860.         do
  861.         *p1++ = *p2++;
  862.         while (--len > 0);
  863.     }
  864.     else
  865.     {
  866.         p1 += len;
  867.         p2 += len;
  868.         do
  869.         *--p1 = *--p2;
  870.         while (--len > 0);
  871.     }
  872.     return dest;
  873. #endif
  874. }
  875.  
  876. LZO_PUBLIC(lzo_voidp)
  877. lzo_memset(lzo_voidp s, int c, lzo_uint len)
  878. {
  879. #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET)
  880.     return memset(s,c,len);
  881. #else
  882.     lzo_byte *p = (lzo_byte *) s;
  883.  
  884.     if (len > 0) do
  885.     *p++ = LZO_BYTE(c);
  886.     while (--len > 0);
  887.     return s;
  888. #endif
  889. }
  890.  
  891. #if 0
  892. #  define IS_SIGNED(type)       (((type) (1ul << (8 * sizeof(type) - 1))) < 0)
  893. #  define IS_UNSIGNED(type)     (((type) (1ul << (8 * sizeof(type) - 1))) > 0)
  894. #else
  895. #  define IS_SIGNED(type)       (((type) (-1)) < ((type) 0))
  896. #  define IS_UNSIGNED(type)     (((type) (-1)) > ((type) 0))
  897. #endif
  898.  
  899. #define IS_POWER_OF_2(x)        (((x) & ((x) - 1)) == 0)
  900.  
  901. static lzo_bool schedule_insns_bug(void);
  902. static lzo_bool strength_reduce_bug(int *);
  903.  
  904. #if 0 || defined(LZO_DEBUG)
  905. #include <stdio.h>
  906. static lzo_bool __lzo_assert_fail(const char *s, unsigned line)
  907. {
  908. #if defined(__palmos__)
  909.     printf("LZO assertion failed in line %u: '%s'\n",line,s);
  910. #else
  911.     fprintf(stderr,"LZO assertion failed in line %u: '%s'\n",line,s);
  912. #endif
  913.     return 0;
  914. }
  915. #  define __lzo_assert(x)   ((x) ? 1 : __lzo_assert_fail(#x,__LINE__))
  916. #else
  917. #  define __lzo_assert(x)   ((x) ? 1 : 0)
  918. #endif
  919.  
  920. #undef COMPILE_TIME_ASSERT
  921. #if 0
  922. #  define COMPILE_TIME_ASSERT(expr)     r &= __lzo_assert(expr)
  923. #else
  924. #  define COMPILE_TIME_ASSERT(expr)     LZO_COMPILE_TIME_ASSERT(expr)
  925. #endif
  926.  
  927. static lzo_bool basic_integral_check(void)
  928. {
  929.     lzo_bool r = 1;
  930.  
  931.     COMPILE_TIME_ASSERT(CHAR_BIT == 8);
  932.     COMPILE_TIME_ASSERT(sizeof(char) == 1);
  933.     COMPILE_TIME_ASSERT(sizeof(short) >= 2);
  934.     COMPILE_TIME_ASSERT(sizeof(long) >= 4);
  935.     COMPILE_TIME_ASSERT(sizeof(int) >= sizeof(short));
  936.     COMPILE_TIME_ASSERT(sizeof(long) >= sizeof(int));
  937.  
  938.     COMPILE_TIME_ASSERT(sizeof(lzo_uint) == sizeof(lzo_int));
  939.     COMPILE_TIME_ASSERT(sizeof(lzo_uint32) == sizeof(lzo_int32));
  940.  
  941.     COMPILE_TIME_ASSERT(sizeof(lzo_uint32) >= 4);
  942.     COMPILE_TIME_ASSERT(sizeof(lzo_uint32) >= sizeof(unsigned));
  943. #if defined(__LZO_STRICT_16BIT)
  944.     COMPILE_TIME_ASSERT(sizeof(lzo_uint) == 2);
  945. #else
  946.     COMPILE_TIME_ASSERT(sizeof(lzo_uint) >= 4);
  947.     COMPILE_TIME_ASSERT(sizeof(lzo_uint) >= sizeof(unsigned));
  948. #endif
  949.  
  950. #if (USHRT_MAX == 65535u)
  951.     COMPILE_TIME_ASSERT(sizeof(short) == 2);
  952. #elif (USHRT_MAX == LZO_0xffffffffL)
  953.     COMPILE_TIME_ASSERT(sizeof(short) == 4);
  954. #elif (USHRT_MAX >= LZO_0xffffffffL)
  955.     COMPILE_TIME_ASSERT(sizeof(short) > 4);
  956. #endif
  957. #if (UINT_MAX == 65535u)
  958.     COMPILE_TIME_ASSERT(sizeof(int) == 2);
  959. #elif (UINT_MAX == LZO_0xffffffffL)
  960.     COMPILE_TIME_ASSERT(sizeof(int) == 4);
  961. #elif (UINT_MAX >= LZO_0xffffffffL)
  962.     COMPILE_TIME_ASSERT(sizeof(int) > 4);
  963. #endif
  964. #if (ULONG_MAX == 65535ul)
  965.     COMPILE_TIME_ASSERT(sizeof(long) == 2);
  966. #elif (ULONG_MAX == LZO_0xffffffffL)
  967.     COMPILE_TIME_ASSERT(sizeof(long) == 4);
  968. #elif (ULONG_MAX >= LZO_0xffffffffL)
  969.     COMPILE_TIME_ASSERT(sizeof(long) > 4);
  970. #endif
  971.  
  972. #if defined(SIZEOF_UNSIGNED)
  973.     COMPILE_TIME_ASSERT(SIZEOF_UNSIGNED == sizeof(unsigned));
  974. #endif
  975. #if defined(SIZEOF_UNSIGNED_LONG)
  976.     COMPILE_TIME_ASSERT(SIZEOF_UNSIGNED_LONG == sizeof(unsigned long));
  977. #endif
  978. #if defined(SIZEOF_UNSIGNED_SHORT)
  979.     COMPILE_TIME_ASSERT(SIZEOF_UNSIGNED_SHORT == sizeof(unsigned short));
  980. #endif
  981. #if !defined(__LZO_IN_MINILZO)
  982. #if defined(SIZEOF_SIZE_T)
  983.     COMPILE_TIME_ASSERT(SIZEOF_SIZE_T == sizeof(size_t));
  984. #endif
  985. #endif
  986.  
  987.     COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned char));
  988.     COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned short));
  989.     COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned));
  990.     COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned long));
  991.     COMPILE_TIME_ASSERT(IS_SIGNED(short));
  992.     COMPILE_TIME_ASSERT(IS_SIGNED(int));
  993.     COMPILE_TIME_ASSERT(IS_SIGNED(long));
  994.  
  995.     COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_uint32));
  996.     COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_uint));
  997.     COMPILE_TIME_ASSERT(IS_SIGNED(lzo_int32));
  998.     COMPILE_TIME_ASSERT(IS_SIGNED(lzo_int));
  999.  
  1000.     COMPILE_TIME_ASSERT(INT_MAX    == LZO_STYPE_MAX(sizeof(int)));
  1001.     COMPILE_TIME_ASSERT(UINT_MAX   == LZO_UTYPE_MAX(sizeof(unsigned)));
  1002.     COMPILE_TIME_ASSERT(LONG_MAX   == LZO_STYPE_MAX(sizeof(long)));
  1003.     COMPILE_TIME_ASSERT(ULONG_MAX  == LZO_UTYPE_MAX(sizeof(unsigned long)));
  1004.     COMPILE_TIME_ASSERT(SHRT_MAX   == LZO_STYPE_MAX(sizeof(short)));
  1005.     COMPILE_TIME_ASSERT(USHRT_MAX  == LZO_UTYPE_MAX(sizeof(unsigned short)));
  1006.     COMPILE_TIME_ASSERT(LZO_UINT32_MAX == LZO_UTYPE_MAX(sizeof(lzo_uint32)));
  1007.     COMPILE_TIME_ASSERT(LZO_UINT_MAX   == LZO_UTYPE_MAX(sizeof(lzo_uint)));
  1008. #if !defined(__LZO_IN_MINILZO)
  1009.     COMPILE_TIME_ASSERT(SIZE_T_MAX     == LZO_UTYPE_MAX(sizeof(size_t)));
  1010. #endif
  1011.  
  1012.     r &= __lzo_assert(LZO_BYTE(257) == 1);
  1013.  
  1014.     return r;
  1015. }
  1016.  
  1017. static lzo_bool basic_ptr_check(void)
  1018. {
  1019.     lzo_bool r = 1;
  1020.  
  1021.     COMPILE_TIME_ASSERT(sizeof(char *) >= sizeof(int));
  1022.     COMPILE_TIME_ASSERT(sizeof(lzo_byte *) >= sizeof(char *));
  1023.  
  1024.     COMPILE_TIME_ASSERT(sizeof(lzo_voidp) == sizeof(lzo_byte *));
  1025.     COMPILE_TIME_ASSERT(sizeof(lzo_voidp) == sizeof(lzo_voidpp));
  1026.     COMPILE_TIME_ASSERT(sizeof(lzo_voidp) == sizeof(lzo_bytepp));
  1027.     COMPILE_TIME_ASSERT(sizeof(lzo_voidp) >= sizeof(lzo_uint));
  1028.  
  1029.     COMPILE_TIME_ASSERT(sizeof(lzo_ptr_t) == sizeof(lzo_voidp));
  1030.     COMPILE_TIME_ASSERT(sizeof(lzo_ptr_t) == sizeof(lzo_sptr_t));
  1031.     COMPILE_TIME_ASSERT(sizeof(lzo_ptr_t) >= sizeof(lzo_uint));
  1032.  
  1033.     COMPILE_TIME_ASSERT(sizeof(lzo_ptrdiff_t) >= 4);
  1034.     COMPILE_TIME_ASSERT(sizeof(lzo_ptrdiff_t) >= sizeof(ptrdiff_t));
  1035.  
  1036.     COMPILE_TIME_ASSERT(sizeof(ptrdiff_t) >= sizeof(size_t));
  1037.     COMPILE_TIME_ASSERT(sizeof(lzo_ptrdiff_t) >= sizeof(lzo_uint));
  1038.  
  1039. #if defined(SIZEOF_CHAR_P)
  1040.     COMPILE_TIME_ASSERT(SIZEOF_CHAR_P == sizeof(char *));
  1041. #endif
  1042. #if defined(SIZEOF_PTRDIFF_T)
  1043.     COMPILE_TIME_ASSERT(SIZEOF_PTRDIFF_T == sizeof(ptrdiff_t));
  1044. #endif
  1045.  
  1046.     COMPILE_TIME_ASSERT(IS_SIGNED(ptrdiff_t));
  1047.     COMPILE_TIME_ASSERT(IS_UNSIGNED(size_t));
  1048.     COMPILE_TIME_ASSERT(IS_SIGNED(lzo_ptrdiff_t));
  1049.     COMPILE_TIME_ASSERT(IS_SIGNED(lzo_sptr_t));
  1050.     COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_ptr_t));
  1051.     COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_moff_t));
  1052.  
  1053.     return r;
  1054. }
  1055.  
  1056. static lzo_bool ptr_check(void)
  1057. {
  1058.     lzo_bool r = 1;
  1059.     int i;
  1060.     char _wrkmem[10 * sizeof(lzo_byte *) + sizeof(lzo_full_align_t)];
  1061.     lzo_bytep wrkmem;
  1062.     lzo_bytepp dict;
  1063.     unsigned char x[4 * sizeof(lzo_full_align_t)];
  1064.     long d;
  1065.     lzo_full_align_t a;
  1066.     lzo_full_align_t u;
  1067.  
  1068.     for (i = 0; i < (int) sizeof(x); ++i)
  1069.         x[i] = LZO_BYTE(i);
  1070.  
  1071.     wrkmem = LZO_PTR_ALIGN_UP((lzo_byte *)_wrkmem,sizeof(lzo_full_align_t));
  1072.  
  1073. #if 0
  1074.     dict = (lzo_bytepp) wrkmem;
  1075. #else
  1076.  
  1077.     u.a_lzo_bytep = wrkmem; dict = u.a_lzo_bytepp;
  1078. #endif
  1079.  
  1080.     d = (long) ((const lzo_bytep) dict - (const lzo_bytep) _wrkmem);
  1081.     r &= __lzo_assert(d >= 0);
  1082.     r &= __lzo_assert(d < (long) sizeof(lzo_full_align_t));
  1083.  
  1084.     memset(&a,0,sizeof(a));
  1085.     r &= __lzo_assert(a.a_lzo_voidp == NULL);
  1086.  
  1087.     memset(&a,0xff,sizeof(a));
  1088.     r &= __lzo_assert(a.a_ushort == USHRT_MAX);
  1089.     r &= __lzo_assert(a.a_uint == UINT_MAX);
  1090.     r &= __lzo_assert(a.a_ulong == ULONG_MAX);
  1091.     r &= __lzo_assert(a.a_lzo_uint == LZO_UINT_MAX);
  1092.     r &= __lzo_assert(a.a_lzo_uint32 == LZO_UINT32_MAX);
  1093.  
  1094.     if (r == 1)
  1095.     {
  1096.         for (i = 0; i < 8; ++i)
  1097.             r &= __lzo_assert((const lzo_voidp) (&dict[i]) == (const lzo_voidp) (&wrkmem[i * sizeof(lzo_byte *)]));
  1098.     }
  1099.  
  1100.     memset(&a,0,sizeof(a));
  1101.     r &= __lzo_assert(a.a_char_p == NULL);
  1102.     r &= __lzo_assert(a.a_lzo_bytep == NULL);
  1103.     r &= __lzo_assert(NULL == (void *)0);
  1104.     if (r == 1)
  1105.     {
  1106.         for (i = 0; i < 10; ++i)
  1107.             dict[i] = wrkmem;
  1108.         BZERO8_PTR(dict+1,sizeof(dict[0]),8);
  1109.         r &= __lzo_assert(dict[0] == wrkmem);
  1110.         for (i = 1; i < 9; ++i)
  1111.             r &= __lzo_assert(dict[i] == NULL);
  1112.         r &= __lzo_assert(dict[9] == wrkmem);
  1113.     }
  1114.  
  1115.     if (r == 1)
  1116.     {
  1117.         unsigned k = 1;
  1118.         const unsigned n = (unsigned) sizeof(lzo_uint32);
  1119.         lzo_byte *p0;
  1120.         lzo_byte *p1;
  1121.  
  1122.         k += __lzo_align_gap(&x[k],n);
  1123.         p0 = (lzo_bytep) &x[k];
  1124. #if defined(PTR_LINEAR)
  1125.         r &= __lzo_assert((PTR_LINEAR(p0) & (n-1)) == 0);
  1126. #else
  1127.         r &= __lzo_assert(n == 4);
  1128.         r &= __lzo_assert(PTR_ALIGNED_4(p0));
  1129. #endif
  1130.  
  1131.         r &= __lzo_assert(k >= 1);
  1132.         p1 = (lzo_bytep) &x[1];
  1133.         r &= __lzo_assert(PTR_GE(p0,p1));
  1134.  
  1135.         r &= __lzo_assert(k < 1+n);
  1136.         p1 = (lzo_bytep) &x[1+n];
  1137.         r &= __lzo_assert(PTR_LT(p0,p1));
  1138.  
  1139.         if (r == 1)
  1140.         {
  1141.             lzo_uint32 v0, v1;
  1142. #if 0
  1143.             v0 = * (lzo_uint32 *) &x[k];
  1144.             v1 = * (lzo_uint32 *) &x[k+n];
  1145. #else
  1146.  
  1147.             u.a_uchar_p = &x[k];
  1148.             v0 = *u.a_lzo_uint32_p;
  1149.             u.a_uchar_p = &x[k+n];
  1150.             v1 = *u.a_lzo_uint32_p;
  1151. #endif
  1152.             r &= __lzo_assert(v0 > 0);
  1153.             r &= __lzo_assert(v1 > 0);
  1154.         }
  1155.     }
  1156.  
  1157.     return r;
  1158. }
  1159.  
  1160. LZO_PUBLIC(int)
  1161. _lzo_config_check(void)
  1162. {
  1163.     lzo_bool r = 1;
  1164.     int i;
  1165.     union {
  1166.         lzo_uint32 a;
  1167.         unsigned short b;
  1168.         lzo_uint32 aa[4];
  1169.         unsigned char x[4*sizeof(lzo_full_align_t)];
  1170.     } u;
  1171.  
  1172.     COMPILE_TIME_ASSERT( (int) ((unsigned char) ((signed char) -1)) == 255);
  1173.     COMPILE_TIME_ASSERT( (((unsigned char)128) << (int)(8*sizeof(int)-8)) < 0);
  1174.  
  1175. #if 0
  1176.     r &= __lzo_assert((const void *)&u == (const void *)&u.a);
  1177.     r &= __lzo_assert((const void *)&u == (const void *)&u.b);
  1178.     r &= __lzo_assert((const void *)&u == (const void *)&u.x[0]);
  1179.     r &= __lzo_assert((const void *)&u == (const void *)&u.aa[0]);
  1180. #endif
  1181.  
  1182.     r &= basic_integral_check();
  1183.     r &= basic_ptr_check();
  1184.     if (r != 1)
  1185.         return LZO_E_ERROR;
  1186.  
  1187.     u.a = 0; u.b = 0;
  1188.     for (i = 0; i < (int) sizeof(u.x); ++i)
  1189.         u.x[i] = LZO_BYTE(i);
  1190.  
  1191. #if defined(LZO_BYTE_ORDER)
  1192.     if (r == 1)
  1193.     {
  1194. #  if (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
  1195.         lzo_uint32 a = (lzo_uint32) (u.a & LZO_0xffffffffL);
  1196.         unsigned short b = (unsigned short) (u.b & 0xffff);
  1197.         r &= __lzo_assert(a == 0x03020100L);
  1198.         r &= __lzo_assert(b == 0x0100);
  1199. #  elif (LZO_BYTE_ORDER == LZO_BIG_ENDIAN)
  1200.         lzo_uint32 a = u.a >> (8 * sizeof(u.a) - 32);
  1201.         unsigned short b = u.b >> (8 * sizeof(u.b) - 16);
  1202.         r &= __lzo_assert(a == 0x00010203L);
  1203.         r &= __lzo_assert(b == 0x0001);
  1204. #  else
  1205. #    error "invalid LZO_BYTE_ORDER"
  1206. #  endif
  1207.     }
  1208. #endif
  1209.  
  1210. #if defined(LZO_UNALIGNED_OK_2)
  1211.     COMPILE_TIME_ASSERT(sizeof(short) == 2);
  1212.     if (r == 1)
  1213.     {
  1214.         unsigned short b[4];
  1215.  
  1216.         for (i = 0; i < 4; ++i)
  1217.             b[i] = * (const unsigned short *) &u.x[i];
  1218.  
  1219. #  if (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
  1220.         r &= __lzo_assert(b[0] == 0x0100);
  1221.         r &= __lzo_assert(b[1] == 0x0201);
  1222.         r &= __lzo_assert(b[2] == 0x0302);
  1223.         r &= __lzo_assert(b[3] == 0x0403);
  1224. #  elif (LZO_BYTE_ORDER == LZO_BIG_ENDIAN)
  1225.         r &= __lzo_assert(b[0] == 0x0001);
  1226.         r &= __lzo_assert(b[1] == 0x0102);
  1227.         r &= __lzo_assert(b[2] == 0x0203);
  1228.         r &= __lzo_assert(b[3] == 0x0304);
  1229. #  endif
  1230.     }
  1231. #endif
  1232.  
  1233. #if defined(LZO_UNALIGNED_OK_4)
  1234.     COMPILE_TIME_ASSERT(sizeof(lzo_uint32) == 4);
  1235.     if (r == 1)
  1236.     {
  1237.         lzo_uint32 a[4];
  1238.  
  1239.         for (i = 0; i < 4; ++i)
  1240.             a[i] = * (const lzo_uint32 *) &u.x[i];
  1241.  
  1242. #  if (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
  1243.         r &= __lzo_assert(a[0] == 0x03020100L);
  1244.         r &= __lzo_assert(a[1] == 0x04030201L);
  1245.         r &= __lzo_assert(a[2] == 0x05040302L);
  1246.         r &= __lzo_assert(a[3] == 0x06050403L);
  1247. #  elif (LZO_BYTE_ORDER == LZO_BIG_ENDIAN)
  1248.         r &= __lzo_assert(a[0] == 0x00010203L);
  1249.         r &= __lzo_assert(a[1] == 0x01020304L);
  1250.         r &= __lzo_assert(a[2] == 0x02030405L);
  1251.         r &= __lzo_assert(a[3] == 0x03040506L);
  1252. #  endif
  1253.     }
  1254. #endif
  1255.  
  1256. #if defined(LZO_ALIGNED_OK_4)
  1257.     COMPILE_TIME_ASSERT(sizeof(lzo_uint32) == 4);
  1258. #endif
  1259.  
  1260.     COMPILE_TIME_ASSERT(lzo_sizeof_dict_t == sizeof(lzo_dict_t));
  1261.  
  1262. #if defined(__LZO_IN_MINLZO)
  1263.     if (r == 1)
  1264.     {
  1265.         lzo_uint32 adler;
  1266.         adler = lzo_adler32(0, NULL, 0);
  1267.         adler = lzo_adler32(adler, lzo_copyright(), 200);
  1268.         r &= __lzo_assert(adler == 0xc76f1751L);
  1269.     }
  1270. #endif
  1271.  
  1272.     if (r == 1)
  1273.     {
  1274.         r &= __lzo_assert(!schedule_insns_bug());
  1275.     }
  1276.  
  1277.     if (r == 1)
  1278.     {
  1279.         static int x[3];
  1280.         static unsigned xn = 3;
  1281.         register unsigned j;
  1282.  
  1283.         for (j = 0; j < xn; ++j)
  1284.             x[j] = (int)j - 3;
  1285.         r &= __lzo_assert(!strength_reduce_bug(x));
  1286.     }
  1287.  
  1288.     if (r == 1)
  1289.     {
  1290.         r &= ptr_check();
  1291.     }
  1292.  
  1293.     return r == 1 ? LZO_E_OK : LZO_E_ERROR;
  1294. }
  1295.  
  1296. static lzo_bool schedule_insns_bug(void)
  1297. {
  1298. #if defined(__LZO_CHECKER)
  1299.     return 0;
  1300. #else
  1301.     const int clone[] = {1, 2, 0};
  1302.     const int *q;
  1303.     q = clone;
  1304.     return (*q) ? 0 : 1;
  1305. #endif
  1306. }
  1307.  
  1308. static lzo_bool strength_reduce_bug(int *x)
  1309. {
  1310.     return x[0] != -3 || x[1] != -2 || x[2] != -1;
  1311. }
  1312.  
  1313. #undef COMPILE_TIME_ASSERT
  1314.  
  1315. int __lzo_init_done = 0;
  1316.  
  1317. LZO_PUBLIC(int)
  1318. __lzo_init2(unsigned v, int s1, int s2, int s3, int s4, int s5,
  1319.             int s6, int s7, int s8, int s9)
  1320. {
  1321.     int r;
  1322.  
  1323.     __lzo_init_done = 1;
  1324.  
  1325.     if (v == 0)
  1326.         return LZO_E_ERROR;
  1327.  
  1328.     r = (s1 == -1 || s1 == (int) sizeof(short)) &&
  1329.         (s2 == -1 || s2 == (int) sizeof(int)) &&
  1330.         (s3 == -1 || s3 == (int) sizeof(long)) &&
  1331.         (s4 == -1 || s4 == (int) sizeof(lzo_uint32)) &&
  1332.         (s5 == -1 || s5 == (int) sizeof(lzo_uint)) &&
  1333.         (s6 == -1 || s6 == (int) lzo_sizeof_dict_t) &&
  1334.         (s7 == -1 || s7 == (int) sizeof(char *)) &&
  1335.         (s8 == -1 || s8 == (int) sizeof(lzo_voidp)) &&
  1336.         (s9 == -1 || s9 == (int) sizeof(lzo_compress_t));
  1337.     if (!r)
  1338.         return LZO_E_ERROR;
  1339.  
  1340.     r = _lzo_config_check();
  1341.     if (r != LZO_E_OK)
  1342.         return r;
  1343.  
  1344.     return r;
  1345. }
  1346.  
  1347. #if !defined(__LZO_IN_MINILZO)
  1348.  
  1349. LZO_EXTERN(int)
  1350. __lzo_init(unsigned v,int s1,int s2,int s3,int s4,int s5,int s6,int s7);
  1351.  
  1352. LZO_PUBLIC(int)
  1353. __lzo_init(unsigned v,int s1,int s2,int s3,int s4,int s5,int s6,int s7)
  1354. {
  1355.     if (v == 0 || v > 0x1010)
  1356.         return LZO_E_ERROR;
  1357.     return __lzo_init2(v,s1,s2,s3,s4,s5,-1,-1,s6,s7);
  1358. }
  1359.  
  1360. #endif
  1361.  
  1362. #define do_compress         _lzo1x_1_do_compress
  1363.  
  1364. #define LZO_NEED_DICT_H
  1365. #define D_BITS          14
  1366. #define D_INDEX1(d,p)       d = DM((0x21*DX3(p,5,5,6)) >> 5)
  1367. #define D_INDEX2(d,p)       d = (d & (D_MASK & 0x7ff)) ^ (D_HIGH | 0x1f)
  1368.  
  1369. #ifndef __LZO_CONFIG1X_H
  1370. #define __LZO_CONFIG1X_H
  1371.  
  1372. #if !defined(LZO1X) && !defined(LZO1Y) && !defined(LZO1Z)
  1373. #  define LZO1X
  1374. #endif
  1375.  
  1376. #if !defined(__LZO_IN_MINILZO)
  1377. #include <lzo1x.h>
  1378. #endif
  1379.  
  1380. #define LZO_EOF_CODE
  1381. #undef LZO_DETERMINISTIC
  1382.  
  1383. #define M1_MAX_OFFSET   0x0400
  1384. #ifndef M2_MAX_OFFSET
  1385. #define M2_MAX_OFFSET   0x0800
  1386. #endif
  1387. #define M3_MAX_OFFSET   0x4000
  1388. #define M4_MAX_OFFSET   0xbfff
  1389.  
  1390. #define MX_MAX_OFFSET   (M1_MAX_OFFSET + M2_MAX_OFFSET)
  1391.  
  1392. #define M1_MIN_LEN      2
  1393. #define M1_MAX_LEN      2
  1394. #define M2_MIN_LEN      3
  1395. #ifndef M2_MAX_LEN
  1396. #define M2_MAX_LEN      8
  1397. #endif
  1398. #define M3_MIN_LEN      3
  1399. #define M3_MAX_LEN      33
  1400. #define M4_MIN_LEN      3
  1401. #define M4_MAX_LEN      9
  1402.  
  1403. #define M1_MARKER       0
  1404. #define M2_MARKER       64
  1405. #define M3_MARKER       32
  1406. #define M4_MARKER       16
  1407.  
  1408. #ifndef MIN_LOOKAHEAD
  1409. #define MIN_LOOKAHEAD       (M2_MAX_LEN + 1)
  1410. #endif
  1411.  
  1412. #if defined(LZO_NEED_DICT_H)
  1413.  
  1414. #ifndef LZO_HASH
  1415. #define LZO_HASH            LZO_HASH_LZO_INCREMENTAL_B
  1416. #endif
  1417. #define DL_MIN_LEN          M2_MIN_LEN
  1418.  
  1419. #ifndef __LZO_DICT_H
  1420. #define __LZO_DICT_H
  1421.  
  1422. #ifdef __cplusplus
  1423. extern "C" {
  1424. #endif
  1425.  
  1426. #if !defined(D_BITS) && defined(DBITS)
  1427. #  define D_BITS        DBITS
  1428. #endif
  1429. #if !defined(D_BITS)
  1430. #  error "D_BITS is not defined"
  1431. #endif
  1432. #if (D_BITS < 16)
  1433. #  define D_SIZE        LZO_SIZE(D_BITS)
  1434. #  define D_MASK        LZO_MASK(D_BITS)
  1435. #else
  1436. #  define D_SIZE        LZO_USIZE(D_BITS)
  1437. #  define D_MASK        LZO_UMASK(D_BITS)
  1438. #endif
  1439. #define D_HIGH          ((D_MASK >> 1) + 1)
  1440.  
  1441. #if !defined(DD_BITS)
  1442. #  define DD_BITS       0
  1443. #endif
  1444. #define DD_SIZE         LZO_SIZE(DD_BITS)
  1445. #define DD_MASK         LZO_MASK(DD_BITS)
  1446.  
  1447. #if !defined(DL_BITS)
  1448. #  define DL_BITS       (D_BITS - DD_BITS)
  1449. #endif
  1450. #if (DL_BITS < 16)
  1451. #  define DL_SIZE       LZO_SIZE(DL_BITS)
  1452. #  define DL_MASK       LZO_MASK(DL_BITS)
  1453. #else
  1454. #  define DL_SIZE       LZO_USIZE(DL_BITS)
  1455. #  define DL_MASK       LZO_UMASK(DL_BITS)
  1456. #endif
  1457.  
  1458. #if (D_BITS != DL_BITS + DD_BITS)
  1459. #  error "D_BITS does not match"
  1460. #endif
  1461. #if (D_BITS < 8 || D_BITS > 18)
  1462. #  error "invalid D_BITS"
  1463. #endif
  1464. #if (DL_BITS < 8 || DL_BITS > 20)
  1465. #  error "invalid DL_BITS"
  1466. #endif
  1467. #if (DD_BITS < 0 || DD_BITS > 6)
  1468. #  error "invalid DD_BITS"
  1469. #endif
  1470.  
  1471. #if !defined(DL_MIN_LEN)
  1472. #  define DL_MIN_LEN    3
  1473. #endif
  1474. #if !defined(DL_SHIFT)
  1475. #  define DL_SHIFT      ((DL_BITS + (DL_MIN_LEN - 1)) / DL_MIN_LEN)
  1476. #endif
  1477.  
  1478. #define LZO_HASH_GZIP                   1
  1479. #define LZO_HASH_GZIP_INCREMENTAL       2
  1480. #define LZO_HASH_LZO_INCREMENTAL_A      3
  1481. #define LZO_HASH_LZO_INCREMENTAL_B      4
  1482.  
  1483. #if !defined(LZO_HASH)
  1484. #  error "choose a hashing strategy"
  1485. #endif
  1486.  
  1487. #if (DL_MIN_LEN == 3)
  1488. #  define _DV2_A(p,shift1,shift2) \
  1489.     (((( (lzo_uint32)((p)[0]) << shift1) ^ (p)[1]) << shift2) ^ (p)[2])
  1490. #  define _DV2_B(p,shift1,shift2) \
  1491.     (((( (lzo_uint32)((p)[2]) << shift1) ^ (p)[1]) << shift2) ^ (p)[0])
  1492. #  define _DV3_B(p,shift1,shift2,shift3) \
  1493.     ((_DV2_B((p)+1,shift1,shift2) << (shift3)) ^ (p)[0])
  1494. #elif (DL_MIN_LEN == 2)
  1495. #  define _DV2_A(p,shift1,shift2) \
  1496.     (( (lzo_uint32)(p[0]) << shift1) ^ p[1])
  1497. #  define _DV2_B(p,shift1,shift2) \
  1498.     (( (lzo_uint32)(p[1]) << shift1) ^ p[2])
  1499. #else
  1500. #  error "invalid DL_MIN_LEN"
  1501. #endif
  1502. #define _DV_A(p,shift)      _DV2_A(p,shift,shift)
  1503. #define _DV_B(p,shift)      _DV2_B(p,shift,shift)
  1504. #define DA2(p,s1,s2) \
  1505.     (((((lzo_uint32)((p)[2]) << (s2)) + (p)[1]) << (s1)) + (p)[0])
  1506. #define DS2(p,s1,s2) \
  1507.     (((((lzo_uint32)((p)[2]) << (s2)) - (p)[1]) << (s1)) - (p)[0])
  1508. #define DX2(p,s1,s2) \
  1509.     (((((lzo_uint32)((p)[2]) << (s2)) ^ (p)[1]) << (s1)) ^ (p)[0])
  1510. #define DA3(p,s1,s2,s3) ((DA2((p)+1,s2,s3) << (s1)) + (p)[0])
  1511. #define DS3(p,s1,s2,s3) ((DS2((p)+1,s2,s3) << (s1)) - (p)[0])
  1512. #define DX3(p,s1,s2,s3) ((DX2((p)+1,s2,s3) << (s1)) ^ (p)[0])
  1513. #define DMS(v,s)        ((lzo_uint) (((v) & (D_MASK >> (s))) << (s)))
  1514. #define DM(v)           DMS(v,0)
  1515.  
  1516. #if (LZO_HASH == LZO_HASH_GZIP)
  1517. #  define _DINDEX(dv,p)     (_DV_A((p),DL_SHIFT))
  1518.  
  1519. #elif (LZO_HASH == LZO_HASH_GZIP_INCREMENTAL)
  1520. #  define __LZO_HASH_INCREMENTAL
  1521. #  define DVAL_FIRST(dv,p)  dv = _DV_A((p),DL_SHIFT)
  1522. #  define DVAL_NEXT(dv,p)   dv = (((dv) << DL_SHIFT) ^ p[2])
  1523. #  define _DINDEX(dv,p)     (dv)
  1524. #  define DVAL_LOOKAHEAD    DL_MIN_LEN
  1525.  
  1526. #elif (LZO_HASH == LZO_HASH_LZO_INCREMENTAL_A)
  1527. #  define __LZO_HASH_INCREMENTAL
  1528. #  define DVAL_FIRST(dv,p)  dv = _DV_A((p),5)
  1529. #  define DVAL_NEXT(dv,p) \
  1530.     dv ^= (lzo_uint32)(p[-1]) << (2*5); dv = (((dv) << 5) ^ p[2])
  1531. #  define _DINDEX(dv,p)     ((0x9f5f * (dv)) >> 5)
  1532. #  define DVAL_LOOKAHEAD    DL_MIN_LEN
  1533.  
  1534. #elif (LZO_HASH == LZO_HASH_LZO_INCREMENTAL_B)
  1535. #  define __LZO_HASH_INCREMENTAL
  1536. #  define DVAL_FIRST(dv,p)  dv = _DV_B((p),5)
  1537. #  define DVAL_NEXT(dv,p) \
  1538.     dv ^= p[-1]; dv = (((dv) >> 5) ^ ((lzo_uint32)(p[2]) << (2*5)))
  1539. #  define _DINDEX(dv,p)     ((0x9f5f * (dv)) >> 5)
  1540. #  define DVAL_LOOKAHEAD    DL_MIN_LEN
  1541.  
  1542. #else
  1543. #  error "choose a hashing strategy"
  1544. #endif
  1545.  
  1546. #ifndef DINDEX
  1547. #define DINDEX(dv,p)        ((lzo_uint)((_DINDEX(dv,p)) & DL_MASK) << DD_BITS)
  1548. #endif
  1549. #if !defined(DINDEX1) && defined(D_INDEX1)
  1550. #define DINDEX1             D_INDEX1
  1551. #endif
  1552. #if !defined(DINDEX2) && defined(D_INDEX2)
  1553. #define DINDEX2             D_INDEX2
  1554. #endif
  1555.  
  1556. #if !defined(__LZO_HASH_INCREMENTAL)
  1557. #  define DVAL_FIRST(dv,p)  ((void) 0)
  1558. #  define DVAL_NEXT(dv,p)   ((void) 0)
  1559. #  define DVAL_LOOKAHEAD    0
  1560. #endif
  1561.  
  1562. #if !defined(DVAL_ASSERT)
  1563. #if defined(__LZO_HASH_INCREMENTAL) && !defined(NDEBUG)
  1564.     static void DVAL_ASSERT(lzo_uint32 dv, const lzo_byte *p)
  1565.     {
  1566.         lzo_uint32 df;
  1567.         DVAL_FIRST(df,(p));
  1568.         assert(DINDEX(dv,p) == DINDEX(df,p));
  1569.     }
  1570. #else
  1571. #  define DVAL_ASSERT(dv,p) ((void) 0)
  1572. #endif
  1573. #endif
  1574.  
  1575. #if defined(LZO_DICT_USE_PTR)
  1576. #  define DENTRY(p,in)                          (p)
  1577. #  define GINDEX(m_pos,m_off,dict,dindex,in)    m_pos = dict[dindex]
  1578. #else
  1579. #  define DENTRY(p,in)                          ((lzo_uint) ((p)-(in)))
  1580. #  define GINDEX(m_pos,m_off,dict,dindex,in)    m_off = dict[dindex]
  1581. #endif
  1582.  
  1583. #if (DD_BITS == 0)
  1584.  
  1585. #  define UPDATE_D(dict,drun,dv,p,in)       dict[ DINDEX(dv,p) ] = DENTRY(p,in)
  1586. #  define UPDATE_I(dict,drun,index,p,in)    dict[index] = DENTRY(p,in)
  1587. #  define UPDATE_P(ptr,drun,p,in)           (ptr)[0] = DENTRY(p,in)
  1588.  
  1589. #else
  1590.  
  1591. #  define UPDATE_D(dict,drun,dv,p,in)   \
  1592.     dict[ DINDEX(dv,p) + drun++ ] = DENTRY(p,in); drun &= DD_MASK
  1593. #  define UPDATE_I(dict,drun,index,p,in)    \
  1594.     dict[ (index) + drun++ ] = DENTRY(p,in); drun &= DD_MASK
  1595. #  define UPDATE_P(ptr,drun,p,in)   \
  1596.     (ptr) [ drun++ ] = DENTRY(p,in); drun &= DD_MASK
  1597.  
  1598. #endif
  1599.  
  1600. #if defined(LZO_DICT_USE_PTR)
  1601.  
  1602. #define LZO_CHECK_FPOS_DET(m_pos,m_off,in,ip,max_offset) \
  1603.     (m_pos == NULL || (m_off = (lzo_moff_t) (ip - m_pos)) > max_offset)
  1604.  
  1605. #define LZO_CHECK_FPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
  1606.     (BOUNDS_CHECKING_OFF_IN_EXPR( \
  1607.     (PTR_LT(m_pos,in) || \
  1608.     (m_off = (lzo_moff_t) PTR_DIFF(ip,m_pos)) <= 0 || \
  1609.     m_off > max_offset) ))
  1610.  
  1611. #else
  1612.  
  1613. #define LZO_CHECK_FPOS_DET(m_pos,m_off,in,ip,max_offset) \
  1614.     (m_off == 0 || \
  1615.     ((m_off = (lzo_moff_t) ((ip)-(in)) - m_off) > max_offset) || \
  1616.     (m_pos = (ip) - (m_off), 0) )
  1617.  
  1618. #define LZO_CHECK_FPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
  1619.     ((lzo_moff_t) ((ip)-(in)) <= m_off || \
  1620.     ((m_off = (lzo_moff_t) ((ip)-(in)) - m_off) > max_offset) || \
  1621.     (m_pos = (ip) - (m_off), 0) )
  1622.  
  1623. #endif
  1624.  
  1625. #if defined(LZO_DETERMINISTIC)
  1626. #  define LZO_CHECK_FPOS    LZO_CHECK_MPOS_DET
  1627. #else
  1628. #  define LZO_CHECK_FPOS    LZO_CHECK_MPOS_NON_DET
  1629. #endif
  1630.  
  1631. #ifdef __cplusplus
  1632. }
  1633. #endif
  1634.  
  1635. #endif
  1636.  
  1637. #endif
  1638.  
  1639. #endif
  1640.  
  1641. #define DO_COMPRESS     lzo1x_1_compress
  1642.  
  1643. static
  1644. lzo_uint do_compress     ( const lzo_byte *in , lzo_uint  in_len,
  1645.                           lzo_byte *out, lzo_uintp out_len,
  1646.                           lzo_voidp wrkmem )
  1647. {
  1648. #if 0 && defined(__GNUC__) && defined(__i386__)
  1649.     register const lzo_byte *ip __asm__("%esi");
  1650. #else
  1651.     register const lzo_byte *ip;
  1652. #endif
  1653.     lzo_byte *op;
  1654.     const lzo_byte * const in_end = in + in_len;
  1655.     const lzo_byte * const ip_end = in + in_len - M2_MAX_LEN - 5;
  1656.     const lzo_byte *ii;
  1657.     lzo_dict_p const dict = (lzo_dict_p) wrkmem;
  1658.  
  1659.     op = out;
  1660.     ip = in;
  1661.     ii = ip;
  1662.  
  1663.     ip += 4;
  1664.     for (;;)
  1665.     {
  1666. #if 0 && defined(__GNUC__) && defined(__i386__)
  1667.         register const lzo_byte *m_pos __asm__("%edi");
  1668. #else
  1669.         register const lzo_byte *m_pos;
  1670. #endif
  1671.         lzo_moff_t m_off;
  1672.         lzo_uint m_len;
  1673.         lzo_uint dindex;
  1674.  
  1675.         DINDEX1(dindex,ip);
  1676.         GINDEX(m_pos,m_off,dict,dindex,in);
  1677.         if (LZO_CHECK_FPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
  1678.             goto literal;
  1679. #if 1
  1680.         if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
  1681.             goto try_match;
  1682.         DINDEX2(dindex,ip);
  1683. #endif
  1684.         GINDEX(m_pos,m_off,dict,dindex,in);
  1685.         if (LZO_CHECK_FPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
  1686.             goto literal;
  1687.         if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
  1688.             goto try_match;
  1689.         goto literal;
  1690.  
  1691. try_match:
  1692. #if 1 && defined(LZO_UNALIGNED_OK_2)
  1693.         if (* (const lzo_ushortp) m_pos != * (const lzo_ushortp) ip)
  1694. #else
  1695.         if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
  1696. #endif
  1697.         {
  1698.         }
  1699.         else
  1700.         {
  1701.             if (m_pos[2] == ip[2])
  1702.             {
  1703. #if 0
  1704.                 if (m_off <= M2_MAX_OFFSET)
  1705.                     goto match;
  1706.                 if (lit <= 3)
  1707.                     goto match;
  1708.                 if (lit == 3)
  1709.                 {
  1710.                     assert(op - 2 > out); op[-2] |= LZO_BYTE(3);
  1711.                     *op++ = *ii++; *op++ = *ii++; *op++ = *ii++;
  1712.                     goto code_match;
  1713.                 }
  1714.                 if (m_pos[3] == ip[3])
  1715. #endif
  1716.                     goto match;
  1717.             }
  1718.             else
  1719.             {
  1720. #if 0
  1721. #if 0
  1722.                 if (m_off <= M1_MAX_OFFSET && lit > 0 && lit <= 3)
  1723. #else
  1724.                 if (m_off <= M1_MAX_OFFSET && lit == 3)
  1725. #endif
  1726.                 {
  1727.                     register lzo_uint t;
  1728.  
  1729.                     t = lit;
  1730.                     assert(op - 2 > out); op[-2] |= LZO_BYTE(t);
  1731.                     do *op++ = *ii++; while (--t > 0);
  1732.                     assert(ii == ip);
  1733.                     m_off -= 1;
  1734.                     *op++ = LZO_BYTE(M1_MARKER | ((m_off & 3) << 2));
  1735.                     *op++ = LZO_BYTE(m_off >> 2);
  1736.                     ip += 2;
  1737.                     goto match_done;
  1738.                 }
  1739. #endif
  1740.             }
  1741.         }
  1742.  
  1743.         literal:
  1744.         UPDATE_I(dict,0,dindex,ip,in);
  1745.         ++ip;
  1746.  
  1747.         if (ip >= ip_end)
  1748.             break;
  1749.  
  1750.         continue;
  1751.  
  1752. match:
  1753.         UPDATE_I(dict,0,dindex,ip,in);
  1754.         if (pd(ip,ii) > 0)
  1755.         {
  1756.             register lzo_uint t = pd(ip,ii);
  1757.  
  1758.             if (t <= 3)
  1759.             {
  1760.                 assert(op - 2 > out);
  1761.                 op[-2] |= LZO_BYTE(t);
  1762.             }
  1763.             else if (t <= 18)
  1764.                 *op++ = LZO_BYTE(t - 3);
  1765.             else
  1766.             {
  1767.                 register lzo_uint tt = t - 18;
  1768.  
  1769.                 *op++ = 0;
  1770.                 while (tt > 255)
  1771.                 {
  1772.                     tt -= 255;
  1773.                     *op++ = 0;
  1774.                 }
  1775.                 assert(tt > 0);
  1776.                 *op++ = LZO_BYTE(tt);
  1777.             }
  1778.             do *op++ = *ii++; while (--t > 0);
  1779.         }
  1780.  
  1781.         assert(ii == ip);
  1782.         ip += 3;
  1783.         if (m_pos[3] != *ip++ || m_pos[4] != *ip++ || m_pos[5] != *ip++ ||
  1784.             m_pos[6] != *ip++ || m_pos[7] != *ip++ || m_pos[8] != *ip++
  1785. #ifdef LZO1Y
  1786.             || m_pos[ 9] != *ip++ || m_pos[10] != *ip++ || m_pos[11] != *ip++
  1787.             || m_pos[12] != *ip++ || m_pos[13] != *ip++ || m_pos[14] != *ip++
  1788. #endif
  1789.             )
  1790.         {
  1791.             --ip;
  1792.             m_len = ip - ii;
  1793.             assert(m_len >= 3); assert(m_len <= M2_MAX_LEN);
  1794.  
  1795.             if (m_off <= M2_MAX_OFFSET)
  1796.             {
  1797.                 m_off -= 1;
  1798. #if defined(LZO1X)
  1799.                 *op++ = LZO_BYTE(((m_len - 1) << 5) | ((m_off & 7) << 2));
  1800.                 *op++ = LZO_BYTE(m_off >> 3);
  1801. #elif defined(LZO1Y)
  1802.                 *op++ = LZO_BYTE(((m_len + 1) << 4) | ((m_off & 3) << 2));
  1803.                 *op++ = LZO_BYTE(m_off >> 2);
  1804. #endif
  1805.             }
  1806.             else if (m_off <= M3_MAX_OFFSET)
  1807.             {
  1808.                 m_off -= 1;
  1809.                 *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
  1810.                 goto m3_m4_offset;
  1811.             }
  1812.             else
  1813. #if defined(LZO1X)
  1814.             {
  1815.                 m_off -= 0x4000;
  1816.                 assert(m_off > 0); assert(m_off <= 0x7fff);
  1817.                 *op++ = LZO_BYTE(M4_MARKER |
  1818.                     ((m_off & 0x4000) >> 11) | (m_len - 2));
  1819.                 goto m3_m4_offset;
  1820.             }
  1821. #elif defined(LZO1Y)
  1822.                 goto m4_match;
  1823. #endif
  1824.         }
  1825.         else
  1826.         {
  1827.             {
  1828.                 const lzo_byte *end = in_end;
  1829.                 const lzo_byte *m = m_pos + M2_MAX_LEN + 1;
  1830.                 while (ip < end && *m == *ip)
  1831.                     m++, ip++;
  1832.                 m_len = (ip - ii);
  1833.             }
  1834.             assert(m_len > M2_MAX_LEN);
  1835.  
  1836.             if (m_off <= M3_MAX_OFFSET)
  1837.             {
  1838.                 m_off -= 1;
  1839.                 if (m_len <= 33)
  1840.                     *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
  1841.                 else
  1842.                 {
  1843.                     m_len -= 33;
  1844.                     *op++ = M3_MARKER | 0;
  1845.                     goto m3_m4_len;
  1846.                 }
  1847.             }
  1848.             else
  1849.             {
  1850. #if defined(LZO1Y)
  1851. m4_match:
  1852. #endif
  1853.                 m_off -= 0x4000;
  1854.                 assert(m_off > 0); assert(m_off <= 0x7fff);
  1855.                 if (m_len <= M4_MAX_LEN)
  1856.                     *op++ = LZO_BYTE(M4_MARKER |
  1857.                     ((m_off & 0x4000) >> 11) | (m_len - 2));
  1858.                 else
  1859.                 {
  1860.                     m_len -= M4_MAX_LEN;
  1861.                     *op++ = LZO_BYTE(M4_MARKER | ((m_off & 0x4000) >> 11));
  1862. m3_m4_len:
  1863.                     while (m_len > 255)
  1864.                     {
  1865.                         m_len -= 255;
  1866.                         *op++ = 0;
  1867.                     }
  1868.                     assert(m_len > 0);
  1869.                     *op++ = LZO_BYTE(m_len);
  1870.                 }
  1871.             }
  1872.  
  1873. m3_m4_offset:
  1874.             *op++ = LZO_BYTE((m_off & 63) << 2);
  1875.             *op++ = LZO_BYTE(m_off >> 6);
  1876.         }
  1877.  
  1878. #if 0
  1879. match_done:
  1880. #endif
  1881.         ii = ip;
  1882.         if (ip >= ip_end)
  1883.             break;
  1884.     }
  1885.  
  1886.     *out_len = op - out;
  1887.     return pd(in_end,ii);
  1888. }
  1889.  
  1890. LZO_PUBLIC(int)
  1891. DO_COMPRESS      ( const lzo_byte *in , lzo_uint  in_len,
  1892.                   lzo_byte *out, lzo_uintp out_len,
  1893.                   lzo_voidp wrkmem )
  1894. {
  1895.     lzo_byte *op = out;
  1896.     lzo_uint t;
  1897.  
  1898. #if defined(__LZO_QUERY_COMPRESS)
  1899.     if (__LZO_IS_COMPRESS_QUERY(in,in_len,out,out_len,wrkmem))
  1900.         return __LZO_QUERY_COMPRESS(in,in_len,out,out_len,wrkmem,D_SIZE,lzo_sizeof(lzo_dict_t));
  1901. #endif
  1902.  
  1903.     if (in_len <= M2_MAX_LEN + 5)
  1904.         t = in_len;
  1905.     else
  1906.     {
  1907.         t = do_compress(in,in_len,op,out_len,wrkmem);
  1908.         op += *out_len;
  1909.     }
  1910.  
  1911.     if (t > 0)
  1912.     {
  1913.         const lzo_byte *ii = in + in_len - t;
  1914.  
  1915.         if (op == out && t <= 238)
  1916.             *op++ = LZO_BYTE(17 + t);
  1917.         else if (t <= 3)
  1918.             op[-2] |= LZO_BYTE(t);
  1919.         else if (t <= 18)
  1920.             *op++ = LZO_BYTE(t - 3);
  1921.         else
  1922.         {
  1923.             lzo_uint tt = t - 18;
  1924.  
  1925.             *op++ = 0;
  1926.             while (tt > 255)
  1927.             {
  1928.                 tt -= 255;
  1929.                 *op++ = 0;
  1930.             }
  1931.             assert(tt > 0);
  1932.             *op++ = LZO_BYTE(tt);
  1933.         }
  1934.         do *op++ = *ii++; while (--t > 0);
  1935.     }
  1936.  
  1937.     *op++ = M4_MARKER | 1;
  1938.     *op++ = 0;
  1939.     *op++ = 0;
  1940.  
  1941.     *out_len = op - out;
  1942.     return LZO_E_OK;
  1943. }
  1944.  
  1945. #undef do_compress
  1946. #undef DO_COMPRESS
  1947. #undef LZO_HASH
  1948.  
  1949. #undef LZO_TEST_DECOMPRESS_OVERRUN
  1950. #undef LZO_TEST_DECOMPRESS_OVERRUN_INPUT
  1951. #undef LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT
  1952. #undef LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND
  1953. #undef DO_DECOMPRESS
  1954. #define DO_DECOMPRESS       lzo1x_decompress
  1955.  
  1956. #if defined(LZO_TEST_DECOMPRESS_OVERRUN)
  1957. #  if !defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
  1958. #    define LZO_TEST_DECOMPRESS_OVERRUN_INPUT       2
  1959. #  endif
  1960. #  if !defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
  1961. #    define LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT      2
  1962. #  endif
  1963. #  if !defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
  1964. #    define LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND
  1965. #  endif
  1966. #endif
  1967.  
  1968. #undef TEST_IP
  1969. #undef TEST_OP
  1970. #undef TEST_LOOKBEHIND
  1971. #undef NEED_IP
  1972. #undef NEED_OP
  1973. #undef HAVE_TEST_IP
  1974. #undef HAVE_TEST_OP
  1975. #undef HAVE_NEED_IP
  1976. #undef HAVE_NEED_OP
  1977. #undef HAVE_ANY_IP
  1978. #undef HAVE_ANY_OP
  1979.  
  1980. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
  1981. #  if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 1)
  1982. #    define TEST_IP             (ip < ip_end)
  1983. #  endif
  1984. #  if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 2)
  1985. #    define NEED_IP(x) \
  1986.     if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x))  goto input_overrun
  1987. #  endif
  1988. #endif
  1989.  
  1990. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
  1991. #  if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 1)
  1992. #    define TEST_OP             (op <= op_end)
  1993. #  endif
  1994. #  if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 2)
  1995. #    undef TEST_OP
  1996. #    define NEED_OP(x) \
  1997.     if ((lzo_uint)(op_end - op) < (lzo_uint)(x))  goto output_overrun
  1998. #  endif
  1999. #endif
  2000.  
  2001. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
  2002. #  define TEST_LOOKBEHIND(m_pos,out)    if (m_pos < out) goto lookbehind_overrun
  2003. #else
  2004. #  define TEST_LOOKBEHIND(m_pos,op)     ((void) 0)
  2005. #endif
  2006.  
  2007. #if !defined(LZO_EOF_CODE) && !defined(TEST_IP)
  2008. #  define TEST_IP               (ip < ip_end)
  2009. #endif
  2010.  
  2011. #if defined(TEST_IP)
  2012. #  define HAVE_TEST_IP
  2013. #else
  2014. #  define TEST_IP               1
  2015. #endif
  2016. #if defined(TEST_OP)
  2017. #  define HAVE_TEST_OP
  2018. #else
  2019. #  define TEST_OP               1
  2020. #endif
  2021.  
  2022. #if defined(NEED_IP)
  2023. #  define HAVE_NEED_IP
  2024. #else
  2025. #  define NEED_IP(x)            ((void) 0)
  2026. #endif
  2027. #if defined(NEED_OP)
  2028. #  define HAVE_NEED_OP
  2029. #else
  2030. #  define NEED_OP(x)            ((void) 0)
  2031. #endif
  2032.  
  2033. #if defined(HAVE_TEST_IP) || defined(HAVE_NEED_IP)
  2034. #  define HAVE_ANY_IP
  2035. #endif
  2036. #if defined(HAVE_TEST_OP) || defined(HAVE_NEED_OP)
  2037. #  define HAVE_ANY_OP
  2038. #endif
  2039.  
  2040. #undef __COPY4
  2041. #define __COPY4(dst,src)    * (lzo_uint32p)(dst) = * (const lzo_uint32p)(src)
  2042.  
  2043. #undef COPY4
  2044. #if defined(LZO_UNALIGNED_OK_4)
  2045. #  define COPY4(dst,src)    __COPY4(dst,src)
  2046. #elif defined(LZO_ALIGNED_OK_4)
  2047. #  define COPY4(dst,src)    __COPY4((lzo_ptr_t)(dst),(lzo_ptr_t)(src))
  2048. #endif
  2049.  
  2050. #if defined(DO_DECOMPRESS)
  2051. LZO_PUBLIC(int)
  2052. DO_DECOMPRESS  ( const lzo_byte *in , lzo_uint  in_len,
  2053.                 lzo_byte *out, lzo_uintp out_len,
  2054.                 lzo_voidp wrkmem )
  2055. #endif
  2056. {
  2057.     register lzo_byte *op;
  2058.     register const lzo_byte *ip;
  2059.     register lzo_uint t;
  2060. #if defined(COPY_DICT)
  2061.     lzo_uint m_off;
  2062.     const lzo_byte *dict_end;
  2063. #else
  2064.     register const lzo_byte *m_pos;
  2065. #endif
  2066.  
  2067.     const lzo_byte * const ip_end = in + in_len;
  2068. #if defined(HAVE_ANY_OP)
  2069.     lzo_byte * const op_end = out + *out_len;
  2070. #endif
  2071. #if defined(LZO1Z)
  2072.     lzo_uint last_m_off = 0;
  2073. #endif
  2074.  
  2075.     LZO_UNUSED(wrkmem);
  2076.  
  2077. #if defined(__LZO_QUERY_DECOMPRESS)
  2078.     if (__LZO_IS_DECOMPRESS_QUERY(in,in_len,out,out_len,wrkmem))
  2079.         return __LZO_QUERY_DECOMPRESS(in,in_len,out,out_len,wrkmem,0,0);
  2080. #endif
  2081.  
  2082. #if defined(COPY_DICT)
  2083.     if (dict)
  2084.     {
  2085.         if (dict_len > M4_MAX_OFFSET)
  2086.         {
  2087.             dict += dict_len - M4_MAX_OFFSET;
  2088.             dict_len = M4_MAX_OFFSET;
  2089.         }
  2090.         dict_end = dict + dict_len;
  2091.     }
  2092.     else
  2093.     {
  2094.         dict_len = 0;
  2095.         dict_end = NULL;
  2096.     }
  2097. #endif
  2098.  
  2099.     *out_len = 0;
  2100.  
  2101.     op = out;
  2102.     ip = in;
  2103.  
  2104.     if (*ip > 17)
  2105.     {
  2106.         t = *ip++ - 17;
  2107.         if (t < 4)
  2108.             goto match_next;
  2109.         assert(t > 0); NEED_OP(t); NEED_IP(t+1);
  2110.         do *op++ = *ip++; while (--t > 0);
  2111.         goto first_literal_run;
  2112.     }
  2113.  
  2114.     while (TEST_IP && TEST_OP)
  2115.     {
  2116.         t = *ip++;
  2117.         if (t >= 16)
  2118.             goto match;
  2119.         if (t == 0)
  2120.         {
  2121.             NEED_IP(1);
  2122.             while (*ip == 0)
  2123.             {
  2124.                 t += 255;
  2125.                 ip++;
  2126.                 NEED_IP(1);
  2127.             }
  2128.             t += 15 + *ip++;
  2129.         }
  2130.         assert(t > 0); NEED_OP(t+3); NEED_IP(t+4);
  2131. #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
  2132. #if !defined(LZO_UNALIGNED_OK_4)
  2133.         if (PTR_ALIGNED2_4(op,ip))
  2134.         {
  2135. #endif
  2136.             COPY4(op,ip);
  2137.             op += 4; ip += 4;
  2138.             if (--t > 0)
  2139.             {
  2140.                 if (t >= 4)
  2141.                 {
  2142.                     do {
  2143.                         COPY4(op,ip);
  2144.                         op += 4; ip += 4; t -= 4;
  2145.                     } while (t >= 4);
  2146.                     if (t > 0) do *op++ = *ip++; while (--t > 0);
  2147.                 }
  2148.                 else
  2149.                     do *op++ = *ip++; while (--t > 0);
  2150.             }
  2151. #if !defined(LZO_UNALIGNED_OK_4)
  2152.         }
  2153.         else
  2154. #endif
  2155. #endif
  2156. #if !defined(LZO_UNALIGNED_OK_4)
  2157.         {
  2158.             *op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
  2159.             do *op++ = *ip++; while (--t > 0);
  2160.         }
  2161. #endif
  2162.  
  2163. first_literal_run:
  2164.  
  2165.         t = *ip++;
  2166.         if (t >= 16)
  2167.             goto match;
  2168. #if defined(COPY_DICT)
  2169. #if defined(LZO1Z)
  2170.         m_off = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
  2171.         last_m_off = m_off;
  2172. #else
  2173.         m_off = (1 + M2_MAX_OFFSET) + (t >> 2) + (*ip++ << 2);
  2174. #endif
  2175.         NEED_OP(3);
  2176.         t = 3; COPY_DICT(t,m_off)
  2177. #else
  2178. #if defined(LZO1Z)
  2179.         t = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
  2180.         m_pos = op - t;
  2181.         last_m_off = t;
  2182. #else
  2183.         m_pos = op - (1 + M2_MAX_OFFSET);
  2184.         m_pos -= t >> 2;
  2185.         m_pos -= *ip++ << 2;
  2186. #endif
  2187.         TEST_LOOKBEHIND(m_pos,out); NEED_OP(3);
  2188.         *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos;
  2189. #endif
  2190.         goto match_done;
  2191.  
  2192.         while (TEST_IP && TEST_OP)
  2193.         {
  2194. match:
  2195.             if (t >= 64)
  2196.             {
  2197. #if defined(COPY_DICT)
  2198. #if defined(LZO1X)
  2199.                 m_off = 1 + ((t >> 2) & 7) + (*ip++ << 3);
  2200.                 t = (t >> 5) - 1;
  2201. #elif defined(LZO1Y)
  2202.                 m_off = 1 + ((t >> 2) & 3) + (*ip++ << 2);
  2203.                 t = (t >> 4) - 3;
  2204. #elif defined(LZO1Z)
  2205.                 m_off = t & 0x1f;
  2206.                 if (m_off >= 0x1c)
  2207.                     m_off = last_m_off;
  2208.                 else
  2209.                 {
  2210.                     m_off = 1 + (m_off << 6) + (*ip++ >> 2);
  2211.                     last_m_off = m_off;
  2212.                 }
  2213.                 t = (t >> 5) - 1;
  2214. #endif
  2215. #else
  2216. #if defined(LZO1X)
  2217.                 m_pos = op - 1;
  2218.                 m_pos -= (t >> 2) & 7;
  2219.                 m_pos -= *ip++ << 3;
  2220.                 t = (t >> 5) - 1;
  2221. #elif defined(LZO1Y)
  2222.                 m_pos = op - 1;
  2223.                 m_pos -= (t >> 2) & 3;
  2224.                 m_pos -= *ip++ << 2;
  2225.                 t = (t >> 4) - 3;
  2226. #elif defined(LZO1Z)
  2227.                 {
  2228.                     lzo_uint off = t & 0x1f;
  2229.                     m_pos = op;
  2230.                     if (off >= 0x1c)
  2231.                     {
  2232.                         assert(last_m_off > 0);
  2233.                         m_pos -= last_m_off;
  2234.                     }
  2235.                     else
  2236.                     {
  2237.                         off = 1 + (off << 6) + (*ip++ >> 2);
  2238.                         m_pos -= off;
  2239.                         last_m_off = off;
  2240.                     }
  2241.                 }
  2242.                 t = (t >> 5) - 1;
  2243. #endif
  2244.                 TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t+3-1);
  2245.                 goto copy_match;
  2246. #endif
  2247.             }
  2248.             else if (t >= 32)
  2249.             {
  2250.                 t &= 31;
  2251.                 if (t == 0)
  2252.                 {
  2253.                     NEED_IP(1);
  2254.                     while (*ip == 0)
  2255.                     {
  2256.                         t += 255;
  2257.                         ip++;
  2258.                         NEED_IP(1);
  2259.                     }
  2260.                     t += 31 + *ip++;
  2261.                 }
  2262. #if defined(COPY_DICT)
  2263. #if defined(LZO1Z)
  2264.                 m_off = 1 + (ip[0] << 6) + (ip[1] >> 2);
  2265.                 last_m_off = m_off;
  2266. #else
  2267.                 m_off = 1 + (ip[0] >> 2) + (ip[1] << 6);
  2268. #endif
  2269. #else
  2270. #if defined(LZO1Z)
  2271.                 {
  2272.                     lzo_uint off = 1 + (ip[0] << 6) + (ip[1] >> 2);
  2273.                     m_pos = op - off;
  2274.                     last_m_off = off;
  2275.                 }
  2276. #elif defined(LZO_UNALIGNED_OK_2) && (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
  2277.                 m_pos = op - 1;
  2278.                 m_pos -= (* (const lzo_ushortp) ip) >> 2;
  2279. #else
  2280.                 m_pos = op - 1;
  2281.                 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
  2282. #endif
  2283. #endif
  2284.                 ip += 2;
  2285.             }
  2286.             else if (t >= 16)
  2287.             {
  2288. #if defined(COPY_DICT)
  2289.                 m_off = (t & 8) << 11;
  2290. #else
  2291.                 m_pos = op;
  2292.                 m_pos -= (t & 8) << 11;
  2293. #endif
  2294.                 t &= 7;
  2295.                 if (t == 0)
  2296.                 {
  2297.                     NEED_IP(1);
  2298.                     while (*ip == 0)
  2299.                     {
  2300.                         t += 255;
  2301.                         ip++;
  2302.                         NEED_IP(1);
  2303.                     }
  2304.                     t += 7 + *ip++;
  2305.                 }
  2306. #if defined(COPY_DICT)
  2307. #if defined(LZO1Z)
  2308.                 m_off += (ip[0] << 6) + (ip[1] >> 2);
  2309. #else
  2310.                 m_off += (ip[0] >> 2) + (ip[1] << 6);
  2311. #endif
  2312.                 ip += 2;
  2313.                 if (m_off == 0)
  2314.                     goto eof_found;
  2315.                 m_off += 0x4000;
  2316. #if defined(LZO1Z)
  2317.                 last_m_off = m_off;
  2318. #endif
  2319. #else
  2320. #if defined(LZO1Z)
  2321.                 m_pos -= (ip[0] << 6) + (ip[1] >> 2);
  2322. #elif defined(LZO_UNALIGNED_OK_2) && (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
  2323.                 m_pos -= (* (const lzo_ushortp) ip) >> 2;
  2324. #else
  2325.                 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
  2326. #endif
  2327.                 ip += 2;
  2328.                 if (m_pos == op)
  2329.                     goto eof_found;
  2330.                 m_pos -= 0x4000;
  2331. #if defined(LZO1Z)
  2332.                 last_m_off = op - m_pos;
  2333. #endif
  2334. #endif
  2335.             }
  2336.             else
  2337.             {
  2338. #if defined(COPY_DICT)
  2339. #if defined(LZO1Z)
  2340.                 m_off = 1 + (t << 6) + (*ip++ >> 2);
  2341.                 last_m_off = m_off;
  2342. #else
  2343.                 m_off = 1 + (t >> 2) + (*ip++ << 2);
  2344. #endif
  2345.                 NEED_OP(2);
  2346.                 t = 2; COPY_DICT(t,m_off)
  2347. #else
  2348. #if defined(LZO1Z)
  2349.                 t = 1 + (t << 6) + (*ip++ >> 2);
  2350.                 m_pos = op - t;
  2351.                 last_m_off = t;
  2352. #else
  2353.                 m_pos = op - 1;
  2354.                 m_pos -= t >> 2;
  2355.                 m_pos -= *ip++ << 2;
  2356. #endif
  2357.                 TEST_LOOKBEHIND(m_pos,out); NEED_OP(2);
  2358.                 *op++ = *m_pos++; *op++ = *m_pos;
  2359. #endif
  2360.                 goto match_done;
  2361.             }
  2362.  
  2363. #if defined(COPY_DICT)
  2364.  
  2365.             NEED_OP(t+3-1);
  2366.             t += 3-1; COPY_DICT(t,m_off)
  2367.  
  2368. #else
  2369.  
  2370.             TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t+3-1);
  2371. #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
  2372. #if !defined(LZO_UNALIGNED_OK_4)
  2373.             if (t >= 2 * 4 - (3 - 1) && PTR_ALIGNED2_4(op,m_pos))
  2374.             {
  2375.                 assert((op - m_pos) >= 4);
  2376. #else
  2377.             if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
  2378.             {
  2379. #endif
  2380.                 COPY4(op,m_pos);
  2381.                 op += 4; m_pos += 4; t -= 4 - (3 - 1);
  2382.                 do {
  2383.                     COPY4(op,m_pos);
  2384.                     op += 4; m_pos += 4; t -= 4;
  2385.                 } while (t >= 4);
  2386.                 if (t > 0) do *op++ = *m_pos++; while (--t > 0);
  2387.             }
  2388.             else
  2389. #endif
  2390.             {
  2391. copy_match:
  2392.                 *op++ = *m_pos++; *op++ = *m_pos++;
  2393.                 do *op++ = *m_pos++; while (--t > 0);
  2394.             }
  2395.  
  2396. #endif
  2397.  
  2398. match_done:
  2399. #if defined(LZO1Z)
  2400.             t = ip[-1] & 3;
  2401. #else
  2402.             t = ip[-2] & 3;
  2403. #endif
  2404.             if (t == 0)
  2405.                 break;
  2406.  
  2407. match_next:
  2408.             assert(t > 0); NEED_OP(t); NEED_IP(t+1);
  2409.             do *op++ = *ip++; while (--t > 0);
  2410.             t = *ip++;
  2411.         }
  2412.     }
  2413.  
  2414. #if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
  2415.     *out_len = op - out;
  2416.     return LZO_E_EOF_NOT_FOUND;
  2417. #endif
  2418.  
  2419. eof_found:
  2420.     assert(t == 1);
  2421.     *out_len = op - out;
  2422.     return (ip == ip_end ? LZO_E_OK :
  2423.         (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
  2424.  
  2425. #if defined(HAVE_NEED_IP)
  2426. input_overrun:
  2427.     *out_len = op - out;
  2428.     return LZO_E_INPUT_OVERRUN;
  2429. #endif
  2430.  
  2431. #if defined(HAVE_NEED_OP)
  2432. output_overrun:
  2433.     *out_len = op - out;
  2434.     return LZO_E_OUTPUT_OVERRUN;
  2435. #endif
  2436.  
  2437. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
  2438. lookbehind_overrun:
  2439.     *out_len = op - out;
  2440.     return LZO_E_LOOKBEHIND_OVERRUN;
  2441. #endif
  2442. }
  2443.  
  2444. #define LZO_TEST_DECOMPRESS_OVERRUN
  2445. #undef DO_DECOMPRESS
  2446. #define DO_DECOMPRESS       lzo1x_decompress_safe
  2447.  
  2448. #if defined(LZO_TEST_DECOMPRESS_OVERRUN)
  2449. #  if !defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
  2450. #    define LZO_TEST_DECOMPRESS_OVERRUN_INPUT       2
  2451. #  endif
  2452. #  if !defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
  2453. #    define LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT      2
  2454. #  endif
  2455. #  if !defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
  2456. #    define LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND
  2457. #  endif
  2458. #endif
  2459.  
  2460. #undef TEST_IP
  2461. #undef TEST_OP
  2462. #undef TEST_LOOKBEHIND
  2463. #undef NEED_IP
  2464. #undef NEED_OP
  2465. #undef HAVE_TEST_IP
  2466. #undef HAVE_TEST_OP
  2467. #undef HAVE_NEED_IP
  2468. #undef HAVE_NEED_OP
  2469. #undef HAVE_ANY_IP
  2470. #undef HAVE_ANY_OP
  2471.  
  2472. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
  2473. #  if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 1)
  2474. #    define TEST_IP             (ip < ip_end)
  2475. #  endif
  2476. #  if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 2)
  2477. #    define NEED_IP(x) \
  2478.     if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x))  goto input_overrun
  2479. #  endif
  2480. #endif
  2481.  
  2482. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
  2483. #  if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 1)
  2484. #    define TEST_OP             (op <= op_end)
  2485. #  endif
  2486. #  if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 2)
  2487. #    undef TEST_OP
  2488. #    define NEED_OP(x) \
  2489.     if ((lzo_uint)(op_end - op) < (lzo_uint)(x))  goto output_overrun
  2490. #  endif
  2491. #endif
  2492.  
  2493. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
  2494. #  define TEST_LOOKBEHIND(m_pos,out)    if (m_pos < out) goto lookbehind_overrun
  2495. #else
  2496. #  define TEST_LOOKBEHIND(m_pos,op)     ((void) 0)
  2497. #endif
  2498.  
  2499. #if !defined(LZO_EOF_CODE) && !defined(TEST_IP)
  2500. #  define TEST_IP               (ip < ip_end)
  2501. #endif
  2502.  
  2503. #if defined(TEST_IP)
  2504. #  define HAVE_TEST_IP
  2505. #else
  2506. #  define TEST_IP               1
  2507. #endif
  2508. #if defined(TEST_OP)
  2509. #  define HAVE_TEST_OP
  2510. #else
  2511. #  define TEST_OP               1
  2512. #endif
  2513.  
  2514. #if defined(NEED_IP)
  2515. #  define HAVE_NEED_IP
  2516. #else
  2517. #  define NEED_IP(x)            ((void) 0)
  2518. #endif
  2519. #if defined(NEED_OP)
  2520. #  define HAVE_NEED_OP
  2521. #else
  2522. #  define NEED_OP(x)            ((void) 0)
  2523. #endif
  2524.  
  2525. #if defined(HAVE_TEST_IP) || defined(HAVE_NEED_IP)
  2526. #  define HAVE_ANY_IP
  2527. #endif
  2528. #if defined(HAVE_TEST_OP) || defined(HAVE_NEED_OP)
  2529. #  define HAVE_ANY_OP
  2530. #endif
  2531.  
  2532. #undef __COPY4
  2533. #define __COPY4(dst,src)    * (lzo_uint32p)(dst) = * (const lzo_uint32p)(src)
  2534.  
  2535. #undef COPY4
  2536. #if defined(LZO_UNALIGNED_OK_4)
  2537. #  define COPY4(dst,src)    __COPY4(dst,src)
  2538. #elif defined(LZO_ALIGNED_OK_4)
  2539. #  define COPY4(dst,src)    __COPY4((lzo_ptr_t)(dst),(lzo_ptr_t)(src))
  2540. #endif
  2541.  
  2542. #if defined(DO_DECOMPRESS)
  2543. LZO_PUBLIC(int)
  2544. DO_DECOMPRESS  ( const lzo_byte *in , lzo_uint  in_len,
  2545.                 lzo_byte *out, lzo_uintp out_len,
  2546.                 lzo_voidp wrkmem )
  2547. #endif
  2548. {
  2549.     register lzo_byte *op;
  2550.     register const lzo_byte *ip;
  2551.     register lzo_uint t;
  2552. #if defined(COPY_DICT)
  2553.     lzo_uint m_off;
  2554.     const lzo_byte *dict_end;
  2555. #else
  2556.     register const lzo_byte *m_pos;
  2557. #endif
  2558.  
  2559.     const lzo_byte * const ip_end = in + in_len;
  2560. #if defined(HAVE_ANY_OP)
  2561.     lzo_byte * const op_end = out + *out_len;
  2562. #endif
  2563. #if defined(LZO1Z)
  2564.     lzo_uint last_m_off = 0;
  2565. #endif
  2566.  
  2567.     LZO_UNUSED(wrkmem);
  2568.  
  2569. #if defined(__LZO_QUERY_DECOMPRESS)
  2570.     if (__LZO_IS_DECOMPRESS_QUERY(in,in_len,out,out_len,wrkmem))
  2571.         return __LZO_QUERY_DECOMPRESS(in,in_len,out,out_len,wrkmem,0,0);
  2572. #endif
  2573.  
  2574. #if defined(COPY_DICT)
  2575.     if (dict)
  2576.     {
  2577.         if (dict_len > M4_MAX_OFFSET)
  2578.         {
  2579.             dict += dict_len - M4_MAX_OFFSET;
  2580.             dict_len = M4_MAX_OFFSET;
  2581.         }
  2582.         dict_end = dict + dict_len;
  2583.     }
  2584.     else
  2585.     {
  2586.         dict_len = 0;
  2587.         dict_end = NULL;
  2588.     }
  2589. #endif
  2590.  
  2591.     *out_len = 0;
  2592.  
  2593.     op = out;
  2594.     ip = in;
  2595.  
  2596.     if (*ip > 17)
  2597.     {
  2598.         t = *ip++ - 17;
  2599.         if (t < 4)
  2600.             goto match_next;
  2601.         assert(t > 0); NEED_OP(t); NEED_IP(t+1);
  2602.         do *op++ = *ip++; while (--t > 0);
  2603.         goto first_literal_run;
  2604.     }
  2605.  
  2606.     while (TEST_IP && TEST_OP)
  2607.     {
  2608.         t = *ip++;
  2609.         if (t >= 16)
  2610.             goto match;
  2611.         if (t == 0)
  2612.         {
  2613.             NEED_IP(1);
  2614.             while (*ip == 0)
  2615.             {
  2616.                 t += 255;
  2617.                 ip++;
  2618.                 NEED_IP(1);
  2619.             }
  2620.             t += 15 + *ip++;
  2621.         }
  2622.         assert(t > 0); NEED_OP(t+3); NEED_IP(t+4);
  2623. #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
  2624. #if !defined(LZO_UNALIGNED_OK_4)
  2625.         if (PTR_ALIGNED2_4(op,ip))
  2626.         {
  2627. #endif
  2628.             COPY4(op,ip);
  2629.             op += 4; ip += 4;
  2630.             if (--t > 0)
  2631.             {
  2632.                 if (t >= 4)
  2633.                 {
  2634.                     do {
  2635.                         COPY4(op,ip);
  2636.                         op += 4; ip += 4; t -= 4;
  2637.                     } while (t >= 4);
  2638.                     if (t > 0) do *op++ = *ip++; while (--t > 0);
  2639.                 }
  2640.                 else
  2641.                     do *op++ = *ip++; while (--t > 0);
  2642.             }
  2643. #if !defined(LZO_UNALIGNED_OK_4)
  2644.         }
  2645.         else
  2646. #endif
  2647. #endif
  2648. #if !defined(LZO_UNALIGNED_OK_4)
  2649.         {
  2650.             *op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
  2651.             do *op++ = *ip++; while (--t > 0);
  2652.         }
  2653. #endif
  2654.  
  2655. first_literal_run:
  2656.  
  2657.         t = *ip++;
  2658.         if (t >= 16)
  2659.             goto match;
  2660. #if defined(COPY_DICT)
  2661. #if defined(LZO1Z)
  2662.         m_off = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
  2663.         last_m_off = m_off;
  2664. #else
  2665.         m_off = (1 + M2_MAX_OFFSET) + (t >> 2) + (*ip++ << 2);
  2666. #endif
  2667.         NEED_OP(3);
  2668.         t = 3; COPY_DICT(t,m_off)
  2669. #else
  2670. #if defined(LZO1Z)
  2671.         t = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
  2672.         m_pos = op - t;
  2673.         last_m_off = t;
  2674. #else
  2675.         m_pos = op - (1 + M2_MAX_OFFSET);
  2676.         m_pos -= t >> 2;
  2677.         m_pos -= *ip++ << 2;
  2678. #endif
  2679.         TEST_LOOKBEHIND(m_pos,out); NEED_OP(3);
  2680.         *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos;
  2681. #endif
  2682.         goto match_done;
  2683.  
  2684.         while (TEST_IP && TEST_OP)
  2685.         {
  2686. match:
  2687.             if (t >= 64)
  2688.             {
  2689. #if defined(COPY_DICT)
  2690. #if defined(LZO1X)
  2691.                 m_off = 1 + ((t >> 2) & 7) + (*ip++ << 3);
  2692.                 t = (t >> 5) - 1;
  2693. #elif defined(LZO1Y)
  2694.                 m_off = 1 + ((t >> 2) & 3) + (*ip++ << 2);
  2695.                 t = (t >> 4) - 3;
  2696. #elif defined(LZO1Z)
  2697.                 m_off = t & 0x1f;
  2698.                 if (m_off >= 0x1c)
  2699.                     m_off = last_m_off;
  2700.                 else
  2701.                 {
  2702.                     m_off = 1 + (m_off << 6) + (*ip++ >> 2);
  2703.                     last_m_off = m_off;
  2704.                 }
  2705.                 t = (t >> 5) - 1;
  2706. #endif
  2707. #else
  2708. #if defined(LZO1X)
  2709.                 m_pos = op - 1;
  2710.                 m_pos -= (t >> 2) & 7;
  2711.                 m_pos -= *ip++ << 3;
  2712.                 t = (t >> 5) - 1;
  2713. #elif defined(LZO1Y)
  2714.                 m_pos = op - 1;
  2715.                 m_pos -= (t >> 2) & 3;
  2716.                 m_pos -= *ip++ << 2;
  2717.                 t = (t >> 4) - 3;
  2718. #elif defined(LZO1Z)
  2719.                 {
  2720.                     lzo_uint off = t & 0x1f;
  2721.                     m_pos = op;
  2722.                     if (off >= 0x1c)
  2723.                     {
  2724.                         assert(last_m_off > 0);
  2725.                         m_pos -= last_m_off;
  2726.                     }
  2727.                     else
  2728.                     {
  2729.                         off = 1 + (off << 6) + (*ip++ >> 2);
  2730.                         m_pos -= off;
  2731.                         last_m_off = off;
  2732.                     }
  2733.                 }
  2734.                 t = (t >> 5) - 1;
  2735. #endif
  2736.                 TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t+3-1);
  2737.                 goto copy_match;
  2738. #endif
  2739.             }
  2740.             else if (t >= 32)
  2741.             {
  2742.                 t &= 31;
  2743.                 if (t == 0)
  2744.                 {
  2745.                     NEED_IP(1);
  2746.                     while (*ip == 0)
  2747.                     {
  2748.                         t += 255;
  2749.                         ip++;
  2750.                         NEED_IP(1);
  2751.                     }
  2752.                     t += 31 + *ip++;
  2753.                 }
  2754. #if defined(COPY_DICT)
  2755. #if defined(LZO1Z)
  2756.                 m_off = 1 + (ip[0] << 6) + (ip[1] >> 2);
  2757.                 last_m_off = m_off;
  2758. #else
  2759.                 m_off = 1 + (ip[0] >> 2) + (ip[1] << 6);
  2760. #endif
  2761. #else
  2762. #if defined(LZO1Z)
  2763.                 {
  2764.                     lzo_uint off = 1 + (ip[0] << 6) + (ip[1] >> 2);
  2765.                     m_pos = op - off;
  2766.                     last_m_off = off;
  2767.                 }
  2768. #elif defined(LZO_UNALIGNED_OK_2) && (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
  2769.                 m_pos = op - 1;
  2770.                 m_pos -= (* (const lzo_ushortp) ip) >> 2;
  2771. #else
  2772.                 m_pos = op - 1;
  2773.                 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
  2774. #endif
  2775. #endif
  2776.                 ip += 2;
  2777.             }
  2778.             else if (t >= 16)
  2779.             {
  2780. #if defined(COPY_DICT)
  2781.                 m_off = (t & 8) << 11;
  2782. #else
  2783.                 m_pos = op;
  2784.                 m_pos -= (t & 8) << 11;
  2785. #endif
  2786.                 t &= 7;
  2787.                 if (t == 0)
  2788.                 {
  2789.                     NEED_IP(1);
  2790.                     while (*ip == 0)
  2791.                     {
  2792.                         t += 255;
  2793.                         ip++;
  2794.                         NEED_IP(1);
  2795.                     }
  2796.                     t += 7 + *ip++;
  2797.                 }
  2798. #if defined(COPY_DICT)
  2799. #if defined(LZO1Z)
  2800.                 m_off += (ip[0] << 6) + (ip[1] >> 2);
  2801. #else
  2802.                 m_off += (ip[0] >> 2) + (ip[1] << 6);
  2803. #endif
  2804.                 ip += 2;
  2805.                 if (m_off == 0)
  2806.                     goto eof_found;
  2807.                 m_off += 0x4000;
  2808. #if defined(LZO1Z)
  2809.                 last_m_off = m_off;
  2810. #endif
  2811. #else
  2812. #if defined(LZO1Z)
  2813.                 m_pos -= (ip[0] << 6) + (ip[1] >> 2);
  2814. #elif defined(LZO_UNALIGNED_OK_2) && (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
  2815.                 m_pos -= (* (const lzo_ushortp) ip) >> 2;
  2816. #else
  2817.                 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
  2818. #endif
  2819.                 ip += 2;
  2820.                 if (m_pos == op)
  2821.                     goto eof_found;
  2822.                 m_pos -= 0x4000;
  2823. #if defined(LZO1Z)
  2824.                 last_m_off = op - m_pos;
  2825. #endif
  2826. #endif
  2827.             }
  2828.             else
  2829.             {
  2830. #if defined(COPY_DICT)
  2831. #if defined(LZO1Z)
  2832.                 m_off = 1 + (t << 6) + (*ip++ >> 2);
  2833.                 last_m_off = m_off;
  2834. #else
  2835.                 m_off = 1 + (t >> 2) + (*ip++ << 2);
  2836. #endif
  2837.                 NEED_OP(2);
  2838.                 t = 2; COPY_DICT(t,m_off)
  2839. #else
  2840. #if defined(LZO1Z)
  2841.                 t = 1 + (t << 6) + (*ip++ >> 2);
  2842.                 m_pos = op - t;
  2843.                 last_m_off = t;
  2844. #else
  2845.                 m_pos = op - 1;
  2846.                 m_pos -= t >> 2;
  2847.                 m_pos -= *ip++ << 2;
  2848. #endif
  2849.                 TEST_LOOKBEHIND(m_pos,out); NEED_OP(2);
  2850.                 *op++ = *m_pos++; *op++ = *m_pos;
  2851. #endif
  2852.                 goto match_done;
  2853.             }
  2854.  
  2855. #if defined(COPY_DICT)
  2856.  
  2857.             NEED_OP(t+3-1);
  2858.             t += 3-1; COPY_DICT(t,m_off)
  2859.  
  2860. #else
  2861.  
  2862.             TEST_LOOKBEHIND(m_pos,out); assert(t > 0); NEED_OP(t+3-1);
  2863. #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
  2864. #if !defined(LZO_UNALIGNED_OK_4)
  2865.             if (t >= 2 * 4 - (3 - 1) && PTR_ALIGNED2_4(op,m_pos))
  2866.             {
  2867.                 assert((op - m_pos) >= 4);
  2868. #else
  2869.             if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
  2870.             {
  2871. #endif
  2872.                 COPY4(op,m_pos);
  2873.                 op += 4; m_pos += 4; t -= 4 - (3 - 1);
  2874.                 do {
  2875.                     COPY4(op,m_pos);
  2876.                     op += 4; m_pos += 4; t -= 4;
  2877.                 } while (t >= 4);
  2878.                 if (t > 0) do *op++ = *m_pos++; while (--t > 0);
  2879.             }
  2880.             else
  2881. #endif
  2882.             {
  2883. copy_match:
  2884.                 *op++ = *m_pos++; *op++ = *m_pos++;
  2885.                 do *op++ = *m_pos++; while (--t > 0);
  2886.             }
  2887.  
  2888. #endif
  2889.  
  2890. match_done:
  2891. #if defined(LZO1Z)
  2892.             t = ip[-1] & 3;
  2893. #else
  2894.             t = ip[-2] & 3;
  2895. #endif
  2896.             if (t == 0)
  2897.                 break;
  2898.  
  2899. match_next:
  2900.             assert(t > 0); NEED_OP(t); NEED_IP(t+1);
  2901.             do *op++ = *ip++; while (--t > 0);
  2902.             t = *ip++;
  2903.         }
  2904.     }
  2905.  
  2906. #if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
  2907.     *out_len = op - out;
  2908.     return LZO_E_EOF_NOT_FOUND;
  2909. #endif
  2910.  
  2911. eof_found:
  2912.     assert(t == 1);
  2913.     *out_len = op - out;
  2914.     return (ip == ip_end ? LZO_E_OK :
  2915.         (ip < ip_end  ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
  2916.  
  2917. #if defined(HAVE_NEED_IP)
  2918. input_overrun:
  2919.     *out_len = op - out;
  2920.     return LZO_E_INPUT_OVERRUN;
  2921. #endif
  2922.  
  2923. #if defined(HAVE_NEED_OP)
  2924. output_overrun:
  2925.     *out_len = op - out;
  2926.     return LZO_E_OUTPUT_OVERRUN;
  2927. #endif
  2928.  
  2929. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
  2930. lookbehind_overrun:
  2931.     *out_len = op - out;
  2932.     return LZO_E_LOOKBEHIND_OVERRUN;
  2933. #endif
  2934. }
  2935.  
  2936. /***** End of minilzo.c *****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement