Add single-stream tests
[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
MD
25static
26uint64_t _aligned_uint_read(struct stream_pos *pos,
f6625916 27 const struct declaration_integer *integer_declaration)
6dc2ca62 28{
f6625916 29 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 30
f6625916 31 align_pos(pos, integer_declaration->p.alignment);
bed864a7 32 assert(!(pos->offset % CHAR_BIT));
f6625916 33 switch (integer_declaration->len) {
6dc2ca62
MD
34 case 8:
35 {
36 uint8_t v;
37
bed864a7 38 v = *(const uint8_t *)pos->base;
f6625916 39 move_pos(pos, integer_declaration->len);
6dc2ca62
MD
40 return v;
41 }
42 case 16:
43 {
44 uint16_t v;
45
bed864a7 46 v = *(const uint16_t *)pos->base;
f6625916 47 move_pos(pos, integer_declaration->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;
f6625916 55 move_pos(pos, integer_declaration->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;
f6625916 63 move_pos(pos, integer_declaration->len);
6dc2ca62
MD
64 return rbo ? GUINT64_SWAP_LE_BE(v) : v;
65 }
66 default:
67 assert(0);
68 }
69}
70
7fe00194
MD
71static
72int64_t _aligned_int_read(struct stream_pos *pos,
f6625916 73 const struct declaration_integer *integer_declaration)
6dc2ca62 74{
f6625916 75 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 76
f6625916 77 align_pos(pos, integer_declaration->p.alignment);
bed864a7 78 assert(!(pos->offset % CHAR_BIT));
f6625916 79 switch (integer_declaration->len) {
6dc2ca62
MD
80 case 8:
81 {
82 int8_t v;
83
bed864a7 84 v = *(const int8_t *)pos->base;
f6625916 85 move_pos(pos, integer_declaration->len);
6dc2ca62
MD
86 return v;
87 }
88 case 16:
89 {
90 int16_t v;
91
bed864a7 92 v = *(const int16_t *)pos->base;
f6625916 93 move_pos(pos, integer_declaration->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;
f6625916 101 move_pos(pos, integer_declaration->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;
f6625916 109 move_pos(pos, integer_declaration->len);
6dc2ca62
MD
110 return rbo ? GUINT64_SWAP_LE_BE(v) : v;
111 }
112 default:
113 assert(0);
114 }
115}
116
7fe00194
MD
117static
118void _aligned_uint_write(struct stream_pos *pos,
f6625916 119 const struct declaration_integer *integer_declaration,
bed864a7 120 uint64_t v)
6dc2ca62 121{
f6625916 122 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 123
f6625916 124 align_pos(pos, integer_declaration->p.alignment);
bed864a7
MD
125 assert(!(pos->offset % CHAR_BIT));
126 if (pos->dummy)
6dc2ca62
MD
127 goto end;
128
f6625916 129 switch (integer_declaration->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 }
149end:
f6625916 150 move_pos(pos, integer_declaration->len);
6dc2ca62
MD
151}
152
7fe00194
MD
153static
154void _aligned_int_write(struct stream_pos *pos,
f6625916 155 const struct declaration_integer *integer_declaration,
bed864a7 156 int64_t v)
6dc2ca62 157{
f6625916 158 int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
6dc2ca62 159
f6625916 160 align_pos(pos, integer_declaration->p.alignment);
bed864a7
MD
161 assert(!(pos->offset % CHAR_BIT));
162 if (pos->dummy)
6dc2ca62
MD
163 goto end;
164
f6625916 165 switch (integer_declaration->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 }
185end:
f6625916 186 move_pos(pos, integer_declaration->len);
bed864a7 187 return;
6dc2ca62 188}
7fe00194
MD
189
190uint64_t ctf_uint_read(struct stream_pos *pos,
f6625916 191 const struct declaration_integer *integer_declaration)
7fe00194 192{
47e0f2e2 193 uint64_t v = 0;
7fe00194 194
f6625916
MD
195 align_pos(pos, integer_declaration->p.alignment);
196 if (integer_declaration->byte_order == LITTLE_ENDIAN)
47e0f2e2 197 bt_bitfield_read_le(pos->base, unsigned long, pos->offset,
f6625916 198 integer_declaration->len, &v);
7fe00194 199 else
47e0f2e2 200 bt_bitfield_read_be(pos->base, unsigned long, pos->offset,
f6625916
MD
201 integer_declaration->len, &v);
202 move_pos(pos, integer_declaration->len);
7fe00194
MD
203 return v;
204}
205
206int64_t ctf_int_read(struct stream_pos *pos,
f6625916 207 const struct declaration_integer *integer_declaration)
7fe00194 208{
47e0f2e2 209 int64_t v = 0;
7fe00194 210
f6625916
MD
211 align_pos(pos, integer_declaration->p.alignment);
212 if (integer_declaration->byte_order == LITTLE_ENDIAN)
47e0f2e2 213 bt_bitfield_read_le(pos->base, unsigned long, pos->offset,
f6625916 214 integer_declaration->len, &v);
7fe00194 215 else
47e0f2e2 216 bt_bitfield_read_be(pos->base, unsigned long, pos->offset,
f6625916
MD
217 integer_declaration->len, &v);
218 move_pos(pos, integer_declaration->len);
7fe00194
MD
219 return v;
220}
221
222void ctf_uint_write(struct stream_pos *pos,
f6625916 223 const struct declaration_integer *integer_declaration,
7fe00194
MD
224 uint64_t v)
225{
f6625916 226 align_pos(pos, integer_declaration->p.alignment);
7fe00194
MD
227 if (pos->dummy)
228 goto end;
f6625916 229 if (integer_declaration->byte_order == LITTLE_ENDIAN)
47e0f2e2 230 bt_bitfield_write_le(pos->base, unsigned long, pos->offset,
f6625916 231 integer_declaration->len, v);
7fe00194 232 else
47e0f2e2 233 bt_bitfield_write_be(pos->base, unsigned long, pos->offset,
f6625916 234 integer_declaration->len, v);
7fe00194 235end:
f6625916 236 move_pos(pos, integer_declaration->len);
7fe00194
MD
237}
238
239void ctf_int_write(struct stream_pos *pos,
f6625916 240 const struct declaration_integer *integer_declaration,
7fe00194
MD
241 int64_t v)
242{
f6625916 243 align_pos(pos, integer_declaration->p.alignment);
7fe00194
MD
244 if (pos->dummy)
245 goto end;
f6625916 246 if (integer_declaration->byte_order == LITTLE_ENDIAN)
47e0f2e2 247 bt_bitfield_write_le(pos->base, unsigned long, pos->offset,
f6625916 248 integer_declaration->len, v);
7fe00194 249 else
47e0f2e2 250 bt_bitfield_write_be(pos->base, unsigned long, pos->offset,
f6625916 251 integer_declaration->len, v);
7fe00194 252end:
f6625916 253 move_pos(pos, integer_declaration->len);
7fe00194 254}
This page took 0.053537 seconds and 4 git commands to generate.