Skip to content

Commit

Permalink
Merge pull request CesiumGS#798 from CesiumGS/upgrade-to-zlib-ng
Browse files Browse the repository at this point in the history
Replace zlib with zlib-ng
  • Loading branch information
kring authored Feb 15, 2024
2 parents bec474f + c70998f commit 09c1eb7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
[submodule "extern/libjpeg-turbo"]
path = extern/libjpeg-turbo
url = https://github.com/CesiumGS/libjpeg-turbo.git
[submodule "extern/zlib"]
path = extern/zlib
url = https://github.com/madler/zlib.git
[submodule "extern/meshoptimizer"]
path = extern/meshoptimizer
url = https://github.com/zeux/meshoptimizer
[submodule "extern/zlib-ng"]
path = extern/zlib-ng
url = https://github.com/zlib-ng/zlib-ng.git
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- Added `contains` method to `BoundingSphere`.
- Added `GlobeRectangle::MAXIMUM` static field.
- Switched from `zlib` to `zlib-ng` in order to improve the performance of decompressing gzipped data.

##### Fixes :wrench:

Expand Down
12 changes: 6 additions & 6 deletions CesiumUtility/src/Gunzip.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "CesiumUtility/Gunzip.h"
#define ZLIB_CONST
#include "zlib.h"
#include "zlib-ng.h"

#define CHUNK 65536

Expand All @@ -16,13 +16,13 @@ bool CesiumUtility::gunzip(
std::vector<std::byte>& out) {
int ret;
unsigned int index = 0;
z_stream strm;
zng_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit2(&strm, 16 + MAX_WBITS);
ret = zng_inflateInit2(&strm, 16 + MAX_WBITS);
if (ret != Z_OK) {
return false;
}
Expand All @@ -34,18 +34,18 @@ bool CesiumUtility::gunzip(
out.resize(index + CHUNK);
strm.next_out = reinterpret_cast<Bytef*>(&out[index]);
strm.avail_out = CHUNK;
ret = inflate(&strm, Z_NO_FLUSH);
ret = zng_inflate(&strm, Z_NO_FLUSH);
switch (ret) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
inflateEnd(&strm);
zng_inflateEnd(&strm);
return false;
}
index += CHUNK - strm.avail_out;
} while (ret != Z_STREAM_END);

inflateEnd(&strm);
zng_inflateEnd(&strm);
out.resize(index);
return true;
}
48 changes: 48 additions & 0 deletions CesiumUtility/test/TestGunzip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "CesiumUtility/Gunzip.h"

#include <catch2/catch.hpp>

#include <algorithm>

using namespace CesiumUtility;

// "hello world!" gzipped, without file name or date info
static std::byte testZippedArray[] = {
std::byte{0x1f}, std::byte{0x8b}, std::byte{0x08}, std::byte{0x00},
std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00},
std::byte{0x00}, std::byte{0x0b}, std::byte{0xcb}, std::byte{0x48},
std::byte{0xcd}, std::byte{0xc9}, std::byte{0xc9}, std::byte{0x57},
std::byte{0x28}, std::byte{0xcf}, std::byte{0x2f}, std::byte{0xca},
std::byte{0x49}, std::byte{0x51}, std::byte{0x04}, std::byte{0x00},
std::byte{0x6d}, std::byte{0xc2}, std::byte{0xb4}, std::byte{0x03},
std::byte{0x0c}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}};
static const gsl::span<std::byte> testZipped(testZippedArray, 32);

// "hello world!" in ASCII
static std::byte testUnzippedArray[] = {
std::byte{0x68},
std::byte{0x65},
std::byte{0x6c},
std::byte{0x6c},
std::byte{0x6f},
std::byte{0x20},
std::byte{0x77},
std::byte{0x6f},
std::byte{0x72},
std::byte{0x6c},
std::byte{0x64},
std::byte{0x21}};
static const gsl::span<std::byte> testUnzipped(testUnzippedArray, 12);

TEST_CASE("CesiumUtility::isGzip") {
CHECK(CesiumUtility::isGzip(testZipped));
CHECK(!CesiumUtility::isGzip(testUnzipped));
}

TEST_CASE("CesiumUtility::gunzip") {
std::vector<std::byte> outVector;
CHECK(CesiumUtility::gunzip(testZipped, outVector));
CHECK(outVector.size() == testUnzipped.size());
CHECK(std::equal(outVector.begin(), outVector.end(), testUnzipped.begin()));
CHECK(!CesiumUtility::gunzip(testUnzipped, outVector));
}
5 changes: 3 additions & 2 deletions extern/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ add_subdirectory(libmorton)

# The zlib cmake build makes its working directory dirty.
# So we make a copy of it to avoid that annoyance.
file(COPY "${CMAKE_CURRENT_LIST_DIR}/zlib/" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/zlib-src")
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/zlib-src ${CMAKE_CURRENT_BINARY_DIR}/zlib)
set(WITH_GTEST OFF CACHE INTERNAL "Disable zlib-ng testing")
file(COPY "${CMAKE_CURRENT_LIST_DIR}/zlib-ng/" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/zlib-ng-src")
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/zlib-ng-src ${CMAKE_CURRENT_BINARY_DIR}/zlib-ng)

if(DEFINED CMAKE_TOOLCHAIN_FILE)
if(NOT IS_ABSOLUTE ${CMAKE_TOOLCHAIN_FILE})
Expand Down
1 change: 0 additions & 1 deletion extern/zlib
Submodule zlib deleted from 04f42c
1 change: 1 addition & 0 deletions extern/zlib-ng
Submodule zlib-ng added at 742537

0 comments on commit 09c1eb7

Please sign in to comment.