CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Data/Encoding/Compression.cpp
Views: 1401
// Little utility functions for data compression.1// Taken from http://panthema.net/2007/0328-ZLibString.html234#include <string>5#include <stdexcept>6#include <iostream>7#include <iomanip>8#include <sstream>9#include <cstring>1011#include <zlib.h>1213#include "Common/Log.h"1415/** Compress a STL string using zlib with given compression level and return16* the binary data. */17bool compress_string(const std::string& str, std::string *dest, int compressionlevel) {18z_stream zs; // z_stream is zlib's control structure19memset(&zs, 0, sizeof(zs));2021if (deflateInit(&zs, compressionlevel) != Z_OK) {22ERROR_LOG(Log::IO, "deflateInit failed while compressing.");23return false;24}2526zs.next_in = (Bytef*)str.data();27zs.avail_in = (uInt)str.size(); // set the z_stream's input2829int ret;30char outbuffer[32768];31std::string outstring;3233// retrieve the compressed bytes blockwise34do {35zs.next_out = reinterpret_cast<Bytef*>(outbuffer);36zs.avail_out = sizeof(outbuffer);3738ret = deflate(&zs, Z_FINISH);3940if (outstring.size() < zs.total_out) {41// append the block to the output string42outstring.append(outbuffer,43zs.total_out - outstring.size());44}45} while (ret == Z_OK);4647deflateEnd(&zs);4849if (ret != Z_STREAM_END) { // an error occurred that was not EOF50std::ostringstream oss;51oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;52return false;53}5455*dest = outstring;56return true;57}5859/** Decompress an STL string using zlib and return the original data. */60bool decompress_string(const std::string& str, std::string *dest) {61if (!str.size())62return false;6364z_stream zs; // z_stream is zlib's control structure65memset(&zs, 0, sizeof(zs));6667// modification by hrydgard: inflateInit2, 16+MAXWBITS makes it read gzip data too68if (inflateInit2(&zs, 32+MAX_WBITS) != Z_OK) {69ERROR_LOG(Log::IO, "inflateInit failed while decompressing.");70return false;71}7273zs.next_in = (Bytef*)str.data();74zs.avail_in = (uInt)str.size();7576int ret;77char outbuffer[32768];78std::string outstring;7980// get the decompressed bytes blockwise using repeated calls to inflate81do {82zs.next_out = reinterpret_cast<Bytef*>(outbuffer);83zs.avail_out = sizeof(outbuffer);8485ret = inflate(&zs, 0);8687if (outstring.size() < zs.total_out) {88outstring.append(outbuffer,89zs.total_out - outstring.size());90}9192} while (ret == Z_OK);9394inflateEnd(&zs);9596if (ret != Z_STREAM_END) { // an error occurred that was not EOF97ERROR_LOG(Log::IO, "Exception during zlib decompression: (%i) %s", ret, zs.msg);98return false;99}100101*dest = outstring;102return true;103}104105106