xref: /freebsd/contrib/xz/src/liblzma/api/lzma/index_hash.h (revision 3b35e7ee8de9b0260149a2b77e87a2b9c7a36244)
1*3b35e7eeSXin LI /* SPDX-License-Identifier: 0BSD */
2*3b35e7eeSXin LI 
381ad8388SMartin Matuska /**
481ad8388SMartin Matuska  * \file        lzma/index_hash.h
5542aef48SMartin Matuska  * \brief       Validate Index by using a hash function
6c917796cSXin LI  * \note        Never include this file directly. Use <lzma.h> instead.
781ad8388SMartin Matuska  *
881ad8388SMartin Matuska  * Hashing makes it possible to use constant amount of memory to validate
981ad8388SMartin Matuska  * Index of arbitrary size.
1081ad8388SMartin Matuska  */
1181ad8388SMartin Matuska 
1281ad8388SMartin Matuska /*
1381ad8388SMartin Matuska  * Author: Lasse Collin
1481ad8388SMartin Matuska  */
1581ad8388SMartin Matuska 
1681ad8388SMartin Matuska #ifndef LZMA_H_INTERNAL
1781ad8388SMartin Matuska #	error Never include this file directly. Use <lzma.h> instead.
1881ad8388SMartin Matuska #endif
1981ad8388SMartin Matuska 
2081ad8388SMartin Matuska /**
2181ad8388SMartin Matuska  * \brief       Opaque data type to hold the Index hash
2281ad8388SMartin Matuska  */
2381ad8388SMartin Matuska typedef struct lzma_index_hash_s lzma_index_hash;
2481ad8388SMartin Matuska 
2581ad8388SMartin Matuska 
2681ad8388SMartin Matuska /**
2781ad8388SMartin Matuska  * \brief       Allocate and initialize a new lzma_index_hash structure
2881ad8388SMartin Matuska  *
29c917796cSXin LI  * If index_hash is NULL, this function allocates and initializes a new
30c917796cSXin LI  * lzma_index_hash structure and returns a pointer to it. If allocation
31c917796cSXin LI  * fails, NULL is returned.
3281ad8388SMartin Matuska  *
33c917796cSXin LI  * If index_hash is non-NULL, this function reinitializes the lzma_index_hash
34c917796cSXin LI  * structure and returns the same pointer. In this case, return value cannot
35c917796cSXin LI  * be NULL or a different pointer than the index_hash that was given as
36c917796cSXin LI  * an argument.
37c917796cSXin LI  *
38c917796cSXin LI  * \param       index_hash  Pointer to a lzma_index_hash structure or NULL.
39c917796cSXin LI  * \param       allocator   lzma_allocator for custom allocator functions.
40c917796cSXin LI  *                          Set to NULL to use malloc() and free().
41c917796cSXin LI  *
42c917796cSXin LI  * \return      Initialized lzma_index_hash structure on success or
43c917796cSXin LI  *              NULL on failure.
4481ad8388SMartin Matuska  */
4581ad8388SMartin Matuska extern LZMA_API(lzma_index_hash *) lzma_index_hash_init(
4653200025SRui Paulo 		lzma_index_hash *index_hash, const lzma_allocator *allocator)
4781ad8388SMartin Matuska 		lzma_nothrow lzma_attr_warn_unused_result;
4881ad8388SMartin Matuska 
4981ad8388SMartin Matuska 
5081ad8388SMartin Matuska /**
5181ad8388SMartin Matuska  * \brief       Deallocate lzma_index_hash structure
52c917796cSXin LI  *
53c917796cSXin LI  * \param       index_hash  Pointer to a lzma_index_hash structure to free.
54c917796cSXin LI  * \param       allocator   lzma_allocator for custom allocator functions.
55c917796cSXin LI  *                          Set to NULL to use malloc() and free().
5681ad8388SMartin Matuska  */
5781ad8388SMartin Matuska extern LZMA_API(void) lzma_index_hash_end(
5853200025SRui Paulo 		lzma_index_hash *index_hash, const lzma_allocator *allocator)
5981ad8388SMartin Matuska 		lzma_nothrow;
6081ad8388SMartin Matuska 
6181ad8388SMartin Matuska 
6281ad8388SMartin Matuska /**
6381ad8388SMartin Matuska  * \brief       Add a new Record to an Index hash
6481ad8388SMartin Matuska  *
650ca90ed4SXin LI  * \param       index_hash        Pointer to a lzma_index_hash structure
6681ad8388SMartin Matuska  * \param       unpadded_size     Unpadded Size of a Block
6781ad8388SMartin Matuska  * \param       uncompressed_size Uncompressed Size of a Block
6881ad8388SMartin Matuska  *
69c917796cSXin LI  * \return      Possible lzma_ret values:
70c917796cSXin LI  *              - LZMA_OK
7181ad8388SMartin Matuska  *              - LZMA_DATA_ERROR: Compressed or uncompressed size of the
7281ad8388SMartin Matuska  *                Stream or size of the Index field would grow too big.
7381ad8388SMartin Matuska  *              - LZMA_PROG_ERROR: Invalid arguments or this function is being
7481ad8388SMartin Matuska  *                used when lzma_index_hash_decode() has already been used.
7581ad8388SMartin Matuska  */
7681ad8388SMartin Matuska extern LZMA_API(lzma_ret) lzma_index_hash_append(lzma_index_hash *index_hash,
7781ad8388SMartin Matuska 		lzma_vli unpadded_size, lzma_vli uncompressed_size)
7881ad8388SMartin Matuska 		lzma_nothrow lzma_attr_warn_unused_result;
7981ad8388SMartin Matuska 
8081ad8388SMartin Matuska 
8181ad8388SMartin Matuska /**
8281ad8388SMartin Matuska  * \brief       Decode and validate the Index field
8381ad8388SMartin Matuska  *
8481ad8388SMartin Matuska  * After telling the sizes of all Blocks with lzma_index_hash_append(),
8581ad8388SMartin Matuska  * the actual Index field is decoded with this function. Specifically,
8681ad8388SMartin Matuska  * once decoding of the Index field has been started, no more Records
8781ad8388SMartin Matuska  * can be added using lzma_index_hash_append().
8881ad8388SMartin Matuska  *
8981ad8388SMartin Matuska  * This function doesn't use lzma_stream structure to pass the input data.
9081ad8388SMartin Matuska  * Instead, the input buffer is specified using three arguments. This is
9181ad8388SMartin Matuska  * because it matches better the internal APIs of liblzma.
9281ad8388SMartin Matuska  *
9381ad8388SMartin Matuska  * \param       index_hash      Pointer to a lzma_index_hash structure
9481ad8388SMartin Matuska  * \param       in              Pointer to the beginning of the input buffer
95c917796cSXin LI  * \param[out]  in_pos          in[*in_pos] is the next byte to process
9681ad8388SMartin Matuska  * \param       in_size         in[in_size] is the first byte not to process
9781ad8388SMartin Matuska  *
98c917796cSXin LI  * \return      Possible lzma_ret values:
99c917796cSXin LI  *              - LZMA_OK: So far good, but more input is needed.
10081ad8388SMartin Matuska  *              - LZMA_STREAM_END: Index decoded successfully and it matches
10181ad8388SMartin Matuska  *                the Records given with lzma_index_hash_append().
10281ad8388SMartin Matuska  *              - LZMA_DATA_ERROR: Index is corrupt or doesn't match the
10381ad8388SMartin Matuska  *                information given with lzma_index_hash_append().
10481ad8388SMartin Matuska  *              - LZMA_BUF_ERROR: Cannot progress because *in_pos >= in_size.
10581ad8388SMartin Matuska  *              - LZMA_PROG_ERROR
10681ad8388SMartin Matuska  */
10781ad8388SMartin Matuska extern LZMA_API(lzma_ret) lzma_index_hash_decode(lzma_index_hash *index_hash,
10881ad8388SMartin Matuska 		const uint8_t *in, size_t *in_pos, size_t in_size)
10981ad8388SMartin Matuska 		lzma_nothrow lzma_attr_warn_unused_result;
11081ad8388SMartin Matuska 
11181ad8388SMartin Matuska 
11281ad8388SMartin Matuska /**
11381ad8388SMartin Matuska  * \brief       Get the size of the Index field as bytes
11481ad8388SMartin Matuska  *
11581ad8388SMartin Matuska  * This is needed to verify the Backward Size field in the Stream Footer.
116c917796cSXin LI  *
117c917796cSXin LI  * \param       index_hash      Pointer to a lzma_index_hash structure
118c917796cSXin LI  *
119c917796cSXin LI  * \return      Size of the Index field in bytes.
12081ad8388SMartin Matuska  */
12181ad8388SMartin Matuska extern LZMA_API(lzma_vli) lzma_index_hash_size(
12281ad8388SMartin Matuska 		const lzma_index_hash *index_hash)
12381ad8388SMartin Matuska 		lzma_nothrow lzma_attr_pure;
124