diff --git a/ChangeLog b/ChangeLog index f05b290c..1941c760 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2008-08-24 Tatsuhiro Tsujikawa + + Added ntoh64 and hton64 as inline functions. + * src/Util.cc + * test/UtilTest.cc + 2008-08-24 Tatsuhiro Tsujikawa Added sqlite3 depenency and Firefox3 cookie support. diff --git a/src/Util.h b/src/Util.h index 422e9911..7e63b060 100644 --- a/src/Util.h +++ b/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& hp, diff --git a/test/UtilTest.cc b/test/UtilTest.cc index b2f8d0cd..95f9d6d7 100644 --- a/test/UtilTest.cc +++ b/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