Beispiel #1
0
        /// <summary>
        /// Uncompresses the current compressed message and returns the message that
        /// was inside of the compressed message. Usually this should be a literal
        /// data message.
        /// </summary>
        /// <returns>Returns the message that was inside of the compressed message.
        /// Usually this should be a literal data message.</returns>
        /// <exception cref="System.Exception">Throws an exception if the content
        /// of the compressed message is not another valid message.</exception>
        /// <remarks>No remarks</remarks>
        public Message Uncompress()
        {
            if (!(pPackets[0] is CompressedDataPacket))
            {
                throw new System.Exception("You should never see this message. If you do, something in CompressedMessage went terribly wrong!");
            }

            CompressedDataPacket cdpPacket = (CompressedDataPacket)pPackets[0];

            Packet[] pContent = cdpPacket.Uncompress();

            // A compressed data packet can contain:

            // - a literal message
            LiteralMessage lmMessage = new LiteralMessage();

            try {
                int iPos = lmMessage.ParseMessage(pContent);
                return(lmMessage);
            } catch (Exception) {}

            // - a signed message
            SignedMessage smMessage = new SignedMessage();

            try {
                int iPos = smMessage.ParseMessage(pContent);
                return(smMessage);
            } catch (Exception) {}

            // TODO: Try to think of other packets that might
            // occur in a compressed data packet

            throw new Exception("The content of the compressed message does not appear to be a valid OpenPGP message!");
        }
Beispiel #2
0
        internal PgpCompressedMessage(IPacketReader packetReader)
        {
            var packet = packetReader.ReadStreamablePacket();

            this.compressedDataPacket = (CompressedDataPacket)packet.Packet;
            this.inputStream          = packet.Stream;
            this.packetReader         = packetReader;
        }
        public PgpCompressedData(
            BcpgInputStream bcpgInput)
        {
            Packet packet = bcpgInput.ReadPacket();

            if (!(packet is CompressedDataPacket))
            {
                throw new IOException("unexpected packet in stream: " + packet);
            }

            this.data = (CompressedDataPacket)packet;
        }
Beispiel #4
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);
        }
Beispiel #5
0
        /// <summary>
        /// Gets the OpenPGP encoded representation of the compressed data
        /// message.
        /// </summary>
        /// <returns>Returns a byte array that contains the binary
        /// representation of the compressed message.</returns>
        /// <remarks>No remarks</remarks>
        public override byte[] GetEncoded()
        {
            if (pPackets.Length > 1)
            {
                throw new System.ApplicationException("A compressed data message can contain only one packet. Something /really/ strange happened!");
            }

            if (pPackets.Length == 0)
            {
                pPackets = new Packet[1];
                CompressedDataPacket cdpPacket = new CompressedDataPacket();
                cdpPacket.Algorithm      = caAlgorithm;
                cdpPacket.CompressedData = bCompressedData;
                pPackets[0] = cdpPacket;
            }

            return(pPackets[0].Generate());
        }
        /// <summary>
        /// Return an output stream which will save the data being written to
        /// the compressed object.
        /// </summary>
        /// <param name="writer">Writer to be used for output.</param>
        /// <returns>A Stream for output of the compressed data.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="IOException"></exception>
        protected override IPacketWriter Open()
        {
            var writer = base.Open();

            var packet = new CompressedDataPacket(algorithm);

            this.pkOut = writer.GetPacketStream(packet);

            switch (algorithm)
            {
            case PgpCompressionAlgorithm.Uncompressed:
                dOut = pkOut;
                break;

            case PgpCompressionAlgorithm.Zip:
                dOut = new DeflateStream(pkOut, compressionLevel, leaveOpen: true);
                break;

            case PgpCompressionAlgorithm.ZLib:
                checksum = new Adler32();
                byte cmf = 0x78;  // Deflate, 32K window size
                byte flg = 0;     // Fastest compression level
                // Checksum
                flg |= (byte)(31 - ((cmf << 8) + flg) % 31);
                Debug.Assert(((cmf << 8) + flg) % 31 == 0);
                pkOut.WriteByte(cmf);
                pkOut.WriteByte(flg);
                dOut =
                    new CryptoStream(
                        new DeflateStream(pkOut, compressionLevel, leaveOpen: true),
                        checksum,
                        CryptoStreamMode.Write);
                break;

            /*case CompressionAlgorithmTag.BZip2:
             *  dOut = new SafeCBZip2OutputStream(pkOut);
             *  break;*/
            default:
                // Constructor should guard against this possibility
                throw new InvalidOperationException();
            }

            return(writer.CreateNestedWriter(new WrappedGeneratorStream(dOut, _ => Close())));
        }
Beispiel #7
0
		public PgpCompressedData(
            BcpgInputStream bcpgInput)
        {
            data = (CompressedDataPacket) bcpgInput.ReadPacket();
        }
Beispiel #8
0
 public PgpCompressedData(
     BcpgInputStream bcpgInput)
 {
     data = (CompressedDataPacket)bcpgInput.ReadPacket();
 }
        /// <summary>
        /// Gets the OpenPGP encoded representation of the compressed data
        /// message.
        /// </summary>
        /// <returns>Returns a byte array that contains the binary
        /// representation of the compressed message.</returns>
        /// <remarks>No remarks</remarks>
        public override byte[] GetEncoded()
        {
            if (pPackets.Length > 1)
                throw new System.ApplicationException("A compressed data message can contain only one packet. Something /really/ strange happened!");

            if (pPackets.Length == 0) {
                pPackets = new Packet[1];
                CompressedDataPacket cdpPacket = new CompressedDataPacket();
                cdpPacket.Algorithm = caAlgorithm;
                cdpPacket.CompressedData = bCompressedData;
                pPackets[0] = cdpPacket;
            }

            return pPackets[0].Generate();
        }