3 * \brief Handling of Stream size information
5 * \author Copyright (C) 1999-2006 Igor Pavlov
6 * \author Copyright (C) 2007 Lasse Collin
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.
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.
19 #ifndef LZMA_H_INTERNAL
20 # error Never include this file directly. Use <lzma.h> instead.
29 * \brief Opaque data type to hold the size information
31 typedef struct lzma_info_s lzma_info;
36 * \brief Total Size of this Block
38 * This can be LZMA_VLI_VALUE_UNKNOWN.
43 * \brief Uncompressed Size of this Block
45 * This can be LZMA_VLI_VALUE_UNKNOWN.
47 lzma_vli uncompressed_size;
50 * \brief Offset of the first byte of the Block
52 * In encoder, this is useful to find out the alignment of the Block.
54 * In decoder, this is useful when doing random-access reading
55 * with help from lzma_info_data_locate().
57 lzma_vli stream_offset;
60 * \brief Uncompressed offset of the Block
62 * Offset of the first uncompressed byte of the Block relative to
63 * all uncompressed data in the Block.
66 lzma_vli uncompressed_offset;
69 * \brief Pointers to internal data structures
71 * Applications must not touch these.
79 LZMA_INFO_STREAM_START,
80 LZMA_INFO_HEADER_METADATA,
82 LZMA_INFO_UNCOMPRESSED,
83 LZMA_INFO_FOOTER_METADATA
88 * \brief Allocates and initializes a new lzma_info structure
90 * If info is NULL, a new lzma_info structure is allocated, initialized, and
91 * a pointer to it returned. If allocation fails, NULL is returned.
93 * If info is non-NULL, it is reinitialized and the same pointer returned.
94 * (In this case, return value cannot be NULL or a different pointer than
95 * the info given as argument.)
97 extern lzma_info *lzma_info_init(lzma_info *info, lzma_allocator *allocator);
101 * \brief Resets lzma_info
103 * This is like calling lzma_info_end() and lzma_info_create(), but
104 * re-uses the existing base structure.
106 extern void lzma_info_reset(
107 lzma_info *info, lzma_allocator *allocator);
111 * \brief Frees memory allocated for a lzma_info structure
113 extern void lzma_info_free(lzma_info *info, lzma_allocator *allocator);
116 /************************
117 * Setting known values *
118 ************************/
121 * \brief Set a known size value
123 * \param info Pointer returned by lzma_info_create()
124 * \param type Any value from lzma_info_size
125 * \param size Value to set or verify
127 * \return LZMA_OK on success, LZMA_DATA_ERROR if the size doesn't
128 * match the existing information, or LZMA_PROG_ERROR
129 * if type is invalid or size is not a valid VLI.
131 extern lzma_ret lzma_info_size_set(
132 lzma_info *info, lzma_info_size type, lzma_vli size);
136 * \brief Sets the Index
138 * The given lzma_index list is "absorbed" by this function. The application
139 * must not access it after this function call, even if this function returns
142 * \note The given lzma_index will at some point get freed by the
143 * lzma_info_* functions. If you use a custom lzma_allocator,
144 * make sure that it can free the lzma_index.
146 extern lzma_ret lzma_info_index_set(
147 lzma_info *info, lzma_allocator *allocator,
148 lzma_index *index, lzma_bool eat_index);
152 * \brief Sets information from a known Metadata Block
154 * This is a shortcut for calling lzma_info_size_set() with different type
155 * arguments, lzma_info_index_set() with metadata->index.
157 extern lzma_ret lzma_info_metadata_set(lzma_info *info,
158 lzma_allocator *allocator, lzma_metadata *metadata,
159 lzma_bool is_header_metadata, lzma_bool eat_index);
167 * \brief Prepares an iterator to be used with given lzma_info structure
171 extern void lzma_info_iter_begin(lzma_info *info, lzma_info_iter *iter);
175 * \brief Moves to the next Index Record
179 extern lzma_ret lzma_info_iter_next(
180 lzma_info_iter *iter, lzma_allocator *allocator);
184 * \brief Sets or verifies the sizes in the Index Record
186 * \param iter Pointer to iterator to be set or verified
188 * Total Size in bytes or LZMA_VLI_VALUE_UNKNOWN
189 * \param uncompressed_size
190 * Uncompressed Size or LZMA_VLI_VALUE_UNKNOWN
192 * \return - LZMA_OK: All OK.
193 * - LZMA_DATA_ERROR: Given sizes don't match with the already
195 * - LZMA_PROG_ERROR: Internal error, possibly integer
196 * overflow (e.g. the sum of all the known sizes is too big)
198 extern lzma_ret lzma_info_iter_set(lzma_info_iter *iter,
199 lzma_vli total_size, lzma_vli uncompressed_size);
203 * \brief Locates a Data Block
205 * \param iter Properly initialized iterator
206 * \param allocator Pointer to lzma_allocator or NULL
207 * \param uncompressed_offset
208 * Target offset to locate. The final offset
209 * will be equal or smaller than this.
210 * \param allow_alloc True if this function is allowed to call
211 * lzma_info_iter_next() to allocate a new Record
212 * if the requested offset reached end of Index
213 * Record list. Note that if Index has been marked
214 * final, lzma_info_iter_next() is never called.
216 * \return - LZMA_OK: All OK, *iter updated accordingly.
217 * - LZMA_DATA_ERROR: Trying to search past the end of the Index
218 * Record list, and allocating a new Record was not allowed
219 * either because allow_alloc was false or Index was final.
220 * - LZMA_PROG_ERROR: Internal error (probably integer
221 * overflow causing some lzma_vli getting too big).
223 extern lzma_ret lzma_info_iter_locate(lzma_info_iter *iter,
224 lzma_allocator *allocator, lzma_vli uncompressed_offset,
225 lzma_bool allow_alloc);
229 * \brief Finishes incrementally constructed Index
231 * This sets the known Total Size and Uncompressed of the Data Blocks
232 * based on the information collected from the Index Records, and marks
233 * the Index as final.
235 extern lzma_ret lzma_info_index_finish(lzma_info *info);
238 /***************************
239 * Reading the information *
240 ***************************/
243 * \brief Gets a known size
247 extern lzma_vli lzma_info_size_get(
248 const lzma_info *info, lzma_info_size type);
250 extern lzma_vli lzma_info_metadata_locate(
251 const lzma_info *info, lzma_bool is_header_metadata);
254 * \brief Gets a pointer to the beginning of the Index list
256 * If detach is true, the Index will be detached from the lzma_info
257 * structure, and thus not be modified or freed by lzma_info_end().
259 * If detach is false, the application must not modify the Index in any way.
260 * Also, the Index list is guaranteed to be valid only till the next call
261 * to any lzma_info_* function.
263 extern lzma_index *lzma_info_index_get(lzma_info *info, lzma_bool detach);
266 extern size_t lzma_info_index_count_get(const lzma_info *info);
269 extern uint32_t lzma_info_metadata_alignment_get(
270 const lzma_info *info, lzma_bool is_header_metadata);
275 * \brief Locate a Block containing the given uncompressed offset
277 * This function is useful when you need to do random-access reading in
278 * a Multi-Block Stream.
280 * \param info Pointer to lzma_info that has at least one
281 * Index Record. The Index doesn't need to be finished.
282 * \param uncompressed_target
283 * Uncompressed target offset which the caller would
284 * like to locate from the Stream.
285 * \param stream_offset
286 * Starting offset (relative to the beginning the Stream)
287 * of the Block containing the requested location.
288 * \param uncompressed_offset
289 * The actual uncompressed offset of the beginning of
290 * the Block. uncompressed_offset <= uncompressed_target
291 * is always true; the application needs to uncompress
292 * uncompressed_target - uncompressed_offset bytes to
293 * reach the requested target offset.
295 * Total Size of the Block. If the Index is incomplete,
296 * this may be LZMA_VLI_VALUE_UNKNOWN indicating unknown
298 * \param uncompressed_size
299 * Uncompressed Size of the Block. If the Index is
300 * incomplete, this may be LZMA_VLI_VALUE_UNKNOWN
301 * indicating unknown size. The application must pass
302 * this value to the Block decoder to verify FIXME
306 * \note This function is currently implemented as a linear search.
307 * If there are many Index Records, this can be really slow.
308 * This can be improved in newer liblzma versions if needed.
310 extern lzma_bool lzma_info_data_locate(const lzma_info *info,
311 lzma_vli uncompressed_target,
312 lzma_vli *lzma_restrict stream_offset,
313 lzma_vli *lzma_restrict uncompressed_offset,
314 lzma_vli *lzma_restrict total_size,
315 lzma_vli *lzma_restrict uncompressed_size);