]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/alone_encoder.c
Imported to git.
[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 struct lzma_coder_s {
25         lzma_next_coder next;
26
27         enum {
28                 SEQ_PROPERTIES,
29                 SEQ_DICTIONARY_SIZE,
30                 SEQ_UNCOMPRESSED_SIZE,
31                 SEQ_CODE,
32         } sequence;
33
34         size_t pos;
35
36         lzma_options_alone options;
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_PROPERTIES:
51                 if (lzma_lzma_encode_properties(
52                                 &coder->options.lzma, out + *out_pos)) {
53                         return LZMA_PROG_ERROR;
54                 }
55
56                 coder->sequence = SEQ_DICTIONARY_SIZE;
57                 ++*out_pos;
58                 break;
59
60         case SEQ_DICTIONARY_SIZE:
61                 out[*out_pos] = coder->options.lzma.dictionary_size
62                                 >> (coder->pos * 8);
63
64                 if (++coder->pos == 4) {
65                         coder->pos = 0;
66                         coder->sequence = SEQ_UNCOMPRESSED_SIZE;
67                 }
68
69                 ++*out_pos;
70                 break;
71
72         case SEQ_UNCOMPRESSED_SIZE:
73                 out[*out_pos] = coder->options.uncompressed_size
74                                 >> (coder->pos * 8);
75
76                 if (++coder->pos == 8) {
77                         coder->pos = 0;
78                         coder->sequence = SEQ_CODE;
79                 }
80
81                 ++*out_pos;
82                 break;
83
84         case SEQ_CODE: {
85                 return coder->next.code(coder->next.coder,
86                                 allocator, in, in_pos, in_size,
87                                 out, out_pos, out_size, action);
88         }
89
90         default:
91                 return LZMA_PROG_ERROR;
92         }
93
94         return LZMA_OK;
95 }
96
97
98 static void
99 alone_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
100 {
101         lzma_next_coder_end(&coder->next, allocator);
102         lzma_free(coder, allocator);
103         return;
104 }
105
106
107 // At least for now, this is not used by any internal function.
108 static lzma_ret
109 alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
110                 const lzma_options_alone *options)
111 {
112         if (next->coder == NULL) {
113                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
114                 if (next->coder == NULL)
115                         return LZMA_MEM_ERROR;
116
117                 next->code = &alone_encode;
118                 next->end = &alone_encoder_end;
119                 next->coder->next = LZMA_NEXT_CODER_INIT;
120         }
121
122         // Initialize the LZMA_Alone coder variables.
123         next->coder->sequence = SEQ_PROPERTIES;
124         next->coder->pos = 0;
125         next->coder->options = *options;
126
127         // Verify uncompressed_size since the other functions assume that
128         // it is valid.
129         if (!lzma_vli_is_valid(next->coder->options.uncompressed_size))
130                 return LZMA_PROG_ERROR;
131
132         // Initialize the LZMA encoder.
133         const lzma_filter_info filters[2] = {
134                 {
135                         .init = &lzma_lzma_encoder_init,
136                         .options = &next->coder->options.lzma,
137                         .uncompressed_size = next->coder->options
138                                         .uncompressed_size,
139                 }, {
140                         .init = NULL,
141                 }
142         };
143
144         return lzma_next_filter_init(&next->coder->next, allocator, filters);
145 }
146
147
148 /*
149 extern lzma_ret
150 lzma_alone_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
151                 const lzma_options_alone *options)
152 {
153         lzma_next_coder_init(alone_encoder_init, next, allocator, options);
154 }
155 */
156
157
158 extern LZMA_API lzma_ret
159 lzma_alone_encoder(lzma_stream *strm, const lzma_options_alone *options)
160 {
161         lzma_next_strm_init(strm, alone_encoder_init, options);
162
163         strm->internal->supported_actions[LZMA_RUN] = true;
164         strm->internal->supported_actions[LZMA_FINISH] = true;
165
166         return LZMA_OK;
167 }