stringbuf.c: chipped away at style modernization
[citadel.git] / libcitadel / lib / stringbuf.c
index e4824cf369fd5d64ac8dfd8bf253c2b5cd0ad531..2101465e0a1f3d181a4e138195dd91a1c075f1ad 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c) 1987-2022 by the citadel.org team
+// Copyright (c) 1987-2023 by the citadel.org team
 //
 // This program is open source software.  Use, duplication, or disclosure
 // is subject to the terms of the GNU General Public License, version 3.
@@ -33,8 +33,7 @@
 
 #ifdef HAVE_ZLIB
 #include <zlib.h>
-int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen,
-                          const Bytef * source, uLong sourceLen, int level);
+int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen, const Bytef * source, uLong sourceLen, int level);
 #endif
 int BaseStrBufSize = 64;
 int EnableSplice = 0;
@@ -66,18 +65,16 @@ const char HexList[256][3] = {
        "F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"};
 
 
-/*
- * Private Structure for the Stringbuffer
- */
+// Private Structure for the Stringbuffer
 struct StrBuf {
-       char *buf;         /**< the pointer to the dynamic buffer */
-       long BufSize;      /**< how many spcae do we optain */
-       long BufUsed;      /**< StNumber of Chars used excluding the trailing \\0 */
-       int ConstBuf;      /**< are we just a wrapper arround a static buffer and musn't we be changed? */
+       char *buf;              // the pointer to the dynamic buffer
+       long BufSize;           // how many spcae do we optain
+       long BufUsed;           // Number of Chars used excluding the trailing \\0
+       int ConstBuf;           // are we just a wrapper arround a static buffer and musn't we be changed?
 #ifdef SIZE_DEBUG
-       long nIncreases;   /**< for profiling; cound how many times we needed more */
-       char bt [SIZ];     /**< Stacktrace of last increase */
-       char bt_lastinc [SIZ]; /**< How much did we increase last time? */
+       long nIncreases;        // for profiling; cound how many times we needed more
+       char bt [SIZ];          // Stacktrace of last increase
+       char bt_lastinc[SIZ];   // How much did we increase last time?
 #endif
 };
 
@@ -94,22 +91,24 @@ static void StrBufBacktrace(StrBuf *Buf, int which) {
        size_t size, i;
        char **strings;
 
-       if (which)
+       if (which) {
                pstart = pch = Buf->bt;
-       else
+       }
+       else {
                pstart = pch = Buf->bt_lastinc;
+       }
        size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
        strings = backtrace_symbols(stack_frames, size);
        for (i = 0; i < size; i++) {
-               if (strings != NULL)
+               if (strings != NULL) {
                        n = snprintf(pch, SIZ - (pch - pstart), "%s\\n", strings[i]);
-               else
+               }
+               else {
                        n = snprintf(pch, SIZ - (pch - pstart), "%p\\n", stack_frames[i]);
+               }
                pch += n;
        }
        free(strings);
-
-
 }
 #endif
 
