Hash.js, minor formatting changes

This commit is contained in:
rlx 2012-03-29 10:26:02 +00:00
parent 44ec0d1ecb
commit def7c88639

View file

@ -1,10 +1,11 @@
'use strict'; 'use strict';
/*@ /*@
Ox.oshash <f> compute oshahs for given File or Blob object (async) Ox.oshash <f> Calculates oshash for a given file or blob object. Async.
@*/ @*/
Ox.oshash = function(file, callback) { Ox.oshash = function(file, callback) {
//needs to go via string to work for files > 2GB
// Needs to go via string to work for files > 2GB
var hash = fromString(file.size.toString()); var hash = fromString(file.size.toString());
read(0); read(0);
@ -18,7 +19,7 @@ Ox.oshash = function(file, callback) {
c &= 0xffff; c &= 0xffff;
a = A[0] + B[0] + (b >> 16); a = A[0] + B[0] + (b >> 16);
b &= 0xffff; b &= 0xffff;
//cut of overflow // Cut off overflow
a &= 0xffff; a &= 0xffff;
return [a, b, c ,d]; return [a, b, c ,d];
} }
@ -26,10 +27,10 @@ Ox.oshash = function(file, callback) {
function fromData(s, offset) { function fromData(s, offset) {
offset = offset || 0; offset = offset || 0;
return [ return [
s.charCodeAt(offset+6) + (s.charCodeAt(offset+7) << 8), s.charCodeAt(offset + 6) + (s.charCodeAt(offset + 7) << 8),
s.charCodeAt(offset+4) + (s.charCodeAt(offset+5) << 8), s.charCodeAt(offset + 4) + (s.charCodeAt(offset + 5) << 8),
s.charCodeAt(offset+2) + (s.charCodeAt(offset+3) << 8), s.charCodeAt(offset + 2) + (s.charCodeAt(offset + 3) << 8),
s.charCodeAt(offset+0) + (s.charCodeAt(offset+1) << 8) s.charCodeAt(offset + 0) + (s.charCodeAt(offset + 1) << 8)
]; ];
} }
@ -40,13 +41,13 @@ Ox.oshash = function(file, callback) {
num, num,
pos, pos,
r = [0, 0, 0, 0]; r = [0, 0, 0, 0];
for(pos=0;pos<str.length;pos++) { for (pos = 0; pos < str.length; pos++) {
num = parseInt(str.charAt(pos), base); num = parseInt(str.charAt(pos), base);
i = 0; i = 0;
do { do {
while (i < blen) { while (i < blen) {
num += r[3-i] * base; num += r[3 - i] * base;
r[3-i++] = (num & 0xffff); r[3 - i++] = (num & 0xffff);
num >>>= 16; num >>>= 16;
} }
if (num) { if (num) {
@ -58,10 +59,12 @@ Ox.oshash = function(file, callback) {
} }
function hex(h) { function hex(h) {
return (Ox.pad(h[0].toString(16), 4) return (
Ox.pad(h[0].toString(16), 4)
+ Ox.pad(h[1].toString(16), 4) + Ox.pad(h[1].toString(16), 4)
+ Ox.pad(h[2].toString(16), 4) + Ox.pad(h[2].toString(16), 4)
+ Ox.pad(h[3].toString(16), 4)).toLowerCase(); + Ox.pad(h[3].toString(16), 4)
).toLowerCase();
} }
function read(offset, last) { function read(offset, last) {
@ -73,21 +76,21 @@ Ox.oshash = function(file, callback) {
var s = data.target.result, var s = data.target.result,
s_length = s.length - length, s_length = s.length - length,
i; i;
for(i=0;i<=s_length; i+=length) { for (i = 0; i <= s_length; i += length) {
hash = add(hash, fromData(s, i)); hash = add(hash, fromData(s, i));
} }
if(file.size < block || last) { if (file.size < block || last) {
callback(hex(hash)); callback(hex(hash));
} else { } else {
read(file.size - block, true); read(file.size - block, true);
} }
}; };
if(file.mozSlice) { if (file.mozSlice) {
blob = file.mozSlice(offset, offset+block); blob = file.mozSlice(offset, offset + block);
} else if(file.webkitSlice) { } else if (file.webkitSlice) {
blob = file.webkitSlice(offset, offset+block); blob = file.webkitSlice(offset, offset + block);
} else { } else {
blob = file.slice(offset, offset+block); blob = file.slice(offset, offset + block);
} }
reader.readAsBinaryString(blob); reader.readAsBinaryString(blob);
} }