5#include "../include/TiledDecoder.h"
6#include "../../system/system_utils.h"
7#include "../../TiledLevelLoader/third_party/miniz/miniz.h"
15 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
16 "abcdefghijklmnopqrstuvwxyz"
21 std::vector<uint8_t>
result;
27 if (!
isspace(
static_cast<unsigned char>(
c))) {
41 for (
i = 0;
i < 4;
i++)
48 for (
i = 0; (
i < 3);
i++)
55 for (
j =
i;
j < 4;
j++)
58 for (
j = 0;
j < 4;
j++)
65 for (
j = 0; (
j <
i - 1);
j++)
75 SYSTEM_LOG <<
"TiledDecoder: ERROR - Empty input for gzip decompression" << std::endl;
76 return std::vector<uint8_t>();
96 if (status !=
MZ_OK) {
97 const char*
errorStr =
"unknown error";
104 SYSTEM_LOG <<
"TiledDecoder: ERROR - Gzip decompression failed"
105 <<
"\n Error code: " << status <<
" (" <<
errorStr <<
")"
106 <<
"\n Input size: " <<
compressed.size() <<
" bytes"
107 <<
"\n This indicates corrupted, truncated, or invalid gzip data"
109 return std::vector<uint8_t>();
124 std::vector<uint32_t>
result;
130 if (
bytes.size() % 4 != 0) {
131 SYSTEM_LOG <<
"TiledDecoder: ERROR - Byte array size (" <<
bytes.size()
132 <<
") is not a multiple of 4"
133 <<
"\n This indicates corrupted or truncated tile data"
134 <<
"\n Each tile ID requires exactly 4 bytes"
135 <<
"\n Missing bytes: " << (4 - (
bytes.size() % 4))
142 for (
size_t i = 0;
i <
bytes.size();
i += 4) {
157 std::vector<uint32_t>
result;
166 token.erase(0,
token.find_first_not_of(
" \t\n\r\f\v"));
167 token.erase(
token.find_last_not_of(
" \t\n\r\f\v") + 1);
169 if (!
token.empty()) {
173 }
catch (
const std::invalid_argument&
e) {
176 <<
": '" <<
token <<
"' (not a valid number). Error : " <<
e.what() << std::endl;
177 }
catch (
const std::out_of_range&
e) {
180 <<
": '" <<
token <<
"' (exceeds uint32_t maximum). Error " <<
e.what() << std::endl;
184 <<
": '" <<
token <<
"'" << std::endl;
190 SYSTEM_LOG <<
"TiledDecoder: WARNING - Parsed " <<
result.size() <<
" valid tiles from "
198 const std::string& data,
199 const std::string& encoding,
200 const std::string& compression)
202 if (encoding ==
"csv") {
205 else if (encoding ==
"base64") {
209 SYSTEM_LOG <<
"TiledDecoder: ERROR - Base64 decode failed"
210 <<
"\n Input data length: " << data.length() <<
" characters"
211 <<
"\n This may indicate corrupted or invalid base64 data"
213 return std::vector<uint32_t>();
216 SYSTEM_LOG <<
"TiledDecoder: Base64 decoded " <<
decoded.size() <<
" bytes" << std::endl;
219 if (compression ==
"gzip") {
223 SYSTEM_LOG <<
"TiledDecoder: ERROR - Gzip decompression failed"
225 <<
"\n This may indicate corrupted compression data"
227 return std::vector<uint32_t>();
230 <<
" to " <<
decoded.size() <<
" bytes" << std::endl;
232 else if (compression ==
"zlib") {
236 SYSTEM_LOG <<
"TiledDecoder: ERROR - Zlib decompression failed"
238 <<
"\n This may indicate corrupted compression data"
240 return std::vector<uint32_t>();
243 <<
" to " <<
decoded.size() <<
" bytes" << std::endl;
250 SYSTEM_LOG <<
"TiledDecoder: ERROR - Unsupported encoding: '" << encoding <<
"'"
251 <<
"\n Supported encodings: 'csv', 'base64'"
253 return std::vector<uint32_t>();
ComponentTypeID GetComponentTypeID_Static()
static std::vector< uint8_t > DecodeBase64(const std::string &encoded)
static const std::string base64_chars
static bool IsBase64(unsigned char c)
static std::vector< uint32_t > DecodeTileData(const std::string &data, const std::string &encoding, const std::string &compression)
static std::vector< uint8_t > DecompressGzip(const std::vector< uint8_t > &compressed)
static std::vector< uint32_t > ParseCSV(const std::string &csv)
static std::vector< uint32_t > BytesToTileIds(const std::vector< uint8_t > &bytes)
static std::vector< uint8_t > DecompressZlib(const std::vector< uint8_t > &compressed)