1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file alone_decoder.c
4 /// \brief Decoder for LZMA_Alone files
6 // Copyright (C) 2007 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 ///////////////////////////////////////////////////////////////////////////////
21 #include "lzma_encoder.h"
30 SEQ_UNCOMPRESSED_SIZE,
36 lzma_options_alone options;
41 alone_encode(lzma_coder *coder,
42 lzma_allocator *allocator lzma_attribute((unused)),
43 const uint8_t *restrict in, size_t *restrict in_pos,
44 size_t in_size, uint8_t *restrict out,
45 size_t *restrict out_pos, size_t out_size,
48 while (*out_pos < out_size)
49 switch (coder->sequence) {
51 if (lzma_lzma_encode_properties(
52 &coder->options.lzma, out + *out_pos)) {
53 return LZMA_PROG_ERROR;
56 coder->sequence = SEQ_DICTIONARY_SIZE;
60 case SEQ_DICTIONARY_SIZE:
61 out[*out_pos] = coder->options.lzma.dictionary_size
64 if (++coder->pos == 4) {
66 coder->sequence = SEQ_UNCOMPRESSED_SIZE;
72 case SEQ_UNCOMPRESSED_SIZE:
73 out[*out_pos] = coder->options.uncompressed_size
76 if (++coder->pos == 8) {
78 coder->sequence = SEQ_CODE;
85 return coder->next.code(coder->next.coder,
86 allocator, in, in_pos, in_size,
87 out, out_pos, out_size, action);
91 return LZMA_PROG_ERROR;
99 alone_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
101 lzma_next_coder_end(&coder->next, allocator);
102 lzma_free(coder, allocator);
107 // At least for now, this is not used by any internal function.
109 alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
110 const lzma_options_alone *options)
112 if (next->coder == NULL) {
113 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
114 if (next->coder == NULL)
115 return LZMA_MEM_ERROR;
117 next->code = &alone_encode;
118 next->end = &alone_encoder_end;
119 next->coder->next = LZMA_NEXT_CODER_INIT;
122 // Initialize the LZMA_Alone coder variables.
123 next->coder->sequence = SEQ_PROPERTIES;
124 next->coder->pos = 0;
125 next->coder->options = *options;
127 // Verify uncompressed_size since the other functions assume that
129 if (!lzma_vli_is_valid(next->coder->options.uncompressed_size))
130 return LZMA_PROG_ERROR;
132 // Initialize the LZMA encoder.
133 const lzma_filter_info filters[2] = {
135 .init = &lzma_lzma_encoder_init,
136 .options = &next->coder->options.lzma,
137 .uncompressed_size = next->coder->options
144 return lzma_next_filter_init(&next->coder->next, allocator, filters);
150 lzma_alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
151 const lzma_options_alone *options)
153 lzma_next_coder_init(alone_encoder_init, next, allocator, options);
158 extern LZMA_API lzma_ret
159 lzma_alone_encoder(lzma_stream *strm, const lzma_options_alone *options)
161 lzma_next_strm_init(strm, alone_encoder_init, options);
163 strm->internal->supported_actions[LZMA_RUN] = true;
164 strm->internal->supported_actions[LZMA_FINISH] = true;