]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/easy.c
Make physmem.h work on old Windows versions.
[icculus/xz.git] / src / liblzma / common / easy.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       easy.c
4 /// \brief      Easy 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 "stream_encoder.h"
21
22
23 struct lzma_coder_s {
24         lzma_next_coder stream_encoder;
25
26         /// Options for LZMA2
27         lzma_options_lzma opt_lzma;
28
29         /// We need to keep the filters array available in case
30         /// LZMA_FULL_FLUSH is used.
31         lzma_filter filters[LZMA_FILTERS_MAX + 1];
32 };
33
34
35 static bool
36 easy_set_filters(lzma_coder *coder, uint32_t preset)
37 {
38         if (lzma_lzma_preset(&coder->opt_lzma, preset))
39                 return true;
40
41         coder->filters[0].id = LZMA_FILTER_LZMA2;
42         coder->filters[0].options = &coder->opt_lzma;
43         coder->filters[1].id = LZMA_VLI_UNKNOWN;
44
45         return false;
46 }
47
48
49 static lzma_ret
50 easy_encode(lzma_coder *coder, lzma_allocator *allocator,
51                 const uint8_t *restrict in, size_t *restrict in_pos,
52                 size_t in_size, uint8_t *restrict out,
53                 size_t *restrict out_pos, size_t out_size, lzma_action action)
54 {
55         return coder->stream_encoder.code(
56                         coder->stream_encoder.coder, allocator,
57                         in, in_pos, in_size, out, out_pos, out_size, action);
58 }
59
60
61 static void
62 easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
63 {
64         lzma_next_end(&coder->stream_encoder, allocator);
65         lzma_free(coder, allocator);
66         return;
67 }
68
69
70 static lzma_ret
71 easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
72                 uint32_t preset, lzma_check check)
73 {
74         lzma_next_coder_init(easy_encoder_init, next, allocator);
75
76         if (next->coder == NULL) {
77                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
78                 if (next->coder == NULL)
79                         return LZMA_MEM_ERROR;
80
81                 next->code = &easy_encode;
82                 next->end = &easy_encoder_end;
83
84                 next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
85         }
86
87         if (easy_set_filters(next->coder, preset))
88                 return LZMA_OPTIONS_ERROR;
89
90         return lzma_stream_encoder_init(&next->coder->stream_encoder,
91                         allocator, next->coder->filters, check);
92 }
93
94
95 extern LZMA_API(lzma_ret)
96 lzma_easy_encoder(lzma_stream *strm, uint32_t preset, lzma_check check)
97 {
98         lzma_next_strm_init(easy_encoder_init, strm, preset, check);
99
100         strm->internal->supported_actions[LZMA_RUN] = true;
101         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
102         strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
103         strm->internal->supported_actions[LZMA_FINISH] = true;
104
105         return LZMA_OK;
106 }
107
108
109 extern LZMA_API(uint64_t)
110 lzma_easy_encoder_memusage(uint32_t preset)
111 {
112         lzma_coder coder;
113         if (easy_set_filters(&coder, preset))
114                 return UINT32_MAX;
115
116         return lzma_raw_encoder_memusage(coder.filters);
117 }
118
119
120 extern LZMA_API(uint64_t)
121 lzma_easy_decoder_memusage(uint32_t preset)
122 {
123         lzma_coder coder;
124         if (easy_set_filters(&coder, preset))
125                 return UINT32_MAX;
126
127         return lzma_raw_decoder_memusage(coder.filters);
128 }