pdf-reader 2.7.0 → 2.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +20 -0
  3. data/Rakefile +1 -1
  4. data/lib/pdf/reader/aes_v2_security_handler.rb +41 -0
  5. data/lib/pdf/reader/aes_v3_security_handler.rb +38 -0
  6. data/lib/pdf/reader/bounding_rectangle_runs_filter.rb +16 -0
  7. data/lib/pdf/reader/buffer.rb +36 -34
  8. data/lib/pdf/reader/cmap.rb +64 -51
  9. data/lib/pdf/reader/error.rb +8 -0
  10. data/lib/pdf/reader/filter/ascii85.rb +1 -1
  11. data/lib/pdf/reader/filter/ascii_hex.rb +1 -1
  12. data/lib/pdf/reader/filter/depredict.rb +1 -1
  13. data/lib/pdf/reader/filter/flate.rb +3 -3
  14. data/lib/pdf/reader/filter/lzw.rb +1 -1
  15. data/lib/pdf/reader/filter/null.rb +1 -2
  16. data/lib/pdf/reader/filter/run_length.rb +1 -1
  17. data/lib/pdf/reader/filter.rb +10 -11
  18. data/lib/pdf/reader/font.rb +71 -16
  19. data/lib/pdf/reader/font_descriptor.rb +18 -17
  20. data/lib/pdf/reader/form_xobject.rb +14 -5
  21. data/lib/pdf/reader/key_builder_v5.rb +138 -0
  22. data/lib/pdf/reader/null_security_handler.rb +0 -4
  23. data/lib/pdf/reader/object_hash.rb +251 -44
  24. data/lib/pdf/reader/page.rb +51 -22
  25. data/lib/pdf/reader/page_layout.rb +14 -28
  26. data/lib/pdf/reader/page_state.rb +1 -1
  27. data/lib/pdf/reader/page_text_receiver.rb +52 -10
  28. data/lib/pdf/reader/parser.rb +22 -7
  29. data/lib/pdf/reader/point.rb +1 -1
  30. data/lib/pdf/reader/rc4_security_handler.rb +38 -0
  31. data/lib/pdf/reader/rectangle.rb +20 -2
  32. data/lib/pdf/reader/{resource_methods.rb → resources.rb} +15 -13
  33. data/lib/pdf/reader/security_handler_factory.rb +79 -0
  34. data/lib/pdf/reader/{standard_security_handler.rb → standard_key_builder.rb} +23 -95
  35. data/lib/pdf/reader/stream.rb +2 -2
  36. data/lib/pdf/reader/text_run.rb +13 -6
  37. data/lib/pdf/reader/type_check.rb +52 -0
  38. data/lib/pdf/reader/validating_receiver.rb +262 -0
  39. data/lib/pdf/reader/width_calculator/true_type.rb +1 -1
  40. data/lib/pdf/reader/xref.rb +20 -3
  41. data/lib/pdf/reader.rb +32 -11
  42. data/rbi/pdf-reader.rbi +408 -174
  43. metadata +16 -9
  44. data/lib/pdf/reader/standard_security_handler_v5.rb +0 -92
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf-reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Healy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-13 00:00:00.000000000 Z
11
+ date: 2022-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -200,6 +200,8 @@ files:
200
200
  - examples/version.rb
201
201
  - lib/pdf-reader.rb
202
202
  - lib/pdf/reader.rb
203
+ - lib/pdf/reader/aes_v2_security_handler.rb
204
+ - lib/pdf/reader/aes_v3_security_handler.rb
203
205
  - lib/pdf/reader/afm/Courier-Bold.afm
204
206
  - lib/pdf/reader/afm/Courier-BoldOblique.afm
205
207
  - lib/pdf/reader/afm/Courier-Oblique.afm
@@ -215,6 +217,7 @@ files:
215
217
  - lib/pdf/reader/afm/Times-Italic.afm
216
218
  - lib/pdf/reader/afm/Times-Roman.afm
217
219
  - lib/pdf/reader/afm/ZapfDingbats.afm
220
+ - lib/pdf/reader/bounding_rectangle_runs_filter.rb
218
221
  - lib/pdf/reader/buffer.rb
219
222
  - lib/pdf/reader/cid_widths.rb
220
223
  - lib/pdf/reader/cmap.rb
@@ -241,6 +244,7 @@ files:
241
244
  - lib/pdf/reader/glyph_hash.rb
242
245
  - lib/pdf/reader/glyphlist-zapfdingbats.txt
243
246
  - lib/pdf/reader/glyphlist.txt
247
+ - lib/pdf/reader/key_builder_v5.rb
244
248
  - lib/pdf/reader/lzw.rb
245
249
  - lib/pdf/reader/null_security_handler.rb
246
250
  - lib/pdf/reader/object_cache.rb
@@ -255,18 +259,21 @@ files:
255
259
  - lib/pdf/reader/parser.rb
256
260
  - lib/pdf/reader/point.rb
257
261
  - lib/pdf/reader/print_receiver.rb
262
+ - lib/pdf/reader/rc4_security_handler.rb
258
263
  - lib/pdf/reader/rectangle.rb
259
264
  - lib/pdf/reader/reference.rb
260
265
  - lib/pdf/reader/register_receiver.rb
261
- - lib/pdf/reader/resource_methods.rb
262
- - lib/pdf/reader/standard_security_handler.rb
263
- - lib/pdf/reader/standard_security_handler_v5.rb
266
+ - lib/pdf/reader/resources.rb
267
+ - lib/pdf/reader/security_handler_factory.rb
268
+ - lib/pdf/reader/standard_key_builder.rb
264
269
  - lib/pdf/reader/stream.rb
265
270
  - lib/pdf/reader/synchronized_cache.rb
