line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 "buf.h" |
9
|
|
|
|
|
|
|
#include "common.h" |
10
|
|
|
|
|
|
|
|
11
|
57
|
|
|
|
|
|
int git_buf_sanitize(git_buf *buf) |
12
|
|
|
|
|
|
|
{ |
13
|
57
|
50
|
|
|
|
|
GIT_ASSERT_ARG(buf); |
14
|
|
|
|
|
|
|
|
15
|
57
|
100
|
|
|
|
|
if (buf->reserved > 0) |
16
|
1
|
|
|
|
|
|
buf->ptr[0] = '\0'; |
17
|
|
|
|
|
|
|
else |
18
|
56
|
|
|
|
|
|
buf->ptr = git_str__initstr; |
19
|
|
|
|
|
|
|
|
20
|
57
|
|
|
|
|
|
buf->size = 0; |
21
|
57
|
|
|
|
|
|
return 0; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
57
|
|
|
|
|
|
int git_buf_tostr(git_str *out, git_buf *buf) |
25
|
|
|
|
|
|
|
{ |
26
|
57
|
50
|
|
|
|
|
GIT_ASSERT_ARG(out); |
27
|
57
|
50
|
|
|
|
|
GIT_ASSERT_ARG(buf); |
28
|
|
|
|
|
|
|
|
29
|
57
|
50
|
|
|
|
|
if (git_buf_sanitize(buf) < 0) |
30
|
0
|
|
|
|
|
|
return -1; |
31
|
|
|
|
|
|
|
|
32
|
57
|
|
|
|
|
|
out->ptr = buf->ptr; |
33
|
57
|
|
|
|
|
|
out->asize = buf->reserved; |
34
|
57
|
|
|
|
|
|
out->size = buf->size; |
35
|
|
|
|
|
|
|
|
36
|
57
|
|
|
|
|
|
buf->ptr = git_str__initstr; |
37
|
57
|
|
|
|
|
|
buf->reserved = 0; |
38
|
57
|
|
|
|
|
|
buf->size = 0; |
39
|
|
|
|
|
|
|
|
40
|
57
|
|
|
|
|
|
return 0; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
52
|
|
|
|
|
|
int git_buf_fromstr(git_buf *out, git_str *str) |
44
|
|
|
|
|
|
|
{ |
45
|
52
|
50
|
|
|
|
|
GIT_ASSERT_ARG(out); |
46
|
52
|
50
|
|
|
|
|
GIT_ASSERT_ARG(str); |
47
|
|
|
|
|
|
|
|
48
|
52
|
|
|
|
|
|
out->ptr = str->ptr; |
49
|
52
|
|
|
|
|
|
out->reserved = str->asize; |
50
|
52
|
|
|
|
|
|
out->size = str->size; |
51
|
|
|
|
|
|
|
|
52
|
52
|
|
|
|
|
|
str->ptr = git_str__initstr; |
53
|
52
|
|
|
|
|
|
str->asize = 0; |
54
|
52
|
|
|
|
|
|
str->size = 0; |
55
|
|
|
|
|
|
|
|
56
|
52
|
|
|
|
|
|
return 0; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
63
|
|
|
|
|
|
void git_buf_dispose(git_buf *buf) |
60
|
|
|
|
|
|
|
{ |
61
|
63
|
50
|
|
|
|
|
if (!buf) |
62
|
0
|
|
|
|
|
|
return; |
63
|
|
|
|
|
|
|
|
64
|
63
|
100
|
|
|
|
|
if (buf->ptr != git_str__initstr) |
65
|
59
|
|
|
|
|
|
git__free(buf->ptr); |
66
|
|
|
|
|
|
|
|
67
|
63
|
|
|
|
|
|
buf->ptr = git_str__initstr; |
68
|
63
|
|
|
|
|
|
buf->reserved = 0; |
69
|
63
|
|
|
|
|
|
buf->size = 0; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
#ifndef GIT_DEPRECATE_HARD |
73
|
5
|
|
|
|
|
|
int git_buf_grow(git_buf *buffer, size_t target_size) |
74
|
|
|
|
|
|
|
{ |
75
|
|
|
|
|
|
|
char *newptr; |
76
|
|
|
|
|
|
|
|
77
|
5
|
50
|
|
|
|
|
if (buffer->reserved >= target_size) |
78
|
0
|
|
|
|
|
|
return 0; |
79
|
|
|
|
|
|
|
|
80
|
5
|
100
|
|
|
|
|
if (buffer->ptr == git_str__initstr) |
81
|
1
|
|
|
|
|
|
newptr = git__malloc(target_size); |
82
|
|
|
|
|
|
|
else |
83
|
4
|
|
|
|
|
|
newptr = git__realloc(buffer->ptr, target_size); |
84
|
|
|
|
|
|
|
|
85
|
5
|
50
|
|
|
|
|
if (!newptr) |
86
|
0
|
|
|
|
|
|
return -1; |
87
|
|
|
|
|
|
|
|
88
|
5
|
|
|
|
|
|
buffer->ptr = newptr; |
89
|
5
|
|
|
|
|
|
buffer->reserved = target_size; |
90
|
5
|
|
|
|
|
|
return 0; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
4
|
|
|
|
|
|
int git_buf_set(git_buf *buffer, const void *data, size_t datalen) |
94
|
|
|
|
|
|
|
{ |
95
|
|
|
|
|
|
|
size_t alloclen; |
96
|
|
|
|
|
|
|
|
97
|
4
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, datalen, 1); |
|
|
50
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
4
|
50
|
|
|
|
|
if (git_buf_grow(buffer, alloclen) < 0) |
100
|
0
|
|
|
|
|
|
return -1; |
101
|
|
|
|
|
|
|
|
102
|
4
|
|
|
|
|
|
memmove(buffer->ptr, data, datalen); |
103
|
4
|
|
|
|
|
|
buffer->size = datalen; |
104
|
4
|
|
|
|
|
|
buffer->ptr[buffer->size] = '\0'; |
105
|
|
|
|
|
|
|
|
106
|
4
|
|
|
|
|
|
return 0; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
int git_buf_is_binary(const git_buf *buf) |
110
|
|
|
|
|
|
|
{ |
111
|
0
|
|
|
|
|
|
git_str str = GIT_STR_INIT_CONST(buf->ptr, buf->size); |
112
|
0
|
|
|
|
|
|
return git_str_is_binary(&str); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
int git_buf_contains_nul(const git_buf *buf) |
116
|
|
|
|
|
|
|
{ |
117
|
0
|
|
|
|
|
|
git_str str = GIT_STR_INIT_CONST(buf->ptr, buf->size); |
118
|
0
|
|
|
|
|
|
return git_str_contains_nul(&str); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
void git_buf_free(git_buf *buffer) |
122
|
|
|
|
|
|
|
{ |
123
|
0
|
|
|
|
|
|
git_buf_dispose(buffer); |
124
|
0
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
#endif |