diff options
author | Neal Walfield <neal@walfield.org> | 2002-10-31 16:52:21 +0000 |
---|---|---|
committer | Neal Walfield <neal@walfield.org> | 2002-10-31 16:52:21 +0000 |
commit | e638b3342a80ce6a01e44b2c47e51c51bb42e5c5 (patch) | |
tree | 5874e8019e3b5bf280a4fcd8e76d528b98af4ee2 | |
parent | 94ed73759229876b70718836b2ce88475fc14d4a (diff) | |
download | libassuan-e638b3342a80ce6a01e44b2c47e51c51bb42e5c5.tar.gz |
2002-10-31 Neal H. Walfield <neal@g10code.de>
* assuan-util.c: Include <ctype.h>.
(_assuan_log_print_buffer): Elide the magic numbers preferring the
standard isfoo functions. Use putc_unlocked where possible.
(_assuan_log_sanitized_string): Rewrite to use putc_unlocked and
the isfoo functions.
-rw-r--r-- | src/ChangeLog | 8 | ||||
-rw-r--r-- | src/assuan-util.c | 94 |
2 files changed, 74 insertions, 28 deletions
diff --git a/src/ChangeLog b/src/ChangeLog index 7fe3c40..552a9c8 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2002-10-31 Neal H. Walfield <neal@g10code.de> + + * assuan-util.c: Include <ctype.h>. + (_assuan_log_print_buffer): Elide the magic numbers preferring the + standard isfoo functions. Use putc_unlocked where possible. + (_assuan_log_sanitized_string): Rewrite to use putc_unlocked and + the isfoo functions. + 2002-09-05 Neal H. Walfield <neal@g10code.de> * assuan-defs.h (_assuan_read_wrapper): Depreciated. diff --git a/src/assuan-util.c b/src/assuan-util.c index 24a9799..76f7f06 100644 --- a/src/assuan-util.c +++ b/src/assuan-util.c @@ -22,6 +22,7 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <ctype.h> #include "assuan-defs.h" @@ -127,6 +128,8 @@ assuan_end_confidential (ASSUAN_CONTEXT ctx) } } +/* Dump a possibly binary string (used for debugging). Distinguish + ascii text from binary and print it accordingly. */ void _assuan_log_print_buffer (FILE *fp, const void *buffer, size_t length) { @@ -134,26 +137,31 @@ _assuan_log_print_buffer (FILE *fp, const void *buffer, size_t length) int n; for (n=length,s=buffer; n; n--, s++) - { - if (*s < ' ' || (*s >= 0x7f && *s <= 0xa0)) - break; - } + if (!isascii (*s) || iscntrl (*s) || !isprint (*s)) + break; + s = buffer; if (!n && *s != '[') fwrite (buffer, length, 1, fp); else { - putc ('[', fp); +#ifdef HAVE_FLOCKFILE + flockfile (fp); +#endif + putc_unlocked ('[', fp); for (n=0; n < length; n++, s++) fprintf (fp, " %02x", *s); - putc (' ', fp); - putc (']', fp); + putc_unlocked (' ', fp); + putc_unlocked (']', fp); +#ifdef HAVE_FUNLOCKFILE + funlockfile (fp); +#endif } } -/* print a user supplied string after filtering out potential bad - characters*/ +/* Log a user supplied string. Escapes non-printable before + printing. */ void _assuan_log_sanitized_string (const char *string) { @@ -164,29 +172,59 @@ _assuan_log_sanitized_string (const char *string) FILE *fp = stderr; #endif + if (! *s) + return; + +#ifdef HAVE_FLOCKFILE + flockfile (fp); +#endif + for (; *s; s++) { - if (*s < 0x20 || (*s >= 0x7f && *s <= 0xa0)) - { - putc ('\\', fp); - if (*s == '\n') - putc ('n', fp); - else if (*s == '\r') - putc ('r', fp); - else if (*s == '\f') - putc ('f', fp); - else if (*s == '\v') - putc ('v', fp); - else if (*s == '\b') - putc ('b', fp); - else if (!*s) - putc ('0', fp); - else - fprintf (fp, "x%02x", *s ); + int c = 0; + + switch (*s) + { + case '\r': + c = 'r'; + break; + + case '\n': + c = 'n'; + break; + + case '\f': + c = 'f'; + break; + + case '\v': + c = 'v'; + break; + + case '\b': + c = 'b'; + break; + + default: + if (isascii (*s) && isprint (*s)) + putc_unlocked (*s, fp); + else + { + putc_unlocked ('\\', fp); + fprintf (fp, "x%02x", *s); + } + } + + if (c) + { + putc_unlocked ('\\', fp); + putc_unlocked (c, fp); } - else - putc (*s, fp); } + +#ifdef HAVE_FUNLOCKFILE + funlockfile (fp); +#endif } |