summaryrefslogtreecommitdiffabout
path: root/pwmanager/libcrypt/crypt/misc.c
Unidiff
Diffstat (limited to 'pwmanager/libcrypt/crypt/misc.c') (more/less context) (show whitespace changes)
-rw-r--r--pwmanager/libcrypt/crypt/misc.c241
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
31static int verbosity_level = 0;
32
33static void (*fatal_error_handler)(void*,int, const char*) = NULL;
34static void *fatal_error_handler_value = 0;
35static void (*log_handler)(void*,int, const char*, va_list) = NULL;
36static void *log_handler_value = 0;
37
38static const char *(*user_gettext_handler)( const char * ) = NULL;
39
40void
41gcry_set_gettext_handler( const char *(*f)(const char*) )
42{
43 user_gettext_handler = f;
44}
45
46
47const 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
56void
57gcry_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
63static void
64write2stderr( 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 */
73void
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
88void
89gcry_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
96void
97_gcry_set_log_verbosity( int level )
98{
99 verbosity_level = level;
100}
101
102int
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 */
112static 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
137void
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 )
149void
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
157void
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
166void
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
176void
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
187void
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
198void
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
209void
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
219void
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
232void
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}