str.c 30.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 "str.h"
9
#include "posix.h"
10
#include <ctype.h>
11

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

17
char git_str__oom[1];
18

19
#define ENSURE_SIZE(b, d) \
20 21
	if ((b)->ptr == git_str__oom || \
	    ((d) > (b)->asize && git_str_grow((b), (d)) < 0))\
22
		return -1;
23

24

25
int git_str_init(git_str *buf, size_t initial_size)
26 27 28
{
	buf->asize = 0;
	buf->size = 0;
29
	buf->ptr = git_str__initstr;
30

31 32 33
	ENSURE_SIZE(buf, initial_size);

	return 0;
34 35
}

36 37
int git_str_try_grow(
	git_str *buf, size_t target_size, bool mark_oom)
38
{
39
	char *new_ptr;
40
	size_t new_size;
41

42
	if (buf->ptr == git_str__oom)
43
		return -1;
44

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

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

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

56 57 58 59
	if (buf->asize == 0) {
		new_size = target_size;
		new_ptr = NULL;
	} else {
60
		new_size = buf->asize;
61 62 63 64 65 66 67 68
		/*
		 * 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;
69 70
		new_ptr = buf->ptr;
	}
71

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

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

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

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

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

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

105
	return 0;
106 107
}

108
int git_str_grow(git_str *buffer, size_t target_size)
109
{
110
	return git_str_try_grow(buffer, target_size, true);
111 112
}

113
int git_str_grow_by(git_str *buffer, size_t additional_size)
114
{
115 116 117
	size_t newsize;

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

122
	return git_str_try_grow(buffer, newsize, true);
123 124
}

125
void git_str_dispose(git_str *buf)
126 127 128
{
	if (!buf) return;

129
	if (buf->asize > 0 && buf->ptr != NULL && buf->ptr != git_str__oom)
130 131
		git__free(buf->ptr);

132
	git_str_init(buf, 0);
133 134
}

135
void git_str_clear(git_str *buf)
136 137
{
	buf->size = 0;
138

139
	if (!buf->ptr) {
140
		buf->ptr = git_str__initstr;
141 142
		buf->asize = 0;
	}
143

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

148
int git_str_set(git_str *buf, const void *data, size_t len)
149
{
150 151
	size_t alloclen;

152
	if (len == 0 || data == NULL) {
153
		git_str_clear(buf);
154
	} else {
155
		if (data != buf->ptr) {
156
			GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1);
157
			ENSURE_SIZE(buf, alloclen);
158 159
			memmove(buf->ptr, data, len);
		}
160

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

165
	}
166
	return 0;
167 168
}

169
int git_str_sets(git_str *buf, const char *string)
170
{
171
	return git_str_set(buf, string, string ? strlen(string) : 0);
172 173
}

174
int git_str_putc(git_str *buf, char c)
175
{
176
	size_t new_size;
177
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, 2);
178
	ENSURE_SIZE(buf, new_size);
179
	buf->ptr[buf->size++] = c;
180
	buf->ptr[buf->size] = '\0';
181
	return 0;
182 183
}

184
int git_str_putcn(git_str *buf, char c, size_t len)
185
{
186
	size_t new_size;
187 188
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len);
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
189
	ENSURE_SIZE(buf, new_size);
190 191 192 193 194 195
	memset(buf->ptr + buf->size, c, len);
	buf->size += len;
	buf->ptr[buf->size] = '\0';
	return 0;
}

196
int git_str_put(git_str *buf, const char *data, size_t len)
197
{
198
	if (len) {
199 200
		size_t new_size;

201
		GIT_ASSERT_ARG(data);
202

203 204
		GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len);
		GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
205
		ENSURE_SIZE(buf, new_size);
206 207 208 209
		memmove(buf->ptr + buf->size, data, len);
		buf->size += len;
		buf->ptr[buf->size] = '\0';
	}
210
	return 0;
211 212
}

213
int git_str_puts(git_str *buf, const char *string)
214
{
215 216
	GIT_ASSERT_ARG(string);

217
	return git_str_put(buf, string, strlen(string));
218 219
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
static char hex_encode[] = "0123456789abcdef";

int git_str_encode_hexstr(git_str *str, const char *data, size_t len)
{
	size_t new_size, i;
	char *s;

	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&new_size, len, 2);
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);

	if (git_str_grow_by(str, new_size) < 0)
		return -1;

	s = str->ptr + str->size;

	for (i = 0; i < len; i++) {
		*s++ = hex_encode[(data[i] & 0xf0) >> 4];
		*s++ = hex_encode[(data[i] & 0x0f)];
	}

	str->size += (len * 2);
	str->ptr[str->size] = '\0';

	return 0;
}

246
static const char base64_encode[] =
247 248
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

249
int git_str_encode_base64(git_str *buf, const char *data, size_t len)
250 251 252 253
{
	size_t extra = len % 3;
	uint8_t *write, a, b, c;
	const uint8_t *read = (const uint8_t *)data;
254 255
	size_t blocks = (len / 3) + !!extra, alloclen;

256 257 258
	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);
259

260
	ENSURE_SIZE(buf, alloclen);
261 262 263 264 265 266 267 268
	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++;

269 270 271 272
		*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];
273 274 275 276 277 278
	}

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

279 280 281
		*write++ = base64_encode[a >> 2];
		*write++ = base64_encode[(a & 0x03) << 4 | b >> 4];
		*write++ = (extra > 1) ? base64_encode[(b & 0x0f) << 2] : '=';
282 283 284 285 286 287 288 289 290
		*write++ = '=';
	}

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

	return 0;
}

291
/* The inverse of base64_encode */
292
static const int8_t base64_decode[] = {
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	-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
309 310
};

311
int git_str_decode_base64(git_str *buf, const char *base64, size_t len)
312 313 314
{
	size_t i;
	int8_t a, b, c, d;
315
	size_t orig_size = buf->size, new_size;
316

317
	if (len % 4) {
318
		git_error_set(GIT_ERROR_INVALID, "invalid base64 input");
319 320 321
		return -1;
	}

322
	GIT_ASSERT_ARG(len % 4 == 0);
323 324
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, (len / 4 * 3), buf->size);
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
325
	ENSURE_SIZE(buf, new_size);
326 327

	for (i = 0; i < len; i += 4) {
328 329 330 331
		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) {
332 333 334
			buf->size = orig_size;
			buf->ptr[buf->size] = '\0';

335
			git_error_set(GIT_ERROR_INVALID, "invalid base64 input");
336 337 338 339 340 341 342 343 344 345 346 347
			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;
}

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

351
int git_str_encode_base85(git_str *buf, const char *data, size_t len)
352
{
353
	size_t blocks = (len / 4) + !!(len % 4), alloclen;
354

355 356 357
	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);
358 359

	ENSURE_SIZE(buf, alloclen);
360 361 362 363 364 365 366 367

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

		for (i = 24; i >= 0; i -= 8) {
			uint8_t ch = *data++;
368
			acc |= (uint32_t)ch << i;
369 370 371 372 373 374 375 376 377

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

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

378
			b85[i] = base85_encode[val];
379 380 381 382 383 384 385 386 387 388 389
		}

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

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

	return 0;
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
/* 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
};

410 411
int git_str_decode_base85(
	git_str *buf,
412 413 414 415 416 417 418 419
	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) {
420
		git_error_set(GIT_ERROR_INVALID, "invalid base85 input");
421 422 423
		return -1;
	}

424 425
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, output_len, buf->size);
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	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;

452
		cnt = (output_len < 4) ? (int)output_len : 4;
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		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';

468
	git_error_set(GIT_ERROR_INVALID, "invalid base85 input");
469 470 471
	return -1;
}

472 473
#define HEX_DECODE(c) ((c | 32) % 39 - 9)

474 475
int git_str_decode_percent(
	git_str *buf,
476 477 478 479 480
	const char *str,
	size_t str_len)
{
	size_t str_pos, new_size;

481 482
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, str_len);
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	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;
}

502
int git_str_vprintf(git_str *buf, const char *format, va_list ap)
503
{
504
	size_t expected_size, new_size;
505
	int len;
506

507 508
	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&expected_size, strlen(format), 2);
	GIT_ERROR_CHECK_ALLOC_ADD(&expected_size, expected_size, buf->size);
509
	ENSURE_SIZE(buf, expected_size);
510 511

	while (1) {
512 513 514 515 516 517 518 519
		va_list args;
		va_copy(args, ap);

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

521 522
		va_end(args);

523
		if (len < 0) {
524
			git__free(buf->ptr);
525
			buf->ptr = git_str__oom;
526
			return -1;
527 528
		}

529
		if ((size_t)len + 1 <= buf->asize - buf->size) {
530
			buf->size += len;
531
			break;
532 533
		}

534 535
		GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len);
		GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1);
536
		ENSURE_SIZE(buf, new_size);
537
	}
538

539
	return 0;
540 541
}

542
int git_str_printf(git_str *buf, const char *format, ...)
543 544 545 546 547
{
	int r;
	va_list ap;

	va_start(ap, format);
548
	r = git_str_vprintf(buf, format, ap);
549 550 551 552 553
	va_end(ap);

	return r;
}

554
int git_str_copy_cstr(char *data, size_t datasize, const git_str *buf)
555 556 557
{
	size_t copylen;

558 559 560
	GIT_ASSERT_ARG(data);
	GIT_ASSERT_ARG(datasize);
	GIT_ASSERT_ARG(buf);
561 562 563 564

	data[0] = '\0';

	if (buf->size == 0 || buf->asize <= 0)
565
		return 0;
566 567 568 569 570 571

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

	return 0;
574 575
}

576
void git_str_consume_bytes(git_str *buf, size_t len)
577
{
578
	git_str_consume(buf, buf->ptr + len);
579 580
}

581
void git_str_consume(git_str *buf, const char *end)
582
{
583 584 585 586
	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;
587
		buf->ptr[buf->size] = '\0';
588 589 590
	}
}

591
void git_str_truncate(git_str *buf, size_t len)
592
{
593 594 595 596 597
	if (len >= buf->size)
		return;

	buf->size = len;
	if (buf->size < buf->asize)
598 599 600
		buf->ptr[buf->size] = '\0';
}

601
void git_str_shorten(git_str *buf, size_t amount)
602
{
603
	if (buf->size > amount)
604
		git_str_truncate(buf, buf->size - amount);
605
	else
606
		git_str_clear(buf);
607 608
}

609
void git_str_truncate_at_char(git_str *buf, char separator)
610
{
611
	ssize_t idx = git_str_find(buf, separator);
612
	if (idx >= 0)
613
		git_str_truncate(buf, (size_t)idx);
614 615
}

616
void git_str_rtruncate_at_char(git_str *buf, char separator)
617
{
618 619
	ssize_t idx = git_str_rfind_next(buf, separator);
	git_str_truncate(buf, idx < 0 ? 0 : (size_t)idx);
620 621
}

622
void git_str_swap(git_str *str_a, git_str *str_b)
623
{
624 625 626
	git_str t = *str_a;
	*str_a = *str_b;
	*str_b = t;
627
}
628

629
char *git_str_detach(git_str *buf)
630
{
631
	char *data = buf->ptr;
632

633
	if (buf->asize == 0 || buf->ptr == git_str__oom)
634 635
		return NULL;

636
	git_str_init(buf, 0);
637 638 639 640

	return data;
}

641
int git_str_attach(git_str *buf, char *ptr, size_t asize)
642
{
643
	git_str_dispose(buf);
644 645 646 647 648 649 650 651 652

	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;
	}
653 654 655

	ENSURE_SIZE(buf, asize);
	return 0;
656
}
657

658
void git_str_attach_notowned(git_str *buf, const char *ptr, size_t size)
659
{
660 661
	if (git_str_is_allocated(buf))
		git_str_dispose(buf);
662 663

	if (!size) {
664
		git_str_init(buf, 0);
665 666 667 668 669 670 671
	} else {
		buf->ptr = (char *)ptr;
		buf->asize = 0;
		buf->size = size;
	}
}

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

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

682 683
	/* Make two passes to avoid multiple reallocation */

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

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

		segment_len = strlen(segment);
694

695
		GIT_ERROR_CHECK_ALLOC_ADD(&total_size, total_size, segment_len);
696

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

702
	/* expand buffer if needed */
703 704
	if (total_size == 0)
		return 0;
705

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

	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) {
718
		const char *segment;
719
		size_t segment_len;
720 721 722 723 724

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

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

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

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

757
	return 0;
758 759
}

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

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

	/* 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;
781 782
	}

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

787 788 789
	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);
790
	ENSURE_SIZE(buf, alloc_len);
791

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

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

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

806
	return 0;
807
}
808

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

	/* for this function, disallow pointers into the existing buffer */
824 825 826
	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);
