2 * Helpers for getting linearized buffers from iov / filling buffers into iovs
4 * Copyright IBM, Corp. 2007, 2008
5 * Copyright (C) 2010 Red Hat, Inc.
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
20 size_t iov_from_buf(struct iovec
*iov
, unsigned int iov_cnt
,
21 const void *buf
, size_t iov_off
, size_t size
)
23 size_t iovec_off
, buf_off
;
28 for (i
= 0; i
< iov_cnt
&& size
; i
++) {
29 if (iov_off
< (iovec_off
+ iov
[i
].iov_len
)) {
30 size_t len
= MIN((iovec_off
+ iov
[i
].iov_len
) - iov_off
, size
);
32 memcpy(iov
[i
].iov_base
+ (iov_off
- iovec_off
), buf
+ buf_off
, len
);
38 iovec_off
+= iov
[i
].iov_len
;
43 size_t iov_to_buf(const struct iovec
*iov
, const unsigned int iov_cnt
,
44 void *buf
, size_t iov_off
, size_t size
)
47 size_t iovec_off
, buf_off
;
53 for (i
= 0; i
< iov_cnt
&& size
; i
++) {
54 if (iov_off
< (iovec_off
+ iov
[i
].iov_len
)) {
55 size_t len
= MIN((iovec_off
+ iov
[i
].iov_len
) - iov_off
, size
);
57 memcpy(ptr
+ buf_off
, iov
[i
].iov_base
+ (iov_off
- iovec_off
), len
);
63 iovec_off
+= iov
[i
].iov_len
;
68 size_t iov_clear(const struct iovec
*iov
, const unsigned int iov_cnt
,
69 size_t iov_off
, size_t size
)
71 size_t iovec_off
, buf_off
;
76 for (i
= 0; i
< iov_cnt
&& size
; i
++) {
77 if (iov_off
< (iovec_off
+ iov
[i
].iov_len
)) {
78 size_t len
= MIN((iovec_off
+ iov
[i
].iov_len
) - iov_off
, size
);
80 memset(iov
[i
].iov_base
+ (iov_off
- iovec_off
), 0, len
);
86 iovec_off
+= iov
[i
].iov_len
;
91 size_t iov_size(const struct iovec
*iov
, const unsigned int iov_cnt
)
97 for (i
= 0; i
< iov_cnt
; i
++) {
98 len
+= iov
[i
].iov_len
;
103 void iov_hexdump(const struct iovec
*iov
, const unsigned int iov_cnt
,
104 FILE *fp
, const char *prefix
, size_t limit
)
106 unsigned int i
, v
, b
;
110 for (i
= 0, v
= 0, b
= 0; b
< limit
; i
++, b
++) {
111 if (i
== iov
[v
].iov_len
) {
119 fprintf(fp
, "%s: %04x:", prefix
, b
);
124 fprintf(fp
, " %02x", c
[i
]);
125 if ((b
% 16) == 15) {