summaryrefslogtreecommitdiff
path: root/print_utils.h
blob: 519cd4017c93e762eeb0f980004b3164cba8285e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef STRACE_PRINT_UTILS_H
# define STRACE_PRINT_UTILS_H

# include <inttypes.h>

/* Hexadecimal output utils */

static const char hex_chars[16] = "0123456789abcdef";

/**
 * Character array representing hexadecimal encoding of a character value.
 *
 * @param b_ Byte to provide representation for.
 */
# define BYTE_HEX_CHARS(b_) \
	hex_chars[((uint8_t) (b_)) >> 4], hex_chars[((uint8_t) (b_)) & 0xf]
# define BYTE_HEX_CHARS_PRINTF(b_) \
	'\\', 'x', BYTE_HEX_CHARS(b_)
# define BYTE_HEX_CHARS_PRINTF_QUOTED(b_) \
	'\'', BYTE_HEX_CHARS_PRINTF(b_), '\''

static inline char *
sprint_byte_hex(char *buf, uint8_t val)
{
	*buf++ = hex_chars[val >> 4];
	*buf++ = hex_chars[val & 0xf];

	return buf;
}

/* Character classification utils */

static inline bool
is_print(uint8_t c)
{
	return (c >= ' ') && (c < 0x7f);
}

#endif /* STRACE_PRINT_UTILS_H */