Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.4.30

- Standard:
. Fixed bug GH-25956 (quoted-printable stream filter leaves trailing
whitespace unencoded with custom line breaks). (Sebastian Turner,
Ilia Alshanetsky)


17 Dec 2026, PHP 8.4.29

Expand Down
19 changes: 10 additions & 9 deletions ext/standard/filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,19 +650,18 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins
unsigned int lb_cnt;
unsigned int trail_ws;
int opts;
bool flush;
static const char qp_digits[] = "0123456789ABCDEF";

line_ccnt = inst->line_ccnt;
opts = inst->opts;
lb_ptr = inst->lb_ptr;
lb_cnt = inst->lb_cnt;

if (in_pp == NULL || in_left_p == NULL) {
return PHP_CONV_ERR_SUCCESS;
}
flush = (in_pp == NULL || in_left_p == NULL);

ps = (unsigned char *)(*in_pp);
icnt = *in_left_p;
ps = flush ? NULL : (unsigned char *)(*in_pp);
icnt = flush ? 0 : *in_left_p;
pd = (unsigned char *)(*out_pp);
ocnt = *out_left_p;
trail_ws = 0;
Expand Down Expand Up @@ -696,7 +695,7 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins
}
}

if (lb_ptr >= lb_cnt && icnt == 0) {
if (icnt == 0 && (lb_ptr >= lb_cnt || !flush)) {
break;
}

Expand Down Expand Up @@ -734,7 +733,7 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins
ps2 = ps;
trail_ws = 1;

for (j = icnt - 1; j > 0; j--, ps2++) {
for (j = icnt; j > 1; j--, ps2++) {
if (*ps2 == inst->lbchars[lb_cnt2]) {
lb_cnt2++;
if (lb_cnt2 >= inst->lbchars_len) {
Expand Down Expand Up @@ -813,8 +812,10 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins
}
}

*in_pp = (const char *)ps;
*in_left_p = icnt;
if (!flush) {
*in_pp = (const char *)ps;
*in_left_p = icnt;
}
*out_pp = (char *)pd;
*out_left_p = ocnt;
inst->line_ccnt = line_ccnt;
Expand Down
43 changes: 43 additions & 0 deletions ext/standard/tests/filters/gh25956.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
GH-25956 quoted-printable encode preserves trailing whitespace with a partial line break
--FILE--
<?php
function encodeRead(string $input, string $lineBreak): string {
$stream = fopen('php://temp', 'w+');
fwrite($stream, $input);
rewind($stream);
stream_filter_append($stream, 'convert.quoted-printable-encode', STREAM_FILTER_READ, [
'line-break-chars' => $lineBreak,
'line-length' => 76,
]);
$encoded = stream_get_contents($stream);
fclose($stream);
return bin2hex($encoded);
}

function encodeWrite(string $input, string $lineBreak): string {
$stream = fopen('php://temp', 'w+');
$filter = stream_filter_append($stream, 'convert.quoted-printable-encode', STREAM_FILTER_WRITE, [
'line-break-chars' => $lineBreak,
'line-length' => 76,
]);
fwrite($stream, $input);
stream_filter_remove($filter);
rewind($stream);
$encoded = stream_get_contents($stream);
fclose($stream);
return bin2hex($encoded);
}

echo "read trailing space: ", encodeRead("abc ", " X"), "\n";
echo "read trailing tab: ", encodeRead("abc\t", "\tX"), "\n";
echo "read two pending spaces: ", encodeRead("abc ", " X"), "\n";
echo "read complete line break: ", encodeRead("abc Xdef", " X"), "\n";
echo "write trailing space: ", encodeWrite("abc ", " X"), "\n";
?>
--EXPECT--
read trailing space: 6162633d3230
read trailing tab: 6162633d3039
read two pending spaces: 6162633d32303d3230
read complete line break: 6162632058646566
write trailing space: 6162633d3230
35 changes: 35 additions & 0 deletions ext/standard/tests/filters/qp_encode_partial_line_break.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
convert.quoted-printable-encode keeps a partial line break sequence until more input or the end of the stream
--FILE--
<?php
function encode(array $chunks): string {
$fp = fopen('php://temp', 'w+');
$filter = stream_filter_append($fp, 'convert.quoted-printable-encode', STREAM_FILTER_WRITE, [
'line-length' => 76,
'line-break-chars' => "\r\n",
]);
foreach ($chunks as $chunk) {
fwrite($fp, $chunk);
}
stream_filter_remove($filter);
rewind($fp);
return stream_get_contents($fp);
}

foreach ([["a\r"], ["a", "=\r"], ["Hello=\r", "\nWorld"], ["Hello=\r\n"], ["=\r\n\r", "\n"]] as $chunks) {
$encoded = encode($chunks);
echo json_encode($encoded), "\n";
var_dump(quoted_printable_decode($encoded) === implode('', $chunks));
}
?>
--EXPECT--
"a=0D"
bool(true)
"a=3D=0D"
bool(true)
"Hello=3D\r\nWorld"
bool(true)
"Hello=3D\r\n"
bool(true)
"=3D\r\n\r\n"
bool(true)
Loading