forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializationUtil.cpp
51 lines (46 loc) · 1.41 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
/**
* 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 "SerializationUtil.h"
#include <folly/Varint.h>
namespace facebook {
namespace wdt {
void encodeInt(char *dest, int64_t &off, int64_t value) {
off += folly::encodeVarint(value, (uint8_t *)dest + off);
}
int64_t decodeInt(folly::ByteRange &br) {
return folly::decodeVarint(br);
}
void encodeString(char *dest, int64_t &off, const std::string &str) {
int64_t strLen = str.length();
off += folly::encodeVarint(strLen, (uint8_t *)dest + off);
memcpy(dest + off, str.data(), strLen);
off += strLen;
}
bool decodeString(folly::ByteRange &br, char *src, int64_t max,
std::string &str) {
int64_t strLen = folly::decodeVarint(br);
int64_t off = br.start() - (uint8_t *)src;
if (off + strLen > max) {
LOG(ERROR) << "Not enough room with " << max << " to decode " << strLen
<< " at " << off;
return false;
}
str.assign((const char *)(br.start()), strLen);
br.advance(strLen);
return true;
}
bool checkForOverflow(int64_t off, int64_t max) {
if (off > max) {
LOG(ERROR) << "Read past the end:" << off << " " << max;
return true;
}
return false;
}
}
}