PdfParser.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. import calendar
  2. import codecs
  3. import collections
  4. import mmap
  5. import os
  6. import re
  7. import time
  8. import zlib
  9. # see 7.9.2.2 Text String Type on page 86 and D.3 PDFDocEncoding Character Set
  10. # on page 656
  11. def encode_text(s):
  12. return codecs.BOM_UTF16_BE + s.encode("utf_16_be")
  13. PDFDocEncoding = {
  14. 0x16: "\u0017",
  15. 0x18: "\u02D8",
  16. 0x19: "\u02C7",
  17. 0x1A: "\u02C6",
  18. 0x1B: "\u02D9",
  19. 0x1C: "\u02DD",
  20. 0x1D: "\u02DB",
  21. 0x1E: "\u02DA",
  22. 0x1F: "\u02DC",
  23. 0x80: "\u2022",
  24. 0x81: "\u2020",
  25. 0x82: "\u2021",
  26. 0x83: "\u2026",
  27. 0x84: "\u2014",
  28. 0x85: "\u2013",
  29. 0x86: "\u0192",
  30. 0x87: "\u2044",
  31. 0x88: "\u2039",
  32. 0x89: "\u203A",
  33. 0x8A: "\u2212",
  34. 0x8B: "\u2030",
  35. 0x8C: "\u201E",
  36. 0x8D: "\u201C",
  37. 0x8E: "\u201D",
  38. 0x8F: "\u2018",
  39. 0x90: "\u2019",
  40. 0x91: "\u201A",
  41. 0x92: "\u2122",
  42. 0x93: "\uFB01",
  43. 0x94: "\uFB02",
  44. 0x95: "\u0141",
  45. 0x96: "\u0152",
  46. 0x97: "\u0160",
  47. 0x98: "\u0178",
  48. 0x99: "\u017D",
  49. 0x9A: "\u0131",
  50. 0x9B: "\u0142",
  51. 0x9C: "\u0153",
  52. 0x9D: "\u0161",
  53. 0x9E: "\u017E",
  54. 0xA0: "\u20AC",
  55. }
  56. def decode_text(b):
  57. if b[: len(codecs.BOM_UTF16_BE)] == codecs.BOM_UTF16_BE:
  58. return b[len(codecs.BOM_UTF16_BE) :].decode("utf_16_be")
  59. else:
  60. return "".join(PDFDocEncoding.get(byte, chr(byte)) for byte in b)
  61. class PdfFormatError(RuntimeError):
  62. """An error that probably indicates a syntactic or semantic error in the
  63. PDF file structure"""
  64. pass
  65. def check_format_condition(condition, error_message):
  66. if not condition:
  67. raise PdfFormatError(error_message)
  68. class IndirectReference(
  69. collections.namedtuple("IndirectReferenceTuple", ["object_id", "generation"])
  70. ):
  71. def __str__(self):
  72. return "%s %s R" % self
  73. def __bytes__(self):
  74. return self.__str__().encode("us-ascii")
  75. def __eq__(self, other):
  76. return (
  77. other.__class__ is self.__class__
  78. and other.object_id == self.object_id
  79. and other.generation == self.generation
  80. )
  81. def __ne__(self, other):
  82. return not (self == other)
  83. def __hash__(self):
  84. return hash((self.object_id, self.generation))
  85. class IndirectObjectDef(IndirectReference):
  86. def __str__(self):
  87. return "%s %s obj" % self
  88. class XrefTable:
  89. def __init__(self):
  90. self.existing_entries = {} # object ID => (offset, generation)
  91. self.new_entries = {} # object ID => (offset, generation)
  92. self.deleted_entries = {0: 65536} # object ID => generation
  93. self.reading_finished = False
  94. def __setitem__(self, key, value):
  95. if self.reading_finished:
  96. self.new_entries[key] = value
  97. else:
  98. self.existing_entries[key] = value
  99. if key in self.deleted_entries:
  100. del self.deleted_entries[key]
  101. def __getitem__(self, key):
  102. try:
  103. return self.new_entries[key]
  104. except KeyError:
  105. return self.existing_entries[key]
  106. def __delitem__(self, key):
  107. if key in self.new_entries:
  108. generation = self.new_entries[key][1] + 1
  109. del self.new_entries[key]
  110. self.deleted_entries[key] = generation
  111. elif key in self.existing_entries:
  112. generation = self.existing_entries[key][1] + 1
  113. self.deleted_entries[key] = generation
  114. elif key in self.deleted_entries:
  115. generation = self.deleted_entries[key]
  116. else:
  117. raise IndexError(
  118. "object ID " + str(key) + " cannot be deleted because it doesn't exist"
  119. )
  120. def __contains__(self, key):
  121. return key in self.existing_entries or key in self.new_entries
  122. def __len__(self):
  123. return len(
  124. set(self.existing_entries.keys())
  125. | set(self.new_entries.keys())
  126. | set(self.deleted_entries.keys())
  127. )
  128. def keys(self):
  129. return (
  130. set(self.existing_entries.keys()) - set(self.deleted_entries.keys())
  131. ) | set(self.new_entries.keys())
  132. def write(self, f):
  133. keys = sorted(set(self.new_entries.keys()) | set(self.deleted_entries.keys()))
  134. deleted_keys = sorted(set(self.deleted_entries.keys()))
  135. startxref = f.tell()
  136. f.write(b"xref\n")
  137. while keys:
  138. # find a contiguous sequence of object IDs
  139. prev = None
  140. for index, key in enumerate(keys):
  141. if prev is None or prev + 1 == key:
  142. prev = key
  143. else:
  144. contiguous_keys = keys[:index]
  145. keys = keys[index:]
  146. break
  147. else:
  148. contiguous_keys = keys
  149. keys = None
  150. f.write(b"%d %d\n" % (contiguous_keys[0], len(contiguous_keys)))
  151. for object_id in contiguous_keys:
  152. if object_id in self.new_entries:
  153. f.write(b"%010d %05d n \n" % self.new_entries[object_id])
  154. else:
  155. this_deleted_object_id = deleted_keys.pop(0)
  156. check_format_condition(
  157. object_id == this_deleted_object_id,
  158. f"expected the next deleted object ID to be {object_id}, "
  159. f"instead found {this_deleted_object_id}",
  160. )
  161. try:
  162. next_in_linked_list = deleted_keys[0]
  163. except IndexError:
  164. next_in_linked_list = 0
  165. f.write(
  166. b"%010d %05d f \n"
  167. % (next_in_linked_list, self.deleted_entries[object_id])
  168. )
  169. return startxref
  170. class PdfName:
  171. def __init__(self, name):
  172. if isinstance(name, PdfName):
  173. self.name = name.name
  174. elif isinstance(name, bytes):
  175. self.name = name
  176. else:
  177. self.name = name.encode("us-ascii")
  178. def name_as_str(self):
  179. return self.name.decode("us-ascii")
  180. def __eq__(self, other):
  181. return (
  182. isinstance(other, PdfName) and other.name == self.name
  183. ) or other == self.name
  184. def __hash__(self):
  185. return hash(self.name)
  186. def __repr__(self):
  187. return f"PdfName({repr(self.name)})"
  188. @classmethod
  189. def from_pdf_stream(cls, data):
  190. return cls(PdfParser.interpret_name(data))
  191. allowed_chars = set(range(33, 127)) - {ord(c) for c in "#%/()<>[]{}"}
  192. def __bytes__(self):
  193. result = bytearray(b"/")
  194. for b in self.name:
  195. if b in self.allowed_chars:
  196. result.append(b)
  197. else:
  198. result.extend(b"#%02X" % b)
  199. return bytes(result)
  200. class PdfArray(list):
  201. def __bytes__(self):
  202. return b"[ " + b" ".join(pdf_repr(x) for x in self) + b" ]"
  203. class PdfDict(collections.UserDict):
  204. def __setattr__(self, key, value):
  205. if key == "data":
  206. collections.UserDict.__setattr__(self, key, value)
  207. else:
  208. self[key.encode("us-ascii")] = value
  209. def __getattr__(self, key):
  210. try:
  211. value = self[key.encode("us-ascii")]
  212. except KeyError as e:
  213. raise AttributeError(key) from e
  214. if isinstance(value, bytes):
  215. value = decode_text(value)
  216. if key.endswith("Date"):
  217. if value.startswith("D:"):
  218. value = value[2:]
  219. relationship = "Z"
  220. if len(value) > 17:
  221. relationship = value[14]
  222. offset = int(value[15:17]) * 60
  223. if len(value) > 20:
  224. offset += int(value[18:20])
  225. format = "%Y%m%d%H%M%S"[: len(value) - 2]
  226. value = time.strptime(value[: len(format) + 2], format)
  227. if relationship in ["+", "-"]:
  228. offset *= 60
  229. if relationship == "+":
  230. offset *= -1
  231. value = time.gmtime(calendar.timegm(value) + offset)
  232. return value
  233. def __bytes__(self):
  234. out = bytearray(b"<<")
  235. for key, value in self.items():
  236. if value is None:
  237. continue
  238. value = pdf_repr(value)
  239. out.extend(b"\n")
  240. out.extend(bytes(PdfName(key)))
  241. out.extend(b" ")
  242. out.extend(value)
  243. out.extend(b"\n>>")
  244. return bytes(out)
  245. class PdfBinary:
  246. def __init__(self, data):
  247. self.data = data
  248. def __bytes__(self):
  249. return b"<%s>" % b"".join(b"%02X" % b for b in self.data)
  250. class PdfStream:
  251. def __init__(self, dictionary, buf):
  252. self.dictionary = dictionary
  253. self.buf = buf
  254. def decode(self):
  255. try:
  256. filter = self.dictionary.Filter
  257. except AttributeError:
  258. return self.buf
  259. if filter == b"FlateDecode":
  260. try:
  261. expected_length = self.dictionary.DL
  262. except AttributeError:
  263. expected_length = self.dictionary.Length
  264. return zlib.decompress(self.buf, bufsize=int(expected_length))
  265. else:
  266. raise NotImplementedError(
  267. f"stream filter {repr(self.dictionary.Filter)} unknown/unsupported"
  268. )
  269. def pdf_repr(x):
  270. if x is True:
  271. return b"true"
  272. elif x is False:
  273. return b"false"
  274. elif x is None:
  275. return b"null"
  276. elif isinstance(x, (PdfName, PdfDict, PdfArray, PdfBinary)):
  277. return bytes(x)
  278. elif isinstance(x, int):
  279. return str(x).encode("us-ascii")
  280. elif isinstance(x, float):
  281. return str(x).encode("us-ascii")
  282. elif isinstance(x, time.struct_time):
  283. return b"(D:" + time.strftime("%Y%m%d%H%M%SZ", x).encode("us-ascii") + b")"
  284. elif isinstance(x, dict):
  285. return bytes(PdfDict(x))
  286. elif isinstance(x, list):
  287. return bytes(PdfArray(x))
  288. elif isinstance(x, str):
  289. return pdf_repr(encode_text(x))
  290. elif isinstance(x, bytes):
  291. # XXX escape more chars? handle binary garbage
  292. x = x.replace(b"\\", b"\\\\")
  293. x = x.replace(b"(", b"\\(")
  294. x = x.replace(b")", b"\\)")
  295. return b"(" + x + b")"
  296. else:
  297. return bytes(x)
  298. class PdfParser:
  299. """Based on
  300. https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
  301. Supports PDF up to 1.4
  302. """
  303. def __init__(self, filename=None, f=None, buf=None, start_offset=0, mode="rb"):
  304. if buf and f:
  305. raise RuntimeError("specify buf or f or filename, but not both buf and f")
  306. self.filename = filename
  307. self.buf = buf
  308. self.f = f
  309. self.start_offset = start_offset
  310. self.should_close_buf = False
  311. self.should_close_file = False
  312. if filename is not None and f is None:
  313. self.f = f = open(filename, mode)
  314. self.should_close_file = True
  315. if f is not None:
  316. self.buf = buf = self.get_buf_from_file(f)
  317. self.should_close_buf = True
  318. if not filename and hasattr(f, "name"):
  319. self.filename = f.name
  320. self.cached_objects = {}
  321. if buf:
  322. self.read_pdf_info()
  323. else:
  324. self.file_size_total = self.file_size_this = 0
  325. self.root = PdfDict()
  326. self.root_ref = None
  327. self.info = PdfDict()
  328. self.info_ref = None
  329. self.page_tree_root = {}
  330. self.pages = []
  331. self.orig_pages = []
  332. self.pages_ref = None
  333. self.last_xref_section_offset = None
  334. self.trailer_dict = {}
  335. self.xref_table = XrefTable()
  336. self.xref_table.reading_finished = True
  337. if f:
  338. self.seek_end()
  339. def __enter__(self):
  340. return self
  341. def __exit__(self, exc_type, exc_value, traceback):
  342. self.close()
  343. return False # do not suppress exceptions
  344. def start_writing(self):
  345. self.close_buf()
  346. self.seek_end()
  347. def close_buf(self):
  348. try:
  349. self.buf.close()
  350. except AttributeError:
  351. pass
  352. self.buf = None
  353. def close(self):
  354. if self.should_close_buf:
  355. self.close_buf()
  356. if self.f is not None and self.should_close_file:
  357. self.f.close()
  358. self.f = None
  359. def seek_end(self):
  360. self.f.seek(0, os.SEEK_END)
  361. def write_header(self):
  362. self.f.write(b"%PDF-1.4\n")
  363. def write_comment(self, s):
  364. self.f.write(f"% {s}\n".encode("utf-8"))
  365. def write_catalog(self):
  366. self.del_root()
  367. self.root_ref = self.next_object_id(self.f.tell())
  368. self.pages_ref = self.next_object_id(0)
  369. self.rewrite_pages()
  370. self.write_obj(self.root_ref, Type=PdfName(b"Catalog"), Pages=self.pages_ref)
  371. self.write_obj(
  372. self.pages_ref,
  373. Type=PdfName(b"Pages"),
  374. Count=len(self.pages),
  375. Kids=self.pages,
  376. )
  377. return self.root_ref
  378. def rewrite_pages(self):
  379. pages_tree_nodes_to_delete = []
  380. for i, page_ref in enumerate(self.orig_pages):
  381. page_info = self.cached_objects[page_ref]
  382. del self.xref_table[page_ref.object_id]
  383. pages_tree_nodes_to_delete.append(page_info[PdfName(b"Parent")])
  384. if page_ref not in self.pages:
  385. # the page has been deleted
  386. continue
  387. # make dict keys into strings for passing to write_page
  388. stringified_page_info = {}
  389. for key, value in page_info.items():
  390. # key should be a PdfName
  391. stringified_page_info[key.name_as_str()] = value
  392. stringified_page_info["Parent"] = self.pages_ref
  393. new_page_ref = self.write_page(None, **stringified_page_info)
  394. for j, cur_page_ref in enumerate(self.pages):
  395. if cur_page_ref == page_ref:
  396. # replace the page reference with the new one
  397. self.pages[j] = new_page_ref
  398. # delete redundant Pages tree nodes from xref table
  399. for pages_tree_node_ref in pages_tree_nodes_to_delete:
  400. while pages_tree_node_ref:
  401. pages_tree_node = self.cached_objects[pages_tree_node_ref]
  402. if pages_tree_node_ref.object_id in self.xref_table:
  403. del self.xref_table[pages_tree_node_ref.object_id]
  404. pages_tree_node_ref = pages_tree_node.get(b"Parent", None)
  405. self.orig_pages = []
  406. def write_xref_and_trailer(self, new_root_ref=None):
  407. if new_root_ref:
  408. self.del_root()
  409. self.root_ref = new_root_ref
  410. if self.info:
  411. self.info_ref = self.write_obj(None, self.info)
  412. start_xref = self.xref_table.write(self.f)
  413. num_entries = len(self.xref_table)
  414. trailer_dict = {b"Root": self.root_ref, b"Size": num_entries}
  415. if self.last_xref_section_offset is not None:
  416. trailer_dict[b"Prev"] = self.last_xref_section_offset
  417. if self.info:
  418. trailer_dict[b"Info"] = self.info_ref
  419. self.last_xref_section_offset = start_xref
  420. self.f.write(
  421. b"trailer\n"
  422. + bytes(PdfDict(trailer_dict))
  423. + b"\nstartxref\n%d\n%%%%EOF" % start_xref
  424. )
  425. def write_page(self, ref, *objs, **dict_obj):
  426. if isinstance(ref, int):
  427. ref = self.pages[ref]
  428. if "Type" not in dict_obj:
  429. dict_obj["Type"] = PdfName(b"Page")
  430. if "Parent" not in dict_obj:
  431. dict_obj["Parent"] = self.pages_ref
  432. return self.write_obj(ref, *objs, **dict_obj)
  433. def write_obj(self, ref, *objs, **dict_obj):
  434. f = self.f
  435. if ref is None:
  436. ref = self.next_object_id(f.tell())
  437. else:
  438. self.xref_table[ref.object_id] = (f.tell(), ref.generation)
  439. f.write(bytes(IndirectObjectDef(*ref)))
  440. stream = dict_obj.pop("stream", None)
  441. if stream is not None:
  442. dict_obj["Length"] = len(stream)
  443. if dict_obj:
  444. f.write(pdf_repr(dict_obj))
  445. for obj in objs:
  446. f.write(pdf_repr(obj))
  447. if stream is not None:
  448. f.write(b"stream\n")
  449. f.write(stream)
  450. f.write(b"\nendstream\n")
  451. f.write(b"endobj\n")
  452. return ref
  453. def del_root(self):
  454. if self.root_ref is None:
  455. return
  456. del self.xref_table[self.root_ref.object_id]
  457. del self.xref_table[self.root[b"Pages"].object_id]
  458. @staticmethod
  459. def get_buf_from_file(f):
  460. if hasattr(f, "getbuffer"):
  461. return f.getbuffer()
  462. elif hasattr(f, "getvalue"):
  463. return f.getvalue()
  464. else:
  465. try:
  466. return mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
  467. except ValueError: # cannot mmap an empty file
  468. return b""
  469. def read_pdf_info(self):
  470. self.file_size_total = len(self.buf)
  471. self.file_size_this = self.file_size_total - self.start_offset
  472. self.read_trailer()
  473. self.root_ref = self.trailer_dict[b"Root"]
  474. self.info_ref = self.trailer_dict.get(b"Info", None)
  475. self.root = PdfDict(self.read_indirect(self.root_ref))
  476. if self.info_ref is None:
  477. self.info = PdfDict()
  478. else:
  479. self.info = PdfDict(self.read_indirect(self.info_ref))
  480. check_format_condition(b"Type" in self.root, "/Type missing in Root")
  481. check_format_condition(
  482. self.root[b"Type"] == b"Catalog", "/Type in Root is not /Catalog"
  483. )
  484. check_format_condition(b"Pages" in self.root, "/Pages missing in Root")
  485. check_format_condition(
  486. isinstance(self.root[b"Pages"], IndirectReference),
  487. "/Pages in Root is not an indirect reference",
  488. )
  489. self.pages_ref = self.root[b"Pages"]
  490. self.page_tree_root = self.read_indirect(self.pages_ref)
  491. self.pages = self.linearize_page_tree(self.page_tree_root)
  492. # save the original list of page references
  493. # in case the user modifies, adds or deletes some pages
  494. # and we need to rewrite the pages and their list
  495. self.orig_pages = self.pages[:]
  496. def next_object_id(self, offset=None):
  497. try:
  498. # TODO: support reuse of deleted objects
  499. reference = IndirectReference(max(self.xref_table.keys()) + 1, 0)
  500. except ValueError:
  501. reference = IndirectReference(1, 0)
  502. if offset is not None:
  503. self.xref_table[reference.object_id] = (offset, 0)
  504. return reference
  505. delimiter = br"[][()<>{}/%]"
  506. delimiter_or_ws = br"[][()<>{}/%\000\011\012\014\015\040]"
  507. whitespace = br"[\000\011\012\014\015\040]"
  508. whitespace_or_hex = br"[\000\011\012\014\015\0400-9a-fA-F]"
  509. whitespace_optional = whitespace + b"*"
  510. whitespace_mandatory = whitespace + b"+"
  511. whitespace_optional_no_nl = br"[\000\011\014\015\040]*" # no "\012" aka "\n"
  512. newline_only = br"[\r\n]+"
  513. newline = whitespace_optional_no_nl + newline_only + whitespace_optional_no_nl
  514. re_trailer_end = re.compile(
  515. whitespace_mandatory
  516. + br"trailer"
  517. + whitespace_optional
  518. + br"\<\<(.*\>\>)"
  519. + newline
  520. + br"startxref"
  521. + newline
  522. + br"([0-9]+)"
  523. + newline
  524. + br"%%EOF"
  525. + whitespace_optional
  526. + br"$",
  527. re.DOTALL,
  528. )
  529. re_trailer_prev = re.compile(
  530. whitespace_optional
  531. + br"trailer"
  532. + whitespace_optional
  533. + br"\<\<(.*?\>\>)"
  534. + newline
  535. + br"startxref"
  536. + newline
  537. + br"([0-9]+)"
  538. + newline
  539. + br"%%EOF"
  540. + whitespace_optional,
  541. re.DOTALL,
  542. )
  543. def read_trailer(self):
  544. search_start_offset = len(self.buf) - 16384
  545. if search_start_offset < self.start_offset:
  546. search_start_offset = self.start_offset
  547. m = self.re_trailer_end.search(self.buf, search_start_offset)
  548. check_format_condition(m, "trailer end not found")
  549. # make sure we found the LAST trailer
  550. last_match = m
  551. while m:
  552. last_match = m
  553. m = self.re_trailer_end.search(self.buf, m.start() + 16)
  554. if not m:
  555. m = last_match
  556. trailer_data = m.group(1)
  557. self.last_xref_section_offset = int(m.group(2))
  558. self.trailer_dict = self.interpret_trailer(trailer_data)
  559. self.xref_table = XrefTable()
  560. self.read_xref_table(xref_section_offset=self.last_xref_section_offset)
  561. if b"Prev" in self.trailer_dict:
  562. self.read_prev_trailer(self.trailer_dict[b"Prev"])
  563. def read_prev_trailer(self, xref_section_offset):
  564. trailer_offset = self.read_xref_table(xref_section_offset=xref_section_offset)
  565. m = self.re_trailer_prev.search(
  566. self.buf[trailer_offset : trailer_offset + 16384]
  567. )
  568. check_format_condition(m, "previous trailer not found")
  569. trailer_data = m.group(1)
  570. check_format_condition(
  571. int(m.group(2)) == xref_section_offset,
  572. "xref section offset in previous trailer doesn't match what was expected",
  573. )
  574. trailer_dict = self.interpret_trailer(trailer_data)
  575. if b"Prev" in trailer_dict:
  576. self.read_prev_trailer(trailer_dict[b"Prev"])
  577. re_whitespace_optional = re.compile(whitespace_optional)
  578. re_name = re.compile(
  579. whitespace_optional
  580. + br"/([!-$&'*-.0-;=?-Z\\^-z|~]+)(?="
  581. + delimiter_or_ws
  582. + br")"
  583. )
  584. re_dict_start = re.compile(whitespace_optional + br"\<\<")
  585. re_dict_end = re.compile(whitespace_optional + br"\>\>" + whitespace_optional)
  586. @classmethod
  587. def interpret_trailer(cls, trailer_data):
  588. trailer = {}
  589. offset = 0
  590. while True:
  591. m = cls.re_name.match(trailer_data, offset)
  592. if not m:
  593. m = cls.re_dict_end.match(trailer_data, offset)
  594. check_format_condition(
  595. m and m.end() == len(trailer_data),
  596. "name not found in trailer, remaining data: "
  597. + repr(trailer_data[offset:]),
  598. )
  599. break
  600. key = cls.interpret_name(m.group(1))
  601. value, offset = cls.get_value(trailer_data, m.end())
  602. trailer[key] = value
  603. check_format_condition(
  604. b"Size" in trailer and isinstance(trailer[b"Size"], int),
  605. "/Size not in trailer or not an integer",
  606. )
  607. check_format_condition(
  608. b"Root" in trailer and isinstance(trailer[b"Root"], IndirectReference),
  609. "/Root not in trailer or not an indirect reference",
  610. )
  611. return trailer
  612. re_hashes_in_name = re.compile(br"([^#]*)(#([0-9a-fA-F]{2}))?")
  613. @classmethod
  614. def interpret_name(cls, raw, as_text=False):
  615. name = b""
  616. for m in cls.re_hashes_in_name.finditer(raw):
  617. if m.group(3):
  618. name += m.group(1) + bytearray.fromhex(m.group(3).decode("us-ascii"))
  619. else:
  620. name += m.group(1)
  621. if as_text:
  622. return name.decode("utf-8")
  623. else:
  624. return bytes(name)
  625. re_null = re.compile(whitespace_optional + br"null(?=" + delimiter_or_ws + br")")
  626. re_true = re.compile(whitespace_optional + br"true(?=" + delimiter_or_ws + br")")
  627. re_false = re.compile(whitespace_optional + br"false(?=" + delimiter_or_ws + br")")
  628. re_int = re.compile(
  629. whitespace_optional + br"([-+]?[0-9]+)(?=" + delimiter_or_ws + br")"
  630. )
  631. re_real = re.compile(
  632. whitespace_optional
  633. + br"([-+]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+))(?="
  634. + delimiter_or_ws
  635. + br")"
  636. )
  637. re_array_start = re.compile(whitespace_optional + br"\[")
  638. re_array_end = re.compile(whitespace_optional + br"]")
  639. re_string_hex = re.compile(
  640. whitespace_optional + br"\<(" + whitespace_or_hex + br"*)\>"
  641. )
  642. re_string_lit = re.compile(whitespace_optional + br"\(")
  643. re_indirect_reference = re.compile(
  644. whitespace_optional
  645. + br"([-+]?[0-9]+)"
  646. + whitespace_mandatory
  647. + br"([-+]?[0-9]+)"
  648. + whitespace_mandatory
  649. + br"R(?="
  650. + delimiter_or_ws
  651. + br")"
  652. )
  653. re_indirect_def_start = re.compile(
  654. whitespace_optional
  655. + br"([-+]?[0-9]+)"
  656. + whitespace_mandatory
  657. + br"([-+]?[0-9]+)"
  658. + whitespace_mandatory
  659. + br"obj(?="
  660. + delimiter_or_ws
  661. + br")"
  662. )
  663. re_indirect_def_end = re.compile(
  664. whitespace_optional + br"endobj(?=" + delimiter_or_ws + br")"
  665. )
  666. re_comment = re.compile(
  667. br"(" + whitespace_optional + br"%[^\r\n]*" + newline + br")*"
  668. )
  669. re_stream_start = re.compile(whitespace_optional + br"stream\r?\n")
  670. re_stream_end = re.compile(
  671. whitespace_optional + br"endstream(?=" + delimiter_or_ws + br")"
  672. )
  673. @classmethod
  674. def get_value(cls, data, offset, expect_indirect=None, max_nesting=-1):
  675. if max_nesting == 0:
  676. return None, None
  677. m = cls.re_comment.match(data, offset)
  678. if m:
  679. offset = m.end()
  680. m = cls.re_indirect_def_start.match(data, offset)
  681. if m:
  682. check_format_condition(
  683. int(m.group(1)) > 0,
  684. "indirect object definition: object ID must be greater than 0",
  685. )
  686. check_format_condition(
  687. int(m.group(2)) >= 0,
  688. "indirect object definition: generation must be non-negative",
  689. )
  690. check_format_condition(
  691. expect_indirect is None
  692. or expect_indirect
  693. == IndirectReference(int(m.group(1)), int(m.group(2))),
  694. "indirect object definition different than expected",
  695. )
  696. object, offset = cls.get_value(data, m.end(), max_nesting=max_nesting - 1)
  697. if offset is None:
  698. return object, None
  699. m = cls.re_indirect_def_end.match(data, offset)
  700. check_format_condition(m, "indirect object definition end not found")
  701. return object, m.end()
  702. check_format_condition(
  703. not expect_indirect, "indirect object definition not found"
  704. )
  705. m = cls.re_indirect_reference.match(data, offset)
  706. if m:
  707. check_format_condition(
  708. int(m.group(1)) > 0,
  709. "indirect object reference: object ID must be greater than 0",
  710. )
  711. check_format_condition(
  712. int(m.group(2)) >= 0,
  713. "indirect object reference: generation must be non-negative",
  714. )
  715. return IndirectReference(int(m.group(1)), int(m.group(2))), m.end()
  716. m = cls.re_dict_start.match(data, offset)
  717. if m:
  718. offset = m.end()
  719. result = {}
  720. m = cls.re_dict_end.match(data, offset)
  721. while not m:
  722. key, offset = cls.get_value(data, offset, max_nesting=max_nesting - 1)
  723. if offset is None:
  724. return result, None
  725. value, offset = cls.get_value(data, offset, max_nesting=max_nesting - 1)
  726. result[key] = value
  727. if offset is None:
  728. return result, None
  729. m = cls.re_dict_end.match(data, offset)
  730. offset = m.end()
  731. m = cls.re_stream_start.match(data, offset)
  732. if m:
  733. try:
  734. stream_len = int(result[b"Length"])
  735. except (TypeError, KeyError, ValueError) as e:
  736. raise PdfFormatError(
  737. "bad or missing Length in stream dict (%r)"
  738. % result.get(b"Length", None)
  739. ) from e
  740. stream_data = data[m.end() : m.end() + stream_len]
  741. m = cls.re_stream_end.match(data, m.end() + stream_len)
  742. check_format_condition(m, "stream end not found")
  743. offset = m.end()
  744. result = PdfStream(PdfDict(result), stream_data)
  745. else:
  746. result = PdfDict(result)
  747. return result, offset
  748. m = cls.re_array_start.match(data, offset)
  749. if m:
  750. offset = m.end()
  751. result = []
  752. m = cls.re_array_end.match(data, offset)
  753. while not m:
  754. value, offset = cls.get_value(data, offset, max_nesting=max_nesting - 1)
  755. result.append(value)
  756. if offset is None:
  757. return result, None
  758. m = cls.re_array_end.match(data, offset)
  759. return result, m.end()
  760. m = cls.re_null.match(data, offset)
  761. if m:
  762. return None, m.end()
  763. m = cls.re_true.match(data, offset)
  764. if m:
  765. return True, m.end()
  766. m = cls.re_false.match(data, offset)
  767. if m:
  768. return False, m.end()
  769. m = cls.re_name.match(data, offset)
  770. if m:
  771. return PdfName(cls.interpret_name(m.group(1))), m.end()
  772. m = cls.re_int.match(data, offset)
  773. if m:
  774. return int(m.group(1)), m.end()
  775. m = cls.re_real.match(data, offset)
  776. if m:
  777. # XXX Decimal instead of float???
  778. return float(m.group(1)), m.end()
  779. m = cls.re_string_hex.match(data, offset)
  780. if m:
  781. # filter out whitespace
  782. hex_string = bytearray(
  783. [b for b in m.group(1) if b in b"0123456789abcdefABCDEF"]
  784. )
  785. if len(hex_string) % 2 == 1:
  786. # append a 0 if the length is not even - yes, at the end
  787. hex_string.append(ord(b"0"))
  788. return bytearray.fromhex(hex_string.decode("us-ascii")), m.end()
  789. m = cls.re_string_lit.match(data, offset)
  790. if m:
  791. return cls.get_literal_string(data, m.end())
  792. # return None, offset # fallback (only for debugging)
  793. raise PdfFormatError("unrecognized object: " + repr(data[offset : offset + 32]))
  794. re_lit_str_token = re.compile(
  795. br"(\\[nrtbf()\\])|(\\[0-9]{1,3})|(\\(\r\n|\r|\n))|(\r\n|\r|\n)|(\()|(\))"
  796. )
  797. escaped_chars = {
  798. b"n": b"\n",
  799. b"r": b"\r",
  800. b"t": b"\t",
  801. b"b": b"\b",
  802. b"f": b"\f",
  803. b"(": b"(",
  804. b")": b")",
  805. b"\\": b"\\",
  806. ord(b"n"): b"\n",
  807. ord(b"r"): b"\r",
  808. ord(b"t"): b"\t",
  809. ord(b"b"): b"\b",
  810. ord(b"f"): b"\f",
  811. ord(b"("): b"(",
  812. ord(b")"): b")",
  813. ord(b"\\"): b"\\",
  814. }
  815. @classmethod
  816. def get_literal_string(cls, data, offset):
  817. nesting_depth = 0
  818. result = bytearray()
  819. for m in cls.re_lit_str_token.finditer(data, offset):
  820. result.extend(data[offset : m.start()])
  821. if m.group(1):
  822. result.extend(cls.escaped_chars[m.group(1)[1]])
  823. elif m.group(2):
  824. result.append(int(m.group(2)[1:], 8))
  825. elif m.group(3):
  826. pass
  827. elif m.group(5):
  828. result.extend(b"\n")
  829. elif m.group(6):
  830. result.extend(b"(")
  831. nesting_depth += 1
  832. elif m.group(7):
  833. if nesting_depth == 0:
  834. return bytes(result), m.end()
  835. result.extend(b")")
  836. nesting_depth -= 1
  837. offset = m.end()
  838. raise PdfFormatError("unfinished literal string")
  839. re_xref_section_start = re.compile(whitespace_optional + br"xref" + newline)
  840. re_xref_subsection_start = re.compile(
  841. whitespace_optional
  842. + br"([0-9]+)"
  843. + whitespace_mandatory
  844. + br"([0-9]+)"
  845. + whitespace_optional
  846. + newline_only
  847. )
  848. re_xref_entry = re.compile(br"([0-9]{10}) ([0-9]{5}) ([fn])( \r| \n|\r\n)")
  849. def read_xref_table(self, xref_section_offset):
  850. subsection_found = False
  851. m = self.re_xref_section_start.match(
  852. self.buf, xref_section_offset + self.start_offset
  853. )
  854. check_format_condition(m, "xref section start not found")
  855. offset = m.end()
  856. while True:
  857. m = self.re_xref_subsection_start.match(self.buf, offset)
  858. if not m:
  859. check_format_condition(
  860. subsection_found, "xref subsection start not found"
  861. )
  862. break
  863. subsection_found = True
  864. offset = m.end()
  865. first_object = int(m.group(1))
  866. num_objects = int(m.group(2))
  867. for i in range(first_object, first_object + num_objects):
  868. m = self.re_xref_entry.match(self.buf, offset)
  869. check_format_condition(m, "xref entry not found")
  870. offset = m.end()
  871. is_free = m.group(3) == b"f"
  872. generation = int(m.group(2))
  873. if not is_free:
  874. new_entry = (int(m.group(1)), generation)
  875. check_format_condition(
  876. i not in self.xref_table or self.xref_table[i] == new_entry,
  877. "xref entry duplicated (and not identical)",
  878. )
  879. self.xref_table[i] = new_entry
  880. return offset
  881. def read_indirect(self, ref, max_nesting=-1):
  882. offset, generation = self.xref_table[ref[0]]
  883. check_format_condition(
  884. generation == ref[1],
  885. f"expected to find generation {ref[1]} for object ID {ref[0]} in xref "
  886. f"table, instead found generation {generation} at offset {offset}",
  887. )
  888. value = self.get_value(
  889. self.buf,
  890. offset + self.start_offset,
  891. expect_indirect=IndirectReference(*ref),
  892. max_nesting=max_nesting,
  893. )[0]
  894. self.cached_objects[ref] = value
  895. return value
  896. def linearize_page_tree(self, node=None):
  897. if node is None:
  898. node = self.page_tree_root
  899. check_format_condition(
  900. node[b"Type"] == b"Pages", "/Type of page tree node is not /Pages"
  901. )
  902. pages = []
  903. for kid in node[b"Kids"]:
  904. kid_object = self.read_indirect(kid)
  905. if kid_object[b"Type"] == b"Page":
  906. pages.append(kid)
  907. else:
  908. pages.extend(self.linearize_page_tree(node=kid_object))
  909. return pages