IStorage.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Ryujinx.HLE.HOS.Ipc;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace Ryujinx.HLE.HOS.Services.FspSrv
  5. {
  6. class IStorage : IpcService
  7. {
  8. private Dictionary<int, ServiceProcessRequest> m_Commands;
  9. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  10. private Stream BaseStream;
  11. public IStorage(Stream BaseStream)
  12. {
  13. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  14. {
  15. { 0, Read }
  16. };
  17. this.BaseStream = BaseStream;
  18. }
  19. // Read(u64 offset, u64 length) -> buffer<u8, 0x46, 0> buffer
  20. public long Read(ServiceCtx Context)
  21. {
  22. long Offset = Context.RequestData.ReadInt64();
  23. long Size = Context.RequestData.ReadInt64();
  24. if (Context.Request.ReceiveBuff.Count > 0)
  25. {
  26. IpcBuffDesc BuffDesc = Context.Request.ReceiveBuff[0];
  27. //Use smaller length to avoid overflows.
  28. if (Size > BuffDesc.Size)
  29. {
  30. Size = BuffDesc.Size;
  31. }
  32. byte[] Data = new byte[Size];
  33. lock (BaseStream)
  34. {
  35. BaseStream.Seek(Offset, SeekOrigin.Begin);
  36. BaseStream.Read(Data, 0, Data.Length);
  37. }
  38. Context.Memory.WriteBytes(BuffDesc.Position, Data);
  39. }
  40. return 0;
  41. }
  42. }
  43. }