From dc08ff26ea833b68f6b5e55f24efba675b718d58 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Tue, 31 Jan 2017 22:47:57 -0500 Subject: [PATCH] New function StrBufDecodeQP() to decode a quoted-printable StrBuf in-place without reallocation --- libcitadel/configure.in | 4 ++-- libcitadel/lib/libcitadel.h | 5 +++-- libcitadel/lib/stringbuf.c | 43 ++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/libcitadel/configure.in b/libcitadel/configure.in index 2be9c363d..21c0636d0 100755 --- a/libcitadel/configure.in +++ b/libcitadel/configure.in @@ -5,7 +5,7 @@ dnl dnl Ensure that libcitadel is configured with autoconf 2.52 or newer AC_PREREQ(2.52) -AC_INIT(libcitadel, 903, http://uncensored.citadel.org) +AC_INIT(libcitadel, 904, http://uncensored.citadel.org) AC_CONFIG_SRCDIR(Makefile.in) AC_CONFIG_AUX_DIR(conftools) @@ -20,7 +20,7 @@ dnl Set LIBAGE to 0. dnl LIBCURRENT=4 -LIBREVISION=903 +LIBREVISION=904 LIBAGE=0 sinclude(conftools/libtool.m4) diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index eb0247001..87ab9f21c 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -1,7 +1,7 @@ /* * Header file for libcitadel * - * Copyright (c) 1987-2016 by the citadel.org team + * Copyright (c) 1987-2017 by the citadel.org team * * This program is open source software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3. @@ -28,7 +28,7 @@ #include #include -#define LIBCITADEL_VERSION_NUMBER 903 +#define LIBCITADEL_VERSION_NUMBER 904 /* * Here's a bunch of stupid magic to make the MIME parser portable. @@ -353,6 +353,7 @@ int StrBufDestroyStreamContext(eStreamType type, vStreamT **Stream, const char * int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, vStreamT *Stream, int LastChunk, const char **Err); int StrBufDecodeBase64(StrBuf *Buf); +void StrBufDecodeQP(StrBuf *Buf); int StrBufDecodeBase64To(const StrBuf *BufIn, StrBuf *BufOut); int StrBufDecodeHex(StrBuf *Buf); StrBuf *StrBufRFC2047encodeMessage(const StrBuf *EncodeMe); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 12fd259e4..8398757de 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1987-2013 by the citadel.org team + * Copyright (c) 1987-2017 by the citadel.org team * * This program is open source software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -5622,3 +5622,44 @@ void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash) } +/* + * Decode a quoted-printable encoded StrBuf buffer "in place" + * This is possible because the decoded will always be shorter than the encoded + * so we don't have to worry about the buffer being to small. + */ +void StrBufDecodeQP(StrBuf *Buf) +{ + if (!Buf) { // sanity check #1 + return; + } + + int source_len = StrLength(Buf); + if (source_len < 1) { // sanity check #2 + return; + } + + int spos = 0; // source position + int tpos = 0; // target position + + while (spos < source_len) { + if (!strncmp(&Buf->buf[spos], "=\r\n", 3)) { + spos += 3; + } + else if (!strncmp(&Buf->buf[spos], "=\n", 2)) { + spos += 2; + } + else if (Buf->buf[spos] == '=') { + ++spos; + int ch; + sscanf(&Buf->buf[spos], "%02x", &ch); + Buf->buf[tpos++] = ch; + spos +=2; + } + else { + Buf->buf[tpos++] = Buf->buf[spos++]; + } + } + + Buf->buf[tpos] = 0; + Buf->BufUsed = tpos; +} -- 2.30.2