827 828 829 830 831 832 833 834 835 836 837 838

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

839 840 841 842 843
	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);
844
	ENSURE_SIZE(buf, len_total);
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868

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

869
void git_str_rtrim(git_str *buf)
870 871
{
	while (buf->size > 0) {
872
		if (!git__isspace(buf->ptr[buf->size - 1]))
873 874 875 876
			break;

		buf->size--;
	}
877

878 879
	if (buf->asize > buf->size)
		buf->ptr[buf->size] = '\0';
880
}
881

882
int git_str_cmp(const git_str *a, const git_str *b)
883 884 885 886 887
{
	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;
}
888

889 890
int git_str_splice(
	git_str *buf,
891 892 893 894 895
	size_t where,
	size_t nb_to_remove,
	const char *data,
	size_t nb_to_insert)
{
896 897
	char *splice_loc;
	size_t new_size, alloc_size;
898

899 900 901
	GIT_ASSERT(buf);
	GIT_ASSERT(where <= buf->size);
	GIT_ASSERT(nb_to_remove <= buf->size - where);
902 903

	splice_loc = buf->ptr + where;
904 905 906 907

	/* Ported from git.git
	 * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176
	 */
908 909
	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);
910
	ENSURE_SIZE(buf, alloc_size);
911

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

916
	memcpy(splice_loc, data, nb_to_insert);
