xref: /freebsd/contrib/xz/src/liblzma/api/lzma/index_hash.h (revision 7ab1a32cd43cbae61ad4dd435d6a482bbf61cb52)
1 /* SPDX-License-Identifier: 0BSD */
2 
3 /**
4  * \file        lzma/index_hash.h
5  * \brief       Validate Index by using a hash function
6  * \note        Never include this file directly. Use <lzma.h> instead.
7  *
8  * Hashing makes it possible to use constant amount of memory to validate
9  * Index of arbitrary size.
10  */
11 
12 /*
13  * Author: Lasse Collin
14  */
15 
16 #ifndef LZMA_H_INTERNAL
17 #	error Never include this file directly. Use <lzma.h> instead.
18 #endif
19 
20 /**
21  * \brief       Opaque data type to hold the Index hash
22  */
23 typedef struct lzma_index_hash_s lzma_index_hash;
24 
25 
26 /**
27  * \brief       Allocate and initialize a new lzma_index_hash structure
28  *
29  * If index_hash is NULL, this function allocates and initializes a new
30  * lzma_index_hash structure and returns a pointer to it. If allocation
31  * fails, NULL is returned.
32  *
33  * If index_hash is non-NULL, this function reinitializes the lzma_index_hash
34  * structure and returns the same pointer. In this case, return value cannot
35  * be NULL or a different pointer than the index_hash that was given as
36  * an argument.
37  *
38  * \param       index_hash  Pointer to a lzma_index_hash structure or NULL.
39  * \param       allocator   lzma_allocator for custom allocator functions.
40  *                          Set to NULL to use malloc() and free().
41  *
42  * \return      Initialized lzma_index_hash structure on success or
43  *              NULL on failure.
44  */
45 extern LZMA_API(lzma_index_hash *) lzma_index_hash_init(
46 		lzma_index_hash *index_hash, const lzma_allocator *allocator)
47 		lzma_nothrow lzma_attr_warn_unused_result;
48 
49 
50 /**
51  * \brief       Deallocate lzma_index_hash structure
52  *
53  * \param       index_hash  Pointer to a lzma_index_hash structure to free.
54  * \param       allocator   lzma_allocator for custom allocator functions.
55  *                          Set to NULL to use malloc() and free().
56  */
57 extern LZMA_API(void) lzma_index_hash_end(
58 		lzma_index_hash *index_hash, const lzma_allocator *allocator)
59 		lzma_nothrow;
60 
61 
62 /**
63  * \brief       Add a new Record to an Index hash
64  *
65  * \param       index_hash        Pointer to a lzma_index_hash structure
66  * \param       unpadded_size     Unpadded Size of a Block
67  * \param       uncompressed_size Uncompressed Size of a Block
68  *
69  * \return      Possible lzma_ret values:
70  *              - LZMA_OK
71  *              - LZMA_DATA_ERROR: Compressed or uncompressed size of the
72  *                Stream or size of the Index field would grow too big.
73  *              - LZMA_PROG_ERROR: Invalid arguments or this function is being
74  *                used when lzma_index_hash_decode() has already been used.
75  */
76 extern LZMA_API(lzma_ret) lzma_index_hash_append(lzma_index_hash *index_hash,
77 		lzma_vli unpadded_size, lzma_vli uncompressed_size)
78 		lzma_nothrow lzma_attr_warn_unused_result;
79 
80 
81 /**
82  * \brief       Decode and validate the Index field
83  *
84  * After telling the sizes of all Blocks with lzma_index_hash_append(),
85  * the actual Index field is decoded with this function. Specifically,
86  * once decoding of the Index field has been started, no more Records
87  * can be added using lzma_index_hash_append().
88  *
89  * This function doesn't use lzma_stream structure to pass the input data.
90  * Instead, the input buffer is specified using three arguments. This is
91  * because it matches better the internal APIs of liblzma.
92  *
93  * \param       index_hash      Pointer to a lzma_index_hash structure
94  * \param       in              Pointer to the beginning of the input buffer
95  * \param[out]  in_pos          in[*in_pos] is the next byte to process
96  * \param       in_size         in[in_size] is the first byte not to process
97  *
98  * \return      Possible lzma_ret values:
99  *              - LZMA_OK: So far good, but more input is needed.
100  *              - LZMA_STREAM_END: Index decoded successfully and it matches
101  *                the Records given with lzma_index_hash_append().
102  *              - LZMA_DATA_ERROR: Index is corrupt or doesn't match the
103  *                information given with lzma_index_hash_append().
104  *              - LZMA_BUF_ERROR: Cannot progress because *in_pos >= in_size.
105  *              - LZMA_PROG_ERROR
106  */
107 extern LZMA_API(lzma_ret) lzma_index_hash_decode(lzma_index_hash *index_hash,
108 		const uint8_t *in, size_t *in_pos, size_t in_size)
109 		lzma_nothrow lzma_attr_warn_unused_result;
110 
111 
112 /**
113  * \brief       Get the size of the Index field as bytes
114  *
115  * This is needed to verify the Backward Size field in the Stream Footer.
116  *
117  * \param       index_hash      Pointer to a lzma_index_hash structure
118  *
119  * \return      Size of the Index field in bytes.
120  */
121 extern LZMA_API(lzma_vli) lzma_index_hash_size(
122 		const lzma_index_hash *index_hash)
123 		lzma_nothrow lzma_attr_pure;
124