Browse Source

Replace string::uncompress() with a more C++y one.

Edit documentation.
tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
4583ec7e73
2 changed files with 28 additions and 5 deletions
  1. +2
    -3
      include/string.hpp
  2. +26
    -2
      src/string.cpp

+ 2
- 3
include/string.hpp View File

@@ -77,11 +77,10 @@ std::vector<uint8_t> compress(const uint8_t* data, size_t dataLen);
std::vector<uint8_t> compress(const std::vector<uint8_t>& data);
/** Uncompress bytes with zlib.
Before calling this function, set `dataLen` to the capacity of `data`.
After returning, `dataLen` is set to the number of bytes written.
Unfortunately the output buffer cannot be computed from the compressed data, so you may need to hard-code the maximum expected size.
After returning, `dataLen` is set to the actual number of bytes written.
*/
void uncompress(const uint8_t* compressed, size_t compressedLen, uint8_t* data, size_t* dataLen);
void uncompress(const std::vector<uint8_t>& compressed, uint8_t* data, size_t* dataLen);
std::vector<uint8_t> uncompress(const std::vector<uint8_t>& compressed);


struct CaseInsensitiveCompare {


+ 26
- 2
src/string.cpp View File

@@ -263,8 +263,32 @@ void uncompress(const uint8_t* compressed, size_t compressedLen, uint8_t* data,
}


void uncompress(const std::vector<uint8_t>& compressed, uint8_t* data, size_t* dataLen) {
uncompress(compressed.data(), compressed.size(), data, dataLen);
std::vector<uint8_t> uncompress(const std::vector<uint8_t>& compressed) {
// We don't know the uncompressed size, so we can't use the easy compress/uncompress API.
std::vector<uint8_t> data;

z_stream zs;
std::memset(&zs, 0, sizeof(zs));
zs.next_in = (Bytef*) &compressed[0];
zs.avail_in = compressed.size();
inflateInit(&zs);

while (true) {
uint8_t buffer[16384];
zs.next_out = (Bytef*) buffer;
zs.avail_out = sizeof(buffer);
int err = inflate(&zs, Z_NO_FLUSH);

if (err < 0)
throw Exception(string::f("zlib error %d", err));

data.insert(data.end(), buffer, zs.next_out);
if (err == Z_STREAM_END)
break;
}

inflateEnd(&zs);
return data;
}




Loading…
Cancel
Save