Package org.apache.tika.fuzzing.pdf
Class EvilCOSWriter
- java.lang.Object
-
- org.apache.tika.fuzzing.pdf.EvilCOSWriter
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,org.apache.pdfbox.cos.ICOSVisitor
public class EvilCOSWriter extends Object implements org.apache.pdfbox.cos.ICOSVisitor, Closeable
-
-
Field Summary
Fields Modifier and Type Field Description static byte[]
ARRAY_CLOSE
The array close token.static byte[]
ARRAY_OPEN
The array open token.static byte[]
COMMENT
The start to a PDF comment.static byte[]
DICT_CLOSE
The dictionary close token.static byte[]
DICT_OPEN
The dictionary open token.static byte[]
ENDOBJ
The end object token.static byte[]
ENDSTREAM
The close stream token.static byte[]
EOF
The EOF constant.static byte[]
GARBAGE
Garbage bytes used to create the PDF header.static byte[]
OBJ
The starting object token.static byte[]
REFERENCE
The reference token.static byte[]
SPACE
space character.static byte[]
STARTXREF
The start xref token.static byte[]
STREAM
The open stream token.static byte[]
TRAILER
The trailer token.static byte[]
VERSION
The output version of the PDF.static byte[]
XREF
The XREF token.static byte[]
XREF_FREE
The xref free token.static byte[]
XREF_USED
The xref used token.
-
Constructor Summary
Constructors Constructor Description EvilCOSWriter(OutputStream outputStream, PDFTransformerConfig config)
COSWriter constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addXRefEntry(org.apache.pdfbox.pdfwriter.COSWriterXRefEntry entry)
add an entry in the x ref table for later dump.void
close()
This will close the stream.protected void
doWriteBody(org.apache.pdfbox.cos.COSDocument doc)
This will write the body of the document.protected void
doWriteHeader(org.apache.pdfbox.cos.COSDocument doc)
This will write the header to the PDF document.void
doWriteObject(org.apache.pdfbox.cos.COSBase obj)
This will write a COS object.protected void
doWriteTrailer(org.apache.pdfbox.cos.COSDocument doc)
This will write the trailer to the PDF document.InputStream
getDataToSign()
Return the stream of PDF data to be signed.protected long
getNumber()
This will get the current object number.Map<org.apache.pdfbox.cos.COSBase,org.apache.pdfbox.cos.COSObjectKey>
getObjectKeys()
This will get all available object keys.protected OutputStream
getOutput()
This will get the output stream.protected org.apache.pdfbox.pdfwriter.COSStandardOutputStream
getStandardOutput()
This will get the standard output stream.protected long
getStartxref()
This will get the current start xref.protected List<org.apache.pdfbox.pdfwriter.COSWriterXRefEntry>
getXRefEntries()
This will get the xref entries.protected Long[]
getXRefRanges(List<org.apache.pdfbox.pdfwriter.COSWriterXRefEntry> xRefEntriesList)
check the xref entries and write out the ranges.protected void
setNumber(long newNumber)
This will set the current object number.protected void
setStartxref(long newStartxref)
This will set the start xref.Object
visitFromArray(org.apache.pdfbox.cos.COSArray obj)
Object
visitFromBoolean(org.apache.pdfbox.cos.COSBoolean obj)
Object
visitFromDictionary(org.apache.pdfbox.cos.COSDictionary obj)
Object
visitFromDocument(org.apache.pdfbox.cos.COSDocument doc)
Object
visitFromFloat(org.apache.pdfbox.cos.COSFloat obj)
Object
visitFromInt(org.apache.pdfbox.cos.COSInteger obj)
Object
visitFromName(org.apache.pdfbox.cos.COSName obj)
Object
visitFromNull(org.apache.pdfbox.cos.COSNull obj)
Object
visitFromStream(org.apache.pdfbox.cos.COSStream obj)
Object
visitFromString(org.apache.pdfbox.cos.COSString obj)
void
write(org.apache.pdfbox.cos.COSDocument doc)
This will write the pdf document.void
write(org.apache.pdfbox.pdmodel.fdf.FDFDocument doc)
This will write the fdf document.void
write(org.apache.pdfbox.pdmodel.PDDocument doc)
This will write the pdf document.void
write(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface signInterface)
This will write the pdf document.void
writeExternalSignature(byte[] cmsSignature)
Write externally created signature of PDF data obtained viagetDataToSign()
method.void
writeReference(org.apache.pdfbox.cos.COSBase obj)
visitFromObjRef method comment.static void
writeString(byte[] bytes, OutputStream output)
This will output the given text/byte getString as a PDF object.static void
writeString(org.apache.pdfbox.cos.COSString string, OutputStream output)
This will output the given byte getString as a PDF object.
-
-
-
Field Detail
-
DICT_OPEN
public static final byte[] DICT_OPEN
The dictionary open token.
-
DICT_CLOSE
public static final byte[] DICT_CLOSE
The dictionary close token.
-
SPACE
public static final byte[] SPACE
space character.
-
COMMENT
public static final byte[] COMMENT
The start to a PDF comment.
-
VERSION
public static final byte[] VERSION
The output version of the PDF.
-
GARBAGE
public static final byte[] GARBAGE
Garbage bytes used to create the PDF header.
-
EOF
public static final byte[] EOF
The EOF constant.
-
REFERENCE
public static final byte[] REFERENCE
The reference token.
-
XREF
public static final byte[] XREF
The XREF token.
-
XREF_FREE
public static final byte[] XREF_FREE
The xref free token.
-
XREF_USED
public static final byte[] XREF_USED
The xref used token.
-
TRAILER
public static final byte[] TRAILER
The trailer token.
-
STARTXREF
public static final byte[] STARTXREF
The start xref token.
-
OBJ
public static final byte[] OBJ
The starting object token.
-
ENDOBJ
public static final byte[] ENDOBJ
The end object token.
-
ARRAY_OPEN
public static final byte[] ARRAY_OPEN
The array open token.
-
ARRAY_CLOSE
public static final byte[] ARRAY_CLOSE
The array close token.
-
STREAM
public static final byte[] STREAM
The open stream token.
-
ENDSTREAM
public static final byte[] ENDSTREAM
The close stream token.
-
-
Constructor Detail
-
EvilCOSWriter
public EvilCOSWriter(OutputStream outputStream, PDFTransformerConfig config)
COSWriter constructor.- Parameters:
outputStream
- The output stream to write the PDF. It will be closed when this object is closed.
-
-
Method Detail
-
writeString
public static void writeString(org.apache.pdfbox.cos.COSString string, OutputStream output) throws IOException
This will output the given byte getString as a PDF object.- Parameters:
string
- COSString to be writtenoutput
- The stream to write to.- Throws:
IOException
- If there is an error writing to the stream.
-
writeString
public static void writeString(byte[] bytes, OutputStream output) throws IOException
This will output the given text/byte getString as a PDF object.- Parameters:
bytes
- byte array representation of a string to be writtenoutput
- The stream to write to.- Throws:
IOException
- If there is an error writing to the stream.
-
addXRefEntry
protected void addXRefEntry(org.apache.pdfbox.pdfwriter.COSWriterXRefEntry entry)
add an entry in the x ref table for later dump.- Parameters:
entry
- The new entry to add.
-
close
public void close() throws IOException
This will close the stream.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- If the underlying stream throws an exception.
-
getNumber
protected long getNumber()
This will get the current object number.- Returns:
- The current object number.
-
setNumber
protected void setNumber(long newNumber)
This will set the current object number.- Parameters:
newNumber
- The new object number.
-
getObjectKeys
public Map<org.apache.pdfbox.cos.COSBase,org.apache.pdfbox.cos.COSObjectKey> getObjectKeys()
This will get all available object keys.- Returns:
- A map of all object keys.
-
getOutput
protected OutputStream getOutput()
This will get the output stream.- Returns:
- The output stream.
-
getStandardOutput
protected org.apache.pdfbox.pdfwriter.COSStandardOutputStream getStandardOutput()
This will get the standard output stream.- Returns:
- The standard output stream.
-
getStartxref
protected long getStartxref()
This will get the current start xref.- Returns:
- The current start xref.
-
setStartxref
protected void setStartxref(long newStartxref)
This will set the start xref.- Parameters:
newStartxref
- The new start xref attribute.
-
getXRefEntries
protected List<org.apache.pdfbox.pdfwriter.COSWriterXRefEntry> getXRefEntries()
This will get the xref entries.- Returns:
- All available xref entries.
-
doWriteBody
protected void doWriteBody(org.apache.pdfbox.cos.COSDocument doc) throws IOException
This will write the body of the document.- Parameters:
doc
- The document to write the body for.- Throws:
IOException
- If there is an error writing the data.
-
doWriteObject
public void doWriteObject(org.apache.pdfbox.cos.COSBase obj) throws IOException
This will write a COS object.- Parameters:
obj
- The object to write.- Throws:
IOException
- if the output cannot be written
-
doWriteHeader
protected void doWriteHeader(org.apache.pdfbox.cos.COSDocument doc) throws IOException
This will write the header to the PDF document.- Parameters:
doc
- The document to get the data from.- Throws:
IOException
- If there is an error writing to the stream.
-
doWriteTrailer
protected void doWriteTrailer(org.apache.pdfbox.cos.COSDocument doc) throws IOException
This will write the trailer to the PDF document.- Parameters:
doc
- The document to create the trailer for.- Throws:
IOException
- If there is an IOError while writing the document.
-
getDataToSign
public InputStream getDataToSign() throws IOException
Return the stream of PDF data to be signed. Clients should use this method only to create signatures externally.write(PDDocument)
method should have been called prior. The created signature should be set usingwriteExternalSignature(byte[])
.When
SignatureInterface
instance is used, COSWriter obtains and writes the signature itself.- Returns:
- data stream to be signed
- Throws:
IllegalStateException
- if PDF is not prepared for external signingIOException
- if input data is closed
-
writeExternalSignature
public void writeExternalSignature(byte[] cmsSignature) throws IOException
Write externally created signature of PDF data obtained viagetDataToSign()
method.- Parameters:
cmsSignature
- CMS signature byte array- Throws:
IllegalStateException
- if PDF is not prepared for external signingIOException
- if source data stream is closed
-
getXRefRanges
protected Long[] getXRefRanges(List<org.apache.pdfbox.pdfwriter.COSWriterXRefEntry> xRefEntriesList)
check the xref entries and write out the ranges. The format of the returned array is exactly the same as the pdf specification. See section 7.5.4 of ISO32000-1:2008, example 1 (page 40) for reference.example: 0 1 2 5 6 7 8 10
will create a array with follow ranges
0 3 5 4 10 1
this mean that the element 0 is followed by two other related numbers that represent a cluster of the size 3. 5 is follow by three other related numbers and create a cluster of size 4. etc.
- Parameters:
xRefEntriesList
- list with the xRef entries that was written- Returns:
- a integer array with the ranges
-
visitFromArray
public Object visitFromArray(org.apache.pdfbox.cos.COSArray obj) throws IOException
- Specified by:
visitFromArray
in interfaceorg.apache.pdfbox.cos.ICOSVisitor
- Throws:
IOException
-
visitFromBoolean
public Object visitFromBoolean(org.apache.pdfbox.cos.COSBoolean obj) throws IOException
- Specified by:
visitFromBoolean
in interfaceorg.apache.pdfbox.cos.ICOSVisitor
- Throws:
IOException
-
visitFromDictionary
public Object visitFromDictionary(org.apache.pdfbox.cos.COSDictionary obj) throws IOException
- Specified by:
visitFromDictionary
in interfaceorg.apache.pdfbox.cos.ICOSVisitor
- Throws:
IOException
-
visitFromDocument
public Object visitFromDocument(org.apache.pdfbox.cos.COSDocument doc) throws IOException
- Specified by:
visitFromDocument
in interfaceorg.apache.pdfbox.cos.ICOSVisitor
- Throws:
IOException
-
visitFromFloat
public Object visitFromFloat(org.apache.pdfbox.cos.COSFloat obj) throws IOException
- Specified by:
visitFromFloat
in interfaceorg.apache.pdfbox.cos.ICOSVisitor
- Throws:
IOException
-
visitFromInt
public Object visitFromInt(org.apache.pdfbox.cos.COSInteger obj) throws IOException
- Specified by:
visitFromInt
in interfaceorg.apache.pdfbox.cos.ICOSVisitor
- Throws:
IOException
-
visitFromName
public Object visitFromName(org.apache.pdfbox.cos.COSName obj) throws IOException
- Specified by:
visitFromName
in interfaceorg.apache.pdfbox.cos.ICOSVisitor
- Throws:
IOException
-
visitFromNull
public Object visitFromNull(org.apache.pdfbox.cos.COSNull obj) throws IOException
- Specified by:
visitFromNull
in interfaceorg.apache.pdfbox.cos.ICOSVisitor
- Throws:
IOException
-
writeReference
public void writeReference(org.apache.pdfbox.cos.COSBase obj) throws IOException
visitFromObjRef method comment.- Parameters:
obj
- The object that is being visited.- Throws:
IOException
- If there is an exception while visiting this object.
-
visitFromStream
public Object visitFromStream(org.apache.pdfbox.cos.COSStream obj) throws IOException
- Specified by:
visitFromStream
in interfaceorg.apache.pdfbox.cos.ICOSVisitor
- Throws:
IOException
-
visitFromString
public Object visitFromString(org.apache.pdfbox.cos.COSString obj) throws IOException
- Specified by:
visitFromString
in interfaceorg.apache.pdfbox.cos.ICOSVisitor
- Throws:
IOException
-
write
public void write(org.apache.pdfbox.cos.COSDocument doc) throws IOException
This will write the pdf document. }- Parameters:
doc
- The document to write.- Throws:
IOException
- If an error occurs while generating the data.
-
write
public void write(org.apache.pdfbox.pdmodel.PDDocument doc) throws IOException
This will write the pdf document. If signature should be created externally,writeExternalSignature(byte[])
should be invoked to set signature after calling this method.- Parameters:
doc
- The document to write.- Throws:
IOException
- If an error occurs while generating the data.
-
write
public void write(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface signInterface) throws IOException
This will write the pdf document. If signature should be created externally,writeExternalSignature(byte[])
should be invoked to set signature after calling this method.- Parameters:
doc
- The document to write.signInterface
- class to be used for signing;null
if external signing would be performed or there will be no signing at all- Throws:
IOException
- If an error occurs while generating the data.IllegalStateException
- If the document has an encryption dictionary but no protection policy.
-
write
public void write(org.apache.pdfbox.pdmodel.fdf.FDFDocument doc) throws IOException
This will write the fdf document.- Parameters:
doc
- The document to write.- Throws:
IOException
- If an error occurs while generating the data.
-
-