]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/easy.c
Update the code to mostly match the new simpler file format
[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         /// We need to keep the filters array available in case
27         /// LZMA_FULL_FLUSH is used.
28         lzma_options_filter filters[5];
29 };
30
31
32 static bool
33 easy_set_filters(lzma_options_filter *filters, uint32_t level)
34 {
35         bool error = false;
36
37         if (level == 0) {
38                 // TODO FIXME Use Subblock or LZMA2 with no compression.
39                 error = true;
40
41 #ifdef HAVE_FILTER_LZMA
42         } else if (level <= 9) {
43                 filters[0].id = LZMA_FILTER_LZMA;
44                 filters[0].options = (void *)(&lzma_preset_lzma[level - 1]);
45                 filters[1].id = LZMA_VLI_VALUE_UNKNOWN;
46 #endif
47
48         } else {
49                 error = true;
50         }
51
52         return error;
53 }
54
55
56 static lzma_ret
57 easy_encode(lzma_coder *coder, lzma_allocator *allocator,
58                 const uint8_t *restrict in, size_t *restrict in_pos,
59                 size_t in_size, uint8_t *restrict out,
60                 size_t *restrict out_pos, size_t out_size, lzma_action action)
61 {
62         return coder->stream_encoder.code(
63                         coder->stream_encoder.coder, allocator,
64                         in, in_pos, in_size, out, out_pos, out_size, action);
65 }
66
67
68 static void
69 easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
70 {
71         lzma_next_coder_end(&coder->stream_encoder, allocator);
72         lzma_free(coder, allocator);
73         return;
74 }
75
76
77 static lzma_ret
78 easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
79                 lzma_easy_level level)
80 {
81         if (next->coder == NULL) {
82                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
83                 if (next->coder == NULL)
84                         return LZMA_MEM_ERROR;
85
86                 next->code = &easy_encode;
87                 next->end = &easy_encoder_end;
88
89                 next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
90         }
91
92         if (easy_set_filters(next->coder->filters, level))
93                 return LZMA_HEADER_ERROR;
94
95         return lzma_stream_encoder_init(&next->coder->stream_encoder,
96                         allocator, next->coder->filters, LZMA_CHECK_CRC32);
97 }
98
99
100 extern LZMA_API lzma_ret
101 lzma_easy_encoder(lzma_stream *strm, lzma_easy_level level)
102 {
103         lzma_next_strm_init(strm, easy_encoder_init, level);
104
105         strm->internal->supported_actions[LZMA_RUN] = true;
106         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
107         strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
108         strm->internal->supported_actions[LZMA_FINISH] = true;
109
110         return LZMA_OK;
111 }
112
113
114 extern LZMA_API uint32_t
115 lzma_easy_memory_usage(lzma_easy_level level)
116 {
117         lzma_options_filter filters[5];
118         if (easy_set_filters(filters, level))
119                 return UINT32_MAX;
120
121         return lzma_memory_usage(filters, true);
122 }