pdfrb 0.8.12 → 0.8.13

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20aa9dfc7f1144740e4138b85909b2434e1652fcc88ea6d83445d0e76c40f68b
4
- data.tar.gz: 8384f5165e823cc2077b290db27d74c2534b5f53951c4afa6d61a4eec277c340
3
+ metadata.gz: 0ad553584a2493f6da5f6df48476a5a348ab503d09745e2b64e43d2cd59f1cf0
4
+ data.tar.gz: d5211243bd235620e630160ad5e73c39874affb7773df0a8fed3688e6ed3dca3
5
5
  SHA512:
6
- metadata.gz: e988fa5b3a1e691b49ebb716311daa0a30d67d1b4c20a1e18e4d32d827f791ffd3931abd7250fcd4732cb4ad53cd92f45b84ac030b380f6d8e32de4edd52f1aa
7
- data.tar.gz: 9d280097604c785c8c39aee8defb1fb7979901cec4b965e4b76d8b553d5d620036a7efb2925cd7b473baf72538662aa17c0c1f0217f67d58efd7ba4b3d3185e6
6
+ metadata.gz: f9a55bb12f57b10bbe2f5ec125ccd040250c3e3f44682da595570ab0190a45d1161b30b4ef5168e90c72d615b8be52d6bbea56e31cd5d8d89d7f08c1ee0fe904
7
+ data.tar.gz: 26870c2bc38912312770d2fed3d5fb879847222c22ff6f910edc74360f79f766d32b50f404136d4b0142df01f067553755288757cd1d9eb6b43ca6d06b59b6ef
data/.rubocop.yml CHANGED
@@ -30,3 +30,11 @@ RSpec/DescribeClass:
30
30
  # the snake_case → CamelCase file-path convention.
31
31
  RSpec/SpecFilePathFormat:
32
32
  Enabled: false
33
+
34
+ # actions/checkout v6 injects `.required_attributes` (`* text=auto`)
35
+ # into .git/info/attributes, which overrides this repo's .gitattributes
36
+ # (`*.rb eol=lf`) and checks text files out as CRLF on Windows runners.
37
+ # Line endings are a git/checkout concern, not a code concern, so the
38
+ # cop is disabled here rather than fought per-runner.
39
+ Layout/EndOfLine:
40
+ Enabled: false
@@ -332,8 +332,9 @@ module Pdfrb
332
332
  decipher.key = key
333
333
  decipher.iv = iv
334
334
  decipher.update(ciphertext) + decipher.final
335
- rescue OpenSSL::Cipher::CipherError
336
- data
335
+ rescue OpenSSL::Cipher::CipherError => e
336
+ raise Pdfrb::EncryptionError,
337
+ "AES stream/string decryption failed (#{e.message})"
337
338
  end
338
339
  end
339
340
 
@@ -61,8 +61,8 @@ module Pdfrb
61
61
  components << glyph_index
62
62
  offset += 4
63
63
 
64
- offset += 4 if flags & 0x0001 != 0
65
- offset += 2 if flags & 0x0002 != 0
64
+ offset += 4 if flags.anybits?(0x0001)
65
+ offset += 2 if flags.anybits?(0x0002)
66
66
  break if flags.nobits?(0x0020)
67
67
  end
68
68
  components
@@ -26,8 +26,8 @@ module Pdfrb
26
26
  def long_loca?; @index_to_loc_format == 1; end
27
27
  def short_loca?; @index_to_loc_format.zero?; end
28
28
 
29
- def bold?; (@mac_style || 0) & 0x01 != 0; end
30
- def italic?; (@mac_style || 0) & 0x02 != 0; end
29
+ def bold?; (@mac_style || 0).anybits?(0x01); end
30
+ def italic?; (@mac_style || 0).anybits?(0x02); end
31
31
 
32
32
  private
33
33
 
@@ -21,7 +21,7 @@ module Pdfrb
21
21
  sub_length = u16(data, offset + 2)
22
22
  coverage = u16(data, offset + 4)
23
23
 
24
- if version.zero? && coverage & 0x0001 != 0
24
+ if version.zero? && coverage.anybits?(0x0001)
25
25
  n_pairs = u16(data, offset + 6)
26
26
  pair_offset = offset + 14
27
27
 
@@ -34,8 +34,8 @@ module Pdfrb
34
34
  @fs_selection = u16(62)
35
35
  end
36
36
 
37
- def bold?; (@fs_selection || 0) & 0x20 != 0; end
38
- def italic?; (@fs_selection || 0) & 0x01 != 0; end
37
+ def bold?; (@fs_selection || 0).anybits?(0x20); end
38
+ def italic?; (@fs_selection || 0).anybits?(0x01); end
39
39
 
