コード例 #1
0
ファイル: BinaryFormatter.cs プロジェクト: wuhaiying83/EMR
        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
ファイル: Utility.cs プロジェクト: landon/WebGraphs
        public static string Compress(string s)
        {
            var uncompressed = Encoding.Unicode.GetBytes(s);
            var compressed   = QuickLZ.compress(uncompressed);

            return(Convert.ToBase64String(compressed));
        }
コード例 #5
0
        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
ファイル: BinaryFormatter.cs プロジェクト: wuhaiying83/EMR
        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
ファイル: Utility.cs プロジェクト: landon/WebGraphs
        public static string Decompress(string s)
        {
            var compressed   = Convert.FromBase64String(s);
            var uncompressed = QuickLZ.decompress(compressed);

            return(Encoding.Unicode.GetString(uncompressed, 0, uncompressed.Length));
        }
コード例 #8
0
        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
ファイル: CompressedBinaryFile.cs プロジェクト: gbrian/resin
        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
ファイル: TableSerializer.cs プロジェクト: ksec/resin
        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
ファイル: ResourceResolver.cs プロジェクト: kidhudi/de4dot
		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
ファイル: QuickLZ.cs プロジェクト: atom-chen/wudihanghai
 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
ファイル: CompactSerializer.cs プロジェクト: landon/WebGraphs
        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
ファイル: QuickLZ.cs プロジェクト: atom-chen/wudihanghai
 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
ファイル: Serializer.cs プロジェクト: AhmedHamam/resin
        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
ファイル: PostingsWriter.cs プロジェクト: crazyants/resin
        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
ファイル: PaceClient.cs プロジェクト: yakkobr/pace
        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
ファイル: DbDocumentWriter.cs プロジェクト: gbrian/resin
 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);
     }
 }
コード例 #25
0
        public GenericFile(string filePath, bool bCompress)
        {
            OriginalFileData = File.ReadAllBytes(filePath);

            if (bCompress)
            {
                OriginalFileData = QuickLZ.compress(OriginalFileData, 3);
            }

            setEncryptionKey();
        }
コード例 #26
0
ファイル: LzoCompressorStream.cs プロジェクト: radtek/BackO
 /// <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
ファイル: PaceClient.cs プロジェクト: yakkobr/pace
        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
ファイル: QuickLzTests.cs プロジェクト: chrisa23/QuickLZ.net
 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
ファイル: QuickLzTests.cs プロジェクト: chrisa23/QuickLZ.net
 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
ファイル: QuickLzTests.cs プロジェクト: chrisa23/QuickLZ.net
 public void BadLevelTest0()
 {
     var qlz = new QuickLZ(0);
 }
コード例 #34
0
ファイル: QuickLzTests.cs プロジェクト: chrisa23/QuickLZ.net
 public void BadLevelTest4()
 {
     var qlz = new QuickLZ(4);
 }