gp: Test with binary content for certificate data
[Samba.git] / third_party / heimdal / lib / base / error_string.c
bloba562833a91a00080cc9cb844fdd69241e0b0cbfa
1 /*
2 * Copyright (c) 2001, 2003, 2005 - 2020 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "baselocl.h"
36 #undef __attribute__
37 #define __attribute__(x)
39 void
40 heim_clear_error_message(heim_context context)
42 if (!context)
43 return;
44 if (context->error_string)
45 free(context->error_string);
46 context->error_code = 0;
47 context->error_string = NULL;
50 void
51 heim_set_error_message(heim_context context, heim_error_code ret,
52 const char *fmt, ...)
53 __attribute__ ((__format__ (__printf__, 3, 4)))
55 va_list ap;
57 va_start(ap, fmt);
58 if (context)
59 heim_vset_error_message(context, ret, fmt, ap);
60 va_end(ap);
63 void
64 heim_vset_error_message(heim_context context, heim_error_code ret,
65 const char *fmt, va_list args)
66 __attribute__ ((__format__ (__printf__, 3, 0)))
68 int r;
70 if (context == NULL)
71 return;
72 if (context->error_string) {
73 free(context->error_string);
74 context->error_string = NULL;
76 context->error_code = ret;
77 r = vasprintf(&context->error_string, fmt, args);
78 if (r < 0)
79 context->error_string = NULL;
80 if (context->error_string)
81 heim_debug(context, 200, "error message: %s: %d", context->error_string, ret);
84 void
85 heim_prepend_error_message(heim_context context, heim_error_code ret,
86 const char *fmt, ...)
87 __attribute__ ((__format__ (__printf__, 3, 4)))
89 va_list ap;
91 va_start(ap, fmt);
92 heim_vprepend_error_message(context, ret, fmt, ap);
93 va_end(ap);
96 void
97 heim_vprepend_error_message(heim_context context, heim_error_code ret,
98 const char *fmt, va_list args)
99 __attribute__ ((__format__ (__printf__, 3, 0)))
101 char *str = NULL, *str2 = NULL;
103 if (context == NULL || context->error_code != ret ||
104 vasprintf(&str, fmt, args) < 0 || str == NULL)
105 return;
106 if (context->error_string) {
107 int e;
109 e = asprintf(&str2, "%s: %s", str, context->error_string);
110 free(context->error_string);
111 if (e < 0 || str2 == NULL)
112 context->error_string = NULL;
113 else
114 context->error_string = str2;
115 free(str);
116 } else
117 context->error_string = str;
120 const char *
121 heim_get_error_message(heim_context context, heim_error_code code)
123 const char *cstr = NULL;
124 char *str = NULL;
125 char buf[128];
126 int free_context = 0;
128 if (code == 0)
129 return strdup("Success");
132 * The MIT version of this function ignores the krb5_context
133 * and several widely deployed applications call krb5_get_error_message()
134 * with a NULL context in order to translate an error code as a
135 * replacement for error_message(). Another reason a NULL context
136 * might be provided is if the krb5_init_context() call itself
137 * failed.
139 if (context &&
140 context->error_string &&
141 (code == context->error_code || context->error_code == 0) &&
142 (cstr = strdup(context->error_string)))
143 return cstr;
145 if (context == NULL && (context = heim_context_init()))
146 free_context = 1;
147 if (context)
148 cstr = com_right_r(context->et_list, code, buf, sizeof(buf));
149 if (free_context)
150 heim_context_free(&context);
152 if (cstr || (cstr = error_message(code)))
153 return strdup(cstr);
154 if (asprintf(&str, "<unknown error: %d>", (int)code) == -1 || str == NULL)
155 return NULL;
156 return str;
159 const char *
160 heim_get_error_string(heim_context context)
162 if (context && context->error_string)
163 return strdup(context->error_string);
164 return NULL;
168 heim_have_error_string(heim_context context)
170 return context && context->error_string != NULL;
173 void
174 heim_free_error_message(heim_context context, const char *msg)
176 free(rk_UNCONST(msg));