Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 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. |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 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. |
Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "iov.h" |
| 19 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 20 | size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt, |
| 21 | const void *buf, size_t iov_off, size_t size) |
Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 22 | { |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 23 | size_t iovec_off, buf_off; |
Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 24 | unsigned int i; |
| 25 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 26 | iovec_off = 0; |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 27 | buf_off = 0; |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 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); |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 31 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 32 | memcpy(iov[i].iov_base + (iov_off - iovec_off), buf + buf_off, len); |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 33 | |
| 34 | buf_off += len; |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 35 | iov_off += len; |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 36 | size -= len; |
| 37 | } |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 38 | iovec_off += iov[i].iov_len; |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 39 | } |
| 40 | return buf_off; |
| 41 | } |
| 42 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 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 | |
Gerd Hoffmann | 8d15028 | 2011-07-13 15:16:08 +0200 | [diff] [blame] | 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 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 91 | size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt) |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 92 | { |
| 93 | size_t len; |
| 94 | unsigned int i; |
| 95 | |
| 96 | len = 0; |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 97 | for (i = 0; i < iov_cnt; i++) { |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 98 | len += iov[i].iov_len; |
| 99 | } |
| 100 | return len; |
| 101 | } |
Gerd Hoffmann | 3a1dca9 | 2011-07-12 13:35:10 +0200 | [diff] [blame] | 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 | } |