1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2016 Jakub Klama <jceel@FreeBSD.org>.
5 * Copyright (c) 2018 Alexander Motin <mav@FreeBSD.org>
6 * Copyright (c) 2026 Hans Rosenfeld
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer
14 * in this position and unchanged.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/uio.h>
35
36 #include <assert.h>
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include "iov.h"
42
43
44 /*
45 * Given an iovec and an offset, split the iovec into two at the offset and
46 * return a pointer to the beginning of the second iovec.
47 *
48 * The caller is responsible for providing an extra iovec in the array for the
49 * split. That is, there should be space for *niov1 + 1 iovecs in the array.
50 */
51 struct iovec *
split_iov(struct iovec * iov,size_t * niov1,size_t offset,size_t * niov2)52 split_iov(struct iovec *iov, size_t *niov1, size_t offset, size_t *niov2)
53 {
54 size_t count, resid;
55
56 /* Find the iovec entry that contains the offset. */
57 resid = offset;
58 for (count = 0; count < *niov1; count++) {
59 if (resid < iov[count].iov_len)
60 break;
61 resid -= iov[count].iov_len;
62 }
63
64 if (resid == 0 || count == *niov1) {
65 /* Easy case, we don't have to split an iovec entry. */
66 *niov2 = *niov1 - count;
67 *niov1 = count;
68 if (*niov2 == 0)
69 return (NULL);
70 return (&iov[count]);
71 }
72
73 /* The entry iov[count] needs to be split. */
74 *niov1 = count + 1;
75 *niov2 = *niov1 - count;
76 memmove(&iov[count + 1], &iov[count], sizeof(struct iovec) * (*niov2));
77 iov[count].iov_len = resid;
78 iov[count + 1].iov_base = (char *)iov[count].iov_base + resid;
79 iov[count + 1].iov_len -= resid;
80 return (&iov[count + 1]);
81 }
82
83 size_t
count_iov(const struct iovec * iov,size_t niov)84 count_iov(const struct iovec *iov, size_t niov)
85 {
86 size_t total = 0;
87 size_t i;
88
89 for (i = 0; i < niov; i++) {
90 assert(total <= SIZE_MAX - iov[i].iov_len);
91 total += iov[i].iov_len;
92 }
93
94 return (total);
95 }
96
97 bool
check_iov_len(const struct iovec * iov,size_t niov,size_t len)98 check_iov_len(const struct iovec *iov, size_t niov, size_t len)
99 {
100 size_t total = 0;
101 size_t i;
102
103 for (i = 0; i < niov; i++) {
104 assert(total <= SIZE_MAX - iov[i].iov_len);
105 total += iov[i].iov_len;
106 if (total >= len)
107 return (true);
108 }
109
110 return (false);
111 }
112
113 ssize_t
iov_to_buf(const struct iovec * iov,size_t niov,void ** buf)114 iov_to_buf(const struct iovec *iov, size_t niov, void **buf)
115 {
116 size_t ptr, total;
117 size_t i;
118
119 total = count_iov(iov, niov);
120 *buf = realloc(*buf, total);
121 if (*buf == NULL)
122 return (-1);
123
124 for (i = 0, ptr = 0; i < niov; i++) {
125 memcpy((uint8_t *)*buf + ptr, iov[i].iov_base, iov[i].iov_len);
126 ptr += iov[i].iov_len;
127 }
128
129 return (total);
130 }
131
132 ssize_t
buf_to_iov(const void * buf,size_t buflen,const struct iovec * iov,size_t niov)133 buf_to_iov(const void *buf, size_t buflen, const struct iovec *iov, size_t niov)
134 {
135 size_t off = 0, len;
136 size_t i;
137
138 for (i = 0; i < niov && off < buflen; i++) {
139 len = MIN(iov[i].iov_len, buflen - off);
140 memcpy(iov[i].iov_base, (const uint8_t *)buf + off, len);
141 off += len;
142 }
143
144 return ((ssize_t)off);
145 }
146