From 4b3d889bcb389caf803878906c7b1a077d4ac6b3 Mon Sep 17 00:00:00 2001 From: Ashley Rogers Date: Tue, 30 Jan 2024 14:48:14 -0500 Subject: [PATCH 1/4] Replace zlib with zlib-ng --- .gitmodules | 6 ++-- CesiumUtility/src/Gunzip.cpp | 12 ++++---- CesiumUtility/test/TestGunzip.cpp | 50 +++++++++++++++++++++++++++++++ extern/CMakeLists.txt | 5 ++-- extern/zlib | 1 - extern/zlib-ng | 1 + 6 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 CesiumUtility/test/TestGunzip.cpp delete mode 160000 extern/zlib create mode 160000 extern/zlib-ng diff --git a/.gitmodules b/.gitmodules index 4c42c9ceb..cc5e6d3f2 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/CesiumUtility/src/Gunzip.cpp b/CesiumUtility/src/Gunzip.cpp index 378ed94e5..3a68d72fb 100644 --- a/CesiumUtility/src/Gunzip.cpp +++ b/CesiumUtility/src/Gunzip.cpp @@ -1,6 +1,6 @@ #include "CesiumUtility/Gunzip.h" #define ZLIB_CONST -#include "zlib.h" +#include "zlib-ng.h" #define CHUNK 65536 @@ -16,13 +16,13 @@ bool CesiumUtility::gunzip( std::vector& 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; } @@ -34,18 +34,18 @@ bool CesiumUtility::gunzip( out.resize(index + CHUNK); strm.next_out = reinterpret_cast(&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; } diff --git a/CesiumUtility/test/TestGunzip.cpp b/CesiumUtility/test/TestGunzip.cpp new file mode 100644 index 000000000..d97f57e11 --- /dev/null +++ b/CesiumUtility/test/TestGunzip.cpp @@ -0,0 +1,50 @@ +#include "CesiumUtility/Gunzip.h" + +#include +#include + +#include +#include + +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 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 testUnzipped(testUnzippedArray, 12); + +TEST_CASE("CesiumUtility::isGzip") { + CHECK(CesiumUtility::isGzip(testZipped)); + CHECK(!CesiumUtility::isGzip(testUnzipped)); +} + +TEST_CASE("CesiumUtility::gunzip") { + std::vector 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)); +} diff --git a/extern/CMakeLists.txt b/extern/CMakeLists.txt index 1d6f21c44..1cabdbdfa 100644 --- a/extern/CMakeLists.txt +++ b/extern/CMakeLists.txt @@ -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}) diff --git a/extern/zlib b/extern/zlib deleted file mode 160000 index 04f42ceca..000000000 --- a/extern/zlib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 04f42ceca40f73e2978b50e93806c2a18c1281fc diff --git a/extern/zlib-ng b/extern/zlib-ng new file mode 160000 index 000000000..74253725f --- /dev/null +++ b/extern/zlib-ng @@ -0,0 +1 @@ +Subproject commit 74253725f884e2424a0dd8ae3f69896d5377f325 From 9c0899f0af6860de76cd45dec6b9b855f2ac54f5 Mon Sep 17 00:00:00 2001 From: Ashley Rogers Date: Tue, 30 Jan 2024 16:09:20 -0500 Subject: [PATCH 2/4] Fix Linux build --- CesiumUtility/test/TestGunzip.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/CesiumUtility/test/TestGunzip.cpp b/CesiumUtility/test/TestGunzip.cpp index d97f57e11..26cfd57fc 100644 --- a/CesiumUtility/test/TestGunzip.cpp +++ b/CesiumUtility/test/TestGunzip.cpp @@ -1,10 +1,7 @@ #include "CesiumUtility/Gunzip.h" -#include #include - #include -#include using namespace CesiumUtility; From 62e9fb3f4fd387e780d60fcc423cb9a68364a094 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Thu, 15 Feb 2024 18:34:43 +1100 Subject: [PATCH 3/4] Formatting. --- CesiumUtility/test/TestGunzip.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/CesiumUtility/test/TestGunzip.cpp b/CesiumUtility/test/TestGunzip.cpp index 26cfd57fc..2201676c7 100644 --- a/CesiumUtility/test/TestGunzip.cpp +++ b/CesiumUtility/test/TestGunzip.cpp @@ -1,6 +1,7 @@ #include "CesiumUtility/Gunzip.h" #include + #include using namespace CesiumUtility; From c70998f99be06acceb637d99cd6f61bc6c730b88 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Thu, 15 Feb 2024 18:36:30 +1100 Subject: [PATCH 4/4] Update CHANGES.md. --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 0231905f0..5dfb1a19f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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: