| 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>cxx . . $ `<file>/config.h`/ |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "assuan-defs.h"cxx . . $ `<file>/assuan/assuan-defs.h`/ |
| 27 | |
| 28 | #ifdef HAVE_JNLIB_LOGGING |
| 29 | #include "../jnlib/logging.h" |
| 30 | #endif |
| 31 | |
| 32 | |
| 33 | static void *(*alloc_funccxx . . $ alloc_func.)(size_tcxx . . $ size_t# n) = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).; |
| 34 | static void *(*realloc_funccxx . . $ realloc_func.)(void *p, size_tcxx . . $ size_t# n) = realloccxx . . $ realloc(417ab160f6baadc6).; |
| 35 | static void (*free_funccxx . . $ free_func.)(void*) = freecxx . . $ free(5d22bdacc48458e8).; |
| 36 | |
| 37 | |
| 38 | |
| 39 | void |
| 40 | assuan_set_malloc_hookscxx . . $ assuan_set_malloc_hooks(dc9a7692af199bc0). ( void *(*new_alloc_func)(size_tcxx . . $ size_t# n), |
| 41 | void *(*new_realloc_func)(void *p, size_tcxx . . $ size_t# n), |
| 42 | void (*new_free_func)(void*) ) |
| 43 | { |
| 44 | alloc_funccxx . . $ alloc_func. = new_alloc_func; |
| 45 | realloc_funccxx . . $ realloc_func. = new_realloc_func; |
| 46 | free_funccxx . . $ free_func. = new_free_func; |
| 47 | } |
| 48 | |
| 49 | void * |
| 50 | _assuan_malloccxx . . $ _assuan_malloc(bfcfa4d6b7f7ef64). (size_tcxx . . $ size_t# n) |
| 51 | { |
| 52 | return alloc_funccxx . . $ alloc_func. (n); |
| 53 | } |
| 54 | |
| 55 | void * |
| 56 | _assuan_realloccxx . . $ _assuan_realloc(417ab160f6baadc6). (void *a, size_tcxx . . $ size_t# n) |
| 57 | { |
| 58 | return realloc_funccxx . . $ realloc_func. (a, n); |
| 59 | } |
| 60 | |
| 61 | void * |
| 62 | _assuan_calloccxx . . $ _assuan_calloc(63a7bf2941bfcd7a). (size_tcxx . . $ size_t# n, size_tcxx . . $ size_t# m) |
| 63 | { |
| 64 | void *p = _assuan_malloccxx . . $ _assuan_malloc(bfcfa4d6b7f7ef64). (n*m); |
| 65 | if (p) |
| 66 | memsetcxx . . $ memset(d1de1e4fb215f1c6). (p, 0, n* m); |
| 67 | return p; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | _assuan_freecxx . . $ _assuan_free(5d22bdacc48458e8). (void *p) |
| 72 | { |
| 73 | if (p) |
| 74 | free_funccxx . . $ 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_errorcxx . . $ assuan_set_error(40f5040e2767dd06). (ASSUAN_CONTEXTcxx . . $ ASSUAN_CONTEXT# ctx, int err, const char *text) |
| 84 | { |
| 85 | ctx->err_nocxx . . $ assuan_context_s#err_no. = err; |
| 86 | ctx->err_strcxx . . $ assuan_context_s#err_str. = text; |
| 87 | return err; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | assuan_set_pointercxx . . $ assuan_set_pointer(912dca11611d8d56). (ASSUAN_CONTEXTcxx . . $ ASSUAN_CONTEXT# ctx, void *pointer) |
| 92 | { |
| 93 | if (ctx) |
| 94 | ctx->user_pointercxx . . $ assuan_context_s#user_pointer. = pointer; |
| 95 | } |
| 96 | |
| 97 | void * |
| 98 | assuan_get_pointercxx . . $ assuan_get_pointer(6d659d4ae2bb0aae). (ASSUAN_CONTEXTcxx . . $ ASSUAN_CONTEXT# ctx) |
| 99 | { |
| 100 | return ctx? ctx->user_pointercxx . . $ assuan_context_s#user_pointer. : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | void |
| 105 | assuan_set_log_streamcxx . . $ assuan_set_log_stream(5f9012af00acff80). (ASSUAN_CONTEXTcxx . . $ ASSUAN_CONTEXT# ctx, FILEcxx . . $ FILE# *fp) |
| 106 | { |
| 107 | if (ctx) |
| 108 | { |
| 109 | if (ctx->log_fpcxx . . $ assuan_context_s#log_fp.) |
| 110 | fflushcxx . . $ fflush(89bc1efcdac0ca22). (ctx->log_fpcxx . . $ assuan_context_s#log_fp.); |
| 111 | ctx->log_fpcxx . . $ assuan_context_s#log_fp. = fp; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | void |
| 117 | assuan_begin_confidentialcxx . . $ assuan_begin_confidential(746ddb6f27ff0333). (ASSUAN_CONTEXTcxx . . $ ASSUAN_CONTEXT# ctx) |
| 118 | { |
| 119 | if (ctx) |
| 120 | { |
| 121 | ctx->confidentialcxx . . $ assuan_context_s#confidential. = 1; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | assuan_end_confidentialcxx . . $ assuan_end_confidential(746ddb6f27ff0333). (ASSUAN_CONTEXTcxx . . $ ASSUAN_CONTEXT# ctx) |
| 127 | { |
| 128 | if (ctx) |
| 129 | { |
| 130 | ctx->confidentialcxx . . $ assuan_context_s#confidential. = 0; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | _assuan_log_print_buffercxx . . $ _assuan_log_print_buffer(4a34f311aa103ecc). (FILEcxx . . $ FILE# *fp, const void *buffer, size_tcxx . . $ 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 | fwritecxx . . $ fwrite(ab0609fa07c47d6e). (buffer, length, 1, fp); |
| 148 | else |
| 149 | { |
| 150 | putccxx . . $ putc(b1cd03d6c470d1bd). ('[', fp); |
| 151 | for (n=0; n < length; n++, s++) |
| 152 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (fp, " %02x", *s); |
| 153 | putccxx . . $ putc(b1cd03d6c470d1bd). (' ', fp); |
| 154 | putccxx . . $ putc(b1cd03d6c470d1bd). (']', fp); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /* print a user supplied string after filtering out potential bad |
| 160 | characters*/ |
| 161 | void |
| 162 | _assuan_log_sanitized_stringcxx . . $ _assuan_log_sanitized_string(85c52e162fed56f9). (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 | FILEcxx . . $ FILE# *fp = stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!; |
| 169 | #endif |
| 170 | |
| 171 | for (; *s; s++) |
| 172 | { |
| 173 | if (*s < 0x20 || (*s >= 0x7f && *s <= 0xa0)) |
| 174 | { |
| 175 | putccxx . . $ putc(b1cd03d6c470d1bd). ('\\', fp); |
| 176 | if (*s == '\n') |
| 177 | putccxx . . $ putc(b1cd03d6c470d1bd). ('n', fp); |
| 178 | else if (*s == '\r') |
| 179 | putccxx . . $ putc(b1cd03d6c470d1bd). ('r', fp); |
| 180 | else if (*s == '\f') |
| 181 | putccxx . . $ putc(b1cd03d6c470d1bd). ('f', fp); |
| 182 | else if (*s == '\v') |
| 183 | putccxx . . $ putc(b1cd03d6c470d1bd). ('v', fp); |
| 184 | else if (*s == '\b') |
| 185 | putccxx . . $ putc(b1cd03d6c470d1bd). ('b', fp); |
| 186 | else if (!*s) |
| 187 | putccxx . . $ putc(b1cd03d6c470d1bd). ('0', fp); |
| 188 | else |
| 189 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (fp, "x%02x", *s ); |
| 190 | } |
| 191 | else |
| 192 | putccxx . . $ putc(b1cd03d6c470d1bd). (*s, fp); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 |