buffer.c 16.5 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8
#include "buffer.h"
#include "posix.h"
9
#include "git2/buffer.h"
joshaber committed
10
#include "buf_text.h"
11
#include <ctype.h>
12

13 14 15
/* Used as default value for git_buf->ptr so that people can always
 * assume ptr is non-NULL and zero terminated even for new git_bufs.
 */
16
char git_buf__initbuf[1];
17

18
char git_buf__oom[1];
19

20
#define ENSURE_SIZE(b, d) \
21 22
	if ((d) > buf->asize && git_buf_grow(b, (d)) < 0)\
		return -1;
23

24

25 26 27 28
void git_buf_init(git_buf *buf, size_t initial_size)
{
	buf->asize = 0;
	buf->size = 0;
29
	buf->ptr = git_buf__initbuf;
30 31 32 33 34

	if (initial_size)
		git_buf_grow(buf, initial_size);
}

35
int git_buf_try_grow(
36
	git_buf *buf, size_t target_size, bool mark_oom)
37
{
38
	char *new_ptr;
39
	size_t new_size;
40

41
	if (buf->ptr == git_buf__oom)
42
		return -1;
43

44 45 46 47
	if (buf->asize == 0 && buf->size != 0) {
		giterr_set(GITERR_INVALID, "cannot grow a borrowed buffer");
		return GIT_EINVALID;
	}
48

49 50 51
	if (!target_size)
		target_size = buf->size;

52
	if (target_size <= buf->asize)
53
		return 0;
54

55 56 57 58
	if (buf->asize == 0) {
		new_size = target_size;
		new_ptr = NULL;
	} else {
59
		new_size = buf->asize;
60 61
		new_ptr = buf->ptr;
	}
62 63 64

	/* grow the buffer size by 1.5, until it's big enough
	 * to fit our target size */
65 66
	while (new_size < target_size)
		new_size = (new_size << 1) - (new_size >> 1);
67

68
	/* round allocation up to multiple of 8 */
69
	new_size = (new_size + 7) & ~7;
70

71 72 73 74 75 76 77 78
	if (new_size < buf->size) {
		if (mark_oom)
			buf->ptr = git_buf__oom;

		giterr_set_oom();
		return -1;
	}

79
	new_ptr = git__realloc(new_ptr, new_size);
80 81

	if (!new_ptr) {
82
		if (mark_oom) {
83 84
			if (buf->ptr && (buf->ptr != git_buf__initbuf))
				git__free(buf->ptr);
85
			buf->ptr = git_buf__oom;
86
		}
87
		return -1;
88
	}
89

90 91 92 93 94 95 96 97
	buf->asize = new_size;
	buf->ptr   = new_ptr;

	/* truncate the existing buffer size if necessary */
	if (buf->size >= buf->asize)
		buf->size = buf->asize - 1;
	buf->ptr[buf->size] = '\0';

98
	return 0;
99 100
}

101 102
int git_buf_grow(git_buf *buffer, size_t target_size)
{
103
	return git_buf_try_grow(buffer, target_size, true);
104 105
}

106 107
int git_buf_grow_by(git_buf *buffer, size_t additional_size)
{
108 109 110
	size_t newsize;

	if (GIT_ADD_SIZET_OVERFLOW(&newsize, buffer->size, additional_size)) {
111 112 113 114
		buffer->ptr = git_buf__oom;
		return -1;
	}

115
	return git_buf_try_grow(buffer, newsize, true);
116 117
}

118 119 120 121
void git_buf_free(git_buf *buf)
{
	if (!buf) return;

122
	if (buf->asize > 0 && buf->ptr != NULL && buf->ptr != git_buf__oom)
123 124 125 126 127
		git__free(buf->ptr);

	git_buf_init(buf, 0);
}

128 129 130
void git_buf_sanitize(git_buf *buf)
{
	if (buf->ptr == NULL) {
131
		assert(buf->size == 0 && buf->asize == 0);
132
		buf->ptr = git_buf__initbuf;
133 134
	} else if (buf->asize > buf->size)
		buf->ptr[buf->size] = '\0';
135 136
}

