Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decompress file from Boost filtering_streambuf to std::vector<unsigned char>?

I am writing a program that performs ssl 3.1 encryption decryption data exchange. I receive data which is also compressed in the gzip format. I am able to decrypt this data and print it to my console, using this chuck of code :

void decompress(unsigned char * arr, int nCopySize, std::vector <unsigned char>& vecArg)
{
    using namespace boost::iostreams;
    using namespace boost::interprocess;
    std::string str1(reinterpret_cast<const char *>(arr), nCopySize);
    auto sizeT = str1.size();
    stringstream file2(str1, ios_base::in | ios_base::binary);
    filtering_streambuf<input> iin;
    iin.push(gzip_decompressor());
    iin.push(file2);
    boost::iostreams::copy(iin, cout);

//stops working here (the decompressed code was successfully printed in console)

    boost::interprocess::basic_vectorstream<std::vector<char>> vectorStream;
    boost::iostreams::copy(iin, vectorStream); // this produces compile time error
    std::vector<unsigned char> chars(
        vectorStream.vector().begin(),
        vectorStream.vector().end()
        );


}

I am trying to store the decompressed code into an unsigned char vector but while using the last chunk of code found here : Decompress file from Boost filtering_streambuf to std::vector<char>? I am having this compile time error :

Error   5   error C2243: 'type cast' : conversion from 'boost::interprocess::basic_vectorstream<std::vector<char,std::allocator<char>>,std::char_traits<char>> *' to 'volatile const std::basic_streambuf<char,std::char_traits<char>> *' exists, but is inaccessible   c:\boost_1_59_0\boost\iostreams\traits.hpp  57  1   TeraLauncher

I have done other tries, using char arrays or string, managed to compile but ran into run time crash. Ideally, I would like it to work with the code I have written (std::vector). Thanks

like image 936
Glenn Ulysse Avatar asked Mar 13 '23 02:03

Glenn Ulysse


1 Answers

Live On Coliru

#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/device/array.hpp>
#include <iostream>
#include <vector>
#include <iterator>

namespace bio = boost::iostreams;

void decompress(unsigned char const* arr, std::size_t nCopySize, std::vector <unsigned char>& vecArg) {
    bio::array_source arrs{ reinterpret_cast<char const*>(arr), nCopySize };

    bio::filtering_istreambuf iin;
    iin.push(bio::gzip_decompressor{});
    iin.push(arrs);

    vecArg.assign(std::istreambuf_iterator<char>{&iin}, {});
}

