(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o> 3] >> (offset & 7) & 0x1; }; BitView.prototype._setBit = function (offset, on) { if (on) { this._view[offset >> 3] |= 1 << (offset & 7); } else { this._view[offset >> 3] &= ~(1 << (offset & 7)); } }; BitView.prototype.getBits = function (offset, bits, signed) { var available = (this._view.length * 8 - offset); if (bits > available) { throw new Error('Cannot get ' + bits + ' bit(s) from offset ' + offset + ', ' + available + ' available'); } var value = 0; for (var i = 0; i < bits;) { var read; // Read an entire byte if we can. if ((bits - i) >= 8 && ((offset & 7) === 0)) { value |= (this._view[offset >> 3] << i); read = 8; } else { value |= (this._getBit(offset) << i); read = 1; } offset += read; i += read; } if (signed) { // If we're not working with a full 32 bits, check the // imaginary MSB for this bit count and convert to a // valid 32-bit signed value if set. if (bits !== 32 && value & (1 << (bits - 1))) { value |= -1 ^ ((1 << bits) - 1); } return value; } return value >>> 0; }; BitView.prototype.setBits = function (offset, value, bits) { var available = (this._view.length * 8 - offset); if (bits > available) { throw new Error('Cannot set ' + bits + ' bit(s) from offset ' + offset + ', ' + available + ' available'); } for (var i = 0; i < bits;) { var wrote; // Write an entire byte if we can. if ((bits - i) >= 8 && ((offset & 7) === 0)) { this._view[offset >> 3] = value & 0xFF; wrote = 8; } else { this._setBit(offset, value & 0x1); wrote = 1; } value = (value >> wrote); offset += wrote; i += wrote; } }; BitView.prototype.getBoolean = function (offset) { return this.getBits(offset, 1, false) !== 0; }; BitView.prototype.getInt8 = function (offset) { return this.getBits(offset, 8, true); }; BitView.prototype.getUint8 = function (offset) { return this.getBits(offset, 8, false); }; BitView.prototype.getInt16 = function (offset) { return this.getBits(offset, 16, true); }; BitView.prototype.getUint16 = function (offset) { return this.getBits(offset, 16, false); }; BitView.prototype.getInt32 = function (offset) { return this.getBits(offset, 32, true); }; BitView.prototype.getUint32 = function (offset) { return this.getBits(offset, 32, false); }; BitView.prototype.getFloat32 = function (offset) { BitView._scratch.setUint32(0, this.getUint32(offset)); return BitView._scratch.getFloat32(0); }; BitView.prototype.getFloat64 = function (offset) { BitView._scratch.setUint32(0, this.getUint32(offset)); // DataView offset is in bytes. BitView._scratch.setUint32(4, this.getUint32(offset+32)); return BitView._scratch.getFloat64(0); }; BitView.prototype.setBoolean = function (offset, value) { this.setBits(offset, value ? 1 : 0, 1); }; BitView.prototype.setInt8 = BitView.prototype.setUint8 = function (offset, value) { this.setBits(offset, value, 8); }; BitView.prototype.setInt16 = BitView.prototype.setUint16 = function (offset, value) { this.setBits(offset, value, 16); }; BitView.prototype.setInt32 = BitView.prototype.setUint32 = function (offset, value) { this.setBits(offset, value, 32); }; BitView.prototype.setFloat32 = function (offset, value) { BitView._scratch.setFloat32(0, value); this.setBits(offset, BitView._scratch.getUint32(0), 32); }; BitView.prototype.setFloat64 = function (offset, value) { BitView._scratch.setFloat64(0, value); this.setBits(offset, BitView._scratch.getUint32(0), 32); this.setBits(offset+32, BitView._scratch.getUint32(4), 32); }; /********************************************************** * * BitStream * * Small wrapper for a BitView to maintain your position, * as well as to handle reading / writing of string data * to the underlying buffer. * **********************************************************/ var reader = function (name, size) { return function () { var val = this._view[name](this._index); this._index += size; return val; }; }; var writer = function (name, size) { return function (value) { this._view[name](this._index, value); this._index += size; }; }; function readASCIIString(stream, bytes) { return readString(stream, bytes, false); } function readUTF8String(stream, bytes) { return readString(stream, bytes, true); } function readString(stream, bytes, utf8) { var i = 0; var chars = []; var append = true; // Read while we still have space available, or until we've // hit the fixed byte length passed in. while (!bytes || (bytes && i < bytes)) { var c = stream.readUint8(); // Stop appending chars once we hit 0x00 if (c === 0x00) { append = false; // If we don't have a fixed length to read, break out now. if (!bytes) { break; } } if (append) { chars.push(c); } i++; } var string = String.fromCharCode.apply(null, chars); if (utf8) { try { return decodeURIComponent(escape(string)); // https://stackoverflow.com/a/17192845 } catch (e) { return string; } } else { return string; } } function writeASCIIString(stream, string, bytes) { var length = bytes || string.length + 1; // + 1 for NULL for (var i = 0; i < length; i++) { stream.writeUint8(i < string.length ? string.charCodeAt(i) : 0x00); } } function writeUTF8String(stream, string, bytes) { var byteArray = stringToByteArray(string); var length = bytes || byteArray.length + 1; // + 1 for NULL for (var i = 0; i < length; i++) { stream.writeUint8(i < byteArray.length ? byteArray[i] : 0x00); } } function stringToByteArray(str) { // https://gist.github.com/volodymyr-mykhailyk/2923227 var b = [], i, unicode; for (i = 0; i < str.length; i++) { unicode = str.charCodeAt(i); // 0x00000000 - 0x0000007f -> 0xxxxxxx if (unicode <= 0x7f) { b.push(unicode); // 0x00000080 - 0x000007ff -> 110xxxxx 10xxxxxx } else if (unicode <= 0x7ff) { b.push((unicode >> 6) | 0xc0); b.push((unicode & 0x3F) | 0x80); // 0x00000800 - 0x0000ffff -> 1110xxxx 10xxxxxx 10xxxxxx } else if (unicode <= 0xffff) { b.push((unicode >> 12) | 0xe0); b.push(((unicode >> 6) & 0x3f) | 0x80); b.push((unicode & 0x3f) | 0x80); // 0x00010000 - 0x001fffff -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else { b.push((unicode >> 18) | 0xf0); b.push(((unicode >> 12) & 0x3f) | 0x80); b.push(((unicode >> 6) & 0x3f) | 0x80); b.push((unicode & 0x3f) | 0x80); } } return b; } var BitStream = function (source, byteOffset, byteLength) { var isBuffer = source instanceof ArrayBuffer || (typeof Buffer !== 'undefined' && source instanceof Buffer); if (!(source instanceof BitView) && !isBuffer) { throw new Error('Must specify a valid BitView, ArrayBuffer or Buffer'); } if (isBuffer) { this._view = new BitView(source, byteOffset, byteLength); } else { this._view = source; } this._index = 0; }; Object.defineProperty(BitStream.prototype, 'byteIndex', { // Ceil the returned value, over compensating for the amount of // bits written to the stream. get: function () { return Math.ceil(this._index / 8); }, set: function (val) { this._index = val * 8; }, enumerable: true, configurable: true }); Object.defineProperty(BitStream.prototype, 'buffer', { get: function () { return this._view.buffer; }, enumerable: true, configurable: false }); Object.defineProperty(BitStream.prototype, 'view', { get: function () { return this._view; }, enumerable: true, configurable: false }); BitStream.prototype.readBits = function (bits, signed) { var val = this._view.getBits(this._index, bits, signed); this._index += bits; return val; }; BitStream.prototype.writeBits = function (value, bits) { this._view.setBits(this._index, value, bits); this._index += bits; }; BitStream.prototype.readBoolean = reader('getBoolean', 1); BitStream.prototype.readInt8 = reader('getInt8', 8); BitStream.prototype.readUint8 = reader('getUint8', 8); BitStream.prototype.readInt16 = reader('getInt16', 16); BitStream.prototype.readUint16 = reader('getUint16', 16); BitStream.prototype.readInt32 = reader('getInt32', 32); BitStream.prototype.readUint32 = reader('getUint32', 32); BitStream.prototype.readFloat32 = reader('getFloat32', 32); BitStream.prototype.readFloat64 = reader('getFloat64', 64); BitStream.prototype.writeBoolean = writer('setBoolean', 1); BitStream.prototype.writeInt8 = writer('setInt8', 8); BitStream.prototype.writeUint8 = writer('setUint8', 8); BitStream.prototype.writeInt16 = writer('setInt16', 16); BitStream.prototype.writeUint16 = writer('setUint16', 16); BitStream.prototype.writeInt32 = writer('setInt32', 32); BitStream.prototype.writeUint32 = writer('setUint32', 32); BitStream.prototype.writeFloat32 = writer('setFloat32', 32); BitStream.prototype.writeFloat64 = writer('setFloat64', 64); BitStream.prototype.readASCIIString = function (bytes) { return readASCIIString(this, bytes); }; BitStream.prototype.readUTF8String = function (bytes) { return readUTF8String(this, bytes); }; BitStream.prototype.writeASCIIString = function (string, bytes) { writeASCIIString(this, string, bytes); }; BitStream.prototype.writeUTF8String = function (string, bytes) { writeUTF8String(this, string, bytes); }; // AMD / RequireJS if (typeof define !== 'undefined' && define.amd) { define(function () { return { BitView: BitView, BitStream: BitStream }; }); } // Node.js else if (typeof module !== 'undefined' && module.exports) { module.exports = { BitView: BitView, BitStream: BitStream }; } }(this)); }).call(this,require("buffer").Buffer) },{"buffer":11}],6:[function(require,module,exports){ var ParserGenerator = require('./parsergenerator'); var StringTable = require('./stringtable'); var PacketStringTable = require('./packetstringtable'); // https://code.google.com/p/coldemoplayer/source/browse/branches/2.0/compLexity+Demo+Player/CDP.Source/Messages/?r=219 // https://github.com/TimePath/hl2-toolkit/tree/master/src/main/java/com/timepath/hl2/io/demo function logBase2(num) { var result = 0; while ((num >>= 1) != 0) { result++; } return result; } var Packet = function (type, tick, stream, length) { this.type = type; this.tick = tick; this.stream = stream; this.length = length;//length in bytes }; Packet.gameEventMap = {}; Object.defineProperty(Packet.prototype, 'bitsLeft', { get: function () { return (this.length * 8) - this.stream._index; } }); Packet.prototype.parse = function () { var packets = []; while (this.bitsLeft > 6) { // last 6 bits for NOOP var type = this.stream.readBits(6); if (Packet.parsers[type]) { var packet = Packet.parsers[type](this.stream); //console.log(packet); packets.push(packet); } else { throw 'Unknown packet type ' + type; } } return packets; }; Packet.parseGameEvent = function (eventId, stream) { if (!Packet.gameEventMap[eventId]) { return 'unknown'; } var eventDescription = this.gameEventMap[eventId]; var values = {}; for (var i = 0; i < eventDescription.entries.length; i++) { var entry = eventDescription.entries[i]; values[entry.name] = Packet.getGameEventValue(stream, entry); } return { name : eventDescription.name, type : eventDescription.type, values: values }; }; Packet.getGameEventValue = function (stream, entry) { switch (entry.type) { case 1: return stream.readUTF8String(); case 2: return stream.readFloat32(); case 3: return stream.readInt32(); case 4: return stream.readBits(16); case 5: return stream.readBits(8); case 6: return !!stream.readBits(1); case 7: return 'local value'; default: throw 'invalid game event type'; } }; Packet.parsers = { 0 : function () { }, 2 : ParserGenerator.make('file', 'transferId{32}fileName{s}requested{b}'), 3 : ParserGenerator.make('netTick', 'tick{32}frameTime{16}stdDev{16}'), 4 : ParserGenerator.make('stringCmd', 'command{s}'), 5 : function (stream) { var count = stream.readBits(8); var vars = {}; for (var i = 0; i < count; i++) { vars[stream.readUTF8String()] = stream.readUTF8String(); } return { packetType: 'setConVar', vars : vars } }, 6 : ParserGenerator.make('sigOnState', 'state{8}count{32}'), 7 : ParserGenerator.make('print', 'value{s}'), 8 : ParserGenerator.make('serverInfo', 'version{16}serverCount{32}stv{b}dedicated{b}maxCrc{32}maxClasses{16}' + 'mapHash{128}playerCount{8}maxPlayerCount{8}intervalPerTick{f32}platform{s1}' + 'game{s}map{s}skybox{s}serverName{s}replay{b}'), 10: function (stream) { var number = stream.readBits(16); var create = !!stream.readBits(1); var entries = []; if (!create) { var bits = logBase2(number) + 1; for (var i = 0; i < number; i++) { var entry = { 'classId' : stream.readBits(bits), 'className' : stream.readASCIIString(), 'dataTableName': stream.readASCIIString() }; entries.push(entry); } } return { 'packetType': 'classInfo', number : number, create : create, entries : entries } }, 11: ParserGenerator.make('setPause', 'paused{b}'), 12: function (stream) { //todo // https://coldemoplayer.googlecode.com/svn/branches/2.0/code/plugins/CDP.Source/Messages/SvcCreateStringTable.cs var name = stream.readASCIIString(); var maxEntries = stream.readBits(16); var bits = logBase2(maxEntries); var numEntries = stream.readBits(bits + 1); var length = stream.readBits(20); var userDataFixedSize = !!stream.readBits(1); if (userDataFixedSize) { var userSize = stream.readBits(12); var userDataBits = stream.readBits(4); } var end = stream._index + length; var stringTable = new PacketStringTable(name, maxEntries, bits, userDataFixedSize, userSize || -1, userDataBits || -1, numEntries); stringTable.parse(stream); //console.log(stringTable); //console.log(stream.readBits(6)); //console.log(stream.readASCIIString()); //maxEntries = stream.readBits(16); //console.log(maxEntries); //bits = logBase2(maxEntries); //numEntries = stream.readBits(bits + 1); //console.log('entries: ' + numEntries); //console.log(stream.readBits(175)); //for (var i = 0; i < numEntries; i++) { // console.log(stream.readBits(6)); // console.log(stream.readASCIIString()); //} //console.log(stream.readASCIIString()); //console.log(stream.readASCIIString()); //console.log(); //console.log(length); //console.log(end - stream._index); //console.log(); //throw false; //if((end/8)> 50515){ // console.log(stringTable); // throw 'found'; //} //throw false; stream._index = end; return { packetType: 'createStringTable', table : stringTable }; }, 13: function (stream) { var tableId = stream.readBits(5); var changeEntries = stream.readBits(1) ? stream.readBits(16) : 1; var length = stream.readBits(20); var end = stream._index + length; stream.readBits(7); var strings = {}; //var table = StringTable.tables[tableId]; // no idea why but it mostly works var a = stream.readBits(1); var b = stream.readBits(1); if (a && !b) { stream.readBits(12); } else if (!b) { stream.readBits(16); } else { stream.readBits(6); } //console.log(a ? 'a' : '!a') //console.log('table: ' + table.name); //console.log(' ' + table.entries.length + ' entries'); for (var i = 0; i < changeEntries; i++) { //console.log(stream.readBits(2)); var string = stream.readASCIIString(); stream.readBits(16); //todo last entry overflows by 13 (3 bits at the end 13 before next entry?) strings[i] = string; } //throw false; //console.log(changeEntries); //console.log(strings); //console.log(end - stream._index); stream._index = end; //throw false; return { packetType : 'updateStringTables', tableId : tableId, changedEntries: changeEntries, length : length, strings : strings } }, 14: ParserGenerator.make('voiceInit', 'codec{s}quality{8}'), 15: ParserGenerator.make('voiceData', 'client{8}proximity{8}length{16}_{$length}'), 17: function (stream) { var reliable = !!stream.readBits(1); var num = (reliable) ? 1 : stream.readBits(8); var length = (reliable) ? stream.readBits(8) : stream.readBits(16); stream._index += length; return { packetType: 'parseSounds', reliable : reliable, num : num, length : length } }, 18: ParserGenerator.make('setView', 'index{11}'), 19: ParserGenerator.make('fixAngle', 'relative{b}x{16}y{16}z{16}'), 21: function (stream) { var getCoord = function (stream) { var hasInt = !!stream.readBits(1); var hasFract = !!stream.readBits(1); var value = 0; if (hasInt || hasFract) { var sign = !!stream.readBits(1); if (hasInt) { value += stream.readBits(14) + 1; } if (hasFract) { value += stream.readBits(5) * (1 / 32); } if (sign) { value = -value; } } return value; }; var getVecCoord = function (stream) { var hasX = !!stream.readBits(1); var hasY = !!stream.readBits(1); var hasZ = !!stream.readBits(1); return { x: hasX ? getCoord(stream) : 0, y: hasY ? getCoord(stream) : 0, z: hasZ ? getCoord(stream) : 0 } }; var position = getVecCoord(stream); var textureIndex = stream.readBits(9); if (stream.readBits(1)) { var entIndex = stream.readBits(11); var modelIndex = stream.readBits(12); } var lowPriority = !!stream.readBits(1); return { packetType : 'BSPDecal', position : position, textureIndex: textureIndex, entIndex : entIndex, modelIndex : modelIndex, lowPriority : lowPriority } }, 23: function (stream) { // user message var type = stream.readBits(8); var length = stream.readBits(11); var pos = stream._index; if (Packet.userMessageParsers[type]) { var result = Packet.userMessageParsers[type](stream); } else { result = { packetType: 'unknownUserMessage', type : type } } //console.log(result); //console.log(((pos + length) - stream._index) + ' bits left'); stream._index = pos + length; return result; }, 24: ParserGenerator.make('entityMessage', 'index{11}id{9}length{11}data{$length}'), 25: function (stream) { var length = stream.readBits(11); var end = stream._index + length; var eventId = stream.readBits(9); var event = Packet.parseGameEvent(eventId, stream); stream._index = end; return { packetType: 'gameEvent', event : event } }, 26: function (stream) { // todo var maxEntries = stream.readBits(11); var isDelta = !!stream.readBits(1); if (isDelta) { var delta = stream.readInt32(); } else { delta = null; } var baseLink = !!stream.readBits(1); var updatedEntries = stream.readBits(11); var length = stream.readBits(20); var updatedBaseLink = !!stream.readBits(1); stream._index += length; return { packetType : 'packetEntities', maxEntries : maxEntries, isDelta : isDelta, delta : delta, baseLink : baseLink, updatedEntries : updatedEntries, length : length, updatedBaseLink: updatedBaseLink } }, 27: ParserGenerator.make('tempEntities', 'count{8}length{17}_{$length}'), 28: ParserGenerator.make('preFetch', 'index{14}'), 29: ParserGenerator.make('menu', 'type{16}length{16}_{$length}_{$length}_{$length}_{$length}_{$length}_{$length}_{$length}'),//length*8 30: function (stream) { // list of game events and parameters var numEvents = stream.readBits(9); var length = stream.readBits(20); var events = {}; for (var i = 0; i < numEvents; i++) { var id = stream.readBits(9); var name = stream.readASCIIString(); var type = stream.readBits(3); var entries = []; while (type !== 0) { var entryName = stream.readASCIIString(); entries.push({ type: type, name: entryName }); type = stream.readBits(3); } events[id] = { id : id, name : name, type : type, entries: entries }; } Packet.gameEventMap = events; return { packetType: 'gameEventList', events : events } }, 31: ParserGenerator.make('getCvarValue', 'cookie{32}value{s}'), 32: ParserGenerator.make('cmdKeyValues', 'length{32}data{$length}') }; Packet.userMessageParsers = { 4: function (stream) { var client = stream.readBits(8); var raw = stream.readBits(8); var pos = stream._index; var from, text, kind, arg1, arg2; if (stream.readBits(8) === 1) { var first = stream.readBits(8); if (first === 7) { var color = stream.readUTF8String(6); } else { stream._index = pos + 8; } text = stream.readUTF8String(); if (text.substr(0, 6) === '*DEAD*') { // grave talk is in the format '*DEAD* \u0003$from\u0001: $text' var start = text.indexOf('\u0003'); var end = text.indexOf('\u0001'); from = text.substr(start + 1, end - start - 1); text = text.substr(end + 5); kind = 'TF_Chat_AllDead'; } } else { stream._index = pos; kind = stream.readUTF8String(); from = stream.readUTF8String(); text = stream.readUTF8String(); stream.readASCIIString(); stream.readASCIIString(); } // cleanup color codes text = text.replace(/\u0001/g, ''); text = text.replace(/\u0003/g, ''); while ((pos = text.indexOf('\u0007')) !== -1) { text = text.slice(0, pos) + text.slice(pos + 7); } return { packetType: 'sayText2', client : client, raw : raw, kind : kind, from : from, text : text } }, //4: ParserGenerator.make('sayText2', 'client{8}raw{8}kind{s}from{s}text{s}arg1{s}arg2{s}'), 5: ParserGenerator.make('textMsg', 'destType{8}text{s}') }; var UserMessageType = { Geiger : 0, Train : 1, HudText : 2, SayText : 3, SayText2 : 4, TextMsg : 5, ResetHUD : 6, GameTitle : 7, ItemPickup : 8, ShowMenu : 9, Shake : 10, Fade : 11, VGUIMenu : 12, Rumble : 13, CloseCaption : 14, SendAudio : 15, VoiceMask : 16, RequestState : 17, Damage : 18, HintText : 19, KeyHintText : 20, HudMsg : 21, AmmoDenied : 22, AchievementEvent : 23, UpdateRadar : 24, VoiceSubtitle : 25, HudNotify : 26, HudNotifyCustom : 27, PlayerStatsUpdate : 28, PlayerIgnited : 29, PlayerIgnitedInv : 30, HudArenaNotify : 31, UpdateAchievement : 32, TrainingMsg : 33, TrainingObjective : 34, DamageDodged : 35, PlayerJarated : 36, PlayerExtinguished : 37, PlayerJaratedFade : 38, PlayerShieldBlocked: 39, BreakModel : 40, CheapBreakModel : 41, BreakModel_Pumpkin : 42, BreakModelRocketDud: 43, CallVoteFailed : 44, VoteStart : 45, VotePass : 46, VoteFailed : 47, VoteSetup : 48, PlayerBonusPoints : 49, SpawnFlyingBird : 50, PlayerGodRayEffect : 51, SPHapWeapEvent : 52, HapDmg : 53, HapPunch : 54, HapSetDrag : 55, HapSet : 56, HapMeleeContact : 57 }; module.exports = Packet; },{"./packetstringtable":7,"./parsergenerator":9,"./stringtable":10}],7:[function(require,module,exports){ var PacketStringTable = function (name, maxEntries, entryBits, userDataFixedSize, userDataSize, userDataSizeBits, numEntries) { this.name = name; this.maxEntries = maxEntries; this.entryBits = entryBits; this.userDataFixedSize = userDataFixedSize; this.userDataSize = userDataSize; this.userDataSizeBits = userDataSizeBits; this.numEntries = numEntries; this.id = PacketStringTable.tables.length; this.strings = []; PacketStringTable.tables.push(this); }; PacketStringTable.prototype.parse = function (stream) { var entryIndex, lastEntry = -1; for (var i = 0; i < this.numEntries; i++) { entryIndex = lastEntry + 1; this.strings.push(stream.readASCIIString()); //if (!stream.readBits(1)) { // entryIndex = stream.readBits(this.entryBits); //} //lastEntry = entryIndex; //if (entryIndex < 0 || entryIndex >= this.maxEntries) { // throw 'invalid index'; //} //var string = ''; //if (stream.readBits(1)) { // if (stream.readBits(1)) { // throw 'substr not implented'; // } else { // string = stream.readASCIIString(); // } //} if (stream.readBits(1)) { //user data if (this.userDataFixedSize) { var userData = stream.readBits(this.userDataSizeBits) } else { var bits = stream.readBits(14); userData = stream.readBits(bits); } console.log('userdata: ' + userData); } //this.strings.push(string); } }; PacketStringTable.tables = []; module.exports = PacketStringTable; },{}],8:[function(require,module,exports){ var Packet = require('./packet'); var ConsoleCmd = require('./consolecmd'); var StringTable = require('./stringtable'); var DataTable = require('./datatable'); var BitStream = require('bit-buffer').BitStream; var Parser = function (steam) { this.stream = steam; this.state = { chat : [], users : {}, deaths: [], rounds: [] }; }; Parser.MessageType = { Sigon : 1, Packet : 2, SyncTick : 3, ConsoleCmd : 4, UserCmd : 5, DataTables : 6, Stop : 7, StringTables: 8 }; Parser.prototype.readHeader = function () { return { 'type' : this.stream.readASCIIString(8), 'version' : this.stream.readInt32(), 'protocol': this.stream.readInt32(), 'server' : this.stream.readASCIIString(260), 'nick' : this.stream.readASCIIString(260), 'map' : this.stream.readASCIIString(260), 'game' : this.stream.readASCIIString(260), 'duration': this.stream.readFloat32(), 'ticks' : this.stream.readInt32(), 'frames' : this.stream.readInt32(), 'sigon' : this.stream.readInt32() } }; Parser.prototype.parseBody = function () { var message, i, tick = 0; while (message = this.readMessage()) { if (message.parse) { var packets = message.parse(); for (i = 0; i < packets.length; i++) { var packet = packets[i]; if (!packet) { continue; } switch (packet.packetType) { case 'netTick': tick = packet.tick; break; case 'sayText2': this.state.chat.push({ kind: packet.kind, from: packet.from, text: packet.text, tick: tick }); break; case 'stringTable': if (packet.tables.userinfo) { for (var j = 0; j < packet.tables.userinfo.length; j++) { if (packet.tables.userinfo[j].extraData) { this.state.users[packet.tables.userinfo[j].text] = packet.tables.userinfo[j].extraData } } } break; case 'gameEvent': switch (packet.event.name) { case 'player_death': // todo get player names, not same id as the name string table var assister = packet.event.values.assister < 32 ? packet.event.values.assister : null; this.state.deaths.push({ killer : packet.event.values.attacker, assister: assister, victim : packet.event.values.userid, weapon : packet.event.values.weapon, tick : tick }); break; case 'teamplay_round_win': if (packet.event.values.winreason !== 6) {// 6 = timelimit this.state.rounds.push({ winner : packet.event.values.team === 2 ? 'red' : 'blue', length : packet.event.values.round_time, end_tick: tick }); } } break; } } } } return this.state; }; Parser.prototype.readMessage = function () { //console.log(); //console.log('start message'); //console.log(this.stream.byteIndex); var type = this.stream.readBits(8); //console.log(type); if (type === Parser.MessageType.Stop) { return null; } var tick = this.stream.readInt32(); var data, start, length, buffer; switch (type) { case Parser.MessageType.Sigon: case Parser.MessageType.Packet: this.stream.byteIndex += 0x54; // command/sequence info break; case Parser.MessageType.UserCmd: this.stream.byteIndex += 0x04; // unknown / outgoing sequence break; case Parser.MessageType.Stop: return false; case Parser.MessageType.SyncTick: return true; } length = this.stream.readInt32(); //console.log('message length: ' + length + ' byte'); start = this.stream.byteIndex; buffer = this.stream.buffer.slice(start, start + length); this.stream.byteIndex += length; data = new BitStream(buffer); //console.log(this.stream.buffer); switch (type) { case Parser.MessageType.Sigon: case Parser.MessageType.Packet: return new Packet(type, tick, data, length); case Parser.MessageType.ConsoleCmd: return new ConsoleCmd(type, tick, data, length); case Parser.MessageType.UserCmd: console.log('usercmd'); return true; case Parser.MessageType.DataTables: return new DataTable(type, tick, data, length); case Parser.MessageType.StringTables: return new StringTable(type, tick, data, length); default: return true; //throw 'Unknown message type: ' + type; } }; module.exports = Parser; },{"./consolecmd":2,"./datatable":3,"./packet":6,"./stringtable":10,"bit-buffer":5}],9:[function(require,module,exports){ var Generator = {}; Generator.make = function (name, string) { var parts = string.substr(0, string.length - 1).split('}');//remove leading } to prevent empty part var items = parts.map(function (part) { return part.split('{'); }); return function (stream) { var result = { 'packetType': name }; try { for (var i = 0; i < items.length; i++) { var value = Generator.readItem(stream, items[i][1], result); if(items[i][0] !== '_'){ result[items[i][0]] = value; } } } catch (e) { throw 'Failed reading pattern ' + string; } return result; } }; Generator.readItem = function (stream, description, data) { var length; if (description[0] === 'b') { return !!stream.readBits(1); } else if (description[0] === 's') { if (description.length === 1) { return stream.readUTF8String(); } else { length = parseInt(description.substr(1), 10); return stream.readASCIIString(length); } } else if (description === 'f32') { return stream.readFloat32(); } else if (description[0] === 'u') { length = parseInt(description.substr(1), 10); return stream.readBits(length); } else if (description[0] === '$') { var variable = description.substr(1); return stream.readBits(variable); } else { return stream.readBits(parseInt(description, 10), true); } }; module.exports = Generator; },{}],10:[function(require,module,exports){ var StringTable = function (type, tick, stream, length) { this.type = type; this.tick = tick; this.stream = stream; this.length = length;//length in bytes }; StringTable.tables = []; StringTable.prototype.parse = function () { var tableCount = this.stream.readBits(8); var tables = {}; for (var i = 0; i < tableCount; i++) { var entries = []; var tableName = this.stream.readASCIIString(); var entryCount = this.stream.readBits(16); for (var j = 0; j < entryCount; j++) { var entry = { text: this.stream.readUTF8String() }; if (this.stream.readBits(1)) { var extraDataLength = this.stream.readBits(16); entry.extraData = this.stream.readUTF8String(extraDataLength); //console.log(entry.extraData.length-extraDataLength); } entries.push(entry); } tables[tableName] = entries; StringTable.tables.push({ name: tableName, entries: entries }); if (this.stream.readBits(1)) { this.stream.readASCIIString(); if (this.stream.readBits(1)) { //throw 'more extra data not implemented'; var extraDataLength = this.stream.readBits(16); this.stream.readBits(extraDataLength); } } } //console.log(tables); return [{ packetType: 'stringTable', tables: tables }]; }; module.exports = StringTable; },{}],11:[function(require,module,exports){ /*! * The buffer module from node.js, for the browser. * * @author Feross Aboukhadijeh * @license MIT */ var base64 = require('base64-js') var ieee754 = require('ieee754') var isArray = require('is-array') exports.Buffer = Buffer exports.SlowBuffer = SlowBuffer exports.INSPECT_MAX_BYTES = 50 Buffer.poolSize = 8192 // not used by this implementation var kMaxLength = 0x3fffffff var rootParent = {} /** * If `Buffer.TYPED_ARRAY_SUPPORT`: * === true Use Uint8Array implementation (fastest) * === false Use Object implementation (most compatible, even IE6) * * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, * Opera 11.6+, iOS 4.2+. * * Note: * * - Implementation must support adding new properties to `Uint8Array` instances. * Firefox 4-29 lacked support, fixed in Firefox 30+. * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438. * * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function. * * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of * incorrect length in some situations. * * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they will * get the Object implementation, which is slower but will work correctly. */ Buffer.TYPED_ARRAY_SUPPORT = (function () { try { var buf = new ArrayBuffer(0) var arr = new Uint8Array(buf) arr.foo = function () { return 42 } return 42 === arr.foo() && // typed array instances can be augmented typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray` new Uint8Array(1).subarray(1, 1).byteLength === 0 // ie10 has broken `subarray` } catch (e) { return false } })() /** * Class: Buffer * ============= * * The Buffer constructor returns instances of `Uint8Array` that are augmented * with function properties for all the node `Buffer` API functions. We use * `Uint8Array` so that square bracket notation works as expected -- it returns * a single octet. * * By augmenting the instances, we can avoid modifying the `Uint8Array` * prototype. */ function Buffer (subject, encoding, noZero) { if (!(this instanceof Buffer)) return new Buffer(subject, encoding, noZero) var type = typeof subject // Find the length var length if (type === 'number') length = subject > 0 ? subject >>> 0 : 0 else if (type === 'string') { length = Buffer.byteLength(subject, encoding) } else if (type === 'object' && subject !== null) { // assume object is array-like if (subject.type === 'Buffer' && isArray(subject.data)) subject = subject.data length = +subject.length > 0 ? Math.floor(+subject.length) : 0 } else throw new TypeError('must start with number, buffer, array or string') if (length > kMaxLength) throw new RangeError('Attempt to allocate Buffer larger than maximum ' + 'size: 0x' + kMaxLength.toString(16) + ' bytes') var buf if (Buffer.TYPED_ARRAY_SUPPORT) { // Preferred: Return an augmented `Uint8Array` instance for best performance buf = Buffer._augment(new Uint8Array(length)) } else { // Fallback: Return THIS instance of Buffer (created by `new`) buf = this buf.length = length buf._isBuffer = true } var i if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') { // Speed optimization -- use set if we're copying from a typed array buf._set(subject) } else if (isArrayish(subject)) { // Treat array-ish objects as a byte array if (Buffer.isBuffer(subject)) { for (i = 0; i < length; i++) buf[i] = subject.readUInt8(i) } else { for (i = 0; i < length; i++) buf[i] = ((subject[i] % 256) + 256) % 256 } } else if (type === 'string') { buf.write(subject, 0, encoding) } else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT && !noZero) { for (i = 0; i < length; i++) { buf[i] = 0 } } if (length > 0 && length <= Buffer.poolSize) buf.parent = rootParent return buf } function SlowBuffer(subject, encoding, noZero) { if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding, noZero) var buf = new Buffer(subject, encoding, noZero) delete buf.parent return buf } Buffer.isBuffer = function (b) { return !!(b != null && b._isBuffer) } Buffer.compare = function (a, b) { if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) throw new TypeError('Arguments must be Buffers') var x = a.length var y = b.length for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {} if (i !== len) { x = a[i] y = b[i] } if (x < y) return -1 if (y < x) return 1 return 0 } Buffer.isEncoding = function (encoding) { switch (String(encoding).toLowerCase()) { case 'hex': case 'utf8': case 'utf-8': case 'ascii': case 'binary': case 'base64': case 'raw': case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return true default: return false } } Buffer.concat = function (list, totalLength) { if (!isArray(list)) throw new TypeError('Usage: Buffer.concat(list[, length])') if (list.length === 0) { return new Buffer(0) } else if (list.length === 1) { return list[0] } var i if (totalLength === undefined) { totalLength = 0 for (i = 0; i < list.length; i++) { totalLength += list[i].length } } var buf = new Buffer(totalLength) var pos = 0 for (i = 0; i < list.length; i++) { var item = list[i] item.copy(buf, pos) pos += item.length } return buf } Buffer.byteLength = function (str, encoding) { var ret str = str + '' switch (encoding || 'utf8') { case 'ascii': case 'binary': case 'raw': ret = str.length break case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': ret = str.length * 2 break case 'hex': ret = str.length >>> 1 break case 'utf8': case 'utf-8': ret = utf8ToBytes(str).length break case 'base64': ret = base64ToBytes(str).length break default: ret = str.length } return ret } // pre-set for values that may exist in the future Buffer.prototype.length = undefined Buffer.prototype.parent = undefined // toString(encoding, start=0, end=buffer.length) Buffer.prototype.toString = function (encoding, start, end) { var loweredCase = false start = start >>> 0 end = end === undefined || end === Infinity ? this.length : end >>> 0 if (!encoding) encoding = 'utf8' if (start < 0) start = 0 if (end > this.length) end = this.length if (end <= start) return '' while (true) { switch (encoding) { case 'hex': return hexSlice(this, start, end) case 'utf8': case 'utf-8': return utf8Slice(this, start, end) case 'ascii': return asciiSlice(this, start, end) case 'binary': return binarySlice(this, start, end) case 'base64': return base64Slice(this, start, end) case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return utf16leSlice(this, start, end) default: if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) encoding = (encoding + '').toLowerCase() loweredCase = true } } } Buffer.prototype.equals = function (b) { if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') return Buffer.compare(this, b) === 0 } Buffer.prototype.inspect = function () { var str = '' var max = exports.INSPECT_MAX_BYTES if (this.length > 0) { str = this.toString('hex', 0, max).match(/.{2}/g).join(' ') if (this.length > max) str += ' ... ' } return '' } Buffer.prototype.compare = function (b) { if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') return Buffer.compare(this, b) } // `get` will be removed in Node 0.13+ Buffer.prototype.get = function (offset) { console.log('.get() is deprecated. Access using array indexes instead.') return this.readUInt8(offset) } // `set` will be removed in Node 0.13+ Buffer.prototype.set = function (v, offset) { console.log('.set() is deprecated. Access using array indexes instead.') return this.writeUInt8(v, offset) } function hexWrite (buf, string, offset, length) { offset = Number(offset) || 0 var remaining = buf.length - offset if (!length) { length = remaining } else { length = Number(length) if (length > remaining) { length = remaining } } // must be an even number of digits var strLen = string.length if (strLen % 2 !== 0) throw new Error('Invalid hex string') if (length > strLen / 2) { length = strLen / 2 } for (var i = 0; i < length; i++) { var byte = parseInt(string.substr(i * 2, 2), 16) if (isNaN(byte)) throw new Error('Invalid hex string') buf[offset + i] = byte } return i } function utf8Write (buf, string, offset, length) { var charsWritten = blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) return charsWritten } function asciiWrite (buf, string, offset, length) { var charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length) return charsWritten } function binaryWrite (buf, string, offset, length) { return asciiWrite(buf, string, offset, length) } function base64Write (buf, string, offset, length) { var charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length) return charsWritten } function utf16leWrite (buf, string, offset, length) { var charsWritten = blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length, 2) return charsWritten } Buffer.prototype.write = function (string, offset, length, encoding) { // Support both (string, offset, length, encoding) // and the legacy (string, encoding, offset, length) if (isFinite(offset)) { if (!isFinite(length)) { encoding = length length = undefined } } else { // legacy var swap = encoding encoding = offset offset = length length = swap } offset = Number(offset) || 0 if (length < 0 || offset < 0 || offset > this.length) throw new RangeError('attempt to write outside buffer bounds'); var remaining = this.length - offset if (!length) { length = remaining } else { length = Number(length) if (length > remaining) { length = remaining } } encoding = String(encoding || 'utf8').toLowerCase() var ret switch (encoding) { case 'hex': ret = hexWrite(this, string, offset, length) break case 'utf8': case 'utf-8': ret = utf8Write(this, string, offset, length) break case 'ascii': ret = asciiWrite(this, string, offset, length) break case 'binary': ret = binaryWrite(this, string, offset, length) break case 'base64': ret = base64Write(this, string, offset, length) break case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': ret = utf16leWrite(this, string, offset, length) break default: throw new TypeError('Unknown encoding: ' + encoding) } return ret } Buffer.prototype.toJSON = function () { return { type: 'Buffer', data: Array.prototype.slice.call(this._arr || this, 0) } } function base64Slice (buf, start, end) { if (start === 0 && end === buf.length) { return base64.fromByteArray(buf) } else { return base64.fromByteArray(buf.slice(start, end)) } } function utf8Slice (buf, start, end) { var res = '' var tmp = '' end = Math.min(buf.length, end) for (var i = start; i < end; i++) { if (buf[i] <= 0x7F) { res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i]) tmp = '' } else { tmp += '%' + buf[i].toString(16) } } return res + decodeUtf8Char(tmp) } function asciiSlice (buf, start, end) { var ret = '' end = Math.min(buf.length, end) for (var i = start; i < end; i++) { ret += String.fromCharCode(buf[i] & 0x7F) } return ret } function binarySlice (buf, start, end) { var ret = '' end = Math.min(buf.length, end) for (var i = start; i < end; i++) { ret += String.fromCharCode(buf[i]) } return ret } function hexSlice (buf, start, end) { var len = buf.length if (!start || start < 0) start = 0 if (!end || end < 0 || end > len) end = len var out = '' for (var i = start; i < end; i++) { out += toHex(buf[i]) } return out } function utf16leSlice (buf, start, end) { var bytes = buf.slice(start, end) var res = '' for (var i = 0; i < bytes.length; i += 2) { res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256) } return res } Buffer.prototype.slice = function (start, end) { var len = this.length start = ~~start end = end === undefined ? len : ~~end if (start < 0) { start += len; if (start < 0) start = 0 } else if (start > len) { start = len } if (end < 0) { end += len if (end < 0) end = 0 } else if (end > len) { end = len } if (end < start) end = start var newBuf if (Buffer.TYPED_ARRAY_SUPPORT) { newBuf = Buffer._augment(this.subarray(start, end)) } else { var sliceLen = end - start newBuf = new Buffer(sliceLen, undefined, true) for (var i = 0; i < sliceLen; i++) { newBuf[i] = this[i + start] } } if (newBuf.length) newBuf.parent = this.parent || this return newBuf } /* * Need to make sure that buffer isn't trying to write out of bounds. */ function checkOffset (offset, ext, length) { if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') } Buffer.prototype.readUIntLE = function (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) checkOffset(offset, byteLength, this.length) var val = this[offset] var mul = 1 var i = 0 while (++i < byteLength && (mul *= 0x100)) val += this[offset + i] * mul return val } Buffer.prototype.readUIntBE = function (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) checkOffset(offset, byteLength, this.length) var val = this[offset + --byteLength] var mul = 1 while (byteLength > 0 && (mul *= 0x100)) val += this[offset + --byteLength] * mul; return val } Buffer.prototype.readUInt8 = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 1, this.length) return this[offset] } Buffer.prototype.readUInt16LE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 2, this.length) return this[offset] | (this[offset + 1] << 8) } Buffer.prototype.readUInt16BE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 2, this.length) return (this[offset] << 8) | this[offset + 1] } Buffer.prototype.readUInt32LE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 4, this.length) return ((this[offset]) | (this[offset + 1] << 8) | (this[offset + 2] << 16)) + (this[offset + 3] * 0x1000000) } Buffer.prototype.readUInt32BE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 4, this.length) return (this[offset] * 0x1000000) + ((this[offset + 1] << 16) | (this[offset + 2] << 8) | this[offset + 3]) } Buffer.prototype.readIntLE = function (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) checkOffset(offset, byteLength, this.length) var val = this[offset] var mul = 1 var i = 0 while (++i < byteLength && (mul *= 0x100)) val += this[offset + i] * mul mul *= 0x80 if (val >= mul) val -= Math.pow(2, 8 * byteLength) return val } Buffer.prototype.readIntBE = function (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) checkOffset(offset, byteLength, this.length) var i = byteLength var mul = 1 var val = this[offset + --i] while (i > 0 && (mul *= 0x100)) val += this[offset + --i] * mul mul *= 0x80 if (val >= mul) val -= Math.pow(2, 8 * byteLength) return val } Buffer.prototype.readInt8 = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 1, this.length) if (!(this[offset] & 0x80)) return (this[offset]) return ((0xff - this[offset] + 1) * -1) } Buffer.prototype.readInt16LE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 2, this.length) var val = this[offset] | (this[offset + 1] << 8) return (val & 0x8000) ? val | 0xFFFF0000 : val } Buffer.prototype.readInt16BE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 2, this.length) var val = this[offset + 1] | (this[offset] << 8) return (val & 0x8000) ? val | 0xFFFF0000 : val } Buffer.prototype.readInt32LE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 4, this.length) return (this[offset]) | (this[offset + 1] << 8) | (this[offset + 2] << 16) | (this[offset + 3] << 24) } Buffer.prototype.readInt32BE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 4, this.length) return (this[offset] << 24) | (this[offset + 1] << 16) | (this[offset + 2] << 8) | (this[offset + 3]) } Buffer.prototype.readFloatLE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 4, this.length) return ieee754.read(this, offset, true, 23, 4) } Buffer.prototype.readFloatBE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 4, this.length) return ieee754.read(this, offset, false, 23, 4) } Buffer.prototype.readDoubleLE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 8, this.length) return ieee754.read(this, offset, true, 52, 8) } Buffer.prototype.readDoubleBE = function (offset, noAssert) { if (!noAssert) checkOffset(offset, 8, this.length) return ieee754.read(this, offset, false, 52, 8) } function checkInt (buf, value, offset, ext, max, min) { if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance') if (value > max || value < min) throw new RangeError('value is out of bounds') if (offset + ext > buf.length) throw new RangeError('index out of range') } Buffer.prototype.writeUIntLE = function (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) var mul = 1 var i = 0 this[offset] = value & 0xFF while (++i < byteLength && (mul *= 0x100)) this[offset + i] = (value / mul) >>> 0 & 0xFF return offset + byteLength } Buffer.prototype.writeUIntBE = function (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) var i = byteLength - 1 var mul = 1 this[offset + i] = value & 0xFF while (--i >= 0 && (mul *= 0x100)) this[offset + i] = (value / mul) >>> 0 & 0xFF return offset + byteLength } Buffer.prototype.writeUInt8 = function (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) this[offset] = value return offset + 1 } function objectWriteUInt16 (buf, value, offset, littleEndian) { if (value < 0) value = 0xffff + value + 1 for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) { buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>> (littleEndian ? i : 1 - i) * 8 } } Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = value this[offset + 1] = (value >>> 8) } else objectWriteUInt16(this, value, offset, true) return offset + 2 } Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = (value >>> 8) this[offset + 1] = value } else objectWriteUInt16(this, value, offset, false) return offset + 2 } function objectWriteUInt32 (buf, value, offset, littleEndian) { if (value < 0) value = 0xffffffff + value + 1 for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) { buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff } } Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset + 3] = (value >>> 24) this[offset + 2] = (value >>> 16) this[offset + 1] = (value >>> 8) this[offset] = value } else objectWriteUInt32(this, value, offset, true) return offset + 4 } Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = (value >>> 24) this[offset + 1] = (value >>> 16) this[offset + 2] = (value >>> 8) this[offset + 3] = value } else objectWriteUInt32(this, value, offset, false) return offset + 4 } Buffer.prototype.writeIntLE = function (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) { checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength - 1) - 1, -Math.pow(2, 8 * byteLength - 1)) } var i = 0 var mul = 1 var sub = value < 0 ? 1 : 0 this[offset] = value & 0xFF while (++i < byteLength && (mul *= 0x100)) this[offset + i] = ((value / mul) >> 0) - sub & 0xFF return offset + byteLength } Buffer.prototype.writeIntBE = function (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) { checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength - 1) - 1, -Math.pow(2, 8 * byteLength - 1)) } var i = byteLength - 1 var mul = 1 var sub = value < 0 ? 1 : 0 this[offset + i] = value & 0xFF while (--i >= 0 && (mul *= 0x100)) this[offset + i] = ((value / mul) >> 0) - sub & 0xFF return offset + byteLength } Buffer.prototype.writeInt8 = function (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) if (value < 0) value = 0xff + value + 1 this[offset] = value return offset + 1 } Buffer.prototype.writeInt16LE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = value this[offset + 1] = (value >>> 8) } else objectWriteUInt16(this, value, offset, true) return offset + 2 } Buffer.prototype.writeInt16BE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = (value >>> 8) this[offset + 1] = value } else objectWriteUInt16(this, value, offset, false) return offset + 2 } Buffer.prototype.writeInt32LE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = value this[offset + 1] = (value >>> 8) this[offset + 2] = (value >>> 16) this[offset + 3] = (value >>> 24) } else objectWriteUInt32(this, value, offset, true) return offset + 4 } Buffer.prototype.writeInt32BE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) if (value < 0) value = 0xffffffff + value + 1 if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = (value >>> 24) this[offset + 1] = (value >>> 16) this[offset + 2] = (value >>> 8) this[offset + 3] = value } else objectWriteUInt32(this, value, offset, false) return offset + 4 } function checkIEEE754 (buf, value, offset, ext, max, min) { if (value > max || value < min) throw new RangeError('value is out of bounds') if (offset + ext > buf.length) throw new RangeError('index out of range') if (offset < 0) throw new RangeError('index out of range') } function writeFloat (buf, value, offset, littleEndian, noAssert) { if (!noAssert) checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) ieee754.write(buf, value, offset, littleEndian, 23, 4) return offset + 4 } Buffer.prototype.writeFloatLE = function (value, offset, noAssert) { return writeFloat(this, value, offset, true, noAssert) } Buffer.prototype.writeFloatBE = function (value, offset, noAssert) { return writeFloat(this, value, offset, false, noAssert) } function writeDouble (buf, value, offset, littleEndian, noAssert) { if (!noAssert) checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) ieee754.write(buf, value, offset, littleEndian, 52, 8) return offset + 8 } Buffer.prototype.writeDoubleLE = function (value, offset, noAssert) { return writeDouble(this, value, offset, true, noAssert) } Buffer.prototype.writeDoubleBE = function (value, offset, noAssert) { return writeDouble(this, value, offset, false, noAssert) } // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) Buffer.prototype.copy = function (target, target_start, start, end) { var source = this if (!start) start = 0 if (!end && end !== 0) end = this.length if (target_start >= target.length) target_start = target.length if (!target_start) target_start = 0 if (end > 0 && end < start) end = start // Copy 0 bytes; we're done if (end === start) return 0 if (target.length === 0 || source.length === 0) return 0 // Fatal error conditions if (target_start < 0) throw new RangeError('targetStart out of bounds') if (start < 0 || start >= source.length) throw new RangeError('sourceStart out of bounds') if (end < 0) throw new RangeError('sourceEnd out of bounds') // Are we oob? if (end > this.length) end = this.length if (target.length - target_start < end - start) end = target.length - target_start + start var len = end - start if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) { for (var i = 0; i < len; i++) { target[i + target_start] = this[i + start] } } else { target._set(this.subarray(start, start + len), target_start) } return len } // fill(value, start=0, end=buffer.length) Buffer.prototype.fill = function (value, start, end) { if (!value) value = 0 if (!start) start = 0 if (!end) end = this.length if (end < start) throw new RangeError('end < start') // Fill 0 bytes; we're done if (end === start) return if (this.length === 0) return if (start < 0 || start >= this.length) throw new RangeError('start out of bounds') if (end < 0 || end > this.length) throw new RangeError('end out of bounds') var i if (typeof value === 'number') { for (i = start; i < end; i++) { this[i] = value } } else { var bytes = utf8ToBytes(value.toString()) var len = bytes.length for (i = start; i < end; i++) { this[i] = bytes[i % len] } } return this } /** * Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance. * Added in Node 0.12. Only available in browsers that support ArrayBuffer. */ Buffer.prototype.toArrayBuffer = function () { if (typeof Uint8Array !== 'undefined') { if (Buffer.TYPED_ARRAY_SUPPORT) { return (new Buffer(this)).buffer } else { var buf = new Uint8Array(this.length) for (var i = 0, len = buf.length; i < len; i += 1) { buf[i] = this[i] } return buf.buffer } } else { throw new TypeError('Buffer.toArrayBuffer not supported in this browser') } } // HELPER FUNCTIONS // ================ var BP = Buffer.prototype /** * Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods */ Buffer._augment = function (arr) { arr.constructor = Buffer arr._isBuffer = true // save reference to original Uint8Array get/set methods before overwriting arr._get = arr.get arr._set = arr.set // deprecated, will be removed in node 0.13+ arr.get = BP.get arr.set = BP.set arr.write = BP.write arr.toString = BP.toString arr.toLocaleString = BP.toString arr.toJSON = BP.toJSON arr.equals = BP.equals arr.compare = BP.compare arr.copy = BP.copy arr.slice = BP.slice arr.readUIntLE = BP.readUIntLE arr.readUIntBE = BP.readUIntBE arr.readUInt8 = BP.readUInt8 arr.readUInt16LE = BP.readUInt16LE arr.readUInt16BE = BP.readUInt16BE arr.readUInt32LE = BP.readUInt32LE arr.readUInt32BE = BP.readUInt32BE arr.readIntLE = BP.readIntLE arr.readIntBE = BP.readIntBE arr.readInt8 = BP.readInt8 arr.readInt16LE = BP.readInt16LE arr.readInt16BE = BP.readInt16BE arr.readInt32LE = BP.readInt32LE arr.readInt32BE = BP.readInt32BE arr.readFloatLE = BP.readFloatLE arr.readFloatBE = BP.readFloatBE arr.readDoubleLE = BP.readDoubleLE arr.readDoubleBE = BP.readDoubleBE arr.writeUInt8 = BP.writeUInt8 arr.writeUIntLE = BP.writeUIntLE arr.writeUIntBE = BP.writeUIntBE arr.writeUInt16LE = BP.writeUInt16LE arr.writeUInt16BE = BP.writeUInt16BE arr.writeUInt32LE = BP.writeUInt32LE arr.writeUInt32BE = BP.writeUInt32BE arr.writeIntLE = BP.writeIntLE arr.writeIntBE = BP.writeIntBE arr.writeInt8 = BP.writeInt8 arr.writeInt16LE = BP.writeInt16LE arr.writeInt16BE = BP.writeInt16BE arr.writeInt32LE = BP.writeInt32LE arr.writeInt32BE = BP.writeInt32BE arr.writeFloatLE = BP.writeFloatLE arr.writeFloatBE = BP.writeFloatBE arr.writeDoubleLE = BP.writeDoubleLE arr.writeDoubleBE = BP.writeDoubleBE arr.fill = BP.fill arr.inspect = BP.inspect arr.toArrayBuffer = BP.toArrayBuffer return arr } var INVALID_BASE64_RE = /[^+\/0-9A-z\-]/g function base64clean (str) { // Node strips out invalid characters like \n and \t from the string, base64-js does not str = stringtrim(str).replace(INVALID_BASE64_RE, '') // Node converts strings with length < 2 to '' if (str.length < 2) return '' // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not while (str.length % 4 !== 0) { str = str + '=' } return str } function stringtrim (str) { if (str.trim) return str.trim() return str.replace(/^\s+|\s+$/g, '') } function isArrayish (subject) { return isArray(subject) || Buffer.isBuffer(subject) || subject && typeof subject === 'object' && typeof subject.length === 'number' } function toHex (n) { if (n < 16) return '0' + n.toString(16) return n.toString(16) } function utf8ToBytes(string, units) { var codePoint, length = string.length var leadSurrogate = null units = units || Infinity var bytes = [] var i = 0 for (; i 0xD7FF && codePoint < 0xE000) { // last char was a lead if (leadSurrogate) { // 2 leads in a row if (codePoint < 0xDC00) { if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) leadSurrogate = codePoint continue } // valid surrogate pair else { codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000 leadSurrogate = null } } // no lead yet else { // unexpected trail if (codePoint > 0xDBFF) { if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) continue } // unpaired lead else if (i + 1 === length) { if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) continue } // valid lead else { leadSurrogate = codePoint continue } } } // valid bmp char, but last char was a lead else if (leadSurrogate) { if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) leadSurrogate = null } // encode utf8 if (codePoint < 0x80) { if ((units -= 1) < 0) break bytes.push(codePoint) } else if (codePoint < 0x800) { if ((units -= 2) < 0) break bytes.push( codePoint >> 0x6 | 0xC0, codePoint & 0x3F | 0x80 ); } else if (codePoint < 0x10000) { if ((units -= 3) < 0) break bytes.push( codePoint >> 0xC | 0xE0, codePoint >> 0x6 & 0x3F | 0x80, codePoint & 0x3F | 0x80 ); } else if (codePoint < 0x200000) { if ((units -= 4) < 0) break bytes.push( codePoint >> 0x12 | 0xF0, codePoint >> 0xC & 0x3F | 0x80, codePoint >> 0x6 & 0x3F | 0x80, codePoint & 0x3F | 0x80 ); } else { throw new Error('Invalid code point') } } return bytes } function asciiToBytes (str) { var byteArray = [] for (var i = 0; i < str.length; i++) { // Node's code seems to be doing this and not & 0x7F.. byteArray.push(str.charCodeAt(i) & 0xFF) } return byteArray } function utf16leToBytes (str, units) { var c, hi, lo var byteArray = [] for (var i = 0; i < str.length; i++) { if ((units -= 2) < 0) break c = str.charCodeAt(i) hi = c >> 8 lo = c % 256 byteArray.push(lo) byteArray.push(hi) } return byteArray } function base64ToBytes (str) { return base64.toByteArray(base64clean(str)) } function blitBuffer (src, dst, offset, length, unitSize) { if (unitSize) length -= length % unitSize; for (var i = 0; i < length; i++) { if ((i + offset >= dst.length) || (i >= src.length)) break dst[i + offset] = src[i] } return i } function decodeUtf8Char (str) { try { return decodeURIComponent(str) } catch (err) { return String.fromCharCode(0xFFFD) // UTF 8 invalid char } } },{"base64-js":12,"ieee754":13,"is-array":14}],12:[function(require,module,exports){ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; ;(function (exports) { 'use strict'; var Arr = (typeof Uint8Array !== 'undefined') ? Uint8Array : Array var PLUS = '+'.charCodeAt(0) var SLASH = '/'.charCodeAt(0) var NUMBER = '0'.charCodeAt(0) var LOWER = 'a'.charCodeAt(0) var UPPER = 'A'.charCodeAt(0) var PLUS_URL_SAFE = '-'.charCodeAt(0) var SLASH_URL_SAFE = '_'.charCodeAt(0) function decode (elt) { var code = elt.charCodeAt(0) if (code === PLUS || code === PLUS_URL_SAFE) return 62 // '+' if (code === SLASH || code === SLASH_URL_SAFE) return 63 // '/' if (code < NUMBER) return -1 //no match if (code < NUMBER + 10) return code - NUMBER + 26 + 26 if (code < UPPER + 26) return code - UPPER if (code < LOWER + 26) return code - LOWER + 26 } function b64ToByteArray (b64) { var i, j, l, tmp, placeHolders, arr if (b64.length % 4 > 0) { throw new Error('Invalid string. Length must be a multiple of 4') } // the number of equal signs (place holders) // if there are two placeholders, than the two characters before it // represent one byte // if there is only one, then the three characters before it represent 2 bytes // this is just a cheap hack to not do indexOf twice var len = b64.length placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 0 // base64 is 4/3 + up to two characters of the original data arr = new Arr(b64.length * 3 / 4 - placeHolders) // if there are placeholders, only get up to the last complete 4 chars l = placeHolders > 0 ? b64.length - 4 : b64.length var L = 0 function push (v) { arr[L++] = v } for (i = 0, j = 0; i < l; i += 4, j += 3) { tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3)) push((tmp & 0xFF0000) >> 16) push((tmp & 0xFF00) >> 8) push(tmp & 0xFF) } if (placeHolders === 2) { tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4) push(tmp & 0xFF) } else if (placeHolders === 1) { tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2) push((tmp >> 8) & 0xFF) push(tmp & 0xFF) } return arr } function uint8ToBase64 (uint8) { var i, extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes output = "", temp, length function encode (num) { return lookup.charAt(num) } function tripletToBase64 (num) { return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F) } // go through the array every three bytes, we'll deal with trailing stuff later for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) { temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) output += tripletToBase64(temp) } // pad the end with zeros, but make sure to not forget the extra bytes switch (extraBytes) { case 1: temp = uint8[uint8.length - 1] output += encode(temp >> 2) output += encode((temp << 4) & 0x3F) output += '==' break case 2: temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]) output += encode(temp >> 10) output += encode((temp >> 4) & 0x3F) output += encode((temp << 2) & 0x3F) output += '=' break } return output } exports.toByteArray = b64ToByteArray exports.fromByteArray = uint8ToBase64 }(typeof exports === 'undefined' ? (this.base64js = {}) : exports)) },{}],13:[function(require,module,exports){ exports.read = function(buffer, offset, isLE, mLen, nBytes) { var e, m, eLen = nBytes * 8 - mLen - 1, eMax = (1 << eLen) - 1, eBias = eMax >> 1, nBits = -7, i = isLE ? (nBytes - 1) : 0, d = isLE ? -1 : 1, s = buffer[offset + i]; i += d; e = s & ((1 << (-nBits)) - 1); s >>= (-nBits); nBits += eLen; for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8); m = e & ((1 << (-nBits)) - 1); e >>= (-nBits); nBits += mLen; for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8); if (e === 0) { e = 1 - eBias; } else if (e === eMax) { return m ? NaN : ((s ? -1 : 1) * Infinity); } else { m = m + Math.pow(2, mLen); e = e - eBias; } return (s ? -1 : 1) * m * Math.pow(2, e - mLen); }; exports.write = function(buffer, value, offset, isLE, mLen, nBytes) { var e, m, c, eLen = nBytes * 8 - mLen - 1, eMax = (1 << eLen) - 1, eBias = eMax >> 1, rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0), i = isLE ? 0 : (nBytes - 1), d = isLE ? 1 : -1, s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0; value = Math.abs(value); if (isNaN(value) || value === Infinity) { m = isNaN(value) ? 1 : 0; e = eMax; } else { e = Math.floor(Math.log(value) / Math.LN2); if (value * (c = Math.pow(2, -e)) < 1) { e--; c *= 2; } if (e + eBias >= 1) { value += rt / c; } else { value += rt * Math.pow(2, 1 - eBias); } if (value * c >= 2) { e++; c /= 2; } if (e + eBias >= eMax) { m = 0; e = eMax; } else if (e + eBias >= 1) { m = (value * c - 1) * Math.pow(2, mLen); e = e + eBias; } else { m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen); e = 0; } } for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8); e = (e << mLen) | m; eLen += mLen; for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8); buffer[offset + i - d] |= s * 128; }; },{}],14:[function(require,module,exports){ /** * isArray */ var isArray = Array.isArray; /** * toString */ var str = Object.prototype.toString; /** * Whether or not the given `val` * is an array. * * example: * * isArray([]); * // > true * isArray(arguments); * // > false * isArray(''); * // > false * * @param {mixed} val * @return {bool} */ module.exports = isArray || function (val) { return !! val && '[object Array]' == str.call(val); }; },{}]},{},[1]);