Coverage Report

Created: 2026-12-01 06:16

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
0
{
21
0
  int error;
22
23
0
  switch (algorithm) {
24
0
  case GIT_HASH_ALGORITHM_SHA1:
25
0
    error = git_hash_sha1_ctx_init(&ctx->ctx.sha1);
26
0
    break;
27
0
  case GIT_HASH_ALGORITHM_SHA256:
28
0
    error = git_hash_sha256_ctx_init(&ctx->ctx.sha256);
29
0
    break;
30
0
  default:
31
0
    git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
32
0
    error = -1;
33
0
  }
34
35
0
  ctx->algorithm = algorithm;
36
0
  return error;
37
0
}
38
39
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
40
0
{
41
0
  switch (ctx->algorithm) {
42
0
  case GIT_HASH_ALGORITHM_SHA1:
43
0
    git_hash_sha1_ctx_cleanup(&ctx->ctx.sha1);
44
0
    return;
45
0
  case GIT_HASH_ALGORITHM_SHA256:
46
0
    git_hash_sha256_ctx_cleanup(&ctx->ctx.sha256);
47
0
    return;
48
0
  default:
49
0
    /* unreachable */ ;
50
0
  }
51
0
}
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
0
{
70
0
  switch (ctx->algorithm) {
71
0
  case GIT_HASH_ALGORITHM_SHA1:
72
0
    return git_hash_sha1_update(&ctx->ctx.sha1, data, len);
73
0
  case GIT_HASH_ALGORITHM_SHA256:
74
0
    return git_hash_sha256_update(&ctx->ctx.sha256, data, len);
75
0
  default:
76
0
    /* unreachable */ ;
77
0
  }
78
79
0
  git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
80
0
  return -1;
81
0
}
82
83
int git_hash_final(unsigned char *out, git_hash_ctx *ctx)
84
0
{
85
0
  switch (ctx->algorithm) {
86
0
  case GIT_HASH_ALGORITHM_SHA1:
87
0
    return git_hash_sha1_final(out, &ctx->ctx.sha1);
88
0
  case GIT_HASH_ALGORITHM_SHA256:
89
0
    return git_hash_sha256_final(out, &ctx->ctx.sha256);
90
0
  default:
91
0
    /* unreachable */ ;
92
0
  }
93
94
0
  git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
95
0
  return -1;
96
0
}
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
0
{
104
0
  git_hash_ctx ctx;
105
0
  int error = 0;
106
107
0
  if (git_hash_ctx_init(&ctx, algorithm) < 0)
108
0
    return -1;
109
110
0
  if ((error = git_hash_update(&ctx, data, len)) >= 0)
111
0
    error = git_hash_final(out, &ctx);
112
113
0
  git_hash_ctx_cleanup(&ctx);
114
115
0
  return error;
116
0
}
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
}