block_header_encoder.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file block_header_encoder.c
  4. /// \brief Encodes Block Header for .xz files
  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 "check.h"
  14. extern LZMA_API(lzma_ret)
  15. lzma_block_header_size(lzma_block *block)
  16. {
  17. if (block->version > 1)
  18. return LZMA_OPTIONS_ERROR;
  19. // Block Header Size + Block Flags + CRC32.
  20. uint32_t size = 1 + 1 + 4;
  21. // Compressed Size
  22. if (block->compressed_size != LZMA_VLI_UNKNOWN) {
  23. const uint32_t add = lzma_vli_size(block->compressed_size);
  24. if (add == 0 || block->compressed_size == 0)
  25. return LZMA_PROG_ERROR;
  26. size += add;
  27. }
  28. // Uncompressed Size
  29. if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
  30. const uint32_t add = lzma_vli_size(block->uncompressed_size);
  31. if (add == 0)
  32. return LZMA_PROG_ERROR;
  33. size += add;
  34. }
  35. // List of Filter Flags
  36. if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
  37. return LZMA_PROG_ERROR;
  38. for (size_t i = 0; block->filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
  39. // Don't allow too many filters.
  40. if (i == LZMA_FILTERS_MAX)
  41. return LZMA_PROG_ERROR;
  42. uint32_t add;
  43. return_if_error(lzma_filter_flags_size(&add,
  44. block->filters + i));
  45. size += add;
  46. }
  47. // Pad to a multiple of four bytes.
  48. block->header_size = (size + 3) & ~UINT32_C(3);
  49. // NOTE: We don't verify that the encoded size of the Block stays
  50. // within limits. This is because it is possible that we are called
  51. // with exaggerated Compressed Size (e.g. LZMA_VLI_MAX) to reserve
  52. // space for Block Header, and later called again with lower,
  53. // real values.
  54. return LZMA_OK;
  55. }
  56. extern LZMA_API(lzma_ret)
  57. lzma_block_header_encode(const lzma_block *block, uint8_t *out)
  58. {
  59. // Validate everything but filters.
  60. if (lzma_block_unpadded_size(block) == 0
  61. || !lzma_vli_is_valid(block->uncompressed_size))
  62. return LZMA_PROG_ERROR;
  63. // Indicate the size of the buffer _excluding_ the CRC32 field.
  64. const size_t out_size = block->header_size - 4;
  65. // Store the Block Header Size.
  66. out[0] = out_size / 4;
  67. // We write Block Flags in pieces.
  68. out[1] = 0x00;
  69. size_t out_pos = 2;
  70. // Compressed Size
  71. if (block->compressed_size != LZMA_VLI_UNKNOWN) {
  72. return_if_error(lzma_vli_encode(block->compressed_size, NULL,
  73. out, &out_pos, out_size));
  74. out[1] |= 0x40;
  75. }
  76. // Uncompressed Size
  77. if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
  78. return_if_error(lzma_vli_encode(block->uncompressed_size, NULL,
  79. out, &out_pos, out_size));
  80. out[1] |= 0x80;
  81. }
  82. // Filter Flags
  83. if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
  84. return LZMA_PROG_ERROR;
  85. size_t filter_count = 0;
  86. do {
  87. // There can be a maximum of four filters.
  88. if (filter_count == LZMA_FILTERS_MAX)
  89. return LZMA_PROG_ERROR;
  90. return_if_error(lzma_filter_flags_encode(
  91. block->filters + filter_count,
  92. out, &out_pos, out_size));
  93. } while (block->filters[++filter_count].id != LZMA_VLI_UNKNOWN);
  94. out[1] |= filter_count - 1;
  95. // Padding
  96. memzero(out + out_pos, out_size - out_pos);
  97. // CRC32
  98. write32le(out + out_size, lzma_crc32(out, out_size, 0));
  99. return LZMA_OK;
  100. }