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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ PHP 8.6 INTERNALS UPGRADE NOTES
zend_enum_RoundingMode parameter.
. Added Z_PARAM_ENUM().
. Added zend_enum_fetch_case_id().
. Added zend_bin2hex(), zend_bin2hex_upper() and zend_bin2hex_str() as helper
functions to remove dependencies on /ext/hash in various extensions.
. ZEND_INI_GET_ADDR() is now a void* pointer instead of a char* pointer. This
more correctly represents the generic nature of the returned pointer and
allows to remove explicit casts, but possibly breaks pointer arithmetic
Expand Down
32 changes: 32 additions & 0 deletions Zend/zend_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ ZEND_API zend_string *zend_empty_string = NULL;
ZEND_API zend_string *zend_one_char_string[256];
ZEND_API zend_string **zend_known_strings = NULL;

/* this is read-only, so it's ok */
ZEND_SET_ALIGNED(16, static const char zend_hexconvtab_lower[]) = "0123456789abcdef";
ZEND_SET_ALIGNED(16, static const char zend_hexconvtab_upper[]) = "0123456789ABCDEF";

static zend_always_inline void zend_bin2hex_impl(char *out, const unsigned char *in, size_t in_len, const char *hexconvtab)
{
for (size_t i = 0; i < in_len; i++) {
out[i * 2] = hexconvtab[in[i] >> 4];
out[i * 2 + 1] = hexconvtab[in[i] & 0x0f];
}
}

ZEND_API zend_ulong ZEND_FASTCALL zend_string_hash_func(zend_string *str)
{
return ZSTR_H(str) = zend_hash_func(ZSTR_VAL(str), ZSTR_LEN(str));
Expand All @@ -62,6 +74,26 @@ ZEND_API zend_ulong ZEND_FASTCALL zend_hash_func(const char *str, size_t len)
return zend_inline_hash_func(str, len);
}

ZEND_API void ZEND_FASTCALL zend_bin2hex(char *out, const unsigned char *in, size_t in_len)
{
zend_bin2hex_impl(out, in, in_len, zend_hexconvtab_lower);
}

ZEND_API void ZEND_FASTCALL zend_bin2hex_upper(char *out, const unsigned char *in, size_t in_len)
{
zend_bin2hex_impl(out, in, in_len, zend_hexconvtab_upper);
}

ZEND_API zend_string *zend_bin2hex_str(const unsigned char *in, size_t in_len)
{
zend_string *result = zend_string_safe_alloc(in_len, 2 * sizeof(char), 0, 0);

zend_bin2hex(ZSTR_VAL(result), in, in_len);
ZSTR_VAL(result)[in_len * 2] = '\0';

return result;
}

static void _str_dtor(zval *zv)
{
zend_string *str = Z_STR_P(zv);
Expand Down
3 changes: 3 additions & 0 deletions Zend/zend_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ ZEND_API extern zend_string_init_existing_interned_func_t zend_string_init_exist
ZEND_API zend_ulong ZEND_FASTCALL zend_string_hash_func(zend_string *str);
ZEND_API zend_ulong ZEND_FASTCALL zend_hash_func(const char *str, size_t len);
ZEND_API zend_string* ZEND_FASTCALL zend_interned_string_find_permanent(zend_string *str);
ZEND_API void ZEND_FASTCALL zend_bin2hex(char *out, const unsigned char *in, size_t in_len);
ZEND_API void ZEND_FASTCALL zend_bin2hex_upper(char *out, const unsigned char *in, size_t in_len);
ZEND_API zend_string *zend_bin2hex_str(const unsigned char *in, size_t in_len);

ZEND_API zend_string *zend_string_concat2(
const char *str1, size_t str1_len,
Expand Down