From e136e7436c7d0f47f7222e43daa56ed721d12b0e Mon Sep 17 00:00:00 2001 From: ygrek Date: Sat, 6 Aug 2022 17:07:23 +0100 Subject: [PATCH] sync zlibstubs.c with upstream --- src/utils/cdk/bzip2stubs.c | 2 +- src/utils/cdk/zlibstubs.c | 56 ++++++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/utils/cdk/bzip2stubs.c b/src/utils/cdk/bzip2stubs.c index d27c416b..d06e0c1a 100644 --- a/src/utils/cdk/bzip2stubs.c +++ b/src/utils/cdk/bzip2stubs.c @@ -31,7 +31,7 @@ Boston, MA 02110-1301, USA. #define BZStream_val(v) ((bz_stream *) (v)) -static value * camlzip_bzerror_exn = NULL; +static const value * camlzip_bzerror_exn = NULL; #ifdef USE_BZIP2 static void camlzip_bzerror(char * fn, int err) diff --git a/src/utils/cdk/zlibstubs.c b/src/utils/cdk/zlibstubs.c index e745c0a5..48277c99 100644 --- a/src/utils/cdk/zlibstubs.c +++ b/src/utils/cdk/zlibstubs.c @@ -6,7 +6,8 @@ /* */ /* Copyright 2001 Institut National de Recherche en Informatique et */ /* en Automatique. All rights reserved. This file is distributed */ -/* under the terms of the GNU Library General Public License. */ +/* under the terms of the GNU Lesser General Public License, with */ +/* the special exception on linking described in file LICENSE. */ /* */ /***********************************************************************/ @@ -23,10 +24,11 @@ #include #include #include +#include -#define ZStream_val(v) ((z_stream *) (v)) +#define ZStream_val(v) (*((z_streamp *)Data_custom_val(v))) -static value * camlzip_error_exn = NULL; +static const value * camlzip_error_exn = NULL; static void camlzip_error(char * fn, value vzs) { @@ -38,28 +40,40 @@ static void camlzip_error(char * fn, value vzs) if (camlzip_error_exn == NULL) { camlzip_error_exn = caml_named_value("Zlib.Error"); if (camlzip_error_exn == NULL) - invalid_argument("Exception Zlib.Error not initialized"); + caml_invalid_argument("Exception Zlib.Error not initialized"); } Begin_roots3(s1, s2, bucket); - s1 = copy_string(fn); - s2 = copy_string(msg); - bucket = alloc_small(3, 0); + s1 = caml_copy_string(fn); + s2 = caml_copy_string(msg); + bucket = caml_alloc_small(3, 0); Field(bucket, 0) = *camlzip_error_exn; Field(bucket, 1) = s1; Field(bucket, 2) = s2; End_roots(); - mlraise(bucket); + caml_raise(bucket); } +void camlzip_free_stream(value vzs) +{ + caml_stat_free(ZStream_val(vzs)); + ZStream_val(vzs) = NULL; +} + +static struct custom_operations camlzip_stream_ops = { + "camlzip_stream_ops", &camlzip_free_stream, NULL, NULL, NULL, NULL +}; + static value camlzip_new_stream(void) { - z_stream * zs = (z_stream *) malloc(sizeof(z_stream)); - zs->zalloc = NULL; - zs->zfree = NULL; - zs->opaque = NULL; - zs->next_in = NULL; - zs->next_out = NULL; - return (value) zs; + value res = caml_alloc_custom(&camlzip_stream_ops, sizeof(z_streamp), 0, 1); + + ZStream_val(res) = caml_stat_alloc(sizeof(z_stream)); + ZStream_val(res)->zalloc = NULL; + ZStream_val(res)->zfree = NULL; + ZStream_val(res)->opaque = NULL; + ZStream_val(res)->next_in = NULL; + ZStream_val(res)->next_out = NULL; + return res; } value camlzip_deflateInit(value vlevel, value expect_header) @@ -92,12 +106,12 @@ value camlzip_deflate(value vzs, value srcbuf, value srcpos, value srclen, zs->next_out = &Byte_u(dstbuf, Long_val(dstpos)); zs->avail_out = Long_val(dstlen); retcode = deflate(zs, camlzip_flush_table[Int_val(vflush)]); - if (retcode < 0) camlzip_error("Zlib.deflate", vzs); + if (retcode < 0 && retcode != Z_BUF_ERROR) camlzip_error("Zlib.deflate", vzs); used_in = Long_val(srclen) - zs->avail_in; used_out = Long_val(dstlen) - zs->avail_out; zs->next_in = NULL; /* not required, but cleaner */ zs->next_out = NULL; /* (avoid dangling pointers into Caml heap) */ - res = alloc_small(3, 0); + res = caml_alloc_small(3, 0); Field(res, 0) = Val_bool(retcode == Z_STREAM_END); Field(res, 1) = Val_int(used_in); Field(res, 2) = Val_int(used_out); @@ -114,7 +128,6 @@ value camlzip_deflateEnd(value vzs) { if (deflateEnd(ZStream_val(vzs)) != Z_OK) camlzip_error("Zlib.deflateEnd", vzs); - free(ZStream_val(vzs)); return Val_unit; } @@ -141,13 +154,13 @@ value camlzip_inflate(value vzs, value srcbuf, value srcpos, value srclen, zs->next_out = &Byte_u(dstbuf, Long_val(dstpos)); zs->avail_out = Long_val(dstlen); retcode = inflate(zs, camlzip_flush_table[Int_val(vflush)]); - if (retcode < 0 || retcode == Z_NEED_DICT) + if ((retcode < 0 && retcode != Z_BUF_ERROR) || retcode == Z_NEED_DICT) camlzip_error("Zlib.inflate", vzs); used_in = Long_val(srclen) - zs->avail_in; used_out = Long_val(dstlen) - zs->avail_out; zs->next_in = NULL; /* not required, but cleaner */ zs->next_out = NULL; /* (avoid dangling pointers into Caml heap) */ - res = alloc_small(3, 0); + res = caml_alloc_small(3, 0); Field(res, 0) = Val_bool(retcode == Z_STREAM_END); Field(res, 1) = Val_int(used_in); Field(res, 2) = Val_int(used_out); @@ -164,13 +177,12 @@ value camlzip_inflateEnd(value vzs) { if (inflateEnd(ZStream_val(vzs)) != Z_OK) camlzip_error("Zlib.inflateEnd", vzs); - free(ZStream_val(vzs)); return Val_unit; } value camlzip_update_crc32(value crc, value buf, value pos, value len) { - return copy_int32(crc32((uint32_t) Int32_val(crc), + return caml_copy_int32(crc32((uint32_t) Int32_val(crc), &Byte_u(buf, Long_val(pos)), Long_val(len))); } -- 2.11.4.GIT