| 1 | /* assuan-util.c - Utility functions for Assuan |
| 2 | * Copyright (C) 2001 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This file is part of GnuPG. |
| 5 | * |
| 6 | * GnuPG is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * GnuPG 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 General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA |
| 19 | */ |
| 20 | |
| 21 | #include <config.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "assuan-defs.h" |
| 27 | |
| 28 | #ifdef HAVE_JNLIB_LOGGING |
| 29 | #include "../jnlib/logging.h" |
| 30 | #endif |
| 31 | |
| 32 | |
| 33 | static void *(*alloc_func)(size_t n) = malloc; |
| 34 | static void *(*realloc_func)(void *p, size_t n) = realloc; |
| 35 | static void (*free_func)(void*) = free; |
| 36 | |
| 37 | |
| 38 | |
| 39 | void |
| 40 | assuan_set_malloc_hooks ( void *(*new_alloc_func)(size_t n), |
| 41 | void *(*new_realloc_func)(void *p, size_t n), |
| 42 | void (*new_free_func)(void*) ) |
| 43 | { |
| 44 | alloc_func = new_alloc_func; |
| 45 | realloc_func = new_realloc_func; |
| 46 | free_func = new_free_func; |
| 47 | } |
| 48 | |
| 49 | void * |
| 50 | _assuan_malloc (size_t n) |
| 51 | { |
| 52 | return alloc_func (n); |
| 53 | } |
| 54 | |
| 55 | void * |
| 56 | _assuan_realloc (void *a, size_t n) |
| 57 | { |
| 58 | return realloc_func (a, n); |
| 59 | } |
| 60 | |
| 61 | void * |
| 62 | _assuan_calloc (size_t n, size_t m) |
| 63 | { |
| 64 | void *p = _assuan_malloc (n*m); |
| 65 | if (p) |
| 66 | memset (p, 0, n* m); |
| 67 | return p; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | _assuan_free (void *p) |
| 72 | { |
| 73 | if (p) |
| 74 | free_func (p); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | /* Store the error in the context so that the error sending function |
| 80 | can take out a descriptive text. Inside the assuan code, use the |
| 81 | macro set_error instead of this function. */ |
| 82 | int |
| 83 | assuan_set_error (ASSUAN_CONTEXT ctx, int err, const char *text) |
| 84 | { |
| 85 | ctx->err_no = err; |
| 86 | ctx->err_str = text; |
| 87 | return err; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | assuan_set_pointer (ASSUAN_CONTEXT ctx, void *pointer) |
| 92 | { |
| 93 | if (ctx) |
| 94 | ctx->user_pointer = pointer; |
| 95 | } |
| 96 | |
| 97 | void * |
| 98 | assuan_get_pointer (ASSUAN_CONTEXT ctx) |
| 99 | { |
| 100 | return ctx? ctx->user_pointer : NULL; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | void |
| 105 | assuan_set_log_stream (ASSUAN_CONTEXT ctx, FILE *fp) |
| 106 | { |
| 107 | if (ctx) |
| 108 | { |
| 109 | if (ctx->log_fp) |
| 110 | fflush (ctx->log_fp); |
| 111 | ctx->log_fp = fp; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | void |
| 117 | assuan_begin_confidential (ASSUAN_CONTEXT ctx) |
| 118 | { |
| 119 | if (ctx) |
| 120 | { |
| 121 | ctx->confidential = 1; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | assuan_end_confidential (ASSUAN_CONTEXT ctx) |
| 127 | { |
| 128 | if (ctx) |
| 129 | { |
| 130 | ctx->confidential = 0; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | _assuan_log_print_buffer (FILE *fp, const void *buffer, size_t length) |
| 136 | { |
| 137 | const unsigned char *s; |
| 138 | int n; |
| 139 | |
| 140 | for (n=length,s=buffer; n; n--, s++) |
| 141 | { |
| 142 | if (*s < ' ' || (*s >= 0x7f && *s <= 0xa0)) |
| 143 | break; |
| 144 | } |
| 145 | s = buffer; |
| 146 | if (!n && *s != '[') |
| 147 | fwrite (buffer, length, 1, fp); |
| 148 | else |
| 149 | { |
| 150 | putc ('[', fp); |
| 151 | for (n=0; n < length; n++, s++) |
| 152 | fprintf (fp, " %02x", *s); |
| 153 | putc (' ', fp); |
| 154 | putc (']', fp); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /* print a user supplied string after filtering out potential bad |
| 160 | characters*/ |
| 161 | void |
| 162 | _assuan_log_sanitized_string (const char *string) |
| 163 | { |
| 164 | const unsigned char *s = (const unsigned char*)string; |
| 165 | #ifdef HAVE_JNLIB_LOGGING |
| 166 | FILE *fp = log_get_stream (); |
| 167 | #else |
| 168 | FILE *fp = stderr; |
| 169 | #endif |
| 170 | |
| 171 | for (; *s; s++) |
| 172 | { |
| 173 | if (*s < 0x20 || (*s >= 0x7f && *s <= 0xa0)) |
| 174 | { |
| 175 | putc ('\\', fp); |
| 176 | if (*s == '\n') |
| 177 | putc ('n', fp); |
| 178 | else if (*s == '\r') |
| 179 | putc ('r', fp); |
| 180 | else if (*s == '\f') |
| 181 | putc ('f', fp); |
| 182 | else if (*s == '\v') |
| 183 | putc ('v', fp); |
| 184 | else if (*s == '\b') |
| 185 | putc ('b', fp); |
| 186 | else if (!*s) |
| 187 | putc ('0', fp); |
| 188 | else |
| 189 | fprintf (fp, "x%02x", *s ); |
| 190 | } |
| 191 | else |
| 192 | putc (*s, fp); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 |