buffer.c 23.9 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 ((b)->ptr == git_buf__oom || \
	    ((d) > (b)->asize && git_buf_grow((b), (d)) < 0))\
23
		return -1;
24

25

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

32 33 34
	ENSURE_SIZE(buf, initial_size);

	return 0;
35 36
}

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

43
	if (buf->ptr == git_buf__oom)
44
		return -1;
45

46
	if (buf->asize == 0 && buf->size != 0) {
47
		git_error_set(GIT_ERROR_INVALID, "cannot grow a borrowed buffer");
48 49
		return GIT_EINVALID;
	}
50

51 52 53
	if (!target_size)
		target_size = buf->size;

54
	if (target_size <= buf->asize)
55
		return 0;
56

57 58 59 60
	if (buf->asize == 0) {
		new_size = target_size;
		new_ptr = NULL;
	} else {
61
		new_size = buf->asize;
62 63 64 65 66 67 68 69
		/*
		 * Grow the allocated buffer by 1.5 to allow
		 * re-use of memory holes resulting from the
		 * realloc. If this is still too small, then just
		 * use the target size.
		 */
		if ((new_size = (new_size << 1) - (new_size >> 1)) < target_size)
			new_size = target_size;
70 71
		new_ptr = buf->ptr;
	}
72

73
	/* round allocation up to multiple of 8 */
74
	new_size = (new_size + 7) & ~7;
75

76
	if (new_size < buf->size) {
77 78 79
		if (mark_oom) {
			if (buf->ptr && buf->ptr != git_buf__initbuf)
				git__free(buf->ptr);
80
			buf->ptr = git_buf__oom;
81
		}
82

83
		git_error_set_oom();
84 85 86
		return -1;
	}

87
	new_ptr = git__realloc(new_ptr, new_size);
88 89

	if (!new_ptr) {
90
		if (mark_oom) {
91 92
			if (buf->ptr && (buf->ptr != git_buf__initbuf))
				git__free(buf->ptr);
93
			buf->ptr = git_buf__oom;
94
		}
95
		return -1;
96
	}
97

98 99 100 101 102 103 104 105
	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';

106
	return 0;
107 108
}

109 110
int git_buf_grow(git_buf *buffer, size_t target_size)
{
111
	return git_buf_try_grow(buffer, target_size, true);
112 113
}

114 115
int git_buf_grow_by(git_buf *buffer, size_t additional_size)
{
116 117 118
	size_t newsize;

	if (GIT_ADD_SIZET_OVERFLOW(&newsize, buffer->size, additional_size)) {
119 120 121 122
		buffer->ptr = git_buf__oom;
		return -1;
	}

123
	return git_buf_try_grow(buffer, newsize, true);
124 125
}

126
void git_buf_dispose(git_buf *buf)
127 128 129
{
	if (!buf) return;

130
	if (buf->asize > 0 && buf->ptr != NULL && buf->ptr != git_buf__oom)
131 132 133 134 135
		git__free(buf->ptr);

	git_buf_init(buf, 0);
}

136
#ifndef GIT_DEPRECATE_HARD
137 138 139 140
void git_buf_free(git_buf *buf)
{
	git_buf_dispose(buf);
}
141
#endif
142

143
int git_buf_sanitize(git_buf *buf)
144 145
{
	if (buf->ptr == NULL) {
146 147
		GIT_ASSERT_ARG(buf->size == 0 && buf->asize == 0);

148
		buf->ptr = git_buf__initbuf;
149
	} else if (buf->asize > buf->size) {
150
		buf->ptr[buf->size] = '\0';
151 152 153
	}

	return 0;
154 155
}

156 157 158
void git_buf_clear(git_buf *buf)
{
	buf->size = 0;
159

160
	if (!buf->ptr) {
161
		buf->ptr = git_buf__initbuf;
162 163
		buf->asize = 0;
	}
164

165 166 167 168
	if (buf->asize > 0)
		buf->ptr[0] = '\0';
}

169
int git_buf_set(git_buf *buf, const void *data, size_t len)
170
{
171 172
	size_t alloclen;

173 174 175
	if (len == 0 || data == NULL) {
		git_buf_clear(buf);
	} else {
176
		if (data != buf->ptr) {
177
			GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1);
178
			ENSURE_SIZE(buf, alloclen);
179 180
			memmove(buf->ptr, data, len);
		}
181

182
		buf->size = len;
183 184 185
		if (buf->asize > buf->size)
			buf->ptr[buf->size] = '\0';

186
	}
