1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 module libgit2_d.buffer;
8 
9 
10 /**
11  * @file git2/buffer.h
12  * @brief Buffer export structure
13  *
14  * @ingroup Git
15  * @{
16  */
17 extern (C):
18 nothrow @nogc:
19 public:
20 
21 /**
22  * A data buffer for exporting data from libgit2
23  *
24  * Sometimes libgit2 wants to return an allocated data buffer to the
25  * caller and have the caller take responsibility for freeing that memory.
26  * This can be awkward if the caller does not have easy access to the same
27  * allocation functions that libgit2 is using.  In those cases, libgit2
28  * will fill in a `git_buf` and the caller can use `git_buf_dispose()` to
29  * release it when they are done.
30  *
31  * A `git_buf` may also be used for the caller to pass in a reference to
32  * a block of memory they hold.  In this case, libgit2 will not resize or
33  * free the memory, but will read from it as needed.
34  *
35  * Some APIs may occasionally do something slightly unusual with a buffer,
36  * such as setting `ptr_` to a value that was passed in by the user.  In
37  * those cases, the behavior will be clearly documented by the API.
38  */
39 struct git_buf
40 {
41 	/**
42 	 * The buffer contents.
43 	 *
44 	 * `ptr_` points to the start of the allocated memory.  If it is NULL,
45 	 * then the `git_buf` is considered empty and libgit2 will feel free
46 	 * to overwrite it with new data.
47 	 */
48 	char* ptr_;
49 
50 	/**
51 	 * `asize` holds the known total amount of allocated memory if the `ptr_`
52 	 *  was allocated by libgit2.  It may be larger than `size`.  If `ptr_`
53 	 *  was not allocated by libgit2 and should not be resized and/or freed,
54 	 *  then `asize` will be set to zero.
55 	 */
56 	size_t asize;
57 
58 	/**
59 	 * `size` holds the size (in bytes) of the data that is actually used.
60 	 */
61 	size_t size;
62 }
63 
64 /**
65  * Static initializer for git_buf from static buffer
66  */
67 pragma(inline, true)
68 pure nothrow @safe @nogc
69 .git_buf GIT_BUF_INIT_CONST(char* STR, size_t LEN)
70 
71 	do
72 	{
73 		.git_buf OUTPUT =
74 		{
75 			ptr_: STR,
76 			asize: 0,
77 			size: LEN,
78 		};
79 
80 		return OUTPUT;
81 	}
82 
83 /**
84  * Free the memory referred to by the git_buf.
85  *
86  * Note that this does not free the `git_buf` itself, just the memory
87  * pointed to by `buffer->ptr_`.  This will not free the memory if it looks
88  * like it was not allocated internally, but it will clear the buffer back
89  * to the empty state.
90  *
91  * Params:
92  *      buffer = The buffer to deallocate
93  */
94 //GIT_EXTERN
95 void git_buf_dispose(.git_buf* buffer);
96 
97 /**
98  * Resize the buffer allocation to make more space.
99  *
100  * This will attempt to grow the buffer to accommodate the target size.
101  *
102  * If the buffer refers to memory that was not allocated by libgit2 (i.e.
103  * the `asize` field is zero), then `ptr_` will be replaced with a newly
104  * allocated block of data.  Be careful so that memory allocated by the
105  * caller is not lost.  As a special variant, if you pass `target_size` as
106  * 0 and the memory is not allocated by libgit2, this will allocate a new
107  * buffer of size `size` and copy the external data into it.
108  *
109  * Currently, this will never shrink a buffer, only expand it.
110  *
111  * If the allocation fails, this will return an error and the buffer will be
112  * marked as invalid for future operations, invaliding the contents.
113  *
114  * Params:
115  *      buffer = The buffer to be resized; may or may not be allocated yet
116  *      target_size = The desired available size
117  *
118  * Returns: 0 on success, -1 on allocation failure
119  */
120 //GIT_EXTERN
121 int git_buf_grow(.git_buf* buffer, size_t target_size);
122 
123 /**
124  * Set buffer to a copy of some raw data.
125  *
126  * Params:
127  *      buffer = The buffer to set
128  *      data = The data to copy into the buffer
129  *      datalen = The length of the data to copy into the buffer
130  *
131  * Returns: 0 on success, -1 on allocation failure
132  */
133 //GIT_EXTERN
134 int git_buf_set(.git_buf* buffer, const (void)* data, size_t datalen);
135 
136 /**
137  * Check quickly if buffer looks like it contains binary data
138  *
139  * Params:
140  *      buf = Buffer to check
141  *
142  * Returns: 1 if buffer looks like non-text data
143  */
144 //GIT_EXTERN
145 int git_buf_is_binary(const (.git_buf)* buf);
146 
147 /**
148  * Check quickly if buffer contains a NUL byte
149  *
150  * Params:
151  *      buf = Buffer to check
152  *
153  * Returns: 1 if buffer contains a NUL byte
154  */
155 //GIT_EXTERN
156 int git_buf_contains_nul(const (.git_buf)* buf);
157 
158 /** @} */