xref: /freebsd/contrib/xz/src/liblzma/common/index.h (revision 38b3683592d4c20a74f52a6e8e29368e6fa61858)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       index.h
6 /// \brief      Handling of Index
7 /// \note       This header file does not include common.h or lzma.h because
8 ///             this file is needed by both liblzma internally and by the
9 ///             tests. Including common.h will include and define many things
10 ///             the tests do not need and prevents issues with header file
11 ///             include order. This way, if lzma.h or common.h are not
12 ///             included before this file it will break on every OS instead
13 ///             of causing more subtle errors.
14 //
15 //  Author:     Lasse Collin
16 //
17 ///////////////////////////////////////////////////////////////////////////////
18 
19 #ifndef LZMA_INDEX_H
20 #define LZMA_INDEX_H
21 
22 
23 /// Minimum Unpadded Size
24 #define UNPADDED_SIZE_MIN LZMA_VLI_C(5)
25 
26 /// Maximum Unpadded Size
27 #define UNPADDED_SIZE_MAX (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
28 
29 /// Index Indicator based on xz specification
30 #define INDEX_INDICATOR 0
31 
32 
33 /// Get the size of the Index Padding field. This is needed by Index encoder
34 /// and decoder, but applications should have no use for this.
35 extern uint32_t lzma_index_padding_size(const lzma_index *i);
36 
37 
38 /// Set for how many Records to allocate memory the next time
39 /// lzma_index_append() needs to allocate space for a new Record.
40 /// This is used only by the Index decoder.
41 extern void lzma_index_prealloc(lzma_index *i, lzma_vli records);
42 
43 
44 /// Round the variable-length integer to the next multiple of four.
45 static inline lzma_vli
46 vli_ceil4(lzma_vli vli)
47 {
48 	assert(vli <= UNPADDED_SIZE_MAX);
49 	return (vli + 3) & ~LZMA_VLI_C(3);
50 }
51 
52 
53 /// Calculate the size of the Index field excluding Index Padding
54 static inline lzma_vli
55 index_size_unpadded(lzma_vli count, lzma_vli index_list_size)
56 {
57 	// Index Indicator + Number of Records + List of Records + CRC32
58 	return 1 + lzma_vli_size(count) + index_list_size + 4;
59 }
60 
61 
62 /// Calculate the size of the Index field including Index Padding
63 static inline lzma_vli
64 index_size(lzma_vli count, lzma_vli index_list_size)
65 {
66 	return vli_ceil4(index_size_unpadded(count, index_list_size));
67 }
68 
69 
70 /// Calculate the total size of the Stream
71 static inline lzma_vli
72 index_stream_size(lzma_vli blocks_size,
73 		lzma_vli count, lzma_vli index_list_size)
74 {
75 	return LZMA_STREAM_HEADER_SIZE + blocks_size
76 			+ index_size(count, index_list_size)
77 			+ LZMA_STREAM_HEADER_SIZE;
78 }
79 
80 #endif
81