Fixed memory leaks in test suite
[libgit2.git] / src / hash.c
blob775e4b4c1296e9e3104f2a36ca9cf9356a130959
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
26 #include "common.h"
27 #include "hash.h"
29 #if defined(PPC_SHA1)
30 # include "ppc/sha1.h"
31 #elif defined(OPENSSL_SHA1)
32 # include <openssl/sha.h>
33 #else
34 # include "block-sha1/sha1.h"
35 #endif
37 struct git_hash_ctx {
38 SHA_CTX c;
41 git_hash_ctx *git_hash_new_ctx(void)
43 git_hash_ctx *ctx = git__malloc(sizeof(*ctx));
45 if (!ctx)
46 return NULL;
48 SHA1_Init(&ctx->c);
50 return ctx;
53 void git_hash_free_ctx(git_hash_ctx *ctx)
55 free(ctx);
58 void git_hash_init(git_hash_ctx *ctx)
60 assert(ctx);
61 SHA1_Init(&ctx->c);
64 void git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
66 assert(ctx);
67 SHA1_Update(&ctx->c, data, len);
70 void git_hash_final(git_oid *out, git_hash_ctx *ctx)
72 assert(ctx);
73 SHA1_Final(out->id, &ctx->c);
76 void git_hash_buf(git_oid *out, const void *data, size_t len)
78 SHA_CTX c;
80 SHA1_Init(&c);
81 SHA1_Update(&c, data, len);
82 SHA1_Final(out->id, &c);
85 void git_hash_vec(git_oid *out, git_buf_vec *vec, size_t n)
87 SHA_CTX c;
88 size_t i;
90 SHA1_Init(&c);
91 for (i = 0; i < n; i++)
92 SHA1_Update(&c, vec[i].data, vec[i].len);
93 SHA1_Final(out->id, &c);