Last active
January 8, 2016 18:04
-
-
Save MORTAL2000/05f5b100166203ed3cc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Graphics.hpp> | |
#include <string> | |
#include <fstream> | |
#include <sstream> | |
#include <iostream> | |
#include <array> | |
static const std::string base64_chars = | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
"abcdefghijklmnopqrstuvwxyz" | |
"0123456789+/"; | |
std::string base64_encode(const std::string& data) | |
{ | |
std::string ret; | |
std::array<unsigned char, 3> char_array; | |
for (auto begin = data.begin(), end = data.end(), it = begin; it != end; ++it) | |
{ | |
auto index = it - begin; | |
// Read a group of three bytes (avoid buffer overrun by replacing with 0) | |
char_array[index % char_array.size()] = *it; | |
if (index % char_array.size() != char_array.size() - 1) continue; | |
// Transform into four base 64 characters | |
ret += base64_chars[(char_array[0] & 0xfc) >> 2]; | |
ret += base64_chars[((char_array[0] & 0x03) << 4) + ((char_array[1] & 0xf0) >> 4)]; | |
ret += base64_chars[((char_array[1] & 0x0f) << 2) + ((char_array[2] & 0xc0) >> 6)]; | |
ret += base64_chars[((char_array[2] & 0x3f) << 0)]; | |
} | |
return ret; | |
} | |
std::string stringFromFile(const std::string& file) | |
{ | |
std::ifstream fin; | |
fin.open(file, std::ios::binary); | |
std::ostringstream out; | |
out << fin.rdbuf(); | |
fin.close(); | |
return out.str(); | |
} | |
int main() | |
{ | |
std::string encoded = base64_encode(stringFromFile("sfml-logo-small.png")); | |
std::ofstream file; | |
file.open("sfml-logo-small.pin"); | |
file << encoded; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment