diff options
-rw-r--r-- | Makefile.am | 3 | ||||
-rw-r--r-- | fetchmail.h | 11 | ||||
-rw-r--r-- | sdump.c | 37 | ||||
-rw-r--r-- | sdump.h | 8 | ||||
-rw-r--r-- | xmalloc.h | 19 |
5 files changed, 67 insertions, 11 deletions
diff --git a/Makefile.am b/Makefile.am index db6fb731..f8eea5d0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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) diff --git a/fetchmail.h b/fetchmail.h index afbf0d72..702fcae5 100644 --- a/fetchmail.h +++ b/fetchmail.h @@ -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 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; +} diff --git a/sdump.h b/sdump.h new file mode 100644 index 00000000..478b9720 --- /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 index 00000000..e1865013 --- /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 |