tracetool: Add module for the 'h' format
[qemu/opensuse.git] / iov.c
blob0f964939d0e9ecd7e4b64a87fac72511be91ca47
1 /*
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.
7 * Author(s):
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.
18 #include "iov.h"
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;
24 unsigned int i;
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);
32 memcpy(iov[i].iov_base + (iov_off - iovec_off), buf + buf_off, len);
34 buf_off += len;
35 iov_off += len;
36 size -= len;
38 iovec_off += iov[i].iov_len;
40 return buf_off;
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)
46 uint8_t *ptr;
47 size_t iovec_off, buf_off;
48 unsigned int i;
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);
57 memcpy(ptr + buf_off, iov[i].iov_base + (iov_off - iovec_off), len);
59 buf_off += len;
60 iov_off += len;
61 size -= len;
63 iovec_off += iov[i].iov_len;
65 return buf_off;
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;
72 unsigned int i;
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);
80 memset(iov[i].iov_base + (iov_off - iovec_off), 0, len);
82 buf_off += len;
83 iov_off += len;
84 size -= len;
86 iovec_off += iov[i].iov_len;
88 return buf_off;
91 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt)
93 size_t len;
94 unsigned int i;
96 len = 0;
97 for (i = 0; i < iov_cnt; i++) {
98 len += iov[i].iov_len;
100 return 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;
107 uint8_t *c;
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;
116 c = iov[v].iov_base;
118 if ((b % 16) == 0) {
119 fprintf(fp, "%s: %04x:", prefix, b);
121 if ((b % 4) == 0) {
122 fprintf(fp, " ");
124 fprintf(fp, " %02x", c[i]);
125 if ((b % 16) == 15) {
126 fprintf(fp, "\n");
129 if ((b % 16) != 0) {
130 fprintf(fp, "\n");