index_hash.h 4.6 KB

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