Skip to content

Commit

Permalink
crc32 big endian fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Aug 4, 2020
1 parent 4669383 commit 7dcd091
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
16 changes: 16 additions & 0 deletions include/libtorrent/aux_/byteswap.hpp
Expand Up @@ -42,6 +42,8 @@ POSSIBILITY OF SUCH DAMAGE.

#include "libtorrent/aux_/disable_warnings_push.hpp"

#include <boost/predef/other/endian.h>

#ifdef TORRENT_WINDOWS
#include <winsock2.h>
#else
Expand All @@ -65,6 +67,20 @@ inline std::uint16_t host_to_network(std::uint16_t x)
inline std::uint16_t network_to_host(std::uint16_t x)
{ return ntohs(x); }

inline std::uint32_t little_endian_to_host(std::uint32_t x)
{
#if BOOST_ENDIAN_BIG_BYTE
return (x & 0xff000000) >> 24
| (x & 0x00ff0000) >> 8
| (x & 0x0000ff00) << 8
| (x & 0x000000ff) << 24;
#elif BOOST_ENDIAN_LITTLE_BYTE
return x;
#else
#error "unknown endian"
#endif
}

}
}

Expand Down
23 changes: 14 additions & 9 deletions src/crc32c.cpp
Expand Up @@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/crc32c.hpp"
#include "libtorrent/aux_/cpuid.hpp"
#include "libtorrent/aux_/byteswap.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp"

#include <boost/crc.hpp>
Expand All @@ -48,6 +49,10 @@ POSSIBILITY OF SUCH DAMAGE.

namespace libtorrent {

// The general CRC32 function doesn't output an integer, it outputs a
// sequence of 4 bytes. We interpret those 4 bytes as an integer, and as
// such we need to decide whether to interpret it as big endian or little
// endian. For historical reasons, it's interpreted as little endian.
std::uint32_t crc32c_32(std::uint32_t v)
{
#if TORRENT_HAS_SSE
Expand All @@ -57,13 +62,13 @@ namespace libtorrent {
#ifdef __GNUC__
// we can't use these because then we'd have to tell
// -msse4.2 to gcc on the command line
// return __builtin_ia32_crc32si(ret, v) ^ 0xffffffff;
// return aux::little_endian_to_host(__builtin_ia32_crc32si(ret, v) ^ 0xffffffff);
asm ("crc32l\t" "(%1), %0"
: "=r"(ret)
: "r"(&v), "0"(ret));
return ret ^ 0xffffffff;
return aux::little_endian_to_host(ret ^ 0xffffffff);
#else
return _mm_crc32_u32(ret, v) ^ 0xffffffff;
return aux::little_endian_to_host(_mm_crc32_u32(ret, v) ^ 0xffffffff);
#endif
}
#endif
Expand All @@ -72,13 +77,13 @@ namespace libtorrent {
if (aux::arm_crc32c_support)
{
std::uint32_t ret = 0xffffffff;
return __crc32cw(ret, v) ^ 0xffffffff;
return aux::little_endian_to_host(__crc32cw(ret, v) ^ 0xffffffff);
}
#endif

boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc;
crc.process_bytes(&v, 4);
return crc.checksum();
return aux::little_endian_to_host(crc.checksum());
}

std::uint32_t crc32c(std::uint64_t const* buf, int num_words)
Expand All @@ -102,7 +107,7 @@ namespace libtorrent {
ret = _mm_crc32_u64(ret, buf[i]);
#endif
}
return std::uint32_t(ret) ^ 0xffffffff;
return aux::little_endian_to_host(std::uint32_t(ret) ^ 0xffffffff);
#else
std::uint32_t ret = 0xffffffff;
std::uint32_t const* buf0 = reinterpret_cast<std::uint32_t const*>(buf);
Expand All @@ -124,7 +129,7 @@ namespace libtorrent {
ret = _mm_crc32_u32(ret, buf0[i*2+1]);
#endif
}
return ret ^ 0xffffffff;
return aux::little_endian_to_host(ret ^ 0xffffffff);
#endif // amd64 or x86
}
#endif // x86 or amd64 and gcc or msvc
Expand All @@ -137,12 +142,12 @@ namespace libtorrent {
{
ret = __crc32cd(ret, buf[i]);
}
return ret ^ 0xffffffff;
return aux::little_endian_to_host(ret ^ 0xffffffff);
}
#endif

boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc;
crc.process_bytes(buf, std::size_t(num_words) * 8);
return crc.checksum();
return aux::little_endian_to_host(crc.checksum());
}
}
14 changes: 7 additions & 7 deletions test/test_crc32.cpp
Expand Up @@ -32,8 +32,8 @@ POSSIBILITY OF SUCH DAMAGE.

#include "libtorrent/crc32c.hpp"
#include "libtorrent/aux_/cpuid.hpp"
#include "libtorrent/aux_/byteswap.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/aux_/byteswap.hpp"
#include "test.hpp"

TORRENT_TEST(crc32)
Expand All @@ -42,27 +42,27 @@ TORRENT_TEST(crc32)

std::uint32_t out;

std::uint32_t in1 = 0x5aa5feef;
std::uint32_t in1 = aux::host_to_network(0xeffea55a);
out = crc32c_32(in1);

TEST_EQUAL(out, aux::host_to_network(0xd5b9e35eU));
TEST_EQUAL(out, 0x5ee3b9d5);

std::uint64_t buf[4];
memcpy(buf, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 32);

// https://tools.ietf.org/html/rfc3720#appendix-B.4
out = crc32c(buf, 4);
TEST_EQUAL(out, aux::host_to_network(0xaa36918aU));
TEST_EQUAL(out, 0x8a9136aaU);

memcpy(buf, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", 32);
out = crc32c(buf, 4);
TEST_EQUAL(out, aux::host_to_network(0x43aba862U));
TEST_EQUAL(out, 0x62a8ab43U);

memcpy(buf, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 32);
out = crc32c(buf, 4);
TEST_EQUAL(out, aux::host_to_network(0x4e79dd46U));
TEST_EQUAL(out, 0x46dd794eU);

#if TORRENT_HAS_ARM
TORRENT_ASSERT(aux::arm_crc32c_support);
Expand Down

0 comments on commit 7dcd091

Please sign in to comment.