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