Skip to content

Commit

Permalink
improve handling of empty PDF Names
Browse files Browse the repository at this point in the history
* '/' is a valid PDF name, we really don't want to throw a
  MalformedPDFError
  • Loading branch information
yob committed Jan 31, 2012
1 parent 4ae9208 commit 26de193
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
10 changes: 9 additions & 1 deletion lib/pdf/reader/buffer.rb
Expand Up @@ -314,7 +314,7 @@ def prepare_regular_token
@tokens << chr
tok = ""
break
when "\x28", "\x5B", "\x7B", "\x2F"
when "\x28", "\x5B", "\x7B"
# opening delimiter, start of new token
@tokens << tok if tok.size > 0
@tokens << chr
Expand All @@ -326,6 +326,14 @@ def prepare_regular_token
@tokens << chr
tok = ""
break
when "\x2F"
# PDF name, start of new token
@tokens << tok if tok.size > 0
@tokens << chr
next_char = peek_char
@tokens << "" if chr == "/" && (next_char == " " || next_char.nil?)
tok = ""
break
else
tok << chr
end
Expand Down
1 change: 1 addition & 0 deletions lib/pdf/reader/parser.rb
Expand Up @@ -130,6 +130,7 @@ def dictionary
# reads a PDF name from the buffer and converts it to a Ruby Symbol
def pdf_name
tok = @buffer.token
tok = " " if tok == "" && RUBY_VERSION < "1.9"
tok.gsub!(/#([A-Fa-f0-9]{2})/) do |match|
match[1, 2].hex.chr
end
Expand Down
10 changes: 10 additions & 0 deletions spec/buffer_spec.rb
Expand Up @@ -89,6 +89,16 @@
buf.token.should be_nil
end

it "should correctly return two empty name tokens" do
buf = parse_string("/ /")

buf.token.should eql("/")
buf.token.should eql("")
buf.token.should eql("/")
buf.token.should eql("")
buf.token.should be_nil
end

it "should tokenise a dict correctly" do
buf = parse_string("/Registry (Adobe) /Ordering (Japan1) /Supplement")
buf.token.should eql("/")
Expand Down
30 changes: 25 additions & 5 deletions spec/parser_spec.rb
Expand Up @@ -19,11 +19,31 @@
parse_string("/Ja#6des").parse_token.should eql(:"James")
end

# '/' is a valid PDF name, but :"" is not a valid ruby symbol.
# How should I handle this?
it "should parse an empty name correctly" #do
#parse_string("/").parse_token.should eql(:"")
#end
# '/' is a valid PDF name, but :"" is only a valid ruby symbol in 1.9
# On 1.8 VMs the best I can do is a string with a single space. This
# is bound to cause trouble but it's better than treating empty PDF
# Names as a syntax error
if RUBY_VERSION >= "1.9"
it "should parse an empty name correctly" do
parse_string("/").parse_token.should eql(:"")
end

it "should parse two empty names correctly" do
parser = parse_string("/ /")
parser.parse_token.should eql(:"")
parser.parse_token.should eql(:"")
end
else
it "should parse an empty name correctly" do
parse_string("/").parse_token.should eql(:" ")
end

it "should parse two empty names correctly" do
parser = parse_string("/ /")
parser.parse_token.should eql(:" ")
parser.parse_token.should eql(:" ")
end
end

it "should parse booleans correctly" do
parse_string("true").parse_token.should be_true
Expand Down

0 comments on commit 26de193

Please sign in to comment.