55using System ;
66using System . Buffers ;
77using System . Diagnostics ;
8+ using System . Runtime . CompilerServices ;
89using System . Runtime . InteropServices ;
910using System . Text ;
1011using LibObjectFile . Collections ;
@@ -18,6 +19,8 @@ namespace LibObjectFile.PE;
1819[ DebuggerDisplay ( "{ToString(),nq}" ) ]
1920public sealed class PEDebugSectionDataRSDS : PEDebugSectionData
2021{
22+ private const uint Signature = 0x53445352 ;
23+
2124 /// <summary>
2225 /// Initializes a new instance of the <see cref="PEDebugSectionDataRSDS"/> class.
2326 /// </summary>
@@ -57,7 +60,7 @@ public override unsafe void Read(PEImageReader reader)
5760 }
5861
5962 var signature = MemoryMarshal . Read < uint > ( span ) ;
60- if ( signature != 0x53445352 )
63+ if ( signature != Signature )
6164 {
6265 reader . Diagnostics . Error ( DiagnosticId . PE_ERR_InvalidDebugDataRSDSSignature , $ "Invalid signature for PEDebugDataRSDS") ;
6366 return ;
@@ -74,8 +77,36 @@ public override unsafe void Read(PEImageReader reader)
7477 Guid = MemoryMarshal . Read < Guid > ( span . Slice ( 4 ) ) ;
7578 Age = MemoryMarshal . Read < uint > ( span . Slice ( sizeof ( uint ) + sizeof ( Guid ) ) ) ;
7679 PdbPath = System . Text . Encoding . UTF8 . GetString ( pdbPath . Slice ( 0 , indexOfZero ) ) ;
80+
81+ Debug . Assert ( size == CalculateSize ( ) ) ;
7782 }
7883
84+ public override unsafe void Write ( PEImageWriter writer )
85+ {
86+ var size = ( int ) Size ;
87+ using var tempSpan = TempSpan < byte > . Create ( size , out var span ) ;
88+
89+ MemoryMarshal . Write ( span , Signature ) ;
90+ span = span . Slice ( sizeof ( uint ) ) ;
91+ MemoryMarshal . Write ( span , Guid ) ;
92+ span = span . Slice ( sizeof ( Guid ) ) ;
93+ MemoryMarshal . Write ( span , Age ) ;
94+ span = span . Slice ( sizeof ( uint ) ) ;
95+ int written = Encoding . UTF8 . GetBytes ( PdbPath , span ) ;
96+ span [ written ] = 0 ;
97+ span . Slice ( written + 1 ) ;
98+
99+ writer . Write ( tempSpan . AsBytes ) ;
100+ }
101+
102+ public override void UpdateLayout ( PELayoutContext layoutContext )
103+ {
104+ Size = CalculateSize ( ) ;
105+ }
106+
107+ private unsafe uint CalculateSize ( )
108+ => ( uint ) ( sizeof ( uint ) + sizeof ( Guid ) + sizeof ( uint ) + Encoding . UTF8 . GetByteCount ( PdbPath ) + 1 ) ;
109+
79110 /// <inheritdoc />
80111 protected override bool PrintMembers ( StringBuilder builder )
81112 {
0 commit comments