Exemplo n.º 1
0
        public void LoadIndex(Stream fs)
        {
            Int64 nIndexPosition = Info.IndexOffset;

            fs.Seek(nIndexPosition, SeekOrigin.Begin);
            MountPoint = ReadString(fs);
            NumEntries = ReadInt32(fs);
            for (int i = 0; i < NumEntries; i++)
            {
                string Filename = ReadString(fs);

                Console.WriteLine("[" + (i + 1) + " of " + NumEntries + "] " + Filename);

                PakEntry Entry = new PakEntry();
                Entry.DeSerialize(fs);
                nIndexPosition = fs.Position;

                if (Entry.bEncrypted != 0)
                {
                    throw new Exception("Encription not supported!");
                }

                fs.Seek(Entry.Offset, SeekOrigin.Begin);
                PakEntry FileHeader = new PakEntry();
                FileHeader.DeSerialize(fs);

                if (!System.Collections.StructuralComparisons.StructuralEqualityComparer.Equals(Entry.Hash, FileHeader.Hash))
                {
                    throw new Exception("Wrong hash!");
                }

                string FilePath = Path.Combine(Program.OutPath, Filename.Replace('/', '\\'));
                CreatePath(FilePath);
                Stream fs_out = new FileStream(FilePath, FileMode.Create);

                if (Entry.CompressionMethod == 0)
                {
                    byte[] Data = new byte[Entry.Size];
                    fs.Read(Data, 0, Data.Length);
                    fs_out.Write(Data, 0, Data.Length);
                }
                else if (Entry.CompressionMethod == 1)
                {
                    for (int j = 0; j < Entry.CompressionBlocks.Count; j++)
                    {
                        fs.Seek(Entry.CompressionBlocks[j].CompressedStart + 2, SeekOrigin.Begin);
                        System.IO.Compression.DeflateStream fs_decompressed = new System.IO.Compression.DeflateStream(fs, System.IO.Compression.CompressionMode.Decompress, true);
                        fs_decompressed.CopyTo(fs_out);
                        fs_decompressed.Close();
                    }
                }
                else
                {
                    throw new Exception("This compression method not supported!");
                }

                fs_out.Close();
                fs.Seek(nIndexPosition, SeekOrigin.Begin);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Looks up the given file in the Mix database and returns a <see cref="MemoryStream"/> for it.
        /// </summary>
        /// <param name="fileName">The file to find.</param>
        /// <returns>Returns a <see cref="MemoryStream"/> if found, <see cref="null"/> otherwise.</returns>
        public static Stream GetStream(string fileName)
        {
            Console.WriteLine("Mix: get stream for \"{0}\"...", fileName);
            if (File.Exists(Path.Combine("data", fileName)))
            {
                return(new MemoryStream(File.ReadAllBytes(Path.Combine("data", fileName))));
            }
            if (!fileList.ContainsKey(fileName))
            {
                throw new FileNotFoundException("File " + fileName + " was not found in the MIX files.");
            }
            //MemoryStream ret;
            var entry = fileList[fileName];

            using (var mStream = new BinaryReader(File.Open(entry.MixFile, FileMode.Open)))
            {
                mStream.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
                if (!entry.IsCompressed)
                {
                    return(new MemoryStream(mStream.ReadBytes(entry.Length)));
                }
                else
                {
                    var cStream      = new MemoryStream(mStream.ReadBytes(entry.Length));
                    var decompressor = new System.IO.Compression.DeflateStream(cStream, System.IO.Compression.CompressionMode.Decompress);
                    var outStream    = new MemoryStream();
                    decompressor.CopyTo(outStream);
                    //ret = new byte[outStream.Length];
                    outStream.Seek(0, SeekOrigin.Begin);
                    return(outStream);
                    //outStream.Read(ret, 0, ret.Length);
                }
            }
        }
Exemplo n.º 3
0
        private IEnumerable <VersionDescription> ParseResponse(WebHeaderCollection header, byte[] body)
        {
            switch (header.Get("Content-Encoding"))
            {
            case "gzip":
                using (var dst = new System.IO.MemoryStream())
                    using (var s = new System.IO.Compression.GZipStream(new System.IO.MemoryStream(body), System.IO.Compression.CompressionMode.Decompress)) {
                        s.CopyTo(dst);
                        dst.Flush();
                        body = dst.ToArray();
                    }
                break;

            case "deflate":
                using (var dst = new System.IO.MemoryStream())
                    using (var s = new System.IO.Compression.DeflateStream(new System.IO.MemoryStream(body), System.IO.Compression.CompressionMode.Decompress)) {
                        s.CopyTo(dst);
                        dst.Flush();
                        body = dst.ToArray();
                    }
                break;

            default:
                break;
            }
            return(ParseAppCast(System.Text.Encoding.UTF8.GetString(body)));
        }
Exemplo n.º 4
0
 public void Decompress()
 {
     using (var streamUncompressed = new System.IO.FileStream(@"C:\Users\Adrian\Downloads\7z1604-x64_2.exe", System.IO.FileMode.Create))
         using (var streamCompressed = new System.IO.FileStream(@"C:\Users\Adrian\Downloads\7z1604-x64.z", System.IO.FileMode.Open))
         {
             var deflateStream = new System.IO.Compression.DeflateStream(streamCompressed, System.IO.Compression.CompressionMode.Decompress);
             deflateStream.CopyTo(streamUncompressed);
         }
 }
Exemplo n.º 5
0
 private static byte[] StreamDecompress(byte[] data)
 {
     using var dataStream = new MemoryStream(data);
     using var val        = new MemoryStream();
     using (var compressor = new System.IO.Compression.DeflateStream(dataStream, System.IO.Compression.CompressionMode.Decompress)) {
         compressor.CopyTo(val);
     }
     return(val.ToArray());
 }
Exemplo n.º 6
0
 internal static byte[] Decompress(byte[] data)
 {
     using var dataStream = new MemoryStream(data);
     using var val        = new MemoryStream(DecompressedLengthEstimate(data));
     using (var compressor = new System.IO.Compression.DeflateStream(dataStream, System.IO.Compression.CompressionMode.Decompress)) {
         compressor.CopyTo(val);
     }
     return(val.ToArray());
 }
Exemplo n.º 7
0
        public void DeSerialize(Stream fs)
        {
            BulkDataFlags = ReadUInt32(fs);

            bPayloadAtEndOfFile = ((BulkDataFlags & 0x01) == 0x01);
            bCompressed         = ((BulkDataFlags & 0x02) == 0x02);

            ElementCount         = ReadInt32(fs);
            BulkDataSizeOnDisk   = ReadInt32(fs);
            BulkDataOffsetInFile = ReadInt64(fs);

            if (!bPayloadAtEndOfFile)
            {
                BulkData = new byte[BulkDataSizeOnDisk];
                fs.Read(BulkData, 0, BulkDataSizeOnDisk);
            }
            else
            {
                Int64 nCurOffset = fs.Position;
                fs.Seek(BulkDataOffsetInFile + FileSummary.BulkDataStartOffset, SeekOrigin.Begin);
                BulkData = new byte[BulkDataSizeOnDisk];
                fs.Read(BulkData, 0, BulkDataSizeOnDisk);
                fs.Seek(nCurOffset, SeekOrigin.Begin);
            }

            if (bCompressed)
            {
                MemoryStream ms = new MemoryStream(BulkData);
                PackageFileTag       = ReadInt64(ms);
                CompressionChunkSize = ReadInt64(ms);
                CompressedSize       = ReadInt64(ms);
                UncompressedSize     = ReadInt64(ms);
                TotalChunkCount      = (UncompressedSize + CompressionChunkSize - 1) / CompressionChunkSize;
                for (int i = 0; i < TotalChunkCount; i++)
                {
                    ChanksInfo.Add(new KeyValuePair <long, long>(ReadInt64(ms), ReadInt64(ms)));
                }
                BulkDataDecompressed = new byte[ElementCount];
                MemoryStream ms_decompressed = new MemoryStream(BulkDataDecompressed);
                Int64        CurrentOffset   = ms.Position;
                for (int i = 0; i < ChanksInfo.Count; i++)
                {
                    ms.Seek(CurrentOffset + 2, SeekOrigin.Begin);
                    System.IO.Compression.DeflateStream fs_decompressed = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress, true);
                    fs_decompressed.CopyTo(ms_decompressed);
                    fs_decompressed.Close();
                    CurrentOffset += ChanksInfo[i].Key;
                }
            }
            else
            {
                BulkDataDecompressed = BulkData;
            }
        }
Exemplo n.º 8
0
        private static byte[] StreamDecompress(byte[] data, int size)
        {
            using var dataStream = new MemoryStream(data);
            var output = new byte[size];

            using (var val = new MemoryStream(output)) {
                using var compressor = new System.IO.Compression.DeflateStream(dataStream, System.IO.Compression.CompressionMode.Decompress);
                compressor.CopyTo(val);
            }
            return(output);
        }
Exemplo n.º 9
0
        public void DecompressData(Stream pStream, out MemoryStream Outz)
        {
            MemoryStream msInner = new MemoryStream();

            using (System.IO.Compression.DeflateStream z = new System.IO.Compression.DeflateStream(pStream, System.IO.Compression.CompressionMode.Decompress))
            {
                MemoryStream _o = new MemoryStream();
                z.CopyTo(_o);
                _o.Seek(0, SeekOrigin.Begin);
                Outz = _o;
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Decompresses a deflate compressed animation tree
 /// </summary>
 /// <param name="input"></param>
 /// <returns></returns>
 private static byte[] DecompressAnimTree(byte[] input)
 {
     using (var compressedStream = new MemoryStream(input))
     {
         using (var decompressedStream = new MemoryStream())
         {
             using (var deflater = new System.IO.Compression.DeflateStream(compressedStream, System.IO.Compression.CompressionMode.Decompress))
                 deflater.CopyTo(decompressedStream);
             return(decompressedStream.ToArray());
         }
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// Uncompresses a byte array compressed with <c>Compress</c>.
        /// </summary>
        /// <param name="compressedBytes">Compressed byte array.</param>
        /// <returns>An uncompressed byte array.</returns>
        static public byte[] Uncompress(byte[] compressedBytes)
        {
            MemoryStream inStream = new MemoryStream(compressedBytes);

            using (MemoryStream outStream = new MemoryStream())
                using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(inStream, System.IO.Compression.CompressionMode.Decompress))
                {
                    deflate.CopyTo(outStream);

                    return(outStream.ToArray());
                }
        }
Exemplo n.º 12
0
 private static MemoryStream Inflate(Stream s, bool leaveOpen = false)
 {
     using (var output = new MemoryStream())
     {
         using (Stream input = new DeflateStream(s, CompressionMode.Decompress, leaveOpen))
         {
             input.CopyTo(output);
             input.Close();
             return(new MemoryStream(output.ToArray()));
         }
     }
 }
        public void asdaTest()
        {
            //var tempPath = @"C:\Users\user\Downloads\CUETools_2.1.2a\CUE Tools\LocalDB.xml";
            var tempPath = @"C:\Users\user\Downloads\CUETools_2.1.9\CUE Tools\LocalDB.xml";
            var path     = tempPath + @".z";

            //using (var fileStream = new System.IO.FileStream(tempPath, System.IO.FileMode.CreateNew))
            //using (var deflateStream = new System.IO.Compression.DeflateStream(fileStream, System.IO.Compression.CompressionMode.Compress))
            using (var fileStream1 = new System.IO.FileStream(tempPath, System.IO.FileMode.CreateNew))
                using (var fileStream2 = new System.IO.FileStream(path, System.IO.FileMode.Open))
                    using (var deflateStream = new System.IO.Compression.DeflateStream(fileStream2, System.IO.Compression.CompressionMode.Decompress))
                        deflateStream.CopyTo(fileStream1);
        }
Exemplo n.º 14
0
        static public byte[] Inflate(byte[] input)
        {
            using (var inflatedStream = new System.IO.MemoryStream())
            {
                using (var deflateStream = new System.IO.Compression.DeflateStream(
                           new System.IO.MemoryStream(input), System.IO.Compression.CompressionMode.Decompress))
                {
                    deflateStream.CopyTo(inflatedStream);

                    return(inflatedStream.ToArray());
                }
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Decompresses the byte array using System.IO.Compression.DeflateStream
        /// </summary>
        /// <param name="bytes">The byte array containing compressed data</param>
        /// <returns>A byte array of the decompressed data</returns>
        //[System.Diagnostics.DebuggerStepThrough]
        public static byte[] FromCompressed(this byte[] bytes)
        {
            var input = new System.IO.MemoryStream(bytes);

            //var output = new MemoryStream();
            using (var output = new System.IO.MemoryStream())
            {
                using (var deflater = new System.IO.Compression.DeflateStream(input, System.IO.Compression.CompressionMode.Decompress))
                {
                    deflater.CopyTo(output);
                }
                return(output.ToArray());
            }
        }
Exemplo n.º 16
0
        internal override Stream GetDataStream(V8ItemHandle Handle)
        {
            MemoryStream resultStream = new MemoryStream();

            using (var ReadStream = base.GetDataStream(Handle))
            {
                using (var DeflateStream = new System.IO.Compression.DeflateStream(ReadStream, System.IO.Compression.CompressionMode.Decompress))
                {
                    DeflateStream.CopyTo(resultStream);
                }
            }

            resultStream.Position = 0;
            return(resultStream);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Decompresses the byte array using System.IO.Compression.DeflateStream
        /// </summary>
        /// <param name="bytes">The byte array containing compressed data</param>
        /// <returns>A byte array of the decompressed data</returns>
        public static byte[] AsDecompressed(this byte[] bytes)
        {
            var input  = new MemoryStream(bytes);
            var output = new MemoryStream();

            using (var deflater = new System.IO.Compression.DeflateStream(input, System.IO.Compression.CompressionMode.Decompress))
            {
                deflater.CopyTo(output);
            }
#if WINDOWS_PORTABLE || WINDOWS_UWP || NETCOREAPP1_0 || NETCOREAPP1_1
            return(output.ToArray());
#else
            return(output.GetBuffer());
#endif
        }
Exemplo n.º 18
0
        public void DecompressData()
        {
            byte[] data = new byte[] { 43, 73, 45, 46, 1, 0 };
            byte[] data2;

            using (var streamUncompressed = new System.IO.MemoryStream())
                using (var streamCompressed = new System.IO.MemoryStream(data))
                {
                    var deflateStream = new System.IO.Compression.DeflateStream(streamCompressed, System.IO.Compression.CompressionMode.Decompress, true);
                    deflateStream.CopyTo(streamUncompressed);
                    deflateStream.Flush();
                    deflateStream.Close();

                    data2 = streamUncompressed.ToArray();
                }
        }
Exemplo n.º 19
0
 private byte[] Decompress(byte[] inputBuffer)
 {
     byte[] output;
     using (var inputstream = new MemoryStream(inputBuffer))
     {
         using (var outputstream = new MemoryStream())
         {
             using (var algostream = new System.IO.Compression.DeflateStream(inputstream, System.IO.Compression.CompressionMode.Decompress, true))
             {
                 algostream.CopyTo(outputstream);
             }
             outputstream.Seek(0L, SeekOrigin.Begin);
             output = outputstream.ToArray();
         }
     }
     return(output);
 }
Exemplo n.º 20
0
        internal static byte[] Deflate(byte[] source_byte, System.IO.Compression.CompressionMode type_deflate)
        {
            byte[] dest_byte = null;

            using (System.IO.MemoryStream dest_mem = new System.IO.MemoryStream())
            {
                using (System.IO.MemoryStream source_mem = new System.IO.MemoryStream(source_byte))
                {
                    if (source_mem.CanSeek)
                    {
                        source_mem.Seek(0, System.IO.SeekOrigin.Begin);
                    }

                    if (type_deflate == System.IO.Compression.CompressionMode.Compress)
                    {
                        using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(dest_mem, type_deflate))
                        {
                            source_mem.CopyTo(deflate);
                            deflate.Flush();
                        }
                    }
                    else
                    {
                        using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(source_mem, type_deflate))
                        {
                            deflate.CopyTo(dest_mem);
                            deflate.Flush();
                        }
                    }

                    source_mem.Flush();
                }
                if (dest_mem.CanSeek)
                {
                    dest_mem.Seek(0, System.IO.SeekOrigin.Begin);
                }
                dest_byte = dest_mem.ToArray();
                dest_mem.Flush();
            }

            return(dest_byte);
        }
Exemplo n.º 21
0
        X509Certificate2 GetDefaultSelfSignedCertFromResource()
        {
            var currasm            = Assembly.GetExecutingAssembly();
            var pfxcmp             = SharpSocks.host2cert_pfx;
            X509Certificate2 x5092 = null;

            using (var pfxcmpbts = new System.IO.MemoryStream(pfxcmp))
                using (var decompressedCert = new System.IO.MemoryStream())
                {
                    using (var decompressionStream = new System.IO.Compression.DeflateStream(pfxcmpbts, System.IO.Compression.CompressionMode.Decompress))
                    {
                        decompressionStream.CopyTo(decompressedCert);
                        decompressionStream.Close();
                        var sc = new System.Security.SecureString();
                        "SharpSocksKey".ToCharArray().ToList().ForEach(x => { sc.AppendChar(x); });
                        x5092 = new X509Certificate2(decompressedCert.ToArray(), sc);
                    }
                }
            return(x5092);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Распаковка
        /// </summary>
        /// <param name="compressedMemoryStream"></param>
        /// <param name="outBufStream"></param>
        /// <returns></returns>
        public static bool Inflate(MemoryTributary compressedMemoryStream, out MemoryTributary outBufStream)
        {
            bool result = true;

            outBufStream = new MemoryTributary();

            try
            {
                compressedMemoryStream.Position = 0;
                System.IO.Compression.DeflateStream decompressStream = new System.IO.Compression.DeflateStream(compressedMemoryStream, System.IO.Compression.CompressionMode.Decompress);
                decompressStream.CopyTo(outBufStream);
            }
            catch (Exception ex)
            {
                outBufStream = compressedMemoryStream;
                result       = false;
            }

            return(result);
        }
Exemplo n.º 23
0
        private static bool TryLoadFromCosturaResource(string refAssemblyFilename)
        {
            string refAssemblyFilenameCostura = "costura." + refAssemblyFilename.ToLower() + ".zip";

            using (Stream stream = GetManagedEntryAssembly().GetManifestResourceStream(refAssemblyFilenameCostura))
            {
                if (stream == null)
                {
                    return(false);
                }

                //TODO: Costura fody zips the files, so unzipping is required
                System.IO.Compression.DeflateStream unzipper = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress);
                MemoryStream decompressed = new MemoryStream();
                unzipper.CopyTo(decompressed);
                Assembly.ReflectionOnlyLoad(decompressed.ToArray());
            }

            return(true);
        }
Exemplo n.º 24
0
            public override Stream GetStream(Stream container)
            {
                switch (CompressionMethod)
                {
                case CompressionMethod.None:
                    return(new SubStream(container, DataStartOffset, CompressedSize));

                case CompressionMethod.Deflate:
                    var sub  = new SubStream(container, DataStartOffset, CompressedSize);
                    var defl = new System.IO.Compression.DeflateStream(sub, System.IO.Compression.CompressionMode.Decompress);
                    using (defl)
                    {
                        var ms = new MemoryStream((int)UncompressedSize);
                        defl.CopyTo(ms);
                        ms.Seek(0, SeekOrigin.Begin);
                        return(ms);
                    }

                default:
                    throw new NotSupportedException("This zip compression method (" + CompressionMethod + ") is not supported.");
                }
            }
Exemplo n.º 25
0
        internal static List <OutEntity> DecompressFromBase64String(this string compressedString)
        {
            //var compressedString = File.ReadAllText("Assets\\unsyncd.txt");
            var barray = System.Convert.FromBase64String(compressedString);

            var toReturn = new MemoryStream();

            var jsonString = string.Empty;

            using (var outStream = new System.IO.MemoryStream(barray))
                using (var deflateStream = new System.IO.Compression
                                           .DeflateStream(outStream,
                                                          System.IO.Compression.CompressionMode.Decompress))
                {
                    deflateStream.CopyTo(toReturn);
                    deflateStream.Close();
                    jsonString = System.Text.Encoding.UTF8.GetString(toReturn.ToArray());
                }
            var js = Newtonsoft.Json.JsonConvert
                     .DeserializeObject <List <MobileCollector.model.OutEntity> >(jsonString);

            return(js);
        }
Exemplo n.º 26
0
        private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
        {
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            AssemblyName assemblyName = new AssemblyName(args.Name);
            string path = assemblyName.Name + ".gzip";
            if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false)
            {
                path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path);
            }

            using (Stream stream = executingAssembly.GetManifestResourceStream(path))
            {
                if (stream == null)
                    return null;

                using (var dll = new MemoryStream())
                using (var inflate = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Decompress))
                {
                    inflate.CopyTo(dll);
                    return Assembly.Load(dll.ToArray());
                }
            }
        }
Exemplo n.º 27
0
        short[,] DecompressImageData(byte[] data, int width, int height)
        {
            // Decompress with deflate, then remove filter

            short[,] pixels = new short[height, width];
            using (MemoryStream imageData = new MemoryStream())
            {
                using (MemoryStream ms = new MemoryStream(data))
                {
                    // skip 2 bytes for zlib specs
                    ms.ReadByte();
                    ms.ReadByte();
                    using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress, false))
                    {
                        deflate.CopyTo(imageData);
                    }
                }
                
                // Remove Filter
                imageData.Seek(0, SeekOrigin.Begin);

                for (int i = 0; i < height; ++i)
                {
                    // beginning of scanline, there's one byte of filter type
                    byte filterType = (byte)imageData.ReadByte();
                    for (int j = 0; j < width; ++j)
                    {
                        // within the scanline, each pixel is 2 bytes
                        byte hiX = (byte)imageData.ReadByte();
                        byte loX = (byte)imageData.ReadByte();

                        byte hiA = 0;
                        byte loA = 0;
                        byte hiB = 0;
                        byte loB = 0;
                        byte hiC = 0;
                        byte loC = 0;

                        if (j > 0)
                        {
                            // A = previous pixel
                            hiA = (byte)(pixels[i, j - 1] >> 8);
                            loA = (byte)(pixels[i, j - 1] & 0xFF);
                        }

                        // B = pixel above scanline
                        if (i > 0)
                        {
                            hiB = (byte)(pixels[i - 1, j] >> 8);
                            loB = (byte)(pixels[i - 1, j] & 0xFF);
                        }

                        if (i > 0 && j > 0)
                        {
                            // C previous pixel above scanline
                            hiC = (byte)(pixels[i - 1, j - 1] >> 8);
                            loC = (byte)(pixels[i - 1, j - 1] & 0xFF);
                        }

                        pixels[i, j] = (short)(ReconstructFilteredByte(filterType, hiX, hiA, hiB, hiC) << 8 |
                            ReconstructFilteredByte(filterType, loX, loA, loB, loC));
                    }
                }
            }

            return pixels;
        }
