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"
24 #define ALONE_HEADER_SIZE (1 + 4 + 8)
36 uint8_t header[ALONE_HEADER_SIZE];
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 bufcpy(coder->header, &coder->header_pos,
53 out, out_pos, out_size);
54 if (coder->header_pos < ALONE_HEADER_SIZE)
57 coder->sequence = SEQ_CODE;
61 return coder->next.code(coder->next.coder,
62 allocator, in, in_pos, in_size,
63 out, out_pos, out_size, action);
67 return LZMA_PROG_ERROR;
75 alone_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
77 lzma_next_coder_end(&coder->next, allocator);
78 lzma_free(coder, allocator);
83 // At least for now, this is not used by any internal function.
85 alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
86 const lzma_options_lzma *options)
88 if (next->coder == NULL) {
89 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
90 if (next->coder == NULL)
91 return LZMA_MEM_ERROR;
93 next->code = &alone_encode;
94 next->end = &alone_encoder_end;
95 next->coder->next = LZMA_NEXT_CODER_INIT;
98 // Basic initializations
99 next->coder->sequence = SEQ_HEADER;
100 next->coder->header_pos = 0;
102 // Encode the header:
103 // - Properties (1 byte)
104 if (lzma_lzma_encode_properties(options, next->coder->header))
105 return LZMA_PROG_ERROR;
107 // - Dictionary size (4 bytes)
108 if (options->dictionary_size < LZMA_DICTIONARY_SIZE_MIN
109 || options->dictionary_size > LZMA_DICTIONARY_SIZE_MAX)
110 return LZMA_PROG_ERROR;
112 // Round up to to the next 2^n or 2^n + 2^(n - 1) depending on which
113 // one is the next. While the header would allow any 32-bit integer,
114 // we do this to keep the decoder of liblzma accepting the resulting
116 uint32_t d = options->dictionary_size - 1;
124 integer_write_32(next->coder->header + 1, d);
126 // - Uncompressed size (always unknown and using EOPM)
127 memset(next->coder->header + 1 + 4, 0xFF, 8);
129 // Initialize the LZMA encoder.
130 const lzma_filter_info filters[2] = {
132 .init = &lzma_lzma_encoder_init,
133 .options = (void *)(options),
139 return lzma_next_filter_init(&next->coder->next, allocator, filters);
145 lzma_alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
146 const lzma_options_alone *options)
148 lzma_next_coder_init(alone_encoder_init, next, allocator, options);
153 extern LZMA_API lzma_ret
154 lzma_alone_encoder(lzma_stream *strm, const lzma_options_lzma *options)
156 lzma_next_strm_init(strm, alone_encoder_init, options);
158 strm->internal->supported_actions[LZMA_RUN] = true;
159 strm->internal->supported_actions[LZMA_FINISH] = true;