]>
git.proxmox.com Git - libgit2.git/blob - src/buffer.c
2 * Copyright (C) 2009-2012 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.
12 /* Used as default value for git_buf->ptr so that people can always
13 * assume ptr is non-NULL and zero terminated even for new git_bufs.
15 char git_buf__initbuf
[1];
19 #define ENSURE_SIZE(b, d) \
20 if ((d) > buf->asize && git_buf_grow(b, (d)) < 0)\
24 void git_buf_init(git_buf
*buf
, size_t initial_size
)
28 buf
->ptr
= git_buf__initbuf
;
31 git_buf_grow(buf
, initial_size
);
34 int git_buf_try_grow(git_buf
*buf
, size_t target_size
, bool mark_oom
)
39 if (buf
->ptr
== git_buf__oom
)
42 if (target_size
<= buf
->asize
)
45 if (buf
->asize
== 0) {
46 new_size
= target_size
;
49 new_size
= buf
->asize
;
53 /* grow the buffer size by 1.5, until it's big enough
54 * to fit our target size */
55 while (new_size
< target_size
)
56 new_size
= (new_size
<< 1) - (new_size
>> 1);
58 /* round allocation up to multiple of 8 */
59 new_size
= (new_size
+ 7) & ~7;
61 new_ptr
= git__realloc(new_ptr
, new_size
);
65 buf
->ptr
= git_buf__oom
;
69 buf
->asize
= new_size
;
72 /* truncate the existing buffer size if necessary */
73 if (buf
->size
>= buf
->asize
)
74 buf
->size
= buf
->asize
- 1;
75 buf
->ptr
[buf
->size
] = '\0';
80 void git_buf_free(git_buf
*buf
)
84 if (buf
->ptr
!= git_buf__initbuf
&& buf
->ptr
!= git_buf__oom
)
90 void git_buf_clear(git_buf
*buf
)
97 int git_buf_set(git_buf
*buf
, const char *data
, size_t len
)
99 if (len
== 0 || data
== NULL
) {
102 if (data
!= buf
->ptr
) {
103 ENSURE_SIZE(buf
, len
+ 1);
104 memmove(buf
->ptr
, data
, len
);
107 buf
->ptr
[buf
->size
] = '\0';
112 int git_buf_sets(git_buf
*buf
, const char *string
)
114 return git_buf_set(buf
, string
, string
? strlen(string
) : 0);
117 int git_buf_putc(git_buf
*buf
, char c
)
119 ENSURE_SIZE(buf
, buf
->size
+ 2);
120 buf
->ptr
[buf
->size
++] = c
;
121 buf
->ptr
[buf
->size
] = '\0';
125 int git_buf_put(git_buf
*buf
, const char *data
, size_t len
)
127 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
128 memmove(buf
->ptr
+ buf
->size
, data
, len
);
130 buf
->ptr
[buf
->size
] = '\0';
134 int git_buf_puts(git_buf
*buf
, const char *string
)
137 return git_buf_put(buf
, string
, strlen(string
));
140 static const char b64str
[64] =
141 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
143 int git_buf_put_base64(git_buf
*buf
, const char *data
, size_t len
)
145 size_t extra
= len
% 3;
146 uint8_t *write
, a
, b
, c
;
147 const uint8_t *read
= (const uint8_t *)data
;
149 ENSURE_SIZE(buf
, buf
->size
+ 4 * ((len
/ 3) + !!extra
) + 1);
150 write
= (uint8_t *)&buf
->ptr
[buf
->size
];
152 /* convert each run of 3 bytes into 4 output bytes */
153 for (len
-= extra
; len
> 0; len
-= 3) {
158 *write
++ = b64str
[a
>> 2];
159 *write
++ = b64str
[(a
& 0x03) << 4 | b
>> 4];
160 *write
++ = b64str
[(b
& 0x0f) << 2 | c
>> 6];
161 *write
++ = b64str
[c
& 0x3f];
166 b
= (extra
> 1) ? *read
++ : 0;
168 *write
++ = b64str
[a
>> 2];
169 *write
++ = b64str
[(a
& 0x03) << 4 | b
>> 4];
170 *write
++ = (extra
> 1) ? b64str
[(b
& 0x0f) << 2] : '=';
174 buf
->size
= ((char *)write
) - buf
->ptr
;
175 buf
->ptr
[buf
->size
] = '\0';
180 int git_buf_vprintf(git_buf
*buf
, const char *format
, va_list ap
)
183 const size_t expected_size
= buf
->size
+ (strlen(format
) * 2);
185 ENSURE_SIZE(buf
, expected_size
);
192 buf
->ptr
+ buf
->size
,
193 buf
->asize
- buf
->size
,
199 buf
->ptr
= git_buf__oom
;
203 if ((size_t)len
+ 1 <= buf
->asize
- buf
->size
) {
208 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
214 int git_buf_printf(git_buf
*buf
, const char *format
, ...)
219 va_start(ap
, format
);
220 r
= git_buf_vprintf(buf
, format
, ap
);
226 void git_buf_copy_cstr(char *data
, size_t datasize
, const git_buf
*buf
)
230 assert(data
&& datasize
&& buf
);
234 if (buf
->size
== 0 || buf
->asize
<= 0)
238 if (copylen
> datasize
- 1)
239 copylen
= datasize
- 1;
240 memmove(data
, buf
->ptr
, copylen
);
241 data
[copylen
] = '\0';
244 void git_buf_consume(git_buf
*buf
, const char *end
)
246 if (end
> buf
->ptr
&& end
<= buf
->ptr
+ buf
->size
) {
247 size_t consumed
= end
- buf
->ptr
;
248 memmove(buf
->ptr
, end
, buf
->size
- consumed
);
249 buf
->size
-= consumed
;
250 buf
->ptr
[buf
->size
] = '\0';
254 void git_buf_truncate(git_buf
*buf
, size_t len
)
256 if (len
< buf
->size
) {
258 buf
->ptr
[buf
->size
] = '\0';
262 void git_buf_rtruncate_at_char(git_buf
*buf
, char separator
)
264 ssize_t idx
= git_buf_rfind_next(buf
, separator
);
265 git_buf_truncate(buf
, idx
< 0 ? 0 : (size_t)idx
);
268 void git_buf_swap(git_buf
*buf_a
, git_buf
*buf_b
)
275 char *git_buf_detach(git_buf
*buf
)
277 char *data
= buf
->ptr
;
279 if (buf
->asize
== 0 || buf
->ptr
== git_buf__oom
)
282 git_buf_init(buf
, 0);
287 void git_buf_attach(git_buf
*buf
, char *ptr
, size_t asize
)
293 buf
->size
= strlen(ptr
);
295 buf
->asize
= (asize
< buf
->size
) ? buf
->size
+ 1 : asize
;
296 else /* pass 0 to fall back on strlen + 1 */
297 buf
->asize
= buf
->size
+ 1;
299 git_buf_grow(buf
, asize
);
303 int git_buf_join_n(git_buf
*buf
, char separator
, int nbuf
, ...)
307 size_t total_size
= 0, original_size
= buf
->size
;
308 char *out
, *original
= buf
->ptr
;
310 if (buf
->size
> 0 && buf
->ptr
[buf
->size
- 1] != separator
)
311 ++total_size
; /* space for initial separator */
313 /* Make two passes to avoid multiple reallocation */
316 for (i
= 0; i
< nbuf
; ++i
) {
320 segment
= va_arg(ap
, const char *);
324 segment_len
= strlen(segment
);
325 total_size
+= segment_len
;
326 if (segment_len
== 0 || segment
[segment_len
- 1] != separator
)
327 ++total_size
; /* space for separator */
331 /* expand buffer if needed */
334 if (git_buf_grow(buf
, buf
->size
+ total_size
+ 1) < 0)
337 out
= buf
->ptr
+ buf
->size
;
339 /* append separator to existing buf if needed */
340 if (buf
->size
> 0 && out
[-1] != separator
)
344 for (i
= 0; i
< nbuf
; ++i
) {
348 segment
= va_arg(ap
, const char *);
352 /* deal with join that references buffer's original content */
353 if (segment
>= original
&& segment
< original
+ original_size
) {
354 size_t offset
= (segment
- original
);
355 segment
= buf
->ptr
+ offset
;
356 segment_len
= original_size
- offset
;
358 segment_len
= strlen(segment
);
361 /* skip leading separators */
362 if (out
> buf
->ptr
&& out
[-1] == separator
)
363 while (segment_len
> 0 && *segment
== separator
) {
368 /* copy over next buffer */
369 if (segment_len
> 0) {
370 memmove(out
, segment
, segment_len
);
374 /* append trailing separator (except for last item) */
375 if (i
< nbuf
- 1 && out
> buf
->ptr
&& out
[-1] != separator
)
380 /* set size based on num characters actually written */
381 buf
->size
= out
- buf
->ptr
;
382 buf
->ptr
[buf
->size
] = '\0';
393 size_t strlen_a
= str_a
? strlen(str_a
) : 0;
394 size_t strlen_b
= strlen(str_b
);
396 ssize_t offset_a
= -1;
398 /* not safe to have str_b point internally to the buffer */
399 assert(str_b
< buf
->ptr
|| str_b
> buf
->ptr
+ buf
->size
);
401 /* figure out if we need to insert a separator */
402 if (separator
&& strlen_a
) {
403 while (*str_b
== separator
) { str_b
++; strlen_b
--; }
404 if (str_a
[strlen_a
- 1] != separator
)
408 /* str_a could be part of the buffer */
409 if (str_a
>= buf
->ptr
&& str_a
< buf
->ptr
+ buf
->size
)
410 offset_a
= str_a
- buf
->ptr
;
412 if (git_buf_grow(buf
, strlen_a
+ strlen_b
+ need_sep
+ 1) < 0)
415 /* fix up internal pointers */
417 str_a
= buf
->ptr
+ offset_a
;
419 /* do the actual copying */
421 memmove(buf
->ptr
, str_a
, strlen_a
);
423 buf
->ptr
[strlen_a
] = separator
;
424 memcpy(buf
->ptr
+ strlen_a
+ need_sep
, str_b
, strlen_b
);
426 buf
->size
= strlen_a
+ strlen_b
+ need_sep
;
427 buf
->ptr
[buf
->size
] = '\0';
432 void git_buf_rtrim(git_buf
*buf
)
434 while (buf
->size
> 0) {
435 if (!git__isspace(buf
->ptr
[buf
->size
- 1]))
441 buf
->ptr
[buf
->size
] = '\0';
444 int git_buf_cmp(const git_buf
*a
, const git_buf
*b
)
446 int result
= memcmp(a
->ptr
, b
->ptr
, min(a
->size
, b
->size
));
447 return (result
!= 0) ? result
:
448 (a
->size
< b
->size
) ? -1 : (a
->size
> b
->size
) ? 1 : 0;
459 where
<= git_buf_len(buf
) &&
460 where
+ nb_to_remove
<= git_buf_len(buf
));
462 /* Ported from git.git
463 * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176
465 if (git_buf_grow(buf
, git_buf_len(buf
) + nb_to_insert
- nb_to_remove
) < 0)
468 memmove(buf
->ptr
+ where
+ nb_to_insert
,
469 buf
->ptr
+ where
+ nb_to_remove
,
470 buf
->size
- where
- nb_to_remove
);
472 memcpy(buf
->ptr
+ where
, data
, nb_to_insert
);
474 buf
->size
= buf
->size
+ nb_to_insert
- nb_to_remove
;
475 buf
->ptr
[buf
->size
] = '\0';