core: move functions string_base_encode and string_base_decode from plugin-api.c to wee-string.c

This commit is contained in:
Sébastien Helleu 2020-05-21 00:02:24 +02:00
parent 0ac936a5cf
commit 1994d5641d
6 changed files with 117 additions and 51 deletions

View File

@ -2917,7 +2917,8 @@ string_format_size (unsigned long long size)
* Argument "length" is number of bytes in "from" to convert (commonly
* strlen(from)).
*
* Returns length of string in "*to" (it does not count final \0).
* Returns length of string in "*to" (it does not count final \0),
* -1 if error.
*/
int
@ -2944,7 +2945,8 @@ string_base16_encode (const char *from, int length, char *to)
/*
* Decodes a base16 string (hexadecimal).
*
* Returns length of string in "*to" (it does not count final \0).
* Returns length of string in "*to" (it does not count final \0),
* -1 if error.
*/
int
@ -3013,7 +3015,8 @@ string_base16_decode (const char *from, char *to)
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Returns length of string in "*to" (it does not count final \0).
* Returns length of string in "*to" (it does not count final \0),
* -1 if error.
*/
int
@ -3090,7 +3093,8 @@ string_base32_encode (const char *from, int length, char *to)
* limitations under the License.
*
*
* Returns length of string in "*to" (it does not count final \0).
* Returns length of string in "*to" (it does not count final \0),
* -1 if error.
*/
int
@ -3166,7 +3170,8 @@ string_convbase64_8x3_to_6x4 (const char *from, char *to)
* Argument "length" is number of bytes in "from" to convert (commonly
* strlen(from)).
*
* Returns length of string in "*to" (it does not count final \0).
* Returns length of string in "*to" (it does not count final \0),
* -1 if error.
*/
int
@ -3237,7 +3242,8 @@ string_convbase64_6x4_to_8x3 (const unsigned char *from, unsigned char *to)
/*
* Decodes a base64 string.
*
* Returns length of string in "*to" (it does not count final \0).
* Returns length of string in "*to" (it does not count final \0),
* -1 if error.
*/
int
@ -3300,6 +3306,50 @@ string_base64_decode (const char *from, char *to)
return to_length;
}
/*
* Encodes a string in base 16, 32, or 64.
*
* Returns length of string in "*to" (it does not count final \0),
* -1 if error.
*/
int
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.
*
* Returns length of string in "*to" (it does not count final \0),
* -1 if error.
*/
int
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;
}
/*
* Dumps a data buffer as hexadecimal + ascii.
*

View File

@ -115,6 +115,9 @@ 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 int string_base_encode (int base, const char *from, int length,
char *to);
extern int string_base_decode (int base, 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);

View File

@ -97,45 +97,6 @@ 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;
}
/*
* Computes hash of data using the given algorithm.
*

View File

@ -28,10 +28,6 @@ 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);
/* crypto */
extern int plugin_api_crypto_hash (const void *data, int data_size,

View File

@ -633,8 +633,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_base_encode = &plugin_api_string_base_encode;
new_plugin->string_base_decode = &plugin_api_string_base_decode;
new_plugin->string_base_encode = &string_base_encode;
new_plugin->string_base_decode = &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;

View File

@ -1840,6 +1840,62 @@ TEST(CoreString, Base64)
}
}
/*
* Tests functions:
* string_base_encode
*/
TEST(CoreString, BaseEncode)
{
char str[1024];
LONGS_EQUAL(-1, string_base_encode (0, NULL, 0, NULL));
LONGS_EQUAL(-1, string_base_encode (0, "", 0, str));
LONGS_EQUAL(-1, string_base_encode (16, NULL, 0, str));
LONGS_EQUAL(-1, string_base_encode (32, NULL, 0, str));
LONGS_EQUAL(-1, string_base_encode (64, NULL, 0, str));
str[0] = 0xAA;
LONGS_EQUAL(16, string_base_encode (16, "abcdefgh", 8, str));
STRCMP_EQUAL("6162636465666768", str);
str[0] = 0xAA;
LONGS_EQUAL(16, string_base_encode (32, "abcdefgh", 8, str));
STRCMP_EQUAL("MFRGGZDFMZTWQ===", str);
str[0] = 0xAA;
LONGS_EQUAL(20, string_base_encode (64, "This is a test.", 15, str));
STRCMP_EQUAL("VGhpcyBpcyBhIHRlc3Qu", str);
}
/*
* Tests functions:
* string_base_decode
*/
TEST(CoreString, BaseDecode)
{
char str[1024];
LONGS_EQUAL(-1, string_base_decode (0, NULL, NULL));
LONGS_EQUAL(-1, string_base_decode (0, "", str));
LONGS_EQUAL(-1, string_base_decode (16, NULL, str));
LONGS_EQUAL(-1, string_base_decode (32, NULL, str));
LONGS_EQUAL(-1, string_base_decode (64, NULL, str));
str[0] = 0xAA;
LONGS_EQUAL(8, string_base_decode (16, "6162636465666768", str));
STRCMP_EQUAL("abcdefgh", str);
str[0] = 0xAA;
LONGS_EQUAL(8, string_base_decode (32, "MFRGGZDFMZTWQ===", str));
STRCMP_EQUAL("abcdefgh", str);
str[0] = 0xAA;
LONGS_EQUAL(15, string_base_decode (64, "VGhpcyBpcyBhIHRlc3Qu", str));
STRCMP_EQUAL("This is a test.", str);
}
/*
* Tests functions:
* string_hex_dump