public static int GetInt(Stream stream) { if (stream.Length != 4) { throw new ArgumentException(); } byte[] buffer = null; try { buffer = _bufferManager.TakeBuffer(4); stream.Read(buffer, 0, 4); return(NetworkConverter.ToInt32(buffer)); } finally { if (buffer != null) { _bufferManager.ReturnBuffer(buffer); } } }
private static ArraySegment <byte>[] FromBuffer(ArraySegment <byte> buffer, BufferManager bufferManager) { List <ArraySegment <byte> > list = new List <ArraySegment <byte> >(); using (MemoryStream memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count)) { byte[] lengthBuffer = new byte[4]; for (; ;) { if (memoryStream.Read(lengthBuffer, 0, 4) < 4) { break; } int length = NetworkConverter.ToInt32(lengthBuffer); byte[] buffer2 = bufferManager.TakeBuffer(length); if (memoryStream.Read(buffer2, 0, length) < length) { break; } list.Add(new ArraySegment <byte>(buffer2, 0, length)); } } return(list.ToArray()); }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager) { using (DeadlockMonitor.Lock(this.ThisLock)) { Encoding encoding = new UTF8Encoding(false); byte[] lengthBuffer = new byte[4]; for (; ;) { if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } int length = NetworkConverter.ToInt32(lengthBuffer); byte id = (byte)stream.ReadByte(); using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.CommandMessage) { this.CommandMessages.Add(CommandMessage.Import(rangeStream, bufferManager)); } } } } }
private static Stream RemoveType(Stream stream, string type) { if (stream == null) { throw new ArgumentNullException("stream"); } Encoding encoding = new UTF8Encoding(false); byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { throw new FormatException(); } int length = NetworkConverter.ToInt32(lengthBuffer); using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { using (StreamReader reader = new StreamReader(rangeStream, encoding)) { if (type != reader.ReadToEnd()) { throw new FormatException(); } } } return(new RangeStream(stream, true)); }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager) { Encoding encoding = new UTF8Encoding(false); byte[] lengthBuffer = new byte[4]; for (; ;) { if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } int length = NetworkConverter.ToInt32(lengthBuffer); byte id = (byte)stream.ReadByte(); using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.Id) { byte[] buffer = new byte[rangeStream.Length]; rangeStream.Read(buffer, 0, buffer.Length); this.Id = buffer; } else if (id == (byte)SerializeId.Name) { using (StreamReader reader = new StreamReader(rangeStream, encoding)) { this.Name = reader.ReadToEnd(); } } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count) { lock (this.ThisLock) { for (; ;) { byte id; { byte[] idBuffer = new byte[1]; if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length) { return; } id = idBuffer[0]; } int length; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } length = NetworkConverter.ToInt32(lengthBuffer); } using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.Key) { this.Keys.Add(Key.Import(rangeStream, bufferManager)); } else if (id == (byte)SerializeId.CorrectionAlgorithm) { this.CorrectionAlgorithm = (CorrectionAlgorithm)Enum.Parse(typeof(CorrectionAlgorithm), ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.InformationLength) { this.InformationLength = ItemUtilities.GetInt(rangeStream); } else if (id == (byte)SerializeId.BlockLength) { this.BlockLength = ItemUtilities.GetInt(rangeStream); } else if (id == (byte)SerializeId.Length) { this.Length = ItemUtilities.GetLong(rangeStream); } } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count) { lock (this.ThisLock) { for (; ;) { byte id; { byte[] idBuffer = new byte[1]; if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length) { return; } id = idBuffer[0]; } int length; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } length = NetworkConverter.ToInt32(lengthBuffer); } using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.KeyExchangeAlgorithm) { this.KeyExchangeAlgorithm = EnumEx <KeyExchangeAlgorithm> .Parse(ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.KeyDerivationAlgorithm) { this.KeyDerivationAlgorithm = EnumEx <KeyDerivationAlgorithm> .Parse(ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.CryptoAlgorithm) { this.CryptoAlgorithm = EnumEx <CryptoAlgorithm> .Parse(ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.HashAlgorithm) { this.HashAlgorithm = EnumEx <HashAlgorithm> .Parse(ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.SessionId) { this.SessionId = ItemUtilities.GetByteArray(rangeStream); } } } } }
private static Stream AddPadding(Stream stream, int size) { if (stream == null) { throw new ArgumentNullException("stream"); } try { byte[] seedBuffer = new byte[4]; _random.GetBytes(seedBuffer); Random random = new Random(NetworkConverter.ToInt32(seedBuffer)); BufferStream metadataStream = new BufferStream(_bufferManager); byte[] lengthBuffer = NetworkConverter.GetBytes((int)stream.Length); metadataStream.Write(lengthBuffer, 0, lengthBuffer.Length); int paddingLength = size - ((int)stream.Length + 4); BufferStream paddingStream = new BufferStream(_bufferManager); { byte[] buffer = null; try { buffer = _bufferManager.TakeBuffer(1024); while (paddingLength > 0) { int writeSize = Math.Min(paddingLength, buffer.Length); random.NextBytes(buffer); paddingStream.Write(buffer, 0, writeSize); paddingLength -= writeSize; } } finally { _bufferManager.ReturnBuffer(buffer); } } return(new UniteStream(metadataStream, new WrapperStream(stream, true), paddingStream)); } catch (Exception e) { throw new ArgumentException(e.Message, e); } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count) { for (; ;) { byte id; { byte[] idBuffer = new byte[1]; if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length) { return; } id = idBuffer[0]; } int length; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } length = NetworkConverter.ToInt32(lengthBuffer); } using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.Tag) { this.Tag = Chat.Import(rangeStream, bufferManager); } else if (id == (byte)SerializeId.CreationTime) { this.CreationTime = DateTime.ParseExact(ItemUtilities.GetString(rangeStream), "yyyy-MM-ddTHH:mm:ssZ", System.Globalization.DateTimeFormatInfo.InvariantInfo).ToUniversalTime(); } else if (id == (byte)SerializeId.FormatType) { this.FormatType = (HypertextFormatType)Enum.Parse(typeof(HypertextFormatType), ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.Hypertext) { this.Hypertext = ItemUtilities.GetString(rangeStream); } else if (id == (byte)SerializeId.Certificate) { this.Certificate = Certificate.Import(rangeStream, bufferManager); } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager) { using (DeadlockMonitor.Lock(this.ThisLock)) { Encoding encoding = new UTF8Encoding(false); byte[] lengthBuffer = new byte[4]; for (; ;) { if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } int length = NetworkConverter.ToInt32(lengthBuffer); byte id = (byte)stream.ReadByte(); using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.Name) { using (StreamReader reader = new StreamReader(rangeStream, encoding)) { this.Name = reader.ReadToEnd(); } } else if (id == (byte)SerializeId.State) { using (StreamReader reader = new StreamReader(rangeStream, encoding)) { this.State = (ChannelState)Enum.Parse(typeof(ChannelState), reader.ReadToEnd()); } } else if (id == (byte)SerializeId.Manager) { byte[] buffer = new byte[rangeStream.Length]; rangeStream.Read(buffer, 0, buffer.Length); this.Managers.Add(buffer); } else if (id == (byte)SerializeId.Member) { byte[] buffer = new byte[rangeStream.Length]; rangeStream.Read(buffer, 0, buffer.Length); this.Members.Add(buffer); } } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count) { lock (this.ThisLock) { for (; ;) { byte id; { byte[] idBuffer = new byte[1]; if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length) { return; } id = idBuffer[0]; } int length; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } length = NetworkConverter.ToInt32(lengthBuffer); } using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.CreationTime) { this.CreationTime = DateTime.ParseExact(ItemUtilities.GetString(rangeStream), "yyyy-MM-ddTHH:mm:ssZ", System.Globalization.DateTimeFormatInfo.InvariantInfo).ToUniversalTime(); } if (id == (byte)SerializeId.ExchangeKey) { this.ExchangeKey = ItemUtilities.GetByteArray(rangeStream); } if (id == (byte)SerializeId.ProtocolHash) { this.ProtocolHash = ItemUtilities.GetByteArray(rangeStream); } else if (id == (byte)SerializeId.Certificate) { this.Certificate = Certificate.Import(rangeStream, bufferManager); } } } } }
public void Test_NetworkConverter() { Assert.IsTrue(NetworkConverter.ToHexString(new byte[] { 0x00, 0x9e, 0x0f }) == "009e0f", "ToHexString"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.FromHexString("1af4b"), new byte[] { 0x01, 0xaf, 0x4b }), "FromHexString"); Assert.IsTrue(NetworkConverter.ToBoolean(new byte[] { 0x01 }), "ToBoolean"); Assert.IsTrue(NetworkConverter.ToChar(new byte[] { 0x00, 0x41 }) == 'A', "ToChar"); Assert.IsTrue(NetworkConverter.ToInt16(new byte[] { 0x74, 0xab }) == 0x74ab, "ToInt16"); Assert.IsTrue(NetworkConverter.ToUInt16(new byte[] { 0x74, 0xab }) == 0x74ab, "ToUInt16"); Assert.IsTrue(NetworkConverter.ToInt32(new byte[] { 0x74, 0xab, 0x05, 0xc1 }) == 0x74ab05c1, "ToInt32"); Assert.IsTrue(NetworkConverter.ToUInt32(new byte[] { 0x74, 0xab, 0x05, 0xc1 }) == 0x74ab05c1, "ToUInt32"); Assert.IsTrue(NetworkConverter.ToInt64(new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }) == 0x74aba5bbf5397b15, "ToInt64"); Assert.IsTrue(NetworkConverter.ToUInt64(new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }) == 0x74aba5bbf5397b15, "ToUInt64"); Assert.IsTrue(NetworkConverter.ToSingle(new byte[] { 0x4a, 0x8a, 0xd0, 0x64 }) == 4548658.0, "ToSingle"); Assert.IsTrue(NetworkConverter.ToDouble(new byte[] { 0x41, 0xb8, 0xa6, 0xb9, 0x83, 0x27, 0x97, 0xa3 }) == 413579651.15465754, "ToDouble"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes(true), new byte[] { 0x01 }), "GetBytes #bool"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((char)'A'), new byte[] { 0x00, 0x41 }), "GetBytes #char"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((short)0x74ab), new byte[] { 0x74, 0xab }), "GetBytes #short"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((ushort)0x74ab), new byte[] { 0x74, 0xab }), "GetBytes #ushort"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((int)0x74ab05c1), new byte[] { 0x74, 0xab, 0x05, 0xc1 }), "GetBytes #int"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((uint)0x74ab05c1), new byte[] { 0x74, 0xab, 0x05, 0xc1 }), "GetBytes #uint"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((long)0x74aba5bbf5397b15), new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }), "GetBytes #long"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((ulong)0x74aba5bbf5397b15), new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }), "GetBytes #ulong"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((float)4548658.0), new byte[] { 0x4a, 0x8a, 0xd0, 0x64 }), "GetBytes #float"); Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((double)413579651.15465754), new byte[] { 0x41, 0xb8, 0xa6, 0xb9, 0x83, 0x27, 0x97, 0xa3 }), "GetBytes #double"); Assert.IsTrue(NetworkConverter.ToInt32(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x74, 0xab, 0x05, 0xc1 }, 4) == 0x74ab05c1, "ToInt32"); for (int i = 0; i < 1024; i++) { byte[] buffer = new byte[_random.Next(0, 128)]; _random.NextBytes(buffer); var s = NetworkConverter.ToBase64UrlString(buffer); Assert.IsTrue(CollectionUtilities.Equals(buffer, NetworkConverter.FromBase64UrlString(s))); } for (int i = 0; i < 1024; i++) { byte[] buffer = new byte[_random.Next(0, 128)]; _random.NextBytes(buffer); var s = NetworkConverter.ToHexString(buffer); Assert.IsTrue(CollectionUtilities.Equals(buffer, NetworkConverter.FromHexString(s))); } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count) { for (; ;) { byte id; { byte[] idBuffer = new byte[1]; if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length) { return; } id = idBuffer[0]; } int length; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } length = NetworkConverter.ToInt32(lengthBuffer); } using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.Nickname) { this.Nickname = ItemUtilities.GetString(rangeStream); } else if (id == (byte)SerializeId.DigitalSignatureAlgorithm) { this.DigitalSignatureAlgorithm = (DigitalSignatureAlgorithm)Enum.Parse(typeof(DigitalSignatureAlgorithm), ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.PublicKey) { this.PublicKey = ItemUtilities.GetByteArray(rangeStream); } else if (id == (byte)SerializeId.PrivateKey) { this.PrivateKey = ItemUtilities.GetByteArray(rangeStream); } } } }
private static Stream RemovePadding(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } try { byte[] lengthBuffer = new byte[4]; stream.Read(lengthBuffer, 0, lengthBuffer.Length); int length = NetworkConverter.ToInt32(lengthBuffer); return(new RangeStream(stream, 4, length, true)); } catch (Exception e) { throw new ArgumentException(e.Message, e); } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count) { for (; ;) { byte id; { byte[] idBuffer = new byte[1]; if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length) { return; } id = idBuffer[0]; } int length; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } length = NetworkConverter.ToInt32(lengthBuffer); } using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.Id) { this.Id = ItemUtilities.GetByteArray(rangeStream); } else if (id == (byte)SerializeId.Uri) { this.ProtectedUris.Add(ItemUtilities.GetString(rangeStream)); } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager) { using (DeadlockMonitor.Lock(this.ThisLock)) { Encoding encoding = new UTF8Encoding(false); byte[] lengthBuffer = new byte[4]; for (; ;) { if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } int length = NetworkConverter.ToInt32(lengthBuffer); byte id = (byte)stream.ReadByte(); using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.Command) { using (StreamReader reader = new StreamReader(rangeStream, encoding)) { this.Command = reader.ReadToEnd(); } } else if (id == (byte)SerializeId.Content) { byte[] buff = new byte[(int)rangeStream.Length]; rangeStream.Read(buff, 0, buff.Length); this.Content = new ArraySegment <byte>(buff, 0, (int)rangeStream.Length); } } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count) { lock (this.ThisLock) { for (; ;) { byte id; { byte[] idBuffer = new byte[1]; if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length) { return; } id = idBuffer[0]; } int length; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } length = NetworkConverter.ToInt32(lengthBuffer); } using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.Box) { this.Boxes.Add(Box.Import(rangeStream, bufferManager)); } } } } }
private static Stream Decrypt(Stream stream, IExchangeDecrypt privateKey) { if (stream == null) { throw new ArgumentNullException("stream"); } if (privateKey == null) { throw new ArgumentNullException("privateKey"); } try { byte type = (byte)stream.ReadByte(); if (type == (byte)ConvertCryptoAlgorithm.Aes256) { byte[] cryptoKey; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { throw new ArgumentException(); } int length = NetworkConverter.ToInt32(lengthBuffer); byte[] encryptedBuffer = new byte[length]; if (stream.Read(encryptedBuffer, 0, encryptedBuffer.Length) != encryptedBuffer.Length) { throw new ArgumentException(); } cryptoKey = Exchange.Decrypt(privateKey, encryptedBuffer); } BufferStream outStream = null; try { outStream = new BufferStream(_bufferManager); using (Stream dataStream = new WrapperStream(stream, true)) { var iv = new byte[32]; dataStream.Read(iv, 0, iv.Length); using (var rijndael = new RijndaelManaged() { KeySize = 256, BlockSize = 256, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }) using (var inStream = new RangeStream(dataStream, dataStream.Position, dataStream.Length - dataStream.Position)) using (CryptoStream cs = new CryptoStream(inStream, rijndael.CreateDecryptor(cryptoKey, iv), CryptoStreamMode.Read)) { byte[] buffer = null; try { buffer = _bufferManager.TakeBuffer(1024 * 4); int i = -1; while ((i = cs.Read(buffer, 0, buffer.Length)) > 0) { outStream.Write(buffer, 0, i); } } finally { _bufferManager.ReturnBuffer(buffer); } } } outStream.Seek(0, SeekOrigin.Begin); } catch (Exception) { if (outStream != null) { outStream.Dispose(); } throw; } return(outStream); } throw new NotSupportedException(); } catch (Exception e) { throw new ArgumentException(e.Message, e); } }
public override Stream Receive(TimeSpan timeout, Information options) { if (_disposed) { throw new ObjectDisposedException(this.GetType().FullName); } if (!_connect) { throw new ConnectionException(); } lock (_receiveLock) { try { _receiveStopwatch.Restart(); Restart :; int length = 0; { byte[] lengthbuffer = new byte[4]; var time = BaseConnection.CheckTimeout(_receiveStopwatch.Elapsed, timeout); time = (time < _receiveTimeSpan) ? time : _receiveTimeSpan; _cap.Receive(lengthbuffer, time); _receivedByteCount.Add(4); length = NetworkConverter.ToInt32(lengthbuffer); } if (length == 0) { Thread.Sleep(100); goto Restart; } else if (length > _maxReceiveCount) { throw new ConnectionException(); } BufferStream bufferStream = null; try { bufferStream = new BufferStream(_bufferManager); byte[] receiveBuffer = null; try { receiveBuffer = _bufferManager.TakeBuffer(1024 * 4); do { int receiveLength = Math.Min(receiveBuffer.Length, length); if (_bandwidthLimit != null) { receiveLength = _bandwidthLimit.GetInBandwidth(this, receiveLength); if (receiveLength < 0) { throw new ConnectionException(); } } var time = BaseConnection.CheckTimeout(_receiveStopwatch.Elapsed, timeout); time = (time < _receiveTimeSpan) ? time : _receiveTimeSpan; _cap.Receive(receiveBuffer, 0, receiveLength, time); _receivedByteCount.Add(receiveLength); bufferStream.Write(receiveBuffer, 0, receiveLength); length -= receiveLength; } while (length > 0); } finally { _bufferManager.ReturnBuffer(receiveBuffer); } } catch (Exception e) { if (bufferStream != null) { bufferStream.Dispose(); } throw e; } bufferStream.Seek(0, SeekOrigin.Begin); return(bufferStream); } catch (ConnectionException e) { throw e; } catch (Exception e) { throw new ConnectionException(e.Message, e); } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count) { for (; ;) { byte id; { byte[] idBuffer = new byte[1]; if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length) { return; } id = idBuffer[0]; } int length; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } length = NetworkConverter.ToInt32(lengthBuffer); } using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.CreationTime) { this.CreationTime = DateTime.ParseExact(ItemUtilities.GetString(rangeStream), "yyyy-MM-ddTHH:mm:ssZ", System.Globalization.DateTimeFormatInfo.InvariantInfo).ToUniversalTime(); } else if (id == (byte)SerializeId.Cost) { this.Cost = ItemUtilities.GetInt(rangeStream); } else if (id == (byte)SerializeId.ExchangePublicKey) { this.ExchangePublicKey = ExchangePublicKey.Import(rangeStream, bufferManager); } else if (id == (byte)SerializeId.TrustSignature) { this.ProtectedTrustSignatures.Add(ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.DeleteSignature) { this.ProtectedDeleteSignatures.Add(ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.Wiki) { this.ProtectedWikis.Add(Wiki.Import(rangeStream, bufferManager)); } else if (id == (byte)SerializeId.Chat) { this.ProtectedChats.Add(Chat.Import(rangeStream, bufferManager)); } else if (id == (byte)SerializeId.Certificate) { this.Certificate = Certificate.Import(rangeStream, bufferManager); } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count) { //if (count > 256) throw new ArgumentException(); lock (this.ThisLock) { Encoding encoding = new UTF8Encoding(false); for (; ;) { byte id; { byte[] idBuffer = new byte[1]; if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length) { return; } id = idBuffer[0]; } int length; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } length = NetworkConverter.ToInt32(lengthBuffer); } using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.Name) { using (StreamReader reader = new StreamReader(rangeStream, encoding)) { this.Name = reader.ReadToEnd(); } } else if (id == (byte)SerializeId.CreationTime) { using (StreamReader reader = new StreamReader(rangeStream, encoding)) { this.CreationTime = DateTime.ParseExact(reader.ReadToEnd(), "yyyy-MM-ddTHH:mm:ssZ", System.Globalization.DateTimeFormatInfo.InvariantInfo).ToUniversalTime(); } } else if (id == (int)SerializeId.Comment) { using (StreamReader reader = new StreamReader(rangeStream, encoding)) { this.Comment = reader.ReadToEnd(); } } else if (id == (byte)SerializeId.Seed) { this.Seeds.Add(Seed.Import(rangeStream, bufferManager)); } else if (id == (byte)SerializeId.D_Box) { this.D_Boxes.Add(T_Box.Import(rangeStream, bufferManager, count + 1)); } else if (id == (byte)SerializeId.Certificate) { this.Certificate = Certificate.Import(rangeStream, bufferManager); } } } } }
protected override void ProtectedImport(Stream stream, BufferManager bufferManager, int count) { lock (this.ThisLock) { for (; ;) { byte id; { byte[] idBuffer = new byte[1]; if (stream.Read(idBuffer, 0, idBuffer.Length) != idBuffer.Length) { return; } id = idBuffer[0]; } int length; { byte[] lengthBuffer = new byte[4]; if (stream.Read(lengthBuffer, 0, lengthBuffer.Length) != lengthBuffer.Length) { return; } length = NetworkConverter.ToInt32(lengthBuffer); } using (RangeStream rangeStream = new RangeStream(stream, stream.Position, length, true)) { if (id == (byte)SerializeId.Name) { this.Name = ItemUtilities.GetString(rangeStream); } else if (id == (byte)SerializeId.Length) { this.Length = ItemUtilities.GetLong(rangeStream); } else if (id == (byte)SerializeId.CreationTime) { this.CreationTime = DateTime.ParseExact(ItemUtilities.GetString(rangeStream), "yyyy-MM-ddTHH:mm:ssZ", System.Globalization.DateTimeFormatInfo.InvariantInfo).ToUniversalTime(); } else if (id == (byte)SerializeId.Comment) { this.Comment = ItemUtilities.GetString(rangeStream); } else if (id == (byte)SerializeId.Rank) { this.Rank = ItemUtilities.GetInt(rangeStream); } else if (id == (byte)SerializeId.Key) { this.Key = Key.Import(rangeStream, bufferManager); } else if (id == (byte)SerializeId.Keyword) { this.Keywords.Add(ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.CompressionAlgorithm) { this.CompressionAlgorithm = (CompressionAlgorithm)Enum.Parse(typeof(CompressionAlgorithm), ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.CryptoAlgorithm) { this.CryptoAlgorithm = (CryptoAlgorithm)Enum.Parse(typeof(CryptoAlgorithm), ItemUtilities.GetString(rangeStream)); } else if (id == (byte)SerializeId.CryptoKey) { this.CryptoKey = ItemUtilities.GetByteArray(rangeStream); } else if (id == (byte)SerializeId.Certificate) { this.Certificate = Certificate.Import(rangeStream, bufferManager); } } } } }