Skip to content

Commit 3d3f11e

Browse files
committed
Fixed the UTF-8 and long path support in the streams on Windows.
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.
1 parent 3abd9c3 commit 3d3f11e

File tree

266 files changed

+1045292
-206
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

266 files changed

+1045292
-206
lines changed

TSRM/tsrm_config_common.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ char *alloca ();
4242
#endif
4343

4444
#ifndef MAXPATHLEN
45-
# ifdef PATH_MAX
45+
# if _WIN32
46+
# include "win32/ioutil.h"
47+
# define MAXPATHLEN PHP_WIN32_IOUTIL_MAXPATHLEN
48+
# elif PATH_MAX
4649
# define MAXPATHLEN PATH_MAX
4750
# elif defined(MAX_PATH)
4851
# define MAXPATHLEN MAX_PATH

TSRM/tsrm_win32.c

+75-17
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <Sddl.h>
3434
#include "tsrm_win32.h"
3535
#include "zend_virtual_cwd.h"
36+
#include "win32/ioutil.h"
3637

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

212+
PHP_WIN32_IOUTIL_INIT_W(pathname)
213+
if (!pathw) {
214+
return -1;
215+
}
216+
211217
realpath_cache_bucket * bucket = NULL;
212218
char * real_path = NULL;
213219

214220
if (mode == 1 /*X_OK*/) {
215221
DWORD type;
216-
return GetBinaryType(pathname, &type) ? 0 : -1;
222+
int ret;
223+
224+
ret = GetBinaryTypeW(pathw, &type) ? 0 : -1;
225+
226+
PHP_WIN32_IOUTIL_CLEANUP_W()
227+
228+
return ret;
217229
} else {
218230
if(!IS_ABSOLUTE_PATH(pathname, strlen(pathname)+1)) {
219-
real_path = (char *)malloc(MAX_PATH);
231+
real_path = (char *)malloc(MAXPATHLEN);
220232
if(tsrm_realpath(pathname, real_path) == NULL) {
221233
goto Finished;
222234
}
223235
pathname = real_path;
236+
PHP_WIN32_IOUTIL_REINIT_W(pathname);
224237
}
225238

226-
if(access(pathname, mode)) {
239+
if(php_win32_ioutil_access(pathname, mode)) {
240+
PHP_WIN32_IOUTIL_CLEANUP_W()
227241
free(real_path);
228242
return errno;
229243
}
230244

231245
/* If only existence check is made, return now */
232246
if (mode == 0) {
247+
PHP_WIN32_IOUTIL_CLEANUP_W()
233248
free(real_path);
234249
return 0;
235250
}
@@ -285,10 +300,11 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode)
285300
if(bucket == NULL && real_path == NULL) {
286301
/* We used the pathname directly. Call tsrm_realpath */
287302
/* so that entry is created in realpath cache */
288-
real_path = (char *)malloc(MAX_PATH);
303+
real_path = (char *)malloc(MAXPATHLEN);
289304
if(tsrm_realpath(pathname, real_path) != NULL) {
290305
pathname = real_path;
291306
bucket = realpath_cache_lookup(pathname, (int)strlen(pathname), t);
307+
PHP_WIN32_IOUTIL_REINIT_W(pathname);
292308
}
293309
}
294310
}
@@ -325,13 +341,13 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode)
325341
}
326342

327343
/* Get size of security buffer. Call is expected to fail */
328-
if(GetFileSecurity(pathname, sec_info, NULL, 0, &sec_desc_length)) {
344+
if(GetFileSecurityW(pathw, sec_info, NULL, 0, &sec_desc_length)) {
329345
goto Finished;
330346
}
331347

332348
psec_desc = (BYTE *)malloc(sec_desc_length);
333349
if(psec_desc == NULL ||
334-
!GetFileSecurity(pathname, sec_info, (PSECURITY_DESCRIPTOR)psec_desc, sec_desc_length, &sec_desc_length)) {
350+
!GetFileSecurityW(pathw, sec_info, (PSECURITY_DESCRIPTOR)psec_desc, sec_desc_length, &sec_desc_length)) {
335351
goto Finished;
336352
}
337353

@@ -373,6 +389,7 @@ TSRM_API int tsrm_win32_access(const char *pathname, int mode)
373389
real_path = NULL;
374390
}
375391

