myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [SCM] GNU MyServer branch, master, updated. cfd1b4f4a9


From: Giuseppe Scrivano
Subject: [myserver-commit] [SCM] GNU MyServer branch, master, updated. cfd1b4f4a992238b6f7596def6cb9f8b293b618c
Date: Tue, 03 Nov 2009 16:38:44 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU MyServer".

The branch, master has been updated
       via  cfd1b4f4a992238b6f7596def6cb9f8b293b618c (commit)
      from  0ba7d30d08f068588c079035a8e1794ec5a77317 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------


commit cfd1b4f4a992238b6f7596def6cb9f8b293b618c
Author: Giuseppe Scrivano <address@hidden>
Date:   Tue Nov 3 17:38:31 2009 +0100

    Move the reallocation logic from `MemBuf::setLength' to a new method 
`MemBuf::setRealLength'.

diff --git a/myserver/include/base/mem_buff/mem_buff.h 
b/myserver/include/base/mem_buff/mem_buff.h
index 21e42ce..7e4f008 100644
--- a/myserver/include/base/mem_buff/mem_buff.h
+++ b/myserver/include/base/mem_buff/mem_buff.h
@@ -1,20 +1,20 @@
 /* -*- mode: c++ -*- */
 /*
-MyServer
-Copyright (C) 2002, 2003, 2004, 2009 Free Software Foundation, Inc.
-Copyright (C) 2004, Guinet Adrien (grainailleur)
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  MyServer
+  Copyright (C) 2002, 2003, 2004, 2009 Free Software Foundation, Inc.
+  Copyright (C) 2004, Guinet Adrien (grainailleur)
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifndef MEMBUF_H
@@ -34,89 +34,95 @@ using namespace std;
 class MemBuf
 {
 public:
-       MemBuf ();
-       MemBuf (const void* pAdr, u_int size);
-       MemBuf (const MemBuf& srcBuf);
-       MemBuf (MemBuf& srcBuf, int bCopy);
-       ~MemBuf ();
-
-       void setExternalBuffer (const void* pAdr, u_int size);
-       int setBuffer (const void* pAdr, u_int size);
-       void setLength (u_int newSize);
-
-       void addBuffer (const void* pAdr, u_int size);
-       void addBuffer (MemBuf *nmb);
-
-       int free ();
-
-       u_int find (char c, u_int start = 0);
-       u_int find (MemBuf *smb, u_int start = 0);
-       u_int find (const void* pAdr, u_int size, u_int start = 0);
-       void replace (char what, char by);
-       char& getAt (u_int nIndex);
-       char& operator[](u_int nIndex);
-
-       int getPart (u_int nStart, u_int nEnd, MemBuf& result);
-       int getPartAsString (u_int nStart, u_int nEnd, MemBuf& result);
-
-       char* getBuffersetLength (u_int newSize);
-
-       u_int getLength ();
-       u_int getRealLength ();
-
-       int isValid ();
-
-       char* getBuffer ();
-       operator const void* ();
-       MemBuf operator+ (MemBuf& src);
-       MemBuf operator+ (const char* src);
-       const MemBuf& operator+= (MemBuf& add);
-       const MemBuf& operator+= (const char* pStr);
-       const MemBuf& operator+= (char c) ;
-
-       MemBuf& operator<< (const char* pSrc) ;
-       MemBuf& operator<< (int i) ;
-       MemBuf& operator<< (unsigned int i) ;
-       MemBuf& operator<< (long i) ;
-       MemBuf& operator<< (unsigned long i);
-       MemBuf& operator<< (char c) ;
-       MemBuf& operator<< (unsigned char c) ;
-       MemBuf& operator<< (const MemBuf &src) ;
-       MemBuf& operator<< (const string &src) ;
-       MemBuf& operator= (const MemBuf& src) ;
-       MemBuf& operator= (const char* src);
+  MemBuf ();
+  MemBuf (const void* pAdr, u_int size);
+  MemBuf (const MemBuf& srcBuf);
+  MemBuf (MemBuf& srcBuf, int bCopy);
+  ~MemBuf ();
+
+  void setExternalBuffer (const void* pAdr, u_int size);
+  int setBuffer (const void* pAdr, u_int size);
+  void setLength (u_int newSize);
+  void setRealLength (u_int newSize);
+
+  void addBuffer (const void* pAdr, u_int size);
+  void addBuffer (MemBuf *nmb);
+
+  int free ();
+
+  u_int find (char c, u_int start = 0);
+  u_int find (MemBuf *smb, u_int start = 0);
+  u_int find (const void* pAdr, u_int size, u_int start = 0);
+  void replace (char what, char by);
+  char& getAt (u_int nIndex);
+  char& operator[](u_int nIndex);
+
+  int getPart (u_int nStart, u_int nEnd, MemBuf& result);
+  int getPartAsString (u_int nStart, u_int nEnd, MemBuf& result);
+
+  char* getBuffersetLength (u_int newSize);
+
+  u_int getLength ();
+  u_int getRealLength ();
+
+  int isValid ();
+
+  char* getBuffer ();
+  operator const void* ();
+  MemBuf operator+ (MemBuf& src);
+  MemBuf operator+ (const char* src);
+  const MemBuf& operator+= (MemBuf& add);
+  const MemBuf& operator+= (const char* pStr);
+  const MemBuf& operator+= (char c) ;
+
+  MemBuf& operator<< (const char* pSrc) ;
+  MemBuf& operator<< (int i) ;
+  MemBuf& operator<< (unsigned int i) ;
+  MemBuf& operator<< (long i) ;
+  MemBuf& operator<< (unsigned long i);
+  MemBuf& operator<< (char c) ;
+  MemBuf& operator<< (unsigned char c) ;
+  MemBuf& operator<< (const MemBuf &src) ;
+  MemBuf& operator<< (const string &src) ;
+  MemBuf& operator= (const MemBuf& src) ;
+  MemBuf& operator= (const char* src);
+
+  void hashMD5(const void* pAdr, u_int nSize);
+  void hashCRC (const void* pAdr, u_int nSize);
+  void hex (const void* pAdr, u_int nSize);
+  void uintToStr (u_int i);
+  void intToStr (int i);
+  u_int strToUint (const char* pAdr);
+  unsigned char hexCharToNumber (unsigned char c);
+  MemBuf hexToData (const void* pAdr, u_int nSize);
+  int strToInt (const char* pAdr);
+  void hex (MemBuf& membuf) ;
+  void hashMD5 (MemBuf& membuf);
+  void hashCRC (MemBuf& membuf);
+  void uintToStr (u_int i, char* pBufToUse, u_int nBufSize) ;
+  void xIntToStr (u_int i, int bNegative, char* pBufToUse, u_int nBufSize);
+  void intToStr (int i, char* pBufToUse, u_int nBufSize);
+
+  u_int getSizeLimit () {return nSizeLimit;}
+  void setSizeLimit (u_int newSize) {nSizeLimit = newSize;}
+
+protected:
+
 
   /* The maximun size that the buffer can reached ; 0 if none.  */
