From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Taylor Blau <me@ttaylorr.com>, Junio C Hamano <gitster@pobox.com>
Subject: [PATCH 1/4] hash: convert hashing context to a structure
Date: Fri, 31 Jan 2025 13:55:28 +0100 [thread overview]
Message-ID: <20250131-b4-pks-hash-context-direct-v1-1-67a6d3f49d6e@pks.im> (raw)
In-Reply-To: <20250131-b4-pks-hash-context-direct-v1-0-67a6d3f49d6e@pks.im>
The `git_hash_context` is a union containing the different hash-specific
states for SHA1, its unsafe variant as well as SHA256. We know that only
one of these states will ever be in use at the same time because hash
contexts cannot be used for multiple different hashes at the same point
in time.
We're about to extend the structure though to keep track of the hash
algorithm used to initialize the context, which is impossible to do
while the context is a union. Refactor it to instead be a structure that
contains the union of context states.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
hash.h | 13 +++++++------
object-file.c | 30 +++++++++++++++---------------
2 files changed, 22 insertions(+), 21 deletions(-)
diff --git a/hash.h b/hash.h
index ad2c919991..5b88d9b714 100644
--- a/hash.h
+++ b/hash.h
@@ -234,13 +234,14 @@ enum get_oid_result {
#endif
/* A suitably aligned type for stack allocations of hash contexts. */
-union git_hash_ctx {
- git_SHA_CTX sha1;
- git_SHA_CTX_unsafe sha1_unsafe;
-
- git_SHA256_CTX sha256;
+struct git_hash_ctx {
+ union {
+ git_SHA_CTX sha1;
+ git_SHA_CTX_unsafe sha1_unsafe;
+ git_SHA256_CTX sha256;
+ } state;
};
-typedef union git_hash_ctx git_hash_ctx;
+typedef struct git_hash_ctx git_hash_ctx;
typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src);
diff --git a/object-file.c b/object-file.c
index 6ce1caacae..7505aa6b60 100644
--- a/object-file.c
+++ b/object-file.c
@@ -88,82 +88,82 @@ static const struct object_id null_oid_sha256 = {
static void git_hash_sha1_init(git_hash_ctx *ctx)
{
- git_SHA1_Init(&ctx->sha1);
+ git_SHA1_Init(&ctx->state.sha1);
}
static void git_hash_sha1_clone(git_hash_ctx *dst, const git_hash_ctx *src)
{
- git_SHA1_Clone(&dst->sha1, &src->sha1);
+ git_SHA1_Clone(&dst->state.sha1, &src->state.sha1);
}
static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
{
- git_SHA1_Update(&ctx->sha1, data, len);
+ git_SHA1_Update(&ctx->state.sha1, data, len);
}
static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx)
{
- git_SHA1_Final(hash, &ctx->sha1);
+ git_SHA1_Final(hash, &ctx->state.sha1);
}
static void git_hash_sha1_final_oid(struct object_id *oid, git_hash_ctx *ctx)
{
- git_SHA1_Final(oid->hash, &ctx->sha1);
+ git_SHA1_Final(oid->hash, &ctx->state.sha1);
memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ);
oid->algo = GIT_HASH_SHA1;
}
static void git_hash_sha1_init_unsafe(git_hash_ctx *ctx)
{
- git_SHA1_Init_unsafe(&ctx->sha1_unsafe);
+ git_SHA1_Init_unsafe(&ctx->state.sha1_unsafe);
}
static void git_hash_sha1_clone_unsafe(git_hash_ctx *dst, const git_hash_ctx *src)
{
- git_SHA1_Clone_unsafe(&dst->sha1_unsafe, &src->sha1_unsafe);
+ git_SHA1_Clone_unsafe(&dst->state.sha1_unsafe, &src->state.sha1_unsafe);
}
static void git_hash_sha1_update_unsafe(git_hash_ctx *ctx, const void *data,
size_t len)
{
- git_SHA1_Update_unsafe(&ctx->sha1_unsafe, data, len);
+ git_SHA1_Update_unsafe(&ctx->state.sha1_unsafe, data, len);
}
static void git_hash_sha1_final_unsafe(unsigned char *hash, git_hash_ctx *ctx)
{
- git_SHA1_Final_unsafe(hash, &ctx->sha1_unsafe);
+ git_SHA1_Final_unsafe(hash, &ctx->state.sha1_unsafe);
}
static void git_hash_sha1_final_oid_unsafe(struct object_id *oid, git_hash_ctx *ctx)
{
- git_SHA1_Final_unsafe(oid->hash, &ctx->sha1_unsafe);
+ git_SHA1_Final_unsafe(oid->hash, &ctx->state.sha1_unsafe);
memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ);
oid->algo = GIT_HASH_SHA1;
}
static void git_hash_sha256_init(git_hash_ctx *ctx)
{
- git_SHA256_Init(&ctx->sha256);
+ git_SHA256_Init(&ctx->state.sha256);
}
static void git_hash_sha256_clone(git_hash_ctx *dst, const git_hash_ctx *src)
{
- git_SHA256_Clone(&dst->sha256, &src->sha256);
+ git_SHA256_Clone(&dst->state.sha256, &src->state.sha256);
}
static void git_hash_sha256_update(git_hash_ctx *ctx, const void *data, size_t len)
{
- git_SHA256_Update(&ctx->sha256, data, len);
+ git_SHA256_Update(&ctx->state.sha256, data, len);
}
static void git_hash_sha256_final(unsigned char *hash, git_hash_ctx *ctx)
{
- git_SHA256_Final(hash, &ctx->sha256);
+ git_SHA256_Final(hash, &ctx->state.sha256);
}
static void git_hash_sha256_final_oid(struct object_id *oid, git_hash_ctx *ctx)
{
- git_SHA256_Final(oid->hash, &ctx->sha256);
+ git_SHA256_Final(oid->hash, &ctx->state.sha256);
/*
* This currently does nothing, so the compiler should optimize it out,
* but keep it in case we extend the hash size again.
--
2.48.1.502.g6dc24dfdaf.dirty
next prev parent reply other threads:[~2025-01-31 12:55 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-31 12:55 [PATCH 0/4] hash: introduce generic wrappers to update hash contexts Patrick Steinhardt
2025-01-31 12:55 ` Patrick Steinhardt [this message]
2025-01-31 12:55 ` [PATCH 2/4] hash: stop typedeffing the hash context Patrick Steinhardt
2025-01-31 12:55 ` [PATCH 3/4] hash: provide generic wrappers to update hash contexts Patrick Steinhardt
2025-01-31 12:55 ` [PATCH 4/4] global: adapt callers to use generic hash context helpers Patrick Steinhardt
2025-01-31 18:16 ` [PATCH 0/4] hash: introduce generic wrappers to update hash contexts Junio C Hamano
2025-02-03 5:42 ` Patrick Steinhardt
2025-02-10 22:55 ` Taylor Blau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250131-b4-pks-hash-context-direct-v1-1-67a6d3f49d6e@pks.im \
--to=ps@pks.im \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=me@ttaylorr.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).