Beispiel #1
0
static void free_stream_inf( value v ) {
	z_stream *s = val_stream(v);
	if (s) {
		inflateEnd(s); // no error
		free(s);
		s = NULL;
	}
	val_gc(v,NULL);
}
Beispiel #2
0
/**
	set_flush_mode : 'stream -> string -> void
	<doc>Change the flush mode ("NO","SYNC","FULL","FINISH","BLOCK")</doc>
**/
static value set_flush_mode( value s, value flush ) {
	int f;
	if( !val_is_kind(s,k_stream_inf) )
		val_check_kind(s,k_stream_def);
	val_check(flush,string);
	if( strcmp(val_string(flush),"NO") == 0 )
		f = Z_NO_FLUSH;
	else if( strcmp(val_string(flush),"SYNC") == 0 )
		f = Z_SYNC_FLUSH;
	else if( strcmp(val_string(flush),"FULL") == 0 )
		f = Z_FULL_FLUSH;
	else if( strcmp(val_string(flush),"FINISH") == 0 )
		f = Z_FINISH;
	else if( strcmp(val_string(flush),"BLOCK") == 0 )
		f = Z_BLOCK;
	else
		return alloc_null();
	val_flush(val_stream(s)) = f;
	return alloc_null();
}
Beispiel #3
0
/**
	inflate_buffer : 'istream -> src:string -> srcpos:int -> dst:string -> dstpos:int -> { done => bool, read => int, write => int }
**/
static value inflate_buffer( value s, value src, value srcpos, value dst, value dstpos ) {
	z_stream *z;
	int err;
	value o;
	val_check_kind(s,k_stream_inf);
	val_check(srcpos,int);

	buffer src_buf = val_to_buffer(src);
	if (!src_buf)
	   hx_failure("invalid source buffer");
	buffer dst_buf = val_to_buffer(dst);
	if (!dst_buf)
	   hx_failure("invalid destination buffer");

	int slen = buffer_size(src_buf);
	int dlen = buffer_size(dst_buf);

	val_check(dstpos,int);
	z = val_stream(s);
	if( val_int(srcpos) < 0 || val_int(dstpos) < 0 )
		return alloc_null();
	slen -= val_int(srcpos);
	dlen -= val_int(dstpos);
	if( slen < 0 || dlen < 0 )
		return alloc_null();

	z->next_in = (Bytef*)buffer_data(src_buf) + val_int(srcpos);
	z->next_out = (Bytef*)buffer_data(dst_buf) + val_int(dstpos);
	z->avail_in = slen;
	z->avail_out = dlen;
	if( (err = inflate(z,val_flush(z))) < 0 )
		zlib_error(z,err);
	z->next_in = NULL;
	z->next_out = NULL;
	o = alloc_empty_object();
	alloc_field(o,id_done,alloc_bool(err == Z_STREAM_END));
	alloc_field(o,id_read,alloc_int((int)(slen - z->avail_in)));
	alloc_field(o,id_write,alloc_int((int)(dlen - z->avail_out)));
	return o;
}
Beispiel #4
0
/**
	deflate_bound : 'dstream -> n:int -> int
	<doc>Return the maximum buffer size needed to write [n] bytes</doc>
**/
static value deflate_bound( value s, value size ) {
	val_check_kind(s,k_stream_def);
	val_check(size,int);
	return alloc_int(deflateBound(val_stream(s),val_int(size)));
}
Beispiel #5
0
/**
	get_adler32 : 'stream -> 'int32
	<doc>Returns the adler32 value of the stream</doc>
**/
static value get_adler32( value s ) {
	if( !val_is_kind(s,k_stream_inf) )
		val_check_kind(s,k_stream_def);
	return alloc_int32(val_stream(s)->adler);
}
Beispiel #6
0
static void free_stream_def( value v ) {
	z_stream *s = val_stream(v);
	deflateEnd(s); // no error
	free(s);
	val_gc(v,NULL);
}
Beispiel #7
0
static void free_stream_inf( value v ) {
	z_stream *s = val_stream(v);
	inflateEnd(s); // no error
	free(s);
	free_abstract(v);
}