Пример #1
0
        void LoadAndCompress()
        {
            compressedmem = new System.IO.MemoryStream();

            System.IO.FileInfo   fi         = new System.IO.FileInfo(filename);
            System.IO.FileStream filestream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            ComponentAce.Compression.Libs.zlib.ZOutputStream zstream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(compressedmem, ComponentAce.Compression.Libs.zlib.zlibConst.Z_DEFAULT_COMPRESSION);

            int totallen = (int)Size;
            int len      = buffer.Length;

            while (totallen > 0)
            {
                if (len > totallen)
                {
                    len = totallen;
                }
                filestream.Read(buffer, 0, len);
                zstream.Write(buffer, 0, len);
                totallen -= len;
            }
            zstream.finish();
            filestream.Close();

            compressedsize = (uint)compressedmem.Length;
            adler32        = AdlerCheckSum.GetAdler32(compressedmem, 0, (int)compressedmem.Length);
        }
Пример #2
0
        public string GetCodecPrivate()
        {
            if (CodecPrivateRaw == null || CodecPrivateRaw.Length == 0)
            {
                return(string.Empty);
            }

            if (ContentEncodingType == ContentEncodingTypeCompression &&
                (ContentEncodingScope & ContentEncodingScopePrivateDate) > 0 && // second bit set = private data encoded
                CodecPrivateRaw.Length > 2)
            {
                var outStream  = new MemoryStream();
                var outZStream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(outStream);
                var inStream   = new MemoryStream(CodecPrivateRaw);
                try
                {
                    CopyStream(inStream, outZStream);
                    var buffer = new byte[outZStream.TotalOut];
                    outStream.Position = 0;
                    outStream.Read(buffer, 0, buffer.Length);
                    return(Encoding.UTF8.GetString(buffer).TrimEnd('\0'));
                }
                catch
                {
                    return(Encoding.UTF8.GetString(CodecPrivateRaw).TrimEnd('\0'));
                }
                finally
                {
                    outZStream.Close();
                    inStream.Close();
                }
            }

            return(Encoding.UTF8.GetString(CodecPrivateRaw).TrimEnd('\0'));
        }
Пример #3
0
 public ZRLECompressedReader(Stream uncompressedStream)
     : base(uncompressedStream)
 {
     zlibMemoryStream       = new MemoryStream();
     zlibDecompressedStream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(zlibMemoryStream);
     uncompressedReader     = new BinaryReader(zlibMemoryStream);
 }
Пример #4
0
        /// <summary>
        /// Get data, if contentEncodingType == 0, then data is compressed with zlib
        /// </summary>
        /// <param name="matroskaTrackInfo"></param>
        /// <returns>Data byte array (uncompressed)</returns>
        public byte[] GetData(MatroskaTrackInfo matroskaTrackInfo)
        {
            if (matroskaTrackInfo.ContentEncodingType != MatroskaTrackInfo.ContentEncodingTypeCompression ||  // no compression
                (matroskaTrackInfo.ContentEncodingScope & MatroskaTrackInfo.ContentEncodingScopeTracks) == 0) // tracks not compressed
            {
                return(Data);
            }

            var outStream  = new MemoryStream();
            var outZStream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(outStream);
            var inStream   = new MemoryStream(Data);

            byte[] buffer;
            try
            {
                MatroskaTrackInfo.CopyStream(inStream, outZStream);
                buffer             = new byte[outZStream.TotalOut];
                outStream.Position = 0;
                outStream.Read(buffer, 0, buffer.Length);
            }
            finally
            {
                outZStream.Close();
                inStream.Close();
            }
            return(buffer);
        }
Пример #5
0
 private static byte[] Decompress(byte[] data)
 {
     using (var outMemoryStream = new MemoryStream())
         using (var outZStream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(outMemoryStream))
             using (var inMemoryStream = new MemoryStream(data))
             {
                 CopyStream(inMemoryStream, outZStream);
                 outZStream.finish();
                 return(outMemoryStream.ToArray());
             }
 }
Пример #6
0
 public static byte[] GZLib(byte[] bytes)
 {
     using (var msi = new MemoryStream(bytes))
     {
         using (var mso = new MemoryStream())
         {
             using (var gs = new ComponentAce.Compression.Libs.zlib.ZOutputStream(mso, ComponentAce.Compression.Libs.zlib.zlibConst.Z_DEFAULT_COMPRESSION))
             {
                 CopyTo(msi, gs);
             }
             return(mso.ToArray());
         }
     }
 }
