private const int MaxSectionSizeBuffer = 10; // the maximum size of 2 MC values public void Write(BitWriter writer) { void WriteSection(byte[] data) { writer.StartCrcCalculation(initialValue: DwgHeaderVariables.InitialCrcValue); writer.WriteShortBigEndian((short)(data.Length + 2)); // account for crc writer.WriteBytes(data); writer.WriteCrc(writeCrcAsMsb: true); } // write in sections, each section is at most 2032 bytes, stop writing if remaining space is less than 10 var sectionStart = writer.Position; var lastHandle = 0; var lastLocation = 0; var ms = new MemoryStream(); var sectionWriter = new BitWriter(ms); foreach (var kvp in _handleOffsets.OrderBy(k => k.Key)) { var sectionSize = writer.Position - sectionStart; if (sectionSize >= MaxSectionSize - MaxSectionSizeBuffer) { // flush section var sectionBytes = sectionWriter.AsBytes(); WriteSection(sectionBytes); ms.Dispose(); ms = new MemoryStream(); sectionWriter = new BitWriter(ms); sectionStart = writer.Position; } // now write the values var handleDiff = kvp.Key - lastHandle; var locationDiff = kvp.Value - lastLocation; sectionWriter.Write_MC(handleDiff); sectionWriter.Write_MC(locationDiff); lastHandle = kvp.Key; lastLocation = kvp.Value; } // flush the final section var finalBytes = sectionWriter.AsBytes(); if (finalBytes.Length > 0) { WriteSection(finalBytes); } ms.Dispose(); // always finish with an empty section WriteSection(new byte[0]); }
internal void Write(BitWriter writer, DwgVersionId version) { writer.AlignByte(); using (var ms = new MemoryStream()) { // write the variables to memory var variableWriter = new BitWriter(ms); WriteVariables(variableWriter, version); var variableBytes = variableWriter.AsBytes(); // now write it all out writer.WriteBytes(StartSentinel); writer.StartCrcCalculation(initialValue: InitialCrcValue); writer.Write_RL(variableBytes.Length); writer.WriteBytes(variableBytes); writer.WriteCrc(); writer.WriteBytes(EndSentinel); } }
internal static void Write(IList <DwgClassDefinition> classes, BitWriter writer) { writer.AlignByte(); using (var ms = new MemoryStream()) { // write the classes to memory var classWriter = new BitWriter(ms); foreach (var c in classes) { c.Write(classWriter); } var classBytes = classWriter.AsBytes(); // now write it all out writer.WriteBytes(StartSentinel); writer.StartCrcCalculation(initialValue: DwgHeaderVariables.InitialCrcValue); writer.Write_RL(classBytes.Length); writer.WriteBytes(classBytes); writer.WriteCrc(); writer.WriteBytes(EndSentinel); } }
internal void WriteSecondHeader(BitWriter writer, DwgHeaderVariables headerVariables, int pointer) { // write to memory the backtrack to fill in size and crc using (var ms = new MemoryStream()) { var tempWriter = new BitWriter(ms); tempWriter.WriteBytes(SecondHeaderStartSentinel); var sizeOffset = tempWriter.Position; tempWriter.Write_RL(0); // size, filled in later tempWriter.Write_BL(pointer); tempWriter.WriteStringAscii(Version.VersionString(), nullTerminated: false); tempWriter.WriteBytes(new byte[] { 0, 0, 0, 0, 0, 0 }); // 6 zero bytes tempWriter.WriteBits(0x00000000, 4); // 4 zero bits tempWriter.WriteBytes(new byte[] { 0, 0 }); // 2 unknown bytes tempWriter.WriteBytes(SecondHeaderMidSentinel); tempWriter.WriteByte(5); // record locator count HeaderVariablesLocator.Write(tempWriter, writingSecondHeader: true); ClassSectionLocator.Write(tempWriter, writingSecondHeader: true); ObjectMapLocator.Write(tempWriter, writingSecondHeader: true); UnknownSection_R13C3AndLaterLocator.Write(tempWriter, writingSecondHeader: true); UnknownSection_PaddingLocator.Write(tempWriter, writingSecondHeader: true); tempWriter.Write_BS(14); headerVariables.NextAvailableHandle.WriteSecondHeader(tempWriter, 0); headerVariables.BlockControlObjectHandle.WriteSecondHeader(tempWriter, 1); headerVariables.LayerControlObjectHandle.WriteSecondHeader(tempWriter, 2); headerVariables.StyleObjectControlHandle.WriteSecondHeader(tempWriter, 3); headerVariables.LineTypeObjectControlHandle.WriteSecondHeader(tempWriter, 4); headerVariables.ViewControlObjectHandle.WriteSecondHeader(tempWriter, 5); headerVariables.UcsControlObjectHandle.WriteSecondHeader(tempWriter, 6); headerVariables.ViewPortControlObjectHandle.WriteSecondHeader(tempWriter, 7); headerVariables.AppIdControlObjectHandle.WriteSecondHeader(tempWriter, 8); headerVariables.DimStyleControlObjectHandle.WriteSecondHeader(tempWriter, 9); headerVariables.ViewPortEntityHeaderControlObjectHandle.WriteSecondHeader(tempWriter, 10); headerVariables.NamedObjectsDictionaryHandle.WriteSecondHeader(tempWriter, 11); headerVariables.MLineStyleDictionaryHandle.WriteSecondHeader(tempWriter, 12); headerVariables.GroupDictionaryHandle.WriteSecondHeader(tempWriter, 13); tempWriter.WriteByte(0); // unknown tempWriter.AlignByte(); var crcOffset = tempWriter.Position; tempWriter.WriteShort(0); // CRC, filled in later if (Version == DwgVersionId.R14) { // unknown garbage bytes tempWriter.WriteBytes(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }); } tempWriter.WriteBytes(SecondHeaderEndSentinel); // get bytes of header var secondHeaderBytes = tempWriter.AsBytes(); // fill in size var sizeBytes = BitConverter.GetBytes(secondHeaderBytes.Length - SecondHeaderStartSentinel.Length - SecondHeaderEndSentinel.Length); if (!BitConverter.IsLittleEndian) { Array.Reverse(sizeBytes); } Array.Copy(sizeBytes, 0, secondHeaderBytes, sizeOffset, sizeBytes.Length); // fill in crc var crc = BitReaderExtensions.ComputeCRC(secondHeaderBytes, sizeOffset, crcOffset - sizeOffset, DwgHeaderVariables.InitialCrcValue); var crcBytes = BitConverter.GetBytes(crc); if (!BitConverter.IsLittleEndian) { Array.Reverse(crcBytes); } Array.Copy(crcBytes, 0, secondHeaderBytes, crcOffset, crcBytes.Length); // write to the real writer writer.WriteBytes(secondHeaderBytes); } }