Commit | Line | Data |
---|---|---|
fc93b2bd MD |
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(const unsigned char *ptr, | |
27 | unsigned long start, unsigned long len, | |
28 | int byte_order) | |
29 | { | |
30 | uint64_t v; | |
31 | ||
32 | if (byte_order == LITTLE_ENDIAN) | |
33 | ctf_bitfield_read_le(ptr, start, len, &v); | |
34 | else | |
35 | ctf_bitfield_read_be(ptr, start, len, &v); | |
36 | return v; | |
37 | } | |
38 | ||
39 | int64_t ctf_bitfield_signed_read(const unsigned char *ptr, | |
40 | unsigned long start, unsigned long len, | |
41 | int byte_order) | |
42 | { | |
43 | int64_t v; | |
44 | ||
45 | if (byte_order == LITTLE_ENDIAN) | |
46 | ctf_bitfield_read_le(ptr, start, len, &v); | |
47 | else | |
48 | ctf_bitfield_read_be(ptr, start, len, &v); | |
49 | return v; | |
50 | } | |
51 | ||
52 | size_t ctf_bitfield_unsigned_write(unsigned char *ptr, | |
53 | unsigned long start, unsigned long len, | |
54 | int byte_order, uint64_t v) | |
55 | { | |
56 | if (!ptr) | |
57 | goto end; | |
58 | if (byte_order == LITTLE_ENDIAN) | |
59 | ctf_bitfield_write_le(ptr, start, len, v); | |
60 | else | |
61 | ctf_bitfield_write_be(ptr, start, len, v); | |
62 | end: | |
63 | return len; | |
64 | } | |
65 | ||
66 | size_t ctf_bitfield_signed_write(unsigned char *ptr, | |
67 | unsigned long start, unsigned long len, | |
68 | int byte_order, int64_t v) | |
69 | { | |
70 | if (!ptr) | |
71 | goto end; | |
72 | if (byte_order == LITTLE_ENDIAN) | |
73 | ctf_bitfield_write_le(ptr, start, len, v); | |
74 | else | |
75 | ctf_bitfield_write_be(ptr, start, len, v); | |
76 | end: | |
77 | return len; | |
78 | } |