예제 #1
0
        /// <inheritdoc/>
        public byte[] Decrypt(byte[] packedCipher, byte[] key)
        {
            if (packedCipher == null)
            {
                throw new ArgumentNullException("packedCipher");
            }

            CryptoHeader header;

            byte[] cipher;
            CryptoHeaderPacker.UnpackHeaderAndCipher(packedCipher, PackageName, out header, out cipher);

            ISymmetricEncryptionAlgorithm decryptor = new SymmetricEncryptionAlgorithmFactory().CreateAlgorithm(header.AlgorithmName);

            try
            {
                byte[] truncatedKey = CryptoUtils.TruncateKey(key, decryptor.ExpectedKeySize);
                byte[] message      = decryptor.Decrypt(cipher, truncatedKey, header.Nonce);

                if (string.Equals(CompressionGzip, header.Compression, StringComparison.InvariantCultureIgnoreCase))
                {
                    message = CompressUtils.Decompress(message);
                }

                return(message);
            }
            catch (Exception ex)
            {
                throw new CryptoDecryptionException("Could not decrypt cipher, probably because the key is wrong.", ex);
            }
        }
예제 #2
0
        public static T Load <T>(Stream input, Type[] extraTypes, string password)
        {
            //using (MemoryStream memoryStream = new MemoryStream())
            //{
            //    bool compress = !string.IsNullOrEmpty(password);
            //    if (compress)
            //    {
            //        CompressUtils.Decompress(input, memoryStream, password);
            //        memoryStream.Seek(0, SeekOrigin.Begin);
            //    }
            //    var rawStream = compress ? memoryStream : input;

            //    XmlSerializer serializer = new XmlSerializer(typeof(T), extraTypes);
            //    return (T)serializer.Deserialize(rawStream);
            //}
            bool compress     = !string.IsNullOrEmpty(password);
            var  tempFilePath = Path.GetTempFileName();

            if (compress)
            {
                using (FileStream output = new FileStream(tempFilePath, FileMode.Open))
                {
                    CompressUtils.Decompress(input, output, password);
                    output.Seek(0, SeekOrigin.Begin);
                }
            }
            Stream        rawStream  = compress ? new FileStream(tempFilePath, FileMode.Open) : input;
            XmlSerializer serializer = new XmlSerializer(typeof(T), extraTypes);

            return((T)serializer.Deserialize(rawStream));
        }
예제 #3
0
        /// <inheritdoc/>
        public byte[] Decrypt(byte[] packedCipher, SecureString password)
        {
            if (packedCipher == null)
            {
                throw new ArgumentNullException("packedCipher");
            }

            CryptoHeader header;

            byte[] cipher;
            CryptoHeaderPacker.UnpackHeaderAndCipher(packedCipher, PackageName, out header, out cipher);

            ISymmetricEncryptionAlgorithm decryptor = new SymmetricEncryptionAlgorithmFactory().CreateAlgorithm(header.AlgorithmName);
            IKeyDerivationFunction        kdf       = new KeyDerivationFactory().CreateKdf(header.KdfName);

            try
            {
                int    cost    = int.Parse(header.Cost);
                byte[] key     = kdf.DeriveKeyFromPassword(password, decryptor.ExpectedKeySize, header.Salt, cost);
                byte[] message = decryptor.Decrypt(cipher, key, header.Nonce);

                if (string.Equals(CompressionGzip, header.Compression, StringComparison.InvariantCultureIgnoreCase))
                {
                    message = CompressUtils.Decompress(message);
                }

                return(message);
            }
            catch (Exception ex)
            {
                throw new CryptoDecryptionException("Could not decrypt cipher, probably because the key is wrong.", ex);
            }
        }
예제 #4
0
 private void ReadWorker()
 {
     while (_running)
     {
         try
         {
             var messages = CommunicationHub.Net.Hub.Get();
             foreach (var message in messages)
             {
                 try
                 {
                     OnMessage?.Invoke(this, CompressUtils.DeflateDecompress(message).ToArray());
                 }
                 catch (Exception e)
                 {
                     Logger.LogError($"Error occured: {e}");
                 }
             }
             if (messages.Length == 0)
             {
                 Thread.Sleep(TimeSpan.FromMilliseconds(100));
             }
         }
         catch (Exception e)
         {
             Logger.LogError($"Error occured: {e}");
         }
     }
 }
예제 #5
0
        private void OnTemperatureCallback(float[] data)
        {
            if ((temperature == null) || (temperature.Length != data.Length))
            {
                temperature = new byte[data.Length * 4];
            }

            Buffer.BlockCopy(data, 0, temperature, 0, temperature.Length);

            var userData = new UserData()
            {
                temperature     = Convert.ToBase64String(temperature),
                selections      = cell.GetAllSelectionInfo(),
                groupSelections = cell.GetAllSelectionGroupInfo()
            };
            var content = CompressUtils.Compress(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(userData)));
            var header  = new byte[] { 0xAA, 0xBB, 0xCC, 0xDD };
            var length  = BitConverter.GetBytes(content.Length);

            var sei = new byte[header.Length + length.Length + content.Length];

            Buffer.BlockCopy(header, 0, sei, 0, header.Length);
            Buffer.BlockCopy(length, 0, sei, header.Length, length.Length);
            Buffer.BlockCopy(content, 0, sei, header.Length + length.Length, content.Length);
            encoder.EncodeSEI(sei);
        }
