origami 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/COPYING.LESSER +165 -0
  2. data/README +77 -0
  3. data/VERSION +1 -0
  4. data/bin/config/pdfcop.conf.yml +237 -0
  5. data/bin/gui/about.rb +46 -0
  6. data/bin/gui/config.rb +132 -0
  7. data/bin/gui/file.rb +385 -0
  8. data/bin/gui/hexdump.rb +74 -0
  9. data/bin/gui/hexview.rb +91 -0
  10. data/bin/gui/imgview.rb +72 -0
  11. data/bin/gui/menu.rb +392 -0
  12. data/bin/gui/properties.rb +132 -0
  13. data/bin/gui/signing.rb +635 -0
  14. data/bin/gui/textview.rb +107 -0
  15. data/bin/gui/treeview.rb +409 -0
  16. data/bin/gui/walker.rb +282 -0
  17. data/bin/gui/xrefs.rb +79 -0
  18. data/bin/pdf2graph +121 -0
  19. data/bin/pdf2ruby +353 -0
  20. data/bin/pdfcocoon +104 -0
  21. data/bin/pdfcop +455 -0
  22. data/bin/pdfdecompress +104 -0
  23. data/bin/pdfdecrypt +95 -0
  24. data/bin/pdfencrypt +112 -0
  25. data/bin/pdfextract +221 -0
  26. data/bin/pdfmetadata +123 -0
  27. data/bin/pdfsh +13 -0
  28. data/bin/pdfwalker +7 -0
  29. data/bin/shell/.irbrc +104 -0
  30. data/bin/shell/console.rb +136 -0
  31. data/bin/shell/hexdump.rb +83 -0
  32. data/origami.rb +36 -0
  33. data/origami/3d.rb +239 -0
  34. data/origami/acroform.rb +321 -0
  35. data/origami/actions.rb +299 -0
  36. data/origami/adobe/fdf.rb +259 -0
  37. data/origami/adobe/ppklite.rb +489 -0
  38. data/origami/annotations.rb +775 -0
  39. data/origami/array.rb +187 -0
  40. data/origami/boolean.rb +101 -0
  41. data/origami/catalog.rb +486 -0
  42. data/origami/destinations.rb +213 -0
  43. data/origami/dictionary.rb +188 -0
  44. data/origami/docmdp.rb +96 -0
  45. data/origami/encryption.rb +1293 -0
  46. data/origami/export.rb +283 -0
  47. data/origami/file.rb +222 -0
  48. data/origami/filters.rb +250 -0
  49. data/origami/filters/ascii.rb +189 -0
  50. data/origami/filters/ccitt.rb +515 -0
  51. data/origami/filters/crypt.rb +47 -0
  52. data/origami/filters/dct.rb +61 -0
  53. data/origami/filters/flate.rb +112 -0
  54. data/origami/filters/jbig2.rb +63 -0
  55. data/origami/filters/jpx.rb +53 -0
  56. data/origami/filters/lzw.rb +195 -0
  57. data/origami/filters/predictors.rb +276 -0
  58. data/origami/filters/runlength.rb +117 -0
  59. data/origami/font.rb +209 -0
  60. data/origami/functions.rb +93 -0
  61. data/origami/graphics.rb +33 -0
  62. data/origami/graphics/colors.rb +191 -0
  63. data/origami/graphics/instruction.rb +126 -0
  64. data/origami/graphics/path.rb +154 -0
  65. data/origami/graphics/patterns.rb +180 -0
  66. data/origami/graphics/state.rb +164 -0
  67. data/origami/graphics/text.rb +224 -0
  68. data/origami/graphics/xobject.rb +493 -0
  69. data/origami/header.rb +90 -0
  70. data/origami/linearization.rb +318 -0
  71. data/origami/metadata.rb +114 -0
  72. data/origami/name.rb +170 -0
  73. data/origami/null.rb +75 -0
  74. data/origami/numeric.rb +188 -0
  75. data/origami/obfuscation.rb +233 -0
  76. data/origami/object.rb +527 -0
  77. data/origami/outline.rb +59 -0
  78. data/origami/page.rb +559 -0
  79. data/origami/parser.rb +268 -0
  80. data/origami/parsers/fdf.rb +45 -0
  81. data/origami/parsers/pdf.rb +27 -0
  82. data/origami/parsers/pdf/linear.rb +113 -0
  83. data/origami/parsers/ppklite.rb +86 -0
  84. data/origami/pdf.rb +1144 -0
  85. data/origami/reference.rb +113 -0
  86. data/origami/signature.rb +474 -0
  87. data/origami/stream.rb +575 -0
  88. data/origami/string.rb +416 -0
  89. data/origami/trailer.rb +173 -0
  90. data/origami/webcapture.rb +87 -0
  91. data/origami/xfa.rb +3027 -0
  92. data/origami/xreftable.rb +447 -0
  93. data/templates/patterns.rb +66 -0
  94. data/templates/widgets.rb +173 -0
  95. data/templates/xdp.rb +92 -0
  96. data/tests/dataset/test.dummycrt +28 -0
  97. data/tests/dataset/test.dummykey +27 -0
  98. data/tests/tc_actions.rb +32 -0
  99. data/tests/tc_annotations.rb +85 -0
  100. data/tests/tc_pages.rb +37 -0
  101. data/tests/tc_pdfattach.rb +24 -0
  102. data/tests/tc_pdfencrypt.rb +110 -0
  103. data/tests/tc_pdfnew.rb +32 -0
  104. data/tests/tc_pdfparse.rb +98 -0
  105. data/tests/tc_pdfsig.rb +37 -0
  106. data/tests/tc_streams.rb +129 -0
  107. data/tests/ts_pdf.rb +45 -0
  108. metadata +193 -0
