e0de8052940349dbd5d1ff565b8badee08890b1b
[babeltrace.git] / formats / ctf / types / integer.c
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 type_class_integer *int_class)
28 {
29 int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */
30
31 align_pos(pos, int_class->p.alignment);
32 assert(!(pos->offset % CHAR_BIT));
33 switch (int_class->len) {
34 case 8:
35 {
36 uint8_t v;
37
38 v = *(const uint8_t *)pos->base;
39 move_pos(pos, int_class->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, int_class->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, int_class->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, int_class->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 type_class_integer *int_class)
74 {
75 int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */
76
77 align_pos(pos, int_class->p.alignment);
78 assert(!(pos->offset % CHAR_BIT));
79 switch (int_class->len) {
80 case 8:
81 {
82 int8_t v;
83
84 v = *(const int8_t *)pos->base;
85 move_pos(pos, int_class->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, int_class->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, int_class->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, int_class->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 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 static
154 void _aligned_int_write(struct stream_pos *pos,
155 const struct type_class_integer *int_class,
156 int64_t v)
157 {
158 int rbo = (int_class->byte_order != BYTE_ORDER); /* reverse byte order */
159
160 align_pos(pos, int_class->p.alignment);
161 assert(!(pos->offset % CHAR_BIT));
162 if (pos->dummy)
163 goto end;
164
165 switch (int_class->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, int_class->len);
187 return;
188 }
189
190 uint64_t ctf_uint_read(struct stream_pos *pos,
191 const struct type_class_integer *int_class)
192 {
193 uint64_t v;
194
195 align_pos(pos, int_class->p.alignment);
196 if (int_class->byte_order == LITTLE_ENDIAN)
197 ctf_bitfield_read_le(pos->base, pos->offset,
198 int_class->len, &v);
199 else
200 ctf_bitfield_read_be(pos->base, pos->offset,
201 int_class->len, &v);
202 move_pos(pos, int_class->len);
203 return v;
204 }
205
206 int64_t ctf_int_read(struct stream_pos *pos,
207 const struct type_class_integer *int_class)
208 {
209 int64_t v;
210
211 align_pos(pos, int_class->p.alignment);
212 if (int_class->byte_order == LITTLE_ENDIAN)
213 ctf_bitfield_read_le(pos->base, pos->offset,
214 int_class->len, &v);
215 else
216 ctf_bitfield_read_be(pos->base, pos->offset,
217 int_class->len, &v);
218 move_pos(pos, int_class->len);
219 return v;
220 }
221
222 void ctf_uint_write(struct stream_pos *pos,
223 const struct type_class_integer *int_class,
224 uint64_t v)
225 {
226 align_pos(pos, int_class->p.alignment);
227 if (pos->dummy)
228 goto end;
229 if (int_class->byte_order == LITTLE_ENDIAN)
230 ctf_bitfield_write_le(pos->base, pos->offset,
231 int_class->len, v);
232 else
233 ctf_bitfield_write_be(pos->base, pos->offset,
234 int_class->len, v);
235 end:
236 move_pos(pos, int_class->len);
237 }
238
239 void ctf_int_write(struct stream_pos *pos,
240 const struct type_class_integer *int_class,
241 int64_t v)
242 {
243 align_pos(pos, int_class->p.alignment);
244 if (pos->dummy)
245 goto end;
246 if (int_class->byte_order == LITTLE_ENDIAN)
247 ctf_bitfield_write_le(pos->base, pos->offset,
248 int_class->len, v);
249 else
250 ctf_bitfield_write_be(pos->base, pos->offset,
251 int_class->len, v);
252 end:
253 move_pos(pos, int_class->len);
254 }
This page took 0.033317 seconds and 3 git commands to generate.