]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/easy.c
Renamed constants:
[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_filter filters[5];
29 };
30
31
32 static bool
33 easy_set_filters(lzma_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_ENCODER_LZMA2
42         } else if (level <= 9) {
43                 filters[0].id = LZMA_FILTER_LZMA2;
44                 filters[0].options = (void *)(&lzma_preset_lzma[level - 1]);
45                 filters[1].id = LZMA_VLI_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_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         lzma_next_coder_init(easy_encoder_init, next, allocator);
82
83         if (next->coder == NULL) {
84                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
85                 if (next->coder == NULL)
86                         return LZMA_MEM_ERROR;
87
88                 next->code = &easy_encode;
89                 next->end = &easy_encoder_end;
90
91                 next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
92         }
93
94         if (easy_set_filters(next->coder->filters, level))
95                 return LZMA_OPTIONS_ERROR;
96
97         return lzma_stream_encoder_init(&next->coder->stream_encoder,
98                         allocator, next->coder->filters, LZMA_CHECK_CRC32);
99 }
100
101
102 extern LZMA_API lzma_ret
103 lzma_easy_encoder(lzma_stream *strm, lzma_easy_level level)
104 {
105         lzma_next_strm_init(easy_encoder_init, strm, level);
106
107         strm->internal->supported_actions[LZMA_RUN] = true;
108         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
109         strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
110         strm->internal->supported_actions[LZMA_FINISH] = true;
111
112         return LZMA_OK;
113 }
114
115
116 extern LZMA_API uint64_t
117 lzma_easy_memory_usage(lzma_easy_level level)
118 {
119         lzma_filter filters[5];
120         if (easy_set_filters(filters, level))
121                 return UINT32_MAX;
122
123         return lzma_memusage_encoder(filters);
124 }