]>
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 if (buf
->ptr
) git__free(buf
->ptr
);
71 buf
->ptr
= git_buf__oom
;
76 if (preserve_external
&& !buf
->asize
&& buf
->ptr
!= NULL
&& buf
->size
> 0)
77 memcpy(new_ptr
, buf
->ptr
, min(buf
->size
, new_size
));
79 buf
->asize
= new_size
;
82 /* truncate the existing buffer size if necessary */
83 if (buf
->size
>= buf
->asize
)
84 buf
->size
= buf
->asize
- 1;
85 buf
->ptr
[buf
->size
] = '\0';
90 int git_buf_grow(git_buf
*buffer
, size_t target_size
)
92 return git_buf_try_grow(buffer
, target_size
, true, true);
95 void git_buf_free(git_buf
*buf
)
99 if (buf
->asize
> 0 && buf
->ptr
!= NULL
&& buf
->ptr
!= git_buf__oom
)
102 git_buf_init(buf
, 0);
105 void git_buf_sanitize(git_buf
*buf
)
107 if (buf
->ptr
== NULL
) {
108 assert(buf
->size
== 0 && buf
->asize
== 0);
109 buf
->ptr
= git_buf__initbuf
;
110 } else if (buf
->asize
> buf
->size
)
111 buf
->ptr
[buf
->size
] = '\0';
114 void git_buf_clear(git_buf
*buf
)
119 buf
->ptr
= git_buf__initbuf
;
127 int git_buf_set(git_buf
*buf
, const void *data
, size_t len
)
129 if (len
== 0 || data
== NULL
) {
132 if (data
!= buf
->ptr
) {
133 ENSURE_SIZE(buf
, len
+ 1);
134 memmove(buf
->ptr
, data
, len
);
138 if (buf
->asize
> buf
->size
)
139 buf
->ptr
[buf
->size
] = '\0';
145 int git_buf_is_binary(const git_buf
*buf
)
147 return git_buf_text_is_binary(buf
);
150 int git_buf_contains_nul(const git_buf
*buf
)
152 return git_buf_text_contains_nul(buf
);
155 int git_buf_sets(git_buf
*buf
, const char *string
)
157 return git_buf_set(buf
, string
, string
? strlen(string
) : 0);
160 int git_buf_putc(git_buf
*buf
, char c
)
162 ENSURE_SIZE(buf
, buf
->size
+ 2);
163 buf
->ptr
[buf
->size
++] = c
;
164 buf
->ptr
[buf
->size
] = '\0';
168 int git_buf_putcn(git_buf
*buf
, char c
, size_t len
)
170 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
171 memset(buf
->ptr
+ buf
->size
, c
, len
);
173 buf
->ptr
[buf
->size
] = '\0';
177 int git_buf_put(git_buf
*buf
, const char *data
, size_t len
)
179 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
180 memmove(buf
->ptr
+ buf
->size
, data
, len
);
182 buf
->ptr
[buf
->size
] = '\0';
186 int git_buf_puts(git_buf
*buf
, const char *string
)
189 return git_buf_put(buf
, string
, strlen(string
));
192 static const char base64_encode
[] =
193 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
195 int git_buf_encode_base64(git_buf
*buf
, const char *data
, size_t len
)
197 size_t extra
= len
% 3;
198 uint8_t *write
, a
, b
, c
;
199 const uint8_t *read
= (const uint8_t *)data
;
201 ENSURE_SIZE(buf
, buf
->size
+ 4 * ((len
/ 3) + !!extra
) + 1);
202 write
= (uint8_t *)&buf
->ptr
[buf
->size
];
204 /* convert each run of 3 bytes into 4 output bytes */
205 for (len
-= extra
; len
> 0; len
-= 3) {
210 *write
++ = base64_encode
[a
>> 2];
211 *write
++ = base64_encode
[(a
& 0x03) << 4 | b
>> 4];
212 *write
++ = base64_encode
[(b
& 0x0f) << 2 | c
>> 6];
213 *write
++ = base64_encode
[c
& 0x3f];
218 b
= (extra
> 1) ? *read
++ : 0;
220 *write
++ = base64_encode
[a
>> 2];
221 *write
++ = base64_encode
[(a
& 0x03) << 4 | b
>> 4];
222 *write
++ = (extra
> 1) ? base64_encode
[(b
& 0x0f) << 2] : '=';
226 buf
->size
= ((char *)write
) - buf
->ptr
;
227 buf
->ptr
[buf
->size
] = '\0';
232 /* The inverse of base64_encode, offset by '+' == 43. */
233 static const int8_t base64_decode
[] = {
237 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
238 -1, -1, -1, 0, -1, -1, -1,
239 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
240 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
241 -1, -1, -1, -1, -1, -1,
242 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
243 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
246 #define BASE64_DECODE_VALUE(c) (((c) < 43 || (c) > 122) ? -1 : base64_decode[c - 43])
248 int git_buf_decode_base64(git_buf
*buf
, const char *base64
, size_t len
)
252 size_t orig_size
= buf
->size
;
254 assert(len
% 4 == 0);
255 ENSURE_SIZE(buf
, buf
->size
+ (len
/ 4 * 3) + 1);
257 for (i
= 0; i
< len
; i
+= 4) {
258 if ((a
= BASE64_DECODE_VALUE(base64
[i
])) < 0 ||
259 (b
= BASE64_DECODE_VALUE(base64
[i
+1])) < 0 ||
260 (c
= BASE64_DECODE_VALUE(base64
[i
+2])) < 0 ||
261 (d
= BASE64_DECODE_VALUE(base64
[i
+3])) < 0) {
262 buf
->size
= orig_size
;
263 buf
->ptr
[buf
->size
] = '\0';
265 giterr_set(GITERR_INVALID
, "Invalid base64 input");
269 buf
->ptr
[buf
->size
++] = ((a
<< 2) | (b
& 0x30) >> 4);
270 buf
->ptr
[buf
->size
++] = ((b
& 0x0f) << 4) | ((c
& 0x3c) >> 2);
271 buf
->ptr
[buf
->size
++] = (c
& 0x03) << 6 | (d
& 0x3f);
274 buf
->ptr
[buf
->size
] = '\0';
278 static const char b85str
[] =
279 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
281 int git_buf_encode_base85(git_buf
*buf
, const char *data
, size_t len
)
283 ENSURE_SIZE(buf
, buf
->size
+ (5 * ((len
/ 4) + !!(len
% 4))) + 1);
290 for (i
= 24; i
>= 0; i
-= 8) {
291 uint8_t ch
= *data
++;
298 for (i
= 4; i
>= 0; i
--) {
302 b85
[i
] = b85str
[val
];
305 for (i
= 0; i
< 5; i
++)
306 buf
->ptr
[buf
->size
++] = b85
[i
];
309 buf
->ptr
[buf
->size
] = '\0';
314 int git_buf_vprintf(git_buf
*buf
, const char *format
, va_list ap
)
317 const size_t expected_size
= buf
->size
+ (strlen(format
) * 2);
319 ENSURE_SIZE(buf
, expected_size
);
326 buf
->ptr
+ buf
->size
,
327 buf
->asize
- buf
->size
,
335 buf
->ptr
= git_buf__oom
;
339 if ((size_t)len
+ 1 <= buf
->asize
- buf
->size
) {
344 ENSURE_SIZE(buf
, buf
->size
+ len
+ 1);
350 int git_buf_printf(git_buf
*buf
, const char *format
, ...)
355 va_start(ap
, format
);
356 r
= git_buf_vprintf(buf
, format
, ap
);
362 void git_buf_copy_cstr(char *data
, size_t datasize
, const git_buf
*buf
)
366 assert(data
&& datasize
&& buf
);
370 if (buf
->size
== 0 || buf
->asize
<= 0)
374 if (copylen
> datasize
- 1)
375 copylen
= datasize
- 1;
376 memmove(data
, buf
->ptr
, copylen
);
377 data
[copylen
] = '\0';
380 void git_buf_consume(git_buf
*buf
, const char *end
)
382 if (end
> buf
->ptr
&& end
<= buf
->ptr
+ buf
->size
) {
383 size_t consumed
= end
- buf
->ptr
;
384 memmove(buf
->ptr
, end
, buf
->size
- consumed
);
385 buf
->size
-= consumed
;
386 buf
->ptr
[buf
->size
] = '\0';
390 void git_buf_truncate(git_buf
*buf
, size_t len
)
392 if (len
>= buf
->size
)
396 if (buf
->size
< buf
->asize
)
397 buf
->ptr
[buf
->size
] = '\0';
400 void git_buf_shorten(git_buf
*buf
, size_t amount
)
402 if (buf
->size
> amount
)
403 git_buf_truncate(buf
, buf
->size
- amount
);
408 void git_buf_rtruncate_at_char(git_buf
*buf
, char separator
)
410 ssize_t idx
= git_buf_rfind_next(buf
, separator
);
411 git_buf_truncate(buf
, idx
< 0 ? 0 : (size_t)idx
);
414 void git_buf_swap(git_buf
*buf_a
, git_buf
*buf_b
)
421 char *git_buf_detach(git_buf
*buf
)
423 char *data
= buf
->ptr
;
425 if (buf
->asize
== 0 || buf
->ptr
== git_buf__oom
)
428 git_buf_init(buf
, 0);
433 void git_buf_attach(git_buf
*buf
, char *ptr
, size_t asize
)
439 buf
->size
= strlen(ptr
);
441 buf
->asize
= (asize
< buf
->size
) ? buf
->size
+ 1 : asize
;
442 else /* pass 0 to fall back on strlen + 1 */
443 buf
->asize
= buf
->size
+ 1;
445 git_buf_grow(buf
, asize
);
449 int git_buf_join_n(git_buf
*buf
, char separator
, int nbuf
, ...)
453 size_t total_size
= 0, original_size
= buf
->size
;
454 char *out
, *original
= buf
->ptr
;
456 if (buf
->size
> 0 && buf
->ptr
[buf
->size
- 1] != separator
)
457 ++total_size
; /* space for initial separator */
459 /* Make two passes to avoid multiple reallocation */
462 for (i
= 0; i
< nbuf
; ++i
) {
466 segment
= va_arg(ap
, const char *);
470 segment_len
= strlen(segment
);
471 total_size
+= segment_len
;
472 if (segment_len
== 0 || segment
[segment_len
- 1] != separator
)
473 ++total_size
; /* space for separator */
477 /* expand buffer if needed */
480 if (git_buf_grow(buf
, buf
->size
+ total_size
+ 1) < 0)
483 out
= buf
->ptr
+ buf
->size
;
485 /* append separator to existing buf if needed */
486 if (buf
->size
> 0 && out
[-1] != separator
)
490 for (i
= 0; i
< nbuf
; ++i
) {
494 segment
= va_arg(ap
, const char *);
498 /* deal with join that references buffer's original content */
499 if (segment
>= original
&& segment
< original
+ original_size
) {
500 size_t offset
= (segment
- original
);
501 segment
= buf
->ptr
+ offset
;
502 segment_len
= original_size
- offset
;
504 segment_len
= strlen(segment
);
507 /* skip leading separators */
508 if (out
> buf
->ptr
&& out
[-1] == separator
)
509 while (segment_len
> 0 && *segment
== separator
) {
514 /* copy over next buffer */
515 if (segment_len
> 0) {
516 memmove(out
, segment
, segment_len
);
520 /* append trailing separator (except for last item) */
521 if (i
< nbuf
- 1 && out
> buf
->ptr
&& out
[-1] != separator
)
526 /* set size based on num characters actually written */
527 buf
->size
= out
- buf
->ptr
;
528 buf
->ptr
[buf
->size
] = '\0';
539 size_t strlen_a
= str_a
? strlen(str_a
) : 0;
540 size_t strlen_b
= strlen(str_b
);
542 ssize_t offset_a
= -1;
544 /* not safe to have str_b point internally to the buffer */
545 assert(str_b
< buf
->ptr
|| str_b
>= buf
->ptr
+ buf
->size
);
547 /* figure out if we need to insert a separator */
548 if (separator
&& strlen_a
) {
549 while (*str_b
== separator
) { str_b
++; strlen_b
--; }
550 if (str_a
[strlen_a
- 1] != separator
)
554 /* str_a could be part of the buffer */
555 if (str_a
>= buf
->ptr
&& str_a
< buf
->ptr
+ buf
->size
)
556 offset_a
= str_a
- buf
->ptr
;
558 if (git_buf_grow(buf
, strlen_a
+ strlen_b
+ need_sep
+ 1) < 0)
562 /* fix up internal pointers */
564 str_a
= buf
->ptr
+ offset_a
;
566 /* do the actual copying */
567 if (offset_a
!= 0 && str_a
)
568 memmove(buf
->ptr
, str_a
, strlen_a
);
570 buf
->ptr
[strlen_a
] = separator
;
571 memcpy(buf
->ptr
+ strlen_a
+ need_sep
, str_b
, strlen_b
);
573 buf
->size
= strlen_a
+ strlen_b
+ need_sep
;
574 buf
->ptr
[buf
->size
] = '\0';
586 size_t len_a
= strlen(str_a
), len_b
= strlen(str_b
), len_c
= strlen(str_c
);
587 int sep_a
= 0, sep_b
= 0;
590 /* for this function, disallow pointers into the existing buffer */
591 assert(str_a
< buf
->ptr
|| str_a
>= buf
->ptr
+ buf
->size
);
592 assert(str_b
< buf
->ptr
|| str_b
>= buf
->ptr
+ buf
->size
);
593 assert(str_c
< buf
->ptr
|| str_c
>= buf
->ptr
+ buf
->size
);
597 while (*str_b
== separator
) { str_b
++; len_b
--; }
598 sep_a
= (str_a
[len_a
- 1] != separator
);
600 if (len_a
> 0 || len_b
> 0)
601 while (*str_c
== separator
) { str_c
++; len_c
--; }
603 sep_b
= (str_b
[len_b
- 1] != separator
);
606 if (git_buf_grow(buf
, len_a
+ sep_a
+ len_b
+ sep_b
+ len_c
+ 1) < 0)
612 memcpy(tgt
, str_a
, len_a
);
618 memcpy(tgt
, str_b
, len_b
);
624 memcpy(tgt
, str_c
, len_c
);
626 buf
->size
= len_a
+ sep_a
+ len_b
+ sep_b
+ len_c
;
627 buf
->ptr
[buf
->size
] = '\0';
632 void git_buf_rtrim(git_buf
*buf
)
634 while (buf
->size
> 0) {
635 if (!git__isspace(buf
->ptr
[buf
->size
- 1]))
641 if (buf
->asize
> buf
->size
)
642 buf
->ptr
[buf
->size
] = '\0';
645 int git_buf_cmp(const git_buf
*a
, const git_buf
*b
)
647 int result
= memcmp(a
->ptr
, b
->ptr
, min(a
->size
, b
->size
));
648 return (result
!= 0) ? result
:
649 (a
->size
< b
->size
) ? -1 : (a
->size
> b
->size
) ? 1 : 0;
660 where
<= git_buf_len(buf
) &&
661 where
+ nb_to_remove
<= git_buf_len(buf
));
663 /* Ported from git.git
664 * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176
666 ENSURE_SIZE(buf
, buf
->size
+ nb_to_insert
- nb_to_insert
+ 1);
668 memmove(buf
->ptr
+ where
+ nb_to_insert
,
669 buf
->ptr
+ where
+ nb_to_remove
,
670 buf
->size
- where
- nb_to_remove
);
672 memcpy(buf
->ptr
+ where
, data
, nb_to_insert
);
674 buf
->size
= buf
->size
+ nb_to_insert
- nb_to_remove
;
675 buf
->ptr
[buf
->size
] = '\0';