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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ PHP NEWS
. Use recommended TYPE, HELP order for OpenMetrics metadata.
(Marcel Hernandez)

- FTP:
. Improved performance of downloads in FTP_ASCII mode by writing each
received buffer at once. (Alexander Danilov)

- GD:
. Fixed imagecolormatch() and imagejpeg() ValueError messages. (lacatoire)

Expand Down
4 changes: 4 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ PHP 8.7 UPGRADE NOTES
. Parsing HTML with Dom\HTMLDocument or innerHTML uses much less memory for
small inputs.

- FTP:
. Improved performance of downloads in FTP_ASCII mode by writing each
received buffer at once.

- PDO:
. Improved performance of fetching rows with PDO::FETCH_OBJ.

Expand Down
90 changes: 39 additions & 51 deletions ext/ftp/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,39 @@ bool ftp_pasv(ftpbuf_t *ftp, int pasv)
return true;
}

static void ftp_write_ascii_chunk(php_stream *stream, char *buf, size_t len, bool *pending_cr)
{
const char *src = buf;
const char *end = buf + len;
const char *cr;
char *dst = buf;

ZEND_ASSERT(len != 0);

if (*pending_cr && *src != '\n') {
php_stream_putc(stream, '\r');
}

*pending_cr = end[-1] == '\r';
if (*pending_cr) {
end--;
}

while ((cr = memchr(src, '\r', end - src))) {
bool is_crlf = cr + 1 < end && cr[1] == '\n';
size_t chunk_len = cr - src + !is_crlf;

memmove(dst, src, chunk_len);
dst += chunk_len;
src = cr + 1;
}

memmove(dst, src, end - src);
dst += end - src;

php_stream_write(stream, buf, dst - buf);
}

bool ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos)
{
databuf_t *data = NULL;
Expand Down Expand Up @@ -857,48 +890,15 @@ bool ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_
}

if (type == FTPTYPE_ASCII) {
#ifndef PHP_WIN32
char *s;
#endif
char *ptr = data->buf;
char *e = ptr + rcvd;
/* logic depends on the OS EOL
* Win32 -> \r\n
* Everything Else \n
*/
#ifdef PHP_WIN32
php_stream_write(outstream, ptr, (e - ptr));
ptr = e;
php_stream_write(outstream, data->buf, rcvd);
#else
if (pending_cr) {
pending_cr = false;
if (*ptr == '\n') {
php_stream_putc(outstream, '\n');
ptr++;
} else {
php_stream_putc(outstream, '\r');
}
}
while (e > ptr && (s = memchr(ptr, '\r', (e - ptr)))) {
if (s + 1 < e) {
if (*(s + 1) == '\n') {
php_stream_write(outstream, ptr, (s - ptr));
php_stream_putc(outstream, '\n');
ptr = s + 2;
} else {
php_stream_write(outstream, ptr, (s - ptr) + 1);
ptr = s + 1;
}
} else {
php_stream_write(outstream, ptr, (s - ptr));
pending_cr = true;
ptr = s + 1;
}
}
ftp_write_ascii_chunk(outstream, data->buf, rcvd, &pending_cr);
#endif
if (ptr < e) {
php_stream_write(outstream, ptr, (e - ptr));
}
} else if (rcvd != php_stream_write(outstream, data->buf, rcvd)) {
goto bail;
}
Expand Down Expand Up @@ -2096,7 +2096,7 @@ int ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const siz

ftp->data = data;
ftp->stream = outstream;
ftp->lastch = 0;
ftp->pending_cr = false;
ftp->nb = true;

return (ftp_nb_continue_read(ftp));
Expand All @@ -2109,8 +2109,6 @@ int ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const siz
int ftp_nb_continue_read(ftpbuf_t *ftp)
{
databuf_t *data = NULL;
char *ptr;
char lastch;
size_t rcvd;
ftptype_t type;

Expand All @@ -2129,32 +2127,22 @@ int ftp_nb_continue_read(ftpbuf_t *ftp)
ftp->in_use = true;
type = ftp->type;

lastch = ftp->lastch;
if ((rcvd = my_recv(ftp, data->fd, data->buf, FTP_BUFSIZE))) {
if (rcvd == (size_t)-1) {
goto bail;
}

if (type == FTPTYPE_ASCII) {
for (ptr = data->buf; rcvd; rcvd--, ptr++) {
if (lastch == '\r' && *ptr != '\n') {
php_stream_putc(ftp->stream, '\r');
}
if (*ptr != '\r') {
php_stream_putc(ftp->stream, *ptr);
}
lastch = *ptr;
}
ftp_write_ascii_chunk(ftp->stream, data->buf, rcvd, &ftp->pending_cr);
} else if (rcvd != php_stream_write(ftp->stream, data->buf, rcvd)) {
goto bail;
}

ftp->lastch = lastch;
ftp->in_use = false;
return PHP_FTP_MOREDATA;
}

if (type == FTPTYPE_ASCII && lastch == '\r') {
if (type == FTPTYPE_ASCII && ftp->pending_cr) {
php_stream_putc(ftp->stream, '\r');
}

Expand Down Expand Up @@ -2217,7 +2205,7 @@ int ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_strea
}
ftp->data = data;
ftp->stream = instream;
ftp->lastch = 0;
ftp->pending_cr = false;
ftp->nb = true;

return (ftp_nb_continue_write(ftp));
Expand Down
2 changes: 1 addition & 1 deletion ext/ftp/ftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ typedef struct ftpbuf
php_stream *stream; /* output stream for "nonblocking" transfers */
bool nb; /* "nonblocking" transfer in progress */
bool in_use; /* engine transfer in progress; blocks re-entrant ftp_close */
char lastch; /* last char of previous call */
bool pending_cr; /* CR held back by an ASCII "nonblocking" download */
bool direction; /* recv = 0 / send = 1 */
bool closestream;/* close or not close stream */
#ifdef HAVE_FTP_SSL
Expand Down
40 changes: 40 additions & 0 deletions ext/ftp/tests/ftp_nb_get_ascii.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
ftp_nb_get() ASCII mode: CRLF folds to LF, bare CR is preserved
--EXTENSIONS--
ftp
pcntl
--FILE--
<?php
require 'server.inc';

$ftp = ftp_connect('127.0.0.1', $port);
ftp_login($ftp, 'user', 'pass');
$ftp or die("Couldn't connect to the server");

$local = __DIR__ . "/nb_get_ascii_out.txt";

$tests = [
'crlf_boundary' => str_repeat("A", 4095) . "\n" . str_repeat("B", 10),
'bare_cr' => "line1\nba\rre\nend" . str_repeat("X", 4078) . "\r" . str_repeat("Y", 10),
'trailing_cr' => "trail\r",
];

foreach ($tests as $remote => $expected) {
$ret = ftp_nb_get($ftp, $local, $remote, FTP_ASCII);
while ($ret === FTP_MOREDATA) {
$ret = ftp_nb_continue($ftp);
}
var_dump($ret === FTP_FINISHED, file_get_contents($local) === $expected);
}
?>
--CLEAN--
<?php
@unlink(__DIR__ . "/nb_get_ascii_out.txt");
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Loading