예제 #1
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            ToxId id = obj as ToxId;

            if ((object)id == null)
            {
                return(false);
            }

            return(this == id);
        }
예제 #2
0
        internal static ToxDataInfo FromToxData(ToxData data)
        {
            try
            {
                ToxId         id            = null;
                string        name          = null;
                string        statusMessage = null;
                ToxUserStatus status        = ToxUserStatus.None;
                byte[]        secretKey     = null;

                using (var stream = new MemoryStream(data.Bytes))
                    using (var reader = new BinaryReader(stream))
                    {
                        stream.Position += sizeof(uint);

                        uint cookie = reader.ReadUInt32();
                        if (cookie != ToxConstants.Cookie)
                        {
                            throw new Exception("Invalid cookie, this doesn't look like a tox profile");
                        }

                        uint length = reader.ReadUInt32();
                        long left   = reader.BaseStream.Length - reader.BaseStream.Position;

                        while (left >= length)
                        {
                            var type = ReadStateType(reader);

                            if (type == StateType.EOF)
                            {
                                break;
                            }

                            switch (type)
                            {
                            case StateType.NospamKeys:
                                int    nospam    = reader.ReadInt32();
                                byte[] publicKey = reader.ReadBytes(ToxConstants.PublicKeySize);

                                secretKey = reader.ReadBytes(ToxConstants.SecretKeySize);
                                id        = new ToxId(publicKey, nospam);
                                break;

                            case StateType.Name:
                                name = Encoding.UTF8.GetString(reader.ReadBytes((int)length), 0, (int)length);
                                break;

                            case StateType.StatusMessage:
                                statusMessage = Encoding.UTF8.GetString(reader.ReadBytes((int)length), 0, (int)length);
                                break;

                            case StateType.Status:
                                status = (ToxUserStatus)reader.ReadByte();
                                break;

                            default:
                            case StateType.Dht:
                            case StateType.Friends:
                            case StateType.TcpRelay:
                            case StateType.PathNode:
                                stream.Position += length; //skip this
                                break;

                            case StateType.Corrupt:
                                throw new Exception("This Tox save file is corrupt");
                            }

                            left = reader.BaseStream.Length - reader.BaseStream.Position;
                            if (left < sizeof(uint))
                            {
                                break;
                            }
                            else
                            {
                                length = reader.ReadUInt32();
                            }
                        }
                    }

                return(new ToxDataInfo()
                {
                    Id = id,
                    Name = name,
                    StatusMessage = statusMessage,
                    Status = status,
                    SecretKey = new ToxKey(ToxKeyType.Secret, secretKey)
                });
            }
            catch { return(null); }
        }