Пример #7
0
        public Stream ExtractFile(FileEntry fileEntry, Stream streamOut = null)
        {
            var buffer = new byte[header.maxBlockSize];

            int  index = fileEntry.blockIndex;
            long size  = (long)fileEntry.fileSize.Value;
            long total = 0;

            streamIn.Seek((long)fileEntry.dataOffset.Value, SeekOrigin.Begin);

            streamOut = streamOut ?? new MemoryStream((int)(ulong)fileEntry.fileSize);
            long startPosition = streamOut.Position;

            // loop until all blocks have been read
            while (total < size)
            {
                uint blockSize = blockSizes[index];
                streamIn.Read(buffer, 0, (int)blockSize);

                var  zOut       = new ComponentAce.Compression.Libs.zlib.ZOutputStream(streamOut);
                long currentPos = streamOut.Position;

                //Detect zlib blocks
                if (buffer[0] == 0x78)
                {
                    zOut.Write(buffer, 0, (int)blockSize);
                    zOut.Flush();
                    total += zOut.TotalOut;
                }
                else
                {
                    //Make sure that no data has been written to the output stream
                    streamOut.Seek(currentPos, SeekOrigin.Begin);
                    streamOut.Write(buffer, 0, (int)blockSize);
                    total += blockSize;
                }

                index++;
            }


            streamOut.Flush();
            streamOut.Position = startPosition;
            return(streamOut);
        }
Пример #8
0
        public virtual void CopyToStream(Stream fstgt)
        {
            FileStream fssrc = File.OpenRead(sourcePath);

            try
            {
                fssrc.Seek(this.Start, SeekOrigin.Begin);
                if (compressed)
                {
                    fstgt = new ComponentAce.Compression.Libs.zlib.ZOutputStream(fstgt);
                }
                StreamUtils.CopyFromStreamToStream(fssrc, fstgt, this.length);
            }
            finally
            {
                fssrc.Close();
            }
        }
Пример #9
0
        public override void WriteDecompressed(System.IO.Stream writestream)
        {
            System.IO.FileStream filestream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.Stream     readstream = filestream;
            readstream.Position = offset;

            if (compressedmem != null && compressedmem.Length == compressedsize)
            {
                readstream          = compressedmem;
                readstream.Position = 0;
            }

            ComponentAce.Compression.Libs.zlib.ZOutputStream zstream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(writestream);

            int encryptlen = (int)compressedsize;

            byte[] data = new byte[(int)compressedsize];
            readstream.Read(data, 0, (int)compressedsize);

            if (algorithm == AlgorithmType.infilateandblowfish && encryptlen >= BlowfishNET.BlowfishECB.BLOCK_SIZE)
            {
                if (ecb == null)
                {
                    throw new Exception("Encrypted File");
                }

                if (encryptlen % BlowfishNET.BlowfishECB.BLOCK_SIZE != 0)
                {
                    encryptlen -= encryptlen % BlowfishNET.BlowfishECB.BLOCK_SIZE;
                }

                byte[] tmp = (byte [])data.Clone();
                ecb.Decrypt(tmp, 0, data, 0, encryptlen);
            }

            zstream.Write(data, 0, (int)compressedsize);
            zstream.finish();

            filestream.Close();
            writestream.Flush();
        }
Пример #10
0
			public ZRLECompressedReader(Stream uncompressedStream) : base(uncompressedStream)
			{
				zlibMemoryStream = new MemoryStream();
				zlibDecompressedStream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(zlibMemoryStream);
				uncompressedReader = new BinaryReader(zlibMemoryStream);
			}
