diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-11 19:48:49 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-06-11 19:48:49 +0300 |
commit | e484372bf3f618eb30b8e6970fd13efa20c48509 (patch) | |
tree | 54a5f9cc4cc3eb8d787bf123a84630bda19db12f | |
parent | add utils/cxx (diff) | |
download | aes-tools-e484372bf3f618eb30b8e6970fd13efa20c48509.tar.gz aes-tools-e484372bf3f618eb30b8e6970fd13efa20c48509.zip |
add error message formatting functions
-rw-r--r-- | include/aesni/error.h | 9 | ||||
-rw-r--r-- | src/error.c | 52 | ||||
-rw-r--r-- | utils/cxx/include/aesnixx/error.hpp | 13 |
3 files changed, 72 insertions, 2 deletions
diff --git a/include/aesni/error.h b/include/aesni/error.h index 54d3d34..47bd0d6 100644 --- a/include/aesni/error.h +++ b/include/aesni/error.h @@ -8,6 +8,8 @@ #pragma once +#include <stdlib.h> + #ifdef __cplusplus extern "C" { @@ -41,6 +43,13 @@ typedef struct } AesNI_ErrorDetails; +static __inline AesNI_ErrorCode aesni_get_error_code(const AesNI_ErrorDetails* err_details) +{ + return err_details->ec; +} + +size_t aesni_format_error(const AesNI_ErrorDetails*, char*, size_t); + void aesni_make_error_success(AesNI_ErrorDetails*); void aesni_make_error_null_argument(AesNI_ErrorDetails*, const char* arg_name); void aesni_make_error_invalid_pkcs7_padding(AesNI_ErrorDetails*); diff --git a/src/error.c b/src/error.c index c4af82c..e52cd4d 100644 --- a/src/error.c +++ b/src/error.c @@ -8,6 +8,8 @@ #include <aesni/all.h> +#include <assert.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -23,6 +25,56 @@ const char* aesni_strerror(AesNI_ErrorCode ec) return err_msgs[ec]; } +static size_t aesni_format_error_simple( + const AesNI_ErrorDetails* err_details,\ + char* dest, + size_t dest_size) +{ + const AesNI_ErrorCode ec = aesni_get_error_code(err_details); + const char* const msg = aesni_strerror(ec); + + if (dest == NULL && dest_size == 0) + return strlen(msg) + 1; + + strncpy(dest, msg, dest_size); + dest[dest_size - 1] = '\0'; + return strlen(dest); +} + +static size_t aesni_format_error_null_argument( + const AesNI_ErrorDetails* err_details, + char* dest, + size_t dest_size) +{ + static const char* const fmt = "Invalid argument value NULL (argument name: '%s')"; + const char* const arg_name = err_details->params.null_arg.arg_name; + + if (dest == NULL && dest_size == 0) + return _snprintf(NULL, 0, fmt, arg_name) + 1; + + _snprintf(dest, dest_size, fmt, arg_name); + return strlen(dest); +} + +typedef size_t (*AesNI_ErrorFormatter)(const AesNI_ErrorDetails*, char*, size_t); + +static AesNI_ErrorFormatter err_formatters[] = +{ + &aesni_format_error_simple, + &aesni_format_error_null_argument, + &aesni_format_error_simple, +}; + +size_t aesni_format_error( + const AesNI_ErrorDetails* err_details, + char* dest, + size_t dest_size) +{ + assert(err_details); + + return err_formatters[err_details->ec](err_details, dest, dest_size); +} + static void aesni_make_error( AesNI_ErrorDetails* err_details, AesNI_ErrorCode ec) diff --git a/utils/cxx/include/aesnixx/error.hpp b/utils/cxx/include/aesnixx/error.hpp index af61a32..db390ea 100644 --- a/utils/cxx/include/aesnixx/error.hpp +++ b/utils/cxx/include/aesnixx/error.hpp @@ -10,7 +10,11 @@ #include <aesni/all.h> +#include <cstdlib> + #include <stdexcept> +#include <string> +#include <vector> namespace aesni { @@ -24,8 +28,13 @@ namespace aesni ~ErrorDetailsThrowsInDestructor() { - if (m_impl.ec != AESNI_ERROR_SUCCESS) - throw std::runtime_error(aesni_strerror(m_impl.ec)); + if (aesni_get_error_code(get()) != AESNI_ERROR_SUCCESS) + { + std::vector<char> msg; + msg.resize(aesni_format_error(get(), NULL, 0)); + aesni_format_error(get(), msg.data(), msg.size()); + throw std::runtime_error(std::string(msg.begin(), msg.end())); + } } AesNI_ErrorDetails* get() { return &m_impl; } |