ucl_util.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* ucl_util.c -- utilities for the UCL library
  2. This file is part of the UCL data compression library.
  3. Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
  4. All Rights Reserved.
  5. The UCL library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. The UCL library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with the UCL library; see the file COPYING.
  15. If not, write to the Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. Markus F.X.J. Oberhumer
  18. <markus@oberhumer.com>
  19. http://www.oberhumer.com/opensource/ucl/
  20. */
  21. #include "ucl_conf.h"
  22. #include "ucl_util.h"
  23. /***********************************************************************
  24. //
  25. ************************************************************************/
  26. UCL_PUBLIC(ucl_bool)
  27. ucl_assert(int expr)
  28. {
  29. return (expr) ? 1 : 0;
  30. }
  31. /***********************************************************************
  32. //
  33. ************************************************************************/
  34. /* If you use the UCL library in a product, you *must* keep this
  35. * copyright string in the executable of your product.
  36. .*/
  37. const ucl_byte __ucl_copyright[] =
  38. "\n\n\n"
  39. "UCL real-time data compression library.\n"
  40. "$Copyright: UCL (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 Markus Franz Xaver Johannes Oberhumer $\n"
  41. "<markus@oberhumer.com>\n"
  42. "http://www.oberhumer.com\n"
  43. "\n"
  44. "UCL version: v" UCL_VERSION_STRING ", " UCL_VERSION_DATE "\n"
  45. "UCL build date: " __DATE__ " " __TIME__ "\n\n"
  46. "UCL special compilation options:\n"
  47. #ifdef __cplusplus
  48. " __cplusplus\n"
  49. #endif
  50. #if defined(__PIC__)
  51. " __PIC__\n"
  52. #elif defined(__pic__)
  53. " __pic__\n"
  54. #endif
  55. #if (UINT_MAX < UCL_0xffffffffL)
  56. " 16BIT\n"
  57. #endif
  58. #if defined(__UCL_STRICT_16BIT)
  59. " __UCL_STRICT_16BIT\n"
  60. #endif
  61. #if (UINT_MAX > UCL_0xffffffffL)
  62. " UINT_MAX=" _UCL_MEXPAND(UINT_MAX) "\n"
  63. #endif
  64. #if (ULONG_MAX > UCL_0xffffffffL)
  65. " ULONG_MAX=" _UCL_MEXPAND(ULONG_MAX) "\n"
  66. #endif
  67. #if defined(UCL_BYTE_ORDER)
  68. " UCL_BYTE_ORDER=" _UCL_MEXPAND(UCL_BYTE_ORDER) "\n"
  69. #endif
  70. #if defined(UCL_UNALIGNED_OK_2)
  71. " UCL_UNALIGNED_OK_2\n"
  72. #endif
  73. #if defined(UCL_UNALIGNED_OK_4)
  74. " UCL_UNALIGNED_OK_4\n"
  75. #endif
  76. #if defined(UCL_ALIGNED_OK_4)
  77. " UCL_ALIGNED_OK_4\n"
  78. #endif
  79. #if defined(__UCL_IN_MINIUCL)
  80. " __UCL_IN_MINIUCL\n"
  81. #endif
  82. "\n\n"
  83. /* RCS information */
  84. "$Id: UCL " UCL_VERSION_STRING " built " __DATE__ " " __TIME__
  85. #if defined(__GNUC__) && defined(__VERSION__)
  86. " by gcc " __VERSION__
  87. #elif defined(__BORLANDC__)
  88. " by Borland C " _UCL_MEXPAND(__BORLANDC__)
  89. #elif defined(_MSC_VER)
  90. " by Microsoft C " _UCL_MEXPAND(_MSC_VER)
  91. #elif defined(__PUREC__)
  92. " by Pure C " _UCL_MEXPAND(__PUREC__)
  93. #elif defined(__SC__)
  94. " by Symantec C " _UCL_MEXPAND(__SC__)
  95. #elif defined(__TURBOC__)
  96. " by Turbo C " _UCL_MEXPAND(__TURBOC__)
  97. #elif defined(__WATCOMC__)
  98. " by Watcom C " _UCL_MEXPAND(__WATCOMC__)
  99. #endif
  100. " $\n";
  101. UCL_PUBLIC(const ucl_byte *)
  102. ucl_copyright(void)
  103. {
  104. return __ucl_copyright;
  105. }
  106. UCL_PUBLIC(ucl_uint32)
  107. ucl_version(void)
  108. {
  109. return UCL_VERSION;
  110. }
  111. UCL_PUBLIC(const char *)
  112. ucl_version_string(void)
  113. {
  114. return UCL_VERSION_STRING;
  115. }
  116. UCL_PUBLIC(const char *)
  117. ucl_version_date(void)
  118. {
  119. return UCL_VERSION_DATE;
  120. }
  121. UCL_PUBLIC(const ucl_charp)
  122. _ucl_version_string(void)
  123. {
  124. return UCL_VERSION_STRING;
  125. }
  126. UCL_PUBLIC(const ucl_charp)
  127. _ucl_version_date(void)
  128. {
  129. return UCL_VERSION_DATE;
  130. }
  131. /***********************************************************************
  132. // adler32 checksum
  133. // adapted from free code by Mark Adler <madler@alumni.caltech.edu>
  134. // see http://www.cdrom.com/pub/infozip/zlib/
  135. ************************************************************************/
  136. #define UCL_BASE 65521u /* largest prime smaller than 65536 */
  137. #define UCL_NMAX 5552
  138. /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  139. #define UCL_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
  140. #define UCL_DO2(buf,i) UCL_DO1(buf,i); UCL_DO1(buf,i+1);
  141. #define UCL_DO4(buf,i) UCL_DO2(buf,i); UCL_DO2(buf,i+2);
  142. #define UCL_DO8(buf,i) UCL_DO4(buf,i); UCL_DO4(buf,i+4);
  143. #define UCL_DO16(buf,i) UCL_DO8(buf,i); UCL_DO8(buf,i+8);
  144. UCL_PUBLIC(ucl_uint32)
  145. ucl_adler32(ucl_uint32 adler, const ucl_byte *buf, ucl_uint len)
  146. {
  147. ucl_uint32 s1 = adler & 0xffff;
  148. ucl_uint32 s2 = (adler >> 16) & 0xffff;
  149. int k;
  150. if (buf == NULL)
  151. return 1;
  152. while (len > 0)
  153. {
  154. k = len < UCL_NMAX ? (int) len : UCL_NMAX;
  155. len -= k;
  156. if (k >= 16) do
  157. {
  158. UCL_DO16(buf,0);
  159. buf += 16;
  160. k -= 16;
  161. } while (k >= 16);
  162. if (k != 0) do
  163. {
  164. s1 += *buf++;
  165. s2 += s1;
  166. } while (--k > 0);
  167. s1 %= UCL_BASE;
  168. s2 %= UCL_BASE;
  169. }
  170. return (s2 << 16) | s1;
  171. }
  172. /*
  173. vi:ts=4:et
  174. */