1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief LZ in window and match finder API
6 // Copyright (C) 1999-2006 Igor Pavlov
7 // Copyright (C) 2007 Lasse Collin
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.
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.
19 ///////////////////////////////////////////////////////////////////////////////
21 #ifndef LZMA_LZ_ENCODER_H
22 #define LZMA_LZ_ENCODER_H
27 typedef struct lzma_lz_encoder_s lzma_lz_encoder;
28 struct lzma_lz_encoder_s {
35 /// Function to do the actual encoding from the sliding input window
36 /// to the output stream.
37 bool (*process)(lzma_coder *coder, uint8_t *restrict out,
38 size_t *restrict out_pos, size_t out_size);
44 /// Pointer to buffer with data to be compressed
47 /// Total size of the allocated buffer (that is, including all
51 /// Match finders store locations of matches using 32-bit integers.
52 /// To avoid adjusting several megabytes of integers every time the
53 /// input window is moved with move_window(), we only adjust the
54 /// offset of the buffer. Thus, buffer[match_finder_pos - offset]
55 /// is the byte pointed by match_finder_pos.
58 /// buffer[read_pos] is the current byte.
61 /// As long as read_pos is less than read_limit, there is enough
62 /// input available in buffer for at least one encoding loop.
64 /// Because of the stateful API, read_limit may and will get greater
65 /// than read_pos quite often. This is taken into account when
66 /// calculating the value for keep_size_after.
69 /// buffer[write_pos] is the first byte that doesn't contain valid
70 /// uncompressed data; that is, the next input byte will be copied
71 /// to buffer[write_pos].
74 /// Number of bytes not hashed before read_pos. This is needed to
75 /// restart the match finder after LZMA_SYNC_FLUSH.
78 /// Number of bytes that must be kept available in our input history.
79 /// That is, once keep_size_before bytes have been processed,
80 /// buffer[read_pos - keep_size_before] is the oldest byte that
81 /// must be available for reading.
82 size_t keep_size_before;
84 /// Number of bytes that must be kept in buffer after read_pos.
85 /// That is, read_pos <= write_pos - keep_size_after as long as
86 /// stream_end_was_reached is false (once it is true, read_pos
87 /// is allowed to reach write_pos).
88 size_t keep_size_after;
94 // Pointers to match finder functions
95 void (*get_matches)(lzma_lz_encoder *restrict lz,
96 uint32_t *restrict distances);
97 void (*skip)(lzma_lz_encoder *restrict lz, uint32_t num);
100 uint32_t *hash; // TODO: Check if hash aliases son
101 uint32_t *son; // and add 'restrict' if possible.
102 uint32_t cyclic_buffer_pos;
103 uint32_t cyclic_buffer_size; // Must be dictionary_size + 1.
106 uint32_t hash_size_sum;
108 uint32_t match_max_len;
112 #define LZMA_LZ_ENCODER_INIT \
122 extern bool lzma_lz_encoder_hash_properties(lzma_match_finder match_finder,
123 uint32_t history_size, uint32_t *restrict hash_mask,
124 uint32_t *restrict hash_size_sum,
125 uint32_t *restrict num_items);
127 // NOTE: liblzma doesn't use callback API like LZMA SDK does. The caller
128 // must make sure that keep_size_after is big enough for single encoding pass
129 // i.e. keep_size_after >= maximum number of bytes possibly needed after
130 // the current position between calls to lzma_lz_read().
131 extern lzma_ret lzma_lz_encoder_reset(lzma_lz_encoder *lz,
132 lzma_allocator *allocator,
133 bool (*process)(lzma_coder *coder, uint8_t *restrict out,
134 size_t *restrict out_pos, size_t out_size),
135 size_t history_size, size_t additional_buffer_before,
136 size_t match_max_len, size_t additional_buffer_after,
137 lzma_match_finder match_finder, uint32_t match_finder_cycles,
138 const uint8_t *preset_dictionary,
139 size_t preset_dictionary_size);
141 /// Frees memory allocated for in window and match finder buffers.
142 extern void lzma_lz_encoder_end(
143 lzma_lz_encoder *lz, lzma_allocator *allocator);
145 extern lzma_ret lzma_lz_encode(lzma_coder *coder,
146 lzma_allocator *allocator lzma_attribute((unused)),
147 const uint8_t *restrict in, size_t *restrict in_pos,
148 size_t in_size, uint8_t *restrict out,
149 size_t *restrict out_pos, size_t out_size,
152 /// This should not be called directly, but only via move_pos() macro.
153 extern void lzma_lz_encoder_normalize(lzma_lz_encoder *lz);