2 Commits
1.4.1 ... 1.5.1

Author SHA1 Message Date
ben
04eb07f321 Fix help message 2018-09-29 00:17:53 +02:00
ben
caaaaafeb0 Add pdfraw format, v1.5 2018-09-29 00:12:49 +02:00
3 changed files with 67 additions and 10 deletions

View File

@@ -2,6 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from .polyglotpdfzip import PolyglotPdfZip from .polyglotpdfzip import PolyglotPdfZip
from .polyglotpdfraw import PolyglotPdfRaw
from .polyglotzippdf import PolyglotZipPdf from .polyglotzippdf import PolyglotZipPdf
from .polyglotszippdf import PolyglotSZipPdf from .polyglotszippdf import PolyglotSZipPdf

View File

@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
import logging
'''
|-------------------------------| -
|--------- PDF Header ----------K1 | J1
|-------------------------------| -
|----- PDF OBJ 1 = RAW Data ----K2 |
|-------------------------------| -
|---- Original PDF Ojbects -----K3 | J2
|-------------------------------| -
|---------- Xref Table ---------| |
|-------------------------------K5 |
|----------- Trailer -----------| |
|-------------------------------| |
'''
class PolyglotPdfRaw():
from PdfFileTransformer import Pdf
def __init__(self, Pdf, Raw_filename):
self.buffer = bytearray()
self.pdf = Pdf
self.raw_filename = Raw_filename
self.buffer = bytearray()
def generate(self):
raw_buffer = bytearray()
with open(self.raw_filename, "rb") as f:
raw_buffer = f.read()
k2_stream = raw_buffer
self.pdf.insert_new_obj_stream_at_start(k2_stream)
self.buffer = self.pdf.get_build_buffer()
def write(self, filename):
fd = open(filename, "wb")
fd.write(self.buffer)
fd.close()

View File

@@ -6,7 +6,10 @@ import logging
from PdfFileTransformer import Pdf from PdfFileTransformer import Pdf
from ZipFileTransformer import Zip from ZipFileTransformer import Zip
from PolyglotFile import PolyglotZipPdf, PolyglotPdfZip, PolyglotSZipPdf from PolyglotFile import PolyglotZipPdf
from PolyglotFile import PolyglotPdfZip
from PolyglotFile import PolyglotSZipPdf
from PolyglotFile import PolyglotPdfRaw
def main(): def main():
@@ -17,9 +20,11 @@ def main():
' The format is closest to ZIP.\n' + ' The format is closest to ZIP.\n' +
'* szippdf: Generate a file valid as ZIP and PDF.' + '* szippdf: Generate a file valid as ZIP and PDF.' +
' The format is strictly a ZIP.' + ' The format is strictly a ZIP.' +
' Archive is modified.') ' Archive is modified.\n' +
'* pdfraw: Generate a file strictly valid as a PDF ' +
'with a custom first object content.')
usage_str = '%(prog)s format [options] output-file' usage_str = '%(prog)s format [options] output-file'
epilog_str = 'TruePolyglot v1.4.1' epilog_str = 'TruePolyglot v1.5.1'
frm = argparse.RawTextHelpFormatter frm = argparse.RawTextHelpFormatter
parser = argparse.ArgumentParser(description=description_str, parser = argparse.ArgumentParser(description=description_str,
epilog=epilog_str, epilog=epilog_str,
@@ -27,12 +32,15 @@ def main():
formatter_class=frm) formatter_class=frm)
parser.add_argument('format', nargs='+', choices=["pdfzip", parser.add_argument('format', nargs='+', choices=["pdfzip",
"zippdf", "zippdf",
"szippdf"], "szippdf",
"pdfraw"],
help='Output polyglot format') help='Output polyglot format')
parser.add_argument('--pdffile', dest='pdffile', parser.add_argument('--pdffile', dest='pdffile',
help='PDF input file') help='PDF input file')
parser.add_argument('--zipfile', dest='zipfile', parser.add_argument('--zipfile', dest='zipfile',
help='ZIP input file') help='ZIP input file')
parser.add_argument('--rawfile', dest='rawfile',
help='RAW input file')
parser.add_argument('--acrobat-compatibility', parser.add_argument('--acrobat-compatibility',
dest='acrobat_compatibility', dest='acrobat_compatibility',
help='Add a byte at start for Acrobat Reader compatibility with szippdf format', help='Add a byte at start for Acrobat Reader compatibility with szippdf format',
@@ -46,14 +54,14 @@ def main():
args = parser.parse_args() args = parser.parse_args()
formats = ["pdfzip", "zippdf", "szippdf"]
if args.acrobat_compatibility and args.format[0] != "szippdf": if args.acrobat_compatibility and args.format[0] != "szippdf":
parser.error('--acrobat-compatibility is for szippdf only') parser.error('--acrobat-compatibility is for szippdf only')
if args.format[0] in formats: if "pdf" in args.format[0] and args.pdffile is None:
if args.pdffile is None:
parser.error('pdffile is required') parser.error('pdffile is required')
if args.zipfile is None: if "zip" in args.format[0] and args.zipfile is None:
parser.error('zipfile is required') parser.error('zipfile is required')
if "raw" in args.format[0] and args.rawfile is None:
parser.error('rawfile is required')
if args.verbose == "none": if args.verbose == "none":
logging.basicConfig(level=logging.CRITICAL) logging.basicConfig(level=logging.CRITICAL)
@@ -64,14 +72,22 @@ def main():
if args.verbose == "debug": if args.verbose == "debug":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
if args.format[0] == "pdfzip":
p = Pdf(args.pdffile) p = Pdf(args.pdffile)
z = Zip(args.zipfile) z = Zip(args.zipfile)
if args.format[0] == "pdfzip":
a = PolyglotPdfZip(p, z) a = PolyglotPdfZip(p, z)
if args.format[0] == "zippdf": if args.format[0] == "zippdf":
p = Pdf(args.pdffile)
z = Zip(args.zipfile)
a = PolyglotZipPdf(p, z) a = PolyglotZipPdf(p, z)
if args.format[0] == "szippdf": if args.format[0] == "szippdf":
p = Pdf(args.pdffile)
z = Zip(args.zipfile)
a = PolyglotSZipPdf(p, z, args.acrobat_compatibility) a = PolyglotSZipPdf(p, z, args.acrobat_compatibility)
if args.format[0] == "pdfraw":
p = Pdf(args.pdffile)
a = PolyglotPdfRaw(p, args.rawfile)
a.generate() a.generate()
a.write(args.output_file[0]) a.write(args.output_file[0])