예제 #1
0
        public static byte[] Compress(byte[] input, int offset, int len)
        {
            byte[]   sourceArray = new byte[0x400];
            int      num         = 0;
            Deflater deflater    = new Deflater();

            deflater.SetInput(input, offset, len);
            deflater.Finish();
            while (!deflater.IsFinished)
            {
                if (num == sourceArray.Length)
                {
                    byte[] buffer2 = new byte[sourceArray.Length * 2];
                    Array.Copy(sourceArray, buffer2, sourceArray.Length);
                    sourceArray = buffer2;
                }
                try
                {
                    num += deflater.Deflate(sourceArray, num, sourceArray.Length - num);
                }
                catch (FormatException exception)
                {
                    throw new IOException(exception.ToString());
                }
            }
            deflater.Reset();
            byte[] destinationArray = new byte[num];
            Array.Copy(sourceArray, destinationArray, num);
            return(destinationArray);
        }
예제 #2
0
        private const int CopyBufferSize = 32 * 1024;    // 32kb

        public void Compress(Stream source, Stream destination)
        {
            /*
             * var deflater = new DeflaterOutputStream(destination, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
             *
             * var dataBuffer = new byte[CopyBufferSize];
             * StreamUtils.Copy(source, deflater, dataBuffer);
             */

            var def = new Deflater(Deflater.DEFAULT_COMPRESSION, true);

            var inputData = new byte[source.Length - source.Position];

            source.Read(inputData, 0, inputData.Length);

            var buffer = new byte[CopyBufferSize];

            def.SetInput(inputData, 0, inputData.Length);
            def.Finish();

            while (!def.IsFinished)
            {
                int outputLen = def.Deflate(buffer, 0, buffer.Length);
                destination.Write(buffer, 0, outputLen);
            }

            def.Reset();
        }
예제 #3
0
        protected override void Encode(IChannelHandlerContext ctx, IByteBuffer msg, List <object> output)
        {
            var buffer = Unpooled.Buffer();

            if (msg.ReadableBytes >= CompressionThreshold)
            {
                var data = msg.ToArray(out var offset, out var count);

                buffer.WriteVarInt32(count);

                _deflater.SetInput(data, offset, count);
                _deflater.Finish();

                while (!_deflater.IsFinished)
                {
                    var read = _deflater.Deflate(_buffer);
                    buffer.WriteBytes(_buffer, 0, read);
                }

                _deflater.Reset();
            }
            else
            {
                buffer.WriteVarInt32(0);
                buffer.WriteBytes(msg);
            }

            output.Add(buffer);
        }
예제 #4
0
        public static byte[] GZip(byte[] input, int size, out int length)
        {
            if (size == 0)
            {
                var memory = new MemoryStream();
                deflater.Reset();
                using (var stream = new GZipOutputStream(memory, deflater, 4096, deflaterBuffer))
                {
                    stream.Write(input, 0, input.Length);
                }

                var array = memory.ToArray();
                length = array.Length;
                return(array);
            }
            else
            {
                if (size > decompressBuffer.Length)
                {
                    decompressBuffer = new byte[size];
                }

                inflater.Reset();
                using (var stream = new GZipInputStream(new MemoryStream(input), inflater, 4096, inflaterBuffer))
                {
                    stream.Read(decompressBuffer, 0, size);
                }

                length = size;
                return(decompressBuffer);
            }
        }
예제 #5
0
        public static byte[] Compress(byte[] input, int offset, int len)
        {
            byte[]   output     = new byte[1024];
            int      outputused = 0;
            Deflater deflater   = new Deflater();

            deflater.SetInput(input, offset, len);
            deflater.Finish();
            while (deflater.IsFinished == false)
            {
                //				if(deflater.IsNeedingInput && deflater.IsFinished == false)
                //					throw(new Exception("deflateData: input incomplete!"));
                if (outputused == output.Length)
                {
                    byte[] newOutput = new byte[output.Length * 2];
                    Array.Copy(output, newOutput, output.Length);
                    output = newOutput;
                }
                try
                {
                    outputused += deflater.Deflate(output, outputused, output.Length - outputused);
                }
                catch (FormatException e)
                {
                    throw(new IOException(e.ToString()));
                }
            }
            deflater.Reset();
            byte[] realOutput = new byte[outputused];
            Array.Copy(output, realOutput, outputused);
            return(realOutput);
        }
예제 #6
0
            public override void Compress(byte[] bytes, int off, int len, DataOutput @out)
            {
                Compressor.Reset();
                Compressor.SetInput((byte[])(Array)bytes, off, len);
                Compressor.Finish();

                if (Compressor.NeedsInput)
                {
                    // no output
                    Debug.Assert(len == 0, len.ToString());
                    @out.WriteVInt(0);
                    return;
                }

                int totalCount = 0;

                for (; ;)
                {
                    int count = Compressor.Deflate(Compressed, totalCount, Compressed.Length - totalCount);
                    totalCount += count;
                    Debug.Assert(totalCount <= Compressed.Length);
                    if (Compressor.IsFinished)
                    {
                        break;
                    }
                    else
                    {
                        Compressed = ArrayUtil.Grow(Compressed);
                    }
                }

                @out.WriteVInt(totalCount);
                @out.WriteBytes(Compressed, totalCount);
            }
예제 #7
0
        public byte[] GetCompressedOutPacket(int offset, int length)
        {
            var deflater = new Deflater();

            deflater.SetInput(Data, offset, length);
            deflater.Finish();

            var compBuffer = new byte[1024];
            var ret        = new List <byte>();

            while (!deflater.IsFinished)
            {
                try
                {
                    deflater.Deflate(compBuffer);
                    ret.AddRange(compBuffer);
                    Array.Clear(compBuffer, 0, compBuffer.Length);
                }
                catch (Exception ex)
                {
                    Logging.WriteException(ex);
                    return(null);
                }
            }
            deflater.Reset();
            return(ret.ToArray());
        }