예제 #6
0
        /// <inheritdoc/>
        public byte[] Encrypt(
            byte[] message,
            SecureString password,
            KeyDerivationCostType costType,
            string encryptorName,
            string kdfName     = Pbkdf2.CryptoKdfName,
            string compression = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            ValidatePassword(password);
            if (_randomSource == null)
            {
                throw new ArgumentNullException(nameof(_randomSource));
            }
            if (string.IsNullOrWhiteSpace(encryptorName))
            {
                encryptorName = BouncyCastleXChaCha20.CryptoAlgorithmName;
            }
            if (string.IsNullOrWhiteSpace(kdfName))
            {
                encryptorName = Pbkdf2.CryptoKdfName;
            }
            ISymmetricEncryptionAlgorithm encryptor = new SymmetricEncryptionAlgorithmFactory().CreateAlgorithm(encryptorName);
            IKeyDerivationFunction        kdf       = new KeyDerivationFactory().CreateKdf(kdfName);

            // Prepare header
            CryptoHeader header = new CryptoHeader();

            header.PackageName   = PackageName;
            header.AlgorithmName = encryptor.Name;
            header.Nonce         = _randomSource.GetRandomBytes(encryptor.ExpectedNonceSize);
            header.KdfName       = kdf.Name;
            header.Salt          = _randomSource.GetRandomBytes(kdf.ExpectedSaltSizeBytes);
            int cost = kdf.RecommendedCost(costType);

            header.Cost        = cost.ToString();
            header.Compression = compression;

            try
            {
                if (string.Equals(CompressionGzip, header.Compression, StringComparison.InvariantCultureIgnoreCase))
                {
                    message = CompressUtils.Compress(message);
                }

                byte[] key    = kdf.DeriveKeyFromPassword(password, encryptor.ExpectedKeySize, header.Salt, cost);
                byte[] cipher = encryptor.Encrypt(message, key, header.Nonce);
                return(CryptoHeaderPacker.PackHeaderAndCypher(header, cipher));
            }
            catch (Exception ex)
            {
                throw new CryptoException("An unexpected error occured, while encrypting the message.", ex);
            }
        }
예제 #7
0
        internal void LocalFileHeaderFake(ulong filePosition, ulong uncompressedSize, ulong compressedSize, byte[] crc32, MemoryStream ms)
        {
            using BinaryWriter bw       = new(ms, Encoding.UTF8, true);
            RelativeOffsetOfLocalHeader = filePosition;
            SetStatus(LocalFileStatus.TrrntZip);
            UncompressedSize = uncompressedSize;
            _compressedSize  = compressedSize;
            CRC = crc32;

            ZipExtraFieldWrite zefw = new();

            SetStatus(LocalFileStatus.Zip64,
                      zefw.Zip64(UncompressedSize, _compressedSize, RelativeOffsetOfLocalHeader, false,
                                 out uint headerUnCompressedSize, out uint headerCompressedSize, out uint headerRelativeOffsetOfLocalHeader)
                      );
            _extraField = zefw.ExtraField;

            byte[] bFileName;
            if (CompressUtils.IsCodePage437(Filename))
            {
                bFileName = CompressUtils.GetBytes(Filename);
            }
            else
            {
                GeneralPurposeBitFlag |= 1 << 11;
                bFileName              = Encoding.UTF8.GetBytes(Filename);
            }

            ushort versionNeededToExtract = (ushort)(GetStatus(LocalFileStatus.Zip64) ? 45 : 20);

            bw.Write(LocalFileHeaderSignature);
            bw.Write(versionNeededToExtract);
            bw.Write(GeneralPurposeBitFlag);
            bw.Write(_compressionMethod);

            CompressUtils.UtcTicksToDosDateTime(HeaderLastModified, out ushort lastModFileDate, out ushort lastModFileTime);
            bw.Write(lastModFileTime);
            bw.Write(lastModFileDate);

            WriteCRC(bw, CRC);
            bw.Write(headerCompressedSize);
            bw.Write(headerUnCompressedSize);

            ushort fileNameLength = (ushort)bFileName.Length;

            bw.Write(fileNameLength);

            ushort extraFieldLength = (ushort)_extraField.Length;

            bw.Write(extraFieldLength);

            bw.Write(bFileName, 0, fileNameLength);

            bw.Write(_extraField);
        }
