forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerializationUtil.cpp
190 lines (169 loc) · 4.91 KB
/
SerializationUtil.cpp
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
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <wdt/util/SerializationUtil.h>
#include <folly/lang/Bits.h>
using folly::ByteRange;
using std::string;
namespace facebook {
namespace wdt {
ByteRange makeByteRange(string str) {
return ByteRange((uint8_t *)str.data(), str.size());
}
ByteRange makeByteRange(char *dest, int64_t sz, int64_t off) {
WDT_CHECK_GE(off, 0);
WDT_CHECK_GE(sz, 0);
WDT_CHECK(dest != nullptr);
return ByteRange((uint8_t *)(dest + off), sz - off);
}
int64_t offset(const folly::ByteRange &newRange,
const folly::ByteRange &oldRange) {
WDT_CHECK_EQ(newRange.end(), oldRange.end());
return newRange.start() - oldRange.start();
}
bool decodeInt32(ByteRange &br, int32_t &res32) {
int64_t res64;
ByteRange obr = br;
bool ok = decodeInt64(br, res64);
if (!ok) {
return false;
}
if (res64 > INT32_MAX || res64 < INT32_MIN) {
WLOG(ERROR) << "var int32 decoded value " << res64
<< " does not fit in a 32 bit number as expected";
br = obr;
return false;
}
res32 = static_cast<int32_t>(res64);
return true;
}
bool decodeInt64(ByteRange &br, int64_t &res) {
int64_t pos = 0;
bool ret = decodeVarI64((const char *)(br.start()), br.size(), pos, res);
if (!ret) {
return false;
}
WDT_CHECK_GE(pos, 1);
br.advance(pos);
return true;
}
bool decodeUInt64(ByteRange &br, uint64_t &res) {
int64_t pos = 0;
bool ret = decodeVarU64((const char *)(br.start()), br.size(), pos, res);
if (!ret) {
return false;
}
WDT_CHECK_GE(pos, 1);
br.advance(pos);
return true;
}
bool decodeInt64C(ByteRange &br, int64_t &sres) {
uint64_t ures;
bool ret = decodeUInt64(br, ures);
if (!ret) {
return false;
}
if (ures > INT64_MAX) {
WLOG(ERROR) << "Decoded as unsigned into signed, too large " << ures;
return false;
}
sres = ures;
return true;
}
bool decodeInt32C(ByteRange &br, int32_t &res32) {
int64_t res64;
ByteRange obr = br;
bool ok = decodeInt64C(br, res64);
if (!ok) {
return false;
}
if (res64 > INT32_MAX || res64 < 0) {
WLOG(ERROR) << "var int32 decoded value " << res64
<< " does not fit in a 31 bit positive number as expected";
br = obr;
return false;
}
res32 = static_cast<int32_t>(res64);
return true;
}
template <typename T>
bool decodeIntFixedLength(folly::ByteRange &br, T &res) {
if (br.size() < sizeof(T)) {
WLOG(ERROR) << "Not enough to read to decode fixed length encoded int";
return false;
}
res = folly::loadUnaligned<T>(br.start());
res = folly::Endian::little(res);
br.advance(sizeof(T));
if (res < 0) {
WLOG(ERROR) << "negative int decoded " << res;
return false;
}
return true;
}
bool decodeInt16FixedLength(folly::ByteRange &br, int16_t &res) {
bool success = decodeIntFixedLength<int16_t>(br, res);
return success;
}
bool decodeInt32FixedLength(folly::ByteRange &br, int32_t &res) {
return decodeIntFixedLength<int32_t>(br, res);
}
bool decodeInt64FixedLength(folly::ByteRange &br, int64_t &res) {
return decodeIntFixedLength<int64_t>(br, res);
}
template <typename T>
bool encodeIntFixedLength(char *dest, int64_t sz, int64_t &off, const T val) {
constexpr int intLen = sizeof(T);
if (off + intLen > sz) {
WLOG(ERROR) << "Not enough room to encode fixed length int " << val
<< " off: " << off << " buffer size: " << sz;
return false;
}
folly::storeUnaligned<T>(dest + off, folly::Endian::little(val));
off += intLen;
return true;
}
bool encodeInt16FixedLength(char *dest, int64_t sz, int64_t &off, int16_t val) {
return encodeIntFixedLength<int16_t>(dest, sz, off, val);
}
bool encodeInt32FixedLength(char *dest, int64_t sz, int64_t &off, int32_t val) {
return encodeIntFixedLength<int32_t>(dest, sz, off, val);
}
bool encodeInt64FixedLength(char *dest, int64_t sz, int64_t &off, int64_t val) {
return encodeIntFixedLength<int64_t>(dest, sz, off, val);
}
bool encodeString(char *dest, int64_t sz, int64_t &off, const string &str) {
if (!encodeVarU64(dest, sz, off, str.length())) {
return false;
}
const int64_t strLen = str.length();
if ((off + strLen) > sz) {
WLOG(ERROR) << "Not enough room to encode \"" << str << "\" in buf of size "
<< sz;
return false;
}
memcpy(dest + off, str.data(), strLen);
off += strLen;
return true;
}
bool decodeString(ByteRange &br, string &str) {
uint64_t strLen;
if (!decodeUInt64(br, strLen)) {
return false;
}
if (strLen > br.size()) {
WLOG(ERROR) << "Not enough room with " << br.size() << " to decode "
<< strLen;
return false;
}
str.assign((const char *)(br.start()), strLen);
br.advance(strLen);
return true;
}
}
}