137 138 139
void git_buf_clear(git_buf *buf)
{
	buf->size = 0;
140

141
	if (!buf->ptr) {
142
		buf->ptr = git_buf__initbuf;
143 144
		buf->asize = 0;
	}
145

146 147 148 149
	if (buf->asize > 0)
		buf->ptr[0] = '\0';
}

150
int git_buf_set(git_buf *buf, const void *data, size_t len)
151
{
152 153
	size_t alloclen;

154 155 156
	if (len == 0 || data == NULL) {
		git_buf_clear(buf);
	} else {
157
		if (data != buf->ptr) {
158 159
			GITERR_CHECK_ALLOC_ADD(&alloclen, len, 1);
			ENSURE_SIZE(buf, alloclen);
160 161
			memmove(buf->ptr, data, len);
		}
162

163
		buf->size = len;
164 165 166
		if (buf->asize > buf->size)
			buf->ptr[buf->size] = '\0';

167
	}
168
	return 0;
169 170
}

joshaber committed
171 172 173 174 175 176 177 178 179 180
int git_buf_is_binary(const git_buf *buf)
{
	return git_buf_text_is_binary(buf);
}

int git_buf_contains_nul(const git_buf *buf)
{
	return git_buf_text_contains_nul(buf);
}

181
int git_buf_sets(git_buf *buf, const char *string)
182
{
183
	return git_buf_set(buf, string, string ? strlen(string) : 0);
184 185
}

186
int git_buf_putc(git_buf *buf, char c)
187
{
188 189 190
	size_t new_size;
	GITERR_CHECK_ALLOC_ADD(&new_size, buf->size, 2);
	ENSURE_SIZE(buf, new_size);
191
	buf->ptr[buf->size++] = c;
192
	buf->ptr[buf->size] = '\0';
193
	return 0;
194 195
}

196 197
int git_buf_putcn(git_buf *buf, char c, size_t len)
{
198 199 200 201
	size_t new_size;
	GITERR_CHECK_ALLOC_ADD(&new_size, buf->size, len);
	GITERR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
	ENSURE_SIZE(buf, new_size);
202 203 204 205 206 207
	memset(buf->ptr + buf->size, c, len);
	buf->size += len;
	buf->ptr[buf->size] = '\0';
	return 0;
}

208
int git_buf_put(git_buf *buf, const char *data, size_t len)
209
{
210
	if (len) {
211 212
		size_t new_size;

213
		assert(data);
214 215 216 217
		
		GITERR_CHECK_ALLOC_ADD(&new_size, buf->size, len);
		GITERR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
		ENSURE_SIZE(buf, new_size);
218 219 220 221
		memmove(buf->ptr + buf->size, data, len);
		buf->size += len;
		buf->ptr[buf->size] = '\0';
	}
222
	return 0;
223 224
}

225
int git_buf_puts(git_buf *buf, const char *string)
226
{
227
	assert(string);
228
	return git_buf_put(buf, string, strlen(string));
229 230
}

231
static const char base64_encode[] =
232 233
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

234
int git_buf_encode_base64(git_buf *buf, const char *data, size_t len)
235 236 237 238
{
	size_t extra = len % 3;
	uint8_t *write, a, b, c;
	const uint8_t *read = (const uint8_t *)data;
239 240 241 242 243
	size_t blocks = (len / 3) + !!extra, alloclen;

	GITERR_CHECK_ALLOC_ADD(&blocks, blocks, 1);
	GITERR_CHECK_ALLOC_MULTIPLY(&alloclen, blocks, 4);
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, buf->size);
244

245
	ENSURE_SIZE(buf, alloclen);
