]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/easy.c
The LZMA2 decoder fix introduced a bug to LZ decoder,
[icculus/xz.git] / src / liblzma / common / easy.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       easy.c
4 /// \brief      Easy Stream encoder initialization
5 //
6 //  Copyright (C) 2008 Lasse Collin
7 //
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "stream_encoder.h"
21
22
23 struct lzma_coder_s {
24         lzma_next_coder stream_encoder;
25
26         /// Options for LZMA2
27         lzma_options_lzma opt_lzma;
28
29         /// We need to keep the filters array available in case
30         /// LZMA_FULL_FLUSH is used.
31         lzma_filter filters[5];
32 };
33
34
35 static bool
36 easy_set_filters(lzma_coder *coder, uint32_t level)
37 {
38         bool error = false;
39
40         if (level == 0) {
41                 // TODO FIXME Use Subblock or LZMA2 with no compression.
42                 error = true;
43
44 #ifdef HAVE_ENCODER_LZMA2
45         } else if (level <= 9) {
46                 error = lzma_lzma_preset(&coder->opt_lzma, level - 1);
47                 coder->filters[0].id = LZMA_FILTER_LZMA2;
48                 coder->filters[0].options = &coder->opt_lzma;
49                 coder->filters[1].id = LZMA_VLI_UNKNOWN;
50 #endif
51
52         } else {
53                 error = true;
54         }
55
56         return error;
57 }
58
59
60 static lzma_ret
61 easy_encode(lzma_coder *coder, lzma_allocator *allocator,
62                 const uint8_t *restrict in, size_t *restrict in_pos,
63                 size_t in_size, uint8_t *restrict out,
64                 size_t *restrict out_pos, size_t out_size, lzma_action action)
65 {
66         return coder->stream_encoder.code(
67                         coder->stream_encoder.coder, allocator,
68                         in, in_pos, in_size, out, out_pos, out_size, action);
69 }
70
71
72 static void
73 easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
74 {
75         lzma_next_end(&coder->stream_encoder, allocator);
76         lzma_free(coder, allocator);
77         return;
78 }
79
80
81 static lzma_ret
82 easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
83                 lzma_easy_level level)
84 {
85         lzma_next_coder_init(easy_encoder_init, next, allocator);
86
87         if (next->coder == NULL) {
88                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
89                 if (next->coder == NULL)
90                         return LZMA_MEM_ERROR;
91
92                 next->code = &easy_encode;
93                 next->end = &easy_encoder_end;
94
95                 next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
96         }
97
98         if (easy_set_filters(next->coder, level))
99                 return LZMA_OPTIONS_ERROR;
100
101         return lzma_stream_encoder_init(&next->coder->stream_encoder,
102                         allocator, next->coder->filters, LZMA_CHECK_CRC32);
103 }
104
105
106 extern LZMA_API lzma_ret
107 lzma_easy_encoder(lzma_stream *strm, lzma_easy_level level)
108 {
109         lzma_next_strm_init(easy_encoder_init, strm, level);
110
111         strm->internal->supported_actions[LZMA_RUN] = true;
112         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
113         strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
114         strm->internal->supported_actions[LZMA_FINISH] = true;
115
116         return LZMA_OK;
117 }
118
119
120 extern LZMA_API uint64_t
121 lzma_easy_memory_usage(lzma_easy_level level)
122 {
123         lzma_coder coder;
124         if (easy_set_filters(&coder, level))
125                 return UINT32_MAX;
126
127         return lzma_memusage_encoder(coder.filters);
128 }