]> wimlib.net Git - wimlib/blob - src/lzms-compress.c
streamifier_cb(): Fix update of cur_stream_offset
[wimlib] / src / lzms-compress.c
1 /*
2  * lzms-compress.c
3  *
4  * A compressor for the LZMS compression format.
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
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later 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
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 /* This a compressor for the LZMS compression format.  More details about this
27  * format can be found in lzms-decompress.c.  */
28
29 #ifdef HAVE_CONFIG_H
30 #  include "config.h"
31 #endif
32
33 #include "wimlib.h"
34 #include "wimlib/assert.h"
35 #include "wimlib/compressor_ops.h"
36 #include "wimlib/compress_common.h"
37 #include "wimlib/error.h"
38 #include "wimlib/lzms.h"
39 #include "wimlib/util.h"
40
41 #include <string.h>
42
43 struct lzms_compressor {
44         u8 *window;
45         u32 window_size;
46         u32 max_block_size;
47
48         s32 *last_target_usages;
49 };
50
51 static void
52 lzms_preprocess_data(u8 *data, s32 size, s32 *last_target_usages)
53 {
54         for (s32 i = 0; i < size - 11; i++) {
55         }
56 }
57
58 static size_t
59 lzms_compress(const void *uncompressed_data, size_t uncompressed_size,
60               void *compressed_data, size_t compressed_size_avail, void *_ctx)
61 {
62         struct lzms_compressor *ctx = _ctx;
63
64         if (uncompressed_size > ctx->max_block_size) {
65                 LZMS_DEBUG("Can't compress %su bytes: LZMS context "
66                            "only supports %u bytes",
67                            uncompressed_size, ctx->max_block_size);
68                 return 0;
69         }
70
71         memcpy(ctx->window, uncompressed_data, uncompressed_size);
72         ctx->window_size = uncompressed_size;
73
74         lzms_preprocess_data(ctx->window, ctx->window_size,
75                              ctx->last_target_usages);
76
77         return 0;
78 }
79
80 static void
81 lzms_free_compressor(void *_ctx)
82 {
83         struct lzms_compressor *ctx = _ctx;
84
85         if (ctx) {
86                 FREE(ctx->window);
87                 FREE(ctx->last_target_usages);
88                 FREE(ctx);
89         }
90 }
91
92 static int
93 lzms_create_compressor(size_t max_block_size,
94                        const struct wimlib_compressor_params_header *params,
95                        void **ctx_ret)
96 {
97         struct lzms_compressor *ctx;
98
99         if (max_block_size == 0 || max_block_size > 1U << 26) {
100                 LZMS_DEBUG("Invalid max_block_size (%u)", max_block_size);
101                 return WIMLIB_ERR_INVALID_PARAM;
102         }
103
104         ctx = CALLOC(1, sizeof(struct lzms_compressor));
105         if (ctx == NULL)
106                 goto oom;
107
108         ctx->window = MALLOC(max_block_size);
109         if (ctx->window == NULL)
110                 goto oom;
111         ctx->max_block_size = max_block_size;
112
113         ctx->last_target_usages = MALLOC(65536 * sizeof(ctx->last_target_usages[0]));
114         if (ctx->last_target_usages == NULL)
115                 goto oom;
116
117         *ctx_ret = ctx;
118         return 0;
119
120 oom:
121         lzms_free_compressor(ctx);
122         return WIMLIB_ERR_NOMEM;
123 }
124
125 const struct compressor_ops lzms_compressor_ops = {
126         .create_compressor  = lzms_create_compressor,
127         .compress           = lzms_compress,
128         .free_compressor    = lzms_free_compressor,
129 };