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