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