int main() {
    /*
     * If you really want to hear about it, the first thing you'll probably want to know is
     * where I was born, an what my lousy childhood was like, and how my parents were
     * occupied and all before they had me, and all that David Copperfield kind of crap, but I
     * don't feel like going into it, if you want to know the truth. In the first place, that stuff
     * bores me, and in the second place, my parents would have about two hemorrhages apiece
     * if I told anything pretty personal about them. They're quite touchy about anything like
     * that, especially my father.
     */
    unsigned char sample[] = {
        0x1f, 0x8b, 0x08, 0x00, 0x8a, 0x47, 0xd7, 0x56, 0x00, 0x03, 0x55, 0x52, 0x4b, 0x76, 0xc3, 0x20, 0x10, 0xdb, 0xfb, 0x14, 0xda, 0x65, 0xe3, 0x97,
        0x4b, 0xb4, 0x1b, 0xef, 0x7b, 0x81, 0x31, 0x0c, 0x81, 0x17, 0xc2, 0x50, 0x3e, 0xf5, 0xf3, 0xed, 0x3b, 0xd8, 0x69, 0x5e, 0xba, 0x84, 0x91, 0x34,
        0x92, 0x60, 0x71, 0xd8, 0xa5, 0xa3, 0x30, 0xc5, 0xb8, 0x63, 0xa3, 0xd4, 0xd0, 0x04, 0x9e, 0xa9, 0x80, 0x56, 0xe9, 0x0d, 0xa1, 0xcd, 0x68, 0x9e,
        0xe1, 0x42, 0xa9, 0x3a, 0xf3, 0x21, 0xdd, 0x06, 0xe3, 0x12, 0x23, 0x72, 0x91, 0x95, 0xd6, 0x37, 0xda, 0x3d, 0xc9, 0x86, 0x50, 0xa7, 0xcd, 0x73,
        0x61, 0x2c, 0x7a, 0x5f, 0xb1, 0x4a, 0x49, 0x33, 0x28, 0x61, 0xf3, 0xd4, 0xf0, 0xd8, 0x11, 0xa5, 0xd7, 0x1d, 0xc6, 0x87, 0x68, 0xbd, 0x88, 0x3d,
        0x40, 0x31, 0xdc, 0x79, 0x80, 0x2c, 0xbc, 0x2a, 0x28, 0x28, 0x53, 0xe1, 0xd4, 0x2a, 0x36, 0x15, 0x9a, 0xc4, 0x98, 0x9e, 0x03, 0xdb, 0x03, 0xa0,
        0x3e, 0xb1, 0xb2, 0x13, 0x5d, 0xa0, 0xb6, 0x76, 0x78, 0xb2, 0x78, 0x3c, 0xc9, 0x63, 0xd6, 0xc6, 0x9a, 0x4f, 0xfa, 0x09, 0x16, 0x1f, 0x92, 0x33,
        0x17, 0x17, 0x38, 0x5a, 0xdc, 0x83, 0xce, 0xc5, 0xc1, 0x14, 0xca, 0x33, 0x56, 0x0d, 0xb6, 0x4c, 0x56, 0xd2, 0xa5, 0xc1, 0x31, 0xc7, 0xc3, 0x00,
        0x6e, 0x32, 0xc2, 0x85, 0xa4, 0x49, 0x46, 0xea, 0x70, 0x56, 0xf3, 0x2f, 0xdc, 0x68, 0xa2, 0x95, 0xde, 0xfc, 0x15, 0x4b, 0x7a, 0xeb, 0x25, 0x47,
        0x32, 0x3c, 0x9f, 0xcb, 0x6b, 0xeb, 0xce, 0x4d, 0x9a, 0x9b, 0xeb, 0xcb, 0x59, 0x38, 0xc1, 0x95, 0x8d, 0xe8, 0xe9, 0x89, 0x7e, 0xcf, 0x29, 0x5d,
        0x4d, 0x7a, 0xfa, 0xe1, 0x67, 0xed, 0x6d, 0x1b, 0xaf, 0xf0, 0x90, 0x52, 0x3c, 0xdd, 0x54, 0x88, 0xb4, 0x00, 0xc3, 0x93, 0x7a, 0x5a, 0xd4, 0x4c,
        0x1c, 0x5d, 0xec, 0xe7, 0x63, 0xe4, 0xc2, 0xad, 0xa9, 0x12, 0x97, 0x2a, 0x89, 0xe2, 0x1f, 0x5f, 0xc9, 0x57, 0x7c, 0x69, 0x43, 0x17, 0x6d, 0xea,
        0xbb, 0x87, 0xa6, 0xc6, 0xa5, 0x1b, 0xbf, 0x3f, 0x01, 0x2f, 0xfe, 0x88, 0x3e, 0x0d, 0xe3, 0x33, 0xb8, 0x66, 0x36, 0xe1, 0xf8, 0x09, 0xea, 0xcd,
        0x91, 0x8a, 0x94, 0xeb, 0xf4, 0x0b, 0xb2, 0xb3, 0x9b, 0x5a, 0x23, 0x02, 0x00, 0x00 };

    std::vector<unsigned char> d;
    decompress(sample, sizeof(sample), d);

    std::cout.write(reinterpret_cast<char const*>(&*d.begin()), d.size());
}

Prints

If you really want to hear about it, the first thing you'll probably want to know is
where I was born, an what my lousy childhood was like, and how my parents were
occupied and all before they had me, and all that David Copperfield kind of crap, but I
don't feel like going into it, if you want to know the truth. In the first place, that stuff
bores me, and in the second place, my parents would have about two hemorrhages apiece
if I told anything pretty personal about them. They're quite touchy about anything like
that, especially my father.
like image 64
sehe Avatar answered Mar 16 '23 07:03

sehe