|
| 1 | +// Copyright 2022-2023 Quarkslab |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * @file LzmaStreambuf.h |
| 17 | + * A std::streambuf that LZMA-compresses data on-the-fly. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef QUOKKA_LZMA_STREAMBUF_H |
| 21 | +#define QUOKKA_LZMA_STREAMBUF_H |
| 22 | + |
| 23 | +#include <cstdint> |
| 24 | +#include <ostream> |
| 25 | +#include <streambuf> |
| 26 | + |
| 27 | +#include <lzma.h> |
| 28 | + |
| 29 | +namespace quokka { |
| 30 | + |
| 31 | +/** |
| 32 | + * A std::streambuf that LZMA-compresses data on-the-fly and writes the |
| 33 | + * compressed output directly to a destination std::ostream. |
| 34 | + * |
| 35 | + * Usage: |
| 36 | + * std::ofstream file("out.bin", std::ios::binary); |
| 37 | + * LzmaStreambuf lzma_buf(file); |
| 38 | + * std::ostream lzma_out(&lzma_buf); |
| 39 | + * protobuf.SerializeToOstream(&lzma_out); |
| 40 | + * lzma_buf.finish(); // flush & finalize the LZMA stream |
| 41 | + */ |
| 42 | +class LzmaStreambuf : public std::streambuf { |
| 43 | + public: |
| 44 | + explicit LzmaStreambuf(std::ostream& dest, |
| 45 | + uint32_t preset = LZMA_PRESET_DEFAULT) |
| 46 | + : dest_(dest), lzma_stream_(LZMA_STREAM_INIT), finished_(false) { |
| 47 | + lzma_ret ret = lzma_easy_encoder(&lzma_stream_, preset, LZMA_CHECK_CRC64); |
| 48 | + if (ret != LZMA_OK) { |
| 49 | + ok_ = false; |
| 50 | + return; |
| 51 | + } |
| 52 | + ok_ = true; |
| 53 | + setp(in_buf_, in_buf_ + kBufSize); |
| 54 | + } |
| 55 | + |
| 56 | + ~LzmaStreambuf() override { |
| 57 | + if (!finished_) finish(); |
| 58 | + lzma_end(&lzma_stream_); |
| 59 | + } |
| 60 | + |
| 61 | + // Non-copyable, non-movable |
| 62 | + LzmaStreambuf(const LzmaStreambuf&) = delete; |
| 63 | + LzmaStreambuf& operator=(const LzmaStreambuf&) = delete; |
| 64 | + |
| 65 | + /// Finalize the LZMA stream (must be called before reading sizes). |
| 66 | + bool finish() { |
| 67 | + if (finished_) return ok_; |
| 68 | + finished_ = true; |
| 69 | + // Flush whatever remains in the put-area, then signal LZMA_FINISH. |
| 70 | + ok_ = flush_to_lzma(LZMA_FINISH) && ok_; |
| 71 | + return ok_; |
| 72 | + } |
| 73 | + |
| 74 | + bool ok() const { return ok_; } |
| 75 | + uint64_t total_in() const { return lzma_stream_.total_in; } |
| 76 | + uint64_t total_out() const { return lzma_stream_.total_out; } |
| 77 | + |
| 78 | + protected: |
| 79 | + int overflow(int ch) override { |
| 80 | + if (!ok_) return EOF; |
| 81 | + // Flush the full buffer first |
| 82 | + if (!flush_to_lzma(LZMA_RUN)) { |
| 83 | + ok_ = false; |
| 84 | + return EOF; |
| 85 | + } |
| 86 | + // Now the buffer is reset, safe to write the new character |
| 87 | + if (ch != EOF) { |
| 88 | + *pptr() = static_cast<char>(ch); |
| 89 | + pbump(1); |
| 90 | + } |
| 91 | + return (ch == EOF) ? 0 : ch; |
| 92 | + } |
| 93 | + |
| 94 | + int sync() override { |
| 95 | + if (!ok_) return -1; |
| 96 | + if (!flush_to_lzma(LZMA_RUN)) { |
| 97 | + ok_ = false; |
| 98 | + return -1; |
| 99 | + } |
| 100 | + return 0; |
| 101 | + } |
| 102 | + |
| 103 | + private: |
| 104 | + static constexpr size_t kBufSize = 65535; |
| 105 | + |
| 106 | + bool flush_to_lzma(lzma_action action) { |
| 107 | + lzma_stream_.next_in = reinterpret_cast<const uint8_t*>(pbase()); |
| 108 | + lzma_stream_.avail_in = static_cast<size_t>(pptr() - pbase()); |
| 109 | + |
| 110 | + do { |
| 111 | + lzma_stream_.next_out = out_buf_; |
| 112 | + lzma_stream_.avail_out = kBufSize; |
| 113 | + |
| 114 | + lzma_ret ret = lzma_code(&lzma_stream_, action); |
| 115 | + if (ret != LZMA_OK && ret != LZMA_STREAM_END) return false; |
| 116 | + |
| 117 | + size_t have = kBufSize - lzma_stream_.avail_out; |
| 118 | + if (have > 0) { |
| 119 | + dest_.write(reinterpret_cast<const char*>(out_buf_), have); |
| 120 | + if (!dest_) return false; |
| 121 | + } |
| 122 | + |
| 123 | + if (ret == LZMA_STREAM_END) break; |
| 124 | + } while (lzma_stream_.avail_in > 0 || lzma_stream_.avail_out == 0); |
| 125 | + |
| 126 | + setp(in_buf_, in_buf_ + kBufSize); |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + std::ostream& dest_; |
| 131 | + lzma_stream lzma_stream_; |
| 132 | + char in_buf_[kBufSize]; |
| 133 | + uint8_t out_buf_[kBufSize]; |
| 134 | + bool ok_; |
| 135 | + bool finished_; |
| 136 | +}; |
| 137 | + |
| 138 | +} // namespace quokka |
| 139 | + |
| 140 | +#endif // QUOKKA_LZMA_STREAMBUF_H |
0 commit comments