Skip to content

Commit

Permalink
sync zlibstubs.c with upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrek committed Aug 6, 2022
1 parent d241bcf commit e136e74
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/utils/cdk/bzip2stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 34 additions & 22 deletions src/utils/cdk/zlibstubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
/* */
/***********************************************************************/

Expand All @@ -23,10 +24,11 @@
#include <caml/callback.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/custom.h>

#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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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)));
}
Expand Down

0 comments on commit e136e74

Please sign in to comment.