Exemple #1
0
        public static ToffeeInternalPacketReadResult Read(ToffeeParticipant receiver, byte[] packet)
        {
            try
            {
                // Iterate through the packet
                ToffeePacketIterator iterator = new ToffeePacketIterator(receiver, packet);

                // Get the header, data, and CRC
                InternalPacketHeader header = iterator.ReadStruct <InternalPacketHeader>();
                byte[] packetData           = iterator.ReadBytes(header.Length);
                uint   sentCrc = iterator.ReadUInt32();

                // Is the CRC correct?
                uint calculatedCrc = CRC.CalculateCRC32(packetData);
                if (sentCrc != calculatedCrc)
                {
                    return(new ToffeeInternalPacketReadResult(false, new InternalPacketHeader(), null));
                }

                // Is this packet compressed?
                if (header.Compressed)
                {
                    packetData = CompressionService.Decompress(packetData);
                }

                // Return the read result
                return(new ToffeeInternalPacketReadResult(true, header, packetData));
            }
            catch
            {
                return(new ToffeeInternalPacketReadResult(false, new InternalPacketHeader(), null));
            }
        }
        static void Main(string[] args)
        {
            DecompressFileApp app      = new DecompressFileApp();
            Logger            logger   = app.Logger;
            ExitCode          exitCode = ExitCode.None;

            try
            {
                logger.Info("Application started.");

                CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");

                Options options = new Options();

                Parser.Default.ParseArgumentsStrict(args, options, CommandLineOptions.ArgumentParsingFailed);

                using (ICompressionService zipFileService = new CompressionService())
                {
                    zipFileService.Decompress(options.InputFileName, options.OutputPath);
                    exitCode = ExitCode.Success;
                }
            }
            catch (Exception ex)
            {
                exitCode = new ExceptionHandlingService(ex).GetExitCode();
            }
            finally
            {
                logger.Info(CultureInfo.InvariantCulture, "Application exited with code: {0}", (int)exitCode);
                Environment.Exit((int)exitCode);
            }
        }
        public void CompressAndDecompressToSameResultTest(byte[] data)
        {
            var compressionService = new CompressionService();
            var compressedData     = compressionService.Compress(data);
            var decompressedData   = compressionService.Decompress(compressedData);

            Assert.True(data.SequenceEqual(decompressedData));
        }
Exemple #4
0
        public static ToffeeClientPacketReadResult Read(ToffeeParticipant receiver, byte[] packet)
        {
            try
            {
                // Is this packet encrypted?
                bool encrypted = packet[0] == 0x01;
                if (encrypted)
                {
                    // Is this client setup to use encryption?
                    if (receiver.UseEncryption)
                    {
                        packet = receiver.Encryption.Decrypt(packet.Skip(1).ToArray());
                    }
                }
                else
                {
                    packet = packet.Skip(1).ToArray();
                }

                // Iterate through the packet
                ToffeePacketIterator iterator = new ToffeePacketIterator(receiver, packet);

                // Get the header, data, and CRC
                ClientPacketHeader header     = iterator.ReadStruct <ClientPacketHeader>();
                byte[]             packetData = iterator.ReadBytes(header.Length);
                uint sentCrc = iterator.ReadUInt32();

                // Is the CRC correct?
                uint calculatedCrc = CRC.CalculateCRC32(packetData);
                if (sentCrc != calculatedCrc)
                {
                    return(new ToffeeClientPacketReadResult(false, encrypted, new ClientPacketHeader(), null));
                }

                // Is this packet compressed?
                if (header.Compressed)
                {
                    packetData = CompressionService.Decompress(packetData);
                }

                // Return the read result
                return(new ToffeeClientPacketReadResult(true, encrypted, header, packetData));
            }
            catch
            {
                return(new ToffeeClientPacketReadResult(false, false, new ClientPacketHeader(), null));
            }
        }
Exemple #5
0
        public async Task <byte[]> LoadFileAsync(string name)
        {
            _connection.Open();

            var fileContent = await _connection.ExecuteScalarAsync <byte[]>(
                "SELECT Content " +
                "FROM FileInfo " +
                "WHERE Name = @Name",
                param : new { Name = name });

            _connection.Close();

            var decompressedFileContent = _compressionService.Decompress(fileContent);

            return(decompressedFileContent);
        }