Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Replace SkipBytes and SeekOffset with Offset setter (#12375)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Oct 5, 2016
1 parent 136e456 commit ae0e558
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public unsafe struct BlobReader
/// <summary>An array containing the '\0' character.</summary>
private static readonly char[] s_nullCharArray = new char[1] { '\0' };

internal const int InvalidCompressedInteger = Int32.MaxValue;
internal const int InvalidCompressedInteger = int.MaxValue;

private readonly MemoryBlock _block;

Expand Down Expand Up @@ -90,9 +90,25 @@ internal string GetDebuggerDisplay()
public int Length => _block.Length;

/// <summary>
/// Offset from start of underlying memory block to current position.
/// Gets or sets the offset from start of the blob to the current position.
/// </summary>
public int Offset => (int)(_currentPointer - _block.Pointer);
/// <exception cref="BadImageFormatException">Offset is set outside the bounds of underlying reader.</exception>
public int Offset
{
get
{
return (int)(_currentPointer - _block.Pointer);
}
set
{
if (unchecked((uint)value) > (uint)_block.Length)
{
Throw.OutOfBounds();
}

_currentPointer = _block.Pointer + value;
}
}

/// <summary>
/// Bytes remaining from current position to end of underlying memory block.
Expand All @@ -107,37 +123,6 @@ public void Reset()
_currentPointer = _block.Pointer;
}

/// <summary>
/// Repositions the reader to the given offset from the start of the underlying memory block.
/// </summary>
/// <exception cref="BadImageFormatException">Offset is outside the bounds of underlying reader.</exception>
public void SeekOffset(int offset)
{
if (!TrySeekOffset(offset))
{
Throw.OutOfBounds();
}
}

internal bool TrySeekOffset(int offset)
{
if (unchecked((uint)offset) >= (uint)_block.Length)
{
return false;
}

_currentPointer = _block.Pointer + offset;
return true;
}

/// <summary>
/// Repositions the reader forward by the given number of bytes.
/// </summary>
public void SkipBytes(int count)
{
GetCurrentPointerAndAdvance(count);
}

/// <summary>
/// Repositions the reader forward by the number of bytes required to satisfy the given alignment.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static MethodBodyBlock Create(BlobReader reader)
}

var ilBlock = reader.GetMemoryBlockAt(0, ilSize);
reader.SkipBytes(ilSize);
reader.Offset += ilSize;

ImmutableArray<ExceptionRegion> exceptionHandlers;
if (hasExceptionHandlers)
Expand All @@ -174,7 +174,7 @@ public static MethodBodyBlock Create(BlobReader reader)
}
else
{
reader.SkipBytes(2); // skip over reserved field
reader.Offset += 2; // skip over reserved field
exceptionHandlers = ReadSmallExceptionHandlers(ref reader, dataSize / 12);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public unsafe MetadataReader(byte* metadata, int length, MetadataReaderOptions o
/// <exception cref="ArgumentNullException"><paramref name="metadata"/> is null.</exception>
/// <exception cref="ArgumentException">The encoding of <paramref name="utf8Decoder"/> is not <see cref="UTF8Encoding"/>.</exception>
/// <exception cref="PlatformNotSupportedException">The current platform is big-endian.</exception>
/// <exception cref="BadImageFormatException">Bad metadata header.</exception>
public unsafe MetadataReader(byte* metadata, int length, MetadataReaderOptions options, MetadataStringDecoder utf8Decoder)
{
// Do not throw here when length is 0. We'll throw BadImageFormatException later on, so that the caller doesn't need to
Expand Down Expand Up @@ -206,7 +207,7 @@ private void ReadMetadataHeader(ref BlobReader memReader, out string versionStri

int numberOfBytesRead;
versionString = memReader.GetMemoryBlockAt(0, versionStringSize).PeekUtf8NullTerminated(0, null, UTF8Decoder, out numberOfBytesRead, '\0');
memReader.SkipBytes(versionStringSize);
memReader.Offset += versionStringSize;
}

private MetadataKind GetMetadataKind(string versionString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2756,7 +2756,7 @@ private static unsafe byte[] ObfuscateWithExtraData(byte[] unobfuscated, bool se
blobReader.ReadUInt16(); // minor version
blobReader.ReadUInt32(); // reserved
int versionStringSize = blobReader.ReadInt32();
blobReader.SkipBytes(versionStringSize);
blobReader.Offset += versionStringSize;

// read stream headers to collect offsets and sizes to adjust later
blobReader.ReadUInt16(); // reserved
Expand Down

0 comments on commit ae0e558

Please sign in to comment.