Remove unneeded code found by the clang static analyzer
[citadel.git] / libcitadel / lib / stringbuf.c
index 1219401b42c9ed1b97827ed9f8c0d6f48f9d2897..b5bcf22f7a24d11d4b452700e1f5bdb45bc7335f 100644 (file)
@@ -29,9 +29,7 @@
 #include <sys/types.h>
 #define SHOW_ME_VAPPEND_PRINTF
 #include <stdarg.h>
-#ifndef LINUX_SENDFILE
-#include <sys/sendfile.h>
-#endif
+
 #include "libcitadel.h"
 
 #ifdef HAVE_ICONV
@@ -1145,7 +1143,7 @@ void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary)
        const char *pLeft;
        const char *pRight;
 
-       if (Buf == NULL)
+       if ((Buf == NULL) || (Buf->buf == NULL))
                return;
        pLeft = pBuff = Buf->buf;
        while (pBuff != NULL) {
@@ -1823,6 +1821,66 @@ void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
        *pt = '\0';
 }
 
+/** 
+ * @ingroup StrBuf_DeEnCoder
+ * @brief Escape a string for feeding out as a the username/password part of an URL while appending it to a Buffer
+ * @param OutBuf the output buffer
+ * @param In Buffer to encode
+ * @param PlainIn way in from plain old c strings
+ */
+void StrBufUrlescUPAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
+{
+       const char *pch, *pche;
+       char *pt, *pte;
+       int len;
+       
+       if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
+               return;
+       if (PlainIn != NULL) {
+               len = strlen(PlainIn);
+               pch = PlainIn;
+               pche = pch + len;
+       }
+       else {
+               pch = In->buf;
+               pche = pch + In->BufUsed;
+               len = In->BufUsed;
+       }
+
+       if (len == 0) 
+               return;
+
+       pt = OutBuf->buf + OutBuf->BufUsed;
+       pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
+
+       while (pch < pche) {
+               if (pt >= pte) {
+                       IncreaseBuf(OutBuf, 1, -1);
+                       pte = OutBuf->buf + OutBuf->BufSize - 4; /**< we max append 3 chars at once plus the \0 */
+                       pt = OutBuf->buf + OutBuf->BufUsed;
+               }
+
+               if((*pch >= 'a' && *pch <= 'z') ||
+                  (*pch >= 'A' && *pch <= 'Z') || /* A-Z */
+                  (*pch >= '0' && *pch <= ':') || /* 0-9 : */
+                  (*pch == '!') || (*pch == '_') || 
+                  (*pch == ',') || (*pch == '.'))
+               {
+                       *(pt++) = *(pch++);
+                       OutBuf->BufUsed++;
+               }                       
+               else {
+                       *pt = '%';
+                       *(pt + 1) = HexList[(unsigned char)*pch][0];
+                       *(pt + 2) = HexList[(unsigned char)*pch][1];
+                       pt += 3;
+                       OutBuf->BufUsed += 3;
+                       pch ++;
+               }
+       }
+       *pt = '\0';
+}
+
 /** 
  * @ingroup StrBuf_DeEnCoder
  * @brief append a string in hex encoding to the buffer
@@ -2773,7 +2831,7 @@ StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp,
        while ((pch != NULL) && (pch < pche))
        {
                while (isspace(*pch)) pch++;
-               UserStart = UserEnd = EmailStart = EmailEnd = NULL;
+               UserEnd = EmailStart = EmailEnd = NULL;
                
                if ((*pch == '"') || (*pch == '\'')) {
                        UserStart = pch + 1;
@@ -2844,7 +2902,6 @@ StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp,
                                EmailStart ++;
                                if (UserStart >= UserEnd)
                                        UserStart = UserEnd = NULL;
-                               At = strchr(EmailStart, '@');
                        }
                        else { /* this is a local recipient... no domain, just a realname */
                                EmailStart = UserStart;