187
	return 0;
188 189
}

joshaber committed
190 191 192 193 194 195 196 197 198 199
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);
}

200
int git_buf_sets(git_buf *buf, const char *string)
201
{
202
	return git_buf_set(buf, string, string ? strlen(string) : 0);
203 204
}

205
int git_buf_putc(git_buf *buf, char c)
206
{
207
	size_t new_size;
208
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, 2);
209
	ENSURE_SIZE(buf, new_size);
210
	buf->ptr[buf->size++] = c;
211
	buf->ptr[buf->size] = '\0';
212
	return 0;
213 214
}

215 216
int git_buf_putcn(git_buf *buf, char c, size_t len)
{
217
	size_t new_size;
218 219
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len);
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
220
	ENSURE_SIZE(buf, new_size);
221 222 223 224 225 226
	memset(buf->ptr + buf->size, c, len);
	buf->size += len;
	buf->ptr[buf->size] = '\0';
	return 0;
}

227
int git_buf_put(git_buf *buf, const char *data, size_t len)
228
{
229
	if (len) {
230 231
		size_t new_size;

232
		GIT_ASSERT_ARG(data);
233

234 235
		GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len);
		GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
236
		ENSURE_SIZE(buf, new_size);
237 238 239 240
		memmove(buf->ptr + buf->size, data, len);
		buf->size += len;
		buf->ptr[buf->size] = '\0';
	}
241
	return 0;
242 243
}

244
int git_buf_puts(git_buf *buf, const char *string)
245
{
246 247
	GIT_ASSERT_ARG(string);

248
	return git_buf_put(buf, string, strlen(string));
249 250
}

251
static const char base64_encode[] =
252 253
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

254
int git_buf_encode_base64(git_buf *buf, const char *data, size_t len)
255 256 257 258
{
	size_t extra = len % 3;
	uint8_t *write, a, b, c;
	const uint8_t *read = (const uint8_t *)data;
259 260
	size_t blocks = (len / 3) + !!extra, alloclen;

261 262 263
	GIT_ERROR_CHECK_ALLOC_ADD(&blocks, blocks, 1);
	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloclen, blocks, 4);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, buf->size);
264

265
	ENSURE_SIZE(buf, alloclen);
266 267 268 269 270 271 272 273
	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++;

274 275 276 277
		*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];
278 279 280 281 282 283
	}

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

284 285 286
		*write++ = base64_encode[a >> 2];
		*write++ = base64_encode[(a & 0x03) << 4 | b >> 4];
		*write++ = (extra > 1) ? base64_encode[(b & 0x0f) << 2] : '=';
287 288 289 290 291 292 293 294 295
		*write++ = '=';
	}

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

	return 0;
}

296
/* The inverse of base64_encode */
297
static const int8_t base64_decode[] = {
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
314 315 316 317 318 319
};

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

322
	if (len % 4) {
323
		git_error_set(GIT_ERROR_INVALID, "invalid base64 input");
324 325 326
		return -1;
	}

327
	GIT_ASSERT_ARG(len % 4 == 0);
328 329
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, (len / 4 * 3), buf->size);
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
330
	ENSURE_SIZE(buf, new_size);
331 332

	for (i = 0; i < len; i += 4) {
333 334 335 336
		if ((a = base64_decode[(unsigned char)base64[i]]) < 0 ||
			(b = base64_decode[(unsigned char)base64[i+1]]) < 0 ||
			(c = base64_decode[(unsigned char)base64[i+2]]) < 0 ||
			(d = base64_decode[(unsigned char)base64[i+3]]) < 0) {
337 338 339
			buf->size = orig_size;
			buf->ptr[buf->size] = '\0';

340
			git_error_set(GIT_ERROR_INVALID, "invalid base64 input");
341 342 343 344 345 346 347 348 349 350 351 352
			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;
}

353
static const char base85_encode[] =
354 355
	"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";

356
int git_buf_encode_base85(git_buf *buf, const char *data, size_t len)
357
{
358
	size_t blocks = (len / 4) + !!(len % 4), alloclen;
359

360 361 362
	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloclen, blocks, 5);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, buf->size);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