Exemplo n.º 28
0
        public bool InvokeMasterlistupdate(ref string message)
        {
            try
            {
                HtmlWeb      hw          = new HtmlWeb();
                HtmlDocument doc         = hw.Load(ConfigurationSettings.AppSettings["CSCAMasterlist"]);
                string       relativeUrl = string.Empty;
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
                {
                    string href = link.Attributes["href"].Value;
                    if (href.Contains("GermanMasterList.zip"))
                    {
                        relativeUrl = href;
                        break;
                    }
                }
                if (relativeUrl == "")
                {
                    message = "No valid url with CSCA found";
                    return(false);
                }
                else
                {
                    string aboluteUrl = "https://bsi.bund.de" + relativeUrl;
                    using (WebClient client = new WebClient())
                    {
                        client.DownloadFile(aboluteUrl, ConfigurationSettings.AppSettings["JavaReaderPath"] + "CSCAMasterlist.zip");
                        callback.CSCADownloaded("CSCAMasterlist.zip");
                    }

                    System.IO.Compression.ZipArchive zip = new System.IO.Compression.ZipArchive(new FileStream(ConfigurationSettings.AppSettings["JavaReaderPath"] + "CSCAMasterlist.zip", FileMode.Open), System.IO.Compression.ZipArchiveMode.Read);
                    foreach (System.IO.Compression.ZipArchiveEntry zae in zip.Entries)
                    {
                        System.IO.Compression.DeflateStream ds = (System.IO.Compression.DeflateStream)zae.Open();

                        MemoryStream ms = new MemoryStream();
                        ds.CopyTo(ms);

                        File.WriteAllBytes(ConfigurationSettings.AppSettings["JavaReaderPath"] + "CSCAMasterlist.ml", ms.ToArray());
                        callback.CSCAUnzipped("CSCAMasterlist.ml");
                        break;
                    }

                    bool res = ParseMasterlist();
                    if (res)
                    {
                        callback.CSCAParsed("masterlist-content-current.data");
                        message = "Certificates extracted";
                    }
                    else
                    {
                        message = "Error extracting certificates";
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                message = "Error updating masterlist: " + ex.Message;
                return(false);
            }
        }
Exemplo n.º 29
0
        short[,] DecompressImageData(byte[] data, int width, int height)
        {
            // Decompress with deflate, then remove filter

            short[,] pixels = new short[height, width];
            using (MemoryStream imageData = new MemoryStream())
            {
                using (MemoryStream ms = new MemoryStream(data))
                {
                    // skip 2 bytes for zlib specs
                    ms.ReadByte();
                    ms.ReadByte();
                    using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress, false))
                    {
                        deflate.CopyTo(imageData);
                    }
                }

                // Remove Filter
                imageData.Seek(0, SeekOrigin.Begin);

                for (int i = 0; i < height; ++i)
                {
                    // beginning of scanline, there's one byte of filter type
                    byte filterType = (byte)imageData.ReadByte();
                    for (int j = 0; j < width; ++j)
                    {
                        // within the scanline, each pixel is 2 bytes
                        byte hiX = (byte)imageData.ReadByte();
                        byte loX = (byte)imageData.ReadByte();

                        byte hiA = 0;
                        byte loA = 0;
                        byte hiB = 0;
                        byte loB = 0;
                        byte hiC = 0;
                        byte loC = 0;

                        if (j > 0)
                        {
                            // A = previous pixel
                            hiA = (byte)(pixels[i, j - 1] >> 8);
                            loA = (byte)(pixels[i, j - 1] & 0xFF);
                        }

                        // B = pixel above scanline
                        if (i > 0)
                        {
                            hiB = (byte)(pixels[i - 1, j] >> 8);
                            loB = (byte)(pixels[i - 1, j] & 0xFF);
                        }

                        if (i > 0 && j > 0)
                        {
                            // C previous pixel above scanline
                            hiC = (byte)(pixels[i - 1, j - 1] >> 8);
                            loC = (byte)(pixels[i - 1, j - 1] & 0xFF);
                        }

                        pixels[i, j] = (short)(ReconstructFilteredByte(filterType, hiX, hiA, hiB, hiC) << 8 |
                                               ReconstructFilteredByte(filterType, loX, loA, loB, loC));
                    }
                }
            }

            return(pixels);
        }