266
271
  - lib/pdf/reader/text_run.rb
267
272
  - lib/pdf/reader/token.rb
268
273
  - lib/pdf/reader/transformation_matrix.rb
274
+ - lib/pdf/reader/type_check.rb
269
275
  - lib/pdf/reader/unimplemented_security_handler.rb
276
+ - lib/pdf/reader/validating_receiver.rb
270
277
  - lib/pdf/reader/width_calculator.rb
271
278
  - lib/pdf/reader/width_calculator/built_in.rb
272
279
  - lib/pdf/reader/width_calculator/composite.rb
@@ -281,9 +288,9 @@ licenses:
281
288
  - MIT
282
289
  metadata:
283
290
  bug_tracker_uri: https://github.com/yob/pdf-reader/issues
284
- changelog_uri: https://github.com/yob/pdf-reader/blob/v2.7.0/CHANGELOG
285
- documentation_uri: https://www.rubydoc.info/gems/pdf-reader/2.7.0
286
- source_code_uri: https://github.com/yob/pdf-reader/tree/v2.7.0
291
+ changelog_uri: https://github.com/yob/pdf-reader/blob/v2.9.2/CHANGELOG
292
+ documentation_uri: https://www.rubydoc.info/gems/pdf-reader/2.9.2
293
+ source_code_uri: https://github.com/yob/pdf-reader/tree/v2.9.2
287
294
  post_install_message:
288
295
  rdoc_options:
289
296
  - "--title"
@@ -304,7 +311,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
304
311
  - !ruby/object:Gem::Version
305
312
  version: '0'
306
313
  requirements: []
307
- rubygems_version: 3.2.3
314
+ rubygems_version: 3.2.32
308
315
  signing_key:
309
316
  specification_version: 4
310
317
  summary: A library for accessing the content of PDF files
@@ -1,92 +0,0 @@
1
- # coding: utf-8
2
- # typed: true
3
- # frozen_string_literal: true
4
-
5
- require 'digest'
6
- require 'openssl'
7
-
8
- class PDF::Reader
9
-
10
- # class creates interface to encrypt dictionary for use in Decrypt
11
- class StandardSecurityHandlerV5
12
-
13
- attr_reader :key_length, :encrypt_key
14
-
15
- def initialize(opts = {})
16
- @key_length = 256
17
- @O = opts[:O] # hash(32B) + validation salt(8B) + key salt(8B)
18
- @U = opts[:U] # hash(32B) + validation salt(8B) + key salt(8B)
19
- @OE = opts[:OE] # decryption key, encrypted w/ owner password
20
- @UE = opts[:UE] # decryption key, encrypted w/ user password
21
- @encrypt_key = build_standard_key(opts[:password] || '')
22
- end
23
-
24
- # This handler supports AES-256 encryption defined in PDF 1.7 Extension Level 3
25
- def self.supports?(encrypt)
26
- return false if encrypt.nil?
27
-
28
- filter = encrypt.fetch(:Filter, :Standard)
29
- version = encrypt.fetch(:V, 0)
30
- revision = encrypt.fetch(:R, 0)
31
- algorithm = encrypt.fetch(:CF, {}).fetch(encrypt[:StmF], {}).fetch(:CFM, nil)
32
- (filter == :Standard) && (encrypt[:StmF] == encrypt[:StrF]) &&
33
- ((version == 5) && (revision == 5) && (algorithm == :AESV3))
34
- end
35
-
36
- ##7.6.2 General Encryption Algorithm
37
- #
38
- # Algorithm 1: Encryption of data using the RC4 or AES algorithms
39
- #
40
- # used to decrypt RC4/AES encrypted PDF streams (buf)
41
- #
42
- # buf - a string to decrypt
43
- # ref - a PDF::Reader::Reference for the object to decrypt
44
- #
45
- def decrypt( buf, ref )
46
- cipher = OpenSSL::Cipher.new("AES-#{@key_length}-CBC")
47
- cipher.decrypt
48
- cipher.key = @encrypt_key.dup
49
- cipher.iv = buf[0..15]
50
- cipher.update(buf[16..-1]) + cipher.final
51
- end
52
-
53
- private
54
- # Algorithm 3.2a - Computing an encryption key
55
- #
56
- # Defined in PDF 1.7 Extension Level 3
57
- #
58
- # if the string is a valid user/owner password, this will return the decryption key
59
- #
60
- def auth_owner_pass(password)
61
- if Digest::SHA256.digest(password + @O[32..39] + @U) == @O[0..31]
62
- cipher = OpenSSL::Cipher.new('AES-256-CBC')
63
- cipher.decrypt
64
- cipher.key = Digest::SHA256.digest(password + @O[40..-1] + @U)
65
- cipher.iv = "\x00" * 16
66
- cipher.padding = 0
67
- cipher.update(@OE) + cipher.final
68
- end
69
- end
70
-
71
- def auth_user_pass(password)
72
- if Digest::SHA256.digest(password + @U[32..39]) == @U[0..31]
73
- cipher = OpenSSL::Cipher.new('AES-256-CBC')
74
- cipher.decrypt
75
- cipher.key = Digest::SHA256.digest(password + @U[40..-1])
76
- cipher.iv = "\x00" * 16
77
- cipher.padding = 0
78
- cipher.update(@UE) + cipher.final
79
- end
80
- end
81
-
82
- def build_standard_key(pass)
83
- pass = pass.byteslice(0...127) # UTF-8 encoded password. first 127 bytes
84
-
85
- encrypt_key = auth_owner_pass(pass)
86
- encrypt_key ||= auth_user_pass(pass)
87
-
88
- raise PDF::Reader::EncryptedPDFError, "Invalid password (#{pass})" if encrypt_key.nil?
89
- encrypt_key
90
- end
91
- end
92
- end