4
4
using System ;
5
5
using System . Buffers . Binary ;
6
6
using System . Collections . Generic ;
7
+ using System . Numerics ;
7
8
8
9
namespace Microsoft . Diagnostics . DataContractReader ;
9
10
@@ -166,19 +167,7 @@ public byte ReadUInt8(ulong address)
166
167
}
167
168
168
169
public bool TryReadUInt8 ( ulong address , out byte value )
169
- => TryReadUInt8 ( address , _reader , out value ) ;
170
-
171
- private static bool TryReadUInt8 ( ulong address , Reader reader , out byte value )
172
- {
173
- value = 0 ;
174
- fixed ( byte * ptr = & value )
175
- {
176
- if ( reader . ReadFromTarget ( address , ptr , 1 ) < 0 )
177
- return false ;
178
- }
179
-
180
- return true ;
181
- }
170
+ => TryRead ( address , isUnsigned : true , out value ) ;
182
171
183
172
public uint ReadUInt32 ( ulong address )
184
173
{
@@ -189,21 +178,38 @@ public uint ReadUInt32(ulong address)
189
178
}
190
179
191
180
public bool TryReadUInt32 ( ulong address , out uint value )
192
- => TryReadUInt32 ( address , _config . IsLittleEndian , _reader , out value ) ;
181
+ => TryRead ( address , isUnsigned : true , out value ) ;
193
182
194
183
private static bool TryReadUInt32 ( ulong address , bool isLittleEndian , Reader reader , out uint value )
184
+ => TryRead ( address , isLittleEndian , isUnsigned : true , reader , out value ) ;
185
+
186
+ public ulong ReadUInt64 ( ulong address )
195
187
{
196
- value = 0 ;
188
+ if ( ! TryReadUInt64 ( address , out ulong value ) )
189
+ throw new InvalidOperationException ( $ "Failed to read uint32 at 0x{ address : x8} .") ;
197
190
198
- Span < byte > buffer = stackalloc byte [ sizeof ( uint ) ] ;
191
+ return value ;
192
+ }
193
+
194
+ public bool TryReadUInt64 ( ulong address , out ulong value )
195
+ => TryRead ( address , isUnsigned : true , out value ) ;
196
+
197
+ private static bool TryReadUInt64 ( ulong address , bool isLittleEndian , Reader reader , out ulong value )
198
+ => TryRead ( address , isLittleEndian , isUnsigned : true , reader , out value ) ;
199
+
200
+ private bool TryRead < T > ( ulong address , bool isUnsigned , out T value ) where T : unmanaged, IBinaryInteger < T >
201
+ => TryRead ( address , _config . IsLittleEndian , isUnsigned , _reader , out value ) ;
202
+
203
+ private static bool TryRead < T > ( ulong address , bool isLittleEndian , bool isUnsigned , Reader reader , out T value ) where T : unmanaged, IBinaryInteger < T >
204
+ {
205
+ value = default ;
206
+ Span < byte > buffer = stackalloc byte [ sizeof ( T ) ] ;
199
207
if ( reader . ReadFromTarget ( address , buffer ) < 0 )
200
208
return false ;
201
209
202
- value = isLittleEndian
203
- ? BinaryPrimitives . ReadUInt32LittleEndian ( buffer )
204
- : BinaryPrimitives . ReadUInt32BigEndian ( buffer ) ;
205
-
206
- return true ;
210
+ return isLittleEndian
211
+ ? T . TryReadLittleEndian ( buffer , isUnsigned , out value )
212
+ : T . TryReadBigEndian ( buffer , isUnsigned , out value ) ;
207
213
}
208
214
209
215
public TargetPointer ReadPointer ( ulong address )
@@ -225,22 +231,20 @@ private static bool TryReadPointer(ulong address, Configuration config, Reader r
225
231
if ( reader . ReadFromTarget ( address , buffer ) < 0 )
226
232
return false ;
227
233
228
- if ( config . PointerSize == sizeof ( uint ) )
234
+ if ( config . PointerSize == sizeof ( uint )
235
+ && TryReadUInt32 ( address , config . IsLittleEndian , reader , out uint value32 ) )
229
236
{
230
- pointer = new TargetPointer (
231
- config . IsLittleEndian
232
- ? BinaryPrimitives . ReadUInt32LittleEndian ( buffer )
233
- : BinaryPrimitives . ReadUInt32BigEndian ( buffer ) ) ;
237
+ pointer = new TargetPointer ( value32 ) ;
238
+ return true ;
234
239
}
235
- else if ( config . PointerSize == sizeof ( ulong ) )
240
+ else if ( config . PointerSize == sizeof ( ulong )
241
+ && TryReadUInt64 ( address , config . IsLittleEndian , reader , out ulong value64 ) )
236
242
{
237
- pointer = new TargetPointer (
238
- config . IsLittleEndian
239
- ? BinaryPrimitives . ReadUInt64LittleEndian ( buffer )
240
- : BinaryPrimitives . ReadUInt64BigEndian ( buffer ) ) ;
243
+ pointer = new TargetPointer ( value64 ) ;
244
+ return true ;
241
245
}
242
246
243
- return true ;
247
+ return false ;
244
248
}
245
249
246
250
public byte ReadGlobalUInt8 ( string name )
0 commit comments