Example #1
0
File: igzip.c Project: ceph/isa-l
int isal_deflate_stateless(struct isal_zstream *stream)
{
	uint8_t *next_in = stream->next_in;
	const uint32_t avail_in = stream->avail_in;
	const uint32_t total_in = stream->total_in;

	uint8_t *next_out = stream->next_out;
	const uint32_t avail_out = stream->avail_out;
	const uint32_t total_out = stream->total_out;
	const uint32_t gzip_flag = stream->gzip_flag;

	uint32_t crc32 = 0;
	uint32_t stored_len;

	/* Final block has already been written */
	stream->internal_state.has_eob_hdr = 0;
	init(&stream->internal_state.bitbuf);
	stream->internal_state.state = ZSTATE_NEW_HDR;
	stream->internal_state.crc = 0;

	if (stream->flush == NO_FLUSH)
		stream->end_of_stream = 1;

	if (stream->flush != NO_FLUSH && stream->flush != FULL_FLUSH)
		return INVALID_FLUSH;

	if (stream->level != 0 && stream->level != 1)
		return ISAL_INVALID_LEVEL;

	if (avail_in == 0)
		stored_len = STORED_BLK_HDR_BZ;
	else
		stored_len =
		    STORED_BLK_HDR_BZ * ((avail_in + STORED_BLK_MAX_BZ - 1) /
					 STORED_BLK_MAX_BZ) + avail_in;

	/*
	   at least 1 byte compressed data in the case of empty dynamic block which only
	   contains the EOB
	 */

	if (stream->gzip_flag == IGZIP_GZIP)
		stored_len += gzip_hdr_bytes + gzip_trl_bytes;

	else if (stream->gzip_flag == IGZIP_GZIP_NO_HDR)
		stored_len += gzip_trl_bytes;

	/*
	   the output buffer should be no less than 8 bytes
	   while empty stored deflate block is 5 bytes only
	 */
	if (stream->avail_out < 8)
		return STATELESS_OVERFLOW;

	if (isal_deflate_int_stateless(stream) == COMP_OK)
		return COMP_OK;
	else {
		if (stream->flush == FULL_FLUSH) {
			stream->internal_state.file_start =
			    (uint8_t *) & stream->internal_state.buffer;
			reset_match_history(stream);
		}
		stream->internal_state.has_eob_hdr = 0;
	}

	if (avail_out < stored_len)
		return STATELESS_OVERFLOW;

	stream->next_in = next_in;
	stream->avail_in = avail_in;
	stream->total_in = total_in;

	stream->next_out = next_out;
	stream->avail_out = avail_out;
	stream->total_out = total_out;

	stream->gzip_flag = gzip_flag;

	if (stream->gzip_flag)
		crc32 = crc32_gzip(0x0, next_in, avail_in);

	return write_stored_block_stateless(stream, stored_len, crc32);
}
Example #2
0
File: igzip.c Project: 01org/isa-l
int isal_deflate_stateless(struct isal_zstream *stream)
{
	uint8_t *next_in = stream->next_in;
	const uint32_t avail_in = stream->avail_in;

	uint8_t *next_out = stream->next_out;
	const uint32_t avail_out = stream->avail_out;

	uint32_t crc32 = 0;
	uint32_t stored_len;
	uint32_t dyn_min_len;
	uint32_t min_len;
	uint32_t select_stored_blk = 0;

	if (avail_in == 0)
		stored_len = STORED_BLK_HDR_BZ;
	else
		stored_len =
		    STORED_BLK_HDR_BZ * ((avail_in + STORED_BLK_MAX_BZ - 1) /
					 STORED_BLK_MAX_BZ) + avail_in;

	/*
	   at least 1 byte compressed data in the case of empty dynamic block which only
	   contains the EOB
	 */

	dyn_min_len = stream->hufftables->deflate_hdr_count + 1;
#ifndef DEFLATE
	dyn_min_len += gzip_hdr_bytes + gzip_trl_bytes + 1;
	stored_len += gzip_hdr_bytes + gzip_trl_bytes;
#endif

	min_len = dyn_min_len;

	if (stored_len < dyn_min_len) {
		min_len = stored_len;
		select_stored_blk = 1;
	}

	/*
	   the output buffer should be no less than 8 bytes
	   while empty stored deflate block is 5 bytes only
	 */
	if (avail_out < min_len || stream->avail_out < 8)
		return STATELESS_OVERFLOW;

	if (!select_stored_blk) {
		if (isal_deflate_int_stateless(stream, next_in, avail_in) == COMP_OK)
			return COMP_OK;
	}
	if (avail_out < stored_len)
		return STATELESS_OVERFLOW;

	isal_deflate_init(stream);

	stream->next_in = next_in;
	stream->avail_in = avail_in;
	stream->total_in = 0;

	stream->next_out = next_out;
	stream->avail_out = avail_out;
	stream->total_out = 0;

#ifndef DEFLATE
	crc32 = crc32_gzip(0x0, next_in, avail_in);
#endif
	return write_stored_block_stateless(stream, stored_len, crc32);
}