Пример #11
0
        private void LoadBluRaySubFromMatroska(MatroskaSubtitleInfo matroskaSubtitleInfo, string fileName)
        {
            if (matroskaSubtitleInfo.ContentEncodingType == 1)
            {
                MessageBox.Show("Encrypted vobsub content not supported");
            }

            bool isValid;
            var matroska = new Matroska();

            ShowStatus(_language.ParsingMatroskaFile);
            Refresh();
            Cursor.Current = Cursors.WaitCursor;
            List<SubtitleSequence> sub = matroska.GetMatroskaSubtitle(fileName, (int)matroskaSubtitleInfo.TrackNumber, out isValid, MatroskaProgress);
            Cursor.Current = Cursors.Default;
            int noOfErrors = 0;
            string lastError = string.Empty;

            if (isValid)
            {
                MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile);
                _subtitleListViewIndex = -1;
                _subtitle.Paragraphs.Clear();
                var subtitles = new List<Nikse.SubtitleEdit.Logic.BluRaySup.BluRaySupParser.PcsData>();
                StringBuilder log = new StringBuilder();
                foreach (SubtitleSequence p in sub)
                {
                    byte[] buffer = null;
                    if (matroskaSubtitleInfo.ContentEncodingType == 0) // compressed with zlib
                    {
                        MemoryStream outStream = new MemoryStream();
                        ComponentAce.Compression.Libs.zlib.ZOutputStream outZStream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(outStream);
                        MemoryStream inStream = new MemoryStream(p.BinaryData);
                        try
                        {
                            CopyStream(inStream, outZStream);
                            buffer = new byte[outZStream.TotalOut];
                            outStream.Position = 0;
                            outStream.Read(buffer, 0, buffer.Length);
                        }
                        catch (Exception exception)
                        {
                            TimeCode tc = new TimeCode(TimeSpan.FromMilliseconds(p.StartMilliseconds));
                            lastError = tc.ToString() + ": " + exception.Message + ": " + exception.StackTrace;
                            noOfErrors++;
                        }
                        finally
                        {
                            outStream.Close();
                            outZStream.Close();
                            inStream.Close();
                        }
                    }
                    else
                    {
                        buffer = p.BinaryData;
                    }
                    if (buffer != null && buffer.Length > 100)
                    {
                        MemoryStream ms = new MemoryStream(buffer);
                        var list = BluRaySupParser.ParseBluRaySup(ms, log, true);
                        foreach (var sup in list)
                        {
                            sup.StartTime = (long)((p.StartMilliseconds - 45) * 90.0);
                            sup.EndTime = (long)((p.EndMilliseconds - 45) * 90.0);
                            subtitles.Add(sup);

                            // fix overlapping
                            if (subtitles.Count > 1 && sub[subtitles.Count - 2].EndMilliseconds > sub[subtitles.Count - 1].StartMilliseconds)
                                subtitles[subtitles.Count - 2].EndTime = subtitles[subtitles.Count - 1].StartTime - 1;
                        }
                        ms.Close();
                    }
                }

                if (noOfErrors > 0)
                {
                    MessageBox.Show(string.Format("{0} errror(s) occured during extraction of bdsup\r\n\r\n{1}", noOfErrors, lastError));
                }

                var formSubOcr = new VobSubOcr();
                _formPositionsAndSizes.SetPositionAndSize(formSubOcr);
                formSubOcr.Initialize(subtitles, Configuration.Settings.VobSubOcr, fileName);
                if (_loading)
                {
                    formSubOcr.Icon = (Icon)this.Icon.Clone();
                    formSubOcr.ShowInTaskbar = true;
                    formSubOcr.ShowIcon = true;
                }
                if (formSubOcr.ShowDialog(this) == DialogResult.OK)
                {
                    MakeHistoryForUndo(_language.BeforeImportingDvdSubtitle);

                    _subtitle.Paragraphs.Clear();
                    SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                    _subtitle.WasLoadedWithFrameNumbers = false;
                    _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate);
                    foreach (Paragraph p in formSubOcr.SubtitleFromOcr.Paragraphs)
                    {
                        _subtitle.Paragraphs.Add(p);
                    }

                    ShowSource();
                    SubtitleListview1.Fill(_subtitle, _subtitleAlternate);
                    _subtitleListViewIndex = -1;
                    SubtitleListview1.FirstVisibleIndex = -1;
                    SubtitleListview1.SelectIndexAndEnsureVisible(0);

                    _fileName = string.Empty;
                    Text = Title;

                    Configuration.Settings.Save();
                }
                _formPositionsAndSizes.SavePositionAndSize(formSubOcr);
            }
        }
