blob: 10ca241242f9fdffb2e205876b8ee7ceb430d7af [file] [log] [blame]
Tom Sepezc8f6ab62015-01-22 11:20:06 -08001#!/usr/bin/env python
2# Copyright 2014 The PDFium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Expands a hand-written PDF testcase (template) into a valid PDF file.
7
8There are several places in a PDF file where byte-offsets are required. This
9script replaces {{name}}-style variables in the input with calculated results
10
11 {{header}} - expands to the header comment required for PDF files.
12 {{xref}} - expands to a generated xref table, noting the offset.
Lei Zhangbdb330e2017-06-26 12:23:51 -070013 {{trailer}} - expands to a standard trailer with "1 0 R" as the /Root.
Tom Sepezc8f6ab62015-01-22 11:20:06 -080014 {{startxref} - expands to a startxref directive followed by correct offset.
Lei Zhangbdb330e2017-06-26 12:23:51 -070015 {{object x y}} - expands to |x y obj| declaration, noting the offset.
16"""
Tom Sepezc8f6ab62015-01-22 11:20:06 -080017
18import optparse
19import os
20import re
21import sys
22
23class TemplateProcessor:
Lei Zhangbdb330e2017-06-26 12:23:51 -070024 HEADER_TOKEN = '{{header}}'
Tom Sepezc8f6ab62015-01-22 11:20:06 -080025 HEADER_REPLACEMENT = '%PDF-1.7\n%\xa0\xf2\xa4\xf4'
26
27 XREF_TOKEN = '{{xref}}'
28 XREF_REPLACEMENT = 'xref\n%d %d\n'
Tom Sepez93daa3c2015-02-05 10:51:54 -080029
Tom Sepez93daa3c2015-02-05 10:51:54 -080030 XREF_REPLACEMENT_N = '%010d %05d n \n'
31 XREF_REPLACEMENT_F = '0000000000 65535 f \n'
Lei Zhangbdb330e2017-06-26 12:23:51 -070032 # XREF rows must be exactly 20 bytes - space required.
33 assert(len(XREF_REPLACEMENT_F) == 20)
34
35 TRAILER_TOKEN = '{{trailer}}'
36 TRAILER_REPLACEMENT = 'trailer<< /Root 1 0 R /Size %d >>'
Tom Sepezc8f6ab62015-01-22 11:20:06 -080037
38 STARTXREF_TOKEN= '{{startxref}}'
39 STARTXREF_REPLACEMENT = 'startxref\n%d'
40
41 OBJECT_PATTERN = r'\{\{object\s+(\d+)\s+(\d+)\}\}'
42 OBJECT_REPLACEMENT = r'\1 \2 obj'
43
44 def __init__(self):
45 self.offset = 0
46 self.xref_offset = 0
47 self.max_object_number = 0
48 self.objects = { }
49
50 def insert_xref_entry(self, object_number, generation_number):
51 self.objects[object_number] = (self.offset, generation_number)
52 self.max_object_number = max(self.max_object_number, object_number)
53
54 def generate_xref_table(self):
55 result = self.XREF_REPLACEMENT % (0, self.max_object_number + 1)
56 for i in range(0, self.max_object_number + 1):
57 if i in self.objects:
58 result += self.XREF_REPLACEMENT_N % self.objects[i]
59 else:
60 result += self.XREF_REPLACEMENT_F
61 return result
62
63 def process_line(self, line):
64 if self.HEADER_TOKEN in line:
65 line = line.replace(self.HEADER_TOKEN, self.HEADER_REPLACEMENT)
66 if self.XREF_TOKEN in line:
67 self.xref_offset = self.offset
68 line = self.generate_xref_table()
Lei Zhangbdb330e2017-06-26 12:23:51 -070069 if self.TRAILER_TOKEN in line:
70 replacement = self.TRAILER_REPLACEMENT % (self.max_object_number + 1)
71 line = line.replace(self.TRAILER_TOKEN, replacement)
Tom Sepezc8f6ab62015-01-22 11:20:06 -080072 if self.STARTXREF_TOKEN in line:
73 replacement = self.STARTXREF_REPLACEMENT % self.xref_offset
74 line = line.replace(self.STARTXREF_TOKEN, replacement)
75 match = re.match(self.OBJECT_PATTERN, line)
76 if match:
77 self.insert_xref_entry(int(match.group(1)), int(match.group(2)))
78 line = re.sub(self.OBJECT_PATTERN, self.OBJECT_REPLACEMENT, line)
79 self.offset += len(line)
80 return line
81
Tom Sepezb7cb36a2015-02-13 16:54:48 -080082
83def expand_file(input_path, output_path):
Tom Sepezc8f6ab62015-01-22 11:20:06 -080084 processor = TemplateProcessor()
85 try:
Lei Zhang44aa03e2015-09-03 14:50:01 -070086 with open(input_path, 'rb') as infile:
87 with open(output_path, 'wb') as outfile:
Tom Sepezc8f6ab62015-01-22 11:20:06 -080088 for line in infile:
89 outfile.write(processor.process_line(line))
90 except IOError:
Tom Sepezb7cb36a2015-02-13 16:54:48 -080091 print >> sys.stderr, 'failed to process %s' % input_path
92
Tom Sepezc8f6ab62015-01-22 11:20:06 -080093
94def main():
Tom Sepezb7cb36a2015-02-13 16:54:48 -080095 parser = optparse.OptionParser()
96 parser.add_option('--output-dir', default='')
97 options, args = parser.parse_args()
98 for testcase_path in args:
99 testcase_filename = os.path.basename(testcase_path)
100 testcase_root, _ = os.path.splitext(testcase_filename)
101 output_dir = os.path.dirname(testcase_path)
102 if options.output_dir:
103 output_dir = options.output_dir
104 output_path = os.path.join(output_dir, testcase_root + '.pdf')
105 expand_file(testcase_path, output_path)
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800106 return 0
107
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800108
Tom Sepezc8f6ab62015-01-22 11:20:06 -0800109if __name__ == '__main__':
110 sys.exit(main())