index_hash.h 3.8 KB

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