Example #1
0
static int encode_snappy(avro_codec_t c, void * data, int64_t len)
{
        uint32_t crc;
        size_t outlen = snappy_max_compressed_length(len);

	if (!c->block_data) {
		c->block_data = avro_malloc(outlen+4);
		c->block_size = outlen+4;
	} else if (c->block_size < (int64_t) (outlen+4)) {
            c->block_data = avro_realloc(c->block_data, c->block_size, (outlen+4));
		c->block_size = outlen+4;
	}

	if (!c->block_data) {
		avro_set_error("Cannot allocate memory for snappy");
		return 1;
	}

        if (snappy_compress(data, len, c->block_data, &outlen) != SNAPPY_OK)
        {
                avro_set_error("Error compressing block with Snappy");
		return 1;
	}

        crc = __bswap_32(crc32(0, data, len));
        memcpy(c->block_data+outlen, &crc, 4);
        c->used_size = outlen+4;

	return 0;
}
Example #2
0
static int decode_snappy(avro_codec_t c, void * data, int64_t len)
{
        uint32_t crc;
        size_t outlen;

        if (snappy_uncompressed_length(data, len-4, &outlen) != SNAPPY_OK) {
		avro_set_error("Uncompressed length error in snappy");
		return 1;
        }

	if (!c->block_data) {
		c->block_data = avro_malloc(outlen);
		c->block_size = outlen;
	} else if ( (size_t)c->block_size < outlen) {
		c->block_data = avro_realloc(c->block_data, c->block_size, outlen);
		c->block_size = outlen;
	}

	if (!c->block_data)
	{
		avro_set_error("Cannot allocate memory for snappy");
		return 1;
	}

        if (snappy_uncompress(data, len-4, c->block_data, &outlen) != SNAPPY_OK)
        {
                avro_set_error("Error uncompressing block with Snappy");
		return 1;
	}

        crc = __bswap_32(crc32(0, c->block_data, outlen));
        if (memcmp(&crc, (char*)data+len-4, 4))
        {
                avro_set_error("CRC32 check failure uncompressing block with Snappy");
		return 1;
	}

        c->used_size = outlen;

	return 0;
}
Example #3
0
int
avro_raw_array_ensure_size(avro_raw_array_t *array, size_t desired_count)
{
	size_t  required_size = array->element_size * desired_count;
	if (array->allocated_size >= required_size) {
		return 0;
	}

	/*
	 * Double the old size when reallocating.
	 */

	size_t  new_size;
	if (array->allocated_size == 0) {
		/*
		 * Start with an arbitrary 10 items.
		 */

		new_size = 10 * array->element_size;
	} else {
		new_size = array->allocated_size * 2;
	}

	if (required_size > new_size) {
		new_size = required_size;
	}

	array->data = avro_realloc(array->data, array->allocated_size, new_size);
	if (array->data == NULL) {
		avro_set_error("Cannot allocate space in array for %zu elements",
			       desired_count);
		return ENOMEM;
	}
	array->allocated_size = new_size;

	return 0;
}