bsnes-libretro/nall/base64.hpp
Tim Allen 4e2eb23835 Update to v093 release.
byuu says:

Changelog:
- added Cocoa target: higan can now be compiled for OS X Lion
  [Cydrak, byuu]
- SNES/accuracy profile hires color blending improvements - fixes
  Marvelous text [AWJ]
- fixed a slight bug in SNES/SA-1 VBR support caused by a typo
- added support for multi-pass shaders that can load external textures
  (requires OpenGL 3.2+)
- added game library path (used by ananke->Import Game) to
  Settings->Advanced
- system profiles, shaders and cheats database can be stored in "all
  users" shared folders now (eg /usr/share on Linux)
- all configuration files are in BML format now, instead of XML (much
  easier to read and edit this way)
- main window supports drag-and-drop of game folders (but not game files
  / ZIP archives)
- audio buffer clears when entering a modal loop on Windows (prevents
  audio repetition with DirectSound driver)
- a substantial amount of code clean-up (probably the biggest
  refactoring to date)

One highly desired target for this release was to default to the optimal
drivers instead of the safest drivers, but because AMD drivers don't
seem to like my OpenGL 3.2 driver, I've decided to postpone that. AMD
has too big a market share. Hopefully with v093 officially released, we
can get some public input on what AMD doesn't like.
2013-08-18 13:21:14 +10:00

138 lines
3.1 KiB
C++

#ifndef NALL_BASE64_HPP
#define NALL_BASE64_HPP
#include <nall/stdint.hpp>
#include <nall/string.hpp>
#include <nall/vector.hpp>
namespace nall {
struct Base64 {
enum class Format : unsigned { MIME, URI };
inline static string encode(const uint8_t* data, unsigned size, Format format = Format::MIME);
inline static string encode(const vector<uint8_t>& buffer, Format format = Format::MIME);
inline static string encode(const string& text, Format format = Format::MIME);
inline static vector<uint8_t> decode(const string& text);
private:
inline static void table(char* data, Format format);
inline static uint8_t value(char data);
};
string Base64::encode(const uint8_t* data, unsigned size, Format format) {
vector<uint8_t> result;
char lookup[65];
table(lookup, format);
unsigned overflow = (3 - (size % 3)) % 3; //bytes to round to nearest multiple of 3
uint8_t buffer;
for(unsigned i = 0; i < size; i++) {
switch(i % 3) {
case 0:
buffer = data[i] >> 2;
result.append(lookup[buffer]);
buffer = (data[i] & 3) << 4;
result.append(lookup[buffer]);
break;
case 1:
buffer |= data[i] >> 4;
result.last() = lookup[buffer];
buffer = (data[i] & 15) << 2;
result.append(lookup[buffer]);
break;
case 2:
buffer |= data[i] >> 6;
result.last() = lookup[buffer];
buffer = (data[i] & 63);
result.append(lookup[buffer]);
break;
}
}
if(lookup[64]) {
if(overflow >= 1) result.append(lookup[64]);
if(overflow >= 2) result.append(lookup[64]);
}
return result;
}
string Base64::encode(const vector<uint8_t>& buffer, Format format) {
return encode(buffer.data(), buffer.size(), format);
}
string Base64::encode(const string& text, Format format) {
return encode((const uint8_t*)text.data(), text.size(), format);
}
vector<uint8_t> Base64::decode(const string& text) {
vector<uint8_t> result;
uint8_t buffer, output;
for(unsigned i = 0; i < text.size(); i++) {
uint8_t buffer = value(text[i]);
if(buffer == 0) break;
switch(i & 3) {
case 0:
output = buffer << 2;
break;
case 1:
result.append(output | buffer >> 4);
output = (buffer & 15) << 4;
break;
case 2:
result.append(output | buffer >> 2);
output = (buffer & 3) << 6;
break;
case 3:
result.append(output | buffer);
break;
}
}
return result;
}
void Base64::table(char* data, Format format) {
for(unsigned n = 0; n < 26; n++) data[ 0 + n] = 'A' + n;
for(unsigned n = 0; n < 26; n++) data[26 + n] = 'a' + n;
for(unsigned n = 0; n < 10; n++) data[52 + n] = '0' + n;
switch(format) {
case Format::MIME:
data[62] = '+';
data[63] = '/';
data[64] = '=';
break;
case Format::URI:
data[62] = '-';
data[63] = '_';
data[64] = 0;
break;
}
}
uint8_t Base64::value(char n) {
if(n >= 'A' && n <= 'Z') return n - 'A' + 0;
if(n >= 'a' && n <= 'z') return n - 'a' + 26;
if(n >= '0' && n <= '9') return n - '0' + 52;
if(n == '+' || n == '-') return 62;
if(n == '/' || n == '_') return 63;
return 0;
}
}
#endif