summaryrefslogtreecommitdiffstats
path: root/lib/util/iov_buf.h
blob: cb330dea07a42c27009ee6804e3996d5bb4a7a76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Unix SMB/CIFS implementation.
 * Samba system utilities
 * Copyright (C) Volker Lendecke 2014
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __LIB_IOV_BUF_H__
#define __LIB_IOV_BUF_H__

#include "replace.h"
#include <talloc.h>
#include "system/filesys.h"

static inline
ssize_t iov_buf(const struct iovec *iov, int iovcnt,
		uint8_t *buf, size_t buflen)
{
	size_t needed = 0;
	uint8_t *p = buf;
	int i;

	for (i=0; i<iovcnt; i++) {
		size_t thislen = iov[i].iov_len;
		size_t tmp;

		tmp = needed + thislen;

		if (tmp < needed) {
			/* wrap */
			return -1;
		}
		needed = tmp;

		if ((p != NULL) && needed <= buflen && thislen > 0) {
			memcpy(p, iov[i].iov_base, thislen);
			p += thislen;
		}
	}

	return needed;
}

static inline
ssize_t iov_buflen(const struct iovec *iov, int iovcnt)
{
	return iov_buf(iov, iovcnt, NULL, 0);
}

static inline
bool iov_advance(struct iovec **iov, int *iovcnt, size_t n)
{
	struct iovec *v = *iov;
	int cnt = *iovcnt;

	while (n > 0) {
		if (cnt == 0) {
			return false;
		}
		if (n < v->iov_len) {
			v->iov_base = (char *)v->iov_base + n;
			v->iov_len -= n;
			break;
		}
		n -= v->iov_len;
		v += 1;
		cnt -= 1;
	}

	/*
	 * Skip 0-length iovec's
	 *
	 * There might be empty buffers at the end of iov. Next time we do a
	 * readv/writev based on this iov would give 0 transferred bytes, also
	 * known as EPIPE. So we need to be careful discarding them.
	 */

	while ((cnt > 0) && (v->iov_len == 0)) {
		v += 1;
		cnt -= 1;
	}

	*iov = v;
	*iovcnt = cnt;
	return true;
}

uint8_t *iov_concat(TALLOC_CTX *mem_ctx, const struct iovec *iov, int count);

#endif