4 * Compress chunks of data (serial version).
8 * Copyright (C) 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free Software
14 * Foundation; either version 3 of the License, or (at your option) any later
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along with
22 * wimlib; if not, see http://www.gnu.org/licenses/.
30 #include "wimlib/assert.h"
31 #include "wimlib/chunk_compressor.h"
32 #include "wimlib/util.h"
36 struct serial_chunk_compressor {
37 struct chunk_compressor base;
38 struct wimlib_compressor *compressor;
45 serial_chunk_compressor_destroy(struct chunk_compressor *_ctx)
47 struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
52 wimlib_free_compressor(ctx->compressor);
59 serial_chunk_compressor_submit_chunk(struct chunk_compressor *_ctx,
60 const void *chunk, u32 size)
62 struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
67 wimlib_assert(size > 0);
68 wimlib_assert(size <= ctx->base.out_chunk_size);
70 memcpy(ctx->udata, chunk, size);
76 serial_chunk_compressor_get_chunk(struct chunk_compressor *_ctx,
77 const void **cdata_ret, u32 *csize_ret,
80 struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
86 clen = wimlib_compress(ctx->udata, ctx->ulen,
87 ctx->cdata, ctx->ulen - 1,
91 *cdata_ret = ctx->cdata;
94 *cdata_ret = ctx->udata;
95 *csize_ret = ctx->ulen;
97 *usize_ret = ctx->ulen;
104 new_serial_chunk_compressor(int out_ctype, u32 out_chunk_size,
105 struct chunk_compressor **compressor_ret)
107 struct serial_chunk_compressor *ctx;
110 wimlib_assert(out_chunk_size > 0);
112 ctx = CALLOC(1, sizeof(*ctx));
114 return WIMLIB_ERR_NOMEM;
116 ctx->base.out_ctype = out_ctype;
117 ctx->base.out_chunk_size = out_chunk_size;
118 ctx->base.num_threads = 1;
119 ctx->base.destroy = serial_chunk_compressor_destroy;
120 ctx->base.submit_chunk = serial_chunk_compressor_submit_chunk;
121 ctx->base.get_chunk = serial_chunk_compressor_get_chunk;
123 ret = wimlib_create_compressor(out_ctype, out_chunk_size,
124 0, &ctx->compressor);
128 ctx->udata = MALLOC(out_chunk_size);
129 ctx->cdata = MALLOC(out_chunk_size - 1);
131 if (ctx->udata == NULL || ctx->cdata == NULL) {
132 ret = WIMLIB_ERR_NOMEM;
136 *compressor_ret = &ctx->base;
140 serial_chunk_compressor_destroy(&ctx->base);