Exemplo n.º 1
0
        // methods overriding some methods in PdfObject

        /**
         * Returns the PDF representation of this <CODE>PdfString</CODE>.
         *
         * @return        an array of <CODE>byte</CODE>s
         */

        public override void ToPdf(PdfWriter writer, Stream os)
        {
            byte[]        b      = GetBytes();
            PdfEncryption crypto = null;

            if (writer != null)
            {
                crypto = writer.Encryption;
            }
            if (crypto != null)
            {
                b = (byte[])bytes.Clone();
                crypto.PrepareKey();
                crypto.EncryptRC4(b);
            }
            if (hexWriting)
            {
                ByteBuffer buf = new ByteBuffer();
                buf.Append('<');
                int len = b.Length;
                for (int k = 0; k < len; ++k)
                {
                    buf.AppendHex(b[k]);
                }
                buf.Append('>');
                os.Write(buf.ToByteArray(), 0, buf.Size);
            }
            else
            {
                b = PdfContentByte.EscapeString(b);
                os.Write(b, 0, b.Length);
            }
        }
Exemplo n.º 2
0
        internal void Decrypt(PdfReader reader)
        {
            PdfEncryption decrypt = reader.Decrypt;

            if (decrypt != null)
            {
                originalValue = value;
                decrypt.SetHashKey(objNum, objGen);
                decrypt.PrepareKey();
                bytes = PdfEncodings.ConvertToBytes(value, null);
                decrypt.EncryptRC4(bytes);
                value = PdfEncodings.ConvertToString(bytes, null);
            }
        }
Exemplo n.º 3
0
        public override void ToPdf(PdfWriter writer, Stream os)
        {
            if (inputStream != null && compressed)
            {
                Put(PdfName.FILTER, PdfName.FLATEDECODE);
            }
            SuperToPdf(writer, os);
            os.Write(STARTSTREAM, 0, STARTSTREAM.Length);
            PdfEncryption crypto = null;

            if (writer != null)
            {
                crypto = writer.Encryption;
            }
            if (crypto != null)
            {
                crypto.PrepareKey();
            }
            if (inputStream != null)
            {
                rawLength = 0;
                ZDeflaterOutputStream def = null;
                OutputStreamCounter   osc = new OutputStreamCounter(os);
                Stream fout = osc;
                if (crypto != null)
                {
                    fout = new PdfEncryptionStream(fout, crypto);
                }
                if (compressed)
                {
                    fout = def = new ZDeflaterOutputStream(fout);
                }

                byte[] buf = new byte[0x10000];
                while (true)
                {
                    int n = inputStream.Read(buf, 0, buf.Length);
                    if (n <= 0)
                    {
                        break;
                    }
                    fout.Write(buf, 0, n);
                    rawLength += n;
                }
                if (def != null)
                {
                    def.Finish();
                }
                inputStreamLength = osc.Counter;
            }
            else
            {
                if (crypto == null)
                {
                    if (streamBytes != null)
                    {
                        streamBytes.WriteTo(os);
                    }
                    else
                    {
                        os.Write(bytes, 0, bytes.Length);
                    }
                }
                else
                {
                    byte[] b;
                    if (streamBytes != null)
                    {
                        b = streamBytes.ToArray();
                        crypto.EncryptRC4(b);
                    }
                    else
                    {
                        b = new byte[bytes.Length];
                        crypto.EncryptRC4(bytes, b);
                    }
                    os.Write(b, 0, b.Length);
                }
            }
            os.Write(ENDSTREAM, 0, ENDSTREAM.Length);
        }
Exemplo n.º 4
0
        public override void ToPdf(PdfWriter writer, Stream os)
        {
            SuperToPdf(writer, os);
            os.Write(STARTSTREAM, 0, STARTSTREAM.Length);
            if (length > 0)
            {
                PdfEncryption crypto = null;
                if (writer != null)
                {
                    crypto = writer.Encryption;
                }
                if (offset < 0)
                {
                    if (crypto == null)
                    {
                        os.Write(bytes, 0, bytes.Length);
                    }
                    else
                    {
                        crypto.PrepareKey();
                        byte[] buf = new byte[length];
                        crypto.EncryptRC4(bytes, buf);
                        os.Write(buf, 0, buf.Length);
                    }
                }
                else
                {
                    byte[] buf = new byte[Math.Min(length, 4092)];
                    RandomAccessFileOrArray file = writer.GetReaderFile(reader);
                    bool isOpen = file.IsOpen();
                    try {
                        file.Seek(offset);
                        int size = length;

                        //added by ujihara for decryption
                        PdfEncryption decrypt = reader.Decrypt;
                        if (decrypt != null)
                        {
                            decrypt.SetHashKey(objNum, objGen);
                            decrypt.PrepareKey();
                        }

                        if (crypto != null)
                        {
                            crypto.PrepareKey();
                        }
                        while (size > 0)
                        {
                            int r = file.Read(buf, 0, Math.Min(size, buf.Length));
                            size -= r;

                            if (decrypt != null)
                            {
                                decrypt.EncryptRC4(buf, 0, r); //added by ujihara for decryption
                            }
                            if (crypto != null)
                            {
                                crypto.EncryptRC4(buf, 0, r);
                            }
                            os.Write(buf, 0, r);
                        }
                    }
                    finally {
                        if (!isOpen)
                        {
                            try{ file.Close(); }catch {}
                        }
                    }
                }
            }
            os.Write(ENDSTREAM, 0, ENDSTREAM.Length);
        }