1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-09-28 02:15:34 +02:00

Cleanup code around tox ID/pk conversion functions

This commit is contained in:
jfreegman 2021-12-06 10:46:19 -05:00
parent afbd185222
commit e2c8497da9
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
6 changed files with 60 additions and 29 deletions

View File

@ -381,8 +381,11 @@ static long long int extract_val_last_pinged(const char *s)
* Return number of bytes copied to key_buf on success. * Return number of bytes copied to key_buf on success.
* Return -1 on failure. * Return -1 on failure.
*/ */
static int extract_val_pk(const char *s, char *key_buf) static int extract_val_pk(const char *s, char *key_buf, size_t buf_length)
{ {
if (buf_length < TOX_PUBLIC_KEY_SIZE * 2 + 1) {
return -1;
}
int key_len = char_find(0, s, '"'); int key_len = char_find(0, s, '"');
@ -445,13 +448,13 @@ static int extract_node(const char *line, struct Node *node)
} }
char key_string[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char key_string[TOX_PUBLIC_KEY_SIZE * 2 + 1];
int key_len = extract_val_pk(key_start + PK_JSON_KEY_LEN, key_string); int key_len = extract_val_pk(key_start + PK_JSON_KEY_LEN, key_string, sizeof(key_string));
if (key_len == -1) { if (key_len == -1) {
return -6; return -6;
} }
if (hex_string_to_bin(key_string, key_len, node->key, TOX_PUBLIC_KEY_SIZE) == -1) { if (tox_pk_string_to_bytes(key_string, key_len, node->key, sizeof(node->key)) == -1) {
return -6; return -6;
} }

View File

@ -510,8 +510,7 @@ static void conference_update_name_list(uint32_t conferencenum)
if (peer->active) { if (peer->active) {
memcpy(entry->name, peer->name, peer->name_length + 1); memcpy(entry->name, peer->name, peer->name_length + 1);
bin_pubkey_to_string(peer->pubkey, sizeof(peer->pubkey), tox_pk_bytes_to_str(peer->pubkey, sizeof(peer->pubkey), entry->pubkey_str, sizeof(entry->pubkey_str));
entry->pubkey_str, sizeof(entry->pubkey_str));
entry->peernum = i; entry->peernum = i;
++count; ++count;
} }

View File

@ -276,9 +276,9 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)
return; return;
} }
char key_binary[TOX_PUBLIC_KEY_SIZE * 2 + 1]; char key_binary[TOX_PUBLIC_KEY_SIZE];
if (hex_string_to_bin(ascii_key, strlen(ascii_key), key_binary, TOX_PUBLIC_KEY_SIZE) == -1) { if (tox_pk_string_to_bytes(ascii_key, strlen(ascii_key), key_binary, sizeof(key_binary)) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid key."); line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid key.");
return; return;
} }
@ -520,7 +520,7 @@ void cmd_myid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
char bin_id[TOX_ADDRESS_SIZE]; char bin_id[TOX_ADDRESS_SIZE];
tox_self_get_address(m, (uint8_t *) bin_id); tox_self_get_address(m, (uint8_t *) bin_id);
if (bin_id_to_string(bin_id, sizeof(bin_id), id_string, sizeof(id_string)) == -1) { if (tox_id_bytes_to_str(bin_id, sizeof(bin_id), id_string, sizeof(id_string)) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to print ID."); line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to print ID.");
return; return;
} }
@ -537,7 +537,7 @@ void cmd_myqr(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
char bin_id[TOX_ADDRESS_SIZE]; char bin_id[TOX_ADDRESS_SIZE];
tox_self_get_address(m, (uint8_t *) bin_id); tox_self_get_address(m, (uint8_t *) bin_id);
if (bin_id_to_string(bin_id, sizeof(bin_id), id_string, sizeof(id_string)) == -1) { if (tox_id_bytes_to_str(bin_id, sizeof(bin_id), id_string, sizeof(id_string)) == -1) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code."); line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Failed to create QR code.");
return; return;
} }