363 364

	ENSURE_SIZE(buf, alloclen);
365 366 367 368 369 370 371 372

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

		for (i = 24; i >= 0; i -= 8) {
			uint8_t ch = *data++;
373
			acc |= (uint32_t)ch << i;
374 375 376 377 378 379 380 381 382

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

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

383
			b85[i] = base85_encode[val];
384 385 386 387 388 389 390 391 392 393 394
		}

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

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

	return 0;
}

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
/* The inverse of base85_encode */
static const int8_t base85_decode[] = {
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, 63, -1, 64, 65, 66, 67, -1, 68, 69, 70, 71, -1, 72, -1, -1,
	 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, -1, 73, 74, 75, 76, 77,
	78, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
	26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, -1, -1, -1, 79, 80,
	81, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 82, 83, 84, 85, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};

int git_buf_decode_base85(
	git_buf *buf,
	const char *base85,
	size_t base85_len,
	size_t output_len)
{
	size_t orig_size = buf->size, new_size;

	if (base85_len % 5 ||
		output_len > base85_len * 4 / 5) {
425
		git_error_set(GIT_ERROR_INVALID, "invalid base85 input");
426 427 428
		return -1;
	}

429 430
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, output_len, buf->size);
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
	ENSURE_SIZE(buf, new_size);

	while (output_len) {
		unsigned acc = 0;
		int de, cnt = 4;
		unsigned char ch;
		do {
			ch = *base85++;
			de = base85_decode[ch];
			if (--de < 0)
				goto on_error;

			acc = acc * 85 + de;
		} while (--cnt);
		ch = *base85++;
		de = base85_decode[ch];
		if (--de < 0)
			goto on_error;

		/* Detect overflow. */
		if (0xffffffff / 85 < acc ||
			0xffffffff - de < (acc *= 85))
			goto on_error;

		acc += de;

457
		cnt = (output_len < 4) ? (int)output_len : 4;
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
		output_len -= cnt;
		do {
			acc = (acc << 8) | (acc >> 24);
			buf->ptr[buf->size++] = acc;
		} while (--cnt);
	}

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

	return 0;

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

473
	git_error_set(GIT_ERROR_INVALID, "invalid base85 input");
474 475 476
	return -1;
}

477 478 479 480 481 482 483 484 485
#define HEX_DECODE(c) ((c | 32) % 39 - 9)

int git_buf_decode_percent(
	git_buf *buf,
	const char *str,
	size_t str_len)
{
	size_t str_pos, new_size;

486 487
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, str_len);
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
	ENSURE_SIZE(buf, new_size);

	for (str_pos = 0; str_pos < str_len; buf->size++, str_pos++) {
		if (str[str_pos] == '%' &&
			str_len > str_pos + 2 &&
			isxdigit(str[str_pos + 1]) &&
			isxdigit(str[str_pos + 2])) {
			buf->ptr[buf->size] = (HEX_DECODE(str[str_pos + 1]) << 4) +
				HEX_DECODE(str[str_pos + 2]);
			str_pos += 2;
		} else {
			buf->ptr[buf->size] = str[str_pos];
		}
	}

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

507
int git_buf_vprintf(git_buf *buf, const char *format, va_list ap)
508
{
509
	size_t expected_size, new_size;
510
	int len;
511

512 513
	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&expected_size, strlen(format), 2);
	GIT_ERROR_CHECK_ALLOC_ADD(&expected_size, expected_size, buf->size);
514
	ENSURE_SIZE(buf, expected_size);
515 516

	while (1) {
517 518 519 520 521 522 523 524
		va_list args;
		va_copy(args, ap);

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

526 527
		va_end(args);

528
		if (len < 0) {
529
			git__free(buf->ptr);
530
			buf->ptr = git_buf__oom;
531
			return -1;
532 533
		}

534
		if ((size_t)len + 1 <= buf->asize - buf->size) {
535
			buf->size += len;
536
			break;
537 538
		}

539 540
		GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len);
		GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
541
		ENSURE_SIZE(buf, new_size);
542
	}
543

544
	return 0;
545 546
}

547 548 549 550 551 552 553 554 555 556 557 558
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;
}

