buffer.c 10.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"
10
#include <stdarg.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 36
int git_buf_try_grow(
	git_buf *buf, size_t target_size, bool mark_oom, bool preserve_external)
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
	if (!target_size)
		target_size = buf->size;

47
	if (target_size <= buf->asize)
48
		return 0;
49

50 51 52 53
	if (buf->asize == 0) {
		new_size = target_size;
		new_ptr = NULL;
	} else {
54
		new_size = buf->asize;
55 56
		new_ptr = buf->ptr;
	}
57 58 59

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

63
	/* round allocation up to multiple of 8 */
64
	new_size = (new_size + 7) & ~7;
65

66
	new_ptr = git__realloc(new_ptr, new_size);
67 68 69 70

	if (!new_ptr) {
		if (mark_oom)
			buf->ptr = git_buf__oom;
71
		return -1;
72
	}
73

74 75 76
	if (preserve_external && !buf->asize && buf->ptr != NULL && buf->size > 0)
		memcpy(new_ptr, buf->ptr, min(buf->size, new_size));

77 78 79 80 81 82 83 84
	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';

85
	return 0;
86 87
}

88 89 90 91 92
int git_buf_grow(git_buf *buffer, size_t target_size)
{
	return git_buf_try_grow(buffer, target_size, true, true);
}

93 94 95 96
void git_buf_free(git_buf *buf)
{
	if (!buf) return;

97
	if (buf->asize > 0 && buf->ptr != NULL && buf->ptr != git_buf__oom)
98 99 100 101 102 103 104 105
		git__free(buf->ptr);

	git_buf_init(buf, 0);
}

void git_buf_clear(git_buf *buf)
{
	buf->size = 0;
106 107 108 109

	if (!buf->ptr)
		buf->ptr = git_buf__initbuf;

110 111 112 113
	if (buf->asize > 0)
		buf->ptr[0] = '\0';
}

114
int git_buf_set(git_buf *buf, const void *data, size_t len)
115 116 117 118
{
	if (len == 0 || data == NULL) {
		git_buf_clear(buf);
	} else {
119 120 121 122
		if (data != buf->ptr) {
			ENSURE_SIZE(buf, len + 1);
			memmove(buf->ptr, data, len);
		}
123
		buf->size = len;
124
		buf->ptr[buf->size] = '\0';
125
	}
126
	return 0;
127 128
}

129
int git_buf_sets(git_buf *buf, const char *string)
130
{
131
	return git_buf_set(buf, string, string ? strlen(string) : 0);
132 133
}

134
int git_buf_putc(git_buf *buf, char c)
135
{
136
	ENSURE_SIZE(buf, buf->size + 2);
137
	buf->ptr[buf->size++] = c;
138
	buf->ptr[buf->size] = '\0';
139
	return 0;
140 141
}

142
int git_buf_put(git_buf *buf, const char *data, size_t len)
143
{
144
	ENSURE_SIZE(buf, buf->size + len + 1);
145
	memmove(buf->ptr + buf->size, data, len);
146
	buf->size += len;
147
	buf->ptr[buf->size] = '\0';
148
	return 0;
149 150
}

151
int git_buf_puts(git_buf *buf, const char *string)
152
{
153
	assert(string);
154
	return git_buf_put(buf, string, strlen(string));
155 156
}

Linquize committed
157
static const char b64str[] =
158 159 160 161 162 163 164 165
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int git_buf_put_base64(git_buf *buf, const char *data, size_t len)
{
	size_t extra = len % 3;
	uint8_t *write, a, b, c;
	const uint8_t *read = (const uint8_t *)data;

166
	ENSURE_SIZE(buf, buf->size + 4 * ((len / 3) + !!extra) + 1);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	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++;

		*write++ = b64str[a >> 2];
		*write++ = b64str[(a & 0x03) << 4 | b >> 4];
		*write++ = b64str[(b & 0x0f) << 2 | c >> 6];
		*write++ = b64str[c & 0x3f];
	}

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

		*write++ = b64str[a >> 2];
		*write++ = b64str[(a & 0x03) << 4 | b >> 4];
		*write++ = (extra > 1) ? b64str[(b & 0x0f) << 2] : '=';
		*write++ = '=';
	}

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

	return 0;
}

197
int git_buf_vprintf(git_buf *buf, const char *format, va_list ap)
198 199
{
	int len;
200
	const size_t expected_size = buf->size + (strlen(format) * 2);
201

202
	ENSURE_SIZE(buf, expected_size);
203 204

	while (1) {
205 206 207 208 209 210 211 212
		va_list args;
		va_copy(args, ap);

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

214 215
		va_end(args);

216
		if (len < 0) {
217
			git__free(buf->ptr);
218
			buf->ptr = git_buf__oom;
219
			return -1;
220 221
		}

222
		if ((size_t)len + 1 <= buf->asize - buf->size) {
223
			buf->size += len;
224
			break;
225 226 227 228
		}

		ENSURE_SIZE(buf, buf->size + len + 1);
	}
229

230
	return 0;
231 232
}

233 234 235 236 237 238 239 240 241 242 243 244
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;
}

245
void git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf)
246 247 248
{
	size_t copylen;

249
	assert(data && datasize && buf);
250 251 252 253 254 255 256 257 258 259 260

	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';
261 262
}