예제 #8
0
        public static byte[] Deflate(byte[] inputData)
        {
            var deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, false);

            deflater.SetInput(inputData);
            deflater.Finish();

            using (var ms = new MemoryStream())
            {
                var outputBuffer = new byte[65536 * 4];
                while (deflater.IsNeedingInput == false)
                {
                    var read = deflater.Deflate(outputBuffer);
                    ms.Write(outputBuffer, 0, read);

                    if (deflater.IsFinished == true)
                    {
                        break;
                    }
                }
                deflater.Reset();

                return(ms.ToArray());
            }
        }
예제 #9
0
        /// <summary>
        /// Create a writeable byte array to persist data back into the PES data system.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private byte[] CreateWriteableBytes(byte[] input)
        {
            uint fsize = (uint)input.Length;

            Deflater deflater = new Deflater(9);

            deflater.SetInput(input);
            deflater.Finish();
            using (var ms = new MemoryStream())
            {
                var outputBuffer = new byte[65536 * 32];
                while (deflater.IsNeedingInput == false)
                {
                    var read = deflater.Deflate(outputBuffer);
                    ms.Write(outputBuffer, 0, read);

                    if (deflater.IsFinished == true)
                    {
                        break;
                    }
                }

                deflater.Reset();

                uint   zsize  = (uint)ms.Length;
                byte[] header = { 0x04, 0x10, 0x01, 0x57, 0x45, 0x53, 0x59, 0x53 };
                byte[] b1, b2;
                b1 = BitConverter.GetBytes(zsize);
                b2 = BitConverter.GetBytes(fsize);

                byte[] zlib = ms.ToArray();

                return(header.Concat(b1).Concat(b2).Concat(zlib).ToArray());
            }
        }
예제 #10
0
        /// <summary>
        /// Compresses this instance.
        /// </summary>
        public void Compress()
        {
            var deflater = new Deflater();

            byte[] packet = PacketData;
            deflater.SetInput(packet, 0, packet.Length);
            deflater.Finish();

            var compBuffer = new byte[1024];
            var ret        = new List <byte>();

            while (!deflater.IsFinished)
            {
                try
                {
                    deflater.Deflate(compBuffer);
                    ret.AddRange(compBuffer);
                    Array.Clear(compBuffer, 0, compBuffer.Length);
                }
                catch (Exception ex)
                {
                    Logging.WriteException(ex);
                    return;
                }
            }
            deflater.Reset();

            Seek((byte)_headerType, SeekOrigin.Begin);
            // Write the compressed bytes over whatever is there.
            Write(ret.ToArray());
            // Set the stream length to the end of the actual packet data.
            // This makes sure we don't have any 'junk' packets at the end.
            OutStream.SetLength(BaseStream.Position);
        }
예제 #11
0
        private void WriteWhole(Deflater def, int typeCode, byte[] data)
        {
            int sz     = data.Length;
            int hdrlen = 0;

            _buffer[hdrlen++] = (byte)((typeCode << 4) | sz & 15);
            sz = (int)(((uint)sz) >> 4);

            while (sz > 0)
            {
                _buffer[hdrlen - 1] |= 0x80;
                _buffer[hdrlen++]    = (byte)(sz & 0x7f);
                sz = (int)(((uint)sz) >> 7);
            }

            _packDigest.Update(_buffer, 0, hdrlen);
            _crc.Update(_buffer, 0, hdrlen);
            _packOut.Write(_buffer, 0, hdrlen);
            def.Reset();
            def.SetInput(data);
            def.Finish();

            while (!def.IsFinished)
            {
                int datlen = def.Deflate(_buffer);
                _packDigest.Update(_buffer, 0, datlen);
                _crc.Update(_buffer, 0, datlen);
                _packOut.Write(_buffer, 0, datlen);
            }
        }
예제 #12
0
 internal virtual DeflaterOutputStream Compress(OutputStream @out)
 {
     if (deflate == null)
     {
         deflate = new Deflater(config.GetCompression());
     }
     else
     {
         deflate.Reset();
     }
     return(new DeflaterOutputStream(@out, deflate, 8192));
 }
    private static Deflater GetDeflater()
    {
        if (_deflater == null)
        {
            _deflater = new Deflater();
        }
        else
        {
            _deflater.Reset();
        }

        return(_deflater);
    }
예제 #14
0
        /// <exception cref="System.IO.IOException"></exception>
        protected internal override bool OnAppendBase(int typeCode, byte[] data, PackedObjectInfo
                                                      info)
        {
            info.SetOffset(packEnd);
            byte[] buf = Buffer();
            int    sz  = data.Length;
            int    len = 0;

            buf[len++] = unchecked ((byte)((typeCode << 4) | sz & 15));
            sz         = (int)(((uint)sz) >> 4);
            while (sz > 0)
            {
                buf[len - 1] |= unchecked ((int)(0x80));
                buf[len++]    = unchecked ((byte)(sz & unchecked ((int)(0x7f))));
                sz            = (int)(((uint)sz) >> 7);
            }
            tailDigest.Update(buf, 0, len);
            crc.Reset();
            crc.Update(buf, 0, len);
            @out.Seek(packEnd);
            @out.Write(buf, 0, len);
            packEnd += len;
            if (def == null)
            {
                def = new Deflater(Deflater.DEFAULT_COMPRESSION, false);
            }
            else
            {
                def.Reset();
            }
            def.SetInput(data);
            def.Finish();
            while (!def.IsFinished)
            {
                len = def.Deflate(buf);
                tailDigest.Update(buf, 0, len);
                crc.Update(buf, 0, len);
                @out.Write(buf, 0, len);
                packEnd += len;
            }
            info.SetCRC((int)crc.GetValue());
            return(true);
        }
예제 #15
0
        public static byte[] Deflate(byte[] input)
        {
            deflater.SetInput(input);
            deflater.Finish();

            using (MemoryStream ms = new MemoryStream())
            {
                byte[] readbyte = new byte[input.Length];
                while (!deflater.IsNeedingInput)
                {
                    int read = deflater.Deflate(readbyte);
                    ms.Write(readbyte, 0, read);

                    if (deflater.IsFinished)
                    {
                        break;
                    }
                }
                deflater.Reset();
                return(ms.ToArray());
            }
        }
