mirror of
https://github.com/Tha14/toxic.git
synced 2025-07-05 20:16:44 +02:00
Lots of bug fixes and general code cleanup
This commit is contained in:
@ -109,22 +109,27 @@ void get_elapsed_time_str(char *buf, int bufsize, uint64_t secs)
|
||||
snprintf(buf, bufsize, "%ld:%.2ld:%.2ld", hours, minutes, seconds);
|
||||
}
|
||||
|
||||
char *hex_string_to_bin(const char *hex_string)
|
||||
/*
|
||||
* Converts a hexidecimal string of length hex_len to binary format and puts the result in output.
|
||||
* output_size must be exactly half of hex_len.
|
||||
*
|
||||
* Returns 0 on success.
|
||||
* Returns -1 on failure.
|
||||
*/
|
||||
int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size_t output_size)
|
||||
{
|
||||
size_t len = strlen(hex_string);
|
||||
char *val = malloc(len);
|
||||
if (output_size == 0 || hex_len != output_size * 2)
|
||||
return -1;
|
||||
|
||||
if (val == NULL)
|
||||
exit_toxic_err("failed in hex_string_to_bin", FATALERR_MEMORY);
|
||||
for (size_t i = 0; i < output_size; ++i) {
|
||||
sscanf(hex_string, "%2hhx", &output[i]);
|
||||
hex_string += 2;
|
||||
}
|
||||
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < len; ++i, hex_string += 2)
|
||||
sscanf(hex_string, "%2hhx", &val[i]);
|
||||
|
||||
return val;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int hex_string_to_bytes(char *buf, int size, const char *keystr)
|
||||
{
|
||||
if (size % 2 != 0)
|
||||
@ -147,6 +152,9 @@ int hex_string_to_bytes(char *buf, int size, const char *keystr)
|
||||
/* Returns 1 if the string is empty, 0 otherwise */
|
||||
int string_is_empty(const char *string)
|
||||
{
|
||||
if (!string)
|
||||
return true;
|
||||
|
||||
return string[0] == '\0';
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user