I am lost with decoding the following base64 string
nVJPb4IwFL/7KUjvAgUM8CIuZiabicsSNR68deXhWKBteGVx336FbJnz4MG+U997/f1L5yTaxsBGn3Rvt0hGK0LPO7eNIhhnBes7BVpQTaBEiwRWwm75soHID8F02mqpGzZZrwpGScZjkUgpMolpFCfRLH/DPKlmaZXGMkqrMq/CMi6Zd8COaq0K5lCYtybqca3ICmVdK+TZlIfTONxzDtEMeHZk3grJ1krY8dW7tQaCgEepH7rikLoTEHaf2AWNPtXqodUlFonDVr++9rpgH1jq82BsusT8eWPa1yd9RLHdf7HFZD4MYBTTXWRwOwJBjnZQxRaDKnKy6tL4RFrWnWzQl7qdBxfIPzwGdlbYnu4I+wrh0Tm9A8U7iKbH28s0EsCulxKJBuLgmvm693f//6sW3w==
It should be valid base64 data representing deflate data of original XML. When I try online decoder here: https://www.samltool.com/decode.php it gives me the proper XML.
I am doing these two steps:
string text = MyClass::decode_base64(input);
text = MyClass::stringDeflateDecode(text);
First I decode the base64 string:
string MyClass::decode_base64(string str)
{
using namespace boost::archive::iterators;
typedef transform_width<binary_from_base64<remove_whitespace<string::const_iterator> >, 8, 6> ItBinaryT;
try {
boost::erase_all(str, "\r");
boost::erase_all(str, "\n");
// If the input isn't a multiple of 4, pad with =
size_t num_pad_chars((4 - str.size() % 4) % 4);
str.append(num_pad_chars, '=');
size_t pad_chars(std::count(str.begin(), str.end(), '='));
std::replace(str.begin(), str.end(), '=', 'A'); // replace '=' by base64 encoding of '\0'
string output(ItBinaryT(str.begin()), ItBinaryT(str.end()));
output.erase(output.end() - pad_chars, output.end());
return output;
} catch (...) {
return string("");
}
}
The code is basically from here Decode Base64 String Using Boost and it was working fine for text-only base64 decoding (no binary deflate data).
Then I would like to decode the deflate:
string MyClass::stringDeflateDecode(const std::string& data)
{
stringstream compressed(data);
stringstream decompressed;
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
in.push(compressed);
boost::iostreams::copy(in, decompressed);
return decompressed.str();
}
but ::copy operation throws an exception: zlib error: iostream error
Thanks for any hints!
That is Base-64 encoded raw deflate data. That means compressed data in the deflate format, but no zlib nor gzip wrapper around that deflate data. It looks like zlib_decompressor
has a noheader
option that you should set to true
.