11 #include <boost/scoped_array.hpp>
13 #include <epicsExport.h>
19 epicsShareFunc
int epicsShareAPI
compressString(
const std::string& str, std::string& comp_str)
21 uLong len = str.size();
22 uLong comprLen = compressBound(len);
23 boost::scoped_array<Byte> compr(
new Byte[comprLen]);
25 int err = compress(compr.get(), &comprLen, (
const Bytef*)str.c_str(), len);
28 std::cerr <<
"compressString: compress failed with error " << err << std::endl;
31 std::ostringstream oss;
32 for(
int i=0; i<comprLen; ++i)
34 oss << std::hex << std::setfill ('0') << std::setw (2) << static_cast<unsigned>(compr[i]);
41 epicsShareFunc
int epicsShareAPI
uncompressString(
const std::string& comp_str, std::string& str)
43 int length = comp_str.length();
51 std::cerr <<
"uncompressString: input length not an even number of characters" << std::endl;
54 if (comp_str.find_first_of(
" \t\r\n") != std::string::npos)
56 std::cerr <<
"uncompressString: whitespace characters found, did you forget the \"-t\" or \"-S\" options for caget?" << std::endl;
59 std::vector<Byte> compr;
60 compr.reserve(length/2);
61 for(
int i=0; i < length; i+=2)
63 compr.push_back(static_cast<Byte>(strtol(comp_str.substr(i,2).c_str(), NULL, 16)));
66 boost::scoped_array<Byte> uncompr;
67 int err = Z_BUF_ERROR;
68 for(
int i=1; err == Z_BUF_ERROR && i<100; i *= 2)
70 uncomprLen = std::max(length * 20 * i, 65536);
71 uncompr.reset(
new Byte[uncomprLen]);
72 err = uncompress(uncompr.get(), &uncomprLen, &(compr[0]), compr.size());
76 std::cerr <<
"uncompressString: uncompress failed with error " << err <<
" (length=" << length <<
",uncomprLen=" << uncomprLen <<
")" << std::endl;
79 str.reserve(uncomprLen);
80 for(
int i=0; (i < uncomprLen); ++i)
82 str.push_back(static_cast<char>(uncompr[i]));
epicsShareFunc int epicsShareAPI compressString(const std::string &str, std::string &comp_str)
compress a string usinf zlib and then convert compressed bytes into an ascii hex sequence suitable fo...
epicsShareFunc int epicsShareAPI uncompressString(const std::string &comp_str, std::string &str)
uncompress a string created using compressString()