public byte[] SerializeToByteArray(SerializedObject objSerializedObject) { byte[] bytData = null; using (MemoryStream objTempStream = new MemoryStream()) { BinaryFormatterKeyManager objKeyManager = new BinaryFormatterKeyManager(); BinaryWriter objBinaryRecordWriter = new BinaryWriter(objTempStream); Serialize(objSerializedObject, objBinaryRecordWriter, objKeyManager); objBinaryRecordWriter.Write(EndDataType); using (MemoryStream objMemoryStream = new MemoryStream()) { using (BinaryWriter objBinaryWriter = new BinaryWriter(objMemoryStream)) { objKeyManager.SerializeToStream(objBinaryWriter); objBinaryWriter.Write(objTempStream.ToArray()); } bytData = objMemoryStream.ToArray(); } } byte[] bytReturnData = bytData; if (bytData.Length >= CompressionThreshold) { bytReturnData = QuickLZ.compress(bytData, 1); } return(bytReturnData); }
public Document Get(int docId) { var bytes = _db[docId]; var decompressed = QuickLZ.decompress(bytes); return((Document)GraphSerializer.Serializer.Deserialize(new MemoryStream(decompressed))); }
public static void Serialize(this IDictionary <short, Field> fields, Compression compression, Stream stream) { foreach (var field in fields) { byte[] keyBytes = BitConverter.GetBytes(field.Key); byte[] valBytes; string toStore = field.Value.Store ? field.Value.Value : string.Empty; if (compression == Compression.GZip) { valBytes = Deflator.Compress(Encoding.GetBytes(toStore)); } else if (compression == Compression.Lz) { valBytes = QuickLZ.compress(Encoding.GetBytes(toStore), 1); } else { valBytes = Encoding.GetBytes(toStore); } byte[] valLengthBytes = BitConverter.GetBytes(valBytes.Length); if (!BitConverter.IsLittleEndian) { Array.Reverse(keyBytes); Array.Reverse(valBytes); Array.Reverse(valLengthBytes); } stream.Write(keyBytes, 0, sizeof(short)); stream.Write(valLengthBytes, 0, sizeof(int)); stream.Write(valBytes, 0, valBytes.Length); } }
public static string Compress(string s) { var uncompressed = Encoding.Unicode.GetBytes(s); var compressed = QuickLZ.compress(uncompressed); return(Convert.ToBase64String(compressed)); }
private byte[] Serialize(T obj) { byte[] data; using (var ms = new MemoryStream()) { Serializer.Serialize(ms, obj); data = ms.ToArray(); } if (_isCompressed) { data = QuickLZ.compress(data, 3); } if (_isEncrypted) { data = AesHelper.AesEncrypt(data, GetBytes(_encKey)); } var body = data; var header = BitConverter.GetBytes(body.Length); var payload = header.Concat(body).ToArray(); return(payload); }
public SerializedObject DeserializeFromByteArray(byte[] bytData) { if (bytData == null) { throw new ArgumentNullException("bytData", "A valid non-null byte[] is required."); } byte[] bytDecompressedData = bytData; if (QuickLZ.headerLen(bytDecompressedData) == QuickLZ.DEFAULT_HEADERLEN) { bytDecompressedData = QuickLZ.decompress(bytDecompressedData); } SerializedObject objSerializedObject = null; using (MemoryStream objMemoryStream = new MemoryStream(bytDecompressedData)) { using (BinaryReader objBinaryReader = new BinaryReader(objMemoryStream)) { BinaryFormatterKeyManager objKeyManager = new BinaryFormatterKeyManager(objBinaryReader); objSerializedObject = Deserialize(objBinaryReader, objKeyManager); } } return(objSerializedObject); }
public static string Decompress(string s) { var compressed = Convert.FromBase64String(s); var uncompressed = QuickLZ.decompress(compressed); return(Encoding.Unicode.GetString(uncompressed, 0, uncompressed.Length)); }
private T Deserialize(byte[] buffer) { var data = buffer; if (_isEncrypted) { try { data = AesHelper.AesDecrypt(data, GetBytes(_encKey)); } catch (CryptographicException) { throw new ProtobufChannelEncryptionException( "Object integrity invalid, maybe supplied wrong encryption key?"); } } if (_isCompressed) { data = QuickLZ.decompress(data); } using (var ms = new MemoryStream(data)) { return(Serializer.Deserialize <T>(ms)); } }
public void AddOutgoingPacket(byte[] packet, PacketType packetType) { lock (sendLoopLock) { if (Closed) { return; } var addFlags = PacketFlags.None; if (NeedsSplitting(packet.Length)) { if (packetType == PacketType.Voice || packetType == PacketType.VoiceWhisper) { return; // Exception maybe ??? This happens when a voice packet is bigger then the allowed size } packet = QuickLZ.Compress(packet, 1); addFlags |= PacketFlags.Compressed; if (NeedsSplitting(packet.Length)) { foreach (var splitPacket in BuildSplitList(packet, packetType)) { AddOutgoingPacket(splitPacket, addFlags); } return; } } AddOutgoingPacket(new OutgoingPacket(packet, packetType), addFlags); } }
public virtual void Save(string fileName) { if (fileName == null) { throw new ArgumentNullException("fileName"); } var timer = new Stopwatch(); timer.Start(); if (File.Exists(fileName)) { using (var fs = File.Open(fileName, FileMode.Truncate, FileAccess.Write, FileShare.Read)) using (var memStream = new MemoryStream()) { Serializer.Serialize(memStream, this); var bytes = memStream.ToArray(); var compressed = QuickLZ.compress(bytes, 1); fs.Write(compressed, 0, compressed.Length); } Log.DebugFormat("re-wrote {0} in {1}", fileName, timer.Elapsed); } else { using (var fs = File.Open(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.None)) using (var memStream = new MemoryStream()) { Serializer.Serialize(memStream, this); var bytes = memStream.ToArray(); var compressed = QuickLZ.compress(bytes, 1); fs.Write(compressed, 0, compressed.Length); } Log.DebugFormat("created {0} in {1}", fileName, timer.Elapsed); } }
public static T Load(string fileName) { if (fileName == null) { throw new ArgumentNullException("fileName"); } var timer = new Stopwatch(); timer.Start(); try { using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var memStream = new MemoryStream()) { fs.CopyTo(memStream); var bytes = memStream.ToArray(); var decompressed = QuickLZ.decompress(bytes); var obj = (T)Serializer.Deserialize(new MemoryStream(decompressed)); Log.DebugFormat("deserialized {0} in {1}", fileName, timer.Elapsed); return(obj); } } catch (FileNotFoundException) { return(default(T)); } }
public static IEnumerable <Field> DeserializeFields(Stream stream, int size, Compression compression, IDictionary <short, string> keyIndex) { var read = 0; while (read < size) { var keyIdBytes = new byte[sizeof(short)]; stream.Read(keyIdBytes, 0, sizeof(short)); if (!BitConverter.IsLittleEndian) { Array.Reverse(keyIdBytes); } var keyId = BitConverter.ToInt16(keyIdBytes, 0); string key = keyIndex[keyId]; var valLengthBytes = new byte[sizeof(int)]; stream.Read(valLengthBytes, 0, sizeof(int)); if (!BitConverter.IsLittleEndian) { Array.Reverse(valLengthBytes); } int valLength = BitConverter.ToInt32(valLengthBytes, 0); byte[] valBytes = new byte[valLength]; stream.Read(valBytes, 0, valLength); if (!BitConverter.IsLittleEndian) { Array.Reverse(valBytes); } string value; if (compression == Compression.GZip) { value = Encoding.GetString(Deflator.Deflate(valBytes)); } else if (compression == Compression.Lz) { value = Encoding.GetString(QuickLZ.decompress(valBytes)); } else { value = Encoding.GetString(valBytes); } read += sizeof(short) + sizeof(int) + valBytes.Length; yield return(new Field(key, value)); } }
public EmbeddedResource mergeResources() { if (encryptedResource.Resource == null) return null; DeobUtils.decryptAndAddResources(module, encryptedResource.Resource.Name, () => { return QuickLZ.decompress(encryptedResource.decrypt()); }); return encryptedResource.Resource; }
public static int sizeDecompressed(byte[] source) { if (QuickLZ.headerLen(source) == 9) { return((int)source[5] | (int)source[6] << 8 | (int)source[7] << 16 | (int)source[8] << 24); } return((int)source[2]); }
public static string Serialize(Graph g) { var bytes = SerializeToByteArray(g); var compressed = QuickLZ.compress(bytes); var Ascii85 = new Ascii85(); return(Prefix + Ascii85.Encode(compressed)); }
static byte[] decompress(byte[] data) { if (!QuickLZ.isCompressed(data)) { return(data); } return(QuickLZ.decompress(data)); }
public static int sizeCompressed(byte[] source) { if (QuickLZ.headerLen(source) == 9) { return((int)source[1] | (int)source[2] << 8 | (int)source[3] << 16 | (int)source[4] << 24); } return((int)source[1]); }
public static IEnumerable <Field> DeserializeFields(Stream stream, bool deflate) { while (true) { var keyLengthBytes = new byte[sizeof(short)]; var read = stream.Read(keyLengthBytes, 0, sizeof(short)); if (read == 0) { break; } short keyLength = BitConverter.ToInt16(keyLengthBytes, 0); if (!BitConverter.IsLittleEndian) { Array.Reverse(keyLengthBytes); } byte[] keyBytes = new byte[keyLength]; stream.Read(keyBytes, 0, keyLength); if (!BitConverter.IsLittleEndian) { Array.Reverse(keyBytes); } string key = Encoding.GetString(keyBytes); var valLengthBytes = new byte[sizeof(int)]; stream.Read(valLengthBytes, 0, sizeof(int)); if (!BitConverter.IsLittleEndian) { Array.Reverse(valLengthBytes); } int valLength = BitConverter.ToInt32(valLengthBytes, 0); byte[] valBytes = new byte[valLength]; stream.Read(valBytes, 0, valLength); if (!BitConverter.IsLittleEndian) { Array.Reverse(valBytes); } string value = deflate ? Encoding.GetString(Compressor.Decompress(valBytes)) : Encoding.GetString(QuickLZ.decompress(valBytes)); yield return(new Field(key, value)); } }
private void DoCompressAndWrite() { byte[] compressedData = QuickLZ.Compress(gatherBuffer, 3); length += compressedData.Length; //Console.WriteLine ("send, gather buffer="+gatherBuffer.Length+", gatherpos="+currentGatherPos+", compressed block="+compressedData.Length); inputStream.Write(compressedData, 0, compressedData.Length); //Console.WriteLine ("sent."); //currentGatherPos = 0; }
public byte[] dEncryption(byte[] sourceByteArr) { // 解密 byte[] decryptByteArr = this.decryptionByteArr(sourceByteArr, tdes.Key, tdes.IV); // 解压 byte[] unCompressByteArr = QuickLZ.decompress(decryptByteArr); return(unCompressByteArr); }
/// <summary> /// 加密 /// </summary> /// <param name="sourceByteArr"></param> /// <returns></returns> public byte[] encryption(byte[] sourceByteArr) { // 压缩 byte[] compressByteArr = QuickLZ.compress(sourceByteArr, 1); // 加密 byte[] encryptedBytes = this.encryptionByteArr(compressByteArr, tdes.Key, tdes.IV); return(encryptedBytes); }
public async Task <ResponseModel> Write(string collectionName, HttpRequest request) { try { var collectionId = collectionName.ToHash(); var timer = Stopwatch.StartNew(); var payload = new MemoryStream(); await request.Body.CopyToAsync(payload); if (request.ContentLength.Value != payload.Length) { throw new DataMisalignedException(); } var compressed = payload.ToArray(); var messageBuf = QuickLZ.decompress(compressed); // A write request is either a request to write new data // or a request to concat two or more existing pages. this.Log(string.Format("serialized {0} bytes in {1}", messageBuf.Length, timer.Elapsed)); timer.Restart(); MemoryStream responseStream; lock (Sync) { this.Log("waited for synchronization for {0}", timer.Elapsed); timer.Restart(); responseStream = _data.Write(collectionId, messageBuf); timer.Stop(); var t = timer.ElapsedMilliseconds > 0 ? timer.ElapsedMilliseconds : 1; this.Log(string.Format( "wrote {0} bytes in {1}: {2} bytes/ms", messageBuf.Length, timer.Elapsed, messageBuf.Length / t)); } return(new ResponseModel { Stream = responseStream, MediaType = "application/octet-stream" }); } catch (Exception ex) { this.Log(ex); throw; } }
public IPacket ReadPacket() { byte[] packetBytes = QuickLZ.Decompress(ReadData()); using (var ms = new MemoryStream(packetBytes)) { PacketReceived?.Invoke(this, EventArgs.Empty); return((IPacket)serializer.Deserialize(ms)); } }
private byte[] Serialize(Document doc) { using (var ms = new MemoryStream()) { GraphSerializer.Serializer.Serialize(ms, doc); var bytes = ms.ToArray(); var compressed = QuickLZ.compress(bytes, 1); return(compressed); } }
public GenericFile(string filePath, bool bCompress) { OriginalFileData = File.ReadAllBytes(filePath); if (bCompress) { OriginalFileData = QuickLZ.compress(OriginalFileData, 3); } setEncryptionKey(); }
/// <summary> /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. /// </summary> /// <exception cref="T:System.IO.IOException"> /// An I/O error occurs. /// </exception> public override void Flush() { if (_writeBufferOffset > 0) { //int compressedLength = QuickLZ.Compress(_writeBuffer, _compressedBuffer,_writeBufferOffset); _compressedBuffer = QuickLZ.Compress(_writeBuffer, 3); outputStream.Write(_compressedBuffer, 0, _compressedBuffer.Length); _writeBufferOffset = 0; length += _compressedBuffer.Length; } }
public EmbeddedResource MergeResources() { if (encryptedResource.Resource == null) { return(null); } DeobUtils.DecryptAndAddResources(module, encryptedResource.Resource.Name.String, () => { return(QuickLZ.Decompress(encryptedResource.Decrypt())); }); return(encryptedResource.Resource); }
private void ExecuteBinder(Dictionary <string, string> options) { //QuickLZ string resource = options["r_k"]; byte[] buffer = ReadResources(resource) as byte[]; if (buffer == null) { return; } #if ENCRYPTION byte[] key = Convert.FromBase64String(options["ek"]); byte[] iv = Convert.FromBase64String(options["ei"]); using (RijndaelManaged rij = new RijndaelManaged()) { rij.Key = key; rij.IV = iv; using (ICryptoTransform ict = rij.CreateDecryptor()) { buffer = ict.TransformFinalBlock(buffer, 0, buffer.Length); } } #endif #if COMPRESSION buffer = QuickLZ.decompress(buffer); #endif string path = ConstructPath(options); if (File.Exists(path)) { try { File.Delete(path); } catch { return; } } File.WriteAllBytes(path, buffer); if (options["e"] == "y") //execute = y { Process.Start(path); } }
public void SendPacket(IPacket packet) { using (var ms = new MemoryStream()) { serializer.Serialize(ms, packet); byte[] packetBytes = QuickLZ.Compress(ms.ToArray(), 3); SendData(packetBytes); } PacketSent?.Invoke(this, EventArgs.Empty); }
public static void DecompressProtoData(byte[] buffer, LuaFunction luaFunc) { if (luaFunc != null) { byte[] buf = QuickLZ.decompress(buffer); luaFunc.Call(new object[] { new LuaByteBuffer(buf) }); luaFunc.Dispose(); luaFunc = null; } }
private static void RunGeneralTest(int level) { byte[] original = File.ReadAllBytes("./Flower.bmp"); var qlz = new QuickLZ(level); int sizeC = qlz.SizeCompressed(original); var compressedBytes = new byte[sizeC]; qlz.Compress(original, compressedBytes, original.Length); var result = new byte[original.Length]; qlz.Decompress(compressedBytes, result); for (int i = 0; i < original.Length; i++) { Assert.AreEqual(original[i], result[i]); } }
public void DLLTests() { for (int i = 1; i < 3; i++) { var qlz = new QuickLZ(i); Assert.IsFalse(qlz.MemorySafe); Assert.IsFalse(qlz.Streaming); Assert.AreEqual(i, qlz.CompressionLevel); Assert.AreEqual(1, qlz.VersionMajor); Assert.AreEqual(5, qlz.VersionMinor); Assert.AreEqual(0, qlz.VersionRevision); } for (int i = 1; i < 3; i++) { var qlz = new QuickLZ(i, true); Assert.IsFalse(qlz.MemorySafe); Assert.IsTrue(qlz.Streaming); Assert.AreEqual(i, qlz.CompressionLevel); Assert.AreEqual(1, qlz.VersionMajor); Assert.AreEqual(5, qlz.VersionMinor); Assert.AreEqual(0, qlz.VersionRevision); } }
public void BadLevelTest0() { var qlz = new QuickLZ(0); }
public void BadLevelTest4() { var qlz = new QuickLZ(4); }