index_hash.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file index_hash.c
  4. /// \brief Validates Index by using a hash function
  5. //
  6. // Author: Lasse Collin
  7. //
  8. // This file has been put into the public domain.
  9. // You can do whatever you want with this file.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "common.h"
  13. #include "index.h"
  14. #include "check.h"
  15. typedef struct {
  16. /// Sum of the Block sizes (including Block Padding)
  17. lzma_vli blocks_size;
  18. /// Sum of the Uncompressed Size fields
  19. lzma_vli uncompressed_size;
  20. /// Number of Records
  21. lzma_vli count;
  22. /// Size of the List of Index Records as bytes
  23. lzma_vli index_list_size;
  24. /// Check calculated from Unpadded Sizes and Uncompressed Sizes.
  25. lzma_check_state check;
  26. } lzma_index_hash_info;
  27. struct lzma_index_hash_s {
  28. enum {
  29. SEQ_BLOCK,
  30. SEQ_COUNT,
  31. SEQ_UNPADDED,
  32. SEQ_UNCOMPRESSED,
  33. SEQ_PADDING_INIT,
  34. SEQ_PADDING,
  35. SEQ_CRC32,
  36. } sequence;
  37. /// Information collected while decoding the actual Blocks.
  38. lzma_index_hash_info blocks;
  39. /// Information collected from the Index field.
  40. lzma_index_hash_info records;
  41. /// Number of Records not fully decoded
  42. lzma_vli remaining;
  43. /// Unpadded Size currently being read from an Index Record.
  44. lzma_vli unpadded_size;
  45. /// Uncompressed Size currently being read from an Index Record.
  46. lzma_vli uncompressed_size;
  47. /// Position in variable-length integers when decoding them from
  48. /// the List of Records.
  49. size_t pos;
  50. /// CRC32 of the Index
  51. uint32_t crc32;
  52. };
  53. extern LZMA_API(lzma_index_hash *)
  54. lzma_index_hash_init(lzma_index_hash *index_hash,
  55. const lzma_allocator *allocator)
  56. {
  57. if (index_hash == NULL) {
  58. index_hash = lzma_alloc(sizeof(lzma_index_hash), allocator);
  59. if (index_hash == NULL)
  60. return NULL;
  61. }
  62. index_hash->sequence = SEQ_BLOCK;
  63. index_hash->blocks.blocks_size = 0;
  64. index_hash->blocks.uncompressed_size = 0;
  65. index_hash->blocks.count = 0;
  66. index_hash->blocks.index_list_size = 0;
  67. index_hash->records.blocks_size = 0;
  68. index_hash->records.uncompressed_size = 0;
  69. index_hash->records.count = 0;
  70. index_hash->records.index_list_size = 0;
  71. index_hash->unpadded_size = 0;
  72. index_hash->uncompressed_size = 0;
  73. index_hash->pos = 0;
  74. index_hash->crc32 = 0;
  75. // These cannot fail because LZMA_CHECK_BEST is known to be supported.
  76. (void)lzma_check_init(&index_hash->blocks.check, LZMA_CHECK_BEST);
  77. (void)lzma_check_init(&index_hash->records.check, LZMA_CHECK_BEST);
  78. return index_hash;
  79. }
  80. extern LZMA_API(void)
  81. lzma_index_hash_end(lzma_index_hash *index_hash,
  82. const lzma_allocator *allocator)
  83. {
  84. lzma_free(index_hash, allocator);
  85. return;
  86. }
  87. extern LZMA_API(lzma_vli)
  88. lzma_index_hash_size(const lzma_index_hash *index_hash)
  89. {
  90. // Get the size of the Index from ->blocks instead of ->records for
  91. // cases where application wants to know the Index Size before
  92. // decoding the Index.
  93. return index_size(index_hash->blocks.count,
  94. index_hash->blocks.index_list_size);
  95. }
  96. /// Updates the sizes and the hash without any validation.
  97. static lzma_ret
  98. hash_append(lzma_index_hash_info *info, lzma_vli unpadded_size,
  99. lzma_vli uncompressed_size)
  100. {
  101. info->blocks_size += vli_ceil4(unpadded_size);
  102. info->uncompressed_size += uncompressed_size;
  103. info->index_list_size += lzma_vli_size(unpadded_size)
  104. + lzma_vli_size(uncompressed_size);
  105. ++info->count;
  106. const lzma_vli sizes[2] = { unpadded_size, uncompressed_size };
  107. lzma_check_update(&info->check, LZMA_CHECK_BEST,
  108. (const uint8_t *)(sizes), sizeof(sizes));
  109. return LZMA_OK;
  110. }
  111. extern LZMA_API(lzma_ret)
  112. lzma_index_hash_append(lzma_index_hash *index_hash, lzma_vli unpadded_size,
  113. lzma_vli uncompressed_size)
  114. {
  115. // Validate the arguments.
  116. if (index_hash->sequence != SEQ_BLOCK
  117. || unpadded_size < UNPADDED_SIZE_MIN
  118. || unpadded_size > UNPADDED_SIZE_MAX
  119. || uncompressed_size > LZMA_VLI_MAX)
  120. return LZMA_PROG_ERROR;
  121. // Update the hash.
  122. return_if_error(hash_append(&index_hash->blocks,
  123. unpadded_size, uncompressed_size));
  124. // Validate the properties of *info are still in allowed limits.
  125. if (index_hash->blocks.blocks_size > LZMA_VLI_MAX
  126. || index_hash->blocks.uncompressed_size > LZMA_VLI_MAX
  127. || index_size(index_hash->blocks.count,
  128. index_hash->blocks.index_list_size)
  129. > LZMA_BACKWARD_SIZE_MAX
  130. || index_stream_size(index_hash->blocks.blocks_size,
  131. index_hash->blocks.count,
  132. index_hash->blocks.index_list_size)
  133. > LZMA_VLI_MAX)
  134. return LZMA_DATA_ERROR;
  135. return LZMA_OK;
  136. }
  137. extern LZMA_API(lzma_ret)
  138. lzma_index_hash_decode(lzma_index_hash *index_hash, const uint8_t *in,
  139. size_t *in_pos, size_t in_size)
  140. {
  141. // Catch zero input buffer here, because in contrast to Index encoder
  142. // and decoder functions, applications call this function directly
  143. // instead of via lzma_code(), which does the buffer checking.
  144. if (*in_pos >= in_size)
  145. return LZMA_BUF_ERROR;
  146. // NOTE: This function has many similarities to index_encode() and
  147. // index_decode() functions found from index_encoder.c and
  148. // index_decoder.c. See the comments especially in index_encoder.c.
  149. const size_t in_start = *in_pos;
  150. lzma_ret ret = LZMA_OK;
  151. while (*in_pos < in_size)
  152. switch (index_hash->sequence) {
  153. case SEQ_BLOCK:
  154. // Check the Index Indicator is present.
  155. if (in[(*in_pos)++] != 0x00)
  156. return LZMA_DATA_ERROR;
  157. index_hash->sequence = SEQ_COUNT;
  158. break;
  159. case SEQ_COUNT: {
  160. ret = lzma_vli_decode(&index_hash->remaining,
  161. &index_hash->pos, in, in_pos, in_size);
  162. if (ret != LZMA_STREAM_END)
  163. goto out;
  164. // The count must match the count of the Blocks decoded.
  165. if (index_hash->remaining != index_hash->blocks.count)
  166. return LZMA_DATA_ERROR;
  167. ret = LZMA_OK;
  168. index_hash->pos = 0;
  169. // Handle the special case when there are no Blocks.
  170. index_hash->sequence = index_hash->remaining == 0
  171. ? SEQ_PADDING_INIT : SEQ_UNPADDED;
  172. break;
  173. }
  174. case SEQ_UNPADDED:
  175. case SEQ_UNCOMPRESSED: {
  176. lzma_vli *size = index_hash->sequence == SEQ_UNPADDED
  177. ? &index_hash->unpadded_size
  178. : &index_hash->uncompressed_size;
  179. ret = lzma_vli_decode(size, &index_hash->pos,
  180. in, in_pos, in_size);
  181. if (ret != LZMA_STREAM_END)
  182. goto out;
  183. ret = LZMA_OK;
  184. index_hash->pos = 0;
  185. if (index_hash->sequence == SEQ_UNPADDED) {
  186. if (index_hash->unpadded_size < UNPADDED_SIZE_MIN
  187. || index_hash->unpadded_size
  188. > UNPADDED_SIZE_MAX)
  189. return LZMA_DATA_ERROR;
  190. index_hash->sequence = SEQ_UNCOMPRESSED;
  191. } else {
  192. // Update the hash.
  193. return_if_error(hash_append(&index_hash->records,
  194. index_hash->unpadded_size,
  195. index_hash->uncompressed_size));
  196. // Verify that we don't go over the known sizes. Note
  197. // that this validation is simpler than the one used
  198. // in lzma_index_hash_append(), because here we know
  199. // that values in index_hash->blocks are already
  200. // validated and we are fine as long as we don't
  201. // exceed them in index_hash->records.
  202. if (index_hash->blocks.blocks_size
  203. < index_hash->records.blocks_size
  204. || index_hash->blocks.uncompressed_size
  205. < index_hash->records.uncompressed_size
  206. || index_hash->blocks.index_list_size
  207. < index_hash->records.index_list_size)
  208. return LZMA_DATA_ERROR;
  209. // Check if this was the last Record.
  210. index_hash->sequence = --index_hash->remaining == 0
  211. ? SEQ_PADDING_INIT : SEQ_UNPADDED;
  212. }
  213. break;
  214. }
  215. case SEQ_PADDING_INIT:
  216. index_hash->pos = (LZMA_VLI_C(4) - index_size_unpadded(
  217. index_hash->records.count,
  218. index_hash->records.index_list_size)) & 3;
  219. index_hash->sequence = SEQ_PADDING;
  220. // Fall through
  221. case SEQ_PADDING:
  222. if (index_hash->pos > 0) {
  223. --index_hash->pos;
  224. if (in[(*in_pos)++] != 0x00)
  225. return LZMA_DATA_ERROR;
  226. break;
  227. }
  228. // Compare the sizes.
  229. if (index_hash->blocks.blocks_size
  230. != index_hash->records.blocks_size
  231. || index_hash->blocks.uncompressed_size
  232. != index_hash->records.uncompressed_size
  233. || index_hash->blocks.index_list_size
  234. != index_hash->records.index_list_size)
  235. return LZMA_DATA_ERROR;
  236. // Finish the hashes and compare them.
  237. lzma_check_finish(&index_hash->blocks.check, LZMA_CHECK_BEST);
  238. lzma_check_finish(&index_hash->records.check, LZMA_CHECK_BEST);
  239. if (memcmp(index_hash->blocks.check.buffer.u8,
  240. index_hash->records.check.buffer.u8,
  241. lzma_check_size(LZMA_CHECK_BEST)) != 0)
  242. return LZMA_DATA_ERROR;
  243. // Finish the CRC32 calculation.
  244. index_hash->crc32 = lzma_crc32(in + in_start,
  245. *in_pos - in_start, index_hash->crc32);
  246. index_hash->sequence = SEQ_CRC32;
  247. // Fall through
  248. case SEQ_CRC32:
  249. do {
  250. if (*in_pos == in_size)
  251. return LZMA_OK;
  252. if (((index_hash->crc32 >> (index_hash->pos * 8))
  253. & 0xFF) != in[(*in_pos)++])
  254. return LZMA_DATA_ERROR;
  255. } while (++index_hash->pos < 4);
  256. return LZMA_STREAM_END;
  257. default:
  258. assert(0);
  259. return LZMA_PROG_ERROR;
  260. }
  261. out:
  262. // Update the CRC32,
  263. index_hash->crc32 = lzma_crc32(in + in_start,
  264. *in_pos - in_start, index_hash->crc32);
  265. return ret;
  266. }