Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Base64.cpp
1/*
2Copyright (C) 2021,2022,2025 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17
18#include <fstream>
19#include <sstream>
20#include <ostream>
21#include <iostream>
22#include <cstring>
23#if defined(__unix__) || defined(__MINGW32__)
24#include "sys/stat.h"
25#endif
26#include "Base64.h"
27#include "aeongames/Base64.hpp"
28#include "aeongames/Pipeline.hpp"
29#include <filesystem>
30
31namespace AeonGames
32{
33 Base64::Base64() = default;
34 Base64::~Base64() = default;
35 void Base64::ProcessArgs ( int argc, char** argv )
36 {
37 if ( argc < 3 || ( strcmp ( argv[1], "base64" ) != 0 && ! ( strcmp ( argv[2], "encode" ) == 0 || strcmp ( argv[2], "decode" ) == 0 ) ) )
38 {
39 std::ostringstream stream;
40 stream << "Invalid tool name, expected base64 <encode|decode>, got " << ( ( argc < 2 ) ? "nothing" : argv[1] ) << " " << ( ( argc < 3 ) ? "nothing" : argv[2] ) << std::endl;
41 throw std::runtime_error ( stream.str().c_str() );
42 }
43 mDecode = ( strcmp ( argv[2], "decode" ) == 0 );
44 for ( int i = 3; i < argc; ++i )
45 {
46 if ( argv[i][0] == '-' )
47 {
48 if ( argv[i][1] == '-' )
49 {
50 if ( strncmp ( &argv[i][2], "in", sizeof ( "in" ) ) == 0 )
51 {
52 i++;
53 mInputFile = argv[i];
54 }
55 else if ( strncmp ( &argv[i][2], "out", sizeof ( "out" ) ) == 0 )
56 {
57 i++;
58 mOutputFile = argv[i];
59 }
60 }
61 else
62 {
63 switch ( argv[i][1] )
64 {
65 case 'i':
66 i++;
67 mInputFile = argv[i];
68 break;
69 case 'o':
70 i++;
71 mOutputFile = argv[i];
72 break;
73 }
74 }
75 }
76 else
77 {
78 mInputFile = argv[i];
79 }
80 }
81 if ( mInputFile.empty() )
82 {
83 throw std::runtime_error ( "No Input file provided." );
84 }
85 if ( mOutputFile.empty() )
86 {
87 if ( !mDecode )
88 {
89 std::filesystem::path input_path ( mInputFile );
90 mOutputFile = input_path.replace_extension ( ".b64" ).string();
91 }
92 else
93 {
94 throw std::runtime_error ( "No Output file provided. Decoding does not yet infer output extensions." );
95 }
96 }
97 }
98
99 int Base64::operator() ( int argc, char** argv )
100 {
101 ProcessArgs ( argc, argv );
102 std::ifstream in;
103 in.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
104 in.open ( mInputFile, std::ifstream::in | std::ifstream::binary );
105 std::string buffer ( ( std::istreambuf_iterator<char> ( in ) ), ( std::istreambuf_iterator<char>() ) );
106 in.close();
107 std::string output{};
108 if ( mDecode )
109 {
110 output = Base64Decode ( buffer );
111 }
112 else
113 {
114 output = Base64Encode ( buffer );
115 }
116 std::ofstream out;
117 out.exceptions ( std::ofstream::failbit | std::ofstream::badbit );
118 out.open ( mOutputFile, std::ofstream::out | std::ofstream::binary );
119 out.write ( reinterpret_cast<char*> ( output.data() ), output.size() );
120 out.close();
121 return 0;
122 }
123}
Base64 encoding and decoding utilities.
Base64()
Default constructor.
int operator()(int argc, char **argv) override
Execute the Base64 tool.
Definition Base64.cpp:99
~Base64()
Destructor.
<- This is here just for the literals
Definition AABB.hpp:31
DLL std::string Base64Encode(const uint8_t *aData, size_t aDataSize, bool aSplit=true)
Encode a binary string.
Definition Base64.cpp:26
DLL std::string Base64Decode(const uint8_t *aData, size_t aDataSize)
Decode a base64 string.
Definition Base64.cpp:68