/apps/desktop/libvncserver/minilzo.c

http://ftk.googlecode.com/ · C · 2797 lines · 2420 code · 337 blank · 40 comment · 536 complexity · fa4c2a1643439aa7dc35493bac1f82cc MD5 · raw file

Large files are truncated click here to view the full file

  1. /* minilzo.c -- mini subset of the LZO real-time data compression library
  2. This file is part of the LZO real-time data compression library.
  3. Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
  4. Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
  5. Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
  6. Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
  7. Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
  8. Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
  9. Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
  10. All Rights Reserved.
  11. The LZO library is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation; either version 2 of
  14. the License, or (at your option) any later version.
  15. The LZO library is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with the LZO library; see the file COPYING.
  21. If not, write to the Free Software Foundation, Inc.,
  22. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. Markus F.X.J. Oberhumer
  24. <markus@oberhumer.com>
  25. http://www.oberhumer.com/opensource/lzo/
  26. */
  27. /*
  28. * NOTE:
  29. * the full LZO package can be found at
  30. * http://www.oberhumer.com/opensource/lzo/
  31. */
  32. #define __LZO_IN_MINILZO
  33. #define LZO_BUILD
  34. #ifdef MINILZO_HAVE_CONFIG_H
  35. # include <config.h>
  36. #endif
  37. #undef LZO_HAVE_CONFIG_H
  38. #include "minilzo.h"
  39. #if !defined(MINILZO_VERSION) || (MINILZO_VERSION != 0x1080)
  40. # error "version mismatch in miniLZO source files"
  41. #endif
  42. #ifdef MINILZO_HAVE_CONFIG_H
  43. # define LZO_HAVE_CONFIG_H
  44. #endif
  45. #if !defined(LZO_NO_SYS_TYPES_H)
  46. # include <sys/types.h>
  47. #endif
  48. #include <stdio.h>
  49. #ifndef __LZO_CONF_H
  50. #define __LZO_CONF_H
  51. #if !defined(__LZO_IN_MINILZO)
  52. # ifndef __LZOCONF_H
  53. # include <lzoconf.h>
  54. # endif
  55. #endif
  56. #if defined(__BOUNDS_CHECKING_ON)
  57. # include <unchecked.h>
  58. #else
  59. # define BOUNDS_CHECKING_OFF_DURING(stmt) stmt
  60. # define BOUNDS_CHECKING_OFF_IN_EXPR(expr) (expr)
  61. #endif
  62. #if !defined(LZO_HAVE_CONFIG_H)
  63. # include <stddef.h>
  64. # include <string.h>
  65. # if !defined(NO_STDLIB_H)
  66. # include <stdlib.h>
  67. # endif
  68. # define HAVE_MEMCMP
  69. # define HAVE_MEMCPY
  70. # define HAVE_MEMMOVE
  71. # define HAVE_MEMSET
  72. #else
  73. # include <sys/types.h>
  74. # if defined(HAVE_STDDEF_H)
  75. # include <stddef.h>
  76. # endif
  77. # if defined(STDC_HEADERS)
  78. # include <string.h>
  79. # include <stdlib.h>
  80. # endif
  81. #endif
  82. #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
  83. # define HAVE_MALLOC_H
  84. # define HAVE_HALLOC
  85. #endif
  86. #undef NDEBUG
  87. #if !defined(LZO_DEBUG)
  88. # define NDEBUG
  89. #endif
  90. #if defined(LZO_DEBUG) || !defined(NDEBUG)
  91. # if !defined(NO_STDIO_H)
  92. # include <stdio.h>
  93. # endif
  94. #endif
  95. #include <assert.h>
  96. #if !defined(LZO_COMPILE_TIME_ASSERT)
  97. # define LZO_COMPILE_TIME_ASSERT(expr) \
  98. { typedef int __lzo_compile_time_assert_fail[1 - 2 * !(expr)]; }
  99. #endif
  100. #if !defined(LZO_UNUSED)
  101. # if 1
  102. # define LZO_UNUSED(var) ((void)&var)
  103. # elif 0
  104. # define LZO_UNUSED(var) { typedef int __lzo_unused[sizeof(var) ? 2 : 1]; }
  105. # else
  106. # define LZO_UNUSED(parm) (parm = parm)
  107. # endif
  108. #endif
  109. #if !defined(__inline__) && !defined(__GNUC__)
  110. # if defined(__cplusplus)
  111. # define __inline__ inline
  112. # else
  113. # define __inline__
  114. # endif
  115. #endif
  116. #if defined(NO_MEMCMP)
  117. # undef HAVE_MEMCMP
  118. #endif
  119. #if !defined(HAVE_MEMCMP)
  120. # undef memcmp
  121. # define memcmp lzo_memcmp
  122. #endif
  123. #if !defined(HAVE_MEMCPY)
  124. # undef memcpy
  125. # define memcpy lzo_memcpy
  126. #endif
  127. #if !defined(HAVE_MEMMOVE)
  128. # undef memmove
  129. # define memmove lzo_memmove
  130. #endif
  131. #if !defined(HAVE_MEMSET)
  132. # undef memset
  133. # define memset lzo_memset
  134. #endif
  135. #if 0
  136. # define LZO_BYTE(x) ((unsigned char) (x))
  137. #else
  138. # define LZO_BYTE(x) ((unsigned char) ((x) & 0xff))
  139. #endif
  140. #define LZO_MAX(a,b) ((a) >= (b) ? (a) : (b))
  141. #define LZO_MIN(a,b) ((a) <= (b) ? (a) : (b))
  142. #define LZO_MAX3(a,b,c) ((a) >= (b) ? LZO_MAX(a,c) : LZO_MAX(b,c))
  143. #define LZO_MIN3(a,b,c) ((a) <= (b) ? LZO_MIN(a,c) : LZO_MIN(b,c))
  144. #define lzo_sizeof(type) ((lzo_uint) (sizeof(type)))
  145. #define LZO_HIGH(array) ((lzo_uint) (sizeof(array)/sizeof(*(array))))
  146. #define LZO_SIZE(bits) (1u << (bits))
  147. #define LZO_MASK(bits) (LZO_SIZE(bits) - 1)
  148. #define LZO_LSIZE(bits) (1ul << (bits))
  149. #define LZO_LMASK(bits) (LZO_LSIZE(bits) - 1)
  150. #define LZO_USIZE(bits) ((lzo_uint) 1 << (bits))
  151. #define LZO_UMASK(bits) (LZO_USIZE(bits) - 1)
  152. #define LZO_STYPE_MAX(b) (((1l << (8*(b)-2)) - 1l) + (1l << (8*(b)-2)))
  153. #define LZO_UTYPE_MAX(b) (((1ul << (8*(b)-1)) - 1ul) + (1ul << (8*(b)-1)))
  154. #if !defined(SIZEOF_UNSIGNED)
  155. # if (UINT_MAX == 0xffff)
  156. # define SIZEOF_UNSIGNED 2
  157. # elif (UINT_MAX == LZO_0xffffffffL)
  158. # define SIZEOF_UNSIGNED 4
  159. # elif (UINT_MAX >= LZO_0xffffffffL)
  160. # define SIZEOF_UNSIGNED 8
  161. # else
  162. # error "SIZEOF_UNSIGNED"
  163. # endif
  164. #endif
  165. #if !defined(SIZEOF_UNSIGNED_LONG)
  166. # if (ULONG_MAX == LZO_0xffffffffL)
  167. # define SIZEOF_UNSIGNED_LONG 4
  168. # elif (ULONG_MAX >= LZO_0xffffffffL)
  169. # define SIZEOF_UNSIGNED_LONG 8
  170. # else
  171. # error "SIZEOF_UNSIGNED_LONG"
  172. # endif
  173. #endif
  174. #if !defined(SIZEOF_SIZE_T)
  175. # define SIZEOF_SIZE_T SIZEOF_UNSIGNED
  176. #endif
  177. #if !defined(SIZE_T_MAX)
  178. # define SIZE_T_MAX LZO_UTYPE_MAX(SIZEOF_SIZE_T)
  179. #endif
  180. #if 1 && defined(__LZO_i386) && (UINT_MAX == LZO_0xffffffffL)
  181. # if !defined(LZO_UNALIGNED_OK_2) && (USHRT_MAX == 0xffff)
  182. # define LZO_UNALIGNED_OK_2
  183. # endif
  184. # if !defined(LZO_UNALIGNED_OK_4) && (LZO_UINT32_MAX == LZO_0xffffffffL)
  185. # define LZO_UNALIGNED_OK_4
  186. # endif
  187. #endif
  188. #if defined(LZO_UNALIGNED_OK_2) || defined(LZO_UNALIGNED_OK_4)
  189. # if !defined(LZO_UNALIGNED_OK)
  190. # define LZO_UNALIGNED_OK
  191. # endif
  192. #endif
  193. #if defined(__LZO_NO_UNALIGNED)
  194. # undef LZO_UNALIGNED_OK
  195. # undef LZO_UNALIGNED_OK_2
  196. # undef LZO_UNALIGNED_OK_4
  197. #endif
  198. #if defined(LZO_UNALIGNED_OK_2) && (USHRT_MAX != 0xffff)
  199. # error "LZO_UNALIGNED_OK_2 must not be defined on this system"
  200. #endif
  201. #if defined(LZO_UNALIGNED_OK_4) && (LZO_UINT32_MAX != LZO_0xffffffffL)
  202. # error "LZO_UNALIGNED_OK_4 must not be defined on this system"
  203. #endif
  204. #if defined(__LZO_NO_ALIGNED)
  205. # undef LZO_ALIGNED_OK_4
  206. #endif
  207. #if defined(LZO_ALIGNED_OK_4) && (LZO_UINT32_MAX != LZO_0xffffffffL)
  208. # error "LZO_ALIGNED_OK_4 must not be defined on this system"
  209. #endif
  210. #define LZO_LITTLE_ENDIAN 1234
  211. #define LZO_BIG_ENDIAN 4321
  212. #define LZO_PDP_ENDIAN 3412
  213. #if !defined(LZO_BYTE_ORDER)
  214. # if defined(MFX_BYTE_ORDER)
  215. # define LZO_BYTE_ORDER MFX_BYTE_ORDER
  216. # elif defined(__LZO_i386)
  217. # define LZO_BYTE_ORDER LZO_LITTLE_ENDIAN
  218. # elif defined(BYTE_ORDER)
  219. # define LZO_BYTE_ORDER BYTE_ORDER
  220. # elif defined(__BYTE_ORDER)
  221. # define LZO_BYTE_ORDER __BYTE_ORDER
  222. # endif
  223. #endif
  224. #if defined(LZO_BYTE_ORDER)
  225. # if (LZO_BYTE_ORDER != LZO_LITTLE_ENDIAN) && \
  226. (LZO_BYTE_ORDER != LZO_BIG_ENDIAN)
  227. # error "invalid LZO_BYTE_ORDER"
  228. # endif
  229. #endif
  230. #if defined(LZO_UNALIGNED_OK) && !defined(LZO_BYTE_ORDER)
  231. # error "LZO_BYTE_ORDER is not defined"
  232. #endif
  233. #define LZO_OPTIMIZE_GNUC_i386_IS_BUGGY
  234. #if defined(NDEBUG) && !defined(LZO_DEBUG) && !defined(__LZO_CHECKER)
  235. # if defined(__GNUC__) && defined(__i386__)
  236. # if !defined(LZO_OPTIMIZE_GNUC_i386_IS_BUGGY)
  237. # define LZO_OPTIMIZE_GNUC_i386
  238. # endif
  239. # endif
  240. #endif
  241. __LZO_EXTERN_C int __lzo_init_done;
  242. __LZO_EXTERN_C const lzo_byte __lzo_copyright[];
  243. LZO_EXTERN(const lzo_byte *) lzo_copyright(void);
  244. __LZO_EXTERN_C const lzo_uint32 _lzo_crc32_table[256];
  245. #define _LZO_STRINGIZE(x) #x
  246. #define _LZO_MEXPAND(x) _LZO_STRINGIZE(x)
  247. #define _LZO_CONCAT2(a,b) a ## b
  248. #define _LZO_CONCAT3(a,b,c) a ## b ## c
  249. #define _LZO_CONCAT4(a,b,c,d) a ## b ## c ## d
  250. #define _LZO_CONCAT5(a,b,c,d,e) a ## b ## c ## d ## e
  251. #define _LZO_ECONCAT2(a,b) _LZO_CONCAT2(a,b)
  252. #define _LZO_ECONCAT3(a,b,c) _LZO_CONCAT3(a,b,c)
  253. #define _LZO_ECONCAT4(a,b,c,d) _LZO_CONCAT4(a,b,c,d)
  254. #define _LZO_ECONCAT5(a,b,c,d,e) _LZO_CONCAT5(a,b,c,d,e)
  255. #if 0
  256. #define __LZO_IS_COMPRESS_QUERY(i,il,o,ol,w) ((lzo_voidp)(o) == (w))
  257. #define __LZO_QUERY_COMPRESS(i,il,o,ol,w,n,s) \
  258. (*ol = (n)*(s), LZO_E_OK)
  259. #define __LZO_IS_DECOMPRESS_QUERY(i,il,o,ol,w) ((lzo_voidp)(o) == (w))
  260. #define __LZO_QUERY_DECOMPRESS(i,il,o,ol,w,n,s) \
  261. (*ol = (n)*(s), LZO_E_OK)
  262. #define __LZO_IS_OPTIMIZE_QUERY(i,il,o,ol,w) ((lzo_voidp)(o) == (w))
  263. #define __LZO_QUERY_OPTIMIZE(i,il,o,ol,w,n,s) \
  264. (*ol = (n)*(s), LZO_E_OK)
  265. #endif
  266. #ifndef __LZO_PTR_H
  267. #define __LZO_PTR_H
  268. #ifdef __cplusplus
  269. extern "C" {
  270. #endif
  271. #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
  272. # include <dos.h>
  273. # if 1 && defined(__WATCOMC__)
  274. # include <i86.h>
  275. __LZO_EXTERN_C unsigned char _HShift;
  276. # define __LZO_HShift _HShift
  277. # elif 1 && defined(_MSC_VER)
  278. __LZO_EXTERN_C unsigned short __near _AHSHIFT;
  279. # define __LZO_HShift ((unsigned) &_AHSHIFT)
  280. # elif defined(__LZO_WIN16)
  281. # define __LZO_HShift 3
  282. # else
  283. # define __LZO_HShift 12
  284. # endif
  285. # if !defined(_FP_SEG) && defined(FP_SEG)
  286. # define _FP_SEG FP_SEG
  287. # endif
  288. # if !defined(_FP_OFF) && defined(FP_OFF)
  289. # define _FP_OFF FP_OFF
  290. # endif
  291. #endif
  292. #if !defined(lzo_ptrdiff_t)
  293. # if (UINT_MAX >= LZO_0xffffffffL)
  294. typedef ptrdiff_t lzo_ptrdiff_t;
  295. # else
  296. typedef long lzo_ptrdiff_t;
  297. # endif
  298. #endif
  299. #if !defined(__LZO_HAVE_PTR_T)
  300. # if defined(lzo_ptr_t)
  301. # define __LZO_HAVE_PTR_T
  302. # endif
  303. #endif
  304. #if !defined(__LZO_HAVE_PTR_T)
  305. # if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_LONG)
  306. # if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_LONG)
  307. typedef unsigned long lzo_ptr_t;
  308. typedef long lzo_sptr_t;
  309. # define __LZO_HAVE_PTR_T
  310. # endif
  311. # endif
  312. #endif
  313. #if !defined(__LZO_HAVE_PTR_T)
  314. # if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED)
  315. # if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED)
  316. typedef unsigned int lzo_ptr_t;
  317. typedef int lzo_sptr_t;
  318. # define __LZO_HAVE_PTR_T
  319. # endif
  320. # endif
  321. #endif
  322. #if !defined(__LZO_HAVE_PTR_T)
  323. # if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_SHORT)
  324. # if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_SHORT)
  325. typedef unsigned short lzo_ptr_t;
  326. typedef short lzo_sptr_t;
  327. # define __LZO_HAVE_PTR_T
  328. # endif
  329. # endif
  330. #endif
  331. #if !defined(__LZO_HAVE_PTR_T)
  332. # if defined(LZO_HAVE_CONFIG_H) || defined(SIZEOF_CHAR_P)
  333. # error "no suitable type for lzo_ptr_t"
  334. # else
  335. typedef unsigned long lzo_ptr_t;
  336. typedef long lzo_sptr_t;
  337. # define __LZO_HAVE_PTR_T
  338. # endif
  339. #endif
  340. #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
  341. #define PTR(a) ((lzo_bytep) (a))
  342. #define PTR_ALIGNED_4(a) ((_FP_OFF(a) & 3) == 0)
  343. #define PTR_ALIGNED2_4(a,b) (((_FP_OFF(a) | _FP_OFF(b)) & 3) == 0)
  344. #else
  345. #define PTR(a) ((lzo_ptr_t) (a))
  346. #define PTR_LINEAR(a) PTR(a)
  347. #define PTR_ALIGNED_4(a) ((PTR_LINEAR(a) & 3) == 0)
  348. #define PTR_ALIGNED_8(a) ((PTR_LINEAR(a) & 7) == 0)
  349. #define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
  350. #define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
  351. #endif
  352. #define PTR_LT(a,b) (PTR(a) < PTR(b))
  353. #define PTR_GE(a,b) (PTR(a) >= PTR(b))
  354. #define PTR_DIFF(a,b) ((lzo_ptrdiff_t) (PTR(a) - PTR(b)))
  355. #define pd(a,b) ((lzo_uint) ((a)-(b)))
  356. LZO_EXTERN(lzo_ptr_t)
  357. __lzo_ptr_linear(const lzo_voidp ptr);
  358. typedef union
  359. {
  360. char a_char;
  361. unsigned char a_uchar;
  362. short a_short;
  363. unsigned short a_ushort;
  364. int a_int;
  365. unsigned int a_uint;
  366. long a_long;
  367. unsigned long a_ulong;
  368. lzo_int a_lzo_int;
  369. lzo_uint a_lzo_uint;
  370. lzo_int32 a_lzo_int32;
  371. lzo_uint32 a_lzo_uint32;
  372. ptrdiff_t a_ptrdiff_t;
  373. lzo_ptrdiff_t a_lzo_ptrdiff_t;
  374. lzo_ptr_t a_lzo_ptr_t;
  375. lzo_voidp a_lzo_voidp;
  376. void * a_void_p;
  377. lzo_bytep a_lzo_bytep;
  378. lzo_bytepp a_lzo_bytepp;
  379. lzo_uintp a_lzo_uintp;
  380. lzo_uint * a_lzo_uint_p;
  381. lzo_uint32p a_lzo_uint32p;
  382. lzo_uint32 * a_lzo_uint32_p;
  383. unsigned char * a_uchar_p;
  384. char * a_char_p;
  385. }
  386. lzo_full_align_t;
  387. #ifdef __cplusplus
  388. }
  389. #endif
  390. #endif
  391. #define LZO_DETERMINISTIC
  392. #define LZO_DICT_USE_PTR
  393. #if defined(__LZO_DOS16) || defined(__LZO_WIN16) || defined(__LZO_STRICT_16BIT)
  394. # undef LZO_DICT_USE_PTR
  395. #endif
  396. #if defined(LZO_DICT_USE_PTR)
  397. # define lzo_dict_t const lzo_bytep
  398. # define lzo_dict_p lzo_dict_t __LZO_MMODEL *
  399. #else
  400. # define lzo_dict_t lzo_uint
  401. # define lzo_dict_p lzo_dict_t __LZO_MMODEL *
  402. #endif
  403. #if !defined(lzo_moff_t)
  404. #define lzo_moff_t lzo_uint
  405. #endif
  406. #endif
  407. LZO_PUBLIC(lzo_ptr_t)
  408. __lzo_ptr_linear(const lzo_voidp ptr)
  409. {
  410. lzo_ptr_t p;
  411. #if defined(__LZO_DOS16) || defined(__LZO_WIN16)
  412. p = (((lzo_ptr_t)(_FP_SEG(ptr))) << (16 - __LZO_HShift)) + (_FP_OFF(ptr));
  413. #else
  414. p = PTR_LINEAR(ptr);
  415. #endif
  416. return p;
  417. }
  418. LZO_PUBLIC(unsigned)
  419. __lzo_align_gap(const lzo_voidp ptr, lzo_uint size)
  420. {
  421. lzo_ptr_t p, s, n;
  422. assert(size > 0);
  423. p = __lzo_ptr_linear(ptr);
  424. s = (lzo_ptr_t) (size - 1);
  425. #if 0
  426. assert((size & (size - 1)) == 0);
  427. n = ((p + s) & ~s) - p;
  428. #else
  429. n = (((p + s) / size) * size) - p;
  430. #endif
  431. assert((long)n >= 0);
  432. assert(n <= s);
  433. return (unsigned)n;
  434. }
  435. #ifndef __LZO_UTIL_H
  436. #define __LZO_UTIL_H
  437. #ifndef __LZO_CONF_H
  438. #endif
  439. #ifdef __cplusplus
  440. extern "C" {
  441. #endif
  442. #if 1 && defined(HAVE_MEMCPY)
  443. #if !defined(__LZO_DOS16) && !defined(__LZO_WIN16)
  444. #define MEMCPY8_DS(dest,src,len) \
  445. memcpy(dest,src,len); \
  446. dest += len; \
  447. src += len
  448. #endif
  449. #endif
  450. #if 0 && !defined(MEMCPY8_DS)
  451. #define MEMCPY8_DS(dest,src,len) \
  452. { do { \
  453. *dest++ = *src++; \
  454. *dest++ = *src++; \
  455. *dest++ = *src++; \
  456. *dest++ = *src++; \
  457. *dest++ = *src++; \
  458. *dest++ = *src++; \
  459. *dest++ = *src++; \
  460. *dest++ = *src++; \
  461. len -= 8; \
  462. } while (len > 0); }
  463. #endif
  464. #if !defined(MEMCPY8_DS)
  465. #define MEMCPY8_DS(dest,src,len) \
  466. { register lzo_uint __l = (len) / 8; \
  467. do { \
  468. *dest++ = *src++; \
  469. *dest++ = *src++; \
  470. *dest++ = *src++; \
  471. *dest++ = *src++; \
  472. *dest++ = *src++; \
  473. *dest++ = *src++; \
  474. *dest++ = *src++; \
  475. *dest++ = *src++; \
  476. } while (--__l > 0); }
  477. #endif
  478. #define MEMCPY_DS(dest,src,len) \
  479. do *dest++ = *src++; \
  480. while (--len > 0)
  481. #define MEMMOVE_DS(dest,src,len) \
  482. do *dest++ = *src++; \
  483. while (--len > 0)
  484. #if 0 && defined(LZO_OPTIMIZE_GNUC_i386)
  485. #define BZERO8_PTR(s,l,n) \
  486. __asm__ __volatile__( \
  487. "movl %0,%%eax \n" \
  488. "movl %1,%%edi \n" \
  489. "movl %2,%%ecx \n" \
  490. "cld \n" \
  491. "rep \n" \
  492. "stosl %%eax,(%%edi) \n" \
  493. : \
  494. :"g" (0),"g" (s),"g" (n) \
  495. :"eax","edi","ecx", "memory", "cc" \
  496. )
  497. #elif (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET)
  498. #if 1
  499. #define BZERO8_PTR(s,l,n) memset((s),0,(lzo_uint)(l)*(n))
  500. #else
  501. #define BZERO8_PTR(s,l,n) memset((lzo_voidp)(s),0,(lzo_uint)(l)*(n))
  502. #endif
  503. #else
  504. #define BZERO8_PTR(s,l,n) \
  505. lzo_memset((lzo_voidp)(s),0,(lzo_uint)(l)*(n))
  506. #endif
  507. #if 0
  508. #if defined(__GNUC__) && defined(__i386__)
  509. unsigned char lzo_rotr8(unsigned char value, int shift);
  510. extern __inline__ unsigned char lzo_rotr8(unsigned char value, int shift)
  511. {
  512. unsigned char result;
  513. __asm__ __volatile__ ("movb %b1, %b0; rorb %b2, %b0"
  514. : "=a"(result) : "g"(value), "c"(shift));
  515. return result;
  516. }
  517. unsigned short lzo_rotr16(unsigned short value, int shift);
  518. extern __inline__ unsigned short lzo_rotr16(unsigned short value, int shift)
  519. {
  520. unsigned short result;
  521. __asm__ __volatile__ ("movw %b1, %b0; rorw %b2, %b0"
  522. : "=a"(result) : "g"(value), "c"(shift));
  523. return result;
  524. }
  525. #endif
  526. #endif
  527. #ifdef __cplusplus
  528. }
  529. #endif
  530. #endif
  531. LZO_PUBLIC(lzo_bool)
  532. lzo_assert(int expr)
  533. {
  534. return (expr) ? 1 : 0;
  535. }
  536. /* If you use the LZO library in a product, you *must* keep this
  537. * copyright string in the executable of your product.
  538. */
  539. const lzo_byte __lzo_copyright[] =
  540. #if !defined(__LZO_IN_MINLZO)
  541. LZO_VERSION_STRING;
  542. #else
  543. "\n\n\n"
  544. "LZO real-time data compression library.\n"
  545. "Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 Markus Franz Xaver Johannes Oberhumer\n"
  546. "<markus.oberhumer@jk.uni-linz.ac.at>\n"
  547. "http://www.oberhumer.com/opensource/lzo/\n"
  548. "\n"
  549. "LZO version: v" LZO_VERSION_STRING ", " LZO_VERSION_DATE "\n"
  550. "LZO build date: " __DATE__ " " __TIME__ "\n\n"
  551. "LZO special compilation options:\n"
  552. #ifdef __cplusplus
  553. " __cplusplus\n"
  554. #endif
  555. #if defined(__PIC__)
  556. " __PIC__\n"
  557. #elif defined(__pic__)
  558. " __pic__\n"
  559. #endif
  560. #if (UINT_MAX < LZO_0xffffffffL)
  561. " 16BIT\n"
  562. #endif
  563. #if defined(__LZO_STRICT_16BIT)
  564. " __LZO_STRICT_16BIT\n"
  565. #endif
  566. #if (UINT_MAX > LZO_0xffffffffL)
  567. " UINT_MAX=" _LZO_MEXPAND(UINT_MAX) "\n"
  568. #endif
  569. #if (ULONG_MAX > LZO_0xffffffffL)
  570. " ULONG_MAX=" _LZO_MEXPAND(ULONG_MAX) "\n"
  571. #endif
  572. #if defined(LZO_BYTE_ORDER)
  573. " LZO_BYTE_ORDER=" _LZO_MEXPAND(LZO_BYTE_ORDER) "\n"
  574. #endif
  575. #if defined(LZO_UNALIGNED_OK_2)
  576. " LZO_UNALIGNED_OK_2\n"
  577. #endif
  578. #if defined(LZO_UNALIGNED_OK_4)
  579. " LZO_UNALIGNED_OK_4\n"
  580. #endif
  581. #if defined(LZO_ALIGNED_OK_4)
  582. " LZO_ALIGNED_OK_4\n"
  583. #endif
  584. #if defined(LZO_DICT_USE_PTR)
  585. " LZO_DICT_USE_PTR\n"
  586. #endif
  587. #if defined(__LZO_QUERY_COMPRESS)
  588. " __LZO_QUERY_COMPRESS\n"
  589. #endif
  590. #if defined(__LZO_QUERY_DECOMPRESS)
  591. " __LZO_QUERY_DECOMPRESS\n"
  592. #endif
  593. #if defined(__LZO_IN_MINILZO)
  594. " __LZO_IN_MINILZO\n"
  595. #endif
  596. "\n\n"
  597. "$Id: LZO " LZO_VERSION_STRING " built " __DATE__ " " __TIME__
  598. #if defined(__GNUC__) && defined(__VERSION__)
  599. " by gcc " __VERSION__
  600. #elif defined(__BORLANDC__)
  601. " by Borland C " _LZO_MEXPAND(__BORLANDC__)
  602. #elif defined(_MSC_VER)
  603. " by Microsoft C " _LZO_MEXPAND(_MSC_VER)
  604. #elif defined(__PUREC__)
  605. " by Pure C " _LZO_MEXPAND(__PUREC__)
  606. #elif defined(__SC__)
  607. " by Symantec C " _LZO_MEXPAND(__SC__)
  608. #elif defined(__TURBOC__)
  609. " by Turbo C " _LZO_MEXPAND(__TURBOC__)
  610. #elif defined(__WATCOMC__)
  611. " by Watcom C " _LZO_MEXPAND(__WATCOMC__)
  612. #endif
  613. " $\n"
  614. "$Copyright: LZO (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 Markus Franz Xaver Johannes Oberhumer $\n";
  615. #endif
  616. LZO_PUBLIC(const lzo_byte *)
  617. lzo_copyright(void)
  618. {
  619. return __lzo_copyright;
  620. }
  621. LZO_PUBLIC(unsigned)
  622. lzo_version(void)
  623. {
  624. return LZO_VERSION;
  625. }
  626. LZO_PUBLIC(const char *)
  627. lzo_version_string(void)
  628. {
  629. return LZO_VERSION_STRING;
  630. }
  631. LZO_PUBLIC(const char *)
  632. lzo_version_date(void)
  633. {
  634. return LZO_VERSION_DATE;
  635. }
  636. LZO_PUBLIC(const lzo_charp)
  637. _lzo_version_string(void)
  638. {
  639. return LZO_VERSION_STRING;
  640. }
  641. LZO_PUBLIC(const lzo_charp)
  642. _lzo_version_date(void)
  643. {
  644. return LZO_VERSION_DATE;
  645. }
  646. #define LZO_BASE 65521u
  647. #define LZO_NMAX 5552
  648. #define LZO_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
  649. #define LZO_DO2(buf,i) LZO_DO1(buf,i); LZO_DO1(buf,i+1);
  650. #define LZO_DO4(buf,i) LZO_DO2(buf,i); LZO_DO2(buf,i+2);
  651. #define LZO_DO8(buf,i) LZO_DO4(buf,i); LZO_DO4(buf,i+4);
  652. #define LZO_DO16(buf,i) LZO_DO8(buf,i); LZO_DO8(buf,i+8);
  653. LZO_PUBLIC(lzo_uint32)
  654. lzo_adler32(lzo_uint32 adler, const lzo_byte *buf, lzo_uint len)
  655. {
  656. lzo_uint32 s1 = adler & 0xffff;
  657. lzo_uint32 s2 = (adler >> 16) & 0xffff;
  658. int k;
  659. if (buf == NULL)
  660. return 1;
  661. while (len > 0)
  662. {
  663. k = len < LZO_NMAX ? (int) len : LZO_NMAX;
  664. len -= k;
  665. if (k >= 16) do
  666. {
  667. LZO_DO16(buf,0);
  668. buf += 16;
  669. k -= 16;
  670. } while (k >= 16);
  671. if (k != 0) do
  672. {
  673. s1 += *buf++;
  674. s2 += s1;
  675. } while (--k > 0);
  676. s1 %= LZO_BASE;
  677. s2 %= LZO_BASE;
  678. }
  679. return (s2 << 16) | s1;
  680. }
  681. LZO_PUBLIC(int)
  682. lzo_memcmp(const lzo_voidp s1, const lzo_voidp s2, lzo_uint len)
  683. {
  684. #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCMP)
  685. return memcmp(s1,s2,len);
  686. #else
  687. const lzo_byte *p1 = (const lzo_byte *) s1;
  688. const lzo_byte *p2 = (const lzo_byte *) s2;
  689. int d;
  690. if (len > 0) do
  691. {
  692. d = *p1 - *p2;
  693. if (d != 0)
  694. return d;
  695. p1++;
  696. p2++;
  697. }
  698. while (--len > 0);
  699. return 0;
  700. #endif
  701. }
  702. LZO_PUBLIC(lzo_voidp)
  703. lzo_memcpy(lzo_voidp dest, const lzo_voidp src, lzo_uint len)
  704. {
  705. #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCPY)
  706. return memcpy(dest,src,len);
  707. #else
  708. lzo_byte *p1 = (lzo_byte *) dest;
  709. const lzo_byte *p2 = (const lzo_byte *) src;
  710. if (len <= 0 || p1 == p2)
  711. return dest;
  712. do
  713. *p1++ = *p2++;
  714. while (--len > 0);
  715. return dest;
  716. #endif
  717. }
  718. LZO_PUBLIC(lzo_voidp)
  719. lzo_memmove(lzo_voidp dest, const lzo_voidp src, lzo_uint len)
  720. {
  721. #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMMOVE)
  722. return memmove(dest,src,len);
  723. #else
  724. lzo_byte *p1 = (lzo_byte *) dest;
  725. const lzo_byte *p2 = (const lzo_byte *) src;
  726. if (len <= 0 || p1 == p2)
  727. return dest;
  728. if (p1 < p2)
  729. {
  730. do
  731. *p1++ = *p2++;
  732. while (--len > 0);
  733. }
  734. else
  735. {
  736. p1 += len;
  737. p2 += len;
  738. do
  739. *--p1 = *--p2;
  740. while (--len > 0);
  741. }
  742. return dest;
  743. #endif
  744. }
  745. LZO_PUBLIC(lzo_voidp)
  746. lzo_memset(lzo_voidp s, int c, lzo_uint len)
  747. {
  748. #if (LZO_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET)
  749. return memset(s,c,len);
  750. #else
  751. lzo_byte *p = (lzo_byte *) s;
  752. if (len > 0) do
  753. *p++ = LZO_BYTE(c);
  754. while (--len > 0);
  755. return s;
  756. #endif
  757. }
  758. #if 0
  759. # define IS_SIGNED(type) (((type) (1ul << (8 * sizeof(type) - 1))) < 0)
  760. # define IS_UNSIGNED(type) (((type) (1ul << (8 * sizeof(type) - 1))) > 0)
  761. #else
  762. # define IS_SIGNED(type) (((type) (-1)) < ((type) 0))
  763. # define IS_UNSIGNED(type) (((type) (-1)) > ((type) 0))
  764. #endif
  765. #define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
  766. static lzo_bool schedule_insns_bug(void);
  767. static lzo_bool strength_reduce_bug(int *);
  768. #if 0 || defined(LZO_DEBUG)
  769. #include <stdio.h>
  770. static lzo_bool __lzo_assert_fail(const char *s, unsigned line)
  771. {
  772. #if defined(__palmos__)
  773. printf("LZO assertion failed in line %u: '%s'\n",line,s);
  774. #else
  775. fprintf(stderr,"LZO assertion failed in line %u: '%s'\n",line,s);
  776. #endif
  777. return 0;
  778. }
  779. # define __lzo_assert(x) ((x) ? 1 : __lzo_assert_fail(#x,__LINE__))
  780. #else
  781. # define __lzo_assert(x) ((x) ? 1 : 0)
  782. #endif
  783. #undef COMPILE_TIME_ASSERT
  784. #if 0
  785. # define COMPILE_TIME_ASSERT(expr) r &= __lzo_assert(expr)
  786. #else
  787. # define COMPILE_TIME_ASSERT(expr) LZO_COMPILE_TIME_ASSERT(expr)
  788. #endif
  789. static lzo_bool basic_integral_check(void)
  790. {
  791. lzo_bool r = 1;
  792. COMPILE_TIME_ASSERT(CHAR_BIT == 8);
  793. COMPILE_TIME_ASSERT(sizeof(char) == 1);
  794. COMPILE_TIME_ASSERT(sizeof(short) >= 2);
  795. COMPILE_TIME_ASSERT(sizeof(long) >= 4);
  796. COMPILE_TIME_ASSERT(sizeof(int) >= sizeof(short));
  797. COMPILE_TIME_ASSERT(sizeof(long) >= sizeof(int));
  798. COMPILE_TIME_ASSERT(sizeof(lzo_uint) == sizeof(lzo_int));
  799. COMPILE_TIME_ASSERT(sizeof(lzo_uint32) == sizeof(lzo_int32));
  800. COMPILE_TIME_ASSERT(sizeof(lzo_uint32) >= 4);
  801. COMPILE_TIME_ASSERT(sizeof(lzo_uint32) >= sizeof(unsigned));
  802. #if defined(__LZO_STRICT_16BIT)
  803. COMPILE_TIME_ASSERT(sizeof(lzo_uint) == 2);
  804. #else
  805. COMPILE_TIME_ASSERT(sizeof(lzo_uint) >= 4);
  806. COMPILE_TIME_ASSERT(sizeof(lzo_uint) >= sizeof(unsigned));
  807. #endif
  808. #if (USHRT_MAX == 65535u)
  809. COMPILE_TIME_ASSERT(sizeof(short) == 2);
  810. #elif (USHRT_MAX == LZO_0xffffffffL)
  811. COMPILE_TIME_ASSERT(sizeof(short) == 4);
  812. #elif (USHRT_MAX >= LZO_0xffffffffL)
  813. COMPILE_TIME_ASSERT(sizeof(short) > 4);
  814. #endif
  815. #if (UINT_MAX == 65535u)
  816. COMPILE_TIME_ASSERT(sizeof(int) == 2);
  817. #elif (UINT_MAX == LZO_0xffffffffL)
  818. COMPILE_TIME_ASSERT(sizeof(int) == 4);
  819. #elif (UINT_MAX >= LZO_0xffffffffL)
  820. COMPILE_TIME_ASSERT(sizeof(int) > 4);
  821. #endif
  822. #if (ULONG_MAX == 65535ul)
  823. COMPILE_TIME_ASSERT(sizeof(long) == 2);
  824. #elif (ULONG_MAX == LZO_0xffffffffL)
  825. COMPILE_TIME_ASSERT(sizeof(long) == 4);
  826. #elif (ULONG_MAX >= LZO_0xffffffffL)
  827. COMPILE_TIME_ASSERT(sizeof(long) > 4);
  828. #endif
  829. #if defined(SIZEOF_UNSIGNED)
  830. COMPILE_TIME_ASSERT(SIZEOF_UNSIGNED == sizeof(unsigned));
  831. #endif
  832. #if defined(SIZEOF_UNSIGNED_LONG)
  833. COMPILE_TIME_ASSERT(SIZEOF_UNSIGNED_LONG == sizeof(unsigned long));
  834. #endif
  835. #if defined(SIZEOF_UNSIGNED_SHORT)
  836. COMPILE_TIME_ASSERT(SIZEOF_UNSIGNED_SHORT == sizeof(unsigned short));
  837. #endif
  838. #if !defined(__LZO_IN_MINILZO)
  839. #if defined(SIZEOF_SIZE_T)
  840. COMPILE_TIME_ASSERT(SIZEOF_SIZE_T == sizeof(size_t));
  841. #endif
  842. #endif
  843. COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned char));
  844. COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned short));
  845. COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned));
  846. COMPILE_TIME_ASSERT(IS_UNSIGNED(unsigned long));
  847. COMPILE_TIME_ASSERT(IS_SIGNED(short));
  848. COMPILE_TIME_ASSERT(IS_SIGNED(int));
  849. COMPILE_TIME_ASSERT(IS_SIGNED(long));
  850. COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_uint32));
  851. COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_uint));
  852. COMPILE_TIME_ASSERT(IS_SIGNED(lzo_int32));
  853. COMPILE_TIME_ASSERT(IS_SIGNED(lzo_int));
  854. COMPILE_TIME_ASSERT(INT_MAX == LZO_STYPE_MAX(sizeof(int)));
  855. COMPILE_TIME_ASSERT(UINT_MAX == LZO_UTYPE_MAX(sizeof(unsigned)));
  856. COMPILE_TIME_ASSERT(LONG_MAX == LZO_STYPE_MAX(sizeof(long)));
  857. COMPILE_TIME_ASSERT(ULONG_MAX == LZO_UTYPE_MAX(sizeof(unsigned long)));
  858. COMPILE_TIME_ASSERT(SHRT_MAX == LZO_STYPE_MAX(sizeof(short)));
  859. COMPILE_TIME_ASSERT(USHRT_MAX == LZO_UTYPE_MAX(sizeof(unsigned short)));
  860. COMPILE_TIME_ASSERT(LZO_UINT32_MAX == LZO_UTYPE_MAX(sizeof(lzo_uint32)));
  861. COMPILE_TIME_ASSERT(LZO_UINT_MAX == LZO_UTYPE_MAX(sizeof(lzo_uint)));
  862. #if !defined(__LZO_IN_MINILZO)
  863. COMPILE_TIME_ASSERT(SIZE_T_MAX == LZO_UTYPE_MAX(sizeof(size_t)));
  864. #endif
  865. r &= __lzo_assert(LZO_BYTE(257) == 1);
  866. return r;
  867. }
  868. static lzo_bool basic_ptr_check(void)
  869. {
  870. lzo_bool r = 1;
  871. COMPILE_TIME_ASSERT(sizeof(char *) >= sizeof(int));
  872. COMPILE_TIME_ASSERT(sizeof(lzo_byte *) >= sizeof(char *));
  873. COMPILE_TIME_ASSERT(sizeof(lzo_voidp) == sizeof(lzo_byte *));
  874. COMPILE_TIME_ASSERT(sizeof(lzo_voidp) == sizeof(lzo_voidpp));
  875. COMPILE_TIME_ASSERT(sizeof(lzo_voidp) == sizeof(lzo_bytepp));
  876. COMPILE_TIME_ASSERT(sizeof(lzo_voidp) >= sizeof(lzo_uint));
  877. COMPILE_TIME_ASSERT(sizeof(lzo_ptr_t) == sizeof(lzo_voidp));
  878. COMPILE_TIME_ASSERT(sizeof(lzo_ptr_t) == sizeof(lzo_sptr_t));
  879. COMPILE_TIME_ASSERT(sizeof(lzo_ptr_t) >= sizeof(lzo_uint));
  880. COMPILE_TIME_ASSERT(sizeof(lzo_ptrdiff_t) >= 4);
  881. COMPILE_TIME_ASSERT(sizeof(lzo_ptrdiff_t) >= sizeof(ptrdiff_t));
  882. COMPILE_TIME_ASSERT(sizeof(ptrdiff_t) >= sizeof(size_t));
  883. COMPILE_TIME_ASSERT(sizeof(lzo_ptrdiff_t) >= sizeof(lzo_uint));
  884. #if defined(SIZEOF_CHAR_P)
  885. COMPILE_TIME_ASSERT(SIZEOF_CHAR_P == sizeof(char *));
  886. #endif
  887. #if defined(SIZEOF_PTRDIFF_T)
  888. COMPILE_TIME_ASSERT(SIZEOF_PTRDIFF_T == sizeof(ptrdiff_t));
  889. #endif
  890. COMPILE_TIME_ASSERT(IS_SIGNED(ptrdiff_t));
  891. COMPILE_TIME_ASSERT(IS_UNSIGNED(size_t));
  892. COMPILE_TIME_ASSERT(IS_SIGNED(lzo_ptrdiff_t));
  893. COMPILE_TIME_ASSERT(IS_SIGNED(lzo_sptr_t));
  894. COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_ptr_t));
  895. COMPILE_TIME_ASSERT(IS_UNSIGNED(lzo_moff_t));
  896. return r;
  897. }
  898. static lzo_bool ptr_check(void)
  899. {
  900. lzo_bool r = 1;
  901. int i;
  902. char _wrkmem[10 * sizeof(lzo_byte *) + sizeof(lzo_full_align_t)];
  903. lzo_bytep wrkmem;
  904. lzo_bytepp dict;
  905. unsigned char x[4 * sizeof(lzo_full_align_t)];
  906. long d;
  907. lzo_full_align_t a;
  908. lzo_full_align_t u;
  909. for (i = 0; i < (int) sizeof(x); i++)
  910. x[i] = LZO_BYTE(i);
  911. wrkmem = LZO_PTR_ALIGN_UP((lzo_byte *)_wrkmem,sizeof(lzo_full_align_t));
  912. #if 0
  913. dict = (lzo_bytepp) wrkmem;
  914. #else
  915. u.a_lzo_bytep = wrkmem; dict = u.a_lzo_bytepp;
  916. #endif
  917. d = (long) ((const lzo_bytep) dict - (const lzo_bytep) _wrkmem);
  918. r &= __lzo_assert(d >= 0);
  919. r &= __lzo_assert(d < (long) sizeof(lzo_full_align_t));
  920. memset(&a,0,sizeof(a));
  921. r &= __lzo_assert(a.a_lzo_voidp == NULL);
  922. memset(&a,0xff,sizeof(a));
  923. r &= __lzo_assert(a.a_ushort == USHRT_MAX);
  924. r &= __lzo_assert(a.a_uint == UINT_MAX);
  925. r &= __lzo_assert(a.a_ulong == ULONG_MAX);
  926. r &= __lzo_assert(a.a_lzo_uint == LZO_UINT_MAX);
  927. r &= __lzo_assert(a.a_lzo_uint32 == LZO_UINT32_MAX);
  928. if (r == 1)
  929. {
  930. for (i = 0; i < 8; i++)
  931. r &= __lzo_assert((const lzo_voidp) (&dict[i]) == (const lzo_voidp) (&wrkmem[i * sizeof(lzo_byte *)]));
  932. }
  933. memset(&a,0,sizeof(a));
  934. r &= __lzo_assert(a.a_char_p == NULL);
  935. r &= __lzo_assert(a.a_lzo_bytep == NULL);
  936. r &= __lzo_assert(NULL == (void *)0);
  937. if (r == 1)
  938. {
  939. for (i = 0; i < 10; i++)
  940. dict[i] = wrkmem;
  941. BZERO8_PTR(dict+1,sizeof(dict[0]),8);
  942. r &= __lzo_assert(dict[0] == wrkmem);
  943. for (i = 1; i < 9; i++)
  944. r &= __lzo_assert(dict[i] == NULL);
  945. r &= __lzo_assert(dict[9] == wrkmem);
  946. }
  947. if (r == 1)
  948. {
  949. unsigned k = 1;
  950. const unsigned n = (unsigned) sizeof(lzo_uint32);
  951. lzo_byte *p0;
  952. lzo_byte *p1;
  953. k += __lzo_align_gap(&x[k],n);
  954. p0 = (lzo_bytep) &x[k];
  955. #if defined(PTR_LINEAR)
  956. r &= __lzo_assert((PTR_LINEAR(p0) & (n-1)) == 0);
  957. #else
  958. r &= __lzo_assert(n == 4);
  959. r &= __lzo_assert(PTR_ALIGNED_4(p0));
  960. #endif
  961. r &= __lzo_assert(k >= 1);
  962. p1 = (lzo_bytep) &x[1];
  963. r &= __lzo_assert(PTR_GE(p0,p1));
  964. r &= __lzo_assert(k < 1+n);
  965. p1 = (lzo_bytep) &x[1+n];
  966. r &= __lzo_assert(PTR_LT(p0,p1));
  967. if (r == 1)
  968. {
  969. lzo_uint32 v0, v1;
  970. #if 0
  971. v0 = * (lzo_uint32 *) &x[k];
  972. v1 = * (lzo_uint32 *) &x[k+n];
  973. #else
  974. u.a_uchar_p = &x[k];
  975. v0 = *u.a_lzo_uint32_p;
  976. u.a_uchar_p = &x[k+n];
  977. v1 = *u.a_lzo_uint32_p;
  978. #endif
  979. r &= __lzo_assert(v0 > 0);
  980. r &= __lzo_assert(v1 > 0);
  981. }
  982. }
  983. return r;
  984. }
  985. LZO_PUBLIC(int)
  986. _lzo_config_check(void)
  987. {
  988. lzo_bool r = 1;
  989. int i;
  990. union {
  991. lzo_uint32 a;
  992. unsigned short b;
  993. lzo_uint32 aa[4];
  994. unsigned char x[4*sizeof(lzo_full_align_t)];
  995. } u;
  996. COMPILE_TIME_ASSERT( (int) ((unsigned char) ((signed char) -1)) == 255);
  997. COMPILE_TIME_ASSERT( (((unsigned char)128) << (int)(8*sizeof(int)-8)) < 0);
  998. #if 0
  999. r &= __lzo_assert((const void *)&u == (const void *)&u.a);
  1000. r &= __lzo_assert((const void *)&u == (const void *)&u.b);
  1001. r &= __lzo_assert((const void *)&u == (const void *)&u.x[0]);
  1002. r &= __lzo_assert((const void *)&u == (const void *)&u.aa[0]);
  1003. #endif
  1004. r &= basic_integral_check();
  1005. r &= basic_ptr_check();
  1006. if (r != 1)
  1007. return LZO_E_ERROR;
  1008. u.a = 0; u.b = 0;
  1009. for (i = 0; i < (int) sizeof(u.x); i++)
  1010. u.x[i] = LZO_BYTE(i);
  1011. #if defined(LZO_BYTE_ORDER)
  1012. if (r == 1)
  1013. {
  1014. # if (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
  1015. lzo_uint32 a = (lzo_uint32) (u.a & LZO_0xffffffffL);
  1016. unsigned short b = (unsigned short) (u.b & 0xffff);
  1017. r &= __lzo_assert(a == 0x03020100L);
  1018. r &= __lzo_assert(b == 0x0100);
  1019. # elif (LZO_BYTE_ORDER == LZO_BIG_ENDIAN)
  1020. lzo_uint32 a = u.a >> (8 * sizeof(u.a) - 32);
  1021. unsigned short b = u.b >> (8 * sizeof(u.b) - 16);
  1022. r &= __lzo_assert(a == 0x00010203L);
  1023. r &= __lzo_assert(b == 0x0001);
  1024. # else
  1025. # error "invalid LZO_BYTE_ORDER"
  1026. # endif
  1027. }
  1028. #endif
  1029. #if defined(LZO_UNALIGNED_OK_2)
  1030. COMPILE_TIME_ASSERT(sizeof(short) == 2);
  1031. if (r == 1)
  1032. {
  1033. unsigned short b[4];
  1034. for (i = 0; i < 4; i++)
  1035. b[i] = * (const unsigned short *) &u.x[i];
  1036. # if (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
  1037. r &= __lzo_assert(b[0] == 0x0100);
  1038. r &= __lzo_assert(b[1] == 0x0201);
  1039. r &= __lzo_assert(b[2] == 0x0302);
  1040. r &= __lzo_assert(b[3] == 0x0403);
  1041. # elif (LZO_BYTE_ORDER == LZO_BIG_ENDIAN)
  1042. r &= __lzo_assert(b[0] == 0x0001);
  1043. r &= __lzo_assert(b[1] == 0x0102);
  1044. r &= __lzo_assert(b[2] == 0x0203);
  1045. r &= __lzo_assert(b[3] == 0x0304);
  1046. # endif
  1047. }
  1048. #endif
  1049. #if defined(LZO_UNALIGNED_OK_4)
  1050. COMPILE_TIME_ASSERT(sizeof(lzo_uint32) == 4);
  1051. if (r == 1)
  1052. {
  1053. lzo_uint32 a[4];
  1054. for (i = 0; i < 4; i++)
  1055. a[i] = * (const lzo_uint32 *) &u.x[i];
  1056. # if (LZO_BYTE_ORDER == LZO_LITTLE_ENDIAN)
  1057. r &= __lzo_assert(a[0] == 0x03020100L);
  1058. r &= __lzo_assert(a[1] == 0x04030201L);
  1059. r &= __lzo_assert(a[2] == 0x05040302L);
  1060. r &= __lzo_assert(a[3] == 0x06050403L);
  1061. # elif (LZO_BYTE_ORDER == LZO_BIG_ENDIAN)
  1062. r &= __lzo_assert(a[0] == 0x00010203L);
  1063. r &= __lzo_assert(a[1] == 0x01020304L);
  1064. r &= __lzo_assert(a[2] == 0x02030405L);
  1065. r &= __lzo_assert(a[3] == 0x03040506L);
  1066. # endif
  1067. }
  1068. #endif
  1069. #if defined(LZO_ALIGNED_OK_4)
  1070. COMPILE_TIME_ASSERT(sizeof(lzo_uint32) == 4);
  1071. #endif
  1072. COMPILE_TIME_ASSERT(lzo_sizeof_dict_t == sizeof(lzo_dict_t));
  1073. #if defined(__LZO_IN_MINLZO)
  1074. if (r == 1)
  1075. {
  1076. lzo_uint32 adler;
  1077. adler = lzo_adler32(0, NULL, 0);
  1078. adler = lzo_adler32(adler, lzo_copyright(), 200);
  1079. r &= __lzo_assert(adler == 0xc76f1751L);
  1080. }
  1081. #endif
  1082. if (r == 1)
  1083. {
  1084. r &= __lzo_assert(!schedule_insns_bug());
  1085. }
  1086. if (r == 1)
  1087. {
  1088. static int x[3];
  1089. static unsigned xn = 3;
  1090. register unsigned j;
  1091. for (j = 0; j < xn; j++)
  1092. x[j] = (int)j - 3;
  1093. r &= __lzo_assert(!strength_reduce_bug(x));
  1094. }
  1095. if (r == 1)
  1096. {
  1097. r &= ptr_check();
  1098. }
  1099. return r == 1 ? LZO_E_OK : LZO_E_ERROR;
  1100. }
  1101. static lzo_bool schedule_insns_bug(void)
  1102. {
  1103. #if defined(__LZO_CHECKER)
  1104. return 0;
  1105. #else
  1106. const int clone[] = {1, 2, 0};
  1107. const int *q;
  1108. q = clone;
  1109. return (*q) ? 0 : 1;
  1110. #endif
  1111. }
  1112. static lzo_bool strength_reduce_bug(int *x)
  1113. {
  1114. return x[0] != -3 || x[1] != -2 || x[2] != -1;
  1115. }
  1116. #undef COMPILE_TIME_ASSERT
  1117. int __lzo_init_done = 0;
  1118. LZO_PUBLIC(int)
  1119. __lzo_init2(unsigned v, int s1, int s2, int s3, int s4, int s5,
  1120. int s6, int s7, int s8, int s9)
  1121. {
  1122. int r;
  1123. __lzo_init_done = 1;
  1124. if (v == 0)
  1125. return LZO_E_ERROR;
  1126. r = (s1 == -1 || s1 == (int) sizeof(short)) &&
  1127. (s2 == -1 || s2 == (int) sizeof(int)) &&
  1128. (s3 == -1 || s3 == (int) sizeof(long)) &&
  1129. (s4 == -1 || s4 == (int) sizeof(lzo_uint32)) &&
  1130. (s5 == -1 || s5 == (int) sizeof(lzo_uint)) &&
  1131. (s6 == -1 || s6 == (int) lzo_sizeof_dict_t) &&
  1132. (s7 == -1 || s7 == (int) sizeof(char *)) &&
  1133. (s8 == -1 || s8 == (int) sizeof(lzo_voidp)) &&
  1134. (s9 == -1 || s9 == (int) sizeof(lzo_compress_t));
  1135. if (!r)
  1136. return LZO_E_ERROR;
  1137. r = _lzo_config_check();
  1138. if (r != LZO_E_OK)
  1139. return r;
  1140. return r;
  1141. }
  1142. #if !defined(__LZO_IN_MINILZO)
  1143. LZO_EXTERN(int)
  1144. __lzo_init(unsigned v,int s1,int s2,int s3,int s4,int s5,int s6,int s7);
  1145. LZO_PUBLIC(int)
  1146. __lzo_init(unsigned v,int s1,int s2,int s3,int s4,int s5,int s6,int s7)
  1147. {
  1148. if (v == 0 || v > 0x1010)
  1149. return LZO_E_ERROR;
  1150. return __lzo_init2(v,s1,s2,s3,s4,s5,-1,-1,s6,s7);
  1151. }
  1152. #endif
  1153. #define do_compress _lzo1x_1_do_compress
  1154. #define LZO_NEED_DICT_H
  1155. #define D_BITS 14
  1156. #define D_INDEX1(d,p) d = DM((0x21*DX3(p,5,5,6)) >> 5)
  1157. #define D_INDEX2(d,p) d = (d & (D_MASK & 0x7ff)) ^ (D_HIGH | 0x1f)
  1158. #ifndef __LZO_CONFIG1X_H
  1159. #define __LZO_CONFIG1X_H
  1160. #if !defined(LZO1X) && !defined(LZO1Y) && !defined(LZO1Z)
  1161. # define LZO1X
  1162. #endif
  1163. #if !defined(__LZO_IN_MINILZO)
  1164. #include <lzo1x.h>
  1165. #endif
  1166. #define LZO_EOF_CODE
  1167. #undef LZO_DETERMINISTIC
  1168. #define M1_MAX_OFFSET 0x0400
  1169. #ifndef M2_MAX_OFFSET
  1170. #define M2_MAX_OFFSET 0x0800
  1171. #endif
  1172. #define M3_MAX_OFFSET 0x4000
  1173. #define M4_MAX_OFFSET 0xbfff
  1174. #define MX_MAX_OFFSET (M1_MAX_OFFSET + M2_MAX_OFFSET)
  1175. #define M1_MIN_LEN 2
  1176. #define M1_MAX_LEN 2
  1177. #define M2_MIN_LEN 3
  1178. #ifndef M2_MAX_LEN
  1179. #define M2_MAX_LEN 8
  1180. #endif
  1181. #define M3_MIN_LEN 3
  1182. #define M3_MAX_LEN 33
  1183. #define M4_MIN_LEN 3
  1184. #define M4_MAX_LEN 9
  1185. #define M1_MARKER 0
  1186. #define M2_MARKER 64
  1187. #define M3_MARKER 32
  1188. #define M4_MARKER 16
  1189. #ifndef MIN_LOOKAHEAD
  1190. #define MIN_LOOKAHEAD (M2_MAX_LEN + 1)
  1191. #endif
  1192. #if defined(LZO_NEED_DICT_H)
  1193. #ifndef LZO_HASH
  1194. #define LZO_HASH LZO_HASH_LZO_INCREMENTAL_B
  1195. #endif
  1196. #define DL_MIN_LEN M2_MIN_LEN
  1197. #ifndef __LZO_DICT_H
  1198. #define __LZO_DICT_H
  1199. #ifdef __cplusplus
  1200. extern "C" {
  1201. #endif
  1202. #if !defined(D_BITS) && defined(DBITS)
  1203. # define D_BITS DBITS
  1204. #endif
  1205. #if !defined(D_BITS)
  1206. # error "D_BITS is not defined"
  1207. #endif
  1208. #if (D_BITS < 16)
  1209. # define D_SIZE LZO_SIZE(D_BITS)
  1210. # define D_MASK LZO_MASK(D_BITS)
  1211. #else
  1212. # define D_SIZE LZO_USIZE(D_BITS)
  1213. # define D_MASK LZO_UMASK(D_BITS)
  1214. #endif
  1215. #define D_HIGH ((D_MASK >> 1) + 1)
  1216. #if !defined(DD_BITS)
  1217. # define DD_BITS 0
  1218. #endif
  1219. #define DD_SIZE LZO_SIZE(DD_BITS)
  1220. #define DD_MASK LZO_MASK(DD_BITS)
  1221. #if !defined(DL_BITS)
  1222. # define DL_BITS (D_BITS - DD_BITS)
  1223. #endif
  1224. #if (DL_BITS < 16)
  1225. # define DL_SIZE LZO_SIZE(DL_BITS)
  1226. # define DL_MASK LZO_MASK(DL_BITS)
  1227. #else
  1228. # define DL_SIZE LZO_USIZE(DL_BITS)
  1229. # define DL_MASK LZO_UMASK(DL_BITS)
  1230. #endif
  1231. #if (D_BITS != DL_BITS + DD_BITS)
  1232. # error "D_BITS does not match"
  1233. #endif
  1234. #if (D_BITS < 8 || D_BITS > 18)
  1235. # error "invalid D_BITS"
  1236. #endif
  1237. #if (DL_BITS < 8 || DL_BITS > 20)
  1238. # error "invalid DL_BITS"
  1239. #endif
  1240. #if (DD_BITS < 0 || DD_BITS > 6)
  1241. # error "invalid DD_BITS"
  1242. #endif
  1243. #if !defined(DL_MIN_LEN)
  1244. # define DL_MIN_LEN 3
  1245. #endif
  1246. #if !defined(DL_SHIFT)
  1247. # define DL_SHIFT ((DL_BITS + (DL_MIN_LEN - 1)) / DL_MIN_LEN)
  1248. #endif
  1249. #define LZO_HASH_GZIP 1
  1250. #define LZO_HASH_GZIP_INCREMENTAL 2
  1251. #define LZO_HASH_LZO_INCREMENTAL_A 3
  1252. #define LZO_HASH_LZO_INCREMENTAL_B 4
  1253. #if !defined(LZO_HASH)
  1254. # error "choose a hashing strategy"
  1255. #endif
  1256. #if (DL_MIN_LEN == 3)
  1257. # define _DV2_A(p,shift1,shift2) \
  1258. (((( (lzo_uint32)((p)[0]) << shift1) ^ (p)[1]) << shift2) ^ (p)[2])
  1259. # define _DV2_B(p,shift1,shift2) \
  1260. (((( (lzo_uint32)((p)[2]) << shift1) ^ (p)[1]) << shift2) ^ (p)[0])
  1261. # define _DV3_B(p,shift1,shift2,shift3) \
  1262. ((_DV2_B((p)+1,shift1,shift2) << (shift3)) ^ (p)[0])
  1263. #elif (DL_MIN_LEN == 2)
  1264. # define _DV2_A(p,shift1,shift2) \
  1265. (( (lzo_uint32)(p[0]) << shift1) ^ p[1])
  1266. # define _DV2_B(p,shift1,shift2) \
  1267. (( (lzo_uint32)(p[1]) << shift1) ^ p[2])
  1268. #else
  1269. # error "invalid DL_MIN_LEN"
  1270. #endif
  1271. #define _DV_A(p,shift) _DV2_A(p,shift,shift)
  1272. #define _DV_B(p,shift) _DV2_B(p,shift,shift)
  1273. #define DA2(p,s1,s2) \
  1274. (((((lzo_uint32)((p)[2]) << (s2)) + (p)[1]) << (s1)) + (p)[0])
  1275. #define DS2(p,s1,s2) \
  1276. (((((lzo_uint32)((p)[2]) << (s2)) - (p)[1]) << (s1)) - (p)[0])
  1277. #define DX2(p,s1,s2) \
  1278. (((((lzo_uint32)((p)[2]) << (s2)) ^ (p)[1]) << (s1)) ^ (p)[0])
  1279. #define DA3(p,s1,s2,s3) ((DA2((p)+1,s2,s3) << (s1)) + (p)[0])
  1280. #define DS3(p,s1,s2,s3) ((DS2((p)+1,s2,s3) << (s1)) - (p)[0])
  1281. #define DX3(p,s1,s2,s3) ((DX2((p)+1,s2,s3) << (s1)) ^ (p)[0])
  1282. #define DMS(v,s) ((lzo_uint) (((v) & (D_MASK >> (s))) << (s)))
  1283. #define DM(v) DMS(v,0)
  1284. #if (LZO_HASH == LZO_HASH_GZIP)
  1285. # define _DINDEX(dv,p) (_DV_A((p),DL_SHIFT))
  1286. #elif (LZO_HASH == LZO_HASH_GZIP_INCREMENTAL)
  1287. # define __LZO_HASH_INCREMENTAL
  1288. # define DVAL_FIRST(dv,p) dv = _DV_A((p),DL_SHIFT)
  1289. # define DVAL_NEXT(dv,p) dv = (((dv) << DL_SHIFT) ^ p[2])
  1290. # define _DINDEX(dv,p) (dv)
  1291. # define DVAL_LOOKAHEAD DL_MIN_LEN
  1292. #elif (LZO_HASH == LZO_HASH_LZO_INCREMENTAL_A)
  1293. # define __LZO_HASH_INCREMENTAL
  1294. # define DVAL_FIRST(dv,p) dv = _DV_A((p),5)
  1295. # define DVAL_NEXT(dv,p) \
  1296. dv ^= (lzo_uint32)(p[-1]) << (2*5); dv = (((dv) << 5) ^ p[2])
  1297. # define _DINDEX(dv,p) ((0x9f5f * (dv)) >> 5)
  1298. # define DVAL_LOOKAHEAD DL_MIN_LEN
  1299. #elif (LZO_HASH == LZO_HASH_LZO_INCREMENTAL_B)
  1300. # define __LZO_HASH_INCREMENTAL
  1301. # define DVAL_FIRST(dv,p) dv = _DV_B((p),5)
  1302. # define DVAL_NEXT(dv,p) \
  1303. dv ^= p[-1]; dv = (((dv) >> 5) ^ ((lzo_uint32)(p[2]) << (2*5)))
  1304. # define _DINDEX(dv,p) ((0x9f5f * (dv)) >> 5)
  1305. # define DVAL_LOOKAHEAD DL_MIN_LEN
  1306. #else
  1307. # error "choose a hashing strategy"
  1308. #endif
  1309. #ifndef DINDEX
  1310. #define DINDEX(dv,p) ((lzo_uint)((_DINDEX(dv,p)) & DL_MASK) << DD_BITS)
  1311. #endif
  1312. #if !defined(DINDEX1) && defined(D_INDEX1)
  1313. #define DINDEX1 D_INDEX1
  1314. #endif
  1315. #if !defined(DINDEX2) && defined(D_INDEX2)
  1316. #define DINDEX2 D_INDEX2
  1317. #endif
  1318. #if !defined(__LZO_HASH_INCREMENTAL)
  1319. # define DVAL_FIRST(dv,p) ((void) 0)
  1320. # define DVAL_NEXT(dv,p) ((void) 0)
  1321. # define DVAL_LOOKAHEAD 0
  1322. #endif
  1323. #if !defined(DVAL_ASSERT)
  1324. #if defined(__LZO_HASH_INCREMENTAL) && !defined(NDEBUG)
  1325. static void DVAL_ASSERT(lzo_uint32 dv, const lzo_byte *p)
  1326. {
  1327. lzo_uint32 df;
  1328. DVAL_FIRST(df,(p));
  1329. assert(DINDEX(dv,p) == DINDEX(df,p));
  1330. }
  1331. #else
  1332. # define DVAL_ASSERT(dv,p) ((void) 0)
  1333. #endif
  1334. #endif
  1335. #if defined(LZO_DICT_USE_PTR)
  1336. # define DENTRY(p,in) (p)
  1337. # define GINDEX(m_pos,m_off,dict,dindex,in) m_pos = dict[dindex]
  1338. #else
  1339. # define DENTRY(p,in) ((lzo_uint) ((p)-(in)))
  1340. # define GINDEX(m_pos,m_off,dict,dindex,in) m_off = dict[dindex]
  1341. #endif
  1342. #if (DD_BITS == 0)
  1343. # define UPDATE_D(dict,drun,dv,p,in) dict[ DINDEX(dv,p) ] = DENTRY(p,in)
  1344. # define UPDATE_I(dict,drun,index,p,in) dict[index] = DENTRY(p,in)
  1345. # define UPDATE_P(ptr,drun,p,in) (ptr)[0] = DENTRY(p,in)
  1346. #else
  1347. # define UPDATE_D(dict,drun,dv,p,in) \
  1348. dict[ DINDEX(dv,p) + drun++ ] = DENTRY(p,in); drun &= DD_MASK
  1349. # define UPDATE_I(dict,drun,index,p,in) \
  1350. dict[ (index) + drun++ ] = DENTRY(p,in); drun &= DD_MASK
  1351. # define UPDATE_P(ptr,drun,p,in) \
  1352. (ptr) [ drun++ ] = DENTRY(p,in); drun &= DD_MASK
  1353. #endif
  1354. #if defined(LZO_DICT_USE_PTR)
  1355. #define LZO_CHECK_MPOS_DET(m_pos,m_off,in,ip,max_offset) \
  1356. (m_pos == NULL || (m_off = (lzo_moff_t) (ip - m_pos)) > max_offset)
  1357. #define LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
  1358. (BOUNDS_CHECKING_OFF_IN_EXPR( \
  1359. (PTR_LT(m_pos,in) || \
  1360. (m_off = (lzo_moff_t) PTR_DIFF(ip,m_pos)) <= 0 || \
  1361. m_off > max_offset) ))
  1362. #else
  1363. #define LZO_CHECK_MPOS_DET(m_pos,m_off,in,ip,max_offset) \
  1364. (m_off == 0 || \
  1365. ((m_off = (lzo_moff_t) ((ip)-(in)) - m_off) > max_offset) || \
  1366. (m_pos = (ip) - (m_off), 0) )
  1367. #define LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,max_offset) \
  1368. ((lzo_moff_t) ((ip)-(in)) <= m_off || \
  1369. ((m_off = (lzo_moff_t) ((ip)-(in)) - m_off) > max_offset) || \
  1370. (m_pos = (ip) - (m_off), 0) )
  1371. #endif
  1372. #if defined(LZO_DETERMINISTIC)
  1373. # define LZO_CHECK_MPOS LZO_CHECK_MPOS_DET
  1374. #else
  1375. # define LZO_CHECK_MPOS LZO_CHECK_MPOS_NON_DET
  1376. #endif
  1377. #ifdef __cplusplus
  1378. }
  1379. #endif
  1380. #endif
  1381. #endif
  1382. #endif
  1383. #define DO_COMPRESS lzo1x_1_compress
  1384. static
  1385. lzo_uint do_compress ( const lzo_byte *in , lzo_uint in_len,
  1386. lzo_byte *out, lzo_uintp out_len,
  1387. lzo_voidp wrkmem )
  1388. {
  1389. #if 0 && defined(__GNUC__) && defined(__i386__)
  1390. register const lzo_byte *ip __asm__("%esi");
  1391. #else
  1392. register const lzo_byte *ip;
  1393. #endif
  1394. lzo_byte *op;
  1395. const lzo_byte * const in_end = in + in_len;
  1396. const lzo_byte * const ip_end = in + in_len - M2_MAX_LEN - 5;
  1397. const lzo_byte *ii;
  1398. lzo_dict_p const dict = (lzo_dict_p) wrkmem;
  1399. op = out;
  1400. ip = in;
  1401. ii = ip;
  1402. ip += 4;
  1403. for (;;)
  1404. {
  1405. #if 0 && defined(__GNUC__) && defined(__i386__)
  1406. register const lzo_byte *m_pos __asm__("%edi");
  1407. #else
  1408. register const lzo_byte *m_pos;
  1409. #endif
  1410. lzo_moff_t m_off;
  1411. lzo_uint m_len;
  1412. lzo_uint dindex;
  1413. DINDEX1(dindex,ip);
  1414. GINDEX(m_pos,m_off,dict,dindex,in);
  1415. if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
  1416. goto literal;
  1417. #if 1
  1418. if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
  1419. goto try_match;
  1420. DINDEX2(dindex,ip);
  1421. #endif
  1422. GINDEX(m_pos,m_off,dict,dindex,in);
  1423. if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
  1424. goto literal;
  1425. if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
  1426. goto try_match;
  1427. goto literal;
  1428. try_match:
  1429. #if 1 && defined(LZO_UNALIGNED_OK_2)
  1430. if (* (const lzo_ushortp) m_pos != * (const lzo_ushortp) ip)
  1431. #else
  1432. if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
  1433. #endif
  1434. {
  1435. }
  1436. else
  1437. {
  1438. if (m_pos[2] == ip[2])
  1439. {
  1440. #if 0
  1441. if (m_off <= M2_MAX_OFFSET)
  1442. goto match;
  1443. if (lit <= 3)
  1444. goto match;
  1445. if (lit == 3)
  1446. {
  1447. assert(op - 2 > out); op[-2] |= LZO_BYTE(3);
  1448. *op++ = *ii++; *op++ = *ii++; *op++ = *ii++;
  1449. goto code_match;
  1450. }
  1451. if (m_pos[3] == ip[3])
  1452. #endif
  1453. goto match;
  1454. }
  1455. else
  1456. {
  1457. #if 0
  1458. #if 0
  1459. if (m_off <= M1_MAX_OFFSET && lit > 0 && lit <= 3)
  1460. #else
  1461. if (m_off <= M1_MAX_OFFSET && lit == 3)
  1462. #endif
  1463. {
  1464. register lzo_uint t;
  1465. t = lit;
  1466. assert(op - 2 > out); op[-2] |= LZO_BYTE(t);
  1467. do *op++ = *ii++; while (--t > 0);
  1468. assert(ii == ip);
  1469. m_off -= 1;
  1470. *op++ = LZO_BYTE(M1_MARKER | ((m_off & 3) << 2));
  1471. *op++ = LZO_BYTE(m_off >> 2);
  1472. ip += 2;
  1473. goto match_done;
  1474. }
  1475. #endif
  1476. }
  1477. }
  1478. literal:
  1479. UPDATE_I(dict,0,dindex,ip,in);
  1480. ++ip;
  1481. if (ip >= ip_end)
  1482. break;
  1483. continue;
  1484. match:
  1485. UPDATE_I(dict,0,dindex,ip,in);
  1486. if (pd(ip,ii) > 0)
  1487. {
  1488. register lzo_uint t = pd(ip,ii);
  1489. if (t <= 3)
  1490. {
  1491. assert(op - 2 > out);
  1492. op[-2] |= LZO_BYTE(t);
  1493. }
  1494. else if (t <= 18)
  1495. *op++ = LZO_BYTE(t - 3);
  1496. else
  1497. {
  1498. register lzo_uint tt = t - 18;
  1499. *op++ = 0;
  1500. while (tt > 255)
  1501. {
  1502. tt -= 255;
  1503. *op++ = 0;
  1504. }
  1505. assert(tt > 0);
  1506. *op++ = LZO_BYTE(tt);
  1507. }
  1508. do *op++ = *ii++; while (--t > 0);
  1509. }
  1510. assert(ii == ip);
  1511. ip += 3;
  1512. if (m_pos[3] != *ip++ || m_pos[4] != *ip++ || m_pos[5] != *ip++ ||
  1513. m_pos[6] != *ip++ || m_pos[7] != *ip++ || m_pos[8] != *ip++
  1514. #ifdef LZO1Y
  1515. || m_pos[ 9] != *ip++ || m_pos[10] != *ip++ || m_pos[11] != *ip++
  1516. || m_pos[12] != *ip++ || m_pos[13] != *ip++ || m_pos[14] != *ip++
  1517. #endif
  1518. )
  1519. {
  1520. --ip;
  1521. m_len = ip - ii;
  1522. assert(m_len >= 3); assert(m_len <= M2_MAX_LEN);
  1523. if (m_off <= M2_MAX_OFFSET)
  1524. {
  1525. m_off -= 1;
  1526. #if defined(LZO1X)
  1527. *op++ = LZO_BYTE(((m_len - 1) << 5) | ((m_off & 7) << 2));
  1528. *op++ = LZO_BYTE(m_off >> 3);
  1529. #elif defined(LZO1Y)
  1530. *op++ = LZO_BYTE(((m_len + 1) << 4) | ((m_off & 3) << 2));
  1531. *op++ = LZO_BYTE(m_off >> 2);
  1532. #endif
  1533. }
  1534. else if (m_off <= M3_MAX_OFFSET)
  1535. {
  1536. m_off -= 1;
  1537. *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
  1538. goto m3_m4_offset;
  1539. }
  1540. else
  1541. #if defined(LZO1X)
  1542. {
  1543. m_off -= 0x4000;
  1544. assert(m_off > 0); assert(m_off <= 0x7fff);
  1545. *op++ = LZO_BYTE(M4_MARKER |
  1546. ((m_off & 0x4000) >> 11) | (m_len - 2));
  1547. goto m3_m4_offset;
  1548. }
  1549. #elif defined(LZO1Y)
  1550. goto m4_match;
  1551. #endif
  1552. }
  1553. else
  1554. {
  1555. {
  1556. const lzo_byte *end = in_end;
  1557. const lzo_byte *m = m_pos + M2_MAX_LEN + 1;
  1558. while (ip < end && *m == *ip)
  1559. m++, ip++;
  1560. m_len = (ip - ii);
  1561. }
  1562. assert(m_len > M2_MAX_LEN);
  1563. if (m_off <= M3_MAX_OFFSET)
  1564. {
  1565. m_off -= 1;
  1566. if (m_len <= 33)
  1567. *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
  1568. else
  1569. {
  1570. m_len -= 33;
  1571. *op++ = M3_MARKER | 0;
  1572. goto m3_m4_len;
  1573. }
  1574. }
  1575. else
  1576. {
  1577. #if defined(LZO1Y)
  1578. m4_match:
  1579. #endif
  1580. m_off -= 0x4000;
  1581. assert(m_off > 0); assert(m_off <= 0x7fff);
  1582. if (m_len <= M4_MAX_LEN)
  1583. *op++ = LZO_BYTE(M4_MARKER |
  1584. ((m_off & 0x4000) >> 11) | (m_len - 2));
  1585. else
  1586. {
  1587. m_len -= M4_MAX_LEN;
  1588. *op++ = LZO_BYTE(M4_MARKER | ((m_off & 0x4000) >> 11));
  1589. m3_m4_len:
  1590. while (m_len > 255)
  1591. {
  1592. m_len -= 255;
  1593. *op++ = 0;
  1594. }
  1595. assert(m_len > 0);
  1596. *op++ = LZO_BYTE(m_len);
  1597. }
  1598. }
  1599. m3_m4_offset:
  1600. *op++ = LZO_BYTE((m_off & 63) << 2);
  1601. *op++ = LZO_BYTE(m_off >> 6);
  1602. }
  1603. #if 0
  1604. match_done:
  1605. #endif
  1606. ii = ip;
  1607. if (ip >= ip_end)
  1608. break;
  1609. }
  1610. *out_len = op - out;
  1611. return pd(in_end,ii);
  1612. }
  1613. LZO_PUBLIC(int)
  1614. DO_COMPRESS ( const lzo_byte *in , lzo_uint in_len,
  1615. lzo_byte *out, lzo_uintp out_len,
  1616. lzo_voidp wrkmem )
  1617. {
  1618. lzo_byte *op = out;
  1619. lzo_uint t;
  1620. #if defined(__LZO_QUERY_COMPRESS)
  1621. if (__LZO_IS_COMPRESS_QUERY(in,in_len,out,out_len,wrkmem))
  1622. return __LZO_QUERY_COMPRESS(in,in_len,out,out_len,wrkmem,D_SIZE,lzo_sizeof(lzo_dict_t));
  1623. #endif
  1624. if (in_len <= M2_MAX_LEN + 5)
  1625. t = in_len;
  1626. else
  1627. {
  1628. t = do_compress(in,in_len,op,out_len,wrkmem);
  1629. op += *out_len;
  1630. }
  1631. if (t > 0)
  1632. {
  1633. const lzo_byte *ii = in + in_len - t;
  1634. if (op == out && t <= 238)
  1635. *op++ = LZO_BYTE(17 + t);
  1636. else if (t <= 3)
  1637. op[-2] |= LZO_BYTE(t);
  1638. else if (t <= 18)
  1639. *op++ = LZO_BYTE(t - 3);
  1640. else
  1641. {
  1642. lzo_uint tt = t - 18;
  1643. *op++ = 0;
  1644. while (tt > 255)
  1645. {
  1646. tt -= 255;
  1647. *op++ = 0;
  1648. }
  1649. assert(tt > 0);
  1650. *op++ = LZO_BYTE(tt);
  1651. }
  1652. do *op++ = *ii++; while (--t > 0);
  1653. }
  1654. *op++ = M4_MARKER | 1;
  1655. *op++ = 0;
  1656. *op++ = 0;
  1657. *out_len = op - out;
  1658. return LZO_E_OK;
  1659. }
  1660. #undef do_compress
  1661. #undef DO_COMPRESS
  1662. #undef LZO_HASH
  1663. #undef LZO_TEST_DECOMPRESS_OVERRUN
  1664. #undef LZO_TEST_DECOMPRESS_OVERRUN_INPUT
  1665. #undef LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT
  1666. #undef LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND
  1667. #undef DO_DECOMPRESS
  1668. #define DO_DECOMPRESS lzo1x_decompress
  1669. #if defined(LZO_TEST_DECOMPRESS_OVERRUN)
  1670. # if !defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
  1671. # define LZO_TEST_DECOMPRESS_OVERRUN_INPUT 2
  1672. # endif
  1673. # if !defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
  1674. # define LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT 2
  1675. # endif
  1676. # if !defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
  1677. # define LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND
  1678. # endif
  1679. #endif
  1680. #undef TEST_IP
  1681. #undef TEST_OP
  1682. #undef TEST_LOOKBEHIND
  1683. #undef NEED_IP
  1684. #undef NEED_OP
  1685. #undef HAVE_TEST_IP
  1686. #undef HAVE_TEST_OP
  1687. #undef HAVE_NEED_IP
  1688. #undef HAVE_NEED_OP
  1689. #undef HAVE_ANY_IP
  1690. #undef HAVE_ANY_OP
  1691. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
  1692. # if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 1)
  1693. # define TEST_IP (ip < ip_end)
  1694. # endif
  1695. # if (LZO_TEST_DECOMPRESS_OVERRUN_INPUT >= 2)
  1696. # define NEED_IP(x) \
  1697. if ((lzo_uint)(ip_end - ip) < (lzo_uint)(x)) goto input_overrun
  1698. # endif
  1699. #endif
  1700. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
  1701. # if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 1)
  1702. # define TEST_OP (op <= op_end)
  1703. # endif
  1704. # if (LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT >= 2)
  1705. # undef TEST_OP
  1706. # define NEED_OP(x) \
  1707. if ((lzo_uint)(op_end - op) < (lzo_uint)(x)) goto output_overrun
  1708. # endif
  1709. #endif
  1710. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
  1711. # define TEST_LOOKBEHIND(m_pos,out) if (m_pos < out) goto lookbehind_overrun
  1712. #else
  1713. # define TEST_LOOKBEHIND(m_pos,op) ((void) 0)
  1714. #endif
  1715. #if !defined(LZO_EOF_CODE) && !defined(TEST_IP)
  1716. # define TEST_IP (ip < ip_end)
  1717. #endif
  1718. #if defined(TEST_IP)
  1719. # define HAVE_TEST_IP
  1720. #else
  1721. # define TEST_IP 1