public void RoundTrip(CompressionAlgorithms ca) { var compressionAlgorithm = GetCompressionAlgorithm(ca); int compressedBufferSize = compressionAlgorithm.GetCompressedBufferBounds(NumOriginalBytes); var observedCompressedBytes = new byte[compressedBufferSize]; var smallBuffer = new byte[10]; Assert.Throws <InvalidOperationException>(delegate { compressionAlgorithm.Compress(_originalBytes, NumOriginalBytes, null, compressedBufferSize); }); Assert.Throws <InvalidOperationException>(delegate { compressionAlgorithm.Compress(_originalBytes, NumOriginalBytes, smallBuffer, compressedBufferSize); }); int numCompressedBytes = compressionAlgorithm.Compress(_originalBytes, NumOriginalBytes, observedCompressedBytes, compressedBufferSize); int decompressedBufferSize = compressionAlgorithm.GetDecompressedLength(observedCompressedBytes, numCompressedBytes); var observedDecompressedBytes = new byte[decompressedBufferSize]; Assert.Throws <InvalidOperationException>(delegate { compressionAlgorithm.Decompress(observedCompressedBytes, numCompressedBytes, null, decompressedBufferSize); }); int numDecompressedBytes = compressionAlgorithm.Decompress(observedCompressedBytes, numCompressedBytes, observedDecompressedBytes, decompressedBufferSize); Assert.Equal(NumOriginalBytes, numDecompressedBytes); Assert.Equal(_originalBytes, observedDecompressedBytes); }
/// <summary> /// Parses the packet given as byte array into the current /// class and returns this with the populated parameters. /// </summary> /// <param name="bData">A byte array containing an OpenPGP /// representation of the packet.</param> /// <returns>Returns an CompressedDataPacket that containes /// the parsed properties.</returns> /// <remarks>No remarks</remarks> public override Packet ParsePacket(byte[] bData) { caAlgorithm = (CompressionAlgorithms)bData[0]; bCompressedData = new byte[bData.Length - 1]; Array.Copy(bData, 1, bCompressedData, 0, bData.Length - 1); this.bIsUpdated = false; return(this); }
public string TestConnection() { string macAlgos = string.Join(",", MacAlgorithms.Where(t => t.IsEnabled).Select(t => t.Name)); string encryptionAlgos = string.Join(",", EncryptionAlgorithms.Where(t => t.IsEnabled).Select(t => t.Name)); string compressionAlgos = string.Join(",", CompressionAlgorithms.Where(t => t.IsEnabled).Select(t => t.Name)); string keyExchangeAlgos = string.Join(",", KeyExchangeAlgorithms.Where(t => t.IsEnabled).Select(t => t.Name)); return(_sftpClient.TryConnection(ServerAddress, ServerPort, macAlgos, encryptionAlgos, compressionAlgos, keyExchangeAlgos)); }
/// <summary> /// Decompressed a block of data. It is assumed that this data is compressed with one /// or more algorithms, and that it is prepended by a single byte describing the algorithms /// used. See <see cref="CompressionAlgorithms"/> for valid algorithms. /// </summary> /// <param name="pendingSector">The compressed data.</param> /// <returns>The decompressed data.</returns> public static byte[] DecompressData(byte[] pendingSector) { // The sector is compressed using a combination of techniques. // Examine the first byte to determine the compression algorithms used CompressionAlgorithms compressionAlgorithms = (CompressionAlgorithms)pendingSector[0]; // Drop the first byte byte[] sectorData = new byte[pendingSector.Length - 1]; Buffer.BlockCopy(pendingSector, 1, sectorData, 0, sectorData.Length); pendingSector = sectorData; // Walk through each compression algorithm in reverse order if (compressionAlgorithms.HasFlag(CompressionAlgorithms.BZip2)) { // Decompress sector using BZIP2 pendingSector = DecompressBZip2(pendingSector); } if (compressionAlgorithms.HasFlag(CompressionAlgorithms.Implode)) { // Decompress sector using PKWARE Implode pendingSector = DecompressPKWAREImplode(pendingSector); } if (compressionAlgorithms.HasFlag(CompressionAlgorithms.Deflate)) { // Decompress sector using Deflate pendingSector = DecompressDeflate(pendingSector); } if (compressionAlgorithms.HasFlag(CompressionAlgorithms.Huffman)) { // Decompress sector using Huffman pendingSector = DecompressHuffman(pendingSector); } if (compressionAlgorithms.HasFlag(CompressionAlgorithms.ADPCMStereo)) { // Decompress sector using ADPCM Stereo pendingSector = DecompressADPCMStereo(pendingSector); } if (compressionAlgorithms.HasFlag(CompressionAlgorithms.ADPCMMono)) { // Decompress sector using ADPCM Mono pendingSector = DecompressADPCMMono(pendingSector); } if (compressionAlgorithms.HasFlag(CompressionAlgorithms.Sparse)) { // Decompress sector using Sparse pendingSector = DecompressSparse(pendingSector); } return(pendingSector); }
private static ICompressionAlgorithm GetCompressionAlgorithm(CompressionAlgorithms ca) { switch (ca) { case CompressionAlgorithms.Zlib: return(new Zlib()); case CompressionAlgorithms.Zstandard: return(new Zstandard()); default: throw new InvalidOperationException($"Unknown compression algorithm: {ca}"); } }
private static ICompressionAlgorithm GetCompressionAlgorithm(CompressionAlgorithms ca) { switch (ca) { case CompressionAlgorithms.QuickLz: return(new QuickLZ()); case CompressionAlgorithms.Zlib: return(new Zlib()); case CompressionAlgorithms.Zstandard: return(new Zstandard()); default: throw new GeneralException($"Unknown compression algorithm: {ca}"); } }
/// <summary> /// Parses a compressed message out of the given array of packets. /// In this special case, the first packet in packets MUST be /// a compressed data packet. /// </summary> /// <returns>Returns the number of packets used by the function. /// If everything works fine, it will always return 1.</returns> /// <param name="packets">Array of packets. The first packet in /// the array MUST be a compressed data packet. Otherwise an exception /// is thrown.</param> /// <remarks>No remarks</remarks> public override int ParseMessage(Packet[] packets) { if (packets[0] is CompressedDataPacket) { this.pPackets = new Packet[1]; pPackets[0] = packets[0]; CompressedDataPacket cdpPacket = (CompressedDataPacket)packets[0]; bCompressedData = cdpPacket.CompressedData; caAlgorithm = cdpPacket.Algorithm; } else { throw new System.ArgumentException("Expected a literal data packet as first packet in the array, but did not find it. Looks like something went terribly wrong."); } return(1); }
/// <summary> /// Main constructor used to set the compression algorithm and initialize the list of messages to /// be compressed by the client. /// </summary> /// <param name="compressionAlgorithm">The compression algorithm to use.</param> /// <param name="initializeForCompression">Flag indicating if the initialization is for compression or decompression.</param> public XCompressionController(CompressionAlgorithms compressionAlgorithm, bool initializeForCompression) { if (!Enum.IsDefined(typeof(CompressionAlgorithms), compressionAlgorithm)) { throw new NotSupportedException(string.Format(ResourcesX.CompressionAlgorithmNotSupported, compressionAlgorithm)); } CompressionAlgorithm = compressionAlgorithm; _initializeForCompression = initializeForCompression; // Set the list of messages that should be compressed. ClientSupportedCompressedMessages = new List <ClientMessageId>(); ClientSupportedCompressedMessages.Add(ClientMessageId.CRUD_DELETE); ClientSupportedCompressedMessages.Add(ClientMessageId.CRUD_FIND); ClientSupportedCompressedMessages.Add(ClientMessageId.CRUD_INSERT); ClientSupportedCompressedMessages.Add(ClientMessageId.CRUD_UPDATE); ClientSupportedCompressedMessages.Add(ClientMessageId.SQL_STMT_EXECUTE); // Initialize stream objects. _buffer = new MemoryStream(); switch (CompressionAlgorithm) { case CompressionAlgorithms.zstd_stream: if (!_initializeForCompression) { _zstdDecompressStream = new ZstandardStream(_buffer, CompressionMode.Decompress); } break; #if !NET452 case CompressionAlgorithms.deflate_stream: if (_initializeForCompression) { _deflateCompressStream = new DeflateStream(_buffer, CompressionMode.Compress, true); } else { _deflateDecompressStream = new DeflateStream(_buffer, CompressionMode.Decompress, true); } break; #endif } }
/// <summary> /// Generate specific compression class /// </summary> /// <param name="algorithmType">Get specific class by algorithm enum</param> /// <returns></returns> public static Strategy.Compression Create(CompressionAlgorithms algorithmType) { //switch for algorithm switch (algorithmType) { //case for getting gzip class case CompressionAlgorithms.Gzip: return(new Strategy.Compression( new Algorithms.GZipCompression(), algorithmType, Utils.MagicNumbersAlgorithmDictionary.AlgorithmMagicNumbers[algorithmType])); /* * case CompressionAlgorithms.Deflate: * return new Strategy.Compression(new Algorithms.DeflateCompression(), algorithmType); */ } return(null); }
/// <summary> /// Parses the packet given as byte array into the current /// class and returns this with the populated parameters. /// </summary> /// <param name="bData">A byte array containing an OpenPGP /// representation of the packet.</param> /// <returns>Returns an CompressedDataPacket that containes /// the parsed properties.</returns> /// <remarks>No remarks</remarks> public override Packet ParsePacket(byte[] bData) { caAlgorithm = (CompressionAlgorithms)bData[0]; bCompressedData = new byte[bData.Length - 1]; Array.Copy(bData, 1, bCompressedData, 0, bData.Length - 1); this.bIsUpdated = false; return this; }
/// <summary> /// Creates a new CompressedMessage /// </summary> /// <param name="algo">The compressionalgorithm that shall be /// used to compress the message.</param> /// <remarks>No remarks</remarks> public CompressedMessage(CompressionAlgorithms algo) { bCompressedData = new byte[0]; caAlgorithm = algo; pPackets = new Packet[0]; }
/// <summary> /// Parses a compressed message out of the given array of packets. /// In this special case, the first packet in packets MUST be /// a compressed data packet. /// </summary> /// <returns>Returns the number of packets used by the function. /// If everything works fine, it will always return 1.</returns> /// <param name="packets">Array of packets. The first packet in /// the array MUST be a compressed data packet. Otherwise an exception /// is thrown.</param> /// <remarks>No remarks</remarks> public override int ParseMessage(Packet[] packets) { if (packets[0] is CompressedDataPacket) { this.pPackets = new Packet[1]; pPackets[0] = packets[0]; CompressedDataPacket cdpPacket = (CompressedDataPacket)packets[0]; bCompressedData = cdpPacket.CompressedData; caAlgorithm = cdpPacket.Algorithm; } else throw new System.ArgumentException("Expected a literal data packet as first packet in the array, but did not find it. Looks like something went terribly wrong."); return 1; }