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