2001-04-28 00:41:53 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 17:03:12 +08:00
|
|
|
| Copyright (c) The PHP Group |
|
2001-04-28 00:41:53 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2001-04-28 00:41:53 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-25 13:05:06 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2021-05-06 18:16:35 +08:00
|
|
|
| https://www.php.net/license/3_01.txt |
|
2001-04-28 00:41:53 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Daniel Beulshausen <daniel@php4win.de> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <process.h>
|
2001-08-07 21:06:23 +08:00
|
|
|
#include <time.h>
|
2009-05-18 03:44:27 +08:00
|
|
|
#include <errno.h>
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2003-06-25 13:05:06 +08:00
|
|
|
#define TSRM_INCLUDE_FULL_WINDOWS_HEADERS
|
2009-06-09 17:25:18 +08:00
|
|
|
#include "SAPI.h"
|
2001-04-28 00:41:53 +08:00
|
|
|
#include "TSRM.h"
|
|
|
|
|
|
|
|
#ifdef TSRM_WIN32
|
2009-10-20 07:32:07 +08:00
|
|
|
#include <Sddl.h>
|
2001-04-28 00:41:53 +08:00
|
|
|
#include "tsrm_win32.h"
|
2013-10-17 15:43:52 +08:00
|
|
|
#include "zend_virtual_cwd.h"
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
#include "win32/ioutil.h"
|
2022-11-15 21:01:45 +08:00
|
|
|
#include "win32/winutil.h"
|
2001-04-28 00:41:53 +08:00
|
|
|
|
|
|
|
#ifdef ZTS
|
|
|
|
static ts_rsrc_id win32_globals_id;
|
|
|
|
#else
|
|
|
|
static tsrm_win32_globals win32_globals;
|
|
|
|
#endif
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void tsrm_win32_ctor(tsrm_win32_globals *globals)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2014-10-15 22:33:40 +08:00
|
|
|
#ifdef ZTS
|
2015-02-17 00:19:32 +08:00
|
|
|
TSRMLS_CACHE_UPDATE();
|
2014-10-15 22:33:40 +08:00
|
|
|
#endif
|
2001-04-28 00:41:53 +08:00
|
|
|
globals->process = NULL;
|
2001-08-07 21:06:23 +08:00
|
|
|
globals->shm = NULL;
|
2001-04-28 00:41:53 +08:00
|
|
|
globals->process_size = 0;
|
2001-08-07 21:06:23 +08:00
|
|
|
globals->shm_size = 0;
|
2015-09-01 04:22:46 +08:00
|
|
|
globals->comspec = _strdup("cmd.exe");
|
2009-06-15 23:01:01 +08:00
|
|
|
|
2009-10-20 07:32:07 +08:00
|
|
|
/* Set it to INVALID_HANDLE_VALUE
|
2015-01-03 17:22:58 +08:00
|
|
|
* It will be initialized correctly in tsrm_win32_access or set to
|
2009-10-20 07:32:07 +08:00
|
|
|
* NULL if no impersonation has been done.
|
|
|
|
* the impersonated token can't be set here as the impersonation
|
|
|
|
* will happen later, in fcgi_accept_request (or whatever is the
|
|
|
|
* SAPI being used).
|
|
|
|
*/
|
|
|
|
globals->impersonation_token = INVALID_HANDLE_VALUE;
|
|
|
|
globals->impersonation_token_sid = NULL;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void tsrm_win32_dtor(tsrm_win32_globals *globals)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2001-08-07 21:06:23 +08:00
|
|
|
shm_pair *ptr;
|
|
|
|
|
|
|
|
if (globals->process) {
|
2001-04-28 00:41:53 +08:00
|
|
|
free(globals->process);
|
|
|
|
}
|
2001-08-07 21:06:23 +08:00
|
|
|
|
|
|
|
if (globals->shm) {
|
|
|
|
for (ptr = globals->shm; ptr < (globals->shm + globals->shm_size); ptr++) {
|
|
|
|
UnmapViewOfFile(ptr->addr);
|
|
|
|
CloseHandle(ptr->segment);
|
|
|
|
UnmapViewOfFile(ptr->descriptor);
|
|
|
|
CloseHandle(ptr->info);
|
|
|
|
}
|
|
|
|
free(globals->shm);
|
|
|
|
}
|
|
|
|
|
2001-07-02 04:08:21 +08:00
|
|
|
free(globals->comspec);
|
2009-05-19 04:56:46 +08:00
|
|
|
|
2009-10-20 07:32:07 +08:00
|
|
|
if (globals->impersonation_token && globals->impersonation_token != INVALID_HANDLE_VALUE ) {
|
2009-05-19 04:56:46 +08:00
|
|
|
CloseHandle(globals->impersonation_token);
|
|
|
|
}
|
2009-10-20 07:32:07 +08:00
|
|
|
if (globals->impersonation_token_sid) {
|
|
|
|
free(globals->impersonation_token_sid);
|
|
|
|
}
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
TSRM_API void tsrm_win32_startup(void)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2001-04-28 00:41:53 +08:00
|
|
|
#ifdef ZTS
|
2023-02-07 18:38:17 +08:00
|
|
|
ts_allocate_id(&win32_globals_id, sizeof(tsrm_win32_globals), (ts_allocate_ctor)tsrm_win32_ctor, (ts_allocate_dtor)tsrm_win32_dtor);
|
2001-04-28 00:41:53 +08:00
|
|
|
#else
|
2014-12-14 06:06:14 +08:00
|
|
|
tsrm_win32_ctor(&win32_globals);
|
2001-04-28 00:41:53 +08:00
|
|
|
#endif
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
TSRM_API void tsrm_win32_shutdown(void)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2001-04-28 00:41:53 +08:00
|
|
|
#ifndef ZTS
|
2014-12-14 06:06:14 +08:00
|
|
|
tsrm_win32_dtor(&win32_globals);
|
2001-04-28 00:41:53 +08:00
|
|
|
#endif
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2020-05-27 15:58:10 +08:00
|
|
|
const char * tsrm_win32_get_path_sid_key(const char *pathname, size_t pathname_len, size_t *key_len)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2009-10-20 07:32:07 +08:00
|
|
|
PSID pSid = TWG(impersonation_token_sid);
|
2017-04-28 20:03:06 +08:00
|
|
|
char *ptcSid = NULL;
|
2009-10-20 07:32:07 +08:00
|
|
|
char *bucket_key = NULL;
|
2017-04-27 21:39:32 +08:00
|
|
|
size_t ptc_sid_len;
|
2009-10-20 07:32:07 +08:00
|
|
|
|
|
|
|
if (!pSid) {
|
2017-04-27 21:39:32 +08:00
|
|
|
*key_len = pathname_len;
|
2017-04-27 22:03:12 +08:00
|
|
|
return pathname;
|
2009-10-20 07:32:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ConvertSidToStringSid(pSid, &ptcSid)) {
|
2017-04-27 21:39:32 +08:00
|
|
|
*key_len = 0;
|
2009-10-20 07:32:07 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-13 23:00:00 +08:00
|
|
|
|
|
|
|
ptc_sid_len = strlen(ptcSid);
|
2017-04-27 21:39:32 +08:00
|
|
|
*key_len = pathname_len + ptc_sid_len;
|
|
|
|
bucket_key = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *key_len + 1);
|
2009-10-20 07:32:07 +08:00
|
|
|
if (!bucket_key) {
|
|
|
|
LocalFree(ptcSid);
|
2017-04-27 21:39:32 +08:00
|
|
|
*key_len = 0;
|
2009-10-20 07:32:07 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-13 23:00:00 +08:00
|
|
|
memcpy(bucket_key, ptcSid, ptc_sid_len);
|
|
|
|
memcpy(bucket_key + ptc_sid_len, pathname, pathname_len + 1);
|
2009-10-20 07:32:07 +08:00
|
|
|
|
|
|
|
LocalFree(ptcSid);
|
|
|
|
return bucket_key;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2009-10-20 07:32:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
PSID tsrm_win32_get_token_sid(HANDLE hToken)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2009-10-20 07:32:07 +08:00
|
|
|
DWORD dwLength = 0;
|
|
|
|
PTOKEN_USER pTokenUser = NULL;
|
|
|
|
DWORD sid_len;
|
|
|
|
PSID pResultSid = NULL;
|
2010-01-25 17:01:22 +08:00
|
|
|
|
2009-10-20 07:32:07 +08:00
|
|
|
/* Get the actual size of the TokenUser structure */
|
|
|
|
if (!GetTokenInformation(
|
|
|
|
hToken, TokenUser, (LPVOID) pTokenUser, 0, &dwLength)) {
|
|
|
|
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
goto Finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
pTokenUser = (PTOKEN_USER)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwLength);
|
|
|
|
if (pTokenUser == NULL) {
|
|
|
|
goto Finished;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and fetch it now */
|
|
|
|
if (!GetTokenInformation(
|
|
|
|
hToken, TokenUser, (LPVOID) pTokenUser, dwLength, &dwLength)) {
|
|
|
|
goto Finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
sid_len = GetLengthSid(pTokenUser->User.Sid);
|
|
|
|
|
|
|
|
/* ConvertSidToStringSid(pTokenUser->User.Sid, &ptcSidOwner); */
|
|
|
|
pResultSid = malloc(sid_len);
|
|
|
|
if (!pResultSid) {
|
|
|
|
goto Finished;
|
|
|
|
}
|
|
|
|
if (!CopySid(sid_len, pResultSid, pTokenUser->User.Sid)) {
|
|
|
|
goto Finished;
|
|
|
|
}
|
2010-01-28 00:52:36 +08:00
|
|
|
HeapFree(GetProcessHeap(), 0, (LPVOID)pTokenUser);
|
2009-10-20 07:32:07 +08:00
|
|
|
return pResultSid;
|
|
|
|
|
|
|
|
Finished:
|
|
|
|
if (pResultSid) {
|
|
|
|
free(pResultSid);
|
|
|
|
}
|
|
|
|
/* Free the buffer for the token groups. */
|
|
|
|
if (pTokenUser != NULL) {
|
|
|
|
HeapFree(GetProcessHeap(), 0, (LPVOID)pTokenUser);
|
|
|
|
}
|
|
|
|
return NULL;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2009-10-20 07:32:07 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
TSRM_API int tsrm_win32_access(const char *pathname, int mode)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2009-10-20 07:32:07 +08:00
|
|
|
time_t t;
|
2012-11-02 18:52:12 +08:00
|
|
|
HANDLE thread_token = NULL;
|
2009-10-20 07:32:07 +08:00
|
|
|
PSID token_sid;
|
2009-05-18 03:44:27 +08:00
|
|
|
SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
|
|
|
|
GENERIC_MAPPING gen_map = { FILE_GENERIC_READ, FILE_GENERIC_WRITE, FILE_GENERIC_EXECUTE, FILE_ALL_ACCESS };
|
|
|
|
DWORD priv_set_length = sizeof(PRIVILEGE_SET);
|
|
|
|
|
|
|
|
PRIVILEGE_SET privilege_set = {0};
|
|
|
|
DWORD sec_desc_length = 0, desired_access = 0, granted_access = 0;
|
|
|
|
BYTE * psec_desc = NULL;
|
|
|
|
BOOL fAccess = FALSE;
|
2009-06-16 08:07:05 +08:00
|
|
|
|
2016-07-30 23:32:53 +08:00
|
|
|
realpath_cache_bucket * bucket = NULL;
|
2017-12-09 20:15:28 +08:00
|
|
|
char real_path[MAXPATHLEN] = {0};
|
2016-07-30 23:32:53 +08:00
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
if(!IS_ABSOLUTE_PATH(pathname, strlen(pathname)+1)) {
|
|
|
|
if(tsrm_realpath(pathname, real_path) == NULL) {
|
|
|
|
SET_ERRNO_FROM_WIN32_CODE(ERROR_FILE_NOT_FOUND);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pathname = real_path;
|
|
|
|
}
|
|
|
|
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
PHP_WIN32_IOUTIL_INIT_W(pathname)
|
|
|
|
if (!pathw) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
/* Either access call failed, or the mode was asking for a specific check.*/
|
|
|
|
int ret = php_win32_ioutil_access_w(pathw, mode);
|
|
|
|
if (0 > ret || X_OK == mode || F_OK == mode) {
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
PHP_WIN32_IOUTIL_CLEANUP_W()
|
|
|
|
return ret;
|
2017-12-08 23:35:45 +08:00
|
|
|
}
|
2009-10-20 07:32:07 +08:00
|
|
|
|
|
|
|
/* Only in NTS when impersonate==1 (aka FastCGI) */
|
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
/*
|
|
|
|
AccessCheck() requires an impersonation token. We first get a primary
|
|
|
|
token and then create a duplicate impersonation token. The
|
|
|
|
impersonation token is not actually assigned to the thread, but is
|
|
|
|
used in the call to AccessCheck. Thus, this function itself never
|
|
|
|
impersonates, but does use the identity of the thread. If the thread
|
|
|
|
was impersonating already, this function uses that impersonation context.
|
|
|
|
*/
|
|
|
|
if(!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &thread_token)) {
|
|
|
|
if (GetLastError() == ERROR_NO_TOKEN) {
|
|
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &thread_token)) {
|
|
|
|
TWG(impersonation_token) = NULL;
|
|
|
|
goto Finished;
|
|
|
|
}
|
2009-10-20 07:32:07 +08:00
|
|
|
}
|
2017-12-08 23:35:45 +08:00
|
|
|
}
|
2009-10-20 07:32:07 +08:00
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
/* token_sid will be freed in tsrmwin32_dtor */
|
|
|
|
token_sid = tsrm_win32_get_token_sid(thread_token);
|
|
|
|
if (!token_sid) {
|
|
|
|
if (TWG(impersonation_token_sid)) {
|
|
|
|
free(TWG(impersonation_token_sid));
|
2009-10-20 07:32:07 +08:00
|
|
|
}
|
2017-12-08 23:35:45 +08:00
|
|
|
TWG(impersonation_token_sid) = NULL;
|
|
|
|
goto Finished;
|
|
|
|
}
|
2009-10-20 07:32:07 +08:00
|
|
|
|
2021-03-31 11:49:41 +08:00
|
|
|
/* Different identity, we need a new impersonated token as well */
|
2017-12-08 23:35:45 +08:00
|
|
|
if (!TWG(impersonation_token_sid) || !EqualSid(token_sid, TWG(impersonation_token_sid))) {
|
|
|
|
if (TWG(impersonation_token_sid)) {
|
|
|
|
free(TWG(impersonation_token_sid));
|
2009-10-20 07:32:07 +08:00
|
|
|
}
|
2017-12-08 23:35:45 +08:00
|
|
|
TWG(impersonation_token_sid) = token_sid;
|
2009-10-20 07:32:07 +08:00
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
/* Duplicate the token as impersonated token */
|
|
|
|
if (!DuplicateToken(thread_token, SecurityImpersonation, &TWG(impersonation_token))) {
|
|
|
|
goto Finished;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* we already have it, free it then */
|
|
|
|
free(token_sid);
|
|
|
|
}
|
2009-10-20 07:32:07 +08:00
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
if (CWDG(realpath_cache_size_limit)) {
|
|
|
|
t = time(0);
|
|
|
|
bucket = realpath_cache_lookup(pathname, strlen(pathname), t);
|
2017-12-09 20:15:28 +08:00
|
|
|
if(bucket == NULL && !real_path[0]) {
|
2017-12-08 23:35:45 +08:00
|
|
|
/* We used the pathname directly. Call tsrm_realpath */
|
|
|
|
/* so that entry is created in realpath cache */
|
|
|
|
if(tsrm_realpath(pathname, real_path) != NULL) {
|
|
|
|
pathname = real_path;
|
|
|
|
bucket = realpath_cache_lookup(pathname, strlen(pathname), t);
|
|
|
|
PHP_WIN32_IOUTIL_REINIT_W(pathname);
|
2009-06-16 08:07:05 +08:00
|
|
|
}
|
2017-12-08 23:35:45 +08:00
|
|
|
}
|
|
|
|
}
|
2009-06-16 08:07:05 +08:00
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
/* Do a full access check because access() will only check read-only attribute */
|
|
|
|
if(mode == 0 || mode > 6) {
|
|
|
|
if(bucket != NULL && bucket->is_rvalid) {
|
|
|
|
fAccess = bucket->is_readable;
|
2009-06-15 23:01:01 +08:00
|
|
|
goto Finished;
|
|
|
|
}
|
2017-12-08 23:35:45 +08:00
|
|
|
desired_access = FILE_GENERIC_READ;
|
|
|
|
} else if(mode <= 2) {
|
|
|
|
if(bucket != NULL && bucket->is_wvalid) {
|
|
|
|
fAccess = bucket->is_writable;
|
2009-05-18 03:44:27 +08:00
|
|
|
goto Finished;
|
|
|
|
}
|
2017-12-08 23:35:45 +08:00
|
|
|
desired_access = FILE_GENERIC_WRITE;
|
|
|
|
} else if(mode <= 4) {
|
|
|
|
if(bucket != NULL && bucket->is_rvalid) {
|
|
|
|
fAccess = bucket->is_readable;
|
2009-05-18 03:44:27 +08:00
|
|
|
goto Finished;
|
|
|
|
}
|
2017-12-08 23:35:45 +08:00
|
|
|
desired_access = FILE_GENERIC_READ|FILE_FLAG_BACKUP_SEMANTICS;
|
|
|
|
} else { // if(mode <= 6)
|
|
|
|
if(bucket != NULL && bucket->is_rvalid && bucket->is_wvalid) {
|
|
|
|
fAccess = bucket->is_readable & bucket->is_writable;
|
|
|
|
goto Finished;
|
|
|
|
}
|
|
|
|
desired_access = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
|
|
|
|
}
|
2009-05-18 03:44:27 +08:00
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
if(TWG(impersonation_token) == NULL) {
|
|
|
|
goto Finished;
|
|
|
|
}
|
2009-10-20 07:32:07 +08:00
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
/* Get size of security buffer. Call is expected to fail */
|
|
|
|
if(GetFileSecurityW(pathw, sec_info, NULL, 0, &sec_desc_length)) {
|
|
|
|
goto Finished;
|
|
|
|
}
|
2009-05-18 03:44:27 +08:00
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
psec_desc = (BYTE *)malloc(sec_desc_length);
|
|
|
|
if(psec_desc == NULL ||
|
|
|
|
!GetFileSecurityW(pathw, sec_info, (PSECURITY_DESCRIPTOR)psec_desc, sec_desc_length, &sec_desc_length)) {
|
|
|
|
goto Finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
MapGenericMask(&desired_access, &gen_map);
|
|
|
|
|
|
|
|
if(!AccessCheck((PSECURITY_DESCRIPTOR)psec_desc, TWG(impersonation_token), desired_access, &gen_map, &privilege_set, &priv_set_length, &granted_access, &fAccess)) {
|
|
|
|
goto Finished_Impersonate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep the result in realpath_cache */
|
|
|
|
if(bucket != NULL) {
|
|
|
|
if(desired_access == (FILE_GENERIC_READ|FILE_FLAG_BACKUP_SEMANTICS)) {
|
|
|
|
bucket->is_rvalid = 1;
|
|
|
|
bucket->is_readable = fAccess;
|
2009-06-16 08:07:05 +08:00
|
|
|
}
|
2017-12-08 23:35:45 +08:00
|
|
|
else if(desired_access == FILE_GENERIC_WRITE) {
|
|
|
|
bucket->is_wvalid = 1;
|
|
|
|
bucket->is_writable = fAccess;
|
|
|
|
} else if (desired_access == (FILE_GENERIC_READ | FILE_GENERIC_WRITE)) {
|
|
|
|
bucket->is_rvalid = 1;
|
|
|
|
bucket->is_readable = fAccess;
|
|
|
|
bucket->is_wvalid = 1;
|
|
|
|
bucket->is_writable = fAccess;
|
|
|
|
}
|
|
|
|
}
|
2009-06-16 08:07:05 +08:00
|
|
|
|
2009-10-20 07:32:07 +08:00
|
|
|
Finished_Impersonate:
|
2017-12-08 23:35:45 +08:00
|
|
|
if(psec_desc != NULL) {
|
|
|
|
free(psec_desc);
|
|
|
|
psec_desc = NULL;
|
|
|
|
}
|
2009-05-18 03:44:27 +08:00
|
|
|
|
2009-10-20 07:32:07 +08:00
|
|
|
Finished:
|
2017-12-08 23:35:45 +08:00
|
|
|
if(thread_token != NULL) {
|
|
|
|
CloseHandle(thread_token);
|
|
|
|
}
|
2009-06-16 08:07:05 +08:00
|
|
|
|
2017-12-08 23:35:45 +08:00
|
|
|
PHP_WIN32_IOUTIL_CLEANUP_W()
|
|
|
|
if(fAccess == FALSE) {
|
|
|
|
errno = EACCES;
|
|
|
|
return errno;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2003-06-25 13:05:06 +08:00
|
|
|
}
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2003-06-25 13:05:06 +08:00
|
|
|
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static process_pair *process_get(FILE *stream)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2001-08-07 21:06:23 +08:00
|
|
|
process_pair *ptr;
|
|
|
|
process_pair *newptr;
|
2008-05-29 19:31:02 +08:00
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
for (ptr = TWG(process); ptr < (TWG(process) + TWG(process_size)); ptr++) {
|
2001-07-10 00:44:40 +08:00
|
|
|
if (ptr->stream == stream) {
|
2001-04-28 00:41:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-05-29 19:31:02 +08:00
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
if (ptr < (TWG(process) + TWG(process_size))) {
|
2001-04-28 00:41:53 +08:00
|
|
|
return ptr;
|
|
|
|
}
|
2008-05-29 19:31:02 +08:00
|
|
|
|
2001-08-07 21:06:23 +08:00
|
|
|
newptr = (process_pair*)realloc((void*)TWG(process), (TWG(process_size)+1)*sizeof(process_pair));
|
2001-04-28 02:50:35 +08:00
|
|
|
if (newptr == NULL) {
|
2001-04-28 00:41:53 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-05-29 19:31:02 +08:00
|
|
|
|
2001-04-28 00:41:53 +08:00
|
|
|
TWG(process) = newptr;
|
|
|
|
ptr = newptr + TWG(process_size);
|
|
|
|
TWG(process_size)++;
|
|
|
|
return ptr;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2016-08-23 06:27:17 +08:00
|
|
|
static shm_pair *shm_get(key_t key, void *addr)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2001-08-07 21:06:23 +08:00
|
|
|
shm_pair *ptr;
|
|
|
|
shm_pair *newptr;
|
2008-05-29 19:31:02 +08:00
|
|
|
|
2001-08-07 21:06:23 +08:00
|
|
|
for (ptr = TWG(shm); ptr < (TWG(shm) + TWG(shm_size)); ptr++) {
|
|
|
|
if (!ptr->descriptor) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!addr && ptr->descriptor->shm_perm.key == key) {
|
|
|
|
break;
|
|
|
|
} else if (ptr->addr == addr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptr < (TWG(shm) + TWG(shm_size))) {
|
|
|
|
return ptr;
|
|
|
|
}
|
2008-05-29 19:31:02 +08:00
|
|
|
|
2001-08-07 21:06:23 +08:00
|
|
|
newptr = (shm_pair*)realloc((void*)TWG(shm), (TWG(shm_size)+1)*sizeof(shm_pair));
|
|
|
|
if (newptr == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-05-29 19:31:02 +08:00
|
|
|
|
2001-08-07 21:06:23 +08:00
|
|
|
TWG(shm) = newptr;
|
|
|
|
ptr = newptr + TWG(shm_size);
|
|
|
|
TWG(shm_size)++;
|
2015-09-23 02:33:46 +08:00
|
|
|
memset(ptr, 0, sizeof(*ptr));
|
2001-08-07 21:06:23 +08:00
|
|
|
return ptr;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-08-07 21:06:23 +08:00
|
|
|
|
2017-07-04 23:06:52 +08:00
|
|
|
static HANDLE dupHandle(HANDLE fh, BOOL inherit)
|
|
|
|
{/*{{{*/
|
2001-07-10 00:44:40 +08:00
|
|
|
HANDLE copy, self = GetCurrentProcess();
|
|
|
|
if (!DuplicateHandle(self, fh, self, ©, 0, inherit, DUPLICATE_SAME_ACCESS|DUPLICATE_CLOSE_SOURCE)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return copy;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-07-10 00:44:40 +08:00
|
|
|
|
2001-08-05 09:34:40 +08:00
|
|
|
TSRM_API FILE *popen(const char *command, const char *type)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2010-09-17 18:00:01 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
return popen_ex(command, type, NULL, NULL);
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2002-10-29 15:32:52 +08:00
|
|
|
|
2020-06-07 17:01:19 +08:00
|
|
|
TSRM_API FILE *popen_ex(const char *command, const char *type, const char *cwd, const char *env)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2001-04-28 00:41:53 +08:00
|
|
|
FILE *stream = NULL;
|
2014-08-29 17:21:07 +08:00
|
|
|
int fno, type_len, read, mode;
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
STARTUPINFOW startup;
|
2001-04-28 00:41:53 +08:00
|
|
|
PROCESS_INFORMATION process;
|
|
|
|
SECURITY_ATTRIBUTES security;
|
|
|
|
HANDLE in, out;
|
2009-06-09 08:25:37 +08:00
|
|
|
DWORD dwCreateFlags = 0;
|
2009-09-02 06:51:31 +08:00
|
|
|
BOOL res;
|
2001-08-07 21:06:23 +08:00
|
|
|
process_pair *proc;
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
char *cmd = NULL;
|
|
|
|
wchar_t *cmdw = NULL, *cwdw = NULL, *envw = NULL;
|
2009-08-27 03:57:01 +08:00
|
|
|
char *ptype = (char *)type;
|
2009-09-02 09:59:17 +08:00
|
|
|
HANDLE thread_token = NULL;
|
|
|
|
HANDLE token_user = NULL;
|
2009-09-04 14:59:08 +08:00
|
|
|
BOOL asuser = TRUE;
|
2009-09-04 03:16:50 +08:00
|
|
|
|
2009-08-27 03:57:01 +08:00
|
|
|
if (!type) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-29 02:32:51 +08:00
|
|
|
type_len = (int)strlen(type);
|
2019-02-21 23:59:30 +08:00
|
|
|
if (type_len < 1 || type_len > 2) {
|
2009-08-27 03:57:01 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-02-21 23:59:30 +08:00
|
|
|
if (ptype[0] != 'r' && ptype[0] != 'w') {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type_len > 1 && (ptype[1] != 'b' && ptype[1] != 't')) {
|
|
|
|
return NULL;
|
2009-08-27 03:57:01 +08:00
|
|
|
}
|
|
|
|
|
2020-01-21 17:05:33 +08:00
|
|
|
cmd = (char*)malloc(strlen(command)+strlen(TWG(comspec))+sizeof(" /s /c ")+2);
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
if (!cmd) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:05:33 +08:00
|
|
|
sprintf(cmd, "%s /s /c \"%s\"", TWG(comspec), command);
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-28 00:41:53 +08:00
|
|
|
security.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
security.bInheritHandle = TRUE;
|
|
|
|
security.lpSecurityDescriptor = NULL;
|
|
|
|
|
2009-08-27 03:57:01 +08:00
|
|
|
if (!type_len || !CreatePipe(&in, &out, &security, 2048L)) {
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
free(cmdw);
|
|
|
|
free(cwdw);
|
|
|
|
free(cmd);
|
2001-04-28 00:41:53 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-05-29 19:31:02 +08:00
|
|
|
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
memset(&startup, 0, sizeof(STARTUPINFOW));
|
2001-04-28 02:50:35 +08:00
|
|
|
memset(&process, 0, sizeof(PROCESS_INFORMATION));
|
2001-04-28 00:41:53 +08:00
|
|
|
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
startup.cb = sizeof(STARTUPINFOW);
|
2001-04-28 00:41:53 +08:00
|
|
|
startup.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
startup.hStdError = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
|
|
|
|
read = (type[0] == 'r') ? TRUE : FALSE;
|
2009-08-27 03:57:01 +08:00
|
|
|
mode = ((type_len == 2) && (type[1] == 'b')) ? O_BINARY : O_TEXT;
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
if (read) {
|
2001-07-11 23:10:56 +08:00
|
|
|
in = dupHandle(in, FALSE);
|
2001-04-28 00:41:53 +08:00
|
|
|
startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
startup.hStdOutput = out;
|
2001-04-28 02:50:35 +08:00
|
|
|
} else {
|
2001-07-11 23:10:56 +08:00
|
|
|
out = dupHandle(out, FALSE);
|
2001-04-28 00:41:53 +08:00
|
|
|
startup.hStdInput = in;
|
|
|
|
startup.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
}
|
|
|
|
|
2009-06-09 08:25:37 +08:00
|
|
|
dwCreateFlags = NORMAL_PRIORITY_CLASS;
|
|
|
|
if (strcmp(sapi_module.name, "cli") != 0) {
|
|
|
|
dwCreateFlags |= CREATE_NO_WINDOW;
|
|
|
|
}
|
|
|
|
|
2009-09-02 09:59:17 +08:00
|
|
|
/* Get a token with the impersonated user. */
|
|
|
|
if(OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &thread_token)) {
|
|
|
|
DuplicateTokenEx(thread_token, MAXIMUM_ALLOWED, &security, SecurityImpersonation, TokenPrimary, &token_user);
|
2009-09-04 03:16:50 +08:00
|
|
|
} else {
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
if (err == ERROR_NO_TOKEN) {
|
|
|
|
asuser = FALSE;
|
|
|
|
}
|
2009-09-02 09:59:17 +08:00
|
|
|
}
|
|
|
|
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
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;
|
|
|
|
}
|
2011-07-27 22:23:06 +08:00
|
|
|
}
|
|
|
|
|
2009-09-04 03:16:50 +08:00
|
|
|
if (asuser) {
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
res = CreateProcessAsUserW(token_user, NULL, cmdw, &security, &security, security.bInheritHandle, dwCreateFlags, envw, cwdw, &startup, &process);
|
2009-09-04 03:16:50 +08:00
|
|
|
CloseHandle(token_user);
|
|
|
|
} else {
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
res = CreateProcessW(NULL, cmdw, &security, &security, security.bInheritHandle, dwCreateFlags, envw, cwdw, &startup, &process);
|
2009-09-04 03:16:50 +08:00
|
|
|
}
|
2009-08-20 17:03:19 +08:00
|
|
|
free(cmd);
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
free(cmdw);
|
|
|
|
free(cwdw);
|
|
|
|
free(envw);
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2009-09-02 06:51:31 +08:00
|
|
|
if (!res) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-04-28 00:41:53 +08:00
|
|
|
CloseHandle(process.hThread);
|
2014-12-14 06:06:14 +08:00
|
|
|
proc = process_get(NULL);
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2001-04-28 02:50:35 +08:00
|
|
|
if (read) {
|
2007-04-16 16:09:56 +08:00
|
|
|
fno = _open_osfhandle((tsrm_intptr_t)in, _O_RDONLY | mode);
|
2001-04-28 00:41:53 +08:00
|
|
|
CloseHandle(out);
|
2001-04-28 02:50:35 +08:00
|
|
|
} else {
|
2007-04-16 16:09:56 +08:00
|
|
|
fno = _open_osfhandle((tsrm_intptr_t)out, _O_WRONLY | mode);
|
2001-04-28 00:41:53 +08:00
|
|
|
CloseHandle(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = _fdopen(fno, type);
|
|
|
|
proc->prochnd = process.hProcess;
|
|
|
|
proc->stream = stream;
|
|
|
|
return stream;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2001-08-05 09:34:40 +08:00
|
|
|
TSRM_API int pclose(FILE *stream)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2001-04-28 00:41:53 +08:00
|
|
|
DWORD termstat = 0;
|
2001-08-07 21:06:23 +08:00
|
|
|
process_pair *process;
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
if ((process = process_get(stream)) == NULL) {
|
2001-04-28 00:41:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(process->stream);
|
2009-05-16 01:48:34 +08:00
|
|
|
fclose(process->stream);
|
2001-04-28 00:41:53 +08:00
|
|
|
|
2001-07-10 00:44:40 +08:00
|
|
|
WaitForSingleObject(process->prochnd, INFINITE);
|
2001-04-28 00:41:53 +08:00
|
|
|
GetExitCodeProcess(process->prochnd, &termstat);
|
|
|
|
process->stream = NULL;
|
|
|
|
CloseHandle(process->prochnd);
|
|
|
|
|
|
|
|
return termstat;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-04-28 02:50:35 +08:00
|
|
|
|
2021-09-02 22:15:29 +08:00
|
|
|
#define SEGMENT_PREFIX "TSRM_SHM_SEGMENT:"
|
|
|
|
#define DESCRIPTOR_PREFIX "TSRM_SHM_DESCRIPTOR:"
|
|
|
|
#define INT_MIN_AS_STRING "-2147483648"
|
|
|
|
|
2022-11-15 21:01:45 +08:00
|
|
|
|
|
|
|
#define TSRM_BASE_SHM_KEY_ADDRESS 0x20000000
|
|
|
|
/* Returns a number between 0x2000_0000 and 0x3fff_ffff. On Windows, key_t is int. */
|
|
|
|
static key_t tsrm_choose_random_shm_key(key_t prev_key) {
|
|
|
|
unsigned char buf[4];
|
|
|
|
if (php_win32_get_random_bytes(buf, 4) != SUCCESS) {
|
|
|
|
return prev_key + 2;
|
|
|
|
}
|
|
|
|
uint32_t n =
|
|
|
|
((uint32_t)(buf[0]) << 24) |
|
|
|
|
(((uint32_t)buf[1]) << 16) |
|
|
|
|
(((uint32_t)buf[2]) << 8) |
|
|
|
|
(((uint32_t)buf[3]));
|
|
|
|
return (n & 0x1fffffff) + TSRM_BASE_SHM_KEY_ADDRESS;
|
|
|
|
}
|
|
|
|
|
2016-08-23 06:27:17 +08:00
|
|
|
TSRM_API int shmget(key_t key, size_t size, int flags)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2001-08-07 21:06:23 +08:00
|
|
|
shm_pair *shm;
|
2021-09-02 22:15:29 +08:00
|
|
|
char shm_segment[sizeof(SEGMENT_PREFIX INT_MIN_AS_STRING)], shm_info[sizeof(DESCRIPTOR_PREFIX INT_MIN_AS_STRING)];
|
2020-05-05 15:31:17 +08:00
|
|
|
HANDLE shm_handle = NULL, info_handle = NULL;
|
2001-08-07 21:06:23 +08:00
|
|
|
BOOL created = FALSE;
|
|
|
|
|
2020-05-05 15:31:17 +08:00
|
|
|
if (key != IPC_PRIVATE) {
|
2021-09-02 22:15:29 +08:00
|
|
|
snprintf(shm_segment, sizeof(shm_segment), SEGMENT_PREFIX "%d", key);
|
|
|
|
snprintf(shm_info, sizeof(shm_info), DESCRIPTOR_PREFIX "%d", key);
|
2001-08-07 21:06:23 +08:00
|
|
|
|
2020-05-05 15:31:17 +08:00
|
|
|
shm_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_segment);
|
|
|
|
info_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_info);
|
2022-11-15 21:01:45 +08:00
|
|
|
} else {
|
|
|
|
/* IPC_PRIVATE always creates a new segment even if IPC_CREAT flag isn't passed. */
|
|
|
|
flags |= IPC_CREAT;
|
2020-05-05 15:31:17 +08:00
|
|
|
}
|
2001-08-07 21:06:23 +08:00
|
|
|
|
2014-10-02 19:48:26 +08:00
|
|
|
if (!shm_handle && !info_handle) {
|
2001-08-07 21:06:23 +08:00
|
|
|
if (flags & IPC_CREAT) {
|
2016-08-23 06:27:17 +08:00
|
|
|
#if SIZEOF_SIZE_T == 8
|
|
|
|
DWORD high = size >> 32;
|
|
|
|
DWORD low = (DWORD)size;
|
|
|
|
#else
|
|
|
|
DWORD high = 0;
|
|
|
|
DWORD low = size;
|
|
|
|
#endif
|
2020-05-05 15:31:17 +08:00
|
|
|
shm_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, high, low, key == IPC_PRIVATE ? NULL : shm_segment);
|
|
|
|
info_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(shm->descriptor), key == IPC_PRIVATE ? NULL : shm_info);
|
2001-08-07 21:06:23 +08:00
|
|
|
created = TRUE;
|
|
|
|
}
|
2014-10-02 19:48:26 +08:00
|
|
|
if (!shm_handle || !info_handle) {
|
2015-10-05 04:19:51 +08:00
|
|
|
if (shm_handle) {
|
|
|
|
CloseHandle(shm_handle);
|
|
|
|
}
|
|
|
|
if (info_handle) {
|
|
|
|
CloseHandle(info_handle);
|
|
|
|
}
|
2001-08-07 21:06:23 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2007-03-21 01:57:59 +08:00
|
|
|
} else {
|
|
|
|
if (flags & IPC_EXCL) {
|
2016-08-30 03:45:39 +08:00
|
|
|
if (shm_handle) {
|
|
|
|
CloseHandle(shm_handle);
|
|
|
|
}
|
|
|
|
if (info_handle) {
|
|
|
|
CloseHandle(info_handle);
|
|
|
|
}
|
2007-03-21 01:57:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2001-08-07 21:06:23 +08:00
|
|
|
}
|
|
|
|
|
2022-11-15 21:01:45 +08:00
|
|
|
if (key == IPC_PRIVATE) {
|
|
|
|
/* This should call shm_get with a brand new key id that isn't used yet. See https://man7.org/linux/man-pages/man2/shmget.2.html
|
|
|
|
* Because extensions such as shmop/sysvshm can be used in userland to attach to shared memory segments, use unpredictable high positive numbers to avoid accidentally conflicting with userland. */
|
|
|
|
key = tsrm_choose_random_shm_key(TSRM_BASE_SHM_KEY_ADDRESS);
|
|
|
|
for (shm_pair *ptr = TWG(shm); ptr < (TWG(shm) + TWG(shm_size)); ptr++) {
|
|
|
|
if (ptr->descriptor && ptr->descriptor->shm_perm.key == key) {
|
|
|
|
key = tsrm_choose_random_shm_key(key);
|
|
|
|
ptr = TWG(shm);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-07 21:06:23 +08:00
|
|
|
shm = shm_get(key, NULL);
|
2015-09-22 21:31:02 +08:00
|
|
|
if (!shm) {
|
2015-10-05 04:14:47 +08:00
|
|
|
CloseHandle(shm_handle);
|
|
|
|
CloseHandle(info_handle);
|
2015-09-22 21:31:02 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2001-08-07 21:06:23 +08:00
|
|
|
shm->segment = shm_handle;
|
|
|
|
shm->info = info_handle;
|
|
|
|
shm->descriptor = MapViewOfFileEx(shm->info, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
|
|
|
|
|
2013-07-22 20:50:18 +08:00
|
|
|
if (NULL != shm->descriptor && created) {
|
2001-08-07 21:06:23 +08:00
|
|
|
shm->descriptor->shm_perm.key = key;
|
|
|
|
shm->descriptor->shm_segsz = size;
|
|
|
|
shm->descriptor->shm_ctime = time(NULL);
|
|
|
|
shm->descriptor->shm_cpid = getpid();
|
|
|
|
shm->descriptor->shm_perm.mode = flags;
|
|
|
|
|
|
|
|
shm->descriptor->shm_perm.cuid = shm->descriptor->shm_perm.cgid= 0;
|
|
|
|
shm->descriptor->shm_perm.gid = shm->descriptor->shm_perm.uid = 0;
|
|
|
|
shm->descriptor->shm_atime = shm->descriptor->shm_dtime = 0;
|
|
|
|
shm->descriptor->shm_lpid = shm->descriptor->shm_nattch = 0;
|
|
|
|
shm->descriptor->shm_perm.mode = shm->descriptor->shm_perm.seq = 0;
|
|
|
|
}
|
|
|
|
|
2013-07-22 20:50:18 +08:00
|
|
|
if (NULL != shm->descriptor && (shm->descriptor->shm_perm.key != key || size > shm->descriptor->shm_segsz)) {
|
|
|
|
if (NULL != shm->segment) {
|
|
|
|
CloseHandle(shm->segment);
|
|
|
|
}
|
2001-08-07 21:06:23 +08:00
|
|
|
UnmapViewOfFile(shm->descriptor);
|
|
|
|
CloseHandle(shm->info);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-08-07 21:06:23 +08:00
|
|
|
|
|
|
|
TSRM_API void *shmat(int key, const void *shmaddr, int flags)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2001-08-07 21:06:23 +08:00
|
|
|
shm_pair *shm = shm_get(key, NULL);
|
|
|
|
|
2022-11-02 18:35:30 +08:00
|
|
|
if (!shm || !shm->segment) {
|
2001-08-07 21:06:23 +08:00
|
|
|
return (void*)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
shm->addr = MapViewOfFileEx(shm->segment, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL);
|
|
|
|
|
2017-04-16 00:14:25 +08:00
|
|
|
if (NULL == shm->addr) {
|
|
|
|
int err = GetLastError();
|
2016-08-18 21:26:24 +08:00
|
|
|
SET_ERRNO_FROM_WIN32_CODE(err);
|
2016-08-18 20:15:10 +08:00
|
|
|
return (void*)-1;
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:58:33 +08:00
|
|
|
shm->descriptor->shm_atime = time(NULL);
|
|
|
|
shm->descriptor->shm_lpid = getpid();
|
|
|
|
shm->descriptor->shm_nattch++;
|
|
|
|
|
2001-08-07 21:06:23 +08:00
|
|
|
return shm->addr;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-08-07 21:06:23 +08:00
|
|
|
|
|
|
|
TSRM_API int shmdt(const void *shmaddr)
|
2017-07-04 23:06:52 +08:00
|
|
|
{/*{{{*/
|
2001-08-07 21:06:23 +08:00
|
|
|
shm_pair *shm = shm_get(0, (void*)shmaddr);
|
2019-11-25 21:05:15 +08:00
|
|
|
int ret;
|
2001-08-07 21:06:23 +08:00
|
|
|
|
2022-11-02 18:35:30 +08:00
|
|
|
if (!shm || !shm->segment) {
|
2001-08-07 21:06:23 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
shm->descriptor->shm_dtime = time(NULL);
|
|
|
|
shm->descriptor->shm_lpid = getpid();
|
|
|
|
shm->descriptor->shm_nattch--;
|
|
|
|
|
2019-11-25 21:05:15 +08:00
|
|
|
ret = UnmapViewOfFile(shm->addr) ? 0 : -1;
|
|
|
|
if (!ret && shm->descriptor->shm_nattch <= 0) {
|
|
|
|
ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1;
|
|
|
|
shm->descriptor = NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2001-08-07 21:06:23 +08:00
|
|
|
|
2017-07-04 23:06:52 +08:00
|
|
|
TSRM_API int shmctl(int key, int cmd, struct shmid_ds *buf)
|
|
|
|
{/*{{{*/
|
2001-08-07 21:06:23 +08:00
|
|
|
shm_pair *shm = shm_get(key, NULL);
|
|
|
|
|
2022-11-02 18:35:30 +08:00
|
|
|
if (!shm || !shm->segment) {
|
2001-08-07 21:06:23 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case IPC_STAT:
|
2001-08-07 21:29:51 +08:00
|
|
|
memcpy(buf, shm->descriptor, sizeof(struct shmid_ds));
|
2001-08-07 21:06:23 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case IPC_SET:
|
|
|
|
shm->descriptor->shm_ctime = time(NULL);
|
|
|
|
shm->descriptor->shm_perm.uid = buf->shm_perm.uid;
|
|
|
|
shm->descriptor->shm_perm.gid = buf->shm_perm.gid;
|
|
|
|
shm->descriptor->shm_perm.mode = buf->shm_perm.mode;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case IPC_RMID:
|
|
|
|
if (shm->descriptor->shm_nattch < 1) {
|
|
|
|
shm->descriptor->shm_perm.key = -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2017-07-04 23:06:52 +08:00
|
|
|
}/*}}}*/
|
2003-09-30 17:48:53 +08:00
|
|
|
|
2014-11-10 16:14:15 +08:00
|
|
|
static zend_always_inline void UnixTimeToFileTime(time_t t, LPFILETIME pft) /* {{{ */
|
|
|
|
{
|
|
|
|
// Note that LONGLONG is a 64-bit value
|
|
|
|
LONGLONG ll;
|
|
|
|
|
2019-07-13 15:40:50 +08:00
|
|
|
ll = t * 10000000LL + 116444736000000000LL;
|
2014-11-10 16:14:15 +08:00
|
|
|
pft->dwLowDateTime = (DWORD)ll;
|
|
|
|
pft->dwHighDateTime = ll >> 32;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
TSRM_API int win32_utime(const char *filename, struct utimbuf *buf) /* {{{ */
|
|
|
|
{
|
|
|
|
FILETIME mtime, atime;
|
|
|
|
HANDLE hFile;
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
PHP_WIN32_IOUTIL_INIT_W(filename)
|
2014-11-10 16:14:15 +08:00
|
|
|
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
if (!pathw) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hFile = CreateFileW(pathw, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
|
2014-11-10 16:14:15 +08:00
|
|
|
OPEN_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
|
|
|
|
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.
2016-06-20 15:32:19 +08:00
|
|
|
PHP_WIN32_IOUTIL_CLEANUP_W()
|
|
|
|
|
2014-11-10 16:14:15 +08:00
|
|
|
/* OPEN_ALWAYS mode sets the last error to ERROR_ALREADY_EXISTS but
|
|
|
|
the CreateFile operation succeeds */
|
|
|
|
if (GetLastError() == ERROR_ALREADY_EXISTS) {
|
|
|
|
SetLastError(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( hFile == INVALID_HANDLE_VALUE ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!buf) {
|
|
|
|
SYSTEMTIME st;
|
|
|
|
GetSystemTime(&st);
|
|
|
|
SystemTimeToFileTime(&st, &mtime);
|
|
|
|
atime = mtime;
|
|
|
|
} else {
|
|
|
|
UnixTimeToFileTime(buf->modtime, &mtime);
|
|
|
|
UnixTimeToFileTime(buf->actime, &atime);
|
|
|
|
}
|
|
|
|
if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
#endif
|