Browse Source

* src/Util.cc (isPowerOf): New function.
* src/Util.h (isPowerOf): New function.
* src/PeerMessageUtil.cc (checkLength): Added a check for length
whether or not it is power of 2.

Tatsuhiro Tsujikawa 19 năm trước cách đây
mục cha
commit
fc2307d47c
12 tập tin đã thay đổi với 61 bổ sung57 xóa
  1. 5 0
      ChangeLog
  2. 4 4
      src/CookieBox.cc
  3. 5 5
      src/CookieBox.h
  4. 2 2
      src/FtpConnection.cc
  5. 2 2
      src/FtpConnection.h
  6. 2 16
      src/Peer.h
  7. 7 2
      src/PeerMessageUtil.cc
  8. 2 0
      src/PeerMessageUtil.h
  9. 1 1
      src/Piece.h
  10. 0 7
      src/TorrentMan.h
  11. 21 9
      src/Util.cc
  12. 10 9
      src/Util.h

+ 5 - 0
ChangeLog

@@ -24,6 +24,11 @@
 
 	* src/SendMessageQueue.cc (cancelAllRequest): Fixed.
 
+	* src/Util.cc (isPowerOf): New function.
+	* src/Util.h (isPowerOf): New function.
+	* src/PeerMessageUtil.cc (checkLength): Added a check for length
+	whether or not it is power of 2.
+	
 2006-03-28  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Added new class SendMessageQueue that includes PendingMessages and

+ 4 - 4
src/CookieBox.cc

@@ -30,13 +30,13 @@ void CookieBox::add(const Cookie& cookie) {
   cookies.push_back(cookie);
 }
 
