1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-08 21:18:16 +03:00

lib/util: Speed up slow data-blob-to-hex functions

This is much faster than calling sprintf() for every byte of data, and
improves the performance of functions outputting binary DNs.

Signed-off-by: Jo Sutton <josutton@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
This commit is contained in:
Jo Sutton 2023-03-01 14:50:45 +13:00 committed by Douglas Bagnall
parent 8ab5a032d7
commit 6b6413da93
2 changed files with 60 additions and 4 deletions

View File

@ -171,8 +171,10 @@ _PUBLIC_ char *data_blob_hex_string_lower(TALLOC_CTX *mem_ctx, const DATA_BLOB *
/* this must be lowercase or w2k8 cannot join a samba domain,
as this routine is used to encode extended DNs and windows
only accepts lowercase hexadecimal numbers */
for (i = 0; i < blob->length; i++)
slprintf(&hex_string[i*2], 3, "%02x", blob->data[i]);
for (i = 0; i < blob->length; i++) {
hex_string[i * 2] = nybble_to_hex_lower(blob->data[i] >> 4);
hex_string[i * 2 + 1] = nybble_to_hex_lower(blob->data[i]);
}
hex_string[(blob->length*2)] = '\0';
return hex_string;
@ -188,8 +190,10 @@ _PUBLIC_ char *data_blob_hex_string_upper(TALLOC_CTX *mem_ctx, const DATA_BLOB *
return NULL;
}
for (i = 0; i < blob->length; i++)
slprintf(&hex_string[i*2], 3, "%02X", blob->data[i]);
for (i = 0; i < blob->length; i++) {
hex_string[i * 2] = nybble_to_hex_upper(blob->data[i] >> 4);
hex_string[i * 2 + 1] = nybble_to_hex_upper(blob->data[i]);
}
hex_string[(blob->length*2)] = '\0';
return hex_string;

View File

@ -650,4 +650,56 @@ struct tevent_context *samba_tevent_context_init(TALLOC_CTX *mem_ctx);
*/
void samba_tevent_set_debug(struct tevent_context *ev, const char *name);
static inline char nybble_to_hex_lower(uint8_t val)
{
uint8_t nybble = val & 0xf;
switch (nybble) {
case 0x0: case 0x1: case 0x2: case 0x3: case 0x4:
case 0x5: case 0x6: case 0x7: case 0x8: case 0x9:
return '0' + nybble;
case 0xa:
return 'a';
case 0xb:
return 'b';
case 0xc:
return 'c';
case 0xd:
return 'd';
case 0xe:
return 'e';
case 0xf:
return 'f';
}
/* unreachable */
return '\0';
}
static inline char nybble_to_hex_upper(uint8_t val)
{
uint8_t nybble = val & 0xf;
switch (nybble) {
case 0x0: case 0x1: case 0x2: case 0x3: case 0x4:
case 0x5: case 0x6: case 0x7: case 0x8: case 0x9:
return '0' + nybble;
case 0xa:
return 'A';
case 0xb:
return 'B';
case 0xc:
return 'C';
case 0xd:
return 'D';
case 0xe:
return 'E';
case 0xf:
return 'F';
}
/* unreachable */
return '\0';
}
#endif /* _SAMBA_UTIL_H_ */