private static byte[] preprocess(byte[] bytes, MemoryStream memoryStream, NetWebClient.Encoding encoding, bool encrypt) { NetWebClient.clearStream(memoryStream); if (encoding != NetWebClient.Encoding.Deflate) { if (encoding != NetWebClient.Encoding.GZip) { memoryStream.Write(bytes, 0, bytes.Length); } else { using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) { gzipStream.Write(bytes, 0, bytes.Length); } } } else { uint num = NetWebClient.calcAdler32(bytes.LongLength, bytes); memoryStream.WriteByte(120); memoryStream.WriteByte(156); if (bytes.Length <= 128) { NetWebClient.deflateRawBlock(memoryStream, bytes, 0, bytes.Length); } else { using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, true)) { deflateStream.Write(bytes, 0, bytes.Length); } } memoryStream.WriteByte((byte)(num >> 24 & 255U)); memoryStream.WriteByte((byte)(num >> 16 & 255U)); memoryStream.WriteByte((byte)(num >> 8 & 255U)); memoryStream.WriteByte((byte)(num >> 0 & 255U)); } if (encrypt) { memoryStream.Position = 0L; int num2 = (int)memoryStream.Length; int num3 = Cryptography.calcEncryptedSize(num2); NetWebClient.shared_.resizeTemporary(num3); byte[] tmp_ = NetWebClient.shared_.tmp_; memoryStream.Read(tmp_, 0, num3); Cryptography.padding(num2, tmp_); bytes = new byte[num3]; Cryptography.Instance.encrypt(num3, bytes, num3, tmp_); } else { bytes = memoryStream.ToArray(); } return(bytes); }
private static void readCallback(IAsyncResult asynchronousResult) { NetWebClient netWebClient = asynchronousResult.AsyncState as NetWebClient; try { Stream stream = netWebClient.responseStream_; int num = stream.EndRead(asynchronousResult); if (0 < num) { NetWebClient.shared_.memoryStream_.Write(NetWebClient.shared_.buffer_, 0, num); stream.BeginRead(NetWebClient.shared_.buffer_, 0, 1024, new AsyncCallback(NetWebClient.readCallback), netWebClient); } else if (asynchronousResult.IsCompleted) { byte[] buffer_ = NetWebClient.shared_.buffer_; NetWebClient.shared_.memoryStream_.Position = 0L; if (0 < netWebClient.encryptVersion_) { if (NetWebClient.descryptTo(NetWebClient.shared_.compressedStream_, NetWebClient.shared_.memoryStream_) < 0) { throw new Exception("A message may be corrupted."); } NetWebClient.swap <MemoryStream>(ref NetWebClient.shared_.memoryStream_, ref NetWebClient.shared_.compressedStream_); } NetWebClient.Encoding encoding = netWebClient.encoding_; if (encoding != NetWebClient.Encoding.Deflate) { if (encoding == NetWebClient.Encoding.GZip) { NetWebClient.swap <MemoryStream>(ref NetWebClient.shared_.memoryStream_, ref NetWebClient.shared_.compressedStream_); using (GZipStream gzipStream = new GZipStream(NetWebClient.shared_.compressedStream_, CompressionMode.Decompress, true)) { NetWebClient.copyTo(NetWebClient.shared_.memoryStream_, gzipStream, buffer_, 1024); } } } else { NetWebClient.swap <MemoryStream>(ref NetWebClient.shared_.memoryStream_, ref NetWebClient.shared_.compressedStream_); MemoryStream memoryStream_ = NetWebClient.shared_.memoryStream_; MemoryStream compressedStream_ = NetWebClient.shared_.compressedStream_; compressedStream_.ReadByte(); compressedStream_.ReadByte(); using (DeflateStream deflateStream = new DeflateStream(compressedStream_, CompressionMode.Decompress, true)) { NetWebClient.copyTo(memoryStream_, deflateStream, buffer_, 1024); } if (!NetWebClient.checkHash(compressedStream_, memoryStream_)) { netWebClient.setError(WebExceptionStatus.UnknownError, "Invalid Hash", 5, null); return; } } netWebClient.setSuccess(4); } } catch (WebException ex) { netWebClient.setError(ex.Status, ex.Message, 5, ex.Response as HttpWebResponse); } catch (Exception ex2) { netWebClient.setError(WebExceptionStatus.UnknownError, ex2.Message, 5, null); } }