Coverage Report

Created: 2025-10-10 06:04

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