]>
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"
13 /* Used as default value for git_buf->ptr so that people can always
14 * assume ptr is non-NULL and zero terminated even for new git_bufs.
16 char git_buf__initbuf
[1];
20 #define ENSURE_SIZE(b, d) \
21 if ((d) > buf->asize && git_buf_grow(b, (d)) < 0)\
25 void git_buf_init(git_buf
*buf
, size_t initial_size
)
29 buf
->ptr
= git_buf__initbuf
;
32 git_buf_grow(buf
, initial_size
);
36 git_buf
*buf
, size_t target_size
, bool mark_oom
, bool preserve_external
)
41 if (buf
->ptr
== git_buf__oom
)
45 target_size
= buf
->size
;
47 if (target_size
<= buf
->asize
)
50 if (buf
->asize
== 0) {
51 new_size
= target_size
;
54 new_size
= buf
->asize
;
58 /* grow the buffer size by 1.5, until it's big enough
59 * to fit our target size */
60 while (new_size
< target_size
)
61 new_size
= (new_size
<< 1) - (new_size
>> 1);
63 /* round allocation up to multiple of 8 */
64 new_size
= (new_size
+ 7) & ~7;
66 new_ptr
= git__realloc(new_ptr
, new_size
);
70 buf
->ptr
= git_buf__oom
;
74 if (preserve_external
&& !buf
->asize
&& buf
->ptr
!= NULL
&& buf
->size
> 0)
75 memcpy(new_ptr
, buf
->ptr
, min(buf
->size
, new_size
));
77 buf
->asize
= new_size
;
80 /* truncate the existing buffer size if necessary */
81 if (buf
->size
>= buf
->asize
)
82 buf
->size
= buf
->asize
- 1;
83 buf
->ptr
[buf
->size
] = '\0';
88 int git_buf_grow(git_buf
*buffer
, size_t target_size
)
90 return git_buf_try_grow(buffer
, target_size
, true, true);
93 void git_buf_free(git_buf
*buf
)
97 if (buf
->asize
> 0 && buf
->ptr
!= NULL
&& buf
->ptr
!= git_buf__oom
)
100 git_buf_init(buf
, 0);
103 void git_buf_clear(git_buf
*buf
)
108 buf
->ptr
= git_buf__initbuf
;
114 int git_buf_set(git_buf
*buf
, const void *data
, size_t len
)
116 if (len
== 0 || data
== NULL
) {
119 if (data
!= buf
->ptr
) {
120 ENSURE_SIZE(buf
, len
+ 1);
121 memmove(buf
->ptr
, data
, len
);
124 buf
->ptr
[buf
->size
] = '\0';
129 int git_buf_sets(git_buf
*buf
, const char *string
)
131 return git_buf_set(buf
, string
, string
? strlen(string
) : 0);
134 int git_buf_putc(git_buf
*buf
, char c
)
136 ENSURE_SIZE(buf
, buf
->size
+ 2);
137 buf
->ptr
[buf
->size
++] = c
;
138 buf
->ptr
[buf
->size
] = '\0';
142 int git_buf_put(git_buf
*buf
, const char *data
, size_t len
)
144 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
145 memmove(buf
->ptr
+ buf
->size
, data
, len
);
147 buf
->ptr
[buf
->size
] = '\0';
151 int git_buf_puts(git_buf
*buf
, const char *string
)
154 return git_buf_put(buf
, string
, strlen(string
));
157 static const char b64str
[] =
158 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
160 int git_buf_put_base64(git_buf
*buf
, const char *data
, size_t len
)
162 size_t extra
= len
% 3;
163 uint8_t *write
, a
, b
, c
;
164 const uint8_t *read
= (const uint8_t *)data
;
166 ENSURE_SIZE(buf
, buf
->size
+ 4 * ((len
/ 3) + !!extra
) + 1);
167 write
= (uint8_t *)&buf
->ptr
[buf
->size
];
169 /* convert each run of 3 bytes into 4 output bytes */
170 for (len
-= extra
; len
> 0; len
-= 3) {
175 *write
++ = b64str
[a
>> 2];
176 *write
++ = b64str
[(a
& 0x03) << 4 | b
>> 4];
177 *write
++ = b64str
[(b
& 0x0f) << 2 | c
>> 6];
178 *write
++ = b64str
[c
& 0x3f];
183 b
= (extra
> 1) ? *read
++ : 0;
185 *write
++ = b64str
[a
>> 2];
186 *write
++ = b64str
[(a
& 0x03) << 4 | b
>> 4];
187 *write
++ = (extra
> 1) ? b64str
[(b
& 0x0f) << 2] : '=';
191 buf
->size
= ((char *)write
) - buf
->ptr
;
192 buf
->ptr
[buf
->size
] = '\0';
197 int git_buf_vprintf(git_buf
*buf
, const char *format
, va_list ap
)
200 const size_t expected_size
= buf
->size
+ (strlen(format
) * 2);
202 ENSURE_SIZE(buf
, expected_size
);
209 buf
->ptr
+ buf
->size
,
210 buf
->asize
- buf
->size
,
218 buf
->ptr
= git_buf__oom
;
222 if ((size_t)len
+ 1 <= buf
->asize
- buf
->size
) {
227 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
233 int git_buf_printf(git_buf
*buf
, const char *format
, ...)
238 va_start(ap
, format
);
239 r
= git_buf_vprintf(buf
, format
, ap
);
245 void git_buf_copy_cstr(char *data
, size_t datasize
, const git_buf
*buf
)
249 assert(data
&& datasize
&& buf
);
253 if (buf
->size
== 0 || buf
->asize
<= 0)
257 if (copylen
> datasize
- 1)
258 copylen
= datasize
- 1;
259 memmove(data
, buf
->ptr
, copylen
);
260 data
[copylen
] = '\0';
263 void git_buf_consume(git_buf
*buf
, const char *end
)
265 if (end
> buf
->ptr
&& end
<= buf
->ptr
+ buf
->size
) {
266 size_t consumed
= end
- buf
->ptr
;
267 memmove(buf
->ptr
, end
, buf
->size
- consumed
);
268 buf
->size
-= consumed
;
269 buf
->ptr
[buf
->size
] = '\0';
273 void git_buf_truncate(git_buf
*buf
, size_t len
)
275 if (len
< buf
->size
) {
277 buf
->ptr
[buf
->size
] = '\0';
281 void git_buf_shorten(git_buf
*buf
, size_t amount
)
283 if (amount
> buf
->size
)
286 buf
->size
= buf
->size
- amount
;
287 buf
->ptr
[buf
->size
] = '\0';
290 void git_buf_rtruncate_at_char(git_buf
*buf
, char separator
)
292 ssize_t idx
= git_buf_rfind_next(buf
, separator
);
293 git_buf_truncate(buf
, idx
< 0 ? 0 : (size_t)idx
);
296 void git_buf_swap(git_buf
*buf_a
, git_buf
*buf_b
)
303 char *git_buf_detach(git_buf
*buf
)
305 char *data
= buf
->ptr
;
307 if (buf
->asize
== 0 || buf
->ptr
== git_buf__oom
)
310 git_buf_init(buf
, 0);
315 void git_buf_attach(git_buf
*buf
, char *ptr
, size_t asize
)
321 buf
->size
= strlen(ptr
);
323 buf
->asize
= (asize
< buf
->size
) ? buf
->size
+ 1 : asize
;
324 else /* pass 0 to fall back on strlen + 1 */
325 buf
->asize
= buf
->size
+ 1;
327 git_buf_grow(buf
, asize
);
331 int git_buf_join_n(git_buf
*buf
, char separator
, int nbuf
, ...)
335 size_t total_size
= 0, original_size
= buf
->size
;
336 char *out
, *original
= buf
->ptr
;
338 if (buf
->size
> 0 && buf
->ptr
[buf
->size
- 1] != separator
)
339 ++total_size
; /* space for initial separator */
341 /* Make two passes to avoid multiple reallocation */
344 for (i
= 0; i
< nbuf
; ++i
) {
348 segment
= va_arg(ap
, const char *);
352 segment_len
= strlen(segment
);
353 total_size
+= segment_len
;
354 if (segment_len
== 0 || segment
[segment_len
- 1] != separator
)
355 ++total_size
; /* space for separator */
359 /* expand buffer if needed */
362 if (git_buf_grow(buf
, buf
->size
+ total_size
+ 1) < 0)
365 out
= buf
->ptr
+ buf
->size
;
367 /* append separator to existing buf if needed */
368 if (buf
->size
> 0 && out
[-1] != separator
)
372 for (i
= 0; i
< nbuf
; ++i
) {
376 segment
= va_arg(ap
, const char *);
380 /* deal with join that references buffer's original content */
381 if (segment
>= original
&& segment
< original
+ original_size
) {
382 size_t offset
= (segment
- original
);
383 segment
= buf
->ptr
+ offset
;
384 segment_len
= original_size
- offset
;
386 segment_len
= strlen(segment
);
389 /* skip leading separators */
390 if (out
> buf
->ptr
&& out
[-1] == separator
)
391 while (segment_len
> 0 && *segment
== separator
) {
396 /* copy over next buffer */
397 if (segment_len
> 0) {
398 memmove(out
, segment
, segment_len
);
402 /* append trailing separator (except for last item) */
403 if (i
< nbuf
- 1 && out
> buf
->ptr
&& out
[-1] != separator
)
408 /* set size based on num characters actually written */
409 buf
->size
= out
- buf
->ptr
;
410 buf
->ptr
[buf
->size
] = '\0';
421 size_t strlen_a
= str_a
? strlen(str_a
) : 0;
422 size_t strlen_b
= strlen(str_b
);
424 ssize_t offset_a
= -1;
426 /* not safe to have str_b point internally to the buffer */
427 assert(str_b
< buf
->ptr
|| str_b
> buf
->ptr
+ buf
->size
);
429 /* figure out if we need to insert a separator */
430 if (separator
&& strlen_a
) {
431 while (*str_b
== separator
) { str_b
++; strlen_b
--; }
432 if (str_a
[strlen_a
- 1] != separator
)
436 /* str_a could be part of the buffer */
437 if (str_a
>= buf
->ptr
&& str_a
< buf
->ptr
+ buf
->size
)
438 offset_a
= str_a
- buf
->ptr
;
440 if (git_buf_grow(buf
, strlen_a
+ strlen_b
+ need_sep
+ 1) < 0)
443 /* fix up internal pointers */
445 str_a
= buf
->ptr
+ offset_a
;
447 /* do the actual copying */
449 memmove(buf
->ptr
, str_a
, strlen_a
);
451 buf
->ptr
[strlen_a
] = separator
;
452 memcpy(buf
->ptr
+ strlen_a
+ need_sep
, str_b
, strlen_b
);
454 buf
->size
= strlen_a
+ strlen_b
+ need_sep
;
455 buf
->ptr
[buf
->size
] = '\0';
460 void git_buf_rtrim(git_buf
*buf
)
462 while (buf
->size
> 0) {
463 if (!git__isspace(buf
->ptr
[buf
->size
- 1]))
469 buf
->ptr
[buf
->size
] = '\0';
472 int git_buf_cmp(const git_buf
*a
, const git_buf
*b
)
474 int result
= memcmp(a
->ptr
, b
->ptr
, min(a
->size
, b
->size
));
475 return (result
!= 0) ? result
:
476 (a
->size
< b
->size
) ? -1 : (a
->size
> b
->size
) ? 1 : 0;
487 where
<= git_buf_len(buf
) &&
488 where
+ nb_to_remove
<= git_buf_len(buf
));
490 /* Ported from git.git
491 * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176
493 if (git_buf_grow(buf
, git_buf_len(buf
) + nb_to_insert
- nb_to_remove
) < 0)
496 memmove(buf
->ptr
+ where
+ nb_to_insert
,
497 buf
->ptr
+ where
+ nb_to_remove
,
498 buf
->size
- where
- nb_to_remove
);
500 memcpy(buf
->ptr
+ where
, data
, nb_to_insert
);
502 buf
->size
= buf
->size
+ nb_to_insert
- nb_to_remove
;
503 buf
->ptr
[buf
->size
] = '\0';