block_header_encoder.c 3.2 KB

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