public static byte ReadByte (IntPtr ptr, int ofs) {
unsafe {
- return *(byte*)(ptr + ofs);
+ return *((byte*)ptr + ofs);
}
}
}
public static short ReadInt16 (IntPtr ptr, int ofs) {
- if ((ptr + ofs).ToInt32 () % 2 == 0) {
+ if ((IntPtr.Add (ptr, ofs)).ToInt32 () % 2 == 0) {
unsafe {
- return *(short*)(ptr + ofs);
+ return *(short*)(IntPtr.Add (ptr, ofs));
}
} else {
unsafe {
short s;
- String.memcpy ((byte*)&s, (byte*)(ptr + ofs), 2);
+ String.memcpy ((byte*)&s, (byte*)(IntPtr.Add (ptr, ofs)), 2);
return s;
}
}
[ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
public static int ReadInt32 (IntPtr ptr, int ofs) {
- if ((ptr + ofs).ToInt32 () % 4 == 0) {
+ if ((IntPtr.Add (ptr, ofs)).ToInt32 () % 4 == 0) {
unsafe {
- return *(int*)(ptr + ofs);
+ return *(int*)(IntPtr.Add (ptr, ofs));
}
} else {
unsafe {
int s;
- String.memcpy ((byte*)&s, (byte*)(ptr + ofs), 4);
+ String.memcpy ((byte*)&s, (byte*)(IntPtr.Add (ptr, ofs)), 4);
return s;
}
}
}
public static long ReadInt64 (IntPtr ptr, int ofs) {
- if ((ptr + ofs).ToInt32 () % 8 == 0) {
+ if ((IntPtr.Add (ptr, ofs)).ToInt32 () % 8 == 0) {
unsafe {
- return *(long*)(ptr + ofs);
+ return *(long*)(IntPtr.Add (ptr, ofs));
}
} else {
unsafe {
long s;
- String.memcpy ((byte*)&s, (byte*)(ptr + ofs), 8);
+ String.memcpy ((byte*)&s, (byte*)(IntPtr.Add (ptr, ofs)), 8);
return s;
}
}
public static void WriteByte (IntPtr ptr, int ofs, byte val) {
unsafe {
- *(byte*)(ptr + ofs) = val;
+ *(byte*)(IntPtr.Add (ptr, ofs)) = val;
}
}
}
public static void WriteInt16 (IntPtr ptr, int ofs, short val) {
- if ((ptr + ofs).ToInt32 () % 2 == 0) {
+ if ((IntPtr.Add (ptr, ofs)).ToInt32 () % 2 == 0) {
unsafe {
- *(short*)(ptr + ofs) = val;
+ *(short*)(IntPtr.Add (ptr, ofs)) = val;
}
} else {
unsafe {
- String.memcpy ((byte*)(ptr + ofs), (byte*)&val, 2);
+ String.memcpy ((byte*)(IntPtr.Add (ptr, ofs)), (byte*)&val, 2);
}
}
}
}
public static void WriteInt32 (IntPtr ptr, int ofs, int val) {
- if ((ptr + ofs).ToInt32 () % 4 == 0) {
+ if ((IntPtr.Add (ptr, ofs)).ToInt32 () % 4 == 0) {
unsafe {
- *(int*)(ptr + ofs) = val;
+ *(int*)(IntPtr.Add (ptr, ofs)) = val;
}
} else {
unsafe {
- String.memcpy ((byte*)(ptr + ofs), (byte*)&val, 4);
+ String.memcpy ((byte*)(IntPtr.Add (ptr, ofs)), (byte*)&val, 4);
}
}
}
}
public static void WriteInt64 (IntPtr ptr, int ofs, long val) {
- if ((ptr + ofs).ToInt32 () % 8 == 0) {
+ if ((IntPtr.Add (ptr, ofs)).ToInt32 () % 8 == 0) {
unsafe {
- *(long*)(ptr + ofs) = val;
+ *(long*)(IntPtr.Add (ptr, ofs)) = val;
}
} else {
unsafe {
- String.memcpy ((byte*)(ptr + ofs), (byte*)&val, 8);
+ String.memcpy ((byte*)(IntPtr.Add (ptr, ofs)), (byte*)&val, 8);
}
}
}
Assert.AreEqual (0x1234, Marshal.ReadInt16 (ptr));
Assert.AreEqual (0x1234, Marshal.ReadInt16 (ptr, 0));
Assert.AreEqual (0x4567, Marshal.ReadInt16 (ptr, 2));
+#if NET_4_5
Assert.AreEqual (0x4567, Marshal.ReadInt16 ((ptr + 5)));
+#endif
Assert.AreEqual (0x4567, Marshal.ReadInt16 (ptr, 5));
} finally {
Marshal.FreeHGlobal (ptr);
Assert.AreEqual (0x12345678, Marshal.ReadInt32 (ptr));
Assert.AreEqual (0x12345678, Marshal.ReadInt32 (ptr, 0));
Assert.AreEqual (0x77654321, Marshal.ReadInt32 (ptr, 4));
+#if NET_4_5
Assert.AreEqual (0x77654321, Marshal.ReadInt32 ((ptr + 10)));
+#endif
Assert.AreEqual (0x77654321, Marshal.ReadInt32 (ptr, 10));
} finally {
Marshal.FreeHGlobal (ptr);