diff options
author | Jack Lloyd <[email protected]> | 2018-08-13 08:23:19 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-08-13 08:23:19 -0400 |
commit | 5a2a2d33a9c955691de250b252cb5791158ca2d9 (patch) | |
tree | e17327dc8b444ef77e41c214b5bd1b5e8244277e /src/lib/ffi | |
parent | 43a5eebea5892e04de1e7a3967a175016ae99d94 (diff) | |
parent | 69ba4b0b46db42114f2b98ed41d4ddf4a4392961 (diff) |
Merge GH #1646 Add MPI and FPE to Python wrapper
Diffstat (limited to 'src/lib/ffi')
-rw-r--r-- | src/lib/ffi/ffi.cpp | 49 | ||||
-rw-r--r-- | src/lib/ffi/ffi.h | 1 | ||||
-rw-r--r-- | src/lib/ffi/ffi_util.h | 36 |
3 files changed, 52 insertions, 34 deletions
diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp index 6e683dfdc..7bea1a209 100644 --- a/src/lib/ffi/ffi.cpp +++ b/src/lib/ffi/ffi.cpp @@ -24,6 +24,52 @@ int ffi_error_exception_thrown(const char* func_name, const char* exn, int rc) return rc; } +int ffi_guard_thunk(const char* func_name, std::function<int ()> thunk) + { + try + { + return thunk(); + } + catch(std::bad_alloc&) + { + return ffi_error_exception_thrown(func_name, "bad_alloc", BOTAN_FFI_ERROR_OUT_OF_MEMORY); + } + catch(Botan_FFI::FFI_Error& e) + { + return ffi_error_exception_thrown(func_name, e.what(), e.error_code()); + } + catch(Botan::Lookup_Error& e) + { + return ffi_error_exception_thrown(func_name, e.what(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED); + } + catch(Botan::Invalid_Key_Length& e) + { + return ffi_error_exception_thrown(func_name, e.what(), BOTAN_FFI_ERROR_INVALID_KEY_LENGTH); + } + catch(Botan::Key_Not_Set& e) + { + return ffi_error_exception_thrown(func_name, e.what(), BOTAN_FFI_ERROR_KEY_NOT_SET); + } + catch(Botan::Invalid_Argument& e) + { + return ffi_error_exception_thrown(func_name, e.what(), BOTAN_FFI_ERROR_BAD_PARAMETER); + } + catch(Botan::Not_Implemented& e) + { + return ffi_error_exception_thrown(func_name, e.what(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED); + } + catch(std::exception& e) + { + return ffi_error_exception_thrown(func_name, e.what()); + } + catch(...) + { + return ffi_error_exception_thrown(func_name, "unknown exception"); + } + + return BOTAN_FFI_ERROR_UNKNOWN_ERROR; + } + } extern "C" { @@ -67,6 +113,9 @@ const char* botan_error_description(int err) case BOTAN_FFI_ERROR_KEY_NOT_SET: return "Key not set on object"; + case BOTAN_FFI_ERROR_INVALID_KEY_LENGTH: + return "Invalid key length"; + case BOTAN_FFI_ERROR_NOT_IMPLEMENTED: return "Not implemented"; diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index 96cdf1de4..8ddfcabe2 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -78,6 +78,7 @@ enum BOTAN_FFI_ERROR { BOTAN_FFI_ERROR_NULL_POINTER = -31, BOTAN_FFI_ERROR_BAD_PARAMETER = -32, BOTAN_FFI_ERROR_KEY_NOT_SET = -33, + BOTAN_FFI_ERROR_INVALID_KEY_LENGTH = -34, BOTAN_FFI_ERROR_NOT_IMPLEMENTED = -40, BOTAN_FFI_ERROR_INVALID_OBJECT = -50, diff --git a/src/lib/ffi/ffi_util.h b/src/lib/ffi/ffi_util.h index 623b4bf20..2b9206f0b 100644 --- a/src/lib/ffi/ffi_util.h +++ b/src/lib/ffi/ffi_util.h @@ -10,6 +10,7 @@ #include <cstdint> #include <memory> #include <stdexcept> +#include <functional> #include <botan/exceptn.h> #include <botan/mem_ops.h> @@ -69,40 +70,7 @@ T& safe_get(botan_struct<T,M>* p) throw FFI_Error("Invalid object pointer", BOTAN_FFI_ERROR_INVALID_OBJECT); } -template<typename Thunk> -int ffi_guard_thunk(const char* func_name, Thunk thunk) - { - try - { - return thunk(); - } - catch(std::bad_alloc&) - { - return ffi_error_exception_thrown(func_name, "bad_alloc", BOTAN_FFI_ERROR_OUT_OF_MEMORY); - } - catch(Botan_FFI::FFI_Error& e) - { - return ffi_error_exception_thrown(func_name, e.what(), e.error_code()); - } - catch(Botan::Key_Not_Set& e) - { - return ffi_error_exception_thrown(func_name, e.what(), BOTAN_FFI_ERROR_KEY_NOT_SET); - } - catch(Botan::Invalid_Argument& e) - { - return ffi_error_exception_thrown(func_name, e.what(), BOTAN_FFI_ERROR_BAD_PARAMETER); - } - catch(std::exception& e) - { - return ffi_error_exception_thrown(func_name, e.what()); - } - catch(...) - { - return ffi_error_exception_thrown(func_name, "unknown exception"); - } - - return BOTAN_FFI_ERROR_UNKNOWN_ERROR; - } +int ffi_guard_thunk(const char* func_name, std::function<int ()>); template<typename T, uint32_t M, typename F> int apply_fn(botan_struct<T, M>* o, const char* func_name, F func) |