예제 #8
0
        private void LocalFileHeaderWrite(Stream zipFs)
        {
            using BinaryWriter bw = new(zipFs, Encoding.UTF8, true);
            ZipExtraFieldWrite zefw = new();
            bool zip64 = zefw.Zip64(UncompressedSize, _compressedSize, RelativeOffsetOfLocalHeader, false,
                                    out uint headerUnCompressedSize, out uint headerCompressedSize,
                                    out uint headerRelativeOffsetOfLocalHeader);

            _extraField = zefw.ExtraField;

            SetStatus(LocalFileStatus.Zip64, zip64);

            byte[] bFileName;
            if (CompressUtils.IsCodePage437(Filename))
            {
                bFileName = CompressUtils.GetBytes(Filename);
            }
            else
            {
                GeneralPurposeBitFlag |= 1 << 11;
                bFileName              = Encoding.UTF8.GetBytes(Filename);
            }

            ushort versionNeededToExtract = (ushort)(GetStatus(LocalFileStatus.Zip64) ? 45 : 20);

            RelativeOffsetOfLocalHeader = (ulong)zipFs.Position;
            bw.Write(LocalFileHeaderSignature);
            bw.Write(versionNeededToExtract);
            bw.Write(GeneralPurposeBitFlag);
            bw.Write(_compressionMethod);

            CompressUtils.UtcTicksToDosDateTime(HeaderLastModified, out ushort lastModFileDate, out ushort lastModFileTime);
            bw.Write(lastModFileTime);
            bw.Write(lastModFileDate);

            // these 3 values will be set correctly after the file data has been written
            bw.Write(0xffffffff); // crc
            bw.Write(0xffffffff); // CompressedSize 32bit
            bw.Write(0xffffffff); // UncompressedSie 32bit

            ushort fileNameLength = (ushort)bFileName.Length;

            bw.Write(fileNameLength);

            ushort extraFieldLength = (ushort)_extraField.Length;

            bw.Write(extraFieldLength);

            bw.Write(bFileName, 0, fileNameLength);

            _extraLocation = (ulong)zipFs.Position;
            bw.Write(_extraField);
        }
예제 #9
0
        public void NtfsTime(long mTime, long aTime, long cTime)
        {
            _extraField.AddRange(BitConverter.GetBytes((ushort)0x000a));
            _extraField.AddRange(BitConverter.GetBytes((ushort)32));     // this block is 32 bytes long
            _extraField.AddRange(BitConverter.GetBytes((uint)0));        // Reserved
            _extraField.AddRange(BitConverter.GetBytes((ushort)0x0001)); // tag1 = 1
            _extraField.AddRange(BitConverter.GetBytes((ushort)24));     // size1  block size of date/times

            _extraField.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToNtfsDateTime(mTime)));
            _extraField.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToNtfsDateTime(aTime)));
            _extraField.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToNtfsDateTime(cTime)));
        }
예제 #10
0
        internal void CentralDirectoryWrite(Stream crcStream)
        {
            using BinaryWriter bw = new(crcStream, Encoding.UTF8, true);

            ZipExtraFieldWrite zefw = new();

            SetStatus(LocalFileStatus.Zip64,
                      zefw.Zip64(UncompressedSize, _compressedSize, RelativeOffsetOfLocalHeader, true,
                                 out uint headerUnCompressedSize, out uint headerCompressedSize, out uint headerRelativeOffsetOfLocalHeader)
                      );
            _extraField = zefw.ExtraField;

            ushort extraFieldLength = (ushort)_extraField.Length;

            byte[] bFileName;
            if (CompressUtils.IsCodePage437(Filename))
            {
                bFileName = CompressUtils.GetBytes(Filename);
            }
            else
            {
                GeneralPurposeBitFlag |= 1 << 11;
                bFileName              = Encoding.UTF8.GetBytes(Filename);
            }

            ushort fileNameLength = (ushort)bFileName.Length;

            ushort versionNeededToExtract = (ushort)(GetStatus(LocalFileStatus.Zip64) ? 45 : 20);

            CompressUtils.UtcTicksToDosDateTime(HeaderLastModified, out ushort lastModFileDate, out ushort lastModFileTime);

            bw.Write(CentralDirectoryHeaderSignature);
            bw.Write((ushort)0);
            bw.Write(versionNeededToExtract);
            bw.Write(GeneralPurposeBitFlag);
            bw.Write(_compressionMethod);
            bw.Write(lastModFileTime);
            bw.Write(lastModFileDate);
            WriteCRC(bw, CRC);
            bw.Write(headerCompressedSize);
            bw.Write(headerUnCompressedSize);
            bw.Write(fileNameLength);
            bw.Write(extraFieldLength);
            bw.Write((ushort)0); // file comment length
            bw.Write((ushort)0); // disk number start
            bw.Write((ushort)0); // internal file attributes
            bw.Write((uint)0);   // external file attributes
            bw.Write(headerRelativeOffsetOfLocalHeader);
            bw.Write(bFileName, 0, fileNameLength);
            bw.Write(_extraField, 0, extraFieldLength);
            // No File Comment
        }
