Skip to content

Commit d8df3ae

Browse files
committed
Add header dependencies in winutil header
1 parent f96ce00 commit d8df3ae

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
lines changed

TSRM/tsrm_win32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ TSRM_API int pclose(FILE *stream)
617617
/* Returns a number between 0x2000_0000 and 0x3fff_ffff. On Windows, key_t is int. */
618618
static key_t tsrm_choose_random_shm_key(key_t prev_key) {
619619
unsigned char buf[4];
620-
if (php_win32_get_random_bytes(buf, 4) != SUCCESS) {
620+
if (!php_win32_get_random_bytes(buf, 4)) {
621621
return prev_key + 2;
622622
}
623623
uint32_t n =

ext/random/csprng.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_random_bytes_ex(void *bytes, size_
7070
{
7171
#ifdef PHP_WIN32
7272
/* Defer to CryptGenRandom on Windows */
73-
if (php_win32_get_random_bytes(bytes, size) == FAILURE) {
73+
if (!php_win32_get_random_bytes(bytes, size)) {
7474
snprintf(errstr, errstr_size, "Failed to retrieve randomness from the operating system (BCryptGenRandom)");
7575
return FAILURE;
7676
}

win32/winutil.c

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
+----------------------------------------------------------------------+
1616
*/
1717

18-
#include "php.h"
1918
#include "winutil.h"
19+
#include "ioutil.h"
2020
#include "codepage.h"
2121
#include <bcrypt.h>
2222
#include <lmcons.h>
@@ -54,31 +54,31 @@ PHP_WINUTIL_API void php_win32_error_msg_free(char *msg)
5454
}
5555
}/*}}}*/
5656

57-
int php_win32_check_trailing_space(const char * path, const size_t path_len)
57+
bool php_win32_check_trailing_space(const char * path, const size_t path_len)
5858
{/*{{{*/
5959
if (path_len > MAXPATHLEN - 1) {
60-
return 1;
60+
return true;
6161
}
6262
if (path) {
6363
if (path[0] == ' ' || path[path_len - 1] == ' ') {
64-
return 0;
64+
return false;
6565
} else {
66-
return 1;
66+
return true;
6767
}
6868
} else {
69-
return 0;
69+
return false;
7070
}
7171
}/*}}}*/
7272

7373
static BCRYPT_ALG_HANDLE bcrypt_algo;
74-
static BOOL has_bcrypt_algo = 0;
74+
static bool has_bcrypt_algo = false;
7575

7676
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
7777

7878
#ifdef PHP_EXPORTS
79-
BOOL php_win32_shutdown_random_bytes(void)
79+
bool php_win32_shutdown_random_bytes(void)
8080
{/*{{{*/
81-
BOOL ret = TRUE;
81+
bool ret = true;
8282

8383
if (has_bcrypt_algo) {
8484
ret = NT_SUCCESS(BCryptCloseAlgorithmProvider(bcrypt_algo, 0));
@@ -88,10 +88,10 @@ BOOL php_win32_shutdown_random_bytes(void)
8888
return ret;
8989
}/*}}}*/
9090

91-
BOOL php_win32_init_random_bytes(void)
91+
bool php_win32_init_random_bytes(void)
9292
{/*{{{*/
9393
if (has_bcrypt_algo) {
94-
return TRUE;
94+
return true;
9595
}
9696

9797
has_bcrypt_algo = NT_SUCCESS(BCryptOpenAlgorithmProvider(&bcrypt_algo, BCRYPT_RNG_ALGORITHM, NULL, 0));
@@ -100,24 +100,22 @@ BOOL php_win32_init_random_bytes(void)
100100
}/*}}}*/
101101
#endif
102102

103-
PHP_WINUTIL_API int php_win32_get_random_bytes(unsigned char *buf, size_t size)
103+
PHP_WINUTIL_API bool php_win32_get_random_bytes(unsigned char *buf, size_t size)
104104
{ /* {{{ */
105105

106-
BOOL ret;
107-
108106
#if 0
109107
/* Currently we fail on startup, with CNG API it shows no regressions so far and is secure.
110108
Should switch on and try to reinit, if it fails too often on startup. This means also
111109
bringing locks back. */
112-
if (has_bcrypt_algo == 0) {
113-
return FAILURE;
110+
if (!has_bcrypt_algo) {
111+
return false;
114112
}
115113
#endif
116114

117115
/* No sense to loop here, the limit is huge enough. */
118-
ret = NT_SUCCESS(BCryptGenRandom(bcrypt_algo, buf, (ULONG)size, 0));
116+
bool ret = NT_SUCCESS(BCryptGenRandom(bcrypt_algo, buf, (ULONG)size, 0));
119117

120-
return ret ? SUCCESS : FAILURE;
118+
return ret;
121119
}
122120
/* }}} */
123121

@@ -439,7 +437,7 @@ PHP_WINUTIL_API char *php_win32_get_username(void)
439437
return uname;
440438
}/*}}}*/
441439

442-
static zend_always_inline BOOL is_compatible(HMODULE handle, BOOL is_smaller, char *format, char **err)
440+
static __forceinline bool is_compatible(HMODULE handle, bool is_smaller, char *format, char **err)
443441
{/*{{{*/
444442
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER) handle;
445443
PIMAGE_NT_HEADERS pNTHeader = (PIMAGE_NT_HEADERS)((char *) dosHeader + dosHeader->e_lfanew);
@@ -470,19 +468,19 @@ static zend_always_inline BOOL is_compatible(HMODULE handle, BOOL is_smaller, ch
470468
} else {
471469
spprintf(err, 0, "Can't retrieve the module name (error %u)", GetLastError());
472470
}
473-
return FALSE;
471+
return false;
474472
}
475473

476-
return TRUE;
474+
return true;
477475
}/*}}}*/
478476

479-
PHP_WINUTIL_API BOOL php_win32_image_compatible(HMODULE handle, char **err)
477+
PHP_WINUTIL_API bool php_win32_image_compatible(HMODULE handle, char **err)
480478
{/*{{{*/
481-
return is_compatible(handle, TRUE, "Can't load module '%s' as it's linked with %u.%u, but the core is linked with %d.%d", err);
479+
return is_compatible(handle, true, "Can't load module '%s' as it's linked with %u.%u, but the core is linked with %d.%d", err);
482480
}/*}}}*/
483481

484482
/* Expect a CRT module handle */
485-
PHP_WINUTIL_API BOOL php_win32_crt_compatible(char **err)
483+
PHP_WINUTIL_API bool php_win32_crt_compatible(char **err)
486484
{/*{{{*/
487485
#if PHP_LINKER_MAJOR == 14
488486
/* Extend for other CRT if needed. */
@@ -494,9 +492,9 @@ PHP_WINUTIL_API BOOL php_win32_crt_compatible(char **err)
494492
HMODULE handle = GetModuleHandle(crt_name);
495493
if (handle == NULL) {
496494
spprintf(err, 0, "Can't get handle of module %s (error %u)", crt_name, GetLastError());
497-
return FALSE;
495+
return false;
498496
}
499-
return is_compatible(handle, FALSE, "'%s' %u.%u is not compatible with this PHP build linked with %d.%d", err);
497+
return is_compatible(handle, false, "'%s' %u.%u is not compatible with this PHP build linked with %d.%d", err);
500498
#endif
501-
return TRUE;
499+
return true;
502500
}/*}}}*/

win32/winutil.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@
2323
# define PHP_WINUTIL_API __declspec(dllimport)
2424
#endif
2525

26+
#include <stddef.h>
27+
#include <stdbool.h>
28+
#include <windows.h>
29+
#include <winerror.h>
30+
2631
PHP_WINUTIL_API char *php_win32_error_to_msg(HRESULT error);
2732
PHP_WINUTIL_API void php_win32_error_msg_free(char *msg);
2833

2934
#define php_win_err() php_win32_error_to_msg(GetLastError())
3035
#define php_win_err_free(err) php_win32_error_msg_free(err)
31-
int php_win32_check_trailing_space(const char * path, const size_t path_len);
32-
PHP_WINUTIL_API int php_win32_get_random_bytes(unsigned char *buf, size_t size);
36+
bool php_win32_check_trailing_space(const char * path, const size_t path_len);
37+
PHP_WINUTIL_API bool php_win32_get_random_bytes(unsigned char *buf, size_t size);
3338
#ifdef PHP_EXPORTS
34-
BOOL php_win32_init_random_bytes(void);
35-
BOOL php_win32_shutdown_random_bytes(void);
39+
bool php_win32_init_random_bytes(void);
40+
bool php_win32_shutdown_random_bytes(void);
3641
#endif
3742

3843
#if !defined(ECURDIR)
@@ -53,7 +58,7 @@ PHP_WINUTIL_API int php_win32_code_to_errno(unsigned long w32Err);
5358

5459
PHP_WINUTIL_API char *php_win32_get_username(void);
5560

56-
PHP_WINUTIL_API BOOL php_win32_image_compatible(HMODULE handle, char **err);
57-
PHP_WINUTIL_API BOOL php_win32_crt_compatible(char **err);
61+
PHP_WINUTIL_API bool php_win32_image_compatible(HMODULE handle, char **err);
62+
PHP_WINUTIL_API bool php_win32_crt_compatible(char **err);
5863

5964
#endif

0 commit comments

Comments
 (0)