1 /* assuan-util.c - Utility functions for Assuan
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 * This file is part of Assuan.
6 * Assuan is free software; you can redistribute it and/or modify it
7 * 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.
11 * Assuan is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include "assuan-defs.h"
29 static void *(*alloc_func
)(size_t n
) = malloc
;
30 static void *(*realloc_func
)(void *p
, size_t n
) = realloc
;
31 static void (*free_func
)(void*) = free
;
33 struct assuan_io_hooks _assuan_io_hooks
;
38 assuan_set_malloc_hooks ( void *(*new_alloc_func
)(size_t n
),
39 void *(*new_realloc_func
)(void *p
, size_t n
),
40 void (*new_free_func
)(void*) )
42 alloc_func
= new_alloc_func
;
43 realloc_func
= new_realloc_func
;
44 free_func
= new_free_func
;
49 assuan_set_io_hooks (assuan_io_hooks_t io_hooks
)
51 _assuan_io_hooks
.read_hook
= NULL
;
52 _assuan_io_hooks
.write_hook
= NULL
;
55 _assuan_io_hooks
.read_hook
= io_hooks
->read_hook
;
56 _assuan_io_hooks
.write_hook
= io_hooks
->write_hook
;
62 _assuan_malloc (size_t n
)
64 return alloc_func (n
);
68 _assuan_realloc (void *a
, size_t n
)
70 return realloc_func (a
, n
);
74 _assuan_calloc (size_t n
, size_t m
)
80 if (m
&& nbytes
/ m
!= n
)
86 p
= _assuan_malloc (nbytes
);
88 memset (p
, 0, nbytes
);
93 _assuan_free (void *p
)
100 /* Store the error in the context so that the error sending function
101 can take out a descriptive text. Inside the assuan code, use the
102 macro set_error instead of this function. */
104 assuan_set_error (assuan_context_t ctx
, int err
, const char *text
)
111 void assuan_set_finish_handler(assuan_context_t ctx
,
112 void (*handler
)(assuan_context_t
))
115 ctx
->user_finish_handler
= handler
;
119 assuan_set_pointer (assuan_context_t ctx
, void *pointer
)
122 ctx
->user_pointer
= pointer
;
126 assuan_get_pointer (assuan_context_t ctx
)
128 return ctx
? ctx
->user_pointer
: NULL
;
133 assuan_begin_confidential (assuan_context_t ctx
)
137 ctx
->confidential
= 1;
142 assuan_end_confidential (assuan_context_t ctx
)
146 ctx
->confidential
= 0;
152 assuan_set_io_monitor (assuan_context_t ctx
,
153 unsigned int (*monitor
)(assuan_context_t ctx
,
160 ctx
->io_monitor
= monitor
;
167 /* For context CTX, set the flag FLAG to VALUE. Values for flags
168 are usually 1 or 0 but certain flags might allow for other values;
169 see the description of the type assuan_flag_t for details. */
171 assuan_set_flag (assuan_context_t ctx
, assuan_flag_t flag
, int value
)
177 case ASSUAN_NO_WAITPID
: ctx
->flags
.no_waitpid
= value
; break;
178 case ASSUAN_CONFIDENTIAL
: ctx
->confidential
= value
; break;
182 /* Return the VALUE of FLAG in context CTX. */
184 assuan_get_flag (assuan_context_t ctx
, assuan_flag_t flag
)
190 case ASSUAN_NO_WAITPID
: return ctx
->flags
.no_waitpid
;
191 case ASSUAN_CONFIDENTIAL
: return ctx
->confidential
;