Skip to content

Commit

Permalink
Implement crc32 with Buffer
Browse files Browse the repository at this point in the history
Probable fix for issue #13
  • Loading branch information
alexeyten committed Dec 15, 2014
1 parent a654565 commit 37eee26
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/crc32.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
"use strict";

// ARMv6 (Raspberry Pi) has bug in bitwise operations
// https://code.google.com/p/v8/issues/detail?id=3757
// https://github.com/alexeyten/qr-image/issues/13
if (process.arch === 'arm') {
module.exports = require('./crc32buffer');
return;
}

var crc_table = [];

(function() {
Expand Down
50 changes: 50 additions & 0 deletions lib/crc32buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

var crc_table = [];

for (var n = 0; n < 256; n++) {
var c = crc_table[n] = Buffer(4);
c.writeUInt32BE(n, 0);

for (var k = 0; k < 8; k++) {
var b0 = c[0] & 1;
var b1 = c[1] & 1;
var b2 = c[2] & 1;
var b3 = c[3] & 1;

c[0] = (c[0] >> 1) ^ (b3 ? 0xed : 0);
c[1] = (c[1] >> 1) ^ (b3 ? 0xb8 : 0) ^ (b0 ? 0x80 : 0);
c[2] = (c[2] >> 1) ^ (b3 ? 0x83 : 0) ^ (b1 ? 0x80 : 0);
c[3] = (c[3] >> 1) ^ (b3 ? 0x20 : 0) ^ (b2 ? 0x80 : 0);
}
}

function update(c, buf) {
var l = buf.length;
for (var n = 0; n < l; n++) {
var e = crc_table[c[3] ^ buf[n]];
c[3] = e[3] ^ c[2];
c[2] = e[2] ^ c[1];
c[1] = e[1] ^ c[0];
c[0] = e[0];
}
}

function crc32(/* arguments */) {
var l = arguments.length;
var c = Buffer(4);
c.fill(0xff);

for (var i = 0; i < l; i++) {
update(c, Buffer(arguments[i]));
}

c[0] = c[0] ^ 0xff;
c[1] = c[1] ^ 0xff;
c[2] = c[2] ^ 0xff;
c[3] = c[3] ^ 0xff;

return c.readUInt32BE(0);
}

module.exports = crc32;

0 comments on commit 37eee26

Please sign in to comment.