diff options
Diffstat (limited to 'src/libostree/ostree-bloom.c')
-rw-r--r-- | src/libostree/ostree-bloom.c | 288 |
1 files changed, 141 insertions, 147 deletions
diff --git a/src/libostree/ostree-bloom.c b/src/libostree/ostree-bloom.c index 8c1017d7..6c1d1f14 100644 --- a/src/libostree/ostree-bloom.c +++ b/src/libostree/ostree-bloom.c @@ -24,8 +24,8 @@ #include <assert.h> #include <gio/gio.h> -#include <glib.h> #include <glib-object.h> +#include <glib.h> #include <stdint.h> #include <string.h> @@ -76,13 +76,13 @@ struct _OstreeBloom { guint ref_count; - gsize n_bytes; /* 0 < n_bytes <= G_MAXSIZE / 8 */ - gboolean is_mutable; /* determines which of [im]mutable_bytes is accessed */ + gsize n_bytes; /* 0 < n_bytes <= G_MAXSIZE / 8 */ + gboolean is_mutable; /* determines which of [im]mutable_bytes is accessed */ union - { - guint8 *mutable_bytes; /* owned; mutually exclusive */ - GBytes *immutable_bytes; /* owned; mutually exclusive */ - }; + { + guint8 *mutable_bytes; /* owned; mutually exclusive */ + GBytes *immutable_bytes; /* owned; mutually exclusive */ + }; guint8 k; OstreeBloomHashFunc hash_func; }; @@ -110,11 +110,9 @@ G_DEFINE_BOXED_TYPE (OstreeBloom, ostree_bloom, ostree_bloom_ref, ostree_bloom_u * Since: 2017.8 */ OstreeBloom * -ostree_bloom_new (gsize n_bytes, - guint8 k, - OstreeBloomHashFunc hash_func) +ostree_bloom_new (gsize n_bytes, guint8 k, OstreeBloomHashFunc hash_func) { - g_autoptr(OstreeBloom) bloom = NULL; + g_autoptr (OstreeBloom) bloom = NULL; g_return_val_if_fail (n_bytes > 0, NULL); g_return_val_if_fail (n_bytes <= G_MAXSIZE / 8, NULL); @@ -152,11 +150,9 @@ ostree_bloom_new (gsize n_bytes, * Since: 2017.8 */ OstreeBloom * -ostree_bloom_new_from_bytes (GBytes *bytes, - guint8 k, - OstreeBloomHashFunc hash_func) +ostree_bloom_new_from_bytes (GBytes *bytes, guint8 k, OstreeBloomHashFunc hash_func) { - g_autoptr(OstreeBloom) bloom = NULL; + g_autoptr (OstreeBloom) bloom = NULL; g_return_val_if_fail (bytes != NULL, NULL); g_return_val_if_fail (g_bytes_get_size (bytes) > 0, NULL); @@ -227,8 +223,7 @@ ostree_bloom_unref (OstreeBloom *bloom) /* @idx is in bits, not bytes. */ static inline gboolean -ostree_bloom_get_bit (OstreeBloom *bloom, - gsize idx) +ostree_bloom_get_bit (OstreeBloom *bloom, gsize idx) { const guint8 *bytes; @@ -243,12 +238,11 @@ ostree_bloom_get_bit (OstreeBloom *bloom, /* @idx is in bits, not bytes. */ static inline void -ostree_bloom_set_bit (OstreeBloom *bloom, - gsize idx) +ostree_bloom_set_bit (OstreeBloom *bloom, gsize idx) { g_assert (bloom->is_mutable); g_assert (idx / 8 < bloom->n_bytes); - bloom->mutable_bytes[idx / 8] |= (guint8) (1 << (idx % 8)); + bloom->mutable_bytes[idx / 8] |= (guint8)(1 << (idx % 8)); } /** @@ -265,8 +259,7 @@ ostree_bloom_set_bit (OstreeBloom *bloom, * Since: 2017.8 */ gboolean -ostree_bloom_maybe_contains (OstreeBloom *bloom, - gconstpointer element) +ostree_bloom_maybe_contains (OstreeBloom *bloom, gconstpointer element) { guint8 i; @@ -279,11 +272,11 @@ ostree_bloom_maybe_contains (OstreeBloom *bloom, idx = bloom->hash_func (element, i); - if (!ostree_bloom_get_bit (bloom, (gsize) (idx % (bloom->n_bytes * 8)))) - return FALSE; /* definitely not in the set */ + if (!ostree_bloom_get_bit (bloom, (gsize)(idx % (bloom->n_bytes * 8)))) + return FALSE; /* definitely not in the set */ } - return TRUE; /* possibly in the set */ + return TRUE; /* possibly in the set */ } /** @@ -310,7 +303,8 @@ ostree_bloom_seal (OstreeBloom *bloom) if (bloom->is_mutable) { bloom->is_mutable = FALSE; - bloom->immutable_bytes = g_bytes_new_take (g_steal_pointer (&bloom->mutable_bytes), bloom->n_bytes); + bloom->immutable_bytes + = g_bytes_new_take (g_steal_pointer (&bloom->mutable_bytes), bloom->n_bytes); } return g_bytes_ref (bloom->immutable_bytes); @@ -328,8 +322,7 @@ ostree_bloom_seal (OstreeBloom *bloom) * Since: 2017.8 */ void -ostree_bloom_add_element (OstreeBloom *bloom, - gconstpointer element) +ostree_bloom_add_element (OstreeBloom *bloom, gconstpointer element) { guint8 i; @@ -340,7 +333,7 @@ ostree_bloom_add_element (OstreeBloom *bloom, for (i = 0; i < bloom->k; i++) { guint64 idx = bloom->hash_func (element, i); - ostree_bloom_set_bit (bloom, (gsize) (idx % (bloom->n_bytes * 8))); + ostree_bloom_set_bit (bloom, (gsize)(idx % (bloom->n_bytes * 8))); } } @@ -418,145 +411,147 @@ ostree_bloom_get_hash_func (OstreeBloom *bloom) #define cROUNDS 2 #define dROUNDS 4 -#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) - -#define U32TO8_LE(p, v) \ - (p)[0] = (uint8_t)((v)); \ - (p)[1] = (uint8_t)((v) >> 8); \ - (p)[2] = (uint8_t)((v) >> 16); \ - (p)[3] = (uint8_t)((v) >> 24); - -#define U64TO8_LE(p, v) \ - U32TO8_LE((p), (uint32_t)((v))); \ - U32TO8_LE((p) + 4, (uint32_t)((v) >> 32)); - -#define U8TO64_LE(p) \ - (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \ - ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \ - ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \ - ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) - -#define SIPROUND \ - do { \ - v0 += v1; \ - v1 = ROTL(v1, 13); \ - v1 ^= v0; \ - v0 = ROTL(v0, 32); \ - v2 += v3; \ - v3 = ROTL(v3, 16); \ - v3 ^= v2; \ - v0 += v3; \ - v3 = ROTL(v3, 21); \ - v3 ^= v0; \ - v2 += v1; \ - v1 = ROTL(v1, 17); \ - v1 ^= v2; \ - v2 = ROTL(v2, 32); \ - } while (0) +#define ROTL(x, b) (uint64_t) (((x) << (b)) | ((x) >> (64 - (b)))) + +#define U32TO8_LE(p, v) \ + (p)[0] = (uint8_t)((v)); \ + (p)[1] = (uint8_t)((v) >> 8); \ + (p)[2] = (uint8_t)((v) >> 16); \ + (p)[3] = (uint8_t)((v) >> 24); + +#define U64TO8_LE(p, v) \ + U32TO8_LE ((p), (uint32_t)((v))); \ + U32TO8_LE ((p) + 4, (uint32_t)((v) >> 32)); + +#define U8TO64_LE(p) \ + (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | ((uint64_t)((p)[2]) << 16) \ + | ((uint64_t)((p)[3]) << 24) | ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) \ + | ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) + +#define SIPROUND \ + do \ + { \ + v0 += v1; \ + v1 = ROTL (v1, 13); \ + v1 ^= v0; \ + v0 = ROTL (v0, 32); \ + v2 += v3; \ + v3 = ROTL (v3, 16); \ + v3 ^= v2; \ + v0 += v3; \ + v3 = ROTL (v3, 21); \ + v3 ^= v0; \ + v2 += v1; \ + v1 = ROTL (v1, 17); \ + v1 ^= v2; \ + v2 = ROTL (v2, 32); \ + } \ + while (0) #ifdef DEBUG -#define TRACE \ - do { \ - printf("(%3d) v0 %08x %08x\n", (int)inlen, (uint32_t)(v0 >> 32), \ - (uint32_t)v0); \ - printf("(%3d) v1 %08x %08x\n", (int)inlen, (uint32_t)(v1 >> 32), \ - (uint32_t)v1); \ - printf("(%3d) v2 %08x %08x\n", (int)inlen, (uint32_t)(v2 >> 32), \ - (uint32_t)v2); \ - printf("(%3d) v3 %08x %08x\n", (int)inlen, (uint32_t)(v3 >> 32), \ - (uint32_t)v3); \ - } while (0) +#define TRACE \ + do \ + { \ + printf ("(%3d) v0 %08x %08x\n", (int)inlen, (uint32_t)(v0 >> 32), (uint32_t)v0); \ + printf ("(%3d) v1 %08x %08x\n", (int)inlen, (uint32_t)(v1 >> 32), (uint32_t)v1); \ + printf ("(%3d) v2 %08x %08x\n", (int)inlen, (uint32_t)(v2 >> 32), (uint32_t)v2); \ + printf ("(%3d) v3 %08x %08x\n", (int)inlen, (uint32_t)(v3 >> 32), (uint32_t)v3); \ + } \ + while (0) #else #define TRACE #endif -static int siphash(const uint8_t *in, const size_t inlen, const uint8_t *k, - uint8_t *out, const size_t outlen) { - - assert((outlen == 8) || (outlen == 16)); - uint64_t v0 = 0x736f6d6570736575ULL; - uint64_t v1 = 0x646f72616e646f6dULL; - uint64_t v2 = 0x6c7967656e657261ULL; - uint64_t v3 = 0x7465646279746573ULL; - uint64_t k0 = U8TO64_LE(k); - uint64_t k1 = U8TO64_LE(k + 8); - uint64_t m; - int i; - const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t)); - const int left = inlen & 7; - uint64_t b = ((uint64_t)inlen) << 56; - v3 ^= k1; - v2 ^= k0; - v1 ^= k1; - v0 ^= k0; - - if (outlen == 16) - v1 ^= 0xee; - - for (; in != end; in += 8) { - m = U8TO64_LE(in); - v3 ^= m; - - TRACE; - for (i = 0; i < cROUNDS; ++i) - SIPROUND; - - v0 ^= m; +static int +siphash (const uint8_t *in, const size_t inlen, const uint8_t *k, uint8_t *out, const size_t outlen) +{ + + assert ((outlen == 8) || (outlen == 16)); + uint64_t v0 = 0x736f6d6570736575ULL; + uint64_t v1 = 0x646f72616e646f6dULL; + uint64_t v2 = 0x6c7967656e657261ULL; + uint64_t v3 = 0x7465646279746573ULL; + uint64_t k0 = U8TO64_LE (k); + uint64_t k1 = U8TO64_LE (k + 8); + uint64_t m; + int i; + const uint8_t *end = in + inlen - (inlen % sizeof (uint64_t)); + const int left = inlen & 7; + uint64_t b = ((uint64_t)inlen) << 56; + v3 ^= k1; + v2 ^= k0; + v1 ^= k1; + v0 ^= k0; + + if (outlen == 16) + v1 ^= 0xee; + + for (; in != end; in += 8) + { + m = U8TO64_LE (in); + v3 ^= m; + + TRACE; + for (i = 0; i < cROUNDS; ++i) + SIPROUND; + + v0 ^= m; } - switch (left) { + switch (left) + { case 7: - b |= ((uint64_t)in[6]) << 48; + b |= ((uint64_t)in[6]) << 48; case 6: - b |= ((uint64_t)in[5]) << 40; + b |= ((uint64_t)in[5]) << 40; case 5: - b |= ((uint64_t)in[4]) << 32; + b |= ((uint64_t)in[4]) << 32; case 4: - b |= ((uint64_t)in[3]) << 24; + b |= ((uint64_t)in[3]) << 24; case 3: - b |= ((uint64_t)in[2]) << 16; + b |= ((uint64_t)in[2]) << 16; case 2: - b |= ((uint64_t)in[1]) << 8; + b |= ((uint64_t)in[1]) << 8; case 1: - b |= ((uint64_t)in[0]); - break; + b |= ((uint64_t)in[0]); + break; case 0: - break; + break; } - v3 ^= b; + v3 ^= b; - TRACE; - for (i = 0; i < cROUNDS; ++i) - SIPROUND; + TRACE; + for (i = 0; i < cROUNDS; ++i) + SIPROUND; - v0 ^= b; + v0 ^= b; - if (outlen == 16) - v2 ^= 0xee; - else - v2 ^= 0xff; + if (outlen == 16) + v2 ^= 0xee; + else + v2 ^= 0xff; - TRACE; - for (i = 0; i < dROUNDS; ++i) - SIPROUND; + TRACE; + for (i = 0; i < dROUNDS; ++i) + SIPROUND; - b = v0 ^ v1 ^ v2 ^ v3; - U64TO8_LE(out, b); + b = v0 ^ v1 ^ v2 ^ v3; + U64TO8_LE (out, b); - if (outlen == 8) - return 0; + if (outlen == 8) + return 0; - v1 ^= 0xdd; + v1 ^= 0xdd; - TRACE; - for (i = 0; i < dROUNDS; ++i) - SIPROUND; + TRACE; + for (i = 0; i < dROUNDS; ++i) + SIPROUND; - b = v0 ^ v1 ^ v2 ^ v3; - U64TO8_LE(out + 8, b); + b = v0 ^ v1 ^ v2 ^ v3; + U64TO8_LE (out + 8, b); - return 0; + return 0; } /* End SipHash copied code. */ @@ -581,16 +576,15 @@ static int siphash(const uint8_t *in, const size_t inlen, const uint8_t *k, * Since: 2017.8 */ guint64 -ostree_str_bloom_hash (gconstpointer element, - guint8 k) +ostree_str_bloom_hash (gconstpointer element, guint8 k) { const gchar *str = element; gsize str_len; union - { - guint64 u64; - guint8 u8[8]; - } out_le; + { + guint64 u64; + guint8 u8[8]; + } out_le; guint8 k_array[16]; gsize i; @@ -598,7 +592,7 @@ ostree_str_bloom_hash (gconstpointer element, for (i = 0; i < G_N_ELEMENTS (k_array); i++) k_array[i] = k; - siphash ((const guint8 *) str, str_len, k_array, out_le.u8, sizeof (out_le)); + siphash ((const guint8 *)str, str_len, k_array, out_le.u8, sizeof (out_le)); return le64toh (out_le.u64); } |