Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from deathcap/headercheck
Browse files Browse the repository at this point in the history
Add gzip header check. Closes GH-6
  • Loading branch information
sjmulder committed Mar 31, 2014
2 parents a15c29c + de26af9 commit b215572
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions nbt.js
Expand Up @@ -40,6 +40,13 @@
}
})();

var hasGzipHeader = function(data){
var result=true;
if(data[0]!=0x1f) result=false;
if(data[1]!=0x8b) result=false;
return result;
}

nbt.Reader = function(buffer) {
var offset = 0;

Expand Down Expand Up @@ -141,15 +148,19 @@
result[name] = value;
return result;
}
};

nbt.parse = function(data, callback) {
zlib.unzip(data, function(err, uncompressed) {
if (err) {
callback(null, parseUncompressed(data));
} else {
callback(null, parseUncompressed(uncompressed));
}
});
}

this.parse = function(data, callback) {
if (hasGzipHeader(data)) {
zlib.gunzip(data, function(error, uncompressed) {
if (error) {
callback(error, data);
} else {
callback(null, parseUncompressed(uncompressed));
}
});
} else {
callback(null, parseUncompressed(data));
}
};
}).apply(exports || (nbt = {}));

0 comments on commit b215572

Please sign in to comment.