Files
bluelib/compress-test/stream.c

144 lines
2.8 KiB
C
Raw Normal View History

#include <assert.h>
#include <blue/compress/compressor.h>
#include <blue/compress/cstream.h>
2025-10-28 15:21:19 +00:00
#include <blue/compress/zstd.h>
#include <blue/core/ringbuffer.h>
#include <blue/core/stream.h>
#include <stdio.h>
#include <string.h>
#define BUF_SIZE 32
2025-10-28 15:21:19 +00:00
static int compress(b_type compressor_type, FILE *in, FILE *out)
{
b_stream *out_stream = b_stream_open_fp(out);
b_cstream *cstream;
b_status status = b_cstream_open(
2025-10-28 15:21:19 +00:00
out_stream, compressor_type, B_COMPRESSOR_MODE_COMPRESS, &cstream);
if (!B_OK(status)) {
fprintf(stderr, "cannot initialise compressor\n");
return -1;
}
b_cstream_begin_compressed_section(cstream, NULL);
char buf[4096];
while (1) {
size_t r = fread(buf, 1, sizeof buf, in);
size_t w = 0;
b_cstream_write(cstream, buf, r, &w);
if (r != w) {
fprintf(stderr, "write error\n");
return -1;
}
if (r < sizeof buf) {
break;
}
}
b_cstream_end_compressed_section(cstream, NULL, NULL);
2025-10-28 15:21:19 +00:00
b_cstream_unref(cstream);
2025-10-24 12:51:54 +01:00
b_stream_unref(out_stream);
return 0;
}
2025-10-28 15:21:19 +00:00
static int decompress(b_type compressor_type, FILE *in, FILE *out)
{
b_stream *in_stream = b_stream_open_fp(in);
b_cstream *cstream;
b_status status = b_cstream_open(
2025-10-28 15:21:19 +00:00
in_stream, compressor_type, B_COMPRESSOR_MODE_DECOMPRESS, &cstream);
if (!B_OK(status)) {
fprintf(stderr, "cannot initialise compressor\n");
return -1;
}
b_cstream_begin_compressed_section(cstream, NULL);
char buf[4096];
while (1) {
size_t r = 0;
b_status status = b_cstream_read(cstream, buf, sizeof buf, &r);
if (!B_OK(status)) {
fprintf(stderr, "read error: %s\n",
b_status_description(status));
return -1;
}
size_t w = fwrite(buf, 1, r, out);
if (r != w) {
fprintf(stderr, "write error\n");
return -1;
}
if (r < sizeof buf) {
break;
}
}
b_cstream_end_compressed_section(cstream, NULL, NULL);
2025-10-28 15:21:19 +00:00
b_cstream_unref(cstream);
2025-10-24 12:51:54 +01:00
b_stream_unref(in_stream);
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;
}
2025-10-28 15:21:19 +00:00
b_compressor_mode mode;
if (!strcmp(argv[1], "C")) {
2025-10-28 15:21:19 +00:00
mode = B_COMPRESSOR_MODE_COMPRESS;
} else if (!strcmp(argv[1], "D")) {
2025-10-28 15:21:19 +00:00
mode = B_COMPRESSOR_MODE_DECOMPRESS;
} 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;
}
int ret = 0;
switch (mode) {
2025-10-28 15:21:19 +00:00
case B_COMPRESSOR_MODE_COMPRESS:
ret = compress(B_TYPE_ZSTD_COMPRESSOR, in_fp, out_fp);
break;
2025-10-28 15:21:19 +00:00
case B_COMPRESSOR_MODE_DECOMPRESS:
ret = decompress(B_TYPE_ZSTD_COMPRESSOR, in_fp, out_fp);
break;
default:
ret = -1;
break;
}
printf("Done\n");
fclose(in_fp);
fclose(out_fp);
return ret;
}