Skip to content

Commit

Permalink
Fixed the UTF-8 and long path support in the streams on Windows.
Browse files Browse the repository at this point in the history
Since long the default PHP charset is UTF-8, however the Windows part is
out of step with this important point. The current implementation in PHP
doesn't technically permit to handle UTF-8 filepath and several other
things. Till now, only the ANSI compatible APIs are being used. Here is more
about it

https://msdn.microsoft.com/en-us/library/windows/desktop/dd317752%28v=vs.85%29.aspx

The patch fixes not only issues with multibyte filenames under
incompatible codepages, but indirectly also issues with some other multibyte
encodings like BIG5, Shift-JIS, etc. by providing a clean way to access
filenames in UTF-8. Below is a small list of issues from the bug tracker,
that are getting fixed:

https://bugs.php.net/63401
https://bugs.php.net/41199
https://bugs.php.net/50203
https://bugs.php.net/71509
https://bugs.php.net/64699
https://bugs.php.net/64506
https://bugs.php.net/30195
https://bugs.php.net/65358
https://bugs.php.net/61315
https://bugs.php.net/70943
https://bugs.php.net/70903
https://bugs.php.net/63593
https://bugs.php.net/54977
https://bugs.php.net/54028
https://bugs.php.net/43148
https://bugs.php.net/30730
https://bugs.php.net/33350
https://bugs.php.net/35300
https://bugs.php.net/46990
https://bugs.php.net/61309
https://bugs.php.net/69333
https://bugs.php.net/45517
https://bugs.php.net/70551
https://bugs.php.net/50197
https://bugs.php.net/72200
https://bugs.php.net/37672

Yet more related tickets can for sure be found - on bugs.php.net, Stackoverflow
and Github. Some of the bugs are pretty recent, some descend to early
2000th, but  the user comments in there last even till today. Just for example,
bug #30195 was opened in 2004, the latest comment in there was made in 2014. It
is certain, that these bugs descend not only to pure PHP use cases, but get also
redirected from the popular PHP based projects. Given the modern systems (and
those supported by PHP) are always based on NTFS, there is no excuse to keep
these issues unresolved.

The internalization approach on Windows is in many ways different from
UNIX and Linux, while it supports and is based on Unicode. It depends on the
current system code page, APIs used and exact kind how the binary was compiled
The locale doesn't affect the way Unicode or ANSI API work. PHP in particular
is being compiled without _UNICODE defined and this is conditioned by the
way we handle strings. Here is more about it

https://msdn.microsoft.com/en-us/library/tsbaswba.aspx

However, with any system code page ANSI functions automatically convert
paths to UTF-16. Paths in some encodings incompatible with the
current system code page, won't work correctly with ANSI APIs. PHP
till now only uses the ANSI Windows APIs.

For example, on a system with the current code page 1252, the paths
in cp1252 are supported and transparently converted to UTF-16 by the
ANSI functions. Once one wants to handle a filepath encoded with cp932 on
that particular system, an ANSI or a POSIX compatible function used in
PHP will produce an erroneous result. When trying to convert that cp932 path
to UTF-8 and passing to the ANSI functions, an ANSI function would
likely interpret the UTF-8 string as some string in the current code page and
create a filepath that represents every single byte of the UTF-8 string.
These behaviors are not only broken but also disregard the documented
INI settings.

This patch solves the issies with the multibyte paths on Windows by
intelligently enforcing the usage of the Unicode aware APIs. For
functions expect Unicode (fe CreateFileW, FindFirstFileW, etc.), arguments
will be converted to UTF-16 wide chars. For functions returning Unicode
aware data (fe GetCurrentDirectoryW, etc.), resulting wide string is
converted back to char's depending on the current PHP charset settings,
either to the current ANSI codepage (this is the behavior prior to this patch)
or to UTF-8 (the default behavior).

In a particular case, users might have to explicitly set
internal_encoding or default_charset, if filenames in ANSI codepage are
necessary. Current tests show no regressions and witness that this will be an
exotic case, the current default UTF-8 encoding is compatible with any
supported system. The dependency libraries are long switching to Unicode APIs,
so some tests were also added for extensions not directly related to streams.
At large, the patch brings over 150 related tests into the core. Those target
and was run on various environments with European, Asian, etc. codepages.
General PHP frameworks was tested and showed no regressions.

The impact on the current C code base is low, the most places affected
are the Windows only places in the three files tsrm_win32.c, zend_virtual_cwd.c
and plain_wrapper.c. The actual implementation of the most of the wide
char supporting functionality is in win32/ioutil.* and win32/codepage.*,
several  low level functionsare extended in place to avoid reimplementation for
now. No performance impact was sighted. As previously mentioned, the ANSI APIs
used prior the patch perform Unicode conversions internally. Using the
Unicode  APIs directly while doing custom conversions just retains the status
quo. The ways to optimize it are open (fe. by implementing caching for the
strings converted to wide variants).

The long path implementation is user transparent. If a path exceeds the
length of _MAX_PATH, it'll be automatically prefixed with \\?\. The MAXPATHLEN
is set to 2048 bytes.

