pdf-reader 2.0.0.beta1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4ea96ce79d9f4cc65a0a026ea7e50da7b33cd19
4
- data.tar.gz: e2302c2d18cdc64cd81654f30658b5cf1e8ae3c3
3
+ metadata.gz: d34a3033638fcf4f78ce2d71dd5f5aebc2ea7d68
4
+ data.tar.gz: 912ed3986ab7e7eefce8f987cffc0d0357bcd675
5
5
  SHA512:
6
- metadata.gz: 31da13f8b8e38dffbb19a33855beb1c85a14c2633137fd8e1957db14f3bac16a434c174e79bab6092c72954e6a6f87f0ab585562e79d39d5ede8f9398fd63f7b
7
- data.tar.gz: e097e8aed8bbeb918676bacded748a538e109fdcf1e8740cfe1ce3d63105d891cbe2a90ad228bb87fb494cd796bd7e1c66138592ff21d62c97b87fa270eb0899
6
+ metadata.gz: a24c874cae12838cf122c92338e6da1639d15187be9ef5c1f149eedd0167cc21b53544c3148efd37992535efe2d0ec4487a02fc3a668992295cecc7aa5529f2a
7
+ data.tar.gz: c88b9daa21de84eb2a841cc024c499ecc7c5d4b5908ad6618494fa3e7f5d694d291ae87ceb72f5ff9f4d2db66b3bc781e1ff499485ba50290710c6e1061a76db
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ v2.0.0 (25th February 2017)
2
+ - various bug fixes
3
+
1
4
  v2.0.0.beta1 (15th February 2017)
2
5
  - BREAKING CHANGE: remove all methods that were deprecated in 1.0.0
3
6
  - Bug: Support extra encrypted PDF variants (thanks to Gyuchang Jun)
@@ -275,7 +275,9 @@ require 'pdf/reader/parser'
275
275
  require 'pdf/reader/print_receiver'
276
276
  require 'pdf/reader/reference'
277
277
  require 'pdf/reader/register_receiver'
278
+ require 'pdf/reader/null_security_handler'
278
279
  require 'pdf/reader/standard_security_handler'
280
+ require 'pdf/reader/unimplemented_security_handler'
279
281
  require 'pdf/reader/stream'
280
282
  require 'pdf/reader/text_run'
281
283
  require 'pdf/reader/page_state'
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+
3
+ class PDF::Reader
4
+
5
+ # A null object security handler. Used when a PDF is unencrypted.
6
+ class NullSecurityHandler
7
+
8
+ def self.supports?(encrypt)
9
+ encrypt.nil?
10
+ end
11
+
12
+ def decrypt(buf, _ref)
13
+ buf
14
+ end
15
+ end
16
+ end
@@ -45,6 +45,7 @@ class PDF::Reader
45
45
  @pdf_version = read_version
46
46
  @trailer = @xref.trailer
47
47
  @cache = opts[:cache] || PDF::Reader::ObjectCache.new
48
+ @sec_handler = NullSecurityHandler.new
48
49
  @sec_handler = build_security_handler(opts)
49
50
  end
50
51
 
@@ -286,20 +287,27 @@ class PDF::Reader
286
287
  end
287
288
 
288
289
  def build_security_handler(opts = {})
289
- return nil if trailer[:Encrypt].nil?
290
-
291
- enc = deref(trailer[:Encrypt])
292
- case enc[:Filter]
293
- when :Standard
294
- StandardSecurityHandler.new(enc, deref(trailer[:ID]), opts[:password])
290
+ encrypt = deref(trailer[:Encrypt])
291
+ if NullSecurityHandler.supports?(encrypt)
292
+ NullSecurityHandler.new
293
+ elsif StandardSecurityHandler.supports?(encrypt)
294
+ encmeta = !encrypt.has_key?(:EncryptMetadata) || encrypt[:EncryptMetadata].to_s == "true"
295
+ StandardSecurityHandler.new(
296
+ key_length: (encrypt[:Length] || 40).to_i,
297
+ revision: encrypt[:R],
298
+ owner_key: encrypt[:O],
299
+ user_key: encrypt[:U],
300
+ permissions: encrypt[:P].to_i,
301
+ encrypted_metadata: encmeta,
302
+ file_id: (deref(trailer[:ID]) || []).first,
303
+ password: opts[:password]
304
+ )
295
305
  else
296
- raise PDF::Reader::EncryptedPDFError, "Unsupported encryption method (#{enc[:Filter]})"
306
+ UnimplementedSecurityHandler.new
297
307
  end
298
308
  end
299
309
 
300
310
  def decrypt(ref, obj)
301
- return obj unless sec_handler?
302
-
303
311
  case obj
304
312
  when PDF::Reader::Stream then
305
313
  obj.data = sec_handler.decrypt(obj.data, ref)
@@ -331,7 +339,7 @@ class PDF::Reader
331
339
  # returns a nested array of object references for all pages in this object store.
332
340
  #
333
341
  def get_page_objects(ref)
334
- obj = fetch(ref)
342
+ obj = deref(ref)
335
343
 
