]> Pileus Git - ~andy/fetchmail/commitdiff
Add sdump(), split xmalloc.h out of fetchmail.h
authorMatthias Andree <matthias.andree@gmx.de>
Wed, 5 Aug 2009 16:27:47 +0000 (16:27 -0000)
committerMatthias Andree <matthias.andree@gmx.de>
Wed, 5 Aug 2009 16:27:47 +0000 (16:27 -0000)
svn path=/branches/BRANCH_6-3/; revision=5390

Makefile.am
fetchmail.h
sdump.c [new file with mode: 0644]
sdump.h [new file with mode: 0644]
xmalloc.h [new file with mode: 0644]

index db6fb731b2d83f5aba549972e13459f9a3bf0c63..f8eea5d0095b4ea35d66af3afa5b06d9215597fe 100644 (file)
@@ -37,7 +37,8 @@ libfm_a_SOURCES=      xmalloc.c base64.c rfc822.c report.c rfc2047e.c \
                        servport.c ntlm.h smbbyteorder.h smbdes.h smbmd4.h \
                        smbencrypt.h smbdes.c smbencrypt.c smbmd4.c smbutil.c \
                        libesmtp/gethostbyname.h libesmtp/gethostbyname.c \
-                       smbtypes.h fm_getaddrinfo.c tls.c rfc822valid.c
+                       smbtypes.h fm_getaddrinfo.c tls.c rfc822valid.c \
+                       xmalloc.h sdump.h sdump.c
 libfm_a_LIBADD=                $(EXTRAOBJ)
 libfm_a_DEPENDENCIES=  $(EXTRAOBJ)
 LDADD  =               libfm.a @LIBINTL@ $(LIBOBJS)
index afbf0d72ca47893ccabd56efe4a35c19627365ce..702fcae5e2eaf935e2154e4b964dc69db678487f 100644 (file)
@@ -623,16 +623,7 @@ void interface_parse(char *, struct hostdata *);
 void interface_note_activity(struct hostdata *);
 int interface_approve(struct hostdata *, flag domonitor);
 
-/* xmalloc.c */
-#if defined(HAVE_VOIDPOINTER)
-#define XMALLOCTYPE void
-#else
-#define XMALLOCTYPE char
-#endif
-XMALLOCTYPE *xmalloc(size_t);
-XMALLOCTYPE *xrealloc(/*@null@*/ XMALLOCTYPE *, size_t);
-#define xfree(p) { if (p) { free(p); } (p) = 0; }
-char *xstrdup(const char *);
+#include "xmalloc.h"
 
 /* protocol driver and methods */
 int doPOP2 (struct query *); 
diff --git a/sdump.c b/sdump.c
new file mode 100644 (file)
index 0000000..5782f13
--- /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;
+}
diff --git a/sdump.h b/sdump.h
new file mode 100644 (file)
index 0000000..478b972
--- /dev/null
+++ b/sdump.h
@@ -0,0 +1,8 @@
+#ifndef SDUMP_H
+#define SDUMP_H
+
+#include <stdlib.h>
+
+char *sdump(const char *in, size_t len);
+
+#endif
diff --git a/xmalloc.h b/xmalloc.h
new file mode 100644 (file)
index 0000000..e186501
--- /dev/null
+++ b/xmalloc.h
@@ -0,0 +1,19 @@
+/* xmalloc.h -- split out of fetchmail.h */
+
+#ifndef XMALLOC_H
+#define XMALLOC_H
+
+#include "config.h"
+
+/* xmalloc.c */
+#if defined(HAVE_VOIDPOINTER)
+#define XMALLOCTYPE void
+#else
+#define XMALLOCTYPE char
+#endif
+XMALLOCTYPE *xmalloc(size_t);
+XMALLOCTYPE *xrealloc(/*@null@*/ XMALLOCTYPE *, size_t);
+#define xfree(p) { if (p) { free(p); } (p) = 0; }
+char *xstrdup(const char *);
+
+#endif