4 * Compress chunks of data (serial version).
8 * Copyright (C) 2013 Eric Biggers
10 * This file is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option) any
15 * This file is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this file; if not, see http://www.gnu.org/licenses/.
31 #include "wimlib/assert.h"
32 #include "wimlib/chunk_compressor.h"
33 #include "wimlib/util.h"
35 struct serial_chunk_compressor {
36 struct chunk_compressor base;
37 struct wimlib_compressor *compressor;
46 serial_chunk_compressor_destroy(struct chunk_compressor *_ctx)
48 struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
53 wimlib_free_compressor(ctx->compressor);
60 serial_chunk_compressor_get_chunk_buffer(struct chunk_compressor *_ctx)
62 struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
70 serial_chunk_compressor_signal_chunk_filled(struct chunk_compressor *_ctx, u32 usize)
72 struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
75 wimlib_assert(usize > 0);
76 wimlib_assert(usize <= ctx->base.out_chunk_size);
79 csize = wimlib_compress(ctx->udata, usize, ctx->cdata, usize - 1,
82 ctx->result_data = ctx->cdata;
83 ctx->result_size = csize;
85 ctx->result_data = ctx->udata;
86 ctx->result_size = ctx->usize;
91 serial_chunk_compressor_get_compression_result(struct chunk_compressor *_ctx,
92 const void **cdata_ret, u32 *csize_ret,
95 struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor *)_ctx;
97 if (!ctx->result_data)
100 *cdata_ret = ctx->result_data;
101 *csize_ret = ctx->result_size;
102 *usize_ret = ctx->usize;
104 ctx->result_data = NULL;
109 new_serial_chunk_compressor(int out_ctype, u32 out_chunk_size,
110 struct chunk_compressor **compressor_ret)
112 struct serial_chunk_compressor *ctx;
115 wimlib_assert(out_chunk_size > 0);
117 ctx = CALLOC(1, sizeof(*ctx));
119 return WIMLIB_ERR_NOMEM;
121 ctx->base.out_ctype = out_ctype;
122 ctx->base.out_chunk_size = out_chunk_size;
123 ctx->base.num_threads = 1;
124 ctx->base.destroy = serial_chunk_compressor_destroy;
125 ctx->base.get_chunk_buffer = serial_chunk_compressor_get_chunk_buffer;
126 ctx->base.signal_chunk_filled = serial_chunk_compressor_signal_chunk_filled;
127 ctx->base.get_compression_result = serial_chunk_compressor_get_compression_result;
129 ret = wimlib_create_compressor(out_ctype, out_chunk_size,
130 WIMLIB_COMPRESSOR_FLAG_DESTRUCTIVE,
135 ctx->udata = MALLOC(out_chunk_size);
136 ctx->cdata = MALLOC(out_chunk_size - 1);
137 if (ctx->udata == NULL || ctx->cdata == NULL) {
138 ret = WIMLIB_ERR_NOMEM;
141 ctx->result_data = NULL;
143 *compressor_ret = &ctx->base;
147 serial_chunk_compressor_destroy(&ctx->base);