-       u_int nSizeLimit;
+  u_int nSizeLimit;
 
-  /* Minimun size of new allocated blocks during addings.
+  /* Minimum size of new allocated blocks during addings.
    * We assume that nBlockLength < nSizeLimit.  */
-       u_int nBlockLength; 
-
-       void hashMD5(const void* pAdr, u_int nSize);
-       void hashCRC (const void* pAdr, u_int nSize);
-       void hex (const void* pAdr, u_int nSize);
-       void uintToStr (u_int i);
-       void intToStr (int i);
-       u_int strToUint (const char* pAdr);
-       unsigned char hexCharToNumber (unsigned char c);
-       MemBuf hexToData (const void* pAdr, u_int nSize);
-       int strToInt (const char* pAdr);
-       void hex (MemBuf& membuf) ;
-       void hashMD5 (MemBuf& membuf);
-       void hashCRC (MemBuf& membuf);
-       void uintToStr (u_int i, char* pBufToUse, u_int nBufSize) ;
-       void xIntToStr (u_int i, int bNegative, char* pBufToUse, u_int 
nBufSize);
-       void intToStr (int i, char* pBufToUse, u_int nBufSize);
-
-protected:
-       void xIntToStr (u_int i, int bNegative);
-       void allocBuffer (u_int size);
-       char *buffer;
-       u_int nSize;
-       u_int nRealSize;
-       int bCanDelete;
-       static u_int crc32Table[256];
+  u_int nBlockLength;
+
+  void xIntToStr (u_int i, int bNegative);
+  void allocBuffer (u_int size);
+  char *buffer;
+  u_int nSize;
+  u_int nRealSize;
+  int bCanDelete;
+  static u_int crc32Table[256];
 };
 
 #endif
