mirror of
git://git.gnupg.org/gnupg.git
synced 2025-05-24 16:43:28 +02:00
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 <wk@gnupg.org>
This commit is contained in:
parent
a6acf1f6b3
commit
f2ea7e539c
@ -246,14 +246,78 @@ print_hexstring (FILE *fp, const void *buffer, size_t length, int reserved)
|
|||||||
#undef tohex
|
#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 *
|
||||||
|
try_make_printable_string (const void *p_arg, size_t n, int 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 *
|
char *
|
||||||
make_printable_string (const void *p, size_t n, int delim )
|
make_printable_string (const void *p, size_t n, int delim )
|
||||||
{
|
{
|
||||||
return sanitize_buffer (p, n, delim);
|
char *string = try_make_printable_string (p, n, delim);
|
||||||
|
if (!string)
|
||||||
|
xoutofcore ();
|
||||||
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check if the file is compressed.
|
* Check if the file is compressed.
|
||||||
*/
|
*/
|
||||||
|
@ -687,65 +687,6 @@ hextobyte (const char *s)
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Create a string from the buffer P_ARG of length N which is suitable
|
|
||||||
for printing. Caller must release the created string using xfree.
|
|
||||||
This function terminates the process on memory shortage. */
|
|
||||||
char *
|
|
||||||
sanitize_buffer (const void *p_arg, size_t n, int 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 = xmalloc( 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Given a string containing an UTF-8 encoded text, return the number
|
/* Given a string containing an UTF-8 encoded text, return the number
|
||||||
of characters in this string. It differs from strlen in that it
|
of characters in this string. It differs from strlen in that it
|
||||||
only counts complete UTF-8 characters. SIZE is the maximum length
|
only counts complete UTF-8 characters. SIZE is the maximum length
|
||||||
|
@ -61,9 +61,6 @@ int compare_filenames( const char *a, const char *b );
|
|||||||
|
|
||||||
int hextobyte (const char *s);
|
int hextobyte (const char *s);
|
||||||
|
|
||||||
char *sanitize_buffer (const void *p, size_t n, int delim);
|
|
||||||
|
|
||||||
|
|
||||||
size_t utf8_charcount (const char *s, int len);
|
size_t utf8_charcount (const char *s, int len);
|
||||||
|
|
||||||
|
|
||||||
|
@ -287,6 +287,7 @@ void print_utf8_buffer2 (estream_t fp, const void *p, size_t n, int delim);
|
|||||||
void print_utf8_buffer (estream_t fp, const void *p, size_t n);
|
void print_utf8_buffer (estream_t fp, const void *p, size_t n);
|
||||||
void print_hexstring (FILE *fp, const void *buffer, size_t length,
|
void print_hexstring (FILE *fp, const void *buffer, size_t length,
|
||||||
int reserved);
|
int reserved);
|
||||||
|
char *try_make_printable_string (const void *p, size_t n, int delim);
|
||||||
char *make_printable_string (const void *p, size_t n, int delim);
|
char *make_printable_string (const void *p, size_t n, int delim);
|
||||||
|
|
||||||
int is_file_compressed (const char *s, int *ret_rc);
|
int is_file_compressed (const char *s, int *ret_rc);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user