Skip to content

Commit 7eea782

Browse files
committed
zst compressed JRMAN
1 parent 53d54f3 commit 7eea782

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

lib/rlib/rmanifest.cpp

+43-9
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,61 @@ auto RMAN::read_jrman(std::span<char const> data, Filter const& filter) -> RMAN
372372
while (!iter.empty()) {
373373
std::tie(line, iter) = str_split(iter, '\n');
374374
line = str_strip(line);
375-
if (line.empty()) {
376-
continue;
377-
}
378-
auto file = File::undump(line);
379-
if (!filter(file)) {
380-
files.push_back(std::move(file));
375+
if (!line.empty() && line != "JRMAN") {
376+
auto file = File::undump(line);
377+
if (!filter(file)) {
378+
files.push_back(std::move(file));
379+
}
381380
}
382381
}
383382
return RMAN{.files = std::move(files)};
384383
}
385384

386385
auto RMAN::read_zrman(std::span<char const> data, Filter const& filter) -> RMAN {
387-
rlib_assert(!"not implemented");
388-
return {};
386+
auto files = std::vector<RMAN::File>{};
387+
auto BUF_SIZE = (128u + 32u) * MiB;
388+
389+
auto ctx = std::shared_ptr<ZSTD_DCtx>(ZSTD_createDCtx(), &ZSTD_freeDCtx);
390+
auto buffer = std::make_unique<char[]>(BUF_SIZE);
391+
392+
auto src = ZSTD_inBuffer{data.data(), data.size()};
393+
auto dst = ZSTD_outBuffer{buffer.get(), BUF_SIZE};
394+
395+
while (src.pos != src.size) {
396+
rlib_assert_zstd(ZSTD_decompressStream(ctx.get(), &dst, &src));
397+
auto start = buffer.get();
398+
auto const end = buffer.get() + dst.pos;
399+
for (;;) {
400+
auto new_line = std::find(start, end, '\n');
401+
if (new_line == end) {
402+
break;
403+
}
404+
auto line = str_strip({start, (std::size_t)(new_line - start)});
405+
if (!line.empty() && line != "JRMAN") {
406+
auto file = File::undump(line);
407+
if (!filter(file)) {
408+
files.push_back(std::move(file));
409+
}
410+
}
411+
start = new_line + 1;
412+
}
413+
if (start != buffer.get() && (end - start) != 0) {
414+
std::memmove(buffer.get(), start, end - start);
415+
}
416+
dst.dst = buffer.get();
417+
dst.pos = end - start;
418+
}
419+
return RMAN{.files = std::move(files)};
389420
}
390421

391422
auto RMAN::read(std::span<char const> data, Filter const& filter) -> RMAN {
392423
rlib_assert(data.size() >= 5);
393-
if (std::memcmp(data.data(), "ZRMAN", 5) == 0) {
424+
if (std::memcmp(data.data(), "JRMAN", 5) == 0) {
394425
return read_jrman(data, filter);
395426
}
427+
if (std::memcmp(data.data(), "\x28\xB5\x2F\xFD", 4) == 0) {
428+
return read_zrman(data, filter);
429+
}
396430
return Raw{}.parse(data);
397431
}
398432

0 commit comments

Comments
 (0)