StrBufSipLine(): make input params const; fix several warnings about unused code.
[citadel.git] / libcitadel / lib / stringbuf.c
index f621255c8224363b10fd9a0ba31783f405e0aa58..9804e5e4b6968f3feb7366d5cbb3d36dc3db4bbc 100644 (file)
@@ -276,9 +276,12 @@ static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
                return -1;
                
        if (DestSize > 0)
-               while (NewSize <= DestSize)
+               while ((NewSize <= DestSize) && (NewSize != 0))
                        NewSize *= 2;
 
+       if (NewSize == 0)
+               return -1;
+
        NewBuf= (char*) malloc(NewSize);
        if (NewBuf == NULL)
                return -1;
@@ -479,9 +482,14 @@ StrBuf* NewStrBufPlain(const char* ptr, int nChars)
        else
                CopySize = nChars;
 
-       while (Siz <= CopySize)
+       while ((Siz <= CopySize) && (Siz != 0))
                Siz *= 2;
 
+       if (Siz == 0)
+       {
+               return NULL;
+       }
+
        NewBuf->buf = (char*) malloc(Siz);
        if (NewBuf->buf == NULL)
        {
@@ -532,9 +540,14 @@ int StrBufPlain(StrBuf *Buf, const char* ptr, int nChars)
        else
                CopySize = nChars;
 
-       while (Siz <= CopySize)
+       while ((Siz <= CopySize) && (Siz != 0))
                Siz *= 2;
 
+       if (Siz == 0) {
+               FlushStrBuf(Buf);
+               return -1;
+       }
+
        if (Siz != Buf->BufSize)
                IncreaseBuf(Buf, 0, Siz);
        memcpy(Buf->buf, ptr, CopySize);
@@ -858,7 +871,8 @@ void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
                va_end(apl);
                newused = Offset + nWritten;
                if (newused >= Buf->BufSize) {
-                       IncreaseBuf(Buf, 1, newused);
+                       if (IncreaseBuf(Buf, 1, newused) == -1)
+                               return; /* TODO: error handling? */
                        newused = Buf->BufSize + 1;
                }
                else {
@@ -899,7 +913,8 @@ void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
                va_end(arg_ptr);
                newused = Buf->BufUsed + nWritten;
                if (newused >= Buf->BufSize) {
-                       IncreaseBuf(Buf, 1, newused);
+                       if (IncreaseBuf(Buf, 1, newused) == -1)
+                               return; /* TODO: error handling? */
                        newused = Buf->BufSize + 1;
                }
                else {
@@ -930,7 +945,8 @@ void StrBufPrintf(StrBuf *Buf, const char *format, ...)
                nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
                va_end(arg_ptr);
                if (nWritten >= Buf->BufSize) {
-                       IncreaseBuf(Buf, 0, 0);
+                       if (IncreaseBuf(Buf, 0, 0) == -1)
+                               return; /* TODO: error handling? */
                        nWritten = Buf->BufSize + 1;
                        continue;
                }
@@ -1333,7 +1349,7 @@ int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char se
        //cit_backtrace();
        //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
 
-       while ((s<e) && !IsEmptyStr(s)) {
+       while ((s < e) && !IsEmptyStr(s)) {
                if (*s == separator) {
                        ++current_token;
                }
@@ -1620,7 +1636,7 @@ int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator
        //cit_backtrace();
        //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
 
-       while ((s<EndBuffer) && !IsEmptyStr(s)) {
+       while ((s < EndBuffer) && !IsEmptyStr(s)) {
                if (*s == separator) {
                        ++current_token;
                }
@@ -1787,22 +1803,26 @@ void StrBufUrlescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
  * @param OutBuf the output buffer
  * @param In Buffer to encode
  * @param PlainIn way in from plain old c strings
+ * @param PlainInLen way in from plain old c strings; maybe you've got binary data or know the length?
  */
-void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
+void StrBufHexEscAppend(StrBuf *OutBuf, const StrBuf *In, const unsigned char *PlainIn, long PlainInLen)
 {
-       const char *pch, *pche;
+       const unsigned char *pch, *pche;
        char *pt, *pte;
        int len;
        
        if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
                return;
        if (PlainIn != NULL) {
-               len = strlen(PlainIn);
+               if (PlainInLen < 0)
+                       len = strlen((const char*)PlainIn);
+               else
+                       len = PlainInLen;
                pch = PlainIn;
                pche = pch + len;
        }
        else {
-               pch = In->buf;
+               pch = (const unsigned char*)In->buf;
                pche = pch + In->BufUsed;
                len = In->BufUsed;
        }
@@ -1820,14 +1840,26 @@ void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
                        pt = OutBuf->buf + OutBuf->BufUsed;
                }
 
-               *pt = HexList[(unsigned char)*pch][0];
+               *pt = HexList[*pch][0];
                pt ++;
-               *pt = HexList[(unsigned char)*pch][1];
+               *pt = HexList[*pch][1];
                pt ++; pch ++; OutBuf->BufUsed += 2;
        }
        *pt = '\0';
 }
 
+/** 
+ * @ingroup StrBuf_DeEnCoder
+ * @brief append a string in hex encoding to the buffer
+ * @param OutBuf the output buffer
+ * @param In Buffer to encode
+ * @param PlainIn way in from plain old c strings
+ */
+void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
+{
+       StrBufHexEscAppend(OutBuf, In, (const unsigned char*) PlainIn, -1);
+}
+
 /**
  * @ingroup StrBuf_DeEnCoder
  * @brief Append a string, escaping characters which have meaning in HTML.  
@@ -2590,15 +2622,12 @@ StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp,
                                           StrBuf *EncBuf)
 {
        StrBuf *Target;
-       int need_to_encode;
-
        const char *pch, *pche;
        const char *UserStart, *UserEnd, *EmailStart, *EmailEnd, *At;
 
        if ((Recp == NULL) || (StrLength(Recp) == 0))
                return NULL;
 
-       need_to_encode = 0;
        pch = ChrPtr(Recp);
        pche = pch + StrLength(Recp);
 
@@ -2609,8 +2638,6 @@ StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp,
 
        while ((pch != NULL) && (pch < pche))
        {
-               int ColonOk = 0;
-
                while (isspace(*pch)) pch++;
                UserStart = UserEnd = EmailStart = EmailEnd = NULL;
                
@@ -2640,7 +2667,6 @@ StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp,
                        if (EmailEnd == NULL)
                                EmailEnd = pche;
                        pch = EmailEnd + 1;
-                       ColonOk = 1;
                }
                else {
                        int gt = 0;
@@ -2748,7 +2774,7 @@ void StrBufReplaceChars(StrBuf *buf, char search, char replace)
 
 /**
  * @ingroup StrBuf
- * @brief removes all \r s from the string, or replaces them with \n if its not a combination of both.
+ * @brief removes all \\r s from the string, or replaces them with \n if its not a combination of both.
  * @param buf Buffer to modify
  */
 void StrBufToUnixLF(StrBuf *buf)
@@ -3959,7 +3985,7 @@ static const char *ErrRBLF_BLOBPreConditionFailed="StrBufReadBLOB: Wrong argumen
 int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
 {
        int fdflags;
-       int len, rlen, slen;
+       int rlen;
        int nSuccessLess;
        int nRead = 0;
        char *ptr;
@@ -3979,8 +4005,6 @@ int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **E
 
        ptr = Buf->buf + Buf->BufUsed;
 
-       slen = len = Buf->BufUsed;
-
        fdflags = fcntl(*fd, F_GETFL);
        IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
        nSuccessLess = 0;
@@ -4049,17 +4073,15 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
                           int check, 
                           const char **Error)
 {
-       const char *pche;
        const char *pos;
        int fdflags;
        int len = 0;
-       int rlen, slen;
+       int rlen;
        int nRead = 0;
        int nAlreadyRead = 0;
        int IsNonBlock;
        char *ptr;
        fd_set rfds;
-       const char *pch;
        struct timeval tv;
        int nSuccessLess = 0;
        int MaxTries;
@@ -4088,9 +4110,6 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
            (pos != NULL) && 
            (pos < IOBuf->buf + IOBuf->BufUsed)) 
        {
-               pche = IOBuf->buf + IOBuf->BufUsed;
-               pch = pos;
-
                if (rlen < nBytes) {
                        memcpy(Blob->buf + Blob->BufUsed, pos, rlen);
                        Blob->BufUsed += rlen;
@@ -4118,7 +4137,7 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
                IncreaseBuf(IOBuf, 0, nBytes - nRead);
        ptr = IOBuf->buf;
 
-       slen = len = Blob->BufUsed;
+       len = Blob->BufUsed;
 
        fdflags = fcntl(*fd, F_GETFL);
        IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
@@ -4209,9 +4228,9 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
  * @param Buf BLOB with lines of text...
  * @param Ptr moved arround to keep the next-line across several iterations
  *        has to be &NULL on start; will be &NotNULL on end of buffer
- * @returns size of copied buffer
+ * @returns size of remaining buffer
  */
-int StrBufSipLine(StrBuf *LineBuf, StrBuf *Buf, const char **Ptr)
+int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr)
 {
        const char *aptr, *ptr, *eptr;
        char *optr, *xptr;