예제 #11
0
        public void Test_CompressDecompress()
        {
            var data = Enumerable.Range(0, 10000).SelectMany(_ => "0xdeadbeef".HexToBytes()).ToArray();

            Console.WriteLine($"{data.Length}: {data.ToHex()}");
            var c = CompressUtils.DeflateCompress(data).ToArray();

            Console.WriteLine($"{c.Length}: {c.ToHex()}");
            var restored = CompressUtils.DeflateDecompress(c).ToArray();

            Console.WriteLine($"{restored.ToHex()}");
            Assert.AreEqual(data, restored);
        }
예제 #12
0
        public LocalFile GetLocalFile(int i)
        {
            LocalFile lf = new()
            {
                Filename         = Path.GetFileName(ZipFilename),
                UncompressedSize = UnCompressedSize,
                CRC          = this.CRC,
                IsDirectory  = false,
                ModifiedTime = MTime == 0 ? null : (long?)CompressUtils.UtcTicksFromUnixDateTime((int)MTime)
            };

            return(lf);
        }
예제 #13
0
        public void CompressAndDecompressReturnsOriginalData()
        {
            ICryptoRandomService randomGenerator = CommonMocksAndStubs.CryptoRandomService();
            int byteCount = 1;

            while (byteCount < 1000)
            {
                byte[] data             = randomGenerator.GetRandomBytes(byteCount);
                byte[] compressedData   = CompressUtils.Compress(data);
                byte[] decompressedData = CompressUtils.Decompress(compressedData);

                Assert.AreEqual(data, decompressedData);
                byteCount = byteCount * 2;
            }
        }
예제 #14
0
        /// <inheritdoc/>
        public byte[] Encrypt(
            byte[] message,
            byte[] key,
            string encryptorName,
            string compression = null)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (_randomSource == null)
            {
                throw new ArgumentNullException(nameof(_randomSource));
            }
            if (string.IsNullOrWhiteSpace(encryptorName))
            {
                encryptorName = BouncyCastleXChaCha20.CryptoAlgorithmName;
            }
            ISymmetricEncryptionAlgorithm encryptor = new SymmetricEncryptionAlgorithmFactory().CreateAlgorithm(encryptorName);

            // Prepare header
            CryptoHeader header = new CryptoHeader();

            header.PackageName   = PackageName;
            header.AlgorithmName = encryptor.Name;
            header.Nonce         = _randomSource.GetRandomBytes(encryptor.ExpectedNonceSize);
            header.Compression   = compression;

            try
            {
                if (string.Equals(CompressionGzip, header.Compression, StringComparison.InvariantCultureIgnoreCase))
                {
                    message = CompressUtils.Compress(message);
                }

                byte[] truncatedKey = CryptoUtils.TruncateKey(key, encryptor.ExpectedKeySize);
                byte[] cipher       = encryptor.Encrypt(message, truncatedKey, header.Nonce);
                return(CryptoHeaderPacker.PackHeaderAndCypher(header, cipher));
            }
            catch (Exception ex)
            {
                throw new CryptoException("An unexpected error occured, while encrypting the message.", ex);
            }
        }
예제 #15
0
        public void ZipFileCloseFake(ulong fileOffset, out byte[] centralDir)
        {
            centralDir = null;
            if (ZipOpen != ZipOpenType.OpenFakeWrite)
            {
                return;
            }

            bool lTrrntzip = true;

            _zipFs = new MemoryStream();

            _centralDirStart = fileOffset;


            CrcCalculatorStream crcCs = new(_zipFs, true);

            foreach (ZipLocalFile t in _localFiles)
            {
                t.CentralDirectoryWrite(crcCs);
                lTrrntzip &= t.GetStatus(LocalFileStatus.TrrntZip);
            }

            crcCs.Flush();
            crcCs.Close();

            _centralDirSize = (ulong)_zipFs.Position;

            FileComment = lTrrntzip ? CompressUtils.GetBytes("TORRENTZIPPED-" + crcCs.Crc.ToString("X8")) : new byte[0];
            ZipStatus   = lTrrntzip ? ZipStatus.TrrntZip : ZipStatus.None;

            crcCs.Dispose();

            _zip64 = (_centralDirStart >= 0xffffffff) || (_centralDirSize >= 0xffffffff) || (_localFiles.Count >= 0xffff);

            if (_zip64)
            {
                _endOfCentralDir64 = fileOffset + (ulong)_zipFs.Position;
                Zip64EndOfCentralDirWrite();
                Zip64EndOfCentralDirectoryLocatorWrite();
            }
            EndOfCentralDirWrite();

            centralDir = ((MemoryStream)_zipFs).ToArray();
            _zipFs.Close();
            _zipFs.Dispose();
            ZipOpen = ZipOpenType.Closed;
        }