392+
PHP_WIN32_IOUTIL_CLEANUP_W()
376393
if(fAccess == FALSE) {
377394
errno = EACCES;
378395
return errno;
@@ -459,14 +476,15 @@ TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd,
459476
{
460477
FILE *stream = NULL;
461478
int fno, type_len, read, mode;
462-
STARTUPINFO startup;
479+
STARTUPINFOW startup;
463480
PROCESS_INFORMATION process;
464481
SECURITY_ATTRIBUTES security;
465482
HANDLE in, out;
466483
DWORD dwCreateFlags = 0;
467484
BOOL res;
468485
process_pair *proc;
469-
char *cmd;
486+
char *cmd = NULL;
487+
wchar_t *cmdw = NULL, *cwdw = NULL, *envw = NULL;
470488
int i;
471489
char *ptype = (char *)type;
472490
HANDLE thread_token = NULL;
@@ -490,18 +508,42 @@ TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd,
490508
ptype++;
491509
}
492510

511+
cmd = (char*)malloc(strlen(command)+strlen(TWG(comspec))+sizeof(" /c ")+2);
512+
if (!cmd) {
513+
return NULL;
514+
}
515+
516+
sprintf(cmd, "%s /c \"%s\"", TWG(comspec), command);
517+
cmdw = php_win32_cp_any_to_w(cmd);
518+
if (!cmdw) {
519+
free(cmd);
520+
return NULL;
521+
}
522+
523+
if (cwd) {
524+
cwdw = php_win32_ioutil_any_to_w(cwd);
525+
if (!cwdw) {
526+
free(cmd);
527+
free(cmdw);
528+
return NULL;
529+
}
530+
}
531+
493532
security.nLength = sizeof(SECURITY_ATTRIBUTES);
494533
security.bInheritHandle = TRUE;
495534
security.lpSecurityDescriptor = NULL;
496535

497536
if (!type_len || !CreatePipe(&in, &out, &security, 2048L)) {
537+
free(cmdw);
538+
free(cwdw);
539+
free(cmd);
498540
return NULL;
499541
}
500542

501-
memset(&startup, 0, sizeof(STARTUPINFO));
543+
memset(&startup, 0, sizeof(STARTUPINFOW));
502544
memset(&process, 0, sizeof(PROCESS_INFORMATION));
503545

504-
startup.cb = sizeof(STARTUPINFO);
546+
startup.cb = sizeof(STARTUPINFOW);
505547
startup.dwFlags = STARTF_USESTDHANDLES;
506548
startup.hStdError = GetStdHandle(STD_ERROR_HANDLE);
507549

@@ -533,19 +575,28 @@ TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd,
533575
}
534576
}
535577

536-
cmd = (char*)malloc(strlen(command)+strlen(TWG(comspec))+sizeof(" /c ")+2);
537-
if (!cmd) {
538-
return NULL;
578+
envw = php_win32_cp_env_any_to_w(env);
579+
if (envw) {
580+
dwCreateFlags |= CREATE_UNICODE_ENVIRONMENT;
581+
} else {
582+
if (env) {
583+
free(cmd);
584+
free(cmdw);
585+
free(cwdw);
586+
return NULL;
587+
}
539588
}
540589

541-
sprintf(cmd, "%s /c \"%s\"", TWG(comspec), command);
542590
if (asuser) {
543-
res = CreateProcessAsUser(token_user, NULL, cmd, &security, &security, security.bInheritHandle, dwCreateFlags, env, cwd, &startup, &process);
591+
res = CreateProcessAsUserW(token_user, NULL, cmdw, &security, &security, security.bInheritHandle, dwCreateFlags, envw, cwdw, &startup, &process);
544592
CloseHandle(token_user);
545593
} else {
546-
res = CreateProcess(NULL, cmd, &security, &security, security.bInheritHandle, dwCreateFlags, env, cwd, &startup, &process);
594+
res = CreateProcessW(NULL, cmdw, &security, &security, security.bInheritHandle, dwCreateFlags, envw, cwdw, &startup, &process);
547595
}
548596
free(cmd);
597+
free(cmdw);
598+
free(cwdw);
599+
free(envw);
549600

550601
if (!res) {
551602
return NULL;
@@ -749,10 +800,17 @@ TSRM_API int win32_utime(const char *filename, struct utimbuf *buf) /* {{{ */
749800
{
750801
FILETIME mtime, atime;
751802
HANDLE hFile;
803+
PHP_WIN32_IOUTIL_INIT_W(filename)
752804

753-
hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
805+
if (!pathw) {
806+
return -1;
807+
}
808+
809+
hFile = CreateFileW(pathw, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
754810
OPEN_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, NULL);
755811

812+
PHP_WIN32_IOUTIL_CLEANUP_W()
813+
756814
/* OPEN_ALWAYS mode sets the last error to ERROR_ALREADY_EXISTS but
757815
the CreateFile operation succeeds */
758816
if (GetLastError() == ERROR_ALREADY_EXISTS) {

Zend/zend.c

+5
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,11 @@ int zend_startup(zend_utility_functions *utility_functions, char **extensions) /
759759

760760
zend_ini_startup();
761761

762+
#ifdef ZEND_WIN32
763+
/* Uses INI settings, so needs to be run after it. */
764+
php_win32_cp_setup();
765+
#endif
766+
762767
#ifdef ZTS
763768
tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
764769
#endif

Zend/zend_multibyte.c

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static int dummy_internal_encoding_setter(const zend_encoding *encoding)
6767
return FAILURE;
6868
}
6969

70+
static zend_multibyte_functions multibyte_functions_dummy;
7071
static zend_multibyte_functions multibyte_functions = {
7172
NULL,
7273
dummy_encoding_fetcher,
@@ -108,6 +109,7 @@ ZEND_API int zend_multibyte_set_functions(const zend_multibyte_functions *functi
108109
return FAILURE;
109110
}
110111

112+
multibyte_functions_dummy = multibyte_functions;
111113
multibyte_functions = *functions;
112114

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

125+
ZEND_API void zend_multibyte_restore_functions(void)
126+
{
127+
multibyte_functions = multibyte_functions_dummy;
128+
}
129+
123130
ZEND_API const zend_multibyte_functions *zend_multibyte_get_functions(void)
124131
{
125132
return multibyte_functions.provider_name ? &multibyte_functions: NULL;

Zend/zend_multibyte.h

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ ZEND_API extern const zend_encoding *zend_multibyte_encoding_utf8;
6060

6161
/* multibyte utility functions */
6262
ZEND_API int zend_multibyte_set_functions(const zend_multibyte_functions *functions);
63+
ZEND_API void zend_multibyte_restore_functions(void);
6364
ZEND_API const zend_multibyte_functions *zend_multibyte_get_functions(void);
6465

6566
ZEND_API const zend_encoding *zend_multibyte_fetch_encoding(const char *name);

0 commit comments

Comments
 (0)