Quellcode durchsuchen

2008-08-24 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Added ntoh64 and hton64 as inline functions.
	* src/Util.cc
	* test/UtilTest.cc
Tatsuhiro Tsujikawa vor 17 Jahren
Ursprung
Commit
02dde388b8
3 geänderte Dateien mit 34 neuen und 0 gelöschten Zeilen
  1. 6 0
      ChangeLog
  2. 13 0
      src/Util.h
  3. 15 0
      test/UtilTest.cc

+ 6 - 0
ChangeLog

@@ -1,3 +1,9 @@
+2008-08-24  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Added ntoh64 and hton64 as inline functions.
+	* src/Util.cc
+	* test/UtilTest.cc
+
 2008-08-24  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Added sqlite3 depenency and Firefox3 cookie support.

+ 13 - 0
src/Util.h

@@ -61,6 +61,19 @@ class FileEntry;
 
 #define DIV_FLOOR(X,Y) ((X)/(Y)+((X)%(Y)? 1:0))
 
+#ifdef WORDS_BIGENDIAN
+inline uint64_t ntoh64(uint64_t x) { return x; }
+inline uint64_t hton64(uint64_t x) { return x; }
+#else // !WORDS_BIGENDIAN
+inline uint64_t byteswap64(uint64_t x) {
+  uint64_t v1 = ntohl(x & 0x00000000ffffffff);
+  uint64_t v2 = ntohl(x >> 32);
+  return (v1 << 32)|v2;
+}
+inline uint64_t ntoh64(uint64_t x) { return byteswap64(x); }
+inline uint64_t hton64(uint64_t x) { return byteswap64(x); }
+#endif // !WORDS_BIGENDIAN
+
 class Util {
 public:
   static void split(std::pair<std::string, std::string>& hp,

+ 15 - 0
test/UtilTest.cc

@@ -46,6 +46,7 @@ class UtilTest:public CppUnit::TestFixture {
   CPPUNIT_TEST(testItos);
   CPPUNIT_TEST(testUitos);
   CPPUNIT_TEST(testHttpGMT);
+  CPPUNIT_TEST(testNtoh64);
   CPPUNIT_TEST_SUITE_END();
 private:
 
@@ -85,6 +86,7 @@ public:
   void testItos();
   void testUitos();
   void testHttpGMT();
+  void testNtoh64();
 };
 
 
@@ -681,4 +683,17 @@ void UtilTest::testHttpGMT()
 		       Util::httpGMT("Tue, 2008/10/10 23:33:33 UTC"));
 }
 
+void UtilTest::testNtoh64()
+{
+  uint64_t x = 0xff00ff00ee00ee00LL;
+#ifdef WORDS_BIGENDIAN
+  CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
+  CPPUNIT_ASSERT_EQUAL(x, hton64(x));
+#else // !WORDS_BIGENDIAN
+  uint64_t y = 0x00ee00ee00ff00ffLL;
+  CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
+  CPPUNIT_ASSERT_EQUAL(x, hton64(y));
+#endif // !WORDS_BIGENDIAN
+}
+
 } // namespace aria2