| 1 | /* |
| 2 | * Common Trace Format |
| 3 | * |
| 4 | * Integers read/write functions. |
| 5 | * |
| 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | */ |
| 18 | |
| 19 | #include <babeltrace/ctf/types.h> |
| 20 | #include <babeltrace/bitfield.h> |
| 21 | #include <stdint.h> |
| 22 | #include <glib.h> |
| 23 | #include <endian.h> |
| 24 | |
| 25 | static |
| 26 | uint64_t _aligned_uint_read(struct stream_pos *pos, |
| 27 | const struct declaration_integer *integer_declaration) |
| 28 | { |
| 29 | int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */ |
| 30 | |
| 31 | align_pos(pos, integer_declaration->p.alignment); |
| 32 | assert(!(pos->offset % CHAR_BIT)); |
| 33 | switch (integer_declaration->len) { |
| 34 | case 8: |
| 35 | { |
| 36 | uint8_t v; |
| 37 | |
| 38 | v = *(const uint8_t *)pos->base; |
| 39 | move_pos(pos, integer_declaration->len); |
| 40 | return v; |
| 41 | } |
| 42 | case 16: |
| 43 | { |
| 44 | uint16_t v; |
| 45 | |
| 46 | v = *(const uint16_t *)pos->base; |
| 47 | move_pos(pos, integer_declaration->len); |
| 48 | return rbo ? GUINT16_SWAP_LE_BE(v) : v; |
| 49 | } |
| 50 | case 32: |
| 51 | { |
| 52 | uint32_t v; |
| 53 | |
| 54 | v = *(const uint32_t *)pos->base; |
| 55 | move_pos(pos, integer_declaration->len); |
| 56 | return rbo ? GUINT32_SWAP_LE_BE(v) : v; |
| 57 | } |
| 58 | case 64: |
| 59 | { |
| 60 | uint64_t v; |
| 61 | |
| 62 | v = *(const uint64_t *)pos->base; |
| 63 | move_pos(pos, integer_declaration->len); |
| 64 | return rbo ? GUINT64_SWAP_LE_BE(v) : v; |
| 65 | } |
| 66 | default: |
| 67 | assert(0); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static |
| 72 | int64_t _aligned_int_read(struct stream_pos *pos, |
| 73 | const struct declaration_integer *integer_declaration) |
| 74 | { |
| 75 | int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */ |
| 76 | |
| 77 | align_pos(pos, integer_declaration->p.alignment); |
| 78 | assert(!(pos->offset % CHAR_BIT)); |
| 79 | switch (integer_declaration->len) { |
| 80 | case 8: |
| 81 | { |
| 82 | int8_t v; |
| 83 | |
| 84 | v = *(const int8_t *)pos->base; |
| 85 | move_pos(pos, integer_declaration->len); |
| 86 | return v; |
| 87 | } |
| 88 | case 16: |
| 89 | { |
| 90 | int16_t v; |
| 91 | |
| 92 | v = *(const int16_t *)pos->base; |
| 93 | move_pos(pos, integer_declaration->len); |
| 94 | return rbo ? GUINT16_SWAP_LE_BE(v) : v; |
| 95 | } |
| 96 | case 32: |
| 97 | { |
| 98 | int32_t v; |
| 99 | |
| 100 | v = *(const int32_t *)pos->base; |
| 101 | move_pos(pos, integer_declaration->len); |
| 102 | return rbo ? GUINT32_SWAP_LE_BE(v) : v; |
| 103 | } |
| 104 | case 64: |
| 105 | { |
| 106 | int64_t v; |
| 107 | |
| 108 | v = *(const int64_t *)pos->base; |
| 109 | move_pos(pos, integer_declaration->len); |
| 110 | return rbo ? GUINT64_SWAP_LE_BE(v) : v; |
| 111 | } |
| 112 | default: |
| 113 | assert(0); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static |
| 118 | void _aligned_uint_write(struct stream_pos *pos, |
| 119 | const struct declaration_integer *integer_declaration, |
| 120 | uint64_t v) |
| 121 | { |
| 122 | int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */ |
| 123 | |
| 124 | align_pos(pos, integer_declaration->p.alignment); |
| 125 | assert(!(pos->offset % CHAR_BIT)); |
| 126 | if (pos->dummy) |
| 127 | goto end; |
| 128 | |
| 129 | switch (integer_declaration->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, integer_declaration->len); |
| 151 | } |
| 152 | |
| 153 | static |
| 154 | void _aligned_int_write(struct stream_pos *pos, |
| 155 | const struct declaration_integer *integer_declaration, |
| 156 | int64_t v) |
| 157 | { |
| 158 | int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */ |
| 159 | |
| 160 | align_pos(pos, integer_declaration->p.alignment); |
| 161 | assert(!(pos->offset % CHAR_BIT)); |
| 162 | if (pos->dummy) |
| 163 | goto end; |
| 164 | |
| 165 | switch (integer_declaration->len) { |
| 166 | case 8: *(int8_t *) get_pos_addr(pos) = (int8_t) v; |
| 167 | break; |
| 168 | case 16: |
| 169 | *(int16_t *) get_pos_addr(pos) = rbo ? |
| 170 | GUINT16_SWAP_LE_BE((int16_t) v) : |
| 171 | (int16_t) v; |
| 172 | break; |
| 173 | case 32: |
| 174 | *(int32_t *) get_pos_addr(pos) = rbo ? |
| 175 | GUINT32_SWAP_LE_BE((int32_t) v) : |
| 176 | (int32_t) v; |
| 177 | break; |
| 178 | case 64: |
| 179 | *(int64_t *) get_pos_addr(pos) = rbo ? |
| 180 | GUINT64_SWAP_LE_BE(v) : v; |
| 181 | break; |
| 182 | default: |
| 183 | assert(0); |
| 184 | } |
| 185 | end: |
| 186 | move_pos(pos, integer_declaration->len); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | uint64_t ctf_uint_read(struct stream_pos *pos, |
| 191 | const struct declaration_integer *integer_declaration) |
| 192 | { |
| 193 | uint64_t v = 0; |
| 194 | |
| 195 | align_pos(pos, integer_declaration->p.alignment); |
| 196 | if (integer_declaration->byte_order == LITTLE_ENDIAN) |
| 197 | bt_bitfield_read_le(pos->base, unsigned long, pos->offset, |
| 198 | integer_declaration->len, &v); |
| 199 | else |
| 200 | bt_bitfield_read_be(pos->base, unsigned long, pos->offset, |
| 201 | integer_declaration->len, &v); |
| 202 | move_pos(pos, integer_declaration->len); |
| 203 | return v; |
| 204 | } |
| 205 | |
| 206 | int64_t ctf_int_read(struct stream_pos *pos, |
| 207 | const struct declaration_integer *integer_declaration) |
| 208 | { |
| 209 | int64_t v = 0; |
| 210 | |
| 211 | align_pos(pos, integer_declaration->p.alignment); |
| 212 | if (integer_declaration->byte_order == LITTLE_ENDIAN) |
| 213 | bt_bitfield_read_le(pos->base, unsigned long, pos->offset, |
| 214 | integer_declaration->len, &v); |
| 215 | else |
| 216 | bt_bitfield_read_be(pos->base, unsigned long, pos->offset, |
| 217 | integer_declaration->len, &v); |
| 218 | move_pos(pos, integer_declaration->len); |
| 219 | return v; |
| 220 | } |
| 221 | |
| 222 | void ctf_uint_write(struct stream_pos *pos, |
| 223 | const struct declaration_integer *integer_declaration, |
| 224 | uint64_t v) |
| 225 | { |
| 226 | align_pos(pos, integer_declaration->p.alignment); |
| 227 | if (pos->dummy) |
| 228 | goto end; |
| 229 | if (integer_declaration->byte_order == LITTLE_ENDIAN) |
| 230 | bt_bitfield_write_le(pos->base, unsigned long, pos->offset, |
| 231 | integer_declaration->len, v); |
| 232 | else |
| 233 | bt_bitfield_write_be(pos->base, unsigned long, pos->offset, |
| 234 | integer_declaration->len, v); |
| 235 | end: |
| 236 | move_pos(pos, integer_declaration->len); |
| 237 | } |
| 238 | |
| 239 | void ctf_int_write(struct stream_pos *pos, |
| 240 | const struct declaration_integer *integer_declaration, |
| 241 | int64_t v) |
| 242 | { |
| 243 | align_pos(pos, integer_declaration->p.alignment); |
| 244 | if (pos->dummy) |
| 245 | goto end; |
| 246 | if (integer_declaration->byte_order == LITTLE_ENDIAN) |
| 247 | bt_bitfield_write_le(pos->base, unsigned long, pos->offset, |
| 248 | integer_declaration->len, v); |
| 249 | else |
| 250 | bt_bitfield_write_be(pos->base, unsigned long, pos->offset, |
| 251 | integer_declaration->len, v); |
| 252 | end: |
| 253 | move_pos(pos, integer_declaration->len); |
| 254 | } |