aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-08-13 07:47:47 -0400
committerJack Lloyd <[email protected]>2018-08-13 07:47:47 -0400
commitf0aa0fac6b495acf841079316e2c63318ece839b (patch)
tree15af3dc120dd9c2265c05b1dd0707e1da6a66d4b
parent2ddb3656d721f22c912e428b6c6a6af9398fbed9 (diff)
De-inline ffi_guard_thunk
Saves about 300 Kb of code space in the FFI object files
-rw-r--r--src/lib/ffi/ffi.cpp49
-rw-r--r--src/lib/ffi/ffi.h1
-rw-r--r--src/lib/ffi/ffi_util.h36
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)