246 247 248 249 250 251 252 253
	write = (uint8_t *)&buf->ptr[buf->size];

	/* convert each run of 3 bytes into 4 output bytes */
	for (len -= extra; len > 0; len -= 3) {
		a = *read++;
		b = *read++;
		c = *read++;

254 255 256 257
		*write++ = base64_encode[a >> 2];
		*write++ = base64_encode[(a & 0x03) << 4 | b >> 4];
		*write++ = base64_encode[(b & 0x0f) << 2 | c >> 6];
		*write++ = base64_encode[c & 0x3f];
258 259 260 261 262 263
	}

	if (extra > 0) {
		a = *read++;
		b = (extra > 1) ? *read++ : 0;

264 265 266
		*write++ = base64_encode[a >> 2];
		*write++ = base64_encode[(a & 0x03) << 4 | b >> 4];
		*write++ = (extra > 1) ? base64_encode[(b & 0x0f) << 2] : '=';
267 268 269 270 271 272 273 274 275
		*write++ = '=';
	}

	buf->size = ((char *)write) - buf->ptr;
	buf->ptr[buf->size] = '\0';

	return 0;
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/* The inverse of base64_encode, offset by '+' == 43. */
static const int8_t base64_decode[] = {
	62,
	-1, -1, -1,
	63,
	52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
	-1, -1, -1, 0, -1, -1, -1,
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
	13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
	-1, -1, -1, -1, -1, -1,
	26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
	39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
};

#define BASE64_DECODE_VALUE(c) (((c) < 43 || (c) > 122) ? -1 : base64_decode[c - 43])

int git_buf_decode_base64(git_buf *buf, const char *base64, size_t len)
{
	size_t i;
	int8_t a, b, c, d;
296
	size_t orig_size = buf->size, new_size;
297 298

	assert(len % 4 == 0);
299 300 301
	GITERR_CHECK_ALLOC_ADD(&new_size, (len / 4 * 3), buf->size);
	GITERR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
	ENSURE_SIZE(buf, new_size);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

	for (i = 0; i < len; i += 4) {
		if ((a = BASE64_DECODE_VALUE(base64[i])) < 0 ||
			(b = BASE64_DECODE_VALUE(base64[i+1])) < 0 ||
			(c = BASE64_DECODE_VALUE(base64[i+2])) < 0 ||
			(d = BASE64_DECODE_VALUE(base64[i+3])) < 0) {
			buf->size = orig_size;
			buf->ptr[buf->size] = '\0';

			giterr_set(GITERR_INVALID, "Invalid base64 input");
			return -1;
		}

		buf->ptr[buf->size++] = ((a << 2) | (b & 0x30) >> 4);
		buf->ptr[buf->size++] = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2);
		buf->ptr[buf->size++] = (c & 0x03) << 6 | (d & 0x3f);
	}

	buf->ptr[buf->size] = '\0';
	return 0;
}

324 325 326
static const char b85str[] =
	"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";

327
int git_buf_encode_base85(git_buf *buf, const char *data, size_t len)
328
{
329
	size_t blocks = (len / 4) + !!(len % 4), alloclen;
330

331 332 333 334 335
	GITERR_CHECK_ALLOC_MULTIPLY(&alloclen, blocks, 5);
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, buf->size);
	GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);

	ENSURE_SIZE(buf, alloclen);
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

	while (len) {
		uint32_t acc = 0;
		char b85[5];
		int i;

		for (i = 24; i >= 0; i -= 8) {
			uint8_t ch = *data++;
			acc |= ch << i;

			if (--len == 0)
				break;
		}

		for (i = 4; i >= 0; i--) {
			int val = acc % 85;
			acc /= 85;

			b85[i] = b85str[val];
		}

		for (i = 0; i < 5; i++)
			buf->ptr[buf->size++] = b85[i];
	}

	buf->ptr[buf->size] = '\0';

	return 0;
}

366
int git_buf_vprintf(git_buf *buf, const char *format, va_list ap)
367
{
368
	size_t expected_size, new_size;
369
	int len;
370

371 372
	GITERR_CHECK_ALLOC_MULTIPLY(&expected_size, strlen(format), 2);
	GITERR_CHECK_ALLOC_ADD(&expected_size, expected_size, buf->size);
373
	ENSURE_SIZE(buf, expected_size);
374 375

	while (1) {
376 377 378 379 380 381 382 383
		va_list args;
		va_copy(args, ap);

		len = p_vsnprintf(
			buf->ptr + buf->size,
			buf->asize - buf->size,
			format, args
		);
384

385 386
		va_end(args);

387
		if (len < 0) {
388
			git__free(buf->ptr);
389
			buf->ptr = git_buf__oom;
390
			return -1;
391 392
		}

393
		if ((size_t)len + 1 <= buf->asize - buf->size) {
394
			buf->size += len;
395
			break;
396 397
		}

398 399 400
		GITERR_CHECK_ALLOC_ADD(&new_size, buf->size, len);
		GITERR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
		ENSURE_SIZE(buf, new_size);
401
	}
402

403
	return 0;
404 405
}