336
344
  if obj[:Type] == :Page
337
345
  ref
@@ -42,30 +42,36 @@ class PDF::Reader
42
42
  0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80,
43
43
  0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a ]
44
44
 
45
- attr_reader :filter, :subFilter, :version, :key_length,
46
- :crypt_filter, :stream_filter, :string_filter, :embedded_file_filter,
47
- :encrypt_key
48
- attr_reader :revision, :owner_key, :user_key, :permissions, :file_id, :password
49
-
50
- def initialize( enc, file_id, password )
51
- @filter = enc[:Filter]
52
- @subFilter = enc[:SubFilter]
53
- @version = enc[:V].to_i
54
- @key_length = enc[:Length].to_i/8
55
- @crypt_filter = enc[:CF]
56
- @stream_filter = enc[:StmF]
57
- @string_filter = enc[:StrF]
58
- @revision = enc[:R].to_i
59
- @owner_key = enc[:O]
60
- @user_key = enc[:U]
61
- @permissions = enc[:P].to_i
62
- @embedded_file_filter = enc[:EFF]
63
-
64
- @encryptMeta = enc.has_key?(:EncryptMetadata)? enc[:EncryptMetadata].to_s == "true" : true;
65
-
66
- @file_id = (file_id || []).first || ""
67
-
68
- @encrypt_key = build_standard_key(password)
45
+ attr_reader :key_length, :revision, :encrypt_key
46
+ attr_reader :owner_key, :user_key, :permissions, :file_id, :password
47
+
48
+ def initialize(opts = {})
49
+ @key_length = opts[:key_length].to_i/8
50
+ @revision = opts[:revision].to_i
51
+ @owner_key = opts[:owner_key]
52
+ @user_key = opts[:user_key]
53
+ @permissions = opts[:permissions].to_i
54
+ @encryptMeta = opts.fetch(:encrypted_metadata, true)
55
+ @file_id = opts[:file_id] || ""
56
+ @encrypt_key = build_standard_key(opts[:password] || "")
57
+
58
+ if @key_length != 5 && @key_length != 16
59
+ msg = "StandardSecurityHandler only supports 40 and 128 bit\
60
+ encryption (#{@key_length * 8}bit)"
61
+ raise ArgumentError, msg
62
+ end
63
+ end
64
+
65
+ # This handler supports all RC4 encryption that follows the PDF spec. It does not support
66
+ # AES encryption that was added in later versions of the spec.
67
+ def self.supports?(encrypt)
68
+ return false if encrypt.nil?
69
+
70
+ filter = encrypt.fetch(:Filter, :Standard)
71
+ version = encrypt.fetch(:V, 0)
72
+ algorithm = encrypt.fetch(:CF, {}).fetch(encrypt[:StmF], {}).fetch(:CFM, nil)
73
+ filter == :Standard &&
74
+ (version <= 3 || (version == 4 && algorithm != :AESV2))
69
75
  end
70
76
 
71
77
  ##7.6.2 General Encryption Algorithm
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+
3
+ class PDF::Reader
4
+
5
+ # Security handler for when we don't support the flavour of encryption
6
+ # used in a PDF.
7
+ class UnimplementedSecurityHandler
8
+ def self.supports?(encrypt)
9
+ true
10
+ end
11
+
12
+ def decrypt(buf, ref)
13
+ raise PDF::Reader::EncryptedPDFError, "Unsupported encryption style"
14
+ end
15
+ end
16
+ end
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.0.0.beta1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Healy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-14 00:00:00.000000000 Z
11
+ date: 2017-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -241,6 +241,7 @@ files:
241
241
  - lib/pdf/reader/glyph_hash.rb
242
242
  - lib/pdf/reader/glyphlist.txt
243
243
  - lib/pdf/reader/lzw.rb
244
+ - lib/pdf/reader/null_security_handler.rb
244
245
  - lib/pdf/reader/object_cache.rb
245
246
  - lib/pdf/reader/object_hash.rb
246
247
  - lib/pdf/reader/object_stream.rb
@@ -261,6 +262,7 @@ files:
261
262
  - lib/pdf/reader/text_run.rb
262
263
  - lib/pdf/reader/token.rb
263
264
  - lib/pdf/reader/transformation_matrix.rb
265
+ - lib/pdf/reader/unimplemented_security_handler.rb
264
266
  - lib/pdf/reader/width_calculator.rb
265
267
  - lib/pdf/reader/width_calculator/built_in.rb
266
268
  - lib/pdf/reader/width_calculator/composite.rb
@@ -288,12 +290,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
288
290
  version: 1.9.3
289
291
  required_rubygems_version: !ruby/object:Gem::Requirement
290
292
  requirements:
291
- - - ">"
293
+ - - ">="
292
294
  - !ruby/object:Gem::Version
293
- version: 1.3.1
295
+ version: '0'
294
296
  requirements: []
295
297
  rubyforge_project:
296
- rubygems_version: 2.5.2
298
+ rubygems_version: 2.6.8
297
299
  signing_key:
298
300
  specification_version: 4
299
301
  summary: A library for accessing the content of PDF files