|
| 1 | +/*jslint eqeqeq: true, immed: true, newcap: true, nomen: true, onevar: true, plusplus: true, regexp: true, undef: true, white: true, indent: 2 */ |
| 2 | + |
| 3 | +// Converter between javascript values and "raw" |
| 4 | +// streams which are really arrays of 8 bit integers. |
| 5 | +// the add functions return this so they can be chained. |
| 6 | +// Note that this works directly on the array object. |
| 7 | + |
| 8 | +// Encode number as 32 bit 2s compliment |
| 9 | +Array.prototype.add_int32 = function (number) { |
| 10 | + var unsigned = (number < 0) ? (number + 0x100000000) : number; |
| 11 | + this.push(Math.floor(unsigned / 0xffffff)); |
| 12 | + unsigned &= 0xffffff; |
| 13 | + this.push(Math.floor(unsigned / 0xffff)); |
| 14 | + unsigned &= 0xffff; |
| 15 | + this.push(Math.floor(unsigned / 0xff)); |
| 16 | + unsigned &= 0xff; |
| 17 | + this.push(Math.floor(unsigned)); |
| 18 | + return this; |
| 19 | +}; |
| 20 | + |
| 21 | +// Encode number as 16 bit 2s compliment |
| 22 | +Array.prototype.add_int16 = function (number) { |
| 23 | + var unsigned = (number < 0) ? (number + 0x10000) : number; |
| 24 | + this.push(Math.floor(unsigned / 0xff)); |
| 25 | + unsigned &= 0xff; |
| 26 | + this.push(Math.floor(unsigned)); |
| 27 | + return this; |
| 28 | +}; |
| 29 | + |
| 30 | +// Encode string without null terminator |
| 31 | +Array.prototype.add_raw_string = function (text) { |
| 32 | + for (var i = 0, l = text.length; i < l; i += 1) { |
| 33 | + this.push(text.charCodeAt(i)); |
| 34 | + } |
| 35 | + return this; |
| 36 | +}; |
| 37 | + |
| 38 | +// Encode text as null terminated string |
| 39 | +Array.prototype.add_cstring = function (text) { |
| 40 | + this.add_raw_string(text).push(0); |
| 41 | + return this; |
| 42 | +}; |
| 43 | + |
| 44 | +// Encode as a null terminated array of cstrings |
| 45 | +Array.prototype.add_multi_cstring = function (fields) { |
| 46 | + for (var i = 0, l = fields.length; i < l; i += 1) { |
| 47 | + this.add_cstring(fields[i]); |
| 48 | + } |
| 49 | + this.push(0); |
| 50 | + return this; |
| 51 | +}; |
| 52 | + |
| 53 | +// Convert 4 bytes to signed 32 bit integer |
| 54 | +Array.prototype.parse_int32 = function () { |
| 55 | + var unsigned = this.shift() * 0x1000000 + this.shift() * 0x10000 + this.shift() * 0x100 + this.shift(); |
| 56 | + return (unsigned & 0x80000000) ? (unsigned - 0x100000000) : unsigned; |
| 57 | +}; |
| 58 | + |
| 59 | +// Convert 2 bytes to signed 16 bit integer |
| 60 | +Array.prototype.parse_int16 = function () { |
| 61 | + var unsigned = this.shift() * 0x100 + this.shift(); |
| 62 | + return (unsigned & 0x8000) ? (unsigned - 0x10000) : unsigned; |
| 63 | +}; |
| 64 | + |
| 65 | +// Grab number of bytes as a string |
| 66 | +Array.prototype.parse_raw_string = function (len) { |
| 67 | + var text = ""; |
| 68 | + while (len > 0) { |
| 69 | + len -= 1; |
| 70 | + text += String.fromCharCode(this.shift()); |
| 71 | + } |
| 72 | + return text; |
| 73 | +}; |
| 74 | + |
| 75 | +// Grab a null terminated string from the this |
| 76 | +Array.prototype.parse_cstring = function () { |
| 77 | + var text = ""; |
| 78 | + while (this.length > 0 && this[0] !== 0) { |
| 79 | + text += String.fromCharCode(this.shift()); |
| 80 | + } |
| 81 | + this.shift(); |
| 82 | + return text; |
| 83 | +}; |
| 84 | + |
| 85 | +// Grab a null terminated array of null terminated strings |
| 86 | +Array.prototype.parse_multi_cstring = function () { |
| 87 | + var fields = []; |
| 88 | + while (this[0] !== 0) { |
| 89 | + fields.push(this.parse_cstring()); |
| 90 | + } |
| 91 | + this.pop(); |
| 92 | + return fields; |
| 93 | +}; |
| 94 | + |
| 95 | + |
0 commit comments