]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/lz/lz_encoder.h
Major changes to LZ encoder, LZMA encoder, and range encoder.
[icculus/xz.git] / src / liblzma / lz / lz_encoder.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       lz_encoder.h
4 /// \brief      LZ in window and match finder API
5 //
6 //  Copyright (C) 1999-2006 Igor Pavlov
7 //  Copyright (C) 2007 Lasse Collin
8 //
9 //  This library is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU Lesser General Public
11 //  License as published by the Free Software Foundation; either
12 //  version 2.1 of the License, or (at your option) any later version.
13 //
14 //  This library is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 //  Lesser General Public License for more details.
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20
21 #ifndef LZMA_LZ_ENCODER_H
22 #define LZMA_LZ_ENCODER_H
23
24 #include "common.h"
25
26
27 #define LZMA_LZ_TEMP_SIZE 64
28
29
30 typedef struct lzma_lz_encoder_s lzma_lz_encoder;
31 struct lzma_lz_encoder_s {
32         enum {
33                 SEQ_RUN,
34                 SEQ_FLUSH,
35                 SEQ_FLUSH_END,
36                 SEQ_FINISH,
37                 SEQ_END
38         } sequence;
39
40         bool (*process)(lzma_coder *coder, uint8_t *restrict out,
41                         size_t *restrict out_pos, size_t out_size);
42
43         /// Uncompressed Size or LZMA_VLI_VALUE_UNKNOWN if using EOPM. We need
44         /// to track Uncompressed Size to prevent writing flush marker to the
45         /// very end of stream that doesn't use EOPM.
46         lzma_vli uncompressed_size;
47
48         /// Temporary buffer for range encoder.
49         uint8_t temp[LZMA_LZ_TEMP_SIZE];
50         size_t temp_size;
51
52         ///////////////
53         // In Window //
54         ///////////////
55
56         /// Pointer to buffer with data to be compressed
57         uint8_t *buffer;
58
59         /// Total size of the allocated buffer (that is, including all
60         /// the extra space)
61         size_t size;
62
63         /// Match finders store locations of matches using 32-bit integers.
64         /// To avoid adjusting several megabytes of integers every time the
65         /// input window is moved with move_window(), we only adjust the
66         /// offset of the buffer. Thus, buffer[match_finder_pos - offset]
67         /// is the byte pointed by match_finder_pos.
68         size_t offset;
69
70         /// buffer[read_pos] is the current byte.
71         size_t read_pos;
72
73         /// As long as read_pos is less than read_limit, there is enough
74         /// input available in buffer for at least one encoding loop.
75         ///
76         /// Because of the stateful API, read_limit may and will get greater
77         /// than read_pos quite often. This is taken into account when
78         /// calculating the value for keep_size_after.
79         size_t read_limit;
80
81         /// buffer[write_pos] is the first byte that doesn't contain valid
82         /// uncompressed data; that is, the next input byte will be copied
83         /// to buffer[write_pos].
84         size_t write_pos;
85
86         /// Number of bytes that must be kept available in our input history.
87         /// That is, once keep_size_before bytes have been processed,
88         /// buffer[read_pos - keep_size_before] is the oldest byte that
89         /// must be available for reading.
90         size_t keep_size_before;
91
92         /// Number of bytes that must be kept in buffer after read_pos.
93         /// That is, read_pos <= write_pos - keep_size_after as long as
94         /// stream_end_was_reached is false (once it is true, read_pos
95         /// is allowed to reach write_pos).
96         size_t keep_size_after;
97
98         //////////////////
99         // Match Finder //
100         //////////////////
101
102         // Pointers to match finder functions
103         void (*get_matches)(lzma_lz_encoder *restrict lz,
104                         uint32_t *restrict distances);
105         void (*skip)(lzma_lz_encoder *restrict lz, uint32_t num);
106
107         // Match finder data
108         uint32_t *hash; // TODO: Check if hash aliases son
109         uint32_t *son;  //       and add 'restrict' if possible.
110         uint32_t cyclic_buffer_pos;
111         uint32_t cyclic_buffer_size; // Must be dictionary_size + 1.
112         uint32_t hash_mask;
113         uint32_t cut_value;
114         uint32_t hash_size_sum;
115         uint32_t num_items;
116         uint32_t match_max_len;
117 };
118
119
120 #define LZMA_LZ_ENCODER_INIT \
121         (lzma_lz_encoder){ \
122                 .buffer = NULL, \
123                 .size = 0, \
124                 .hash = NULL, \
125                 .num_items = 0, \
126         }
127
128
129 /// Calculates
130 extern uint32_t lzma_lz_encoder_hash_properties(lzma_match_finder match_finder,
131                 uint32_t history_size, uint32_t *restrict hash_mask,
132                 uint32_t *restrict hash_size_sum,
133                 uint32_t *restrict num_items);
134
135 // NOTE: liblzma doesn't use callback API like LZMA SDK does. The caller
136 // must make sure that keep_size_after is big enough for single encoding pass
137 // i.e. keep_size_after >= maximum number of bytes possibly needed after
138 // the current position between calls to lzma_lz_read().
139 extern lzma_ret lzma_lz_encoder_reset(lzma_lz_encoder *lz,
140                 lzma_allocator *allocator,
141                 bool (*process)(lzma_coder *coder, uint8_t *restrict out,
142                         size_t *restrict out_pos, size_t out_size),
143                 lzma_vli uncompressed_size,
144                 size_t history_size, size_t additional_buffer_before,
145                 size_t match_max_len, size_t additional_buffer_after,
146                 lzma_match_finder match_finder, uint32_t match_finder_cycles,
147                 const uint8_t *preset_dictionary,
148                 size_t preset_dictionary_size);
149
150 /// Frees memory allocated for in window and match finder buffers.
151 extern void lzma_lz_encoder_end(
152                 lzma_lz_encoder *lz, lzma_allocator *allocator);
153
154 extern lzma_ret lzma_lz_encode(lzma_coder *coder,
155                 lzma_allocator *allocator lzma_attribute((unused)),
156                 const uint8_t *restrict in, size_t *restrict in_pos,
157                 size_t in_size, uint8_t *restrict out,
158                 size_t *restrict out_pos, size_t out_size,
159                 lzma_action action);
160
161 /// This should not be called directly, but only via move_pos() macro.
162 extern void lzma_lz_encoder_normalize(lzma_lz_encoder *lz);
163
164 #endif