From 74aa7b20be5ef924f3e8cbde699fcf4f8431f750 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Sat, 12 Dec 2020 00:06:20 -0300
Subject: [PATCH] Rewrite size for fixed size buffers (#1808)

---
 Ryujinx.Cpu/MemoryHelper.cs                     |  4 +++-
 Ryujinx.HLE/HOS/Ipc/IpcPtrBuffDesc.cs           |  5 +++++
 .../Account/Acc/IManagerForApplication.cs       |  4 +++-
 .../HOS/Services/Account/Acc/IProfile.cs        |  2 ++
 .../IDeliveryCacheProgressService.cs            | 13 +++++--------
 .../Friend/ServiceCreator/IFriendService.cs     |  2 ++
 .../HOS/Services/Time/IStaticServiceForPsc.cs   | 17 ++++++-----------
 7 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/Ryujinx.Cpu/MemoryHelper.cs b/Ryujinx.Cpu/MemoryHelper.cs
index 8ef4bc6679..7c400e91ce 100644
--- a/Ryujinx.Cpu/MemoryHelper.cs
+++ b/Ryujinx.Cpu/MemoryHelper.cs
@@ -37,7 +37,7 @@ namespace Ryujinx.Cpu
             }
         }
 
-        public unsafe static void Write<T>(IVirtualMemoryManager memory, long position, T value) where T : struct
+        public unsafe static long Write<T>(IVirtualMemoryManager memory, long position, T value) where T : struct
         {
             long size = Marshal.SizeOf<T>();
 
@@ -49,6 +49,8 @@ namespace Ryujinx.Cpu
             }
 
             memory.Write((ulong)position, data);
+
+            return size;
         }
 
         public static string ReadAsciiString(IVirtualMemoryManager memory, long position, long maxSize = -1)
diff --git a/Ryujinx.HLE/HOS/Ipc/IpcPtrBuffDesc.cs b/Ryujinx.HLE/HOS/Ipc/IpcPtrBuffDesc.cs
index c17f248f80..67cf17c931 100644
--- a/Ryujinx.HLE/HOS/Ipc/IpcPtrBuffDesc.cs
+++ b/Ryujinx.HLE/HOS/Ipc/IpcPtrBuffDesc.cs
@@ -30,6 +30,11 @@ namespace Ryujinx.HLE.HOS.Ipc
             Size = (ushort)(word0 >> 16);
         }
 
+        public IpcPtrBuffDesc WithSize(long size)
+        {
+            return new IpcPtrBuffDesc(Position, Index, size);
+        }
+
         public uint GetWord0()
         {
             uint word0;
diff --git a/Ryujinx.HLE/HOS/Services/Account/Acc/IManagerForApplication.cs b/Ryujinx.HLE/HOS/Services/Account/Acc/IManagerForApplication.cs
index 7e2b5d4fc7..734366dccc 100644
--- a/Ryujinx.HLE/HOS/Services/Account/Acc/IManagerForApplication.cs
+++ b/Ryujinx.HLE/HOS/Services/Account/Acc/IManagerForApplication.cs
@@ -41,7 +41,7 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
         // GetAccountId() -> nn::account::NetworkServiceAccountId
         public ResultCode GetAccountId(ServiceCtx context)
         {
-            // NOTE: This opens the file at "su/baas/USERID_IN_UUID_STRING.dat" (where USERID_IN_UUID_STRING is formatted 
+            // NOTE: This opens the file at "su/baas/USERID_IN_UUID_STRING.dat" (where USERID_IN_UUID_STRING is formatted
             //       as "%08x-%04x-%04x-%02x%02x-%08x%04x") in the account:/ savedata.
             //       Then it searches the NetworkServiceAccountId related to the UserId in this file and returns it.
 
@@ -122,6 +122,8 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
 
             context.ResponseData.Write(NetworkServiceAccountId);
 
+            context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(0L);
+
             // TODO: determine and fill the two output IPC buffers.
 
             return ResultCode.Success;
diff --git a/Ryujinx.HLE/HOS/Services/Account/Acc/IProfile.cs b/Ryujinx.HLE/HOS/Services/Account/Acc/IProfile.cs
index 32ee41d880..4b134c6f45 100644
--- a/Ryujinx.HLE/HOS/Services/Account/Acc/IProfile.cs
+++ b/Ryujinx.HLE/HOS/Services/Account/Acc/IProfile.cs
@@ -24,6 +24,8 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
         {
             Logger.Stub?.PrintStub(LogClass.ServiceAcc);
 
+            context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(0x80L);
+
             long position = context.Request.ReceiveBuff[0].Position;
 
             MemoryHelper.FillWithZeros(context.Memory, position, 0x80);
diff --git a/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheProgressService.cs b/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheProgressService.cs
index 54b1a58acb..741f33632a 100644
--- a/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheProgressService.cs
+++ b/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheProgressService.cs
@@ -1,5 +1,6 @@
 using Ryujinx.Common;
 using Ryujinx.Common.Logging;
+using Ryujinx.Cpu;
 using Ryujinx.HLE.HOS.Ipc;
 using Ryujinx.HLE.HOS.Kernel.Common;
 using Ryujinx.HLE.HOS.Kernel.Threading;
@@ -48,21 +49,17 @@ namespace Ryujinx.HLE.HOS.Services.Bcat.ServiceCreator
                 Result = 0
             };
 
-            WriteDeliveryCacheProgressImpl(context, context.Request.RecvListBuff[0], deliveryCacheProgress);
+            long dcpSize = WriteDeliveryCacheProgressImpl(context, context.Request.RecvListBuff[0], deliveryCacheProgress);
+            context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(dcpSize);
 
             Logger.Stub?.PrintStub(LogClass.ServiceBcat);
 
             return ResultCode.Success;
         }
 
-        private void WriteDeliveryCacheProgressImpl(ServiceCtx context, IpcRecvListBuffDesc ipcDesc, DeliveryCacheProgressImpl deliveryCacheProgress)
+        private long WriteDeliveryCacheProgressImpl(ServiceCtx context, IpcRecvListBuffDesc ipcDesc, DeliveryCacheProgressImpl deliveryCacheProgress)
         {
-            using (MemoryStream memory = new MemoryStream((int)ipcDesc.Size))
-            using (BinaryWriter bufferWriter = new BinaryWriter(memory))
-            {
-                bufferWriter.WriteStruct(deliveryCacheProgress);
-                context.Memory.Write((ulong)ipcDesc.Position, memory.ToArray());
-            }
+            return MemoryHelper.Write(context.Memory, ipcDesc.Position, deliveryCacheProgress);
         }
     }
 }
