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. |
| 13 | */ |
| 14 | |
| 15 | #include "iov.h" |
| 16 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 17 | size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt, |
| 18 | const void *buf, size_t iov_off, size_t size) |
Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 19 | { |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 20 | size_t iovec_off, buf_off; |
Amit Shah | e4d5639 | 2010-04-27 18:04:05 +0530 | [diff] [blame] | 21 | unsigned int i; |
| 22 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 23 | iovec_off = 0; |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 24 | buf_off = 0; |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 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); |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 28 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 29 | 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] | 30 | |
| 31 | buf_off += len; |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 32 | iov_off += len; |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 33 | size -= len; |
| 34 | } |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 35 | iovec_off += iov[i].iov_len; |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 36 | } |
| 37 | return buf_off; |
| 38 | } |
| 39 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 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) |
| 42 | { |
| 43 | uint8_t *ptr; |
| 44 | size_t iovec_off, buf_off; |
| 45 | unsigned int i; |
| 46 | |
| 47 | ptr = buf; |
| 48 | iovec_off = 0; |
| 49 | buf_off = 0; |
| 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); |
| 53 | |
| 54 | memcpy(ptr + buf_off, iov[i].iov_base + (iov_off - iovec_off), len); |
| 55 | |
| 56 | buf_off += len; |
| 57 | iov_off += len; |
| 58 | size -= len; |
| 59 | } |
| 60 | iovec_off += iov[i].iov_len; |
| 61 | } |
| 62 | return buf_off; |
| 63 | } |
| 64 | |
Gerd Hoffmann | 8d15028 | 2011-07-13 15:16:08 +0200 | [diff] [blame] | 65 | size_t iov_clear(const struct iovec *iov, const unsigned int iov_cnt, |
| 66 | size_t iov_off, size_t size) |
| 67 | { |
| 68 | size_t iovec_off, buf_off; |
| 69 | unsigned int i; |
| 70 | |
| 71 | iovec_off = 0; |
| 72 | buf_off = 0; |
| 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); |
| 76 | |
| 77 | memset(iov[i].iov_base + (iov_off - iovec_off), 0, len); |
| 78 | |
| 79 | buf_off += len; |
| 80 | iov_off += len; |
| 81 | size -= len; |
| 82 | } |
| 83 | iovec_off += iov[i].iov_len; |
| 84 | } |
| 85 | return buf_off; |
| 86 | } |
| 87 | |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 88 | 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] | 89 | { |
| 90 | size_t len; |
| 91 | unsigned int i; |
| 92 | |
| 93 | len = 0; |
Hannes Reinecke | 348e7b8 | 2011-07-11 15:02:23 +0200 | [diff] [blame] | 94 | for (i = 0; i < iov_cnt; i++) { |
Amit Shah | fa6111f | 2010-04-27 18:04:06 +0530 | [diff] [blame] | 95 | len += iov[i].iov_len; |
| 96 | } |
| 97 | return len; |
| 98 | } |
Gerd Hoffmann | 3a1dca9 | 2011-07-12 13:35:10 +0200 | [diff] [blame] | 99 | |
| 100 | void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt, |
| 101 | FILE *fp, const char *prefix, size_t limit) |
| 102 | { |
| 103 | unsigned int i, v, b; |
| 104 | uint8_t *c; |
| 105 | |
| 106 | c = iov[0].iov_base; |
| 107 | for (i = 0, v = 0, b = 0; b < limit; i++, b++) { |
| 108 | if (i == iov[v].iov_len) { |
| 109 | i = 0; v++; |
| 110 | if (v == iov_cnt) { |
| 111 | break; |
| 112 | } |
| 113 | c = iov[v].iov_base; |
| 114 | } |
| 115 | if ((b % 16) == 0) { |
| 116 | fprintf(fp, "%s: %04x:", prefix, b); |
| 117 | } |
| 118 | if ((b % 4) == 0) { |
| 119 | fprintf(fp, " "); |
| 120 | } |
| 121 | fprintf(fp, " %02x", c[i]); |
| 122 | if ((b % 16) == 15) { |
| 123 | fprintf(fp, "\n"); |
| 124 | } |
| 125 | } |
| 126 | if ((b % 16) != 0) { |
| 127 | fprintf(fp, "\n"); |
| 128 | } |
| 129 | } |