Ejemplo n.º 1
0
    public static void Decompress(FileInfo fi)
    {
        // Get the stream of the source file.
        using (FileStream inFile = fi.OpenRead())
        {
            // Get original file extension,
            // for example "doc" from report.doc.cmp.
            string curFile = fi.FullName;
            string origName = curFile.Remove(curFile.Length
                    - fi.Extension.Length);

            //Create the decompressed file.
            using (FileStream outFile = File.Create(origName))
            {
                using (DeflateStream Decompress = new DeflateStream(inFile,
                    CompressionMode.Decompress))
                {
                    // Copy the decompression stream
                    // into the output file.
                    Decompress.CopyTo(outFile);

                    Console.WriteLine("Decompressed: {0}", fi.Name);
                }
            }
        }
    }
Ejemplo n.º 2
0
    static void DecompressProfile(string srcfile, string dstfile)
    {
        try
        {
            Console.WriteLine("Reading source...");
            byte[] x = File.ReadAllBytes(srcfile); // load all bytes
            byte[] y = new byte[x.Length-0x1A];

            Console.WriteLine("Copying array...");
            Array.Copy(x, 0x1A, y, 0, y.Length); // skip hash (0x14) + uncompresed size (0x4) + zlib compression type (0x2) = 0x1A

            Console.WriteLine("Initializing streams...");
            DeflateStream defStream = new DeflateStream(new MemoryStream(y),CompressionMode.Decompress);
            FileStream fs = File.Create(dstfile);

            Console.WriteLine("Decompressing data to destination...");
            defStream.CopyTo(fs);

            Console.WriteLine("Decompression done.\n" + dstfile);
            defStream.Close();
            fs.Close();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.GetType().Name + " | " + ex.Message);
            Console.Beep();
        }
        return;
    }
Ejemplo n.º 3
0
        public static Span <T> Read <T>(string filePath) where T : unmanaged
        {
            // open file
            var fileStream = File.OpenRead(filePath);

            // validate signature
            Span <byte> signature = stackalloc byte[8];

            fileStream.Read(signature);
            AggregationFile.ValidateSignature(signature, _signature);

            // version
            var version = fileStream.ReadByte();

            if (version != 1)
            {
                throw new Exception("Only *.nex files of version 1 are supported.");
            }

            // uncompressed size
            Span <byte> sizeBytes = stackalloc byte[4];

            fileStream.Read(sizeBytes);
            var uncompressedSize = BitConverter.ToInt32(sizeBytes);

            // return data
            using var decompressedStream  = new MemoryStream(capacity: uncompressedSize);
            using var decompressionStream = new DeflateStream(fileStream, CompressionMode.Decompress);

            decompressionStream.CopyTo(decompressedStream);

            var span = decompressedStream
                       .GetBuffer()
                       .AsSpan(0, (int)decompressedStream.Length);

            return(MemoryMarshal
                   .Cast <byte, T>(span));
        }
Ejemplo n.º 4
0
        override public bool ReadMod()
        {
            using (FileStream fileStream = File.OpenRead(this.path))
            {
                BinaryReader binaryReader = new BinaryReader(fileStream);
                if (Encoding.ASCII.GetString(binaryReader.ReadBytes(4)) != "TMOD")
                {
                    return(false);
                }

                info.modloaderversion = new Version(binaryReader.ReadString());
                info.modhash          = binaryReader.ReadBytes(20);
                info.modsignature     = binaryReader.ReadBytes(256);
                fileStream.Seek(4, SeekOrigin.Current);

                DeflateStream inflateStream = new DeflateStream(fileStream, CompressionMode.Decompress);
                inflateStream.CopyTo(this.tempfile);
                inflateStream.Close();

                this.tempfile.Seek(0, SeekOrigin.Begin);
                BinaryReader tempFileBinaryReader = new BinaryReader(this.tempfile);

                info.modname    = tempFileBinaryReader.ReadString();
                info.modversion = new Version(tempFileBinaryReader.ReadString());
                info.filecount  = tempFileBinaryReader.ReadInt32();

                for (int index = 0; index < info.filecount; index++)
                {
                    tModFileInfo file = new tModFileInfo();
                    string       path = tempFileBinaryReader.ReadString().Replace("\\", "/");
                    file.filesize  = tempFileBinaryReader.ReadInt32();
                    file.filestart = this.tempfile.Position;
                    this.tempfile.Seek(file.filesize, SeekOrigin.Current);
                    this.files.Add(path, file);
                }
            }
            return(true);
        }
Ejemplo n.º 5
0
        private List <Package> HandleBatch(McpeBatch batch)
        {
            var messages = new List <Package>();

            // Get bytes
            byte[] payload = batch.payload;
            // Decompress bytes

            Console.WriteLine("Package:\n" + Package.HexDump(payload));

            MemoryStream stream = new MemoryStream(payload);

            if (stream.ReadByte() != 0x78)
            {
                throw new InvalidDataException("Incorrect ZLib header. Expected 0x78 0x9C");
            }
            stream.ReadByte();
            using (var defStream2 = new DeflateStream(stream, CompressionMode.Decompress, false))
            {
                // Get actual package out of bytes
                MemoryStream destination = new MemoryStream();
                defStream2.CopyTo(destination);
                destination.Position = 0;
                NbtBinaryReader reader         = new NbtBinaryReader(destination, true);
                int             len            = reader.ReadInt32();
                byte[]          internalBuffer = reader.ReadBytes(len);

                Console.WriteLine($"Package [len={len}:\n" + Package.HexDump(internalBuffer));

                messages.Add(PackageFactory.CreatePackage(internalBuffer[0], internalBuffer, "mcpe") ?? new UnknownPackage(internalBuffer[0], internalBuffer));
                if (destination.Length > destination.Position)
                {
                    throw new Exception("Have more data");
                }
            }

            return(messages);
        }
