From 0e8149eb839637aec5b2794c275b43e6316fb128 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 9 Apr 2012 16:31:46 -0400 Subject: [PATCH] core: Clean up checksum API We want to move towards guchar * for binary checksums. --- src/libostree/ostree-core.c | 100 ++++++++++++++--------------------- src/libostree/ostree-core.h | 27 ++++------ src/libostree/ostree-repo.c | 10 ++-- src/ostree/ostree-pull.c | 2 +- src/ostree/ot-builtin-pack.c | 7 +-- 5 files changed, 58 insertions(+), 88 deletions(-) diff --git a/src/libostree/ostree-core.c b/src/libostree/ostree-core.c index 2bea194a..52b12861 100644 --- a/src/libostree/ostree-core.c +++ b/src/libostree/ostree-core.c @@ -596,23 +596,12 @@ ostree_hash_object_name (gconstpointer a) } int -ostree_cmp_checksum_bytes (GVariant *a, - GVariant *b) +ostree_cmp_checksum_bytes (const guchar *a, + const guchar *b) { - gconstpointer a_data; - gconstpointer b_data; - gsize a_n_elts; - gsize b_n_elts; - - a_data = g_variant_get_fixed_array (a, &a_n_elts, 1); - g_assert (a_n_elts == 32); - b_data = g_variant_get_fixed_array (b, &b_n_elts, 1); - g_assert (b_n_elts == 32); - - return memcmp (a_data, b_data, 32); + return memcmp (a, b, 32); } - GVariant * ostree_object_name_serialize (const char *checksum, OstreeObjectType objtype) @@ -630,10 +619,10 @@ ostree_object_name_deserialize (GVariant *variant, *out_objtype = (OstreeObjectType)objtype_u32; } -GVariant * -ostree_checksum_to_bytes (const char *sha256) +static void +checksum_to_bytes (const char *checksum, + guchar *buf) { - guchar result[32]; guint i; guint j; @@ -641,38 +630,47 @@ ostree_checksum_to_bytes (const char *sha256) { gint big, little; - g_assert (sha256[j]); - g_assert (sha256[j+1]); + g_assert (checksum[j]); + g_assert (checksum[j+1]); - big = g_ascii_xdigit_value (sha256[j]); - little = g_ascii_xdigit_value (sha256[j+1]); + big = g_ascii_xdigit_value (checksum[j]); + little = g_ascii_xdigit_value (checksum[j+1]); g_assert (big != -1); g_assert (little != -1); - result[i] = (big << 4) | little; + buf[i] = (big << 4) | little; } - +} + +guchar * +ostree_checksum_to_bytes (const char *checksum) +{ + guchar *ret = g_malloc (32); + checksum_to_bytes (checksum, ret); + return ret; +} + +GVariant * +ostree_checksum_to_bytes_v (const char *checksum) +{ + guchar result[32]; + checksum_to_bytes (checksum, result); return ot_gvariant_new_bytearray ((guchar*)result, 32); } char * -ostree_checksum_from_bytes (GVariant *csum_bytes) +ostree_checksum_from_bytes (const guchar *csum) { static const gchar hexchars[] = "0123456789abcdef"; char *ret; - const guchar *bytes; - gsize n_elts; guint i, j; - bytes = g_variant_get_fixed_array (csum_bytes, &n_elts, 1); - g_assert (n_elts == 32); - ret = g_malloc (65); for (i = 0, j = 0; i < 32; i++, j += 2) { - guchar byte = bytes[i]; + guchar byte = csum[i]; ret[j] = hexchars[byte >> 4]; ret[j+1] = hexchars[byte & 0xF]; } @@ -681,40 +679,17 @@ ostree_checksum_from_bytes (GVariant *csum_bytes) return ret; } -GVariant * -ostree_object_name_serialize_v2 (const char *checksum, - OstreeObjectType objtype) +char * +ostree_checksum_from_bytes_v (GVariant *csum_bytes) { - return g_variant_new ("(u@ay)", (guint32)objtype, ostree_checksum_to_bytes (checksum)); + return ostree_checksum_from_bytes (ostree_checksum_bytes_peek (csum_bytes)); } -void -ostree_object_name_deserialize_v2_hex (GVariant *variant, - char **out_checksum, - OstreeObjectType *out_objtype) +const guchar * +ostree_checksum_bytes_peek (GVariant *bytes) { - GVariant *csum_bytes; - guint32 objtype_u32; - - g_variant_get (variant, "(u@ay)", &objtype_u32, &csum_bytes); - g_variant_ref_sink (csum_bytes); - *out_checksum = ostree_checksum_from_bytes (csum_bytes); - g_variant_unref (csum_bytes); - *out_objtype = (OstreeObjectType)objtype_u32; -} - -void -ostree_object_name_deserialize_v2_bytes (GVariant *variant, - const guchar **out_checksum, - OstreeObjectType *out_objtype) -{ - GVariant *csum_bytes; - guint32 objtype_u32; gsize n_elts; - - g_variant_get (variant, "(u@ay)", &objtype_u32, &csum_bytes); - *out_checksum = (guchar*)g_variant_get_fixed_array (csum_bytes, &n_elts, 1); - *out_objtype = (OstreeObjectType)objtype_u32; + return g_variant_get_fixed_array (bytes, &n_elts, 1); } char * @@ -1340,7 +1315,7 @@ ostree_read_pack_entry_variant (GVariant *pack_entry, gboolean ostree_pack_index_search (GVariant *index, - GVariant *csum_bytes, + GVariant *csum_v, OstreeObjectType objtype, guint64 *out_offset) { @@ -1348,8 +1323,11 @@ ostree_pack_index_search (GVariant *index, gsize imax, imin; gsize n; guint32 target_objtype; + const guchar *csum; ot_lvariant GVariant *index_contents = NULL; + csum = ostree_checksum_bytes_peek (csum_v); + index_contents = g_variant_get_child_value (index, 2); target_objtype = (guint32) objtype; @@ -1375,7 +1353,7 @@ ostree_pack_index_search (GVariant *index, &cur_csum_bytes, &cur_offset); cur_objtype = GUINT32_FROM_BE (cur_objtype); - c = ostree_cmp_checksum_bytes (cur_csum_bytes, csum_bytes); + c = ostree_cmp_checksum_bytes (ostree_checksum_bytes_peek (cur_csum_bytes), csum); if (c == 0) { if (cur_objtype < target_objtype) diff --git a/src/libostree/ostree-core.h b/src/libostree/ostree-core.h index 5c578d4f..867803e6 100644 --- a/src/libostree/ostree-core.h +++ b/src/libostree/ostree-core.h @@ -132,7 +132,15 @@ typedef enum { gboolean ostree_validate_checksum_string (const char *sha256, GError **error); -GVariant *ostree_checksum_to_bytes (const char *sha256); +guchar *ostree_checksum_to_bytes (const char *checksum); +GVariant *ostree_checksum_to_bytes_v (const char *checksum); + +char * ostree_checksum_from_bytes (const guchar *bytes); +char * ostree_checksum_from_bytes_v (GVariant *bytes); + +const guchar *ostree_checksum_bytes_peek (GVariant *bytes); + +int ostree_cmp_checksum_bytes (const guchar *a, const guchar *b); gboolean ostree_validate_rev (const char *rev, GError **error); @@ -144,8 +152,6 @@ OstreeObjectType ostree_object_type_from_string (const char *str); guint ostree_hash_object_name (gconstpointer a); -int ostree_cmp_checksum_bytes (GVariant *a, GVariant *b); - GVariant *ostree_object_name_serialize (const char *checksum, OstreeObjectType objtype); @@ -153,21 +159,6 @@ void ostree_object_name_deserialize (GVariant *variant, const char **out_checksum, OstreeObjectType *out_objtype); -GVariant *ostree_object_name_serialize_v2 (const char *checksum, - OstreeObjectType objtype); - -void ostree_object_name_deserialize_v2_hex (GVariant *variant, - char **out_checksum, - OstreeObjectType *out_objtype); - - -void ostree_object_name_deserialize_v2_bytes (GVariant *variant, - const guchar **out_checksum, - OstreeObjectType *out_objtype); - -GVariant * ostree_checksum_to_bytes (const char *sha256); -char * ostree_checksum_from_bytes (GVariant *bytes); - char * ostree_object_to_string (const char *checksum, OstreeObjectType objtype); diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c index cb81a5b4..2034d238 100644 --- a/src/libostree/ostree-repo.c +++ b/src/libostree/ostree-repo.c @@ -1567,7 +1567,7 @@ list_pack_checksums_from_superindex_file (GFile *superindex_path, while (g_variant_iter_loop (variant_iter, "(@ay@ay)", &checksum, &bloom)) - g_ptr_array_add (ret_indexes, ostree_checksum_from_bytes (checksum)); + g_ptr_array_add (ret_indexes, ostree_checksum_from_bytes_v (checksum)); checksum = NULL; bloom = NULL; @@ -1663,7 +1663,7 @@ ostree_repo_regenerate_pack_index (OstreeRepo *self, g_variant_builder_add (index_content_builder, "(@ay@ay)", - ostree_checksum_to_bytes (pack_checksum), + ostree_checksum_to_bytes_v (pack_checksum), bloom); g_variant_unref (bloom); } @@ -1806,7 +1806,7 @@ ostree_repo_resync_cached_remote_pack_indexes (OstreeRepo *self, while (g_variant_iter_loop (superindex_contents_iter, "(@ay@ay)", &csum_bytes, &bloom)) { - pack_checksum = ostree_checksum_from_bytes (csum_bytes); + pack_checksum = ostree_checksum_from_bytes_v (csum_bytes); g_hash_table_insert (new_pack_indexes, pack_checksum, pack_checksum); pack_checksum = NULL; /* transfer ownership */ } @@ -3173,7 +3173,7 @@ list_objects_in_index (OstreeRepo *self, G_VARIANT_TYPE_STRING_ARRAY); g_free (checksum); - checksum = ostree_checksum_from_bytes (csum_bytes); + checksum = ostree_checksum_from_bytes_v (csum_bytes); obj_key = ostree_object_name_serialize (checksum, objtype); ot_util_variant_take_ref (obj_key); @@ -3251,7 +3251,7 @@ find_object_in_packs (OstreeRepo *self, ot_lvariant GVariant *csum_bytes = NULL; ot_lvariant GVariant *index_variant = NULL; - csum_bytes = ostree_checksum_to_bytes (checksum); + csum_bytes = ostree_checksum_to_bytes_v (checksum); if (!ostree_repo_list_pack_indexes (self, &index_checksums, cancellable, error)) goto out; diff --git a/src/ostree/ostree-pull.c b/src/ostree/ostree-pull.c index 56172212..f5c1ab0a 100644 --- a/src/ostree/ostree-pull.c +++ b/src/ostree/ostree-pull.c @@ -326,7 +326,7 @@ find_object_in_remote_packs (OtPullData *pull_data, ot_lvariant GVariant *csum_bytes = NULL; ot_lfree char *ret_pack_checksum = NULL; - csum_bytes = ostree_checksum_to_bytes (checksum); + csum_bytes = ostree_checksum_to_bytes_v (checksum); for (i = 0; i < pull_data->cached_pack_indexes->len; i++) { diff --git a/src/ostree/ot-builtin-pack.c b/src/ostree/ot-builtin-pack.c index cc8f5e78..1c402b82 100644 --- a/src/ostree/ot-builtin-pack.c +++ b/src/ostree/ot-builtin-pack.c @@ -209,7 +209,8 @@ compare_index_content (gconstpointer ap, g_variant_get (b_v, "(u@ayt)", &b_objtype, &b_csum_bytes, &b_offset); a_objtype = GUINT32_FROM_BE (a_objtype); b_objtype = GUINT32_FROM_BE (b_objtype); - c = ostree_cmp_checksum_bytes (a_csum_bytes, b_csum_bytes); + c = ostree_cmp_checksum_bytes (ostree_checksum_bytes_peek (a_csum_bytes), + ostree_checksum_bytes_peek (b_csum_bytes)); if (c == 0) { if (a_objtype < b_objtype) @@ -409,7 +410,7 @@ create_pack_file (OtRepackData *data, gsize data_len = g_memory_output_stream_get_data_size (object_data_stream); packed_object = g_variant_new ("(uy@ay@ay)", GUINT32_TO_BE ((guint32)objtype), entry_flags, - ostree_checksum_to_bytes (checksum), + ostree_checksum_to_bytes_v (checksum), ot_gvariant_new_bytearray (data, data_len)); g_clear_object (&object_data_stream); } @@ -420,7 +421,7 @@ create_pack_file (OtRepackData *data, /* offset points to aligned header size */ index_entry = g_variant_new ("(u@ayt)", GUINT32_TO_BE ((guint32)objtype), - ostree_checksum_to_bytes (checksum), + ostree_checksum_to_bytes_v (checksum), GUINT64_TO_BE (offset)); g_ptr_array_add (index_content_list, g_variant_ref_sink (index_entry));