| 1 |
|
|---|
| 2 |
|
|---|
| 3 | #include <iostream>
|
|---|
| 4 | #include <sstream>
|
|---|
| 5 | #include <fstream>
|
|---|
| 6 | #include <boost/iostreams/filtering_streambuf.hpp>
|
|---|
| 7 | #include <boost/iostreams/filtering_stream.hpp>
|
|---|
| 8 | #include <boost/iostreams/copy.hpp>
|
|---|
| 9 | #include <boost/iostreams/filter/zlib.hpp>
|
|---|
| 10 | #include <boost/lexical_cast.hpp>
|
|---|
| 11 | #include <boost/range/iterator_range.hpp>
|
|---|
| 12 |
|
|---|
| 13 | using namespace std;
|
|---|
| 14 | using namespace boost;
|
|---|
| 15 | using namespace boost::iostreams;
|
|---|
| 16 |
|
|---|
| 17 | bool compress(const std::string& source, std::string& dest, size_t destOffset)
|
|---|
| 18 | {
|
|---|
| 19 | #if !0 // this works (at least, no errors have shown so far)
|
|---|
| 20 | filtering_ostream out;
|
|---|
| 21 | out.push(zlib_compressor());
|
|---|
| 22 | out.push(back_inserter(dest));
|
|---|
| 23 |
|
|---|
| 24 | out << source;
|
|---|
| 25 |
|
|---|
| 26 | return true;
|
|---|
| 27 | #elif !0 // this won't work for some strings
|
|---|
| 28 | filtering_istream in;
|
|---|
| 29 | in.push(zlib_compressor());
|
|---|
| 30 | in.push(make_iterator_range(source));
|
|---|
| 31 |
|
|---|
| 32 | ostringstream oss;
|
|---|
| 33 |
|
|---|
| 34 | std::copy(istreambuf_iterator<char>(in), istreambuf_iterator<char>(), ostreambuf_iterator<char>(oss));
|
|---|
| 35 |
|
|---|
| 36 | dest.assign(oss.str());
|
|---|
| 37 |
|
|---|
| 38 | return true;
|
|---|
| 39 | #else // original code by Guy (won't work for some strings)
|
|---|
| 40 | ostringstream _sink;
|
|---|
| 41 | istringstream _source(source);
|
|---|
| 42 |
|
|---|
| 43 | filtering_istreambuf in;
|
|---|
| 44 | zlib_compressor compressor;
|
|---|
| 45 | in.push(compressor);
|
|---|
| 46 | in.push(_source);
|
|---|
| 47 | copy(in, _sink);
|
|---|
| 48 |
|
|---|
| 49 | dest.insert(destOffset, _sink.str());
|
|---|
| 50 |
|
|---|
| 51 | return compressor.unconsumed_input().empty();
|
|---|
| 52 | #endif
|
|---|
| 53 |
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | bool uncompress(const string& source, string& dest, size_t destOffset)
|
|---|
| 57 | {
|
|---|
| 58 | ostringstream _sink;
|
|---|
| 59 | istringstream _source(source);
|
|---|
| 60 |
|
|---|
| 61 | filtering_istreambuf in;
|
|---|
| 62 | zlib_decompressor decompressor;
|
|---|
| 63 | in.push(decompressor);
|
|---|
| 64 | in.push(_source);
|
|---|
| 65 | copy(in, _sink);
|
|---|
| 66 |
|
|---|
| 67 | dest.insert(destOffset, _sink.str());
|
|---|
| 68 |
|
|---|
| 69 | return decompressor.unconsumed_input().empty();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | void SimpleTestCompression(const std::string& OriginalData)
|
|---|
| 73 | {
|
|---|
| 74 |
|
|---|
| 75 | std::string CompressionDest;
|
|---|
| 76 | compress(OriginalData,CompressionDest,0);
|
|---|
| 77 | std::string DecompressionDest;
|
|---|
| 78 | uncompress(CompressionDest,DecompressionDest,0);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | std::string GetBadString()
|
|---|
| 82 | {
|
|---|
| 83 | std::ifstream in("magic.bin", ios_base::binary|ios_base::in);
|
|---|
| 84 |
|
|---|
| 85 | std::string buffer;
|
|---|
| 86 | copy(in, back_inserter(buffer));
|
|---|
| 87 |
|
|---|
| 88 | return buffer;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | int main(int argc, char* argv[])
|
|---|
| 92 | {
|
|---|
| 93 | SimpleTestCompression("Hello123");
|
|---|
| 94 | try {
|
|---|
| 95 | SimpleTestCompression(GetBadString());
|
|---|
| 96 | }
|
|---|
| 97 | catch (const boost::iostreams::zlib_error& e)
|
|---|
| 98 | {
|
|---|
| 99 | std::cout << "What: " << e.what() << " Error: " << e.error() << std::endl;
|
|---|
| 100 | }
|
|---|
| 101 | return 0;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|