zutil.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* zutil.h -- internal interface and configuration of the compression library
  2. * Copyright (C) 1995-1998 Jean-loup Gailly.
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /* WARNING: this file should *not* be used by applications. It is
  6. part of the implementation of the compression library and is
  7. subject to change. Applications should only use zlib.h.
  8. */
  9. /* @(#) $Id: zutil.h,v 1.1 2000/01/01 03:32:23 davem Exp $ */
  10. #ifndef _Z_UTIL_H
  11. #define _Z_UTIL_H
  12. #include <linux/zlib.h>
  13. #include <linux/string.h>
  14. #include <linux/kernel.h>
  15. typedef unsigned char uch;
  16. typedef unsigned short ush;
  17. typedef unsigned long ulg;
  18. /* common constants */
  19. #define STORED_BLOCK 0
  20. #define STATIC_TREES 1
  21. #define DYN_TREES 2
  22. /* The three kinds of block type */
  23. #define MIN_MATCH 3
  24. #define MAX_MATCH 258
  25. /* The minimum and maximum match lengths */
  26. #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
  27. /* target dependencies */
  28. /* Common defaults */
  29. #ifndef OS_CODE
  30. # define OS_CODE 0x03 /* assume Unix */
  31. #endif
  32. /* functions */
  33. typedef uLong (*check_func) (uLong check, const Byte *buf,
  34. uInt len);
  35. /* checksum functions */
  36. #define BASE 65521L /* largest prime smaller than 65536 */
  37. #define NMAX 5552
  38. /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  39. #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
  40. #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
  41. #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
  42. #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
  43. #define DO16(buf) DO8(buf,0); DO8(buf,8);
  44. /* ========================================================================= */
  45. /*
  46. Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  47. return the updated checksum. If buf is NULL, this function returns
  48. the required initial value for the checksum.
  49. An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  50. much faster. Usage example:
  51. uLong adler = adler32(0L, NULL, 0);
  52. while (read_buffer(buffer, length) != EOF) {
  53. adler = adler32(adler, buffer, length);
  54. }
  55. if (adler != original_adler) error();
  56. */
  57. static inline uLong zlib_adler32(uLong adler,
  58. const Byte *buf,
  59. uInt len)
  60. {
  61. unsigned long s1 = adler & 0xffff;
  62. unsigned long s2 = (adler >> 16) & 0xffff;
  63. int k;
  64. if (buf == NULL) return 1L;
  65. while (len > 0) {
  66. k = len < NMAX ? len : NMAX;
  67. len -= k;
  68. while (k >= 16) {
  69. DO16(buf);
  70. buf += 16;
  71. k -= 16;
  72. }
  73. if (k != 0) do {
  74. s1 += *buf++;
  75. s2 += s1;
  76. } while (--k);
  77. s1 %= BASE;
  78. s2 %= BASE;
  79. }
  80. return (s2 << 16) | s1;
  81. }
  82. #endif /* _Z_UTIL_H */