index.h 2.4 KB

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