1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       index.h
4207753Smm/// \brief      Handling of Index
5207753Smm//
6207753Smm//  Author:     Lasse Collin
7207753Smm//
8207753Smm//  This file has been put into the public domain.
9207753Smm//  You can do whatever you want with this file.
10207753Smm//
11207753Smm///////////////////////////////////////////////////////////////////////////////
12207753Smm
13207753Smm#ifndef LZMA_INDEX_H
14207753Smm#define LZMA_INDEX_H
15207753Smm
16207753Smm#include "common.h"
17207753Smm
18207753Smm
19207753Smm/// Minimum Unpadded Size
20207753Smm#define UNPADDED_SIZE_MIN LZMA_VLI_C(5)
21207753Smm
22207753Smm/// Maximum Unpadded Size
23207753Smm#define UNPADDED_SIZE_MAX (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
24207753Smm
25207753Smm
26207753Smm/// Get the size of the Index Padding field. This is needed by Index encoder
27207753Smm/// and decoder, but applications should have no use for this.
28207753Smmextern uint32_t lzma_index_padding_size(const lzma_index *i);
29207753Smm
30207753Smm
31207753Smm/// Set for how many Records to allocate memory the next time
32207753Smm/// lzma_index_append() needs to allocate space for a new Record.
33207753Smm/// This is used only by the Index decoder.
34207753Smmextern void lzma_index_prealloc(lzma_index *i, lzma_vli records);
35207753Smm
36207753Smm
37207753Smm/// Round the variable-length integer to the next multiple of four.
38207753Smmstatic inline lzma_vli
39207753Smmvli_ceil4(lzma_vli vli)
40207753Smm{
41207753Smm	assert(vli <= LZMA_VLI_MAX);
42207753Smm	return (vli + 3) & ~LZMA_VLI_C(3);
43207753Smm}
44207753Smm
45207753Smm
46207753Smm/// Calculate the size of the Index field excluding Index Padding
47207753Smmstatic inline lzma_vli
48207753Smmindex_size_unpadded(lzma_vli count, lzma_vli index_list_size)
49207753Smm{
50207753Smm	// Index Indicator + Number of Records + List of Records + CRC32
51207753Smm	return 1 + lzma_vli_size(count) + index_list_size + 4;
52207753Smm}
53207753Smm
54207753Smm
55207753Smm/// Calculate the size of the Index field including Index Padding
56207753Smmstatic inline lzma_vli
57207753Smmindex_size(lzma_vli count, lzma_vli index_list_size)
58207753Smm{
59207753Smm	return vli_ceil4(index_size_unpadded(count, index_list_size));
60207753Smm}
61207753Smm
62207753Smm
63207753Smm/// Calculate the total size of the Stream
64207753Smmstatic inline lzma_vli
65207753Smmindex_stream_size(lzma_vli blocks_size,
66207753Smm		lzma_vli count, lzma_vli index_list_size)
67207753Smm{
68207753Smm	return LZMA_STREAM_HEADER_SIZE + blocks_size
69207753Smm			+ index_size(count, index_list_size)
70207753Smm			+ LZMA_STREAM_HEADER_SIZE;
71207753Smm}
72207753Smm
73207753Smm#endif
74