Skip to content

Instantly share code, notes, and snippets.

@mdellavo
Created August 5, 2010 00:36
Show Gist options
  • Save mdellavo/509035 to your computer and use it in GitHub Desktop.
Save mdellavo/509035 to your computer and use it in GitHub Desktop.
function DataBuffer(data) {
this.data = data;
this.index = 0;
}
DataBuffer.prototype.check = function(n) {
return this.data.length >= this.index + n;
}
DataBuffer.prototype.readByte = function() {
if(!this.check(1))
return null;
rv = this.data.charCodeAt(this.index) & 0xff;
this.index++;
return rv;
}
DataBuffer.prototype.readShort = function() {
if(!this.check(2))
return null;
hi = (this.data.charCodeAt(this.index) << 8) & 0xff00;
lo = this.data.charCodeAt(this.index+1) & 0xff;
this.index += 2;
return hi|lo;
}
DataBuffer.prototype.readWord = function() {
if(!this.check(4))
return null;
hihi = (this.data.charCodeAt(this.index) << 24) & 0xff000000;
hilo = (this.data.charCodeAt(this.index+1) << 16) & 0x00ff0000;
lohi = (this.data.charCodeAt(this.index+2) << 8) & 0x0000ff00;
lolo = this.data.charCodeAt(this.index+3) & 0x000000ff;
this.index += 4;
return hihi|hilo|lohi|lolo;
}
DataBuffer.prototype.dump = function() {
return dump(this.data.substr(this.index));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment