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().
This commit is contained in:
Nico Weber 2024-02-22 09:47:58 -05:00 committed by Tim Flynn
parent 783b1d1c11
commit b258ba2767
Notes: sideshowbarker 2024-07-17 07:31:31 +09:00

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Hex.h>
#include <AK/ScopeGuard.h>
#include <LibPDF/CommonNames.h>
#include <LibPDF/Document.h>
@ -223,11 +224,7 @@ PDFErrorOr<NonnullRefPtr<NameObject>> 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<char>(hex_value));
continue;
@ -374,13 +371,7 @@ PDFErrorOr<ByteString> 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<char>(hex_value));