-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcfb_util.cpp
More file actions
211 lines (168 loc) · 5.73 KB
/
Copy pathcfb_util.cpp
File metadata and controls
211 lines (168 loc) · 5.73 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <odr/internal/cfb/cfb_util.hpp>
#include <odr/internal/common/file.hpp>
#include <odr/internal/util/string_util.hpp>
#include <algorithm>
#include <string>
#include <utility>
namespace odr::internal::cfb::util {
namespace {
class ReaderBuffer final : public std::streambuf {
public:
ReaderBuffer(const impl::CompoundFileReader &reader,
const impl::CompoundFileEntry &entry)
: m_reader{reader}, m_entry{entry}, m_buffer{new char[m_buffer_size]} {}
ReaderBuffer(const ReaderBuffer &) = delete;
ReaderBuffer(ReaderBuffer &&other) noexcept = delete;
~ReaderBuffer() override { delete[] m_buffer; }
ReaderBuffer &operator=(const ReaderBuffer &) = delete;
ReaderBuffer &operator=(ReaderBuffer &&other) noexcept = delete;
int underflow() override {
const std::uint64_t remaining = m_entry.size - m_offset;
if (remaining <= 0) {
return std::char_traits<char>::eof();
}
const std::uint64_t amount = std::min(remaining, m_buffer_size);
m_reader.read_file(&m_entry, m_offset, m_buffer, amount);
m_offset += amount;
setg(m_buffer, m_buffer, m_buffer + amount);
return std::char_traits<char>::to_int_type(*gptr());
}
private:
const impl::CompoundFileReader &m_reader;
const impl::CompoundFileEntry &m_entry;
std::uint64_t m_offset{0};
std::uint64_t m_buffer_size{4098};
char *m_buffer;
};
class FileInCfbIstream final : public std::istream {
public:
FileInCfbIstream(std::shared_ptr<const Archive> archive,
std::unique_ptr<ReaderBuffer> sbuf)
: std::istream(sbuf.get()), m_archive{std::move(archive)},
m_sbuf{std::move(sbuf)} {}
FileInCfbIstream(std::shared_ptr<const Archive> archive,
const impl::CompoundFileReader &reader,
const impl::CompoundFileEntry &entry)
: FileInCfbIstream(std::move(archive),
std::make_unique<ReaderBuffer>(reader, entry)) {}
private:
std::shared_ptr<const Archive> m_archive;
std::unique_ptr<ReaderBuffer> m_sbuf;
};
class FileInCfb final : public abstract::File {
public:
FileInCfb(std::shared_ptr<const Archive> archive,
const impl::CompoundFileEntry &entry)
: m_archive{std::move(archive)}, m_entry{entry} {}
[[nodiscard]] FileLocation location() const noexcept override {
return m_archive->file()->location();
}
[[nodiscard]] std::size_t size() const override { return m_entry.size; }
[[nodiscard]] std::optional<AbsPath> disk_path() const override {
return std::nullopt;
}
[[nodiscard]] const char *memory_data() const override { return nullptr; }
[[nodiscard]] std::unique_ptr<std::istream> stream() const override {
return std::make_unique<FileInCfbIstream>(m_archive, m_archive->cfb(),
m_entry);
}
private:
std::shared_ptr<const Archive> m_archive;
const impl::CompoundFileEntry &m_entry;
};
} // namespace
bool Archive::Entry::is_file() const { return m_entry->is_stream(); }
bool Archive::Entry::is_directory() const { return !m_entry->is_stream(); }
AbsPath Archive::Entry::path() const { return m_path; }
std::unique_ptr<abstract::File> Archive::Entry::file() const {
if (!is_file()) {
return {};
}
return std::make_unique<FileInCfb>(m_parent->shared_from_this(), *m_entry);
}
std::string Archive::Entry::name() const {
return internal::util::string::c16str_to_string(
reinterpret_cast<const char16_t *>(m_entry->name), m_entry->name_len - 2);
}
std::optional<Archive::Entry> Archive::Entry::left() const {
const auto *left = m_parent->cfb().get_entry(m_entry->left_sibling_id);
if (left == nullptr) {
return {};
}
return Entry(*m_parent, *left, m_path.parent());
}
std::optional<Archive::Entry> Archive::Entry::right() const {
const auto *right = m_parent->cfb().get_entry(m_entry->right_sibling_id);
if (right == nullptr) {
return {};
}
return Entry(*m_parent, *right, m_path.parent());
}
std::optional<Archive::Entry> Archive::Entry::child() const {
const auto *child = m_parent->cfb().get_entry(m_entry->child_id);
if (child == nullptr) {
return {};
}
return Entry(*m_parent, *child, m_path);
}
void Archive::Iterator::dig_left_() {
if (!m_entry.has_value()) {
return;
}
while (true) {
const std::optional<Entry> left = m_entry->left();
if (!left.has_value()) {
break;
}
m_ancestors.push_back(*m_entry);
m_entry = left;
}
}
void Archive::Iterator::next_() {
if (!m_entry.has_value()) {
return;
}
if (const std::optional<Entry> child = m_entry->child(); child.has_value()) {
m_directories.push_back(*m_entry);
m_entry = child;
dig_left_();
return;
}
next_flat_();
}
void Archive::Iterator::next_flat_() {
if (!m_entry.has_value()) {
return;
}
if (const std::optional<Entry> right = m_entry->right(); right.has_value()) {
m_entry = right;
dig_left_();
return;
}
if (!m_ancestors.empty()) {
m_entry = m_ancestors.back();
m_ancestors.pop_back();
return;
}
if (!m_directories.empty()) {
m_entry = m_directories.back();
m_directories.pop_back();
next_flat_();
return;
}
m_entry = {};
}
Archive::Archive(const std::shared_ptr<MemoryFile> &file)
: m_file{file}, m_cfb{file->content().data(), file->content().size()} {}
const impl::CompoundFileReader &Archive::cfb() const { return m_cfb; }
std::shared_ptr<abstract::File> Archive::file() const { return m_file; }
Archive::Iterator Archive::begin() const {
return {*this, *m_cfb.get_root_entry()};
}
Archive::Iterator Archive::end() const { return {}; }
Archive::Iterator Archive::find(const AbsPath &path) const {
return std::find_if(begin(), end(), [&path](const Entry &entry) {
return entry.path() == path;
});
}
} // namespace odr::internal::cfb::util