559
int git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf)
560 561 562
{
	size_t copylen;

563 564 565
	GIT_ASSERT_ARG(data);
	GIT_ASSERT_ARG(datasize);
	GIT_ASSERT_ARG(buf);
566 567 568 569

	data[0] = '\0';

	if (buf->size == 0 || buf->asize <= 0)
570
		return 0;
571 572 573 574 575 576

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

	return 0;
579 580
}

581 582 583 584 585
void git_buf_consume_bytes(git_buf *buf, size_t len)
{
	git_buf_consume(buf, buf->ptr + len);
}

586 587
void git_buf_consume(git_buf *buf, const char *end)
{
588 589 590 591
	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;
592
		buf->ptr[buf->size] = '\0';
593 594 595
	}
}

596
void git_buf_truncate(git_buf *buf, size_t len)
597
{
598 599 600 601 602
	if (len >= buf->size)
		return;

	buf->size = len;
	if (buf->size < buf->asize)
603 604 605
		buf->ptr[buf->size] = '\0';
}

606 607
void git_buf_shorten(git_buf *buf, size_t amount)
{
608 609 610 611
	if (buf->size > amount)
		git_buf_truncate(buf, buf->size - amount);
	else
		git_buf_clear(buf);
612 613
}

614 615
void git_buf_rtruncate_at_char(git_buf *buf, char separator)
{
616 617
	ssize_t idx = git_buf_rfind_next(buf, separator);
	git_buf_truncate(buf, idx < 0 ? 0 : (size_t)idx);
618 619
}

620 621 622 623 624
void git_buf_swap(git_buf *buf_a, git_buf *buf_b)
{
	git_buf t = *buf_a;
	*buf_a = *buf_b;
	*buf_b = t;
625
}
626

627
char *git_buf_detach(git_buf *buf)
628
{
629
	char *data = buf->ptr;
630

631
	if (buf->asize == 0 || buf->ptr == git_buf__oom)
632 633
		return NULL;

634
	git_buf_init(buf, 0);
635 636 637 638

	return data;
}

639
int git_buf_attach(git_buf *buf, char *ptr, size_t asize)
640
{
641
	git_buf_dispose(buf);
642 643 644 645 646 647 648 649 650

	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;
	}
651 652 653

	ENSURE_SIZE(buf, asize);
	return 0;
654
}
655

656 657 658
void git_buf_attach_notowned(git_buf *buf, const char *ptr, size_t size)
{
	if (git_buf_is_allocated(buf))
659
		git_buf_dispose(buf);
660 661 662 663 664 665 666 667 668 669

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

670 671
int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...)
{
672
	va_list ap;
673
	int i;
674 675
	size_t total_size = 0, original_size = buf->size;
	char *out, *original = buf->ptr;
676 677 678 679

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

680 681
	/* Make two passes to avoid multiple reallocation */

682 683 684
	va_start(ap, nbuf);
	for (i = 0; i < nbuf; ++i) {
		const char* segment;
685
		size_t segment_len;
686 687 688 689 690 691

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

		segment_len = strlen(segment);
692

693
		GIT_ERROR_CHECK_ALLOC_ADD(&total_size, total_size, segment_len);
694

695
		if (segment_len == 0 || segment[segment_len - 1] != separator)
696
			GIT_ERROR_CHECK_ALLOC_ADD(&total_size, total_size, 1);
697 698 699
	}
	va_end(ap);

700
	/* expand buffer if needed */
701 702
	if (total_size == 0)
		return 0;
703

704
	GIT_ERROR_CHECK_ALLOC_ADD(&total_size, total_size, 1);
705
	if (git_buf_grow_by(buf, total_size) < 0)
706
		return -1;
707 708 709 710 711 712 713 714 715 716

	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;
717
		size_t segment_len;
718 719 720 721 722

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

723 724 725 726 727 728 729 730 731
		/* 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);
		}

732 733
		/* skip leading separators */
		if (out > buf->ptr && out[-1] == separator)
734 735 736 737
			while (segment_len > 0 && *segment == separator) {
				segment++;
				segment_len--;
			}
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752

		/* 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;
753
	buf->ptr[buf->size] = '\0';
754

755
	return 0;
756 757
}

758
int git_buf_join(
759 760 761 762 763
	git_buf *buf,
	char separator,
	const char *str_a,
	const char *str_b)
{
764
	size_t strlen_a = str_a ? strlen(str_a) : 0;
765
	size_t strlen_b = strlen(str_b);
766
	size_t alloc_len;
767
	int need_sep = 0;
768 769 770
	ssize_t offset_a = -1;

	/* not safe to have str_b point internally to the buffer */
771
	if (buf->size)
772
		GIT_ASSERT_ARG(str_b < buf->ptr || str_b >= buf->ptr + buf->size);
773 774 775 776 777 778

	/* 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;
779 780
	}

781
	/* str_a could be part of the buffer */
782
	if (buf->size && str_a >= buf->ptr && str_a < buf->ptr + buf->size)
783 784
		offset_a = str_a - buf->ptr;

785 786 787
	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, strlen_a, strlen_b);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, need_sep);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1);
