Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
04eb07f321 | ||
|
|
caaaaafeb0 | ||
|
|
7e962d60ad |
@@ -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
|
||||||
|
|
||||||
|
|||||||
40
PolyglotFile/polyglotpdfraw.py
Normal file
40
PolyglotFile/polyglotpdfraw.py
Normal 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()
|
||||||
@@ -28,8 +28,9 @@ from PdfFileTransformer import Pdf
|
|||||||
|
|
||||||
class PolyglotSZipPdf(PolyglotPdfZip):
|
class PolyglotSZipPdf(PolyglotPdfZip):
|
||||||
|
|
||||||
def __init__(self, Pdf, Zip):
|
def __init__(self, Pdf, Zip, acrobat_compatibility):
|
||||||
super().__init__(Pdf, Zip)
|
super().__init__(Pdf, Zip)
|
||||||
|
self.acrobat_compatibility = acrobat_compatibility
|
||||||
|
|
||||||
def get_rebuild_zip_first_part_size(self):
|
def get_rebuild_zip_first_part_size(self):
|
||||||
|
|
||||||
@@ -82,7 +83,11 @@ class PolyglotSZipPdf(PolyglotPdfZip):
|
|||||||
new_pdf.file_offset = offset
|
new_pdf.file_offset = offset
|
||||||
pdf_buffer = new_pdf.get_build_buffer()
|
pdf_buffer = new_pdf.get_build_buffer()
|
||||||
j2 = pdf_buffer[k2_stream_offset + size_k2_stream:]
|
j2 = pdf_buffer[k2_stream_offset + size_k2_stream:]
|
||||||
new_zip.add_data_to_file(b'', j2, True)
|
|
||||||
|
if self.acrobat_compatibility:
|
||||||
|
new_zip.add_data_to_file(b'\x00', j2, True)
|
||||||
|
else:
|
||||||
|
new_zip.add_data_to_file(b'', j2, True)
|
||||||
|
|
||||||
return new_zip.buffer
|
return new_zip.buffer
|
||||||
|
|
||||||
|
|||||||
46
truepolyglot
46
truepolyglot
@@ -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.3'
|
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,14 +32,21 @@ 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',
|
||||||
|
dest='acrobat_compatibility',
|
||||||
|
help='Add a byte at start for Acrobat Reader compatibility with szippdf format',
|
||||||
|
action='store_true')
|
||||||
parser.add_argument('--verbose', dest='verbose',
|
parser.add_argument('--verbose', dest='verbose',
|
||||||
help='Verbosity level (default: debug)',
|
help='Verbosity level (default: info)',
|
||||||
default="info",
|
default="info",
|
||||||
choices=["none", "error", "info", "debug"])
|
choices=["none", "error", "info", "debug"])
|
||||||
parser.add_argument('output_file', nargs='+',
|
parser.add_argument('output_file', nargs='+',
|
||||||
@@ -42,12 +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.format[0] in formats:
|
parser.error('--acrobat-compatibility is for szippdf only')
|
||||||
if args.pdffile is None:
|
if "pdf" in args.format[0] and 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)
|
||||||
@@ -58,14 +72,22 @@ def main():
|
|||||||
if args.verbose == "debug":
|
if args.verbose == "debug":
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
p = Pdf(args.pdffile)
|
|
||||||
z = Zip(args.zipfile)
|
|
||||||
if args.format[0] == "pdfzip":
|
if args.format[0] == "pdfzip":
|
||||||
|
p = Pdf(args.pdffile)
|
||||||
|
z = Zip(args.zipfile)
|
||||||
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":
|
||||||
a = PolyglotSZipPdf(p, z)
|
p = Pdf(args.pdffile)
|
||||||
|
z = Zip(args.zipfile)
|
||||||
|
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])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user