Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc. CRC32 cleaning #189

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions ebml/EbmlCrc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ DECLARE_EBML_BINARY(EbmlCrc32)
void ResetCRC();
void UpdateByte(binary b);

static const std::array<std::uint32_t, 256> m_tab;
std::uint32_t m_crc;
std::uint32_t m_crc_final{0};

Expand All @@ -71,13 +70,7 @@ DECLARE_EBML_BINARY(EbmlCrc32)
template <class T>
inline unsigned int GetAlignment(T * /* dummy */=nullptr) // VC60 workaround
{
#if defined(_MSC_VER) && (_MSC_VER >= 1300)
return __alignof(T);
#elif defined(__GNUC__)
return __alignof__(T);
#else
return sizeof(T);
#endif
return alignof(T);
}

template <class T>
Expand Down
40 changes: 20 additions & 20 deletions src/EbmlCrc32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace libebml {

DEFINE_EBML_CLASS_GLOBAL(EbmlCrc32, 0xBF, 1, "EBMLCrc32\0ratamadabapa")

constexpr std::array<std::uint32_t, 256> EbmlCrc32::m_tab {
static constexpr std::array<std::uint32_t, 256> s_tab {
#ifdef WORDS_BIGENDIAN
0x00000000L, 0x96300777L, 0x2c610eeeL, 0xba510999L, 0x19c46d07L,
0x8ff46a70L, 0x35a563e9L, 0xa395649eL, 0x3288db0eL, 0xa4b8dc79L,
Expand Down Expand Up @@ -154,7 +154,7 @@ void EbmlCrc32::ResetCRC()

void EbmlCrc32::UpdateByte(binary b)
{
m_crc = m_tab.at(CRC32_INDEX(m_crc) ^ b) ^ CRC32_SHIFTED(m_crc);
m_crc = s_tab.at(CRC32_INDEX(m_crc) ^ b) ^ CRC32_SHIFTED(m_crc);
}

void EbmlCrc32::AddElementCRC32(EbmlElement &ElementToCRC)
Expand Down Expand Up @@ -228,20 +228,20 @@ bool EbmlCrc32::CheckCRC(std::uint32_t inputCRC, const binary *input, std::uint3
std::uint32_t crc = CRC32_NEGL;

for(; !IsAligned<std::uint32_t>(input) && length > 0; length--)
crc = m_tab.at(CRC32_INDEX(crc) ^ *input++) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc) ^ *input++) ^ CRC32_SHIFTED(crc);

while (length >= 4) {
crc ^= *reinterpret_cast<const std::uint32_t *>(input);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
length -= 4;
input += 4;
}

while (length--)
crc = m_tab.at(CRC32_INDEX(crc) ^ *input++) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc) ^ *input++) ^ CRC32_SHIFTED(crc);

//Now we finalize the CRC32
crc ^= CRC32_NEGL;
Expand All @@ -258,21 +258,21 @@ void EbmlCrc32::FillCRC32(const binary *input, std::uint32_t length)
/*std::uint32_t crc = CRC32_NEGL;

for(; !IsAligned<std::uint32_t>(s) && n > 0; n--)
crc = m_tab.at(CRC32_INDEX(crc) ^ *s++) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc) ^ *s++) ^ CRC32_SHIFTED(crc);

while (n >= 4)
{
crc ^= *(const std::uint32_t *)s;
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
n -= 4;
s += 4;
}

while (n--)
crc = m_tab.at(CRC32_INDEX(crc) ^ *s++) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc) ^ *s++) ^ CRC32_SHIFTED(crc);

m_crc = crc;

Expand All @@ -288,20 +288,20 @@ void EbmlCrc32::Update(const binary *input, std::uint32_t length)
std::uint32_t crc = m_crc;

for(; !IsAligned<std::uint32_t>(input) && length > 0; length--)
crc = m_tab.at(CRC32_INDEX(crc) ^ *input++) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc) ^ *input++) ^ CRC32_SHIFTED(crc);

while (length >= 4) {
crc ^= *reinterpret_cast<const std::uint32_t *>(input);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = m_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc)) ^ CRC32_SHIFTED(crc);
length -= 4;
input += 4;
}

while (length--)
crc = m_tab.at(CRC32_INDEX(crc) ^ *input++) ^ CRC32_SHIFTED(crc);
crc = s_tab.at(CRC32_INDEX(crc) ^ *input++) ^ CRC32_SHIFTED(crc);

m_crc = crc;
}
Expand Down