Ejemplo n.º 1
0
/**
 * OCaml binding for _zlib_'s `deflateInit2` function.
 *
 * This creates a new stream and initializes it for deflate.
 *
 * This function may raise the following OCaml exceptions:
 * - Out_of_memory exception
 * - Failure exception: Invalid parameters
 * - Failure exception: Invalid version
 * - Failure exception: Unknown zlib return code
 *
 * See:
 * https://github.com/madler/zlib/blob/cacf7f1d4e3d44d871b605da3b647f07d718623f/zlib.h#L538
 *
 * @param levelVal {value} OCaml `int`: the compression level, must be in the range 0..9.
 *     0 gives no compression at all, 1 the best speed, 9 the best compression.
 * @param windowBitsVal {value} OCaml `int`: base two logarithm of the window size (size of the
 *     history buffer) used by _zlib_. It should be in the range 9..15 for this version of _zlib_.
 *     It can also be in the range -15..-8 (the absolute value is used) for raw deflate.
 *     Finally, it can be greater than 15 for gzip encoding. See _zlib_'s documentation for
 *     `deflateInit2` for the exact documentation.
 * @return {value} An OCaml value representing the new stream, initialized for deflate.
 */
CAMLprim value zlib_deflate_init2(value level_val, value window_bits_val) {
	int level = Int_val(level_val);
	int window_bits = Int_val(window_bits_val);
	value z_streamp_val = zlib_new_stream();
	z_streamp stream = ZStreamP_val(z_streamp_val);

	int deflate_init2_result = deflateInit2(
		stream,
		level,
		Z_DEFLATED, // method
		window_bits,
		8, // memLevel
		Z_DEFAULT_STRATEGY // strategy
	);

	if (deflate_init2_result == Z_OK) {
		return z_streamp_val;
	}

	switch (deflate_init2_result) {
		case Z_MEM_ERROR:
			caml_raise_out_of_memory();
			break;
		case Z_STREAM_ERROR:
			// TODO: use stream->msg to get _zlib_'s text message
			failwith("Error in `zlib_deflate_init2` (extc_stubs.c): call to `deflateInit2` failed: Z_STREAM_ERROR");
			break;
		case Z_VERSION_ERROR:
			// TODO: use stream->msg to get _zlib_'s text message
			failwith("Error in `zlib_deflate_init2` (extc_stubs.c): call to `deflateInit2` failed: Z_VERSION_ERROR");
			break;
		default:
			failwith("Error in `zlib_deflate_init2` (extc_stubs.c): unknown return code from `deflateInit2`");
	}
	assert(0);
}
Ejemplo n.º 2
0
CAMLprim value zlib_inflate_init(value wbits) {
	value z = zlib_new_stream();
	if( inflateInit2(ZStreamP_val(z),Int_val(wbits)) != Z_OK )
		failwith("zlib_inflate_init");
	return z;
}
Ejemplo n.º 3
0
CAMLprim value zlib_deflate_init2(value lvl,value wbits) {
	value z = zlib_new_stream();
	if( deflateInit2(zval(z),Int_val(lvl),Z_DEFLATED,Int_val(wbits),8,Z_DEFAULT_STRATEGY) != Z_OK )
		failwith("zlib_deflate_init");
	return z;
}