788
	ENSURE_SIZE(buf, alloc_len);
789

790 791 792 793 794
	/* fix up internal pointers */
	if (offset_a >= 0)
		str_a = buf->ptr + offset_a;

	/* do the actual copying */
795
	if (offset_a != 0 && str_a)
796
		memmove(buf->ptr, str_a, strlen_a);
797 798
	if (need_sep)
		buf->ptr[strlen_a] = separator;
799
	memcpy(buf->ptr + strlen_a + need_sep, str_b, strlen_b);
800

801
	buf->size = strlen_a + strlen_b + need_sep;
802
	buf->ptr[buf->size] = '\0';
803

804
	return 0;
805
}
806

807 808 809 810 811 812 813
int git_buf_join3(
	git_buf *buf,
	char separator,
	const char *str_a,
	const char *str_b,
	const char *str_c)
{
814 815 816 817
	size_t len_a = strlen(str_a),
		len_b = strlen(str_b),
		len_c = strlen(str_c),
		len_total;
818 819 820 821
	int sep_a = 0, sep_b = 0;
	char *tgt;

	/* for this function, disallow pointers into the existing buffer */
822 823 824
	GIT_ASSERT(str_a < buf->ptr || str_a >= buf->ptr + buf->size);
	GIT_ASSERT(str_b < buf->ptr || str_b >= buf->ptr + buf->size);
	GIT_ASSERT(str_c < buf->ptr || str_c >= buf->ptr + buf->size);
825 826 827 828 829 830 831 832 833 834 835 836

	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);
	}

837 838 839 840 841
	GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_a, sep_a);
	GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, len_b);
	GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, sep_b);
	GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, len_c);
	GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, 1);
842
	ENSURE_SIZE(buf, len_total);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866

	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;
}

867 868 869
void git_buf_rtrim(git_buf *buf)
{
	while (buf->size > 0) {
870
		if (!git__isspace(buf->ptr[buf->size - 1]))
871 872 873 874
			break;

		buf->size--;
	}
875

876 877
	if (buf->asize > buf->size)
		buf->ptr[buf->size] = '\0';
878
}
879 880 881 882 883 884 885

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;
}
886

887 888 889 890 891 892 893
int git_buf_splice(
	git_buf *buf,
	size_t where,
	size_t nb_to_remove,
	const char *data,
	size_t nb_to_insert)
{
894 895
	char *splice_loc;
	size_t new_size, alloc_size;
896

897 898 899
	GIT_ASSERT(buf);
	GIT_ASSERT(where <= buf->size);
	GIT_ASSERT(nb_to_remove <= buf->size - where);
900 901

	splice_loc = buf->ptr + where;
902 903 904 905

	/* Ported from git.git
	 * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176
	 */
906 907
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, (buf->size - nb_to_remove), nb_to_insert);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, new_size, 1);
908
	ENSURE_SIZE(buf, alloc_size);
909

910 911 912
	memmove(splice_loc + nb_to_insert,
		splice_loc + nb_to_remove,
		buf->size - where - nb_to_remove);
913

914
	memcpy(splice_loc, data, nb_to_insert);
915

916
	buf->size = new_size;
917 918 919
	buf->ptr[buf->size] = '\0';
	return 0;
}
920