917

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

923
/* Quote per http://marc.info/?l=git&m=112927316408690&w=2 */
924
int git_str_quote(git_str *buf)
925 926
{
	const char whitespace[] = { 'a', 'b', 't', 'n', 'v', 'f', 'r' };
927
	git_str quoted = GIT_STR_INIT;
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
	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;

947 948
	git_str_putc(&quoted, '"');
	git_str_put(&quoted, buf->ptr, i);
949 950 951 952

	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') {
953 954
			git_str_putc(&quoted, '\\');
			git_str_putc(&quoted, whitespace[buf->ptr[i] - '\a']);
955 956 957 958
		}

		/* double quote and backslash must be escaped */
		else if (buf->ptr[i] == '"' || buf->ptr[i] == '\\') {
959 960
			git_str_putc(&quoted, '\\');
			git_str_putc(&quoted, buf->ptr[i]);
961 962 963 964 965
		}

		/* escape anything unprintable as octal */
		else if (buf->ptr[i] != ' ' &&
				(buf->ptr[i] < '!' || buf->ptr[i] > '~')) {
966
			git_str_printf(&quoted, "\\%03o", (unsigned char)buf->ptr[i]);
967 968 969 970
		}

		/* yay, printable! */
		else {
971
			git_str_putc(&quoted, buf->ptr[i]);
972 973 974
		}
	}

