From b258ba2767f8d6cb61b2fa01cd4b32a1cc0bd461 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Thu, 22 Feb 2024 09:47:58 -0500 Subject: [PATCH] LibPDF: Use decode_hex_digit() more For `:#xx` in names, we now also handle lower-case hex digits. The spec is silent on the case of these hex digits. Our previous check (isxdigit(), and now is_ascii_hex_digit()) lets through lower-case hex digits, so it seems better to handle them rather than computing e.g. `'a' - 'A' + 10` (== 42 -- off by 32!). I don't know if this has any visible effect on any files, but it's more correct, and less code, and the code looks more like the code in Filter::decode_ascii_hex(). --- Userland/Libraries/LibPDF/Parser.cpp | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibPDF/Parser.cpp b/Userland/Libraries/LibPDF/Parser.cpp index 12fcf2e4b6..db1e85d312 100644 --- a/Userland/Libraries/LibPDF/Parser.cpp +++ b/Userland/Libraries/LibPDF/Parser.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -223,11 +224,7 @@ PDFErrorOr> Parser::parse_name() auto ch = m_reader.consume(); VERIFY(is_ascii_hex_digit(ch)); hex_value *= 16; - if (ch <= '9') { - hex_value += ch - '0'; - } else { - hex_value += ch - 'A' + 10; - } + hex_value += decode_hex_digit(ch); } builder.append(static_cast(hex_value)); continue; @@ -374,13 +371,7 @@ PDFErrorOr Parser::parse_hex_string() return error("character in hex string isn't hex digit"); hex_value *= 16; - if (ch <= '9') { - hex_value += ch - '0'; - } else if (ch >= 'A' && ch <= 'F') { - hex_value += ch - 'A' + 10; - } else { - hex_value += ch - 'a' + 10; - } + hex_value += decode_hex_digit(ch); } builder.append(static_cast(hex_value));