1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Easy Stream encoder initialization
6 // Copyright (C) 2008 Lasse Collin
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.
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.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "stream_encoder.h"
24 lzma_next_coder stream_encoder;
26 /// We need to keep the filters array available in case
27 /// LZMA_FULL_FLUSH is used.
28 lzma_options_filter filters[5];
33 easy_set_filters(lzma_options_filter *filters, uint32_t level)
38 // TODO FIXME Use Subblock or LZMA2 with no compression.
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;
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)
62 return coder->stream_encoder.code(
63 coder->stream_encoder.coder, allocator,
64 in, in_pos, in_size, out, out_pos, out_size, action);
69 easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
71 lzma_next_coder_end(&coder->stream_encoder, allocator);
72 lzma_free(coder, allocator);
78 easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
79 lzma_easy_level level)
81 if (next->coder == NULL) {
82 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
83 if (next->coder == NULL)
84 return LZMA_MEM_ERROR;
86 next->code = &easy_encode;
87 next->end = &easy_encoder_end;
89 next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
92 if (easy_set_filters(next->coder->filters, level))
93 return LZMA_HEADER_ERROR;
95 return lzma_stream_encoder_init(&next->coder->stream_encoder,
96 allocator, next->coder->filters, LZMA_CHECK_CRC32);
100 extern LZMA_API lzma_ret
101 lzma_easy_encoder(lzma_stream *strm, lzma_easy_level level)
103 lzma_next_strm_init(strm, easy_encoder_init, level);
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;
114 extern LZMA_API uint32_t
115 lzma_easy_memory_usage(lzma_easy_level level)
117 lzma_options_filter filters[5];
118 if (easy_set_filters(filters, level))
121 return lzma_memory_usage(filters, true);