const uint8_t kMagic[2] = {0, 1};
-static void bserEncode(dynamic const& dyn,
- QueueAppender& appender,
- const serialization_opts& opts);
+static void bserEncode(
+ dynamic const& dyn,
+ QueueAppender& appender,
+ const serialization_opts& opts);
serialization_opts::serialization_opts()
: sort_keys(false), growth_increment(8192) {}
-static const dynamic* getTemplate(const serialization_opts& opts,
- dynamic const& dynArray) {
+static const dynamic* getTemplate(
+ const serialization_opts& opts,
+ dynamic const& dynArray) {
if (!opts.templates.hasValue()) {
return nullptr;
}
static void bserEncodeInt(int64_t ival, QueueAppender& appender) {
/* Return the smallest size int that can store the value */
auto size =
- ((ival == ((int8_t)ival)) ? 1 : (ival == ((int16_t)ival))
- ? 2
- : (ival == ((int32_t)ival)) ? 4 : 8);
+ ((ival == ((int8_t)ival))
+ ? 1
+ : (ival == ((int16_t)ival)) ? 2 : (ival == ((int32_t)ival)) ? 4 : 8);
switch (size) {
case 1:
appender.push((uint8_t*)str.data(), str.size());
}
-static void bserEncodeArraySimple(dynamic const& dyn,
- QueueAppender& appender,
- const serialization_opts& opts) {
+static void bserEncodeArraySimple(
+ dynamic const& dyn,
+ QueueAppender& appender,
+ const serialization_opts& opts) {
appender.write((int8_t)BserType::Array);
bserEncodeInt(int64_t(dyn.size()), appender);
for (const auto& ele : dyn) {
}
}
-static void bserEncodeArray(dynamic const& dyn,
- QueueAppender& appender,
- const serialization_opts& opts) {
-
+static void bserEncodeArray(
+ dynamic const& dyn,
+ QueueAppender& appender,
+ const serialization_opts& opts) {
auto templ = getTemplate(opts, dyn);
if (UNLIKELY(templ != nullptr)) {
appender.write((int8_t)BserType::Template);
bserEncodeArraySimple(dyn, appender, opts);
}
-static void bserEncodeObject(dynamic const& dyn,
- QueueAppender& appender,
- const serialization_opts& opts) {
+static void bserEncodeObject(
+ dynamic const& dyn,
+ QueueAppender& appender,
+ const serialization_opts& opts) {
appender.write((int8_t)BserType::Object);
bserEncodeInt(int64_t(dyn.size()), appender);
if (opts.sort_keys) {
- std::vector<std::pair<dynamic, dynamic>> sorted(dyn.items().begin(),
- dyn.items().end());
+ std::vector<std::pair<dynamic, dynamic>> sorted(
+ dyn.items().begin(), dyn.items().end());
std::sort(sorted.begin(), sorted.end());
for (const auto& item : sorted) {
bserEncode(item.first, appender, opts);
}
}
-static void bserEncode(dynamic const& dyn,
- QueueAppender& appender,
- const serialization_opts& opts) {
+static void bserEncode(
+ dynamic const& dyn,
+ QueueAppender& appender,
+ const serialization_opts& opts) {
switch (dyn.type()) {
case dynamic::Type::NULLT:
appender.write((int8_t)BserType::Null);
}
}
-std::unique_ptr<folly::IOBuf> toBserIOBuf(folly::dynamic const& dyn,
- const serialization_opts& opts) {
+std::unique_ptr<folly::IOBuf> toBserIOBuf(
+ folly::dynamic const& dyn,
+ const serialization_opts& opts) {
IOBufQueue q(IOBufQueue::cacheChainLength());
uint8_t hdrbuf[sizeof(kMagic) + 1 + sizeof(int64_t)];
template <typename... ARGS>
[[noreturn]] static void throwDecodeError(Cursor& curs, ARGS&&... args) {
- throw BserDecodeError(folly::to<std::string>(std::forward<ARGS>(args)...,
- " with ",
- curs.length(),
- " bytes remaining in cursor"));
+ throw BserDecodeError(folly::to<std::string>(
+ std::forward<ARGS>(args)...,
+ " with ",
+ curs.length(),
+ " bytes remaining in cursor"));
}
static int64_t decodeInt(Cursor& curs) {
if (available == 0) {
// Saw this case when we decodeHeader was returning the incorrect length
// and we were splitting off too few bytes from the IOBufQueue
- throwDecodeError(curs,
- "no data available while decoding a string, header was "
- "not decoded properly");
+ throwDecodeError(
+ curs,
+ "no data available while decoding a string, header was "
+ "not decoded properly");
}
str.append(reinterpret_cast<const char*>(curs.data()), available);
curs.skipAtMost(available);
} catch (const std::exception& err) {
LOG(ERROR) << err.what() << "\nInput: " << dyn.typeName() << ": " << dyn
<< " decoded back as " << decoded.typeName() << ": " << decoded
- << "\n" << folly::hexDump(str.data(), str.size());
+ << "\n"
+ << folly::hexDump(str.data(), str.size());
throw;
}
}
decoded = folly::bser::parseBser(
folly::ByteRange(template_blob, sizeof(template_blob) - 1));
EXPECT_EQ(decoded, template_dynamic)
- << "Didn't load template value."
- "\nInput: " << template_dynamic.typeName() << ": " << template_dynamic
+ << "Didn't load template value.\n"
+ << "Input: " << template_dynamic.typeName() << ": " << template_dynamic
<< " decoded back as " << decoded.typeName() << ": " << decoded << "\n"
<< folly::hexDump(template_blob, sizeof(template_blob) - 1);
opts.templates = templates;
str = folly::bser::toBser(decoded, opts);
- EXPECT_EQ(folly::ByteRange((const uint8_t*)str.data(), str.size()),
- folly::ByteRange(template_blob, sizeof(template_blob) - 1))
+ EXPECT_EQ(
+ folly::ByteRange((const uint8_t*)str.data(), str.size()),
+ folly::ByteRange(template_blob, sizeof(template_blob) - 1))
<< "Expected:\n"
<< folly::hexDump(template_blob, sizeof(template_blob) - 1) << "\nGot:\n"
<< folly::hexDump(str.data(), str.size());
}
TEST(Bser, PduLength) {
- EXPECT_THROW([] {
- // Try to decode PDU for a short buffer that doesn't even have the
- // complete length available
- auto buf = folly::IOBuf::wrapBuffer(template_blob, 3);
- auto len = folly::bser::decodePduLength(&*buf);
- LOG(ERROR) << "managed to return a length, but only had 3 bytes";
- }(), std::out_of_range);
+ EXPECT_THROW(
+ [] {
+ // Try to decode PDU for a short buffer that doesn't even have the
+ // complete length available
+ auto buf = folly::IOBuf::wrapBuffer(template_blob, 3);
+ auto len = folly::bser::decodePduLength(&*buf);
+ LOG(ERROR) << "managed to return a length, but only had 3 bytes";
+ }(),
+ std::out_of_range);
auto buf = folly::IOBuf::wrapBuffer(template_blob, sizeof(template_blob));
auto len = folly::bser::decodePduLength(&*buf);