-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy patheofparse.cpp
82 lines (72 loc) · 2.28 KB
/
eofparse.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
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2023 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#include <evmc/evmc.hpp>
#include <evmone/eof.hpp>
#include <iostream>
#include <string>
namespace
{
inline constexpr bool isalnum(char ch) noexcept
{
return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
}
template <typename BaseIterator>
struct skip_nonalnum_iterator : evmc::filter_iterator<BaseIterator, isalnum>
{
using evmc::filter_iterator<BaseIterator, isalnum>::filter_iterator;
};
template <typename BaseIterator>
skip_nonalnum_iterator(BaseIterator, BaseIterator) -> skip_nonalnum_iterator<BaseIterator>;
template <typename InputIterator>
std::optional<evmc::bytes> from_hex_skip_nonalnum(InputIterator begin, InputIterator end) noexcept
{
evmc::bytes bs;
if (!from_hex(skip_nonalnum_iterator{begin, end}, skip_nonalnum_iterator{end, end},
std::back_inserter(bs)))
return {};
return bs;
}
} // namespace
int main()
{
try
{
int num_errors = 0;
for (std::string line; std::getline(std::cin, line);)
{
if (line.empty() || line.starts_with('#'))
continue;
auto o = from_hex_skip_nonalnum(line.begin(), line.end());
if (!o)
{
std::cout << "err: invalid hex\n";
++num_errors;
continue;
}
const auto& eof = *o;
const auto err = evmone::validate_eof(EVMC_PRAGUE, evmone::ContainerKind::runtime, eof);
if (err != evmone::EOFValidationError::success)
{
std::cout << "err: " << evmone::get_error_message(err) << "\n";
++num_errors;
continue;
}
const auto header = evmone::read_valid_eof1_header(eof);
std::cout << "OK ";
for (size_t i = 0; i < header.code_sizes.size(); ++i)
{
if (i != 0)
std::cout << ",";
std::cout << evmc::hex(header.get_code(eof, i));
}
std::cout << "\n";
}
return num_errors;
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << "\n";
return -1;
}
}