public void Write(Stream output) { BinaryWriter writer = new BinaryWriter(output, Encoding.Default, true); writer.Write(StringOffset); writer.WriteZeros(4); writer.Write(StringLength); writer.WriteZeros(4); }
public void Write(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true); writer.Write(Encoding.Default.GetBytes(Signature)); writer.Write(LittleEndianMagicNumber); writer.Write((byte) Entries.Count); writer.WriteZeros(1); writer.Write(FfntHeaderSize); writer.AlignWrite(16, 0x00); long entryHeaderPosition = outputStream.Position; outputStream.Position += Entries.Count*FfntEntryHeader.FfntEntryHeaderSize; writer.AlignWrite(16, 0x00); List<FfntEntryHeader> ffntEntryHeaders = new List<FfntEntryHeader>(); foreach (var entry in Entries) { ffntEntryHeaders.Add(entry.GetHeader(outputStream)); entry.Write(outputStream); writer.AlignWrite(16, 0x00); } outputStream.Position = entryHeaderPosition; foreach (var header in ffntEntryHeaders) { header.Write(outputStream); } }
static void WriteAlign(BinaryWriter writer, ref uint offs) { uint align = Utils.AlignUp(offs, HH_ALIGNMENT) - offs; offs += align; writer.WriteZeros((int)align); }
public void Serialize(uint token, uint key, byte[] fieldLayout) { using (var ms = new MemoryStream()) { var writer = new BinaryWriter(ms); foreach (byte i in fieldLayout) switch (i) { case 0: writer.Write((uint)ILCode.Length); break; case 1: writer.Write(MaxStack); break; case 2: writer.Write((uint)EHs.Length); break; case 3: writer.Write((uint)LocalVars.Length); break; case 4: writer.Write(Options); break; case 5: writer.Write(MulSeed); break; } writer.Write(ILCode); writer.Write(LocalVars); foreach (JITEHClause clause in EHs) { writer.Write(clause.Flags); writer.Write(clause.TryOffset); writer.Write(clause.TryLength); writer.Write(clause.HandlerOffset); writer.Write(clause.HandlerLength); writer.Write(clause.ClassTokenOrFilterOffset); } writer.WriteZeros(4 - ((int)ms.Length & 3)); // pad to 4 bytes Body = ms.ToArray(); } Debug.Assert(Body.Length % 4 == 0); // encrypt body uint state = token * key; uint counter = state; for (uint i = 0; i < Body.Length; i += 4) { uint data = Body[i] | (uint)(Body[i + 1] << 8) | (uint)(Body[i + 2] << 16) | (uint)(Body[i + 3] << 24); Body[i + 0] ^= (byte)(state >> 0); Body[i + 1] ^= (byte)(state >> 8); Body[i + 2] ^= (byte)(state >> 16); Body[i + 3] ^= (byte)(state >> 24); state += data ^ counter; counter ^= (state >> 5) | (state << 27); } }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { RVA rva2 = rva; foreach (var resourceData in resources) { writer.Write(resourceData.GetFileLength()); resourceData.VerifyWriteTo(writer); rva2 += 4 + resourceData.GetFileLength(); int padding = (int)rva2.AlignUp(alignment) - (int)rva2; writer.WriteZeros(padding); rva2 += (uint)padding; } }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { WriteToImpl(writer); writer.WriteZeros((int)(Utils.AlignUp(GetRawLength(), ALIGNMENT) - GetRawLength())); }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { writer.Write(code); if (HasExtraSections) { RVA rva2 = rva + (uint)code.Length; writer.WriteZeros((int)rva2.AlignUp(EXTRA_SECTIONS_ALIGNMENT) - (int)rva2); writer.Write(extraSections); } }
public void Write(Stream output) { BinaryWriter writer = new BinaryWriter(output, Encoding.Default, true); writer.Write(DataOffset); writer.WriteZeros(4); writer.Write(DataSize); writer.WriteZeros(4); FilePathFpkString.Write(output); writer.Write(Md5Hash); }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { uint offset = 0; // The order here must be the same as in SetOffset() foreach (var dir in dirList) { uint padding = Utils.AlignUp(offset, RESOURCE_DIR_ALIGNMENT) - offset; writer.WriteZeros((int)padding); offset += padding; if (dirDict[dir] != offset) throw new ModuleWriterException("Invalid Win32 resource directory offset"); offset += WriteTo(writer, dir); } foreach (var dataHeader in dataHeaderList) { uint padding = Utils.AlignUp(offset, RESOURCE_DATA_HEADER_ALIGNMENT) - offset; writer.WriteZeros((int)padding); offset += padding; if (dataHeaderDict[dataHeader] != offset) throw new ModuleWriterException("Invalid Win32 resource data header offset"); offset += WriteTo(writer, dataHeader); } foreach (var s in stringsList) { uint padding = Utils.AlignUp(offset, RESOURCE_STRING_ALIGNMENT) - offset; writer.WriteZeros((int)padding); offset += padding; if (stringsDict[s] != offset) throw new ModuleWriterException("Invalid Win32 resource string offset"); var bytes = Encoding.Unicode.GetBytes(s); if (bytes.Length / 2 > ushort.MaxValue) throw new ModuleWriterException("Win32 resource entry name is too long"); writer.Write((ushort)(bytes.Length / 2)); writer.Write(bytes); offset += 2 + (uint)bytes.Length; } byte[] dataBuffer = new byte[0x2000]; foreach (var data in dataList) { uint padding = Utils.AlignUp(offset, RESOURCE_DATA_ALIGNMENT) - offset; writer.WriteZeros((int)padding); offset += padding; if (dataDict[data] != offset) throw new ModuleWriterException("Invalid Win32 resource data offset"); data.Position = 0; offset += data.WriteTo(writer, dataBuffer); } writer.WriteZeros((int)(Utils.AlignUp(length, ModuleWriterBase.DEFAULT_WIN32_RESOURCES_ALIGNMENT) - length)); }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { writer.Write(options.Signature ?? MetaDataHeaderOptions.DEFAULT_SIGNATURE); writer.Write(options.MajorVersion ?? 1); writer.Write(options.MinorVersion ?? 1); writer.Write(options.Reserved1 ?? 0); var s = GetVersionString(); writer.Write(Utils.AlignUp(s.Length, 4)); writer.Write(s); writer.WriteZeros(Utils.AlignUp(s.Length, 4) - s.Length); writer.Write((byte)(options.StorageFlags ?? 0)); writer.Write(options.Reserved2 ?? 0); writer.Write((ushort)heaps.Count); foreach (var heap in heaps) { writer.Write((uint)(heap.FileOffset - offset)); writer.Write(heap.GetFileLength()); writer.Write(s = GetAsciizName(heap.Name)); if (s.Length > 32) throw new ModuleWriterException(string.Format("Heap name '{0}' is > 32 bytes", heap.Name)); writer.WriteZeros(Utils.AlignUp(s.Length, 4) - s.Length); } }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { writer.Write((uint)importLookupTableRVA); writer.Write(0); // DateTimeStamp writer.Write(0); // ForwarderChain writer.Write((uint)mscoreeDllRVA); // Name writer.Write((uint)ImportAddressTable.RVA); writer.Write(0UL); writer.Write(0UL); writer.Write(0); // ImportLookupTable writer.Write((uint)corXxxMainRVA); writer.Write(0); writer.WriteZeros(stringsPadding); writer.Write((ushort)0); writer.Write(Encoding.UTF8.GetBytes(IsExeFile ? "_CorExeMain\0" : "_CorDllMain\0")); writer.Write(Encoding.UTF8.GetBytes("mscoree.dll\0")); writer.Write((byte)0); }
/// <summary> /// Writes the full table to the hot table /// </summary> /// <param name="writer">Writer</param> void FullWriteTo(BinaryWriter writer) { CreateFullData(); var startPos = writer.BaseStream.Position; writer.Write(mdTable.Rows); // hot records writer.Write(0); // offset of 1st level table writer.Write(0); // offset of 2nd level table if (version == HotHeapVersion.CLR40) writer.Write(0); // offset of indexes table writer.Write(alignedHotTableHeaderSize); // offset of hot data (4-byte aligned) writer.Write((ushort)0); // shift count writer.WriteZeros(alignedHotTableHeaderSize - (int)(writer.BaseStream.Position - startPos)); writer.Write(data); }
/// <inheritdoc/> internal override void PartialWriteTo(BinaryWriter writer) { var startPos = writer.BaseStream.Position; writer.Write(partialData.Count);// hot records writer.Write(firstLevelOffset); // any alignment, all bits are used writer.Write(secondLevelOffset);// any alignment, all bits are used writer.Write(indexesOffset);// any alignment, all bits are used writer.Write(dataOffset); // any alignment, all bits are used writer.Write((ushort)shift);// shift count writer.WriteZeros(alignedHotTableHeaderSize - (int)(writer.BaseStream.Position - startPos)); uint offs; // Data offs = (uint)(writer.BaseStream.Position - startPos); writer.WriteZeros((int)(dataOffset - offs)); WritePartialData(writer); // First level table offs = (uint)(writer.BaseStream.Position - startPos); writer.WriteZeros((int)(firstLevelOffset - offs)); WriteFirstLevelTable(writer); // Second level table offs = (uint)(writer.BaseStream.Position - startPos); writer.WriteZeros((int)(secondLevelOffset - offs)); WriteSecondLevelTable(writer); // Indexes table offs = (uint)(writer.BaseStream.Position - startPos); writer.WriteZeros((int)(indexesOffset - offs)); WriteIndexesTable(writer); }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { writer.WriteZeros((int)padding); writer.Write((byte)0xFF); writer.Write((byte)0x25); writer.Write((uint)PEHeaders.ImageBase + (uint)ImportDirectory.IatCorXxxMainRVA); }
public override void Write(Stream output, IDirectory inputDirectory) { BinaryWriter writer = new BinaryWriter(output, Encoding.Default, true); const int headerSize = 48; int indicesSize = 48*Entries.Count; int referenceSize = 16*References.Count; long startPosition = output.Position; output.Position += headerSize + indicesSize + referenceSize; foreach (var fpkEntry in Entries) { fpkEntry.WriteFilePath(output); } foreach (var fpkReference in References) { fpkReference.WriteFilePath(output); } output.AlignWrite(16, 0x00); foreach (var fpkEntry in Entries) { fpkEntry.WriteData(output, inputDirectory); output.AlignWrite(16, 0x00); } uint fileSize = (uint) output.Position; output.Position = startPosition; writer.Write(0x66786f66); // foxf writer.Write((ushort) 0x6B70); //pk writer.Write((byte) FpkType); writer.Write((byte) 0x77); // w writer.Write((ushort) 0x6E69); // in writer.Write(fileSize); writer.WriteZeros(18); writer.Write(0x00000002); writer.Write(Entries.Count); writer.Write(References.Count); writer.WriteZeros(4); foreach (var fpkEntry in Entries) { fpkEntry.Write(output); } foreach (var fpkReference in References) { fpkReference.Write(output); } }
public void Write(Stream output) { BinaryWriter writer = new BinaryWriter(output, Encoding.Default, true); long headerPosition = output.Position; output.Position += HeaderSize; foreach (var staticProperty in StaticProperties) { staticProperty.Write(output); } uint staticDataSize = (uint) (output.Position - headerPosition); foreach (var dynamicProperty in DynamicProperties) { dynamicProperty.Write(output); } long endPosition = output.Position; uint dataSize = (uint) (endPosition - headerPosition); output.Position = headerPosition; writer.Write(HeaderSize); writer.Write(Unknown1); writer.WriteZeros(2); // padding1 writer.Write(MagicNumber); writer.Write(Address); writer.WriteZeros(4); // padding2 writer.Write(Unknown2); writer.WriteZeros(4); writer.Write(Version); writer.Write(ClassNameHash); writer.Write(Convert.ToUInt16(StaticProperties.Count())); writer.Write(Convert.ToUInt16(DynamicProperties.Count())); writer.Write((int) HeaderSize); writer.Write(staticDataSize); writer.Write(dataSize); output.AlignWrite(16, 0x00); //writer.WriteZeros(12); output.Position = endPosition; }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { writer.Write(options.Reserved1 ?? 0); writer.Write(majorVersion); writer.Write(minorVersion); writer.Write((byte)GetMDStreamFlags()); writer.Write(GetLog2Rid()); writer.Write(GetValidMask()); writer.Write(GetSortedMask()); foreach (var mdt in Tables) { if (!mdt.IsEmpty) writer.Write(mdt.Rows); } if (options.ExtraData.HasValue) writer.Write(options.ExtraData.Value); writer.Write(ModuleTable); writer.Write(TypeRefTable); writer.Write(TypeDefTable); writer.Write(FieldPtrTable); writer.Write(FieldTable); writer.Write(MethodPtrTable); writer.Write(MethodTable); writer.Write(ParamPtrTable); writer.Write(ParamTable); writer.Write(InterfaceImplTable); writer.Write(MemberRefTable); writer.Write(ConstantTable); writer.Write(CustomAttributeTable); writer.Write(FieldMarshalTable); writer.Write(DeclSecurityTable); writer.Write(ClassLayoutTable); writer.Write(FieldLayoutTable); writer.Write(StandAloneSigTable); writer.Write(EventMapTable); writer.Write(EventPtrTable); writer.Write(EventTable); writer.Write(PropertyMapTable); writer.Write(PropertyPtrTable); writer.Write(PropertyTable); writer.Write(MethodSemanticsTable); writer.Write(MethodImplTable); writer.Write(ModuleRefTable); writer.Write(TypeSpecTable); writer.Write(ImplMapTable); writer.Write(FieldRVATable); writer.Write(ENCLogTable); writer.Write(ENCMapTable); writer.Write(AssemblyTable); writer.Write(AssemblyProcessorTable); writer.Write(AssemblyOSTable); writer.Write(AssemblyRefTable); writer.Write(AssemblyRefProcessorTable); writer.Write(AssemblyRefOSTable); writer.Write(FileTable); writer.Write(ExportedTypeTable); writer.Write(ManifestResourceTable); writer.Write(NestedClassTable); writer.Write(GenericParamTable); writer.Write(MethodSpecTable); writer.Write(GenericParamConstraintTable); writer.WriteZeros((int)(Utils.AlignUp(length, HeapBase.ALIGNMENT) - length)); }
public void Write(Stream output) { BinaryWriter writer = new BinaryWriter(output, Encoding.Default, true); long headerPosition = output.Position; output.Position += HeaderSize; foreach (var foxEntity in Entities) { foxEntity.Write(output); } int offsetHashMap = (int) output.Position; foreach (var foxStringLookupLiteral in StringLookupLiterals) { // TODO: Write the encrypted file name. if (string.IsNullOrEmpty(foxStringLookupLiteral.Literal) == false) foxStringLookupLiteral.Write(output); } output.AlignWrite(16, 0x00); writer.Write(new byte[] {0x00, 0x00, 0x65, 0x6E, 0x64}); output.AlignWrite(16, 0x00); long endPosition = output.Position; output.Position = headerPosition; int entityCount = Entities.Count(); writer.Write(MagicNumber1); writer.Write(MagicNumber2); writer.Write(entityCount); writer.Write(offsetHashMap); writer.Write(HeaderSize); writer.WriteZeros(12); output.Position = endPosition; }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { var rva2 = rva; foreach (var mb in tinyMethods) { mb.VerifyWriteTo(writer); rva2 += mb.GetFileLength(); } foreach (var mb in fatMethods) { if (alignFatBodies) { int padding = (int)rva2.AlignUp(FAT_BODY_ALIGNMENT) - (int)rva2; writer.WriteZeros(padding); rva2 += (uint)padding; } mb.VerifyWriteTo(writer); rva2 += mb.GetFileLength(); } }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { writer.WriteZeros(size); }
/// <summary> /// Writes all chunks to <paramref name="writer"/> /// </summary> /// <param name="writer">The writer</param> /// <param name="chunks">All chunks</param> /// <param name="offset">File offset of first chunk</param> /// <param name="fileAlignment">File alignment</param> protected void WriteChunks(BinaryWriter writer, List<IChunk> chunks, FileOffset offset, uint fileAlignment) { foreach (var chunk in chunks) { chunk.VerifyWriteTo(writer); offset += chunk.GetFileLength(); var newOffset = offset.AlignUp(fileAlignment); writer.WriteZeros((int)(newOffset - offset)); offset = newOffset; } }
public void Write(Stream outputStream) { BinaryWriter writer = new BinaryWriter(outputStream, Encoding.Default, true); writer.Write(Size); writer.Write(Convert.ToInt32(Flags)); writer.Write(Height); writer.Write(Width); writer.Write(PitchOrLinearSize); writer.Write(Depth); writer.Write(MipMapCount); // int Reserved1[11]; writer.WriteZeros(44); PixelFormat.Write(outputStream); writer.Write(Convert.ToInt32(Caps)); writer.Write(Convert.ToInt32(Caps2)); writer.Write(Caps3); writer.Write(Caps4); // int Reserved2 writer.WriteZeros(4); }