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
#endif
#include <string>
#include <vector>
namespace YAML
{
class ostream;
void WriteBase64(ostream& out, const unsigned char *data, std::size_t size);
std::vector<unsigned char> ReadBase64(const std::string& input);
std::string EncodeBase64(const unsigned char *data, std::size_t size);
std::vector<unsigned char> DecodeBase64(const std::string& input);
class Binary {
public:

View File

@@ -1,43 +1,46 @@
#include "yaml-cpp/binary.h"
#include "yaml-cpp/ostream.h"
namespace YAML
{
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 = '=';
out << "\"";
std::string ret;
ret.resize(4 * size / 3 + 3);
char *out = &ret[0];
std::size_t chunks = size / 3;
std::size_t remainder = size % 3;
for(std::size_t i=0;i<chunks;i++, data += 3) {
out << encoding[data[0] >> 2];
out << encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
out << encoding[((data[1] & 0xf) << 2) | (data[2] >> 6)];
out << encoding[data[2] & 0x3f];
*out++ = encoding[data[0] >> 2];
*out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
*out++ = encoding[((data[1] & 0xf) << 2) | (data[2] >> 6)];
*out++ = encoding[data[2] & 0x3f];
}
switch(remainder) {
case 0:
break;
case 1:
out << encoding[data[0] >> 2];
out << encoding[((data[0] & 0x3) << 4)];
out << PAD;
out << PAD;
*out++ = encoding[data[0] >> 2];
*out++ = encoding[((data[0] & 0x3) << 4)];
*out++ = PAD;
*out++ = PAD;
break;
case 2:
out << encoding[data[0] >> 2];
out << encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
out << encoding[((data[1] & 0xf) << 2)];
out << PAD;
*out++ = encoding[data[0] >> 2];
*out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
*out++ = encoding[((data[1] & 0xf) << 2)];
*out++ = PAD;
break;
}
out << "\"";
ret.resize(out - &ret[0]);
return ret;
}
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,
};
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;
if(input.empty())

View File

@@ -370,7 +370,7 @@ namespace YAML
bool WriteBinary(ostream& out, const Binary& binary)
{
WriteBase64(out, binary.data(), binary.size());
WriteDoubleQuotedString(out, EncodeBase64(binary.data(), binary.size()), false);
return true;
}
}