예제 #16
0
        /// <summary>
        /// Closes the current entry, updating header and footer information as required
        /// </summary>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurs.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// No entry is active.
        /// </exception>
        public void CloseEntry()
        {
            if (curEntry == null)
            {
                throw new InvalidOperationException("No open entry");
            }

            long csize = size;

            // First finish the deflater, if appropriate
            if (curMethod == CompressionMethod.Deflated)
            {
                if (size >= 0)
                {
                    base.Finish();
                    csize = Deflater.TotalOut;
                }
                else
                {
                    Deflater.Reset();
                }
            }


            if (curEntry.Size < 0)
            {
                curEntry.Size = size;
            }
            else if (curEntry.Size != size)
            {
                throw new ZipException("size was " + size + ", but I expected " + curEntry.Size);
            }

            if (curEntry.CompressedSize < 0)
            {
                curEntry.CompressedSize = csize;
            }
            else if (curEntry.CompressedSize != csize)
            {
                throw new ZipException("compressed size was " + csize + ", but I expected " + curEntry.CompressedSize);
            }

            if (curEntry.Crc < 0)
            {
                curEntry.Crc = crc.Value;
            }
            else if (curEntry.Crc != crc.Value)
            {
                throw new ZipException("crc was " + crc.Value + ", but I expected " + curEntry.Crc);
            }

            offset += csize;

            if (curEntry.IsCrypted)
            {
                if (curEntry.AESKeySize > 0)
                {
                    curEntry.CompressedSize += curEntry.AESOverheadSize;
                }
                else
                {
                    curEntry.CompressedSize += ZipConstants.CryptoHeaderSize;
                }
            }

            // Patch the header if possible
            if (patchEntryHeader)
            {
                patchEntryHeader = false;

                long curPos = BaseOutputStream.Position;
                BaseOutputStream.Seek(crcPatchPos, SeekOrigin.Begin);
                WriteLeInt((int)curEntry.Crc);

                if (curEntry.LocalHeaderRequiresZip64)
                {
                    if (sizePatchPos == -1)
                    {
                        throw new ZipException("Entry requires zip64 but this has been turned off");
                    }

                    BaseOutputStream.Seek(sizePatchPos, SeekOrigin.Begin);
                    WriteLeLong(curEntry.Size);
                    WriteLeLong(curEntry.CompressedSize);
                }
                else
                {
                    WriteLeInt((int)curEntry.CompressedSize);
                    WriteLeInt((int)curEntry.Size);
                }
                BaseOutputStream.Seek(curPos, SeekOrigin.Begin);
            }

            // Add data descriptor if flagged as required
            if ((curEntry.Flags & 8) != 0)
            {
                WriteLeInt(ZipConstants.DataDescriptorSignature);
                WriteLeInt(unchecked ((int)curEntry.Crc));

                if (curEntry.LocalHeaderRequiresZip64)
                {
                    WriteLeLong(curEntry.CompressedSize);
                    WriteLeLong(curEntry.Size);
                    offset += ZipConstants.Zip64DataDescriptorSize;
                }
                else
                {
                    WriteLeInt((int)curEntry.CompressedSize);
                    WriteLeInt((int)curEntry.Size);
                    offset += ZipConstants.DataDescriptorSize;
                }
            }

            entries.Add(curEntry);
            curEntry = null;
        }
예제 #17
0
        public void PutNextEntry(ZipEntry entry)
        {
            bool hasCrc;

            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }
            if (_entries == null)
            {
                throw new InvalidOperationException("ZipOutputStream was finished");
            }
            if (_curEntry != null)
            {
                CloseEntry();
            }
            if (_entries.Count == 0x7fffffff)
            {
                throw new ZipException("Too many entries for Zip file");
            }
            CompressionMethod compressionMethod = entry.CompressionMethod;
            int compressionLevel = _defaultCompressionLevel;

            entry.Flags      &= 0x800;
            _patchEntryHeader = false;
            if (entry.Size == 0L)
            {
                entry.CompressedSize = entry.Size;
                entry.Crc            = 0L;
                compressionMethod    = CompressionMethod.Stored;
                hasCrc = true;
            }
            else
            {
                hasCrc = (entry.Size >= 0L) && entry.HasCrc;
                if (compressionMethod == CompressionMethod.Stored)
                {
                    if (!hasCrc)
                    {
                        if (!CanPatchEntries)
                        {
                            compressionMethod = CompressionMethod.Deflated;
                            compressionLevel  = 0;
                        }
                    }
                    else
                    {
                        entry.CompressedSize = entry.Size;
                        hasCrc = entry.HasCrc;
                    }
                }
            }
            if (!hasCrc)
            {
                if (!CanPatchEntries)
                {
                    entry.Flags |= 8;
                }
                else
                {
                    _patchEntryHeader = true;
                }
            }
            if (Password != null)
            {
                entry.IsCrypted = true;
                if (entry.Crc < 0L)
                {
                    entry.Flags |= 8;
                }
            }
            entry.Offset            = _offset;
            entry.CompressionMethod = compressionMethod;
            _curMethod    = compressionMethod;
            _sizePatchPos = -1L;
            if ((_useZip64 == UseZip64.On) || ((entry.Size < 0L) && (_useZip64 == UseZip64.Dynamic)))
            {
                entry.ForceZip64();
            }
            WriteLeInt(ZipConstants.LocalHeaderSignature);
            WriteLeShort(entry.Version);
            WriteLeShort(entry.Flags);
            WriteLeShort((byte)entry.CompressionMethodForHeader);
            WriteLeInt((int)entry.DosTime);
            if (hasCrc)
            {
                WriteLeInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    WriteLeInt(-1);
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt(entry.IsCrypted ? (((int)entry.CompressedSize) + 12) : ((int)entry.CompressedSize));
                    WriteLeInt((int)entry.Size);
                }
            }
            else
            {
                if (_patchEntryHeader)
                {
                    _crcPatchPos = BaseOutputStream.Position;
                }
                WriteLeInt(0);
                if (_patchEntryHeader)
                {
                    _sizePatchPos = BaseOutputStream.Position;
                }
                if (entry.LocalHeaderRequiresZip64 || _patchEntryHeader)
                {
                    WriteLeInt(-1);
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt(0);
                    WriteLeInt(0);
                }
            }
            byte[] buffer = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
            if (buffer.Length > 0xffff)
            {
                throw new ZipException("Entry name too long.");
            }
            ZipExtraData extraData = new ZipExtraData(entry.ExtraData);

            if (entry.LocalHeaderRequiresZip64)
            {
                extraData.StartNewEntry();
                if (hasCrc)
                {
                    extraData.AddLeLong(entry.Size);
                    extraData.AddLeLong(entry.CompressedSize);
                }
                else
                {
                    extraData.AddLeLong(-1L);
                    extraData.AddLeLong(-1L);
                }
                extraData.AddNewEntry(1);
                if (!extraData.Find(1))
                {
                    throw new ZipException("Internal error cant find extra data");
                }
                if (_patchEntryHeader)
                {
                    _sizePatchPos = extraData.CurrentReadIndex;
                }
            }
            else
            {
                extraData.Delete(1);
            }
            if (entry.AesKeySize > 0)
            {
                AddExtraDataAes(entry, extraData);
            }
            byte[] entryData = extraData.GetEntryData();
            WriteLeShort(buffer.Length);
            WriteLeShort(entryData.Length);
            if (buffer.Length > 0)
            {
                BaseOutputStream.Write(buffer, 0, buffer.Length);
            }
            if (entry.LocalHeaderRequiresZip64 && _patchEntryHeader)
            {
                _sizePatchPos += BaseOutputStream.Position;
            }
            if (entryData.Length > 0)
            {
                BaseOutputStream.Write(entryData, 0, entryData.Length);
            }
            _offset += (30 + buffer.Length) + entryData.Length;
            if (entry.AesKeySize > 0)
            {
                _offset += entry.AesOverheadSize;
            }
            _curEntry = entry;
            _crc.Reset();
            if (compressionMethod == CompressionMethod.Deflated)
            {
                Deflater.Reset();
                Deflater.SetLevel(compressionLevel);
            }
            _size = 0L;
            if (entry.IsCrypted)
            {
                if (entry.AesKeySize > 0)
                {
                    WriteAesHeader(entry);
                }
                else if (entry.Crc < 0L)
                {
                    WriteEncryptionHeader(entry.DosTime << 0x10);
                }
                else
                {
                    WriteEncryptionHeader(entry.Crc);
                }
            }
        }
