-
Notifications
You must be signed in to change notification settings - Fork 1
/
internal.cc
164 lines (149 loc) · 3.5 KB
/
internal.cc
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
#include "./internal.h"
#include <algorithm>
#include <cerrno>
#include <cstring>
#include <memory>
#include <sstream>
#include "./portable_endian.h"
namespace grail {
namespace recordio {
namespace internal {
std::string MagicDebugString(const Magic& m) {
std::ostringstream s;
int n = 0;
s << "[";
for (auto b : m) {
char buf[128];
snprintf(buf, sizeof(buf), "%x", b);
if (n++ > 0) s << ",";
s << buf;
}
s << "]";
return s.str();
}
Error ReadFull(ReadSeeker* in, uint8_t* data, int bytes) {
ssize_t n;
Error err = in->Read(data, bytes, &n);
if (err != "") return err;
if (n != bytes) {
std::ostringstream msg;
msg << "Failed to read " << bytes << " bytes from stream, read " << n
<< " bytes instead: " << std::strerror(errno);
return msg.str();
}
return "";
}
std::string StrError(const std::string& prefix) {
std::ostringstream msg;
msg << prefix << ": " << std::strerror(errno);
return msg.str();
}
Error AbsSeek(ReadSeeker* in, off_t off) {
off_t new_off;
Error err = in->Seek(off, SEEK_SET, &new_off);
if (err != "") return err;
if (new_off != off) {
std::ostringstream msg;
msg << "failed to seek to offset " << off << ", " << SEEK_SET << ": (got "
<< new_off << "): " << std::strerror(errno);
return msg.str();
}
return "";
}
uint64_t BinaryParser::ReadLEUint64() {
uint64_t v;
if (bytes_ < static_cast<int>(sizeof v)) {
err_->Set("Failed to read uint64");
return 0;
}
memcpy(&v, data_, sizeof v);
v = le64toh(v);
data_ += sizeof v;
bytes_ -= sizeof v;
return v;
}
uint32_t BinaryParser::ReadLEUint32() {
uint32_t v;
if (bytes_ < static_cast<int>(sizeof v)) {
err_->Set("Failed to read uint32");
return 0;
}
memcpy(&v, data_, sizeof v);
v = le32toh(v);
data_ += sizeof v;
bytes_ -= sizeof v;
return v;
}
uint64_t BinaryParser::ReadUVarint() {
uint64_t v = 0;
int shift = 0;
int i = 0;
while (bytes_ > 0) {
auto b = *data_;
if (b < 0x80) {
if (i > 9 || (i == 9 && b > 1)) {
err_->Set("Failed to read uvarint");
return 0;
}
--bytes_;
++data_;
v |= (static_cast<uint64_t>(b) << shift);
return v;
}
v |= static_cast<uint64_t>(b & 0x7f) << shift;
shift += 7;
--bytes_;
++data_;
++i;
}
return v;
}
int64_t BinaryParser::ReadVarint() {
uint64_t u;
u = ReadUVarint();
uint64_t x = u >> 1;
if ((u & 1) != 0) {
x = ~x;
}
int64_t v;
memcpy(&v, &x, sizeof(x));
return v;
}
const uint8_t* BinaryParser::ReadBytes(int bytes) {
if (bytes_ < bytes) {
std::ostringstream msg;
msg << "ReadBytes: failed to read " << bytes << " bytes";
err_->Set(msg.str());
return nullptr;
}
const uint8_t* p = data_;
data_ += bytes;
bytes_ -= bytes;
return p;
}
std::string BinaryParser::ReadString(int bytes) {
std::string s;
const uint8_t* v = ReadBytes(bytes);
if (v == nullptr) return s;
s.assign(reinterpret_cast<const char*>(v), bytes);
return s;
}
std::vector<uint8_t> IoVecFlatten(IoVec iov) {
std::vector<uint8_t> buf;
buf.resize(IoVecSize(iov));
size_t n = 0;
for (size_t i = 0; i < iov.size(); i++) {
std::copy(iov[i].begin(), iov[i].end(), buf.data() + n);
n += iov[i].size();
}
return buf;
}
bool HasSuffix(const std::string& str, const std::string& suffix) {
if (str.size() < suffix.size()) {
return false;
}
return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
}
} // namespace internal
} // namespace recordio
} // namespace grail