stream_buffer_encoder.c 4.0 KB

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