1 /*
2 	This file is part of solidity.
3 
4 	solidity is free software: you can redistribute it and/or modify
5 	it under the terms of the GNU General Public License as published by
6 	the Free Software Foundation, either version 3 of the License, or
7 	(at your option) any later version.
8 
9 	solidity is distributed in the hope that it will be useful,
10 	but WITHOUT ANY WARRANTY; without even the implied warranty of
11 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 	GNU General Public License for more details.
13 
14 	You should have received a copy of the GNU General Public License
15 	along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 // SPDX-License-Identifier: GPL-3.0
18 /** @file CommonData.h
19  * @author Gav Wood <i@gavwood.com>
20  * @date 2014
21  *
22  * Shared algorithms and data types.
23  */
24 
25 #pragma once
26 
27 #include <iterator>
28 #include <libsolutil/Common.h>
29 
30 #include <vector>
31 #include <type_traits>
32 #include <cstring>
33 #include <optional>
34 #include <string>
35 #include <set>
36 #include <functional>
37 #include <utility>
38 #include <type_traits>
39 #include <list>
40 #include <algorithm>
41 
42 /// Operators need to stay in the global namespace.
43 
44 /// Concatenate the contents of a container onto a vector
45 template <class T, class U> std::vector<T>& operator+=(std::vector<T>& _a, U& _b)
46 {
47 	for (auto const& i: _b)
48 		_a.push_back(T(i));
49 	return _a;
50 }
51 /// Concatenate the contents of a container onto a vector, move variant.
52 template <class T, class U> std::vector<T>& operator+=(std::vector<T>& _a, U&& _b)
53 {
54 	std::move(_b.begin(), _b.end(), std::back_inserter(_a));
55 	return _a;
56 }
57 
58 /// Concatenate the contents of a container onto a list
59 template <class T, class U> std::list<T>& operator+=(std::list<T>& _a, U& _b)
60 {
61 	for (auto const& i: _b)
62 		_a.push_back(T(i));
63 	return _a;
64 }
65 /// Concatenate the contents of a container onto a list, move variant.
66 template <class T, class U> std::list<T>& operator+=(std::list<T>& _a, U&& _b)
67 {
68 	std::move(_b.begin(), _b.end(), std::back_inserter(_a));
69 	return _a;
70 }
71 
72 /// Concatenate the contents of a container onto a multiset
73 template <class U, class... T> std::multiset<T...>& operator+=(std::multiset<T...>& _a, U& _b)
74 {
75 	_a.insert(_b.begin(), _b.end());
76 	return _a;
77 }
78 /// Concatenate the contents of a container onto a multiset, move variant.
79 template <class U, class... T> std::multiset<T...>& operator+=(std::multiset<T...>& _a, U&& _b)
80 {
81 	for (auto&& x: _b)
82 		_a.insert(std::move(x));
83 	return _a;
84 }
85 /// Concatenate the contents of a container onto a set
86 template <class U, class... T> std::set<T...>& operator+=(std::set<T...>& _a, U& _b)
87 {
88 	_a.insert(_b.begin(), _b.end());
89 	return _a;
90 }
91 /// Concatenate the contents of a container onto a set, move variant.
92 template <class U, class... T> std::set<T...>& operator+=(std::set<T...>& _a, U&& _b)
93 {
94 	for (auto&& x: _b)
95 		_a.insert(std::move(x));
96 	return _a;
97 }
98 
99 /// Concatenate two vectors of elements.
100 template <class T>
101 inline std::vector<T> operator+(std::vector<T> const& _a, std::vector<T> const& _b)
102 {
103 	std::vector<T> ret(_a);
104 	ret += _b;
105 	return ret;
106 }
107 
108 /// Concatenate two vectors of elements, moving them.
109 template <class T>
110 inline std::vector<T> operator+(std::vector<T>&& _a, std::vector<T>&& _b)
111 {
112 	std::vector<T> ret(std::move(_a));
113 	assert(&_a != &_b);
114 	ret += std::move(_b);
115 	return ret;
116 }
117 
118 /// Concatenate something to a sets of elements.
119 template <class U, class... T>
120 inline std::set<T...> operator+(std::set<T...> const& _a, U&& _b)
121 {
122 	std::set<T...> ret(_a);
123 	ret += std::forward<U>(_b);
124 	return ret;
125 }
126 
127 /// Concatenate something to a sets of elements, move variant.
128 template <class U, class... T>
129 inline std::set<T...> operator+(std::set<T...>&& _a, U&& _b)
130 {
131 	std::set<T...> ret(std::move(_a));
132 	ret += std::forward<U>(_b);
133 	return ret;
134 }
135 
136 /// Remove the elements of a container from a set.
137 template <class C, class... T>
138 inline std::set<T...>& operator-=(std::set<T...>& _a, C const& _b)
139 {
140 	for (auto const& x: _b)
141 		_a.erase(x);
142 	return _a;
143 }
144 
145 template <class C, class... T>
146 inline std::set<T...> operator-(std::set<T...> const& _a, C const& _b)
147 {
148 	auto result = _a;
149 	result -= _b;
150 	return result;
151 }
152 
153 /// Remove the elements of a container from a multiset.
154 template <class C, class... T>
155 inline std::multiset<T...>& operator-=(std::multiset<T...>& _a, C const& _b)
156 {
157 	for (auto const& x: _b)
158 		_a.erase(x);
159 	return _a;
160 }
161 
162 namespace solidity::util
163 {
164 
165 /// Functional map.
166 /// Returns a container _oc applying @param _op to each element in @param _c.
167 /// By default _oc is a vector.
168 /// If another return type is desired, an empty contained of that type
169 /// is given as @param _oc.
170 template<class Container, class Callable, class OutputContainer =
171 	std::vector<std::invoke_result_t<
172 		Callable,
173 		decltype(*std::begin(std::declval<Container>()))
174 >>>
175 auto applyMap(Container const& _c, Callable&& _op, OutputContainer _oc = OutputContainer{})
176 {
177 	std::transform(std::begin(_c), std::end(_c), std::inserter(_oc, std::end(_oc)), _op);
178 	return _oc;
179 }
180 
181 /// Filter a vector.
182 /// Returns a copy of the vector after only taking indices `i` such that `_mask[i]` is true.
183 template<typename T>
filter(std::vector<T> const & _vec,std::vector<bool> const & _mask)184 std::vector<T> filter(std::vector<T> const& _vec, std::vector<bool> const& _mask)
185 {
186 	assert(_vec.size() == _mask.size());
187 
188 	std::vector<T> ret;
189 
190 	for (size_t i = 0; i < _mask.size(); ++i)
191 		if (_mask[i])
192 			ret.push_back(_vec[i]);
193 
194 	return ret;
195 }
196 
197 /// Functional fold.
198 /// Given a container @param _c, an initial value @param _acc,
199 /// and a binary operator @param _binaryOp(T, U), accumulate
200 /// the elements of _c over _acc.
201 /// Note that <numeric> has a similar function `accumulate` which
202 /// until C++20 does *not* std::move the partial accumulated.
203 template<class C, class T, class Callable>
fold(C const & _c,T _acc,Callable && _binaryOp)204 auto fold(C const& _c, T _acc, Callable&& _binaryOp)
205 {
206 	for (auto const& e: _c)
207 		_acc = _binaryOp(std::move(_acc), e);
208 	return _acc;
209 }
210 
211 template <class T, class U>
convertContainer(U const & _from)212 T convertContainer(U const& _from)
213 {
214 	return T{_from.cbegin(), _from.cend()};
215 }
216 
217 template <class T, class U>
convertContainer(U && _from)218 T convertContainer(U&& _from)
219 {
220 	return T{
221 		std::make_move_iterator(_from.begin()),
222 		std::make_move_iterator(_from.end())
223 	};
224 }
225 
226 /// Gets a @a K -> @a V map and returns a map where values from the original map are keys and keys
227 /// from the original map are values.
228 ///
229 /// @pre @a originalMap must have unique values.
230 template <typename K, typename V>
invertMap(std::map<K,V> const & originalMap)231 std::map<V, K> invertMap(std::map<K, V> const& originalMap)
232 {
233 	std::map<V, K> inverseMap;
234 	for (auto const& originalPair: originalMap)
235 	{
236 		assert(inverseMap.count(originalPair.second) == 0);
237 		inverseMap.insert({originalPair.second, originalPair.first});
238 	}
239 
240 	return inverseMap;
241 }
242 
243 /// Returns a set of keys of a map.
244 template <typename K, typename V>
keys(std::map<K,V> const & _map)245 std::set<K> keys(std::map<K, V> const& _map)
246 {
247 	return applyMap(_map, [](auto const& _elem) { return _elem.first; }, std::set<K>{});
248 }
249 
250 /// @returns a pointer to the entry of @a _map at @a _key, if there is one, and nullptr otherwise.
251 template<typename MapType, typename KeyType>
decltype(auto)252 decltype(auto) valueOrNullptr(MapType&& _map, KeyType const& _key)
253 {
254 	auto it = _map.find(_key);
255 	return (it == _map.end()) ? nullptr : &it->second;
256 }
257 
258 namespace detail
259 {
260 struct allow_copy {};
261 }
262 static constexpr auto allow_copy = detail::allow_copy{};
263 
264 /// @returns a reference to the entry of @a _map at @a _key, if there is one, and @a _defaultValue otherwise.
265 /// Makes sure no copy is involved, unless allow_copy is passed as fourth argument.
266 template<
267 	typename MapType,
268 	typename KeyType,
269 	typename ValueType = std::decay_t<decltype(std::declval<MapType>().find(std::declval<KeyType>())->second)> const&,
270 	typename AllowCopyType = void*
271 >
decltype(auto)272 decltype(auto) valueOrDefault(MapType&& _map, KeyType const& _key, ValueType&& _defaultValue = {}, AllowCopyType = nullptr)
273 {
274 	auto it = _map.find(_key);
275 	static_assert(
276 		std::is_same_v<AllowCopyType, detail::allow_copy> ||
277 		std::is_reference_v<decltype((it == _map.end()) ? _defaultValue : it->second)>,
278 		"valueOrDefault does not allow copies by default. Pass allow_copy as additional argument, if you want to allow copies."
279 	);
280 	return (it == _map.end()) ? _defaultValue : it->second;
281 }
282 
283 namespace detail
284 {
285 template<typename Callable>
286 struct MapTuple
287 {
288 	Callable callable;
289 	template<typename TupleType>
decltypeMapTuple290 	decltype(auto) operator()(TupleType&& _tuple) {
291 		using PlainTupleType = std::remove_cv_t<std::remove_reference_t<TupleType>>;
292 		return operator()(
293 			std::forward<TupleType>(_tuple),
294 			std::make_index_sequence<std::tuple_size_v<PlainTupleType>>{}
295 		);
296 	}
297 private:
298 	template<typename TupleType, size_t... I>
decltypeMapTuple299 	decltype(auto) operator()(TupleType&& _tuple, std::index_sequence<I...>)
300 	{
301 		return callable(std::get<I>(std::forward<TupleType>(_tuple))...);
302 	}
303 };
304 }
305 
306 /// Wraps @a _callable, which takes multiple arguments, into a callable that takes a single tuple of arguments.
307 /// Since structured binding in lambdas is not allowed, i.e. [](auto&& [key, value]) { ... } is invalid, this allows
308 /// to instead use mapTuple([](auto&& key, auto&& value) { ... }).
309 template<typename Callable>
decltype(auto)310 decltype(auto) mapTuple(Callable&& _callable)
311 {
312 	return detail::MapTuple<Callable>{std::forward<Callable>(_callable)};
313 }
314 
315 /// Merges map @a _b into map @a _a. If the same key exists in both maps,
316 /// calls @a _conflictSolver to combine the two values.
317 template <class K, class V, class F>
joinMap(std::map<K,V> & _a,std::map<K,V> && _b,F _conflictSolver)318 void joinMap(std::map<K, V>& _a, std::map<K, V>&& _b, F _conflictSolver)
319 {
320 	auto ita = _a.begin();
321 	auto aend = _a.end();
322 	auto itb = _b.begin();
323 	auto bend = _b.end();
324 
325 	for (; itb != bend; ++ita)
326 	{
327 		if (ita == aend)
328 			ita = _a.insert(ita, std::move(*itb++));
329 		else if (ita->first < itb->first)
330 			continue;
331 		else if (itb->first < ita->first)
332 			ita = _a.insert(ita, std::move(*itb++));
333 		else
334 		{
335 			_conflictSolver(ita->second, std::move(itb->second));
336 			++itb;
337 		}
338 	}
339 }
340 
341 namespace detail
342 {
343 
344 template<typename Container, typename Value>
345 auto findOffset(Container&& _container, Value&& _value, int)
346 -> decltype(_container.find(_value) == _container.end(), std::distance(_container.begin(), _container.find(_value)), std::optional<size_t>())
347 {
348 	auto it = _container.find(std::forward<Value>(_value));
349 	auto end = _container.end();
350 	if (it == end)
351 		return std::nullopt;
352 	return std::distance(_container.begin(), it);
353 }
354 template<typename Range, typename Value>
355 auto findOffset(Range&& _range, Value&& _value, void*)
356 -> decltype(std::find(std::begin(_range), std::end(_range), std::forward<Value>(_value)) == std::end(_range), std::optional<size_t>())
357 {
358 	auto begin = std::begin(_range);
359 	auto end = std::end(_range);
360 	auto it = std::find(begin, end, std::forward<Value>(_value));
361 	if (it == end)
362 		return std::nullopt;
363 	return std::distance(begin, it);
364 }
365 
366 }
367 
368 /// @returns an std::optional<size_t> containing the offset of the first element in @a _range that is equal to @a _value,
369 /// if any, or std::nullopt otherwise.
370 /// Uses a linear search (``std::find``) unless @a _range is a container and provides a
371 /// suitable ``.find`` function (e.g. it will use the logarithmic ``.find`` function in ``std::set`` instead).
372 template<typename Range>
373 auto findOffset(Range&& _range, std::remove_reference_t<decltype(*std::cbegin(_range))> const& _value)
374 -> decltype(detail::findOffset(std::forward<Range>(_range), _value, 0))
375 {
376 	return detail::findOffset(std::forward<Range>(_range), _value, 0);
377 }
378 
379 // String conversion functions, mainly to/from hex/nibble/byte representations.
380 
381 enum class WhenError
382 {
383 	DontThrow = 0,
384 	Throw = 1,
385 };
386 
387 enum class HexPrefix
388 {
389 	DontAdd = 0,
390 	Add = 1,
391 };
392 
393 enum class HexCase
394 {
395 	Lower = 0,
396 	Upper = 1,
397 	Mixed = 2,
398 };
399 
400 /// Convert a single byte to a string of hex characters (of length two),
401 /// optionally with uppercase hex letters.
402 std::string toHex(uint8_t _data, HexCase _case = HexCase::Lower);
403 
404 /// Convert a series of bytes to the corresponding string of hex duplets,
405 /// optionally with "0x" prefix and with uppercase hex letters.
406 std::string toHex(bytes const& _data, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower);
407 
408 /// Converts a (printable) ASCII hex character into the corresponding integer value.
409 /// @example fromHex('A') == 10 && fromHex('f') == 15 && fromHex('5') == 5
410 int fromHex(char _i, WhenError _throw);
411 
412 /// Converts a (printable) ASCII hex string into the corresponding byte stream.
413 /// @example fromHex("41626261") == asBytes("Abba")
414 /// If _throw = ThrowType::DontThrow, it replaces bad hex characters with 0's, otherwise it will throw an exception.
415 bytes fromHex(std::string const& _s, WhenError _throw = WhenError::DontThrow);
416 /// Converts byte array to a string containing the same (binary) data. Unless
417 /// the byte array happens to contain ASCII data, this won't be printable.
asString(bytes const & _b)418 inline std::string asString(bytes const& _b)
419 {
420 	return std::string((char const*)_b.data(), (char const*)(_b.data() + _b.size()));
421 }
422 
423 /// Converts byte array ref to a string containing the same (binary) data. Unless
424 /// the byte array happens to contain ASCII data, this won't be printable.
asString(bytesConstRef _b)425 inline std::string asString(bytesConstRef _b)
426 {
427 	return std::string((char const*)_b.data(), (char const*)(_b.data() + _b.size()));
428 }
429 
430 /// Converts a string to a byte array containing the string's (byte) data.
asBytes(std::string const & _b)431 inline bytes asBytes(std::string const& _b)
432 {
433 	return bytes((uint8_t const*)_b.data(), (uint8_t const*)(_b.data() + _b.size()));
434 }
435 
436 template <class T, class V>
contains(T const & _t,V const & _v)437 bool contains(T const& _t, V const& _v)
438 {
439 	return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v);
440 }
441 
442 template <class T, class Predicate>
contains_if(T const & _t,Predicate const & _p)443 bool contains_if(T const& _t, Predicate const& _p)
444 {
445 	return std::end(_t) != std::find_if(std::begin(_t), std::end(_t), _p);
446 }
447 
448 /// Function that iterates over a vector, calling a function on each of its
449 /// elements. If that function returns a vector, the element is replaced by
450 /// the returned vector. During the iteration, the original vector is only valid
451 /// on the current element and after that. The actual replacement takes
452 /// place at the end, but already visited elements might be invalidated.
453 /// If nothing is replaced, no copy is performed.
454 template <typename T, typename F>
iterateReplacing(std::vector<T> & _vector,F const & _f)455 void iterateReplacing(std::vector<T>& _vector, F const& _f)
456 {
457 	// Concept: _f must be Callable, must accept param T&, must return optional<vector<T>>
458 	bool useModified = false;
459 	std::vector<T> modifiedVector;
460 	for (size_t i = 0; i < _vector.size(); ++i)
461 	{
462 		if (std::optional<std::vector<T>> r = _f(_vector[i]))
463 		{
464 			if (!useModified)
465 			{
466 				std::move(_vector.begin(), _vector.begin() + ptrdiff_t(i), back_inserter(modifiedVector));
467 				useModified = true;
468 			}
469 			modifiedVector += std::move(*r);
470 		}
471 		else if (useModified)
472 			modifiedVector.emplace_back(std::move(_vector[i]));
473 	}
474 	if (useModified)
475 		_vector = std::move(modifiedVector);
476 }
477 
478 namespace detail
479 {
480 template <typename T, typename F, std::size_t... I>
iterateReplacingWindow(std::vector<T> & _vector,F const & _f,std::index_sequence<I...>)481 void iterateReplacingWindow(std::vector<T>& _vector, F const& _f, std::index_sequence<I...>)
482 {
483 	// Concept: _f must be Callable, must accept sizeof...(I) parameters of type T&, must return optional<vector<T>>
484 	bool useModified = false;
485 	std::vector<T> modifiedVector;
486 	size_t i = 0;
487 	for (; i + sizeof...(I) <= _vector.size(); ++i)
488 	{
489 		if (std::optional<std::vector<T>> r = _f(_vector[i + I]...))
490 		{
491 			if (!useModified)
492 			{
493 				std::move(_vector.begin(), _vector.begin() + ptrdiff_t(i), back_inserter(modifiedVector));
494 				useModified = true;
495 			}
496 			modifiedVector += std::move(*r);
497 			i += sizeof...(I) - 1;
498 		}
499 		else if (useModified)
500 			modifiedVector.emplace_back(std::move(_vector[i]));
501 	}
502 	if (useModified)
503 	{
504 		for (; i < _vector.size(); ++i)
505 			modifiedVector.emplace_back(std::move(_vector[i]));
506 		_vector = std::move(modifiedVector);
507 	}
508 }
509 
510 }
511 
512 /// Function that iterates over the vector @param _vector,
513 /// calling the function @param _f on sequences of @tparam N of its
514 /// elements. If @param _f returns a vector, these elements are replaced by
515 /// the returned vector and the iteration continues with the next @tparam N elements.
516 /// If the function does not return a vector, the iteration continues with an overlapping
517 /// sequence of @tparam N elements that starts with the second element of the previous
518 /// iteration.
519 /// During the iteration, the original vector is only valid
520 /// on the current element and after that. The actual replacement takes
521 /// place at the end, but already visited elements might be invalidated.
522 /// If nothing is replaced, no copy is performed.
523 template <std::size_t N, typename T, typename F>
iterateReplacingWindow(std::vector<T> & _vector,F const & _f)524 void iterateReplacingWindow(std::vector<T>& _vector, F const& _f)
525 {
526 	// Concept: _f must be Callable, must accept N parameters of type T&, must return optional<vector<T>>
527 	detail::iterateReplacingWindow(_vector, _f, std::make_index_sequence<N>{});
528 }
529 
530 /// @returns true iff @a _str passess the hex address checksum test.
531 /// @param _strict if false, hex strings with only uppercase or only lowercase letters
532 /// are considered valid.
533 bool passesAddressChecksum(std::string const& _str, bool _strict);
534 
535 /// @returns the checksummed version of an address
536 /// @param hex strings that look like an address
537 std::string getChecksummedAddress(std::string const& _addr);
538 
539 bool isValidHex(std::string const& _string);
540 bool isValidDecimal(std::string const& _string);
541 
542 /// @returns a quoted string if all characters are printable ASCII chars,
543 /// or its hex representation otherwise.
544 /// _value cannot be longer than 32 bytes.
545 std::string formatAsStringOrNumber(std::string const& _value);
546 
547 /// @returns a string with the usual backslash-escapes for non-printable and non-ASCII
548 /// characters and surrounded by '"'-characters.
549 std::string escapeAndQuoteString(std::string const& _input);
550 
551 template<typename Container, typename Compare>
containerEqual(Container const & _lhs,Container const & _rhs,Compare && _compare)552 bool containerEqual(Container const& _lhs, Container const& _rhs, Compare&& _compare)
553 {
554 	return std::equal(std::begin(_lhs), std::end(_lhs), std::begin(_rhs), std::end(_rhs), std::forward<Compare>(_compare));
555 }
556 
findAnyOf(std::string const & _haystack,std::vector<std::string> const & _needles)557 inline std::string findAnyOf(std::string const& _haystack, std::vector<std::string> const& _needles)
558 {
559 	for (std::string const& needle: _needles)
560 		if (_haystack.find(needle) != std::string::npos)
561 			return needle;
562 	return "";
563 }
564 
565 
566 namespace detail
567 {
568 template<typename T>
variadicEmplaceBack(std::vector<T> &)569 void variadicEmplaceBack(std::vector<T>&) {}
570 template<typename T, typename A, typename... Args>
variadicEmplaceBack(std::vector<T> & _vector,A && _a,Args &&..._args)571 void variadicEmplaceBack(std::vector<T>& _vector, A&& _a, Args&&... _args)
572 {
573 	_vector.emplace_back(std::forward<A>(_a));
574 	variadicEmplaceBack(_vector, std::forward<Args>(_args)...);
575 }
576 }
577 
578 template<typename T, typename... Args>
make_vector(Args &&..._args)579 std::vector<T> make_vector(Args&&... _args)
580 {
581 	std::vector<T> result;
582 	result.reserve(sizeof...(_args));
583 	detail::variadicEmplaceBack(result, std::forward<Args>(_args)...);
584 	return result;
585 }
586 
587 }
588