921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
/* Quote per http://marc.info/?l=git&m=112927316408690&w=2 */
int git_buf_quote(git_buf *buf)
{
	const char whitespace[] = { 'a', 'b', 't', 'n', 'v', 'f', 'r' };
	git_buf quoted = GIT_BUF_INIT;
	size_t i = 0;
	bool quote = false;
	int error = 0;

	/* walk to the first char that needs quoting */
	if (buf->size && buf->ptr[0] == '!')
		quote = true;

	for (i = 0; !quote && i < buf->size; i++) {
		if (buf->ptr[i] == '"' || buf->ptr[i] == '\\' ||
			buf->ptr[i] < ' ' || buf->ptr[i] > '~') {
			quote = true;
			break;
		}
	}

	if (!quote)
		goto done;

	git_buf_putc(&quoted, '"');
	git_buf_put(&quoted, buf->ptr, i);

	for (; i < buf->size; i++) {
		/* whitespace - use the map above, which is ordered by ascii value */
		if (buf->ptr[i] >= '\a' && buf->ptr[i] <= '\r') {
			git_buf_putc(&quoted, '\\');
			git_buf_putc(&quoted, whitespace[buf->ptr[i] - '\a']);
		}

		/* double quote and backslash must be escaped */
		else if (buf->ptr[i] == '"' || buf->ptr[i] == '\\') {
			git_buf_putc(&quoted, '\\');
			git_buf_putc(&quoted, buf->ptr[i]);
		}

		/* escape anything unprintable as octal */
		else if (buf->ptr[i] != ' ' &&
				(buf->ptr[i] < '!' || buf->ptr[i] > '~')) {
964
			git_buf_printf(&quoted, "\\%03o", (unsigned char)buf->ptr[i]);
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
		}

		/* yay, printable! */
		else {
			git_buf_putc(&quoted, buf->ptr[i]);
		}
	}

	git_buf_putc(&quoted, '"');

	if (git_buf_oom(&quoted)) {
		error = -1;
		goto done;
	}

	git_buf_swap(&quoted, buf);

done:
983
	git_buf_dispose(&quoted);
984 985 986
	return error;
}

987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
/* Unquote per http://marc.info/?l=git&m=112927316408690&w=2 */
int git_buf_unquote(git_buf *buf)
{
	size_t i, j;
	char ch;

	git_buf_rtrim(buf);

	if (buf->size < 2 || buf->ptr[0] != '"' || buf->ptr[buf->size-1] != '"')
		goto invalid;

	for (i = 0, j = 1; j < buf->size-1; i++, j++) {
		ch = buf->ptr[j];

		if (ch == '\\') {
			if (j == buf->size-2)
				goto invalid;

			ch = buf->ptr[++j];

			switch (ch) {
			/* \" or \\ simply copy the char in */
			case '"': case '\\':
				break;

			/* add the appropriate escaped char */
			case 'a': ch = '\a'; break;
			case 'b': ch = '\b'; break;
			case 'f': ch = '\f'; break;
			case 'n': ch = '\n'; break;
			case 'r': ch = '\r'; break;
			case 't': ch = '\t'; break;
			case 'v': ch = '\v'; break;

			/* \xyz digits convert to the char*/
1022
			case '0': case '1': case '2': case '3':
1023
				if (j == buf->size-3) {
1024
					git_error_set(GIT_ERROR_INVALID,
1025
						"truncated quoted character \\%c", ch);
1026 1027 1028 1029 1030
					return -1;
				}

				if (buf->ptr[j+1] < '0' || buf->ptr[j+1] > '7' ||
					buf->ptr[j+2] < '0' || buf->ptr[j+2] > '7') {
1031
					git_error_set(GIT_ERROR_INVALID,
1032
						"truncated quoted character \\%c%c%c",
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
						buf->ptr[j], buf->ptr[j+1], buf->ptr[j+2]);
					return -1;
				}

				ch = ((buf->ptr[j] - '0') << 6) |
					((buf->ptr[j+1] - '0') << 3) |
					(buf->ptr[j+2] - '0');
				j += 2;
				break;

			default:
1044
				git_error_set(GIT_ERROR_INVALID, "invalid quoted character \\%c", ch);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
				return -1;
			}
		}

		buf->ptr[i] = ch;
	}

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

	return 0;

invalid:
1058
	git_error_set(GIT_ERROR_INVALID, "invalid quoted line");
1059 1060
	return -1;
}