Diffstat (limited to 'pwmanager/libcrypt/crypt/misc.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | pwmanager/libcrypt/crypt/misc.c | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/pwmanager/libcrypt/crypt/misc.c b/pwmanager/libcrypt/crypt/misc.c new file mode 100644 index 0000000..6b630e6 --- a/dev/null +++ b/pwmanager/libcrypt/crypt/misc.c | |||
@@ -0,0 +1,241 @@ | |||
1 | /* misc.c | ||
2 | *Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser general Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | */ | ||
20 | |||
21 | #include <config.h> | ||
22 | #include <stdio.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | #include <stdarg.h> | ||
26 | #include <assert.h> | ||
27 | #include <unistd.h> | ||
28 | |||
29 | #include "g10lib.h" | ||
30 | |||
31 | static int verbosity_level = 0; | ||
32 | |||
33 | static void (*fatal_error_handler)(void*,int, const char*) = NULL; | ||
34 | static void *fatal_error_handler_value = 0; | ||
35 | static void (*log_handler)(void*,int, const char*, va_list) = NULL; | ||
36 | static void *log_handler_value = 0; | ||
37 | |||
38 | static const char *(*user_gettext_handler)( const char * ) = NULL; | ||
39 | |||
40 | void | ||
41 | gcry_set_gettext_handler( const char *(*f)(const char*) ) | ||
42 | { | ||
43 | user_gettext_handler = f; | ||
44 | } | ||
45 | |||
46 | |||
47 | const char * | ||
48 | _gcry_gettext( const char *key ) | ||
49 | { | ||
50 | if( user_gettext_handler ) | ||
51 | return user_gettext_handler( key ); | ||
52 | /* FIXME: switch the domain to gnupg and restore later */ | ||
53 | return key; | ||
54 | } | ||
55 | |||
56 | void | ||
57 | gcry_set_fatalerror_handler( void (*fnc)(void*,int, const char*), void *value) | ||
58 | { | ||
59 | fatal_error_handler_value = value; | ||
60 | fatal_error_handler = fnc; | ||
61 | } | ||
62 | |||
63 | static void | ||
64 | write2stderr( const char *s ) | ||
65 | { | ||
66 | write( 2, s, strlen(s) ); | ||
67 | } | ||
68 | |||
69 | /* | ||
70 | * This function is called for fatal errors. A caller might want to | ||
71 | * set his own handler because this function simply calls abort(). | ||
72 | */ | ||
73 | void | ||
74 | _gcry_fatal_error (int rc, const char *text) | ||
75 | { | ||
76 | if ( !text ) /* get a default text */ | ||
77 | text = gpg_strerror (rc); | ||
78 | |||
79 | if (fatal_error_handler) | ||
80 | fatal_error_handler (fatal_error_handler_value, rc, text); | ||
81 | |||
82 | write2stderr("\nFatal error: "); | ||
83 | write2stderr(text); | ||
84 | write2stderr("\n"); | ||
85 | abort (); | ||
86 | } | ||
87 | |||
88 | void | ||
89 | gcry_set_log_handler( void (*f)(void*,int, const char*, va_list ), | ||
90 | void *opaque ) | ||
91 | { | ||
92 | log_handler = f; | ||
93 | log_handler_value = opaque; | ||
94 | } | ||
95 | |||
96 | void | ||
97 | _gcry_set_log_verbosity( int level ) | ||
98 | { | ||
99 | verbosity_level = level; | ||
100 | } | ||
101 | |||
102 | int | ||
103 | _gcry_log_verbosity( int level ) | ||
104 | { | ||
105 | return verbosity_level >= level; | ||
106 | } | ||
107 | |||
108 | /**************** | ||
109 | * This is our log function which prints all log messages to stderr or | ||
110 | * using the function defined with gcry_set_log_handler(). | ||
111 | */ | ||
112 | static void | ||
113 | _gcry_logv( int level, const char *fmt, va_list arg_ptr ) | ||
114 | { | ||
115 | if( log_handler ) | ||
116 | log_handler( log_handler_value, level, fmt, arg_ptr ); | ||
117 | else { | ||
118 | switch ( level ) { | ||
119 | case GCRY_LOG_CONT: break; | ||
120 | case GCRY_LOG_INFO: break; | ||
121 | case GCRY_LOG_WARN: break; | ||
122 | case GCRY_LOG_ERROR: break; | ||
123 | case GCRY_LOG_FATAL: fputs("Fatal: ",stderr ); break; | ||
124 | case GCRY_LOG_BUG: fputs("Ohhhh jeeee: ", stderr); break; | ||
125 | case GCRY_LOG_DEBUG: fputs("DBG: ", stderr ); break; | ||
126 | default: fprintf(stderr,"[Unknown log level %d]: ", level ); break; | ||
127 | } | ||
128 | vfprintf(stderr,fmt,arg_ptr) ; | ||
129 | } | ||
130 | |||
131 | if( level == GCRY_LOG_FATAL ) | ||
132 | exit(2); | ||
133 | else if( level == GCRY_LOG_BUG ) | ||
134 | abort(); | ||
135 | } | ||
136 | |||
137 | void | ||
138 | _gcry_log( int level, const char *fmt, ... ) | ||
139 | { | ||
140 | va_list arg_ptr ; | ||
141 | |||
142 | va_start( arg_ptr, fmt ) ; | ||
143 | _gcry_logv( level, fmt, arg_ptr ); | ||
144 | va_end(arg_ptr); | ||
145 | } | ||
146 | |||
147 | |||
148 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) | ||
149 | void | ||
150 | _gcry_bug( const char *file, int line, const char *func ) | ||
151 | { | ||
152 | _gcry_log( GCRY_LOG_BUG, | ||
153 | ("... this is a bug (%s:%d:%s)\n"), file, line, func ); | ||
154 | abort(); /* never called, but it makes the compiler happy */ | ||
155 | } | ||
156 | #else | ||
157 | void | ||
158 | _gcry_bug( const char *file, int line ) | ||
159 | { | ||
160 | _gcry_log( GCRY_LOG_BUG, | ||
161 | _("you found a bug ... (%s:%d)\n"), file, line); | ||
162 | abort(); /* never called, but it makes the compiler happy */ | ||
163 | } | ||
164 | #endif | ||
165 | |||
166 | void | ||
167 | _gcry_log_info( const char *fmt, ... ) | ||
168 | { | ||
169 | va_list arg_ptr ; | ||
170 | |||
171 | va_start( arg_ptr, fmt ) ; | ||
172 | _gcry_logv( GCRY_LOG_INFO, fmt, arg_ptr ); | ||
173 | va_end(arg_ptr); | ||
174 | } | ||
175 | |||
176 | void | ||
177 | _gcry_log_error( const char *fmt, ... ) | ||
178 | { | ||
179 | va_list arg_ptr ; | ||
180 | |||
181 | va_start( arg_ptr, fmt ) ; | ||
182 | _gcry_logv( GCRY_LOG_ERROR, fmt, arg_ptr ); | ||
183 | va_end(arg_ptr); | ||
184 | } | ||
185 | |||
186 | |||
187 | void | ||
188 | _gcry_log_fatal( const char *fmt, ... ) | ||
189 | { | ||
190 | va_list arg_ptr ; | ||
191 | |||
192 | va_start( arg_ptr, fmt ) ; | ||
193 | _gcry_logv( GCRY_LOG_FATAL, fmt, arg_ptr ); | ||
194 | va_end(arg_ptr); | ||
195 | abort(); /* never called, but it makes the compiler happy */ | ||
196 | } | ||
197 | |||
198 | void | ||
199 | _gcry_log_bug( const char *fmt, ... ) | ||
200 | { | ||
201 | va_list arg_ptr ; | ||
202 | |||
203 | va_start( arg_ptr, fmt ) ; | ||
204 | _gcry_logv( GCRY_LOG_BUG, fmt, arg_ptr ); | ||
205 | va_end(arg_ptr); | ||
206 | abort(); /* never called, but it makes the compiler happy */ | ||
207 | } | ||
208 | |||
209 | void | ||
210 | _gcry_log_debug( const char *fmt, ... ) | ||
211 | { | ||
212 | va_list arg_ptr ; | ||
213 | |||
214 | va_start( arg_ptr, fmt ) ; | ||
215 | _gcry_logv( GCRY_LOG_DEBUG, fmt, arg_ptr ); | ||
216 | va_end(arg_ptr); | ||
217 | } | ||
218 | |||
219 | void | ||
220 | _gcry_log_printf (const char *fmt, ...) | ||
221 | { | ||
222 | va_list arg_ptr; | ||
223 | |||
224 | if (fmt) | ||
225 | { | ||
226 | va_start( arg_ptr, fmt ) ; | ||
227 | _gcry_logv (GCRY_LOG_CONT, fmt, arg_ptr); | ||
228 | va_end(arg_ptr); | ||
229 | } | ||
230 | } | ||
231 | |||
232 | void | ||
233 | _gcry_burn_stack (int bytes) | ||
234 | { | ||
235 | char buf[64]; | ||
236 | |||
237 | wipememory (buf, sizeof buf); | ||
238 | bytes -= sizeof buf; | ||
239 | if (bytes > 0) | ||
240 | _gcry_burn_stack (bytes); | ||
241 | } | ||