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.
17 size_t iov_from_buf(struct iovec
*iov
, unsigned int iov_cnt
,
18 const void *buf
, size_t iov_off
, size_t size
)
20 size_t iovec_off
, buf_off
;
25 for (i
= 0; i
< iov_cnt
&& size
; i
++) {
26 if (iov_off
< (iovec_off
+ iov
[i
].iov_len
)) {
27 size_t len
= MIN((iovec_off
+ iov
[i
].iov_len
) - iov_off
, size
);
29 memcpy(iov
[i
].iov_base
+ (iov_off
- iovec_off
), buf
+ buf_off
, len
);
35 iovec_off
+= iov
[i
].iov_len
;
40 size_t iov_to_buf(const struct iovec
*iov
, const unsigned int iov_cnt
,
41 void *buf
, size_t iov_off
, size_t size
)
44 size_t iovec_off
, buf_off
;
50 for (i
= 0; i
< iov_cnt
&& size
; i
++) {
51 if (iov_off
< (iovec_off
+ iov
[i
].iov_len
)) {
52 size_t len
= MIN((iovec_off
+ iov
[i
].iov_len
) - iov_off
, size
);
54 memcpy(ptr
+ buf_off
, iov
[i
].iov_base
+ (iov_off
- iovec_off
), len
);
60 iovec_off
+= iov
[i
].iov_len
;
65 size_t iov_clear(const struct iovec
*iov
, const unsigned int iov_cnt
,
66 size_t iov_off
, size_t size
)
68 size_t iovec_off
, buf_off
;
73 for (i
= 0; i
< iov_cnt
&& size
; i
++) {
74 if (iov_off
< (iovec_off
+ iov
[i
].iov_len
)) {
75 size_t len
= MIN((iovec_off
+ iov
[i
].iov_len
) - iov_off
, size
);
77 memset(iov
[i
].iov_base
+ (iov_off
- iovec_off
), 0, len
);
83 iovec_off
+= iov
[i
].iov_len
;
88 size_t iov_size(const struct iovec
*iov
, const unsigned int iov_cnt
)
94 for (i
= 0; i
< iov_cnt
; i
++) {
95 len
+= iov
[i
].iov_len
;
100 void iov_hexdump(const struct iovec
*iov
, const unsigned int iov_cnt
,
101 FILE *fp
, const char *prefix
, size_t limit
)
103 unsigned int i
, v
, b
;
107 for (i
= 0, v
= 0, b
= 0; b
< limit
; i
++, b
++) {
108 if (i
== iov
[v
].iov_len
) {
116 fprintf(fp
, "%s: %04x:", prefix
, b
);
121 fprintf(fp
, " %02x", c
[i
]);
122 if ((b
% 16) == 15) {