1f9c005a1SMarcelo Araujo /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3f9c005a1SMarcelo Araujo *
4f9c005a1SMarcelo Araujo * Copyright (c) 2016 Jakub Klama <jceel@FreeBSD.org>.
599fa47deSAlexander Motin * Copyright (c) 2018 Alexander Motin <mav@FreeBSD.org>
6f9c005a1SMarcelo Araujo * All rights reserved.
7f9c005a1SMarcelo Araujo *
8f9c005a1SMarcelo Araujo * Redistribution and use in source and binary forms, with or without
9f9c005a1SMarcelo Araujo * modification, are permitted provided that the following conditions
10f9c005a1SMarcelo Araujo * are met:
11f9c005a1SMarcelo Araujo * 1. Redistributions of source code must retain the above copyright
12f9c005a1SMarcelo Araujo * notice, this list of conditions and the following disclaimer
13f9c005a1SMarcelo Araujo * in this position and unchanged.
14f9c005a1SMarcelo Araujo * 2. Redistributions in binary form must reproduce the above copyright
15f9c005a1SMarcelo Araujo * notice, this list of conditions and the following disclaimer in the
16f9c005a1SMarcelo Araujo * documentation and/or other materials provided with the distribution.
17f9c005a1SMarcelo Araujo *
18f9c005a1SMarcelo Araujo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19f9c005a1SMarcelo Araujo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20f9c005a1SMarcelo Araujo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21f9c005a1SMarcelo Araujo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22f9c005a1SMarcelo Araujo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23f9c005a1SMarcelo Araujo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24f9c005a1SMarcelo Araujo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25f9c005a1SMarcelo Araujo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26f9c005a1SMarcelo Araujo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27f9c005a1SMarcelo Araujo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28f9c005a1SMarcelo Araujo * SUCH DAMAGE.
29f9c005a1SMarcelo Araujo */
30f9c005a1SMarcelo Araujo
31f9c005a1SMarcelo Araujo #include <sys/param.h>
32f9c005a1SMarcelo Araujo #include <sys/types.h>
33f9c005a1SMarcelo Araujo #include <sys/uio.h>
34f9c005a1SMarcelo Araujo
35f9c005a1SMarcelo Araujo #include <stdlib.h>
36f9c005a1SMarcelo Araujo #include <string.h>
37f9c005a1SMarcelo Araujo #include "iov.h"
38f9c005a1SMarcelo Araujo
39f9c005a1SMarcelo Araujo void
seek_iov(const struct iovec * iov1,int niov1,struct iovec * iov2,int * niov2,size_t seek)4099fa47deSAlexander Motin seek_iov(const struct iovec *iov1, int niov1, struct iovec *iov2, int *niov2,
41f9c005a1SMarcelo Araujo size_t seek)
42f9c005a1SMarcelo Araujo {
43f9c005a1SMarcelo Araujo size_t remainder = 0;
44f9c005a1SMarcelo Araujo size_t left = seek;
4599fa47deSAlexander Motin int i, j;
46f9c005a1SMarcelo Araujo
47f9c005a1SMarcelo Araujo for (i = 0; i < niov1; i++) {
48f9c005a1SMarcelo Araujo size_t toseek = MIN(left, iov1[i].iov_len);
49f9c005a1SMarcelo Araujo left -= toseek;
50f9c005a1SMarcelo Araujo
51f9c005a1SMarcelo Araujo if (toseek == iov1[i].iov_len)
52f9c005a1SMarcelo Araujo continue;
53f9c005a1SMarcelo Araujo
54f9c005a1SMarcelo Araujo if (left == 0) {
55f9c005a1SMarcelo Araujo remainder = toseek;
56f9c005a1SMarcelo Araujo break;
57f9c005a1SMarcelo Araujo }
58f9c005a1SMarcelo Araujo }
59f9c005a1SMarcelo Araujo
60f9c005a1SMarcelo Araujo for (j = i; j < niov1; j++) {
61f9c005a1SMarcelo Araujo iov2[j - i].iov_base = (char *)iov1[j].iov_base + remainder;
62f9c005a1SMarcelo Araujo iov2[j - i].iov_len = iov1[j].iov_len - remainder;
63f9c005a1SMarcelo Araujo remainder = 0;
64f9c005a1SMarcelo Araujo }
65f9c005a1SMarcelo Araujo
66f9c005a1SMarcelo Araujo *niov2 = j - i;
67f9c005a1SMarcelo Araujo }
68f9c005a1SMarcelo Araujo
69f9c005a1SMarcelo Araujo size_t
count_iov(const struct iovec * iov,int niov)7099fa47deSAlexander Motin count_iov(const struct iovec *iov, int niov)
71f9c005a1SMarcelo Araujo {
7299fa47deSAlexander Motin size_t total = 0;
7399fa47deSAlexander Motin int i;
74f9c005a1SMarcelo Araujo
75f9c005a1SMarcelo Araujo for (i = 0; i < niov; i++)
76f9c005a1SMarcelo Araujo total += iov[i].iov_len;
77f9c005a1SMarcelo Araujo
78f9c005a1SMarcelo Araujo return (total);
79f9c005a1SMarcelo Araujo }
80f9c005a1SMarcelo Araujo
8199fa47deSAlexander Motin void
truncate_iov(struct iovec * iov,int * niov,size_t length)8299fa47deSAlexander Motin truncate_iov(struct iovec *iov, int *niov, size_t length)
83f9c005a1SMarcelo Araujo {
8499fa47deSAlexander Motin size_t done = 0;
8599fa47deSAlexander Motin int i;
86f9c005a1SMarcelo Araujo
8799fa47deSAlexander Motin for (i = 0; i < *niov; i++) {
88f9c005a1SMarcelo Araujo size_t toseek = MIN(length - done, iov[i].iov_len);
89f9c005a1SMarcelo Araujo done += toseek;
90f9c005a1SMarcelo Araujo
9199fa47deSAlexander Motin if (toseek <= iov[i].iov_len) {
92f9c005a1SMarcelo Araujo iov[i].iov_len = toseek;
9399fa47deSAlexander Motin *niov = i + 1;
9499fa47deSAlexander Motin return;
95f9c005a1SMarcelo Araujo }
96f9c005a1SMarcelo Araujo }
97f9c005a1SMarcelo Araujo }
98f9c005a1SMarcelo Araujo
99f9c005a1SMarcelo Araujo ssize_t
iov_to_buf(const struct iovec * iov,int niov,void ** buf)10099fa47deSAlexander Motin iov_to_buf(const struct iovec *iov, int niov, void **buf)
101f9c005a1SMarcelo Araujo {
10299fa47deSAlexander Motin size_t ptr, total;
10399fa47deSAlexander Motin int i;
104f9c005a1SMarcelo Araujo
10599fa47deSAlexander Motin total = count_iov(iov, niov);
106f9c005a1SMarcelo Araujo *buf = realloc(*buf, total);
107f9c005a1SMarcelo Araujo if (*buf == NULL)
108f9c005a1SMarcelo Araujo return (-1);
109f9c005a1SMarcelo Araujo
11099fa47deSAlexander Motin for (i = 0, ptr = 0; i < niov; i++) {
11163898728SMark Johnston memcpy((uint8_t *)*buf + ptr, iov[i].iov_base, iov[i].iov_len);
112f9c005a1SMarcelo Araujo ptr += iov[i].iov_len;
113f9c005a1SMarcelo Araujo }
114f9c005a1SMarcelo Araujo
115f9c005a1SMarcelo Araujo return (total);
116f9c005a1SMarcelo Araujo }
117f9c005a1SMarcelo Araujo
118f9c005a1SMarcelo Araujo ssize_t
buf_to_iov(const void * buf,size_t buflen,const struct iovec * iov,int niov,size_t seek)119f92bb8c1SVincenzo Maffione buf_to_iov(const void *buf, size_t buflen, const struct iovec *iov, int niov,
120f9c005a1SMarcelo Araujo size_t seek)
121f9c005a1SMarcelo Araujo {
122f9c005a1SMarcelo Araujo struct iovec *diov;
12399fa47deSAlexander Motin size_t off = 0, len;
124f92bb8c1SVincenzo Maffione int i;
125f9c005a1SMarcelo Araujo
126f9c005a1SMarcelo Araujo if (seek > 0) {
127f92bb8c1SVincenzo Maffione int ndiov;
128f92bb8c1SVincenzo Maffione
129f9c005a1SMarcelo Araujo diov = malloc(sizeof(struct iovec) * niov);
130f9c005a1SMarcelo Araujo seek_iov(iov, niov, diov, &ndiov, seek);
131f92bb8c1SVincenzo Maffione iov = diov;
132f92bb8c1SVincenzo Maffione niov = ndiov;
133f9c005a1SMarcelo Araujo }
134f9c005a1SMarcelo Araujo
135f92bb8c1SVincenzo Maffione for (i = 0; i < niov && off < buflen; i++) {
136f92bb8c1SVincenzo Maffione len = MIN(iov[i].iov_len, buflen - off);
13763898728SMark Johnston memcpy(iov[i].iov_base, (const uint8_t *)buf + off, len);
13899fa47deSAlexander Motin off += len;
139f9c005a1SMarcelo Araujo }
140f9c005a1SMarcelo Araujo
14199fa47deSAlexander Motin if (seek > 0)
14299fa47deSAlexander Motin free(diov);
14399fa47deSAlexander Motin
144f9c005a1SMarcelo Araujo return ((ssize_t)off);
145f9c005a1SMarcelo Araujo }
146f9c005a1SMarcelo Araujo
147