975
	git_str_putc(&quoted, '"');
976

977
	if (git_str_oom(&quoted)) {
978 979 980 981
		error = -1;
		goto done;
	}

982
	git_str_swap(&quoted, buf);
983 984

done:
985
	git_str_dispose(&quoted);
986 987 988
	return error;
}

989
/* Unquote per http://marc.info/?l=git&m=112927316408690&w=2 */
990
int git_str_unquote(git_str *buf)
991 992 993 994
{
	size_t i, j;
	char ch;

995
	git_str_rtrim(buf);
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 1022 1023

	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*/
1024
			case '0': case '1': case '2': case '3':
1025
				if (j == buf->size-3) {
1026
					git_error_set(GIT_ERROR_INVALID,
1027
						"truncated quoted character \\%c", ch);
1028 1029 1030 1031 1032
					return -1;
				}

				if (buf->ptr[j+1] < '0' || buf->ptr[j+1] > '7' ||
					buf->ptr[j+2] < '0' || buf->ptr[j+2] > '7') {
1033
					git_error_set(GIT_ERROR_INVALID,
1034
						"truncated quoted character \\%c%c%c",
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
						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:
1046
				git_error_set(GIT_ERROR_INVALID, "invalid quoted character \\%c", ch);
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
				return -1;
			}
		}

		buf->ptr[i] = ch;
	}

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

	return 0;

invalid:
1060
	git_error_set(GIT_ERROR_INVALID, "invalid quoted line");
1061 1062
	return -1;
}
1063

1064 1065
int git_str_puts_escaped(
	git_str *buf,
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
	const char *string,
	const char *esc_chars,
	const char *esc_with)
{
	const char *scan;
	size_t total = 0, esc_len = strlen(esc_with), count, alloclen;

	if (!string)
		return 0;

	for (scan = string; *scan; ) {
		/* count run of non-escaped characters */
		count = strcspn(scan, esc_chars);
		total += count;
		scan += count;
		/* count run of escaped characters */
		count = strspn(scan, esc_chars);
		total += count * (esc_len + 1);
		scan += count;
	}

	GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, total, 1);
