2017-12-19 00:04:06 +00:00
|
|
|
/**
|
2016-02-11 19:48:58 +00:00
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
2016-01-22 00:35:33 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-12-19 00:04:06 +00:00
|
|
|
* This source code is licensed under both the Apache 2.0 license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2016-01-22 00:35:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstring>
|
2017-04-05 01:48:31 +00:00
|
|
|
#include <string>
|
2016-01-22 00:35:33 +00:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
namespace osquery {
|
|
|
|
|
|
|
|
#define MOD_GZIP_ZLIB_WINDOWSIZE 15
|
|
|
|
#define MOD_GZIP_ZLIB_CFACTOR 9
|
|
|
|
|
2016-05-03 01:03:04 +00:00
|
|
|
std::string compressString(const std::string& data) {
|
2016-01-22 00:35:33 +00:00
|
|
|
z_stream zs;
|
|
|
|
memset(&zs, 0, sizeof(zs));
|
|
|
|
|
|
|
|
if (deflateInit2(&zs,
|
|
|
|
Z_BEST_COMPRESSION,
|
|
|
|
Z_DEFLATED,
|
|
|
|
MOD_GZIP_ZLIB_WINDOWSIZE + 16,
|
|
|
|
MOD_GZIP_ZLIB_CFACTOR,
|
|
|
|
Z_DEFAULT_STRATEGY) != Z_OK) {
|
2016-05-03 01:03:04 +00:00
|
|
|
return std::string();
|
2016-01-22 00:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zs.next_in = (Bytef*)data.data();
|
2016-09-20 21:18:58 +00:00
|
|
|
zs.avail_in = static_cast<uInt>(data.size());
|
2016-01-22 00:35:33 +00:00
|
|
|
|
|
|
|
int ret = Z_OK;
|
|
|
|
std::string output;
|
|
|
|
|
|
|
|
{
|
|
|
|
char buffer[16384] = {0};
|
|
|
|
while (ret == Z_OK) {
|
|
|
|
zs.next_out = reinterpret_cast<Bytef*>(buffer);
|
2017-04-05 01:48:31 +00:00
|
|
|
zs.avail_out = sizeof(buffer);
|
2016-01-22 00:35:33 +00:00
|
|
|
|
|
|
|
ret = deflate(&zs, Z_FINISH);
|
|
|
|
if (output.size() < zs.total_out) {
|
|
|
|
output.append(buffer, zs.total_out - output.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deflateEnd(&zs);
|
|
|
|
if (ret != Z_STREAM_END) {
|
2016-05-03 01:03:04 +00:00
|
|
|
return std::string();
|
2016-01-22 00:35:33 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:03:04 +00:00
|
|
|
return output;
|
2016-01-22 00:35:33 +00:00
|
|
|
}
|
|
|
|
}
|