-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBinaryMetadataExtractor.cpp
99 lines (80 loc) · 2.68 KB
/
BinaryMetadataExtractor.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
#include "BinaryMetadataExtractor.h"
#include <boost/filesystem/path.hpp>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/coded_stream.h>
#include <fstream>
namespace
{
constexpr int kMaxVarintBytes = 10;
uint64_t DecodeVarint64(char* buffer, size_t* count)
{
uint8_t* buffer_ = reinterpret_cast<uint8_t*>(buffer);
uint64_t result = 0;
uint32_t b;
do
{
if (*count == kMaxVarintBytes)
break;
b = *buffer_;
result |= static_cast<uint64_t>(b & 0x7F) << (7 * *count);
++buffer_;
++* count;
}
while (b & 0x80);
return result;
}
class BinaryMetadata : public MetadataExtractor::Metadata
{
public:
explicit BinaryMetadata(std::string id, uint8_t const* data, int32_t length) : Metadata(std::move(id)), Data(data), Length(length)
{
}
std::shared_ptr<google::protobuf::io::CodedInputStream> CreateCodedInputStream() const override
{
return std::make_shared<google::protobuf::io::CodedInputStream>(Data, Length);
}
private:
uint8_t const* Data;
int32_t Length;
};
}
void BinaryMetadataExtractor::Parse(boost::filesystem::path const& binaryPath)
{
ReadFile(binaryPath);
FindMetadata();
}
void BinaryMetadataExtractor::ReadFile(boost::filesystem::path const& binaryPath)
{
std::ifstream ifs(binaryPath.string(), std::ifstream::binary);
if (!ifs)
throw std::runtime_error("could not open " + binaryPath.string());
ifs >> std::noskipws;
ifs.seekg(0, std::ios_base::end);
_binary.reserve(ifs.tellg());
ifs.seekg(0, std::ios_base::beg);
std::copy(std::istream_iterator<char>(ifs), std::istream_iterator<char>(), std::back_inserter(_binary));
}
void BinaryMetadataExtractor::FindMetadata()
{
size_t itr = 0;
while (itr < _binary.size())
{
itr = _binary.find(".proto", itr);
if (itr == std::string::npos)
break;
itr += 6;
size_t start = _binary.rfind('\xA', itr);
if (start == std::string::npos || _binary[start - 1] != '\0')
continue;
size_t decodedBytes = 0;
uint64_t length = DecodeVarint64(&_binary[start + 1], &decodedBytes);
size_t end = start + 1 + decodedBytes;
if (itr - end != length)
continue;
google::protobuf::io::CodedInputStream stream(reinterpret_cast<uint8_t*>(&_binary[start]), _binary.size() - start);
google::protobuf::FileDescriptorProto descriptor;
if (!descriptor.MergeFromCodedStream(&stream))
continue;
_metadatas.emplace_back(new BinaryMetadata(std::to_string(_metadatas.size()), reinterpret_cast<uint8_t*>(&_binary[start]), stream.CurrentPosition() - 1));
}
}