1088
	if (git_str_grow_by(buf, alloclen) < 0)
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
		return -1;

	for (scan = string; *scan; ) {
		count = strcspn(scan, esc_chars);

		memmove(buf->ptr + buf->size, scan, count);
		scan += count;
		buf->size += count;

		for (count = strspn(scan, esc_chars); count > 0; --count) {
			/* copy escape sequence */
			memmove(buf->ptr + buf->size, esc_with, esc_len);
			buf->size += esc_len;
			/* copy character to be escaped */
			buf->ptr[buf->size] = *scan;
			buf->size++;
			scan++;
		}
	}

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

	return 0;
}

1114
void git_str_unescape(git_str *buf)
1115 1116 1117 1118
{
	buf->size = git__unescape(buf->ptr);
}

1119
int git_str_crlf_to_lf(git_str *tgt, const git_str *src)
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
{
	const char *scan = src->ptr;
	const char *scan_end = src->ptr + src->size;
	const char *next = memchr(scan, '\r', src->size);
	size_t new_size;
	char *out;

	GIT_ASSERT(tgt != src);

	if (!next)
1130
		return git_str_set(tgt, src->ptr, src->size);
1131 1132 1133

	/* reduce reallocs while in the loop */
	GIT_ERROR_CHECK_ALLOC_ADD(&new_size, src->size, 1);
1134
	if (git_str_grow(tgt, new_size) < 0)
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
		return -1;

	out = tgt->ptr;
	tgt->size = 0;

	/* Find the next \r and copy whole chunk up to there to tgt */
	for (; next; scan = next + 1, next = memchr(scan, '\r', scan_end - scan)) {
		if (next > scan) {
			size_t copylen = (size_t)(next - scan);
			memcpy(out, scan, copylen);
			out += copylen;
		}

		/* Do not drop \r unless it is followed by \n */
		if (next + 1 == scan_end || next[1] != '\n')
			*out++ = '\r';
	}

	/* Copy remaining input into dest */
	if (scan < scan_end) {
		size_t remaining = (size_t)(scan_end - scan);
		memcpy(out, scan, remaining);
		out += remaining;
	}

	tgt->size = (size_t)(out - tgt->ptr);
	tgt->ptr[tgt->size] = '\0';

	return 0;
}

1166
int git_str_lf_to_crlf(git_str *tgt, const git_str *src)
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
{
	const char *start = src->ptr;
	const char *end = start + src->size;
	const char *scan = start;
	const char *next = memchr(scan, '\n', src->size);
	size_t alloclen;

	GIT_ASSERT(tgt != src);

	if (!next)
1177
		return git_str_set(tgt, src->ptr, src->size);
1178 1179 1180 1181

	/* attempt to reduce reallocs while in the loop */
	GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, src->size, src->size >> 4);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1);
1182
	if (git_str_grow(tgt, alloclen) < 0)
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
		return -1;
	tgt->size = 0;

	for (; next; scan = next + 1, next = memchr(scan, '\n', end - scan)) {
		size_t copylen = next - scan;

		/* if we find mixed line endings, carry on */
		if (copylen && next[-1] == '\r')
			copylen--;

		GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, copylen, 3);
1194
		if (git_str_grow_by(tgt, alloclen) < 0)
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
			return -1;

		if (copylen) {
			memcpy(tgt->ptr + tgt->size, scan, copylen);
			tgt->size += copylen;
		}

		tgt->ptr[tgt->size++] = '\r';
		tgt->ptr[tgt->size++] = '\n';
	}

	tgt->ptr[tgt->size] = '\0';
1207
	return git_str_put(tgt, scan, end - scan);
1208 1209
}

1210
int git_str_common_prefix(git_str *buf, char *const *const strings, size_t count)
1211 1212 1213 1214
{
	size_t i;
	const char *str, *pfx;

1215
	git_str_clear(buf);
1216

1217
	if (!strings || !count)
1218 1219 1220
		return 0;

	/* initialize common prefix to first string */
1221
	if (git_str_sets(buf, strings[0]) < 0)
1222 1223 1224
		return -1;

	/* go through the rest of the strings, truncating to shared prefix */
1225
	for (i = 1; i < count; ++i) {
1226

1227 1228 1229
		for (str = strings[i], pfx = buf->ptr;
		     *str && *str == *pfx;
		     str++, pfx++)
1230 1231
			/* scanning */;

1232
		git_str_truncate(buf, pfx - buf->ptr);
1233 1234 1235 1236 1237 1238 1239 1240

		if (!buf->size)
			break;
	}

	return 0;
}

