Vendor tiniergltf

This commit is contained in:
Lars Mueller 2024-05-14 20:27:40 +02:00
parent d7c92d35bd
commit 445a2af86f
8 changed files with 1609 additions and 8 deletions

View File

@ -282,6 +282,8 @@ if(BUILD_BENCHMARKS)
add_subdirectory(lib/catch2)
endif()
add_subdirectory(lib/tiniergltf)
# Subdirectories
# Be sure to add all relevant definitions above this
add_subdirectory(src)

View File

@ -11,14 +11,6 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type: Debug or Release" FORCE)
endif()
include(FetchContent)
FetchContent_Declare(
tiniergltf
GIT_REPOSITORY https://github.com/appgurueu/tiniergltf.git
GIT_TAG dafe7cf165af22fae7f852c315934a9910e34e45
)
FetchContent_MakeAvailable(tiniergltf)
# FIXME: tests need to be moved to MT if we want to keep them
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

6
lib/tiniergltf/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
cmake
CMakeCache.txt
CMakeFiles
.cache
compile_commands.json
build

View File

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.12)
project(tiniergltf
VERSION 1.0.0
LANGUAGES CXX
)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(tiniergltf OBJECT tiniergltf.hpp base64.cpp base64.h)
add_library(tiniergltf::tiniergltf ALIAS tiniergltf)
target_include_directories(tiniergltf
INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
"${JSON_INCLUDE_DIR}" # Set in FindJson.cmake
)
target_link_libraries(tiniergltf)

67
lib/tiniergltf/Readme.md Normal file
View File

@ -0,0 +1,67 @@
# TinierGLTF
A safe, modern, tiny glTF loader for C++ 17.
What this is:
* A tiny glTF deserializer which maps JSON objects to appropriate C++ structures.
* Intended to be safe for loading untrusted input.
* Slightly tailored to the needs of [Minetest](https://github.com/minetest/minetest).
What this doesn't and shouldn't do:
* Serialization
* Loading images
* Resolving resources
* Support glTF extensions
## TODOs
- [ ] Add GLB support.
- [ ] Add further checks according to the specification.
- Everything in the JSON schema (+ indices and misc. stuff) is already validated.
Much of the code was generated by a Lua script from the JSON schemata.
- [ ] Consider base64 rewrite.
## License
`tiniergltf.hpp` was written by Lars Müller and is licensed under the MIT license:
> Copyright 2024 Lars Müller
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
>
> 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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The base64 library used is licensed as follows:
> base64.cpp and base64.h
>
> Copyright (C) 2004-2008 René Nyffenegger
> Modified by the Minetest Contributors.
>
> This source code is provided 'as-is', without any express or implied
> warranty. In no event will the author be held liable for any damages
> arising from the use of this software.
>
> Permission is granted to anyone to use this software for any purpose,
> including commercial applications, and to alter it and redistribute it
> freely, subject to the following restrictions:
>
> 1. The origin of this source code must not be misrepresented; you must not
> claim that you wrote the original source code. If you use this source code
> in a product, an acknowledgment in the product documentation would be
> appreciated but is not required.
>
> 2. Altered source versions must be plainly marked as such, and must not be
> misrepresented as being the original source code.
>
> 3. This notice may not be removed or altered from any source distribution.
>
> René Nyffenegger rene.nyffenegger@adp-gmbh.ch
## Bug Bounty
I offer a reward of one (1) virtual headpat per valid bug report.

112
lib/tiniergltf/base64.cpp Normal file
View File

@ -0,0 +1,112 @@
/*
base64.cpp and base64.h
Copyright (C) 2004-2008 René Nyffenegger
Modified by the Minetest Contributors.
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/
#include "base64.h"
#include <string>
#include <string_view>
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static const std::string base64_chars_padding_1 = "AEIMQUYcgkosw048";
static const std::string base64_chars_padding_2 = "AQgw";
static inline bool is_base64(unsigned char c)
{
return (c >= '0' && c <= '9')
|| (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| c == '+' || c == '/';
}
bool base64_is_valid(const std::string_view &s)
{
size_t i = 0;
for (; i < s.size(); ++i)
if (!is_base64(s[i]))
break;
unsigned char padding = 3 - ((i + 3) % 4);
if ((padding == 1 && base64_chars_padding_1.find(s[i - 1]) == std::string::npos)
|| (padding == 2 && base64_chars_padding_2.find(s[i - 1]) == std::string::npos)
|| padding == 3)
return false;
int actual_padding = s.size() - i;
// omission of padding characters is allowed
if (actual_padding == 0)
return true;
// remaining characters (max. 2) may only be padding
for (; i < s.size(); ++i)
if (s[i] != '=')
return false;
// number of padding characters needs to match
return padding == actual_padding;
}
std::vector<unsigned char> base64_decode(const std::string_view &encoded_string) {
std::size_t in_len = encoded_string.size();
std::size_t i = 0, j = 0, in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::vector<unsigned char> ret;
ret.reserve(3 * in_len / 4 + 1);
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret.push_back(char_array_3[i]);
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++)
ret.push_back(char_array_3[j]);
}
return ret;
}

35
lib/tiniergltf/base64.h Normal file
View File

@ -0,0 +1,35 @@
/*
base64.cpp and base64.h
Copyright (C) 2004-2008 René Nyffenegger
Minor modifications by the Minetest Contributors.
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/
#pragma once
#include <vector>
#include <string_view>
bool base64_is_valid(const std::string_view &s);
std::vector<unsigned char> base64_decode(const std::string_view &s);

File diff suppressed because it is too large Load Diff