40
40
  private
41
41
 
@@ -52,14 +52,14 @@ module Pdfrb
52
52
  # right-joining letter that's not already followed by a space).
53
53
  def kashida_insertion_points(text)
54
54
  points = []
55
- prev = nil
55
+ nil
56
56
  text.to_s.each_char.with_index do |ch, i|
57
57
  next_char = text.to_s[i + 1]
58
58
  if RIGHT_JOINING.include?(ch) && next_char &&
59
59
  RIGHT_JOINING.include?(next_char)
60
60
  points << (i + 1)
61
61
  end
62
- prev = ch
62
+ ch
63
63
  end
64
64
  points
65
65
  end
@@ -13,51 +13,51 @@ module Pdfrb
13
13
  def flags; self[:Flags] || 0; end
14
14
 
15
15
  def include_exclude?
16
- flags & 1 != 0
16
+ flags.anybits?(1)
17
17
  end
18
18
 
19
19
  def include_no_value_fields?
20
- flags & 2 != 0
20
+ flags.anybits?(2)
21
21
  end
22
22
 
23
23
  def export_format?
24
- flags & 4 != 0
24
+ flags.anybits?(4)
25
25
  end
26
26
 
27
27
  def get_method?
28
- flags & 8 != 0
28
+ flags.anybits?(8)
29
29
  end
30
30
 
31
31
  def submit_coordinates?
32
- flags & 16 != 0
32
+ flags.anybits?(16)
33
33
  end
34
34
 
35
35
  def submit_as_xfdf?
36
- flags & 32 != 0
36
+ flags.anybits?(32)
37
37
  end
38
38
 
39
39
  def submit_as_append?
40
- flags & 64 != 0
40
+ flags.anybits?(64)
41
41
  end
42
42
 
43
43
  def submit_as_pdf?
44
- flags & 256 != 0
44
+ flags.anybits?(256)
45
45
  end
46
46
 
47
47
  def canonical_date_format?
48
- flags & 512 != 0
48
+ flags.anybits?(512)
49
49
  end
50
50
 
51
51
  def submit_as_xfdf_with_annotations?
52
- flags & 1024 != 0
52
+ flags.anybits?(1024)
53
53
  end
54
54
 
55
55
  def submit_as_xdp?
56
- flags & 2048 != 0
56
+ flags.anybits?(2048)
57
57
  end
58
58
 
59
59
  def submit_as_xfdf_charset?
60
- flags & 4096 != 0
60
+ flags.anybits?(4096)
61
61
  end
62
62
  end
63
63
  end
@@ -53,15 +53,15 @@ module Pdfrb
53
53
  end
54
54
 
55
55
  # ---- Flag predicates ----
56
- def hidden?; flags & 1 != 0; end
57
- def print?; flags & 2 != 0; end
58
- def no_zoom?; flags & 4 != 0; end
59
- def no_rotate?; flags & 8 != 0; end
60
- def no_view?; flags & 16 != 0; end
61
- def read_only?; flags & 32 != 0; end
62
- def locked?; flags & 128 != 0; end
63
- def toggle_no_view?; flags & 256 != 0; end
64
- def locked_contents?; flags & 512 != 0; end
56
+ def hidden?; flags.anybits?(1); end
57
+ def print?; flags.anybits?(2); end
58
+ def no_zoom?; flags.anybits?(4); end
59
+ def no_rotate?; flags.anybits?(8); end
60
+ def no_view?; flags.anybits?(16); end
61
+ def read_only?; flags.anybits?(32); end
62
+ def locked?; flags.anybits?(128); end
63
+ def toggle_no_view?; flags.anybits?(256); end
64
+ def locked_contents?; flags.anybits?(512); end
65
65
 
66
66
  # ---- Appearance management ----
67
67
  def normal_appearance
@@ -7,11 +7,11 @@ module Pdfrb
7
7
  def value; self[:V]; end
8
8
  def opt; self[:Opt]; end
9
9
 
10
- def push_button?; flags & 0x10000 != 0; end
11
- def radio?; flags & 0x8000 != 0; end
10
+ def push_button?; flags.anybits?(0x10000); end
11
+ def radio?; flags.anybits?(0x8000); end
12
12
  def checkbox?; !push_button? && !radio?; end
13
- def no_toggle_to_off?; flags & 0x4000 != 0; end
14
- def radios_in_unison?; flags & 0x2000000 != 0; end
13
+ def no_toggle_to_off?; flags.anybits?(0x4000); end
14
+ def radios_in_unison?; flags.anybits?(0x2000000); end
15
15
  end
16
16
 
