| 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 ctf_uint_read(struct stream_pos *pos, |
| 29 | const struct type_class_integer *int_class) |
| 30 | { |
| 31 | int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */ |
| 32 | |
| 33 | align_pos(pos, int_class->p.alignment); |
| 34 | assert(!(pos->offset % CHAR_BIT)); |
| 35 | switch (int_class->len) { |
| 36 | case 8: |
| 37 | { |
| 38 | uint8_t v; |
| 39 | |
| 40 | v = *(const uint8_t *)pos->base; |
| 41 | move_pos(pos, int_class->len); |
| 42 | return v; |
| 43 | } |
| 44 | case 16: |
| 45 | { |
| 46 | uint16_t v; |
| 47 | |
| 48 | v = *(const uint16_t *)pos->base; |
| 49 | move_pos(pos, int_class->len); |
| 50 | return rbo ? GUINT16_SWAP_LE_BE(v) : v; |
| 51 | } |
| 52 | case 32: |
| 53 | { |
| 54 | uint32_t v; |
| 55 | |
| 56 | v = *(const uint32_t *)pos->base; |
| 57 | move_pos(pos, int_class->len); |
| 58 | return rbo ? GUINT32_SWAP_LE_BE(v) : v; |
| 59 | } |
| 60 | case 64: |
| 61 | { |
| 62 | uint64_t v; |
| 63 | |
| 64 | v = *(const uint64_t *)pos->base; |
| 65 | move_pos(pos, int_class->len); |
| 66 | return rbo ? GUINT64_SWAP_LE_BE(v) : v; |
| 67 | } |
| 68 | default: |
| 69 | assert(0); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | int64_t ctf_int_read(struct stream_pos *pos, |
| 74 | const struct type_class_integer *int_class) |
| 75 | { |
| 76 | int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */ |
| 77 | |
| 78 | align_pos(pos, int_class->p.alignment); |
| 79 | assert(!(pos->offset % CHAR_BIT)); |
| 80 | switch (int_class->len) { |
| 81 | case 8: |
| 82 | { |
| 83 | int8_t v; |
| 84 | |
| 85 | v = *(const int8_t *)pos->base; |
| 86 | move_pos(pos, int_class->len); |
| 87 | return v; |
| 88 | } |
| 89 | case 16: |
| 90 | { |
| 91 | int16_t v; |
| 92 | |
| 93 | v = *(const int16_t *)pos->base; |
| 94 | move_pos(pos, int_class->len); |
| 95 | return rbo ? GUINT16_SWAP_LE_BE(v) : v; |
| 96 | } |
| 97 | case 32: |
| 98 | { |
| 99 | int32_t v; |
| 100 | |
| 101 | v = *(const int32_t *)pos->base; |
| 102 | move_pos(pos, int_class->len); |
| 103 | return rbo ? GUINT32_SWAP_LE_BE(v) : v; |
| 104 | } |
| 105 | case 64: |
| 106 | { |
| 107 | int64_t v; |
| 108 | |
| 109 | v = *(const int64_t *)pos->base; |
| 110 | move_pos(pos, int_class->len); |
| 111 | return rbo ? GUINT64_SWAP_LE_BE(v) : v; |
| 112 | } |
| 113 | default: |
| 114 | assert(0); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void ctf_uint_write(struct stream_pos *pos, |
| 119 | const struct type_class_integer *int_class, |
| 120 | uint64_t v) |
| 121 | { |
| 122 | int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */ |
| 123 | |
| 124 | align_pos(pos, int_class->p.alignment); |
| 125 | assert(!(pos->offset % CHAR_BIT)); |
| 126 | if (pos->dummy) |
| 127 | goto end; |
| 128 | |
| 129 | switch (int_class->len) { |
| 130 | case 8: *(uint8_t *) get_pos_addr(pos) = (uint8_t) v; |
| 131 | break; |
| 132 | case 16: |
| 133 | *(uint16_t *) get_pos_addr(pos) = rbo ? |
| 134 | GUINT16_SWAP_LE_BE((uint16_t) v) : |
| 135 | (uint16_t) v; |
| 136 | break; |
| 137 | case 32: |
| 138 | *(uint32_t *) get_pos_addr(pos) = rbo ? |
| 139 | GUINT32_SWAP_LE_BE((uint32_t) v) : |
| 140 | (uint32_t) v; |
| 141 | break; |
| 142 | case 64: |
| 143 | *(uint64_t *) get_pos_addr(pos) = rbo ? |
| 144 | GUINT64_SWAP_LE_BE(v) : v; |
| 145 | break; |
| 146 | default: |
| 147 | assert(0); |
| 148 | } |
| 149 | end: |
| 150 | move_pos(pos, int_class->len); |
| 151 | } |
| 152 | |
| 153 | void ctf_int_write(struct stream_pos *pos, |
| 154 | const struct type_class_integer *int_class, |
| 155 | int64_t v) |
| 156 | { |
| 157 | int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */ |
| 158 | |
| 159 | align_pos(pos, int_class->p.alignment); |
| 160 | assert(!(pos->offset % CHAR_BIT)); |
| 161 | if (pos->dummy) |
| 162 | goto end; |
| 163 | |
| 164 | switch (int_class->len) { |
| 165 | case 8: *(int8_t *) get_pos_addr(pos) = (int8_t) v; |
| 166 | break; |
| 167 | case 16: |
| 168 | *(int16_t *) get_pos_addr(pos) = rbo ? |
| 169 | GUINT16_SWAP_LE_BE((int16_t) v) : |
| 170 | (int16_t) v; |
| 171 | break; |
| 172 | case 32: |
| 173 | *(int32_t *) get_pos_addr(pos) = rbo ? |
| 174 | GUINT32_SWAP_LE_BE((int32_t) v) : |
| 175 | (int32_t) v; |
| 176 | break; |
| 177 | case 64: |
| 178 | *(int64_t *) get_pos_addr(pos) = rbo ? |
| 179 | GUINT64_SWAP_LE_BE(v) : v; |
| 180 | break; |
| 181 | default: |
| 182 | assert(0); |
| 183 | } |
| 184 | end: |
| 185 | move_pos(pos, int_class->len); |
| 186 | return; |
| 187 | } |