(ArcView): use unsafe pointer to access memory directly.

This commit is contained in:
morkt 2017-03-31 13:27:51 +04:00
parent 6e11704808
commit 3ae37c7a19
2 changed files with 86 additions and 227 deletions

View File

@ -28,7 +28,6 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using GameRes.Utility;
namespace GameRes namespace GameRes
{ {
@ -37,12 +36,7 @@ namespace GameRes
private readonly ArcView.Frame m_view; private readonly ArcView.Frame m_view;
private readonly long m_start; private readonly long m_start;
private readonly long m_size; private readonly long m_size;
private long m_position; private long m_position;
private byte[] m_buffer;
private int m_buffer_pos; // read position within buffer
private int m_buffer_len; // length of bytes read in buffer
private const int DefaultBufferSize = 0x1000;
public string Name { get; set; } public string Name { get; set; }
public uint Signature { get { return ReadSignature(); } } public uint Signature { get { return ReadSignature(); } }
@ -54,20 +48,11 @@ namespace GameRes
public override long Length { get { return m_size; } } public override long Length { get { return m_size; } }
public override long Position public override long Position
{ {
get { return m_position + (m_buffer_pos - m_buffer_len); } get { return m_position; }
set { set {
if (value < 0) if (value < 0)
throw new ArgumentOutOfRangeException ("value", "Stream position is out of range."); throw new ArgumentOutOfRangeException ("value", "Stream position is out of range.");
var buffer_start = m_position - m_buffer_len; m_position = value;
if (m_buffer_pos != m_buffer_len && value >= buffer_start && value < m_position)
{
m_buffer_pos = (int)(value - buffer_start);
}
else
{
m_position = value;
m_buffer_pos = m_buffer_len = 0;
}
} }
} }
@ -132,61 +117,24 @@ namespace GameRes
return new CowArray<byte> (m_header, 0, size); return new CowArray<byte> (m_header, 0, size);
} }
private void RefillBuffer ()
{
if (null == m_buffer)
m_buffer = new byte[DefaultBufferSize];
uint length = (uint)Math.Min (m_size - m_position, m_buffer.Length);
m_buffer_len = m_view.Read (m_start + m_position, m_buffer, 0, length);
m_position += m_buffer_len;
m_buffer_pos = 0;
}
private void FlushBuffer ()
{
if (m_buffer_len != 0)
{
m_position += m_buffer_pos - m_buffer_len;
m_buffer_pos = m_buffer_len = 0;
}
}
private void EnsureAvailable (int length) private void EnsureAvailable (int length)
{ {
if (m_buffer_pos + length > m_buffer_len) if (m_position + length > m_size)
{ throw new EndOfStreamException();
FlushBuffer();
if (m_position + length > m_size)
throw new EndOfStreamException();
RefillBuffer();
}
}
private int ReadFromBuffer (byte[] array, int offset, int count)
{
int available = Math.Min (m_buffer_len - m_buffer_pos, count);
if (available > 0)
{
Buffer.BlockCopy (m_buffer, m_buffer_pos, array, offset, available);
m_buffer_pos += available;
}
return available;
} }
public int PeekByte () public int PeekByte ()
{ {
if (m_buffer_pos == m_buffer_len) if (m_position >= m_size)
RefillBuffer();
if (m_buffer_pos == m_buffer_len)
return -1; return -1;
return m_buffer[m_buffer_pos]; return m_view.ReadByte (m_start+m_position);
} }
public override int ReadByte () public override int ReadByte ()
{ {
int b = PeekByte(); int b = PeekByte();
if (-1 != b) if (-1 != b)
++m_buffer_pos; ++m_position;
return b; return b;
} }
@ -206,8 +154,8 @@ namespace GameRes
public short ReadInt16 () public short ReadInt16 ()
{ {
EnsureAvailable (2); EnsureAvailable (2);
var v = m_buffer.ToInt16 (m_buffer_pos); var v = m_view.ReadInt16 (m_start+m_position);
m_buffer_pos += 2; m_position += 2;
return v; return v;
} }
@ -219,16 +167,17 @@ namespace GameRes
public int ReadInt24 () public int ReadInt24 ()
{ {
EnsureAvailable (3); EnsureAvailable (3);
int v = m_buffer.ToInt24 (m_buffer_pos); int v = m_view.ReadUInt16 (m_start+m_position);
m_buffer_pos += 3; v |= m_view.ReadByte (m_start+m_position+2) << 16;
m_position += 3;
return v; return v;
} }
public int ReadInt32 () public int ReadInt32 ()
{ {
EnsureAvailable (4); EnsureAvailable (4);
int v = m_buffer.ToInt32 (m_buffer_pos); var v = m_view.ReadInt32 (m_start+m_position);
m_buffer_pos += 4; m_position += 4;
return v; return v;
} }
@ -240,8 +189,8 @@ namespace GameRes
public long ReadInt64 () public long ReadInt64 ()
{ {
EnsureAvailable (8); EnsureAvailable (8);
var v = m_buffer.ToInt64 (m_buffer_pos); var v = m_view.ReadInt64 (m_start+m_position);
m_buffer_pos += 8; m_position += 8;
return v; return v;
} }
@ -257,39 +206,10 @@ namespace GameRes
public string ReadCString (int length, Encoding enc) public string ReadCString (int length, Encoding enc)
{ {
if (m_buffer_pos == m_buffer_len && length <= DefaultBufferSize) uint string_length = (uint)Math.Min (length, m_size-m_position);
RefillBuffer(); var str = m_view.ReadString (m_position, (uint)string_length, enc);
if (m_buffer_pos + length <= m_buffer_len) m_position += string_length;
{ return str;
// whole string fit into buffer
var str = Binary.GetCString (m_buffer, m_buffer_pos, length, enc);
m_buffer_pos += length;
return str;
}
else if (length > DefaultBufferSize)
{
// requested string length is larger than internal buffer size
var string_buffer = ReadBytes (length);
return Binary.GetCString (string_buffer, 0, string_buffer.Length, enc);
}
else
{
int available = m_buffer_len - m_buffer_pos;
if (available > 0 && m_buffer_pos != 0)
Buffer.BlockCopy (m_buffer, m_buffer_pos, m_buffer, 0, available);
else if (null == m_buffer)
m_buffer = new byte[DefaultBufferSize];
int count = (int)Math.Min (m_buffer.Length - available, m_size - m_position);
if (count > 0)
{
int read = m_view.Read (m_start + m_position, m_buffer, available, (uint)count);
m_position += read;
available += read;
}
m_buffer_len = available;
m_buffer_pos = Math.Min (length, m_buffer_len);
return Binary.GetCString (m_buffer, 0, m_buffer_pos, enc);
}
} }
public string ReadCString () public string ReadCString ()
@ -299,27 +219,12 @@ namespace GameRes
public string ReadCString (Encoding enc) public string ReadCString (Encoding enc)
{ {
if (m_buffer_pos == m_buffer_len) // underlying view includes rest of the stream
RefillBuffer(); if (m_view.Offset <= m_position && m_view.Offset + m_view.Reserved >= m_start + m_size)
int available = m_buffer_len - m_buffer_pos; return ReadCStringUnsafe (enc);
if (0 == available)
return string.Empty;
int zero = Array.IndexOf<byte> (m_buffer, 0, m_buffer_pos, available); var string_buf = new byte[0x20];
if (zero != -1) int size = 0;
{
// null byte found within buffer
var str = enc.GetString (m_buffer, m_buffer_pos, zero - m_buffer_pos);
m_buffer_pos = zero+1;
return str;
}
// underlying view includes whole stream
if (m_view.Offset <= m_start && m_view.Offset + m_view.Reserved >= m_start + m_size)
return ReadCStringUnsafe (enc, available);
var string_buf = new byte[Math.Max (0x20, available * 2)];
ReadFromBuffer (string_buf, 0, available);
int size = available;
for (;;) for (;;)
{ {
int b = ReadByte(); int b = ReadByte();
@ -337,7 +242,6 @@ namespace GameRes
private unsafe string ReadCStringUnsafe (Encoding enc, int skip_bytes = 0) private unsafe string ReadCStringUnsafe (Encoding enc, int skip_bytes = 0)
{ {
Debug.Assert (m_view.Offset + m_view.Reserved >= m_start + m_size); Debug.Assert (m_view.Offset + m_view.Reserved >= m_start + m_size);
FlushBuffer();
using (var ptr = m_view.GetPointer()) using (var ptr = m_view.GetPointer())
{ {
byte* s = ptr.Value + (m_start - m_view.Offset + m_position); byte* s = ptr.Value + (m_start - m_view.Offset + m_position);
@ -350,44 +254,33 @@ namespace GameRes
m_position += string_length; m_position += string_length;
if (string_length < view_length) if (string_length < view_length)
++m_position; ++m_position;
// return enc.GetString (s, string_length); // .Net v4.6+ only return new string ((sbyte*)s, 0, string_length, enc);
var string_buf = new byte[string_length];
Marshal.Copy ((IntPtr)s, string_buf, 0, string_length);
return enc.GetString (string_buf, 0, string_length);
} }
} }
public byte[] ReadBytes (int count) public byte[] ReadBytes (int count)
{ {
if (m_buffer_pos + count <= m_buffer_len && m_buffer_len != 0) if (0 == count || m_position >= m_size)
{
var data = new CowArray<byte> (m_buffer, m_buffer_pos, count).ToArray();
m_buffer_pos += count;
return data;
}
var current_pos = Position;
if (0 == count || current_pos >= m_size)
return new byte[0]; return new byte[0];
var bytes = m_view.ReadBytes (m_start+current_pos, (uint)Math.Min (count, m_size - current_pos)); var bytes = m_view.ReadBytes (m_start+m_position, (uint)Math.Min (count, m_size - m_position));
Position = current_pos + bytes.Length; m_position += bytes.Length;
return bytes; return bytes;
} }
#region System.IO.Stream methods #region System.IO.Stream methods
public override void Flush() public override void Flush()
{ {
FlushBuffer();
} }
public override long Seek (long offset, SeekOrigin origin) public override long Seek (long offset, SeekOrigin origin)
{ {
switch (origin) switch (origin)
{ {
case SeekOrigin.Begin: Position = offset; break; case SeekOrigin.Current: offset += m_position; break;
case SeekOrigin.Current: Position += offset; break; case SeekOrigin.End: offset += m_size; break;
case SeekOrigin.End: Position = m_size + offset; break;
} }
return Position; Position = offset;
return m_position;
} }
public override void SetLength (long length) public override void SetLength (long length)
@ -397,26 +290,12 @@ namespace GameRes
public override int Read (byte[] buffer, int offset, int count) public override int Read (byte[] buffer, int offset, int count)
{ {
int read_from_buffer = ReadFromBuffer (buffer, offset, count);
offset += read_from_buffer;
count -= read_from_buffer;
if (0 == count || m_position >= m_size) if (0 == count || m_position >= m_size)
return read_from_buffer; return 0;
if (count < DefaultBufferSize) count = (int)Math.Min (count, m_size - m_position);
{ int read = m_view.Read (m_start + m_position, buffer, offset, (uint)count);
RefillBuffer(); m_position += read;
count = Math.Min (count, m_buffer_len); return read;
Buffer.BlockCopy (m_buffer, m_buffer_pos, buffer, offset, count);
m_buffer_pos += count;
return read_from_buffer + count;
}
else
{
uint view_count = (uint)Math.Min (count, m_size - m_position);
int read_from_view = m_view.Read (m_start + m_position, buffer, offset, view_count);
m_position += read_from_view;
return read_from_buffer + read_from_view;
}
} }
public override void Write (byte[] buffer, int offset, int count) public override void Write (byte[] buffer, int offset, int count)

View File

@ -77,27 +77,6 @@ namespace GameRes
public static class MappedViewExtension public static class MappedViewExtension
{ {
static public string ReadString (this MemoryMappedViewAccessor view, long offset, uint size, Encoding enc)
{
if (0 == size)
return string.Empty;
byte[] buffer = new byte[size];
uint n;
for (n = 0; n < size; ++n)
{
byte b = view.ReadByte (offset+n);
if (0 == b)
break;
buffer[n] = b;
}
return enc.GetString (buffer, 0, (int)n);
}
static public string ReadString (this MemoryMappedViewAccessor view, long offset, uint size)
{
return ReadString (view, offset, size, Encodings.cp932);
}
unsafe public static byte* GetPointer (this MemoryMappedViewAccessor view, long offset) unsafe public static byte* GetPointer (this MemoryMappedViewAccessor view, long offset)
{ {
var num = offset % info.dwAllocationGranularity; var num = offset % info.dwAllocationGranularity;
@ -273,6 +252,7 @@ namespace GameRes
private MemoryMappedViewAccessor m_view; private MemoryMappedViewAccessor m_view;
private long m_offset; private long m_offset;
private uint m_size; private uint m_size;
private unsafe byte* m_mem;
public long Offset { get { return m_offset; } } public long Offset { get { return m_offset; } }
public uint Reserved { get { return m_size; } } public uint Reserved { get { return m_size; } }
@ -283,6 +263,7 @@ namespace GameRes
m_offset = 0; m_offset = 0;
m_size = (uint)Math.Min (ArcView.PageSize, m_arc.MaxOffset); m_size = (uint)Math.Min (ArcView.PageSize, m_arc.MaxOffset);
m_view = m_arc.CreateViewAccessor (m_offset, m_size); m_view = m_arc.CreateViewAccessor (m_offset, m_size);
unsafe { m_mem = m_view.GetPointer (m_offset); }
} }
public Frame (Frame other) public Frame (Frame other)
@ -291,6 +272,7 @@ namespace GameRes
m_offset = 0; m_offset = 0;
m_size = (uint)Math.Min (ArcView.PageSize, m_arc.MaxOffset); m_size = (uint)Math.Min (ArcView.PageSize, m_arc.MaxOffset);
m_view = m_arc.CreateViewAccessor (m_offset, m_size); m_view = m_arc.CreateViewAccessor (m_offset, m_size);
unsafe { m_mem = m_view.GetPointer (m_offset); }
} }
public Frame (ArcView arc, long offset, uint size) public Frame (ArcView arc, long offset, uint size)
@ -299,6 +281,7 @@ namespace GameRes
m_offset = Math.Min (offset, m_arc.MaxOffset); m_offset = Math.Min (offset, m_arc.MaxOffset);
m_size = (uint)Math.Min (size, m_arc.MaxOffset-m_offset); m_size = (uint)Math.Min (size, m_arc.MaxOffset-m_offset);
m_view = m_arc.CreateViewAccessor (m_offset, m_size); m_view = m_arc.CreateViewAccessor (m_offset, m_size);
unsafe { m_mem = m_view.GetPointer (m_offset); }
} }
public uint Reserve (long offset, uint size) public uint Reserve (long offset, uint size)
@ -313,28 +296,32 @@ namespace GameRes
size = (uint)(m_arc.MaxOffset-offset); size = (uint)(m_arc.MaxOffset-offset);
var old_view = m_view; var old_view = m_view;
m_view = m_arc.CreateViewAccessor (offset, size); m_view = m_arc.CreateViewAccessor (offset, size);
old_view.SafeMemoryMappedViewHandle.ReleasePointer();
old_view.Dispose(); old_view.Dispose();
m_offset = offset; m_offset = offset;
m_size = size; m_size = size;
unsafe { m_mem = m_view.GetPointer (m_offset); }
} }
return (uint)(m_offset + m_size - offset); return (uint)(m_offset + m_size - offset);
} }
public void StrictReserve (long offset, uint size)
{
if (Reserve (offset, size) < size)
throw new ArgumentException ("Not enough bytes to read in the memory mapped file view.", "offset");
}
public bool AsciiEqual (long offset, string data) public bool AsciiEqual (long offset, string data)
{ {
if (Reserve (offset, (uint)data.Length) < (uint)data.Length) if (Reserve (offset, (uint)data.Length) < (uint)data.Length)
return false; return false;
unsafe unsafe
{ {
byte* ptr = m_view.GetPointer (m_offset) + (offset - m_offset); byte* ptr = m_mem + (offset - m_offset);
try { for (int i = 0; i < data.Length; ++i)
for (int i = 0; i < data.Length; ++i) {
{ if (ptr[i] != data[i])
if (ptr[i] != data[i]) return false;
return false;
}
} finally {
m_view.SafeMemoryMappedViewHandle.ReleasePointer();
} }
return true; return true;
} }
@ -360,12 +347,7 @@ namespace GameRes
private unsafe void UnsafeCopy (long offset, byte[] buf, int buf_offset, int count) private unsafe void UnsafeCopy (long offset, byte[] buf, int buf_offset, int count)
{ {
byte* ptr = m_view.GetPointer (m_offset); Marshal.Copy ((IntPtr)(m_mem + (offset-m_offset)), buf, buf_offset, count);
try {
Marshal.Copy ((IntPtr)(ptr+(offset-m_offset)), buf, buf_offset, count);
} finally {
m_view.SafeMemoryMappedViewHandle.ReleasePointer();
}
} }
/// <summary> /// <summary>
@ -383,77 +365,67 @@ namespace GameRes
public byte ReadByte (long offset) public byte ReadByte (long offset)
{ {
Reserve (offset, 1); StrictReserve (offset, 1);
return m_view.ReadByte (offset-m_offset); unsafe { return m_mem[offset-m_offset]; }
} }
public sbyte ReadSByte (long offset) public sbyte ReadSByte (long offset)
{ {
Reserve (offset, 1); StrictReserve (offset, 1);
return m_view.ReadSByte (offset-m_offset); unsafe { return (sbyte)m_mem[offset-m_offset]; }
} }
public ushort ReadUInt16 (long offset) public ushort ReadUInt16 (long offset)
{ {
Reserve (offset, 2); StrictReserve (offset, 2);
return m_view.ReadUInt16 (offset-m_offset); unsafe { return *(ushort*)(m_mem+offset-m_offset); }
} }
public short ReadInt16 (long offset) public short ReadInt16 (long offset)
{ {
Reserve (offset, 2); StrictReserve (offset, 2);
return m_view.ReadInt16 (offset-m_offset); unsafe { return *(short*)(m_mem+offset-m_offset); }
} }
public uint ReadUInt32 (long offset) public uint ReadUInt32 (long offset)
{ {
Reserve (offset, 4); StrictReserve (offset, 4);
return m_view.ReadUInt32 (offset-m_offset); unsafe { return *(uint*)(m_mem+offset-m_offset); }
} }
public int ReadInt32 (long offset) public int ReadInt32 (long offset)
{ {
Reserve (offset, 4); StrictReserve (offset, 4);
return m_view.ReadInt32 (offset-m_offset); unsafe { return *(int*)(m_mem+offset-m_offset); }
} }
public ulong ReadUInt64 (long offset) public ulong ReadUInt64 (long offset)
{ {
Reserve (offset, 8); StrictReserve (offset, 8);
return m_view.ReadUInt64 (offset-m_offset); unsafe { return *(ulong*)(m_mem+offset-m_offset); }
} }
public long ReadInt64 (long offset) public long ReadInt64 (long offset)
{ {
Reserve (offset, 8); StrictReserve (offset, 8);
return m_view.ReadInt64 (offset-m_offset); unsafe { return *(long*)(m_mem+offset-m_offset); }
} }
public string ReadString (long offset, uint size, Encoding enc) public string ReadString (long offset, uint size, Encoding enc)
{ {
size = Math.Min (size, Reserve (offset, size)); size = Math.Min (size, Reserve (offset, size));
return m_view.ReadString (offset-m_offset, size, enc);
/* unsafe implementation requires .Net v4.6
if (0 == size) if (0 == size)
return string.Empty; return string.Empty;
unsafe unsafe
{ {
byte* s = m_view.GetPointer (m_offset) + (offset - m_offset); byte* s = m_mem + (offset - m_offset);
try uint string_length = 0;
while (string_length < size && 0 != s[string_length])
{ {
uint string_length = 0; ++string_length;
while (string_length < size && 0 != s[string_length])
{
++string_length;
}
return enc.GetString (s, (int)string_length); // .Net v4.6+ only
}
finally
{
m_view.SafeMemoryMappedViewHandle.ReleasePointer();
} }
return new string ((sbyte*)s, 0, (int)string_length, enc);
} }
*/
} }
public string ReadString (long offset, uint size) public string ReadString (long offset, uint size)
@ -481,6 +453,14 @@ namespace GameRes
{ {
if (disposing) if (disposing)
{ {
unsafe
{
if (m_mem != null)
{
m_view.SafeMemoryMappedViewHandle.ReleasePointer();
m_mem = null;
}
}
m_view.Dispose(); m_view.Dispose();
} }
m_arc = null; m_arc = null;