Renamed the base64 methods, and switched the EncodeBase64 one to return a string (to make it easy to use elsewhere)

This commit is contained in:
beder
2012-01-21 01:33:49 -06:00
parent b66197bdf4
commit e17734de33
3 changed files with 24 additions and 22 deletions

View File

@@ -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:

View File

@@ -1,43 +1,46 @@
#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
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())

View File

@@ -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;
} }
} }