2025-07-28 22:28:59 +01:00
|
|
|
#include <assert.h>
|
2026-03-16 10:35:43 +00:00
|
|
|
#include <fx/compress/compressor.h>
|
|
|
|
|
#include <fx/compress/zstd.h>
|
|
|
|
|
#include <fx/core/ringbuffer.h>
|
2025-07-28 22:28:59 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#define BUF_SIZE 32
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
int refill_input_buffer(FILE *fp, fx_ringbuffer *dest)
|
2025-07-28 22:28:59 +01:00
|
|
|
{
|
|
|
|
|
while (1) {
|
|
|
|
|
void *buf;
|
|
|
|
|
size_t capacity;
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_status status
|
|
|
|
|
= fx_ringbuffer_open_write_buffer(dest, &buf, &capacity);
|
|
|
|
|
if (status == FX_ERR_NO_SPACE) {
|
2025-07-28 22:28:59 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
if (!FX_OK(status)) {
|
2025-07-28 22:28:59 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t r = fread(buf, 1, capacity, fp);
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_ringbuffer_close_write_buffer(dest, &buf, r);
|
2025-07-28 22:28:59 +01:00
|
|
|
|
|
|
|
|
if (r == 0) {
|
|
|
|
|
return ferror(fp) ? -1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (r < capacity) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
int flush_output_buffer(FILE *fp, fx_ringbuffer *src)
|
2025-07-28 22:28:59 +01:00
|
|
|
{
|
|
|
|
|
while (1) {
|
2025-07-30 18:32:51 +01:00
|
|
|
const void *buf;
|
2025-07-28 22:28:59 +01:00
|
|
|
size_t capacity;
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_status status
|
|
|
|
|
= fx_ringbuffer_open_read_buffer(src, &buf, &capacity);
|
|
|
|
|
if (status == FX_ERR_NO_DATA) {
|
2025-07-28 22:28:59 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
if (!FX_OK(status)) {
|
2025-07-28 22:28:59 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t w = fwrite(buf, 1, capacity, fp);
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_ringbuffer_close_read_buffer(src, &buf, w);
|
2025-07-28 22:28:59 +01:00
|
|
|
|
|
|
|
|
if (w < capacity) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, const char **argv)
|
|
|
|
|
{
|
|
|
|
|
if (argc < 4) {
|
|
|
|
|
fprintf(stderr, "usage: %s <C/D> <inpath> <outpath>\n", argv[0]);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_compressor_mode mode;
|
2025-07-28 22:28:59 +01:00
|
|
|
if (!strcmp(argv[1], "C")) {
|
2026-03-16 10:35:43 +00:00
|
|
|
mode = FX_COMPRESSOR_MODE_COMPRESS;
|
2025-07-28 22:28:59 +01:00
|
|
|
} else if (!strcmp(argv[1], "D")) {
|
2026-03-16 10:35:43 +00:00
|
|
|
mode = FX_COMPRESSOR_MODE_DECOMPRESS;
|
2025-07-28 22:28:59 +01:00
|
|
|
} else {
|
|
|
|
|
fprintf(stderr, "invalid mode %s\n", argv[1]);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE *in_fp = fopen(argv[2], "rb");
|
|
|
|
|
if (!in_fp) {
|
|
|
|
|
fprintf(stderr, "cannot open input file %s\n", argv[2]);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE *out_fp = fopen(argv[3], "wb");
|
|
|
|
|
if (!out_fp) {
|
|
|
|
|
fclose(in_fp);
|
|
|
|
|
fprintf(stderr, "cannot open output file %s\n", argv[3]);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_status status = FX_SUCCESS;
|
|
|
|
|
fx_type compressor_type = FX_TYPE_ZSTD_COMPRESSOR;
|
|
|
|
|
fx_compressor *compressor = fx_object_create(compressor_type);
|
2025-10-28 15:21:19 +00:00
|
|
|
|
|
|
|
|
size_t inbuf_size, outbuf_size;
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_compressor_get_buffer_size(
|
2025-10-28 15:21:19 +00:00
|
|
|
compressor_type, mode, &inbuf_size, &outbuf_size);
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_ringbuffer *in = fx_ringbuffer_create(inbuf_size);
|
|
|
|
|
fx_ringbuffer *out = fx_ringbuffer_create(outbuf_size);
|
2025-10-28 15:21:19 +00:00
|
|
|
|
|
|
|
|
if (!in || !out) {
|
|
|
|
|
fprintf(stderr, "memory allocation failure");
|
2025-07-28 22:28:59 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_compressor_set_buffer(compressor, in, out);
|
|
|
|
|
fx_compressor_set_mode(compressor, mode);
|
2025-10-28 15:21:19 +00:00
|
|
|
|
2025-07-28 22:28:59 +01:00
|
|
|
int ret = 0;
|
|
|
|
|
while (1) {
|
|
|
|
|
ret = refill_input_buffer(in_fp, in);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
fprintf(stderr, "read error\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
if (!fx_ringbuffer_available_data_remaining(in)) {
|
2025-07-28 22:28:59 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
status = fx_compressor_step(compressor);
|
2025-07-28 22:28:59 +01:00
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
if (status == FX_ERR_NO_DATA) {
|
2025-07-28 22:28:59 +01:00
|
|
|
break;
|
2026-03-16 10:35:43 +00:00
|
|
|
} else if (status == FX_ERR_NO_SPACE) {
|
2025-07-28 22:28:59 +01:00
|
|
|
ret = flush_output_buffer(out_fp, out);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
fprintf(stderr, "write error\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-03-16 10:35:43 +00:00
|
|
|
} else if (!FX_OK(status)) {
|
2025-07-28 22:28:59 +01:00
|
|
|
ret = -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = flush_output_buffer(out_fp, out);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
fprintf(stderr, "write error\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
if (mode == FX_COMPRESSOR_MODE_COMPRESS) {
|
|
|
|
|
while (!fx_compressor_eof(compressor)) {
|
|
|
|
|
status = fx_compressor_end(compressor);
|
|
|
|
|
if (!FX_OK(status)) {
|
2025-07-28 22:28:59 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
|
"compression finalisation error\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
if (!fx_ringbuffer_available_data_remaining(out)) {
|
2025-07-28 22:28:59 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = flush_output_buffer(out_fp, out);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
fprintf(stderr, "write error\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("Done\n");
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_compressor_unref(compressor);
|
2025-07-28 22:28:59 +01:00
|
|
|
|
|
|
|
|
fclose(in_fp);
|
|
|
|
|
fclose(out_fp);
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_ringbuffer_unref(in);
|
|
|
|
|
fx_ringbuffer_unref(out);
|
2025-07-28 22:28:59 +01:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|