@@ -3205,7 +3262,7 @@ void StrBuf_RFC822_2_Utf8(StrBuf *Target,
 #endif
        const char *eptr;
        int passes = 0;
-       int i, len;
+       int i;
        int illegal_non_rfc2047_encoding = 0;
 
        /* Sometimes, badly formed messages contain strings which were simply
@@ -3215,7 +3272,6 @@ void StrBuf_RFC822_2_Utf8(StrBuf *Target,
         *  charset to UTF-8 if we see any nonprintable characters.
         */
        
-       len = StrLength(DecodeMe);
        for (i=0; i<DecodeMe->BufUsed; ++i) {
                if ((DecodeMe->buf[i] < 32) || (DecodeMe->buf[i] > 126)) {
                        illegal_non_rfc2047_encoding = 1;
@@ -3239,8 +3295,7 @@ void StrBuf_RFC822_2_Utf8(StrBuf *Target,
        }
 
        /* pre evaluate the first pair */
-       nextend = end = NULL;
-       len = StrLength(DecodeMee);
+       end = NULL;
        start = strstr(DecodeMee->buf, "=?");
        eptr = DecodeMee->buf + DecodeMee->BufUsed;
        if (start != NULL) 
@@ -3257,7 +3312,6 @@ void StrBuf_RFC822_2_Utf8(StrBuf *Target,
                
                nFront = start - DecodeMee->buf;
                StrBufAppendBufPlain(Target, DecodeMee->buf, nFront, 0);
-               len -= nFront;
        }
        /*
         * Since spammers will go to all sorts of absurd lengths to get their
@@ -3819,9 +3873,7 @@ void FDIOBufferDelete(FDIOBuffer *FDB)
 
 int FileSendChunked(FDIOBuffer *FDB, const char **Err)
 {
-       char *pRead;
-       long nRead = 0;
-       
+
 #ifdef LINUX_SENDFILE
        ssize_t sent;
        sent = sendfile(FDB->IOB->fd, FDB->OtherFD, &FDB->TotalSentAlready, FDB->ChunkSendRemain);
@@ -3834,6 +3886,10 @@ int FileSendChunked(FDIOBuffer *FDB, const char **Err)
        FDB->TotalSentAlready += sent;
        return FDB->ChunkSendRemain;
 #else
+
+       char *pRead;
+       long nRead = 0;
+
        pRead = FDB->ChunkBuffer->buf;
        while ((FDB->ChunkBuffer->BufUsed < FDB->TotalSendSize) && (nRead >= 0))
        {
@@ -3862,9 +3918,10 @@ int FileSendChunked(FDIOBuffer *FDB, const char **Err)
 
 int FileRecvChunked(FDIOBuffer *FDB, const char **Err)
 {
-#ifdef LINUX_SENDFILE
        ssize_t sent, pipesize;
 
+#ifdef LINUX_SENDFILE
+
        pipesize = splice(FDB->IOB->fd, NULL, 
                          FDB->SplicePipe[1], NULL, 
                          FDB->ChunkSendRemain, 
@@ -3886,6 +3943,32 @@ int FileRecvChunked(FDIOBuffer *FDB, const char **Err)
        FDB->ChunkSendRemain -= sent;
        return sent;
 #else
+       
+       sent = read(FDB->IOB->fd, FDB->ChunkBuffer->buf, FDB->ChunkSendRemain);
+       if (sent > 0) {
+               int nWritten = 0;
+               int rc; 
+               
+               FDB->ChunkBuffer->BufUsed = sent;
+
+               while (nWritten < FDB->ChunkBuffer->BufUsed) {
+                       rc =  write(FDB->OtherFD, FDB->ChunkBuffer->buf + nWritten, FDB->ChunkBuffer->BufUsed - nWritten);
+                       if (rc < 0) {
+                               *Err = strerror(errno);
+                               return rc;
+                       }
+                       nWritten += rc;
+
+               }
+               FDB->ChunkBuffer->BufUsed = 0;
+               FDB->TotalSentAlready += sent;
+               FDB->ChunkSendRemain -= sent;
+               return FDB->ChunkSendRemain;
+       }
+       else if (sent < 0) {
+               *Err = strerror(errno);
+               return sent;
+       }
 
 #endif
        return 0;
@@ -4395,8 +4478,7 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
 {
        const char *pos;
        int fdflags;
-       int len = 0;
-       int rlen;
+       int rlen = 0;
        int nRead = 0;
        int nAlreadyRead = 0;
        int IsNonBlock;
@@ -4422,8 +4504,8 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
        pos = *Pos;
 
        if (pos != NULL)
-               len = pos - IOBuf->buf;
-       rlen = IOBuf->BufUsed - len;
+               rlen = pos - IOBuf->buf;
+       rlen = IOBuf->BufUsed - rlen;
 
 
        if ((IOBuf->BufUsed > 0) && 
@@ -4457,8 +4539,6 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
                IncreaseBuf(IOBuf, 0, nBytes - nRead);
        ptr = IOBuf->buf;
 
-       len = Blob->BufUsed;
-
        fdflags = fcntl(*fd, F_GETFL);
        IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
        if (IsNonBlock)