Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
eb8de747
Commit
eb8de747
authored
Dec 28, 2011
by
nulltoken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
util: add git__fromhex()
parent
06fcf057
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
66 additions
and
51 deletions
+66
-51
src/oid.c
+5
-23
src/util.h
+23
-0
tests-clay/clay.h
+1
-0
tests-clay/clay_main.c
+11
-2
tests-clay/core/hex.c
+22
-0
tests-clay/object/raw/chars.c
+2
-13
tests/t01-rawobj.c
+2
-13
No files found.
src/oid.c
View file @
eb8de747
...
...
@@ -11,24 +11,6 @@
#include <string.h>
#include <limits.h>
static
signed
char
from_hex
[]
=
{
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 00 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 10 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 20 */
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 30 */
-
1
,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
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* e0 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* f0 */
};
static
char
to_hex
[]
=
"0123456789abcdef"
;
int
git_oid_fromstrn
(
git_oid
*
out
,
const
char
*
str
,
size_t
length
)
...
...
@@ -43,8 +25,8 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
length
=
GIT_OID_HEXSZ
;
for
(
p
=
0
;
p
<
length
-
1
;
p
+=
2
)
{
v
=
(
from_hex
[(
unsigned
char
)
str
[
p
+
0
]]
<<
4
)
|
from_hex
[(
unsigned
char
)
str
[
p
+
1
]]
;
v
=
(
git__fromhex
(
str
[
p
+
0
])
<<
4
)
|
git__fromhex
(
str
[
p
+
1
])
;
if
(
v
<
0
)
return
git__throw
(
GIT_ENOTOID
,
"Failed to generate sha1. Given string is not a valid sha1 hash"
);
...
...
@@ -53,7 +35,7 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
}
if
(
length
%
2
)
{
v
=
(
from_hex
[(
unsigned
char
)
str
[
p
+
0
]]
<<
4
);
v
=
(
git__fromhex
(
str
[
p
+
0
])
<<
4
);
if
(
v
<
0
)
return
git__throw
(
GIT_ENOTOID
,
"Failed to generate sha1. Given string is not a valid sha1 hash"
);
...
...
@@ -346,7 +328,7 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
is_leaf
=
0
;
for
(
i
=
0
;
i
<
GIT_OID_HEXSZ
;
++
i
)
{
int
c
=
from_hex
[(
int
)
text_oid
[
i
]]
;
int
c
=
git__fromhex
(
text_oid
[
i
])
;
trie_node
*
node
;
if
(
c
==
-
1
)
...
...
@@ -360,7 +342,7 @@ int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
tail
=
node
->
tail
;
node
->
tail
=
NULL
;
node
=
push_leaf
(
os
,
idx
,
from_hex
[(
int
)
tail
[
0
]]
,
&
tail
[
1
]);
node
=
push_leaf
(
os
,
idx
,
git__fromhex
(
tail
[
0
])
,
&
tail
[
1
]);
if
(
node
==
NULL
)
return
GIT_ENOMEM
;
}
...
...
src/util.h
View file @
eb8de747
...
...
@@ -133,5 +133,28 @@ typedef void (*git_refcount_freeptr)(void *r);
#define GIT_REFCOUNT_OWNER(r) (((git_refcount *)(r))->owner)
static
signed
char
from_hex
[]
=
{
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 00 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 10 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 20 */
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 30 */
-
1
,
10
,
11
,
12
,
13
,
14
,
15
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 40 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 50 */
-
1
,
10
,
11
,
12
,
13
,
14
,
15
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 60 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 70 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 80 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* 90 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* a0 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* b0 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* c0 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* d0 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* e0 */
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
/* f0 */
};
GIT_INLINE
(
int
)
git__fromhex
(
char
h
)
{
return
from_hex
[(
unsigned
char
)
h
];
}
#endif
/* INCLUDE_util_h__ */
tests-clay/clay.h
View file @
eb8de747
...
...
@@ -106,6 +106,7 @@ extern void test_core_filebuf__2(void);
extern
void
test_core_filebuf__3
(
void
);
extern
void
test_core_filebuf__4
(
void
);
extern
void
test_core_filebuf__5
(
void
);
extern
void
test_core_hex__fromhex
(
void
);
extern
void
test_core_oid__initialize
(
void
);
extern
void
test_core_oid__streq
(
void
);
extern
void
test_core_path__0_dirname
(
void
);
...
...
tests-clay/clay_main.c
View file @
eb8de747
...
...
@@ -167,6 +167,9 @@ static const struct clay_func _clay_cb_core_filebuf[] = {
{
"4"
,
&
test_core_filebuf__4
},
{
"5"
,
&
test_core_filebuf__5
}
};
static
const
struct
clay_func
_clay_cb_core_hex
[]
=
{
{
"fromhex"
,
&
test_core_hex__fromhex
}
};
static
const
struct
clay_func
_clay_cb_core_oid
[]
=
{
{
"streq"
,
&
test_core_oid__streq
}
};
...
...
@@ -363,6 +366,12 @@ static const struct clay_suite _clay_suites[] = {
_clay_cb_core_filebuf
,
6
},
{
"core::hex"
,
{
NULL
,
NULL
},
{
NULL
,
NULL
},
_clay_cb_core_hex
,
1
},
{
"core::oid"
,
{
"initialize"
,
&
test_core_oid__initialize
},
{
NULL
,
NULL
},
...
...
@@ -538,8 +547,8 @@ static const struct clay_suite _clay_suites[] = {
}
};
static
size_t
_clay_suite_count
=
3
8
;
static
size_t
_clay_callback_count
=
12
2
;
static
size_t
_clay_suite_count
=
3
9
;
static
size_t
_clay_callback_count
=
12
3
;
/* Core test functions */
static
void
...
...
tests-clay/core/hex.c
0 → 100644
View file @
eb8de747
#include "clay_libgit2.h"
#include "util.h"
void
test_core_hex__fromhex
(
void
)
{
/* Passing cases */
cl_assert
(
git__fromhex
(
'0'
)
==
0x0
);
cl_assert
(
git__fromhex
(
'1'
)
==
0x1
);
cl_assert
(
git__fromhex
(
'3'
)
==
0x3
);
cl_assert
(
git__fromhex
(
'9'
)
==
0x9
);
cl_assert
(
git__fromhex
(
'A'
)
==
0xa
);
cl_assert
(
git__fromhex
(
'C'
)
==
0xc
);
cl_assert
(
git__fromhex
(
'F'
)
==
0xf
);
cl_assert
(
git__fromhex
(
'a'
)
==
0xa
);
cl_assert
(
git__fromhex
(
'c'
)
==
0xc
);
cl_assert
(
git__fromhex
(
'f'
)
==
0xf
);
/* Failing cases */
cl_assert
(
git__fromhex
(
'g'
)
==
-
1
);
cl_assert
(
git__fromhex
(
'z'
)
==
-
1
);
cl_assert
(
git__fromhex
(
'X'
)
==
-
1
);
}
tests-clay/object/raw/chars.c
View file @
eb8de747
...
...
@@ -3,17 +3,6 @@
#include "odb.h"
static
int
from_hex
(
unsigned
int
i
)
{
if
(
i
>=
'0'
&&
i
<=
'9'
)
return
i
-
'0'
;
if
(
i
>=
'a'
&&
i
<=
'f'
)
return
10
+
(
i
-
'a'
);
if
(
i
>=
'A'
&&
i
<=
'F'
)
return
10
+
(
i
-
'A'
);
return
-
1
;
}
void
test_object_raw_chars__find_invalid_chars_in_oid
(
void
)
{
git_oid
out
;
...
...
@@ -28,8 +17,8 @@ void test_object_raw_chars__find_invalid_chars_in_oid(void)
for
(
i
=
0
;
i
<
256
;
i
++
)
{
in
[
38
]
=
(
char
)
i
;
if
(
from_
hex
(
i
)
>=
0
)
{
exp
[
19
]
=
(
unsigned
char
)(
from_
hex
(
i
)
<<
4
);
if
(
git__from
hex
(
i
)
>=
0
)
{
exp
[
19
]
=
(
unsigned
char
)(
git__from
hex
(
i
)
<<
4
);
cl_git_pass
(
git_oid_fromstr
(
&
out
,
in
));
cl_assert
(
memcmp
(
out
.
id
,
exp
,
sizeof
(
out
.
id
))
==
0
);
}
else
{
...
...
tests/t01-rawobj.c
View file @
eb8de747
...
...
@@ -52,17 +52,6 @@ BEGIN_TEST(oid2, "fail when parsing an invalid string as oid")
must_fail
(
git_oid_fromstr
(
&
out
,
"moo"
));
END_TEST
static
int
from_hex
(
unsigned
int
i
)
{
if
(
i
>=
'0'
&&
i
<=
'9'
)
return
i
-
'0'
;
if
(
i
>=
'a'
&&
i
<=
'f'
)
return
10
+
(
i
-
'a'
);
if
(
i
>=
'A'
&&
i
<=
'F'
)
return
10
+
(
i
-
'A'
);
return
-
1
;
}
BEGIN_TEST
(
oid3
,
"find all invalid characters when parsing an oid"
)
git_oid
out
;
unsigned
char
exp
[]
=
{
...
...
@@ -77,8 +66,8 @@ BEGIN_TEST(oid3, "find all invalid characters when parsing an oid")
for
(
i
=
0
;
i
<
256
;
i
++
)
{
in
[
38
]
=
(
char
)
i
;
if
(
from_
hex
(
i
)
>=
0
)
{
exp
[
19
]
=
(
unsigned
char
)(
from_
hex
(
i
)
<<
4
);
if
(
git__from
hex
(
i
)
>=
0
)
{
exp
[
19
]
=
(
unsigned
char
)(
git__from
hex
(
i
)
<<
4
);
must_pass
(
git_oid_fromstr
(
&
out
,
in
));
must_be_true
(
memcmp
(
out
.
id
,
exp
,
sizeof
(
out
.
id
))
==
0
);
}
else
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment