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/.
29 #include "wimlib/assert.h"
30 #include "wimlib/chunk_compressor.h"
31 #include "wimlib/util.h"
35 struct serial_chunk_compressor {
36 struct chunk_compressor base;
37 struct wimlib_compressor *compressor;
44 serial_chunk_compressor_destroy(struct chunk_compressor *_ctx)
46 struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
51 wimlib_free_compressor(ctx->compressor);
58 serial_chunk_compressor_submit_chunk(struct chunk_compressor *_ctx,
59 const void *chunk, u32 size)
61 struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
66 wimlib_assert(size > 0);
67 wimlib_assert(size <= ctx->base.out_chunk_size);
69 memcpy(ctx->udata, chunk, size);
75 serial_chunk_compressor_get_chunk(struct chunk_compressor *_ctx,
76 const void **cdata_ret, u32 *csize_ret,
79 struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
85 clen = wimlib_compress(ctx->udata, ctx->ulen,
86 ctx->cdata, ctx->ulen - 1,
90 *cdata_ret = ctx->cdata;
93 *cdata_ret = ctx->udata;
94 *csize_ret = ctx->ulen;
96 *usize_ret = ctx->ulen;
103 new_serial_chunk_compressor(int out_ctype, u32 out_chunk_size,
104 struct chunk_compressor **compressor_ret)
106 struct serial_chunk_compressor *ctx;
109 wimlib_assert(out_chunk_size > 0);
111 ctx = CALLOC(1, sizeof(*ctx));
113 return WIMLIB_ERR_NOMEM;
115 ctx->base.out_ctype = out_ctype;
116 ctx->base.out_chunk_size = out_chunk_size;
117 ctx->base.num_threads = 1;
118 ctx->base.destroy = serial_chunk_compressor_destroy;
119 ctx->base.submit_chunk = serial_chunk_compressor_submit_chunk;
120 ctx->base.get_chunk = serial_chunk_compressor_get_chunk;
122 ret = wimlib_create_compressor(out_ctype, out_chunk_size,
123 0, &ctx->compressor);
127 ctx->udata = MALLOC(out_chunk_size);
128 ctx->cdata = MALLOC(out_chunk_size - 1);
130 if (ctx->udata == NULL || ctx->cdata == NULL) {
131 ret = WIMLIB_ERR_NOMEM;
135 *compressor_ret = &ctx->base;
139 serial_chunk_compressor_destroy(&ctx->base);