@@ -639,11 +638,8 @@ void HFreeStrBuf (void *VFreeMe) {
  *                      Simple string transformations                          *
  *******************************************************************************/
 
-/*
- *  Wrapper around atol
- */
-long StrTol(const StrBuf *Buf)
-{
+//  Wrapper around atol
+long StrTol(const StrBuf *Buf) {
        if (Buf == NULL)
                return 0;
        if(Buf->BufUsed > 0)
@@ -652,11 +648,9 @@ long StrTol(const StrBuf *Buf)
                return 0;
 }
 
-/*
- *  Wrapper around atoi
- */
-int StrToi(const StrBuf *Buf)
-{
+
+//  Wrapper around atoi
+int StrToi(const StrBuf *Buf) {
        if (Buf == NULL)
                return 0;
        if (Buf->BufUsed > 0)
@@ -665,11 +659,10 @@ int StrToi(const StrBuf *Buf)
                return 0;
 }
 
-/*
- *  Checks to see if the string is a pure number 
- *  Buf The buffer to inspect
- * @returns 1 if its a pure number, 0, if not.
- */
+
+// Checks to see if the string is a pure number 
+// Buf The buffer to inspect
+// returns 1 if its a pure number, 0, if not.
 int StrBufIsNumber(const StrBuf *Buf) {
        char * pEnd;
        if ((Buf == NULL) || (Buf->BufUsed == 0)) {
@@ -685,16 +678,14 @@ int StrBufIsNumber(const StrBuf *Buf) {
        return 0;
 } 
 
-/**
- *  modifies a Single char of the Buf
- * You can point to it via char* or a zero-based integer
- *  Buf The buffer to manipulate
- *  ptr char* to zero; use NULL if unused
- *  nThChar zero based pointer into the string; use -1 if unused
- *  PeekValue The Character to place into the position
- */
-long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue)
-{
+
+// modifies a Single char of the Buf
+// You can point to it via char* or a zero-based integer
+// Buf The buffer to manipulate
+// ptr char* to zero; use NULL if unused
+// nThChar zero based pointer into the string; use -1 if unused
+// PeekValue The Character to place into the position
+long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue) {
        if (Buf == NULL)
                return -1;
        if (ptr != NULL)
@@ -705,17 +696,15 @@ long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue)
        return nThChar;
 }
 
-/**
- *  modifies a range of chars of the Buf
- * You can point to it via char* or a zero-based integer
- *  Buf The buffer to manipulate
- *  ptr char* to zero; use NULL if unused
- *  nThChar zero based pointer into the string; use -1 if unused
- *  nChars how many chars are to be flushed?
- *  PookValue The Character to place into that area
- */
-long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char PookValue)
-{
+
+// modifies a range of chars of the Buf
+// You can point to it via char* or a zero-based integer
+// Buf The buffer to manipulate
+// ptr char* to zero; use NULL if unused
+// nThChar zero based pointer into the string; use -1 if unused
+// nChars how many chars are to be flushed?
+// PookValue The Character to place into that area
+long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char PookValue) {
        if (Buf == NULL)
                return -1;
        if (ptr != NULL)
@@ -726,19 +715,17 @@ long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char Po
                nChars =  Buf->BufUsed - nThChar;
 
        memset(Buf->buf + nThChar, PookValue, nChars);
-       /* just to be shure... */
+       // just to be sure...
        Buf->buf[Buf->BufUsed] = 0;
        return nChars;
 }
 
-/**
- *  Append a StringBuffer to the buffer
- *  Buf Buffer to modify
- *  AppendBuf Buffer to copy at the end of our buffer
- *  Offset Should we start copying from an offset?
- */
-void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset)
-{
+
+// Append a StringBuffer to the buffer
+// Buf Buffer to modify
+// AppendBuf Buffer to copy at the end of our buffer
+// Offset Should we start copying from an offset?
+void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset) {
        if ((AppendBuf == NULL) || (AppendBuf->buf == NULL) ||
            (Buf == NULL) || (Buf->buf == NULL))
                return;
@@ -783,15 +770,13 @@ void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, u
        Buf->buf[Buf->BufUsed] = '\0';
 }
 
-/*
- *  sprintf like function appending the formated string to the buffer
- * vsnprintf version to wrap into own calls
- *  Buf Buffer to extend by format and Params
- *  format printf alike format to add
- *  ap va_list containing the items for format
- */
-void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
-{
+
+//  sprintf like function appending the formated string to the buffer
+// vsnprintf version to wrap into own calls
+//  Buf Buffer to extend by format and Params
+//  format printf alike format to add
+//  ap va_list containing the items for format
+void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap) {
        va_list apl;
        size_t BufSize;
        size_t nWritten;
@@ -808,14 +793,12 @@ void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
        
        while (newused >= BufSize) {
                va_copy(apl, ap);
-               nWritten = vsnprintf(Buf->buf + Offset, 
-                                    Buf->BufSize - Offset, 
-                                    format, apl);
+               nWritten = vsnprintf(Buf->buf + Offset, Buf->BufSize - Offset, format, apl);
                va_end(apl);
                newused = Offset + nWritten;
                if (newused >= Buf->BufSize) {
                        if (IncreaseBuf(Buf, 1, newused) == -1)
-                               return; /* TODO: error handling? */
+                               return; // TODO: error handling?
                        newused = Buf->BufSize + 1;
                }
                else {
@@ -826,13 +809,11 @@ void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
        }
 }
 
-/**
- *  sprintf like function appending the formated string to the buffer
- *  Buf Buffer to extend by format and Params
- *  format printf alike format to add
- */
-void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
-{
+
+// sprintf like function appending the formated string to the buffer
+// Buf Buffer to extend by format and Params
+// format printf alike format to add
+void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...) {
        size_t BufSize;
        size_t nWritten;
        size_t Offset;
@@ -849,14 +830,12 @@ void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
 
        while (newused >= BufSize) {
                va_start(arg_ptr, format);
-               nWritten = vsnprintf(Buf->buf + Buf->BufUsed, 
-                                    Buf->BufSize - Buf->BufUsed, 
-                                    format, arg_ptr);
+               nWritten = vsnprintf(Buf->buf + Buf->BufUsed, Buf->BufSize - Buf->BufUsed, format, arg_ptr);
                va_end(arg_ptr);
                newused = Buf->BufUsed + nWritten;
                if (newused >= Buf->BufSize) {
                        if (IncreaseBuf(Buf, 1, newused) == -1)
-                               return; /* TODO: error handling? */
+                               return; // TODO: error handling?
                        newused = Buf->BufSize + 1;
                }
                else {
@@ -867,13 +846,11 @@ void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
        }
 }
 
-/**
- *  sprintf like function putting the formated string into the buffer
- *  Buf Buffer to extend by format and Parameters
- *  format printf alike format to add
- */
-void StrBufPrintf(StrBuf *Buf, const char *format, ...)
-{
+
+//  sprintf like function putting the formated string into the buffer
+//  Buf Buffer to extend by format and Parameters
+//  format printf alike format to add
+void StrBufPrintf(StrBuf *Buf, const char *format, ...) {
        size_t nWritten;
        va_list arg_ptr;
        
@@ -887,7 +864,7 @@ void StrBufPrintf(StrBuf *Buf, const char *format, ...)
                va_end(arg_ptr);
                if (nWritten >= Buf->BufSize) {
                        if (IncreaseBuf(Buf, 0, 0) == -1)
-                               return; /* TODO: error handling? */
+                               return; // TODO: error handling?
                        nWritten = Buf->BufSize + 1;
                        continue;
                }
@@ -895,15 +872,10 @@ void StrBufPrintf(StrBuf *Buf, const char *format, ...)
        }
 }
 
-/**
- *  Callback for cURL to append the webserver reply to a buffer
- *  ptr pre-defined by the cURL API; see man 3 curl for mre info
- *  size pre-defined by the cURL API; see man 3 curl for mre info
- *  nmemb pre-defined by the cURL API; see man 3 curl for mre info
- *  stream pre-defined by the cURL API; see man 3 curl for mre info
- */
-size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream)
-{
+
+//  Callback for cURL to append the webserver reply to a buffer
+//  ptr, size, nmemb, and stream are pre-defined by the cURL API; see man 3 curl for more info
+size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
 
        StrBuf *Target;
 
@@ -916,25 +888,20 @@ size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *strea
 }
 
 
-/**
- *  extracts a substring from Source into dest
- *  dest buffer to place substring into
- *  Source string to copy substring from
- *  Offset chars to skip from start
- *  nChars number of chars to copy
- * @returns the number of chars copied; may be different from nChars due to the size of Source
- */
-int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars)
-{
+// extracts a substring from Source into dest
+// dest buffer to place substring into
+// Source string to copy substring from
+// Offset chars to skip from start
+// nChars number of chars to copy
+// returns the number of chars copied; may be different from nChars due to the size of Source
+int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars) {
        size_t NCharsRemain;
-       if (Offset > Source->BufUsed)
-       {
+       if (Offset > Source->BufUsed) {
                if (dest != NULL)
                        FlushStrBuf(dest);
                return 0;
        }
-       if (Offset + nChars < Source->BufUsed)
-       {
+       if (Offset + nChars < Source->BufUsed) {
                if ((nChars >= dest->BufSize) && 
                    (IncreaseBuf(dest, 0, nChars + 1) == -1))
                        return 0;
@@ -3097,13 +3064,10 @@ int StrBufRFC2047encode(StrBuf **target, const StrBuf *source)
        return (*target)->BufUsed;;
 }
 
-/**
- *     Quoted-Printable encode a message; make it < 80 columns width.
- *     source          Source string to be encoded.
- * @returns     buffer with encoded message.
- */
-StrBuf *StrBufRFC2047encodeMessage(const StrBuf *EncodeMe)
-{
+// Quoted-Printable encode a message; make it < 80 columns width.
+// source      Source string to be encoded.
+// returns     buffer with encoded message.
+StrBuf *StrBufQuotedPrintableEncode(const StrBuf *EncodeMe) {
        StrBuf *OutBuf;
        char *Optr, *OEptr;
        const char *ptr, *eptr;
@@ -3117,10 +3081,8 @@ StrBuf *StrBufRFC2047encodeMessage(const StrBuf *EncodeMe)
        eptr = EncodeMe->buf + EncodeMe->BufUsed;
        LinePos = 0;
 
-       while (ptr < eptr)
-       {
-               if (Optr + 4 >= OEptr)
-               {
+       while (ptr < eptr) {
+               if (Optr + 4 >= OEptr) {
                        long Offset;
                        Offset = Optr - OutBuf->buf;
                        OutBuf->BufUsed = Optr - OutBuf->buf;
@@ -3128,21 +3090,16 @@ StrBuf *StrBufRFC2047encodeMessage(const StrBuf *EncodeMe)
                        Optr = OutBuf->buf + Offset;
                        OEptr = OutBuf->buf + OutBuf->BufSize;
                }
-               if (*ptr == '\r')
-               {
-                       /* ignore carriage returns */
+               if (*ptr == '\r') {             // ignore carriage returns
                        ptr ++;
                }
-               else if (*ptr == '\n') {
-                       /* hard line break */
+               else if (*ptr == '\n') {        // hard line break
                        memcpy(Optr, HKEY("=0A"));
                        Optr += 3;
                        LinePos += 3;
                        ptr ++;
                }
-               else if (( (*ptr >= 32) && (*ptr <= 60) ) ||
-                        ( (*ptr >= 62) && (*ptr <= 126) ))
-               {
+               else if (( (*ptr >= 32) && (*ptr <= 60) ) || ( (*ptr >= 62) && (*ptr <= 126) )) {
                        *Optr = *ptr;
                        Optr ++;
                        ptr ++;
@@ -3160,8 +3117,7 @@ StrBuf *StrBufRFC2047encodeMessage(const StrBuf *EncodeMe)
                        ptr ++;
                }
 
-               if (LinePos > 72) {
-                       /* soft line break */
+               if (LinePos > 72) {             // soft line break
                        if (isspace(*(Optr - 1))) {
                                ch = *(Optr - 1);
                                Optr --;
@@ -3187,11 +3143,7 @@ StrBuf *StrBufRFC2047encodeMessage(const StrBuf *EncodeMe)
 }
 
 
-static void AddRecipient(StrBuf *Target, 
-                        StrBuf *UserName, 
-                        StrBuf *EmailAddress, 
-                        StrBuf *EncBuf)
-{
+static void AddRecipient(StrBuf *Target, StrBuf *UserName, StrBuf *EmailAddress, StrBuf *EncBuf) {
        int QuoteMe = 0;
 
        if (StrLength(Target) > 0) StrBufAppendBufPlain(Target, HKEY(", "), 0);
@@ -3219,11 +3171,7 @@ static void AddRecipient(StrBuf *Target,
  * \param EncBuf Temporary buffer for internal use; Please provide valid buffer.
  * \returns encoded & sanitized buffer with the contents of Recp; Caller owns this memory.
  */
-StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp, 
-                                          StrBuf *UserName, 
-                                          StrBuf *EmailAddress,
-                                          StrBuf *EncBuf)
-{
+StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp, StrBuf *UserName, StrBuf *EmailAddress, StrBuf *EncBuf) {
        StrBuf *Target;
        const char *pch, *pche;
        const char *UserStart, *UserEnd, *EmailStart, *EmailEnd, *At;
@@ -3362,8 +3310,7 @@ StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp,
  *  search character to search
  *  replace character to replace search by
  */
-void StrBufReplaceChars(StrBuf *buf, char search, char replace)
-{
+void StrBufReplaceChars(StrBuf *buf, char search, char replace) {
        long i;
        if (buf == NULL)
                return;
@@ -3897,9 +3844,6 @@ long StrBuf_Utf8StrCut(StrBuf *Buf, int maxlen)
 }
 
 
-
-
-
 /*******************************************************************************
  *                               wrapping ZLib                                 *
  *******************************************************************************/
@@ -4012,6 +3956,7 @@ int CompressBuffer(StrBuf *Buf)
        return 0;
 }
 
+
 /*******************************************************************************
  *           File I/O; Callbacks to libevent                                   *
  *******************************************************************************/
@@ -4029,8 +3974,7 @@ long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB)
         * where a cut left is inexpensive
         */
 
-       if (FB->ReadWritePointer != NULL)
-       {
+       if (FB->ReadWritePointer != NULL) {
                long already_read;
                
                already_read = FB->ReadWritePointer - FB->Buf->buf;
@@ -4046,9 +3990,7 @@ long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB)
                                FB->ReadWritePointer = FB->Buf->buf;
                                bufremain = FB->Buf->BufSize;                   
                        }
-                       else if ((unread < 64) || 
-                                (bufremain < already_read))
-                       {
+                       else if ((unread < 64) || (bufremain < already_read)) {
                                /* 
                                 * if its just a tiny bit remaining, or we run out of space... 
                                 * lets tidy up.
@@ -4061,8 +4003,7 @@ long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB)
                                FB->ReadWritePointer = FB->Buf->buf;
                                bufremain = FB->Buf->BufSize - unread - 1;
                        }
-                       else if (bufremain < (FB->Buf->BufSize / 10))
-                       {
+                       else if (bufremain < (FB->Buf->BufSize / 10)) {
                                /* get a bigger buffer */ 
 
                                IncreaseBuf(FB->Buf, 0, FB->Buf->BufUsed + 1);
@@ -4096,8 +4037,8 @@ long StrBuf_read_one_chunk_callback (int fd, short event, IOBuffer *FB)
        return n;
 }
 
-int StrBuf_write_one_chunk_callback(int fd, short event, IOBuffer *FB)
-{
+
+int StrBuf_write_one_chunk_callback(int fd, short event, IOBuffer *FB) {
        long WriteRemain;
        int n;
 
@@ -4135,16 +4076,14 @@ int StrBuf_write_one_chunk_callback(int fd, short event, IOBuffer *FB)
        return n;
 }
 
-/**
- *  extract a "next line" from Buf; Ptr to persist across several iterations
- *  LineBuf your line will be copied here.
- *  FB BLOB with lines of text...
- *  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
- */
-eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB)
-{
+
+// extract a "next line" from Buf; Ptr to persist across several iterations
+// LineBuf your line will be copied here.
+// FB BLOB with lines of text...
+// 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
+eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB) {
        const char *aptr, *ptr, *eptr;
        char *optr, *xptr;
 
@@ -4167,10 +4106,7 @@ eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB)
        eptr = FB->Buf->buf + FB->Buf->BufUsed;
        xptr = LineBuf->buf + LineBuf->BufSize - 1;
 
-       while ((ptr <= eptr) && 
-              (*ptr != '\n') &&
-              (*ptr != '\r') )
-       {
+       while ((ptr <= eptr) && (*ptr != '\n') && (*ptr != '\r') ) {
                *optr = *ptr;
                optr++; ptr++;
                if (optr == xptr) {
@@ -4187,15 +4123,11 @@ eReadState StrBufChunkSipLine(StrBuf *LineBuf, IOBuffer *FB)
                if ((*(ptr - 1) != '\r') && (*(ptr - 1) != '\n')) {
                        LineBuf->BufUsed = optr - LineBuf->buf;
                        *optr = '\0';
-                       if ((FB->ReadWritePointer != NULL) && 
-                           (FB->ReadWritePointer != FB->Buf->buf))
-                       {
-                               /* Ok, the client application read all the data 
-                                  it was interested in so far. Since there is more to read, 
-                                  we now shrink the buffer, and move the rest over.
-                               */
-                               StrBufCutLeft(FB->Buf, 
-                                             FB->ReadWritePointer - FB->Buf->buf);
+                       if ((FB->ReadWritePointer != NULL) && (FB->ReadWritePointer != FB->Buf->buf)) {
+                               // Ok, the client application read all the data 
+                               // it was interested in so far. Since there is more to read, 
+                               // we now shrink the buffer, and move the rest over.
+                               StrBufCutLeft(FB->Buf, FB->ReadWritePointer - FB->Buf->buf);
                                FB->ReadWritePointer = FB->Buf->buf;
                        }
                        return eMustReadMore;
@@ -4236,8 +4168,8 @@ eReadState StrBufCheckBuffer(IOBuffer *FB)
        return eReadSuccess;
 }
 
-long IOBufferStrLength(IOBuffer *FB)
-{
+
+long IOBufferStrLength(IOBuffer *FB) {
        if ((FB == NULL) || (FB->Buf == NULL))
                return 0;
        if (FB->ReadWritePointer == NULL)
@@ -4299,17 +4231,15 @@ int StrBufTCP_read_line(StrBuf *buf, int *fd, int append, const char **Error)
 }
 
 
-/**
- *  Read a line from socket
- * flushes and closes the FD on error
- *  Line the line to read from the fd / I/O Buffer
- *  buf the buffer to get the input to
- *  fd pointer to the filedescriptor to read
- *  timeout number of successless selects until we bail out
- *  selectresolution how long to wait on each select
- *  Error strerror() on error 
- * @returns numbers of chars read
- */
+// Read a line from socket
+// flushes and closes the FD on error
+// Line the line to read from the fd / I/O Buffer
+// buf the buffer to get the input to
+// fd pointer to the filedescriptor to read
+// timeout number of successless selects until we bail out
+// selectresolution how long to wait on each select
+// Error strerror() on error 
+// returns numbers of chars read
 int StrBufTCP_read_buffered_line(StrBuf *Line, 
                                 StrBuf *buf, 
                                 int *fd, 
@@ -4330,16 +4260,18 @@ int StrBufTCP_read_buffered_line(StrBuf *Line,
                if (pch != NULL) {
                        rlen = 0;
                        len = pch - buf->buf;
-                       if (len > 0 && (*(pch - 1) == '\r') )
+                       if (len > 0 && (*(pch - 1) == '\r') ) {
                                rlen ++;
+                       }
                        StrBufSub(Line, buf, 0, len - rlen);
                        StrBufCutLeft(buf, len + 1);
                        return len - rlen;
                }
        }
        
-       if (buf->BufSize - buf->BufUsed < 10)
+       if (buf->BufSize - buf->BufUsed < 10) {
                IncreaseBuf(buf, 1, -1);
+       }
 
        fdflags = fcntl(*fd, F_GETFL);
        IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
@@ -4362,9 +4294,7 @@ int StrBufTCP_read_buffered_line(StrBuf *Line,
                        nSuccessLess ++;
                        continue;
                }
-               rlen = read(*fd, 
-                           &buf->buf[buf->BufUsed], 
-                           buf->BufSize - buf->BufUsed - 1);
+               rlen = read(*fd, &buf->buf[buf->BufUsed], buf->BufSize - buf->BufUsed - 1);
                if (rlen < 1) {
                        *Error = strerror(errno);
                        close(*fd);
@@ -4376,10 +4306,9 @@ int StrBufTCP_read_buffered_line(StrBuf *Line,
                        buf->BufUsed += rlen;
                        buf->buf[buf->BufUsed] = '\0';
                        pch = strchr(buf->buf, '\n');
-                       if ((pch == NULL) &&
-                           (buf->BufUsed + 10 > buf->BufSize) &&
-                           (IncreaseBuf(buf, 1, -1) == -1))
+                       if ((pch == NULL) && (buf->BufUsed + 10 > buf->BufSize) && (IncreaseBuf(buf, 1, -1) == -1)) {
                                return -1;
+                       }
                        continue;
                }
                
@@ -4400,18 +4329,18 @@ int StrBufTCP_read_buffered_line(StrBuf *Line,
 static const char *ErrRBLF_PreConditionFailed="StrBufTCP_read_buffered_line_fast: Wrong arguments or invalid Filedescriptor";
 static const char *ErrRBLF_SelectFailed="StrBufTCP_read_buffered_line_fast: Select failed without reason";
 static const char *ErrRBLF_NotEnoughSentFromServer="StrBufTCP_read_buffered_line_fast: No complete line was sent from peer";
-/**
- *  Read a line from socket
- * flushes and closes the FD on error
- *  Line where to append our Line read from the fd / I/O Buffer; 
- *  IOBuf the buffer to get the input to; lifetime pair to FD
- *  Pos pointer to the current read position, should be NULL initialized on opening the FD it belongs to.!
- *  fd pointer to the filedescriptor to read
- *  timeout number of successless selects until we bail out
- *  selectresolution how long to wait on each select
- *  Error strerror() on error 
- * @returns numbers of chars read or -1 in case of error. "\n" will become 0
- */
+
+
+// Read a line from socket
+// flushes and closes the FD on error
+// Line where to append our Line read from the fd / I/O Buffer; 
+// IOBuf the buffer to get the input to; lifetime pair to FD
+// Pos pointer to the current read position, should be NULL initialized on opening the FD it belongs to.!
+// fd pointer to the filedescriptor to read
+// timeout number of successless selects until we bail out
+// selectresolution how long to wait on each select
+// Error strerror() on error 
+// returns numbers of chars read or -1 in case of error. "\n" will become 0
 int StrBufTCP_read_buffered_line_fast(StrBuf *Line, 
                                      StrBuf *IOBuf, 
                                      const char **Pos,
@@ -4432,32 +4361,24 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
        struct timeval tv;
        
        retlen = 0;
-       if ((Line == NULL) ||
-           (Pos == NULL) ||
-           (IOBuf == NULL) ||
-           (*fd == -1))
-       {
-               if (Pos != NULL)
+       if ((Line == NULL) || (Pos == NULL) || (IOBuf == NULL) || (*fd == -1)) {
+               if (Pos != NULL) {
                        *Pos = NULL;
+               }
                *Error = ErrRBLF_PreConditionFailed;
                return -1;
        }
 
        pos = *Pos;
-       if ((IOBuf->BufUsed > 0) && 
-           (pos != NULL) && 
-           (pos < IOBuf->buf + IOBuf->BufUsed)) 
-       {
+       if ((IOBuf->BufUsed > 0) && (pos != NULL) && (pos < IOBuf->buf + IOBuf->BufUsed)) {
                char *pcht;
 
                pche = IOBuf->buf + IOBuf->BufUsed;
                pch = pos;
                pcht = Line->buf;
 
-               while ((pch < pche) && (*pch != '\n'))
-               {
-                       if (Line->BufUsed + 10 > Line->BufSize)
-                       {
+               while ((pch < pche) && (*pch != '\n')) {
+                       if (Line->BufUsed + 10 > Line->BufSize) {
                                long apos;
                                apos = pcht - Line->buf;
                                *pcht = '\0';
@@ -4470,8 +4391,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
                }
 
                len = pch - pos;
-               if (len > 0 && (*(pch - 1) == '\r') )
-               {
+               if (len > 0 && (*(pch - 1) == '\r') ) {
                        retlen--;
                        len --;
                        pcht --;
@@ -4479,24 +4399,21 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
                }
                *pcht = '\0';
 
-               if ((pch >= pche) || (*pch == '\0'))
-               {
+               if ((pch >= pche) || (*pch == '\0')) {
                        FlushStrBuf(IOBuf);
                        *Pos = NULL;
                        pch = NULL;
                        pos = 0;
                }
 
-               if ((pch != NULL) && 
-                   (pch <= pche)) 
-               {
+               if ((pch != NULL) && (pch <= pche)) {
                        if (pch + 1 >= pche) {
                                *Pos = NULL;
                                FlushStrBuf(IOBuf);
                        }
-                       else
+                       else {
                                *Pos = pch + 1;
-                       
+                       }
                        return retlen;
                }
                else 
@@ -4505,8 +4422,9 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
 
        /* If we come here, Pos is Unset since we read everything into Line, and now go for more. */
        
-       if (IOBuf->BufSize - IOBuf->BufUsed < 10)
+       if (IOBuf->BufSize - IOBuf->BufUsed < 10) {
                IncreaseBuf(IOBuf, 1, -1);
+       }
 
        fdflags = fcntl(*fd, F_GETFL);
        IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
@@ -4515,8 +4433,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
        while ((nSuccessLess < timeout) && 
               (pLF == NULL) &&
               (*fd != -1)) {
-               if (IsNonBlock)
-               {
+               if (IsNonBlock) {
                        tv.tv_sec = 1;
                        tv.tv_usec = 0;
                
@@ -4535,9 +4452,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
                                continue;
                        }
                }
-               rlen = read(*fd, 
-                           &IOBuf->buf[IOBuf->BufUsed], 
-                           IOBuf->BufSize - IOBuf->BufUsed - 1);
+               rlen = read(*fd, &IOBuf->buf[IOBuf->BufUsed], IOBuf->BufSize - IOBuf->BufUsed - 1);
                if (rlen < 1) {
                        *Error = strerror(errno);
                        close(*fd);
@@ -4557,8 +4472,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
                        if ((pLF >= pche) || (*pLF == '\0'))
                                pLF = NULL;
 
-                       if (IOBuf->BufUsed + 10 > IOBuf->BufSize)
-                       {
+                       if (IOBuf->BufUsed + 10 > IOBuf->BufSize) {
                                long apos = 0;
 
                                if (pLF != NULL) apos = pLF - IOBuf->buf;
@@ -4568,8 +4482,7 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
 
                        continue;
                }
-               else
-               {
+               else {
                        nSuccessLess++;
                }
        }
@@ -4577,11 +4490,11 @@ int StrBufTCP_read_buffered_line_fast(StrBuf *Line,
        if (pLF != NULL) {
                pos = IOBuf->buf;
                len = pLF - pos;
-               if (len > 0 && (*(pLF - 1) == '\r') )
+               if (len > 0 && (*(pLF - 1) == '\r') ) {
                        len --;
+               }
                StrBufAppendBufPlain(Line, ChrPtr(IOBuf), len, 0);
-               if (pLF + 1 >= IOBuf->buf + IOBuf->BufUsed)
-               {
+               if (pLF + 1 >= IOBuf->buf + IOBuf->BufUsed) {
                        FlushStrBuf(IOBuf);
                }
                else 
@@ -4615,26 +4528,23 @@ int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **E
        struct timeval tv;
        fd_set rfds;
 
-       if ((Buf == NULL) || (Buf->buf == NULL) || (*fd == -1))
-       {
+       if ((Buf == NULL) || (Buf->buf == NULL) || (*fd == -1)) {
                *Error = ErrRBLF_BLOBPreConditionFailed;
                return -1;
        }
-       if (!append)
+       if (!append) {
                FlushStrBuf(Buf);
-       if (Buf->BufUsed + nBytes >= Buf->BufSize)
+       }
+       if (Buf->BufUsed + nBytes >= Buf->BufSize) {
                IncreaseBuf(Buf, 1, Buf->BufUsed + nBytes);
-
+       }
        ptr = Buf->buf + Buf->BufUsed;
 
        fdflags = fcntl(*fd, F_GETFL);
        IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
        nSuccessLess = 0;
-       while ((nRead < nBytes) && 
-              (*fd != -1)) 
-       {
-               if (IsNonBlock)
-               {
+       while ((nRead < nBytes) && (*fd != -1)) {
+               if (IsNonBlock) {
                        tv.tv_sec = 1;
                        tv.tv_usec = 0;
                
@@ -4644,8 +4554,9 @@ int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **E
                                *Error = strerror(errno);
                                close (*fd);
                                *fd = -1;
-                               if (*Error == NULL)
+                               if (*Error == NULL) {
                                        *Error = ErrRBLF_SelectFailed;
+                               }
                                return -1;
                        }
                        if (! FD_ISSET(*fd, &rfds) != 0) {
@@ -4672,19 +4583,19 @@ int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **E
 
 const char *ErrRBB_BLOBFPreConditionFailed = "StrBufReadBLOBBuffered: to many selects; aborting.";
 const char *ErrRBB_too_many_selects        = "StrBufReadBLOBBuffered: to many selects; aborting.";
-/**
- *  Input binary data from socket
- * flushes and closes the FD on error
- *  Blob put binary thing here
- *  IOBuf the buffer to get the input to
- *  Pos offset inside of IOBuf
- *  fd pointer to the filedescriptor to read
- *  append Append to an existing string or replace?
- *  nBytes the maximal number of bytes to read
- *  check whether we should search for '000\n' terminators in case of timeouts
- *  Error strerror() on error 
- * @returns numbers of chars read
- */
+
+
+// Input binary data from socket
+// flushes and closes the FD on error
+// Blob put binary thing here
+// IOBuf the buffer to get the input to
+// Pos offset inside of IOBuf
+// fd pointer to the filedescriptor to read
+// append Append to an existing string or replace?
+// nBytes the maximal number of bytes to read
+// check whether we should search for '000\n' terminators in case of timeouts
+// Error strerror() on error 
+// returns numbers of chars read
 int StrBufReadBLOBBuffered(StrBuf *Blob, 
                           StrBuf *IOBuf, 
                           const char **Pos,
@@ -4706,11 +4617,7 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
        int nSuccessLess = 0;
        int MaxTries;
 
-       if ((Blob == NULL)  ||
-           (*fd == -1)     ||
-           (IOBuf == NULL) ||
-           (Pos == NULL))
-       {
+       if ((Blob == NULL) || (*fd == -1) || (IOBuf == NULL) || (Pos == NULL)) {
                if (Pos != NULL)
                        *Pos = NULL;
                *Error = ErrRBB_BLOBFPreConditionFailed;
@@ -4730,8 +4637,7 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
        rlen = IOBuf->BufUsed - rlen;
 
 
-       if ((IOBuf->BufUsed > 0) && (pos != NULL) && (pos < IOBuf->buf + IOBuf->BufUsed)) 
-       {
+       if ((IOBuf->BufUsed > 0) && (pos != NULL) && (pos < IOBuf->buf + IOBuf->BufUsed)) {
                if (rlen < nBytes) {
                        memcpy(Blob->buf + Blob->BufUsed, pos, rlen);
                        Blob->BufUsed += rlen;
@@ -4791,8 +4697,7 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
                        }
                }
                rlen = read(*fd, ptr, IOBuf->BufSize - (ptr - IOBuf->buf));
-               // if (rlen == -1) {            2021feb27 ajc changed this, apparently we will always get at least 1 byte unless the connection is broken
-               if (rlen < 1) {
+               if (rlen < 1) {                 // We will always get at least 1 byte unless the connection is broken
                        close(*fd);
                        *fd = -1;
                        *Error = strerror(errno);
@@ -4836,24 +4741,18 @@ int StrBufReadBLOBBuffered(StrBuf *Blob,
        return nRead + nAlreadyRead;
 }
 
-/**
- *  extract a "next line" from Buf; Ptr to persist across several iterations
- *  LineBuf your line will be copied here.
- *  Buf BLOB with lines of text...
- *  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 remaining buffer
- */
-int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr)
-{
+
+// extract a "next line" from Buf; Ptr to persist across several iterations
+// LineBuf your line will be copied here.
+// Buf BLOB with lines of text...
+// 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 remaining buffer
+int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr) {
        const char *aptr, *ptr, *eptr;
        char *optr, *xptr;
 
-       if ((Buf == NULL) ||
-           (*Ptr == StrBufNOTNULL) ||
-           (LineBuf == NULL)||
-           (LineBuf->buf == NULL))
-       {
+       if ((Buf == NULL) || (*Ptr == StrBufNOTNULL) || (LineBuf == NULL)|| (LineBuf->buf == NULL)) {
                *Ptr = StrBufNOTNULL;
                return 0;
        }
@@ -4868,10 +4767,7 @@ int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr)
        eptr = Buf->buf + Buf->BufUsed;
        xptr = LineBuf->buf + LineBuf->BufSize - 1;
 
-       while ((ptr <= eptr) && 
-              (*ptr != '\n') &&
-              (*ptr != '\r') )
-       {
+       while ((ptr <= eptr) && (*ptr != '\n') && (*ptr != '\r') ) {
                *optr = *ptr;
                optr++; ptr++;
                if (optr == xptr) {
@@ -4902,13 +4798,10 @@ int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr)
 }
 
 
-/**
- *  removes double slashes from pathnames
- *  Dir directory string to filter
- *  RemoveTrailingSlash allows / disallows trailing slashes
- */
-void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash)
-{
+// removes double slashes from pathnames
+// Dir directory string to filter
+// RemoveTrailingSlash allows / disallows trailing slashes
+void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash) {
        char *a, *b;
 
        a = b = Dir->buf;
@@ -4935,13 +4828,10 @@ void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash)
 }
 
 
-/*
- * Decode a quoted-printable encoded StrBuf buffer "in place"
- * This is possible because the decoded will always be shorter than the encoded
- * so we don't have to worry about the buffer being to small.
- */
-void StrBufDecodeQP(StrBuf *Buf)
-{
+// Decode a quoted-printable encoded StrBuf buffer "in place"
+// This is possible because the decoded will always be shorter than the encoded
+// so we don't have to worry about the buffer being to small.
+void StrBufDecodeQP(StrBuf *Buf) {
        if (!Buf) {                             // sanity check #1
                return;
        }