Monero
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
io.h
Go to the documentation of this file.
1
// Copyright (c) 2014-2022, The Monero Project
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification, are
6
// permitted provided that the following conditions are met:
7
//
8
// 1. Redistributions of source code must retain the above copyright notice, this list of
9
// conditions and the following disclaimer.
10
//
11
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12
// of conditions and the following disclaimer in the documentation and/or other
13
// materials provided with the distribution.
14
//
15
// 3. Neither the name of the copyright holder nor the names of its contributors may be
16
// used to endorse or promote products derived from this software without specific
17
// prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
//
29
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31
#include <cstddef>
32
#include <ios>
33
#include <iostream>
34
#include <type_traits>
35
#include <vector>
36
37
inline
bool
hexdecode
(
const
char
*from, std::size_t length,
void
*to) {
38
std::size_t i;
39
for
(i = 0; i < length; i++) {
40
int
v = 0;
41
if
(from[2 * i] >=
'0'
&& from[2 * i] <=
'9'
) {
42
v = from[2 * i] -
'0'
;
43
}
else
if
(from[2 * i] >=
'a'
&& from[2 * i] <=
'f'
) {
44
v = from[2 * i] -
'a'
+ 10;
45
}
else
{
46
return
false
;
47
}
48
v <<= 4;
49
if
(from[2 * i + 1] >=
'0'
&& from[2 * i + 1] <=
'9'
) {
50
v |= from[2 * i + 1] -
'0'
;
51
}
else
if
(from[2 * i + 1] >=
'a'
&& from[2 * i + 1] <=
'f'
) {
52
v |= from[2 * i + 1] -
'a'
+ 10;
53
}
else
{
54
return
false
;
55
}
56
*(
reinterpret_cast<
unsigned
char
*
>
(to) + i) = v;
57
}
58
return
true
;
59
}
60
61
inline
void
get
(std::istream &input,
bool
&
res
) {
62
std::string sres;
63
input >> sres;
64
if
(sres ==
"false"
) {
65
res
=
false
;
66
}
else
if
(sres ==
"true"
) {
67
res
=
true
;
68
}
else
{
69
input.setstate(std::ios_base::failbit);
70
}
71
}
72
73
template
<
typename
T>
74
typename
std::enable_if<std::is_integral<T>::value,
void
>::type
75
get
(std::istream &input,
T
&
res
) {
76
input >>
res
;
77
}
78
79
inline
void
getvar
(std::istream &input, std::size_t length,
void
*
res
) {
80
std::string sres;
81
input >> sres;
82
if
(sres.length() != 2 * length || !
hexdecode
(sres.data(), length,
res
)) {
83
input.setstate(std::ios_base::failbit);
84
}
85
}
86
87
template
<
typename
T>
88
typename
std::enable_if<std::is_standard_layout<T>::value && !std::is_scalar<T>::value,
void
>::type
89
get
(std::istream &input,
T
&
res
) {
90
getvar
(input,
sizeof
(
T
), &
res
);
91
}
92
93
inline
void
get
(std::istream &input, std::vector<char> &
res
) {
94
std::string sres;
95
input >> sres;
96
if
(sres ==
"x"
) {
97
res
.clear();
98
}
else
if
(sres.length() % 2 != 0) {
99
input.setstate(std::ios_base::failbit);
100
}
else
{
101
std::size_t length = sres.length() / 2;
102
res
.resize(length);
103
if
(!
hexdecode
(sres.data(), length,
res
.data())) {
104
input.setstate(std::ios_base::failbit);
105
}
106
}
107
}
108
109
#if !defined(_MSC_VER) || _MSC_VER >= 1800
110
111
template
<
typename
T
,
typename
... TT>
112
typename
std::enable_if<(
sizeof
...(TT) > 0),
void
>::type
113
get
(std::istream &input,
T
&
res
, TT &... resres) {
114
get
(input,
res
);
115
get
(input, resres...);
116
}
117
118
#else
119
#include <boost/preprocessor/cat.hpp>
120
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
121
#include <boost/preprocessor/repetition/enum_params.hpp>
122
#include <boost/preprocessor/repetition/repeat.hpp>
123
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
124
125
#define NESTED_GET(z, n, data) get(input, BOOST_PP_CAT(res, n));
126
#define GET(z, n, data) \
127
template<BOOST_PP_ENUM_PARAMS(n, typename T)> \
128
void get(std::istream &input, BOOST_PP_ENUM_BINARY_PARAMS(n, T, &res)) { \
129
BOOST_PP_REPEAT(n, NESTED_GET, ~) \
130
}
131
BOOST_PP_REPEAT_FROM_TO(2, 5, GET, ~)
132
133
#endif
res
const char * res
Definition
hmac_keccak.cpp:42
getvar
void getvar(std::istream &input, std::size_t length, void *res)
Definition
io.h:79
hexdecode
bool hexdecode(const char *from, std::size_t length, void *to)
Definition
io.h:37
get
void get(std::istream &input, bool &res)
Definition
io.h:61
T
#define T(x)
Generated on
for Monero by
1.17.0