/pe.js
http://pejs.codeplex.com · JavaScript · 3399 lines · 3396 code · 2 blank · 1 comment · 375 complexity · ff2b188e25046e60a590727d514ff056 MD5 · raw file
Large files are truncated click here to view the full file
- var __extends = this.__extends || function (d, b) {
- function __() { this.constructor = d; }
- __.prototype = b.prototype;
- d.prototype = new __();
- };
- var pe;
- (function (pe) {
- (function (io) {
- var Long = (function () {
- function Long(lo, hi) {
- this.lo = lo;
- this.hi = hi;
- }
- Long.prototype.toString = function () {
- var result;
- result = this.lo.toString(16);
- if(this.hi != 0) {
- result = ("0000").substring(result.length) + result;
- result = this.hi.toString(16) + result;
- }
- result = result.toUpperCase() + "h";
- return result;
- };
- return Long;
- })();
- io.Long = Long;
- var AddressRange = (function () {
- function AddressRange(address, size) {
- this.address = address;
- this.size = size;
- if(!this.address) {
- this.address = 0;
- }
- if(!this.size) {
- this.size = 0;
- }
- }
- AddressRange.prototype.mapRelative = function (offset) {
- var result = offset - this.address;
- if(result >= 0 && result < this.size) {
- return result;
- } else {
- return -1;
- }
- };
- AddressRange.prototype.toString = function () {
- return this.address.toString(16).toUpperCase() + ":" + this.size.toString(16).toUpperCase() + "h";
- };
- return AddressRange;
- })();
- io.AddressRange = AddressRange;
- var AddressRangeMap = (function (_super) {
- __extends(AddressRangeMap, _super);
- function AddressRangeMap(address, size, virtualAddress) {
- _super.call(this, address, size);
- this.virtualAddress = virtualAddress;
- if(!this.virtualAddress) {
- this.virtualAddress = 0;
- }
- }
- AddressRangeMap.prototype.toString = function () {
- return this.address.toString(16).toUpperCase() + ":" + this.size.toString(16).toUpperCase() + "@" + this.virtualAddress + "h";
- };
- return AddressRangeMap;
- })(AddressRange);
- io.AddressRangeMap = AddressRangeMap;
- var checkBufferReaderOverrideOnFirstCreation = true;
- var hexUtf = (function () {
- var buf = [];
- for(var i = 0; i < 127; i++) {
- buf.push(String.fromCharCode(i));
- }
- for(var i = 127; i < 256; i++) {
- buf.push("%" + i.toString(16));
- }
- return buf;
- })();
- var BufferReader = (function () {
- function BufferReader(view) {
- this.offset = 0;
- this.sections = [];
- this._currentSectionIndex = 0;
- if(checkBufferReaderOverrideOnFirstCreation) {
- checkBufferReaderOverrideOnFirstCreation = false;
- var global = (function () {
- return this;
- })();
- if(!("DataView" in global)) {
- io.BufferReader = ArrayReader;
- return new ArrayReader(view);
- }
- }
- if(!view) {
- return;
- }
- if("getUint8" in view) {
- this._view = view;
- } else if("byteLength" in view) {
- this._view = new DataView(view);
- } else {
- var arrb = new ArrayBuffer(view.length);
- this._view = new DataView(arrb);
- for(var i = 0; i < view.length; i++) {
- this._view.setUint8(i, view[i]);
- }
- }
- }
- BufferReader.prototype.readByte = function () {
- var result = this._view.getUint8(this.offset);
- this.offset++;
- return result;
- };
- BufferReader.prototype.peekByte = function () {
- var result = this._view.getUint8(this.offset);
- return result;
- };
- BufferReader.prototype.readShort = function () {
- var result = this._view.getUint16(this.offset, true);
- this.offset += 2;
- return result;
- };
- BufferReader.prototype.readInt = function () {
- var result = this._view.getUint32(this.offset, true);
- this.offset += 4;
- return result;
- };
- BufferReader.prototype.readLong = function () {
- var lo = this._view.getUint32(this.offset, true);
- var hi = this._view.getUint32(this.offset + 4, true);
- this.offset += 8;
- return new Long(lo, hi);
- };
- BufferReader.prototype.readBytes = function (length) {
- var result = new Uint8Array(this._view.buffer, this._view.byteOffset + this.offset, length);
- this.offset += length;
- return result;
- };
- BufferReader.prototype.readZeroFilledAscii = function (length) {
- var chars = [];
- for(var i = 0; i < length; i++) {
- var charCode = this._view.getUint8(this.offset + i);
- if(charCode == 0) {
- continue;
- }
- chars.push(String.fromCharCode(charCode));
- }
- this.offset += length;
- return chars.join("");
- };
- BufferReader.prototype.readAsciiZ = function (maxLength) {
- if (typeof maxLength === "undefined") { maxLength = 1024; }
- var chars = [];
- var byteLength = 0;
- while(true) {
- var nextChar = this._view.getUint8(this.offset + chars.length);
- if(nextChar == 0) {
- byteLength = chars.length + 1;
- break;
- }
- chars.push(String.fromCharCode(nextChar));
- if(chars.length == maxLength) {
- byteLength = chars.length;
- break;
- }
- }
- this.offset += byteLength;
- return chars.join("");
- };
- BufferReader.prototype.readUtf8Z = function (maxLength) {
- var buffer = [];
- var isConversionRequired = false;
- for(var i = 0; !maxLength || i < maxLength; i++) {
- var b = this._view.getUint8(this.offset + i);
- if(b == 0) {
- i++;
- break;
- }
- buffer.push(hexUtf[b]);
- if(b >= 127) {
- isConversionRequired = true;
- }
- }
- this.offset += i;
- if(isConversionRequired) {
- return decodeURIComponent(buffer.join(""));
- } else {
- return buffer.join("");
- }
- };
- BufferReader.prototype.getVirtualOffset = function () {
- var result = this.tryMapToVirtual(this.offset);
- if(result < 0) {
- throw new Error("Cannot map current position into virtual address space.");
- }
- return result;
- };
- BufferReader.prototype.setVirtualOffset = function (rva) {
- if(this._currentSectionIndex >= 0 && this._currentSectionIndex < this.sections.length) {
- var s = this.sections[this._currentSectionIndex];
- var relative = rva - s.virtualAddress;
- if(relative >= 0 && relative < s.size) {
- this.offset = relative + s.address;
- return;
- }
- }
- for(var i = 0; i < this.sections.length; i++) {
- var s = this.sections[i];
- var relative = rva - s.virtualAddress;
- if(relative >= 0 && relative < s.size) {
- this._currentSectionIndex = i;
- this.offset = relative + s.address;
- return;
- }
- }
- throw new Error("Address 0x" + rva.toString(16).toUpperCase() + " is outside of virtual address space.");
- };
- BufferReader.prototype.tryMapToVirtual = function (offset) {
- if(this._currentSectionIndex >= 0 && this._currentSectionIndex < this.sections.length) {
- var s = this.sections[this._currentSectionIndex];
- var relative = offset - s.address;
- if(relative >= 0 && relative < s.size) {
- return relative + s.virtualAddress;
- }
- }
- for(var i = 0; i < this.sections.length; i++) {
- var s = this.sections[i];
- var relative = offset - s.address;
- if(relative >= 0 && relative < s.size) {
- this._currentSectionIndex = i;
- return relative + s.virtualAddress;
- }
- }
- return -1;
- };
- return BufferReader;
- })();
- io.BufferReader = BufferReader;
- var ArrayReader = (function (_super) {
- __extends(ArrayReader, _super);
- function ArrayReader(_array) {
- _super.call(this, null);
- this._array = _array;
- this.offset = 0;
- this.sections = [];
- this._currentSectionIndex = 0;
- }
- ArrayReader.prototype.readByte = function () {
- var result = this._array[this.offset];
- this.offset++;
- return result;
- };
- ArrayReader.prototype.peekByte = function () {
- var result = this._array[this.offset];
- return result;
- };
- ArrayReader.prototype.readShort = function () {
- var result = this._array[this.offset] + (this._array[this.offset + 1] << 8);
- this.offset += 2;
- return result;
- };
- ArrayReader.prototype.readInt = function () {
- var result = this._array[this.offset] + (this._array[this.offset + 1] << 8) + (this._array[this.offset + 2] << 16) + (this._array[this.offset + 3] * 16777216);
- this.offset += 4;
- return result;
- };
- ArrayReader.prototype.readLong = function () {
- var lo = this.readInt();
- var hi = this.readInt();
- return new Long(lo, hi);
- };
- ArrayReader.prototype.readBytes = function (length) {
- var result = this._array.slice(this.offset, this.offset + length);
- this.offset += length;
- return result;
- };
- ArrayReader.prototype.readZeroFilledAscii = function (length) {
- var chars = [];
- for(var i = 0; i < length; i++) {
- var charCode = this._array[this.offset + i];
- if(charCode == 0) {
- continue;
- }
- chars.push(String.fromCharCode(charCode));
- }
- this.offset += length;
- return chars.join("");
- };
- ArrayReader.prototype.readAsciiZ = function (maxLength) {
- if (typeof maxLength === "undefined") { maxLength = 1024; }
- var chars = [];
- var byteLength = 0;
- while(true) {
- var nextChar = this._array[this.offset + chars.length];
- if(nextChar == 0) {
- byteLength = chars.length + 1;
- break;
- }
- chars.push(String.fromCharCode(nextChar));
- if(chars.length == maxLength) {
- byteLength = chars.length;
- break;
- }
- }
- this.offset += byteLength;
- return chars.join("");
- };
- ArrayReader.prototype.readUtf8Z = function (maxLength) {
- var buffer = "";
- var isConversionRequired = false;
- for(var i = 0; !maxLength || i < maxLength; i++) {
- var b = this._array[this.offset + i];
- if(b == 0) {
- i++;
- break;
- }
- if(b < 127) {
- buffer += String.fromCharCode(b);
- } else {
- isConversionRequired = true;
- buffer += "%";
- buffer += b.toString(16);
- }
- }
- this.offset += i;
- if(isConversionRequired) {
- return decodeURIComponent(buffer);
- } else {
- return buffer;
- }
- };
- ArrayReader.prototype.getVirtualOffset = function () {
- var result = this.tryMapToVirtual(this.offset);
- if(result < 0) {
- throw new Error("Cannot map current position into virtual address space.");
- }
- return result;
- };
- ArrayReader.prototype.setVirtualOffset = function (rva) {
- if(this._currentSectionIndex >= 0 && this._currentSectionIndex < this.sections.length) {
- var s = this.sections[this._currentSectionIndex];
- var relative = rva - s.virtualAddress;
- if(relative >= 0 && relative < s.size) {
- this.offset = relative + s.address;
- return;
- }
- }
- for(var i = 0; i < this.sections.length; i++) {
- var s = this.sections[i];
- var relative = rva - s.virtualAddress;
- if(relative >= 0 && relative < s.size) {
- this._currentSectionIndex = i;
- this.offset = relative + s.address;
- return;
- }
- }
- throw new Error("Address is outside of virtual address space.");
- };
- ArrayReader.prototype.tryMapToVirtual = function (offset) {
- if(this._currentSectionIndex >= 0 && this._currentSectionIndex < this.sections.length) {
- var s = this.sections[this._currentSectionIndex];
- var relative = offset - s.address;
- if(relative >= 0 && relative < s.size) {
- return relative + s.virtualAddress;
- }
- }
- for(var i = 0; i < this.sections.length; i++) {
- var s = this.sections[i];
- var relative = offset - s.address;
- if(relative >= 0 && relative < s.size) {
- this._currentSectionIndex = i;
- return relative + s.virtualAddress;
- }
- }
- return -1;
- };
- return ArrayReader;
- })(BufferReader);
- io.ArrayReader = ArrayReader;
- function getFileBufferReader(file, onsuccess, onfailure) {
- var reader = new FileReader();
- reader.onerror = onfailure;
- reader.onloadend = function () {
- if(reader.readyState != 2) {
- onfailure(reader.error);
- return;
- }
- var result;
- try {
- var resultArrayBuffer;
- resultArrayBuffer = reader.result;
- result = new BufferReader(resultArrayBuffer);
- } catch (error) {
- onfailure(error);
- }
- onsuccess(result);
- };
- reader.readAsArrayBuffer(file);
- }
- io.getFileBufferReader = getFileBufferReader;
- function getUrlBufferReader(url, onsuccess, onfailure) {
- var request = new XMLHttpRequest();
- request.open("GET", url, true);
- request.responseType = "arraybuffer";
- var requestLoadCompleteCalled = false;
- function requestLoadComplete() {
- if(requestLoadCompleteCalled) {
- return;
- }
- requestLoadCompleteCalled = true;
- var result;
- try {
- var response = request.response;
- if(response) {
- var resultDataView = new DataView(response);
- result = new BufferReader(resultDataView);
- } else {
- var responseBody = new VBArray(request.responseBody).toArray();
- var result = new BufferReader(responseBody);
- }
- } catch (error) {
- onfailure(error);
- return;
- }
- onsuccess(result);
- }
- ;
- request.onerror = onfailure;
- request.onloadend = function () {
- return requestLoadComplete;
- };
- request.onreadystatechange = function () {
- if(request.readyState == 4) {
- requestLoadComplete();
- }
- };
- request.send();
- }
- io.getUrlBufferReader = getUrlBufferReader;
- function bytesToHex(bytes) {
- if(!bytes) {
- return null;
- }
- var result = "";
- for(var i = 0; i < bytes.length; i++) {
- var hex = bytes[i].toString(16).toUpperCase();
- if(hex.length == 1) {
- hex = "0" + hex;
- }
- result += hex;
- }
- return result;
- }
- io.bytesToHex = bytesToHex;
- function formatEnum(value, type) {
- if(!value) {
- if(typeof value == "null") {
- return "null";
- } else if(typeof value == "undefined") {
- return "undefined";
- }
- }
- var textValue = null;
- if(type._map) {
- textValue = type._map[value];
- if(!type._map_fixed) {
- for(var e in type) {
- var num = type[e];
- if(typeof num == "number") {
- type._map[num] = e;
- }
- }
- type._map_fixed = true;
- textValue = type._map[value];
- }
- }
- if(textValue == null) {
- if(typeof value == "number") {
- var enumValues = [];
- var accountedEnumValueMask = 0;
- var zeroName = null;
- for(var kvValueStr in type._map) {
- var kvValue;
- try {
- kvValue = Number(kvValueStr);
- } catch (errorConverting) {
- continue;
- }
- if(kvValue == 0) {
- zeroName = kvKey;
- continue;
- }
- var kvKey = type._map[kvValueStr];
- if(typeof kvValue != "number") {
- continue;
- }
- if((value & kvValue) == kvValue) {
- enumValues.push(kvKey);
- accountedEnumValueMask = accountedEnumValueMask | kvValue;
- }
- }
- var spill = value & accountedEnumValueMask;
- if(!spill) {
- enumValues.push("#" + spill.toString(16).toUpperCase() + "h");
- }
- if(enumValues.length == 0) {
- if(zeroName) {
- textValue = zeroName;
- } else {
- textValue = "0";
- }
- } else {
- textValue = enumValues.join('|');
- }
- } else {
- textValue = "enum:" + value;
- }
- }
- return textValue;
- }
- io.formatEnum = formatEnum;
- })(pe.io || (pe.io = {}));
- var io = pe.io;
- })(pe || (pe = {}));
- var pe;
- (function (pe) {
- (function (headers) {
- var PEFileHeaders = (function () {
- function PEFileHeaders() {
- this.dosHeader = new DosHeader();
- this.peHeader = new PEHeader();
- this.optionalHeader = new OptionalHeader();
- this.sectionHeaders = [];
- }
- PEFileHeaders.prototype.toString = function () {
- var result = "dosHeader: " + (this.dosHeader ? this.dosHeader + "" : "null") + " " + "dosStub: " + (this.dosStub ? "[" + this.dosStub.length + "]" : "null") + " " + "peHeader: " + (this.peHeader ? "[" + this.peHeader.machine + "]" : "null") + " " + "optionalHeader: " + (this.optionalHeader ? "[" + pe.io.formatEnum(this.optionalHeader.subsystem, Subsystem) + "," + this.optionalHeader.imageVersion + "]" : "null") + " " + "sectionHeaders: " + (this.sectionHeaders ? "[" + this.sectionHeaders.length + "]" : "null");
- return result;
- };
- PEFileHeaders.prototype.read = function (reader) {
- var dosHeaderSize = 64;
- if(!this.dosHeader) {
- this.dosHeader = new DosHeader();
- }
- this.dosHeader.read(reader);
- var dosHeaderLength = this.dosHeader.lfanew - dosHeaderSize;
- if(dosHeaderLength > 0) {
- this.dosStub = reader.readBytes(dosHeaderLength);
- } else {
- this.dosStub = null;
- }
- if(!this.peHeader) {
- this.peHeader = new PEHeader();
- }
- this.peHeader.read(reader);
- if(!this.optionalHeader) {
- this.optionalHeader = new OptionalHeader();
- }
- this.optionalHeader.read(reader);
- if(this.peHeader.numberOfSections > 0) {
- if(!this.sectionHeaders || this.sectionHeaders.length != this.peHeader.numberOfSections) {
- this.sectionHeaders = Array(this.peHeader.numberOfSections);
- }
- for(var i = 0; i < this.sectionHeaders.length; i++) {
- if(!this.sectionHeaders[i]) {
- this.sectionHeaders[i] = new SectionHeader();
- }
- this.sectionHeaders[i].read(reader);
- }
- }
- };
- return PEFileHeaders;
- })();
- headers.PEFileHeaders = PEFileHeaders;
- var DosHeader = (function () {
- function DosHeader() {
- this.mz = MZSignature.MZ;
- this.cblp = 144;
- this.cp = 3;
- this.crlc = 0;
- this.cparhdr = 4;
- this.minalloc = 0;
- this.maxalloc = 65535;
- this.ss = 0;
- this.sp = 184;
- this.csum = 0;
- this.ip = 0;
- this.cs = 0;
- this.lfarlc = 64;
- this.ovno = 0;
- this.res1 = new pe.io.Long(0, 0);
- this.oemid = 0;
- this.oeminfo = 0;
- this.reserved = [
- 0,
- 0,
- 0,
- 0,
- 0
- ];
- this.lfanew = 0;
- }
- DosHeader.prototype.toString = function () {
- var result = "[" + (this.mz === MZSignature.MZ ? "MZ" : typeof this.mz === "number" ? (this.mz).toString(16).toUpperCase() + "h" : typeof this.mz) + "]" + ".lfanew=" + (typeof this.lfanew === "number" ? this.lfanew.toString(16).toUpperCase() + "h" : typeof this.lfanew);
- return result;
- };
- DosHeader.prototype.read = function (reader) {
- this.mz = reader.readShort();
- if(this.mz != MZSignature.MZ) {
- throw new Error("MZ signature is invalid: " + ((this.mz)).toString(16).toUpperCase() + "h.");
- }
- this.cblp = reader.readShort();
- this.cp = reader.readShort();
- this.crlc = reader.readShort();
- this.cparhdr = reader.readShort();
- this.minalloc = reader.readShort();
- this.maxalloc = reader.readShort();
- this.ss = reader.readShort();
- this.sp = reader.readShort();
- this.csum = reader.readShort();
- this.ip = reader.readShort();
- this.cs = reader.readShort();
- this.lfarlc = reader.readShort();
- this.ovno = reader.readShort();
- this.res1 = reader.readLong();
- this.oemid = reader.readShort();
- this.oeminfo = reader.readShort();
- if(!this.reserved) {
- this.reserved = [];
- }
- for(var i = 0; i < 5; i++) {
- this.reserved[i] = reader.readInt();
- }
- this.reserved.length = 5;
- this.lfanew = reader.readInt();
- };
- return DosHeader;
- })();
- headers.DosHeader = DosHeader;
- (function (MZSignature) {
- MZSignature._map = [];
- MZSignature.MZ = "M".charCodeAt(0) + ("Z".charCodeAt(0) << 8);
- })(headers.MZSignature || (headers.MZSignature = {}));
- var MZSignature = headers.MZSignature;
- var PEHeader = (function () {
- function PEHeader() {
- this.pe = PESignature.PE;
- this.machine = Machine.I386;
- this.numberOfSections = 0;
- this.timestamp = new Date(0);
- this.pointerToSymbolTable = 0;
- this.numberOfSymbols = 0;
- this.sizeOfOptionalHeader = 0;
- this.characteristics = ImageCharacteristics.Dll | ImageCharacteristics.Bit32Machine;
- }
- PEHeader.prototype.toString = function () {
- var result = pe.io.formatEnum(this.machine, Machine) + " " + pe.io.formatEnum(this.characteristics, ImageCharacteristics) + " " + "Sections[" + this.numberOfSections + "]";
- return result;
- };
- PEHeader.prototype.read = function (reader) {
- this.pe = reader.readInt();
- if(this.pe != PESignature.PE) {
- throw new Error("PE signature is invalid: " + ((this.pe)).toString(16).toUpperCase() + "h.");
- }
- this.machine = reader.readShort();
- this.numberOfSections = reader.readShort();
- if(!this.timestamp) {
- this.timestamp = new Date(0);
- }
- this.timestamp.setTime(reader.readInt() * 1000);
- this.pointerToSymbolTable = reader.readInt();
- this.numberOfSymbols = reader.readInt();
- this.sizeOfOptionalHeader = reader.readShort();
- this.characteristics = reader.readShort();
- };
- return PEHeader;
- })();
- headers.PEHeader = PEHeader;
- (function (PESignature) {
- PESignature._map = [];
- PESignature.PE = "P".charCodeAt(0) + ("E".charCodeAt(0) << 8);
- })(headers.PESignature || (headers.PESignature = {}));
- var PESignature = headers.PESignature;
- (function (Machine) {
- Machine._map = [];
- Machine.Unknown = 0;
- Machine.I386 = 332;
- Machine.R3000 = 354;
- Machine.R4000 = 358;
- Machine.R10000 = 360;
- Machine.WCEMIPSV2 = 361;
- Machine.Alpha = 388;
- Machine.SH3 = 418;
- Machine.SH3DSP = 419;
- Machine.SH3E = 420;
- Machine.SH4 = 422;
- Machine.SH5 = 424;
- Machine.ARM = 448;
- Machine.Thumb = 450;
- Machine.AM33 = 467;
- Machine.PowerPC = 496;
- Machine.PowerPCFP = 497;
- Machine.IA64 = 512;
- Machine.MIPS16 = 614;
- Machine.Alpha64 = 644;
- Machine.MIPSFPU = 870;
- Machine.MIPSFPU16 = 1126;
- Machine.AXP64 = Machine.Alpha64;
- Machine.Tricore = 1312;
- Machine.CEF = 3311;
- Machine.EBC = 3772;
- Machine.AMD64 = 34404;
- Machine.M32R = 36929;
- Machine.CEE = 49390;
- })(headers.Machine || (headers.Machine = {}));
- var Machine = headers.Machine;
- (function (ImageCharacteristics) {
- ImageCharacteristics._map = [];
- ImageCharacteristics.RelocsStripped = 1;
- ImageCharacteristics.ExecutableImage = 2;
- ImageCharacteristics.LineNumsStripped = 4;
- ImageCharacteristics.LocalSymsStripped = 8;
- ImageCharacteristics.AggressiveWsTrim = 16;
- ImageCharacteristics.LargeAddressAware = 32;
- ImageCharacteristics.BytesReversedLo = 128;
- ImageCharacteristics.Bit32Machine = 256;
- ImageCharacteristics.DebugStripped = 512;
- ImageCharacteristics.RemovableRunFromSwap = 1024;
- ImageCharacteristics.NetRunFromSwap = 2048;
- ImageCharacteristics.System = 4096;
- ImageCharacteristics.Dll = 8192;
- ImageCharacteristics.UpSystemOnly = 16384;
- ImageCharacteristics.BytesReversedHi = 32768;
- })(headers.ImageCharacteristics || (headers.ImageCharacteristics = {}));
- var ImageCharacteristics = headers.ImageCharacteristics;
- var OptionalHeader = (function () {
- function OptionalHeader() {
- this.peMagic = PEMagic.NT32;
- this.linkerVersion = "";
- this.sizeOfCode = 0;
- this.sizeOfInitializedData = 0;
- this.sizeOfUninitializedData = 0;
- this.addressOfEntryPoint = 0;
- this.baseOfCode = 8192;
- this.baseOfData = 16384;
- this.imageBase = 16384;
- this.sectionAlignment = 8192;
- this.fileAlignment = 512;
- this.operatingSystemVersion = "";
- this.imageVersion = "";
- this.subsystemVersion = "";
- this.win32VersionValue = 0;
- this.sizeOfImage = 0;
- this.sizeOfHeaders = 0;
- this.checkSum = 0;
- this.subsystem = Subsystem.WindowsCUI;
- this.dllCharacteristics = DllCharacteristics.NxCompatible;
- this.sizeOfStackReserve = 1048576;
- this.sizeOfStackCommit = 4096;
- this.sizeOfHeapReserve = 1048576;
- this.sizeOfHeapCommit = 4096;
- this.loaderFlags = 0;
- this.numberOfRvaAndSizes = 16;
- this.dataDirectories = [];
- }
- OptionalHeader.prototype.toString = function () {
- var result = [];
- var peMagicText = pe.io.formatEnum(this.peMagic, PEMagic);
- if(peMagicText) {
- result.push(peMagicText);
- }
- var subsystemText = pe.io.formatEnum(this.subsystem, Subsystem);
- if(subsystemText) {
- result.push(subsystemText);
- }
- var dllCharacteristicsText = pe.io.formatEnum(this.dllCharacteristics, DllCharacteristics);
- if(dllCharacteristicsText) {
- result.push(dllCharacteristicsText);
- }
- var nonzeroDataDirectoriesText = [];
- if(this.dataDirectories) {
- for(var i = 0; i < this.dataDirectories.length; i++) {
- if(!this.dataDirectories[i] || this.dataDirectories[i].size <= 0) {
- continue;
- }
- var kind = pe.io.formatEnum(i, DataDirectoryKind);
- nonzeroDataDirectoriesText.push(kind);
- }
- }
- result.push("dataDirectories[" + nonzeroDataDirectoriesText.join(",") + "]");
- var resultText = result.join(" ");
- return resultText;
- };
- OptionalHeader.prototype.read = function (reader) {
- this.peMagic = reader.readShort();
- if(this.peMagic != PEMagic.NT32 && this.peMagic != PEMagic.NT64) {
- throw Error("Unsupported PE magic value " + (this.peMagic).toString(16).toUpperCase() + "h.");
- }
- this.linkerVersion = reader.readByte() + "." + reader.readByte();
- this.sizeOfCode = reader.readInt();
- this.sizeOfInitializedData = reader.readInt();
- this.sizeOfUninitializedData = reader.readInt();
- this.addressOfEntryPoint = reader.readInt();
- this.baseOfCode = reader.readInt();
- if(this.peMagic == PEMagic.NT32) {
- this.baseOfData = reader.readInt();
- this.imageBase = reader.readInt();
- } else {
- this.imageBase = reader.readLong();
- }
- this.sectionAlignment = reader.readInt();
- this.fileAlignment = reader.readInt();
- this.operatingSystemVersion = reader.readShort() + "." + reader.readShort();
- this.imageVersion = reader.readShort() + "." + reader.readShort();
- this.subsystemVersion = reader.readShort() + "." + reader.readShort();
- this.win32VersionValue = reader.readInt();
- this.sizeOfImage = reader.readInt();
- this.sizeOfHeaders = reader.readInt();
- this.checkSum = reader.readInt();
- this.subsystem = reader.readShort();
- this.dllCharacteristics = reader.readShort();
- if(this.peMagic == PEMagic.NT32) {
- this.sizeOfStackReserve = reader.readInt();
- this.sizeOfStackCommit = reader.readInt();
- this.sizeOfHeapReserve = reader.readInt();
- this.sizeOfHeapCommit = reader.readInt();
- } else {
- this.sizeOfStackReserve = reader.readLong();
- this.sizeOfStackCommit = reader.readLong();
- this.sizeOfHeapReserve = reader.readLong();
- this.sizeOfHeapCommit = reader.readLong();
- }
- this.loaderFlags = reader.readInt();
- this.numberOfRvaAndSizes = reader.readInt();
- if(this.dataDirectories == null || this.dataDirectories.length != this.numberOfRvaAndSizes) {
- this.dataDirectories = (Array(this.numberOfRvaAndSizes));
- }
- for(var i = 0; i < this.numberOfRvaAndSizes; i++) {
- if(this.dataDirectories[i]) {
- this.dataDirectories[i].address = reader.readInt();
- this.dataDirectories[i].size = reader.readInt();
- } else {
- this.dataDirectories[i] = new pe.io.AddressRange(reader.readInt(), reader.readInt());
- }
- }
- };
- return OptionalHeader;
- })();
- headers.OptionalHeader = OptionalHeader;
- (function (PEMagic) {
- PEMagic._map = [];
- PEMagic.NT32 = 267;
- PEMagic.NT64 = 523;
- PEMagic.ROM = 263;
- })(headers.PEMagic || (headers.PEMagic = {}));
- var PEMagic = headers.PEMagic;
- (function (Subsystem) {
- Subsystem._map = [];
- Subsystem.Unknown = 0;
- Subsystem.Native = 1;
- Subsystem.WindowsGUI = 2;
- Subsystem.WindowsCUI = 3;
- Subsystem.OS2CUI = 5;
- Subsystem.POSIXCUI = 7;
- Subsystem.NativeWindows = 8;
- Subsystem.WindowsCEGUI = 9;
- Subsystem.EFIApplication = 10;
- Subsystem.EFIBootServiceDriver = 11;
- Subsystem.EFIRuntimeDriver = 12;
- Subsystem.EFIROM = 13;
- Subsystem.XBOX = 14;
- Subsystem.BootApplication = 16;
- })(headers.Subsystem || (headers.Subsystem = {}));
- var Subsystem = headers.Subsystem;
- (function (DllCharacteristics) {
- DllCharacteristics._map = [];
- DllCharacteristics.ProcessInit = 1;
- DllCharacteristics.ProcessTerm = 2;
- DllCharacteristics.ThreadInit = 4;
- DllCharacteristics.ThreadTerm = 8;
- DllCharacteristics.DynamicBase = 64;
- DllCharacteristics.ForceIntegrity = 128;
- DllCharacteristics.NxCompatible = 256;
- DllCharacteristics.NoIsolation = 512;
- DllCharacteristics.NoSEH = 1024;
- DllCharacteristics.NoBind = 2048;
- DllCharacteristics.AppContainer = 4096;
- DllCharacteristics.WdmDriver = 8192;
- DllCharacteristics.Reserved = 16384;
- DllCharacteristics.TerminalServerAware = 32768;
- })(headers.DllCharacteristics || (headers.DllCharacteristics = {}));
- var DllCharacteristics = headers.DllCharacteristics;
- (function (DataDirectoryKind) {
- DataDirectoryKind._map = [];
- DataDirectoryKind.ExportSymbols = 0;
- DataDirectoryKind.ImportSymbols = 1;
- DataDirectoryKind.Resources = 2;
- DataDirectoryKind.Exception = 3;
- DataDirectoryKind.Security = 4;
- DataDirectoryKind.BaseRelocation = 5;
- DataDirectoryKind.Debug = 6;
- DataDirectoryKind.CopyrightString = 7;
- DataDirectoryKind.Unknown = 8;
- DataDirectoryKind.ThreadLocalStorage = 9;
- DataDirectoryKind.LoadConfiguration = 10;
- DataDirectoryKind.BoundImport = 11;
- DataDirectoryKind.ImportAddressTable = 12;
- DataDirectoryKind.DelayImport = 13;
- DataDirectoryKind.Clr = 14;
- })(headers.DataDirectoryKind || (headers.DataDirectoryKind = {}));
- var DataDirectoryKind = headers.DataDirectoryKind;
- var SectionHeader = (function (_super) {
- __extends(SectionHeader, _super);
- function SectionHeader() {
- _super.call(this);
- this.name = "";
- this.pointerToRelocations = 0;
- this.pointerToLinenumbers = 0;
- this.numberOfRelocations = 0;
- this.numberOfLinenumbers = 0;
- this.characteristics = SectionCharacteristics.ContainsCode;
- }
- SectionHeader.prototype.toString = function () {
- var result = this.name + " " + _super.prototype.toString.call(this);
- return result;
- };
- SectionHeader.prototype.read = function (reader) {
- this.name = reader.readZeroFilledAscii(8);
- this.virtualSize = reader.readInt();
- this.virtualAddress = reader.readInt();
- var sizeOfRawData = reader.readInt();
- var pointerToRawData = reader.readInt();
- this.size = sizeOfRawData;
- this.address = pointerToRawData;
- this.pointerToRelocations = reader.readInt();
- this.pointerToLinenumbers = reader.readInt();
- this.numberOfRelocations = reader.readShort();
- this.numberOfLinenumbers = reader.readShort();
- this.characteristics = reader.readInt();
- };
- return SectionHeader;
- })(pe.io.AddressRangeMap);
- headers.SectionHeader = SectionHeader;
- (function (SectionCharacteristics) {
- SectionCharacteristics._map = [];
- SectionCharacteristics.Reserved_0h = 0;
- SectionCharacteristics.Reserved_1h = 1;
- SectionCharacteristics.Reserved_2h = 2;
- SectionCharacteristics.Reserved_4h = 4;
- SectionCharacteristics.NoPadding = 8;
- SectionCharacteristics.Reserved_10h = 16;
- SectionCharacteristics.ContainsCode = 32;
- SectionCharacteristics.ContainsInitializedData = 64;
- SectionCharacteristics.ContainsUninitializedData = 128;
- SectionCharacteristics.LinkerOther = 256;
- SectionCharacteristics.LinkerInfo = 512;
- SectionCharacteristics.Reserved_400h = 1024;
- SectionCharacteristics.LinkerRemove = 2048;
- SectionCharacteristics.LinkerCOMDAT = 4096;
- SectionCharacteristics.Reserved_2000h = 8192;
- SectionCharacteristics.NoDeferredSpeculativeExecution = 16384;
- SectionCharacteristics.GlobalPointerRelative = 32768;
- SectionCharacteristics.Reserved_10000h = 65536;
- SectionCharacteristics.MemoryPurgeable = 131072;
- SectionCharacteristics.MemoryLocked = 262144;
- SectionCharacteristics.MemoryPreload = 524288;
- SectionCharacteristics.Align1Bytes = 1048576;
- SectionCharacteristics.Align2Bytes = 2097152;
- SectionCharacteristics.Align4Bytes = 3145728;
- SectionCharacteristics.Align8Bytes = 4194304;
- SectionCharacteristics.Align16Bytes = 5242880;
- SectionCharacteristics.Align32Bytes = 6291456;
- SectionCharacteristics.Align64Bytes = 7340032;
- SectionCharacteristics.Align128Bytes = 8388608;
- SectionCharacteristics.Align256Bytes = 9437184;
- SectionCharacteristics.Align512Bytes = 10485760;
- SectionCharacteristics.Align1024Bytes = 11534336;
- SectionCharacteristics.Align2048Bytes = 12582912;
- SectionCharacteristics.Align4096Bytes = 13631488;
- SectionCharacteristics.Align8192Bytes = 14680064;
- SectionCharacteristics.LinkerRelocationOverflow = 16777216;
- SectionCharacteristics.MemoryDiscardable = 33554432;
- SectionCharacteristics.MemoryNotCached = 67108864;
- SectionCharacteristics.MemoryNotPaged = 134217728;
- SectionCharacteristics.MemoryShared = 268435456;
- SectionCharacteristics.MemoryExecute = 536870912;
- SectionCharacteristics.MemoryRead = 1073741824;
- SectionCharacteristics.MemoryWrite = 2147483648;
- })(headers.SectionCharacteristics || (headers.SectionCharacteristics = {}));
- var SectionCharacteristics = headers.SectionCharacteristics;
- })(pe.headers || (pe.headers = {}));
- var headers = pe.headers;
- })(pe || (pe = {}));
- var pe;
- (function (pe) {
- (function (unmanaged) {
- var DllExport = (function () {
- function DllExport() { }
- DllExport.readExports = function readExports(reader, range) {
- var result = {
- };
- result.flags = reader.readInt();
- if(!result.timestamp) {
- result.timestamp = new Date(0);
- }
- result.timestamp.setTime(reader.readInt() * 1000);
- var majorVersion = reader.readShort();
- var minorVersion = reader.readShort();
- result.version = majorVersion + "." + minorVersion;
- var nameRva = reader.readInt();
- result.ordinalBase = reader.readInt();
- var addressTableEntries = reader.readInt();
- var numberOfNamePointers = reader.readInt();
- var exportAddressTableRva = reader.readInt();
- var namePointerRva = reader.readInt();
- var ordinalTableRva = reader.readInt();
- if(nameRva == 0) {
- result.dllName = null;
- } else {
- var saveOffset = reader.offset;
- reader.setVirtualOffset(nameRva);
- result.dllName = reader.readAsciiZ();
- reader.offset = saveOffset;
- }
- var exports = [];
- for(var i = 0; i < addressTableEntries; i++) {
- var exportEntry = new DllExport();
- exportEntry.readExportEntry(reader, range);
- exportEntry.ordinal = i + this.ordinalBase;
- exports[i] = exportEntry;
- }
- if(numberOfNamePointers != 0 && namePointerRva != 0 && ordinalTableRva != 0) {
- saveOffset = reader.offset;
- for(var i = 0; i < numberOfNamePointers; i++) {
- reader.setVirtualOffset(ordinalTableRva + 2 * i);
- var ordinal = reader.readShort();
- reader.setVirtualOffset(namePointerRva + 4 * i);
- var functionNameRva = reader.readInt();
- var functionName;
- if(functionNameRva == 0) {
- functionName = null;
- } else {
- reader.setVirtualOffset(functionNameRva);
- functionName = reader.readAsciiZ();
- }
- exports[ordinal].name = functionName;
- }
- reader.offset = saveOffset;
- }
- result.exports = exports;
- return result;
- };
- DllExport.prototype.readExportEntry = function (reader, range) {
- var exportOrForwarderRva = reader.readInt();
- if(range.mapRelative(exportOrForwarderRva) >= 0) {
- this.exportRva = 0;
- var forwarderRva = reader.readInt();
- if(forwarderRva == 0) {
- this.forwarder = null;
- } else {
- var saveOffset = reader.offset;
- reader.setVirtualOffset(forwarderRva);
- this.forwarder = reader.readAsciiZ();
- reader.offset = saveOffset;
- }
- } else {
- this.exportRva = reader.readInt();
- this.forwarder = null;
- }
- this.name = null;
- };
- return DllExport;
- })();
- unmanaged.DllExport = DllExport;
- var DllImport = (function () {
- function DllImport() {
- this.name = "";
- this.ordinal = 0;
- this.dllName = "";
- this.timeDateStamp = new Date(0);
- }
- DllImport.read = function read(reader, result) {
- if(!result) {
- result = [];
- }
- var readLength = 0;
- while(true) {
- var originalFirstThunk = reader.readInt();
- var timeDateStamp = new Date(0);
- timeDateStamp.setTime(reader.readInt());
- var forwarderChain = reader.readInt();
- var nameRva = reader.readInt();…