406 407 408 409 410 411 412 413 414 415 416 417
int git_buf_printf(git_buf *buf, const char *format, ...)
{
	int r;
	va_list ap;

	va_start(ap, format);
	r = git_buf_vprintf(buf, format, ap);
	va_end(ap);

	return r;
}

418
void git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf)
419 420 421
{
	size_t copylen;

422
	assert(data && datasize && buf);
423 424 425 426 427 428 429 430 431 432 433

	data[0] = '\0';

	if (buf->size == 0 || buf->asize <= 0)
		return;

	copylen = buf->size;
	if (copylen > datasize - 1)
		copylen = datasize - 1;
	memmove(data, buf->ptr, copylen);
	data[copylen] = '\0';
434 435
}

436 437
void git_buf_consume(git_buf *buf, const char *end)
{
438 439 440 441
	if (end > buf->ptr && end <= buf->ptr + buf->size) {
		size_t consumed = end - buf->ptr;
		memmove(buf->ptr, end, buf->size - consumed);
		buf->size -= consumed;
442
		buf->ptr[buf->size] = '\0';
443 444 445
	}
}

446
void git_buf_truncate(git_buf *buf, size_t len)
447
{
448 449 450 451 452
	if (len >= buf->size)
		return;

	buf->size = len;
	if (buf->size < buf->asize)
453 454 455
		buf->ptr[buf->size] = '\0';
}

456 457
void git_buf_shorten(git_buf *buf, size_t amount)
{
458 459 460 461
	if (buf->size > amount)
		git_buf_truncate(buf, buf->size - amount);
	else
		git_buf_clear(buf);
462 463
}

464 465
void git_buf_rtruncate_at_char(git_buf *buf, char separator)
{
466 467
	ssize_t idx = git_buf_rfind_next(buf, separator);
	git_buf_truncate(buf, idx < 0 ? 0 : (size_t)idx);
468 469
}

470 471 472 473 474
void git_buf_swap(git_buf *buf_a, git_buf *buf_b)
{
	git_buf t = *buf_a;
	*buf_a = *buf_b;
	*buf_b = t;
475
}
476

477
char *git_buf_detach(git_buf *buf)
478
{
479
	char *data = buf->ptr;
480

481
	if (buf->asize == 0 || buf->ptr == git_buf__oom)
482 483
		return NULL;

484
	git_buf_init(buf, 0);
485 486 487 488

	return data;
}

489
void git_buf_attach(git_buf *buf, char *ptr, size_t asize)
490
{
491 492 493 494 495 496 497 498 499 500 501 502 503
	git_buf_free(buf);

	if (ptr) {
		buf->ptr = ptr;
		buf->size = strlen(ptr);
		if (asize)
			buf->asize = (asize < buf->size) ? buf->size + 1 : asize;
		else /* pass 0 to fall back on strlen + 1 */
			buf->asize = buf->size + 1;
	} else {
		git_buf_grow(buf, asize);
	}
}
504

505 506 507 508 509 510 511 512 513 514 515 516 517 518
void git_buf_attach_notowned(git_buf *buf, const char *ptr, size_t size)
{
	if (git_buf_is_allocated(buf))
		git_buf_free(buf);

	if (!size) {
		git_buf_init(buf, 0);
	} else {
		buf->ptr = (char *)ptr;
		buf->asize = 0;
		buf->size = size;
	}
}

