* client_read_sslblob(): fix buffer handling, clean up the read-buffer if neccesary.
authorWilfried Goesgens <dothebart@citadel.org>
Tue, 31 Aug 2010 17:58:14 +0000 (19:58 +0200)
committerWilfried Goesgens <dothebart@citadel.org>
Tue, 31 Aug 2010 17:58:14 +0000 (19:58 +0200)
citadel/modules/crypto/serv_crypto.c

index a5b5632398e4bd7370feb63b52f12b9cefba7fe4..30309f896c29618446c108f62795b43b5a892491 100644 (file)
@@ -559,39 +559,71 @@ int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int
 
 int client_read_sslblob(StrBuf *Target, long bytes, int timeout)
 {
-       long bufremain;
        long baselen;
+       long RemainRead;
        int retval = 0;
        CitContext *CCC = CC;
 
        baselen = StrLength(Target);
 
-       if (CCC->Pos == NULL)
-               CCC->Pos = ChrPtr(CCC->ReadBuf);
-       bufremain = StrLength(CCC->ReadBuf) - 
-               (CCC->Pos - ChrPtr(CCC->ReadBuf));
+       if (StrLength(CCC->ReadBuf) > 0)
+       {
+               long RemainLen;
+               long TotalLen;
+               const char *pchs;
+
+               if (CCC->Pos == NULL)
+                       CCC->Pos = ChrPtr(CCC->ReadBuf);
+               pchs = ChrPtr(CCC->ReadBuf);
+               TotalLen = StrLength(CCC->ReadBuf);
+               RemainLen = TotalLen - (pchs - CCC->Pos);
+               if (RemainLen > bytes)
+                       RemainLen = bytes;
+               if (RemainLen > 0)
+               {
+                       StrBufAppendBufPlain(Target, 
+                                            CCC->Pos, 
+                                            RemainLen, 0);
+                       CCC->Pos += RemainLen;
+               }
+               if ((ChrPtr(CCC->ReadBuf) + StrLength(CCC->ReadBuf)) <= CCC->Pos)
+               {
+                       CCC->Pos = NULL;
+                       FlushStrBuf(CCC->ReadBuf);
+               }
+       }
+
+       if (StrLength(Target) >= bytes + baselen)
+               return 1;
 
-       if (bytes < bufremain)
-               bufremain = bytes;
-       StrBufAppendBufPlain(Target, CCC->Pos, bufremain, 0);
-       StrBufCutLeft(CCC->ReadBuf, bufremain);
        CCC->Pos = NULL;
 
-       if (bytes > bufremain) 
+       while ((StrLength(Target) < bytes + baselen) &&
+              (retval >= 0))
        {
-               while ((StrLength(CCC->ReadBuf) + StrLength(Target) < bytes + baselen) &&
-                      (retval >= 0))
-                       retval = client_read_sslbuffer(CCC->ReadBuf, timeout);
+               retval = client_read_sslbuffer(CCC->ReadBuf, timeout);
                if (retval >= 0) {
+                       RemainRead = bytes - (StrLength (Target) - baselen);
+                       if (RemainRead > StrLength(CCC->ReadBuf))
+                       {
+                               StrBufAppendBufPlain(
+                                       Target, 
+                                       ChrPtr(CCC->ReadBuf), 
+                                       RemainRead, 0);
+                               CCC->Pos = ChrPtr(CCC->ReadBuf) + RemainRead;
+                               break;
+                       }
                        StrBufAppendBuf(Target, CCC->ReadBuf, 0); /* todo: Buf > bytes? */
-                       return 1;
+                       FlushStrBuf(CCC->ReadBuf);
                }
-               else {
+               else 
+               {
+                       FlushStrBuf(CCC->ReadBuf);
                        return -1;
+       
                }
        }
-       else 
-               return 1;
+       return 1;
 }