forked from heimdal/MKShim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcom_err.c
161 lines (118 loc) · 3.22 KB
/
com_err.c
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "heim.h"
#include <com_err.h>
#include <Heimdal/gkrb5_err.h>
#include <Heimdal/wind_err.h>
#include <Heimdal/krb_err.h>
#include <Heimdal/hx509_err.h>
static void
init_error_tables(void * context)
{
struct et_list ** et_list = (struct et_list **) context;
initialize_asn1_error_table_r(et_list);
initialize_gk5_error_table_r(et_list);
initialize_wind_error_table_r(et_list);
initialize_krb5_error_table_r(et_list);
initialize_krb_error_table_r(et_list);
initialize_k524_error_table_r(et_list);
initialize_heim_error_table_r(et_list);
initialize_hx_error_table_r(et_list);
}
static void
mshim_init_error_tables(struct et_list **et_list)
{
static dispatch_once_t once;
dispatch_once_f(&once, (void *) et_list, init_error_tables);
}
const char * KRB5_CALLCONV
mit_error_message(errcode_t code)
{
static struct et_list *et_list = NULL;
static char buffer[1024];
const char *str;
mshim_init_error_tables(&et_list);
str = heim_com_right_r(et_list, code, buffer, sizeof(buffer));
if (str == NULL) {
#ifdef HAVE_STRSAFE
StringCbPrintfA(buffer, sizeof(buffer), "Unknown error %d", (int)code);
#else
snprintf(buffer, sizeof(buffer), "Unknown error %d", (int)code);
#endif
str = buffer;
}
return str;
}
void KRB5_CALLCONV
mit_com_err(const char *progname, errcode_t code, const char *format, ...)
{
va_list args;
va_start(args, format);
heim_com_err_va(progname, code, format, args);
}
void KRB5_CALLCONV
mit_com_err_va(const char *progname, errcode_t code, const char *format, va_list args)
{
heim_com_err_va(progname, code, format, args);
}
#if defined(_WIN32) && !defined(_WIN64)
static volatile com_err_handler_t com_err_handler = NULL;
static void KRB5_CALLCONV
err_handler_thunk(const char * pname, long code, const char * format, va_list args)
{
com_err_handler_t h = com_err_handler;
if (h != NULL)
(*h)(pname, code, format, args);
}
com_err_handler_t
mit_set_com_err_hook(com_err_handler_t handler)
{
com_err_handler_t prev = NULL;
#ifdef HAVE_INTERLOCKED
prev = com_err_handler;
do {
com_err_handler_t t;
t = InterlockedCompareExchangePointer(&com_err_handler,
handler, prev);
if (t == prev)
break;
prev = t;
} while (TRUE);
#else
prev = com_err_handler;
com_err_handler = handler;
#endif
heim_set_com_err_hook(err_handler_thunk);
return prev;
}
com_err_handler_t
mit_reset_com_err_hook(void)
{
com_err_handler_t prev;
#ifdef HAVE_INTERLOCKED
prev = com_err_handler;
do {
com_err_handler_t t;
t = InterlockedCompareExchangePointer(&com_err_handler,
NULL, prev);
if (t == prev)
break;
prev = t;
} while (TRUE);
#else
prev = com_err_handler;
com_err_handler = NULL;
#endif
heim_reset_com_err_hook();
return prev;
}
#elif defined(_WIN64)
com_err_handler_t
mit_set_com_err_hook(com_err_handler_t handler)
{
return heim_set_com_err_hook(handler);
}
com_err_handler_t
mit_reset_com_err_hook(void)
{
return heim_reset_com_err_hook();
}
#endif