]>
git.proxmox.com Git - libgit2.git/blob - src/util.c
2 * Copyright (C) 2009-2011 the libgit2 contributors
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.
18 void git_libgit2_version(int *major
, int *minor
, int *rev
)
20 *major
= LIBGIT2_VER_MAJOR
;
21 *minor
= LIBGIT2_VER_MINOR
;
22 *rev
= LIBGIT2_VER_REVISION
;
25 void git_strarray_free(git_strarray
*array
)
28 for (i
= 0; i
< array
->count
; ++i
)
29 free(array
->strings
[i
]);
34 int git__fnmatch(const char *pattern
, const char *name
, int flags
)
38 ret
= p_fnmatch(pattern
, name
, flags
);
45 return git__throw(GIT_EOSERR
, "Error trying to match path");
49 int git__strtol32(long *result
, const char *nptr
, const char **endptr
, int base
)
53 int c
, ovfl
, v
, neg
, ndig
;
70 if (*p
== '-' || *p
== '+')
82 if (p
[1] == 'x' || p
[1] == 'X') {
87 } else if (base
== 16 && *p
== '0') {
88 if (p
[1] == 'x' || p
[1] == 'X')
90 } else if (base
< 0 || 36 < base
)
94 * Non-empty sequence of digits
101 else if ('a'<=c
&& c
<='z')
103 else if ('A'<=c
&& c
<='Z')
115 return git__throw(GIT_ENOTNUM
, "Failed to convert string to long. Not a number");
121 return git__throw(GIT_EOVERFLOW
, "Failed to convert string to long. Overflow error");
123 *result
= neg
? -n
: n
;
127 void git__strntolower(char *str
, size_t len
)
131 for (i
= 0; i
< len
; ++i
) {
132 str
[i
] = (char) tolower(str
[i
]);
136 void git__strtolower(char *str
)
138 git__strntolower(str
, strlen(str
));
141 int git__prefixcmp(const char *str
, const char *prefix
)
144 char p
= *(prefix
++), s
;
147 if ((s
= *(str
++)) != p
)
152 int git__suffixcmp(const char *str
, const char *suffix
)
154 size_t a
= strlen(str
);
155 size_t b
= strlen(suffix
);
158 return strcmp(str
+ (a
- b
), suffix
);
161 char *git__strtok(char **end
, const char *sep
)
165 while (*ptr
&& strchr(sep
, *ptr
))
172 while (**end
&& !strchr(sep
, **end
))
186 void git__hexdump(const char *buffer
, size_t len
)
188 static const size_t LINE_WIDTH
= 16;
190 size_t line_count
, last_line
, i
, j
;
193 line_count
= (len
/ LINE_WIDTH
);
194 last_line
= (len
% LINE_WIDTH
);
196 for (i
= 0; i
< line_count
; ++i
) {
197 line
= buffer
+ (i
* LINE_WIDTH
);
198 for (j
= 0; j
< LINE_WIDTH
; ++j
, ++line
)
199 printf("%02X ", (unsigned char)*line
& 0xFF);
203 line
= buffer
+ (i
* LINE_WIDTH
);
204 for (j
= 0; j
< LINE_WIDTH
; ++j
, ++line
)
205 printf("%c", (*line
>= 32 && *line
<= 126) ? *line
: '.');
212 line
= buffer
+ (line_count
* LINE_WIDTH
);
213 for (j
= 0; j
< last_line
; ++j
, ++line
)
214 printf("%02X ", (unsigned char)*line
& 0xFF);
216 for (j
= 0; j
< (LINE_WIDTH
- last_line
); ++j
)
221 line
= buffer
+ (line_count
* LINE_WIDTH
);
222 for (j
= 0; j
< last_line
; ++j
, ++line
)
223 printf("%c", (*line
>= 32 && *line
<= 126) ? *line
: '.');
231 #ifdef GIT_LEGACY_HASH
232 uint32_t git__hash(const void *key
, int len
, unsigned int seed
)
234 const uint32_t m
= 0x5bd1e995;
236 uint32_t h
= seed
^ len
;
238 const unsigned char *data
= (const unsigned char *)key
;
241 uint32_t k
= *(uint32_t *)data
;
255 case 3: h
^= data
[2] << 16;
256 case 2: h
^= data
[1] << 8;
257 case 1: h
^= data
[0];
269 Cross-platform version of Murmurhash3
270 http://code.google.com/p/smhasher/wiki/MurmurHash3
271 by Austin Appleby (aappleby@gmail.com)
273 This code is on the public domain.
275 uint32_t git__hash(const void *key
, int len
, uint32_t seed
)
278 #define MURMUR_BLOCK() {\
280 k1 = git__rotl(k1,11);\
283 h1 = h1*3 + 0x52dce729;\
284 c1 = c1*5 + 0x7b7d159c;\
285 c2 = c2*5 + 0x6bce6396;\
288 const uint8_t *data
= (const uint8_t*)key
;
289 const int nblocks
= len
/ 4;
291 const uint32_t *blocks
= (const uint32_t *)(data
+ nblocks
* 4);
292 const uint8_t *tail
= (const uint8_t *)(data
+ nblocks
* 4);
294 uint32_t h1
= 0x971e137b ^ seed
;
297 uint32_t c1
= 0x95543787;
298 uint32_t c2
= 0x2ad7eb25;
302 for (i
= -nblocks
; i
; i
++) {
310 case 3: k1
^= tail
[2] << 16;
311 case 2: k1
^= tail
[1] << 8;
312 case 1: k1
^= tail
[0];
328 * A modified `bsearch` from the BSD glibc.
330 * Copyright (c) 1990 Regents of the University of California.
331 * All rights reserved.
333 void **git__bsearch(const void *key
, void **base
, size_t nmemb
, int (*compar
)(const void *, const void *))
338 for (lim
= nmemb
; lim
!= 0; lim
>>= 1) {
339 p
= base
+ (lim
>> 1);
340 cmp
= (*compar
)(key
, *p
);
341 if (cmp
> 0) { /* key > p: move right */
344 } else if (cmp
== 0) {
346 } /* else move left */
354 * We don't want direct pointers to the CRT on Windows, we may
355 * get stdcall conflicts.
357 int git__strcmp_cb(const void *a
, const void *b
)
359 const char *stra
= (const char *)a
;
360 const char *strb
= (const char *)b
;
362 return strcmp(stra
, strb
);