aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/error.c
blob: 034aa72a81b4cc634729898d21e9bd5b4ec779b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * \file
 * \author Egor Tensin <Egor.Tensin@gmail.com>
 * \date 2015
 * \copyright This file is licensed under the terms of the MIT License.
 *            See LICENSE.txt for details.
 */

#include <aesni/all.h>

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char* err_msgs[] =
{
    "Success",
    "Invalid argument value NULL",
    "Invalid PKCS7 padding (wrong key?)",
};

const char* aesni_strerror(AesNI_StatusCode ec)
{
    return err_msgs[ec];
}

static size_t aesni_format_error_strerror(
    const AesNI_ErrorDetails* err_details,\
    char* dest,
    size_t dest_size)
{
    const AesNI_StatusCode 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_strerror,
    &aesni_format_error_null_argument,
    &aesni_format_error_strerror,
};

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 AesNI_StatusCode aesni_make_error(
    AesNI_ErrorDetails* err_details,
    AesNI_StatusCode ec)
{
    if (err_details == NULL)
        return ec;

    return err_details->ec = ec;
}

AesNI_StatusCode aesni_make_error_success(
    AesNI_ErrorDetails* err_details)
{
    return aesni_make_error(err_details, AESNI_SUCCESS);
}

AesNI_StatusCode aesni_make_error_null_argument(
    AesNI_ErrorDetails* err_details,
    const char* arg_name)
{
    AesNI_StatusCode status = aesni_make_error(err_details, AESNI_ERROR_NULL_ARGUMENT);

    const size_t arg_name_size = sizeof(err_details->params.null_arg.arg_name);
    strncpy(err_details->params.null_arg.arg_name, arg_name, arg_name_size);
    err_details->params.null_arg.arg_name[arg_name_size - 1] = '\0';

    return status;
}

AesNI_StatusCode aesni_make_error_invalid_pkcs7_padding(
    AesNI_ErrorDetails* err_details)
{
    return aesni_make_error(err_details, AESNI_ERROR_INVALID_PKCS7_PADDING);
}