Appreciation to Pierre Joye, Matt Ficken, @algo13 and others for tips, ideas
and testing.

Thanks.
  • Loading branch information
weltling committed Jun 20, 2016
1 parent 3abd9c3 commit 3d3f11e
Show file tree
Hide file tree
Showing 266 changed files with 1,045,292 additions and 206 deletions.
5 changes: 4 additions & 1 deletion TSRM/tsrm_config_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ char *alloca ();
#endif

#ifndef MAXPATHLEN
# ifdef PATH_MAX
# if _WIN32
# include "win32/ioutil.h"
# define MAXPATHLEN PHP_WIN32_IOUTIL_MAXPATHLEN
# elif PATH_MAX
# define MAXPATHLEN PATH_MAX
# elif defined(MAX_PATH)
# define MAXPATHLEN MAX_PATH
Expand Down
92 changes: 75 additions & 17 deletions TSRM/tsrm_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <Sddl.h>
#include "tsrm_win32.h"
#include "zend_virtual_cwd.h"
#include "win32/ioutil.h"

#ifdef ZTS
static ts_rsrc_id win32_globals_id;
Expand Down Expand Up @@ -208,28 +209,42 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode)
BYTE * psec_desc = NULL;
BOOL fAccess = FALSE;

PHP_WIN32_IOUTIL_INIT_W(pathname)
if (!pathw) {
return -1;
}

realpath_cache_bucket * bucket = NULL;
char * real_path = NULL;

if (mode == 1 /*X_OK*/) {
DWORD type;
return GetBinaryType(pathname, &type) ? 0 : -1;
int ret;

ret = GetBinaryTypeW(pathw, &type) ? 0 : -1;

PHP_WIN32_IOUTIL_CLEANUP_W()

return ret;
} else {
if(!IS_ABSOLUTE_PATH(pathname, strlen(pathname)+1)) {
real_path = (char *)malloc(MAX_PATH);
real_path = (char *)malloc(MAXPATHLEN);
if(tsrm_realpath(pathname, real_path) == NULL) {
goto Finished;
}
pathname = real_path;
PHP_WIN32_IOUTIL_REINIT_W(pathname);
}

if(access(pathname, mode)) {
if(php_win32_ioutil_access(pathname, mode)) {
PHP_WIN32_IOUTIL_CLEANUP_W()
free(real_path);
return errno;
}

/* If only existence check is made, return now */
if (mode == 0) {
PHP_WIN32_IOUTIL_CLEANUP_W()
free(real_path);
return 0;
}
Expand Down Expand Up @@ -285,10 +300,11 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode)
if(bucket == NULL && real_path == NULL) {
/* We used the pathname directly. Call tsrm_realpath */
/* so that entry is created in realpath cache */
real_path = (char *)malloc(MAX_PATH);
real_path = (char *)malloc(MAXPATHLEN);
if(tsrm_realpath(pathname, real_path) != NULL) {
pathname = real_path;
bucket = realpath_cache_lookup(pathname, (int)strlen(pathname), t);
PHP_WIN32_IOUTIL_REINIT_W(pathname);
}
}
}
Expand Down Expand Up @@ -325,13 +341,13 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode)
}

/* Get size of security buffer. Call is expected to fail */
if(GetFileSecurity(pathname, sec_info, NULL, 0, &sec_desc_length)) {
if(GetFileSecurityW(pathw, sec_info, NULL, 0, &sec_desc_length)) {
goto Finished;
}

psec_desc = (BYTE *)malloc(sec_desc_length);
if(psec_desc == NULL ||
!GetFileSecurity(pathname, sec_info, (PSECURITY_DESCRIPTOR)psec_desc, sec_desc_length, &sec_desc_length)) {
!GetFileSecurityW(pathw, sec_info, (PSECURITY_DESCRIPTOR)psec_desc, sec_desc_length, &sec_desc_length)) {
goto Finished;
}

Expand Down Expand Up @@ -373,6 +389,7 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode)
real_path = NULL;
}

