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