Exemplo n.º 30
0
        internal static byte[] Deflate(byte[] source_byte, System.IO.Compression.CompressionMode type_deflate)
        {
            byte[] dest_byte = null;

            using (System.IO.MemoryStream dest_mem = new System.IO.MemoryStream())
            {
                using (System.IO.MemoryStream source_mem = new System.IO.MemoryStream(source_byte))
                {
                    if (source_mem.CanSeek)
                    {
                        source_mem.Seek(0, System.IO.SeekOrigin.Begin);
                    }

                    if (type_deflate == System.IO.Compression.CompressionMode.Compress)
                    {
                        using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(dest_mem, type_deflate))
                        {
                            source_mem.CopyTo(deflate);
                            deflate.Flush();
                        }
                    }
                    else
                    {
                        using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(source_mem, type_deflate))
                        {
                            deflate.CopyTo(dest_mem);
                            deflate.Flush();
                        }
                    }

                    source_mem.Flush();
                }
                if (dest_mem.CanSeek)
                {
                    dest_mem.Seek(0, System.IO.SeekOrigin.Begin);
                }
                dest_byte = dest_mem.ToArray();
                dest_mem.Flush();
            }

            return dest_byte;
        }
Exemplo n.º 31
0
            int Inflate(byte[] pData, out byte[] outBuf, UInt32 DataSize, out UInt32 out_buf_len)
            {                
                int ret = 0;

                out_buf_len = DataSize + CHUNK;
                outBuf = new byte[(int)CHUNK];

                try
                {
                    using (MemoryStream memStreamExtract = new MemoryStream())
                    {
                        using (MemoryStream memStream = new MemoryStream(pData))
                        {
                            using (System.IO.Compression.DeflateStream strmDef = new System.IO.Compression.DeflateStream(memStream, System.IO.Compression.CompressionMode.Decompress))
                            {
                                strmDef.CopyTo(memStreamExtract);
                            }

                        }
                        outBuf = memStreamExtract.ToArray();
                        out_buf_len = (UInt32)outBuf.Length;
                    }
                }
                catch
                {
                    outBuf = pData;
                    out_buf_len = DataSize;
                    ret = -1;
                }

                return ret;
            }
