Exemplo n.º 1
0
 private void WriteSockAddr(ServiceCtx context, long bufferPosition, IPEndPoint endPoint)
 {
     context.Memory.WriteByte(bufferPosition, 0);
     context.Memory.WriteByte(bufferPosition + 1, (byte)endPoint.AddressFamily);
     context.Memory.WriteUInt16(bufferPosition + 2, EndianSwap.Swap16((ushort)endPoint.Port));
     context.Memory.WriteBytes(bufferPosition + 4, endPoint.Address.GetAddressBytes());
 }
Exemplo n.º 2
0
 private void WriteSockAddr(ServiceCtx Context, long BufferPosition, IPEndPoint EndPoint)
 {
     Context.Memory.WriteByte(BufferPosition, 0);
     Context.Memory.WriteByte(BufferPosition + 1, (byte)EndPoint.AddressFamily);
     Context.Memory.WriteUInt16(BufferPosition + 2, EndianSwap.Swap16((ushort)EndPoint.Port));
     Context.Memory.WriteBytes(BufferPosition + 4, EndPoint.Address.GetAddressBytes());
 }
Exemplo n.º 3
0
        public static OpusPacketHeader FromStream(BinaryReader reader)
        {
            OpusPacketHeader header = reader.ReadStruct <OpusPacketHeader>();

            header.length     = EndianSwap.FromBigEndianToPlatformEndian(header.length);
            header.finalRange = EndianSwap.FromBigEndianToPlatformEndian(header.finalRange);

            return(header);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Reinterprets a 4-byte integer fourcc as a string containing the human-readable characters it contains.
 /// </summary>
 /// <param name="fourcc">4-byte unsigned integer fourcc</param>
 /// <param name="endian">byte order in which the fourcc is interpreted</param>
 /// <returns>human-readable fourcc string</returns>
 public static string FourCC(uint fourcc, ByteOrder endian)
 {
     byte[] buffer = BitConverter.GetBytes(fourcc);
     if (endian == ByteOrder.LittleEndian)
     {
         EndianSwap.ByteSwap4(buffer);
     }
     return(Encoding.ASCII.GetString(buffer));
 }
Exemplo n.º 5
0
        private IPEndPoint ParseSockAddr(ServiceCtx context, long bufferPosition, long bufferSize)
        {
            int size   = context.Memory.ReadByte(bufferPosition);
            int family = context.Memory.ReadByte(bufferPosition + 1);
            int port   = EndianSwap.Swap16(context.Memory.ReadUInt16(bufferPosition + 2));

            byte[] rawIp = context.Memory.ReadBytes(bufferPosition + 4, 4);

            return(new IPEndPoint(new IPAddress(rawIp), port));
        }
Exemplo n.º 6
0
        private void WriteMagicAndSize(long Position, int Size)
        {
            const int DecMagic = 0x18029a7f;
            const int Key      = 0x49621806;

            int EncryptedSize = EndianSwap.Swap32(Size ^ Key);

            Device.Memory.WriteInt32(Position + 0, DecMagic);
            Device.Memory.WriteInt32(Position + 4, EncryptedSize);
        }
Exemplo n.º 7
0
        private void WriteMagicAndSize(long position, int size)
        {
            const int decMagic = 0x18029a7f;
            const int key      = 0x49621806;

            int encryptedSize = EndianSwap.Swap32(size ^ key);

            _device.Memory.WriteInt32(position + 0, decMagic);
            _device.Memory.WriteInt32(position + 4, encryptedSize);
        }
Exemplo n.º 8
0
        private IPEndPoint ParseSockAddr(ServiceCtx Context, long BufferPosition, long BufferSize)
        {
            int Size   = Context.Memory.ReadByte(BufferPosition);
            int Family = Context.Memory.ReadByte(BufferPosition + 1);
            int Port   = EndianSwap.Swap16(Context.Memory.ReadUInt16(BufferPosition + 2));

            byte[] RawIp = Context.Memory.ReadBytes(BufferPosition + 4, 4);

            return(new IPEndPoint(new IPAddress(RawIp), Port));
        }
Exemplo n.º 9
0
        public Npdm(Stream NPDMStream)
        {
            BinaryReader Reader = new BinaryReader(NPDMStream);

            if (Reader.ReadInt32() != NpdmMagic)
            {
                throw new InvalidNpdmException("NPDM Stream doesn't contain NPDM file!");
            }

            Reader.ReadInt64(); // Padding / Unused

            // MmuFlags, bit0: 64-bit instructions, bits1-3: address space width (1=64-bit, 2=32-bit). Needs to be <= 0xF
            byte MmuFlags = Reader.ReadByte();

            Is64Bits          = (MmuFlags & 1) != 0;
            AddressSpaceWidth = (MmuFlags >> 1) & 7;

            Reader.ReadByte();                      // Padding / Unused

            MainThreadPriority = Reader.ReadByte(); // (0-63)
            DefaultCpuId       = Reader.ReadByte();

            Reader.ReadInt32(); // Padding / Unused

            // System resource size (max size as of 5.x: 534773760). Unknown usage.
            SystemResourceSize = EndianSwap.Swap32(Reader.ReadInt32());

            // ProcessCategory (0: regular title, 1: kernel built-in). Should be 0 here.
            ProcessCategory = EndianSwap.Swap32(Reader.ReadInt32());

            // Main entrypoint stack size
            // (Should(?) be page-aligned. In non-nspwn scenarios, values of 0 can also rarely break in Horizon.
            // This might be something auto-adapting or a security feature of some sort ?)
            MainEntrypointStackSize = Reader.ReadInt32();

            byte[] TempTitleName = Reader.ReadBytes(0x10);
            TitleName = Encoding.UTF8.GetString(TempTitleName, 0, TempTitleName.Length).Trim('\0');

            ProductCode = Reader.ReadBytes(0x10);      // Unknown value

            NPDMStream.Seek(0x30, SeekOrigin.Current); // Skip reserved bytes

            ACI0Offset = Reader.ReadInt32();
            ACI0Size   = Reader.ReadInt32();
            ACIDOffset = Reader.ReadInt32();
            ACIDSize   = Reader.ReadInt32();

            ACI0 = new ACI0(NPDMStream, ACI0Offset);
            ACID = new ACID(NPDMStream, ACIDOffset);

            FSPerms = ACI0.FSAccessHeader.PermissionsBitmask & ACID.FSAccessControl.PermissionsBitmask;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Reinterprets a string (with a length of four characters) as an unsigned integer fourcc.
        /// </summary>
        /// <param name="fourcc">the string to convert</param>
        /// <param name="endian">byte order in which the fourcc is interpreted</param>
        /// <returns>a 4-byte integer fourcc</returns>
        public static uint FourCC(string fourcc, ByteOrder endian)
        {
            if (fourcc.Length != 4)
            {
                throw new ArgumentException("A four character code must contain four characters.", "fourcc");
            }

            byte[] buffer = Encoding.ASCII.GetBytes(fourcc);
            if (endian == ByteOrder.LittleEndian)
            {
                EndianSwap.ByteSwap4(buffer);
            }
            return(BitConverter.ToUInt32(buffer, 0));
        }
Exemplo n.º 11
0
        public Npdm(Stream Stream)
        {
            BinaryReader Reader = new BinaryReader(Stream);

            if (Reader.ReadInt32() != MetaMagic)
            {
                throw new InvalidNpdmException("NPDM Stream doesn't contain NPDM file!");
            }

            Reader.ReadInt64();

            //MmuFlags, bit0: 64-bit instructions, bits1-3: address space width (1=64-bit, 2=32-bit). Needs to be <= 0xF.
            byte MmuFlags = Reader.ReadByte();

            Is64Bits          = (MmuFlags & 1) != 0;
            AddressSpaceWidth = (MmuFlags >> 1) & 7;

            Reader.ReadByte();

            MainThreadPriority = Reader.ReadByte(); //(0-63).
            DefaultCpuId       = Reader.ReadByte();

            Reader.ReadInt32();

            //System resource size (max size as of 5.x: 534773760).
            SystemResourceSize = EndianSwap.Swap32(Reader.ReadInt32());

            //ProcessCategory (0: regular title, 1: kernel built-in). Should be 0 here.
            ProcessCategory = EndianSwap.Swap32(Reader.ReadInt32());

            //Main entrypoint stack size.
            MainEntrypointStackSize = Reader.ReadInt32();

            byte[] TempTitleName = Reader.ReadBytes(0x10);

            TitleName = Encoding.UTF8.GetString(TempTitleName, 0, TempTitleName.Length).Trim('\0');

            ProductCode = Reader.ReadBytes(0x10);

            Stream.Seek(0x30, SeekOrigin.Current);

            int ACI0Offset = Reader.ReadInt32();
            int ACI0Size   = Reader.ReadInt32();
            int ACIDOffset = Reader.ReadInt32();
            int ACIDSize   = Reader.ReadInt32();

            ACI0 = new ACI0(Stream, ACI0Offset);
            ACID = new ACID(Stream, ACIDOffset);
        }
Exemplo n.º 12
0
        public void ParseAddrBuffer(int SocketId, byte[] AddrBuffer)
        {
            using (MemoryStream MS = new MemoryStream(AddrBuffer))
            {
                BinaryReader Reader = new BinaryReader(MS);

                int Size   = Reader.ReadByte();
                int Family = Reader.ReadByte();
                int Port   = EndianSwap.Swap16(Reader.ReadUInt16());

                string IpAddress = Reader.ReadByte().ToString() + "." +
                                   Reader.ReadByte().ToString() + "." +
                                   Reader.ReadByte().ToString() + "." +
                                   Reader.ReadByte().ToString();

                Sockets[SocketId].IpAddress = IPAddress.Parse(IpAddress);
                Sockets[SocketId].RemoteEP  = new IPEndPoint(Sockets[SocketId].IpAddress, Port);
            }
        }
Exemplo n.º 13
0
        public void ParseAddrBuffer(int SocketId, byte[] AddrBuffer)
        {
            using (MemoryStream MS = new MemoryStream(AddrBuffer))
            {
                BinaryReader Reader = new BinaryReader(MS);

                int Size   = Reader.ReadByte();
                int Family = Reader.ReadByte();
                int Port   = EndianSwap.Swap16(Reader.ReadInt16());

                string IpAddress = Reader.ReadByte().ToString() + "." +
                                   Reader.ReadByte().ToString() + "." +
                                   Reader.ReadByte().ToString() + "." +
                                   Reader.ReadByte().ToString();

                Logging.Debug(LogClass.ServiceBsd, $"Try to connect to {IpAddress}:{Port}");

                Sockets[SocketId].IpAddress = IPAddress.Parse(IpAddress);
                Sockets[SocketId].RemoteEP  = new IPEndPoint(Sockets[SocketId].IpAddress, Port);
            }
        }