linux-user: save auxv length
[qemu.git] / iov.c
1 /*
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.
13 *
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.
16 */
17
18 #include "iov.h"
19
20 size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt,
21 const void *buf, size_t iov_off, size_t size)
22 {
23 size_t iovec_off, buf_off;
24 unsigned int i;
25
26 iovec_off = 0;
27 buf_off = 0;
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);
31
32 memcpy(iov[i].iov_base + (iov_off - iovec_off), buf + buf_off, len);
33
34 buf_off += len;
35 iov_off += len;
36 size -= len;
37 }
38 iovec_off += iov[i].iov_len;
39 }
40 return buf_off;
41 }
42
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)
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
68 size_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
91 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
92 {
93 size_t len;
94 unsigned int i;
95
96 len = 0;
97 for (i = 0; i < iov_cnt; i++) {
98 len += iov[i].iov_len;
99 }
100 return len;
101 }
102
103 void 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 }