Fixed memory leaks in test suite
[libgit2.git] / src / oid.c
blob0bc152bdf2707576e6ef9331189607c89f9ef8a1
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 "git/oid.h"
28 #include <string.h>
30 static signed char from_hex[] = {
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
34 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
35 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
36 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
37 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
38 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
39 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
48 static char to_hex[] = "0123456789abcdef";
50 int git_oid_mkstr(git_oid *out, const char *str)
52 size_t p;
53 for (p = 0; p < sizeof(out->id); p++, str += 2) {
54 int v = (from_hex[(unsigned char)str[0]] << 4)
55 | from_hex[(unsigned char)str[1]];
56 if (v < 0)
57 return GIT_ENOTOID;
58 out->id[p] = (unsigned char)v;
60 return GIT_SUCCESS;
63 GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
65 *str++ = to_hex[val >> 4];
66 *str++ = to_hex[val & 0xf];
67 return str;
70 void git_oid_fmt(char *str, const git_oid *oid)
72 size_t i;
74 for (i = 0; i < sizeof(oid->id); i++)
75 str = fmt_one(str, oid->id[i]);
78 void git_oid_pathfmt(char *str, const git_oid *oid)
80 size_t i;
82 str = fmt_one(str, oid->id[0]);
83 *str++ = '/';
84 for (i = 1; i < sizeof(oid->id); i++)
85 str = fmt_one(str, oid->id[i]);
88 char *git_oid_allocfmt(const git_oid *oid)
90 char *str = git__malloc(GIT_OID_HEXSZ + 1);
91 if (!str)
92 return NULL;
93 git_oid_fmt(str, oid);
94 str[GIT_OID_HEXSZ] = '\0';
95 return str;
98 char *git_oid_to_string(char *out, size_t n, const git_oid *oid)
100 char str[GIT_OID_HEXSZ];
102 if (!out || n == 0 || !oid)
103 return "";
105 n--; /* allow room for terminating NUL */
107 if (n > 0) {
108 git_oid_fmt(str, oid);
109 if (n > GIT_OID_HEXSZ)
110 n = GIT_OID_HEXSZ;
111 memcpy(out, str, n);
114 out[n] = '\0';
116 return out;