]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/alone_encoder.c
429fbbf0426d9339ce94ed2889b43b32abd583f6
[icculus/xz.git] / src / liblzma / common / alone_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       alone_decoder.c
4 /// \brief      Decoder for LZMA_Alone files
5 //
6 //  Copyright (C) 2007 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 "common.h"
21 #include "lzma_encoder.h"
22
23
24 #define ALONE_HEADER_SIZE (1 + 4 + 8)
25
26
27 struct lzma_coder_s {
28         lzma_next_coder next;
29
30         enum {
31                 SEQ_HEADER,
32                 SEQ_CODE,
33         } sequence;
34
35         size_t header_pos;
36         uint8_t header[ALONE_HEADER_SIZE];
37 };
38
39
40 static lzma_ret
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,
46                 lzma_action action)
47 {
48         while (*out_pos < out_size)
49         switch (coder->sequence) {
50         case SEQ_HEADER:
51                 lzma_bufcpy(coder->header, &coder->header_pos,
52                                 ALONE_HEADER_SIZE,
53                                 out, out_pos, out_size);
54                 if (coder->header_pos < ALONE_HEADER_SIZE)
55                         return LZMA_OK;
56
57                 coder->sequence = SEQ_CODE;
58                 break;
59
60         case SEQ_CODE:
61                 return coder->next.code(coder->next.coder,
62                                 allocator, in, in_pos, in_size,
63                                 out, out_pos, out_size, action);
64
65         default:
66                 assert(0);
67                 return LZMA_PROG_ERROR;
68         }
69
70         return LZMA_OK;
71 }
72
73
74 static void
75 alone_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
76 {
77         lzma_next_end(&coder->next, allocator);
78         lzma_free(coder, allocator);
79         return;
80 }
81
82
83 // At least for now, this is not used by any internal function.
84 static lzma_ret
85 alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
86                 const lzma_options_lzma *options)
87 {
88         lzma_next_coder_init(alone_encoder_init, next, allocator);
89
90         if (next->coder == NULL) {
91                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
92                 if (next->coder == NULL)
93                         return LZMA_MEM_ERROR;
94
95                 next->code = &alone_encode;
96                 next->end = &alone_encoder_end;
97                 next->coder->next = LZMA_NEXT_CODER_INIT;
98         }
99
100         // Basic initializations
101         next->coder->sequence = SEQ_HEADER;
102         next->coder->header_pos = 0;
103
104         // Encode the header:
105         // - Properties (1 byte)
106         if (lzma_lzma_lclppb_encode(options, next->coder->header))
107                 return LZMA_OPTIONS_ERROR;
108
109         // - Dictionary size (4 bytes)
110         if (options->dict_size < LZMA_DICT_SIZE_MIN)
111                 return LZMA_OPTIONS_ERROR;
112
113         // Round up to to the next 2^n or 2^n + 2^(n - 1) depending on which
114         // one is the next unless it is UINT32_MAX. While the header would
115         // allow any 32-bit integer, we do this to keep the decoder of liblzma
116         // accepting the resulting files.
117         uint32_t d = options->dict_size - 1;
118         d |= d >> 2;
119         d |= d >> 3;
120         d |= d >> 4;
121         d |= d >> 8;
122         d |= d >> 16;
123         if (d != UINT32_MAX)
124                 ++d;
125
126         integer_write_32(next->coder->header + 1, d);
127
128         // - Uncompressed size (always unknown and using EOPM)
129         memset(next->coder->header + 1 + 4, 0xFF, 8);
130
131         // Initialize the LZMA encoder.
132         const lzma_filter_info filters[2] = {
133                 {
134                         .init = &lzma_lzma_encoder_init,
135                         .options = (void *)(options),
136                 }, {
137                         .init = NULL,
138                 }
139         };
140
141         return lzma_next_filter_init(&next->coder->next, allocator, filters);
142 }
143
144
145 /*
146 extern lzma_ret
147 lzma_alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
148                 const lzma_options_alone *options)
149 {
150         lzma_next_coder_init(alone_encoder_init, next, allocator, options);
151 }
152 */
153
154
155 extern LZMA_API(lzma_ret)
156 lzma_alone_encoder(lzma_stream *strm, const lzma_options_lzma *options)
157 {
158         lzma_next_strm_init(alone_encoder_init, strm, options);
159
160         strm->internal->supported_actions[LZMA_RUN] = true;
161         strm->internal->supported_actions[LZMA_FINISH] = true;
162
163         return LZMA_OK;
164 }