Commit d760d037 authored by Jesse Beder's avatar Jesse Beder
Browse files

Renamed the base64 methods, and switched the EncodeBase64 one to return a...

Renamed the base64 methods, and switched the EncodeBase64 one to return a string (to make it easy to use elsewhere)
parent 6105d4cf
...@@ -5,14 +5,13 @@ ...@@ -5,14 +5,13 @@
#pragma once #pragma once
#endif #endif
#include <string>
#include <vector> #include <vector>
namespace YAML namespace YAML
{ {
class ostream; std::string EncodeBase64(const unsigned char *data, std::size_t size);
std::vector<unsigned char> DecodeBase64(const std::string& input);
void WriteBase64(ostream& out, const unsigned char *data, std::size_t size);
std::vector<unsigned char> ReadBase64(const std::string& input);
class Binary { class Binary {
public: public:
......
#include "yaml-cpp/binary.h" #include "yaml-cpp/binary.h"
#include "yaml-cpp/ostream.h"
namespace YAML namespace YAML
{ {
static const char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void WriteBase64(ostream& out, const unsigned char *data, std::size_t size) std::string EncodeBase64(const unsigned char *data, std::size_t size)
{ {
const char PAD = '='; const char PAD = '=';
out << "\""; std::string ret;
ret.resize(4 * size / 3 + 3);
char *out = &ret[0];
std::size_t chunks = size / 3; std::size_t chunks = size / 3;
std::size_t remainder = size % 3; std::size_t remainder = size % 3;
for(std::size_t i=0;i<chunks;i++, data += 3) { for(std::size_t i=0;i<chunks;i++, data += 3) {
out << encoding[data[0] >> 2]; *out++ = encoding[data[0] >> 2];
out << encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)]; *out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
out << encoding[((data[1] & 0xf) << 2) | (data[2] >> 6)]; *out++ = encoding[((data[1] & 0xf) << 2) | (data[2] >> 6)];
out << encoding[data[2] & 0x3f]; *out++ = encoding[data[2] & 0x3f];
} }
switch(remainder) { switch(remainder) {
case 0: case 0:
break; break;
case 1: case 1:
out << encoding[data[0] >> 2]; *out++ = encoding[data[0] >> 2];
out << encoding[((data[0] & 0x3) << 4)]; *out++ = encoding[((data[0] & 0x3) << 4)];
out << PAD; *out++ = PAD;
out << PAD; *out++ = PAD;
break; break;
case 2: case 2:
out << encoding[data[0] >> 2]; *out++ = encoding[data[0] >> 2];
out << encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)]; *out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
out << encoding[((data[1] & 0xf) << 2)]; *out++ = encoding[((data[1] & 0xf) << 2)];
out << PAD; *out++ = PAD;
break; break;
} }
out << "\""; ret.resize(out - &ret[0]);
return ret;
} }
static const unsigned char decoding[] = { static const unsigned char decoding[] = {
...@@ -59,7 +62,7 @@ namespace YAML ...@@ -59,7 +62,7 @@ namespace YAML
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
}; };
std::vector<unsigned char> ReadBase64(const std::string& input) std::vector<unsigned char> DecodeBase64(const std::string& input)
{ {
typedef std::vector<unsigned char> ret_type; typedef std::vector<unsigned char> ret_type;
if(input.empty()) if(input.empty())
......
...@@ -370,7 +370,7 @@ namespace YAML ...@@ -370,7 +370,7 @@ namespace YAML
bool WriteBinary(ostream& out, const Binary& binary) bool WriteBinary(ostream& out, const Binary& binary)
{ {
WriteBase64(out, binary.data(), binary.size()); WriteDoubleQuotedString(out, EncodeBase64(binary.data(), binary.size()), false);
return true; return true;
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment