Skip to content

Commit 9408e3a

Browse files
committed
Provide a clear option to depend on zlib-ng
1 parent e36e670 commit 9408e3a

File tree

5 files changed

+83
-6
lines changed

5 files changed

+83
-6
lines changed

setup.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,12 @@ def build_extensions(self) -> None:
725725

726726
if feature.want("zlib"):
727727
_dbg("Looking for zlib")
728-
if _find_include_file(self, "zlib.h"):
728+
if _find_include_file(self, "zlib-ng.h"):
729+
if _find_library_file(self, "z-ng"):
730+
feature.set("zlib", "z-ng")
731+
elif sys.platform == "win32" and _find_library_file(self, "zlib-ng"):
732+
feature.set("zlib", "zlib-ng")
733+
elif _find_include_file(self, "zlib.h"):
729734
if _find_library_file(self, "z"):
730735
feature.set("zlib", "z")
731736
elif sys.platform == "win32" and _find_library_file(self, "zlib"):
@@ -923,9 +928,11 @@ def build_extensions(self) -> None:
923928
defs.append(("HAVE_OPENJPEG", None))
924929
if sys.platform == "win32" and not PLATFORM_MINGW:
925930
defs.append(("OPJ_STATIC", None))
926-
if feature.get("zlib"):
927-
libs.append(feature.get("zlib"))
931+
if zlib := feature.get("zlib"):
932+
libs.append(zlib)
928933
defs.append(("HAVE_LIBZ", None))
934+
if zlib in ["z-ng", "zlib-ng"]:
935+
defs.append(("HAVE_ZLIBNG", None))
929936
if feature.get("imagequant"):
930937
libs.append(feature.get("imagequant"))
931938
defs.append(("HAVE_LIBIMAGEQUANT", None))

src/_imaging.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@
8585
#endif
8686
#endif
8787

88-
#ifdef HAVE_LIBZ
89-
#include "zlib.h"
88+
#ifdef HAVE_ZLIBNG
89+
#include <zlib-ng.h>
90+
#else
91+
#include <zlib.h>
9092
#endif
9193

9294
#ifdef HAVE_LIBTIFF

src/libImaging/ZipCodecs.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
* Copyright (c) Fredrik Lundh 1996.
88
*/
99

10-
#include "zlib.h"
10+
#ifdef HAVE_ZLIBNG
11+
#include <zlib-ng.h>
12+
#else
13+
#include <zlib.h>
14+
#endif
1115

1216
/* modes */
1317
#define ZIP_PNG 0 /* continuous, filtered image data */
@@ -35,7 +39,11 @@ typedef struct {
3539

3640
/* PRIVATE CONTEXT (set by decoder/encoder) */
3741

42+
#ifdef HAVE_ZLIBNG
43+
zng_stream z_stream; /* (de)compression stream */
44+
#else
3845
z_stream z_stream; /* (de)compression stream */
46+
#endif
3947

4048
UINT8 *previous; /* previous line (allocated) */
4149

src/libImaging/ZipDecode.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
8080
context->z_stream.zfree = (free_func)NULL;
8181
context->z_stream.opaque = (voidpf)NULL;
8282

83+
#ifdef HAVE_ZLIBNG
84+
err = zng_inflateInit(&context->z_stream);
85+
#else
8386
err = inflateInit(&context->z_stream);
87+
#endif
8488
if (err < 0) {
8589
state->errcode = IMAGING_CODEC_CONFIG;
8690
free(context->previous);
@@ -112,7 +116,11 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
112116
context->z_stream.next_out = state->buffer + context->last_output;
113117
context->z_stream.avail_out = row_len + context->prefix - context->last_output;
114118

119+
#ifdef HAVE_ZLIBNG
120+
err = zng_inflate(&context->z_stream, Z_NO_FLUSH);
121+
#else
115122
err = inflate(&context->z_stream, Z_NO_FLUSH);
123+
#endif
116124

117125
if (err < 0) {
118126
/* Something went wrong inside the compression library */
@@ -125,7 +133,11 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
125133
}
126134
free(context->previous);
127135
context->previous = NULL;
136+
#ifdef HAVE_ZLIBNG
137+
zng_inflateEnd(&context->z_stream);
138+
#else
128139
inflateEnd(&context->z_stream);
140+
#endif
129141
return -1;
130142
}
131143

@@ -196,7 +208,11 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
196208
state->errcode = IMAGING_CODEC_UNKNOWN;
197209
free(context->previous);
198210
context->previous = NULL;
211+
#ifdef HAVE_ZLIBNG
212+
zng_inflateEnd(&context->z_stream);
213+
#else
199214
inflateEnd(&context->z_stream);
215+
#endif
200216
return -1;
201217
}
202218
break;
@@ -270,7 +286,11 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
270286

