Skip to content

Commit 9978053

Browse files
committed
Use ctype_* functions to check valid characters
1 parent fc3b7cf commit 9978053

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/Parser.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ private static function parseString(ParsingInput $input): string
272272
}
273273
} elseif ($char === '"') {
274274
return $output;
275-
} elseif (ord($char) <= 0x1f || ord($char) >= 0x7f) {
275+
} elseif (!ctype_print($char)) {
276276
throw new ParseException('Invalid character in string at position ' . ($input->position() - 1));
277277
}
278278

@@ -298,18 +298,21 @@ private static function parseDisplayString(ParsingInput $string): DisplayString
298298
while (!$string->empty()) {
299299
$char = $string->consumeChar();
300300

301-
if (ord($char) <= 0x1f || ord($char) >= 0x7f) {
301+
if (!ctype_print($char)) {
302302
throw new ParseException(
303303
'Invalid character in display string at position ' . ($string->position() - 1)
304304
);
305305
} elseif ($char === '%') {
306-
try {
307-
$encodedString .= '%' . $string->consumeRegex('/^[0-9a-f]{2}/');
308-
} catch (\RuntimeException) {
306+
if ($string->remainingLength() < 2) {
307+
break;
308+
}
309+
$encodedChar = $string->consume(2);
310+
if (!ctype_xdigit($encodedChar) || ctype_upper($encodedChar)) {
309311
throw new ParseException(
310312
'Invalid hex values in display string at position ' . ($string->position() - 1)
311313
);
312314
}
315+
$encodedString .= '%' . $encodedChar;
313316
} elseif ($char === '"') {
314317
$displayString = new DisplayString(rawurldecode($encodedString));
315318
// An invalid UTF-8 subject will cause the preg_* function to match nothing.

src/Serializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private static function serializeDecimal(float $value): string
205205

206206
private static function serializeString(string $value): string
207207
{
208-
if (preg_match('/[^\x20-\x7E]/i', $value)) {
208+
if (!empty($value) && !ctype_print($value)) {
209209
throw new SerializeException("Invalid characters in string");
210210
}
211211

0 commit comments

Comments
 (0)