1 /* sdump.c -- library to allocate a printable version of a string */
3 * \author Matthias Andree
7 #include <ctype.h> /* for isprint() */
8 #include <stdio.h> /* for sprintf() */
9 #include <stdlib.h> /* for size_t */
10 #include "xmalloc.h" /* for xmalloc() */
12 #include "sdump.h" /* for prototype */
14 /** sdump converts a byte string \a in of size \a len into a printable
15 * string and returns a pointer to the memory region.
16 * \returns a pointer to a xmalloc()ed string that the caller must
17 * free() after use. This function causes program abort on failure. */
18 char *sdump(const char *in, size_t len)
23 for (i = 0; i < len; i++) {
24 outlen += isprint((unsigned char)in[i]) ? 1 : 4;
27 oi = out = (char *)xmalloc(outlen + 1);
28 for (i = 0; i < len; i++) {
29 if (isprint((unsigned char)in[i])) {
32 oi += sprintf(oi, "\\x%02X", in[i]);