4 * Integers read/write functions.
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <ctf/ctf-types.h>
20 #include <ctf/bitfield.h>
26 uint64_t _aligned_uint_read(struct stream_pos
*pos
,
27 const struct type_class_integer
*int_class
)
29 int rbo
= (int_class
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
31 align_pos(pos
, int_class
->p
.alignment
);
32 assert(!(pos
->offset
% CHAR_BIT
));
33 switch (int_class
->len
) {
38 v
= *(const uint8_t *)pos
->base
;
39 move_pos(pos
, int_class
->len
);
46 v
= *(const uint16_t *)pos
->base
;
47 move_pos(pos
, int_class
->len
);
48 return rbo
? GUINT16_SWAP_LE_BE(v
) : v
;
54 v
= *(const uint32_t *)pos
->base
;
55 move_pos(pos
, int_class
->len
);
56 return rbo
? GUINT32_SWAP_LE_BE(v
) : v
;
62 v
= *(const uint64_t *)pos
->base
;
63 move_pos(pos
, int_class
->len
);
64 return rbo
? GUINT64_SWAP_LE_BE(v
) : v
;
72 int64_t _aligned_int_read(struct stream_pos
*pos
,
73 const struct type_class_integer
*int_class
)
75 int rbo
= (int_class
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
77 align_pos(pos
, int_class
->p
.alignment
);
78 assert(!(pos
->offset
% CHAR_BIT
));
79 switch (int_class
->len
) {
84 v
= *(const int8_t *)pos
->base
;
85 move_pos(pos
, int_class
->len
);
92 v
= *(const int16_t *)pos
->base
;
93 move_pos(pos
, int_class
->len
);
94 return rbo
? GUINT16_SWAP_LE_BE(v
) : v
;
100 v
= *(const int32_t *)pos
->base
;
101 move_pos(pos
, int_class
->len
);
102 return rbo
? GUINT32_SWAP_LE_BE(v
) : v
;
108 v
= *(const int64_t *)pos
->base
;
109 move_pos(pos
, int_class
->len
);
110 return rbo
? GUINT64_SWAP_LE_BE(v
) : v
;
118 void _aligned_uint_write(struct stream_pos
*pos
,
119 const struct type_class_integer
*int_class
,
122 int rbo
= (int_class
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
124 align_pos(pos
, int_class
->p
.alignment
);
125 assert(!(pos
->offset
% CHAR_BIT
));
129 switch (int_class
->len
) {
130 case 8: *(uint8_t *) get_pos_addr(pos
) = (uint8_t) v
;
133 *(uint16_t *) get_pos_addr(pos
) = rbo
?
134 GUINT16_SWAP_LE_BE((uint16_t) v
) :
138 *(uint32_t *) get_pos_addr(pos
) = rbo
?
139 GUINT32_SWAP_LE_BE((uint32_t) v
) :
143 *(uint64_t *) get_pos_addr(pos
) = rbo
?
144 GUINT64_SWAP_LE_BE(v
) : v
;
150 move_pos(pos
, int_class
->len
);
154 void _aligned_int_write(struct stream_pos
*pos
,
155 const struct type_class_integer
*int_class
,
158 int rbo
= (int_class
->byte_order
!= BYTE_ORDER
); /* reverse byte order */
160 align_pos(pos
, int_class
->p
.alignment
);
161 assert(!(pos
->offset
% CHAR_BIT
));
165 switch (int_class
->len
) {
166 case 8: *(int8_t *) get_pos_addr(pos
) = (int8_t) v
;
169 *(int16_t *) get_pos_addr(pos
) = rbo
?
170 GUINT16_SWAP_LE_BE((int16_t) v
) :
174 *(int32_t *) get_pos_addr(pos
) = rbo
?
175 GUINT32_SWAP_LE_BE((int32_t) v
) :
179 *(int64_t *) get_pos_addr(pos
) = rbo
?
180 GUINT64_SWAP_LE_BE(v
) : v
;
186 move_pos(pos
, int_class
->len
);
190 uint64_t ctf_uint_read(struct stream_pos
*pos
,
191 const struct type_class_bitfield
*int_class
)
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
,
200 ctf_bitfield_read_be(pos
->base
, pos
->offset
,
202 move_pos(pos
, int_class
->len
);
206 int64_t ctf_int_read(struct stream_pos
*pos
,
207 const struct type_class_bitfield
*int_class
)
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
,
216 ctf_bitfield_read_be(pos
->base
, pos
->offset
,
218 move_pos(pos
, int_class
->len
);
222 void ctf_uint_write(struct stream_pos
*pos
,
223 const struct type_class_bitfield
*int_class
,
226 align_pos(pos
, int_class
->p
.alignment
);
229 if (int_class
->byte_order
== LITTLE_ENDIAN
)
230 ctf_bitfield_write_le(pos
->base
, pos
->offset
,
233 ctf_bitfield_write_be(pos
->base
, pos
->offset
,
236 move_pos(pos
, int_class
->len
);
239 void ctf_int_write(struct stream_pos
*pos
,
240 const struct type_class_bitfield
*int_class
,
243 align_pos(pos
, int_class
->p
.alignment
);
246 if (int_class
->byte_order
== LITTLE_ENDIAN
)
247 ctf_bitfield_write_le(pos
->base
, pos
->offset
,
250 ctf_bitfield_write_be(pos
->base
, pos
->offset
,
253 move_pos(pos
, int_class
->len
);
This page took 0.034885 seconds and 4 git commands to generate.