17
17
  # Checkbox button field (s12.7.4.2.2, /Ft /Btn without push or
@@ -10,12 +10,12 @@ module Pdfrb
10
10
  def top_index; self[:TI]; end
11
11
  def indices; self[:I]; end
12
12
 
13
- def combo?; flags & 0x20000 != 0; end
13
+ def combo?; flags.anybits?(0x20000); end
14
14
  def list?; !combo?; end
15
- def edit?; flags & 0x40000 != 0; end
16
- def multi_select?; flags & 0x200000 != 0; end
17
- def sort?; flags & 0x80000 != 0; end
18
- def commit_on_change?; flags & 0x4000000 != 0; end
15
+ def edit?; flags.anybits?(0x40000); end
16
+ def multi_select?; flags.anybits?(0x200000); end
17
+ def sort?; flags.anybits?(0x80000); end
18
+ def commit_on_change?; flags.anybits?(0x4000000); end
19
19
  end
20
20
  end
21
21
  end
@@ -40,9 +40,9 @@ module Pdfrb
40
40
  def choice?; field_type&.to_sym == :Ch; end
41
41
  def signature?; field_type&.to_sym == :Sig; end
42
42
 
43
- def read_only?; flags & 1 != 0; end
44
- def required?; flags & 2 != 0; end
45
- def no_export?; flags & 4 != 0; end
43
+ def read_only?; flags.anybits?(1); end
44
+ def required?; flags.anybits?(2); end
45
+ def no_export?; flags.anybits?(4); end
46
46
 
47
47
  def has_kids?
48
48
  kids && (!kids.is_a?(Array) || !kids.empty?)
@@ -13,9 +13,9 @@ module Pdfrb
13
13
  def cert; self[:Cert]; end
14
14
  def flags; self[:F] || 0; end
15
15
  def legal_attestation; self[:LegalAttestation]; end
16
- def add_rev_info?; flags & 1 != 0; end
17
- def add_doc_mdp?; flags & 2 != 0; end
18
- def add_field_mdp?; flags & 4 != 0; end
16
+ def add_rev_info?; flags.anybits?(1); end
17
+ def add_doc_mdp?; flags.anybits?(2); end
18
+ def add_field_mdp?; flags.anybits?(4); end
19
19
  def required_filter?; filter && flags.anybits?(0x100); end
20
20
  def required_subfilter?; sub_filter && flags.anybits?(0x200); end
21
21
  def required_digest?; digest_method && flags.anybits?(0x400); end
@@ -9,13 +9,13 @@ module Pdfrb
9
9
  def value; self[:V]; end
10
10
  def rich_text_value; self[:RV]; end
11
11
 
12
- def multiline?; flags & 0x1000 != 0; end
13
- def password?; flags & 0x2000 != 0; end
14
- def file_select?; flags & 0x100000 != 0; end
15
- def do_not_spell_check?; flags & 0x400000 != 0; end
16
- def do_not_scroll?; flags & 0x800000 != 0; end
17
- def comb?; flags & 0x1000000 != 0; end
18
- def rich_text?; flags & 0x2000000 != 0; end
12
+ def multiline?; flags.anybits?(0x1000); end
13
+ def password?; flags.anybits?(0x2000); end
14
+ def file_select?; flags.anybits?(0x100000); end
15
+ def do_not_spell_check?; flags.anybits?(0x400000); end
16
+ def do_not_scroll?; flags.anybits?(0x800000); end
17
+ def comb?; flags.anybits?(0x1000000); end
18
+ def rich_text?; flags.anybits?(0x2000000); end
19
19
  end
20
20
  end
21
21
  end
@@ -64,10 +64,10 @@ module Pdfrb
64
64
  case obj
65
65
  when Pdfrb::Model::Cos::Stream
66
66
  dict_value = encrypt_strings ? Pdfrb::Encryption::ValueStrings.encrypt(obj.value, obj.oid, obj.gen, encrypter) : obj.value
67
- dict, payload = emit_stream(obj, dict_value)
67
+ dict, payload = emit_stream(obj, dict_value, encrypter: encrypter && !skip_encryption ? encrypter : nil)
68
68
  buffer << dict
69
69
  buffer << "\nstream\n"
70
- buffer << (encrypter && !skip_encryption ? encrypter.encrypt_stream(payload, obj.oid, obj.gen) : payload)
70
+ buffer << payload
71
71
  buffer << "\nendstream\n"
72
72
  else
73
73
  value = encrypt_strings ? Pdfrb::Encryption::ValueStrings.encrypt(obj.value, obj.oid, obj.gen, encrypter) : obj.value
@@ -82,8 +82,12 @@ module Pdfrb
82
82
 
