Debian Codegraph @

monero/contrib/epee/include/storages/portable_storage_val_converters.h

raw symbols SCIP
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26
27
28
29 #pragma once
30
31 #include <boost/regex.hpp>
32
33 #include "misc_language.h"
34 #include "portable_storage_base.h"
35 #include "parserse_base_utils.h"
36 #include "warnings.h"
37 #include "misc_log_ex.h"
38
39 #include <boost/lexical_cast.hpp>
40 #include <boost/numeric/conversion/bounds.hpp>
41 #include <typeinfo>
42 #include <iomanip>
43
44 namespace epee
45 {
46 namespace serialization
47 {
48 #define ASSERT_AND_THROW_WRONG_CONVERSION() ASSERT_MES_AND_THROW("WRONG DATA CONVERSION: from type=" << typeid(from).name() << " to type " << typeid(to).name())
49
50 template<typename from_type, typename to_type>
51 void convert_int_to_uint(const from_type& from, to_type& to)
52 {
53 PUSH_WARNINGS
54 DISABLE_VS_WARNINGS(4018)
55 CHECK_AND_ASSERT_THROW_MES(from >=0, "unexpected int value with signed storage value less than 0, and unsigned receiver value");
56 DISABLE_GCC_AND_CLANG_WARNING(sign-compare)
57 CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits<to_type>::max(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits<to_type>::max());
58 to = static_cast<to_type>(from);
59 POP_WARNINGS
60 }
61 template<typename from_type, typename to_type>
62 void convert_int_to_int(const from_type& from, to_type& to)
63 {
64 CHECK_AND_ASSERT_THROW_MES(from >= boost::numeric::bounds<to_type>::lowest(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with lowest possible value = " << boost::numeric::bounds<to_type>::lowest());
65 PUSH_WARNINGS
66 DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare)
67 CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits<to_type>::max(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits<to_type>::max());
68 POP_WARNINGS
69 to = static_cast<to_type>(from);
70 }
71 template<typename from_type, typename to_type>
72 void convert_uint_to_any_int(const from_type& from, to_type& to)
73 {
74 PUSH_WARNINGS
75 DISABLE_VS_WARNINGS(4018)
76 DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare)
77 CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits<to_type>::max(), "uint value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits<to_type>::max());
78 to = static_cast<to_type>(from);
79 POP_WARNINGS
80 }
81
82 template<typename from_type, typename to_type, bool, bool> //is from signed, is from to signed
83 struct convert_to_signed_unsigned;
84
85 template<typename from_type, typename to_type>
86 struct convert_to_signed_unsigned<from_type, to_type, true, true>
87 {
88 static void convert(const from_type& from, to_type& to)
89 {//from signed to signed
90 convert_int_to_int(from, to);
91 }
92 };
93
94 template<typename from_type, typename to_type>
95 struct convert_to_signed_unsigned<from_type, to_type, true, false>
96 {
97 static void convert(const from_type& from, to_type& to)
98 {//from signed to unsigned
99 convert_int_to_uint(from, to);
100 }
101 };
102
103 template<typename from_type, typename to_type>
104 struct convert_to_signed_unsigned<from_type, to_type, false, true>
105 {
106 static void convert(const from_type& from, to_type& to)
107 {//from unsigned to signed
108 convert_uint_to_any_int(from, to);
109 }
110 };
111
112 template<typename from_type, typename to_type>
113 struct convert_to_signed_unsigned<from_type, to_type, false, false>
114 {
115 static void convert(const from_type& from, to_type& to)
116 {
117 //from unsigned to unsigned
118 convert_uint_to_any_int(from, to);
119 }
120 };
121
122 template<typename from_type, typename to_type, bool>
123 struct convert_to_integral;
124
125 template<typename from_type, typename to_type>
126 struct convert_to_integral<from_type, to_type, true>
127 {
128 static void convert(const from_type& from, to_type& to)
129 {
130 convert_to_signed_unsigned<from_type, to_type, std::is_signed<from_type>::value, std::is_signed<to_type>::value>::convert(from, to);
131 }
132 };
133
134 template<typename from_type, typename to_type>
135 struct convert_to_integral<from_type, to_type, false>
136 {
137 static void convert(const from_type& from, to_type& to)
138 {
139 ASSERT_AND_THROW_WRONG_CONVERSION();
140 }
141 };
142
143 // For MyMonero/OpenMonero backend compatibility
144 // MyMonero backend sends amount, fees and timestamp values as strings.
145 // Until MM backend is updated, this is needed for compatibility between OpenMonero and MyMonero.
146 template<>
147 struct convert_to_integral<std::string, uint64_t, false>
148 {
149 static void convert(const std::string& from, uint64_t& to)
150 {
151 MTRACE("Converting std::string to uint64_t. Source: " << from);
152 // String only contains digits
153 if(std::all_of(from.begin(), from.end(), epee::misc_utils::parse::isdigit))
154 to = boost::lexical_cast<uint64_t>(from);
155 // MyMonero ISO 8061 timestamp (2017-05-06T16:27:06Z)
156 else if (boost::regex_match (from, boost::regex("\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\dZ")))
157 {
158 // Convert to unix timestamp
159 #ifdef HAVE_STRPTIME
160 struct tm tm;
161 if (strptime(from.c_str(), "%Y-%m-%dT%H:%M:%S", &tm))
162 #else
163 std::tm tm = {};
164 std::istringstream ss(from);
165 if (ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S"))
166 #endif
167 to = std::mktime(&tm);
168 } else
169 ASSERT_AND_THROW_WRONG_CONVERSION();
170 }
171 };
172
173 template<class from_type, class to_type>
174 struct is_convertable: std::integral_constant<bool,
175 std::is_integral<to_type>::value &&
176 std::is_integral<from_type>::value &&
177 !std::is_same<from_type, bool>::value &&
178 !std::is_same<to_type, bool>::value > {};
179
180 template<typename from_type, typename to_type, bool>
181 struct convert_to_same;
182
183 template<typename from_type, typename to_type>
184 struct convert_to_same<from_type, to_type, true>
185 {
186 static void convert(const from_type& from, to_type& to)
187 {
188 to = from;
189 }
190 };
191
192 template<typename from_type, typename to_type>
193 struct convert_to_same<from_type, to_type, false>
194 {
195 static void convert(const from_type& from, to_type& to)
196 {
197 convert_to_integral<from_type, to_type, is_convertable<from_type, to_type>::value>::convert(from, to);// ASSERT_AND_THROW_WRONG_CONVERSION();
198 }
199 };
200
201
202 template<class from_type, class to_type>
203 void convert_t(const from_type& from, to_type& to)
204 {
205 convert_to_same<from_type, to_type, std::is_same<to_type, from_type>::value>::convert(from, to);
206 }
207 }
208 }