#include #include #include #include #include #include #include #include int main(int argc, const char **argv) { if (argc < 2) { fprintf(stderr, "usage: %s \n", argv[0]); return -1; } const b_compression_function *zstd = b_compression_function_get_by_id(B_COMPRESSOR_FUNCTION_ZSTD); if (!zstd) { fprintf(stderr, "zstd support not enabled\n"); return -1; } b_compression_mode mode = B_COMPRESSION_MODE_DECOMPRESS; FILE *in_fp = fopen(argv[1], "rb"); if (!in_fp) { fprintf(stderr, "cannot open input file %s\n", argv[1]); return -1; } b_stream *in_stream = b_stream_open_fp(in_fp); b_cstream *cstream; b_cstream_open(in_stream, zstd, mode, &cstream); bool compressed = false; char buf[513]; for (int i = 0;; i++) { if (compressed) { b_cstream_begin_compressed_section(cstream, NULL); } memset(buf, 0x0, sizeof buf); size_t nr_read = 0; b_status status = b_cstream_read(cstream, buf, sizeof buf - 1, &nr_read); if (!B_OK(status)) { fprintf(stderr, "write error: %s\n", b_status_description(status)); break; } if (nr_read == 0) { break; } size_t nr_read_compressed = 0; if (compressed) { b_cstream_end_compressed_section( cstream, &nr_read_compressed, &nr_read); } size_t tx_total = 0; b_cstream_tx_bytes(cstream, &tx_total); printf(" * iteration %d: read %zu (compressed) / %zu " "(uncompressed) / %zu (total) bytes (%s)\n", i, nr_read_compressed, nr_read, tx_total, compressed ? "compressed" : "uncompressed"); printf("%s\n", buf); compressed = !compressed; } printf("Done\n"); b_cstream_close(cstream); b_stream_close(in_stream); fclose(in_fp); return 0; }