]>
git.proxmox.com Git - libgit2.git/blob - src/buffer.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
9 #include "git2/buffer.h"
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
);
35 git_buf
*buf
, size_t target_size
, bool mark_oom
, bool preserve_external
)
40 if (buf
->ptr
== git_buf__oom
)
44 target_size
= buf
->size
;
46 if (target_size
<= buf
->asize
)
49 if (buf
->asize
== 0) {
50 new_size
= target_size
;
53 new_size
= buf
->asize
;
57 /* grow the buffer size by 1.5, until it's big enough
58 * to fit our target size */
59 while (new_size
< target_size
)
60 new_size
= (new_size
<< 1) - (new_size
>> 1);
62 /* round allocation up to multiple of 8 */
63 new_size
= (new_size
+ 7) & ~7;
65 new_ptr
= git__realloc(new_ptr
, new_size
);
69 if (buf
->ptr
) git__free(buf
->ptr
);
70 buf
->ptr
= git_buf__oom
;
75 if (preserve_external
&& !buf
->asize
&& buf
->ptr
!= NULL
&& buf
->size
> 0)
76 memcpy(new_ptr
, buf
->ptr
, min(buf
->size
, new_size
));
78 buf
->asize
= new_size
;
81 /* truncate the existing buffer size if necessary */
82 if (buf
->size
>= buf
->asize
)
83 buf
->size
= buf
->asize
- 1;
84 buf
->ptr
[buf
->size
] = '\0';
89 int git_buf_grow(git_buf
*buffer
, size_t target_size
)
91 return git_buf_try_grow(buffer
, target_size
, true, true);
94 void git_buf_free(git_buf
*buf
)
98 if (buf
->asize
> 0 && buf
->ptr
!= NULL
&& buf
->ptr
!= git_buf__oom
)
101 git_buf_init(buf
, 0);
104 void git_buf_sanitize(git_buf
*buf
)
106 if (buf
->ptr
== NULL
) {
107 assert (buf
->size
== 0 && buf
->asize
== 0);
108 buf
->ptr
= git_buf__initbuf
;
112 void git_buf_clear(git_buf
*buf
)
117 buf
->ptr
= git_buf__initbuf
;
123 int git_buf_set(git_buf
*buf
, const void *data
, size_t len
)
125 if (len
== 0 || data
== NULL
) {
128 if (data
!= buf
->ptr
) {
129 ENSURE_SIZE(buf
, len
+ 1);
130 memmove(buf
->ptr
, data
, len
);
133 buf
->ptr
[buf
->size
] = '\0';
138 int git_buf_sets(git_buf
*buf
, const char *string
)
140 return git_buf_set(buf
, string
, string
? strlen(string
) : 0);
143 int git_buf_putc(git_buf
*buf
, char c
)
145 ENSURE_SIZE(buf
, buf
->size
+ 2);
146 buf
->ptr
[buf
->size
++] = c
;
147 buf
->ptr
[buf
->size
] = '\0';
151 int git_buf_putcn(git_buf
*buf
, char c
, size_t len
)
153 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
154 memset(buf
->ptr
+ buf
->size
, c
, len
);
156 buf
->ptr
[buf
->size
] = '\0';
160 int git_buf_put(git_buf
*buf
, const char *data
, size_t len
)
162 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
163 memmove(buf
->ptr
+ buf
->size
, data
, len
);
165 buf
->ptr
[buf
->size
] = '\0';
169 int git_buf_puts(git_buf
*buf
, const char *string
)
172 return git_buf_put(buf
, string
, strlen(string
));
175 static const char b64str
[] =
176 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
178 int git_buf_put_base64(git_buf
*buf
, const char *data
, size_t len
)
180 size_t extra
= len
% 3;
181 uint8_t *write
, a
, b
, c
;
182 const uint8_t *read
= (const uint8_t *)data
;
184 ENSURE_SIZE(buf
, buf
->size
+ 4 * ((len
/ 3) + !!extra
) + 1);
185 write
= (uint8_t *)&buf
->ptr
[buf
->size
];
187 /* convert each run of 3 bytes into 4 output bytes */
188 for (len
-= extra
; len
> 0; len
-= 3) {
193 *write
++ = b64str
[a
>> 2];
194 *write
++ = b64str
[(a
& 0x03) << 4 | b
>> 4];
195 *write
++ = b64str
[(b
& 0x0f) << 2 | c
>> 6];
196 *write
++ = b64str
[c
& 0x3f];
201 b
= (extra
> 1) ? *read
++ : 0;
203 *write
++ = b64str
[a
>> 2];
204 *write
++ = b64str
[(a
& 0x03) << 4 | b
>> 4];
205 *write
++ = (extra
> 1) ? b64str
[(b
& 0x0f) << 2] : '=';
209 buf
->size
= ((char *)write
) - buf
->ptr
;
210 buf
->ptr
[buf
->size
] = '\0';
215 int git_buf_vprintf(git_buf
*buf
, const char *format
, va_list ap
)
218 const size_t expected_size
= buf
->size
+ (strlen(format
) * 2);
220 ENSURE_SIZE(buf
, expected_size
);
227 buf
->ptr
+ buf
->size
,
228 buf
->asize
- buf
->size
,
236 buf
->ptr
= git_buf__oom
;
240 if ((size_t)len
+ 1 <= buf
->asize
- buf
->size
) {
245 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
251 int git_buf_printf(git_buf
*buf
, const char *format
, ...)
256 va_start(ap
, format
);
257 r
= git_buf_vprintf(buf
, format
, ap
);
263 void git_buf_copy_cstr(char *data
, size_t datasize
, const git_buf
*buf
)
267 assert(data
&& datasize
&& buf
);
271 if (buf
->size
== 0 || buf
->asize
<= 0)
275 if (copylen
> datasize
- 1)
276 copylen
= datasize
- 1;
277 memmove(data
, buf
->ptr
, copylen
);
278 data
[copylen
] = '\0';
281 void git_buf_consume(git_buf
*buf
, const char *end
)
283 if (end
> buf
->ptr
&& end
<= buf
->ptr
+ buf
->size
) {
284 size_t consumed
= end
- buf
->ptr
;
285 memmove(buf
->ptr
, end
, buf
->size
- consumed
);
286 buf
->size
-= consumed
;
287 buf
->ptr
[buf
->size
] = '\0';
291 void git_buf_truncate(git_buf
*buf
, size_t len
)
293 if (len
< buf
->size
) {
295 buf
->ptr
[buf
->size
] = '\0';
299 void git_buf_shorten(git_buf
*buf
, size_t amount
)
301 if (amount
> buf
->size
)
304 buf
->size
= buf
->size
- amount
;
305 buf
->ptr
[buf
->size
] = '\0';
308 void git_buf_rtruncate_at_char(git_buf
*buf
, char separator
)
310 ssize_t idx
= git_buf_rfind_next(buf
, separator
);
311 git_buf_truncate(buf
, idx
< 0 ? 0 : (size_t)idx
);
314 void git_buf_swap(git_buf
*buf_a
, git_buf
*buf_b
)
321 char *git_buf_detach(git_buf
*buf
)
323 char *data
= buf
->ptr
;
325 if (buf
->asize
== 0 || buf
->ptr
== git_buf__oom
)
328 git_buf_init(buf
, 0);
333 void git_buf_attach(git_buf
*buf
, char *ptr
, size_t asize
)
339 buf
->size
= strlen(ptr
);
341 buf
->asize
= (asize
< buf
->size
) ? buf
->size
+ 1 : asize
;
342 else /* pass 0 to fall back on strlen + 1 */
343 buf
->asize
= buf
->size
+ 1;
345 git_buf_grow(buf
, asize
);
349 int git_buf_join_n(git_buf
*buf
, char separator
, int nbuf
, ...)
353 size_t total_size
= 0, original_size
= buf
->size
;
354 char *out
, *original
= buf
->ptr
;
356 if (buf
->size
> 0 && buf
->ptr
[buf
->size
- 1] != separator
)
357 ++total_size
; /* space for initial separator */
359 /* Make two passes to avoid multiple reallocation */
362 for (i
= 0; i
< nbuf
; ++i
) {
366 segment
= va_arg(ap
, const char *);
370 segment_len
= strlen(segment
);
371 total_size
+= segment_len
;
372 if (segment_len
== 0 || segment
[segment_len
- 1] != separator
)
373 ++total_size
; /* space for separator */
377 /* expand buffer if needed */
380 if (git_buf_grow(buf
, buf
->size
+ total_size
+ 1) < 0)
383 out
= buf
->ptr
+ buf
->size
;
385 /* append separator to existing buf if needed */
386 if (buf
->size
> 0 && out
[-1] != separator
)
390 for (i
= 0; i
< nbuf
; ++i
) {
394 segment
= va_arg(ap
, const char *);
398 /* deal with join that references buffer's original content */
399 if (segment
>= original
&& segment
< original
+ original_size
) {
400 size_t offset
= (segment
- original
);
401 segment
= buf
->ptr
+ offset
;
402 segment_len
= original_size
- offset
;
404 segment_len
= strlen(segment
);
407 /* skip leading separators */
408 if (out
> buf
->ptr
&& out
[-1] == separator
)
409 while (segment_len
> 0 && *segment
== separator
) {
414 /* copy over next buffer */
415 if (segment_len
> 0) {
416 memmove(out
, segment
, segment_len
);
420 /* append trailing separator (except for last item) */
421 if (i
< nbuf
- 1 && out
> buf
->ptr
&& out
[-1] != separator
)
426 /* set size based on num characters actually written */
427 buf
->size
= out
- buf
->ptr
;
428 buf
->ptr
[buf
->size
] = '\0';
439 size_t strlen_a
= str_a
? strlen(str_a
) : 0;
440 size_t strlen_b
= strlen(str_b
);
442 ssize_t offset_a
= -1;
444 /* not safe to have str_b point internally to the buffer */
445 assert(str_b
< buf
->ptr
|| str_b
>= buf
->ptr
+ buf
->size
);
447 /* figure out if we need to insert a separator */
448 if (separator
&& strlen_a
) {
449 while (*str_b
== separator
) { str_b
++; strlen_b
--; }
450 if (str_a
[strlen_a
- 1] != separator
)
454 /* str_a could be part of the buffer */
455 if (str_a
>= buf
->ptr
&& str_a
< buf
->ptr
+ buf
->size
)
456 offset_a
= str_a
- buf
->ptr
;
458 if (git_buf_grow(buf
, strlen_a
+ strlen_b
+ need_sep
+ 1) < 0)
462 /* fix up internal pointers */
464 str_a
= buf
->ptr
+ offset_a
;
466 /* do the actual copying */
467 if (offset_a
!= 0 && str_a
)
468 memmove(buf
->ptr
, str_a
, strlen_a
);
470 buf
->ptr
[strlen_a
] = separator
;
471 memcpy(buf
->ptr
+ strlen_a
+ need_sep
, str_b
, strlen_b
);
473 buf
->size
= strlen_a
+ strlen_b
+ need_sep
;
474 buf
->ptr
[buf
->size
] = '\0';
486 size_t len_a
= strlen(str_a
), len_b
= strlen(str_b
), len_c
= strlen(str_c
);
487 int sep_a
= 0, sep_b
= 0;
490 /* for this function, disallow pointers into the existing buffer */
491 assert(str_a
< buf
->ptr
|| str_a
>= buf
->ptr
+ buf
->size
);
492 assert(str_b
< buf
->ptr
|| str_b
>= buf
->ptr
+ buf
->size
);
493 assert(str_c
< buf
->ptr
|| str_c
>= buf
->ptr
+ buf
->size
);
497 while (*str_b
== separator
) { str_b
++; len_b
--; }
498 sep_a
= (str_a
[len_a
- 1] != separator
);
500 if (len_a
> 0 || len_b
> 0)
501 while (*str_c
== separator
) { str_c
++; len_c
--; }
503 sep_b
= (str_b
[len_b
- 1] != separator
);
506 if (git_buf_grow(buf
, len_a
+ sep_a
+ len_b
+ sep_b
+ len_c
+ 1) < 0)
512 memcpy(tgt
, str_a
, len_a
);
518 memcpy(tgt
, str_b
, len_b
);
524 memcpy(tgt
, str_c
, len_c
);
526 buf
->size
= len_a
+ sep_a
+ len_b
+ sep_b
+ len_c
;
527 buf
->ptr
[buf
->size
] = '\0';
532 void git_buf_rtrim(git_buf
*buf
)
534 while (buf
->size
> 0) {
535 if (!git__isspace(buf
->ptr
[buf
->size
- 1]))
541 buf
->ptr
[buf
->size
] = '\0';
544 int git_buf_cmp(const git_buf
*a
, const git_buf
*b
)
546 int result
= memcmp(a
->ptr
, b
->ptr
, min(a
->size
, b
->size
));
547 return (result
!= 0) ? result
:
548 (a
->size
< b
->size
) ? -1 : (a
->size
> b
->size
) ? 1 : 0;
559 where
<= git_buf_len(buf
) &&
560 where
+ nb_to_remove
<= git_buf_len(buf
));
562 /* Ported from git.git
563 * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176
565 if (git_buf_grow(buf
, git_buf_len(buf
) + nb_to_insert
- nb_to_remove
) < 0)
568 memmove(buf
->ptr
+ where
+ nb_to_insert
,
569 buf
->ptr
+ where
+ nb_to_remove
,
570 buf
->size
- where
- nb_to_remove
);
572 memcpy(buf
->ptr
+ where
, data
, nb_to_insert
);
574 buf
->size
= buf
->size
+ nb_to_insert
- nb_to_remove
;
575 buf
->ptr
[buf
->size
] = '\0';