Blame src/buffer.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_buffer_h__
Packit Service 20376f
#define INCLUDE_buffer_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "git2/strarray.h"
Packit Service 20376f
#include "git2/buffer.h"
Packit Service 20376f
Packit Service 20376f
/* typedef struct {
Packit Service 20376f
 *  	char   *ptr;
Packit Service 20376f
 *  	size_t asize, size;
Packit Service 20376f
 * } git_buf;
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
extern char git_buf__initbuf[];
Packit Service 20376f
extern char git_buf__oom[];
Packit Service 20376f
Packit Service 20376f
/* Use to initialize buffer structure when git_buf is on stack */
Packit Service 20376f
#define GIT_BUF_INIT { git_buf__initbuf, 0, 0 }
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) git_buf_is_allocated(const git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	return (buf->ptr != NULL && buf->asize > 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Initialize a git_buf structure.
Packit Service 20376f
 *
Packit Service 20376f
 * For the cases where GIT_BUF_INIT cannot be used to do static
Packit Service 20376f
 * initialization.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_buf_init(git_buf *buf, size_t initial_size);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Resize the buffer allocation to make more space.
Packit Service 20376f
 *
Packit Service 20376f
 * This will attempt to grow the buffer to accommodate the additional size.
Packit Service 20376f
 * It is similar to `git_buf_grow`, but performs the new size calculation,
Packit Service 20376f
 * checking for overflow.
Packit Service 20376f
 *
Packit Service 20376f
 * Like `git_buf_grow`, if this is a user-supplied buffer, this will allocate
Packit Service 20376f
 * a new buffer.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_buf_grow_by(git_buf *buffer, size_t additional_size);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Attempt to grow the buffer to hold at least `target_size` bytes.
Packit Service 20376f
 *
Packit Service 20376f
 * If the allocation fails, this will return an error.  If `mark_oom` is true,
Packit Service 20376f
 * this will mark the buffer as invalid for future operations; if false,
Packit Service 20376f
 * existing buffer content will be preserved, but calling code must handle
Packit Service 20376f
 * that buffer was not expanded.  If `preserve_external` is true, then any
Packit Service 20376f
 * existing data pointed to be `ptr` even if `asize` is zero will be copied
Packit Service 20376f
 * into the newly allocated buffer.
Packit Service 20376f
 */
Packit Service 20376f
extern int git_buf_try_grow(
Packit Service 20376f
	git_buf *buf, size_t target_size, bool mark_oom);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Sanitizes git_buf structures provided from user input.  Users of the
Packit Service 20376f
 * library, when providing git_buf's, may wish to provide a NULL ptr for
Packit Service 20376f
 * ease of handling.  The buffer routines, however, expect a non-NULL ptr
Packit Service 20376f
 * always.  This helper method simply handles NULL input, converting to a
Packit Service 20376f
 * git_buf__initbuf. If a buffer with a non-NULL ptr is passed in, this method
Packit Service 20376f
 * assures that the buffer is '\0'-terminated.
Packit Service 20376f
 */
Packit Service 20376f
extern void git_buf_sanitize(git_buf *buf);
Packit Service 20376f
Packit Service 20376f
extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
Packit Service 20376f
extern char *git_buf_detach(git_buf *buf);
Packit Service 20376f
extern int git_buf_attach(git_buf *buf, char *ptr, size_t asize);
Packit Service 20376f
Packit Service 20376f
/* Populates a `git_buf` where the contents are not "owned" by the
Packit Service 20376f
 * buffer, and calls to `git_buf_free` will not free the given buf.
Packit Service 20376f
 */
Packit Service 20376f
extern void git_buf_attach_notowned(
Packit Service 20376f
	git_buf *buf, const char *ptr, size_t size);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Test if there have been any reallocation failures with this git_buf.
Packit Service 20376f
 *
Packit Service 20376f
 * Any function that writes to a git_buf can fail due to memory allocation
Packit Service 20376f
 * issues.  If one fails, the git_buf will be marked with an OOM error and
Packit Service 20376f
 * further calls to modify the buffer will fail.  Check git_buf_oom() at the
Packit Service 20376f
 * end of your sequence and it will be true if you ran out of memory at any
Packit Service 20376f
 * point with that buffer.
Packit Service 20376f
 *
Packit Service 20376f
 * @return false if no error, true if allocation error
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(bool) git_buf_oom(const git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	return (buf->ptr == git_buf__oom);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Functions below that return int value error codes will return 0 on
Packit Service 20376f
 * success or -1 on failure (which generally means an allocation failed).
Packit Service 20376f
 * Using a git_buf where the allocation has failed with result in -1 from
Packit Service 20376f
 * all further calls using that buffer.  As a result, you can ignore the
Packit Service 20376f
 * return code of these functions and call them in a series then just call
Packit Service 20376f
 * git_buf_oom at the end.
Packit Service 20376f
 */
Packit Service 20376f
int git_buf_sets(git_buf *buf, const char *string);
Packit Service 20376f
int git_buf_putc(git_buf *buf, char c);
Packit Service 20376f
int git_buf_putcn(git_buf *buf, char c, size_t len);
Packit Service 20376f
int git_buf_put(git_buf *buf, const char *data, size_t len);
Packit Service 20376f
int git_buf_puts(git_buf *buf, const char *string);
Packit Service 20376f
int git_buf_printf(git_buf *buf, const char *format, ...) GIT_FORMAT_PRINTF(2, 3);
Packit Service 20376f
int git_buf_vprintf(git_buf *buf, const char *format, va_list ap);
Packit Service 20376f
void git_buf_clear(git_buf *buf);
Packit Service 20376f
void git_buf_consume(git_buf *buf, const char *end);
Packit Service 20376f
void git_buf_truncate(git_buf *buf, size_t len);
Packit Service 20376f
void git_buf_shorten(git_buf *buf, size_t amount);
Packit Service 20376f
void git_buf_rtruncate_at_char(git_buf *path, char separator);
Packit Service 20376f
Packit Service 20376f
/** General join with separator */
Packit Service 20376f
int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...);
Packit Service 20376f
/** Fast join of two strings - first may legally point into `buf` data */
Packit Service 20376f
int git_buf_join(git_buf *buf, char separator, const char *str_a, const char *str_b);
Packit Service 20376f
/** Fast join of three strings - cannot reference `buf` data */
Packit Service 20376f
int git_buf_join3(git_buf *buf, char separator, const char *str_a, const char *str_b, const char *str_c);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Join two strings as paths, inserting a slash between as needed.
Packit Service 20376f
 * @return 0 on success, -1 on failure
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(int) git_buf_joinpath(git_buf *buf, const char *a, const char *b)
Packit Service 20376f
{
Packit Service 20376f
	return git_buf_join(buf, '/', a, b);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(const char *) git_buf_cstr(const git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	return buf->ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(size_t) git_buf_len(const git_buf *buf)
Packit Service 20376f
{
Packit Service 20376f
	return buf->size;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_buf_copy_cstr(char *data, size_t datasize, const git_buf *buf);
Packit Service 20376f
Packit Service 20376f
#define git_buf_PUTS(buf, str) git_buf_put(buf, str, sizeof(str) - 1)
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(ssize_t) git_buf_rfind_next(const git_buf *buf, char ch)
Packit Service 20376f
{
Packit Service 20376f
	ssize_t idx = (ssize_t)buf->size - 1;
Packit Service 20376f
	while (idx >= 0 && buf->ptr[idx] == ch) idx--;
Packit Service 20376f
	while (idx >= 0 && buf->ptr[idx] != ch) idx--;
Packit Service 20376f
	return idx;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(ssize_t) git_buf_rfind(const git_buf *buf, char ch)
Packit Service 20376f
{
Packit Service 20376f
	ssize_t idx = (ssize_t)buf->size - 1;
Packit Service 20376f
	while (idx >= 0 && buf->ptr[idx] != ch) idx--;
Packit Service 20376f
	return idx;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(ssize_t) git_buf_find(const git_buf *buf, char ch)
Packit Service 20376f
{
Packit Service 20376f
	void *found = memchr(buf->ptr, ch, buf->size);
Packit Service 20376f
	return found ? (ssize_t)((const char *)found - buf->ptr) : -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Remove whitespace from the end of the buffer */
Packit Service 20376f
void git_buf_rtrim(git_buf *buf);
Packit Service 20376f
Packit Service 20376f
int git_buf_cmp(const git_buf *a, const git_buf *b);
Packit Service 20376f
Packit Service 20376f
/* Quote and unquote a buffer as specified in
Packit Service 20376f
 * http://marc.info/?l=git&m=112927316408690&w=2
Packit Service 20376f
 */
Packit Service 20376f
int git_buf_quote(git_buf *buf);
Packit Service 20376f
int git_buf_unquote(git_buf *buf);
Packit Service 20376f
Packit Service 20376f
/* Write data as base64 encoded in buffer */
Packit Service 20376f
int git_buf_encode_base64(git_buf *buf, const char *data, size_t len);
Packit Service 20376f
/* Decode the given bas64 and write the result to the buffer */
Packit Service 20376f
int git_buf_decode_base64(git_buf *buf, const char *base64, size_t len);
Packit Service 20376f
Packit Service 20376f
/* Write data as "base85" encoded in buffer */
Packit Service 20376f
int git_buf_encode_base85(git_buf *buf, const char *data, size_t len);
Packit Service 20376f
/* Decode the given "base85" and write the result to the buffer */
Packit Service 20376f
int git_buf_decode_base85(git_buf *buf, const char *base64, size_t len, size_t output_len);
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Insert, remove or replace a portion of the buffer.
Packit Service 20376f
 *
Packit Service 20376f
 * @param buf The buffer to work with
Packit Service 20376f
 *
Packit Service 20376f
 * @param where The location in the buffer where the transformation
Packit Service 20376f
 * should be applied.
Packit Service 20376f
 *
Packit Service 20376f
 * @param nb_to_remove The number of chars to be removed. 0 to not
Packit Service 20376f
 * remove any character in the buffer.
Packit Service 20376f
 *
Packit Service 20376f
 * @param data A pointer to the data which should be inserted.
Packit Service 20376f
 *
Packit Service 20376f
 * @param nb_to_insert The number of chars to be inserted. 0 to not
Packit Service 20376f
 * insert any character from the buffer.
Packit Service 20376f
 *
Packit Service 20376f
 * @return 0 or an error code.
Packit Service 20376f
 */
Packit Service 20376f
int git_buf_splice(
Packit Service 20376f
	git_buf *buf,
Packit Service 20376f
	size_t where,
Packit Service 20376f
	size_t nb_to_remove,
Packit Service 20376f
	const char *data,
Packit Service 20376f
	size_t nb_to_insert);
Packit Service 20376f
Packit Service 20376f
#endif