Skip to content

Commit 1eb0293

Browse files
committed
DataURI: fix platform specific quirks.
Update filefinder.cpp
1 parent 511ba82 commit 1eb0293

4 files changed

Lines changed: 19 additions & 29 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ add_library(${PROJECT_NAME} OBJECT
6969
src/autobattle.h
7070
src/background.cpp
7171
src/background.h
72-
src/base64.hpp
72+
src/base64.h
7373
src/baseui.cpp
7474
src/baseui.h
7575
src/battle_animation.cpp

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ libeasyrpg_player_a_SOURCES = \
3838
src/autobattle.h \
3939
src/background.cpp \
4040
src/background.h \
41-
src/base64.hpp \
41+
src/base64.h \
4242
src/baseui.cpp \
4343
src/baseui.h \
4444
src/battle_animation.cpp \

src/base64.hpp renamed to src/base64.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <string_view>
1212
#include <type_traits>
1313

14+
#include "output.h"
15+
1416
#if defined(__cpp_lib_bit_cast)
1517
#include <bit> // For std::bit_cast.
1618
#endif
@@ -546,7 +548,7 @@ inline OutputBuffer encode_into(InputIterator begin, InputIterator end) {
546548
break;
547549
}
548550
default: {
549-
throw std::runtime_error{"Invalid base64 encoded data"};
551+
Output::Warning("Invalid base64 encoded data");
550552
}
551553
}
552554

@@ -574,15 +576,13 @@ inline OutputBuffer decode_into(std::string_view base64Text) {
574576
}
575577

576578
if ((base64Text.size() & 3) != 0) {
577-
throw std::runtime_error{
578-
"Invalid base64 encoded data - Size not divisible by 4"};
579+
Output::Warning("Invalid base64 encoded data - Size not divisible by 4");
579580
}
580581

581582
const size_t numPadding =
582583
std::count(base64Text.rbegin(), base64Text.rbegin() + 4, '=');
583584
if (numPadding > 2) {
584-
throw std::runtime_error{
585-
"Invalid base64 encoded data - Found more than 2 padding signs"};
585+
Output::Warning("Invalid base64 encoded data - Found more than 2 padding signs");
586586
}
587587

588588
const size_t decodedsize = (base64Text.size() * 3 >> 2) - numPadding;
@@ -605,8 +605,7 @@ inline OutputBuffer decode_into(std::string_view base64Text) {
605605
const uint32_t temp = d1 | d2 | d3 | d4;
606606

607607
if (temp >= detail::bad_char) {
608-
throw std::runtime_error{
609-
"Invalid base64 encoded data - Invalid character"};
608+
Output::Warning("Invalid base64 encoded data - Invalid character");
610609
}
611610

612611
// Use bit_cast instead of union and type punning to avoid
@@ -636,8 +635,7 @@ inline OutputBuffer decode_into(std::string_view base64Text) {
636635
const uint32_t temp = d1 | d2 | d3;
637636

638637
if (temp >= detail::bad_char) {
639-
throw std::runtime_error{
640-
"Invalid base64 encoded data - Invalid character"};
638+
Output::Warning("Invalid base64 encoded data - Invalid character");
641639
}
642640

643641
// Use bit_cast instead of union and type punning to avoid
@@ -659,8 +657,7 @@ inline OutputBuffer decode_into(std::string_view base64Text) {
659657
const uint32_t temp = d1 | d2;
660658

661659
if (temp >= detail::bad_char) {
662-
throw std::runtime_error{
663-
"Invalid base64 encoded data - Invalid character"};
660+
Output::Warning("Invalid base64 encoded data - Invalid character");
664661
}
665662

666663
const std::array<char, 4> tempBytes =
@@ -669,8 +666,7 @@ inline OutputBuffer decode_into(std::string_view base64Text) {
669666
break;
670667
}
671668
default: {
672-
throw std::runtime_error{
673-
"Invalid base64 encoded data - Invalid padding number"};
669+
Output::Warning("Invalid base64 encoded data - Invalid padding number");
674670
}
675671
}
676672

src/filefinder.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454
#endif
5555

5656
#include "filesystem_stream.h"
57-
#include "base64.hpp"
57+
#include "base64.h"
5858

59-
namespace { // Anonymous namespace
59+
namespace {
6060

6161
struct DataUriResult {
6262
std::string_view mime_type;
@@ -68,7 +68,7 @@ namespace { // Anonymous namespace
6868
if (!StartsWith(uri, "data:")) {
6969
return std::nullopt;
7070
}
71-
// ... (rest of the function is the same)
71+
7272
auto header_end = uri.find(',');
7373
if (header_end == std::string_view::npos) return std::nullopt;
7474
auto header = uri.substr(5, header_end - 5);
@@ -82,13 +82,7 @@ namespace { // Anonymous namespace
8282
result.mime_type = header;
8383
}
8484
if (result.is_base64) {
85-
try {
86-
result.data = base64::decode_into<std::vector<unsigned char>>(data_payload.begin(), data_payload.end());
87-
}
88-
catch (const std::exception& e) {
89-
Output::Warning("Base64 decoding failed: {}", e.what());
90-
return std::nullopt;
91-
}
85+
result.data = base64::decode_into<std::vector<unsigned char>>(data_payload.begin(), data_payload.end());
9286
}
9387
else {
9488
result.data.assign(data_payload.begin(), data_payload.end());
@@ -466,11 +460,11 @@ int FileFinder::GetSavegames() {
466460
}
467461

468462
std::string find_generic(const DirectoryTree::Args& args) {
469-
// *** NEW CENTRALIZED LOGIC ***
463+
// *** DATA URI LOGIC ***
470464
if (StartsWith(args.path, "data:")) {
471465
return std::string(args.path); // The URI is its own "path"
472466
}
473-
// *** END OF NEW LOGIC ***
467+
// *** END OF DATA URI LOGIC ***
474468

475469
if (!Tr::GetCurrentTranslationId().empty()) {
476470
auto tr_fs = Tr::GetCurrentTranslationFilesystem();
@@ -522,7 +516,7 @@ std::string FileFinder::FindFont(std::string_view name) {
522516
}
523517

524518
Filesystem_Stream::InputStream open_generic(std::string_view dir, std::string_view name, DirectoryTree::Args& args) {
525-
// *** NEW CENTRALIZED LOGIC ***
519+
// *** DATA URI LOGIC ***
526520
// Check the 'name' part first, as it's the most likely place for a data URI.
527521
if (StartsWith(name, "data:")) {
528522
auto parsed_result = parse_data_uri(name);
@@ -547,7 +541,7 @@ Filesystem_Stream::InputStream open_generic(std::string_view dir, std::string_vi
547541
return Filesystem_Stream::InputStream(nullptr, ""); // Return an invalid stream
548542
}
549543
}
550-
// *** END OF NEW LOGIC ***
544+
// *** END OF DATA URI LOGIC ***
551545

552546
if (!Tr::GetCurrentTranslationId().empty()) {
553547
auto tr_fs = Tr::GetCurrentTranslationFilesystem();

0 commit comments

Comments
 (0)