diff options
author | Werner Koch <[email protected]> | 2016-08-10 17:04:43 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2016-08-10 17:53:53 +0000 |
commit | f2ea7e539c9a22081e3159dcbca84f57f30724ca (patch) | |
tree | 4d7abfc34b04735580b231f53fd1db48ce6e531b /common/miscellaneous.c | |
parent | tests: Fix distcheck. (diff) | |
download | gnupg-f2ea7e539c9a22081e3159dcbca84f57f30724ca.tar.gz gnupg-f2ea7e539c9a22081e3159dcbca84f57f30724ca.zip |
common: New function try_make_printable_string.
* common/stringhelp.c (sanitize_buffer): Remove. Move code to ...
* common/miscellaneous.c (try_make_printable_string): new.
(make_printable_string): Call try_make_printable_string.
Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'common/miscellaneous.c')
-rw-r--r-- | common/miscellaneous.c | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/common/miscellaneous.c b/common/miscellaneous.c index 8d9a7aa73..1327649c8 100644 --- a/common/miscellaneous.c +++ b/common/miscellaneous.c @@ -246,13 +246,77 @@ print_hexstring (FILE *fp, const void *buffer, size_t length, int reserved) #undef tohex } + +/* Create a string from the buffer P_ARG of length N which is suitable + * for printing. Caller must release the created string using xfree. + * On error ERRNO is set and NULL returned. Errors are only possible + * due to malloc failure. */ char * -make_printable_string (const void *p, size_t n, int delim ) +try_make_printable_string (const void *p_arg, size_t n, int delim) { - return sanitize_buffer (p, n, delim); + const unsigned char *p = p_arg; + size_t save_n, buflen; + const unsigned char *save_p; + char *buffer, *d; + + /* First count length. */ + for (save_n = n, save_p = p, buflen=1 ; n; n--, p++ ) + { + if ( *p < 0x20 || *p == 0x7f || *p == delim || (delim && *p=='\\')) + { + if ( *p=='\n' || *p=='\r' || *p=='\f' + || *p=='\v' || *p=='\b' || !*p ) + buflen += 2; + else + buflen += 5; + } + else + buflen++; + } + p = save_p; + n = save_n; + /* And now make the string */ + d = buffer = xtrymalloc (buflen); + for ( ; n; n--, p++ ) + { + if (*p < 0x20 || *p == 0x7f || *p == delim || (delim && *p=='\\')) { + *d++ = '\\'; + if( *p == '\n' ) + *d++ = 'n'; + else if( *p == '\r' ) + *d++ = 'r'; + else if( *p == '\f' ) + *d++ = 'f'; + else if( *p == '\v' ) + *d++ = 'v'; + else if( *p == '\b' ) + *d++ = 'b'; + else if( !*p ) + *d++ = '0'; + else { + sprintf(d, "x%02x", *p ); + d += 3; + } + } + else + *d++ = *p; + } + *d = 0; + return buffer; } +/* Same as try_make_printable_string but terminates the process on + * memory shortage. */ +char * +make_printable_string (const void *p, size_t n, int delim ) +{ + char *string = try_make_printable_string (p, n, delim); + if (!string) + xoutofcore (); + return string; +} + /* * Check if the file is compressed. |