Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HP Scan invalid Length workaround #215

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/combine_pdf/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PDFParser
attr_reader :info_object, :root_object, :names_object, :forms_object, :outlines_object, :metadata

attr_reader :allow_optional_content, :raise_on_encrypted
attr_reader :check_stream_length
# when creating a parser, it is important to set the data (String) we wish to parse.
#
# <b>the data is required and it is not possible to set the data at a later stage</b>
Expand All @@ -59,6 +60,7 @@ def initialize(string, options = {})
@scanner = nil
@allow_optional_content = options[:allow_optional_content]
@raise_on_encrypted = options[:raise_on_encrypted]
@check_stream_length = options[:check_stream_length]
end

# parse the data in the new parser (the data already set through the initialize / new method)
Expand Down Expand Up @@ -363,7 +365,11 @@ def _parse_
# advance by the publshed stream length (if any)
old_pos = @scanner.pos
if(out.last.is_a?(Hash) && out.last[:Length].is_a?(Integer) && out.last[:Length] > 2)
@scanner.pos += out.last[:Length] - 2
if @check_stream_length
advance_pos_with_length_check(@scanner, out.last[:Length], out.last)
else
@scanner.pos += out.last[:Length] - 2
end
end

# the following was dicarded because some PDF files didn't have an EOL marker as required
Expand Down Expand Up @@ -507,6 +513,29 @@ def _parse_

protected

def advance_pos_with_length_check(scanner, length, obj)
endstream = 'endstream'
orig_pos = scanner.pos
if scanner.rest_size > length
scanner.pos += length
if scanner.check(endstream)
scanner.pos -= 2
return
end
warn "Invalid length no #{endstream} found - object: #{obj}!"
else
warn "Invalid length in stream points out of the file - object: #{obj}!"
end
scanner.pos = orig_pos
skipped = scanner.skip_until(/endstream/)
if skipped
correct_len = skipped - endstream.length
scanner.pos -= endstream.length + 2
else
raise ParsingError, "Parsing Error: PDF file error - a stream object with invalid length of #{length} for object #{out} and no #{endstream} found after, to work around it"
end
end

# resets cataloging and pages
def catalog_pages(catalogs = nil, inheritance_hash = {})
unless catalogs
Expand Down