aboutsummaryrefslogtreecommitdiffstats
path: root/sdump.c
diff options
context:
space:
mode:
authorMatthias Andree <matthias.andree@gmx.de>2009-08-05 16:27:47 +0000
committerMatthias Andree <matthias.andree@gmx.de>2009-08-05 16:27:47 +0000
commitc8f608f4c4c6d2eb6cb7b12cb60de04cadcb3750 (patch)
tree99b131162e6c7b1d87ea295ed09b1fbeb6ffd66f /sdump.c
parent62acd57d67fff935e1c8a1796853e911869ee9f8 (diff)
downloadfetchmail-c8f608f4c4c6d2eb6cb7b12cb60de04cadcb3750.tar.gz
fetchmail-c8f608f4c4c6d2eb6cb7b12cb60de04cadcb3750.tar.bz2
fetchmail-c8f608f4c4c6d2eb6cb7b12cb60de04cadcb3750.zip
Add sdump(), split xmalloc.h out of fetchmail.h
svn path=/branches/BRANCH_6-3/; revision=5390
Diffstat (limited to 'sdump.c')
-rw-r--r--sdump.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/sdump.c b/sdump.c
new file mode 100644
index 00000000..5782f134
--- /dev/null
+++ b/sdump.c
@@ -0,0 +1,37 @@
+/* sdump.c -- library to allocate a printable version of a string */
+/** \file sdump.c
+ * \author Matthias Andree
+ * \date 2009
+ */
+
+#include <ctype.h> /* for isprint() */
+#include <stdio.h> /* for sprintf() */
+#include <stdlib.h> /* for size_t */
+#include "xmalloc.h" /* for xmalloc() */
+
+#include "sdump.h" /* for prototype */
+
+/** sdump converts a byte string \a in of size \a len into a printable
+ * string and returns a pointer to the memory region.
+ * \returns a pointer to a xmalloc()ed string that the caller must
+ * free() after use. This function causes program abort on failure. */
+char *sdump(const char *in, size_t len)
+{
+ size_t outlen = 0, i;
+ char *out, *oi;
+
+ for (i = 0; i < len; i++) {
+ outlen += isprint((unsigned char)in[i]) ? 1 : 4;
+ }
+
+ oi = out = (char *)xmalloc(outlen + 1);
+ for (i = 0; i < len; i++) {
+ if (isprint((unsigned char)in[i])) {
+ *(oi++) = in[i];
+ } else {
+ oi += sprintf(oi, "\\x%02X", in[i]);
+ }
+ }
+ *oi = '\0';
+ return out;
+}