271287
free(context->previous);
272288
context->previous = NULL;
289+
#ifdef HAVE_ZLIBNG
290+
zng_inflateEnd(&context->z_stream);
291+
#else
273292
inflateEnd(&context->z_stream);
293+
#endif
274294
return -1; /* end of file (errcode=0) */
275295
}
276296

@@ -292,7 +312,11 @@ ImagingZipDecodeCleanup(ImagingCodecState state) {
292312

293313
/* Clean up */
294314
if (context->previous) {
315+
#ifdef HAVE_ZLIBNG
316+
zng_inflateEnd(&context->z_stream);
317+
#else
295318
inflateEnd(&context->z_stream);
319+
#endif
296320
free(context->previous);
297321
context->previous = NULL;
298322
}

src/libImaging/ZipEncode.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
8888
compress_type = context->compress_type;
8989
}
9090

91+
#ifdef HAVE_ZLIBNG
92+
err = zng_deflateInit2(
93+
#else
9194
err = deflateInit2(
95+
#endif
9296
&context->z_stream,
9397
/* compression level */
9498
compress_level,
@@ -106,7 +110,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
106110
}
107111

108112
if (context->dictionary && context->dictionary_size > 0) {
113+
#ifdef HAVE_ZLIBNG
114+
err = zng_deflateSetDictionary(
115+
#else
109116
err = deflateSetDictionary(
117+
#endif
110118
&context->z_stream,
111119
(unsigned char *)context->dictionary,
112120
context->dictionary_size
@@ -126,7 +134,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
126134
context->z_stream.avail_out = bytes;
127135
if (context->z_stream.next_in && context->z_stream.avail_in > 0) {
128136
/* We have some data from previous round, deflate it first */
137+
#ifdef HAVE_ZLIBNG
138+
err = zng_deflate(&context->z_stream, Z_NO_FLUSH);
139+
#else
129140
err = deflate(&context->z_stream, Z_NO_FLUSH);
141+
#endif
130142

131143
if (err < 0) {
132144
/* Something went wrong inside the compression library */
@@ -142,7 +154,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
142154
free(context->up);
143155
free(context->prior);
144156
free(context->previous);
157+
#ifdef HAVE_ZLIBNG
158+
zng_deflateEnd(&context->z_stream);
159+
#else
145160
deflateEnd(&context->z_stream);
161+
#endif
146162
return -1;
147163
}
148164
}
@@ -279,7 +295,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
279295
context->z_stream.next_in = context->output;
280296
context->z_stream.avail_in = state->bytes + 1;
281297

298+
#ifdef HAVE_ZLIBNG
299+
err = zng_deflate(&context->z_stream, Z_NO_FLUSH);
300+
#else
282301
err = deflate(&context->z_stream, Z_NO_FLUSH);
302+
#endif
283303

284304
if (err < 0) {
285305
/* Something went wrong inside the compression library */
@@ -295,7 +315,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
295315
free(context->up);
296316
free(context->prior);
297317
free(context->previous);
318+
#ifdef HAVE_ZLIBNG
319+
zng_deflateEnd(&context->z_stream);
320+
#else
298321
deflateEnd(&context->z_stream);
322+
#endif
299323
ImagingSectionLeave(&cookie);
300324
return -1;
301325
}
@@ -315,7 +339,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
315339
/* End of image data; flush compressor buffers */
316340

317341
while (context->z_stream.avail_out > 0) {
342+
#ifdef HAVE_ZLIBNG
343+
err = zng_deflate(&context->z_stream, Z_FINISH);
344+
#else
318345
err = deflate(&context->z_stream, Z_FINISH);
346+
#endif
319347

320348
if (err == Z_STREAM_END) {
321349
free(context->paeth);
@@ -324,7 +352,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
324352
free(context->prior);
325353
free(context->previous);
326354

355+
#ifdef HAVE_ZLIBNG
356+
zng_deflateEnd(&context->z_stream);
357+
#else
327358
deflateEnd(&context->z_stream);
359+
#endif
328360

329361
state->errcode = IMAGING_CODEC_END;
330362

@@ -364,7 +396,11 @@ ImagingZipEncodeCleanup(ImagingCodecState state) {
364396

365397
const char *
366398
ImagingZipVersion(void) {
399+
#ifdef HAVE_ZLIBNG
400+
return zlibng_version();
401+
#else
367402
return zlibVersion();
403+
#endif
368404
}
369405

370406
#endif

0 commit comments

Comments
 (0)