예제 #16
0
        public void CompressHandlesNullData()
        {
            // Null data
            byte[] data           = null;
            byte[] compressedData = CompressUtils.Compress(data);
            Assert.IsNull(compressedData);

            // Empty data
            data           = new byte[0];
            compressedData = CompressUtils.Compress(data);
            byte[] decompressedData = CompressUtils.Decompress(compressedData);
            Assert.IsNotNull(compressedData);
            Assert.AreEqual(0, compressedData.Length);
            Assert.IsNotNull(decompressedData);
            Assert.AreEqual(data, decompressedData);
        }
예제 #17
0
        private void Deploy(string targetPath)
        {
            try
            {
                var progressMAX  = Assets.GetTargetBundles.Count;
                var progressDone = 0;

                void Progress()
                {
                    EditorUtility.DisplayProgressBar("Compressing...", "Compressing and deploying zipmod", progressDone / (float)progressMAX);
                }

                Progress();
                foreach (var targetName in Assets.GetTargetBundles)
                {
                    var targetFolder      = TempZipFolder(targetName);
                    var targetModFileName = GetSplitTargetName(targetName);
                    if (targetModFileName.IsNullOrEmpty())
                    {
                        throw new Exception("Output file name cannot be empty.");
                    }

                    // Props to 2155X for giving me new multi-platform method!
                    var from = targetFolder;
                    var to   = Path.Combine(targetPath, $"{targetModFileName}.zipmod").ToUnixPath();
                    if (File.Exists(to))
                    {
                        File.Delete(to);
                    }

                    CompressUtils.CreateFromDirectory(from, to);
                    if (CleanTempFolderAfterBuild)
                    {
                        Directory.Delete(from, true);
                    }

                    Debug.Log($"Successfully packaged mod {targetModFileName}.zipmod!");
                    progressDone++;
                    Progress();
                }
            }
            catch (Exception)
            {
                EditorUtility.ClearProgressBar();
                throw;
            }
        }
예제 #18
0
        /// <summary>
        /// 保存对象
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="obj">对象</param>
        /// <param name="extraTypes">额外的类型,若对象中存在自定义类型的对象,则必须将这些对象输入</param>
        public static void Save(string path, ICanSerialize obj, Type[] extraTypes, string password)
        {
            //using (FileStream outputStream = new FileStream(path,FileMode.Create))
            //{
            //    using (MemoryStream memoryStream = new MemoryStream())
            //    {
            //        XmlSerializer serializer = XmlSerializerFactory.Instance.Create(obj.GetType(), extraTypes);//new XmlSerializer(obj.GetType(), extraTypes);
            //        serializer.Serialize(memoryStream, obj);

            //        memoryStream.Seek(0, SeekOrigin.Begin);
            //        if (!string.IsNullOrEmpty(password))
            //        {

            //            CompressUtils.Compress(memoryStream, outputStream, password);
            //        }
            //        else
            //        {
            //            memoryStream.WriteTo(outputStream);
            //        }


            //    }


            //}
            var tempFilePath = Path.GetTempFileName();

            using (var tempFileWriter = new StreamWriter(tempFilePath, false, Encoding.UTF8))
            {
                XmlSerializer serializer = XmlSerializerFactory.Instance.Create(obj.GetType(), extraTypes);
                serializer.Serialize(tempFileWriter, obj);
            }
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            if (!string.IsNullOrEmpty(password))
            {
                CompressUtils.Compress(tempFilePath, path, password);
                File.Delete(tempFilePath);
            }
            else
            {
                File.Move(tempFilePath, path);
            }
        }
예제 #19
0
        private void btnAUTODecompress_Click(object sender, EventArgs e)
        {
            const string   LOG_TAG = "AUTO Decompress: ";
            OpenFileDialog ofd     = new OpenFileDialog();

            ofd.AddExtension    = true;
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.ShowReadOnly    = true;
            ofd.Title           = "Open File...";
            ofd.Multiselect     = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string fileName = ofd.FileName;
                if (fileName != null && fileName != "")
                {
                    FileInfo fInfo = new FileInfo(fileName);
                    this.writeToConsole(LOG_TAG + "Opening: " + fInfo.FullName);
                    using (FileStream instream = new FileStream(fInfo.FullName, FileMode.Open, FileAccess.Read))
                    {
                        this.writeToConsole(LOG_TAG + "Reading file...");
                        byte[] input = new byte[instream.Length];
                        instream.Read(input, 0, input.Length);
                        this.writeToConsole(LOG_TAG + "Decompressing...");
                        CompressType usedType;
                        byte[]       output = CompressUtils.decompress_auto(input, out usedType);
                        if (usedType != CompressType.NULL)
                        {
                            this.writeToConsole("Detected Compression: " + usedType.ToString());
                            using (FileStream outstream = new FileStream(fInfo.FullName + ".de" + usedType.ToString(), FileMode.Create, FileAccess.Write))
                            {
                                this.writeToConsole(LOG_TAG + "Writing file: " + fInfo.FullName + ".de" + usedType.ToString());
                                outstream.Write(output, 0, output.Length);
                            }
                            this.writeToConsole(LOG_TAG + "Done!");
                        }
                        else
                        {
                            this.writeToConsole("Failed to detect valid compression format!");
                        }
                    }
                }
            }
        }
