]> git.proxmox.com Git - libgit2.git/blob - src/hash.c
Merge pull request #411 from boyski/gcc4
[libgit2.git] / src / hash.c
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "common.h"
9 #include "hash.h"
10
11 #if defined(PPC_SHA1)
12 # include "ppc/sha1.h"
13 #else
14 # include "sha1.h"
15 #endif
16
17 struct git_hash_ctx {
18 SHA_CTX c;
19 };
20
21 git_hash_ctx *git_hash_new_ctx(void)
22 {
23 git_hash_ctx *ctx = git__malloc(sizeof(*ctx));
24
25 if (!ctx)
26 return NULL;
27
28 SHA1_Init(&ctx->c);
29
30 return ctx;
31 }
32
33 void git_hash_free_ctx(git_hash_ctx *ctx)
34 {
35 free(ctx);
36 }
37
38 void git_hash_init(git_hash_ctx *ctx)
39 {
40 assert(ctx);
41 SHA1_Init(&ctx->c);
42 }
43
44 void git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
45 {
46 assert(ctx);
47 SHA1_Update(&ctx->c, data, len);
48 }
49
50 void git_hash_final(git_oid *out, git_hash_ctx *ctx)
51 {
52 assert(ctx);
53 SHA1_Final(out->id, &ctx->c);
54 }
55
56 void git_hash_buf(git_oid *out, const void *data, size_t len)
57 {
58 SHA_CTX c;
59
60 SHA1_Init(&c);
61 SHA1_Update(&c, data, len);
62 SHA1_Final(out->id, &c);
63 }
64
65 void git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n)
66 {
67 SHA_CTX c;
68 size_t i;
69
70 SHA1_Init(&c);
71 for (i = 0; i < n; i++)
72 SHA1_Update(&c, vec[i].data, vec[i].len);
73 SHA1_Final(out->id, &c);
74 }