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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/object-packer.hpp"
#include "base/debug.hpp"
#include "base/dictionary.hpp"
#include "base/array.hpp"
#include "base/objectlock.hpp"
#include <algorithm>
#include <climits>
#include <cstdint>
#include <string>
#include <utility>
using namespace icinga;
union EndiannessDetector
{
EndiannessDetector()
{
i = 1;
}
int i;
char buf[sizeof(int)];
};
static const EndiannessDetector l_EndiannessDetector;
// Assumption: The compiler will optimize (away) if/else statements using this.
#define MACHINE_LITTLE_ENDIAN (l_EndiannessDetector.buf[0])
static void PackAny(const Value& value, std::string& builder);
/**
* std::swap() seems not to work
*/
static inline void SwapBytes(char& a, char& b)
{
char c = a;
a = b;
b = c;
}
#if CHAR_MIN != 0
union CharU2SConverter
{
CharU2SConverter()
{
s = 0;
}
unsigned char u;
signed char s;
};
#endif
/**
* Avoid implementation-defined overflows during unsigned to signed casts
*/
static inline char UIntToByte(unsigned i)
{
#if CHAR_MIN == 0
return i;
#else
CharU2SConverter converter;
converter.u = i;
return converter.s;
#endif
}
/**
* Append the given int as big-endian 64-bit unsigned int
*/
static inline void PackUInt64BE(uint_least64_t i, std::string& builder)
{
char buf[8] = {
UIntToByte(i >> 56u),
UIntToByte((i >> 48u) & 255u),
UIntToByte((i >> 40u) & 255u),
UIntToByte((i >> 32u) & 255u),
UIntToByte((i >> 24u) & 255u),
UIntToByte((i >> 16u) & 255u),
UIntToByte((i >> 8u) & 255u),
UIntToByte(i & 255u)
};
builder.append((char*)buf, 8);
}
union Double2BytesConverter
{
Double2BytesConverter()
{
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
buf[6] = 0;
buf[7] = 0;
}
double f;
char buf[8];
};
/**
* Append the given double as big-endian IEEE 754 binary64
*/
static inline void PackFloat64BE(double f, std::string& builder)
{
Double2BytesConverter converter;
converter.f = f;
if (MACHINE_LITTLE_ENDIAN) {
SwapBytes(converter.buf[0], converter.buf[7]);
SwapBytes(converter.buf[1], converter.buf[6]);
SwapBytes(converter.buf[2], converter.buf[5]);
SwapBytes(converter.buf[3], converter.buf[4]);
}
builder.append((char*)converter.buf, 8);
}
/**
* Append the given string's length (BE uint64) and the string itself
*/
static inline void PackString(const String& string, std::string& builder)
{
PackUInt64BE(string.GetLength(), builder);
builder += string.GetData();
}
/**
* Append the given array
*/
static inline void PackArray(const Array::Ptr& arr, std::string& builder)
{
ObjectLock olock(arr);
builder += '\5';
PackUInt64BE(arr->GetLength(), builder);
for (const Value& value : arr) {
PackAny(value, builder);
}
}
/**
* Append the given dictionary
*/
static inline void PackDictionary(const Dictionary::Ptr& dict, std::string& builder)
{
ObjectLock olock(dict);
builder += '\6';
PackUInt64BE(dict->GetLength(), builder);
for (const Dictionary::Pair& kv : dict) {
PackString(kv.first, builder);
PackAny(kv.second, builder);
}
}
/**
* Append any JSON-encodable value
*/
static void PackAny(const Value& value, std::string& builder)
{
switch (value.GetType()) {
case ValueString:
builder += '\4';
PackString(value.Get<String>(), builder);
break;
case ValueNumber:
builder += '\3';
PackFloat64BE(value.Get<double>(), builder);
break;
case ValueBoolean:
builder += (value.ToBool() ? '\2' : '\1');
break;
case ValueEmpty:
builder += '\0';
break;
case ValueObject:
{
const Object::Ptr& obj = value.Get<Object::Ptr>();
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(obj);
if (dict) {
PackDictionary(dict, builder);
break;
}
Array::Ptr arr = dynamic_pointer_cast<Array>(obj);
if (arr) {
PackArray(arr, builder);
break;
}
}
builder += '\0';
break;
default:
VERIFY(!"Invalid variant type.");
}
}
/**
* Pack any JSON-encodable value to a BSON-similar structure suitable for consistent hashing
*
* Spec:
* null: 0x00
* false: 0x01
* true: 0x02
* number: 0x03 (ieee754_binary64_bigendian)payload
* string: 0x04 (uint64_bigendian)payload.length (char[])payload
* array: 0x05 (uint64_bigendian)payload.length (any[])payload
* object: 0x06 (uint64_bigendian)payload.length (keyvalue[])payload.sort()
*
* any: null|false|true|number|string|array|object
* keyvalue: (uint64_bigendian)key.length (char[])key (any)value
*
* Assumptions:
* - double is IEEE 754 binary64
* - all int types (signed and unsigned) and all float types share the same endianness
* - char is exactly 8 bits wide and one char is exactly one byte affected by the machine endianness
* - all input strings, arrays and dictionaries are at most 2^64-1 long
*
* If not, this function will silently produce invalid results.
*/
String icinga::PackObject(const Value& value)
{
std::string builder;
PackAny(value, builder);
return std::move(builder);
}
|