1241
int git_str_is_binary(const git_str *buf)
1242 1243
{
	const char *scan = buf->ptr, *end = buf->ptr + buf->size;
1244
	git_str_bom_t bom;
1245 1246
	int printable = 0, nonprintable = 0;

1247
	scan += git_str_detect_bom(&bom, buf);
1248

1249
	if (bom > GIT_STR_BOM_UTF8)
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
		return 1;

	while (scan < end) {
		unsigned char c = *scan++;

		/* Printable characters are those above SPACE (0x1F) excluding DEL,
		 * and including BS, ESC and FF.
		 */
		if ((c > 0x1F && c != 127) || c == '\b' || c == '\033' || c == '\014')
			printable++;
		else if (c == '\0')
			return true;
		else if (!git__isspace(c))
			nonprintable++;
	}

	return ((printable >> 7) < nonprintable);
}

1269
int git_str_contains_nul(const git_str *buf)
1270 1271 1272 1273
{
	return (memchr(buf->ptr, '\0', buf->size) != NULL);
}

1274
int git_str_detect_bom(git_str_bom_t *bom, const git_str *buf)
1275 1276 1277 1278
{
	const char *ptr;
	size_t len;

1279
	*bom = GIT_STR_BOM_NONE;
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	/* need at least 2 bytes to look for any BOM */
	if (buf->size < 2)
		return 0;

	ptr = buf->ptr;
	len = buf->size;

	switch (*ptr++) {
	case 0:
		if (len >= 4 && ptr[0] == 0 && ptr[1] == '\xFE' && ptr[2] == '\xFF') {
1290
			*bom = GIT_STR_BOM_UTF32_BE;
1291 1292 1293 1294 1295
			return 4;
		}
		break;
	case '\xEF':
		if (len >= 3 && ptr[0] == '\xBB' && ptr[1] == '\xBF') {
1296
			*bom = GIT_STR_BOM_UTF8;
1297 1298 1299 1300 1301
			return 3;
		}
		break;
	case '\xFE':
		if (*ptr == '\xFF') {
1302
			*bom = GIT_STR_BOM_UTF16_BE;
1303 1304 1305 1306 1307 1308 1309
			return 2;
		}
		break;
	case '\xFF':
		if (*ptr != '\xFE')
			break;
		if (len >= 4 && ptr[1] == 0 && ptr[2] == 0) {
1310
			*bom = GIT_STR_BOM_UTF32_LE;
1311 1312
			return 4;
		} else {
1313
			*bom = GIT_STR_BOM_UTF16_LE;
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
			return 2;
		}
		break;
	default:
		break;
	}

	return 0;
}

1324 1325
bool git_str_gather_text_stats(
	git_str_text_stats *stats, const git_str *buf, bool skip_bom)
1326 1327 1328 1329 1330 1331 1332
{
	const char *scan = buf->ptr, *end = buf->ptr + buf->size;
	int skip;

	memset(stats, 0, sizeof(*stats));

	/* BOM detection */
1333
	skip = git_str_detect_bom(&stats->bom, buf);
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
	if (skip_bom)
		scan += skip;

	/* Ignore EOF character */
	if (buf->size > 0 && end[-1] == '\032')
		end--;

	/* Counting loop */
	while (scan < end) {
		unsigned char c = *scan++;

		if (c > 0x1F && c != 0x7F)
			stats->printable++;
		else switch (c) {
			case '\0':
				stats->nul++;
				stats->nonprintable++;
				break;
			case '\n':
				stats->lf++;
				break;
			case '\r':
				stats->cr++;
				if (scan < end && *scan == '\n')
					stats->crlf++;
				break;
			case '\t': case '\f': case '\v': case '\b': case 0x1b: /*ESC*/
				stats->printable++;
				break;
			default:
				stats->nonprintable++;
				break;
			}
	}

	/* Treat files with a bare CR as binary */
	return (stats->cr != stats->crlf || stats->nul > 0 ||
		((stats->printable >> 7) < stats->nonprintable));
}