From 197dde918b7abd7e5bf8ba61225c064459430bd0 Mon Sep 17 00:00:00 2001 From: Julian Xhokaxhiu Date: Mon, 28 Nov 2022 23:55:39 +0100 Subject: [PATCH] 7thWrapperLib: Use the Util.CopyToIntptr function instead of unsafe pointers --- 7thWrapperLib/Wrap.cs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/7thWrapperLib/Wrap.cs b/7thWrapperLib/Wrap.cs index b3d7c240..1e9fd8dd 100644 --- a/7thWrapperLib/Wrap.cs +++ b/7thWrapperLib/Wrap.cs @@ -11,9 +11,10 @@ The original developer is Iros using System.Runtime.InteropServices; using System.IO; using System.Diagnostics; +using Iros._7th; namespace _7thWrapperLib { - public static unsafe class Wrap { + public static class Wrap { //private static Dictionary _hMap = new Dictionary(); //private static Dictionary _hNames = new Dictionary(); //private static Dictionary _streamFiles = new Dictionary(); @@ -238,8 +239,8 @@ public static int HReadFile(IntPtr handle, IntPtr bytes, uint numBytesToRead, In if (_varchives.TryGetValue(handle, out va)) { ret = va.ReadFile(bytes, numBytesToRead, ref _numBytesRead); - uint* ptrNumBytesRead = (uint*)numBytesRead.ToPointer(); - *ptrNumBytesRead = _numBytesRead; + byte[] tmp = BitConverter.GetBytes(_numBytesRead); + Util.CopyToIntPtr(tmp, numBytesRead, tmp.Length); return ret; } @@ -395,11 +396,16 @@ public static int HGetFileInformationByHandle(IntPtr hFile, IntPtr lpFileInforma if (result && _varchives.TryGetValue(hFile, out va)) { DebugLogger.DetailedWriteLine($"Overriding GetFileInformationByHandle for dummy file {hFile}"); - Win32.BY_HANDLE_FILE_INFORMATION* ptr = (Win32.BY_HANDLE_FILE_INFORMATION*)lpFileInformation; - *ptr = _lpFileInformation; - ptr->FileSizeHigh = (uint)(va.Size >> 32); - ptr->FileSizeLow = (uint)(va.Size & 0xffffffff); + byte[] tmp = null; + + // FileSizeHigh + tmp = BitConverter.GetBytes((uint)(va.Size >> 32)); + Util.CopyToIntPtr(tmp, IntPtr.Add(lpFileInformation, 32), 4); + + // FileSizeLow + tmp = BitConverter.GetBytes((uint)(va.Size & 0xffffffff)); + Util.CopyToIntPtr(tmp, IntPtr.Add(lpFileInformation, 36), 4); } return result ? 1 : 0; @@ -440,7 +446,10 @@ public static int HGetFileSizeEx(IntPtr hFile, IntPtr lpFileSize) { DebugLogger.WriteLine($"GetFileSizeEx on dummy handle {hFile}"); - *(long*)lpFileSize.ToPointer() = va.Size; + byte[] tmp = null; + tmp = BitConverter.GetBytes(va.Size); + Util.CopyToIntPtr(tmp, lpFileSize, tmp.Length); + ret = 1; } //else