origami 1.2.7 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -0
- data/README.md +112 -0
- data/bin/config/pdfcop.conf.yml +232 -233
- data/bin/gui/about.rb +27 -37
- data/bin/gui/config.rb +108 -117
- data/bin/gui/file.rb +416 -365
- data/bin/gui/gtkhex.rb +1138 -1153
- data/bin/gui/hexview.rb +55 -57
- data/bin/gui/imgview.rb +48 -51
- data/bin/gui/menu.rb +388 -386
- data/bin/gui/properties.rb +114 -130
- data/bin/gui/signing.rb +571 -617
- data/bin/gui/textview.rb +77 -95
- data/bin/gui/treeview.rb +382 -387
- data/bin/gui/walker.rb +227 -232
- data/bin/gui/xrefs.rb +56 -60
- data/bin/pdf2pdfa +53 -57
- data/bin/pdf2ruby +212 -228
- data/bin/pdfcop +338 -348
- data/bin/pdfdecompress +58 -65
- data/bin/pdfdecrypt +56 -60
- data/bin/pdfencrypt +75 -80
- data/bin/pdfexplode +185 -182
- data/bin/pdfextract +201 -218
- data/bin/pdfmetadata +83 -82
- data/bin/pdfsh +4 -5
- data/bin/pdfwalker +1 -2
- data/bin/shell/.irbrc +45 -82
- data/bin/shell/console.rb +105 -130
- data/bin/shell/hexdump.rb +40 -64
- data/examples/README.md +34 -0
- data/examples/attachments/attachment.rb +38 -0
- data/examples/attachments/nested_document.rb +51 -0
- data/examples/encryption/encryption.rb +28 -0
- data/{samples/actions/triggerevents/trigger.rb → examples/events/events.rb} +13 -16
- data/examples/flash/flash.rb +37 -0
- data/{samples → examples}/flash/helloworld.swf +0 -0
- data/examples/forms/javascript.rb +54 -0
- data/examples/forms/xfa.rb +115 -0
- data/examples/javascript/hello_world.rb +22 -0
- data/examples/javascript/js_emulation.rb +54 -0
- data/examples/loop/goto.rb +32 -0
- data/examples/loop/named.rb +33 -0
- data/examples/signature/signature.rb +65 -0
- data/examples/uri/javascript.rb +56 -0
- data/examples/uri/open-uri.rb +21 -0
- data/examples/uri/submitform.rb +47 -0
- data/lib/origami.rb +29 -42
- data/lib/origami/3d.rb +350 -225
- data/lib/origami/acroform.rb +262 -288
- data/lib/origami/actions.rb +268 -288
- data/lib/origami/annotations.rb +697 -722
- data/lib/origami/array.rb +258 -184
- data/lib/origami/boolean.rb +74 -84
- data/lib/origami/catalog.rb +397 -434
- data/lib/origami/collections.rb +144 -0
- data/lib/origami/destinations.rb +233 -194
- data/lib/origami/dictionary.rb +253 -232
- data/lib/origami/encryption.rb +1274 -1243
- data/lib/origami/export.rb +232 -268
- data/lib/origami/extensions/fdf.rb +307 -220
- data/lib/origami/extensions/ppklite.rb +368 -435
- data/lib/origami/filespec.rb +197 -0
- data/lib/origami/filters.rb +301 -295
- data/lib/origami/filters/ascii.rb +177 -180
- data/lib/origami/filters/ccitt.rb +528 -535
- data/lib/origami/filters/crypt.rb +26 -35
- data/lib/origami/filters/dct.rb +46 -52
- data/lib/origami/filters/flate.rb +95 -94
- data/lib/origami/filters/jbig2.rb +49 -55
- data/lib/origami/filters/jpx.rb +38 -44
- data/lib/origami/filters/lzw.rb +189 -183
- data/lib/origami/filters/predictors.rb +221 -235
- data/lib/origami/filters/runlength.rb +103 -104
- data/lib/origami/font.rb +173 -186
- data/lib/origami/functions.rb +67 -81
- data/lib/origami/graphics.rb +25 -21
- data/lib/origami/graphics/colors.rb +178 -187
- data/lib/origami/graphics/instruction.rb +79 -85
- data/lib/origami/graphics/path.rb +142 -148
- data/lib/origami/graphics/patterns.rb +160 -167
- data/lib/origami/graphics/render.rb +43 -50
- data/lib/origami/graphics/state.rb +138 -153
- data/lib/origami/graphics/text.rb +188 -205
- data/lib/origami/graphics/xobject.rb +819 -815
- data/lib/origami/header.rb +63 -78
- data/lib/origami/javascript.rb +596 -597
- data/lib/origami/linearization.rb +285 -290
- data/lib/origami/metadata.rb +139 -148
- data/lib/origami/name.rb +112 -148
- data/lib/origami/null.rb +53 -62
- data/lib/origami/numeric.rb +162 -175
- data/lib/origami/obfuscation.rb +186 -174
- data/lib/origami/object.rb +593 -573
- data/lib/origami/outline.rb +42 -47
- data/lib/origami/outputintents.rb +73 -82
- data/lib/origami/page.rb +703 -592
- data/lib/origami/parser.rb +238 -290
- data/lib/origami/parsers/fdf.rb +41 -33
- data/lib/origami/parsers/pdf.rb +75 -95
- data/lib/origami/parsers/pdf/lazy.rb +137 -0
- data/lib/origami/parsers/pdf/linear.rb +64 -66
- data/lib/origami/parsers/ppklite.rb +34 -70
- data/lib/origami/pdf.rb +1030 -1005
- data/lib/origami/reference.rb +102 -102
- data/lib/origami/signature.rb +591 -609
- data/lib/origami/stream.rb +668 -551
- data/lib/origami/string.rb +397 -373
- data/lib/origami/template/patterns.rb +56 -0
- data/lib/origami/template/widgets.rb +151 -0
- data/lib/origami/trailer.rb +144 -158
- data/lib/origami/tree.rb +62 -0
- data/lib/origami/version.rb +23 -0
- data/lib/origami/webcapture.rb +88 -79
- data/lib/origami/xfa.rb +2863 -2882
- data/lib/origami/xreftable.rb +472 -384
- data/test/dataset/calc.pdf +85 -0
- data/test/dataset/crypto.pdf +82 -0
- data/test/dataset/empty.pdf +49 -0
- data/test/test_actions.rb +27 -0
- data/test/test_annotations.rb +90 -0
- data/test/test_pages.rb +31 -0
- data/test/test_pdf.rb +16 -0
- data/test/test_pdf_attachment.rb +34 -0
- data/test/test_pdf_create.rb +24 -0
- data/test/test_pdf_encrypt.rb +95 -0
- data/test/test_pdf_parse.rb +96 -0
- data/test/test_pdf_sign.rb +58 -0
- data/test/test_streams.rb +182 -0
- data/test/test_xrefs.rb +67 -0
- metadata +88 -58
- data/README +0 -67
- data/bin/pdf2graph +0 -121
- data/bin/pdfcocoon +0 -104
- data/lib/origami/file.rb +0 -233
- data/samples/README.txt +0 -45
- data/samples/actions/launch/calc.rb +0 -87
- data/samples/actions/launch/winparams.rb +0 -22
- data/samples/actions/loop/loopgoto.rb +0 -24
- data/samples/actions/loop/loopnamed.rb +0 -21
- data/samples/actions/named/named.rb +0 -31
- data/samples/actions/samba/smbrelay.rb +0 -26
- data/samples/actions/webbug/submitform.js +0 -26
- data/samples/actions/webbug/webbug-browser.rb +0 -68
- data/samples/actions/webbug/webbug-js.rb +0 -67
- data/samples/actions/webbug/webbug-reader.rb +0 -90
- data/samples/attachments/attach.rb +0 -40
- data/samples/attachments/attached.txt +0 -1
- data/samples/crypto/crypto.rb +0 -28
- data/samples/digsig/signed.rb +0 -46
- data/samples/exploits/cve-2008-2992-utilprintf.rb +0 -87
- data/samples/exploits/cve-2009-0927-geticon.rb +0 -65
- data/samples/exploits/exploit_customdictopen.rb +0 -55
- data/samples/exploits/getannots.rb +0 -69
- data/samples/flash/flash.rb +0 -31
- data/samples/javascript/attached.txt +0 -1
- data/samples/javascript/js.rb +0 -52
- data/templates/patterns.rb +0 -66
- data/templates/widgets.rb +0 -173
- data/templates/xdp.rb +0 -92
- data/test/ts_pdf.rb +0 -50
data/lib/origami/string.rb
CHANGED
|
@@ -1,433 +1,457 @@
|
|
|
1
1
|
=begin
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19
|
-
GNU Lesser General Public License for more details.
|
|
20
|
-
|
|
21
|
-
You should have received a copy of the GNU Lesser General Public License
|
|
22
|
-
along with Origami. If not, see <http://www.gnu.org/licenses/>.
|
|
3
|
+
This file is part of Origami, PDF manipulation framework for Ruby
|
|
4
|
+
Copyright (C) 2016 Guillaume Delugré.
|
|
5
|
+
|
|
6
|
+
Origami is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
Origami is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU Lesser General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+
along with Origami. If not, see <http://www.gnu.org/licenses/>.
|
|
23
18
|
|
|
24
19
|
=end
|
|
25
20
|
|
|
21
|
+
require 'date'
|
|
22
|
+
|
|
26
23
|
module Origami
|
|
27
24
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
25
|
+
#
|
|
26
|
+
# Module common to String objects.
|
|
27
|
+
#
|
|
28
|
+
module String
|
|
29
|
+
|
|
30
|
+
module Encoding
|
|
31
|
+
class EncodingError < Error #:nodoc:
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
module PDFDocEncoding
|
|
35
|
+
CHARMAP =
|
|
36
|
+
[
|
|
37
|
+
"\x00\x00", "\xff\xfd", "\xff\xfd", "\xff\xfd", "\xff\xfd", "\xff\xfd", "\xff\xfd", "\xff\xfd",
|
|
38
|
+
"\xff\xfd", "\x00\x09", "\x00\x0a", "\xff\xfd", "\x00\x0c", "\x00\x0d", "\xff\xfd", "\xff\xfd",
|
|
39
|
+
"\xff\xfd", "\xff\xfd", "\xff\xfd", "\xff\xfd", "\xff\xfd", "\xff\xfd", "\xff\xfd", "\xff\xfd",
|
|
40
|
+
"\x02\xd8", "\x02\xc7", "\x02\xc6", "\x02\xd9", "\x02\xdd", "\x02\xdb", "\x02\xda", "\x02\xdc",
|
|
41
|
+
"\x00\x20", "\x00\x21", "\x00\x22", "\x00\x23", "\x00\x24", "\x00\x25", "\x00\x26", "\x00\x27",
|
|
42
|
+
"\x00\x28", "\x00\x29", "\x00\x2a", "\x00\x2b", "\x00\x2c", "\x00\x2d", "\x00\x2e", "\x00\x2f",
|
|
43
|
+
"\x00\x30", "\x00\x31", "\x00\x32", "\x00\x33", "\x00\x34", "\x00\x35", "\x00\x36", "\x00\x37",
|
|
44
|
+
"\x00\x38", "\x00\x39", "\x00\x3a", "\x00\x3b", "\x00\x3c", "\x00\x3d", "\x00\x3e", "\x00\x3f",
|
|
45
|
+
"\x00\x40", "\x00\x41", "\x00\x42", "\x00\x43", "\x00\x44", "\x00\x45", "\x00\x46", "\x00\x47",
|
|
46
|
+
"\x00\x48", "\x00\x49", "\x00\x4a", "\x00\x4b", "\x00\x4c", "\x00\x4d", "\x00\x4e", "\x00\x4f",
|
|
47
|
+
"\x00\x50", "\x00\x51", "\x00\x52", "\x00\x53", "\x00\x54", "\x00\x55", "\x00\x56", "\x00\x57",
|
|
48
|
+
"\x00\x58", "\x00\x59", "\x00\x5a", "\x00\x5b", "\x00\x5c", "\x00\x5d", "\x00\x5e", "\x00\x5f",
|
|
49
|
+
"\x00\x60", "\x00\x61", "\x00\x62", "\x00\x63", "\x00\x64", "\x00\x65", "\x00\x66", "\x00\x67",
|
|
50
|
+
"\x00\x68", "\x00\x69", "\x00\x6a", "\x00\x6b", "\x00\x6c", "\x00\x6d", "\x00\x6e", "\x00\x6f",
|
|
51
|
+
"\x00\x70", "\x00\x71", "\x00\x72", "\x00\x73", "\x00\x74", "\x00\x75", "\x00\x76", "\x00\x77",
|
|
52
|
+
"\x00\x78", "\x00\x79", "\x00\x7a", "\x00\x7b", "\x00\x7c", "\x00\x7d", "\x00\x7e", "\xff\xfd",
|
|
53
|
+
"\x20\x22", "\x20\x20", "\x20\x21", "\x20\x26", "\x20\x14", "\x20\x13", "\x01\x92", "\x20\x44",
|
|
54
|
+
"\x20\x39", "\x20\x3a", "\x22\x12", "\x20\x30", "\x20\x1e", "\x20\x1c", "\x20\x1d", "\x20\x18",
|
|
55
|
+
"\x20\x19", "\x20\x1a", "\x21\x22", "\xfb\x01", "\xfb\x02", "\x01\x41", "\x01\x52", "\x01\x60",
|
|
56
|
+
"\x01\x78", "\x01\x7d", "\x01\x31", "\x01\x42", "\x01\x53", "\x01\x61", "\x01\x7e", "\xff\xfd",
|
|
57
|
+
"\x20\xac", "\x00\xa1", "\x00\xa2", "\x00\xa3", "\x00\xa4", "\x00\xa5", "\x00\xa6", "\x00\xa7",
|
|
58
|
+
"\x00\xa8", "\x00\xa9", "\x00\xaa", "\x00\xab", "\x00\xac", "\xff\xfd", "\x00\xae", "\x00\xaf",
|
|
59
|
+
"\x00\xb0", "\x00\xb1", "\x00\xb2", "\x00\xb3", "\x00\xb4", "\x00\xb5", "\x00\xb6", "\x00\xb7",
|
|
60
|
+
"\x00\xb8", "\x00\xb9", "\x00\xba", "\x00\xbb", "\x00\xbc", "\x00\xbd", "\x00\xbe", "\x00\xbf",
|
|
61
|
+
"\x00\xc0", "\x00\xc1", "\x00\xc2", "\x00\xc3", "\x00\xc4", "\x00\xc5", "\x00\xc6", "\x00\xc7",
|
|
62
|
+
"\x00\xc8", "\x00\xc9", "\x00\xca", "\x00\xcb", "\x00\xcc", "\x00\xcd", "\x00\xce", "\x00\xcf",
|
|
63
|
+
"\x00\xd0", "\x00\xd1", "\x00\xd2", "\x00\xd3", "\x00\xd4", "\x00\xd5", "\x00\xd6", "\x00\xd7",
|
|
64
|
+
"\x00\xd8", "\x00\xd9", "\x00\xda", "\x00\xdb", "\x00\xdc", "\x00\xdd", "\x00\xde", "\x00\xdf",
|
|
65
|
+
"\x00\xe0", "\x00\xe1", "\x00\xe2", "\x00\xe3", "\x00\xe4", "\x00\xe5", "\x00\xe6", "\x00\xe7",
|
|
66
|
+
"\x00\xe8", "\x00\xe9", "\x00\xea", "\x00\xeb", "\x00\xec", "\x00\xed", "\x00\xee", "\x00\xef",
|
|
67
|
+
"\x00\xf0", "\x00\xf1", "\x00\xf2", "\x00\xf3", "\x00\xf4", "\x00\xf5", "\x00\xf6", "\x00\xf7",
|
|
68
|
+
"\x00\xf8", "\x00\xf9", "\x00\xfa", "\x00\xfb", "\x00\xfc", "\x00\xfd", "\x00\xfe", "\x00\xff"
|
|
69
|
+
].map(&:b)
|
|
70
|
+
|
|
71
|
+
def PDFDocEncoding.to_utf16be(pdfdocstr)
|
|
72
|
+
utf16bestr = UTF16BE::BOM.dup
|
|
73
|
+
pdfdocstr.each_byte do |byte|
|
|
74
|
+
utf16bestr << CHARMAP[byte]
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
utf16bestr.force_encoding('binary')
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def PDFDocEncoding.to_pdfdoc(str)
|
|
81
|
+
str
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
module UTF16BE
|
|
86
|
+
BOM = "\xFE\xFF".b
|
|
87
|
+
|
|
88
|
+
def UTF16BE.to_utf16be(str)
|
|
89
|
+
str
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def UTF16BE.to_pdfdoc(str)
|
|
93
|
+
pdfdoc = []
|
|
94
|
+
i = 2
|
|
95
|
+
|
|
96
|
+
while i < str.size
|
|
97
|
+
char = PDFDocEncoding::CHARMAP.index(str[i,2])
|
|
98
|
+
raise EncodingError, "Can't convert UTF16-BE character to PDFDocEncoding" if char.nil?
|
|
99
|
+
pdfdoc << char
|
|
100
|
+
i = i + 2
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
pdfdoc.pack("C*")
|
|
104
|
+
end
|
|
105
|
+
end
|
|
83
106
|
end
|
|
84
107
|
|
|
85
|
-
|
|
86
|
-
|
|
108
|
+
module ClassMethods #:nodoc:all
|
|
109
|
+
def native_type; Origami::String end
|
|
87
110
|
end
|
|
88
111
|
|
|
89
|
-
|
|
112
|
+
def self.included(receiver) #:nodoc:
|
|
113
|
+
receiver.extend(ClassMethods)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def self.native_type; Origami::String end #:nodoc:
|
|
117
|
+
|
|
118
|
+
include Origami::Object
|
|
90
119
|
|
|
91
|
-
|
|
120
|
+
attr_accessor :encoding
|
|
92
121
|
|
|
93
|
-
|
|
122
|
+
def initialize(str) #:nodoc:
|
|
123
|
+
super(str.force_encoding('binary'))
|
|
94
124
|
|
|
95
|
-
|
|
96
|
-
str
|
|
125
|
+
detect_encoding
|
|
97
126
|
end
|
|
98
127
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
128
|
+
#
|
|
129
|
+
# Convert String object to an UTF8 encoded Ruby string.
|
|
130
|
+
#
|
|
131
|
+
def to_utf8
|
|
132
|
+
detect_encoding
|
|
102
133
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
raise EncodingError, "Can't convert UTF16-BE character to PDFDocEncoding" if char.nil?
|
|
106
|
-
pdfdoc << char
|
|
107
|
-
i = i + 2
|
|
108
|
-
end
|
|
134
|
+
utf16 = self.encoding.to_utf16be(self.value)
|
|
135
|
+
utf16.slice!(0, Encoding::UTF16BE::BOM.size)
|
|
109
136
|
|
|
110
|
-
|
|
137
|
+
utf16.encode("utf-8", "utf-16be")
|
|
111
138
|
end
|
|
112
139
|
|
|
113
|
-
|
|
140
|
+
#
|
|
141
|
+
# Convert String object to an UTF16-BE encoded binary Ruby string.
|
|
142
|
+
#
|
|
143
|
+
def to_utf16be
|
|
144
|
+
detect_encoding
|
|
145
|
+
self.encoding.to_utf16be(self.value)
|
|
146
|
+
end
|
|
114
147
|
|
|
115
|
-
|
|
148
|
+
#
|
|
149
|
+
# Convert String object to a PDFDocEncoding encoded binary Ruby string.
|
|
150
|
+
#
|
|
151
|
+
def to_pdfdoc
|
|
152
|
+
detect_encoding
|
|
153
|
+
self.encoding.to_pdfdoc(self.value)
|
|
154
|
+
end
|
|
116
155
|
|
|
117
|
-
|
|
118
|
-
|
|
156
|
+
def detect_encoding #:nodoc:
|
|
157
|
+
if self.value[0,2] == Encoding::UTF16BE::BOM
|
|
158
|
+
@encoding = Encoding::UTF16BE
|
|
159
|
+
else
|
|
160
|
+
@encoding = Encoding::PDFDocEncoding
|
|
161
|
+
end
|
|
162
|
+
end
|
|
119
163
|
end
|
|
120
164
|
|
|
121
|
-
|
|
122
|
-
receiver.extend(ClassMethods)
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
def self.native_type; Origami::String end #:nodoc:
|
|
126
|
-
|
|
127
|
-
include Origami::Object
|
|
128
|
-
|
|
129
|
-
attr_accessor :encoding
|
|
130
|
-
|
|
131
|
-
def initialize(str) #:nodoc:
|
|
132
|
-
infer_encoding
|
|
133
|
-
super(str)
|
|
165
|
+
class InvalidHexaStringObjectError < InvalidObjectError #:nodoc:
|
|
134
166
|
end
|
|
135
167
|
|
|
136
168
|
#
|
|
137
|
-
#
|
|
169
|
+
# Class representing an hexadecimal-writen String Object.
|
|
138
170
|
#
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if RUBY_VERSION < '1.9'
|
|
143
|
-
require 'iconv'
|
|
144
|
-
i = Iconv.new("UTF-8", "UTF-16")
|
|
145
|
-
utf8str = i.iconv(self.encoding.to_utf16be(self.value))
|
|
146
|
-
i.close
|
|
147
|
-
else
|
|
148
|
-
utf8str = self.encoding.to_utf16be(self.value).encode("utf-8", "utf-16")
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
utf8str
|
|
152
|
-
end
|
|
171
|
+
class HexaString < ::String
|
|
172
|
+
include String
|
|
153
173
|
|
|
154
|
-
|
|
155
|
-
# Convert String object to an UTF16-BE encoded Ruby string.
|
|
156
|
-
#
|
|
157
|
-
def to_utf16be
|
|
158
|
-
infer_encoding
|
|
159
|
-
self.encoding.to_utf16be(self.value)
|
|
160
|
-
end
|
|
174
|
+
TOKENS = %w{ < > } #:nodoc:
|
|
161
175
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
#
|
|
165
|
-
def to_pdfdoc
|
|
166
|
-
infer_encoding
|
|
167
|
-
self.encoding.to_pdfdoc(self.value)
|
|
168
|
-
end
|
|
176
|
+
@@regexp_open = Regexp.new(WHITESPACES + TOKENS.first)
|
|
177
|
+
@@regexp_close = Regexp.new(TOKENS.last)
|
|
169
178
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
class InvalidHexaStringObjectError < InvalidObjectError #:nodoc:
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
#
|
|
184
|
-
# Class representing an hexadecimal-writen String Object.
|
|
185
|
-
#
|
|
186
|
-
class HexaString < ::String
|
|
187
|
-
include String
|
|
188
|
-
|
|
189
|
-
TOKENS = %w{ < > } #:nodoc:
|
|
190
|
-
|
|
191
|
-
@@regexp_open = Regexp.new(WHITESPACES + TOKENS.first)
|
|
192
|
-
@@regexp_close = Regexp.new(TOKENS.last)
|
|
179
|
+
#
|
|
180
|
+
# Creates a new PDF hexadecimal String.
|
|
181
|
+
# _str_:: The string value.
|
|
182
|
+
#
|
|
183
|
+
def initialize(str = "")
|
|
184
|
+
unless str.is_a?(::String)
|
|
185
|
+
raise TypeError, "Expected type String, received #{str.class}."
|
|
186
|
+
end
|
|
193
187
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
# _str_:: The string value.
|
|
197
|
-
#
|
|
198
|
-
def initialize(str = "")
|
|
199
|
-
|
|
200
|
-
unless str.is_a?(::String)
|
|
201
|
-
raise TypeError, "Expected type String, received #{str.class}."
|
|
202
|
-
end
|
|
203
|
-
|
|
204
|
-
super(str)
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
def self.parse(stream, parser = nil) #:nodoc:
|
|
208
|
-
|
|
209
|
-
offset = stream.pos
|
|
210
|
-
|
|
211
|
-
if stream.skip(@@regexp_open).nil?
|
|
212
|
-
raise InvalidHexaStringObjectError, "Hexadecimal string shall start with a '#{TOKENS.first}' token"
|
|
213
|
-
end
|
|
214
|
-
|
|
215
|
-
hexa = stream.scan_until(@@regexp_close)
|
|
216
|
-
if hexa.nil?
|
|
217
|
-
raise InvalidHexaStringObjectError, "Hexadecimal string shall end with a '#{TOKENS.last}' token"
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
decoded = Filter::ASCIIHex.decode(hexa.chomp!(TOKENS.last))
|
|
221
|
-
|
|
222
|
-
hexastr = HexaString.new(decoded)
|
|
223
|
-
hexastr.file_offset = offset
|
|
224
|
-
|
|
225
|
-
hexastr
|
|
226
|
-
end
|
|
227
|
-
|
|
228
|
-
def to_s #:nodoc:
|
|
229
|
-
super(TOKENS.first + Filter::ASCIIHex.encode(to_str) + TOKENS.last)
|
|
230
|
-
end
|
|
188
|
+
super(str)
|
|
189
|
+
end
|
|
231
190
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
191
|
+
def self.parse(stream, parser = nil) #:nodoc:
|
|
192
|
+
offset = stream.pos
|
|
193
|
+
|
|
194
|
+
if stream.skip(@@regexp_open).nil?
|
|
195
|
+
raise InvalidHexaStringObjectError, "Hexadecimal string shall start with a '#{TOKENS.first}' token"
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
hexa = stream.scan_until(@@regexp_close)
|
|
199
|
+
if hexa.nil?
|
|
200
|
+
raise InvalidHexaStringObjectError, "Hexadecimal string shall end with a '#{TOKENS.last}' token"
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
decoded = Filter::ASCIIHex.decode(hexa.chomp!(TOKENS.last))
|
|
204
|
+
|
|
205
|
+
hexastr = HexaString.new(decoded)
|
|
206
|
+
hexastr.file_offset = offset
|
|
238
207
|
|
|
239
|
-
|
|
240
|
-
|
|
208
|
+
hexastr
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def to_s #:nodoc:
|
|
212
|
+
super(TOKENS.first + Filter::ASCIIHex.encode(to_str) + TOKENS.last)
|
|
213
|
+
end
|
|
241
214
|
|
|
242
|
-
|
|
215
|
+
#
|
|
216
|
+
# Converts self to a literal String.
|
|
217
|
+
#
|
|
218
|
+
def to_literal
|
|
219
|
+
LiteralString.new(self.value)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def value
|
|
223
|
+
self.decrypt! if self.is_a?(Encryption::EncryptedString) and not @decrypted
|
|
224
|
+
|
|
225
|
+
to_str
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
class InvalidLiteralStringObjectError < InvalidObjectError #:nodoc:
|
|
243
230
|
end
|
|
244
231
|
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
class InvalidByteStringObjectError < InvalidObjectError #:nodoc:
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
#
|
|
251
|
-
# Class representing an ASCII String Object.
|
|
252
|
-
#
|
|
253
|
-
class ByteString < ::String
|
|
254
|
-
|
|
255
|
-
include String
|
|
256
|
-
|
|
257
|
-
TOKENS = %w{ ( ) } #:nodoc:
|
|
258
|
-
|
|
259
|
-
@@regexp_open = Regexp.new(WHITESPACES + Regexp.escape(TOKENS.first))
|
|
260
|
-
@@regexp_close = Regexp.new(Regexp.escape(TOKENS.last))
|
|
261
|
-
|
|
262
232
|
#
|
|
263
|
-
#
|
|
264
|
-
# _str_:: The string value.
|
|
233
|
+
# Class representing a literal String Object.
|
|
265
234
|
#
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
unless str.is_a?(::String)
|
|
269
|
-
raise TypeError, "Expected type String, received #{str.class}."
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
super(str)
|
|
273
|
-
end
|
|
235
|
+
class LiteralString < ::String
|
|
236
|
+
include String
|
|
274
237
|
|
|
275
|
-
|
|
238
|
+
TOKENS = %w{ ( ) } #:nodoc:
|
|
276
239
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if not stream.skip(@@regexp_open)
|
|
280
|
-
raise InvalidByteStringObjectError, "No literal string start token found"
|
|
281
|
-
end
|
|
282
|
-
|
|
283
|
-
result = ""
|
|
284
|
-
depth = 0
|
|
285
|
-
while depth != 0 or stream.peek(1) != TOKENS.last do
|
|
240
|
+
@@regexp_open = Regexp.new(WHITESPACES + Regexp.escape(TOKENS.first))
|
|
241
|
+
@@regexp_close = Regexp.new(Regexp.escape(TOKENS.last))
|
|
286
242
|
|
|
287
|
-
|
|
288
|
-
|
|
243
|
+
#
|
|
244
|
+
# Creates a new PDF String.
|
|
245
|
+
# _str_:: The string value.
|
|
246
|
+
#
|
|
247
|
+
def initialize(str = "")
|
|
248
|
+
unless str.is_a?(::String)
|
|
249
|
+
raise TypeError, "Expected type String, received #{str.class}."
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
super(str)
|
|
289
253
|
end
|
|
290
254
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
stream.
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
255
|
+
def self.parse(stream, parser = nil) #:nodoc:
|
|
256
|
+
offset = stream.pos
|
|
257
|
+
|
|
258
|
+
unless stream.skip(@@regexp_open)
|
|
259
|
+
raise InvalidLiteralStringObjectError, "No literal string start token found"
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
result = ""
|
|
263
|
+
depth = 0
|
|
264
|
+
while depth != 0 or stream.peek(1) != TOKENS.last do
|
|
265
|
+
raise InvalidLiteralStringObjectError, "Non-terminated string" if stream.eos?
|
|
266
|
+
|
|
267
|
+
c = stream.get_byte
|
|
268
|
+
case c
|
|
269
|
+
when "\\"
|
|
270
|
+
if stream.match?(/\d{1,3}/)
|
|
271
|
+
oct = stream.peek(3).oct.chr
|
|
272
|
+
stream.pos += 3
|
|
273
|
+
result << oct
|
|
274
|
+
elsif stream.match?(/((\r?\n)|(\r\n?))/)
|
|
275
|
+
stream.skip(/((\r?\n)|(\r\n?))/)
|
|
276
|
+
next
|
|
277
|
+
else
|
|
278
|
+
flag = stream.get_byte
|
|
279
|
+
case flag
|
|
280
|
+
when "n" then result << "\n"
|
|
281
|
+
when "r" then result << "\r"
|
|
282
|
+
when "t" then result << "\t"
|
|
283
|
+
when "b" then result << "\b"
|
|
284
|
+
when "f" then result << "\f"
|
|
285
|
+
when "(" then result << "("
|
|
286
|
+
when ")" then result << ")"
|
|
287
|
+
when "\\" then result << "\\"
|
|
288
|
+
when "\r"
|
|
289
|
+
stream.pos += 1 if stream.peek(1) == "\n"
|
|
290
|
+
when "\n"
|
|
291
|
+
else
|
|
292
|
+
result << flag
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
when "(" then
|
|
297
|
+
depth = depth + 1
|
|
298
|
+
result << c
|
|
299
|
+
when ")" then
|
|
300
|
+
depth = depth - 1
|
|
301
|
+
result << c
|
|
302
|
+
else
|
|
303
|
+
result << c
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
unless stream.skip(@@regexp_close)
|
|
308
|
+
raise InvalidLiteralStringObjectError, "Byte string shall be terminated with '#{TOKENS.last}'"
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# Try to cast as a Date object if possible.
|
|
312
|
+
if result[0, 2] == 'D:'
|
|
313
|
+
begin
|
|
314
|
+
date = Date.parse(result)
|
|
315
|
+
date.file_offset = offset
|
|
316
|
+
return date
|
|
317
|
+
rescue InvalidDateError
|
|
318
|
+
end
|
|
319
319
|
end
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
when ")" then
|
|
326
|
-
depth = depth - 1
|
|
327
|
-
result << c
|
|
328
|
-
else
|
|
329
|
-
result << c
|
|
320
|
+
|
|
321
|
+
bytestr = self.new(result)
|
|
322
|
+
bytestr.file_offset = offset
|
|
323
|
+
|
|
324
|
+
bytestr
|
|
330
325
|
end
|
|
331
326
|
|
|
332
|
-
|
|
327
|
+
def expand #:nodoc:
|
|
328
|
+
extended = self.gsub("\\", "\\\\\\\\")
|
|
329
|
+
extended.gsub!(/\)/, "\\)")
|
|
330
|
+
extended.gsub!("\n", "\\n")
|
|
331
|
+
extended.gsub!("\r", "\\r")
|
|
332
|
+
extended.gsub!(/\(/, "\\(")
|
|
333
333
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
end
|
|
334
|
+
extended
|
|
335
|
+
end
|
|
337
336
|
|
|
338
|
-
|
|
339
|
-
|
|
337
|
+
def to_s #:nodoc:
|
|
338
|
+
super(TOKENS.first + self.expand + TOKENS.last)
|
|
339
|
+
end
|
|
340
340
|
|
|
341
|
-
|
|
342
|
-
|
|
341
|
+
#
|
|
342
|
+
# Converts self to HexaString
|
|
343
|
+
#
|
|
344
|
+
def to_hex
|
|
345
|
+
HexaString.new(self.value)
|
|
346
|
+
end
|
|
343
347
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
extended.gsub!("\r", "\\r")
|
|
350
|
-
extended.gsub!(/\(/, "\\(")
|
|
351
|
-
|
|
352
|
-
extended
|
|
353
|
-
end
|
|
354
|
-
|
|
355
|
-
def to_s #:nodoc:
|
|
356
|
-
super(TOKENS.first + self.expand + TOKENS.last)
|
|
357
|
-
end
|
|
348
|
+
#
|
|
349
|
+
# Returns a standard String representation.
|
|
350
|
+
#
|
|
351
|
+
def value
|
|
352
|
+
self.decrypt! if self.is_a?(Encryption::EncryptedString) and not @decrypted
|
|
358
353
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
#
|
|
362
|
-
def to_hex
|
|
363
|
-
HexaString.new(self.value)
|
|
354
|
+
to_str
|
|
355
|
+
end
|
|
364
356
|
end
|
|
365
|
-
|
|
366
|
-
def value
|
|
367
|
-
self.decrypt! if self.is_a?(Encryption::EncryptedString) and not @decrypted
|
|
368
357
|
|
|
369
|
-
|
|
370
|
-
end
|
|
371
|
-
end
|
|
372
|
-
|
|
373
|
-
#
|
|
374
|
-
# Class representing a Date string.
|
|
375
|
-
# _Not used_
|
|
376
|
-
# _Not tested_
|
|
377
|
-
#
|
|
378
|
-
class Date < ByteString #:nodoc:
|
|
379
|
-
|
|
380
|
-
REGEXP_TOKEN = "(D:)?(\\d{4})(\\d{2})?(\\d{2})?(\\d{2})?(\\d{2})?(\\d{2})?(?:([\\+-Z])(?:(\\d{2})')?(?:(\\d{2})')?)?"
|
|
381
|
-
|
|
382
|
-
def initialize(year, month = nil, day = nil, hour = nil, minute = nil, second = nil, ut_sign = nil, ut_hours = nil, ut_min = nil)
|
|
383
|
-
|
|
384
|
-
year_str = '%04d' % year
|
|
385
|
-
month_str = month.nil? ? '01' : '%02d' % month
|
|
386
|
-
day_str = day.nil? ? '01' : '%02d' % day
|
|
387
|
-
hour_str = '%02d' % hour
|
|
388
|
-
minute_str = '%02d' % minute
|
|
389
|
-
second_str = '%02d' % second
|
|
390
|
-
|
|
391
|
-
date_str = "D:#{year_str}#{month_str}#{day_str}#{hour_str}#{minute_str}#{second_str}"
|
|
392
|
-
date_str << "#{ut_sign}#{'%02d' % ut_hours}'#{'%02d' % ut_min}" unless ut_sign.nil?
|
|
393
|
-
|
|
394
|
-
super(date_str)
|
|
358
|
+
class InvalidDateError < Error #:nodoc:
|
|
395
359
|
end
|
|
396
|
-
|
|
397
|
-
def self.parse(stream, parser = nil) #:nodoc:
|
|
398
|
-
|
|
399
|
-
dateReg = Regexp.new(REGEXP_TOKEN)
|
|
400
|
-
|
|
401
|
-
raise InvalidDate if stream.scan(dateReg).nil?
|
|
402
|
-
|
|
403
|
-
year = stream[2].to_i
|
|
404
|
-
month = stream[3] and stream[3].to_i
|
|
405
|
-
day = stream[4] and stream[4].to_i
|
|
406
|
-
hour = stream[5] and stream[5].to_i
|
|
407
|
-
min = stream[6] and stream[6].to_i
|
|
408
|
-
sec = stream[7] and stream[7].to_i
|
|
409
|
-
ut_sign = stream[8]
|
|
410
|
-
ut_hours = stream[9] and stream[9].to_i
|
|
411
|
-
ut_min = stream[10] and stream[10].to_i
|
|
412
|
-
|
|
413
|
-
Origami::Date.new(year, month, day, hour, min, sec, ut_sign, ut_hours, ut_min)
|
|
414
|
-
end
|
|
415
|
-
|
|
360
|
+
|
|
416
361
|
#
|
|
417
|
-
#
|
|
362
|
+
# Class representing a Date string.
|
|
418
363
|
#
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
364
|
+
class Date < LiteralString #:nodoc:
|
|
365
|
+
|
|
366
|
+
REGEXP_TOKEN =
|
|
367
|
+
/D: # Date header
|
|
368
|
+
(?<year>\d{4}) # Year
|
|
369
|
+
(?<month>\d{2})? # Month
|
|
370
|
+
(?<day>\d{2})? # Day
|
|
371
|
+
(?<hour>\d{2})? # Hour
|
|
372
|
+
(?<min>\d{2})? # Minute
|
|
373
|
+
(?<sec>\d{2})? # Second
|
|
374
|
+
(?:
|
|
375
|
+
(?<ut>[\+\-Z]) # UT relationship
|
|
376
|
+
(?<ut_hour_off>\d{2}) # UT hour offset
|
|
377
|
+
('(?<ut_min_off>\d{2}))? # UT minute offset
|
|
378
|
+
)?
|
|
379
|
+
/x
|
|
380
|
+
|
|
381
|
+
attr_reader :year, :month, :day, :hour, :min, :sec, :utc_offset
|
|
382
|
+
|
|
383
|
+
def initialize(year:, month: 1, day: 1, hour: 0, min: 0, sec: 0, utc_offset: 0)
|
|
384
|
+
raise InvalidDateError, "Invalid year #{year}" unless (0..9999) === year
|
|
385
|
+
raise InvalidDateError, "Invalid month #{month}" unless (1..12) === month
|
|
386
|
+
raise InvalidDateError, "Invalid day #{day}" unless (1..31) === day
|
|
387
|
+
raise InvalidDateError, "Invalid hour #{hour}" unless (0..23) === hour
|
|
388
|
+
raise InvalidDateError, "Invalid minute #{min}" unless (0..59) === min
|
|
389
|
+
raise InvalidDateError, "Invalid second #{sec}" unless (0..59) === sec
|
|
390
|
+
|
|
391
|
+
@year, @month, @day, @hour, @min, @sec = year, month, day, hour, min, sec
|
|
392
|
+
@utc_offset = utc_offset
|
|
393
|
+
|
|
394
|
+
date = "D:%04d%02d%02d%02d%02d%02d" % [year, month, day, hour, min, sec ]
|
|
395
|
+
|
|
396
|
+
if utc_offset == 0
|
|
397
|
+
date << "Z00'00"
|
|
398
|
+
else
|
|
399
|
+
date << (if utc_offset < 0 then '-' else '+' end)
|
|
400
|
+
off_hours, off_secs = utc_offset.abs.divmod(3600)
|
|
401
|
+
off_mins = off_secs / 60
|
|
402
|
+
date << "%02d'%02d" % [ off_hours, off_mins ]
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
super(date)
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
def to_datetime
|
|
409
|
+
::DateTime.new(@year, @month, @day, @hour, @min, @sec, (@utc_offset / 3600).to_s)
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
def self.parse(str) #:nodoc:
|
|
413
|
+
raise InvalidDateError, "Not a valid Date string" unless str =~ REGEXP_TOKEN
|
|
414
|
+
|
|
415
|
+
date =
|
|
416
|
+
{
|
|
417
|
+
year: $~['year'].to_i
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
date[:month] = $~['month'].to_i if $~['month']
|
|
421
|
+
date[:day] = $~['day'].to_i if $~['day']
|
|
422
|
+
date[:hour] = $~['hour'].to_i if $~['hour']
|
|
423
|
+
date[:min] = $~['min'].to_i if $~['min']
|
|
424
|
+
date[:sec] = $~['sec'].to_i if $~['sec']
|
|
425
|
+
|
|
426
|
+
if %w[+ -].include?($~['ut'])
|
|
427
|
+
utc_offset = $~['ut_hour_off'].to_i * 3600 + $~['ut_min_off'].to_i * 60
|
|
428
|
+
utc_offset = -utc_offset if $~['ut'] == '-'
|
|
429
|
+
|
|
430
|
+
date[:utc_offset] = utc_offset
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
Origami::Date.new(date)
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
#
|
|
437
|
+
# Returns current Date String in UTC time.
|
|
438
|
+
#
|
|
439
|
+
def self.now
|
|
440
|
+
now = Time.now.utc
|
|
441
|
+
|
|
442
|
+
date =
|
|
443
|
+
{
|
|
444
|
+
year: now.strftime("%Y").to_i,
|
|
445
|
+
month: now.strftime("%m").to_i,
|
|
446
|
+
day: now.strftime("%d").to_i,
|
|
447
|
+
hour: now.strftime("%H").to_i,
|
|
448
|
+
min: now.strftime("%M").to_i,
|
|
449
|
+
sec: now.strftime("%S").to_i,
|
|
450
|
+
utc_offset: now.utc_offset
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
Origami::Date.new(date)
|
|
454
|
+
end
|
|
429
455
|
end
|
|
430
|
-
|
|
431
|
-
end
|
|
432
456
|
|
|
433
457
|
end
|