Skip to content

Commit bfa67d1

Browse files
committed
zlib: add cloudflare/zlib
1 parent f7c6cb4 commit bfa67d1

36 files changed

+14889
-3
lines changed

configure.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,18 @@
376376
dest='shared_openssl_libpath',
377377
help='a directory to search for the shared OpenSSL DLLs')
378378

379+
shared_optgroup.add_argument('--cloudflare-zlib',
380+
action='store_true',
381+
dest='use_cl_zlib',
382+
default=None,
383+
help='use cloudflare/zlib')
384+
385+
shared_optgroup.add_argument('--no-cloudflare-zlib',
386+
action='store_false',
387+
dest='use_cl_zlib',
388+
default=None,
389+
help='do not use use cloudflare/zlib')
390+
379391
shared_optgroup.add_argument('--shared-zlib',
380392
action='store_true',
381393
dest='shared_zlib',
@@ -1522,6 +1534,13 @@ def configure_library(lib, output, pkgname=None):
15221534
elif pkg_libs:
15231535
output['libraries'] += pkg_libs.split()
15241536

1537+
def configure_zlib(o):
1538+
configure_library('zlib', o)
1539+
if (options.use_cl_zlib is None and o['variables']['target_arch'] == 'arm64'
1540+
or options.use_cl_zlib):
1541+
o['variables']['zlib_root'] = 'deps/cl-zlib'
1542+
else:
1543+
o['variables']['zlib_root'] = 'deps/zlib'
15251544

15261545
def configure_v8(o):
15271546
o['variables']['v8_enable_webassembly'] = 0 if options.v8_lite_mode else 1
@@ -2067,7 +2086,7 @@ def make_bin_override():
20672086
configure_node(output)
20682087
configure_node_lib_files(output)
20692088
configure_napi(output)
2070-
configure_library('zlib', output)
2089+
configure_zlib(output)
20712090
configure_library('http_parser', output)
20722091
configure_library('libuv', output)
20732092
configure_library('brotli', output, pkgname=['libbrotlidec', 'libbrotlienc'])

deps/cl-zlib/adler32.c

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/* adler32.c -- compute the Adler-32 checksum of a data stream
2+
* Copyright (C) 1995-2011 Mark Adler
3+
* For conditions of distribution and use, see copyright notice in zlib.h
4+
*/
5+
6+
/* @(#) $Id$ */
7+
8+
#include "zutil.h"
9+
10+
#define local static
11+
12+
local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
13+
14+
#define BASE 65521 /* largest prime smaller than 65536 */
15+
#define NMAX 5552
16+
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
17+
18+
#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
19+
#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
20+
#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
21+
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
22+
#define DO16(buf) DO8(buf,0); DO8(buf,8);
23+
24+
/* use NO_DIVIDE if your processor does not do division in hardware --
25+
try it both ways to see which is faster */
26+
#ifdef NO_DIVIDE
27+
/* note that this assumes BASE is 65521, where 65536 % 65521 == 15
28+
(thank you to John Reiser for pointing this out) */
29+
# define CHOP(a) \
30+
do { \
31+
unsigned long tmp = a >> 16; \
32+
a &= 0xffffUL; \
33+
a += (tmp << 4) - tmp; \
34+
} while (0)
35+
# define MOD28(a) \
36+
do { \
37+
CHOP(a); \
38+
if (a >= BASE) a -= BASE; \
39+
} while (0)
40+
# define MOD(a) \
41+
do { \
42+
CHOP(a); \
43+
MOD28(a); \
44+
} while (0)
45+
# define MOD63(a) \
46+
do { /* this assumes a is not negative */ \
47+
z_off64_t tmp = a >> 32; \
48+
a &= 0xffffffffL; \
49+
a += (tmp << 8) - (tmp << 5) + tmp; \
50+
tmp = a >> 16; \
51+
a &= 0xffffL; \
52+
a += (tmp << 4) - tmp; \
53+
tmp = a >> 16; \
54+
a &= 0xffffL; \
55+
a += (tmp << 4) - tmp; \
56+
if (a >= BASE) a -= BASE; \
57+
} while (0)
58+
#else
59+
# define MOD(a) a %= BASE
60+
# define MOD28(a) a %= BASE
61+
# define MOD63(a) a %= BASE
62+
#endif
63+
64+
#if defined(ADLER32_SIMD_NEON) || defined (ADLER32_SIMD_SSSE3)
65+
#include "adler32_simd.h"
66+
#endif
67+
68+
/* ========================================================================= */
69+
uLong ZEXPORT adler32(adler, buf, len)
70+
uLong adler;
71+
const Bytef *buf;
72+
uInt len;
73+
{
74+
unsigned long sum2;
75+
unsigned n;
76+
77+
#if defined(ADLER32_SIMD_NEON) || defined(ADLER32_SIMD_SSSE3)
78+
if (buf && len >= 64)
79+
return adler32_simd_(adler, buf, len);
80+
#endif
81+
82+
/* split Adler-32 into component sums */
83+
sum2 = (adler >> 16) & 0xffff;
84+
adler &= 0xffff;
85+
86+
/* in case user likes doing a byte at a time, keep it fast */
87+
if (len == 1) {
88+
adler += buf[0];
89+
if (adler >= BASE)
90+
adler -= BASE;
91+
sum2 += adler;
92+
if (sum2 >= BASE)
93+
sum2 -= BASE;
94+
return adler | (sum2 << 16);
95+
}
96+
97+
/* initial Adler-32 value (deferred check for len == 1 speed) */
98+
if (buf == Z_NULL)
99+
return 1L;
100+
101+
/* in case short lengths are provided, keep it somewhat fast */
102+
if (len < 16) {
103+
while (len--) {
104+
adler += *buf++;
105+
sum2 += adler;
106+
}
107+
if (adler >= BASE)
108+
adler -= BASE;
109+
MOD28(sum2); /* only added so many BASE's */
110+
return adler | (sum2 << 16);
111+
}
112+
113+
/* do length NMAX blocks -- requires just one modulo operation */
114+
while (len >= NMAX) {
115+
len -= NMAX;
116+
n = NMAX / 16; /* NMAX is divisible by 16 */
117+
do {
118+
DO16(buf); /* 16 sums unrolled */
119+
buf += 16;
120+
} while (--n);
121+
MOD(adler);
122+
MOD(sum2);
123+
}
124+
125+
/* do remaining bytes (less than NMAX, still just one modulo) */
126+
if (len) { /* avoid modulos if none remaining */
127+
while (len >= 16) {
128+
len -= 16;
129+
DO16(buf);
130+
buf += 16;
131+
}
132+
while (len--) {
133+
adler += *buf++;
134+
sum2 += adler;
135+
}
136+
MOD(adler);
137+
MOD(sum2);
138+
}
139+
140+
/* return recombined sums */
141+
return adler | (sum2 << 16);
142+
}
143+
144+
/* ========================================================================= */
145+
local uLong adler32_combine_(adler1, adler2, len2)
146+
uLong adler1;
147+
uLong adler2;
148+
z_off64_t len2;
149+
{
150+
unsigned long sum1;
151+
unsigned long sum2;
152+
unsigned rem;
153+
154+
/* for negative len, return invalid adler32 as a clue for debugging */
155+
if (len2 < 0)
156+
return 0xffffffffUL;
157+
158+
/* the derivation of this formula is left as an exercise for the reader */
159+
MOD63(len2); /* assumes len2 >= 0 */
160+
rem = (unsigned)len2;
161+
sum1 = adler1 & 0xffff;
162+
sum2 = rem * sum1;
163+
MOD(sum2);
164+
sum1 += (adler2 & 0xffff) + BASE - 1;
165+
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
166+
if (sum1 >= BASE) sum1 -= BASE;
167+
if (sum1 >= BASE) sum1 -= BASE;
168+
if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
169+
if (sum2 >= BASE) sum2 -= BASE;
170+
return sum1 | (sum2 << 16);
171+
}
172+
173+
/* ========================================================================= */
174+
uLong ZEXPORT adler32_combine(adler1, adler2, len2)
175+
uLong adler1;
176+
uLong adler2;
177+
z_off_t len2;
178+
{
179+
return adler32_combine_(adler1, adler2, len2);
180+
}
181+
182+
uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
183+
uLong adler1;
184+
uLong adler2;
185+
z_off64_t len2;
186+
{
187+
return adler32_combine_(adler1, adler2, len2);
188+
}

0 commit comments

Comments
 (0)