On Sat, Oct 28, 2017 at 2:12 PM, brian m. carlson <sandals@xxxxxxxxxxxxxxxxxxxx> wrote: > Since in the future we want to support an additional hash algorithm, add > a structure that represents a hash algorithm and all the data that must > go along with it. Add a constant to allow easy enumeration of hash > algorithms. Implement function typedefs to create an abstract API that > can be used by any hash algorithm, and wrappers for the existing SHA1 > functions that conform to this API. > [...] > Signed-off-by: brian m. carlson <sandals@xxxxxxxxxxxxxxxxxxxx> > --- > diff --git a/cache.h b/cache.h > @@ -77,6 +77,61 @@ struct object_id { > +typedef void (*git_hash_init_fn)(void *ctx); > +typedef void (*git_hash_update_fn)(void *ctx, const void *in, size_t len); > +typedef void (*git_hash_final_fn)(unsigned char *hash, void *ctx); > + > +struct git_hash_algo { > + [...] > + /* The hash initialization function. */ > + git_hash_init_fn init_fn; > + > + /* The hash update function. */ > + git_hash_update_fn update_fn; > + > + /* The hash finalization function. */ > + git_hash_final_fn final_fn; > + [...] > +}; > diff --git a/sha1_file.c b/sha1_file.c > @@ -39,6 +39,49 @@ const struct object_id empty_blob_oid = { > +static inline void git_hash_sha1_init(void *ctx) > +{ > + git_SHA1_Init((git_SHA_CTX *)ctx); > +} > + > +static inline void git_hash_sha1_update(void *ctx, const void *data, size_t len) > +{ > + git_SHA1_Update((git_SHA_CTX *)ctx, data, len); > +} > + > +static inline void git_hash_sha1_final(unsigned char *hash, void *ctx) > +{ > + git_SHA1_Final(hash, (git_SHA_CTX *)ctx); > +} Why 'inline' if you only ever take the addresses of these functions? > +const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = { > + [...] > + { > + "sha-1", > + /* "sha1", big-endian */ > + 0x73686131, > + sizeof(git_SHA_CTX), > + GIT_SHA1_RAWSZ, > + GIT_SHA1_HEXSZ, > + git_hash_sha1_init, > + git_hash_sha1_update, > + git_hash_sha1_final, > + &empty_tree_oid, > + &empty_blob_oid, > + }, > +};