diff --git a/NEWS b/NEWS index b4e6149982d5..211811e20da7 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,8 @@ PHP NEWS (Artem Ukrainskiy) . Fixed array_udiff_assoc() and array_uintersect_assoc() clobbering the user comparison callback. (lacatoire) + . Fixed bug GH-26041 (convert.quoted-printable-encode splits UTF-8 + characters across lines in text mode). (Ilia Alshanetsky) - Date: . Fixed bug GH-26032 (DateTimeZone->getTransitions() includes too-early diff --git a/ext/standard/filters.c b/ext/standard/filters.c index c2042f5d90ef..2d9de626b965 100644 --- a/ext/standard/filters.c +++ b/ext/standard/filters.c @@ -784,7 +784,15 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins line_ccnt--; CONSUME_CHAR(ps, icnt, lb_ptr, lb_cnt); } else { - if (line_ccnt < 4 && inst->lbchars != NULL) { + unsigned int encoded_len = 3; + if (!(opts & PHP_CONV_QPRINT_OPT_BINARY) && c >= 0xc2 && c <= 0xf4) { + unsigned int utf8_len = 2 + (c >= 0xe0) + (c >= 0xf0); + if (3 * utf8_len < inst->line_len) { + encoded_len = 3 * utf8_len; + } + } + + if (line_ccnt <= encoded_len && inst->lbchars != NULL) { if (ocnt < inst->lbchars_len + 1) { err = PHP_CONV_ERR_TOO_BIG; break; diff --git a/ext/standard/tests/filters/gh26041.phpt b/ext/standard/tests/filters/gh26041.phpt new file mode 100644 index 000000000000..01f5b88d3ff6 --- /dev/null +++ b/ext/standard/tests/filters/gh26041.phpt @@ -0,0 +1,47 @@ +--TEST-- +GH-26041 (convert.quoted-printable-encode splits UTF-8 characters across lines) +--FILE-- + $width, 'line-break-chars' => "\r\n", 'binary' => $binary]; + + $read = fopen('php://memory', 'w+'); + fwrite($read, $input); + rewind($read); + stream_filter_append($read, 'convert.quoted-printable-encode', STREAM_FILTER_READ, $options); + $encoded = stream_get_contents($read); + echo $encoded, "\n"; + + $write = fopen('php://memory', 'w+'); + stream_filter_append($write, 'convert.quoted-printable-encode', STREAM_FILTER_WRITE, $options); + foreach (str_split($input) as $byte) { + fwrite($write, $byte); + } + rewind($write); + if (stream_get_contents($write) !== $encoded) { + echo "Write filter produced different output\n"; + } +} + +foreach ([[7, "\u{00A0}"], [4, "\u{20AC}"], [1, "\u{1F600}"]] as [$prefix, $char]) { + $exactFit = str_repeat('A', $prefix) . $char; + encode($exactFit . "\r\nA" . $exactFit, 14); +} +encode("A\u{1F600}", 7); +encode("AAAAAAAA\u{00A0}", 14, binary: true); +?> +--EXPECT-- +AAAAAAA=C2=A0 +AAAAAAAA= +=C2=A0 +AAAA=E2=82=AC +AAAAA= +=E2=82=AC +A=F0=9F=98=80 +AA= +=F0=9F=98=80 +A=F0= +=9F=98= +=80 +AAAAAAAA=C2= +=A0