diff --git a/myserver/src/base/mem_buff/mem_buff.cpp 
b/myserver/src/base/mem_buff/mem_buff.cpp
index e6367dc..3b8b4c3 100644
--- a/myserver/src/base/mem_buff/mem_buff.cpp
+++ b/myserver/src/base/mem_buff/mem_buff.cpp
@@ -214,9 +214,9 @@ void MemBuf::addBuffer (const void* pAdr, u_int size)
   if (nNewSize > nRealSize)
     {
       if (nNewSize - nRealSize < nBlockLength)
-        setLength (nRealSize + nBlockLength);
+        setRealLength (nRealSize + nBlockLength);
       else
-        setLength (nNewSize);
+        setRealLength (nNewSize);
     }
 
   const u_int nAllowedSize = nRealSize - nSize;
@@ -325,45 +325,52 @@ int MemBuf::getPartAsString (u_int nStart, u_int nEnd, 
MemBuf& result)
 }
 
 /*!
- * Set the length of the internal buffer.
+ * Set the real length of the internal buffer.  Ensure the buffer is
+ * at least newSize bytes, if this value is bigger than nSizeLimit
+ * then the latter is used.
+ *
  * If the length is smallest than the existing one, no reallocation is done.
  * if it's biggest, a reallocation is done until nSizeLimit is reached.
  */
-void MemBuf::setLength (u_int newSize)
+void MemBuf::setRealLength (u_int newSize)
 {
-  if (newSize == nRealSize)
+  if (newSize <= nRealSize)
     return;
 
-  if (newSize < nRealSize)
+  if (nSizeLimit != 0 && newSize > nSizeLimit)
     {
-      nSize = newSize;
-      return;
+      newSize = nSizeLimit;
+      if (newSize == nRealSize)
+        return;
     }
 
-  if (newSize > nRealSize)
+  if (buffer == NULL || nRealSize == 0)
     {
-      if (nSizeLimit != 0 && newSize > nSizeLimit)
-        {
-          newSize = nSizeLimit;
-          if (newSize == nRealSize)
-            return;
-        }
-
-      if (buffer == NULL || nRealSize == 0)
-        {
-          allocBuffer (newSize);
-          return;
-        }
-
-      char* temp = mealloc (newSize);
-      memcpy (temp, buffer, nRealSize);
-      if (bCanDelete)
-        mefree (buffer);
-      buffer = temp;
-      nRealSize = newSize;
+      allocBuffer (newSize);
       return;
     }
 
+  char* temp = mealloc (newSize);
+  memcpy (temp, buffer, nRealSize);
+  if (bCanDelete)
+    mefree (buffer);
+  buffer = temp;
+  nRealSize = newSize;
+}
+
+/*!
+ * Set the cursor in the internal buffer.
+ * If the specified position is bigger than the buffer size then a reallocation
+ * is done.
+ */
+void MemBuf::setLength (u_int newSize)
+{
+  setRealLength (newSize);
+
+  if (newSize < nRealSize)
+    nSize = newSize;
+  else
+    nSize = nRealSize;
 }
 
 
