-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #950 from CesiumGS/gzip
Add `CesiumUtility::gzip`
- Loading branch information
Showing
11 changed files
with
239 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
#include <gsl/span> | ||
|
||
#include <vector> | ||
|
||
namespace CesiumUtility { | ||
|
||
/** | ||
* @brief Checks whether the data is gzipped. | ||
* | ||
* @param data The data. | ||
* | ||
* @returns Whether the data is gzipped | ||
*/ | ||
bool isGzip(const gsl::span<const std::byte>& data); | ||
|
||
/** | ||
* @brief Gzips data. | ||
* | ||
* If successful, it will return true and the result will be in the | ||
* provided vector. | ||
* | ||
* @param data The data to gzip. | ||
* @param out The gzipped data. | ||
* | ||
* @returns True if successful, false otherwise. | ||
*/ | ||
bool gzip(const gsl::span<const std::byte>& data, std::vector<std::byte>& out); | ||
|
||
/** | ||
* @brief Gunzips data. | ||
* | ||
* If successful, it will return true and the result will be in the | ||
* provided vector. | ||
* | ||
* @param data The data to gunzip. | ||
* @param out The gunzipped data. | ||
* | ||
* @returns True if successful, false otherwise. | ||
*/ | ||
bool gunzip( | ||
const gsl::span<const std::byte>& data, | ||
std::vector<std::byte>& out); | ||
|
||
} // namespace CesiumUtility |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include "CesiumUtility/Gzip.h" | ||
|
||
#include "CesiumUtility/Assert.h" | ||
|
||
#include <zlib-ng.h> | ||
|
||
#include <cstring> | ||
|
||
namespace CesiumUtility { | ||
|
||
namespace { | ||
constexpr unsigned int ChunkSize = 65536; | ||
} | ||
|
||
bool isGzip(const gsl::span<const std::byte>& data) { | ||
if (data.size() < 3) { | ||
return false; | ||
} | ||
return data[0] == std::byte{31} && data[1] == std::byte{139}; | ||
} | ||
|
||
bool gzip(const gsl::span<const std::byte>& data, std::vector<std::byte>& out) { | ||
int ret; | ||
unsigned int index = 0; | ||
zng_stream strm; | ||
std::memset(&strm, 0, sizeof(strm)); | ||
|
||
ret = zng_deflateInit2( | ||
&strm, | ||
Z_BEST_COMPRESSION, | ||
Z_DEFLATED, | ||
16 + MAX_WBITS, | ||
8, | ||
Z_DEFAULT_STRATEGY); | ||
if (ret != Z_OK) { | ||
return false; | ||
} | ||
|
||
strm.avail_in = static_cast<uInt>(data.size()); | ||
strm.next_in = reinterpret_cast<const Bytef*>(data.data()); | ||
|
||
do { | ||
out.resize(index + ChunkSize); | ||
strm.next_out = reinterpret_cast<Bytef*>(&out[index]); | ||
strm.avail_out = ChunkSize; | ||
ret = zng_deflate(&strm, Z_NO_FLUSH); | ||
CESIUM_ASSERT(ret != Z_STREAM_ERROR); | ||
index += ChunkSize - strm.avail_out; | ||
} while (strm.avail_in != 0); | ||
|
||
do { | ||
out.resize(index + ChunkSize); | ||
strm.next_out = reinterpret_cast<Bytef*>(&out[index]); | ||
strm.avail_out = ChunkSize; | ||
ret = zng_deflate(&strm, Z_FINISH); | ||
CESIUM_ASSERT(ret != Z_STREAM_ERROR); | ||
index += ChunkSize - strm.avail_out; | ||
} while (ret != Z_STREAM_END); | ||
|
||
zng_deflateEnd(&strm); | ||
out.resize(index); | ||
return true; | ||
} | ||
|
||
bool gunzip( | ||
const gsl::span<const std::byte>& data, | ||
std::vector<std::byte>& out) { | ||
int ret; | ||
unsigned int index = 0; | ||
zng_stream strm; | ||
std::memset(&strm, 0, sizeof(strm)); | ||
|
||
ret = zng_inflateInit2(&strm, 16 + MAX_WBITS); | ||
if (ret != Z_OK) { | ||
return false; | ||
} | ||
|
||
strm.avail_in = static_cast<uInt>(data.size()); | ||
strm.next_in = reinterpret_cast<const Bytef*>(data.data()); | ||
|
||
do { | ||
out.resize(index + ChunkSize); | ||
strm.next_out = reinterpret_cast<Bytef*>(&out[index]); | ||
strm.avail_out = ChunkSize; | ||
ret = zng_inflate(&strm, Z_NO_FLUSH); | ||
CESIUM_ASSERT(ret != Z_STREAM_ERROR); | ||
switch (ret) { | ||
case Z_NEED_DICT: | ||
case Z_DATA_ERROR: | ||
case Z_BUF_ERROR: | ||
case Z_MEM_ERROR: | ||
zng_inflateEnd(&strm); | ||
return false; | ||
} | ||
index += ChunkSize - strm.avail_out; | ||
} while (ret != Z_STREAM_END); | ||
|
||
zng_inflateEnd(&strm); | ||
out.resize(index); | ||
return true; | ||
} | ||
|
||
} // namespace CesiumUtility |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <CesiumNativeTests/readFile.h> | ||
#include <CesiumUtility/Gzip.h> | ||
|
||
#include <catch2/catch.hpp> | ||
|
||
#include <filesystem> | ||
|
||
using namespace CesiumUtility; | ||
|
||
namespace { | ||
std::filesystem::path testDataDir = CesiumUtility_TEST_DATA_DIR; | ||
std::filesystem::path compressedDataPath = | ||
testDataDir / "Gzip" / "CesiumMilkTruck.png.gz"; | ||
std::filesystem::path uncompressedDataPath = | ||
testDataDir / "Gzip" / "CesiumMilkTruck.png"; | ||
std::filesystem::path invalidCompressedDataPath = | ||
testDataDir / "Gzip" / "CesiumMilkTruck.png.gz.invalid"; | ||
|
||
} // namespace | ||
|
||
TEST_CASE("isGzip") { | ||
SECTION("returns true if data is gzipped") { | ||
std::vector<std::byte> compressedData = readFile(compressedDataPath); | ||
CHECK(isGzip(compressedData)); | ||
} | ||
|
||
SECTION("returns false if data is not gzipped") { | ||
std::vector<std::byte> uncompressedData = readFile(uncompressedDataPath); | ||
CHECK(!isGzip(uncompressedData)); | ||
} | ||
} | ||
|
||
TEST_CASE("gzip") { | ||
SECTION("gzips data") { | ||
std::vector<std::byte> uncompressedData = readFile(uncompressedDataPath); | ||
std::vector<std::byte> compressedData; | ||
bool result = gzip(uncompressedData, compressedData); | ||
REQUIRE(result); | ||
CHECK(compressedData.size() < uncompressedData.size()); | ||
CHECK(isGzip(compressedData)); | ||
|
||
std::vector<std::byte> decompressedData; | ||
result = gunzip(compressedData, decompressedData); | ||
REQUIRE(result); | ||
CHECK(decompressedData == uncompressedData); | ||
} | ||
} | ||
|
||
TEST_CASE("gunzip") { | ||
SECTION("gunzips data") { | ||
std::vector<std::byte> compressedData = readFile(compressedDataPath); | ||
std::vector<std::byte> uncompressedData = readFile(uncompressedDataPath); | ||
|
||
std::vector<std::byte> decompressedData; | ||
bool result = gunzip(compressedData, decompressedData); | ||
REQUIRE(result); | ||
CHECK(decompressedData == uncompressedData); | ||
} | ||
|
||
SECTION("returns false for invalid header") { | ||
std::vector<std::byte> invalidCompressedData = | ||
readFile(uncompressedDataPath); | ||
|
||
std::vector<std::byte> decompressedData; | ||
bool result = gunzip(invalidCompressedData, decompressedData); | ||
|
||
CHECK(!result); | ||
} | ||
|
||
SECTION("returns false for truncated data") { | ||
std::vector<std::byte> invalidCompressedData = | ||
readFile(invalidCompressedDataPath); | ||
|
||
std::vector<std::byte> decompressedData; | ||
bool result = gunzip(invalidCompressedData, decompressedData); | ||
|
||
CHECK(!result); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.