Exemplo n.º 32
0
            private bool Inflate(MemoryTributary compressedMemoryStream, out MemoryTributary outBufStream)
            {
                bool result = true;

                outBufStream = new MemoryTributary();

                try
                {
                    compressedMemoryStream.Position = 0;
                    System.IO.Compression.DeflateStream decompressStream = new System.IO.Compression.DeflateStream(compressedMemoryStream, System.IO.Compression.CompressionMode.Decompress);
                    decompressStream.CopyTo(outBufStream);                  
                }
                catch (Exception ex)
                {
                    outBufStream = compressedMemoryStream;
                    result = false;
                }

                return result;
            }
Exemplo n.º 33
0
        public virtual FileInfo[] DecompressArchive(string rootDir, FileInfo archive, RirCompressionType type)
        {
            if (archive == null)
            {
                throw new ArgumentNullException(nameof(archive));
            }

            if (!archive.Exists || archive.Length < 1)
            {
                throw new IOException("The archive is either invalid or missing.");
            }

            DirectoryInfo targetDir = new DirectoryInfo(Path.Combine(CacheDirectory, rootDir));

            if (targetDir.Exists)
            {
                targetDir.Delete(true);
            }
            targetDir.Refresh();
            if (!targetDir.Exists)
            {
                targetDir.Create();
            }
            targetDir.Refresh();

            if (Settings.Current.VerboseOutput)
            {
                Log.WriteLine("Decompressing archive %@ as type %@...", LogLevel.Debug, archive.Name, type.ToString());
            }

            List <FileInfo> outputFiles = new List <FileInfo>();

            using (FileStream archiveStream = new FileStream(archive.FullName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                Stream decompressor = null;
                System.IO.Compression.ZipArchive zipArchive = null;
                switch (type)
                {
                case RirCompressionType.None:
                    decompressor = archiveStream;
                    break;

                case RirCompressionType.Gzip:
                    decompressor = new System.IO.Compression.GZipStream(archiveStream, System.IO.Compression.CompressionMode.Decompress, true);
                    break;

                case RirCompressionType.Zip:
                    zipArchive = new System.IO.Compression.ZipArchive(archiveStream, System.IO.Compression.ZipArchiveMode.Read, true);
                    break;

                case RirCompressionType.Deflate:
                    decompressor = new System.IO.Compression.DeflateStream(archiveStream, System.IO.Compression.CompressionMode.Decompress, true);
                    break;

                default:
                    throw new InvalidOperationException("Internal Error. Unknown compression algorithm.");
                }

                if (zipArchive != null)
                {
                    // Specific case for Zip archives
                    Log.WriteLine("Found %@ entries inside the Zip archive. Decompressing...", LogLevel.Debug, zipArchive.Entries.Count.ToString("N0"));
                    foreach (System.IO.Compression.ZipArchiveEntry entry in zipArchive.Entries)
                    {
                        Log.WriteLine("Decompressing file %@  %@ bytes...", LogLevel.Debug, entry.Name, entry.Length.ToString("N0"));
                        FileInfo targetFile = new FileInfo(Path.Combine(targetDir.FullName, entry.FullName));

                        if (!targetFile.Directory.Exists)
                        {
                            targetFile.Directory.Create();
                        }

                        using (Stream decompressedStream = targetFile.Open(FileMode.Open))
                            using (FileStream fsout = new FileStream(targetFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None))
                                decompressedStream.CopyTo(fsout);

                        targetFile.Refresh();
                        outputFiles.Add(targetFile);
                    }

                    Log.WriteLine("Archive decompression completed.", LogLevel.Debug);
                }
                else
                {
                    // Common case for single-file compressed streams
                    FileInfo targetFile = new FileInfo(Path.Combine(targetDir.FullName, Path.GetFileNameWithoutExtension(archive.Name)));

                    if (!targetFile.Directory.Exists)
                    {
                        targetFile.Directory.Create();
                    }

                    using (FileStream fsout = new FileStream(targetFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        Log.WriteLine("Decompressing file %@...", LogLevel.Debug, targetFile.Name);
                        decompressor.CopyTo(fsout);
                        Log.WriteLine("Done. %@ bytes written.", LogLevel.Debug, fsout.Position.ToString("N0"));
                    }

                    targetFile.Refresh();
                    outputFiles.Add(targetFile);
                }
            }

            return(outputFiles.ToArray());
        }