519 520
int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...)
{
521
	va_list ap;
522
	int i;
523 524
	size_t total_size = 0, original_size = buf->size;
	char *out, *original = buf->ptr;
525 526 527 528

	if (buf->size > 0 && buf->ptr[buf->size - 1] != separator)
		++total_size; /* space for initial separator */

529 530
	/* Make two passes to avoid multiple reallocation */

531 532 533
	va_start(ap, nbuf);
	for (i = 0; i < nbuf; ++i) {
		const char* segment;
534
		size_t segment_len;
535 536 537 538 539 540

		segment = va_arg(ap, const char *);
		if (!segment)
			continue;

		segment_len = strlen(segment);
541 542 543

		GITERR_CHECK_ALLOC_ADD(&total_size, total_size, segment_len);

544
		if (segment_len == 0 || segment[segment_len - 1] != separator)
545
			GITERR_CHECK_ALLOC_ADD(&total_size, total_size, 1);
546 547 548
	}
	va_end(ap);

549
	/* expand buffer if needed */
550 551
	if (total_size == 0)
		return 0;
552

553 554
	GITERR_CHECK_ALLOC_ADD(&total_size, total_size, 1);
	if (git_buf_grow_by(buf, total_size) < 0)
555
		return -1;
556 557 558 559 560 561 562 563 564 565

	out = buf->ptr + buf->size;

	/* append separator to existing buf if needed */
	if (buf->size > 0 && out[-1] != separator)
		*out++ = separator;

	va_start(ap, nbuf);
	for (i = 0; i < nbuf; ++i) {
		const char* segment;
566
		size_t segment_len;
567 568 569 570 571

		segment = va_arg(ap, const char *);
		if (!segment)
			continue;

572 573 574 575 576 577 578 579 580
		/* deal with join that references buffer's original content */
		if (segment >= original && segment < original + original_size) {
			size_t offset = (segment - original);
			segment = buf->ptr + offset;
			segment_len = original_size - offset;
		} else {
			segment_len = strlen(segment);
		}

581 582
		/* skip leading separators */
		if (out > buf->ptr && out[-1] == separator)
583 584 585 586
			while (segment_len > 0 && *segment == separator) {
				segment++;
				segment_len--;
			}
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

		/* copy over next buffer */
		if (segment_len > 0) {
			memmove(out, segment, segment_len);
			out += segment_len;
		}

		/* append trailing separator (except for last item) */
		if (i < nbuf - 1 && out > buf->ptr && out[-1] != separator)
			*out++ = separator;
	}
	va_end(ap);

	/* set size based on num characters actually written */
	buf->size = out - buf->ptr;
602
	buf->ptr[buf->size] = '\0';
603

604
	return 0;
605 606
}

607
int git_buf_join(
608 609 610 611 612
	git_buf *buf,
	char separator,
	const char *str_a,
	const char *str_b)
{
613
	size_t strlen_a = str_a ? strlen(str_a) : 0;
614
	size_t strlen_b = strlen(str_b);
615
	size_t alloc_len;
616
	int need_sep = 0;
617 618 619
	ssize_t offset_a = -1;

	/* not safe to have str_b point internally to the buffer */
620
	assert(str_b < buf->ptr || str_b >= buf->ptr + buf->size);
621 622 623 624 625 626

	/* figure out if we need to insert a separator */
	if (separator && strlen_a) {
		while (*str_b == separator) { str_b++; strlen_b--; }
		if (str_a[strlen_a - 1] != separator)
			need_sep = 1;
627 628
	}

629 630 631 632
	/* str_a could be part of the buffer */
	if (str_a >= buf->ptr && str_a < buf->ptr + buf->size)
		offset_a = str_a - buf->ptr;

633 634 635 636
	GITERR_CHECK_ALLOC_ADD(&alloc_len, strlen_a, strlen_b);
	GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, need_sep);
	GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1);
	if (git_buf_grow(buf, alloc_len) < 0)
637
		return -1;
638
	assert(buf->ptr);
639

640 641 642 643 644
	/* fix up internal pointers */
	if (offset_a >= 0)
		str_a = buf->ptr + offset_a;

	/* do the actual copying */
645
	if (offset_a != 0 && str_a)
646
		memmove(buf->ptr, str_a, strlen_a);
647 648
	if (need_sep)
		buf->ptr[strlen_a] = separator;
