stream_buffer_encoder.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file stream_buffer_encoder.c
  5. /// \brief Single-call .xz Stream encoder
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "common.h"
  11. #include "index.h"
  12. /// Maximum size of Index that has exactly one Record.
  13. /// Index Indicator + Number of Records + Record + CRC32 rounded up to
  14. /// the next multiple of four.
  15. #define INDEX_BOUND ((1 + 1 + 2 * LZMA_VLI_BYTES_MAX + 4 + 3) & ~3)
  16. /// Stream Header, Stream Footer, and Index
  17. #define HEADERS_BOUND (2 * LZMA_STREAM_HEADER_SIZE + INDEX_BOUND)
  18. extern LZMA_API(size_t)
  19. lzma_stream_buffer_bound(size_t uncompressed_size)
  20. {
  21. // Get the maximum possible size of a Block.
  22. const size_t block_bound = lzma_block_buffer_bound(uncompressed_size);
  23. if (block_bound == 0)
  24. return 0;
  25. // Catch the possible integer overflow and also prevent the size of
  26. // the Stream exceeding LZMA_VLI_MAX (theoretically possible on
  27. // 64-bit systems).
  28. if (my_min(SIZE_MAX, LZMA_VLI_MAX) - block_bound < HEADERS_BOUND)
  29. return 0;
  30. return block_bound + HEADERS_BOUND;
  31. }
  32. extern LZMA_API(lzma_ret)
  33. lzma_stream_buffer_encode(lzma_filter *filters, lzma_check check,
  34. const lzma_allocator *allocator,
  35. const uint8_t *in, size_t in_size,
  36. uint8_t *out, size_t *out_pos_ptr, size_t out_size)
  37. {
  38. // Sanity checks
  39. if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX
  40. || (in == NULL && in_size != 0) || out == NULL
  41. || out_pos_ptr == NULL || *out_pos_ptr > out_size)
  42. return LZMA_PROG_ERROR;
  43. if (!lzma_check_is_supported(check))
  44. return LZMA_UNSUPPORTED_CHECK;
  45. // Note for the paranoids: Index encoder prevents the Stream from
  46. // getting too big and still being accepted with LZMA_OK, and Block
  47. // encoder catches if the input is too big. So we don't need to
  48. // separately check if the buffers are too big.
  49. // Use a local copy. We update *out_pos_ptr only if everything
  50. // succeeds.
  51. size_t out_pos = *out_pos_ptr;
  52. // Check that there's enough space for both Stream Header and
  53. // Stream Footer.
  54. if (out_size - out_pos <= 2 * LZMA_STREAM_HEADER_SIZE)
  55. return LZMA_BUF_ERROR;
  56. // Reserve space for Stream Footer so we don't need to check for
  57. // available space again before encoding Stream Footer.
  58. out_size -= LZMA_STREAM_HEADER_SIZE;
  59. // Encode the Stream Header.
  60. lzma_stream_flags stream_flags = {
  61. .version = 0,
  62. .check = check,
  63. };
  64. if (lzma_stream_header_encode(&stream_flags, out + out_pos)
  65. != LZMA_OK)
  66. return LZMA_PROG_ERROR;
  67. out_pos += LZMA_STREAM_HEADER_SIZE;
  68. // Encode a Block but only if there is at least one byte of input.
  69. lzma_block block = {
  70. .version = 0,
  71. .check = check,
  72. .filters = filters,
  73. };
  74. if (in_size > 0)
  75. return_if_error(lzma_block_buffer_encode(&block, allocator,
  76. in, in_size, out, &out_pos, out_size));
  77. // Index
  78. {
  79. // Create an Index. It will have one Record if there was
  80. // at least one byte of input to encode. Otherwise the
  81. // Index will be empty.
  82. lzma_index *i = lzma_index_init(allocator);
  83. if (i == NULL)
  84. return LZMA_MEM_ERROR;
  85. lzma_ret ret = LZMA_OK;
  86. if (in_size > 0)
  87. ret = lzma_index_append(i, allocator,
  88. lzma_block_unpadded_size(&block),
  89. block.uncompressed_size);
  90. // If adding the Record was successful, encode the Index
  91. // and get its size which will be stored into Stream Footer.
  92. if (ret == LZMA_OK) {
  93. ret = lzma_index_buffer_encode(
  94. i, out, &out_pos, out_size);
  95. stream_flags.backward_size = lzma_index_size(i);
  96. }
  97. lzma_index_end(i, allocator);
  98. if (ret != LZMA_OK)
  99. return ret;
  100. }
  101. // Stream Footer. We have already reserved space for this.
  102. if (lzma_stream_footer_encode(&stream_flags, out + out_pos)
  103. != LZMA_OK)
  104. return LZMA_PROG_ERROR;
  105. out_pos += LZMA_STREAM_HEADER_SIZE;
  106. // Everything went fine, make the new output position available
  107. // to the application.
  108. *out_pos_ptr = out_pos;
  109. return LZMA_OK;
  110. }