]> git.proxmox.com Git - libgit2.git/blame - src/libgit2/buf.h
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / src / libgit2 / buf.h
CommitLineData
e579e0f7
MB
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#ifndef INCLUDE_buf_h__
8#define INCLUDE_buf_h__
9
10#include "git2/buffer.h"
11#include "common.h"
12
13/*
14 * Adapts a private API that takes a `git_str` into a public API that
15 * takes a `git_buf`.
16 */
17
18#define GIT_BUF_WRAP_PRIVATE(buf, fn, ...) \
19 { \
20 git_str str = GIT_STR_INIT; \
21 int error; \
22 if ((error = git_buf_tostr(&str, buf)) == 0 && \
23 (error = fn(&str, __VA_ARGS__)) == 0) \
24 error = git_buf_fromstr(buf, &str); \
25 git_str_dispose(&str); \
26 return error; \
27}
28
29/**
30 * "Sanitizes" a buffer from user input. This simply ensures that the
31 * `git_buf` has nice defaults if the user didn't set the members to
32 * anything, so that if we return early we don't leave it populated
33 * with nonsense.
34 */
35extern int git_buf_sanitize(git_buf *from_user);
36
37/**
38 * Populate a `git_str` from a `git_buf` for passing to libgit2 internal
39 * functions. Sanitizes the given `git_buf` before proceeding. The
40 * `git_buf` will no longer point to this memory.
41 */
42extern int git_buf_tostr(git_str *out, git_buf *buf);
43
44/**
45 * Populate a `git_buf` from a `git_str` for returning to a user.
46 * The `git_str` will no longer point to this memory.
47 */
48extern int git_buf_fromstr(git_buf *out, git_str *str);
49
50#endif