@@ -372,7 +379,7 @@ void MemBuf::setLength (u_int newSize)
 void MemBuf::hex (const void* pAdr, u_int nSize)
 {
   const u_int nFinalSize = nSize * 2;
-  setLength (nFinalSize + 1);
+  setRealLength (nFinalSize + 1);
   const char* hex_chars = "0123456789abcdef";
   for (u_int i = 0; i < nSize; i++)
     {
@@ -402,7 +409,7 @@ MemBuf MemBuf::hexToData (const void* pAdr, u_int nSize)
     return MemBuf ("", 1);
   MemBuf memFinal;
   memFinal.bCanDelete = false;
-  memFinal.setLength (nSize >> 1);
+  memFinal.setRealLength (nSize >> 1);
   const char* pTmp = (const char*) pAdr;
   const char* pEnd = pTmp + nSize;
   for ( ; pTmp < pEnd; pTmp += 2)
@@ -415,7 +422,7 @@ MemBuf MemBuf::hexToData (const void* pAdr, u_int nSize)
 void MemBuf::hashMD5(const void* pAdr, u_int nSize)
 {
   Md5 md5;
-  setLength (16);
+  setRealLength (16);
 
   md5.init ();
   md5.update ((char*) pAdr, nSize);
@@ -426,7 +433,7 @@ void MemBuf::hashMD5(const void* pAdr, u_int nSize)
 /*! CRC hashing function.  */
 void MemBuf::hashCRC (const void* pAdr, u_int nSize)
 {
-  setLength (4);
+  setRealLength (4);
   u_int* nCrc32 = (u_int*) getBuffer ();
   *nCrc32 = 0xFFFFFFFF;
   for (u_int i = 0; i < nSize; i++)
@@ -448,7 +455,7 @@ void MemBuf::xIntToStr (u_int i, int bNegative)
       nSize = 1;
       return;
     }
-  setLength (12);
+  setRealLength (12);
   do
     {
       *this << (char) ('0' + i % 10);
diff --git a/myserver/src/protocol/ftp/ftp.cpp 
b/myserver/src/protocol/ftp/ftp.cpp
index 476ddde..c094ad4 100644
--- a/myserver/src/protocol/ftp/ftp.cpp
+++ b/myserver/src/protocol/ftp/ftp.cpp
@@ -747,7 +747,7 @@ DEFINE_THREAD (SendAsciiFile, pParam)
       int nLineLength = 0;
       std::string sLine;
       MemBuf buffer, secondaryBuffer;
-      buffer.setLength (1024);
+      buffer.setRealLength (1024);
       while (filesize != 0)
         {
           memset (buffer.getBuffer (), 0, buffer.getRealLength ());
@@ -983,7 +983,7 @@ DEFINE_THREAD (SendImageFile, pParam)
       pFtpuserData->m_nFileSize = filesize;
 
       MemBuf secondaryBuffer;
-      secondaryBuffer.setLength (1024);
+      secondaryBuffer.setRealLength (1024);
       while (filesize != 0)
         {
           nBufferSize =
@@ -1160,7 +1160,7 @@ DEFINE_THREAD (ReceiveAsciiFile, pParam)
         }
 
       MemBuf buffer, secondaryBuffer;
-      buffer.setLength (1024);
+      buffer.setRealLength (1024);
       memset (buffer.getBuffer (), 0, buffer.getRealLength ());
       char *pLine = NULL;
       int nLineLength = 0;
@@ -1357,7 +1357,7 @@ DEFINE_THREAD (ReceiveImageFile, pParam)
         }
       u_long nbr;
       MemBuf buffer;
-      buffer.setLength (1024);
+      buffer.setRealLength (1024);
       memset (buffer.getBuffer (), 0, buffer.getRealLength ());
       while (pFtpuserData->m_pDataConnection->socket->read (buffer.getBuffer 
(),
                                                             (u_long) buffer.
diff --git a/myserver/src/server/clients_thread.cpp 
b/myserver/src/server/clients_thread.cpp
index d1827fb..0ba0b08 100644
--- a/myserver/src/server/clients_thread.cpp
+++ b/myserver/src/server/clients_thread.cpp
@@ -122,10 +122,11 @@ DEFINE_THREAD (clients_thread, pParam)
   ct->threadIsStopped = false;
   ct->bufferSize = ct->server->getBuffersize ();
 
-  ct->buffer.setLength (ct->bufferSize);
-  ct->buffer.nSizeLimit = ct->bufferSize;
-  ct->secondaryBuffer.setLength (ct->bufferSize);
-  ct->secondaryBuffer.nSizeLimit = ct->bufferSize;
+  ct->buffer.setRealLength (ct->bufferSize);
+  ct->buffer.setSizeLimit (ct->bufferSize);
+
+  ct->secondaryBuffer.setRealLength (ct->bufferSize);
+  ct->secondaryBuffer.setSizeLimit (ct->bufferSize);
 
   /* Built-in protocols will be initialized at the first use.  */
   ct->initialized = true;
@@ -269,7 +270,7 @@ int ClientsThread::controlConnections ()
   if (err == -1 && !server->deleteConnection (c))
     return 0;
 
-  buffer.setLength (dataRead + err);
+  buffer.setRealLength (dataRead + err);
   c->setForceControl (0);
 
   /* Refresh with the right value.  */
diff --git a/myserver/tests/test_ftp.cpp b/myserver/tests/test_ftp.cpp
index 3146dc0..9fad1a5 100644
--- a/myserver/tests/test_ftp.cpp
+++ b/myserver/tests/test_ftp.cpp
@@ -25,28 +25,28 @@
 
 class TestFtp : public CppUnit::TestFixture
 {
-       CPPUNIT_TEST_SUITE (TestFtp);
-       CPPUNIT_TEST (testEscapeTelnet);
-       CPPUNIT_TEST_SUITE_END ();
+  CPPUNIT_TEST_SUITE (TestFtp);
+  CPPUNIT_TEST (testEscapeTelnet);
+  CPPUNIT_TEST_SUITE_END ();
 
 public:
-       void setUp () {}
-       void tearDown () {}
-       void testEscapeTelnet ()
-       {
-               MemBuf inBuf, outBuf;
-               inBuf << "\377\366RE\377\374TTR 
abc\377\377def\015\377\376\012\012";
-
-               Ftp ftp;
-               ftp.escapeTelnet (inBuf, outBuf);
-               char szOut[128], szExpected[128];
-               memset (szOut, 0, 128);
-               strncpy (szOut, outBuf.getBuffer (), outBuf.getLength ());
-               memset (szExpected, 0, 128);
-               strcpy (szExpected, "RETR abc\377def\015\012");
-
-               CPPUNIT_ASSERT (strcmp (szOut, szExpected) == 0);
-       }
+  void setUp () {}
+  void tearDown () {}
+  void testEscapeTelnet ()
+  {
+    MemBuf inBuf, outBuf;
+    inBuf << "\377\366RE\377\374TTR abc\377\377def\015\377\376\012\012";
+
+    Ftp ftp;
+    ftp.escapeTelnet (inBuf, outBuf);
+    char szOut[128], szExpected[128];
+    memset (szOut, 0, 128);
+    strncpy (szOut, outBuf.getBuffer (), outBuf.getLength ());
+    memset (szExpected, 0, 128);
+    strcpy (szExpected, "RETR abc\377def\015\012");
+
+    CPPUNIT_ASSERT (strcmp (szOut, szExpected) == 0);
+  }
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION (TestFtp);
diff --git a/myserver/tests/test_mem_buff.cpp b/myserver/tests/test_mem_buff.cpp
index 5d72d63..5547097 100644
--- a/myserver/tests/test_mem_buff.cpp
+++ b/myserver/tests/test_mem_buff.cpp
@@ -54,18 +54,20 @@ public:
     delete memBuff;
   }
 
-
   void testLength ()
   {
     u_int len = 10;
-    memBuff->setLength (len);
-    CPPUNIT_ASSERT_EQUAL (memBuff->getRealLength (), len);
-
+    memBuff->setRealLength (len);
     CPPUNIT_ASSERT_EQUAL (memBuff->getLength (), 0u);
+    CPPUNIT_ASSERT_EQUAL (memBuff->getRealLength (), len);
 
     *memBuff << (const char*)"1234567890";
 
     CPPUNIT_ASSERT_EQUAL (memBuff->getLength (), 10u);
+
+    memBuff->setLength (len / 2);
+    CPPUNIT_ASSERT_EQUAL (memBuff->getRealLength (), len);
+    CPPUNIT_ASSERT_EQUAL (memBuff->getLength (), len / 2);
   }
 
   void testFind ()

-----------------------------------------------------------------------

Summary of changes:
 myserver/include/base/mem_buff/mem_buff.h |  194 +++++++++++++++--------------
 myserver/src/base/mem_buff/mem_buff.cpp   |   73 ++++++-----
 myserver/src/protocol/ftp/ftp.cpp         |    8 +-
 myserver/src/server/clients_thread.cpp    |   11 +-
 myserver/tests/test_ftp.cpp               |   40 +++---
 myserver/tests/test_mem_buff.cpp          |   10 +-
 6 files changed, 176 insertions(+), 160 deletions(-)


hooks/post-receive
-- 
GNU MyServer




reply via email to

[Prev in Thread] Current Thread [Next in Thread]