]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/easy_encoder.c
Fixed a crash in liblzma.
[icculus/xz.git] / src / liblzma / common / easy_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       easy_encoder.c
4 /// \brief      Easy .xz Stream encoder initialization
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "easy_preset.h"
14 #include "stream_encoder.h"
15
16
17 struct lzma_coder_s {
18         lzma_next_coder stream_encoder;
19         lzma_options_easy opt_easy;
20 };
21
22
23 static lzma_ret
24 easy_encode(lzma_coder *coder, lzma_allocator *allocator,
25                 const uint8_t *restrict in, size_t *restrict in_pos,
26                 size_t in_size, uint8_t *restrict out,
27                 size_t *restrict out_pos, size_t out_size, lzma_action action)
28 {
29         return coder->stream_encoder.code(
30                         coder->stream_encoder.coder, allocator,
31                         in, in_pos, in_size, out, out_pos, out_size, action);
32 }
33
34
35 static void
36 easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
37 {
38         lzma_next_end(&coder->stream_encoder, allocator);
39         lzma_free(coder, allocator);
40         return;
41 }
42
43
44 static lzma_ret
45 easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
46                 uint32_t preset, lzma_check check)
47 {
48         lzma_next_coder_init(&easy_encoder_init, next, allocator);
49
50         if (next->coder == NULL) {
51                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
52                 if (next->coder == NULL)
53                         return LZMA_MEM_ERROR;
54
55                 next->code = &easy_encode;
56                 next->end = &easy_encoder_end;
57
58                 next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
59         }
60
61         if (lzma_easy_preset(&next->coder->opt_easy, preset))
62                 return LZMA_OPTIONS_ERROR;
63
64         return lzma_stream_encoder_init(&next->coder->stream_encoder,
65                         allocator, next->coder->opt_easy.filters, check);
66 }
67
68
69 extern LZMA_API(lzma_ret)
70 lzma_easy_encoder(lzma_stream *strm, uint32_t preset, lzma_check check)
71 {
72         lzma_next_strm_init(easy_encoder_init, strm, preset, check);
73
74         strm->internal->supported_actions[LZMA_RUN] = true;
75         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
76         strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
77         strm->internal->supported_actions[LZMA_FINISH] = true;
78
79         return LZMA_OK;
80 }