From: Wilfried Goesgens Date: Tue, 31 Aug 2010 17:58:14 +0000 (+0200) Subject: * client_read_sslblob(): fix buffer handling, clean up the read-buffer if neccesary. X-Git-Tag: v8.01~808 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=d91993b02381c683f14bfc6f45476c68ef45662a * client_read_sslblob(): fix buffer handling, clean up the read-buffer if neccesary. --- diff --git a/citadel/modules/crypto/serv_crypto.c b/citadel/modules/crypto/serv_crypto.c index a5b563239..30309f896 100644 --- a/citadel/modules/crypto/serv_crypto.c +++ b/citadel/modules/crypto/serv_crypto.c @@ -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; }