From e17734de33d28178e9eb2e6fe9fdedea0e833c0f Mon Sep 17 00:00:00 2001 From: beder Date: Sat, 21 Jan 2012 01:33:49 -0600 Subject: [PATCH] Renamed the base64 methods, and switched the EncodeBase64 one to return a string (to make it easy to use elsewhere) --- include/yaml-cpp/binary.h | 7 +++---- src/binary.cpp | 37 ++++++++++++++++++++----------------- src/emitterutils.cpp | 2 +- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/include/yaml-cpp/binary.h b/include/yaml-cpp/binary.h index acb5e5f..243a284 100644 --- a/include/yaml-cpp/binary.h +++ b/include/yaml-cpp/binary.h @@ -5,14 +5,13 @@ #pragma once #endif +#include #include namespace YAML { - class ostream; - - void WriteBase64(ostream& out, const unsigned char *data, std::size_t size); - std::vector ReadBase64(const std::string& input); + std::string EncodeBase64(const unsigned char *data, std::size_t size); + std::vector DecodeBase64(const std::string& input); class Binary { public: diff --git a/src/binary.cpp b/src/binary.cpp index f599073..62a6032 100644 --- a/src/binary.cpp +++ b/src/binary.cpp @@ -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> 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 ReadBase64(const std::string& input) + std::vector DecodeBase64(const std::string& input) { typedef std::vector ret_type; if(input.empty()) diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index f27e10e..3d184d6 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -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; } }