Guest User

Untitled

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