Question

I want to combine two precalculated crc32 into new one:

Example code in nodejs:

// i'm using crc32 lib: https://github.com/brianloveswords/buffer-crc32/blob/master/index.js
var crc32 = require('buffer-crc32');

var foo = new Buffer('foo');
var bar = new Buffer('bar');

var fooCrc32 = crc32(foo); // <Buffer 8c 73 65 21>
var barCrc32 = crc32(bar); // <Buffer 76 ff 8c aa>

// how to combine crc32 of foo and crc32 of bar to get crc32 of 'foobar'
var foobarCrc32 = some_function(fooCrc32, barCrc32);  // <Buffer 9e f6 1f 95> 

How to to this in nodejs? I know this is possible because of zlib crc32_combine function:

ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); Combine two CRC-32 check values into one. For two sequences of bytes, seq1 and seq2 with lengths len1 and len2, CRC-32 check values were calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and len2.

But I don't know how to implement it in nodejs. Is there any library in nodejs that do this or maybe some GIST? Can anyone provide that function in nodejs?

Was it helpful?

Solution 2

If someone else need that functionality I've made module for node:

https://github.com/tereska/node-crc-utils

OTHER TIPS

Since they're using zlib anyway, you could ask the nodejs authors to add an interface to zlib's crc32_combine().

Barring that, you could copy the source code from zlib and rewrite it in js.

Note that just the two crc's is insufficient. You need the two crc's and the length of the second piece.

It should be possible to implement the operation in Node, here's the code in zlib: http://www.raspberryginger.com/jbailey/minix/html/crc32_8c-source.html#l00370

That being said, your probably better off calling the zlib C from node, rather than reimplementing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top