Пример #12
0
        private void LoadVobSubFromMatroska(MatroskaSubtitleInfo matroskaSubtitleInfo, string fileName)
        {
            if (matroskaSubtitleInfo.ContentEncodingType == 1)
            {
                MessageBox.Show("Encrypted vobsub content not supported");
            }

            bool isValid;
            var matroska = new Matroska();

            ShowStatus(_language.ParsingMatroskaFile);
            Refresh();
            Cursor.Current = Cursors.WaitCursor;
            List<SubtitleSequence> sub = matroska.GetMatroskaSubtitle(fileName, (int)matroskaSubtitleInfo.TrackNumber, out isValid, MatroskaProgress);
            Cursor.Current = Cursors.Default;

            if (isValid)
            {
                MakeHistoryForUndo(_language.BeforeImportFromMatroskaFile);
                _subtitleListViewIndex = -1;
                _subtitle.Paragraphs.Clear();

                List<VobSubMergedPack> mergedVobSubPacks = new List<VobSubMergedPack>();
                Nikse.SubtitleEdit.Logic.VobSub.Idx idx = new Logic.VobSub.Idx(matroskaSubtitleInfo.CodecPrivate.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                foreach (SubtitleSequence p in sub)
                {
                    if (matroskaSubtitleInfo.ContentEncodingType == 0) // compressed with zlib
                    {
                        bool error = false;
                        MemoryStream outStream = new MemoryStream();
                        ComponentAce.Compression.Libs.zlib.ZOutputStream outZStream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(outStream);
                        MemoryStream inStream = new MemoryStream(p.BinaryData);
                        byte[] buffer = null;
                        try
                        {
                            CopyStream(inStream, outZStream);
                            buffer = new byte[outZStream.TotalOut];
                            outStream.Position = 0;
                            outStream.Read(buffer, 0, buffer.Length);
                        }
                        catch (Exception exception)
                        {
                            MessageBox.Show(exception.Message + Environment.NewLine + Environment.NewLine + exception.StackTrace);
                            error = true;
                        }
                        finally
                        {
                            outStream.Close();
                            outZStream.Close();
                            inStream.Close();
                        }

                        if (!error)
                            mergedVobSubPacks.Add(new VobSubMergedPack(buffer, TimeSpan.FromMilliseconds(p.StartMilliseconds), 32, null));
                    }
                    else
                    {
                        mergedVobSubPacks.Add(new VobSubMergedPack(p.BinaryData, TimeSpan.FromMilliseconds(p.StartMilliseconds), 32, null));
                    }
                    mergedVobSubPacks[mergedVobSubPacks.Count - 1].EndTime = TimeSpan.FromMilliseconds(p.EndMilliseconds);

                    // fix overlapping (some versions of Handbrake makes overlapping time codes - thx Hawke)
                    if (mergedVobSubPacks.Count > 1 && mergedVobSubPacks[mergedVobSubPacks.Count - 2].EndTime > mergedVobSubPacks[mergedVobSubPacks.Count - 1].StartTime)
                        mergedVobSubPacks[mergedVobSubPacks.Count - 2].EndTime = TimeSpan.FromMilliseconds(mergedVobSubPacks[mergedVobSubPacks.Count - 1].StartTime.TotalMilliseconds - 1);
                }

                var formSubOcr = new VobSubOcr();
                _formPositionsAndSizes.SetPositionAndSize(formSubOcr);
                formSubOcr.Initialize(mergedVobSubPacks, idx.Palette, Configuration.Settings.VobSubOcr, null); //TODO - language???
                if (_loading)
                {
                    formSubOcr.Icon = (Icon)this.Icon.Clone();
                    formSubOcr.ShowInTaskbar = true;
                    formSubOcr.ShowIcon = true;
                }
                if (formSubOcr.ShowDialog(this) == DialogResult.OK)
                {
                    ResetSubtitle();
                    _subtitle.Paragraphs.Clear();
                    _subtitle.WasLoadedWithFrameNumbers = false;
                    foreach (Paragraph p in formSubOcr.SubtitleFromOcr.Paragraphs)
                        _subtitle.Paragraphs.Add(p);

                    ShowSource();
                    SubtitleListview1.Fill(_subtitle, _subtitleAlternate);
                    _subtitleListViewIndex = -1;
                    SubtitleListview1.FirstVisibleIndex = -1;
                    SubtitleListview1.SelectIndexAndEnsureVisible(0);

                    _fileName =  Path.GetFileNameWithoutExtension(fileName);
                    _converted = true;
                    Text = Title;

                    Configuration.Settings.Save();
                }
                _formPositionsAndSizes.SavePositionAndSize(formSubOcr);
            }
        }
Пример #13
0
        private byte[] decompress(byte[] inData)
        {
            byte[] result = null;
            using (var outMemoryStream = new System.IO.MemoryStream())
            using (var inMemoryStream = new System.IO.MemoryStream(inData))
            using (var outZStream = new ComponentAce.Compression.Libs.zlib.ZOutputStream(outMemoryStream))
            {
                try
                {
                    CopyStream(inMemoryStream, outZStream);
                    result = new byte[outMemoryStream.Length];
                    outMemoryStream.Position = 0;
                    var ok = outMemoryStream.Read(result, 0, (int)outMemoryStream.Length);
                }
                finally
                {
                    outZStream.Close();
                    outMemoryStream.Close();
                    inMemoryStream.Close();
                }
            }

            return result;

        }
Пример #14
0
        public virtual void CopyToStream(string sourceNbuFile, Stream fstgt)
        {
            FileStream fssrc = File.OpenRead(sourceNbuFile);
            try
            {
                fssrc.Seek(this.Start, SeekOrigin.Begin);

                byte[] buff = new byte[1024];
                long rest = this.length;

                if (compressed)
                {
                    fstgt = new ComponentAce.Compression.Libs.zlib.ZOutputStream(fstgt);
                }

                while (true)
                {
                    if (fssrc.Read(buff, 0, buff.Length) == 0)
                    {
                        throw new EndOfStreamException();
                    }
                    if (rest > buff.Length)
                    {
                        fstgt.Write(buff, 0, buff.Length);
                        rest -= buff.Length;
                    }
                    else
                    {
                        fstgt.Write(buff, 0, (int)rest);
                        break;
                    }
                }
            }
            finally
            {
                fssrc.Close();
            }
        }