zutil.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/errno.h>
  15. #include <linux/kernel.h>
  16. typedef unsigned char uch;
  17. typedef unsigned short ush;
  18. typedef unsigned long ulg;
  19. /* common constants */
  20. #ifndef DEF_WBITS
  21. # define DEF_WBITS MAX_WBITS
  22. #endif
  23. /* default windowBits for decompression. MAX_WBITS is for compression only */
  24. #if MAX_MEM_LEVEL >= 8
  25. # define DEF_MEM_LEVEL 8
  26. #else
  27. # define DEF_MEM_LEVEL MAX_MEM_LEVEL
  28. #endif
  29. /* default memLevel */
  30. #define STORED_BLOCK 0
  31. #define STATIC_TREES 1
  32. #define DYN_TREES 2
  33. /* The three kinds of block type */
  34. #define MIN_MATCH 3
  35. #define MAX_MATCH 258
  36. /* The minimum and maximum match lengths */
  37. #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
  38. /* target dependencies */
  39. /* Common defaults */
  40. #ifndef OS_CODE
  41. # define OS_CODE 0x03 /* assume Unix */
  42. #endif
  43. /* functions */
  44. typedef uLong (*check_func) (uLong check, const Byte *buf,
  45. uInt len);
  46. /* checksum functions */
  47. #define BASE 65521L /* largest prime smaller than 65536 */
  48. #define NMAX 5552
  49. /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  50. #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
  51. #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
  52. #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
  53. #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
  54. #define DO16(buf) DO8(buf,0); DO8(buf,8);
  55. /* ========================================================================= */
  56. /*
  57. Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  58. return the updated checksum. If buf is NULL, this function returns
  59. the required initial value for the checksum.
  60. An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  61. much faster. Usage example:
  62. uLong adler = adler32(0L, NULL, 0);
  63. while (read_buffer(buffer, length) != EOF) {
  64. adler = adler32(adler, buffer, length);
  65. }
  66. if (adler != original_adler) error();
  67. */
  68. static inline uLong zlib_adler32(uLong adler,
  69. const Byte *buf,
  70. uInt len)
  71. {
  72. unsigned long s1 = adler & 0xffff;
  73. unsigned long s2 = (adler >> 16) & 0xffff;
  74. int k;
  75. if (buf == NULL) return 1L;
  76. while (len > 0) {
  77. k = len < NMAX ? len : NMAX;
  78. len -= k;
  79. while (k >= 16) {
  80. DO16(buf);
  81. buf += 16;
  82. k -= 16;
  83. }
  84. if (k != 0) do {
  85. s1 += *buf++;
  86. s2 += s1;
  87. } while (--k);
  88. s1 %= BASE;
  89. s2 %= BASE;
  90. }
  91. return (s2 << 16) | s1;
  92. }
  93. #endif /* _Z_UTIL_H */