| 1 | /* |
| 2 | * Common Trace Format |
| 3 | * |
| 4 | * Integers read/write functions. |
| 5 | * |
| 6 | * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <ctf/ctf-types.h> |
| 24 | #include <stdint.h> |
| 25 | #include <glib.h> |
| 26 | #include <endian.h> |
| 27 | |
| 28 | uint64_t uint_read(const uint8_t *ptr, size_t len, int byte_order) |
| 29 | { |
| 30 | int rbo = (byte_order != BYTE_ORDER); /* reverse byte order */ |
| 31 | |
| 32 | switch (len) { |
| 33 | case 8: |
| 34 | { |
| 35 | uint8_t v; |
| 36 | |
| 37 | v = *(const uint8_t *)ptr; |
| 38 | return v; |
| 39 | } |
| 40 | case 16: |
| 41 | { |
| 42 | uint16_t v; |
| 43 | |
| 44 | v = *(const uint16_t *)ptr; |
| 45 | return rbo ? GUINT16_SWAP_LE_BE(v) : v; |
| 46 | } |
| 47 | case 32: |
| 48 | { |
| 49 | uint32_t v; |
| 50 | |
| 51 | v = *(const uint32_t *)ptr; |
| 52 | return rbo ? GUINT32_SWAP_LE_BE(v) : v; |
| 53 | } |
| 54 | case 64: |
| 55 | { |
| 56 | uint64_t v; |
| 57 | |
| 58 | v = *(const uint64_t *)ptr; |
| 59 | return rbo ? GUINT64_SWAP_LE_BE(v) : v; |
| 60 | } |
| 61 | default: |
| 62 | assert(0); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | int64_t int_read(const uint8_t *ptr, size_t len, int byte_order) |
| 67 | { |
| 68 | int rbo = (byte_order != BYTE_ORDER); /* reverse byte order */ |
| 69 | |
| 70 | switch (len) { |
| 71 | case 8: |
| 72 | { |
| 73 | int8_t v; |
| 74 | |
| 75 | v = *(const int8_t *)ptr; |
| 76 | return v; |
| 77 | } |
| 78 | case 16: |
| 79 | { |
| 80 | int16_t v; |
| 81 | |
| 82 | v = *(const int16_t *)ptr; |
| 83 | return rbo ? GUINT16_SWAP_LE_BE(v) : v; |
| 84 | } |
| 85 | case 32: |
| 86 | { |
| 87 | int32_t v; |
| 88 | |
| 89 | v = *(const int32_t *)ptr; |
| 90 | return rbo ? GUINT32_SWAP_LE_BE(v) : v; |
| 91 | } |
| 92 | case 64: |
| 93 | { |
| 94 | int64_t v; |
| 95 | |
| 96 | v = *(const int64_t *)ptr; |
| 97 | return rbo ? GUINT64_SWAP_LE_BE(v) : v; |
| 98 | } |
| 99 | default: |
| 100 | assert(0); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | size_t uint_write(uint8_t *ptr, size_t len, int byte_order, uint64_t v) |
| 105 | { |
| 106 | int rbo = (byte_order != BYTE_ORDER); /* reverse byte order */ |
| 107 | |
| 108 | if (!ptr) |
| 109 | goto end; |
| 110 | |
| 111 | switch (len) { |
| 112 | case 8: *(uint8_t *)ptr = (uint8_t) v; |
| 113 | break; |
| 114 | case 16: |
| 115 | *(uint16_t *)ptr = rbo ? GUINT16_SWAP_LE_BE((uint16_t) v) : |
| 116 | (uint16_t) v; |
| 117 | break; |
| 118 | case 32: |
| 119 | *(uint32_t *)ptr = rbo ? GUINT32_SWAP_LE_BE((uint32_t) v) : |
| 120 | (uint32_t) v; |
| 121 | break; |
| 122 | case 64: |
| 123 | *(uint64_t *)ptr = rbo ? GUINT64_SWAP_LE_BE(v) : v; |
| 124 | break; |
| 125 | default: |
| 126 | assert(0); |
| 127 | } |
| 128 | end: |
| 129 | return len; |
| 130 | } |
| 131 | |
| 132 | size_t int_write(uint8_t *ptr, size_t len, int byte_order, int64_t v) |
| 133 | { |
| 134 | int rbo = (byte_order != BYTE_ORDER); /* reverse byte order */ |
| 135 | |
| 136 | if (!ptr) |
| 137 | goto end; |
| 138 | |
| 139 | switch (len) { |
| 140 | case 8: *(int8_t *)ptr = (int8_t) v; |
| 141 | break; |
| 142 | case 16: |
| 143 | *(int16_t *)ptr = rbo ? GUINT16_SWAP_LE_BE((int16_t) v) : |
| 144 | (int16_t) v; |
| 145 | break; |
| 146 | case 32: |
| 147 | *(int32_t *)ptr = rbo ? GUINT32_SWAP_LE_BE((int32_t) v) : |
| 148 | (int32_t) v; |
| 149 | break; |
| 150 | case 64: |
| 151 | *(int64_t *)ptr = rbo ? GUINT64_SWAP_LE_BE(v) : v; |
| 152 | break; |
| 153 | default: |
| 154 | assert(0); |
| 155 | } |
| 156 | end: |
| 157 | return len; |
| 158 | } |