From 190382aecdec919c91d526c2268eb44c83eaa30d Mon Sep 17 00:00:00 2001 From: Alexander Wood Date: Fri, 8 Oct 2021 12:19:13 -0400 Subject: [PATCH 1/5] GetBaseFilename contained a logic error in that a filename that does not contain a path separator would result in a crash. Removed the roundUp lambda, as this generalization is not necessary; the caller always attempts 4 byte alignment. --- tiny_gltf.h | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index 185bb0d..23095fd 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -2040,9 +2040,12 @@ static std::string GetBaseDir(const std::string &filepath) { return ""; } -// https://stackoverflow.com/questions/8520560/get-a-file-name-from-a-path static std::string GetBaseFilename(const std::string &filepath) { - return filepath.substr(filepath.find_last_of("/\\") + 1); + constexpr char path_separators[2] = { '/', '\\' }; + auto idx = filepath.find_last_of(path_separators); + if (idx != std::string::npos) + return filepath.substr(idx + 1); + return filepath; } std::string base64_encode(unsigned char const *, unsigned int len); @@ -7528,24 +7531,17 @@ static void WriteBinaryGltfStream(std::ostream &stream, const std::string header = "glTF"; const int version = 2; - // https://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number - auto roundUp = [](uint32_t numToRound, uint32_t multiple) { - if (multiple == 0) return numToRound; - - uint32_t remainder = numToRound % multiple; - if (remainder == 0) return numToRound; - - return numToRound + multiple - remainder; - }; - - const uint32_t padding_size = - roundUp(uint32_t(content.size()), 4) - uint32_t(content.size()); + const uint32_t content_size = uint32_t(content.size()); + const uint32_t binBuffer_size = uint32_t(binBuffer.size()); + // determine number of padding bytes required to ensure 4 byte alignment + const uint32_t padding_size = content_size % 4 == 0 ? 0 : 4 - content_size % 4; + const uint32_t bin_padding_size = binBuffer_size % 4 == 0 ? 0 : 4 - binBuffer_size % 4; // 12 bytes for header, JSON content length, 8 bytes for JSON chunk info. - // Chunk data must be located at 4-byte boundary. + // Chunk data must be located at 4-byte boundary, which may require padding const uint32_t length = - 12 + 8 + roundUp(uint32_t(content.size()), 4) + - (binBuffer.size() ? (8 + roundUp(uint32_t(binBuffer.size()), 4)) : 0); + 12 + 8 + content_size + content_padding + + (binBuffer_size ? (8 + binBuffer_size + bin_padding_size : 0); stream.write(header.c_str(), std::streamsize(header.size())); stream.write(reinterpret_cast(&version), sizeof(version)); @@ -7566,8 +7562,6 @@ static void WriteBinaryGltfStream(std::ostream &stream, stream.write(padding.c_str(), std::streamsize(padding.size())); } if (binBuffer.size() > 0) { - const uint32_t bin_padding_size = - roundUp(uint32_t(binBuffer.size()), 4) - uint32_t(binBuffer.size()); // BIN chunk info, then BIN data const uint32_t bin_length = uint32_t(binBuffer.size()) + bin_padding_size; const uint32_t bin_format = 0x004e4942; From 9e3d1f6db55231ce44194b3a1c882ce7ae0c0b23 Mon Sep 17 00:00:00 2001 From: Alexander Wood Date: Fri, 8 Oct 2021 16:57:56 -0400 Subject: [PATCH 2/5] Build fix. --- tiny_gltf.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index 23095fd..40b3983 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -7534,13 +7534,13 @@ static void WriteBinaryGltfStream(std::ostream &stream, const uint32_t content_size = uint32_t(content.size()); const uint32_t binBuffer_size = uint32_t(binBuffer.size()); // determine number of padding bytes required to ensure 4 byte alignment - const uint32_t padding_size = content_size % 4 == 0 ? 0 : 4 - content_size % 4; + const uint32_t content_padding_size = content_size % 4 == 0 ? 0 : 4 - content_size % 4; const uint32_t bin_padding_size = binBuffer_size % 4 == 0 ? 0 : 4 - binBuffer_size % 4; // 12 bytes for header, JSON content length, 8 bytes for JSON chunk info. // Chunk data must be located at 4-byte boundary, which may require padding const uint32_t length = - 12 + 8 + content_size + content_padding + + 12 + 8 + content_size + content_padding_size + (binBuffer_size ? (8 + binBuffer_size + bin_padding_size : 0); stream.write(header.c_str(), std::streamsize(header.size())); @@ -7548,7 +7548,7 @@ static void WriteBinaryGltfStream(std::ostream &stream, stream.write(reinterpret_cast(&length), sizeof(length)); // JSON chunk info, then JSON data - const uint32_t model_length = uint32_t(content.size()) + padding_size; + const uint32_t model_length = uint32_t(content.size()) + content_padding_size; const uint32_t model_format = 0x4E4F534A; stream.write(reinterpret_cast(&model_length), sizeof(model_length)); @@ -7557,8 +7557,8 @@ static void WriteBinaryGltfStream(std::ostream &stream, stream.write(content.c_str(), std::streamsize(content.size())); // Chunk must be multiplies of 4, so pad with spaces - if (padding_size > 0) { - const std::string padding = std::string(size_t(padding_size), ' '); + if (content_padding_size > 0) { + const std::string padding = std::string(size_t(content_padding_size), ' '); stream.write(padding.c_str(), std::streamsize(padding.size())); } if (binBuffer.size() > 0) { From a3d5d5d1c98f318e17d3417b0d800e16c440d717 Mon Sep 17 00:00:00 2001 From: Alexander Wood Date: Fri, 8 Oct 2021 20:09:08 -0400 Subject: [PATCH 3/5] Build fix. --- tiny_gltf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index 40b3983..0ab92e3 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -7541,7 +7541,7 @@ static void WriteBinaryGltfStream(std::ostream &stream, // Chunk data must be located at 4-byte boundary, which may require padding const uint32_t length = 12 + 8 + content_size + content_padding_size + - (binBuffer_size ? (8 + binBuffer_size + bin_padding_size : 0); + (binBuffer_size ? (8 + binBuffer_size + bin_padding_size : 0)); stream.write(header.c_str(), std::streamsize(header.size())); stream.write(reinterpret_cast(&version), sizeof(version)); From 13803659f88976ac8dd450e0713ca896d01c40f1 Mon Sep 17 00:00:00 2001 From: Alexander Wood Date: Fri, 8 Oct 2021 20:13:07 -0400 Subject: [PATCH 4/5] Build fix. --- tiny_gltf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index 0ab92e3..a2c2018 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -7541,7 +7541,7 @@ static void WriteBinaryGltfStream(std::ostream &stream, // Chunk data must be located at 4-byte boundary, which may require padding const uint32_t length = 12 + 8 + content_size + content_padding_size + - (binBuffer_size ? (8 + binBuffer_size + bin_padding_size : 0)); + (binBuffer_size ? (8 + binBuffer_size + bin_padding_size) : 0); stream.write(header.c_str(), std::streamsize(header.size())); stream.write(reinterpret_cast(&version), sizeof(version)); From e4bc6c7bd49755f21fd213890a2bb1ab816a6bff Mon Sep 17 00:00:00 2001 From: Alexander Wood Date: Thu, 14 Oct 2021 08:54:59 -0400 Subject: [PATCH 5/5] - Remove clang warning suppression. I assume this stackoverflow reference can just be removed as it isn't a code snippet but a repost of clang documentation of ignoring warning levels. - Remove stackoverflow reference that recommends the use of dlib. There's no source that was pulled from this s.o. post, only a recommendation on a library, I wouldn't think a link to s.o. is necessary here. Per the Boost License, I've included a copy of the license along with the snippet --- tiny_gltf.h | 61 +++++++++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index a2c2018..82a2e6a 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -253,7 +253,6 @@ bool DecodeDataURI(std::vector *out, std::string &mime_type, #ifdef __clang__ #pragma clang diagnostic push // Suppress warning for : static Value null_value -// https://stackoverflow.com/questions/15708411/how-to-deal-with-global-constructor-warning-in-clang #pragma clang diagnostic ignored "-Wexit-time-destructors" #pragma clang diagnostic ignored "-Wpadded" #endif @@ -2191,47 +2190,35 @@ std::string base64_decode(std::string const &encoded_string) { // TODO(syoyo): Use uriparser https://uriparser.github.io/ for stricter Uri // decoding? // -// https://stackoverflow.com/questions/18307429/encode-decode-url-in-c +// Uri Decoding from DLIB // http://dlib.net/dlib/server/server_http.cpp.html - -// --- dlib beign ------------------------------------------------------------ +// --- dlib begin ------------------------------------------------------------ // Copyright (C) 2003 Davis E. King (davis@dlib.net) -// License: Boost Software License See LICENSE.txt for the full license. +// License: Boost Software License +// Boost Software License - Version 1.0 - August 17th, 2003 +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// namespace dlib { -#if 0 - inline unsigned char to_hex( unsigned char x ) - { - return x + (x > 9 ? ('A'-10) : '0'); - } - - const std::string urlencode( const std::string& s ) - { - std::ostringstream os; - - for ( std::string::const_iterator ci = s.begin(); ci != s.end(); ++ci ) - { - if ( (*ci >= 'a' && *ci <= 'z') || - (*ci >= 'A' && *ci <= 'Z') || - (*ci >= '0' && *ci <= '9') ) - { // allowed - os << *ci; - } - else if ( *ci == ' ') - { - os << '+'; - } - else - { - os << '%' << to_hex(static_cast(*ci >> 4)) << to_hex(static_cast(*ci % 16)); - } - } - - return os.str(); - } -#endif - inline unsigned char from_hex(unsigned char ch) { if (ch <= '9' && ch >= '0') ch -= '0';