From e3e7a535f4370c4cd45cb9f820cb858eef349e80 Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Wed, 25 Sep 2013 00:33:29 +0200 Subject: [PATCH] add strbuff wrapper for base64 encoding --- libcitadel/lib/libcitadel.h | 1 + libcitadel/lib/stringbuf.c | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index af6e16c17..062ac3520 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -352,6 +352,7 @@ void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn); void StrBufUrlescUPAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn); void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn); void StrBufHexEscAppend(StrBuf *OutBuf, const StrBuf *In, const unsigned char *PlainIn, long PlainInLen); +void StrBufBase64Append(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn, long PlainInLen, int linebreaks); long StrEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks); long StrECMAEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn); long StrHtmlEcmaEscAppend(StrBuf *Target, const StrBuf *Source, const char *PlainIn, int nbsp, int nolinebreaks); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 1b9ea0723..4cee2be32 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -1985,6 +1985,45 @@ void StrBufHexEscAppend(StrBuf *OutBuf, const StrBuf *In, const unsigned char *P *pt = '\0'; } +void StrBufBase64Append(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn, long PlainInLen, int linebreaks) +{ + const char *pch; + char *pt; + int len; + long ExpectLen; + + if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) ) + return; + if (PlainIn != NULL) { + if (PlainInLen < 0) + len = strlen(PlainIn); + else + len = PlainInLen; + pch = PlainIn; + } + else { + pch = In->buf; + len = In->BufUsed; + } + + if (len == 0) + return; + + ExpectLen = ((len * 134) / 100) + OutBuf->BufUsed; + + if (ExpectLen > OutBuf->BufSize) + if (IncreaseBuf(OutBuf, 1, ExpectLen) < ExpectLen) + return; + + pt = OutBuf->buf + OutBuf->BufUsed; + + len = CtdlEncodeBase64(pt, pch, len, linebreaks); + + pt += len; + OutBuf->BufUsed += len; + *pt = '\0'; +} + /** * @ingroup StrBuf_DeEnCoder * @brief append a string in hex encoding to the buffer -- 2.30.2