Blame include/git2/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_git_buf_h__
Packit Service 20376f
#define INCLUDE_git_buf_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * @file git2/buffer.h
Packit Service 20376f
 * @brief Buffer export structure
Packit Service 20376f
 *
Packit Service 20376f
 * @ingroup Git
Packit Service 20376f
 * @{
Packit Service 20376f
 */
Packit Service 20376f
GIT_BEGIN_DECL
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * A data buffer for exporting data from libgit2
Packit Service 20376f
 *
Packit Service 20376f
 * Sometimes libgit2 wants to return an allocated data buffer to the
Packit Service 20376f
 * caller and have the caller take responsibility for freeing that memory.
Packit Service 20376f
 * This can be awkward if the caller does not have easy access to the same
Packit Service 20376f
 * allocation functions that libgit2 is using.  In those cases, libgit2
Packit Service 20376f
 * will fill in a `git_buf` and the caller can use `git_buf_free()` to
Packit Service 20376f
 * release it when they are done.
Packit Service 20376f
 *
Packit Service 20376f
 * A `git_buf` may also be used for the caller to pass in a reference to
Packit Service 20376f
 * a block of memory they hold.  In this case, libgit2 will not resize or
Packit Service 20376f
 * free the memory, but will read from it as needed.
Packit Service 20376f
 *
Packit Service 20376f
 * A `git_buf` is a public structure with three fields:
Packit Service 20376f
 *
Packit Service 20376f
 * - `ptr` points to the start of the allocated memory.  If it is NULL,
Packit Service 20376f
 *   then the `git_buf` is considered empty and libgit2 will feel free
Packit Service 20376f
 *   to overwrite it with new data.
Packit Service 20376f
 *
Packit Service 20376f
 * - `size` holds the size (in bytes) of the data that is actually used.
Packit Service 20376f
 *
Packit Service 20376f
 * - `asize` holds the known total amount of allocated memory if the `ptr`
Packit Service 20376f
 *    was allocated by libgit2.  It may be larger than `size`.  If `ptr`
Packit Service 20376f
 *    was not allocated by libgit2 and should not be resized and/or freed,
Packit Service 20376f
 *    then `asize` will be set to zero.
Packit Service 20376f
 *
Packit Service 20376f
 * Some APIs may occasionally do something slightly unusual with a buffer,
Packit Service 20376f
 * such as setting `ptr` to a value that was passed in by the user.  In
Packit Service 20376f
 * those cases, the behavior will be clearly documented by the API.
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
 * Static initializer for git_buf from static buffer
Packit Service 20376f
 */
Packit Service 20376f
#define GIT_BUF_INIT_CONST(STR,LEN) { (char *)(STR), 0, (size_t)(LEN) }
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Free the memory referred to by the git_buf.
Packit Service 20376f
 *
Packit Service 20376f
 * Note that this does not free the `git_buf` itself, just the memory
Packit Service 20376f
 * pointed to by `buffer->ptr`.  This will not free the memory if it looks
Packit Service 20376f
 * like it was not allocated internally, but it will clear the buffer back
Packit Service 20376f
 * to the empty state.
Packit Service 20376f
 *
Packit Service 20376f
 * @param buffer The buffer to deallocate
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(void) git_buf_free(git_buf *buffer);
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 target size.
Packit Service 20376f
 *
Packit Service 20376f
 * If the buffer refers to memory that was not allocated by libgit2 (i.e.
Packit Service 20376f
 * the `asize` field is zero), then `ptr` will be replaced with a newly
Packit Service 20376f
 * allocated block of data.  Be careful so that memory allocated by the
Packit Service 20376f
 * caller is not lost.  As a special variant, if you pass `target_size` as
Packit Service 20376f
 * 0 and the memory is not allocated by libgit2, this will allocate a new
Packit Service 20376f
 * buffer of size `size` and copy the external data into it.
Packit Service 20376f
 *
Packit Service 20376f
 * Currently, this will never shrink a buffer, only expand it.
Packit Service 20376f
 *
Packit Service 20376f
 * If the allocation fails, this will return an error and the buffer will be
Packit Service 20376f
 * marked as invalid for future operations, invaliding the contents.
Packit Service 20376f
 *
Packit Service 20376f
 * @param buffer The buffer to be resized; may or may not be allocated yet
Packit Service 20376f
 * @param target_size The desired available size
Packit Service 20376f
 * @return 0 on success, -1 on allocation failure
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_buf_grow(git_buf *buffer, size_t target_size);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Set buffer to a copy of some raw data.
Packit Service 20376f
 *
Packit Service 20376f
 * @param buffer The buffer to set
Packit Service 20376f
 * @param data The data to copy into the buffer
Packit Service 20376f
 * @param datalen The length of the data to copy into the buffer
Packit Service 20376f
 * @return 0 on success, -1 on allocation failure
Packit Service 20376f
 */
Packit Service 20376f
GIT_EXTERN(int) git_buf_set(
Packit Service 20376f
	git_buf *buffer, const void *data, size_t datalen);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
* Check quickly if buffer looks like it contains binary data
Packit Service 20376f
*
Packit Service 20376f
* @param buf Buffer to check
Packit Service 20376f
* @return 1 if buffer looks like non-text data
Packit Service 20376f
*/
Packit Service 20376f
GIT_EXTERN(int) git_buf_is_binary(const git_buf *buf);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
* Check quickly if buffer contains a NUL byte
Packit Service 20376f
*
Packit Service 20376f
* @param buf Buffer to check
Packit Service 20376f
* @return 1 if buffer contains a NUL byte
Packit Service 20376f
*/
Packit Service 20376f
GIT_EXTERN(int) git_buf_contains_nul(const git_buf *buf);
Packit Service 20376f
Packit Service 20376f
GIT_END_DECL
Packit Service 20376f
Packit Service 20376f
/** @} */
Packit Service 20376f
Packit Service 20376f
#endif