예제 #20
0
        private void zipFileCloseWrite()
        {
            bool lTrrntzip = true;

            _centralDirStart = (ulong)_zipFs.Position;

            using (CrcCalculatorStream crcCs = new CrcCalculatorStream(_zipFs, true))
            {
                foreach (ZipLocalFile t in _localFiles)
                {
                    t.CentralDirectoryWrite(crcCs);
                    lTrrntzip &= t.GetStatus(LocalFileStatus.TrrntZip);
                }

                crcCs.Flush();
                crcCs.Close();

                _centralDirSize = (ulong)_zipFs.Position - _centralDirStart;

                FileComment = lTrrntzip ? CompressUtils.GetBytes("TORRENTZIPPED-" + crcCs.Crc.ToString("X8")) : new byte[0];
                ZipStatus   = lTrrntzip ? ZipStatus.TrrntZip : ZipStatus.None;
            }

            _zip64  = false;
            _zip64 |= _centralDirStart >= 0xffffffff;
            _zip64 |= _centralDirSize >= 0xffffffff;
            _zip64 |= _localFiles.Count >= 0xffff;

            if (_zip64)
            {
                _endOfCentralDir64 = (ulong)_zipFs.Position;
                Zip64EndOfCentralDirWrite();
                Zip64EndOfCentralDirectoryLocatorWrite();
            }
            EndOfCentralDirWrite();

            _zipFs.SetLength(_zipFs.Position);
            _zipFs.Flush();
            _zipFs.Close();
            _zipFs.Dispose();
            _zipFileInfo = new FileInfo(_zipFileInfo.FullName);
            ZipOpen      = ZipOpenType.Closed;
        }
예제 #21
0
        static void PrintHelp()
        {
            PrintVersion();
            System.Console.WriteLine(Encoding.UTF8.GetString(CompressUtils.Decompress(art_console)));
            System.Console.WriteLine($"Copyright (C) 2020. Commnunity Crawler Developer");
            System.Console.WriteLine($"E-Mail: [email protected]");
            System.Console.WriteLine($"Source-code: https://github.com/rollrat/com_crawler");
            System.Console.WriteLine($"");
            System.Console.WriteLine("Usage: ./com_crawler.Console [OPTIONS...] <URL> [URL OPTIONS ...]");

            var builder = new StringBuilder();

            CommandLineParser.GetFields(typeof(Options)).ToList().ForEach(
                x =>
            {
                var key = x.Key;
                if (!key.StartsWith("--"))
                {
                    return;
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.ShortOption))
                {
                    key = $"{x.Value.Item2.ShortOption}, " + key;
                }
                var help = "";
                if (!string.IsNullOrEmpty(x.Value.Item2.Help))
                {
                    help = $"[{x.Value.Item2.Help}]";
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.Info))
                {
                    builder.Append($"   {key}".PadRight(30) + $" {x.Value.Item2.Info} {help}\r\n");
                }
                else
                {
                    builder.Append($"   {key}".PadRight(30) + $" {help}\r\n");
                }
            });
            System.Console.Write(builder.ToString());

            System.Console.WriteLine($"");
            System.Console.WriteLine("Enter './com_crawler.Console --list-extractor' to get more url options.");
        }
예제 #22
0
        public ZipReturn ZipFileCreate(string newFilename)
        {
            if (ZipOpen != ZipOpenType.Closed)
            {
                return(ZipReturn.ZipFileAlreadyOpen);
            }

            CompressUtils.CreateDirForFile(newFilename);
            _fileInfo = new FileInfo(newFilename);

            int errorCode = FileStream.OpenFileWrite(newFilename, out _inStream);

            if (errorCode != 0)
            {
                ZipFileClose();
                return(ZipReturn.ZipErrorOpeningFile);
            }
            ZipOpen = ZipOpenType.OpenWrite;
            return(ZipReturn.ZipGood);
        }
예제 #23
0
        static void PrintHelp()
        {
            PrintVersion();
            Console.WriteLine(Encoding.UTF8.GetString(CompressUtils.Decompress(art_console)));
            Console.WriteLine($"Copyright (C) 2020. Inha Univ AlarmBot(Notification Server) Project.");
            Console.WriteLine($"E-Mail: [email protected]");
            Console.WriteLine($"Source-code: https://github.com/rollrat/inha-alarm");
            Console.WriteLine($"Source-code: https://github.com/rollrat/INHANoti");
            Console.WriteLine($"");
            Console.WriteLine("Usage: ./NotiServer [OPTIONS...]");

            var builder = new StringBuilder();

            CommandLineParser.GetFields(typeof(Options)).ToList().ForEach(
                x =>
            {
                var key = x.Key;
                if (!key.StartsWith("--"))
                {
                    return;
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.ShortOption))
                {
                    key = $"{x.Value.Item2.ShortOption}, " + key;
                }
                var help = "";
                if (!string.IsNullOrEmpty(x.Value.Item2.Help))
                {
                    help = $"[{x.Value.Item2.Help}]";
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.Info))
                {
                    builder.Append($"   {key}".PadRight(30) + $" {x.Value.Item2.Info} {help}\r\n");
                }
                else
                {
                    builder.Append($"   {key}".PadRight(30) + $" {help}\r\n");
                }
            });
            Console.Write(builder.ToString());
        }
