Comparing version 0.0.1 to 0.0.2
41
index.js
@@ -16,8 +16,11 @@ #!/usr/bin/env node | ||
*/ | ||
var NMAX = 5521; | ||
var NMAX = 5552; | ||
exports.sum = function(buf) | ||
exports.sum = function(buf, adler) | ||
{ | ||
var a = 1, | ||
b = 0, | ||
if (adler == null) | ||
adler = 1; | ||
var a = adler & 0xFFFF, | ||
b = (adler >>> 16) & 0xFFFF, | ||
i = 0, | ||
@@ -27,9 +30,9 @@ max = buf.length, | ||
for (; i < max; ++i) | ||
while (i < max) | ||
{ | ||
n = Math.min(NMAX, max - 1); | ||
n = Math.min(NMAX, max - i); | ||
do | ||
{ | ||
a += buf[i]; | ||
a += buf[i++]<<0; | ||
b += a; | ||
@@ -46,14 +49,2 @@ } | ||
exports.concat = function(sum0, sum1, length1) | ||
{ | ||
var a0 = sum0 & 0xFFFF, | ||
b0 = (sum0 >>> 16) & 0xFFFF, | ||
a1 = sum1 & 0xFFFF, | ||
b1 = (sum1 >>> 16) & 0xFFFF, | ||
a = (a0 + a1) % BASE, | ||
b = ((b0 * length1) + b1) % BASE; | ||
return ((b << 16) | a) >>> 0; | ||
}; | ||
exports.roll = function(sum, length, oldByte, newByte) | ||
@@ -64,6 +55,14 @@ { | ||
a = (a - oldByte + newByte) % BASE; | ||
b = (b - (length * oldByte) + a) % BASE; | ||
if (newByte != null) | ||
{ | ||
a = (a - oldByte + newByte + BASE) % BASE; | ||
b = (b - ((length * oldByte) % BASE) + a - 1 + BASE) % BASE; | ||
} | ||
else | ||
{ | ||
a = (a - oldByte + BASE) % BASE; | ||
b = (b - ((length * oldByte) % BASE) - 1 + BASE) % BASE; | ||
} | ||
return ((b << 16) | a) >>> 0; | ||
}; |
{ | ||
"name": "adler32", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Adler-32 hashing algorithm", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"mocha": ">=1.20.1", | ||
"mhash": ">=1.0.0" | ||
"mhash": ">=1.0.0", | ||
"should": ">=4.0.4" | ||
}, | ||
@@ -11,0 +12,0 @@ "scripts": { |
75
test.js
#!/usr/bin/env node | ||
"use strict"; | ||
var should = require('should'); | ||
var fs = require('fs'); | ||
var Adler32 = require('./'); | ||
var hash = require('mhash').hash; | ||
var buf = fs.readFileSync(__filename); | ||
describe('Adler32', function() { | ||
describe('.sum(buf, adler = 1)', function() { | ||
var sum = Adler32.sum(buf); | ||
it('should return a number above 0 and below 4293984241', function() { | ||
sum.should.be.above(0); | ||
sum.should.be.below(4293984241); | ||
}); | ||
it('should match the sum calculated by mhash', function() { | ||
sum.should.be.exactly(parseInt(hash('adler32', buf), 16)); | ||
}); | ||
it('should be the same if done as a running sum', function() { | ||
var half = (buf.length / 2)<<0; | ||
var part0 = buf.slice(0, half); | ||
var part1 = buf.slice(half); | ||
var partial = Adler32.sum(part0); | ||
var sum0 = Adler32.sum(buf); | ||
var sum1 = Adler32.sum(part1, partial); | ||
sum1.should.be.exactly(sum0); | ||
}); | ||
}); | ||
describe('.roll(sum, length, oldByte, newByte = null)', function() { | ||
it('should result in the same value as sum for each offset chunk of size 64', function() { | ||
rollTest(64); | ||
}); | ||
it('should result in the same value as sum for each offset chunk of size 128', function() { | ||
rollTest(128); | ||
}); | ||
it('should result in the same value as sum for each offset chunk of size 256', function() { | ||
rollTest(256); | ||
}); | ||
it('should result in the same value as sum for each offset chunk of size 512', function() { | ||
rollTest(512); | ||
}); | ||
it('should result in the same value as sum for each offset chunk of size 1024', function() { | ||
rollTest(1024); | ||
}); | ||
it('should result in the same value as sum for each offset chunk of size 2048', function() { | ||
rollTest(2048); | ||
}); | ||
it('should result in the same value as sum for each offset chunk of size 4096', function() { | ||
rollTest(4096); | ||
}); | ||
}); | ||
}); | ||
function rollTest(chunkSize) | ||
{ | ||
var sum = Adler32.sum(buf.slice(0, chunkSize)); | ||
var i = 0; | ||
for (; i < buf.length; ++i) | ||
{ | ||
sum = Adler32.roll(sum, Math.min(chunkSize, buf.length - i), buf[i], buf[i + chunkSize]); | ||
sum.should.be.exactly(Adler32.sum(buf.slice(i + 1, i + 1 + chunkSize))); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5705
113
3
1