#include <stdarg.h>
[libpwmd.git] / assuan / assuan-util.c
blob07800f2c8dfb251b9f56ce9466234e6547b5e803
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/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <errno.h>
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;
37 void
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;
48 void
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;
53 if (io_hooks)
55 _assuan_io_hooks.read_hook = io_hooks->read_hook;
56 _assuan_io_hooks.write_hook = io_hooks->write_hook;
61 void *
62 _assuan_malloc (size_t n)
64 return alloc_func (n);
67 void *
68 _assuan_realloc (void *a, size_t n)
70 return realloc_func (a, n);
73 void *
74 _assuan_calloc (size_t n, size_t m)
76 void *p;
77 size_t nbytes;
79 nbytes = n * m;
80 if (m && nbytes / m != n)
82 errno = ENOMEM;
83 return NULL;
86 p = _assuan_malloc (nbytes);
87 if (p)
88 memset (p, 0, nbytes);
89 return p;
92 void
93 _assuan_free (void *p)
95 if (p)
96 free_func (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)
106 ctx->err_no = err;
107 ctx->err_str = text;
108 return err;
111 void assuan_set_finish_handler(assuan_context_t ctx,
112 void (*handler)(assuan_context_t))
114 if (ctx)
115 ctx->user_finish_handler = handler;
118 void
119 assuan_set_pointer (assuan_context_t ctx, void *pointer)
121 if (ctx)
122 ctx->user_pointer = pointer;
125 void *
126 assuan_get_pointer (assuan_context_t ctx)
128 return ctx? ctx->user_pointer : NULL;
132 void
133 assuan_begin_confidential (assuan_context_t ctx)
135 if (ctx)
137 ctx->confidential = 1;
141 void
142 assuan_end_confidential (assuan_context_t ctx)
144 if (ctx)
146 ctx->confidential = 0;
151 void
152 assuan_set_io_monitor (assuan_context_t ctx,
153 unsigned int (*monitor)(assuan_context_t ctx,
154 int direction,
155 const char *line,
156 size_t linelen))
158 if (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. */
170 void
171 assuan_set_flag (assuan_context_t ctx, assuan_flag_t flag, int value)
173 if (!ctx)
174 return;
175 switch (flag)
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)
186 if (!ctx)
187 return 0;
188 switch (flag)
190 case ASSUAN_NO_WAITPID: return ctx->flags.no_waitpid;
191 case ASSUAN_CONFIDENTIAL: return ctx->confidential;
193 return 0;