263 264
void git_buf_consume(git_buf *buf, const char *end)
{
265 266 267 268
	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;
269
		buf->ptr[buf->size] = '\0';
270 271 272
	}
}

273
void git_buf_truncate(git_buf *buf, size_t len)
274
{
275
	if (len < buf->size) {
276 277 278 279 280
		buf->size = len;
		buf->ptr[buf->size] = '\0';
	}
}

281 282 283 284 285 286 287 288 289
void git_buf_shorten(git_buf *buf, size_t amount)
{
	if (amount > buf->size)
		amount = buf->size;

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

290 291
void git_buf_rtruncate_at_char(git_buf *buf, char separator)
{
292 293
	ssize_t idx = git_buf_rfind_next(buf, separator);
	git_buf_truncate(buf, idx < 0 ? 0 : (size_t)idx);
294 295
}

296 297 298 299 300
void git_buf_swap(git_buf *buf_a, git_buf *buf_b)
{
	git_buf t = *buf_a;
	*buf_a = *buf_b;
	*buf_b = t;
301
}
302

303
char *git_buf_detach(git_buf *buf)
304
{
305
	char *data = buf->ptr;
306

307
	if (buf->asize == 0 || buf->ptr == git_buf__oom)
308 309
		return NULL;

310
	git_buf_init(buf, 0);
311 312 313 314

	return data;
}

315
void git_buf_attach(git_buf *buf, char *ptr, size_t asize)
316
{
317 318 319 320 321 322 323 324 325 326 327 328 329
	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);
	}
}
330

331 332
int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...)
{
333
	va_list ap;
334
	int i;
335 336
	size_t total_size = 0, original_size = buf->size;
	char *out, *original = buf->ptr;
337 338 339 340

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

341 342
	/* Make two passes to avoid multiple reallocation */

343 344 345
	va_start(ap, nbuf);
	for (i = 0; i < nbuf; ++i) {
		const char* segment;
346
		size_t segment_len;
347 348 349 350 351 352 353 354 355 356 357 358

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

		segment_len = strlen(segment);
		total_size += segment_len;
		if (segment_len == 0 || segment[segment_len - 1] != separator)
			++total_size; /* space for separator */
	}
	va_end(ap);

359
	/* expand buffer if needed */
360 361 362
	if (total_size == 0)
		return 0;
	if (git_buf_grow(buf, buf->size + total_size + 1) < 0)
363
		return -1;
364 365 366 367 368 369 370 371 372 373

	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;
374
		size_t segment_len;
375 376 377 378 379

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

380 381 382 383 384 385 386 387 388
		/* 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);
		}

389 390
		/* skip leading separators */
		if (out > buf->ptr && out[-1] == separator)
391 392 393 394
			while (segment_len > 0 && *segment == separator) {
				segment++;
				segment_len--;
			}
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

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

412
	return 0;
413 414
}

415
int git_buf_join(
416 417 418 419 420
	git_buf *buf,
	char separator,
	const char *str_a,
	const char *str_b)
{
421
	size_t strlen_a = str_a ? strlen(str_a) : 0;
422 423
	size_t strlen_b = strlen(str_b);
	int need_sep = 0;
424 425 426 427
	ssize_t offset_a = -1;

	/* not safe to have str_b point internally to the buffer */
	assert(str_b < buf->ptr || str_b > buf->ptr + buf->size);
428 429 430 431 432 433

	/* 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;
434 435
	}

436 437 438 439
	/* 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;

440 441
	if (git_buf_grow(buf, strlen_a + strlen_b + need_sep + 1) < 0)
		return -1;
442

443 444 445 446 447 448 449
	/* fix up internal pointers */
	if (offset_a >= 0)
		str_a = buf->ptr + offset_a;

	/* do the actual copying */
	if (offset_a != 0)
		memmove(buf->ptr, str_a, strlen_a);
450 451
	if (need_sep)
		buf->ptr[strlen_a] = separator;
452
	memcpy(buf->ptr + strlen_a + need_sep, str_b, strlen_b);
453

454
	buf->size = strlen_a + strlen_b + need_sep;
455
	buf->ptr[buf->size] = '\0';
456

457
	return 0;
458
}
459 460 461 462

void git_buf_rtrim(git_buf *buf)
{
	while (buf->size > 0) {
463
		if (!git__isspace(buf->ptr[buf->size - 1]))
464 465 466 467
			break;

		buf->size--;
	}
468 469

	buf->ptr[buf->size] = '\0';
470
}
471 472 473 474 475 476 477

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

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
int git_buf_splice(
	git_buf *buf,
	size_t where,
	size_t nb_to_remove,
	const char *data,
	size_t nb_to_insert)
{
	assert(buf &&
		where <= git_buf_len(buf) &&
		where + nb_to_remove <= git_buf_len(buf));

	/* Ported from git.git
	 * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176
	 */
	if (git_buf_grow(buf, git_buf_len(buf) + nb_to_insert - nb_to_remove) < 0)
		return -1;

	memmove(buf->ptr + where + nb_to_insert,
			buf->ptr + where + nb_to_remove,
			buf->size - where - nb_to_remove);

	memcpy(buf->ptr + where, data, nb_to_insert);

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