PHP_WIN32_IOUTIL_CLEANUP_W()
if(fAccess == FALSE) {
errno = EACCES;
return errno;
Expand Down Expand Up @@ -459,14 +476,15 @@ TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd,
{
FILE *stream = NULL;
int fno, type_len, read, mode;
STARTUPINFO startup;
STARTUPINFOW startup;
PROCESS_INFORMATION process;
SECURITY_ATTRIBUTES security;
HANDLE in, out;
DWORD dwCreateFlags = 0;
BOOL res;
process_pair *proc;
char *cmd;
char *cmd = NULL;
wchar_t *cmdw = NULL, *cwdw = NULL, *envw = NULL;
int i;
char *ptype = (char *)type;
HANDLE thread_token = NULL;
Expand All @@ -490,18 +508,42 @@ TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd,
ptype++;
}

cmd = (char*)malloc(strlen(command)+strlen(TWG(comspec))+sizeof(" /c ")+2);
if (!cmd) {
return NULL;
}

sprintf(cmd, "%s /c \"%s\"", TWG(comspec), command);
cmdw = php_win32_cp_any_to_w(cmd);
if (!cmdw) {
free(cmd);
return NULL;
}

if (cwd) {
cwdw = php_win32_ioutil_any_to_w(cwd);
if (!cwdw) {
free(cmd);
free(cmdw);
return NULL;
}
}

security.nLength = sizeof(SECURITY_ATTRIBUTES);
security.bInheritHandle = TRUE;
security.lpSecurityDescriptor = NULL;

if (!type_len || !CreatePipe(&in, &out, &security, 2048L)) {
free(cmdw);
free(cwdw);
free(cmd);
return NULL;
}

memset(&startup, 0, sizeof(STARTUPINFO));
memset(&startup, 0, sizeof(STARTUPINFOW));
memset(&process, 0, sizeof(PROCESS_INFORMATION));

startup.cb = sizeof(STARTUPINFO);
startup.cb = sizeof(STARTUPINFOW);
startup.dwFlags = STARTF_USESTDHANDLES;
startup.hStdError = GetStdHandle(STD_ERROR_HANDLE);

Expand Down Expand Up @@ -533,19 +575,28 @@ TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd,
}
}

cmd = (char*)malloc(strlen(command)+strlen(TWG(comspec))+sizeof(" /c ")+2);
if (!cmd) {
return NULL;
envw = php_win32_cp_env_any_to_w(env);
if (envw) {
dwCreateFlags |= CREATE_UNICODE_ENVIRONMENT;
} else {
if (env) {
free(cmd);
free(cmdw);
free(cwdw);
return NULL;
}
}

sprintf(cmd, "%s /c \"%s\"", TWG(comspec), command);
if (asuser) {
res = CreateProcessAsUser(token_user, NULL, cmd, &security, &security, security.bInheritHandle, dwCreateFlags, env, cwd, &startup, &process);
res = CreateProcessAsUserW(token_user, NULL, cmdw, &security, &security, security.bInheritHandle, dwCreateFlags, envw, cwdw, &startup, &process);
CloseHandle(token_user);
} else {
res = CreateProcess(NULL, cmd, &security, &security, security.bInheritHandle, dwCreateFlags, env, cwd, &startup, &process);
res = CreateProcessW(NULL, cmdw, &security, &security, security.bInheritHandle, dwCreateFlags, envw, cwdw, &startup, &process);
}
free(cmd);
free(cmdw);
free(cwdw);
free(envw);

if (!res) {
return NULL;
Expand Down Expand Up @@ -749,10 +800,17 @@ TSRM_API int win32_utime(const char *filename, struct utimbuf *buf) /* {{{ */
{
FILETIME mtime, atime;
HANDLE hFile;
PHP_WIN32_IOUTIL_INIT_W(filename)

hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
if (!pathw) {
return -1;
}

hFile = CreateFileW(pathw, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, NULL);

PHP_WIN32_IOUTIL_CLEANUP_W()

/* OPEN_ALWAYS mode sets the last error to ERROR_ALREADY_EXISTS but
the CreateFile operation succeeds */
if (GetLastError() == ERROR_ALREADY_EXISTS) {
Expand Down
5 changes: 5 additions & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,11 @@ int zend_startup(zend_utility_functions *utility_functions, char **extensions) /

zend_ini_startup();

#ifdef ZEND_WIN32
/* Uses INI settings, so needs to be run after it. */
php_win32_cp_setup();
#endif

#ifdef ZTS
tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
#endif
Expand Down
7 changes: 7 additions & 0 deletions Zend/zend_multibyte.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static int dummy_internal_encoding_setter(const zend_encoding *encoding)
return FAILURE;
}

static zend_multibyte_functions multibyte_functions_dummy;
static zend_multibyte_functions multibyte_functions = {
NULL,
dummy_encoding_fetcher,
Expand Down Expand Up @@ -108,6 +109,7 @@ ZEND_API int zend_multibyte_set_functions(const zend_multibyte_functions *functi
return FAILURE;
}

multibyte_functions_dummy = multibyte_functions;
multibyte_functions = *functions;

/* As zend_multibyte_set_functions() gets called after ini settings were
Expand All @@ -120,6 +122,11 @@ ZEND_API int zend_multibyte_set_functions(const zend_multibyte_functions *functi
return SUCCESS;
}

ZEND_API void zend_multibyte_restore_functions(void)
{
multibyte_functions = multibyte_functions_dummy;
}

ZEND_API const zend_multibyte_functions *zend_multibyte_get_functions(void)
{
return multibyte_functions.provider_name ? &multibyte_functions: NULL;
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_multibyte.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ ZEND_API extern const zend_encoding *zend_multibyte_encoding_utf8;

/* multibyte utility functions */
ZEND_API int zend_multibyte_set_functions(const zend_multibyte_functions *functions);
ZEND_API void zend_multibyte_restore_functions(void);
ZEND_API const zend_multibyte_functions *zend_multibyte_get_functions(void);

ZEND_API const zend_encoding *zend_multibyte_fetch_encoding(const char *name);
Expand Down
Loading

0 comments on commit 3d3f11e

Please sign in to comment.