예제 #24
0
        protected GetRecordsResult DecorateRecords(GetRecordsResult result)
        {
            //解压
            if (_disConfig.IsDataCompressEnabled())
            {
                if (result.Records != null)
                {
                    for (int i = 0; i < result.Records.Count; i++)
                    {
                        byte[] input = result.Records[i].Data;
                        try
                        {
                            byte[] uncompressedInput = CompressUtils.Decompress(input);
                            result.Records[i].Data = uncompressedInput;
                        }
                        catch (IOException e)
                        {
                            logger.Error(e.Message, e);
                            throw new Exception(e.Message);
                        }
                    }
                }
            }

            //解密
            if (IsEncrypt())
            {
                List <Record> records = result.Records;
                if (records != null)
                {
                    foreach (var record in records)
                    {
                        record.Data = Decrypt(record.Data);
                    }
                }
            }

            return(result);
        }
예제 #25
0
        //public PutRecordResult PutRecord(PutRecordRequest putRecordParam)
        //{
        //    ObsWebServiceRequest obsWebServiceRequest = new DISWebServiceRequest();
        //    OBS.Runtime.Internal.IRequest requestobs = new DISDefaultRequest(obsWebServiceRequest, Constants.SERVICENAME)
        //    {
        //        HttpMethod = HttpMethodName.POST.ToString()
        //    };

        //    string resourcePath = ResourcePathBuilder.Standard()
        //        .WithProjectId(_disConfig.GetProjectId())
        //        .WithResource(new RecordResource(null))
        //        .Build();
        //    requestobs.ResourcePath = resourcePath;
        //    var results = Request<PutRecordResult>(putRecordParam, requestobs);

        //    return results;
        //}

        protected PutRecordsRequest DecorateRecords(PutRecordsRequest putRecordsParam)
        {
            //加密
            if (IsEncrypt())
            {
                if (putRecordsParam.Records != null)
                {
                    for (int i = 0; i < putRecordsParam.Records.Count; i++)
                    {
                        putRecordsParam.Records[i].Data = Encrypt(new MemoryStream(putRecordsParam.Records[i].Data));
                    }
                }
            }

            //压缩
            if (_disConfig.IsDataCompressEnabled())
            {
                if (putRecordsParam.Records != null)
                {
                    for (int i = 0; i < putRecordsParam.Records.Count; i++)
                    {
                        byte[] input = putRecordsParam.Records[i].Data;
                        try
                        {
                            byte[] compressedInput = CompressUtils.Compress(input);
                            putRecordsParam.Records[i].Data = compressedInput;
                        }
                        catch (IOException e)
                        {
                            logger.Error(e.Message, e);
                            throw new Exception(e.Message);
                        }
                    }
                }
            }

            return(putRecordsParam);
        }
예제 #26
0
        static void PrintHelp()
        {
            PrintVersion();
            Console.WriteLine(Encoding.UTF8.GetString(CompressUtils.Decompress(art_console)));
            Console.WriteLine($"Copyright (C) 2020-2021. project violet-server.");
            Console.WriteLine("Usage: ./hsync [OPTIONS...]");

            var builder = new StringBuilder();

            CommandLineParser.GetFields(typeof(Options)).ToList().ForEach(
                x =>
            {
                var key = x.Key;
                if (!key.StartsWith("--"))
                {
                    return;
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.ShortOption))
                {
                    key = $"{x.Value.Item2.ShortOption}, " + key;
                }
                var help = "";
                if (!string.IsNullOrEmpty(x.Value.Item2.Help))
                {
                    help = $"[{x.Value.Item2.Help}]";
                }
                if (!string.IsNullOrEmpty(x.Value.Item2.Info))
                {
                    builder.Append($"   {key}".PadRight(30) + $" {x.Value.Item2.Info} {help}\r\n");
                }
                else
                {
                    builder.Append($"   {key}".PadRight(30) + $" {help}\r\n");
                }
            });
            Console.Write(builder.ToString());
        }
예제 #27
0
        public void LinuxTime(long?mTime, long?aTime, long?cTime, bool centralDir)
        {
            List <byte> eTime = new();
            byte        flags = 0;

            if (mTime != null)
            {
                flags |= 0x01;
                eTime.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToUnixDateTime((long)mTime)));
            }

            if (!centralDir)
            {
                if (aTime != null)
                {
                    flags |= 0x02;
                    eTime.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToUnixDateTime((long)aTime)));
                }
                if (cTime != null)
                {
                    flags |= 0x04;
                    eTime.AddRange(BitConverter.GetBytes(CompressUtils.UtcTicksToUnixDateTime((long)cTime)));
                }
            }

            if (flags == 0)
            {
                return;
            }

            _extraField.AddRange(BitConverter.GetBytes((ushort)0x5455));
            _extraField.AddRange(BitConverter.GetBytes((ushort)eTime.Count + 1));

            _extraField.Add(flags);
            _extraField.AddRange(eTime);
        }
