Skip to content

Commit 3ef8817

Browse files
committed
MFH(r-1.55) Fix 'soft line break' handling in convert.quoted-printable-decode
1 parent cfaf640 commit 3ef8817

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

ext/standard/filters.c

+24-5
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,18 @@ static php_conv_err_t php_conv_qprint_decode_convert(php_conv_qprint_decode *ins
10311031
scan_stat = 4;
10321032
ps++, icnt--;
10331033
break;
1034+
} else if (!inst->lbchars && lb_cnt == 0 && *ps == '\r') {
1035+
/* auto-detect line endings, looks like network line ending \r\n (could be mac \r) */
1036+
lb_cnt++;
1037+
scan_stat = 5;
1038+
ps++, icnt--;
1039+
break;
1040+
} else if (!inst->lbchars && lb_cnt == 0 && *ps == '\n') {
1041+
/* auto-detect line endings, looks like unix-lineendings, not to spec, but it is seem in the wild, a lot */
1042+
lb_cnt = lb_ptr = 0;
1043+
scan_stat = 0;
1044+
ps++, icnt--;
1045+
break;
10341046
} else if (lb_cnt < inst->lbchars_len &&
10351047
*ps == (unsigned char)inst->lbchars[lb_cnt]) {
10361048
lb_cnt++;
@@ -1088,7 +1100,16 @@ static php_conv_err_t php_conv_qprint_decode_convert(php_conv_qprint_decode *ins
10881100
} break;
10891101

10901102
case 5: {
1091-
if (lb_cnt >= inst->lbchars_len) {
1103+
if (!inst->lbchars && lb_cnt == 1 && *ps == '\n') {
1104+
/* auto-detect soft line breaks, found network line break */
1105+
lb_cnt = lb_ptr = 0;
1106+
scan_stat = 0;
1107+
ps++, icnt--; /* consume \n */
1108+
} else if (!inst->lbchars && lb_cnt > 0) {
1109+
/* auto-detect soft line breaks, found mac line break */
1110+
lb_cnt = lb_ptr = 0;
1111+
scan_stat = 0;
1112+
} else if (lb_cnt >= inst->lbchars_len) {
10921113
/* soft line break */
10931114
lb_cnt = lb_ptr = 0;
10941115
scan_stat = 0;
@@ -1408,12 +1429,10 @@ static php_conv *php_conv_open(int conv_mode, const HashTable *options, int pers
14081429
size_t lbchars_len;
14091430

14101431
if (options != NULL) {
1432+
/* If line-break-chars are not specified, filter will attempt to detect line endings (\r, \n, or \r\n) */
14111433
GET_STR_PROP(options, lbchars, lbchars_len, "line-break-chars", 0);
1412-
if (lbchars == NULL) {
1413-
lbchars = pestrdup("\r\n", 0);
1414-
lbchars_len = 2;
1415-
}
14161434
}
1435+
14171436
retval = pemalloc(sizeof(php_conv_qprint_decode), persistent);
14181437
if (lbchars != NULL) {
14191438
if (php_conv_qprint_decode_ctor((php_conv_qprint_decode *)retval, lbchars, lbchars_len, 1, persistent)) {

0 commit comments

Comments
 (0)