source src/hash.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
1 | - | /* | ||
2 | - | * Copyright (C) the libgit2 contributors. All rights reserved. | ||
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 "hash.h" | ||
9 | - | |||
10 | 9 | 2 | int git_hash_global_init(void) | |
11 | - | { | ||
12 | 9 | 2 | return git_hash_sha1_global_init(); | |
13 | - | } | ||
14 | - | |||
15 | 140896 | 2 | int git_hash_ctx_init(git_hash_ctx *ctx) | |
16 | - | { | ||
17 | - | int error; | ||
18 | - | |||
19 | 140896 | 2,3 | if ((error = git_hash_sha1_ctx_init(&ctx->sha1)) < 0) | |
20 | ##### | 4 | return error; | |
21 | - | |||
22 | 140775 | 5 | ctx->algo = GIT_HASH_ALGO_SHA1; | |
23 | - | |||
24 | 140775 | 5 | return 0; | |
25 | - | } | ||
26 | - | |||
27 | 141366 | 2 | void git_hash_ctx_cleanup(git_hash_ctx *ctx) | |
28 | - | { | ||
29 | 141366 | 2 | switch (ctx->algo) { | |
30 | - | case GIT_HASH_ALGO_SHA1: | ||
31 | 141366 | 3 | git_hash_sha1_ctx_cleanup(&ctx->sha1); | |
32 | 141314 | 5 | return; | |
33 | - | default: | ||
34 | - | 4 | assert(0); | |
35 | - | } | ||
36 | - | } | ||
37 | - | |||
38 | 5302 | 2 | int git_hash_init(git_hash_ctx *ctx) | |
39 | - | { | ||
40 | 5302 | 2 | switch (ctx->algo) { | |
41 | - | case GIT_HASH_ALGO_SHA1: | ||
42 | 5302 | 3 | return git_hash_sha1_init(&ctx->sha1); | |
43 | - | default: | ||
44 | - | 4 | assert(0); | |
45 | - | return -1; | ||
46 | - | } | ||
47 | - | } | ||
48 | - | |||
49 | 256406 | 2 | int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) | |
50 | - | { | ||
51 | 256406 | 2 | switch (ctx->algo) { | |
52 | - | case GIT_HASH_ALGO_SHA1: | ||
53 | 256406 | 3 | return git_hash_sha1_update(&ctx->sha1, data, len); | |
54 | - | default: | ||
55 | - | 4 | assert(0); | |
56 | - | return -1; | ||
57 | - | } | ||
58 | - | } | ||
59 | - | |||
60 | 141162 | 2 | int git_hash_final(git_oid *out, git_hash_ctx *ctx) | |
61 | - | { | ||
62 | 141162 | 2 | switch (ctx->algo) { | |
63 | - | case GIT_HASH_ALGO_SHA1: | ||
64 | 141162 | 3 | return git_hash_sha1_final(out, &ctx->sha1); | |
65 | - | default: | ||
66 | - | 4 | assert(0); | |
67 | - | return -1; | ||
68 | - | } | ||
69 | - | } | ||
70 | - | |||
71 | 43213 | 2 | int git_hash_buf(git_oid *out, const void *data, size_t len) | |
72 | - | { | ||
73 | - | git_hash_ctx ctx; | ||
74 | 43213 | 2 | int error = 0; | |
75 | - | |||
76 | 43215 | 2,3 | if (git_hash_ctx_init(&ctx) < 0) | |
77 | ##### | 4 | return -1; | |
78 | - | |||
79 | 43215 | 5,6 | if ((error = git_hash_update(&ctx, data, len)) >= 0) | |
80 | 43213 | 7 | error = git_hash_final(out, &ctx); | |
81 | - | |||
82 | 43213 | 8 | git_hash_ctx_cleanup(&ctx); | |
83 | - | |||
84 | 43212 | 9 | return error; | |
85 | - | } | ||
86 | - | |||
87 | 83502 | 2 | int git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n) | |
88 | - | { | ||
89 | - | git_hash_ctx ctx; | ||
90 | - | size_t i; | ||
91 | 83502 | 2 | int error = 0; | |
92 | - | |||
93 | 83502 | 2,3 | if (git_hash_ctx_init(&ctx) < 0) | |
94 | 11 | 4 | return -1; | |
95 | - | |||
96 | 250286 | 5,9,10 | for (i = 0; i < n; i++) { | |
97 | 166958 | 6,7 | if ((error = git_hash_update(&ctx, vec[i].data, vec[i].len)) < 0) | |
98 | ##### | 8 | goto done; | |
99 | - | } | ||
100 | - | |||
101 | 83983 | 11 | error = git_hash_final(out, &ctx); | |
102 | - | |||
103 | - | done: | ||
104 | 84030 | 12 | git_hash_ctx_cleanup(&ctx); | |
105 | - | |||
106 | 83890 | 13 | return error; | |
107 | - | } |