83
83
  # Build the dict body + payload for a stream object, applying
84
84
  # FlateDecode compression when enabled and the stream is large
85
- # enough to benefit. Returns [dict_bytes, payload_bytes].
86
- def emit_stream(stream_obj, dict_value = stream_obj.value)
85
+ # enough to benefit, then encryption. Returns [dict_bytes,
86
+ # payload_bytes]. Encryption is applied before /Length is
87
+ # recorded: AESV2 grows the payload (IV + padding), so the
88
+ # declared /Length must be computed from the final stored bytes
89
+ # (s7.3.8.2 — filters first, then encryption).
90
+ def emit_stream(stream_obj, dict_value = stream_obj.value, encrypter: nil)
87
91
  payload = stream_obj.stream
88
92
  hash = dict_value.dup
89
93
  existing_filter = hash[:Filter]
@@ -92,11 +96,11 @@ module Pdfrb
92
96
  compressed = compress_bytes(payload)
93
97
  if compressed.bytesize < payload.bytesize
94
98
  hash[:Filter] = combine_filters(existing_filter, :FlateDecode)
95
- hash[:Length] = compressed.bytesize
96
- return [serialize_dict(hash), compressed]
99
+ payload = compressed
97
100
  end
98
101
  end
99
102
 
103
+ payload = encrypter.encrypt_stream(payload, stream_obj.oid, stream_obj.gen) if encrypter
100
104
  hash[:Length] = payload.bytesize
101
105
  [serialize_dict(hash), payload]
102
106
  end
@@ -123,10 +123,12 @@ module Pdfrb
123
123
 
124
124
  def read_stream_payload(dict_value)
125
125
  skip_single_eol
126
+ # Prefer /Length when it self-validates: the stored payload is
127
+ # followed by at most one EOL before "endstream". Only fall
128
+ # back to scanning (real-world generators emit wrong /Length)
129
+ # when the declared length contradicts the available span.
126
130
  # Always scan for "endstream" from the current position.
127
- # /Length is unreliable in real-world PDFs (generator bugs,
128
- # CRLF→LF conversion). Scanning is correct in all cases.
129
- scan_to_endstream
131
+ scan_to_endstream(dict_value)
130
132
  end
131
133
 
132
134
  def endstream_next?
@@ -149,7 +151,7 @@ module Pdfrb
149
151
  peek_str == "endstream"
150
152
  end
151
153
 
152
- def scan_to_endstream
154
+ def scan_to_endstream(dict_value = nil)
153
155
  start = io.pos
154
156
  chunk = io.read
155
157
  idx = chunk.index("endstream")
@@ -158,16 +160,33 @@ module Pdfrb
158
160
  # expect_keyword("endstream") can consume it normally.
159
161
  io.seek(start + idx, IO::SEEK_SET)
160
162
  content = chunk.byteslice(0, idx)
161
- # Strip one trailing EOL before "endstream" (spec: it's
162
- # not part of the stream data).
163
- content = content.sub(/\r?\n\z/, "".b) if content.end_with?("\n")
164
- content
163
+ strip_stream_eol(content, dict_value)
165
164
  else
166
165
  io.seek(start, IO::SEEK_SET)
167
166
  +""
168
167
  end
169
168
  end
170
169
 
170
+ # Stripping the EOL that precedes "endstream" (it is not part of
171
+ # the stream data) is only unambiguous against the declared
172
+ # /Length: when /Length is present and the bytes after it are
173
+ # pure EOL, the payload is exactly /Length bytes. Without a
174
+ # trustworthy /Length, strip exactly one EOL ("\r\n", "\n", or
175
+ # "\r") — never more, or a payload whose final data byte looks
176
+ # like an EOL loses a real byte (this corrupted AES ciphertext
177
+ # whose last byte happened to be 0x0D).
178
+ def strip_stream_eol(content, dict_value)
179
+ length = length_of(dict_value)
180
+ if length && content.bytesize >= length &&
181
+ content.byteslice(length, 2).match?(/\A(\r\n|\n|\r)?\z/)
182
+ return content.byteslice(0, length)
183
+ end
184
+
185
+ return content.sub(/\r\n\z/, "".b) if content.end_with?("\r\n")
186
+
187
+ content.sub(/\n\z|\r\z/, "".b)
188
+ end
189
+
171
190
  def length_of(dict_value)
172
191
  return nil unless dict_value.is_a?(::Hash)
173
192
 
data/lib/pdfrb/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pdfrb
4
- VERSION = "0.8.12"
4
+ VERSION = "0.8.13"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdfrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.12
4
+ version: 0.8.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-09-02 00:00:00.000000000 Z
11
+ date: 2026-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: benchmark