]> wimlib.net Git - wimlib/blob - src/compress_serial.c
b3c6293eef95019aec730229f4e8aab3c5b0c14e
[wimlib] / src / compress_serial.c
1 /*
2  * compress_serial.c
3  *
4  * Compress chunks of data (serial version).
5  */
6
7 /*
8  * Copyright (C) 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
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
15  * version.
16  *
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.
20  *
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/.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #include "wimlib.h"
30 #include "wimlib/assert.h"
31 #include "wimlib/compress_chunks.h"
32 #include "wimlib/util.h"
33
34 #include <string.h>
35
36 struct serial_chunk_compressor {
37         struct chunk_compressor base;
38         struct wimlib_lzx_context *comp_ctx;
39         u8 *udata;
40         u8 *cdata;
41         unsigned ulen;
42         unsigned clen;
43 };
44
45 static void
46 serial_chunk_compressor_destroy(struct chunk_compressor *_ctx)
47 {
48         struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
49
50         if (ctx == NULL)
51                 return;
52
53         FREE(ctx->udata);
54         FREE(ctx->cdata);
55         FREE(ctx);
56 }
57
58 static bool
59 serial_chunk_compressor_submit_chunk(struct chunk_compressor *_ctx,
60                                      const void *chunk, size_t size)
61 {
62         struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
63
64         if (ctx->ulen != 0)
65                 return false;
66
67         wimlib_assert(size > 0);
68         wimlib_assert(size <= ctx->base.out_chunk_size);
69
70         memcpy(ctx->udata, chunk, size);
71         ctx->ulen = size;
72         return true;
73 }
74
75 static bool
76 serial_chunk_compressor_get_chunk(struct chunk_compressor *_ctx,
77                                   const void **cdata_ret, unsigned *csize_ret,
78                                   unsigned *usize_ret)
79 {
80         struct serial_chunk_compressor *ctx = (struct serial_chunk_compressor*)_ctx;
81
82         if (ctx->ulen == 0)
83                 return false;
84
85         ctx->clen = compress_chunk(ctx->udata, ctx->ulen,
86                                    ctx->cdata, ctx->base.out_ctype,
87                                    ctx->comp_ctx);
88
89         if (ctx->clen) {
90                 *cdata_ret = ctx->cdata;
91                 *csize_ret = ctx->clen;
92         } else {
93                 *cdata_ret = ctx->udata;
94                 *csize_ret = ctx->ulen;
95         }
96         *usize_ret = ctx->ulen;
97
98         ctx->ulen = 0;
99         return true;
100 }
101
102 int
103 new_serial_chunk_compressor(int out_ctype, u32 out_chunk_size,
104                             struct wimlib_lzx_context *comp_ctx,
105                             struct chunk_compressor **compressor_ret)
106 {
107         struct serial_chunk_compressor *ctx;
108
109         ctx = CALLOC(1, sizeof(*ctx));
110         if (ctx == NULL)
111                 goto err;
112
113         ctx->base.out_ctype = out_ctype;
114         ctx->base.out_chunk_size = out_chunk_size;
115         ctx->base.num_threads = 1;
116         ctx->base.destroy = serial_chunk_compressor_destroy;
117         ctx->base.submit_chunk = serial_chunk_compressor_submit_chunk;
118         ctx->base.get_chunk = serial_chunk_compressor_get_chunk;
119
120         ctx->comp_ctx = comp_ctx;
121         ctx->udata = MALLOC(out_chunk_size);
122         ctx->cdata = MALLOC(out_chunk_size - 1);
123         ctx->ulen = 0;
124         if (ctx->udata == NULL || ctx->cdata == NULL)
125                 goto err;
126
127         *compressor_ret = &ctx->base;
128         return 0;
129
130 err:
131         serial_chunk_compressor_destroy(&ctx->base);
132         return WIMLIB_ERR_NOMEM;
133 }