util: add support for forcing printing string as hex-escaped

This could be useful in cases when some binary data should not be
interpreted as an ASCII string, but rather as an array of byte values.

* defs.h (QUOTE_FORCE_HEX): New macro constant.
* util.c (quote_string): Enable use_hex when QUOTE_FORCE_HEX is set
in user_style parameter.
This commit is contained in:
Eugene Syromyatnikov 2017-01-06 09:55:30 +01:00 committed by Dmitry V. Levin
parent a8f0442fd7
commit 34a920baad
2 changed files with 3 additions and 2 deletions

1
defs.h
View File

@ -505,6 +505,7 @@ extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_b
#define QUOTE_0_TERMINATED 0x01
#define QUOTE_OMIT_LEADING_TRAILING_QUOTES 0x02
#define QUOTE_OMIT_TRAILING_0 0x08
#define QUOTE_FORCE_HEX 0x10
extern int string_quote(const char *, char *, unsigned int, unsigned int);
extern int print_quoted_string(const char *, unsigned int, unsigned int);

4
util.c
View File

@ -654,9 +654,9 @@ string_quote(const char *instr, char *outstr, const unsigned int size,
eol = 0x100; /* this can never match a char */
usehex = 0;
if (xflag > 1)
if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) {
usehex = 1;
else if (xflag) {
} else if (xflag) {
/* Check for presence of symbol which require
to hex-quote the whole string. */
for (i = 0; i < size; ++i) {