Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/lzma_encoder.h" |
| 2 | |
| 3 | #include "glog/logging.h" |
| 4 | |
| 5 | namespace aos::logger { |
| 6 | namespace { |
| 7 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 8 | // Returns true if `status` is not an error code, false if it is recoverable, or |
| 9 | // otherwise logs the appropriate error message and crashes. |
Austin Schuh | ed292dc | 2020-12-22 22:32:59 -0800 | [diff] [blame^] | 10 | bool LzmaCodeIsOk(lzma_ret status, std::string_view filename = "") { |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 11 | switch (status) { |
| 12 | case LZMA_OK: |
| 13 | case LZMA_STREAM_END: |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 14 | return true; |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 15 | case LZMA_MEM_ERROR: |
| 16 | LOG(FATAL) << "Memory allocation failed:" << status; |
| 17 | case LZMA_OPTIONS_ERROR: |
| 18 | LOG(FATAL) << "The given compression preset or decompression options are " |
| 19 | "not supported: " |
| 20 | << status; |
| 21 | case LZMA_UNSUPPORTED_CHECK: |
| 22 | LOG(FATAL) << "The given check type is not supported: " << status; |
| 23 | case LZMA_PROG_ERROR: |
| 24 | LOG(FATAL) << "One or more of the parameters have values that will never " |
| 25 | "be valid: " |
| 26 | << status; |
| 27 | case LZMA_MEMLIMIT_ERROR: |
| 28 | LOG(FATAL) << "Decoder needs more memory than allowed by the specified " |
| 29 | "memory usage limit: " |
| 30 | << status; |
| 31 | case LZMA_FORMAT_ERROR: |
Austin Schuh | ed292dc | 2020-12-22 22:32:59 -0800 | [diff] [blame^] | 32 | if (filename.empty()) { |
| 33 | LOG(FATAL) << "File format not recognized: " << status; |
| 34 | } else { |
| 35 | LOG(FATAL) << "File format of " << filename |
| 36 | << " not recognized: " << status; |
| 37 | } |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 38 | case LZMA_DATA_ERROR: |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 39 | LOG(WARNING) << "Compressed file is corrupt: " << status; |
| 40 | return false; |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 41 | case LZMA_BUF_ERROR: |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 42 | LOG(WARNING) << "Compressed file is truncated or corrupt: " << status; |
| 43 | return false; |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 44 | default: |
| 45 | LOG(FATAL) << "Unexpected return value: " << status; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | LzmaEncoder::LzmaEncoder(const uint32_t compression_preset) |
| 52 | : stream_(LZMA_STREAM_INIT), compression_preset_(compression_preset) { |
| 53 | CHECK_GE(compression_preset_, 0u) |
| 54 | << ": Compression preset must be in the range [0, 9]."; |
| 55 | CHECK_LE(compression_preset_, 9u) |
| 56 | << ": Compression preset must be in the range [0, 9]."; |
| 57 | |
| 58 | lzma_ret status = |
| 59 | lzma_easy_encoder(&stream_, compression_preset_, LZMA_CHECK_CRC64); |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 60 | CHECK(LzmaCodeIsOk(status)); |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 61 | stream_.avail_out = 0; |
| 62 | VLOG(2) << "LzmaEncoder: Initialization succeeded."; |
| 63 | } |
| 64 | |
| 65 | LzmaEncoder::~LzmaEncoder() { lzma_end(&stream_); } |
| 66 | |
| 67 | void LzmaEncoder::Encode(flatbuffers::DetachedBuffer &&in) { |
| 68 | CHECK(in.data()) << ": Encode called with nullptr."; |
| 69 | |
| 70 | stream_.next_in = in.data(); |
| 71 | stream_.avail_in = in.size(); |
| 72 | |
| 73 | RunLzmaCode(LZMA_RUN); |
| 74 | } |
| 75 | |
| 76 | void LzmaEncoder::Finish() { RunLzmaCode(LZMA_FINISH); } |
| 77 | |
| 78 | void LzmaEncoder::Clear(const int n) { |
| 79 | CHECK_GE(n, 0); |
| 80 | CHECK_LE(static_cast<size_t>(n), queue_size()); |
| 81 | queue_.erase(queue_.begin(), queue_.begin() + n); |
| 82 | if (queue_.empty()) { |
| 83 | stream_.next_out = nullptr; |
| 84 | stream_.avail_out = 0; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | std::vector<absl::Span<const uint8_t>> LzmaEncoder::queue() const { |
| 89 | std::vector<absl::Span<const uint8_t>> queue; |
| 90 | if (queue_.empty()) { |
| 91 | return queue; |
| 92 | } |
| 93 | for (size_t i = 0; i < queue_.size() - 1; ++i) { |
| 94 | queue.emplace_back( |
| 95 | absl::MakeConstSpan(queue_.at(i).data(), queue_.at(i).size())); |
| 96 | } |
| 97 | // For the last buffer in the queue, we must account for the possibility that |
| 98 | // the buffer isn't full yet. |
| 99 | queue.emplace_back(absl::MakeConstSpan( |
| 100 | queue_.back().data(), queue_.back().size() - stream_.avail_out)); |
| 101 | return queue; |
| 102 | } |
| 103 | |
| 104 | size_t LzmaEncoder::queued_bytes() const { |
| 105 | size_t bytes = queue_size() * kEncodedBufferSizeBytes; |
| 106 | // Subtract the bytes that the encoder hasn't filled yet. |
| 107 | bytes -= stream_.avail_out; |
| 108 | return bytes; |
| 109 | } |
| 110 | |
| 111 | void LzmaEncoder::RunLzmaCode(lzma_action action) { |
| 112 | CHECK(!finished_); |
| 113 | |
| 114 | // This is to keep track of how many bytes resulted from encoding this input |
| 115 | // buffer. |
| 116 | size_t last_avail_out = stream_.avail_out; |
| 117 | |
| 118 | while (stream_.avail_in > 0 || action == LZMA_FINISH) { |
| 119 | // If output buffer is full, create a new one, queue it up, and resume |
| 120 | // encoding. This could happen in the first call to Encode after |
| 121 | // construction or a Reset, or when an input buffer is large enough to fill |
| 122 | // more than one output buffer. |
| 123 | if (stream_.avail_out == 0) { |
| 124 | queue_.emplace_back(); |
| 125 | queue_.back().resize(kEncodedBufferSizeBytes); |
| 126 | stream_.next_out = queue_.back().data(); |
| 127 | stream_.avail_out = kEncodedBufferSizeBytes; |
| 128 | // Update the byte count. |
| 129 | total_bytes_ += last_avail_out; |
| 130 | last_avail_out = stream_.avail_out; |
| 131 | } |
| 132 | |
| 133 | // Encode the data. |
| 134 | lzma_ret status = lzma_code(&stream_, action); |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 135 | CHECK(LzmaCodeIsOk(status)); |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 136 | if (action == LZMA_FINISH) { |
| 137 | if (status == LZMA_STREAM_END) { |
| 138 | // This is returned when lzma_code is all done. |
| 139 | finished_ = true; |
| 140 | break; |
| 141 | } |
| 142 | } else { |
| 143 | CHECK(status != LZMA_STREAM_END); |
| 144 | } |
| 145 | VLOG(2) << "LzmaEncoder: Encoded chunk."; |
| 146 | } |
| 147 | |
| 148 | // Update the number of resulting encoded bytes. |
| 149 | total_bytes_ += last_avail_out - stream_.avail_out; |
| 150 | } |
| 151 | |
| 152 | LzmaDecoder::LzmaDecoder(std::string_view filename) |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 153 | : dummy_decoder_(filename), stream_(LZMA_STREAM_INIT), filename_(filename) { |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 154 | compressed_data_.resize(kBufSize); |
| 155 | |
| 156 | lzma_ret status = |
| 157 | lzma_stream_decoder(&stream_, UINT64_MAX, LZMA_CONCATENATED); |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 158 | CHECK(LzmaCodeIsOk(status)) << "Failed initializing LZMA stream decoder."; |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 159 | stream_.avail_out = 0; |
| 160 | VLOG(2) << "LzmaDecoder: Initialization succeeded."; |
| 161 | } |
| 162 | |
| 163 | LzmaDecoder::~LzmaDecoder() { lzma_end(&stream_); } |
| 164 | |
| 165 | size_t LzmaDecoder::Read(uint8_t *begin, uint8_t *end) { |
| 166 | if (finished_) { |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | // Write into the given range. |
| 171 | stream_.next_out = begin; |
| 172 | stream_.avail_out = end - begin; |
| 173 | // Keep decompressing until we run out of buffer space. |
| 174 | while (stream_.avail_out > 0) { |
| 175 | if (action_ == LZMA_RUN && stream_.avail_in == 0) { |
| 176 | // Read more bytes from the file if we're all out. |
| 177 | const size_t count = |
| 178 | dummy_decoder_.Read(compressed_data_.begin(), compressed_data_.end()); |
| 179 | if (count == 0) { |
| 180 | // No more data to read in the file, begin the finishing operation. |
| 181 | action_ = LZMA_FINISH; |
| 182 | } else { |
| 183 | stream_.next_in = compressed_data_.data(); |
| 184 | stream_.avail_in = count; |
| 185 | } |
| 186 | } |
| 187 | // Decompress the data. |
| 188 | const lzma_ret status = lzma_code(&stream_, action_); |
| 189 | // Return if we're done. |
| 190 | if (status == LZMA_STREAM_END) { |
| 191 | CHECK_EQ(action_, LZMA_FINISH) |
| 192 | << ": Got LZMA_STREAM_END when action wasn't LZMA_FINISH"; |
| 193 | finished_ = true; |
| 194 | return (end - begin) - stream_.avail_out; |
| 195 | } |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 196 | |
| 197 | // If we fail to decompress, give up. Return everything that has been |
| 198 | // produced so far. |
Austin Schuh | ed292dc | 2020-12-22 22:32:59 -0800 | [diff] [blame^] | 199 | if (!LzmaCodeIsOk(status, filename_)) { |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 200 | finished_ = true; |
| 201 | LOG(WARNING) << filename_ << " is truncated or corrupted."; |
| 202 | return (end - begin) - stream_.avail_out; |
| 203 | } |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 204 | } |
| 205 | return end - begin; |
| 206 | } |
| 207 | |
| 208 | } // namespace aos::logger |