summaryrefslogtreecommitdiff
path: root/src/decompress_lzma.cc
blob: 6baea18af0a5fb0a2ad4885f30eb0b391ef09bad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "decompress.hh"

#include "buffer.hh"

#include <lzma.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <expected>
#include <memory>
#include <optional>
#include <utility>

namespace decompress {

namespace {

const size_t kBufferSizeXz = static_cast<size_t>(1024) * 1024;

class XzReader : public io::Reader {
 public:
  explicit XzReader(std::unique_ptr<io::Reader> reader)
      : reader_(std::move(reader)) {}

  ~XzReader() override {
    if (initialized_)
      lzma_end(&stream_);
  }

  std::expected<size_t, io::ReadError> read(void* dst, size_t max) override {
    auto err = fill();
    if (err.has_value())
      return std::unexpected(err.value());

    stream_.next_out = reinterpret_cast<unsigned char*>(dst);
    stream_.avail_out = max;

    if (!initialized_) {
      if (in_eof_ && buffer_->empty())
        return 0;

      lzma_mt options;
      memset(&options, 0, sizeof(options));
      options.threads = std::max(static_cast<uint32_t>(1), lzma_cputhreads());
      options.memlimit_threading = lzma_physmem() / 4;
      options.memlimit_stop = lzma_physmem() / 4;
      auto ret = lzma_stream_decoder_mt(&stream_, &options);
      if (ret != LZMA_OK)
        return std::unexpected(io::ReadError::Error);
      initialized_ = true;
    }

    auto* const rptr = stream_.next_in;
    auto ret = lzma_code(&stream_, in_eof_ ? LZMA_FINISH : LZMA_RUN);
    auto got = max - stream_.avail_out;
    if (ret == LZMA_STREAM_END) {
      lzma_end(&stream_);
      initialized_ = false;
      buffer_->consume(stream_.next_in - rptr);
    } else if (ret == LZMA_OK) {
      if (!in_eof_)
        buffer_->consume(stream_.next_in - rptr);
    } else {
      return std::unexpected(
          ret == LZMA_DATA_ERROR
          ? io::ReadError::InvalidData : io::ReadError::Error);
    }
    return got;
  }

  std::expected<size_t, io::ReadError> skip(size_t max) override {
    auto tmp = std::make_unique_for_overwrite<char[]>(max);
    return read(tmp.get(), max);
  }

 private:
  std::optional<io::ReadError> fill() {
    auto* rptr = buffer_->rptr(stream_.avail_in);
    if (!in_eof_ && stream_.avail_in < kBufferSizeXz / 2) {
      auto* wptr = buffer_->wptr(stream_.avail_in);
      auto got = reader_->read(wptr, stream_.avail_in);
      if (got.has_value()) {
        buffer_->commit(got.value());
        if (got.value() == 0)
          in_eof_ = true;
      } else {
        return got.error();
      }
      rptr = buffer_->rptr(stream_.avail_in);
    }
    stream_.next_in = reinterpret_cast<const unsigned char*>(rptr);
    return std::nullopt;
  }

  std::unique_ptr<io::Reader> reader_;
  bool in_eof_{false};
  std::unique_ptr<Buffer> buffer_{Buffer::fixed(kBufferSizeXz)};
  bool initialized_{false};
  lzma_stream stream_ = LZMA_STREAM_INIT;
};

}  // namespace

std::unique_ptr<io::Reader> xz(std::unique_ptr<io::Reader> reader) {
  return std::make_unique<XzReader>(std::move(reader));
}

}  // namespace decompress