@@ -0,0 +1,213 @@
1
+ =begin
2
+
3
+ = File
4
+ destinations.rb
5
+
6
+ = Info
7
+ This file is part of Origami, PDF manipulation framework for Ruby
8
+ Copyright (C) 2010 Guillaume Delugr� <guillaume@security-labs.org>
9
+ All right reserved.
10
+
11
+ Origami is free software: you can redistribute it and/or modify
12
+ it under the terms of the GNU Lesser General Public License as published by
13
+ the Free Software Foundation, either version 3 of the License, or
14
+ (at your option) any later version.
15
+
16
+ Origami is distributed in the hope that it will be useful,
17
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
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/>.
23
+
24
+ =end
25
+
26
+ module Origami
27
+
28
+ class PDF
29
+
30
+ #
31
+ # Lookup destination in the destination name directory.
32
+ #
33
+ def get_destination_by_name(name)
34
+ resolve_name Names::Root::DESTS, name
35
+ end
36
+
37
+ #
38
+ # Calls block for each named destination.
39
+ #
40
+ def each_named_dest(&b)
41
+ each_name(Names::Root::DESTS, &b)
42
+ end
43
+ end
44
+
45
+ #
46
+ # A destination represents a specified location into the document.
47
+ #
48
+ module Destination
49
+
50
+ attr_reader :top, :left, :right, :bottom, :zoom
51
+
52
+ #
53
+ # Class representing a Destination zooming on a part of a document.
54
+ #
55
+ class Zoom < Origami::Array
56
+
57
+ include Destination
58
+
59
+ #
60
+ # Creates a new zoom Destination.
61
+ # _pageref_:: A Reference to a Page.
62
+ # _left_, _top_:: Coords in the Page.
63
+ # _zoom_:: Zoom factor.
64
+ #
65
+ def initialize(pageref, left = 0, top = 0, zoom = 0)
66
+
67
+ @left, @top, @zoom = left, top, zoom
68
+
69
+ super([pageref, :XYZ, left, top, zoom])
70
+
71
+ end
72
+
73
+ end
74
+
75
+ #
76
+ # Class representing a Destination showing a Page globally.
77
+ #
78
+ class GlobalFit < Origami::Array
79
+
80
+ include Destination
81
+
82
+ #
83
+ # Creates a new global fit Destination.
84
+ # _pageref_:: A Reference to a Page.
85
+ #
86
+ def initialize(pageref)
87
+ super([pageref, :Fit])
88
+ end
89
+
90
+ end
91
+
92
+ #
93
+ # Class representing a Destination fitting a Page horizontally.
94
+ #
95
+ class HorizontalFit < Origami::Array
96
+
97
+ include Destination
98
+
99
+ #
100
+ # Creates a new horizontal fit destination.
101
+ # _pageref_:: A Reference to a Page.
102
+ # _top_:: The vertical coord in the Page.
103
+ #
104
+ def initialize(pageref, top = 0)
105
+
106
+ @top = top
107
+ super([pageref, :FitH, top])
108
+
109
+ end
110
+
111
+ end
112
+
113
+ #
114
+ # Class representing a Destination fitting a Page vertically.
115
+ # _pageref_:: A Reference to a Page.
116
+ # _left_:: The horizontal coord in the Page.
117
+ #
118
+ class VerticalFit < Origami::Array
119
+
120
+ include Destination
121
+
122
+ def initialize(pageref, left = 0)
123
+
124
+ @left = left
125
+ super([pageref, :FitV, left])
126
+
127
+ end
128
+
129
+ end
130
+
131
+ #
132
+ # Class representing a Destination fitting the view on a rectangle in a Page.
133
+ #
134
+ class RectangleFit < Origami::Array
135
+
136
+ include Destination
137
+
138
+ #
139
+ # Creates a new rectangle fit Destination.
140
+ # _pageref_:: A Reference to a Page.
141
+ # _left_, _bottom_, _right_, _top_:: The rectangle to fit in.
142
+ #
143
+ def initialize(pageref, left = 0, bottom = 0, right = 0, top = 0)
144
+
145
+ @left, @bottom, @right, @top = left, bottom, right, top
146
+ super([pageref, :FitR, left, bottom, right, top])
147
+
148
+ end
149
+
150
+ end
151
+
152
+ #
153
+ # Class representing a Destination fitting the bounding box of a Page.
154
+ #
155
+ class GlobalBoundingBoxFit < Origami::Array
156
+
157
+ include Destination
158
+
159
+ #
160
+ # Creates a new bounding box fit Destination.
161
+ # _pageref_:: A Reference to a Page.
162
+ #
163
+ def initialize(pageref)
164
+ super([pageref, :FitB])
165
+ end
166
+
167
+ end
168
+
169
+ #
170
+ # Class representing a Destination fitting horizontally the bouding box a Page.
171
+ #
172
+ class HorizontalBoudingBoxFit < Origami::Array
173
+
174
+ include Destination
175
+
176
+ #
177
+ # Creates a new horizontal bounding box fit Destination.
178
+ # _pageref_:: A Reference to a Page.
179
+ # _top_:: The vertical coord.
180
+ #
181
+ def initialize(pageref, top = 0)
182
+
183
+ @top = top
184
+ super([pageref, :FitBH, top])
185
+
186
+ end
187
+
188
+ end
189
+
190
+ #
191
+ # Class representing a Destination fitting vertically the bounding box of a Page.
192
+ #
193
+ class VerticalBoundingBoxFit < Origami::Array
194
+
195
+ include Destination
196
+
197
+ #
198
+ # Creates a new vertical bounding box fit Destination.
199
+ # _pageref_:: A Reference to a Page.
200
+ # _left_:: The horizontal coord.
201
+ #
202
+ def initialize(pageref, left = 0)
203
+
204
+ @left = left
205
+ super([pageref, :FitBV, left])
206
+
207
+ end
208
+
209
+ end
210
+
211
+ end
212
+
213
+ end
@@ -0,0 +1,188 @@
1
+ =begin
2
+
3
+ = File
4
+ dictionary.rb
5
+
6
+ = Info
7
+ This file is part of Origami, PDF manipulation framework for Ruby
8
+ Copyright (C) 2010 Guillaume DelugrÈ <guillaume@security-labs.org>
9
+ All right reserved.
10
+
11
+ Origami is free software: you can redistribute it and/or modify
12
+ it under the terms of the GNU Lesser General Public License as published by
13
+ the Free Software Foundation, either version 3 of the License, or
14
+ (at your option) any later version.
15
+
16
+ Origami is distributed in the hope that it will be useful,
17
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
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/>.
23
+
24
+ =end
25
+
26
+ module Origami
27
+
28
+ class InvalidDictionaryObjectError < InvalidObjectError #:nodoc:
29
+ end
30
+
31
+ #
32
+ # Class representing a Dictionary Object.
33
+ # Dictionaries are containers associating a Name to an embedded Object.
34
+ #
35
+ class Dictionary < Hash
36
+ include Origami::Object
37
+
38
+ TOKENS = %w{ << >> } #:nodoc:
39
+ @@regexp_open = Regexp.new(WHITESPACES + Regexp.escape(TOKENS.first) + WHITESPACES)
40
+ @@regexp_close = Regexp.new(WHITESPACES + Regexp.escape(TOKENS.last) + WHITESPACES)
41
+
42
+ attr_reader :strings_cache, :names_cache, :xref_cache
43
+
44
+ #
45
+ # Creates a new Dictionary.
46
+ # _hash_:: The hash representing the new Dictionary.
47
+ #
48
+ def initialize(hash = {})
49
+ raise TypeError, "Expected type Hash, received #{hash.class}." unless hash.is_a?(Hash)
50
+ super()
51
+
52
+ @strings_cache = []
53
+ @names_cache = []
54
+ @xref_cache = {}
55
+
56
+ hash.each_pair do |k,v|
57
+ @names_cache.push(k.to_o)
58
+ case val = v.to_o
59
+ when String then @strings_cache.push(val)
60
+ when Name then @names_cache.push(val)
61
+ when Reference then
62
+ (@xref_cache[val] ||= []).push(self)
63
+ when Dictionary,Array then
64
+ @strings_cache.concat(val.strings_cache)
65
+ @names_cache.concat(val.names_cache)
66
+ @xref_cache.update(val.xref_cache) do |ref, cache1, cache2|
67
+ cache1.concat(cache2)
68
+ end
69
+
70
+ val.strings_cache.clear
71
+ val.names_cache.clear
72
+ val.xref_cache.clear
73
+ end
74
+
75
+ self[k.to_o] = val unless k.nil?
76
+ end
77
+ end
78
+
79
+ def self.parse(stream) #:nodoc:
80
+
81
+ offset = stream.pos
82
+
83
+ if stream.skip(@@regexp_open).nil?
84
+ raise InvalidDictionaryObjectError, "No token '#{TOKENS.first}' found"
85
+ end
86
+
87
+ pairs = {}
88
+ while stream.skip(@@regexp_close).nil? do
89
+ key = Name.parse(stream)
90
+
91
+ type = Object.typeof(stream)
92
+ if type.nil?
93
+ raise InvalidDictionaryObjectError, "Invalid object for field #{key.to_s}"
94
+ end
95
+ value = type.parse(stream)
96
+
97
+ pairs[key] = value
98
+ end
99
+
100
+ dict =
101
+ if Origami::OPTIONS[:enable_type_guessing]
102
+ type = pairs[Name.new(:Type)]
103
+ if type.is_a?(Name) and @@dict_special_types.include?(type.value)
104
+ @@dict_special_types[type.value].new(pairs)
105
+ else
106
+ Dictionary.new(pairs)
107
+ end
108
+
109
+ else
110
+ Dictionary.new(pairs)
111
+ end
112
+
113
+ dict.file_offset = offset
114
+
115
+ dict
116
+ end
117
+
118
+ alias to_h to_hash
119
+
120
+ def to_s(indent = 1) #:nodoc:
121
+ if indent > 0
122
+ content = TOKENS.first + EOL
123
+ self.each_pair do |key,value|
124
+ content << "\t" * indent + key.to_s + " " + (value.is_a?(Dictionary) ? value.to_s(indent + 1) : value.to_s) + EOL
125
+ end
126
+
127
+ content << "\t" * (indent - 1) + TOKENS.last
128
+ else
129
+ content = TOKENS.first.dup
130
+ self.each_pair do |key,value|
131
+ content << "#{key.to_s} #{value.is_a?(Dictionary) ? value.to_s(0) : value.to_s}"
132
+ end
133
+ content << TOKENS.last
134
+ end
135
+
136
+ super(content)
137
+ end
138
+
139
+ def map!(&b)
140
+ self.each_pair do |k,v|
141
+ self[k] = b.call(v)
142
+ end
143
+ end
144
+
145
+ def merge(dict)
146
+ Dictionary.new(super(dict))
147
+ end
148
+
149
+ def []=(key,val)
150
+ unless key.is_a?(Symbol) or key.is_a?(Name)
151
+ fail "Expecting a Name for a Dictionary entry, found #{key.class} instead."
152
+ end
153
+
154
+ key = key.to_o
155
+ if not val.nil?
156
+ val = val.to_o
157
+ super(key,val)
158
+
159
+ key.parent = self
160
+ val.parent = self unless val.is_indirect? or val.parent.equal?(self)
161
+
162
+ val
163
+ else
164
+ delete(key)
165
+ end
166
+ end
167
+
168
+ def [](key)
169
+ super(key.to_o)
170
+ end
171
+
172
+ def has_key?(key)
173
+ super(key.to_o)
174
+ end
175
+
176
+ def delete(key)
177
+ super(key.to_o)
178
+ end
179
+
180
+ alias each each_value
181
+
182
+ def real_type ; Dictionary end
183
+
184
+ alias value to_h
185
+
186
+ end #class
187
+
188
+ end # Origami
@@ -0,0 +1,96 @@
1
+ =begin
2
+
3
+ = File
4
+ docmdp.rb
5
+
6
+ = Info
7
+ This file is part of Origami, PDF manipulation framework for Ruby
8
+ Copyright (C) 2010 Guillaume Delugr� <guillaume@security-labs.org>
9
+ All right reserved.
10
+
11
+ Origami is free software: you can redistribute it and/or modify
12
+ it under the terms of the GNU Lesser General Public License as published by
13
+ the Free Software Foundation, either version 3 of the License, or
14
+ (at your option) any later version.
15
+
16
+ Origami is distributed in the hope that it will be useful,
17
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
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/>.
23
+
24
+ =end
25
+
26
+ module Origami
27
+
28
+ class Null
29
+ def to_docmdp_str
30
+ "\000"
31
+ end
32
+ end
33
+
34
+ class Integer
35
+ def to_docmdp_str
36
+ [ 1, self.value & 0xFFFFFFFF ].pack("CN")
37
+ end
38
+ end
39
+
40
+ class Real
41
+ def to_docmdp_str
42
+ [ 2, self.value.round & 0xFFFFFFFF ].pack("CN")
43
+ end
44
+ end
45
+
46
+ class Boolean
47
+ def to_docmdp_str
48
+ [ 3, (self.false?) ? 0 : 1 ].pack("CN")
49
+ end
50
+ end
51
+
52
+ class Name
53
+ def to_docmdp_str
54
+ [ 4, self.to_s.length, self.to_s ].pack("CNA*")
55
+ end
56
+ end
57
+
58
+ class String
59
+ def to_docmdp_str
60
+ [ 5, self.to_s.length, self.to_s ].pack("CNA*")
61
+ end
62
+ end
63
+
64
+ class Dictionary
65
+ def to_docmdp_str(*fields)
66
+ if fields.empty?
67
+ self.each_pair { |key, value|
68
+
69
+ }
70
+ else
71
+
72
+ end
73
+ end
74
+ end
75
+
76
+ class Array
77
+ def to_docmdp_str
78
+ str = [ 7, self.length ].pack("CN")
79
+
80
+ self.each do |obj|
81
+ str << obj.to_docmdp_str
82
+ end
83
+
84
+ str
85
+ end
86
+ end
87
+
88
+ class Stream
89
+ def to_docmdp_str
90
+ [ 8, self.dictionary.size ].pack("CN") +
91
+ self.dictionary.to_docmdp_str(:DecodeParms, :F, :FDecodeParms, :FFilter, :Filter, :Length) +
92
+ [ self.rawdata.length, self.rawdata ].pack("NA*")
93
+ end
94
+ end
95
+
96
+ end