예제 #18
0
    static int Deflate(Deflater def, byte [] src, byte [] dest)
    {
        bool count;
        int offset, length, remain;

        if (dest == null) {
            dest = new byte [BlockSize];
            count = true;
        } else
            count = false;

        def.Reset ();
        def.SetInput (src);

        offset = 0;
        while (!def.IsFinished) {
            if (def.IsNeedingInput)
                def.Finish ();

            remain = Math.Min (dest.Length - offset, BlockSize);
            if (remain == 0)
                break;

            length = def.Deflate (dest, offset, remain);
            if (!count)
                offset += length;
        }

        return def.TotalOut;
    }
예제 #19
0
        public void PackText(FileStream fileStream, uint off, uint zsize, uint size, Dictionary <ulong, string> dbd, uint hash2, uint hash1, int endtable, int somework)
        {
            BinaryReader binaryReader = new BinaryReader(fileStream);

            binaryReader.BaseStream.Position = off;
            binaryReader.ReadBytes(36);
            byte[]   input    = binaryReader.ReadBytes((int)zsize);
            byte[]   buffer   = new byte[size];
            Inflater inflater = new Inflater();

            inflater.SetInput(input);
            inflater.Inflate(buffer);
            BinaryReader binaryReader2 = new BinaryReader(new MemoryStream(buffer));

            binaryReader2.ReadBytes(3);
            int num  = binaryReader2.ReadInt32();
            int num2 = num * 26 + 7;
            Dictionary <ulong, string> dictionary = new Dictionary <ulong, string>();
            int          num3         = 0;
            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter binaryWriter = new BinaryWriter(memoryStream);

            binaryWriter.Write((byte)1);
            binaryWriter.Write((byte)0);
            binaryWriter.Write((byte)0);
            binaryWriter.Write(num);
            while (num3 < num)
            {
                long  num4   = binaryReader2.ReadInt64();
                byte  b      = binaryReader2.ReadByte();
                byte  value  = binaryReader2.ReadByte();
                float value2 = binaryReader2.ReadSingle();
                int   num5   = binaryReader2.ReadInt32();
                int   num6   = binaryReader2.ReadInt32();
                binaryReader2.ReadInt32();
                long   position     = binaryReader2.BaseStream.Position;
                string change_value = "0";
                if (num5 > 0)
                {
                    binaryReader2.BaseStream.Seek(num6, SeekOrigin.Begin);
                    byte[] bytes = binaryReader2.ReadBytes(num5);
                    string text  = Encoding.UTF8.GetString(bytes).Replace("\n", "\\n");
                    ulong  key   = UniqueId(num4, b);
                    if (changes == "1")
                    {
                        if (dictionary_en.TryGetValue(key, out string _))
                        {
                            if (dictionary_en[key] != text)
                            {
                                dbd.Remove(key);
                                dictionary_xml_m.Remove(key);
                                dictionary_xml_w.Remove(key);
                                dictionary_xml_translator_m.Remove(key);
                                dictionary_xml_translator_w.Remove(key);
                                change_value = "1";
                            }
                            else
                            {
                                change_value = "0";
                            }
                        }
                    }
                    if (dbd.TryGetValue(key, out string _))
                    {
                        dictionary.Add(key, dbd[key]);
                        _    = dbd[key];
                        num5 = Encoding.UTF8.GetBytes(dbd[key].Replace("\\n", "\n")).Length;
                    }
                    else
                    {
                        transl_a = "";
                        if (ConfigurationManager.AppSettings["a_translate"] == "1" && somework == 1)
                        {
                            if (File.Exists(@"C:\\Program Files\\Mozilla Firefox\\firefox.exe") || File.Exists(@"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"))
                            {
                                transl_a = Translator(text, "Deepl");

                                /*string instrrr = "INSERT INTO Translated (fileinfo,hash,key_unic,text_en,text_ru_m,translator_m) VALUES ('" + hash1 + "','" + hash1 + "','" + key + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(transl_a) + "','Deepl');";
                                 * using (StreamWriter file_for_exam = new StreamWriter("db\\deepl_trans.txt", true))
                                 * {
                                 *  file_for_exam.WriteLine(instrrr);
                                 * }
                                 * transl_a = "";*/
                            }
                            else
                            {
                                transl_a = Translator(text, "Promt");
                            }
                        }
                        if (transl_a == "")
                        {
                            dictionary.Add(key, text);
                            dictionary_xml_m.Add(key, text);
                            dictionary_xml_w.Add(key, text);
                            dictionary_xml_translator_m.Add(key, "Deepl");
                            dictionary_xml_translator_w.Add(key, "Deepl");
                            num5 = Encoding.UTF8.GetBytes(text.Replace("\\n", "\n")).Length;
                        }
                        else
                        {
                            dictionary.Add(key, transl_a);
                            dictionary_xml_m.Add(key, transl_a);
                            dictionary_xml_w.Add(key, transl_a);
                            dictionary_xml_translator_m.Add(key, "Deepl");
                            dictionary_xml_translator_w.Add(key, "Deepl");
                            num5 = Encoding.UTF8.GetBytes(transl_a.Replace("\\n", "\n")).Length;
                            using (SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=db\\translate.db3; Version = 3; New = True; Compress = True; "))
                            {
                                sqlite_conn.Open();
                                using (SQLiteCommand sqlite_cmd = new SQLiteCommand(sqlite_conn))
                                {
                                    if (change_value == "1")
                                    {
                                        sqlite_cmd.CommandText = "UPDATE Translated SET text_en='" + WebUtility.HtmlEncode(text) + "', text_ru_m='" + WebUtility.HtmlEncode(transl_a) + "',translator_m='Deepl',text_ru_w=NULL,translator_w=NULL WHERE key_unic ='" + key + "'";
                                    }
                                    else
                                    {
                                        sqlite_cmd.CommandText = "INSERT INTO Translated (fileinfo,hash,key_unic,text_en,text_ru_m,translator_m) VALUES ('" + hash1 + "','" + hash1 + "','" + key + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(transl_a) + "','Deepl')";
                                    }
                                    sqlite_cmd.ExecuteNonQuery();
                                }
                            }
                        }

                        /*string xml_text = "<hash>" + hash1 + "</hash><key>" + key + "</key><text_en>" + WebUtility.HtmlEncode(text) + "</text_en>";
                         * using (StreamWriter file_for_exam =
                         * new StreamWriter("db\\new_eng.txt", true))
                         * {
                         *  file_for_exam.WriteLine(xml_text);
                         * }
                         *
                         * string xml_text = "<filesinfo></filesinfo><hash>" + hash1 + "</hash><key>" + key + "</key><text_en>" + WebUtility.HtmlEncode(text) + "</text_en><text_ru_m transl=\"3\">" + WebUtility.HtmlEncode(dbd[key]) + "</text_ru_m><text_ru_w  transl=\"3\"></text_ru_w>";
                         *      using (StreamWriter file_for_exam =
                         *      new StreamWriter(@"C:\Users\Tidus\source\repos\SWToR_RUS\bin\Debug\db\all2.txt", true, encoding: Encoding.UTF8))
                         *      {
                         *          file_for_exam.WriteLine(xml_text);
                         *      }
                         *      SQLiteConnection sqlite_conn;
                         *      sqlite_conn = new SQLiteConnection("Data Source=db\\translate.db3; Version = 3; New = True; Compress = True; ");
                         *      sqlite_conn.Open();
                         *      SQLiteCommand sqlite_cmd;
                         *      sqlite_cmd = sqlite_conn.CreateCommand();
                         *      string sql_insert = "INSERT INTO Translated (hash,key_unic,text_en,text_ru_m,translator_m) VALUES ('" + hash1.ToString() + "','" + key.ToString() + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(dbd[key]) + "','3')";
                         *      sqlite_cmd.CommandText = sql_insert;
                         *      sqlite_cmd.ExecuteNonQuery();
                         *      sqlite_conn.Close();*/
                    }

                    /*if (somework == 1)
                     * {
                     *  string tmp_base;
                     *  if (dictionary_xml_translator_w.TryGetValue(key, out string sdfg) && dictionary_xml_m[key] != dictionary_xml_w[key])
                     *  {
                     *      tmp_base = "INSERT INTO Translated (fileinfo,hash,key_unic,text_en,text_ru_m,translator_m,text_ru_w,translator_w) VALUES ('" + hash1 + "','" + hash1 + "','" + key + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(dictionary_xml_m[key]).Replace("\n \n", "\n\n").Replace("\n", "\\n") + "','" + dictionary_xml_translator_m[key] + "','" + WebUtility.HtmlEncode(dictionary_xml_w[key]).Replace("\n \n", "\n\n").Replace("\n", "\\n") + "','" + dictionary_xml_translator_w[key] + "');";
                     *  }
                     *  else
                     *  {
                     *      tmp_base = "INSERT INTO Translated (fileinfo,hash,key_unic,text_en,text_ru_m,translator_m) VALUES ('" + hash1 + "','" + hash1 + "','" + key + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(dictionary_xml_m[key]).Replace("\n \n", "\n\n").Replace("\n", "\\n") + "','" + dictionary_xml_translator_m[key] + "');";
                     *  }
                     * tmp_base = "'" + nu88 + "','" + num4 + "','" + b + "','" + value + "','" + value2 + "','" + num6 + "','" + hash1 + "','" + key + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(dictionary_xml_m[key]).Replace("\n \n", "\n\n").Replace("\n", "\\n") + "','" + dictionary_xml_translator_m[key] + "'";
                     *  using (StreamWriter file_for_exam = new StreamWriter("db\\allbase.txt", true))
                     *  {
                     *      file_for_exam.WriteLine(tmp_base);
                     *  }
                     * }*/
                }
                binaryReader2.BaseStream.Position = position;
                num3++;
                binaryWriter.Write(num4);
                binaryWriter.Write(b);
                binaryWriter.Write(value);
                binaryWriter.Write(value2);
                binaryWriter.Write(num5);
                binaryWriter.Write(num2);
                num2 += num5;
                binaryWriter.Write(num5);
            }
            foreach (KeyValuePair <ulong, string> item in dictionary)
            {
                Encoding.UTF8.GetBytes(item.Value.Replace("\\n", "\n"));
                binaryWriter.Write(Encoding.UTF8.GetBytes(item.Value.Replace("\\n", "\n")));
            }
            MemoryStream memoryStream2 = new MemoryStream();
            Deflater     deflater      = new Deflater();

            deflater.SetInput(memoryStream.ToArray());
            deflater.Finish();
            byte[] array = new byte[size * 3];
            while (!deflater.IsNeedingInput)
            {
                int count = deflater.Deflate(array);
                memoryStream2.Write(array, 0, count);
                if (deflater.IsFinished)
                {
                    break;
                }
            }
            deflater.Reset();
            int newsize  = memoryStream.ToArray().Length;
            int newzsize = memoryStream2.ToArray().Length;

            byte[] array2 = new byte[36];
            array2[0] = 2;
            array2[2] = 32;
            byte[] array3 = new byte[array2.Length + memoryStream2.ToArray().Length];
            array2.CopyTo(array3, 0);
            memoryStream2.ToArray().CopyTo(array3, array2.Length);

            if (somework == 1)
            {
                using (StreamReader Hashes_Names = new StreamReader("db\\hashes_filename.txt", Encoding.Default))
                {
                    while (!Hashes_Names.EndOfStream)
                    {
                        string line = Hashes_Names.ReadLine();
                        if (line.IndexOf(hash1.ToString("X")) != -1)
                        {
                            string rez  = line.Substring(line.IndexOf("/resources/en-us/str/") + 21);
                            string rez2 = rez.Substring(0, rez.IndexOf("#"));
                            if (File_Name_List.Contains(hash1.ToString()))
                            {
                                using (SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=db\\translate.db3; Version = 3; New = True; Compress = True; "))
                                {
                                    sqlite_conn.Open();
                                    using (SQLiteCommand sqlite_cmd = new SQLiteCommand(sqlite_conn))
                                    {
                                        sqlite_cmd.CommandText = "UPDATE Translated SET fileinfo='" + WebUtility.HtmlEncode(rez2) + "' WHERE hash ='" + hash1 + "'";
                                        sqlite_cmd.ExecuteNonQuery();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Hhh(fileStream, array3, newzsize, newsize, hash2, hash1, endtable);
        }
예제 #20
0
        private void WritePendingFrame(bool forceKeyFrame)
        {
            if (!framePending)
            {
                return;
            }
            framePending = false;

            bool keyFrame = forceKeyFrame || frameCount % keyFramePeriodInFrames == 0;

            FlvWriter.FlvVideoFrameFlags frameFlags = FlvWriter.FlvVideoFrameFlags.Codec_ScreenVideo
                                                      | (keyFrame ? FlvWriter.FlvVideoFrameFlags.Type_KeyFrame : FlvWriter.FlvVideoFrameFlags.Type_InterFrame);

            flvWriter.WriteFlvVideoFrame(frameFlags,
                                         (int)(frameCount * 1000.0 / framesPerSecond),
                                         reserveBytesPerFrame,
                                         delegate(byte[] buffer, ref int bufferOffset)
            {
                // write frame header
                buffer[bufferOffset++] = (byte)(((nominalBlockWidth / 16 - 1) << 4) | (width >> 8));
                buffer[bufferOffset++] = (byte)width;
                buffer[bufferOffset++] = (byte)(((nominalBlockHeight / 16 - 1) << 4) | (height >> 8));
                buffer[bufferOffset++] = (byte)height;

                // proceed from bottom-left to top-right row by row
                for (int blockYOrigin = height; blockYOrigin >= 0; blockYOrigin -= nominalBlockHeight)
                {
                    int blockHeight = Math.Min(blockYOrigin, nominalBlockHeight);

                    for (int blockXOrigin = 0; blockXOrigin < width; blockXOrigin += nominalBlockWidth)
                    {
                        int blockWidth = Math.Min(width - blockXOrigin, nominalBlockWidth);

                        bool diff             = false;
                        int blockBufferOffset = 0;
                        int frameOffset       = blockYOrigin * width + blockXOrigin;
                        for (int y = 0; y < blockHeight; y++)
                        {
                            frameOffset -= width;

                            for (int x = 0; x < blockWidth; x++)
                            {
                                int color = currentFramePixels[frameOffset];

                                blockBuffer[blockBufferOffset++] = (byte)color;         // B
                                blockBuffer[blockBufferOffset++] = (byte)(color >> 8);  // G
                                blockBuffer[blockBufferOffset++] = (byte)(color >> 16); // R

                                if (previousFramePixels[frameOffset++] != color)
                                {
                                    diff = true;
                                }
                            }

                            frameOffset -= blockWidth;
                        }

                        if (keyFrame || diff)
                        {
                            deflater.Reset();
                            deflater.SetInput(blockBuffer, 0, blockBufferOffset);
                            deflater.Finish();

                            int blockLength = deflater.Deflate(buffer, bufferOffset + 2, buffer.Length - bufferOffset - 2);
                            Debug.Assert(deflater.IsFinished, "Deflater should be finished.");

                            buffer[bufferOffset++] = (byte)(blockLength >> 8);
                            buffer[bufferOffset++] = (byte)blockLength;
                            bufferOffset          += blockLength;
                        }
                        else
                        {
                            buffer[bufferOffset++] = 0;
                            buffer[bufferOffset++] = 0;
                        }
                    }
                }
            });

            frameCount += 1;
        }
예제 #21
0
        public void CloseEntry()
        {
            if (_curEntry == null)
            {
                throw new InvalidOperationException("No open entry");
            }
            long compressedSize = _size;

            if (_curMethod == CompressionMethod.Deflated)
            {
                if (_size >= 0L)
                {
                    Finish();
                    compressedSize = Deflater.TotalOut;
                }
                else
                {
                    Deflater.Reset();
                }
            }
            if (_curEntry.AesKeySize > 0)
            {
                BaseOutputStream.Write(AesAuthCode, 0, 10);
            }
            if (_curEntry.Size < 0L)
            {
                _curEntry.Size = _size;
            }
            else if (_curEntry.Size != _size)
            {
                throw new ZipException(string.Concat("size was ", _size, ", but I expected ", _curEntry.Size));
            }
            if (_curEntry.CompressedSize < 0L)
            {
                _curEntry.CompressedSize = compressedSize;
            }
            else if (_curEntry.CompressedSize != compressedSize)
            {
                throw new ZipException(string.Concat("compressed size was ", compressedSize, ", but I expected ", _curEntry.CompressedSize));
            }
            if (_curEntry.Crc < 0L)
            {
                _curEntry.Crc = _crc.Value;
            }
            else if (_curEntry.Crc != _crc.Value)
            {
                throw new ZipException(string.Concat("crc was ", _crc.Value, ", but I expected ", _curEntry.Crc));
            }
            _offset += compressedSize;
            if (_curEntry.IsCrypted)
            {
                if (_curEntry.AesKeySize > 0)
                {
                    _curEntry.CompressedSize += _curEntry.AesOverheadSize;
                }
                else
                {
                    _curEntry.CompressedSize += 12L;
                }
            }
            if (_patchEntryHeader)
            {
                _patchEntryHeader = false;
                long position = BaseOutputStream.Position;
                BaseOutputStream.Seek(_crcPatchPos, SeekOrigin.Begin);
                WriteLeInt((int)_curEntry.Crc);
                if (_curEntry.LocalHeaderRequiresZip64)
                {
                    if (_sizePatchPos == -1L)
                    {
                        throw new ZipException("Entry requires zip64 but this has been turned off");
                    }
                    BaseOutputStream.Seek(_sizePatchPos, SeekOrigin.Begin);
                    WriteLeLong(_curEntry.Size);
                    WriteLeLong(_curEntry.CompressedSize);
                }
                else
                {
                    WriteLeInt((int)_curEntry.CompressedSize);
                    WriteLeInt((int)_curEntry.Size);
                }
                BaseOutputStream.Seek(position, SeekOrigin.Begin);
            }
            if ((_curEntry.Flags & 8) != 0)
            {
                WriteLeInt(ZipConstants.DataDescriptorSignature);
                WriteLeInt((int)_curEntry.Crc);
                if (_curEntry.LocalHeaderRequiresZip64)
                {
                    WriteLeLong(_curEntry.CompressedSize);
                    WriteLeLong(_curEntry.Size);
                    _offset += 0x18L;
                }
                else
                {
                    WriteLeInt((int)_curEntry.CompressedSize);
                    WriteLeInt((int)_curEntry.Size);
                    _offset += 0x10L;
                }
            }
            _entries.Add(_curEntry);
            _curEntry = null;
        }
예제 #22
0
        internal ObjectId WriteObject(ObjectType type, long len, Stream input, bool store)
        {
            FileInfo             info;
            DeflaterOutputStream stream;
            FileStream           stream2;
            ObjectId             objectId = null;

            if (store)
            {
                info    = _r.ObjectsDirectory.CreateTempFile("noz");
                stream2 = info.OpenWrite();
            }
            else
            {
                info    = null;
                stream2 = null;
            }

            _md.Reset();
            if (store)
            {
                _def.Reset();
                stream = new DeflaterOutputStream(stream2, _def);
            }
            else
            {
                stream = null;
            }

            try
            {
                int    num;
                byte[] bytes = Codec.EncodedTypeString(type);
                _md.Update(bytes);
                if (stream != null)
                {
                    stream.Write(bytes, 0, bytes.Length);
                }

                _md.Update(0x20);
                if (stream != null)
                {
                    stream.WriteByte(0x20);
                }

                bytes = Constants.encodeASCII(len.ToString());
                _md.Update(bytes);
                if (stream != null)
                {
                    stream.Write(bytes, 0, bytes.Length);
                }

                _md.Update(0);
                if (stream != null)
                {
                    stream.WriteByte(0);
                }
                while ((len > 0L) && ((num = input.Read(_buf, 0, (int)Math.Min(len, _buf.Length))) > 0))
                {
                    _md.Update(_buf, 0, num);
                    if (stream != null)
                    {
                        stream.Write(_buf, 0, num);
                    }
                    len -= num;
                }

                if (len != 0L)
                {
                    throw new IOException("Input did not match supplied Length. " + len + " bytes are missing.");
                }

                if (stream != null)
                {
                    stream.Close();
                    if (info != null)
                    {
                        info.IsReadOnly = true;
                    }
                }
                objectId = ObjectId.FromRaw(_md.Digest());
            }
            finally
            {
                if ((objectId == null) && (stream != null))
                {
                    try
                    {
                        stream.Close();
                    }
                    finally
                    {
                        info.DeleteFile();
                    }
                }
            }
            if (info != null)
            {
                if (_r.HasObject(objectId))
                {
                    // Object is already in the repository so remove
                    // the temporary file.
                    //
                    info.DeleteFile();
                }
                else
                {
                    FileInfo info2 = _r.ToFile(objectId);
                    if (!info.RenameTo(info2.FullName))
                    {
                        // Maybe the directory doesn't exist yet as the object
                        // directories are always lazily created. Note that we
                        // try the rename first as the directory likely does exist.
                        //
                        if (info2.Directory != null)
                        {
                            info2.Directory.Create();
                        }
                        if (!info.RenameTo(info2.FullName) && !_r.HasObject(objectId))
                        {
                            // The object failed to be renamed into its proper
                            // location and it doesn't exist in the repository
                            // either. We really don't know what went wrong, so
                            // fail.
                            //
                            info.DeleteFile();
                            throw new ObjectWritingException("Unable to create new object: " + info2);
                        }
                    }
                }
            }
            return(objectId);
        }
예제 #23
0
        /// <summary>
        /// Starts a new Zip entry. It automatically closes the previous
        /// entry if present.
        /// All entry elements bar name are optional, but must be correct if present.
        /// If the compression method is stored and the output is not patchable
        /// the compression for that entry is automatically changed to deflate level 0
        /// </summary>
        /// <param name="entry">
        /// the entry.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// if entry passed is null.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// if an I/O error occured.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// if stream was finished
        /// </exception>
        /// <exception cref="ZipException">
        /// Too many entries in the Zip file<br/>
        /// Entry name is too long<br/>
        /// Finish has already been called<br/>
        /// </exception>
        public void PutNextEntry(ZipEntry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }

            if (entries == null)
            {
                throw new InvalidOperationException("ZipOutputStream was finished");
            }

            if (curEntry != null)
            {
                CloseEntry();
            }

            if (entries.Count == int.MaxValue)
            {
                throw new ZipException("Too many entries for Zip file");
            }

            CompressionMethod method = entry.CompressionMethod;
            int compressionLevel     = defaultCompressionLevel;

            // Clear flags that the library manages internally
            entry.Flags     &= (int)GeneralBitFlags.UnicodeText;
            patchEntryHeader = false;

            bool headerInfoAvailable;

            // No need to compress - definitely no data.
            if (entry.Size == 0)
            {
                entry.CompressedSize = entry.Size;
                entry.Crc            = 0;
                method = CompressionMethod.Stored;
                headerInfoAvailable = true;
            }
            else
            {
                headerInfoAvailable = (entry.Size >= 0) && entry.HasCrc && entry.CompressedSize >= 0;

                // Switch to deflation if storing isnt possible.
                if (method == CompressionMethod.Stored)
                {
                    if (!headerInfoAvailable)
                    {
                        if (!CanPatchEntries)
                        {
                            // Can't patch entries so storing is not possible.
                            method           = CompressionMethod.Deflated;
                            compressionLevel = 0;
                        }
                    }
                    else                       // entry.size must be > 0
                    {
                        entry.CompressedSize = entry.Size;
                        headerInfoAvailable  = entry.HasCrc;
                    }
                }
            }

            if (headerInfoAvailable == false)
            {
                if (CanPatchEntries == false)
                {
                    // Only way to record size and compressed size is to append a data descriptor
                    // after compressed data.

                    // Stored entries of this form have already been converted to deflating.
                    entry.Flags |= 8;
                }
                else
                {
                    patchEntryHeader = true;
                }
            }


            entry.Offset            = offset;
            entry.CompressionMethod = (CompressionMethod)method;

            curMethod    = method;
            sizePatchPos = -1;

            if ((useZip64_ == UseZip64.On) || ((entry.Size < 0) && (useZip64_ == UseZip64.Dynamic)))
            {
                entry.ForceZip64();
            }

            // Write the local file header
            WriteLeInt(ZipConstants.LocalHeaderSignature);

            WriteLeShort(entry.Version);
            WriteLeShort(entry.Flags);
            WriteLeShort((byte)entry.CompressionMethodForHeader);
            WriteLeInt((int)entry.DosTime);

            // TODO: Refactor header writing.  Its done in several places.
            if (headerInfoAvailable)
            {
                WriteLeInt((int)entry.Crc);
                if (entry.LocalHeaderRequiresZip64)
                {
                    WriteLeInt(-1);
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize);
                    WriteLeInt((int)entry.Size);
                }
            }
            else
            {
                if (patchEntryHeader)
                {
                    crcPatchPos = BaseOutputStream.Position;
                }
                WriteLeInt(0);                  // Crc

                if (patchEntryHeader)
                {
                    sizePatchPos = BaseOutputStream.Position;
                }

                // For local header both sizes appear in Zip64 Extended Information
                if (entry.LocalHeaderRequiresZip64 || patchEntryHeader)
                {
                    WriteLeInt(-1);
                    WriteLeInt(-1);
                }
                else
                {
                    WriteLeInt(0);                      // Compressed size
                    WriteLeInt(0);                      // Uncompressed size
                }
            }

            byte[] name = ZipStrings.ConvertToArray(entry.Flags, entry.Name);

            if (name.Length > 0xFFFF)
            {
                throw new ZipException("Entry name too long.");
            }

            var ed = new ZipExtraData(entry.ExtraData);

            if (entry.LocalHeaderRequiresZip64)
            {
                ed.StartNewEntry();
                if (headerInfoAvailable)
                {
                    ed.AddLeLong(entry.Size);
                    ed.AddLeLong(entry.CompressedSize);
                }
                else
                {
                    ed.AddLeLong(-1);
                    ed.AddLeLong(-1);
                }
                ed.AddNewEntry(1);

                if (!ed.Find(1))
                {
                    throw new ZipException("Internal error cant find extra data");
                }

                if (patchEntryHeader)
                {
                    sizePatchPos = ed.CurrentReadIndex;
                }
            }
            else
            {
                ed.Delete(1);
            }

            if (entry.AESKeySize > 0)
            {
                AddExtraDataAES(entry, ed);
            }
            byte[] extra = ed.GetEntryData();

            WriteLeShort(name.Length);
            WriteLeShort(extra.Length);

            if (name.Length > 0)
            {
                BaseOutputStream.Write(name, 0, name.Length);
            }

            if (entry.LocalHeaderRequiresZip64 && patchEntryHeader)
            {
                sizePatchPos += BaseOutputStream.Position;
            }

            if (extra.Length > 0)
            {
                BaseOutputStream.Write(extra, 0, extra.Length);
            }

            offset += ZipConstants.LocalHeaderBaseSize + name.Length + extra.Length;
            // Fix offsetOfCentraldir for AES
            if (entry.AESKeySize > 0)
            {
                offset += entry.AESOverheadSize;
            }

            // Activate the entry.
            curEntry = entry;
            crc.Reset();
            if (method == CompressionMethod.Deflated)
            {
                Deflater.Reset();
                Deflater.SetLevel(compressionLevel);
            }
            size = 0;
        }