Handle situation in where we have multiple buffers to send at the end.
authorWilfried Goesgens <dothebart@citadel.org>
Sun, 18 Jan 2015 11:40:42 +0000 (12:40 +0100)
committerWilfried Goesgens <dothebart@citadel.org>
Sun, 18 Jan 2015 11:40:42 +0000 (12:40 +0100)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c
libcitadel/tests/stringbuf_stream.c

index 0ee3df9..cba677a 100644 (file)
@@ -348,7 +348,7 @@ typedef enum __eStreamType {
 
 void *StrBufNewStreamContext(eStreamType type);
 void StrBufDestroyStreamContext(eStreamType type, void **Stream);
-void StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, void *Stream, int LastChunk);
+int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, void *Stream, int LastChunk);
 
 int StrBufDecodeBase64(StrBuf *Buf);
 int StrBufDecodeBase64To(const StrBuf *BufIn, StrBuf *BufOut);
index 9ebc2dd..7b2c663 100644 (file)
@@ -2884,8 +2884,10 @@ void *StrBufNewStreamContext(eStreamType type)
 
                err = inflateInit(&stream->zstream);
 
-               if (err != Z_OK)
-                       return NULL;/// tODO cleanup
+               if (err != Z_OK) {
+                       StrBufDestroyStreamContext(type, (void**)&stream);
+                       return NULL;
+               }
                return stream;
 
        }
@@ -2913,8 +2915,10 @@ void *StrBufNewStreamContext(eStreamType type)
                                   -MAX_WBITS,
                                   DEF_MEM_LEVEL,
                                   Z_DEFAULT_STRATEGY);
-               if (err != Z_OK)
-                       return NULL;/// tODO cleanup
+               if (err != Z_OK) {
+                       StrBufDestroyStreamContext(type, (void**) &stream);
+                       return NULL;
+               }
                return stream;
        }
        case eEmtyCodec:
@@ -2927,6 +2931,9 @@ void *StrBufNewStreamContext(eStreamType type)
 
 void StrBufDestroyStreamContext(eStreamType type, void **vStream)
 {
+       if (*vStream) {
+               return;
+       }
        switch (type)
        {
        case eBase64Encode:
@@ -2955,7 +2962,7 @@ void StrBufDestroyStreamContext(eStreamType type, void **vStream)
        }
 }
 
-void StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, void *vStream, int LastChunk)
+int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, void *vStream, int LastChunk)
 {
 
        switch (type)
@@ -3067,6 +3074,7 @@ void StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, con
                                        (In->Buf->BufUsed - stream->zstream.avail_in);
                        }
                }
+               return (LastChunk && (err != Z_FINISH));
 
        }
        break;
@@ -3140,6 +3148,7 @@ void StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, con
        }
                break; /// TODO
        }
+       return 0;
 }
 
 /**
index 6ea03a7..1c4a52e 100644 (file)
@@ -70,6 +70,7 @@ static void StreamEncode(void)
        IOBuffer ReadBuffer;
        IOBuffer WriteBuffer;
        int err;
+       int ret = 0;
        int done = 0;
        void *vStream;
        
@@ -102,21 +103,23 @@ static void StreamEncode(void)
 
                done = StrBuf_read_one_chunk_callback(fdin,
                                                      0,
-                                                     &ReadBuffer) < SIZ * 4;
+                                                     &ReadBuffer) < (SIZ * 4) -1 ;
                if (IOBufferStrLength(&ReadBuffer) == 0)
                {
                        done = 1;
                }
                do
                {
-                       StrBufStreamTranscode(ST, &WriteBuffer, &ReadBuffer, NULL, -1, vStream, done);
-
-                       while (IOBufferStrLength(&WriteBuffer) > 0)
-                       {
-                               err = StrBuf_write_one_chunk_callback(fdout,
-                                                                     0,
-                                                                     &WriteBuffer);
-                       }
+                       do {
+                               ret = StrBufStreamTranscode(ST, &WriteBuffer, &ReadBuffer, NULL, -1, vStream, done);
+                               
+                               while (IOBufferStrLength(&WriteBuffer) > 0)
+                               {
+                                       err = StrBuf_write_one_chunk_callback(fdout,
+                                                                             0,
+                                                                             &WriteBuffer);
+                               }
+                       } while (ret > 0);
                } while (IOBufferStrLength(&ReadBuffer) > 0);
        }