]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/easy_multi.c
Removed src/liblzma/common/sysdefs.h symlink, which was
[icculus/xz.git] / src / liblzma / common / easy_multi.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       easy_multi.c
4 /// \brief      Easy Multi-Block Stream encoder initialization
5 //
6 //  Copyright (C) 2008 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 "easy_common.h"
21 #include "stream_encoder_multi.h"
22
23
24 struct lzma_coder_s {
25         lzma_next_coder encoder;
26         lzma_options_stream options;
27 };
28
29
30 static lzma_ret
31 easy_encode(lzma_coder *coder, lzma_allocator *allocator,
32                 const uint8_t *restrict in, size_t *restrict in_pos,
33                 size_t in_size, uint8_t *restrict out,
34                 size_t *restrict out_pos, size_t out_size, lzma_action action)
35 {
36         return coder->encoder.code(coder->encoder.coder, allocator,
37                         in, in_pos, in_size, out, out_pos, out_size, action);
38 }
39
40
41 static void
42 easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
43 {
44         lzma_next_coder_end(&coder->encoder, allocator);
45         lzma_free(coder, allocator);
46         return;
47 }
48
49
50 static lzma_ret
51 easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
52                 lzma_easy_level level, lzma_easy_level metadata_level,
53                 const lzma_extra *header, const lzma_extra *footer)
54 {
55         if (next->coder == NULL) {
56                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
57                 if (next->coder == NULL)
58                         return LZMA_MEM_ERROR;
59
60                 next->code = &easy_encode;
61                 next->end = &easy_encoder_end;
62
63                 next->coder->encoder = LZMA_NEXT_CODER_INIT;
64         }
65
66         next->coder->options = (lzma_options_stream){
67                 .check = LZMA_CHECK_CRC32,
68                 .has_crc32 = true,
69                 .uncompressed_size = LZMA_VLI_VALUE_UNKNOWN,
70                 .alignment = 0,
71                 .header = header,
72                 .footer = footer,
73         };
74
75         if (lzma_easy_set_filters(next->coder->options.filters, level)
76                         || lzma_easy_set_filters(
77                                 next->coder->options.metadata_filters,
78                                 metadata_level))
79                 return LZMA_HEADER_ERROR;
80
81         return lzma_stream_encoder_multi_init(&next->coder->encoder,
82                         allocator, &next->coder->options);
83 }
84
85
86 extern LZMA_API lzma_ret
87 lzma_easy_encoder_multi(lzma_stream *strm,
88                 lzma_easy_level level, lzma_easy_level metadata_level,
89                 const lzma_extra *header, const lzma_extra *footer)
90 {
91         // This is more complicated than lzma_easy_encoder_single(),
92         // because lzma_stream_encoder_multi() wants that the options
93         // structure is available until the encoding is finished.
94         lzma_next_strm_init(strm, easy_encoder_init,
95                         level, metadata_level, header, footer);
96
97         strm->internal->supported_actions[LZMA_RUN] = true;
98         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
99         strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
100         strm->internal->supported_actions[LZMA_FINISH] = true;
101
102         return LZMA_OK;
103 }