From d9f59c6fd5529fd517d680d9929bf7409e3d49f7 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 14 Aug 2013 18:17:09 -0400 Subject: [PATCH] core: Add API to convert checksum -> csum in place We already have the opposite, and this will be used in some places to avoid a malloc. https://bugzilla.gnome.org/show_bug.cgi?id=706031 --- src/libostree/ostree-core.c | 18 +++++++++++++----- src/libostree/ostree-core.h | 3 +++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/libostree/ostree-core.c b/src/libostree/ostree-core.c index f1e1fc97..92311418 100644 --- a/src/libostree/ostree-core.c +++ b/src/libostree/ostree-core.c @@ -972,9 +972,17 @@ ostree_object_name_deserialize (GVariant *variant, *out_objtype = (OstreeObjectType)objtype_u32; } -static void -checksum_to_bytes (const char *checksum, - guchar *buf) +/** + * ostree_checksum_inplace_to_bytes: + * @checksum: a SHA256 string + * @buf: Output buffer with at least 32 bytes of space + * + * Convert @checksum from a string to binary in-place, without + * allocating memory. Use this function in hot code paths. + */ +void +ostree_checksum_inplace_to_bytes (const char *checksum, + guchar *buf) { guint i; guint j; @@ -1000,7 +1008,7 @@ guchar * ostree_checksum_to_bytes (const char *checksum) { guchar *ret = g_malloc (32); - checksum_to_bytes (checksum, ret); + ostree_checksum_inplace_to_bytes (checksum, ret); return ret; } @@ -1008,7 +1016,7 @@ GVariant * ostree_checksum_to_bytes_v (const char *checksum) { guchar result[32]; - checksum_to_bytes (checksum, result); + ostree_checksum_inplace_to_bytes (checksum, result); return ot_gvariant_new_bytearray ((guchar*)result, 32); } diff --git a/src/libostree/ostree-core.h b/src/libostree/ostree-core.h index 6f892659..a28b0ca8 100644 --- a/src/libostree/ostree-core.h +++ b/src/libostree/ostree-core.h @@ -132,6 +132,9 @@ char * ostree_checksum_from_bytes_v (GVariant *bytes); void ostree_checksum_inplace_from_bytes (const guchar *bytes, char *buf); +void ostree_checksum_inplace_to_bytes (const char *checksum, + guchar *buf); + const guchar *ostree_checksum_bytes_peek (GVariant *bytes); int ostree_cmp_checksum_bytes (const guchar *a, const guchar *b);