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