View File

@ -137,15 +137,18 @@ void get_elapsed_time_str(char *buf, int bufsize, time_t secs)
} }
/* /*
* Converts a hexidecimal string of length hex_len to binary format and puts the result in output. * Converts a hexidecimal string representation of a Tox public key to binary format and puts
* output_size must be exactly half of hex_len. * the result in output.
*
* `hex_len` must be exactly TOX_PUBLIC_KEY_SIZE * 2, and `output_size` must have room
* for TOX_PUBLIC_KEY_SIZE bytes.
* *
* Returns 0 on success. * Returns 0 on success.
* Returns -1 on failure. * Returns -1 on failure.
*/ */
int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size_t output_size) int tox_pk_string_to_bytes(const char *hex_string, size_t hex_len, char *output, size_t output_size)
{ {
if (output_size == 0 || hex_len != output_size * 2) { if (output_size != TOX_PUBLIC_KEY_SIZE || hex_len > output_size * 2) {
return -1; return -1;
} }
@ -157,6 +160,11 @@ int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size
return 0; return 0;
} }
/* Convert a hexadecimcal string of length `size` to bytes and puts the result in `keystr`.
*
* Returns 0 on success.
* Returns -1 on failure.
*/
int hex_string_to_bytes(char *buf, int size, const char *keystr) int hex_string_to_bytes(char *buf, int size, const char *keystr)
{ {
if (size % 2 != 0) { if (size % 2 != 0) {
@ -178,11 +186,14 @@ int hex_string_to_bytes(char *buf, int size, const char *keystr)
} }
/* Converts a binary representation of a Tox ID into a string. /* Converts a binary representation of a Tox ID into a string.
*
* `bin_id_size` must be exactly TOX_ADDRESS_SIZE bytes in length, and
* `output_size` must be at least TOX_ADDRESS_SIZE * 2 + 1.
* *
* Returns 0 on success. * Returns 0 on success.
* Returns -1 on failure. * Returns -1 on failure.
*/ */
int bin_id_to_string(const char *bin_id, size_t bin_id_size, char *output, size_t output_size) int tox_id_bytes_to_str(const char *bin_id, size_t bin_id_size, char *output, size_t output_size)
{ {
if (bin_id_size != TOX_ADDRESS_SIZE || output_size < (TOX_ADDRESS_SIZE * 2 + 1)) { if (bin_id_size != TOX_ADDRESS_SIZE || output_size < (TOX_ADDRESS_SIZE * 2 + 1)) {
return -1; return -1;
@ -196,11 +207,14 @@ int bin_id_to_string(const char *bin_id, size_t bin_id_size, char *output, size_
} }
/* Converts a binary representation of a Tox public key into a string. /* Converts a binary representation of a Tox public key into a string.
*
* `bin_pubkey_size` must be exactly TOX_PUBLIC_KEY_SIZE bytes in size, and
* `output_size` must be at least TOX_PUBLIC_KEY_SIZE * 2 + 1.
* *
* Returns 0 on success. * Returns 0 on success.
* Returns -1 on failure. * Returns -1 on failure.
*/ */
int bin_pubkey_to_string(const uint8_t *bin_pubkey, size_t bin_pubkey_size, char *output, size_t output_size) int tox_pk_bytes_to_str(const uint8_t *bin_pubkey, size_t bin_pubkey_size, char *output, size_t output_size)
{ {
if (bin_pubkey_size != TOX_PUBLIC_KEY_SIZE || output_size < (TOX_PUBLIC_KEY_SIZE * 2 + 1)) { if (bin_pubkey_size != TOX_PUBLIC_KEY_SIZE || output_size < (TOX_PUBLIC_KEY_SIZE * 2 + 1)) {
return -1; return -1;

View File

@ -53,30 +53,43 @@ void clear_screen(void);
void hst_to_net(uint8_t *num, uint16_t numbytes); void hst_to_net(uint8_t *num, uint16_t numbytes);
/* /*
* Converts a hexidecimal string of length hex_len to binary format and puts the result in output. * Converts a hexidecimal string representation of a Tox public key to binary format and puts
* output_size must be exactly half of hex_len. * the result in output.
*
* `hex_len` must be exactly TOX_PUBLIC_KEY_SIZE * 2, and `output_size` must have room
* for TOX_PUBLIC_KEY_SIZE bytes.
* *
* Returns 0 on success. * Returns 0 on success.
* Returns -1 on failure. * Returns -1 on failure.
*/ */
int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size_t output_size); int tox_pk_string_to_bytes(const char *hex_string, size_t hex_len, char *output, size_t output_size);
/* convert a hex string to bytes. returns 0 on success, -1 on failure */ /* Converts a binary representation of a Tox public key into a string.
*
* `bin_pubkey_size` must be exactly TOX_PUBLIC_KEY_SIZE bytes in size, and
* `output_size` must be at least TOX_PUBLIC_KEY_SIZE * 2 + 1.
*
* Returns 0 on success.
* Returns -1 on failure.
*/
int tox_pk_bytes_to_str(const uint8_t *bin_pubkey, size_t bin_pubkey_size, char *output, size_t output_size);
/* Convert a hexadecimcal string of length `size` to bytes and puts the result in `keystr`.
*
* Returns 0 on success.
* Returns -1 on failure.
*/
int hex_string_to_bytes(char *buf, int size, const char *keystr); int hex_string_to_bytes(char *buf, int size, const char *keystr);
/* Converts a binary representation of a Tox ID into a string. /* Converts a binary representation of a Tox ID into a string.
* *
* Returns 0 on success. * `bin_id_size` must be exactly TOX_ADDRESS_SIZE bytes in length, and
* Returns -1 on failure. * `output_size` must be at least TOX_ADDRESS_SIZE * 2 + 1.
*/
int bin_id_to_string(const char *bin_id, size_t bin_id_size, char *output, size_t output_size);
/* Converts a binary representation of a Tox public key into a string.
* *
* Returns 0 on success. * Returns 0 on success.
* Returns -1 on failure. * Returns -1 on failure.
*/ */
int bin_pubkey_to_string(const uint8_t *bin_pubkey, size_t bin_pubkey_size, char *output, size_t output_size); int tox_id_bytes_to_str(const char *bin_id, size_t bin_id_size, char *output, size_t output_size);
/* get the current unix time (not thread safe) */ /* get the current unix time (not thread safe) */
time_t get_unix_time(void); time_t get_unix_time(void);

View File

@ -132,8 +132,10 @@ static int load_nameserver_list(const char *path)
continue; continue;
} }
snprintf(Nameservers.names[Nameservers.lines], sizeof(Nameservers.names[Nameservers.lines]), "%s", name); const size_t idx = Nameservers.lines;
int res = hex_string_to_bytes(Nameservers.keys[Nameservers.lines], SERVER_KEY_SIZE, keystr); snprintf(Nameservers.names[idx], sizeof(Nameservers.names[idx]), "%s", name);
int res = hex_string_to_bytes(Nameservers.keys[idx], SERVER_KEY_SIZE, keystr);
if (res == -1) { if (res == -1) {
continue; continue;
@ -231,7 +233,7 @@ static int process_response(struct Recv_Curl_Data *recv_data)
memcpy(ID_string, IDstart + prefix_size, TOX_ADDRESS_SIZE * 2); memcpy(ID_string, IDstart + prefix_size, TOX_ADDRESS_SIZE * 2);
ID_string[TOX_ADDRESS_SIZE * 2] = 0; ID_string[TOX_ADDRESS_SIZE * 2] = 0;
if (hex_string_to_bin(ID_string, strlen(ID_string), t_data.id_bin, sizeof(t_data.id_bin)) == -1) { if (tox_pk_string_to_bytes(ID_string, strlen(ID_string), t_data.id_bin, sizeof(t_data.id_bin)) == -1) {
return -1; return -1;
} }