| 1 | /* assuan-logging.c - Default logging function. |
| 2 | * Copyright (C) 2002, 2003, 2004, 2007 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 | #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:410:9`! |
| 21 | #include <config.h>cxx . . $ `<file>/config.h`/ |
| 22 | #endif |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <stdarg.h> |
| 27 | #ifdef HAVE_W32_SYSTEM |
| 28 | #include <windows.h> |
| 29 | #endif /*HAVE_W32_SYSTEM*/ |
| 30 | #include <errno.h> |
| 31 | #include <ctype.h> |
| 32 | |
| 33 | #include "assuan-defs.h"cxx . . $ `<file>/src/assuan/assuan-defs.h`/ |
| 34 | |
| 35 | static char prefix_buffercxx . . $ prefix_buffer.[80]; |
| 36 | static FILEcxx . . $ FILE# *_assuan_logcxx . . $ _assuan_log.; |
| 37 | static int full_loggingcxx . . $ full_logging.; |
| 38 | |
| 39 | void |
| 40 | _assuan_set_default_log_streamcxx . . $ `src/assuan/assuan.h:183:9`! (FILEcxx . . $ FILE# *fp) |
| 41 | { |
| 42 | if (!_assuan_logcxx . . $ _assuan_log.) |
| 43 | { |
| 44 | _assuan_logcxx . . $ _assuan_log. = fp; |
| 45 | full_loggingcxx . . $ full_logging. = !!getenvcxx . . $ getenv(a8b8e32347182212). ("ASSUAN_FULL_LOGGING"); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | assuan_set_assuan_log_streamcxx . . $ `src/assuan/assuan.h:141:9`! (FILEcxx . . $ FILE# *fp) |
| 51 | { |
| 52 | _assuan_logcxx . . $ _assuan_log. = fp; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* Set the per context log stream. Also enable the default log stream |
| 57 | if it has not been set. */ |
| 58 | void |
| 59 | assuan_set_log_streamcxx . . $ `src/assuan/assuan.h:131:9`! (assuan_context_tcxx . . $ assuan_context_t# ctx, FILEcxx . . $ FILE# *fp) |
| 60 | { |
| 61 | if (ctx) |
| 62 | { |
| 63 | if (ctx->log_fpcxx . . $ assuan_context_s#log_fp.) |
| 64 | fflushcxx . . $ fflush(89bc1efcdac0ca22). (ctx->log_fpcxx . . $ assuan_context_s#log_fp.); |
| 65 | ctx->log_fpcxx . . $ assuan_context_s#log_fp. = fp; |
| 66 | _assuan_set_default_log_streamcxx . . $ `src/assuan/assuan.h:183:9`! (fp); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | FILEcxx . . $ FILE# * |
| 72 | assuan_get_assuan_log_streamcxx . . $ `src/assuan/assuan.h:143:9`! (void) |
| 73 | { |
| 74 | return _assuan_logcxx . . $ _assuan_log. ? _assuan_logcxx . . $ _assuan_log. : stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* Set the prefix to be used for logging to TEXT or |
| 79 | resets it to the default if TEXT is NULL. */ |
| 80 | void |
| 81 | assuan_set_assuan_log_prefixcxx . . $ `src/assuan/assuan.h:150:9`! (const char *text) |
| 82 | { |
| 83 | if (text) |
| 84 | { |
| 85 | strncpycxx . . $ strncpy(255158a415b673b0). (prefix_buffercxx . . $ prefix_buffer., text, sizeof (prefix_buffercxx . . $ prefix_buffer.)-1); |
| 86 | prefix_buffercxx . . $ prefix_buffer.[sizeof (prefix_buffercxx . . $ prefix_buffer.)-1] = 0; |
| 87 | } |
| 88 | else |
| 89 | *prefix_buffercxx . . $ prefix_buffer. = 0; |
| 90 | } |
| 91 | |
| 92 | const char * |
| 93 | assuan_get_assuan_log_prefixcxx . . $ `src/assuan/assuan.h:145:9`! (void) |
| 94 | { |
| 95 | return prefix_buffercxx . . $ prefix_buffer.; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void |
| 100 | _assuan_log_printfcxx . . $ `src/assuan/assuan.h:182:9`! (const char *format, ...) |
| 101 | { |
| 102 | va_listcxx . . $ va_list# arg_ptr; |
| 103 | FILEcxx . . $ FILE# *fp; |
| 104 | const char *prf; |
| 105 | int save_errno = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 106 | |
| 107 | fp = assuan_get_assuan_log_streamcxx . . $ `src/assuan/assuan.h:143:9`! (); |
| 108 | prf = assuan_get_assuan_log_prefixcxx . . $ `src/assuan/assuan.h:145:9`! (); |
| 109 | if (*prf) |
| 110 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (fp, "%s[%u]: ", prf, (unsigned int)getpidcxx . . $ getpid(b126dc7c1de90089). ()); |
| 111 | |
| 112 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`! (arg_ptr, format); |
| 113 | vfprintfcxx . . $ vfprintf(bded311e54d67f9c). (fp, format, arg_ptr ); |
| 114 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`! (arg_ptr); |
| 115 | /* If the log stream is a file, the output would be buffered. This |
| 116 | is bad for debugging, thus we flush the stream if FORMAT ends |
| 117 | with a LF. */ |
| 118 | if (format && *format && format[strlencxx . . $ strlen(751346f8406fd082).(format)-1] == '\n') |
| 119 | fflushcxx . . $ fflush(89bc1efcdac0ca22). (fp); |
| 120 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = save_errno; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /* Dump a possibly binary string (used for debugging). Distinguish |
| 125 | ascii text from binary and print it accordingly. This function |
| 126 | takes FILE pointer arg because logging may be enabled on a per |
| 127 | context basis. */ |
| 128 | void |
| 129 | _assuan_log_print_buffercxx . . $ `src/assuan/assuan.h:179:9`! (FILEcxx . . $ FILE# *fp, const void *buffer, size_t length) |
| 130 | { |
| 131 | const unsigned char *s; |
| 132 | unsigned int n; |
| 133 | |
| 134 | for (n = length, s = buffer; n; n--, s++) |
| 135 | if ((! isasciicxx . . $ `<external>/17904084882347303253/ctype.h:225:11`! (*s) || iscntrlcxx . . $ `<external>/17904084882347303253/ctype.h:191:10`! (*s) || ! isprintcxx . . $ `<external>/17904084882347303253/ctype.h:195:10`! (*s)) && !(*s >= 0x80)) |
| 136 | break; |
| 137 | |
| 138 | s = buffer; |
| 139 | if (! n && *s != '[') |
| 140 | fwritecxx . . $ fwrite(ab0609fa07c47d6e). (buffer, length, 1, fp); |
| 141 | else |
| 142 | { |
| 143 | #ifdef HAVE_FLOCKFILE |
| 144 | flockfile (fp); |
| 145 | #endif |
| 146 | putc_unlockedcxx . . $ putc_unlocked(b1cd03d6c470d1bd). ('[', fp); |
| 147 | if (length > 16 && ! full_loggingcxx . . $ full_logging.) |
| 148 | { |
| 149 | for (n = 0; n < 12; n++, s++) |
| 150 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (fp, " %02x", *s); |
| 151 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (fp, " ...(%d bytes skipped)", (int) length - 12); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | for (n = 0; n < length; n++, s++) |
| 156 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (fp, " %02x", *s); |
| 157 | } |
| 158 | putc_unlockedcxx . . $ putc_unlocked(b1cd03d6c470d1bd). (' ', fp); |
| 159 | putc_unlockedcxx . . $ putc_unlocked(b1cd03d6c470d1bd). (']', fp); |
| 160 | #ifdef HAVE_FUNLOCKFILE |
| 161 | funlockfile (fp); |
| 162 | #endif |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /* Log a user supplied string. Escapes non-printable before |
| 167 | printing. */ |
| 168 | void |
| 169 | _assuan_log_sanitized_stringcxx . . $ `src/assuan/assuan.h:180:9`! (const char *string) |
| 170 | { |
| 171 | const unsigned char *s = (const unsigned char *) string; |
| 172 | FILEcxx . . $ FILE# *fp = assuan_get_assuan_log_streamcxx . . $ `src/assuan/assuan.h:143:9`! (); |
| 173 | |
| 174 | if (! *s) |
| 175 | return; |
| 176 | |
| 177 | #ifdef HAVE_FLOCKFILE |
| 178 | flockfile (fp); |
| 179 | #endif |
| 180 | |
| 181 | for (; *s; s++) |
| 182 | { |
| 183 | int c = 0; |
| 184 | |
| 185 | switch (*s) |
| 186 | { |
| 187 | case '\r': |
| 188 | c = 'r'; |
| 189 | break; |
| 190 | |
| 191 | case '\n': |
| 192 | c = 'n'; |
| 193 | break; |
| 194 | |
| 195 | case '\f': |
| 196 | c = 'f'; |
| 197 | break; |
| 198 | |
| 199 | case '\v': |
| 200 | c = 'v'; |
| 201 | break; |
| 202 | |
| 203 | case '\b': |
| 204 | c = 'b'; |
| 205 | break; |
| 206 | |
| 207 | default: |
| 208 | if ((isasciicxx . . $ `<external>/17904084882347303253/ctype.h:225:11`! (*s) && isprintcxx . . $ `<external>/17904084882347303253/ctype.h:195:10`! (*s)) || (*s >= 0x80)) |
| 209 | putc_unlockedcxx . . $ putc_unlocked(b1cd03d6c470d1bd). (*s, fp); |
| 210 | else |
| 211 | { |
| 212 | putc_unlockedcxx . . $ putc_unlocked(b1cd03d6c470d1bd). ('\\', fp); |
| 213 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (fp, "x%02x", *s); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (c) |
| 218 | { |
| 219 | putc_unlockedcxx . . $ putc_unlocked(b1cd03d6c470d1bd). ('\\', fp); |
| 220 | putc_unlockedcxx . . $ putc_unlocked(b1cd03d6c470d1bd). (c, fp); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | #ifdef HAVE_FUNLOCKFILE |
| 225 | funlockfile (fp); |
| 226 | #endif |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | #ifdef HAVE_W32_SYSTEM |
| 232 | const char * |
| 233 | _assuan_w32_strerror (int ec) |
| 234 | { |
| 235 | static char strerr[256]; |
| 236 | |
| 237 | if (ec == -1) |
| 238 | ec = (int)GetLastError (); |
| 239 | FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, ec, |
| 240 | MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 241 | strerr, sizeof (strerr)-1, NULL); |
| 242 | return strerr; |
| 243 | } |
| 244 | |
| 245 | static int (*my_strerror_r) (unsigned int err, char *buf, size_t buflen); |
| 246 | static const char * (*my_strsource) (unsigned int err); |
| 247 | |
| 248 | static int |
| 249 | load_libgpg_error (void) |
| 250 | { |
| 251 | /* This code is not race free but suitable for our purpose. */ |
| 252 | static volatile int initialized; |
| 253 | void *handle; |
| 254 | |
| 255 | if (initialized) |
| 256 | return (my_strerror_r && my_strsource)? 0:-1; |
| 257 | handle = LoadLibrary ("libgpg-error-0.dll"); |
| 258 | if (handle) |
| 259 | { |
| 260 | void *foo, *bar; |
| 261 | foo = GetProcAddress (handle, "gpg_strerror_r"); |
| 262 | bar = GetProcAddress (handle, "gpg_strsource"); |
| 263 | if (foo && bar) |
| 264 | { |
| 265 | my_strerror_r = foo; |
| 266 | my_strsource = bar; |
| 267 | } |
| 268 | else |
| 269 | CloseHandle (handle); |
| 270 | } |
| 271 | initialized = 1; |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | int |
| 276 | _assuan_gpg_strerror_r (unsigned int err, char *buf, size_t buflen) |
| 277 | { |
| 278 | if (load_libgpg_error ()) |
| 279 | return -1; |
| 280 | return my_strerror_r (err, buf, buflen); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | const char * |
| 285 | _assuan_gpg_strsource (unsigned int err) |
| 286 | { |
| 287 | if (load_libgpg_error ()) |
| 288 | return NULL; |
| 289 | return my_strsource (err); |
| 290 | } |
| 291 | #endif /*HAVE_W32_SYSTEM*/ |