-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathinflate.cc
More file actions
81 lines (69 loc) · 2.42 KB
/
inflate.cc
File metadata and controls
81 lines (69 loc) · 2.42 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
#include "libpstack/inflatereader.h"
#include "libpstack/stringify.h"
#include <dlfcn.h>
#include <zlib.h>
namespace {
struct Zlib {
int (*inflateInit2_)(z_streamp, int, const char *, int);
int (*inflate)(z_streamp, int);
int (*inflateEnd)(z_streamp);
};
const Zlib *loadZlib() {
static Zlib zlib;
static const Zlib *result = [] () -> const Zlib * {
void *handle = dlopen("libz.so.1", RTLD_LAZY | RTLD_GLOBAL);
if (!handle)
return nullptr;
zlib.inflateInit2_ = reinterpret_cast<decltype(zlib.inflateInit2_)>(dlsym(handle, "inflateInit2_"));
zlib.inflate = reinterpret_cast<decltype(zlib.inflate)> (dlsym(handle, "inflate"));
zlib.inflateEnd = reinterpret_cast<decltype(zlib.inflateEnd)> (dlsym(handle, "inflateEnd"));
if (!zlib.inflateInit2_ || !zlib.inflate || !zlib.inflateEnd) {
dlclose(handle);
return nullptr;
}
return &zlib;
}();
return result;
}
} // namespace
namespace pstack {
bool zlibAvailable() {
return loadZlib() != nullptr;
}
InflateReader::InflateReader(size_t inflatedSize, const Reader &upstream)
: AbstractMemReader(std::string("inflated content from ") + stringify(upstream))
, data_(inflatedSize)
{
auto *zlib = loadZlib();
if (!zlib)
throw (Exception() << "zlib not available at runtime");
char xferbuf[32768];
z_stream stream{};
int window = 15;
if (zlib->inflateInit2_(&stream, window, ZLIB_VERSION, (int)sizeof stream) != Z_OK)
throw (Exception() << "inflateInit2 failed");
stream.avail_out = inflatedSize;
stream.next_out = reinterpret_cast<Bytef *>(data_.data());
bool eof = false;
size_t inputOffset = 0;
for (bool done = false; !done; ) {
if (stream.avail_in == 0 && !eof) {
stream.avail_in = upstream.read(inputOffset, sizeof xferbuf, xferbuf);
inputOffset += stream.avail_in;
stream.next_in = reinterpret_cast<Bytef *>(xferbuf);
if (stream.avail_in == 0)
eof = true;
}
switch (zlib->inflate(&stream, eof ? Z_FINISH : Z_SYNC_FLUSH)) {
case Z_STREAM_END:
done = true;
[[fallthrough]];
case Z_OK:
break;
default:
throw (Exception() << "inflate failed");
}
}
zlib->inflateEnd(&stream);
}
} // namespace pstack