1/*
2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License").  You may not use
5 * this file except in compliance with the License.  You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#include <openssl/err.h>
11#include <openssl/e_os2.h>
12
13static ossl_inline void err_get_slot(ERR_STATE *es)
14{
15    es->top = (es->top + 1) % ERR_NUM_ERRORS;
16    if (es->top == es->bottom)
17        es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
18}
19
20static ossl_inline void err_clear_data(ERR_STATE *es, size_t i, int deall)
21{
22    if (es->err_data_flags[i] & ERR_TXT_MALLOCED) {
23        if (deall) {
24            OPENSSL_free(es->err_data[i]);
25            es->err_data[i] = NULL;
26            es->err_data_size[i] = 0;
27            es->err_data_flags[i] = 0;
28        } else if (es->err_data[i] != NULL) {
29            es->err_data[i][0] = '\0';
30            es->err_data_flags[i] = ERR_TXT_MALLOCED;
31        }
32    } else {
33        es->err_data[i] = NULL;
34        es->err_data_size[i] = 0;
35        es->err_data_flags[i] = 0;
36    }
37}
38
39static ossl_inline void err_set_error(ERR_STATE *es, size_t i,
40                                      int lib, int reason)
41{
42    es->err_buffer[i] =
43        lib == ERR_LIB_SYS
44        ? (unsigned int)(ERR_SYSTEM_FLAG |  reason)
45        : ERR_PACK(lib, 0, reason);
46}
47
48static ossl_inline void err_set_debug(ERR_STATE *es, size_t i,
49                                      const char *file, int line,
50                                      const char *fn)
51{
52    /*
53     * We dup the file and fn strings because they may be provider owned. If the
54     * provider gets unloaded, they may not be valid anymore.
55     */
56    OPENSSL_free(es->err_file[i]);
57    if (file == NULL || file[0] == '\0')
58        es->err_file[i] = NULL;
59    else
60        es->err_file[i] = OPENSSL_strdup(file);
61    es->err_line[i] = line;
62    OPENSSL_free(es->err_func[i]);
63    if (fn == NULL || fn[0] == '\0')
64        es->err_func[i] = NULL;
65    else
66        es->err_func[i] = OPENSSL_strdup(fn);
67}
68
69static ossl_inline void err_set_data(ERR_STATE *es, size_t i,
70                                     void *data, size_t datasz, int flags)
71{
72    if ((es->err_data_flags[i] & ERR_TXT_MALLOCED) != 0)
73        OPENSSL_free(es->err_data[i]);
74    es->err_data[i] = data;
75    es->err_data_size[i] = datasz;
76    es->err_data_flags[i] = flags;
77}
78
79static ossl_inline void err_clear(ERR_STATE *es, size_t i, int deall)
80{
81    err_clear_data(es, i, (deall));
82    es->err_marks[i] = 0;
83    es->err_flags[i] = 0;
84    es->err_buffer[i] = 0;
85    es->err_line[i] = -1;
86    OPENSSL_free(es->err_file[i]);
87    es->err_file[i] = NULL;
88    OPENSSL_free(es->err_func[i]);
89    es->err_func[i] = NULL;
90}
91
92ERR_STATE *ossl_err_get_state_int(void);
93void ossl_err_string_int(unsigned long e, const char *func,
94                         char *buf, size_t len);
95