public MemoryStream ToShell(Stream stream, Dictionary <string, object> context = null) { bool fast = false; if (context != null && context.ContainsKey(FreeMount.PsbZlibFastCompress)) { fast = (bool)context[FreeMount.PsbZlibFastCompress]; } var oriLen = (int)stream.Length; var pos = stream.Position; var compressedStream = ZlibCompress.CompressToStream(stream, fast); MemoryStream ms = new MemoryStream(16 + (int)compressedStream.Length); using (var bw = new BinaryWriter(ms, Encoding.UTF8, true)) { stream.Position = pos; Adler32 checksumer = new Adler32(); checksumer.Update(stream); var checksum = (uint)checksumer.Checksum; bw.Write(Signature); bw.Write((int)compressedStream.Length + 4); bw.Write(oriLen); bw.Write((int)0); compressedStream.CopyTo(ms); bw.WriteBE(checksum); compressedStream.Dispose(); } ms.Position = 0; return(ms); }
public string DownloadBytearr2(string URL, string refer, bool newCookie) { byte[] arrOutput = DownloadBytearr(URL, string.Empty, false); byte[] arrDescrypt = ZlibCompress.DecompressBytes(arrOutput); string outputstr = System.Text.Encoding.UTF8.GetString(arrDescrypt); return(outputstr); }
public string Post_retbyte2(string URL, string refer, string postingdata) { byte[] arrOutput = Post_retbyte(URL, refer, postingdata); byte[] arrDescrypt = ZlibCompress.DecompressBytes(arrOutput); string outputstr = System.Text.Encoding.UTF8.GetString(arrDescrypt); return(outputstr); }
public static string GetDeBase64ZipEncry(string sEnStr) { byte[] bpath = Convert.FromBase64String(sEnStr); bpath = ZlibCompress.DecompressBytes(bpath); string sDeStr = Encoding.Default.GetString(bpath, 0, bpath.Length); DelphiDes des = new DelphiDes(); return(des.DecryStrHex(sDeStr, "uto@+~9%")); }
public static string GetEncryZipBase64(string sDeStr) { DelphiDes des = new DelphiDes(); string sEnStr = des.EncryStrHex(sDeStr, "uto@+~9%"); byte[] bEnStr = new byte[0]; bEnStr = Encoding.Default.GetBytes(sEnStr); bEnStr = ZlibCompress.CompressBytes(bEnStr); return(Convert.ToBase64String(bEnStr)); }
public MemoryStream ToPsb(Stream stream, Dictionary <string, object> context = null) { using (var br = new BinaryReader(stream)) { br.ReadBytes(4); //PSZ var zippedLen = br.ReadInt32(); var oriLen = br.ReadInt32(); br.ReadInt32(); //0 br.ReadByte(); //0x78 var config = br.ReadByte(); //0x9C: fast; 0xDA: compact if (context != null) { context[FreeMount.PsbZlibFastCompress] = config == (byte)0x9C; } return(ZlibCompress.DecompressToStream(stream) as MemoryStream); } }
public bool SendByteArr(byte[] bytearr, ref string retstr) { try { NetworkStream ns = m_tcpClient.GetStream(); if (ns.CanWrite) { ns.Write(bytearr, 0, bytearr.Length); ns.Flush(); byte[] recvarr = ReceiveByteArray(ns); // 获取待解码字节流 int nPos = 4 + 32 + 4;//起始位置 int nTotalLen = (int)(recvarr[0] << 24) + (int)(recvarr[1] << 16) + (int)(recvarr[2] << 8) + (int)(recvarr[3]); int nLen = nTotalLen - 32 - 4; byte[] outputarr = new byte[nLen]; Array.Copy(recvarr, nPos, outputarr, 0, nLen); // 解码 byte[] arrDescrypt = ZlibCompress.DecompressBytes(outputarr); string outputstr = System.Text.Encoding.UTF8.GetString(arrDescrypt); retstr = outputstr; } else { Console.WriteLine("不能写入数据流"); //Console.WriteLine("You cannot write data to this stream."); m_tcpClient.Close(); // Closing the tcpClient instance does not close the network stream. ns.Close(); return(false); } return(true); } catch (Exception ex) { Console.WriteLine("转发数据失败,Reason=" + ex.Message); } return(false); }
/// <summary> /// Save as pure MDF /// </summary> /// <param name="psb"></param> /// <param name="key"></param> /// <returns></returns> public static byte[] SaveAsMdf(this PSB psb, uint?key = null) { psb.Merge(); var bytes = psb.Build(); Adler32 adler = new Adler32(); uint checksum = 0; if (key == null) { adler.Update(bytes); checksum = (uint)adler.Checksum; } MemoryStream ms = new MemoryStream(bytes); using (MemoryStream fs = new MemoryStream()) { if (key != null) { MemoryStream nms = new MemoryStream((int)ms.Length); PsbFile.Encode(key.Value, EncodeMode.Encrypt, EncodePosition.Auto, ms, nms); ms.Dispose(); ms = nms; var pos = ms.Position; adler.Update(ms); checksum = (uint)adler.Checksum; ms.Position = pos; } BinaryWriter bw = new BinaryWriter(fs); bw.WriteStringZeroTrim(MdfFile.Signature); bw.Write((uint)ms.Length); //bw.Write(ZlibCompress.Compress(ms)); ZlibCompress.CompressToBinaryWriter(bw, ms); bw.WriteBE(checksum); ms.Dispose(); bw.Flush(); return(fs.ToArray()); } }
/// <summary> /// Save PSB as MDF file /// </summary> /// <param name="psb"></param> /// <param name="path"></param> /// <param name="key"></param> public static void SaveAsMdfFile(this PSB psb, string path, uint?key = null) { psb.Merge(); var bytes = psb.Build(); Adler32 checksumer = new Adler32(); uint checksum = 0; if (key == null) { checksumer.Update(bytes); checksum = (uint)checksumer.Checksum; } MemoryStream ms = new MemoryStream(bytes); using (Stream fs = new FileStream(path, FileMode.Create)) { if (key != null) { MemoryStream nms = new MemoryStream((int)ms.Length); PsbFile.Encode(key.Value, EncodeMode.Encrypt, EncodePosition.Auto, ms, nms); ms.Dispose(); ms = nms; var pos = ms.Position; checksumer.Update(ms); checksum = (uint)checksumer.Checksum; ms.Position = pos; } BinaryWriter bw = new BinaryWriter(fs); bw.WriteStringZeroTrim(MdfFile.Signature); bw.Write((uint)ms.Length); bw.Write(ZlibCompress.Compress(ms)); bw.WriteBE(checksum); ms.Dispose(); bw.Flush(); } }
public static List <KeyValuePair <string, string> > getZipByteArr(byte[] recvarr) { List <KeyValuePair <string, string> > lst = new List <KeyValuePair <string, string> >(); int index = 0; try { while (index != -1) { int nPos = 4 + 32 + 4 + index;//起始位置 int nTotalLen = (int)(recvarr[index + 0] << 24) + (int)(recvarr[index + 1] << 16) + (int)(recvarr[index + 2] << 8) + (int)(recvarr[index + 3]); int nLen = nTotalLen - 32 - 4; if (nLen > recvarr.Length - index) {// 格式不正确的报文不解析 byte[] temparr = new byte[recvarr.Length - index]; Array.Copy(recvarr, index, temparr, 0, recvarr.Length - index); // 打印 出错字符串 string str = byteToHexStr(temparr); System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding(); //String StringMessage = encoder.GetString(recvarr, index, 40); ConsoleLog.Instance.writeInformationLog("无法解析的报文,字节数组=" + str + ";iindex=" + index); return(lst); } else if (nLen < 0) { break; } byte[] outputarr = new byte[nLen]; Array.Copy(recvarr, nPos, outputarr, 0, nLen); byte[] cmd = new byte[32]; Array.Copy(recvarr, index + 4, cmd, 0, 32); string cmdstr = System.Text.Encoding.Default.GetString(cmd).TrimEnd('\0'); // 将字节流解析为字符串 byte[] arrDescrypt = ZlibCompress.DecompressBytes(outputarr); if (arrDescrypt != null) { string outputstr = System.Text.Encoding.UTF8.GetString(arrDescrypt); lst.Add(new KeyValuePair <string, string>(cmdstr, outputstr)); } if (index + nTotalLen + 4 < recvarr.Length) { index += nTotalLen + 4; } else { index = -1; } } } catch (Exception ex) { // 打印 出错字符串 string str = byteToHexStr(recvarr); ConsoleLog.Instance.writeInformationLog("解析时发生异常,字节数组=" + str + ";index=" + index); Console.WriteLine(ex.Message); } return(lst); }