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

Unsigned value handling in CRC calculator #2

Open
sparkpunkd opened this issue Nov 24, 2017 · 0 comments
Open

Unsigned value handling in CRC calculator #2

sparkpunkd opened this issue Nov 24, 2017 · 0 comments

Comments

@sparkpunkd
Copy link

sparkpunkd commented Nov 24, 2017

I am writing a 100% Javascript MPEG-TS library for Node-JS.

FYI, I was following the patterns in your CRC code and found an issues where in certain cases the CRC values could go negative. This is caused by the way Javascript treats unsigned integers when bit shifting. My altered version of the CRC calculator uses the >>> 0 operator to force treatment as unsigned and that seems to fix the issue. For example:

function crc (b) => {
  var crc = 0xffffffff;
  for (var i = 0; i < b.length; ++i) {
    var tableIndex = ((crc >>> 24) ^ b[i]) & 0xff;
    crc = ((crcTable[tableIndex] ^ (crc << 8)) & 0xffffffff) >>> 0;
  }
  return (crc & 0xffffffff) >>> 0;
};

Tested with values generated using http://www.sunshine2k.de/coding/javascript/crc/crc_js.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant