diff options
author | Jack Lloyd <[email protected]> | 2018-02-07 17:46:38 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-02-07 17:46:38 -0500 |
commit | 3d868667ab5a29c31acde34c22b5b4176619260e (patch) | |
tree | e601c3edfe0f1e79f3ed4c3a6898f8130d576ec2 /src/lib/ffi | |
parent | dc5b0ab629942d6675f70b5450f7fd12c05320ed (diff) |
Make FFI errors an enum, and add a function to translate to strings
Diffstat (limited to 'src/lib/ffi')
-rw-r--r-- | src/lib/ffi/ffi.cpp | 45 | ||||
-rw-r--r-- | src/lib/ffi/ffi.h | 80 |
2 files changed, 71 insertions, 54 deletions
diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp index 39749c4d4..ecc8d2899 100644 --- a/src/lib/ffi/ffi.cpp +++ b/src/lib/ffi/ffi.cpp @@ -26,6 +26,50 @@ extern "C" { using namespace Botan_FFI; +const char* botan_error_description(int err) + { + switch(err) + { + case BOTAN_FFI_SUCCESS: + return "OK"; + + case BOTAN_FFI_INVALID_VERIFIER: + return "Invalid verifier"; + + case BOTAN_FFI_ERROR_INVALID_INPUT: + return "Invalid input"; + + case BOTAN_FFI_ERROR_BAD_MAC: + return "Invalid authentication code"; + + case BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE: + return "Insufficient buffer space"; + + case BOTAN_FFI_ERROR_EXCEPTION_THROWN: + return "Exception thrown"; + + case BOTAN_FFI_ERROR_BAD_FLAG: + return "Bad flag"; + + case BOTAN_FFI_ERROR_NULL_POINTER: + return "Null pointer argument"; + + case BOTAN_FFI_ERROR_BAD_PARAMETER: + return "Bad parameter"; + + case BOTAN_FFI_ERROR_NOT_IMPLEMENTED: + return "Not implemented"; + + case BOTAN_FFI_ERROR_INVALID_OBJECT: + return "Invalid object handle"; + + case BOTAN_FFI_ERROR_UNKNOWN_ERROR: + return "Unknown error"; + } + + return "Unknown error"; + } + /* * Versioning */ @@ -120,4 +164,3 @@ int botan_base64_decode(const char* base64_str, size_t in_len, } } - diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index 20611fde9..323bf54de 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -62,6 +62,33 @@ how to provide the cleanest API for such users would be most welcome. #include <stddef.h> /** +* Error codes +*/ +enum BOTAN_FFI_ERROR { + BOTAN_FFI_SUCCESS = 0, + BOTAN_FFI_INVALID_VERIFIER = 1, + + BOTAN_FFI_ERROR_INVALID_INPUT = -1, + BOTAN_FFI_ERROR_BAD_MAC = -2, + + BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE = -10, + BOTAN_FFI_ERROR_EXCEPTION_THROWN = -20, + BOTAN_FFI_ERROR_BAD_FLAG = -30, + BOTAN_FFI_ERROR_NULL_POINTER = -31, + BOTAN_FFI_ERROR_BAD_PARAMETER = -32, + BOTAN_FFI_ERROR_NOT_IMPLEMENTED = -40, + BOTAN_FFI_ERROR_INVALID_OBJECT = -50, + + BOTAN_FFI_ERROR_UNKNOWN_ERROR = -100, +}; + +/** +* Convert an error code into a string. Returns "Unknown error" +* if the error code is not a known one. +*/ +const char* botan_error_description(int err); + +/** * Return the version of the currently supported FFI API. This is * expressed in the form YYYYMMDD of the release date of this version * of the API. @@ -100,59 +127,6 @@ BOTAN_PUBLIC_API(2,0) uint32_t botan_version_patch(); */ BOTAN_PUBLIC_API(2,0) uint32_t botan_version_datestamp(); -/* -* Error handling -* -* Some way of exporting these values to other languages would be useful - - - THIS FUNCTION ASSUMES BOTH ARGUMENTS ARE LITERAL STRINGS - so it retains only the pointers and does not make a copy. - -int botan_make_error(const char* msg, const char* func, int line); -* This value is returned to callers ^^ - - normally called like - return botan_make_error(BOTAN_ERROR_STRING_NOT_IMPLEMENTED, BOTAN_FUNCTION, __LINE__); - -// This would seem to require both saving the message permanently -catch(std::exception& e) { -return botan_make_error_from_transient_string(e.what(), BOTAN_FUNCTION, __LINE__); -} - -#define botan_make_error_inf(s) return botan_make_error(s, BOTAN_FUNCTION, __LINE__); - -Easier to return a const char* from each function directly? However, - -catch(std::exception& e) { return e.what(); } - -doesn't exactly work well either! - -* -* Later call: -* const char* botan_get_error_str(int); -* To recover the msg, func, and line - -*/ -#define BOTAN_FFI_SUCCESS (0) - -#define BOTAN_FFI_INVALID_VERIFIER (1) - -#define BOTAN_FFI_ERROR_INVALID_INPUT (-1) -#define BOTAN_FFI_ERROR_BAD_MAC (-2) - -#define BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE (-10) -#define BOTAN_FFI_ERROR_EXCEPTION_THROWN (-20) -#define BOTAN_FFI_ERROR_BAD_FLAG (-30) -#define BOTAN_FFI_ERROR_NULL_POINTER (-31) -#define BOTAN_FFI_ERROR_BAD_PARAMETER (-32) -#define BOTAN_FFI_ERROR_NOT_IMPLEMENTED (-40) -#define BOTAN_FFI_ERROR_INVALID_OBJECT (-50) - -#define BOTAN_FFI_ERROR_UNKNOWN_ERROR (-100) - -//const char* botan_error_description(int err); - /** * Returns 0 if x[0..len] == y[0..len], or otherwise -1 */ |