]>
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_put(git_buf
*buf
, const char *data
, size_t len
)
153 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
154 memmove(buf
->ptr
+ buf
->size
, data
, len
);
156 buf
->ptr
[buf
->size
] = '\0';
160 int git_buf_puts(git_buf
*buf
, const char *string
)
163 return git_buf_put(buf
, string
, strlen(string
));
166 static const char b64str
[] =
167 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
169 int git_buf_put_base64(git_buf
*buf
, const char *data
, size_t len
)
171 size_t extra
= len
% 3;
172 uint8_t *write
, a
, b
, c
;
173 const uint8_t *read
= (const uint8_t *)data
;
175 ENSURE_SIZE(buf
, buf
->size
+ 4 * ((len
/ 3) + !!extra
) + 1);
176 write
= (uint8_t *)&buf
->ptr
[buf
->size
];
178 /* convert each run of 3 bytes into 4 output bytes */
179 for (len
-= extra
; len
> 0; len
-= 3) {
184 *write
++ = b64str
[a
>> 2];
185 *write
++ = b64str
[(a
& 0x03) << 4 | b
>> 4];
186 *write
++ = b64str
[(b
& 0x0f) << 2 | c
>> 6];
187 *write
++ = b64str
[c
& 0x3f];
192 b
= (extra
> 1) ? *read
++ : 0;
194 *write
++ = b64str
[a
>> 2];
195 *write
++ = b64str
[(a
& 0x03) << 4 | b
>> 4];
196 *write
++ = (extra
> 1) ? b64str
[(b
& 0x0f) << 2] : '=';
200 buf
->size
= ((char *)write
) - buf
->ptr
;
201 buf
->ptr
[buf
->size
] = '\0';
206 int git_buf_vprintf(git_buf
*buf
, const char *format
, va_list ap
)
209 const size_t expected_size
= buf
->size
+ (strlen(format
) * 2);
211 ENSURE_SIZE(buf
, expected_size
);
218 buf
->ptr
+ buf
->size
,
219 buf
->asize
- buf
->size
,
227 buf
->ptr
= git_buf__oom
;
231 if ((size_t)len
+ 1 <= buf
->asize
- buf
->size
) {
236 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
242 int git_buf_printf(git_buf
*buf
, const char *format
, ...)
247 va_start(ap
, format
);
248 r
= git_buf_vprintf(buf
, format
, ap
);
254 void git_buf_copy_cstr(char *data
, size_t datasize
, const git_buf
*buf
)
258 assert(data
&& datasize
&& buf
);
262 if (buf
->size
== 0 || buf
->asize
<= 0)
266 if (copylen
> datasize
- 1)
267 copylen
= datasize
- 1;
268 memmove(data
, buf
->ptr
, copylen
);
269 data
[copylen
] = '\0';
272 void git_buf_consume(git_buf
*buf
, const char *end
)
274 if (end
> buf
->ptr
&& end
<= buf
->ptr
+ buf
->size
) {
275 size_t consumed
= end
- buf
->ptr
;
276 memmove(buf
->ptr
, end
, buf
->size
- consumed
);
277 buf
->size
-= consumed
;
278 buf
->ptr
[buf
->size
] = '\0';
282 void git_buf_truncate(git_buf
*buf
, size_t len
)
284 if (len
< buf
->size
) {
286 buf
->ptr
[buf
->size
] = '\0';
290 void git_buf_shorten(git_buf
*buf
, size_t amount
)
292 if (amount
> buf
->size
)
295 buf
->size
= buf
->size
- amount
;
296 buf
->ptr
[buf
->size
] = '\0';
299 void git_buf_rtruncate_at_char(git_buf
*buf
, char separator
)
301 ssize_t idx
= git_buf_rfind_next(buf
, separator
);
302 git_buf_truncate(buf
, idx
< 0 ? 0 : (size_t)idx
);
305 void git_buf_swap(git_buf
*buf_a
, git_buf
*buf_b
)
312 char *git_buf_detach(git_buf
*buf
)
314 char *data
= buf
->ptr
;
316 if (buf
->asize
== 0 || buf
->ptr
== git_buf__oom
)
319 git_buf_init(buf
, 0);
324 void git_buf_attach(git_buf
*buf
, char *ptr
, size_t asize
)
330 buf
->size
= strlen(ptr
);
332 buf
->asize
= (asize
< buf
->size
) ? buf
->size
+ 1 : asize
;
333 else /* pass 0 to fall back on strlen + 1 */
334 buf
->asize
= buf
->size
+ 1;
336 git_buf_grow(buf
, asize
);
340 int git_buf_join_n(git_buf
*buf
, char separator
, int nbuf
, ...)
344 size_t total_size
= 0, original_size
= buf
->size
;
345 char *out
, *original
= buf
->ptr
;
347 if (buf
->size
> 0 && buf
->ptr
[buf
->size
- 1] != separator
)
348 ++total_size
; /* space for initial separator */
350 /* Make two passes to avoid multiple reallocation */
353 for (i
= 0; i
< nbuf
; ++i
) {
357 segment
= va_arg(ap
, const char *);
361 segment_len
= strlen(segment
);
362 total_size
+= segment_len
;
363 if (segment_len
== 0 || segment
[segment_len
- 1] != separator
)
364 ++total_size
; /* space for separator */
368 /* expand buffer if needed */
371 if (git_buf_grow(buf
, buf
->size
+ total_size
+ 1) < 0)
374 out
= buf
->ptr
+ buf
->size
;
376 /* append separator to existing buf if needed */
377 if (buf
->size
> 0 && out
[-1] != separator
)
381 for (i
= 0; i
< nbuf
; ++i
) {
385 segment
= va_arg(ap
, const char *);
389 /* deal with join that references buffer's original content */
390 if (segment
>= original
&& segment
< original
+ original_size
) {
391 size_t offset
= (segment
- original
);
392 segment
= buf
->ptr
+ offset
;
393 segment_len
= original_size
- offset
;
395 segment_len
= strlen(segment
);
398 /* skip leading separators */
399 if (out
> buf
->ptr
&& out
[-1] == separator
)
400 while (segment_len
> 0 && *segment
== separator
) {
405 /* copy over next buffer */
406 if (segment_len
> 0) {
407 memmove(out
, segment
, segment_len
);
411 /* append trailing separator (except for last item) */
412 if (i
< nbuf
- 1 && out
> buf
->ptr
&& out
[-1] != separator
)
417 /* set size based on num characters actually written */
418 buf
->size
= out
- buf
->ptr
;
419 buf
->ptr
[buf
->size
] = '\0';
430 size_t strlen_a
= str_a
? strlen(str_a
) : 0;
431 size_t strlen_b
= strlen(str_b
);
433 ssize_t offset_a
= -1;
435 /* not safe to have str_b point internally to the buffer */
436 assert(str_b
< buf
->ptr
|| str_b
>= buf
->ptr
+ buf
->size
);
438 /* figure out if we need to insert a separator */
439 if (separator
&& strlen_a
) {
440 while (*str_b
== separator
) { str_b
++; strlen_b
--; }
441 if (str_a
[strlen_a
- 1] != separator
)
445 /* str_a could be part of the buffer */
446 if (str_a
>= buf
->ptr
&& str_a
< buf
->ptr
+ buf
->size
)
447 offset_a
= str_a
- buf
->ptr
;
449 if (git_buf_grow(buf
, strlen_a
+ strlen_b
+ need_sep
+ 1) < 0)
453 /* fix up internal pointers */
455 str_a
= buf
->ptr
+ offset_a
;
457 /* do the actual copying */
458 if (offset_a
!= 0 && str_a
)
459 memmove(buf
->ptr
, str_a
, strlen_a
);
461 buf
->ptr
[strlen_a
] = separator
;
462 memcpy(buf
->ptr
+ strlen_a
+ need_sep
, str_b
, strlen_b
);
464 buf
->size
= strlen_a
+ strlen_b
+ need_sep
;
465 buf
->ptr
[buf
->size
] = '\0';
477 size_t len_a
= strlen(str_a
), len_b
= strlen(str_b
), len_c
= strlen(str_c
);
478 int sep_a
= 0, sep_b
= 0;
481 /* for this function, disallow pointers into the existing buffer */
482 assert(str_a
< buf
->ptr
|| str_a
>= buf
->ptr
+ buf
->size
);
483 assert(str_b
< buf
->ptr
|| str_b
>= buf
->ptr
+ buf
->size
);
484 assert(str_c
< buf
->ptr
|| str_c
>= buf
->ptr
+ buf
->size
);
488 while (*str_b
== separator
) { str_b
++; len_b
--; }
489 sep_a
= (str_a
[len_a
- 1] != separator
);
491 if (len_a
> 0 || len_b
> 0)
492 while (*str_c
== separator
) { str_c
++; len_c
--; }
494 sep_b
= (str_b
[len_b
- 1] != separator
);
497 if (git_buf_grow(buf
, len_a
+ sep_a
+ len_b
+ sep_b
+ len_c
+ 1) < 0)
503 memcpy(tgt
, str_a
, len_a
);
509 memcpy(tgt
, str_b
, len_b
);
515 memcpy(tgt
, str_c
, len_c
);
517 buf
->size
= len_a
+ sep_a
+ len_b
+ sep_b
+ len_c
;
518 buf
->ptr
[buf
->size
] = '\0';
523 void git_buf_rtrim(git_buf
*buf
)
525 while (buf
->size
> 0) {
526 if (!git__isspace(buf
->ptr
[buf
->size
- 1]))
532 buf
->ptr
[buf
->size
] = '\0';
535 int git_buf_cmp(const git_buf
*a
, const git_buf
*b
)
537 int result
= memcmp(a
->ptr
, b
->ptr
, min(a
->size
, b
->size
));
538 return (result
!= 0) ? result
:
539 (a
->size
< b
->size
) ? -1 : (a
->size
> b
->size
) ? 1 : 0;
550 where
<= git_buf_len(buf
) &&
551 where
+ nb_to_remove
<= git_buf_len(buf
));
553 /* Ported from git.git
554 * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176
556 if (git_buf_grow(buf
, git_buf_len(buf
) + nb_to_insert
- nb_to_remove
) < 0)
559 memmove(buf
->ptr
+ where
+ nb_to_insert
,
560 buf
->ptr
+ where
+ nb_to_remove
,
561 buf
->size
- where
- nb_to_remove
);
563 memcpy(buf
->ptr
+ where
, data
, nb_to_insert
);
565 buf
->size
= buf
->size
+ nb_to_insert
- nb_to_remove
;
566 buf
->ptr
[buf
->size
] = '\0';