示例#1
0
        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);
        }
示例#2
0
        public Document Get(int docId)
        {
            var bytes        = _db[docId];
            var decompressed = QuickLZ.decompress(bytes);

            return((Document)GraphSerializer.Serializer.Deserialize(new MemoryStream(decompressed)));
        }
示例#3
0
        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);
            }
        }
示例#4
0
        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);
        }
示例#6
0
        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);
        }
示例#7
0
        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));
            }
        }
示例#9
0
        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);
            }
        }
示例#10
0
        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);
            }
        }
示例#11
0
        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));
            }
        }
示例#12
0
        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));
            }
        }
示例#13
0
		public EmbeddedResource mergeResources() {
			if (encryptedResource.Resource == null)
				return null;
			DeobUtils.decryptAndAddResources(module, encryptedResource.Resource.Name, () => {
				return QuickLZ.decompress(encryptedResource.decrypt());
			});
			return encryptedResource.Resource;
		}
示例#14
0
 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]);
 }
示例#15
0
        public static string Serialize(Graph g)
        {
            var bytes      = SerializeToByteArray(g);
            var compressed = QuickLZ.compress(bytes);
            var Ascii85    = new Ascii85();

            return(Prefix + Ascii85.Encode(compressed));
        }
示例#16
0
 static byte[] decompress(byte[] data)
 {
     if (!QuickLZ.isCompressed(data))
     {
         return(data);
     }
     return(QuickLZ.decompress(data));
 }
示例#17
0
 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]);
 }
示例#18
0
        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));
            }
        }
示例#19
0
 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;
 }
示例#20
0
        public byte[] dEncryption(byte[] sourceByteArr)
        {
            //  解密
            byte[] decryptByteArr = this.decryptionByteArr(sourceByteArr, tdes.Key, tdes.IV);
            //  解压
            byte[] unCompressByteArr = QuickLZ.decompress(decryptByteArr);

            return(unCompressByteArr);
        }
示例#21
0
        /// <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);
        }
示例#22
0
        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;
            }
        }
示例#23
0
        public IPacket ReadPacket()
        {
            byte[] packetBytes = QuickLZ.Decompress(ReadData());

            using (var ms = new MemoryStream(packetBytes))
            {
                PacketReceived?.Invoke(this, EventArgs.Empty);
                return((IPacket)serializer.Deserialize(ms));
            }
        }
示例#24
0
 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();
        }
示例#26
0
 /// <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;
     }
 }
示例#27
0
 public EmbeddedResource MergeResources()
 {
     if (encryptedResource.Resource == null)
     {
         return(null);
     }
     DeobUtils.DecryptAndAddResources(module, encryptedResource.Resource.Name.String, () => {
         return(QuickLZ.Decompress(encryptedResource.Decrypt()));
     });
     return(encryptedResource.Resource);
 }
示例#28
0
        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);
            }
        }
示例#29
0
        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);
        }
示例#30
0
 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;
     }
 }
示例#31
0
 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]);
     }
 }
示例#32
0
 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);
     }
 }
示例#33
0
 public void BadLevelTest0()
 {
     var qlz = new QuickLZ(0);
 }
示例#34
0
 public void BadLevelTest4()
 {
     var qlz = new QuickLZ(4);
 }