1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-01 07:16:37 +02:00

api: add functions string_base_{encode,decode}, remove functions string_{encode,decode}_base64

This commit is contained in:
Sébastien Helleu
2018-11-04 00:30:57 +01:00
parent a8b6fa08b7
commit ed3f281ba9
19 changed files with 280 additions and 149 deletions
+1 -1
View File
@@ -267,7 +267,7 @@ network_pass_httpproxy (struct t_proxy *proxy, int sock, const char *address,
snprintf (authbuf, sizeof (authbuf), "%s:%s", username, password);
free (username);
free (password);
if (string_encode_base64 (authbuf, strlen (authbuf), authbuf_base64) < 0)
if (string_base64_encode (authbuf, strlen (authbuf), authbuf_base64) < 0)
return 0;
length = snprintf (buffer, sizeof (buffer),
"CONNECT %s:%d HTTP/1.0\r\nProxy-Authorization: "
+2 -2
View File
@@ -274,7 +274,7 @@ secure_config_data_read_cb (const void *pointer, void *data,
if (!buffer)
return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
length_buffer = string_decode_base16 (value, buffer);
length_buffer = string_base16_decode (value, buffer);
while (1)
{
decrypted = NULL;
@@ -370,7 +370,7 @@ secure_config_data_write_map_cb (void *data,
buffer_base16 = malloc ((length_buffer * 2) + 1);
if (buffer_base16)
{
if (string_encode_base16 (buffer, length_buffer,
if (string_base16_encode (buffer, length_buffer,
buffer_base16) >= 0)
{
config_file_write_line (config_file, key,
+3 -3
View File
@@ -466,7 +466,7 @@ secure_decrypt_data_not_decrypted (const char *passphrase)
buffer = malloc (strlen (value) + 1);
if (buffer)
{
length_buffer = string_decode_base16 (value, buffer);
length_buffer = string_base16_decode (value, buffer);
decrypted = NULL;
length_decrypted = 0;
rc = secure_decrypt_data (
@@ -595,7 +595,7 @@ secure_totp_generate (const char *secret_base32, time_t totp_time, int digits)
if (!secret)
goto error;
length_secret = string_decode_base32 (secret_base32, secret);
length_secret = string_base32_decode (secret_base32, secret);
if (length_secret < 0)
goto error;
@@ -654,7 +654,7 @@ secure_totp_validate (const char *secret_base32, time_t totp_time, int window,
if (!secret)
goto error;
length_secret = string_decode_base32 (secret_base32, secret);
length_secret = string_base32_decode (secret_base32, secret);
if (length_secret < 0)
goto error;
+6 -6
View File
@@ -2707,7 +2707,7 @@ string_format_size (unsigned long long size)
*/
int
string_encode_base16 (const char *from, int length, char *to)
string_base16_encode (const char *from, int length, char *to)
{
int i, count;
const char *hexa = "0123456789ABCDEF";
@@ -2734,7 +2734,7 @@ string_encode_base16 (const char *from, int length, char *to)
*/
int
string_decode_base16 (const char *from, char *to)
string_base16_decode (const char *from, char *to)
{
int length, i, pos, count;
unsigned char value;
@@ -2803,7 +2803,7 @@ string_decode_base16 (const char *from, char *to)
*/
int
string_encode_base32 (const char *from, int length, char *to)
string_base32_encode (const char *from, int length, char *to)
{
unsigned char base32_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
int count, value, next, bits_left, pad, index;
@@ -2880,7 +2880,7 @@ string_encode_base32 (const char *from, int length, char *to)
*/
int
string_decode_base32 (const char *from, char *to)
string_base32_decode (const char *from, char *to)
{
const char *ptr_from;
int value, bits_left, count;
@@ -2956,7 +2956,7 @@ string_convbase64_8x3_to_6x4 (const char *from, char *to)
*/
int
string_encode_base64 (const char *from, int length, char *to)
string_base64_encode (const char *from, int length, char *to)
{
const char *ptr_from;
char rest[3];
@@ -3027,7 +3027,7 @@ string_convbase64_6x4_to_8x3 (const unsigned char *from, unsigned char *to)
*/
int
string_decode_base64 (const char *from, char *to)
string_base64_decode (const char *from, char *to)
{
const char *ptr_from;
int length, to_length, i;
+6 -6
View File
@@ -105,12 +105,12 @@ extern char *string_iconv_from_internal (const char *charset,
const char *string);
extern int string_fprintf (FILE *file, const char *data, ...);
extern char *string_format_size (unsigned long long size);
extern int string_encode_base16 (const char *from, int length, char *to);
extern int string_decode_base16 (const char *from, char *to);
extern int string_encode_base32 (const char *from, int length, char *to);
extern int string_decode_base32 (const char *from, char *to);
extern int string_encode_base64 (const char *from, int length, char *to);
extern int string_decode_base64 (const char *from, char *to);
extern int string_base16_encode (const char *from, int length, char *to);
extern int string_base16_decode (const char *from, char *to);
extern int string_base32_encode (const char *from, int length, char *to);
extern int string_base32_decode (const char *from, char *to);
extern int string_base64_encode (const char *from, int length, char *to);
extern int string_base64_decode (const char *from, char *to);
extern char *string_hex_dump (const char *data, int data_size,
int bytes_per_line,
const char *prefix, const char *suffix);
+1 -1
View File
@@ -2509,7 +2509,7 @@ gui_window_send_clipboard (const char *storage_unit, const char *text)
text_base64 = malloc ((length * 4) + 1);
if (text_base64)
{
if (string_encode_base64 (text, length, text_base64) >= 0)
if (string_base64_encode (text, length, text_base64) >= 0)
{
fprintf (stderr, "\033]52;%s;%s\a",
(storage_unit) ? storage_unit : "",
+12 -11
View File
@@ -73,8 +73,8 @@ irc_sasl_mechanism_plain (const char *sasl_username, const char *sasl_password)
answer_base64 = malloc (length * 4);
if (answer_base64)
{
if (weechat_string_encode_base64 (string, length - 1,
answer_base64) < 0)
if (weechat_string_base_encode (64, string, length - 1,
answer_base64) < 0)
{
free (answer_base64);
answer_base64 = NULL;
@@ -180,7 +180,7 @@ irc_sasl_mechanism_ecdsa_nist256p_challenge (struct t_irc_server *server,
data = malloc (strlen (data_base64) + 1);
if (!data)
return NULL;
length_data = weechat_string_decode_base64 (data_base64, data);
length_data = weechat_string_base_decode (64, data_base64, data);
/* read file with private key */
str_privkey = irc_sasl_get_key_content (server, sasl_key);
@@ -226,8 +226,8 @@ irc_sasl_mechanism_ecdsa_nist256p_challenge (struct t_irc_server *server,
pubkey_base64 = malloc ((x.size + 1 + 1) * 4);
if (pubkey_base64)
{
if (weechat_string_encode_base64 (pubkey, x.size + 1,
pubkey_base64) >= 0)
if (weechat_string_base_encode (64, pubkey, x.size + 1,
pubkey_base64) >= 0)
{
weechat_printf (
server->buffer,
@@ -299,7 +299,8 @@ irc_sasl_mechanism_ecdsa_nist256p_challenge (struct t_irc_server *server,
answer_base64 = malloc ((length + 1) * 4);
if (answer_base64)
{
if (weechat_string_encode_base64 (string, length, answer_base64) < 0)
if (weechat_string_base_encode (64, string, length,
answer_base64) < 0)
{
free (answer_base64);
answer_base64 = NULL;
@@ -364,7 +365,7 @@ irc_sasl_dh (const char *data_base64,
data = malloc (strlen (data_base64) + 1);
if (!data)
goto dhend;
length_data = weechat_string_decode_base64 (data_base64, data);
length_data = weechat_string_base_decode (64, data_base64, data);
ptr_data = (unsigned char *)data;
/* extract prime number */
@@ -520,8 +521,8 @@ irc_sasl_mechanism_dh_blowfish (const char *data_base64,
answer_base64 = malloc ((length_answer + 1) * 4);
if (answer_base64)
{
if (weechat_string_encode_base64 (answer, length_answer,
answer_base64) < 0)
if (weechat_string_base_encode (64, answer, length_answer,
answer_base64) < 0)
{
free (answer_base64);
answer_base64 = NULL;
@@ -649,8 +650,8 @@ irc_sasl_mechanism_dh_aes (const char *data_base64,
answer_base64 = malloc ((length_answer + 1) * 4);
if (answer_base64)
{
if (weechat_string_encode_base64 (answer, length_answer,
answer_base64) < 0)
if (weechat_string_base_encode (64, answer, length_answer,
answer_base64) < 0)
{
free (answer_base64);
answer_base64 = NULL;
+39
View File
@@ -94,6 +94,45 @@ plugin_api_ngettext (const char *single, const char *plural, int count)
return NG_(single, plural, count);
}
/*
* Encodes a string in base 16, 32, or 64.
*/
int
plugin_api_string_base_encode (int base, const char *from, int length,
char *to)
{
switch (base)
{
case 16:
return string_base16_encode (from, length, to);
case 32:
return string_base32_encode (from, length, to);
case 64:
return string_base64_encode (from, length, to);
}
return -1;
}
/*
* Decodes a string encoded in base 16, 32, or 64.
*/
int
plugin_api_string_base_decode (int base, const char *from, char *to)
{
switch (base)
{
case 16:
return string_base16_decode (from, to);
case 32:
return string_base32_decode (from, to);
case 64:
return string_base64_decode (from, to);
}
return -1;
}
/*
* Frees an option.
*/
+4
View File
@@ -28,6 +28,10 @@ extern void plugin_api_charset_set (struct t_weechat_plugin *plugin,
extern const char *plugin_api_gettext (const char *string);
extern const char *plugin_api_ngettext (const char *single, const char *plural,
int count);
extern int plugin_api_string_base_encode (int base, const char *from,
int length, char *to);
extern int plugin_api_string_base_decode (int base, const char *from,
char *to);
/* config */
extern void plugin_api_config_file_option_free (struct t_config_option *option);
+2 -2
View File
@@ -636,8 +636,8 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv)
new_plugin->string_free_split_command = &string_free_split_command;
new_plugin->string_format_size = &string_format_size;
new_plugin->string_remove_color = &gui_color_decode;
new_plugin->string_encode_base64 = &string_encode_base64;
new_plugin->string_decode_base64 = &string_decode_base64;
new_plugin->string_base_encode = &plugin_api_string_base_encode;
new_plugin->string_base_decode = &plugin_api_string_base_decode;
new_plugin->string_hex_dump = &string_hex_dump;
new_plugin->string_is_command_char = &string_is_command_char;
new_plugin->string_input_for_buffer = &string_input_for_buffer;
+2 -2
View File
@@ -214,8 +214,8 @@ relay_websocket_build_handshake (struct t_relay_client *client)
length = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
gcry_md_write (hd, key, strlen (key));
result = gcry_md_read (hd, GCRY_MD_SHA1);
if (weechat_string_encode_base64 ((char *)result, length,
sec_websocket_accept) < 0)
if (weechat_string_base_encode (64, (char *)result, length,
sec_websocket_accept) < 0)
{
sec_websocket_accept[0] = '\0';
}
+9 -7
View File
@@ -67,7 +67,7 @@ struct timeval;
* please change the date with current one; for a second change at same
* date, increment the 01, otherwise please keep 01.
*/
#define WEECHAT_PLUGIN_API_VERSION "20181102-01"
#define WEECHAT_PLUGIN_API_VERSION "20181104-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@@ -323,8 +323,9 @@ struct t_weechat_plugin
void (*string_free_split_command) (char **split_command);
char *(*string_format_size) (unsigned long long size);
char *(*string_remove_color) (const char *string, const char *replacement);
int (*string_encode_base64) (const char *from, int length, char *to);
int (*string_decode_base64) (const char *from, char *to);
int (*string_base_encode) (int base, const char *from, int length,
char *to);
int (*string_base_decode) (int base, const char *from, char *to);
char *(*string_hex_dump) (const char *data, int data_size,
int bytes_per_line, const char *prefix,
const char *suffix);
@@ -1224,10 +1225,11 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
(weechat_plugin->string_format_size)(__size)
#define weechat_string_remove_color(__string, __replacement) \
(weechat_plugin->string_remove_color)(__string, __replacement)
#define weechat_string_encode_base64(__from, __length, __to) \
(weechat_plugin->string_encode_base64)(__from, __length, __to)
#define weechat_string_decode_base64(__from, __to) \
(weechat_plugin->string_decode_base64)(__from, __to)
#define weechat_string_base_encode(__base, __from, __length, __to) \
(weechat_plugin->string_base_encode)(__base, __from, __length, \
__to)
#define weechat_string_base_decode(__base, __from, __to) \
(weechat_plugin->string_base_decode)(__base, __from, __to)
#define weechat_string_hex_dump(__data, __data_size, __bytes_per_line, \
__prefix, __suffix) \
(weechat_plugin->string_hex_dump)(__data, __data_size, \