line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
87
|
|
|
|
|
|
int git_hash_global_init(void) |
11
|
|
|
|
|
|
|
{ |
12
|
174
|
|
|
|
|
|
if (git_hash_sha1_global_init() < 0 || |
13
|
87
|
|
|
|
|
|
git_hash_sha256_global_init() < 0) |
14
|
0
|
|
|
|
|
|
return -1; |
15
|
|
|
|
|
|
|
|
16
|
87
|
|
|
|
|
|
return 0; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
1531
|
|
|
|
|
|
int git_hash_ctx_init(git_hash_ctx *ctx, git_hash_algorithm_t algorithm) |
20
|
|
|
|
|
|
|
{ |
21
|
|
|
|
|
|
|
int error; |
22
|
|
|
|
|
|
|
|
23
|
1531
|
|
|
|
|
|
switch (algorithm) { |
24
|
|
|
|
|
|
|
case GIT_HASH_ALGORITHM_SHA1: |
25
|
1531
|
|
|
|
|
|
error = git_hash_sha1_ctx_init(&ctx->ctx.sha1); |
26
|
1531
|
|
|
|
|
|
break; |
27
|
|
|
|
|
|
|
case GIT_HASH_ALGORITHM_SHA256: |
28
|
0
|
|
|
|
|
|
error = git_hash_sha256_ctx_init(&ctx->ctx.sha256); |
29
|
0
|
|
|
|
|
|
break; |
30
|
|
|
|
|
|
|
default: |
31
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); |
32
|
0
|
|
|
|
|
|
error = -1; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
1531
|
|
|
|
|
|
ctx->algorithm = algorithm; |
36
|
1531
|
|
|
|
|
|
return error; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
1531
|
|
|
|
|
|
void git_hash_ctx_cleanup(git_hash_ctx *ctx) |
40
|
|
|
|
|
|
|
{ |
41
|
1531
|
|
|
|
|
|
switch (ctx->algorithm) { |
42
|
|
|
|
|
|
|
case GIT_HASH_ALGORITHM_SHA1: |
43
|
1531
|
|
|
|
|
|
git_hash_sha1_ctx_cleanup(&ctx->ctx.sha1); |
44
|
1531
|
|
|
|
|
|
return; |
45
|
|
|
|
|
|
|
case GIT_HASH_ALGORITHM_SHA256: |
46
|
0
|
|
|
|
|
|
git_hash_sha256_ctx_cleanup(&ctx->ctx.sha256); |
47
|
0
|
|
|
|
|
|
return; |
48
|
|
|
|
|
|
|
default: |
49
|
|
|
|
|
|
|
/* unreachable */ ; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
47
|
|
|
|
|
|
int git_hash_init(git_hash_ctx *ctx) |
54
|
|
|
|
|
|
|
{ |
55
|
47
|
|
|
|
|
|
switch (ctx->algorithm) { |
56
|
|
|
|
|
|
|
case GIT_HASH_ALGORITHM_SHA1: |
57
|
47
|
|
|
|
|
|
return git_hash_sha1_init(&ctx->ctx.sha1); |
58
|
|
|
|
|
|
|
case GIT_HASH_ALGORITHM_SHA256: |
59
|
0
|
|
|
|
|
|
return git_hash_sha256_init(&ctx->ctx.sha256); |
60
|
|
|
|
|
|
|
default: |
61
|
|
|
|
|
|
|
/* unreachable */ ; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); |
65
|
0
|
|
|
|
|
|
return -1; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
3046
|
|
|
|
|
|
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) |
69
|
|
|
|
|
|
|
{ |
70
|
3046
|
|
|
|
|
|
switch (ctx->algorithm) { |
71
|
|
|
|
|
|
|
case GIT_HASH_ALGORITHM_SHA1: |
72
|
3046
|
|
|
|
|
|
return git_hash_sha1_update(&ctx->ctx.sha1, data, len); |
73
|
|
|
|
|
|
|
case GIT_HASH_ALGORITHM_SHA256: |
74
|
0
|
|
|
|
|
|
return git_hash_sha256_update(&ctx->ctx.sha256, data, len); |
75
|
|
|
|
|
|
|
default: |
76
|
|
|
|
|
|
|
/* unreachable */ ; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); |
80
|
0
|
|
|
|
|
|
return -1; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
1519
|
|
|
|
|
|
int git_hash_final(unsigned char *out, git_hash_ctx *ctx) |
84
|
|
|
|
|
|
|
{ |
85
|
1519
|
|
|
|
|
|
switch (ctx->algorithm) { |
86
|
|
|
|
|
|
|
case GIT_HASH_ALGORITHM_SHA1: |
87
|
1519
|
|
|
|
|
|
return git_hash_sha1_final(out, &ctx->ctx.sha1); |
88
|
|
|
|
|
|
|
case GIT_HASH_ALGORITHM_SHA256: |
89
|
0
|
|
|
|
|
|
return git_hash_sha256_final(out, &ctx->ctx.sha256); |
90
|
|
|
|
|
|
|
default: |
91
|
|
|
|
|
|
|
/* unreachable */ ; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm"); |
95
|
0
|
|
|
|
|
|
return -1; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
186
|
|
|
|
|
|
int git_hash_buf( |
99
|
|
|
|
|
|
|
unsigned char *out, |
100
|
|
|
|
|
|
|
const void *data, |
101
|
|
|
|
|
|
|
size_t len, |
102
|
|
|
|
|
|
|
git_hash_algorithm_t algorithm) |
103
|
|
|
|
|
|
|
{ |
104
|
|
|
|
|
|
|
git_hash_ctx ctx; |
105
|
186
|
|
|
|
|
|
int error = 0; |
106
|
|
|
|
|
|
|
|
107
|
186
|
50
|
|
|
|
|
if (git_hash_ctx_init(&ctx, algorithm) < 0) |
108
|
0
|
|
|
|
|
|
return -1; |
109
|
|
|
|
|
|
|
|
110
|
186
|
50
|
|
|
|
|
if ((error = git_hash_update(&ctx, data, len)) >= 0) |
111
|
186
|
|
|
|
|
|
error = git_hash_final(out, &ctx); |
112
|
|
|
|
|
|
|
|
113
|
186
|
|
|
|
|
|
git_hash_ctx_cleanup(&ctx); |
114
|
|
|
|
|
|
|
|
115
|
186
|
|
|
|
|
|
return error; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
994
|
|
|
|
|
|
int git_hash_vec( |
119
|
|
|
|
|
|
|
unsigned char *out, |
120
|
|
|
|
|
|
|
git_str_vec *vec, |
121
|
|
|
|
|
|
|
size_t n, |
122
|
|
|
|
|
|
|
git_hash_algorithm_t algorithm) |
123
|
|
|
|
|
|
|
{ |
124
|
|
|
|
|
|
|
git_hash_ctx ctx; |
125
|
|
|
|
|
|
|
size_t i; |
126
|
994
|
|
|
|
|
|
int error = 0; |
127
|
|
|
|
|
|
|
|
128
|
994
|
50
|
|
|
|
|
if (git_hash_ctx_init(&ctx, algorithm) < 0) |
129
|
0
|
|
|
|
|
|
return -1; |
130
|
|
|
|
|
|
|
|
131
|
2982
|
100
|
|
|
|
|
for (i = 0; i < n; i++) { |
132
|
1988
|
50
|
|
|
|
|
if ((error = git_hash_update(&ctx, vec[i].data, vec[i].len)) < 0) |
133
|
0
|
|
|
|
|
|
goto done; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
994
|
|
|
|
|
|
error = git_hash_final(out, &ctx); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
done: |
139
|
994
|
|
|
|
|
|
git_hash_ctx_cleanup(&ctx); |
140
|
|
|
|
|
|
|
|
141
|
994
|
|
|
|
|
|
return error; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
5
|
|
|
|
|
|
int git_hash_fmt(char *out, unsigned char *hash, size_t hash_len) |
145
|
|
|
|
|
|
|
{ |
146
|
|
|
|
|
|
|
static char hex[] = "0123456789abcdef"; |
147
|
5
|
|
|
|
|
|
char *str = out; |
148
|
|
|
|
|
|
|
size_t i; |
149
|
|
|
|
|
|
|
|
150
|
105
|
100
|
|
|
|
|
for (i = 0; i < hash_len; i++) { |
151
|
100
|
|
|
|
|
|
*str++ = hex[hash[i] >> 4]; |
152
|
100
|
|
|
|
|
|
*str++ = hex[hash[i] & 0x0f]; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
5
|
|
|
|
|
|
*str++ = '\0'; |
156
|
|
|
|
|
|
|
|
157
|
5
|
|
|
|
|
|
return 0; |
158
|
|
|
|
|
|
|
} |