[glib] gchecksum: Add g_compute_checksum_for_bytes()
- From: Stefan Walter <stefw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] gchecksum: Add g_compute_checksum_for_bytes()
- Date: Mon, 6 Aug 2012 08:42:03 +0000 (UTC)
commit 278fe0c67f1d3a8731356fdda0e0adfb633cd7c4
Author: Stef Walter <stefw gnome org>
Date: Tue Jul 31 16:47:45 2012 +0200
gchecksum: Add g_compute_checksum_for_bytes()
* Add a GBytes based version of g_compute_checksum_for_data()
* Add appropriate tests
https://bugzilla.gnome.org/show_bug.cgi?id=680912
docs/reference/glib/glib-sections.txt | 1 +
glib/gchecksum.c | 30 ++++++++++++++++++++++
glib/gchecksum.h | 5 +++
glib/glib.symbols | 1 +
glib/tests/checksum.c | 45 ++++++++++++++++++++++++++++++---
5 files changed, 78 insertions(+), 4 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index b78fdf9..dacacbb 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -2845,6 +2845,7 @@ g_checksum_get_digest
<SUBSECTION>
g_compute_checksum_for_data
g_compute_checksum_for_string
+g_compute_checksum_for_bytes
</SECTION>
<SECTION>
diff --git a/glib/gchecksum.c b/glib/gchecksum.c
index f543d96..2920479 100644
--- a/glib/gchecksum.c
+++ b/glib/gchecksum.c
@@ -1462,3 +1462,33 @@ g_compute_checksum_for_string (GChecksumType checksum_type,
return g_compute_checksum_for_data (checksum_type, (const guchar *) str, length);
}
+
+/**
+ * g_compute_checksum_for_bytes:
+ * @checksum_type: a #GChecksumType
+ * @data: binary blob to compute the digest of
+ *
+ * Computes the checksum for a binary @data. This is a
+ * convenience wrapper for g_checksum_new(), g_checksum_get_string()
+ * and g_checksum_free().
+ *
+ * The hexadecimal string returned will be in lower case.
+ *
+ * Return value: the digest of the binary data as a string in hexadecimal.
+ * The returned string should be freed with g_free() when done using it.
+ *
+ * Since: 2.34
+ */
+gchar *
+g_compute_checksum_for_bytes (GChecksumType checksum_type,
+ GBytes *data)
+{
+ gconstpointer byte_data;
+ gsize length;
+
+ g_return_val_if_fail (IS_VALID_TYPE (checksum_type), NULL);
+ g_return_val_if_fail (data != NULL, NULL);
+
+ byte_data = g_bytes_get_data (data, &length);
+ return g_compute_checksum_for_data (checksum_type, byte_data, length);
+}
diff --git a/glib/gchecksum.h b/glib/gchecksum.h
index 69fe1f4..b0621f2 100644
--- a/glib/gchecksum.h
+++ b/glib/gchecksum.h
@@ -26,6 +26,7 @@
#define __G_CHECKSUM_H__
#include <glib/gtypes.h>
+#include <glib/gbytes.h>
G_BEGIN_DECLS
@@ -81,6 +82,10 @@ gchar *g_compute_checksum_for_string (GChecksumType checksum_t
const gchar *str,
gssize length);
+GLIB_AVAILABLE_IN_2_34
+gchar *g_compute_checksum_for_bytes (GChecksumType checksum_type,
+ GBytes *data);
+
G_END_DECLS
#endif /* __G_CHECKSUM_H__ */
diff --git a/glib/glib.symbols b/glib/glib.symbols
index 4d83f3d..0490b66 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -169,6 +169,7 @@ g_checksum_get_string
g_checksum_get_digest
g_compute_checksum_for_data
g_compute_checksum_for_string
+g_compute_checksum_for_bytes
g_completion_add_items
g_completion_clear_items
g_completion_complete
diff --git a/glib/tests/checksum.c b/glib/tests/checksum.c
index fd2ffb9..d900b9a 100644
--- a/glib/tests/checksum.c
+++ b/glib/tests/checksum.c
@@ -700,12 +700,12 @@ test_checksum_reset (gconstpointer d)
typedef struct {
GChecksumType checksum_type;
const gchar **sums;
-} ChecksumStringTest;
+} ChecksumComputeTest;
static void
test_checksum_string (gconstpointer d)
{
- const ChecksumStringTest *test = d;
+ const ChecksumComputeTest *test = d;
int length;
gchar *checksum;
@@ -726,6 +726,25 @@ test_checksum_string (gconstpointer d)
}
static void
+test_checksum_bytes (gconstpointer d)
+{
+ const ChecksumComputeTest *test = d;
+ GBytes *input;
+ int length;
+ gchar *checksum;
+
+ for (length = 0; length <= FIXED_LEN; length++)
+ {
+ input = g_bytes_new_static (FIXED_STR, length);
+ checksum = g_compute_checksum_for_bytes (test->checksum_type, input);
+ g_bytes_unref (input);
+
+ g_assert_cmpstr (checksum, ==, test->sums[length]);
+ g_free (checksum);
+ }
+}
+
+static void
add_checksum_test (GChecksumType type,
const char *type_name,
const char *sum,
@@ -750,9 +769,9 @@ add_checksum_string_test (GChecksumType type,
const gchar *type_name,
const gchar **sums)
{
- ChecksumStringTest *test;
+ ChecksumComputeTest *test;
gchar *path;
- test = g_new0 (ChecksumStringTest, 1);
+ test = g_new0 (ChecksumComputeTest, 1);
test->checksum_type = type;
test->sums = sums;
path = g_strdup_printf ("/checksum/%s/string", type_name);
@@ -761,6 +780,21 @@ add_checksum_string_test (GChecksumType type,
}
static void
+add_checksum_bytes_test (GChecksumType type,
+ const gchar *type_name,
+ const gchar **sums)
+{
+ ChecksumComputeTest *test;
+ gchar *path;
+ test = g_new0 (ChecksumComputeTest, 1);
+ test->checksum_type = type;
+ test->sums = sums;
+ path = g_strdup_printf ("/checksum/%s/bytes", type_name);
+ g_test_add_data_func (path, test, test_checksum_bytes);
+ g_free (path);
+}
+
+static void
test_unsupported (void)
{
g_assert_cmpint (g_checksum_type_get_length (20), ==, -1);
@@ -779,14 +813,17 @@ main (int argc, char *argv[])
for (length = 0; length <= FIXED_LEN; length++)
add_checksum_test (G_CHECKSUM_MD5, "MD5", MD5_sums[length], length);
add_checksum_string_test (G_CHECKSUM_MD5, "MD5", MD5_sums);
+ add_checksum_bytes_test (G_CHECKSUM_MD5, "MD5", MD5_sums);
for (length = 0; length <= FIXED_LEN; length++)
add_checksum_test (G_CHECKSUM_SHA1, "SHA1", SHA1_sums[length], length);
add_checksum_string_test (G_CHECKSUM_SHA1, "SHA1", SHA1_sums);
+ add_checksum_bytes_test (G_CHECKSUM_SHA1, "SHA1", SHA1_sums);
for (length = 0; length <= FIXED_LEN; length++)
add_checksum_test (G_CHECKSUM_SHA256, "SHA256", SHA256_sums[length], length);
add_checksum_string_test (G_CHECKSUM_SHA256, "SHA256", SHA256_sums);
+ add_checksum_bytes_test (G_CHECKSUM_SHA256, "SHA256", SHA256_sums);
return g_test_run ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]