blob: 0f964939d0e9ecd7e4b64a87fac72511be91ca47 [file] [log] [blame]
Amit Shahe4d56392010-04-27 18:04:05 +05301/*
2 * Helpers for getting linearized buffers from iov / filling buffers into iovs
3 *
4 * Copyright IBM, Corp. 2007, 2008
5 * Copyright (C) 2010 Red Hat, Inc.
6 *
7 * Author(s):
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010013 *
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.
Amit Shahe4d56392010-04-27 18:04:05 +053016 */
17
18#include "iov.h"
19
Hannes Reinecke348e7b82011-07-11 15:02:23 +020020size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt,
21 const void *buf, size_t iov_off, size_t size)
Amit Shahe4d56392010-04-27 18:04:05 +053022{
Hannes Reinecke348e7b82011-07-11 15:02:23 +020023 size_t iovec_off, buf_off;
Amit Shahe4d56392010-04-27 18:04:05 +053024 unsigned int i;
25
Hannes Reinecke348e7b82011-07-11 15:02:23 +020026 iovec_off = 0;
Amit Shahfa6111f2010-04-27 18:04:06 +053027 buf_off = 0;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020028 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);
Amit Shahfa6111f2010-04-27 18:04:06 +053031
Hannes Reinecke348e7b82011-07-11 15:02:23 +020032 memcpy(iov[i].iov_base + (iov_off - iovec_off), buf + buf_off, len);
Amit Shahfa6111f2010-04-27 18:04:06 +053033
34 buf_off += len;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020035 iov_off += len;
Amit Shahfa6111f2010-04-27 18:04:06 +053036 size -= len;
37 }
Hannes Reinecke348e7b82011-07-11 15:02:23 +020038 iovec_off += iov[i].iov_len;
Amit Shahfa6111f2010-04-27 18:04:06 +053039 }
40 return buf_off;
41}
42
Hannes Reinecke348e7b82011-07-11 15:02:23 +020043size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
44 void *buf, size_t iov_off, size_t size)
45{
46 uint8_t *ptr;
47 size_t iovec_off, buf_off;
48 unsigned int i;
49
50 ptr = buf;
51 iovec_off = 0;
52 buf_off = 0;
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);
56
57 memcpy(ptr + buf_off, iov[i].iov_base + (iov_off - iovec_off), len);
58
59 buf_off += len;
60 iov_off += len;
61 size -= len;
62 }
63 iovec_off += iov[i].iov_len;
64 }
65 return buf_off;
66}
67
Gerd Hoffmann8d150282011-07-13 15:16:08 +020068size_t iov_clear(const struct iovec *iov, const unsigned int iov_cnt,
69 size_t iov_off, size_t size)
70{
71 size_t iovec_off, buf_off;
72 unsigned int i;
73
74 iovec_off = 0;
75 buf_off = 0;
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);
79
80 memset(iov[i].iov_base + (iov_off - iovec_off), 0, len);
81
82 buf_off += len;
83 iov_off += len;
84 size -= len;
85 }
86 iovec_off += iov[i].iov_len;
87 }
88 return buf_off;
89}
90
Hannes Reinecke348e7b82011-07-11 15:02:23 +020091size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
Amit Shahfa6111f2010-04-27 18:04:06 +053092{
93 size_t len;
94 unsigned int i;
95
96 len = 0;
Hannes Reinecke348e7b82011-07-11 15:02:23 +020097 for (i = 0; i < iov_cnt; i++) {
Amit Shahfa6111f2010-04-27 18:04:06 +053098 len += iov[i].iov_len;
99 }
100 return len;
101}
Gerd Hoffmann3a1dca92011-07-12 13:35:10 +0200102
103void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
104 FILE *fp, const char *prefix, size_t limit)
105{
106 unsigned int i, v, b;
107 uint8_t *c;
108
109 c = iov[0].iov_base;
110 for (i = 0, v = 0, b = 0; b < limit; i++, b++) {
111 if (i == iov[v].iov_len) {
112 i = 0; v++;
113 if (v == iov_cnt) {
114 break;
115 }
116 c = iov[v].iov_base;
117 }
118 if ((b % 16) == 0) {
119 fprintf(fp, "%s: %04x:", prefix, b);
120 }
121 if ((b % 4) == 0) {
122 fprintf(fp, " ");
123 }
124 fprintf(fp, " %02x", c[i]);
125 if ((b % 16) == 15) {
126 fprintf(fp, "\n");
127 }
128 }
129 if ((b % 16) != 0) {
130 fprintf(fp, "\n");
131 }
132}