Skip to content

Commit a1bfced

Browse files
committed
Merge branch 'oscardssmith-patch-1'
2 parents 4fc7f9d + 77629e6 commit a1bfced

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

PyPDF2/generic.py

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -340,55 +340,56 @@ def readStringFromStream(stream):
340340
break
341341
elif tok == b_("\\"):
342342
tok = stream.read(1)
343-
if tok == b_("n"):
344-
tok = b_("\n")
345-
elif tok == b_("r"):
346-
tok = b_("\r")
347-
elif tok == b_("t"):
348-
tok = b_("\t")
349-
elif tok == b_("b"):
350-
tok = b_("\b")
351-
elif tok == b_("f"):
352-
tok = b_("\f")
353-
elif tok == b_("c"):
354-
tok = b_("\c")
355-
elif tok == b_("("):
356-
tok = b_("(")
357-
elif tok == b_(")"):
358-
tok = b_(")")
359-
elif tok == b_("/"):
360-
tok = b_("/")
361-
elif tok == b_("\\"):
362-
tok = b_("\\")
363-
elif tok in (b_(" "), b_("/"), b_("%"), b_("<"), b_(">"), b_("["),
364-
b_("]"), b_("#"), b_("_"), b_("&"), b_('$')):
365-
# odd/unnessecary escape sequences we have encountered
366-
tok = b_(tok)
367-
elif tok.isdigit():
368-
# "The number ddd may consist of one, two, or three
369-
# octal digits; high-order overflow shall be ignored.
370-
# Three octal digits shall be used, with leading zeros
371-
# as needed, if the next character of the string is also
372-
# a digit." (PDF reference 7.3.4.2, p 16)
373-
for i in range(2):
374-
ntok = stream.read(1)
375-
if ntok.isdigit():
376-
tok += ntok
377-
else:
378-
break
379-
tok = b_(chr(int(tok, base=8)))
380-
elif tok in b_("\n\r"):
381-
# This case is hit when a backslash followed by a line
382-
# break occurs. If it's a multi-char EOL, consume the
383-
# second character:
384-
tok = stream.read(1)
385-
if not tok in b_("\n\r"):
386-
stream.seek(-1, 1)
387-
# Then don't add anything to the actual string, since this
388-
# line break was escaped:
389-
tok = b_('')
390-
else:
391-
raise utils.PdfReadError(r"Unexpected escaped string: %s" % tok)
343+
ESCAPE_DICT = {b_("n") : b_("\n"),
344+
b_("r") : b_("\r"),
345+
b_("t") : b_("\t"),
346+
b_("b") : b_("\b"),
347+
b_("f") : b_("\f"),
348+
b_("c") : b_("\c"),
349+
b_("(") : b_("("),
350+
b_(")") : b_(")"),
351+
b_("/") : b_("/"),
352+
b_("\\") : b_("\\"),
353+
b_(" ") : b_(" "),
354+
b_("/") : b_("/"),
355+
b_("%") : b_("%"),
356+
b_("<") : b_("<"),
357+
b_(">") : b_(">"),
358+
b_("[") : b_("["),
359+
b_("]") : b_("]"),
360+
b_("#") : b_("#"),
361+
b_("_") : b_("_"),
362+
b_("&") : b_("&"),
363+
b_('$') : b_('$'),
364+
}
365+
try:
366+
tok = ESCAPE_DICT[tok]
367+
except KeyError:
368+
if tok.isdigit():
369+
# "The number ddd may consist of one, two, or three
370+
# octal digits; high-order overflow shall be ignored.
371+
# Three octal digits shall be used, with leading zeros
372+
# as needed, if the next character of the string is also
373+
# a digit." (PDF reference 7.3.4.2, p 16)
374+
for i in range(2):
375+
ntok = stream.read(1)
376+
if ntok.isdigit():
377+
tok += ntok
378+
else:
379+
break
380+
tok = b_(chr(int(tok, base=8)))
381+
elif tok in b_("\n\r"):
382+
# This case is hit when a backslash followed by a line
383+
# break occurs. If it's a multi-char EOL, consume the
384+
# second character:
385+
tok = stream.read(1)
386+
if not tok in b_("\n\r"):
387+
stream.seek(-1, 1)
388+
# Then don't add anything to the actual string, since this
389+
# line break was escaped:
390+
tok = b_('')
391+
else:
392+
raise utils.PdfReadError(r"Unexpected escaped string: %s" % tok)
392393
txt += tok
393394
return createStringObject(txt)
394395

0 commit comments

Comments
 (0)