Ejemplo n.º 6
0
        private void Connection_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
        {
            FileDownloadInfo info = e.UserState as FileDownloadInfo;

            if (info == null)
            {
                return;
            }

            if (e.Error != null || e.Cancelled)
            {
                info.Args.Progress = -1;
                FileDownloadError?.Invoke(this, info.Args);
            }

            info.Args.Progress = 1;
            FileDownloadProgress?.Invoke(this, info.Args);

            lock (info.LocalFile.Directory)
            {
                if (!info.LocalFile.Directory.Exists)
                {
                    info.LocalFile.Directory.Create();
                }
            }

            var           fs = info.LocalFile.OpenWrite();
            var           ms = new MemoryStream(e.Result);
            DeflateStream df = new DeflateStream(ms, CompressionMode.Decompress);

            df.CopyTo(fs);
            df.Close();
            fs.Close();

            FileDownloadCompleted?.Invoke(this, info.Args);

            StartDLJob(info);
        }
Ejemplo n.º 7
0
        // Methods
        public static Map FromId(double id)
        {
            lock (CheckLock)
            {
                if (MapId_Map.Count > 20)
                {
                    MapId_Map.Remove(MapId_Map.Keys.First());
                }

                if (MapId_Map.ContainsKey(id))
                {
                    return(MapId_Map[id]);
                }

                var str      = id % 10 + "/" + id + ".dlm";
                var mapBytes = D2pFileManager.GetMapBytes(str);
                if (mapBytes != null)
                {
                    MemoryStream stream = new MemoryStream();
                    using (MemoryStream decompressMapStream = new MemoryStream(mapBytes)
                    {
                        Position = 2
                    })
                        using (DeflateStream mapDeflateStream = new DeflateStream(decompressMapStream, CompressionMode.Decompress))
                        {
                            mapDeflateStream.CopyTo(stream);
                        }
                    stream.Position = 0;
                    //Console.WriteLine(BitConverter.ToString(stream.ToArray()).Replace("-",""));
                    BigEndianReader Raw = new BigEndianReader(stream);
                    Map             map = new Map();
                    map.Init(Raw);
                    return(map);
                }
                MapId_Map.Add(id, null);
                return(null);
            }
        }
        /// <summary>
        /// Decompresses data using the deflate_stream compression algorithm.
        /// </summary>
        /// <param name="input">The data to decompress.</param>
        /// <param name="length">The expected length of the decompressed data.</param>
        /// <returns>A decompressed byte array.</returns>
        private byte[] DecompressUsingDeflateStream(byte[] input, int length)
        {
            if (input[0] == 0x78 && (input[1] == 0x9c || input[1] == 0x5E))
            {
                _buffer.Write(input, 2, input.Length - 2);
            }
            else
            {
                _buffer.Write(input, 0, input.Length);
            }

            _buffer.Position = 0;
            var target = new MemoryStream();

            _deflateDecompressStream.CopyTo(target, length);
            _deflateDecompressStream.Flush();
            var decompressedData = target.ToArray();

            _buffer.SetLength(0);
            target.Dispose();

            return(decompressedData);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Extracts an embedded resource which has originally been compressed by Fody.Costura.
        /// Output folder is user specified.
        /// </summary>
        /// <param name="resourceName">The name of the resource to extract to disk. e.g. ReloadedAssembler.exe</param>
        /// <param name="folderLocation">The folder to extract the file to</param>
        /// <returns>true if the operation succeeded, else false</returns>
        public static bool ExtractResource(string resourceName, string folderLocation)
        {
            try
            {
                // Costura embeds names with lowercase.
                string lowerCase = resourceName.ToLower();

                var embeddedAssemblerStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("costura." + lowerCase + ".compressed");
                if (embeddedAssemblerStream == null)
                {
                    return(false);
                }

                using (Stream outputFileStream = new FileStream(folderLocation + "//" + resourceName, FileMode.Create, FileAccess.Write))
                    using (Stream decompressStream = new DeflateStream(embeddedAssemblerStream, CompressionMode.Decompress))
                    {
                        decompressStream.CopyTo(outputFileStream);
                    }

                return(true);
            }
            catch { return(false); }
        }
Ejemplo n.º 10
0
    public static byte[] DecompressZlib(byte[] data, int decompressedSize)
    {
        try
        {
            var unpackedData = new byte[decompressedSize];

            using (var inflate = new DeflateStream(new MemoryStream(data, 2, data.Length - 2), CompressionMode.Decompress))
            {
                var decompressed = new MemoryStream();
                inflate.CopyTo(decompressed);
                decompressed.Seek(0, SeekOrigin.Begin);
                for (int i = 0; i < decompressedSize; i++)
                {
                    unpackedData[i] = (byte)decompressed.ReadByte();
                }
            }
            return(unpackedData);
        }
        catch
        {
            return(null);
        }
    }
Ejemplo n.º 11
0
        private static byte[] Decompress(byte[] input, CompressMode mode)
        {
            var output = new MemoryStream();

            if (mode == CompressMode.Deflate)
            {
                using (var compressStream = new MemoryStream(input))
                    using (var decompressor = new DeflateStream(compressStream, CompressionMode.Decompress))
                        decompressor.CopyTo(output);

                output.Position = 0;
                return(output.ToArray());
            }
            else
            {
                using (var compressStream = new MemoryStream(input))
                    using (var decompressor = new DeflateStream(compressStream, CompressionMode.Decompress))
                        decompressor.CopyTo(output);

                output.Position = 0;
                return(output.ToArray());
            }
        }
Ejemplo n.º 12
0
        private string DecompressFile(string compressedFile)
        {
            string decompressedFile = "";

            byte[] fileInBytes = File.ReadAllBytes(compressedFile);
            if (fileInBytes[0] == 0x78 && fileInBytes[1] == 0x9C)
            {
                // We skip the first 2 bytes as these are only needed for the header.
                Stream byteStreamOriginal = new MemoryStream(fileInBytes, 2, fileInBytes.Length - 2);
                using (DeflateStream decompressionStream = new DeflateStream(byteStreamOriginal, CompressionMode.Decompress))
                {
                    string currentFileName = compressedFile;
                    decompressedFile = currentFileName.Replace(".compressed", ".exe");
                    using (FileStream decompressedFileStream = File.Create(decompressedFile))
                    {
                        decompressionStream.CopyTo(decompressedFileStream);
                        Console.WriteLine("-> {0}", decompressedFile);
                    }
                    File.Delete(compressedFile);
                }
            }
            return(decompressedFile);
        }
Ejemplo n.º 13
0
    private static byte[] Deflate(byte[] buffer, CompressionMode compressionMode)
    {
        using var source = compressionMode == CompressionMode.Compress
            ? new MemoryStream()
            : new MemoryStream(buffer);

        using var deflate = new DeflateStream(source, compressionMode);

        if (compressionMode == CompressionMode.Compress)
        {
            deflate.Write(buffer, 0, buffer.Length);
            deflate.Dispose();

            return(source.ToArray());
        }
        else
        {
            using var decompressed = new MemoryStream();
            deflate.CopyTo(decompressed);

            return(decompressed.ToArray());
        }
    }
Ejemplo n.º 14
0
 private void packetHandler(Byte[] bytes)
 {
     Byte[][] packets = GoIMProtocol.Cut(bytes);
     Program.log("INFO", userpoint + " Received " + packets.Length + " Packets from Remote");
     foreach (Byte[] packet in packets)
     {
         GoIMMessage data = GoIMProtocol.Decode(packet);
         if (data.operation == GoIMProtocol.WS_OP_MESSAGE && data.protocolVersion == 2)
         {
             Program.log("INFO", userpoint + " Received Compressed Packets from Remote");
             var           rawStream     = new MemoryStream(data.Body, 2, data.Body.Length - 2);
             DeflateStream deflateStream = new DeflateStream(rawStream, CompressionMode.Decompress, false);
             MemoryStream  memoryStream  = new MemoryStream();
             deflateStream.CopyTo(memoryStream);
             packetHandler(memoryStream.ToArray());
         }
         else
         {
             Program.log("INFO", userpoint + " Sending " + packet.Length + " Bytes to Local.");
             Send(packet);
         }
     }
 }
Ejemplo n.º 15
0
        public Caff(BinaryReader br, CaffHeader header, uint caffOffset, int index)
        {
            m_header = header;
            m_index  = index;
            br.BaseStream.Seek(m_header.stream0Offset + caffOffset, SeekOrigin.Begin);

            streams[0] = new MemoryStream();

            if (m_header.stream0CSize != m_header.stream0UncSize)
            {
                MemoryStream tempStream = new MemoryStream();
                Globals.CopyStream(br.BaseStream, tempStream, (int)m_header.stream0CSize);

                tempStream.Seek(2, SeekOrigin.Begin);
                using (DeflateStream deflateStream = new DeflateStream(tempStream, CompressionMode.Decompress))
                {
                    deflateStream.CopyTo(streams[0]);
                    deflateStream.Dispose();
                }
            }

            GetChunkStrings();
        }
Ejemplo n.º 16
0
        public static byte[] Decompress(byte[] data)
        {
            byte[] decompressedArray = null;
            try
            {
                using (MemoryStream decompressedStream = new MemoryStream())
                {
                    using (MemoryStream compressStream = new MemoryStream(data))
                    {
                        using (DeflateStream deflateStream = new DeflateStream(compressStream, CompressionMode.Decompress))
                        {
                            deflateStream.CopyTo(decompressedStream);
                        }
                    }
                    decompressedArray = decompressedStream.ToArray();
                }
            }
            catch (Exception)
            {
            }

            return(decompressedArray);
        }
Ejemplo n.º 17
0
        public string Decompress(string compressedText)
        {
            if (string.IsNullOrWhiteSpace(compressedText))
            {
                throw new ArgumentNullException("compressedText");
            }

            var compressedBytes = compressedText.Base64DecodeBytes();

            using (var input = new MemoryStream(compressedBytes))
            {
                using (var decompressStream = new MemoryStream())
                {
                    using (var decompressor = new DeflateStream(input, CompressionMode.Decompress))
                    {
                        decompressor.CopyTo(decompressStream);
                        decompressor.Dispose();
                        var decompressedBytes = decompressStream.ToArray();
                        return(Encoding.UTF8.GetString(decompressedBytes));
                    }
                }
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Extracts a single file inside of a PAK archive to a destination directory.
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="outputDirectory"></param>
        public void Extract(string filename, string outputDirectory)
        {
            var pak = this.PakFiles.FirstOrDefault(x => x.FileName == filename);

            if (pak == null)
            {
                throw new FileNotFoundException($"Error. Could not find file inside the archive.", filename);
            }

            var buffer     = new byte[pak.UncompressedSize];
            var outputFile = Path.GetFullPath(Path.Combine(outputDirectory, filename));

            Directory.CreateDirectory(Path.GetDirectoryName(outputFile));

            using (var ms = new MemoryStream(pak.Zipped))
                using (var writer = File.Create(outputFile))
                {
                    using (var ds = new DeflateStream(ms, CompressionMode.Decompress))
                    {
                        ds.CopyTo(writer);
                    }
                }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Decompress byte array
        /// </summary>
        /// <param name="data">input byte array</param>
        /// <exception cref="ArgumentNullException">Thrown if data is null or empty</exception>
        /// <returns>compressed string as byte array</returns>
        public static string DecompressString(this byte[] data)
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentNullException(nameof(data));
            }

            using (var inputStream = new MemoryStream(data))
            {
                using (var outputStream = new MemoryStream())
                {
                    using (var deflate = new DeflateStream(inputStream, CompressionMode.Decompress))
                    {
                        deflate.CopyTo(outputStream);
                    }

                    outputStream.Position = 0;
                    var reader = new StreamReader(outputStream);

                    return(reader.ReadToEnd());
                }
            }
        }
 private string Unzip(byte[] bytes)
 {
     using (var msi = new MemoryStream(bytes))
         using (var mso = new MemoryStream())
         {
             if (_compressionType == CompressionType.deflate)
             {
                 using (var gs = new DeflateStream(msi, CompressionMode.Decompress))
                 {
                     gs.CopyTo(mso);
                 }
             }
             else if (_compressionType == CompressionType.gzip)
             {
                 using (var gs = new GZipStream(msi, CompressionMode.Decompress))
                 {
                     CopyTo(gs, mso);
                 }
             }
             var bytes1 = mso.ToArray();
             return(Encoding.UTF8.GetString(bytes1));
         }
 }
Ejemplo n.º 21
0
        public static T Decode <T>(string wireData)
        {
            // Step 1: Base64 decode the wire data into a gzip blob
            byte[] gzipData = Convert.FromBase64String(wireData);

            // Step 2: Decompress gzip blob into JSON
            string json = null;

            using (var decompressedStream = new MemoryStream())
                using (var compressedStream = new MemoryStream(gzipData))
                    using (var deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
                    {
                        deflateStream.CopyTo(decompressedStream);
                        decompressedStream.Position = 0;
                        using (var streamReader = new StreamReader(decompressedStream))
                        {
                            json = streamReader.ReadToEnd();
                        }
                    }

            // Step 3: Deserialize the JSON string into a strongly-typed object
            return(JsonConvert.DeserializeObject <T>(json, _jsonSerializerSettings));
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Unpacks a compressed block.
        /// </summary>
        /// <param name="compressed">The compressed data.</param>
        /// <param name="type">The type data type contained in the block.</param>
        /// <returns>The decompressed block.</returns>
        static byte[] UnpackCompressedBlock(byte[] compressed, out DataType type)
        {
            byte[] data;
            using (var decompressed = new MemoryStream())
            {
                using (var compressedStream = new MemoryStream(compressed, 2, compressed.Length - 6))
                    using (var decompressor = new DeflateStream(compressedStream, CompressionMode.Decompress))
                    {
                        decompressor.CopyTo(decompressed);
                    }

                decompressed.Position = 0;
                var buf = new byte[4];
                decompressed.Read(buf, 0, 4);
                type = (DataType)BitConverter.ToInt32(buf, 0);
                decompressed.Read(buf, 0, 4);
                var size = BitConverter.ToInt32(buf, 0);
                data = new byte[size];
                decompressed.Read(data, 0, size);
            }

            return(data);
        }
Ejemplo n.º 23
0
        private byte[] FlateDecode(byte[] bytes)
        {
            using (MemoryStream inputStream = new MemoryStream(bytes))
            {
                using (MemoryStream outputStream = new MemoryStream())
                {
                    using (DeflateStream decodeStream = new DeflateStream(inputStream, CompressionMode.Decompress))
                    {
                        // Skip the zlib 2 byte header
                        inputStream.Position = 2;
                        decodeStream.CopyTo(outputStream);
                        bytes = outputStream.GetBuffer();
                    }
                }
            }

            if (Dictionary.ContainsName("Predictor"))
            {
                throw new NotImplementedException($"Cannot process FlatDecode predictors.");
            }

            return(bytes);
        }
Ejemplo n.º 24
0
        public static SourceText GetEmbeddedSource(this MetadataReader reader, DocumentHandle document)
        {
            byte[] bytes = (from handle in reader.GetCustomDebugInformation(document)
                            let cdi = reader.GetCustomDebugInformation(handle)
                                      where reader.GetGuid(cdi.Kind) == PortableCustomDebugInfoKinds.EmbeddedSource
                                      select reader.GetBlobBytes(cdi.Value)).SingleOrDefault();

            if (bytes == null)
            {
                return(null);
            }

            int uncompressedSize = BitConverter.ToInt32(bytes, 0);
            var stream           = new MemoryStream(bytes, sizeof(int), bytes.Length - sizeof(int));

            if (uncompressedSize != 0)
            {
                var decompressed = new MemoryStream(uncompressedSize);

                using (var deflater = new DeflateStream(stream, CompressionMode.Decompress))
                {
                    deflater.CopyTo(decompressed);
                }

                if (decompressed.Length != uncompressedSize)
                {
                    throw new InvalidDataException();
                }

                stream = decompressed;
            }

            using (stream)
            {
                return(EncodedStringText.Create(stream));
            }
        }
Ejemplo n.º 25
0
        public void DeflateStream_Compress_1()
        {
            void Template(string sampleFileName, ZLibCompLevel level)
            {
                string filePath = Path.Combine(TestSetup.SampleDir, sampleFileName);

                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    using (MemoryStream compMs = new MemoryStream())
                        using (MemoryStream decompMs = new MemoryStream())
                        {
                            using (DeflateStream zs = new DeflateStream(compMs, ZLibMode.Compress, level, true))
                            {
                                fs.CopyTo(zs);
                            }

                            fs.Position     = 0;
                            compMs.Position = 0;

                            // Decompress compMs again
                            using (DeflateStream zs = new DeflateStream(compMs, ZLibMode.Decompress, true))
                            {
                                zs.CopyTo(decompMs);
                            }

                            decompMs.Position = 0;

                            // Compare SHA256 Digest
                            byte[] decompDigest = TestSetup.SHA256Digest(decompMs);
                            byte[] fileDigest   = TestSetup.SHA256Digest(fs);
                            Assert.IsTrue(decompDigest.SequenceEqual(fileDigest));
                        }
            }

            Template("ex1.jpg", ZLibCompLevel.Default);
            Template("ex2.jpg", ZLibCompLevel.BestCompression);
            Template("ex3.jpg", ZLibCompLevel.BestSpeed);
        }
Ejemplo n.º 26
0
        public IList <T> Get <T, TProperty>(TProperty key)
        {
            var hash = BitConverter.ToString(SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(Convert.ToString(key)))).Replace("-", "").ToLower();

            try
            {
                using (var fs = _fileSystem.File.Open(String.Join(@"\", _root, Directory, ObjectsDirectory, String.Join("", hash.ToCharArray().Take(2)), String.Join("", hash.ToCharArray().Skip(2).Take(38))), FileMode.Open, FileAccess.Read))
                {
                    using (var os = new MemoryStream())
                    {
                        var bf = new BinaryFormatter();

                        using (var ds = new DeflateStream(fs, CompressionMode.Decompress, true))
                        {
                            ds.CopyTo(os);
                        }

                        os.Position = 0;

                        if (os.Length == 0)
                        {
                            return(new List <T>());
                        }

                        return((List <T>)bf.Deserialize(os));
                    }
                }
            }
            catch (FileNotFoundException)
            {
                return(new List <T>());
            }
            catch (DirectoryNotFoundException)
            {
                return(new List <T>());
            }
        }
Ejemplo n.º 27
0
        public DiscordSocketApiClient(RestClientProvider restClientProvider, WebSocketProvider webSocketProvider, JsonSerializer serializer = null, RequestQueue requestQueue = null)
            : base(restClientProvider, serializer, requestQueue)
        {
            _gatewayClient = webSocketProvider();
            //_gatewayClient.SetHeader("user-agent", DiscordConfig.UserAgent); (Causes issues in .Net 4.6+)
            _gatewayClient.BinaryMessage += async(data, index, count) =>
            {
                using (var compressed = new MemoryStream(data, index + 2, count - 2))
                    using (var decompressed = new MemoryStream())
                    {
                        using (var zlib = new DeflateStream(compressed, CompressionMode.Decompress))
                            zlib.CopyTo(decompressed);
                        decompressed.Position = 0;
                        using (var reader = new StreamReader(decompressed))
                            using (var jsonReader = new JsonTextReader(reader))
                            {
                                var msg = _serializer.Deserialize <WebSocketMessage>(jsonReader);
                                await _receivedGatewayEvent.InvokeAsync((GatewayOpCode)msg.Operation, msg.Sequence, msg.Type, msg.Payload).ConfigureAwait(false);
                            }
                    }
            };
            _gatewayClient.TextMessage += async text =>
            {
                using (var reader = new StringReader(text))
                    using (var jsonReader = new JsonTextReader(reader))
                    {
                        var msg = _serializer.Deserialize <WebSocketMessage>(jsonReader);
                        await _receivedGatewayEvent.InvokeAsync((GatewayOpCode)msg.Operation, msg.Sequence, msg.Type, msg.Payload).ConfigureAwait(false);
                    }
            };
            _gatewayClient.Closed += async ex =>
            {
                await DisconnectAsync().ConfigureAwait(false);

                await _disconnectedEvent.InvokeAsync(ex).ConfigureAwait(false);
            };
        }
Ejemplo n.º 28
0
        /// <summary>
        ///     Loads the Manifest state from the provided File.
        /// </summary>
        /// <remarks>
        ///     This method expects a DEFLATE-compressed XML binary.
        /// </remarks>
        /// <returns>
        ///     Instance of a Manifest type.
        /// </returns>
        /// <exception cref="FileNotFoundException">
        ///     Specified manifest binary does not exist on the filesystem.
        /// </exception>
        public Manifest Load()
        {
            byte[] Inflate(byte[] deflatedBytes)
            {
                using (var inflatedStream = new MemoryStream())
                    using (var deflatedStream = new MemoryStream(deflatedBytes))
                        using (var compressStream = new DeflateStream(deflatedStream, CompressionMode.Decompress))
                        {
                            compressStream.CopyTo(inflatedStream);
                            compressStream.Close();
                            return(inflatedStream.ToArray());
                        }
            }

            if (!System.IO.File.Exists(_file))
            {
                throw new FileNotFoundException("Specified manifest binary does not exist on the filesystem.");
            }

            var deflatedData = System.IO.File.ReadAllBytes(_file);
            var inflatedData = Inflate(deflatedData);
            var utf8AsString = Encoding.UTF8.GetString(inflatedData);

            var serializer = new XmlSerializer(typeof(Manifest));

            using (var reader = new StringReader(utf8AsString))
            {
                try
                {
                    return((Manifest)serializer.Deserialize(reader));
                }
                catch (InvalidOperationException)
                {
                    throw new ManifestException("Failed to deserialise decompressed metadata binary.");
                }
            }
        }
Ejemplo n.º 29
0
        private void ProcessMessage(ZMessage msg)
        {
            // extract frames
            var topic = msg[0].ToString(Encoding.UTF8);
            var flags = msg[1].ReadUInt32();
            var data  = msg[2].Read();
            var sent  = DateTimeOffset.FromUnixTimeMilliseconds(msg[3].ReadInt64()).DateTime;

            // TMP FIX
            if (flags != 0 && ((flags & 1) == 0))
            {
                flags = BitConverter.ToUInt32(BitConverter.GetBytes(flags).ToNewReverseArray());
            }

            // compressed
            if ((flags & 1) == 1)
            {
                using (var stm = new MemoryStream(data))
                {
                    using (var stmOut = new MemoryStream())
                    {
                        using (var ds = new DeflateStream(stm, CompressionMode.Decompress))
                        {
                            ds.CopyTo(stmOut);
                        }

                        data = stmOut.ToArray();
                    }
                }
            }

            // convert
            var content = Encoding.UTF8.GetString(data);

            // publish
            messageBus.SendMessage(new BtStreamMessage(topic, content, sent, DateTime.UtcNow));
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Inflates a deflated zlib byte stream
        /// </summary>
        /// <param name="data">The data to deflate</param>
        /// <returns>The deflated result</returns>
        public byte[] ProcessZlib(byte[] data)
        {
            // See RFC 1950 (https://tools.ietf.org/html/rfc1950)
            // zlib adds a header to DEFLATE streams - usually 2 bytes,
            // but can be 6 bytes if FDICT is set.
            // There's also 4 checksum bytes at the end of the stream.

            byte zlibCmf = data[0];

            if ((zlibCmf & 0x0F) != 0x08)
            {
                throw new NotSupportedException("Only the DEFLATE algorithm is supported for zlib data.");
            }

            const int zlibFooter = 4;
            int       zlibHeader = 2;

            // If the FDICT bit (0x20) is 1, then the 4-byte dictionary is included in the header, we need to skip it
            byte zlibFlg = data[1];

            if ((zlibFlg & 0x20) == 0x20)
            {
                zlibHeader += 4;
            }

            using (MemoryStream ms = new MemoryStream(data, zlibHeader, data.Length - (zlibHeader + zlibFooter)))
            {
                using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress))
                {
                    using (MemoryStream target = new MemoryStream())
                    {
                        ds.CopyTo(target);
                        return(target.ToArray());
                    }
                }
            }
        }
Ejemplo n.º 31
0
            public override void Decompress(DataInput input, int originalLength, int offset, int length, BytesRef bytes)
            {
                if (Debugging.AssertsEnabled)
                {
                    Debugging.Assert(offset + length <= originalLength);
                }
                if (length == 0)
                {
                    bytes.Length = 0;
                    return;
                }

                byte[] compressedBytes = new byte[input.ReadVInt32()];
                input.ReadBytes(compressedBytes, 0, compressedBytes.Length);
                byte[] decompressedBytes = null;

                using (MemoryStream decompressedStream = new MemoryStream())
                {
                    using (MemoryStream compressedStream = new MemoryStream(compressedBytes))
                    {
                        using (DeflateStream dStream = new DeflateStream(compressedStream, System.IO.Compression.CompressionMode.Decompress))
                        {
                            dStream.CopyTo(decompressedStream);
                        }
                    }
                    decompressedBytes = decompressedStream.ToArray();
                }

                if (decompressedBytes.Length != originalLength)
                {
                    throw new CorruptIndexException("Length mismatch: " + decompressedBytes.Length + " != " + originalLength + " (resource=" + input + ")");
                }

                bytes.Bytes  = decompressedBytes;
                bytes.Offset = offset;
                bytes.Length = length;
            }
Ejemplo n.º 32
0
        /// <summary>
        /// 解压缩字节数组.
        /// </summary>
        private void uncompress(string algorithm = "deflate")
        {
            position = 0;

            if (algorithm == "zlib")
            {
                int firstByte = _memoryStream.ReadByte();

                int secondByte = _memoryStream.ReadByte();

                if (((firstByte == 0x78) && (secondByte == 0x9C)) || ((firstByte == 0x78) && (secondByte == 0xDA)) || ((firstByte == 0x58) && (secondByte == 0x85)))
                {
                }
                else
                {
                    position = 0;
                }
            }

            DeflateStream deflateStream = new DeflateStream(_memoryStream, CompressionMode.Decompress, false);

            MemoryStream ms = new MemoryStream();

            deflateStream.CopyTo(ms);

            deflateStream.Close();

            _memoryStream.Close();

            _memoryStream.Dispose();

            _memoryStream = ms;

            _memoryStream.Position = 0;

            Reset();
        }
        /// <exception cref="PngProcessingException"/>
        /// <exception cref="System.IO.IOException"/>
        private static IEnumerable<Directory> ProcessChunk([NotNull] PngChunk chunk)
        {
            // For more guidance:
            // http://www.w3.org/TR/PNG-Decoders.html#D.Text-chunk-processing
            // http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.iCCP
            // by spec, PNG is generally supposed to use this encoding
            const string defaultEncodingName = "ISO-8859-1";
            var defaultEncoding = Encoding.GetEncoding(defaultEncodingName);

            var chunkType = chunk.ChunkType;
            var bytes = chunk.Bytes;

            if (chunkType == PngChunkType.IHDR)
            {
                var header = new PngHeader(bytes);
                var directory = new PngDirectory(PngChunkType.IHDR);
                directory.Set(PngDirectory.TagImageWidth, header.ImageWidth);
                directory.Set(PngDirectory.TagImageHeight, header.ImageHeight);
                directory.Set(PngDirectory.TagBitsPerSample, header.BitsPerSample);
                directory.Set(PngDirectory.TagColorType, header.ColorType.NumericValue);
                directory.Set(PngDirectory.TagCompressionType, header.CompressionType);
                directory.Set(PngDirectory.TagFilterMethod, header.FilterMethod);
                directory.Set(PngDirectory.TagInterlaceMethod, header.InterlaceMethod);
                yield return directory;
            }
            else if (chunkType == PngChunkType.PLTE)
            {
                var directory = new PngDirectory(PngChunkType.PLTE);
                directory.Set(PngDirectory.TagPaletteSize, bytes.Length / 3);
                yield return directory;
            }
            else if (chunkType == PngChunkType.tRNS)
            {
                var directory = new PngDirectory(PngChunkType.tRNS);
                directory.Set(PngDirectory.TagPaletteHasTransparency, 1);
                yield return directory;
            }
            else if (chunkType == PngChunkType.sRGB)
            {
                int srgbRenderingIntent = unchecked((sbyte)bytes[0]);
                var directory = new PngDirectory(PngChunkType.sRGB);
                directory.Set(PngDirectory.TagSrgbRenderingIntent, srgbRenderingIntent);
                yield return directory;
            }
            else if (chunkType == PngChunkType.cHRM)
            {
                var chromaticities = new PngChromaticities(bytes);
                var directory = new PngChromaticitiesDirectory();
                directory.Set(PngChromaticitiesDirectory.TagWhitePointX, chromaticities.WhitePointX);
                directory.Set(PngChromaticitiesDirectory.TagWhitePointY, chromaticities.WhitePointY);
                directory.Set(PngChromaticitiesDirectory.TagRedX, chromaticities.RedX);
                directory.Set(PngChromaticitiesDirectory.TagRedY, chromaticities.RedY);
                directory.Set(PngChromaticitiesDirectory.TagGreenX, chromaticities.GreenX);
                directory.Set(PngChromaticitiesDirectory.TagGreenY, chromaticities.GreenY);
                directory.Set(PngChromaticitiesDirectory.TagBlueX, chromaticities.BlueX);
                directory.Set(PngChromaticitiesDirectory.TagBlueY, chromaticities.BlueY);
                yield return directory;
            }
            else if (chunkType == PngChunkType.gAMA)
            {
                var gammaInt = ByteConvert.ToInt32BigEndian(bytes);
                var directory = new PngDirectory(PngChunkType.gAMA);
                directory.Set(PngDirectory.TagGamma, gammaInt / 100000.0);
                yield return directory;
            }
            else if (chunkType == PngChunkType.iCCP)
            {
                var reader = new SequentialByteArrayReader(bytes);
                var profileName = reader.GetNullTerminatedStringValue(maxLengthBytes: 79);
                var directory = new PngDirectory(PngChunkType.iCCP);
                directory.Set(PngDirectory.TagIccProfileName, profileName);
                var compressionMethod = reader.GetSByte();
                if (compressionMethod == 0)
                {
                    // Only compression method allowed by the spec is zero: deflate
                    // This assumes 1-byte-per-char, which it is by spec.
                    var bytesLeft = bytes.Length - profileName.Bytes.Length - 2;
                    var compressedProfile = reader.GetBytes(bytesLeft);
                    using (var inflaterStream = new InflaterInputStream(new MemoryStream(compressedProfile)))
                    {
                        var iccDirectory = new IccReader().Extract(new IndexedCapturingReader(inflaterStream));
                        iccDirectory.Parent = directory;
                        yield return iccDirectory;
                    }
                }
                else
                {
                    directory.AddError("Invalid compression method value");
                }
                yield return directory;
            }
            else if (chunkType == PngChunkType.bKGD)
            {
                var directory = new PngDirectory(PngChunkType.bKGD);
                directory.Set(PngDirectory.TagBackgroundColor, bytes);
                yield return directory;
            }
            else if (chunkType == PngChunkType.tEXt)
            {
                var reader = new SequentialByteArrayReader(bytes);
                var keyword = reader.GetNullTerminatedStringValue(maxLengthBytes: 79).ToString(defaultEncoding);
                var bytesLeft = bytes.Length - keyword.Length - 1;
                var value = reader.GetNullTerminatedStringValue(bytesLeft, defaultEncoding);

                var textPairs = new List<KeyValuePair> { new KeyValuePair(keyword, value) };
                var directory = new PngDirectory(PngChunkType.iTXt);
                directory.Set(PngDirectory.TagTextualData, textPairs);
                yield return directory;
            }
            else if (chunkType == PngChunkType.iTXt)
            {
                var reader = new SequentialByteArrayReader(bytes);
                var keyword = reader.GetNullTerminatedStringValue(maxLengthBytes: 79).ToString(defaultEncoding);
                var compressionFlag = reader.GetSByte();
                var compressionMethod = reader.GetSByte();
                var languageTag = reader.GetNullTerminatedStringValue(bytes.Length, defaultEncoding);

                var translatedKeyword = reader.GetNullTerminatedStringValue(bytes.Length, defaultEncoding);

                var bytesLeft = bytes.Length - keyword.Length - 1 - 1 - 1 - languageTag.Bytes.Length - 1 - translatedKeyword.Bytes.Length - 1;
                byte[] textBytes = null;
                if (compressionFlag == 0)
                {
                    textBytes = reader.GetNullTerminatedBytes(bytesLeft);
                }
                else if (compressionFlag == 1)
                {
                    if (compressionMethod == 0)
                    {
                        using (var inflaterStream = new DeflateStream(new MemoryStream(bytes, bytes.Length - bytesLeft, bytesLeft), CompressionMode.Decompress))
                        using (var decompStream = new MemoryStream())
                        {
#if !NET35
                            inflaterStream.CopyTo(decompStream);
#else
                            byte[] buffer = new byte[256];
                            int count;
                            int totalBytes = 0;
                            while ((count = inflaterStream.Read(buffer, 0, 256)) > 0)
                            {
                                decompStream.Write(buffer, 0, count);
                                totalBytes += count;
                            }
#endif
                            textBytes = decompStream.ToArray();
                        }
                    }
                    else
                    {
                        var directory = new PngDirectory(PngChunkType.iTXt);
                        directory.AddError("Invalid compression method value");
                        yield return directory;
                    }
                }
                else
                {
                    var directory = new PngDirectory(PngChunkType.iTXt);
                    directory.AddError("Invalid compression flag value");
                    yield return directory;
                }

                if (textBytes != null)
                {
                    if (keyword == "XML:com.adobe.xmp")
                    {
                        // NOTE in testing images, the XMP has parsed successfully, but we are not extracting tags from it as necessary
                        yield return new XmpReader().Extract(textBytes);
                    }
                    else
                    {
                        var textPairs = new List<KeyValuePair> { new KeyValuePair(keyword, new StringValue(textBytes, defaultEncoding)) };
                        var directory = new PngDirectory(PngChunkType.iTXt);
                        directory.Set(PngDirectory.TagTextualData, textPairs);
                        yield return directory;
                    }
                }
            }
            else if (chunkType == PngChunkType.tIME)
            {
                var reader = new SequentialByteArrayReader(bytes);
                var year = reader.GetUInt16();
                var month = reader.GetByte();
                int day = reader.GetByte();
                int hour = reader.GetByte();
                int minute = reader.GetByte();
                int second = reader.GetByte();
                var directory = new PngDirectory(PngChunkType.tIME);
                try
                {
                    var time = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Unspecified);
                    directory.Set(PngDirectory.TagLastModificationTime, time);
                }
                catch (ArgumentOutOfRangeException e)
                {
                    directory.AddError("Error constructing DateTime: " + e.Message);
                }
                yield return directory;
            }
            else if (chunkType == PngChunkType.pHYs)
            {
                var reader = new SequentialByteArrayReader(bytes);
                var pixelsPerUnitX = reader.GetInt32();
                var pixelsPerUnitY = reader.GetInt32();
                var unitSpecifier = reader.GetSByte();
                var directory = new PngDirectory(PngChunkType.pHYs);
                directory.Set(PngDirectory.TagPixelsPerUnitX, pixelsPerUnitX);
                directory.Set(PngDirectory.TagPixelsPerUnitY, pixelsPerUnitY);
                directory.Set(PngDirectory.TagUnitSpecifier, unitSpecifier);
                yield return directory;
            }
            else if (chunkType.Equals(PngChunkType.sBIT))
            {
                var directory = new PngDirectory(PngChunkType.sBIT);
                directory.Set(PngDirectory.TagSignificantBits, bytes);
                yield return directory;
            }
        }