| 1 | /* assuan-util.c - Utility functions for Assuan |
| 2 | * Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This file is part of Assuan. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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/>. |
| 18 | */ |
| 19 | |
| 20 | #include <config.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <ctype.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #include "assuan-defs.h" |
| 28 | |
| 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; |
| 32 | |
| 33 | struct assuan_io_hooks _assuan_io_hooks; |
| 34 | |
| 35 | |
| 36 | |
| 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*) ) |
| 41 | { |
| 42 | alloc_func = new_alloc_func; |
| 43 | realloc_func = new_realloc_func; |
| 44 | free_func = new_free_func; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | void |
| 49 | assuan_set_io_hooks (assuan_io_hooks_t io_hooks) |
| 50 | { |
| 51 | _assuan_io_hooks.read_hook = NULL; |
| 52 | _assuan_io_hooks.write_hook = NULL; |
| 53 | if (io_hooks) |
| 54 | { |
| 55 | _assuan_io_hooks.read_hook = io_hooks->read_hook; |
| 56 | _assuan_io_hooks.write_hook = io_hooks->write_hook; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | void * |
| 62 | _assuan_malloc (size_t n) |
| 63 | { |
| 64 | return alloc_func (n); |
| 65 | } |
| 66 | |
| 67 | void * |
| 68 | _assuan_realloc (void *a, size_t n) |
| 69 | { |
| 70 | return realloc_func (a, n); |
| 71 | } |
| 72 | |
| 73 | void * |
| 74 | _assuan_calloc (size_t n, size_t m) |
| 75 | { |
| 76 | void *p; |
| 77 | size_t nbytes; |
| 78 | |
| 79 | nbytes = n * m; |
| 80 | if (m && nbytes / m != n) |
| 81 | { |
| 82 | errno = ENOMEM; |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | p = _assuan_malloc (nbytes); |
| 87 | if (p) |
| 88 | memset (p, 0, nbytes); |
| 89 | return p; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | _assuan_free (void *p) |
| 94 | { |
| 95 | if (p) |
| 96 | free_func (p); |
| 97 | } |
| 98 | |
| 99 | |
| 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. */ |
| 103 | int |
| 104 | assuan_set_error (assuan_context_t ctx, int err, const char *text) |
| 105 | { |
| 106 | ctx->err_no = err; |
| 107 | ctx->err_str = text; |
| 108 | return err; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | assuan_set_pointer (assuan_context_t ctx, void *pointer) |
| 113 | { |
| 114 | if (ctx) |
| 115 | ctx->user_pointer = pointer; |
| 116 | } |
| 117 | |
| 118 | void * |
| 119 | assuan_get_pointer (assuan_context_t ctx) |
| 120 | { |
| 121 | return ctx? ctx->user_pointer : NULL; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | void |
| 126 | assuan_begin_confidential (assuan_context_t ctx) |
| 127 | { |
| 128 | if (ctx) |
| 129 | { |
| 130 | ctx->confidential = 1; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | assuan_end_confidential (assuan_context_t ctx) |
| 136 | { |
| 137 | if (ctx) |
| 138 | { |
| 139 | ctx->confidential = 0; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void |
| 145 | assuan_set_io_monitor (assuan_context_t ctx, |
| 146 | unsigned int (*monitor)(assuan_context_t ctx, |
| 147 | int direction, |
| 148 | const char *line, |
| 149 | size_t linelen)) |
| 150 | { |
| 151 | if (ctx) |
| 152 | { |
| 153 | ctx->io_monitor = monitor; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | /* For context CTX, set the flag FLAG to VALUE. Values for flags |
| 161 | are usually 1 or 0 but certain flags might allow for other values; |
| 162 | see the description of the type assuan_flag_t for details. */ |
| 163 | void |
| 164 | assuan_set_flag (assuan_context_t ctx, assuan_flag_t flag, int value) |
| 165 | { |
| 166 | if (!ctx) |
| 167 | return; |
| 168 | switch (flag) |
| 169 | { |
| 170 | case ASSUAN_NO_WAITPID: ctx->flags.no_waitpid = value; break; |
| 171 | case ASSUAN_CONFIDENTIAL: ctx->confidential = value; break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* Return the VALUE of FLAG in context CTX. */ |
| 176 | int |
| 177 | assuan_get_flag (assuan_context_t ctx, assuan_flag_t flag) |
| 178 | { |
| 179 | if (!ctx) |
| 180 | return 0; |
| 181 | switch (flag) |
| 182 | { |
| 183 | case ASSUAN_NO_WAITPID: return ctx->flags.no_waitpid; |
| 184 | case ASSUAN_CONFIDENTIAL: return ctx->confidential; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 |