From 98abe9aa24e9f881d4b434a11608cf9952a93a4f Mon Sep 17 00:00:00 2001 From: Jon Iles Date: Thu, 14 Jun 2018 11:24:04 +0100 Subject: [PATCH] Gracefully handle malformed hex bytes. --- .../rtfparserkit/parser/raw/RawRtfParser.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/RTF Parser Kit/src/com/rtfparserkit/parser/raw/RawRtfParser.java b/RTF Parser Kit/src/com/rtfparserkit/parser/raw/RawRtfParser.java index 1d460b8..67ab0d1 100644 --- a/RTF Parser Kit/src/com/rtfparserkit/parser/raw/RawRtfParser.java +++ b/RTF Parser Kit/src/com/rtfparserkit/parser/raw/RawRtfParser.java @@ -135,7 +135,22 @@ private void handleCharacterByte(int ch) throws IOException { throw new IllegalStateException("Unexpected end of file"); } - b += HexUtils.parseHexDigit(ch); + + // Have encountered malformed RTF where only a single hex digit + // has been supplied. e.g. \'AA\'B\'CC so we hit the next \ + // rather than getting a hex digit. Try to handle this specific + // case gracefully by unreading the next character and working with + // the single digit we have. + if (ch == '\\') + { + b = b >> 4; + source.unread(ch); + } + else + { + b += HexUtils.parseHexDigit(ch); + } + buffer.add(b); parsingHex = false; }