update integer text output formatting
[babeltrace.git] / formats / ctf / types / integer.c
CommitLineData
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 25static
d11e9c49
MD
26void _aligned_integer_read(struct stream_pos *ppos,
27 struct definition *definition)
6dc2ca62 28{
d11e9c49
MD
29 struct definition_integer *integer_definition =
30 container_of(definition, struct definition_integer, p);
31 const struct declaration_integer *integer_declaration =
32 integer_definition->declaration;
46322b33 33 struct ctf_stream_pos *pos = ctf_pos(ppos);
f6625916 34 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 35
46322b33 36 ctf_align_pos(pos, integer_declaration->p.alignment);
bed864a7 37 assert(!(pos->offset % CHAR_BIT));
6dc2ca62 38
d11e9c49
MD
39 if (!integer_declaration->signedness) {
40 switch (integer_declaration->len) {
41 case 8:
42 {
43 uint8_t v;
44
45 v = *(const uint8_t *)pos->base;
46 integer_definition->value._unsigned = v;
47 break;
48 }
49 case 16:
50 {
51 uint16_t v;
52
53 v = *(const uint16_t *)pos->base;
54 integer_definition->value._unsigned =
55 rbo ? GUINT16_SWAP_LE_BE(v) : v;
56 break;
57 }
58 case 32:
59 {
60 uint32_t v;
61
62 v = *(const uint32_t *)pos->base;
63 integer_definition->value._unsigned =
64 rbo ? GUINT32_SWAP_LE_BE(v) : v;
65 break;
66 }
67 case 64:
68 {
69 uint64_t v;
70
71 v = *(const uint64_t *)pos->base;
72 integer_definition->value._unsigned =
73 rbo ? GUINT64_SWAP_LE_BE(v) : v;
74 break;
75 }
76 default:
77 assert(0);
78 }
79 } else {
80 switch (integer_declaration->len) {
81 case 8:
82 {
83 int8_t v;
84
85 v = *(const int8_t *)pos->base;
86 integer_definition->value._signed = v;
87 break;
88 }
89 case 16:
90 {
91 int16_t v;
92
93 v = *(const int16_t *)pos->base;
94 integer_definition->value._signed =
95 rbo ? GUINT16_SWAP_LE_BE(v) : v;
96 break;
97 }
98 case 32:
99 {
100 int32_t v;
101
102 v = *(const int32_t *)pos->base;
103 integer_definition->value._signed =
104 rbo ? GUINT32_SWAP_LE_BE(v) : v;
105 break;
106 }
107 case 64:
108 {
109 int64_t v;
110
111 v = *(const int64_t *)pos->base;
112 integer_definition->value._signed =
113 rbo ? GUINT64_SWAP_LE_BE(v) : v;
114 break;
115 }
116 default:
117 assert(0);
118 }
6dc2ca62 119 }
d11e9c49 120 ctf_move_pos(pos, integer_declaration->len);
6dc2ca62
MD
121}
122
7fe00194 123static
d11e9c49
MD
124void _aligned_integer_write(struct stream_pos *ppos,
125 struct definition *definition)
6dc2ca62 126{
d11e9c49
MD
127 struct definition_integer *integer_definition =
128 container_of(definition, struct definition_integer, p);
129 const struct declaration_integer *integer_declaration =
130 integer_definition->declaration;
46322b33 131 struct ctf_stream_pos *pos = ctf_pos(ppos);
f6625916 132 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 133
46322b33 134 ctf_align_pos(pos, integer_declaration->p.alignment);
bed864a7 135 assert(!(pos->offset % CHAR_BIT));
6dc2ca62 136
bed864a7 137 if (pos->dummy)
6dc2ca62 138 goto end;
d11e9c49
MD
139 if (!integer_declaration->signedness) {
140 uint64_t v = integer_definition->value._unsigned;
141
142 switch (integer_declaration->len) {
143 case 8: *(uint8_t *) ctf_get_pos_addr(pos) = (uint8_t) v;
144 break;
145 case 16:
146 *(uint16_t *) ctf_get_pos_addr(pos) = rbo ?
147 GUINT16_SWAP_LE_BE((uint16_t) v) :
148 (uint16_t) v;
149 break;
150 case 32:
151 *(uint32_t *) ctf_get_pos_addr(pos) = rbo ?
152 GUINT32_SWAP_LE_BE((uint32_t) v) :
153 (uint32_t) v;
154 break;
155 case 64:
156 *(uint64_t *) ctf_get_pos_addr(pos) = rbo ?
157 GUINT64_SWAP_LE_BE(v) : v;
158 break;
159 default:
160 assert(0);
161 }
162 } else {
163 int64_t v = integer_definition->value._signed;
164
165 switch (integer_declaration->len) {
166 case 8: *(int8_t *) ctf_get_pos_addr(pos) = (int8_t) v;
167 break;
168 case 16:
169 *(int16_t *) ctf_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 *) ctf_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 *) ctf_get_pos_addr(pos) = rbo ?
180 GUINT64_SWAP_LE_BE(v) : v;
181 break;
182 default:
183 assert(0);
184 }
6dc2ca62
MD
185 }
186end:
46322b33 187 ctf_move_pos(pos, integer_declaration->len);
6dc2ca62
MD
188}
189
d11e9c49 190void ctf_integer_read(struct stream_pos *ppos, struct definition *definition)
6dc2ca62 191{
d11e9c49
MD
192 struct definition_integer *integer_definition =
193 container_of(definition, struct definition_integer, p);
194 const struct declaration_integer *integer_declaration =
195 integer_definition->declaration;
46322b33 196 struct ctf_stream_pos *pos = ctf_pos(ppos);
6dc2ca62 197
46322b33 198 ctf_align_pos(pos, integer_declaration->p.alignment);
d11e9c49
MD
199 if (!integer_declaration->signedness) {
200 if (integer_declaration->byte_order == LITTLE_ENDIAN)
201 bt_bitfield_read_le(pos->base, unsigned long,
202 pos->offset, integer_declaration->len,
203 &integer_definition->value._unsigned);
204 else
205 bt_bitfield_read_be(pos->base, unsigned long,
206 pos->offset, integer_declaration->len,
207 &integer_definition->value._unsigned);
208 } else {
209 if (integer_declaration->byte_order == LITTLE_ENDIAN)
210 bt_bitfield_read_le(pos->base, unsigned long,
211 pos->offset, integer_declaration->len,
212 &integer_definition->value._signed);
213 else
214 bt_bitfield_read_be(pos->base, unsigned long,
215 pos->offset, integer_declaration->len,
216 &integer_definition->value._signed);
6dc2ca62 217 }
46322b33 218 ctf_move_pos(pos, integer_declaration->len);
6dc2ca62 219}
7fe00194 220
d11e9c49 221void ctf_integer_write(struct stream_pos *ppos, struct definition *definition)
7fe00194 222{
d11e9c49
MD
223 struct definition_integer *integer_definition =
224 container_of(definition, struct definition_integer, p);
225 const struct declaration_integer *integer_declaration =
226 integer_definition->declaration;
46322b33
MD
227 struct ctf_stream_pos *pos = ctf_pos(ppos);
228
229 ctf_align_pos(pos, integer_declaration->p.alignment);
7fe00194
MD
230 if (pos->dummy)
231 goto end;
d11e9c49
MD
232 if (!integer_declaration->signedness) {
233 if (integer_declaration->byte_order == LITTLE_ENDIAN)
234 bt_bitfield_write_le(pos->base, unsigned long,
235 pos->offset, integer_declaration->len,
236 integer_definition->value._unsigned);
237 else
238 bt_bitfield_write_be(pos->base, unsigned long,
239 pos->offset, integer_declaration->len,
240 integer_definition->value._unsigned);
241 } else {
242 if (integer_declaration->byte_order == LITTLE_ENDIAN)
243 bt_bitfield_write_le(pos->base, unsigned long,
244 pos->offset, integer_declaration->len,
245 integer_definition->value._signed);
246 else
247 bt_bitfield_write_be(pos->base, unsigned long,
248 pos->offset, integer_declaration->len,
249 integer_definition->value._signed);
250 }
7fe00194 251end:
46322b33 252 ctf_move_pos(pos, integer_declaration->len);
7fe00194 253}
This page took 0.051054 seconds and 4 git commands to generate.