BASE64: add stream handler for base 64 decoding - make shure its not called for to...
authorWilfried Goesgens <dothebart@citadel.org>
Mon, 6 Jan 2014 23:38:27 +0000 (00:38 +0100)
committerWilfried Goesgens <dothebart@citadel.org>
Mon, 6 Jan 2014 23:38:27 +0000 (00:38 +0100)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c

index a1aacb5fb2aa965b8d28873420e886ff46c107df..7dd18e64cb351f465aaa9499a8db2ec41a8f1dcc 100644 (file)
@@ -338,6 +338,14 @@ void StrBuf_RFC822_2_Utf8(StrBuf *Target,
 /* deprecated old version: */
 void StrBuf_RFC822_to_Utf8(StrBuf *Target, const StrBuf *DecodeMe, const StrBuf* DefaultCharset, StrBuf *FoundCharset);
 
+typedef enum __eStreamType {
+       eBase64Decode
+} eStreamType;
+
+void *StrBufNewStreamContext(eStreamType type);
+void StrBufDestroyStreamContext(eStreamType type, void **Stream);
+void StrBufStreamDecodeTo(StrBuf *Target, const StrBuf *In, const char* pIn, long pInLen, void *Stream);
+
 int StrBufDecodeBase64(StrBuf *Buf);
 int StrBufDecodeBase64To(const StrBuf *BufIn, StrBuf *BufOut);
 int StrBufDecodeHex(StrBuf *Buf);
index 547037751961c011899f9b364d74a463ae829637..5e89ea2f19b48f8c38edd1b5835f3754109f3379 100644 (file)
@@ -32,6 +32,9 @@
 
 #include "libcitadel.h"
 
+#include "b64/cencode.h"
+#include "b64/cdecode.h"
+
 #ifdef HAVE_ICONV
 #include <iconv.h>
 #endif
@@ -2843,6 +2846,56 @@ int StrBufDecodeBase64To(const StrBuf *BufIn, StrBuf *BufOut)
        return BufOut->BufUsed;
 }
 
+void *StrBufNewStreamContext(eStreamType type)
+{
+       base64_decodestate *state;;
+
+       switch (type)
+       {
+       case eBase64Decode:
+               state = (base64_decodestate*) malloc(sizeof(base64_decodestate));
+               base64_init_decodestate(state);
+               return state;
+               break;
+       }
+       return NULL;
+}
+
+void StrBufDestroyStreamContext(eStreamType type, void **Stream)
+{
+       switch (type)
+       {
+       case eBase64Decode:
+               free(*Stream);
+               *Stream = NULL;
+               break;
+       }
+}
+void StrBufStreamDecodeTo(StrBuf *Target, const StrBuf *In, const char* pIn, long pInLen, void *Stream)
+{
+       base64_decodestate *state = Stream;
+       long ExpectLen;
+
+       if (In != NULL)
+       {
+               pIn = In->buf;
+               pInLen = In->BufUsed;
+       }
+       if ((pIn == NULL) || (Stream == NULL))
+               return;
+       
+       ExpectLen = (pInLen / 4) * 3;
+
+       if (Target->BufSize - Target->BufUsed < ExpectLen)
+       {
+               IncreaseBuf(Target, 1, Target->BufUsed + ExpectLen + 1);
+       }
+
+       ExpectLen = base64_decode_block(pIn, pInLen, Target->buf + Target->BufUsed, state);
+       Target->BufUsed += ExpectLen;
+       Target->buf[Target->BufUsed] = '\0';
+}
+
 /**
  * @ingroup StrBuf_DeEnCoder
  * @brief decode a buffer from base 64 encoding; destroys original