proxygen
folly::bser Namespace Reference

Classes

class  BserDecodeError
 
struct  serialization_opts
 

Enumerations

enum  BserType : int8_t {
  BserType::Array = 0, BserType::Object, BserType::String, BserType::Int8,
  BserType::Int16, BserType::Int32, BserType::Int64, BserType::Real,
  BserType::True, BserType::False, BserType::Null, BserType::Template,
  BserType::Skip
}
 

Functions

folly::dynamic parseBser (folly::StringPiece)
 
folly::dynamic parseBser (folly::ByteRange)
 
folly::dynamic parseBser (const folly::IOBuf *)
 
size_t decodePduLength (const folly::IOBuf *)
 
folly::fbstring toBser (folly::dynamic const &, const serialization_opts &)
 
std::unique_ptr< folly::IOBuftoBserIOBuf (folly::dynamic const &, const serialization_opts &)
 
static void bserEncode (dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
 
static const dynamicgetTemplate (const serialization_opts &opts, dynamic const &dynArray)
 
static void bserEncodeInt (int64_t ival, QueueAppender &appender)
 
static void bserEncodeString (folly::StringPiece str, QueueAppender &appender)
 
static void bserEncodeArraySimple (dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
 
static void bserEncodeArray (dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
 
static void bserEncodeObject (dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
 
static dynamic parseBser (Cursor &curs)
 
template<typename... ARGS>
static void throwDecodeError (Cursor &curs, ARGS &&...args)
 
static int64_t decodeInt (Cursor &curs)
 
static std::string decodeString (Cursor &curs)
 
static dynamic decodeArray (Cursor &curs)
 
static dynamic decodeObject (Cursor &curs)
 
static dynamic decodeTemplate (Cursor &curs)
 
static size_t decodeHeader (Cursor &curs)
 

Variables

const uint8_t kMagic [2] = {0, 1}
 

Enumeration Type Documentation

Enumerator
Array 
Object 
String 
Int8 
Int16 
Int32 
Int64 
Real 
True 
False 
Null 
Template 
Skip 

Definition at line 41 of file Bser.h.

Function Documentation

static void folly::bser::bserEncode ( dynamic const &  dyn,
QueueAppender appender,
const serialization_opts opts 
)
static

Definition at line 158 of file Dump.cpp.

References bserEncodeArray(), bserEncodeInt(), bserEncodeObject(), bserEncodeString(), False, folly::dynamic::getBool(), folly::dynamic::getDouble(), folly::dynamic::getInt(), folly::dynamic::getString(), int8_t, Null, Real, STRING, True, folly::dynamic::type(), and folly::io::QueueAppender::write().

Referenced by bserEncodeArray(), bserEncodeArraySimple(), bserEncodeObject(), and toBserIOBuf().

161  {
162  switch (dyn.type()) {
163  case dynamic::Type::NULLT:
164  appender.write((int8_t)BserType::Null);
165  return;
166  case dynamic::Type::BOOL:
167  appender.write(
168  (int8_t)(dyn.getBool() ? BserType::True : BserType::False));
169  return;
170  case dynamic::Type::DOUBLE: {
171  double dval = dyn.getDouble();
172  appender.write((int8_t)BserType::Real);
173  appender.write(dval);
174  return;
175  }
176  case dynamic::Type::INT64:
177  bserEncodeInt(dyn.getInt(), appender);
178  return;
179  case dynamic::Type::OBJECT:
180  bserEncodeObject(dyn, appender, opts);
181  return;
182  case dynamic::Type::ARRAY:
183  bserEncodeArray(dyn, appender, opts);
184  return;
186  bserEncodeString(dyn.getString(), appender);
187  return;
188  }
189 }
std::true_type True
Definition: TypeList.h:82
static void bserEncodeString(folly::StringPiece str, QueueAppender &appender)
Definition: Dump.cpp:81
std::enable_if< std::is_arithmetic< T >::value >::type write(T value)
Definition: Cursor.h:1137
#define STRING
std::false_type False
Definition: TypeList.h:83
static void bserEncodeInt(int64_t ival, QueueAppender &appender)
Definition: Dump.cpp:52
static void bserEncodeArray(dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
Definition: Dump.cpp:98
static void bserEncodeObject(dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
Definition: Dump.cpp:135
static void folly::bser::bserEncodeArray ( dynamic const &  dyn,
QueueAppender appender,
const serialization_opts opts 
)
static

Definition at line 98 of file Dump.cpp.

References bserEncode(), bserEncodeArraySimple(), bserEncodeInt(), getTemplate(), int64_t, int8_t, name, folly::dynamic::size(), Skip, Template, UNLIKELY, and folly::io::QueueAppender::write().

Referenced by bserEncode().

101  {
102  auto templ = getTemplate(opts, dyn);
103  if (UNLIKELY(templ != nullptr)) {
104  appender.write((int8_t)BserType::Template);
105 
106  // Emit the list of property names
107  bserEncodeArraySimple(*templ, appender, opts);
108 
109  // The number of objects in the array
110  bserEncodeInt(int64_t(dyn.size()), appender);
111 
112  // For each object in the array
113  for (const auto& ele : dyn) {
114  // For each key in the template
115  for (const auto& name : *templ) {
116  if (auto found = ele.get_ptr(name)) {
117  if (found->isNull()) {
118  // Prefer to Skip rather than encode a null value for
119  // compatibility with the other bser implementations
120  appender.write((int8_t)BserType::Skip);
121  } else {
122  bserEncode(*found, appender, opts);
123  }
124  } else {
125  appender.write((int8_t)BserType::Skip);
126  }
127  }
128  }
129  return;
130  }
131 
132  bserEncodeArraySimple(dyn, appender, opts);
133 }
std::enable_if< std::is_arithmetic< T >::value >::type write(T value)
Definition: Cursor.h:1137
def Skip(lines, pos, regex)
Definition: pump.py:261
static const dynamic * getTemplate(const serialization_opts &opts, dynamic const &dynArray)
Definition: Dump.cpp:38
const char * name
Definition: http_parser.c:437
static void bserEncodeInt(int64_t ival, QueueAppender &appender)
Definition: Dump.cpp:52
static void bserEncode(dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
Definition: Dump.cpp:158
static void bserEncodeArraySimple(dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
Definition: Dump.cpp:87
#define UNLIKELY(x)
Definition: Likely.h:48
static void folly::bser::bserEncodeArraySimple ( dynamic const &  dyn,
QueueAppender appender,
const serialization_opts opts 
)
static

Definition at line 87 of file Dump.cpp.

References Array, bserEncode(), bserEncodeInt(), int64_t, int8_t, folly::dynamic::size(), and folly::io::QueueAppender::write().

Referenced by bserEncodeArray().

90  {
91  appender.write((int8_t)BserType::Array);
92  bserEncodeInt(int64_t(dyn.size()), appender);
93  for (const auto& ele : dyn) {
94  bserEncode(ele, appender, opts);
95  }
96 }
std::enable_if< std::is_arithmetic< T >::value >::type write(T value)
Definition: Cursor.h:1137
static void bserEncodeInt(int64_t ival, QueueAppender &appender)
Definition: Dump.cpp:52
static void bserEncode(dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
Definition: Dump.cpp:158
static void folly::bser::bserEncodeInt ( int64_t  ival,
QueueAppender appender 
)
static

Definition at line 52 of file Dump.cpp.

References Int16, int16_t, Int32, int32_t, Int64, Int8, int8_t, folly::size(), and folly::io::QueueAppender::write().

Referenced by bserEncode(), bserEncodeArray(), bserEncodeArraySimple(), bserEncodeObject(), and bserEncodeString().

52  {
53  /* Return the smallest size int that can store the value */
54  auto size =
55  ((ival == ((int8_t)ival))
56  ? 1
57  : (ival == ((int16_t)ival)) ? 2 : (ival == ((int32_t)ival)) ? 4 : 8);
58 
59  switch (size) {
60  case 1:
61  appender.write((int8_t)BserType::Int8);
62  appender.write(int8_t(ival));
63  return;
64  case 2:
65  appender.write((int8_t)BserType::Int16);
66  appender.write(int16_t(ival));
67  return;
68  case 4:
69  appender.write((int8_t)BserType::Int32);
70  appender.write(int32_t(ival));
71  return;
72  case 8:
73  appender.write((int8_t)BserType::Int64);
74  appender.write(ival);
75  return;
76  default:
77  throw std::runtime_error("impossible integer size");
78  }
79 }
TypeWithSize< 8 >::Int Int64
Definition: gtest-port.h:2496
TypeWithSize< 4 >::Int Int32
Definition: gtest-port.h:2494
std::enable_if< std::is_arithmetic< T >::value >::type write(T value)
Definition: Cursor.h:1137
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
static void folly::bser::bserEncodeObject ( dynamic const &  dyn,
QueueAppender appender,
const serialization_opts opts 
)
static

Definition at line 135 of file Dump.cpp.

References bserEncode(), bserEncodeInt(), int64_t, int8_t, folly::dynamic::items(), Object, folly::dynamic::size(), folly::bser::serialization_opts::sort_keys, and folly::io::QueueAppender::write().

Referenced by bserEncode().

138  {
139  appender.write((int8_t)BserType::Object);
140  bserEncodeInt(int64_t(dyn.size()), appender);
141 
142  if (opts.sort_keys) {
143  std::vector<std::pair<dynamic, dynamic>> sorted(
144  dyn.items().begin(), dyn.items().end());
145  std::sort(sorted.begin(), sorted.end());
146  for (const auto& item : sorted) {
147  bserEncode(item.first, appender, opts);
148  bserEncode(item.second, appender, opts);
149  }
150  } else {
151  for (const auto& item : dyn.items()) {
152  bserEncode(item.first, appender, opts);
153  bserEncode(item.second, appender, opts);
154  }
155  }
156 }
std::enable_if< std::is_arithmetic< T >::value >::type write(T value)
Definition: Cursor.h:1137
static void bserEncodeInt(int64_t ival, QueueAppender &appender)
Definition: Dump.cpp:52
static void bserEncode(dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
Definition: Dump.cpp:158
static void folly::bser::bserEncodeString ( folly::StringPiece  str,
QueueAppender appender 
)
static

Definition at line 81 of file Dump.cpp.

References bserEncodeInt(), folly::Range< Iter >::data(), int64_t, int8_t, folly::io::detail::Writable< Derived >::push(), folly::Range< Iter >::size(), String, uint8_t, and folly::io::QueueAppender::write().

Referenced by bserEncode().

81  {
82  appender.write((int8_t)BserType::String);
83  bserEncodeInt(int64_t(str.size()), appender);
84  appender.push((uint8_t*)str.data(), str.size());
85 }
std::enable_if< std::is_arithmetic< T >::value >::type write(T value)
Definition: Cursor.h:1137
constexpr size_type size() const
Definition: Range.h:431
void push(const uint8_t *buf, size_t len)
Definition: Cursor.h:755
static void bserEncodeInt(int64_t ival, QueueAppender &appender)
Definition: Dump.cpp:52
constexpr Iter data() const
Definition: Range.h:446
static dynamic folly::bser::decodeArray ( Cursor curs)
static

Definition at line 83 of file Load.cpp.

References folly::dynamic::array(), decodeInt(), parseBser(), folly::dynamic::push_back(), and folly::size().

Referenced by decodeTemplate(), and parseBser().

83  {
84  dynamic arr = dynamic::array();
85  auto size = decodeInt(curs);
86  while (size-- > 0) {
87  arr.push_back(parseBser(curs));
88  }
89  return arr;
90 }
static dynamic parseBser(Cursor &curs)
Definition: Load.cpp:136
static int64_t decodeInt(Cursor &curs)
Definition: Load.cpp:38
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
void push_back(dynamic const &)
Definition: dynamic-inl.h:969
static size_t folly::bser::decodeHeader ( Cursor curs)
static

Definition at line 173 of file Load.cpp.

References folly::Range< Iter >::at(), decodeInt(), Int16, Int32, Int64, Int8, kMagic, folly::io::detail::CursorBase< Derived, BufType >::peekBytes(), and folly::io::detail::CursorBase< Derived, BufType >::pull().

Referenced by decodePduLength(), and parseBser().

173  {
174  char header[sizeof(kMagic)];
175  curs.pull(header, sizeof(header));
176  if (memcmp(header, kMagic, sizeof(kMagic))) {
177  throw std::runtime_error("invalid BSER magic header");
178  }
179 
180  auto enc = (BserType)curs.peekBytes().at(0);
181  size_t int_size;
182  switch (enc) {
183  case BserType::Int8:
184  int_size = 1;
185  break;
186  case BserType::Int16:
187  int_size = 2;
188  break;
189  case BserType::Int32:
190  int_size = 4;
191  break;
192  case BserType::Int64:
193  int_size = 8;
194  break;
195  default:
196  int_size = 0;
197  }
198 
199  return int_size + 3 /* magic + int type */ + decodeInt(curs);
200 }
TypeWithSize< 8 >::Int Int64
Definition: gtest-port.h:2496
TypeWithSize< 4 >::Int Int32
Definition: gtest-port.h:2494
static int64_t decodeInt(Cursor &curs)
Definition: Load.cpp:38
void pull(void *buf, size_t len)
Definition: Cursor.h:418
BserType
Definition: Bser.h:41
value_type & at(size_t i)
Definition: Range.h:628
const uint8_t kMagic[2]
Definition: Dump.cpp:28
static int64_t folly::bser::decodeInt ( Cursor curs)
static

Definition at line 38 of file Load.cpp.

References Int16, int16_t, Int32, int32_t, Int64, int64_t, Int8, int8_t, folly::io::detail::CursorBase< Derived, BufType >::read(), and throwDecodeError().

Referenced by decodeArray(), decodeHeader(), decodeObject(), decodeString(), and decodeTemplate().

38  {
39  auto enc = (BserType)curs.read<int8_t>();
40  switch (enc) {
41  case BserType::Int8:
42  return curs.read<int8_t>();
43  case BserType::Int16:
44  return curs.read<int16_t>();
45  case BserType::Int32:
46  return curs.read<int32_t>();
47  case BserType::Int64:
48  return curs.read<int64_t>();
49  default:
51  curs, "invalid integer encoding detected (", (int8_t)enc, ")");
52  }
53 }
TypeWithSize< 8 >::Int Int64
Definition: gtest-port.h:2496
TypeWithSize< 4 >::Int Int32
Definition: gtest-port.h:2494
BserType
Definition: Bser.h:41
static void throwDecodeError(Cursor &curs, ARGS &&...args)
Definition: Load.cpp:30
static dynamic folly::bser::decodeObject ( Cursor curs)
static

Definition at line 92 of file Load.cpp.

References decodeInt(), decodeString(), int8_t, folly::dynamic::object(), parseBser(), folly::io::detail::CursorBase< Derived, BufType >::read(), folly::size(), String, and throwDecodeError().

Referenced by parseBser().

92  {
94  auto size = decodeInt(curs);
95  while (size-- > 0) {
96  if ((BserType)curs.read<int8_t>() != BserType::String) {
97  throwDecodeError(curs, "expected String");
98  }
99  auto key = decodeString(curs);
100  obj[key] = parseBser(curs);
101  }
102  return obj;
103 }
void * object
Definition: AtFork.cpp:32
static dynamic parseBser(Cursor &curs)
Definition: Load.cpp:136
static int64_t decodeInt(Cursor &curs)
Definition: Load.cpp:38
BserType
Definition: Bser.h:41
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
static void throwDecodeError(Cursor &curs, ARGS &&...args)
Definition: Load.cpp:30
static std::string decodeString(Cursor &curs)
Definition: Load.cpp:55
size_t folly::bser::decodePduLength ( const folly::IOBuf buf)

Definition at line 202 of file Load.cpp.

References decodeHeader().

Referenced by TEST().

202  {
203  Cursor curs(buf);
204  return decodeHeader(curs);
205 }
static size_t decodeHeader(Cursor &curs)
Definition: Load.cpp:173
static std::string folly::bser::decodeString ( Cursor curs)
static

Definition at line 55 of file Load.cpp.

References decodeInt(), upload::dest, folly::io::detail::CursorBase< Derived, BufType >::pullAtMost(), string, and throwDecodeError().

Referenced by decodeObject(), and parseBser().

55  {
56  auto len = decodeInt(curs);
57  std::string str;
58 
59  if (len < 0) {
60  throw std::range_error("string length must not be negative");
61  }
62 
63  // We could use Cursor::readFixedString() here, but we'd like
64  // to throw our own exception with some increased diagnostics.
65  str.resize(len);
66 
67  // The start of the string data, mutable.
68  auto* dest = &str[0];
69 
70  auto pulled = curs.pullAtMost(dest, len);
71  if (pulled != size_t(len)) {
72  // Saw this case when decodeHeader was returning the incorrect length
73  // and we were splitting off too few bytes from the IOBufQueue
75  curs,
76  "no data available while decoding a string, header was "
77  "not decoded properly");
78  }
79 
80  return str;
81 }
dest
Definition: upload.py:394
size_t pullAtMost(void *buf, size_t len)
Definition: Cursor.h:407
static int64_t decodeInt(Cursor &curs)
Definition: Load.cpp:38
static void throwDecodeError(Cursor &curs, ARGS &&...args)
Definition: Load.cpp:30
const char * string
Definition: Conv.cpp:212
static dynamic folly::bser::decodeTemplate ( Cursor curs)
static

Definition at line 105 of file Load.cpp.

References Array, folly::dynamic::array(), decodeArray(), decodeInt(), int8_t, folly::gen::move, name, folly::dynamic::object(), parseBser(), folly::io::detail::CursorBase< Derived, BufType >::peekBytes(), folly::dynamic::push_back(), folly::io::detail::CursorBase< Derived, BufType >::read(), folly::size(), Skip, and folly::io::detail::CursorBase< Derived, BufType >::skipAtMost().

Referenced by parseBser().

105  {
107 
108  // List of property names
109  if ((BserType)curs.read<int8_t>() != BserType::Array) {
110  throw std::runtime_error("Expected array encoding for property names");
111  }
112  auto names = decodeArray(curs);
113 
114  auto size = decodeInt(curs);
115 
116  while (size-- > 0) {
117  dynamic obj = dynamic::object;
118 
119  for (auto& name : names) {
120  auto bytes = curs.peekBytes();
121  if ((BserType)bytes.at(0) == BserType::Skip) {
122  obj[name.getString()] = nullptr;
123  curs.skipAtMost(1);
124  continue;
125  }
126 
127  obj[name.getString()] = parseBser(curs);
128  }
129 
130  arr.push_back(std::move(obj));
131  }
132 
133  return arr;
134 }
void * object
Definition: AtFork.cpp:32
static dynamic parseBser(Cursor &curs)
Definition: Load.cpp:136
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
def Skip(lines, pos, regex)
Definition: pump.py:261
static int64_t decodeInt(Cursor &curs)
Definition: Load.cpp:38
BserType
Definition: Bser.h:41
const char * name
Definition: http_parser.c:437
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
size_t skipAtMost(size_t len)
Definition: Cursor.h:362
static dynamic decodeArray(Cursor &curs)
Definition: Load.cpp:83
void push_back(dynamic const &)
Definition: dynamic-inl.h:969
static void array(EmptyArrayTag)
Definition: dynamic-inl.h:233
static const dynamic* folly::bser::getTemplate ( const serialization_opts opts,
dynamic const &  dynArray 
)
static

Definition at line 38 of file Dump.cpp.

References folly::Optional< Value >::hasValue(), folly::bser::serialization_opts::templates, and folly::Optional< Value >::value().

Referenced by bserEncodeArray().

40  {
41  if (!opts.templates.hasValue()) {
42  return nullptr;
43  }
44  const auto& templates = opts.templates.value();
45  const auto it = templates.find(&dynArray);
46  if (it == templates.end()) {
47  return nullptr;
48  }
49  return &it->second;
50 }
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
Definition: Optional.h:300
folly::Optional< TemplateMap > templates
Definition: Bser.h:82
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
static dynamic folly::bser::parseBser ( Cursor curs)
static

Definition at line 136 of file Load.cpp.

References Array, decodeArray(), decodeObject(), decodeString(), decodeTemplate(), False, Int16, int16_t, Int32, int32_t, Int64, int64_t, Int8, int8_t, Null, Object, folly::io::detail::CursorBase< Derived, BufType >::pull(), folly::io::detail::CursorBase< Derived, BufType >::read(), Real, Skip, String, Template, and True.

136  {
137  switch ((BserType)curs.read<int8_t>()) {
138  case BserType::Int8:
139  return curs.read<int8_t>();
140  case BserType::Int16:
141  return curs.read<int16_t>();
142  case BserType::Int32:
143  return curs.read<int32_t>();
144  case BserType::Int64:
145  return curs.read<int64_t>();
146  case BserType::Real: {
147  double dval;
148  curs.pull((void*)&dval, sizeof(dval));
149  return dval;
150  }
151  case BserType::Null:
152  return nullptr;
153  case BserType::True:
154  return (bool)true;
155  case BserType::False:
156  return (bool)false;
157  case BserType::String:
158  return decodeString(curs);
159  case BserType::Array:
160  return decodeArray(curs);
161  case BserType::Object:
162  return decodeObject(curs);
163  case BserType::Template:
164  return decodeTemplate(curs);
165  case BserType::Skip:
166  throw std::runtime_error(
167  "Skip not valid at this location in the bser stream");
168  default:
169  throw std::runtime_error("invalid bser encoding");
170  }
171 }
TypeWithSize< 8 >::Int Int64
Definition: gtest-port.h:2496
std::true_type True
Definition: TypeList.h:82
TypeWithSize< 4 >::Int Int32
Definition: gtest-port.h:2494
def Skip(lines, pos, regex)
Definition: pump.py:261
static dynamic decodeObject(Cursor &curs)
Definition: Load.cpp:92
std::false_type False
Definition: TypeList.h:83
void pull(void *buf, size_t len)
Definition: Cursor.h:418
BserType
Definition: Bser.h:41
static dynamic decodeArray(Cursor &curs)
Definition: Load.cpp:83
static std::string decodeString(Cursor &curs)
Definition: Load.cpp:55
static dynamic decodeTemplate(Cursor &curs)
Definition: Load.cpp:105
folly::dynamic folly::bser::parseBser ( folly::StringPiece  str)

Definition at line 219 of file Load.cpp.

References folly::Range< Iter >::data(), folly::Range< Iter >::size(), and uint8_t.

Referenced by decodeArray(), decodeObject(), decodeTemplate(), parseBser(), and TEST().

219  {
220  return parseBser(ByteRange((uint8_t*)str.data(), str.size()));
221 }
static dynamic parseBser(Cursor &curs)
Definition: Load.cpp:136
constexpr size_type size() const
Definition: Range.h:431
constexpr Iter data() const
Definition: Range.h:446
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
folly::dynamic folly::bser::parseBser ( folly::ByteRange  str)

Definition at line 214 of file Load.cpp.

References folly::Range< Iter >::data(), parseBser(), folly::Range< Iter >::size(), and folly::IOBuf::wrapBuffer().

214  {
215  auto buf = IOBuf::wrapBuffer(str.data(), str.size());
216  return parseBser(&*buf);
217 }
static dynamic parseBser(Cursor &curs)
Definition: Load.cpp:136
constexpr size_type size() const
Definition: Range.h:431
constexpr Iter data() const
Definition: Range.h:446
folly::dynamic folly::bser::parseBser ( const folly::IOBuf buf)

Definition at line 207 of file Load.cpp.

References decodeHeader(), and parseBser().

207  {
208  Cursor curs(buf);
209 
210  decodeHeader(curs);
211  return parseBser(curs);
212 }
static dynamic parseBser(Cursor &curs)
Definition: Load.cpp:136
static size_t decodeHeader(Cursor &curs)
Definition: Load.cpp:173
template<typename... ARGS>
static void folly::bser::throwDecodeError ( Cursor curs,
ARGS &&...  args 
)
static

Definition at line 30 of file Load.cpp.

References folly::io::detail::CursorBase< Derived, BufType >::length().

Referenced by decodeInt(), decodeObject(), and decodeString().

30  {
31  throw BserDecodeError(folly::to<std::string>(
32  std::forward<ARGS>(args)...,
33  " with ",
34  curs.length(),
35  " bytes remaining in cursor"));
36 }
size_t length() const
Definition: Cursor.h:118
fbstring folly::bser::toBser ( folly::dynamic const &  dyn,
const serialization_opts opts 
)

Definition at line 246 of file Dump.cpp.

References toBserIOBuf().

Referenced by TEST().

246  {
247  auto buf = toBserIOBuf(dyn, opts);
248  return buf->moveToFbString();
249 }
std::unique_ptr< folly::IOBuf > toBserIOBuf(folly::dynamic const &, const serialization_opts &)
Definition: Dump.cpp:191
std::unique_ptr< folly::IOBuf > folly::bser::toBserIOBuf ( folly::dynamic const &  dyn,
const serialization_opts opts 
)

Definition at line 191 of file Dump.cpp.

References folly::IOBufQueue::append(), bserEncode(), folly::IOBufQueue::cacheChainLength(), folly::IOBufQueue::chainLength(), folly::IOBuf::create(), folly::bser::serialization_opts::growth_increment, Int16, int16_t, Int32, int32_t, Int64, int64_t, Int8, int8_t, kMagic, max, folly::IOBufQueue::move(), folly::gen::move, folly::IOBufQueue::prepend(), uint64_t, and uint8_t.

Referenced by toBser().

193  {
194  IOBufQueue q(IOBufQueue::cacheChainLength());
195  uint8_t hdrbuf[sizeof(kMagic) + 1 + sizeof(int64_t)];
196 
197  // Reserve some headroom for the overall PDU size; we'll fill this in
198  // after we've serialized the data and know the length
199  auto firstbuf = IOBuf::create(opts.growth_increment);
200  firstbuf->advance(sizeof(hdrbuf));
201  q.append(std::move(firstbuf));
202 
203  // encode the value
204  QueueAppender appender(&q, opts.growth_increment);
205  bserEncode(dyn, appender, opts);
206 
207  // compute the length
208  auto len = q.chainLength();
210  throw std::range_error(folly::to<std::string>(
211  "serialized data size ", len, " is too large to represent as BSER"));
212  }
213 
214  // This is a bit verbose, but it computes a header that is appropriate
215  // to the size of the serialized data
216 
217  memcpy(hdrbuf, kMagic, sizeof(kMagic));
218  size_t hdrlen = sizeof(kMagic) + 1;
219  auto magicptr = hdrbuf + sizeof(kMagic);
220  auto lenptr = hdrbuf + hdrlen;
221 
223  *magicptr = (int8_t)BserType::Int64;
224  *(int64_t*)lenptr = (int64_t)len;
225  hdrlen += sizeof(int64_t);
226  } else if (len > uint64_t(std::numeric_limits<int16_t>::max())) {
227  *magicptr = (int8_t)BserType::Int32;
228  *(int32_t*)lenptr = (int32_t)len;
229  hdrlen += sizeof(int32_t);
230  } else if (len > uint64_t(std::numeric_limits<int8_t>::max())) {
231  *magicptr = (int8_t)BserType::Int16;
232  *(int16_t*)lenptr = (int16_t)len;
233  hdrlen += sizeof(int16_t);
234  } else {
235  *magicptr = (int8_t)BserType::Int8;
236  *(int8_t*)lenptr = (int8_t)len;
237  hdrlen += sizeof(int8_t);
238  }
239 
240  // and place the data in the headroom
241  q.prepend(hdrbuf, hdrlen);
242 
243  return q.move();
244 }
TypeWithSize< 8 >::Int Int64
Definition: gtest-port.h:2496
LogLevel max
Definition: LogLevel.cpp:31
TypeWithSize< 4 >::Int Int32
Definition: gtest-port.h:2494
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static void bserEncode(dynamic const &dyn, QueueAppender &appender, const serialization_opts &opts)
Definition: Dump.cpp:158
const uint8_t kMagic[2]
Definition: Dump.cpp:28

Variable Documentation