コード例 #1
0
ファイル: PacketReader.cs プロジェクト: neoscrib/secureshell
        public object ReadProperty(SshPropertyInfo p)
        {
            Type type = p.Information.PropertyType;

            // ordered by most common type.
            if (typeof(Enum).IsAssignableFrom(type) && Enum.GetUnderlyingType(type).Equals(typeof(byte)))
            {
                return(Enum.ToObject(type, this.ReadByte()));
            }
            else if (type.Equals(typeof(string[])))
            {
                return(this.ReadStringAsString().Split(','));
            }
            else if (type.Equals(typeof(string)))
            {
                return(this.ReadStringAsString());
            }
            else if (type.Equals(typeof(uint)))
            {
                return(this.ReadUInt32());
            }
            else if (type.Equals(typeof(byte[])))
            {
                if (p.Attributes.Raw)
                {
                    return(this.ReadBytes(p.Attributes.RawLength));
                }
                else
                {
                    return(this.ReadString());
                }
            }
            else if (type.Equals(typeof(bool)))
            {
                return(this.ReadBoolean());
            }
            else if (typeof(Enum).IsAssignableFrom(type) && Enum.GetUnderlyingType(type).Equals(typeof(uint)))
            {
                return(Enum.ToObject(type, this.ReadUInt32()));
            }
            else if (type.Equals(typeof(short)))
            {
                return(this.ReadInt16());
            }
            else if (type.Equals(typeof(int)))
            {
                return(this.ReadInt32());
            }
            else if (type.Equals(typeof(long)))
            {
                return(this.ReadInt64());
            }
            else if (type.Equals(typeof(ulong)))
            {
                return(this.ReadUInt64());
            }
            else if (type.Equals(typeof(Dictionary <string, string>)))
            {
                var d = new Dictionary <string, string>();
                while (this.Position < this.Length)
                {
                    d.Add(this.ReadStringAsString(), this.ReadStringAsString());
                }
                return(d);
            }
            else if (type.Equals(typeof(SftpFileAttributes)))
            {
                var v = new SftpFileAttributes();
                v.Flags = (FileInfoFlags)this.ReadUInt32();
                if (v.Flags.HasFlag(FileInfoFlags.SSH_FILEXFER_ATTR_PERMISSIONS))
                {
                    v.Permissions = (PermissionsFlags)this.ReadUInt32();
                }
            }
            else if (type.Equals(typeof(SftpFileInfo)))
            {
                return(new SftpFileInfo(this, string.Empty, string.Empty, string.Empty));
            }
            else if (type.Equals(typeof(SftpFileInfo[])))
            {
                var d = new List <SftpFileInfo>();
                while (this.Position < this.Length)
                {
                    d.Add(new SftpFileInfo(this, string.Empty));
                }
                return(d.ToArray());
            }

            return(null);
        }
コード例 #2
0
ファイル: PacketWriter.cs プロジェクト: neoscrib/secureshell
        public void WriteProperty(SshPropertyInfo p, IPacket packet)
        {
            Type   type  = p.Information.PropertyType;
            object value = p.Information.GetValue(packet, null);

            // ordered by most common type.
            if (type.Equals(typeof(uint)))
            {
                this.WriteUInt32((uint)value);
            }
            else if (typeof(Enum).IsAssignableFrom(type) && Enum.GetUnderlyingType(type).Equals(typeof(byte)))
            {
                this.Write((byte)value);
            }
            else if (type.Equals(typeof(string[])))
            {
                this.WriteBytes(((string[])value).ToCSV());
            }
            else if (type.Equals(typeof(string)))
            {
                this.WriteBytes((string)value);
            }
            else if (type.Equals(typeof(SecureString)))
            {
                ((SecureString)value).Consume(d => this.WriteString(d));
            }
            else if (type.Equals(typeof(byte[])))
            {
                if (p.Attributes.Raw)
                {
                    this.Write((byte[])value);
                }
                else
                {
                    this.WriteString((byte[])value);
                }
            }
            else if (type.Equals(typeof(bool)))
            {
                this.Write((bool)value);
            }
            else if (typeof(Enum).IsAssignableFrom(type) && Enum.GetUnderlyingType(type).Equals(typeof(uint)))
            {
                this.WriteUInt32((uint)value);
            }
            else if (type.Equals(typeof(short)))
            {
                this.WriteInt16((short)value);
            }
            else if (type.Equals(typeof(int)))
            {
                this.WriteInt32((int)value);
            }
            else if (type.Equals(typeof(long)))
            {
                this.WriteInt64((long)value);
            }
            else if (type.Equals(typeof(ulong)))
            {
                this.WriteUInt64((ulong)value);
            }
            else if (type.Equals(typeof(Dictionary <string, string>)))
            {
                this.WriteDictionary((Dictionary <string, string>)value);
            }
            else if (type.Equals(typeof(SftpFileAttributes)))
            {
                this.WriteSftpFileAttributes((SftpFileAttributes)value);
            }
        }