-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move checked integer arithmetic to int_utils.h
- Loading branch information
Showing
7 changed files
with
86 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* (C) 2024 Jack Lloyd | ||
* | ||
* Botan is released under the Simplified BSD License (see license.txt) | ||
*/ | ||
|
||
#ifndef BOTAN_INT_UTILS_H_ | ||
#define BOTAN_INT_UTILS_H_ | ||
|
||
#include <botan/concepts.h> | ||
#include <botan/types.h> | ||
#include <optional> | ||
|
||
namespace Botan { | ||
|
||
template <std::unsigned_integral T> | ||
constexpr inline std::optional<T> checked_add(T a, T b) { | ||
const T r = a + b; | ||
if(r - a != b) { | ||
return {}; | ||
} | ||
return r; | ||
} | ||
|
||
template <std::unsigned_integral T, std::unsigned_integral... Ts> | ||
requires all_same_v<T, Ts...> | ||
constexpr inline std::optional<T> checked_add(T a, T b, Ts... rest) { | ||
if(auto r = checked_add(a, b)) { | ||
return checked_add(r.value(), rest...); | ||
} else { | ||
return {}; | ||
} | ||
} | ||
|
||
template <std::unsigned_integral T> | ||
constexpr inline std::optional<T> checked_mul(T a, T b) { | ||
const T r = a * b; | ||
// If a == 0 then the multiply certainly did not overflow | ||
// Otherwise r / a == b unless overflow occured | ||
if(a != 0 && r / a != b) { | ||
return {}; | ||
} | ||
return r; | ||
} | ||
|
||
template <std::unsigned_integral T, std::unsigned_integral RT> | ||
std::optional<RT> checked_cast(T input) { | ||
const RT cast = static_cast<RT>(input); | ||
if(static_cast<T>(cast) != input) { | ||
return {}; | ||
} | ||
return cast; | ||
} | ||
|
||
} // namespace Botan | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.