compress: compressor: convert b_compressor to a b_object interface
This commit is contained in:
@@ -1,25 +1,27 @@
|
||||
#include "../compressor.h"
|
||||
#include "../function.h"
|
||||
|
||||
#include <blue/compress/zstd.h>
|
||||
#include <blue/core/ringbuffer.h>
|
||||
#include <zstd.h>
|
||||
|
||||
struct zstd_ctx {
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
struct b_zstd_compressor_p {
|
||||
union {
|
||||
ZSTD_CCtx *zstd_c;
|
||||
ZSTD_DCtx *zstd_d;
|
||||
};
|
||||
};
|
||||
|
||||
static enum b_status buffer_size(
|
||||
enum b_compression_mode mode, size_t *inbuf_size, size_t *outbuf_size)
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
b_status b_zstd_compressor_get_buffer_size(
|
||||
b_compressor_mode mode, size_t *inbuf_size, size_t *outbuf_size)
|
||||
{
|
||||
switch (mode) {
|
||||
case B_COMPRESSION_MODE_COMPRESS:
|
||||
case B_COMPRESSOR_MODE_COMPRESS:
|
||||
*inbuf_size = ZSTD_CStreamInSize();
|
||||
*outbuf_size = ZSTD_CStreamOutSize();
|
||||
break;
|
||||
case B_COMPRESSION_MODE_DECOMPRESS:
|
||||
case B_COMPRESSOR_MODE_DECOMPRESS:
|
||||
*inbuf_size = ZSTD_DStreamInSize();
|
||||
*outbuf_size = ZSTD_DStreamOutSize();
|
||||
break;
|
||||
@@ -30,54 +32,44 @@ static enum b_status buffer_size(
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static enum b_status init(struct b_compressor *compressor)
|
||||
{
|
||||
struct zstd_ctx *ctx = b_compressor_get_function_ctx(compressor);
|
||||
switch (compressor->c_mode) {
|
||||
case B_COMPRESSION_MODE_COMPRESS:
|
||||
ctx->zstd_c = ZSTD_createCCtx();
|
||||
if (!ctx->zstd_c) {
|
||||
return B_ERR_NO_MEMORY;
|
||||
}
|
||||
break;
|
||||
case B_COMPRESSION_MODE_DECOMPRESS:
|
||||
ctx->zstd_d = ZSTD_createDCtx();
|
||||
if (!ctx->zstd_d) {
|
||||
return B_ERR_NO_MEMORY;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return B_ERR_BAD_STATE;
|
||||
}
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
return B_SUCCESS;
|
||||
static void zstd_compressor_init(b_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
static enum b_status fini(struct b_compressor *compressor)
|
||||
static void zstd_compressor_fini(b_object *obj, void *priv)
|
||||
{
|
||||
struct zstd_ctx *ctx = b_compressor_get_function_ctx(compressor);
|
||||
switch (compressor->c_mode) {
|
||||
case B_COMPRESSION_MODE_COMPRESS:
|
||||
b_compressor_data *c = b_object_get_protected(obj, B_TYPE_COMPRESSOR);
|
||||
struct b_zstd_compressor_p *ctx = priv;
|
||||
switch (c->c_mode) {
|
||||
case B_COMPRESSOR_MODE_COMPRESS:
|
||||
ZSTD_freeCCtx(ctx->zstd_c);
|
||||
break;
|
||||
case B_COMPRESSION_MODE_DECOMPRESS:
|
||||
case B_COMPRESSOR_MODE_DECOMPRESS:
|
||||
ZSTD_freeDCtx(ctx->zstd_d);
|
||||
break;
|
||||
default:
|
||||
return B_ERR_BAD_STATE;
|
||||
break;
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static enum b_status reset(struct b_compressor *compressor)
|
||||
static enum b_status reset(b_compressor *compressor)
|
||||
{
|
||||
struct zstd_ctx *ctx = b_compressor_get_function_ctx(compressor);
|
||||
switch (compressor->c_mode) {
|
||||
case B_COMPRESSION_MODE_COMPRESS:
|
||||
struct b_zstd_compressor_p *ctx
|
||||
= b_object_get_private(compressor, B_TYPE_ZSTD_COMPRESSOR);
|
||||
b_compressor_data *data
|
||||
= b_object_get_protected(compressor, B_TYPE_COMPRESSOR);
|
||||
|
||||
if (!ctx || !data) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
switch (data->c_mode) {
|
||||
case B_COMPRESSOR_MODE_COMPRESS:
|
||||
ZSTD_CCtx_reset(ctx->zstd_c, ZSTD_reset_session_only);
|
||||
break;
|
||||
case B_COMPRESSION_MODE_DECOMPRESS:
|
||||
case B_COMPRESSOR_MODE_DECOMPRESS:
|
||||
ZSTD_DCtx_reset(ctx->zstd_d, ZSTD_reset_session_only);
|
||||
break;
|
||||
default:
|
||||
@@ -87,12 +79,20 @@ static enum b_status reset(struct b_compressor *compressor)
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static enum b_status compress(struct b_compressor *compressor)
|
||||
static enum b_status compress(b_compressor *compressor)
|
||||
{
|
||||
enum b_status status = B_SUCCESS;
|
||||
struct zstd_ctx *ctx = b_compressor_get_function_ctx(compressor);
|
||||
struct b_ringbuffer *in = compressor->c_in;
|
||||
struct b_ringbuffer *out = compressor->c_out;
|
||||
struct b_zstd_compressor_p *ctx
|
||||
= b_object_get_private(compressor, B_TYPE_ZSTD_COMPRESSOR);
|
||||
b_compressor_data *data
|
||||
= b_object_get_protected(compressor, B_TYPE_COMPRESSOR);
|
||||
|
||||
if (!ctx || !data) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
b_ringbuffer *in = data->c_in;
|
||||
b_ringbuffer *out = data->c_out;
|
||||
|
||||
if (b_ringbuffer_available_data_remaining(in) == 0) {
|
||||
return B_ERR_NO_DATA;
|
||||
@@ -156,12 +156,19 @@ static enum b_status compress(struct b_compressor *compressor)
|
||||
return status;
|
||||
}
|
||||
|
||||
static enum b_status compress_end(struct b_compressor *compressor)
|
||||
static enum b_status compress_end(b_compressor *compressor)
|
||||
{
|
||||
enum b_status status = B_SUCCESS;
|
||||
struct zstd_ctx *ctx = b_compressor_get_function_ctx(compressor);
|
||||
struct b_zstd_compressor_p *ctx
|
||||
= b_object_get_private(compressor, B_TYPE_ZSTD_COMPRESSOR);
|
||||
b_compressor_data *data
|
||||
= b_object_get_protected(compressor, B_TYPE_COMPRESSOR);
|
||||
|
||||
struct b_ringbuffer *out = compressor->c_out;
|
||||
if (!ctx || !data) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
b_ringbuffer *out = data->c_out;
|
||||
if (b_ringbuffer_write_capacity_remaining(out) == 0) {
|
||||
return B_ERR_NO_SPACE;
|
||||
}
|
||||
@@ -192,7 +199,7 @@ static enum b_status compress_end(struct b_compressor *compressor)
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
compressor->c_flags |= COMPRESSOR_EOF;
|
||||
data->c_flags |= B_COMPRESSOR_EOF;
|
||||
finished = true;
|
||||
}
|
||||
} while (!finished && z_out.pos < z_out.size);
|
||||
@@ -203,12 +210,20 @@ static enum b_status compress_end(struct b_compressor *compressor)
|
||||
return status;
|
||||
}
|
||||
|
||||
static enum b_status decompress(struct b_compressor *compressor)
|
||||
static enum b_status decompress(b_compressor *compressor)
|
||||
{
|
||||
enum b_status status = B_SUCCESS;
|
||||
struct zstd_ctx *ctx = b_compressor_get_function_ctx(compressor);
|
||||
struct b_ringbuffer *in = compressor->c_in;
|
||||
struct b_ringbuffer *out = compressor->c_out;
|
||||
struct b_zstd_compressor_p *ctx
|
||||
= b_object_get_private(compressor, B_TYPE_ZSTD_COMPRESSOR);
|
||||
b_compressor_data *data
|
||||
= b_object_get_protected(compressor, B_TYPE_COMPRESSOR);
|
||||
|
||||
if (!ctx || !data) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
b_ringbuffer *in = data->c_in;
|
||||
b_ringbuffer *out = data->c_out;
|
||||
|
||||
if (b_ringbuffer_available_data_remaining(in) == 0) {
|
||||
return B_ERR_NO_DATA;
|
||||
@@ -220,7 +235,7 @@ static enum b_status decompress(struct b_compressor *compressor)
|
||||
|
||||
size_t nr_consumed = 0;
|
||||
|
||||
while (!(compressor->c_flags & COMPRESSOR_EOF)) {
|
||||
while (!(data->c_flags & B_COMPRESSOR_EOF)) {
|
||||
size_t in_available = 0, out_capacity = 0;
|
||||
const void *in_buf = NULL;
|
||||
void *out_buf = NULL;
|
||||
@@ -258,7 +273,7 @@ static enum b_status decompress(struct b_compressor *compressor)
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
compressor->c_flags |= COMPRESSOR_EOF;
|
||||
data->c_flags |= B_COMPRESSOR_EOF;
|
||||
break;
|
||||
}
|
||||
} while (z_in.pos < z_in.size && z_out.pos < z_out.size);
|
||||
@@ -277,14 +292,71 @@ static enum b_status decompress(struct b_compressor *compressor)
|
||||
return status;
|
||||
}
|
||||
|
||||
const struct b_compression_function z__b_compression_function_zstd = {
|
||||
.f_name = "zstd",
|
||||
.f_ctx_size = sizeof(struct zstd_ctx),
|
||||
.f_buffer_size = buffer_size,
|
||||
.f_init = init,
|
||||
.f_fini = fini,
|
||||
.f_reset = reset,
|
||||
.f_compress = compress,
|
||||
.f_compress_end = compress_end,
|
||||
.f_decompress = decompress,
|
||||
};
|
||||
static enum b_status set_mode(b_compressor *compressor, b_compressor_mode mode)
|
||||
{
|
||||
struct b_zstd_compressor_p *ctx
|
||||
= b_object_get_private(compressor, B_TYPE_ZSTD_COMPRESSOR);
|
||||
b_compressor_data *data
|
||||
= b_object_get_protected(compressor, B_TYPE_COMPRESSOR);
|
||||
|
||||
if (!ctx || !data) {
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (mode == data->c_mode) {
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
switch (data->c_mode) {
|
||||
case B_COMPRESSOR_MODE_COMPRESS:
|
||||
ZSTD_freeCCtx(ctx->zstd_c);
|
||||
break;
|
||||
case B_COMPRESSOR_MODE_DECOMPRESS:
|
||||
ZSTD_freeDCtx(ctx->zstd_d);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
data->c_mode = mode;
|
||||
|
||||
switch (data->c_mode) {
|
||||
case B_COMPRESSOR_MODE_COMPRESS:
|
||||
ctx->zstd_c = ZSTD_createCCtx();
|
||||
break;
|
||||
case B_COMPRESSOR_MODE_DECOMPRESS:
|
||||
ctx->zstd_d = ZSTD_createDCtx();
|
||||
break;
|
||||
default:
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
B_TYPE_CLASS_DEFINITION_BEGIN(b_zstd_compressor)
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
||||
B_INTERFACE_ENTRY(to_string) = NULL;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
||||
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_compressor, B_TYPE_COMPRESSOR)
|
||||
B_INTERFACE_ENTRY(c_buffer_size)
|
||||
= b_zstd_compressor_get_buffer_size;
|
||||
B_INTERFACE_ENTRY(c_compress) = compress;
|
||||
B_INTERFACE_ENTRY(c_compress_end) = compress_end;
|
||||
B_INTERFACE_ENTRY(c_decompress) = decompress;
|
||||
B_INTERFACE_ENTRY(c_reset) = reset;
|
||||
B_INTERFACE_ENTRY(c_set_mode) = set_mode;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_compressor, B_TYPE_COMPRESSOR)
|
||||
B_TYPE_CLASS_DEFINITION_END(b_zstd_compressor)
|
||||
|
||||
B_TYPE_DEFINITION_BEGIN(b_zstd_compressor)
|
||||
B_TYPE_ID(0x51d437fc, 0xe789, 0x4105, 0xbac7, 0xe6b3f45df198);
|
||||
B_TYPE_EXTENDS(B_TYPE_COMPRESSOR);
|
||||
B_TYPE_CLASS(b_zstd_compressor_class);
|
||||
B_TYPE_INSTANCE_PRIVATE(struct b_zstd_compressor_p);
|
||||
B_TYPE_INSTANCE_INIT(zstd_compressor_init);
|
||||
B_TYPE_INSTANCE_FINI(zstd_compressor_fini);
|
||||
B_TYPE_DEFINITION_END(b_zstd_compressor)
|
||||
|
||||
Reference in New Issue
Block a user