Skip to content

Commit d6409b2

Browse files
committed
Zend: Add bin2hex helpers
1 parent 77cd291 commit d6409b2

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

UPGRADING.INTERNALS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ PHP 8.6 INTERNALS UPGRADE NOTES
6666
. Added Z_PARAM_ENUM().
6767
. Added zend_enum_fetch_case_id().
6868
. Added zend_enum_get_case_by_id().
69+
. Added zend_bin2hex() and zend_bin2hex_str() as helper functions to remove
70+
dependencies on /ext/hash in various extensions.
6971
. ZEND_INI_GET_ADDR() is now a void* pointer instead of a char* pointer. This
7072
more correctly represents the generic nature of the returned pointer and
7173
allows to remove explicit casts, but possibly breaks pointer arithmetic

Zend/zend_string.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ ZEND_API zend_string *zend_empty_string = NULL;
5252
ZEND_API zend_string *zend_one_char_string[256];
5353
ZEND_API zend_string **zend_known_strings = NULL;
5454

55+
/* this is read-only, so it's ok */
56+
ZEND_SET_ALIGNED(16, static const char zend_hexconvtab_lower[]) = "0123456789abcdef";
57+
58+
static zend_always_inline void zend_bin2hex_impl(char *out, const unsigned char *in, size_t in_len, const char *hexconvtab)
59+
{
60+
for (size_t i = 0; i < in_len; i++) {
61+
out[i * 2] = hexconvtab[in[i] >> 4];
62+
out[i * 2 + 1] = hexconvtab[in[i] & 0x0f];
63+
}
64+
}
65+
5566
ZEND_API zend_ulong ZEND_FASTCALL zend_string_hash_func(zend_string *str)
5667
{
5768
return ZSTR_H(str) = zend_hash_func(ZSTR_VAL(str), ZSTR_LEN(str));
@@ -62,6 +73,21 @@ ZEND_API zend_ulong ZEND_FASTCALL zend_hash_func(const char *str, size_t len)
6273
return zend_inline_hash_func(str, len);
6374
}
6475

76+
ZEND_API void ZEND_FASTCALL zend_bin2hex(char *out, const unsigned char *in, size_t in_len)
77+
{
78+
zend_bin2hex_impl(out, in, in_len, zend_hexconvtab_lower);
79+
}
80+
81+
ZEND_API zend_string *zend_bin2hex_str(const unsigned char *in, size_t in_len)
82+
{
83+
zend_string *result = zend_string_safe_alloc(in_len, 2 * sizeof(char), 0, 0);
84+
85+
zend_bin2hex(ZSTR_VAL(result), in, in_len);
86+
ZSTR_VAL(result)[in_len * 2] = '\0';
87+
88+
return result;
89+
}
90+
6591
static void _str_dtor(zval *zv)
6692
{
6793
zend_string *str = Z_STR_P(zv);

Zend/zend_string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ ZEND_API extern zend_string_init_existing_interned_func_t zend_string_init_exist
3737
ZEND_API zend_ulong ZEND_FASTCALL zend_string_hash_func(zend_string *str);
3838
ZEND_API zend_ulong ZEND_FASTCALL zend_hash_func(const char *str, size_t len);
3939
ZEND_API zend_string* ZEND_FASTCALL zend_interned_string_find_permanent(zend_string *str);
40+
ZEND_API void ZEND_FASTCALL zend_bin2hex(char *out, const unsigned char *in, size_t in_len);
41+
ZEND_API zend_string *zend_bin2hex_str(const unsigned char *in, size_t in_len);
4042

4143
ZEND_API zend_string *zend_string_concat2(
4244
const char *str1, size_t str1_len,

0 commit comments

Comments
 (0)