2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/experimental/bser/Bser.h>
19 #include <folly/String.h>
20 #include <folly/io/Cursor.h>
22 using namespace folly;
23 using folly::io::Cursor;
27 static dynamic parseBser(Cursor& curs);
29 template <typename... ARGS>
30 [[noreturn]] static void throwDecodeError(Cursor& curs, ARGS&&... args) {
31 throw BserDecodeError(folly::to<std::string>(
32 std::forward<ARGS>(args)...,
35 " bytes remaining in cursor"));
38 static int64_t decodeInt(Cursor& curs) {
39 auto enc = (BserType)curs.read<int8_t>();
42 return curs.read<int8_t>();
44 return curs.read<int16_t>();
46 return curs.read<int32_t>();
48 return curs.read<int64_t>();
51 curs, "invalid integer encoding detected (", (int8_t)enc, ")");
55 static std::string decodeString(Cursor& curs) {
56 auto len = decodeInt(curs);
60 throw std::range_error("string length must not be negative");
62 str.reserve(size_t(len));
64 size_t available = curs.length();
65 while (available < (size_t)len) {
67 // Saw this case when we decodeHeader was returning the incorrect length
68 // and we were splitting off too few bytes from the IOBufQueue
71 "no data available while decoding a string, header was "
72 "not decoded properly");
74 str.append(reinterpret_cast<const char*>(curs.data()), available);
75 curs.skipAtMost(available);
77 available = curs.length();
80 str.append(reinterpret_cast<const char*>(curs.data()), size_t(len));
81 curs.skipAtMost(size_t(len));
85 static dynamic decodeArray(Cursor& curs) {
86 dynamic arr = dynamic::array();
87 auto size = decodeInt(curs);
89 arr.push_back(parseBser(curs));
94 static dynamic decodeObject(Cursor& curs) {
95 dynamic obj = dynamic::object;
96 auto size = decodeInt(curs);
98 if ((BserType)curs.read<int8_t>() != BserType::String) {
99 throwDecodeError(curs, "expected String");
101 auto key = decodeString(curs);
102 obj[key] = parseBser(curs);
107 static dynamic decodeTemplate(Cursor& curs) {
108 dynamic arr = folly::dynamic::array;
110 // List of property names
111 if ((BserType)curs.read<int8_t>() != BserType::Array) {
112 throw std::runtime_error("Expected array encoding for property names");
114 auto names = decodeArray(curs);
116 auto size = decodeInt(curs);
119 dynamic obj = dynamic::object;
121 for (auto& name : names) {
122 auto bytes = curs.peekBytes();
123 if ((BserType)bytes.at(0) == BserType::Skip) {
124 obj[name.getString()] = nullptr;
129 obj[name.getString()] = parseBser(curs);
132 arr.push_back(std::move(obj));
138 static dynamic parseBser(Cursor& curs) {
139 switch ((BserType)curs.read<int8_t>()) {
141 return curs.read<int8_t>();
142 case BserType::Int16:
143 return curs.read<int16_t>();
144 case BserType::Int32:
145 return curs.read<int32_t>();
146 case BserType::Int64:
147 return curs.read<int64_t>();
148 case BserType::Real: {
150 curs.pull((void*)&dval, sizeof(dval));
157 case BserType::False:
159 case BserType::String:
160 return decodeString(curs);
161 case BserType::Array:
162 return decodeArray(curs);
163 case BserType::Object:
164 return decodeObject(curs);
165 case BserType::Template:
166 return decodeTemplate(curs);
168 throw std::runtime_error(
169 "Skip not valid at this location in the bser stream");
171 throw std::runtime_error("invalid bser encoding");
175 static size_t decodeHeader(Cursor& curs) {
176 char header[sizeof(kMagic)];
177 curs.pull(header, sizeof(header));
178 if (memcmp(header, kMagic, sizeof(kMagic))) {
179 throw std::runtime_error("invalid BSER magic header");
182 auto enc = (BserType)curs.peekBytes().at(0);
188 case BserType::Int16:
191 case BserType::Int32:
194 case BserType::Int64:
201 return int_size + 3 /* magic + int type */ + decodeInt(curs);
204 size_t decodePduLength(const folly::IOBuf* buf) {
206 return decodeHeader(curs);
209 folly::dynamic parseBser(const IOBuf* buf) {
213 return parseBser(curs);
216 folly::dynamic parseBser(ByteRange str) {
217 auto buf = IOBuf::wrapBuffer(str.data(), str.size());
218 return parseBser(&*buf);
221 folly::dynamic parseBser(StringPiece str) {
222 return parseBser(ByteRange((uint8_t*)str.data(), str.size()));