diff --git a/tiny_gltf.h b/tiny_gltf.h index 185bb0d..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 @@ -2040,9 +2039,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); @@ -2188,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'; @@ -7528,31 +7518,24 @@ 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 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. + // 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_size + + (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)); 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)); @@ -7561,13 +7544,11 @@ 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) { - 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;