Skip to content

Adding a method to uncompress a gzipped std::string #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions source/framework/tools/inc/TRestTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class TRestTools {

static std::string POSTRequest(const std::string& url, const std::map<std::string, std::string>& keys);
static void ChangeDirectory(const std::string& toDirectory);

static bool GzipInflate(const std::string& compressedBytes, std::string& uncompressedBytes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should avoid passing mutable references and instead use the method's return. One strong reason to avoid doing so is that these functions are much more difficult to call from the python bindings (it's possible but much more complicated as just passing python objects as strings and so on).

};

namespace REST_InitTools {
Expand Down
69 changes: 69 additions & 0 deletions source/framework/tools/src/TRestTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
#include <curl/curl.h>
#endif

#include "zconf.h"
#include "zlib.h"

#ifdef WIN32
#include <io.h>
#else
Expand Down Expand Up @@ -1264,3 +1267,69 @@ string ToTimeStringLong(double seconds) {
return TString::Format("%.2f %s", seconds / (60.0 * 60.0 * 24.0), "days").Data();
}
}

///////////////////////////////////////////////
/// It will decompress a std::string that has been initialized from a gzip file.
///
bool TRestTools::GzipInflate(const std::string& compressedBytes, std::string& uncompressedBytes) {
if (compressedBytes.size() == 0) {
uncompressedBytes = compressedBytes;
return true;
}

uncompressedBytes.clear();

unsigned full_length = compressedBytes.size();
unsigned half_length = compressedBytes.size() / 2;

unsigned uncompLength = full_length;
char* uncomp = (char*)calloc(sizeof(char), uncompLength);

z_stream strm;
strm.next_in = (Bytef*)compressedBytes.c_str();
strm.avail_in = compressedBytes.size();
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;

bool done = false;

if (inflateInit2(&strm, (16 + MAX_WBITS)) != Z_OK) {
free(uncomp);
return false;
}

while (!done) {
// If our output buffer is too small
if (strm.total_out >= uncompLength) {
// Increase size of output buffer
char* uncomp2 = (char*)calloc(sizeof(char), uncompLength + half_length);
memcpy(uncomp2, uncomp, uncompLength);
uncompLength += half_length;
free(uncomp);
uncomp = uncomp2;
}

strm.next_out = (Bytef*)(uncomp + strm.total_out);
strm.avail_out = uncompLength - strm.total_out;

// Inflate another chunk.
int err = inflate(&strm, Z_SYNC_FLUSH);
if (err == Z_STREAM_END)
done = true;
else if (err != Z_OK) {
break;
}
}

if (inflateEnd(&strm) != Z_OK) {
free(uncomp);
return false;
}

for (size_t i = 0; i < strm.total_out; ++i) {
uncompressedBytes += uncomp[i];
}
free(uncomp);
return true;
}