-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlib_test.cpp
136 lines (122 loc) · 2.79 KB
/
zlib_test.cpp
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
//
//
//
#include <array>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "zlib.hpp"
namespace
{
class zinp : public ZLIB::input
{
public:
struct buff
{
Bytef* ptr;
size_t sz;
buff() {}
buff(const buff& b)
{
ptr = b.ptr;
sz = b.sz;
}
template <class T>
buff(T& v)
{
ptr = v.data();
sz = v.size();
}
void update(size_t s)
{
ptr += s;
sz -= s;
}
};
private:
buff src;
buff dst;
size_t wsz;
std::ofstream& ofstream;
public:
zinp(const buff& i, const buff& o, std::ofstream& ofs) : src(i), dst(o), wsz(0), ofstream(ofs) {}
void* getSource() override { return src.ptr; }
void* getDestination() override { return dst.ptr; }
size_t getRemainSize() const override { return src.sz; }
size_t getWritableSize() const override { return dst.sz; }
bool runOut(size_t rsize, size_t wsize) override
{
ofstream.write((char*)dst.ptr, wsize);
src.update(rsize);
std::cout << "read: " << rsize << ", write: " << wsize << std::endl;
return src.sz == 0;
}
void flush(size_t wsize) override
{
ofstream.write((char*)dst.ptr, wsize);
std::cout << "flush: " << wsize << std::endl;
}
void complete(bool ok, size_t tws) override
{
std::cout << "write: " << tws << ", " << (ok ? "SUCCESS" : "FAILES") << std::endl;
wsz = tws;
}
void error() override { std::cerr << "error!" << std::endl; }
size_t getTotalSize() const { return wsz; }
};
} // namespace
//
int
main(int argc, char** argv)
{
std::string ifname, ofname;
bool uncompress = false;
for (int i = 1; i < argc; i++)
{
std::string a = argv[i];
if (a == "-d")
uncompress = true;
else if (ifname.empty())
ifname = a;
else if (ofname.empty())
ofname = a;
}
if (ifname.empty() || ofname.empty())
{
std::cerr << "no input filename." << std::endl;
return 1;
}
std::ifstream ifs{ifname, std::ios::binary | std::ios::ate};
if (ifs.good() == false)
{
std::cerr << "open error<input>: " << ifname << std::endl;
return 1;
}
std::ofstream ofs{ofname, std::ios::binary};
if (ofs.good() == false)
{
std::cerr << "open error<output>: " << ofname << std::endl;
return 1;
}
auto isz = ifs.tellg();
ifs.seekg(0);
std::cout << "input file size: " << isz << std::endl;
std::vector<Bytef> ibuffer;
ibuffer.resize(isz);
ifs.read((char*)ibuffer.data(), ibuffer.size());
std::array<Bytef, 4096> obuffer;
ZLIB::zlib zlib{uncompress};
zinp::buff isrc{ibuffer}, idst{obuffer};
zinp zi{isrc, idst, ofs};
if (uncompress)
zlib.uncompress(zi);
else
zlib.compress(zi);
std::cout << "work-size: " << zlib.getWorkSize() << std::endl;
return 0;
}
//
// End
//