mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Refactored the base64 binary to its own space with a unified class that (will) be used for parsing (in addition to emitting)
This commit is contained in:
46
include/yaml-cpp/binary.h
Normal file
46
include/yaml-cpp/binary.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#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);
|
||||
|
||||
class Binary {
|
||||
public:
|
||||
Binary(): m_unownedData(0), m_unownedSize(0) {}
|
||||
Binary(const unsigned char *data, std::size_t size): m_unownedData(data), m_unownedSize(size) {}
|
||||
|
||||
bool owned() const { return !m_unownedData; }
|
||||
std::size_t size() const { return owned() ? m_data.size() : m_unownedSize; }
|
||||
const unsigned char *data() const { return owned() ? &m_data[0] : m_unownedData; }
|
||||
|
||||
void swap(std::vector<unsigned char>& rhs) {
|
||||
if(m_unownedData) {
|
||||
m_data.swap(rhs);
|
||||
rhs.clear();
|
||||
rhs.resize(m_unownedSize);
|
||||
std::copy(m_unownedData, m_unownedData + m_unownedSize, &rhs[0]);
|
||||
m_unownedData = 0;
|
||||
m_unownedSize = 0;
|
||||
} else {
|
||||
m_data.swap(rhs);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<unsigned char> m_data;
|
||||
const unsigned char *m_unownedData;
|
||||
std::size_t m_unownedSize;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
@@ -7,6 +7,7 @@
|
||||
|
||||
|
||||
#include "yaml-cpp/dll.h"
|
||||
#include "yaml-cpp/binary.h"
|
||||
#include "yaml-cpp/emittermanip.h"
|
||||
#include "yaml-cpp/ostream.h"
|
||||
#include "yaml-cpp/noncopyable.h"
|
||||
@@ -60,7 +61,7 @@ namespace YAML
|
||||
Emitter& Write(const _Tag& tag);
|
||||
Emitter& Write(const _Comment& comment);
|
||||
Emitter& Write(const _Null& null);
|
||||
Emitter& Write(const _Binary& binary);
|
||||
Emitter& Write(const Binary& binary);
|
||||
|
||||
template <typename T>
|
||||
Emitter& WriteIntegralType(T value);
|
||||
@@ -153,7 +154,7 @@ namespace YAML
|
||||
inline Emitter& operator << (Emitter& emitter, const _Tag& v) { return emitter.Write(v); }
|
||||
inline Emitter& operator << (Emitter& emitter, const _Comment& v) { return emitter.Write(v); }
|
||||
inline Emitter& operator << (Emitter& emitter, const _Null& v) { return emitter.Write(v); }
|
||||
inline Emitter& operator << (Emitter& emitter, const _Binary& b) { return emitter.Write(b); }
|
||||
inline Emitter& operator << (Emitter& emitter, const Binary& b) { return emitter.Write(b); }
|
||||
|
||||
inline Emitter& operator << (Emitter& emitter, const char *v) { return emitter.Write(std::string(v)); }
|
||||
|
||||
|
@@ -126,16 +126,6 @@ namespace YAML
|
||||
return _Comment(content);
|
||||
}
|
||||
|
||||
struct _Binary {
|
||||
_Binary(const unsigned char *data_, std::size_t size_): data(data_), size(size_) {}
|
||||
const unsigned char *data;
|
||||
std::size_t size;
|
||||
};
|
||||
|
||||
inline _Binary Binary(const unsigned char *data, std::size_t size) {
|
||||
return _Binary(data, size);
|
||||
}
|
||||
|
||||
struct _Precision {
|
||||
_Precision(int floatPrecision_, int doublePrecision_): floatPrecision(floatPrecision_), doublePrecision(doublePrecision_) {}
|
||||
|
||||
|
90
src/binary.cpp
Normal file
90
src/binary.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#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)
|
||||
{
|
||||
const char PAD = '=';
|
||||
|
||||
out << "\"";
|
||||
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];
|
||||
}
|
||||
|
||||
switch(remainder) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
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;
|
||||
break;
|
||||
}
|
||||
|
||||
out << "\"";
|
||||
}
|
||||
|
||||
static const unsigned char decoding[] = {
|
||||
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,
|
||||
255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
|
||||
255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
|
||||
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,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,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,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,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,255,255,255,255,255,
|
||||
};
|
||||
|
||||
std::vector<unsigned char> ReadBase64(const std::string& input)
|
||||
{
|
||||
typedef std::vector<unsigned char> ret_type;
|
||||
if(input.empty())
|
||||
return ret_type();
|
||||
|
||||
ret_type ret(3 * input.size() / 4 + 1);
|
||||
unsigned char *out = &ret[0];
|
||||
|
||||
unsigned value = 0;
|
||||
for(std::size_t i=0;i<input.size();i++) {
|
||||
unsigned char d = decoding[static_cast<unsigned>(input[i])];
|
||||
if(d == 255)
|
||||
return ret_type();
|
||||
|
||||
value = (value << 6) | d;
|
||||
if(i % 4 == 3) {
|
||||
*out++ = value >> 16;
|
||||
if(i > 0 && input[i - 1] != '=')
|
||||
*out++ = value >> 8;
|
||||
if(input[i] != '=')
|
||||
*out++ = value;
|
||||
}
|
||||
}
|
||||
|
||||
ret.resize(out - &ret[0]);
|
||||
return ret;
|
||||
}
|
||||
}
|
@@ -865,7 +865,7 @@ namespace YAML
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const _Binary& binary)
|
||||
Emitter& Emitter::Write(const Binary& binary)
|
||||
{
|
||||
Write(SecondaryTag("binary"));
|
||||
|
||||
@@ -874,7 +874,7 @@ namespace YAML
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
Utils::WriteBinary(m_stream, binary.data, binary.size);
|
||||
Utils::WriteBinary(m_stream, binary);
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#include "emitterutils.h"
|
||||
#include "exp.h"
|
||||
#include "indentation.h"
|
||||
#include "yaml-cpp/binary.h"
|
||||
#include "yaml-cpp/exceptions.h"
|
||||
#include "stringsource.h"
|
||||
#include <sstream>
|
||||
@@ -367,41 +368,10 @@ namespace YAML
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteBinary(ostream& out, const unsigned char *data, std::size_t size)
|
||||
bool WriteBinary(ostream& out, const Binary& binary)
|
||||
{
|
||||
static const char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
const char PAD = '=';
|
||||
|
||||
out << "\"";
|
||||
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];
|
||||
}
|
||||
|
||||
switch(remainder) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
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;
|
||||
break;
|
||||
}
|
||||
|
||||
out << "\"";
|
||||
return true;
|
||||
WriteBase64(out, binary.data(), binary.size());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Binary;
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
bool WriteString(ostream& out, const std::string& str, bool inFlow, bool escapeNonAscii);
|
||||
@@ -23,7 +25,7 @@ namespace YAML
|
||||
bool WriteAnchor(ostream& out, const std::string& str);
|
||||
bool WriteTag(ostream& out, const std::string& str, bool verbatim);
|
||||
bool WriteTagWithPrefix(ostream& out, const std::string& prefix, const std::string& tag);
|
||||
bool WriteBinary(ostream& out, const unsigned char *data, std::size_t size);
|
||||
bool WriteBinary(ostream& out, const Binary& binary);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user