summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2020-05-21 00:02:24 +0200
committerSébastien Helleu <flashcode@flashtux.org>2020-05-21 00:02:24 +0200
commit1994d5641dda09d3825b4cc6baf3b57a20b2ffc2 (patch)
treefe874854efeb99ed1a52186fb8f36d456f4b0fee
parent0ac936a5cfe3f0b962eb1f6cfea0192107656117 (diff)
downloadweechat-1994d5641dda09d3825b4cc6baf3b57a20b2ffc2.zip
core: move functions string_base_encode and string_base_decode from plugin-api.c to wee-string.c
-rw-r--r--src/core/wee-string.c62
-rw-r--r--src/core/wee-string.h3
-rw-r--r--src/plugins/plugin-api.c39
-rw-r--r--src/plugins/plugin-api.h4
-rw-r--r--src/plugins/plugin.c4
-rw-r--r--tests/unit/core/test-core-string.cpp56
6 files changed, 117 insertions, 51 deletions
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index 1ff0c53b9..4c41239c3 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -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
@@ -3301,6 +3307,50 @@ string_base64_decode (const char *from, char *to)
}
/*
+ * 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.
*
* Note: result must be freed after use.
diff --git a/src/core/wee-string.h b/src/core/wee-string.h
index 585cb399d..a34dbd041 100644
--- a/src/core/wee-string.h
+++ b/src/core/wee-string.h
@@ -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);
diff --git a/src/plugins/plugin-api.c b/src/plugins/plugin-api.c
index d4cd2fca6..07094944c 100644
--- a/src/plugins/plugin-api.c
+++ b/src/plugins/plugin-api.c
@@ -98,45 +98,6 @@ plugin_api_ngettext (const char *single, const char *plural, int 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.
*
* Returns:
diff --git a/src/plugins/plugin-api.h b/src/plugins/plugin-api.h
index e2c78a010..d20ecd412 100644
--- a/src/plugins/plugin-api.h
+++ b/src/plugins/plugin-api.h
@@ -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,
diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c
index ab929983e..5f0a75f5c 100644
--- a/src/plugins/plugin.c
+++ b/src/plugins/plugin.c
@@ -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;
diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp
index 21dc894be..3f0217741 100644
--- a/tests/unit/core/test-core-string.cpp
+++ b/tests/unit/core/test-core-string.cpp
@@ -1842,6 +1842,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
*/