예제 #28
0
        public ZipReturn ZipFileCreate(string newFilename, SevenZipCompressType compressOutput, int dictionarySize = 1 << 24, int numFastBytes = 64)
        {
            if (ZipOpen != ZipOpenType.Closed)
            {
                return(ZipReturn.ZipFileAlreadyOpen);
            }

            CompressUtils.CreateDirForFile(newFilename);
            _zipFileInfo = new FileInfo(newFilename);

            int errorCode = FileStream.OpenFileWrite(newFilename, out _zipFs);

            if (errorCode != 0)
            {
                ZipFileClose();
                return(ZipReturn.ZipErrorOpeningFile);
            }
            ZipOpen = ZipOpenType.OpenWrite;

            _signatureHeader = new SignatureHeader();
            _header          = new Header();

            using (BinaryWriter bw = new(_zipFs, Encoding.UTF8, true))
            {
                _signatureHeader.Write(bw);
            }
            _baseOffset = _zipFs.Position;


            _packedOutStreams = new List <outStreams>();

            _compType = compressOutput;

#if solid
            outStreams newStream = new()
            {
                packedStart     = (ulong)_zipFs.Position,
                compType        = compressOutput,
                packedSize      = 0,
                unpackedStreams = new List <UnpackedStreamInfo>()
            };
            switch (compressOutput)
            {
            case SevenZipCompressType.lzma:
                LzmaEncoderProperties ep  = new(true, dictionarySize, numFastBytes);
                LzmaStream            lzs = new(ep, false, _zipFs);

                newStream.Method     = new byte[] { 3, 1, 1 };
                newStream.Properties = lzs.Properties;
                _compressStream      = lzs;
                break;

            case SevenZipCompressType.zstd:
                ZstdSharp.CompressionStream zss = new(_zipFs, 19);
                newStream.Method     = new byte[] { 4, 247, 17, 1 };
                newStream.Properties = new byte[] { 1, 5, 19, 0, 0 };
                _compressStream      = zss;
                break;

            case SevenZipCompressType.uncompressed:
                newStream.Method     = new byte[] { 0 };
                newStream.Properties = null;
                _compressStream      = _zipFs;
                break;
            }

            _packedOutStreams.Add(newStream);
#endif
            return(ZipReturn.ZipGood);
        }
예제 #29
0
 public void Send(byte[] publicKey, byte[] message)
 {
     CommunicationHub.Net.Hub.Send(publicKey, CompressUtils.DeflateCompress(message).ToArray());
 }
예제 #30
0
        public static XmlDB Load(string filename, string password)
        {
            XmlDB db           = null;
            var   memoryStream = new MemoryStream();

            try
            {
                if (File.Exists(filename))
                {
                    var types    = new List <Type>();
                    var compress = !string.IsNullOrEmpty(password) && CompressUtils.CheckZip(filename, password);
                    if (compress)
                    {
                        using (var fileStream = new FileStream(filename, FileMode.Open))
                        {
                            CompressUtils.Decompress(fileStream, memoryStream, password);
                        }
                        memoryStream.Seek(0, SeekOrigin.Begin);
                    }

                    using (var xmlStream = compress ? (Stream)memoryStream : new FileStream(filename, FileMode.Open))
                    {
                        using (var reader = XmlReader.Create(xmlStream))
                        {
                            while (reader.Read())
                            {
                                if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "DBEntity")
                                {
                                    var typeName = reader.GetAttribute("xsi:type");
                                    types.Add(TypeCache.Instance.GetTypeFromCache(typeof(DBEntity), typeName));
                                }
                            }
                        }

                        types.AddRange(ReflectionUtils.FindTypes(typeof(DBEntity).Assembly, typeof(Geometry)));

                        xmlStream.Seek(0, SeekOrigin.Begin);
                        db = XmlSerializeUtils.Load <XmlDB>(xmlStream, types.Distinct().ToArray()
                                                            /*GetExtraTypes(NAMESPACES)*/, string.Empty);
                        db.DecodeUnprited();
                    }
                }
                else
                {
                    db = new XmlDB {
                        FileName = filename, Tables = new List <XmlTable>()
                    };
                }
            }
            catch (Exception ex)//check it later
            {
                db = new XmlDB {
                    FileName = filename, Tables = new List <XmlTable>()
                };
                LogManager.Instance.Error(ex);
            }
            finally
            {
                db.FileName = filename;
                db.BuildIndex();
                memoryStream.Dispose();
            }
            return(db);
        }