\ No newline at end of file
diff --git a/Ryujinx.HLE/HOS/Services/Friend/ServiceCreator/IFriendService.cs b/Ryujinx.HLE/HOS/Services/Friend/ServiceCreator/IFriendService.cs
index 64ef5b2b22..71502cda21 100644
--- a/Ryujinx.HLE/HOS/Services/Friend/ServiceCreator/IFriendService.cs
+++ b/Ryujinx.HLE/HOS/Services/Friend/ServiceCreator/IFriendService.cs
@@ -221,6 +221,8 @@ namespace Ryujinx.HLE.HOS.Services.Friend.ServiceCreator
             bool   unknownBool = context.RequestData.ReadBoolean();
             UserId userId      = context.RequestData.ReadStruct<UserId>();
 
+            context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(0x40L);
+
             long bufferPosition  = context.Request.RecvListBuff[0].Position;
 
             if (userId.IsNull)
diff --git a/Ryujinx.HLE/HOS/Services/Time/IStaticServiceForPsc.cs b/Ryujinx.HLE/HOS/Services/Time/IStaticServiceForPsc.cs
index 8de3f72654..dee3e47629 100644
--- a/Ryujinx.HLE/HOS/Services/Time/IStaticServiceForPsc.cs
+++ b/Ryujinx.HLE/HOS/Services/Time/IStaticServiceForPsc.cs
@@ -1,4 +1,5 @@
 using Ryujinx.Common;
+using Ryujinx.Cpu;
 using Ryujinx.HLE.HOS.Ipc;
 using Ryujinx.HLE.HOS.Kernel.Common;
 using Ryujinx.HLE.HOS.Kernel.Threading;
@@ -245,6 +246,8 @@ namespace Ryujinx.HLE.HOS.Services.Time
         {
             byte type = context.RequestData.ReadByte();
 
+            context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(Marshal.SizeOf<ClockSnapshot>());
+
             ResultCode result = _timeManager.StandardUserSystemClock.GetClockContext(context.Thread, out SystemClockContext userContext);
 
             if (result == ResultCode.Success)
@@ -271,6 +274,8 @@ namespace Ryujinx.HLE.HOS.Services.Time
         {
             byte type = context.RequestData.ReadByte();
 
+            context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(Marshal.SizeOf<ClockSnapshot>());
+
             context.RequestData.BaseStream.Position += 7;
 
             SystemClockContext userContext    = context.RequestData.ReadStruct<SystemClockContext>();
@@ -413,17 +418,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
 
         private void WriteClockSnapshotFromBuffer(ServiceCtx context, IpcRecvListBuffDesc ipcDesc, ClockSnapshot clockSnapshot)
         {
-            Debug.Assert(ipcDesc.Size == Marshal.SizeOf<ClockSnapshot>());
-
-            MemoryStream memory = new MemoryStream((int)ipcDesc.Size);
-
-            using (BinaryWriter bufferWriter = new BinaryWriter(memory))
-            {
-                bufferWriter.WriteStruct(clockSnapshot);
-            }
-
-            context.Memory.Write((ulong)ipcDesc.Position, memory.ToArray());
-            memory.Dispose();
+            MemoryHelper.Write(context.Memory, ipcDesc.Position, clockSnapshot);
         }
     }
 }
\ No newline at end of file