649
	memcpy(buf->ptr + strlen_a + need_sep, str_b, strlen_b);
650

651
	buf->size = strlen_a + strlen_b + need_sep;
652
	buf->ptr[buf->size] = '\0';
653

654
	return 0;
655
}
656

657 658 659 660 661 662 663
int git_buf_join3(
	git_buf *buf,
	char separator,
	const char *str_a,
	const char *str_b,
	const char *str_c)
{
664 665 666 667
	size_t len_a = strlen(str_a),
		len_b = strlen(str_b),
		len_c = strlen(str_c),
		len_total;
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
	int sep_a = 0, sep_b = 0;
	char *tgt;

	/* for this function, disallow pointers into the existing buffer */
	assert(str_a < buf->ptr || str_a >= buf->ptr + buf->size);
	assert(str_b < buf->ptr || str_b >= buf->ptr + buf->size);
	assert(str_c < buf->ptr || str_c >= buf->ptr + buf->size);

	if (separator) {
		if (len_a > 0) {
			while (*str_b == separator) { str_b++; len_b--; }
			sep_a = (str_a[len_a - 1] != separator);
		}
		if (len_a > 0 || len_b > 0)
			while (*str_c == separator) { str_c++; len_c--; }
		if (len_b > 0)
			sep_b = (str_b[len_b - 1] != separator);
	}

687 688 689 690 691 692
	GITERR_CHECK_ALLOC_ADD(&len_total, len_a, sep_a);
	GITERR_CHECK_ALLOC_ADD(&len_total, len_total, len_b);
	GITERR_CHECK_ALLOC_ADD(&len_total, len_total, sep_b);
	GITERR_CHECK_ALLOC_ADD(&len_total, len_total, len_c);
	GITERR_CHECK_ALLOC_ADD(&len_total, len_total, 1);
	if (git_buf_grow(buf, len_total) < 0)
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
		return -1;

	tgt = buf->ptr;

	if (len_a) {
		memcpy(tgt, str_a, len_a);
		tgt += len_a;
	}
	if (sep_a)
		*tgt++ = separator;
	if (len_b) {
		memcpy(tgt, str_b, len_b);
		tgt += len_b;
	}
	if (sep_b)
		*tgt++ = separator;
	if (len_c)
		memcpy(tgt, str_c, len_c);

	buf->size = len_a + sep_a + len_b + sep_b + len_c;
	buf->ptr[buf->size] = '\0';

	return 0;
}

718 719 720
void git_buf_rtrim(git_buf *buf)
{
	while (buf->size > 0) {
721
		if (!git__isspace(buf->ptr[buf->size - 1]))
722 723 724 725
			break;

		buf->size--;
	}
726

727 728
	if (buf->asize > buf->size)
		buf->ptr[buf->size] = '\0';
729
}
730 731 732 733 734 735 736

int git_buf_cmp(const git_buf *a, const git_buf *b)
{
	int result = memcmp(a->ptr, b->ptr, min(a->size, b->size));
	return (result != 0) ? result :
		(a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0;
}
737

738 739 740 741 742 743 744
int git_buf_splice(
	git_buf *buf,
	size_t where,
	size_t nb_to_remove,
	const char *data,
	size_t nb_to_insert)
{
745 746
	char *splice_loc;
	size_t new_size, alloc_size;
747

748 749 750
	assert(buf && where <= buf->size && nb_to_remove <= buf->size - where);

	splice_loc = buf->ptr + where;
751 752 753 754

	/* Ported from git.git
	 * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176
	 */
755 756 757
	GITERR_CHECK_ALLOC_ADD(&new_size, (buf->size - nb_to_remove), nb_to_insert);
	GITERR_CHECK_ALLOC_ADD(&alloc_size, new_size, 1);
	ENSURE_SIZE(buf, alloc_size);
758

759 760 761
	memmove(splice_loc + nb_to_insert,
		splice_loc + nb_to_remove,
		buf->size - where - nb_to_remove);
762

763
	memcpy(splice_loc, data, nb_to_insert);
764

765
	buf->size = new_size;
766 767 768
	buf->ptr[buf->size] = '\0';
	return 0;
}