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