| 1 | /* |
| 2 | * Common Trace Format |
| 3 | * |
| 4 | * Bitfields 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/bitfield.h> |
| 24 | #include <endian.h> |
| 25 | |
| 26 | uint64_t ctf_bitfield_unsigned_read(struct stream_pos *pos, |
| 27 | const struct type_class_bitfield *bitfield_class) |
| 28 | { |
| 29 | uint64_t v; |
| 30 | |
| 31 | align_pos(pos, bitfield_class->p.p.alignment); |
| 32 | if (bitfield_class->p.byte_order == LITTLE_ENDIAN) |
| 33 | ctf_bitfield_read_le(pos->base, pos->offset, |
| 34 | bitfield_class->p.len, &v); |
| 35 | else |
| 36 | ctf_bitfield_read_be(pos->base, pos->offset, |
| 37 | bitfield_class->p.len, &v); |
| 38 | move_pos(pos, bitfield_class->p.len); |
| 39 | return v; |
| 40 | } |
| 41 | |
| 42 | int64_t ctf_bitfield_signed_read(struct stream_pos *pos, |
| 43 | const struct type_class_bitfield *bitfield_class) |
| 44 | { |
| 45 | int64_t v; |
| 46 | |
| 47 | align_pos(pos, bitfield_class->p.p.alignment); |
| 48 | |
| 49 | if (bitfield_class->p.byte_order == LITTLE_ENDIAN) |
| 50 | ctf_bitfield_read_le(pos->base, pos->offset, |
| 51 | bitfield_class->p.len, &v); |
| 52 | else |
| 53 | ctf_bitfield_read_be(pos->base, pos->offset, |
| 54 | bitfield_class->p.len, &v); |
| 55 | move_pos(pos, bitfield_class->p.len); |
| 56 | return v; |
| 57 | } |
| 58 | |
| 59 | void ctf_bitfield_unsigned_write(struct stream_pos *pos, |
| 60 | const struct type_class_bitfield *bitfield_class, |
| 61 | uint64_t v) |
| 62 | { |
| 63 | align_pos(pos, bitfield_class->p.p.alignment); |
| 64 | if (pos->dummy) |
| 65 | goto end; |
| 66 | if (bitfield_class->p.byte_order == LITTLE_ENDIAN) |
| 67 | ctf_bitfield_write_le(pos->base, pos->offset, |
| 68 | bitfield_class->p.len, v); |
| 69 | else |
| 70 | ctf_bitfield_write_be(pos->base, pos->offset, |
| 71 | bitfield_class->p.len,, v); |
| 72 | end: |
| 73 | move_pos(pos, bitfield_class->p.len); |
| 74 | } |
| 75 | |
| 76 | void ctf_bitfield_signed_write(struct stream_pos *pos, |
| 77 | const struct type_class_bitfield *bitfield_class, |
| 78 | int64_t v) |
| 79 | { |
| 80 | align_pos(pos, bitfield_class->p.p.alignment); |
| 81 | if (pos->dummy) |
| 82 | goto end; |
| 83 | if (bitfield_class->p.byte_order == LITTLE_ENDIAN) |
| 84 | ctf_bitfield_write_le(pos->base, pos->offset, |
| 85 | bitfield_class->p.len, v); |
| 86 | else |
| 87 | ctf_bitfield_write_be(pos->base, pos->offset, |
| 88 | bitfield_class->p.len, v); |
| 89 | end: |
| 90 | move_pos(pos, bitfield_class->p.len); |
| 91 | } |