-void CookieBox::add(string cookieStr) {
+void CookieBox::add(const string& cookieStr) {
   Cookie c;
   parse(c, cookieStr);
   cookies.push_back(c);
 }
 
-void CookieBox::setField(Cookie& cookie, string name, string value) const {
+void CookieBox::setField(Cookie& cookie, const string& name, const string& value) const {
   if(name.size() == string("secure").size() &&
      strcasecmp(name.c_str(), "secure") == 0) {
     cookie.secure = true;
@@ -52,7 +52,7 @@ void CookieBox::setField(Cookie& cookie, string name, string value) const {
   }
 }
 
-void CookieBox::parse(Cookie& cookie, string cookieStr) const {
+void CookieBox::parse(Cookie& cookie, const string& cookieStr) const {
   cookie.clear();
   Strings terms;
   Util::slice(terms, cookieStr, ';');
@@ -63,7 +63,7 @@ void CookieBox::parse(Cookie& cookie, string cookieStr) const {
   }
 }
 
-Cookies CookieBox::criteriaFind(string host, string dir, bool secure) const {
+Cookies CookieBox::criteriaFind(const string& host, const string& dir, bool secure) const {
   Cookies result;
   for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
     const Cookie& c = *itr;

+ 5 - 5
src/CookieBox.h

@@ -37,7 +37,7 @@ public:
   string domain;
   bool secure;
 public:
-  Cookie(string name, string value, string expires, string path, string domain, bool secure):name(name), value(value), expires(expires), path(path), domain(domain), secure(secure) {}
+  Cookie(const string& name, const string& value, const string& expires, const string& path, const string& domain, bool secure):name(name), value(value), expires(expires), path(path), domain(domain), secure(secure) {}
   Cookie():secure(false) {}
   ~Cookie() {}
   string toString() const {
@@ -54,15 +54,15 @@ typedef deque<Cookie> Cookies;
 class CookieBox {
 private:
   Cookies cookies;
-  void setField(Cookie& cookie, string name, string value) const;
+  void setField(Cookie& cookie, const string& name, const string& value) const;
 public:
   CookieBox();
   ~CookieBox();
   void clear();
   void add(const Cookie& cookie);
-  void add(string cookieStr);
-  void parse(Cookie& cookie, string cookieStr) const;
-  Cookies criteriaFind(string host, string dir, bool secure) const;
+  void add(const string& cookieStr);
+  void parse(Cookie& cookie, const string& cookieStr) const;
+  Cookies criteriaFind(const string& host, const string& dir, bool secure) const;
 };
 
 #endif // _D_COOKIE_BOX_H_

+ 2 - 2
src/FtpConnection.cc

@@ -108,7 +108,7 @@ void FtpConnection::sendRetr() const {
   socket->writeData(request);
 }
 
-int FtpConnection::getStatus(string response) const {
+int FtpConnection::getStatus(const string& response) const {
   int status;
   // When the response is not like "%d %*s",
   // we return 0.
@@ -123,7 +123,7 @@ int FtpConnection::getStatus(string response) const {
   }
 }
 
-bool FtpConnection::isEndOfResponse(int status, string response) const {
+bool FtpConnection::isEndOfResponse(int status, const string& response) const {
   if(response.size() <= 4) {
     return false;
   }

+ 2 - 2
src/FtpConnection.h

@@ -42,8 +42,8 @@ private:
 
   string strbuf;
 
-  int getStatus(string response) const;
-  bool isEndOfResponse(int status, string response) const;
+  int getStatus(const string& response) const;
+  bool isEndOfResponse(int status, const string& response) const;
   bool bulkReceiveResponse(pair<int, string>& response);
 public:
   FtpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger);

+ 2 - 16
src/Peer.h

@@ -45,9 +45,7 @@ public:
   int cuid;
 private:
   char peerId[PEER_ID_LENGTH];
-  //unsigned char* bitfield;
   BitfieldMan* bitfield;
-  //int bitfieldLength;
   long long int peerUpload;
   long long int peerDownload;
   int pieceLength;
@@ -58,7 +56,7 @@ public:
     amChocking(true), amInterested(false),
     peerChoking(true), peerInterested(false),
     tryCount(0), error(0), cuid(0),
-    bitfield(NULL),// bitfieldLength(0),
+    bitfield(NULL),
     peerUpload(0), peerDownload(0),
     pieceLength(pieceLength), totalLength(totalLength) {
     this->bitfield = new BitfieldMan(pieceLength, totalLength);
@@ -66,7 +64,7 @@ public:
 
   ~Peer() {
     if(bitfield != NULL) {
-      delete /*[]*/ bitfield;
+      delete bitfield;
     }
   }
 
@@ -77,22 +75,10 @@ public:
   
   void setBitfield(const unsigned char* bitfield, int bitfieldLength) {
     this->bitfield->setBitfield(bitfield, bitfieldLength);
-    /*
-    if(this->bitfield != NULL) {
-      delete [] this->bitfield;
-    }
-    this->bitfieldLength = bitfieldLength;
-    this->bitfield = new unsigned char[this->bitfieldLength];
-    memcpy(this->bitfield, bitfield, this->bitfieldLength);
-    */
   }
   const unsigned char* getBitfield() const { return bitfield->getBitfield(); }
   int getBitfieldLength() const { return bitfield->getBitfieldLength(); }
 
-  /*
-  void initBitfield(int pieces);
-  */
-
   /**
    * operation = 1: set index-th bit 1
    * operation = 0: set index-th bit 0

+ 7 - 2
src/PeerMessageUtil.cc

@@ -151,8 +151,13 @@ void PeerMessageUtil::checkPieceOffset(const PeerMessage* message, int pieceLeng
 }
 
 void PeerMessageUtil::checkLength(const PeerMessage* message) {
-  if(message->getLength() > 128*1024) {
-    throw new DlAbortEx("too large length %d > 128KB", message->getLength());
+  if(message->getLength() > MAX_BLOCK_LENGTH) {
+    throw new DlAbortEx("too large length %d > %dKB", message->getLength(),
+			MAX_BLOCK_LENGTH/1024);
+  }
+  if(!Util::isPowerOf(message->getLength(), 2)) {
+    throw new DlAbortEx("invalid length %d, which is not power of 2",
+			message->getLength());
   }
 }
 

+ 2 - 0
src/PeerMessageUtil.h

@@ -24,6 +24,8 @@
 
 #include "PeerConnection.h"
 
+#define MAX_BLOCK_LENGTH (128*1024)
+
 class PeerMessageUtil {
 private:
   PeerMessageUtil() {}

+ 1 - 1
src/Piece.h

@@ -25,7 +25,7 @@
 #include "BitfieldMan.h"
 #include "common.h"
 
-#define BLOCK_LENGTH 16*1024
+#define BLOCK_LENGTH (16*1024)
 
 class Piece {
 private:

+ 0 - 7
src/TorrentMan.h

@@ -35,18 +35,11 @@
 
 using namespace std;
 
-#define DEFAULT_BLOCK_LEN 16*1024;
-#define MAX_BLOCK_LEN 128*1024;
-
 #define INFO_HASH_LENGTH 20
-
-#define IS_NULL_PIECE(X) (X.index == 0 && X.length == 0)
-
 #define DEFAULT_ANNOUNCE_INTERVAL 300
 #define DEFAULT_ANNOUNCE_MIN_INTERVAL 300
 #define MAX_PEERS 55
 #define MAX_PEER_UPDATE 15
-
 #define MAX_PEER_LIST_SIZE 250
 #define END_GAME_PIECE_NUM 20
 #define MAX_PEER_ERROR 5

+ 21 - 9
src/Util.cc

@@ -59,7 +59,7 @@ string Util::llitos(long long int value, bool comma)
   return str;
 }
 
-string Util::trim(string src) {
+string Util::trim(const string& src) {
   string::size_type sp = src.find_first_not_of(" ");
   string::size_type ep = src.find_last_not_of(" ");
   if(sp == string::npos || ep == string::npos) {
@@ -69,7 +69,7 @@ string Util::trim(string src) {
   }
 }
 
-void Util::split(pair<string, string>& hp, string src, char delim) {
+void Util::split(pair<string, string>& hp, const string& src, char delim) {
   hp.first = "";
   hp.second = "";
   string::size_type p = src.find(delim);
@@ -90,7 +90,7 @@ long long int Util::difftv(struct timeval tv1, struct timeval tv2) {
 	  tv1.tv_usec-tv2.tv_usec);
 }
 
-void Util::slice(Strings& result, string src, char delim) {
+void Util::slice(Strings& result, const string& src, char delim) {
   string::size_type p = 0;
   while(1) {
     string::size_type np = src.find(delim, p);
@@ -107,7 +107,7 @@ void Util::slice(Strings& result, string src, char delim) {
   } 
 }
 
-bool Util::startsWith(string target, string part) {
+bool Util::startsWith(const string& target, const string& part) {
   if(target.size() < part.size()) {
     return false;
   }
@@ -121,7 +121,7 @@ bool Util::startsWith(string target, string part) {
   }
 }
 
-bool Util::endsWith(string target, string part) {
+bool Util::endsWith(const string& target, const string& part) {
   if(target.size() < part.size()) {
     return false;
   }
@@ -135,7 +135,7 @@ bool Util::endsWith(string target, string part) {
   }
 }
 
-string Util::replace(string target, string oldstr, string newstr) {
+string Util::replace(const string& target, const string& oldstr, const string& newstr) {
   if(target == "" || oldstr == "" ) {
     return target;
   }
@@ -186,17 +186,17 @@ string Util::toHex(const unsigned char* src, int len) {
   return hex;
 }
 
-FILE* Util::openFile(string filename, string mode) {
+FILE* Util::openFile(const string& filename, const string& mode) {
   FILE* file = fopen(filename.c_str(), mode.c_str());
   return file;
 }
 
-void Util::fileCopy(string dest, string src) {
+void Util::fileCopy(const string& dest, const string& src) {
   File file(src);
   rangedFileCopy(dest, src, 0, file.size());
 }
 
-void Util::rangedFileCopy(string dest, string src, long long int srcOffset, long long int length) {
+void Util::rangedFileCopy(const string& dest, const string& src, long long int srcOffset, long long int length) {
   int bufSize = 4096;
   char buf[bufSize];
   int destFd = -1;
@@ -246,3 +246,15 @@ void Util::rangedFileCopy(string dest, string src, long long int srcOffset, long
   }
 }
 
+bool Util::isPowerOf(int num, int base) {
+  if(base <= 0) { return false; }
+  if(base == 1) { return true; }
+
+  while(num%base == 0) {
+    num /= base;
+    if(num == 1) {
+      return true;
+    }
+  }
+  return false;
+}

+ 10 - 9
src/Util.h

@@ -34,7 +34,7 @@ using namespace std;
 
 class Util {
 public:
-  static void split(pair<string, string>& hp, string src, char delim);
+  static void split(pair<string, string>& hp, const string& src, char delim);
   static string llitos(long long int value, bool comma = false);
   static string itos(int value, bool comma = false);
   /**
@@ -47,26 +47,27 @@ public:
    * Take a string src which is a deliminated list and add its elements
    * into result. result is not cleared before conversion begins.
    */
-  static void slice(Strings& result, string src, char delim);
+  static void slice(Strings& result, const string& src, char delim);
   
-  static string trim(string src);
+  static string trim(const string& src);
 
-  static bool startsWith(string target, string part);
+  static bool startsWith(const string& target, const string& part);
 
-  static bool endsWith(string target, string part);
+  static bool endsWith(const string& target, const string& part);
 
-  static string replace(string target, string oldstr, string newstr);
+  static string replace(const string& target, const string& oldstr, const string& newstr);
 
   static string urlencode(const unsigned char* target, int len);
 
   static string toHex(const unsigned char* src, int len);
 
-  static FILE* openFile(string filename, string mode);
+  static FILE* openFile(const string& filename, const string& mode);
 
-  static void fileCopy(string destFile, string src);
+  static void fileCopy(const string& destFile, const string& src);
 
-  static void rangedFileCopy(string destFile, string src, long long int srcOffset, long long int length);
+  static void rangedFileCopy(const string& destFile, const string& src, long long int srcOffset, long long int length);
 
+  static bool isPowerOf(int num, int base);
 };
 
 #endif // _D_UTIL_H_