Commit | Line | Data |
---|---|---|
6dc2ca62 MD |
1 | /* |
2 | * Common Trace Format | |
3 | * | |
4 | * Integers read/write functions. | |
5 | * | |
6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * Dual LGPL v2.1/GPL v2 license. | |
9 | */ | |
10 | ||
11 | #include <ctf/ctf-types.h> | |
12 | #include <stdint.h> | |
13 | #include <glib.h> | |
14 | #include <endian.h> | |
15 | ||
16 | uint64_t uint_read(const uint8_t *ptr, size_t len, int byte_order) | |
17 | { | |
18 | int rbo = (byte_order != BYTE_ORDER); /* reverse byte order */ | |
19 | ||
20 | switch (len) { | |
21 | case 8: | |
22 | { | |
23 | uint8_t v; | |
24 | ||
25 | v = *(const uint8_t *)ptr; | |
26 | return v; | |
27 | } | |
28 | case 16: | |
29 | { | |
30 | uint16_t v; | |
31 | ||
32 | v = *(const uint16_t *)ptr; | |
33 | return rbo ? GUINT16_SWAP_LE_BE(v) : v; | |
34 | } | |
35 | case 32: | |
36 | { | |
37 | uint32_t v; | |
38 | ||
39 | v = *(const uint32_t *)ptr; | |
40 | return rbo ? GUINT32_SWAP_LE_BE(v) : v; | |
41 | } | |
42 | case 64: | |
43 | { | |
44 | uint64_t v; | |
45 | ||
46 | v = *(const uint64_t *)ptr; | |
47 | return rbo ? GUINT64_SWAP_LE_BE(v) : v; | |
48 | } | |
49 | default: | |
50 | assert(0); | |
51 | } | |
52 | } | |
53 | ||
54 | int64_t int_read(const uint8_t *ptr, size_t len, int byte_order) | |
55 | { | |
56 | int rbo = (byte_order != BYTE_ORDER); /* reverse byte order */ | |
57 | ||
58 | switch (len) { | |
59 | case 8: | |
60 | { | |
61 | int8_t v; | |
62 | ||
63 | v = *(const int8_t *)ptr; | |
64 | return v; | |
65 | } | |
66 | case 16: | |
67 | { | |
68 | int16_t v; | |
69 | ||
70 | v = *(const int16_t *)ptr; | |
71 | return rbo ? GUINT16_SWAP_LE_BE(v) : v; | |
72 | } | |
73 | case 32: | |
74 | { | |
75 | int32_t v; | |
76 | ||
77 | v = *(const int32_t *)ptr; | |
78 | return rbo ? GUINT32_SWAP_LE_BE(v) : v; | |
79 | } | |
80 | case 64: | |
81 | { | |
82 | int64_t v; | |
83 | ||
84 | v = *(const int64_t *)ptr; | |
85 | return rbo ? GUINT64_SWAP_LE_BE(v) : v; | |
86 | } | |
87 | default: | |
88 | assert(0); | |
89 | } | |
90 | } | |
91 | ||
92 | size_t uint_write(uint8_t *ptr, size_t len, int byte_order, uint64_t v) | |
93 | { | |
94 | int rbo = (byte_order != BYTE_ORDER); /* reverse byte order */ | |
95 | ||
96 | if (!ptr) | |
97 | goto end; | |
98 | ||
99 | switch (len) { | |
100 | case 8: *(uint8_t *)ptr = (uint8_t) v; | |
101 | break; | |
102 | case 16: | |
103 | *(uint16_t *)ptr = rbo ? GUINT16_SWAP_LE_BE((uint16_t) v) : | |
104 | (uint16_t) v; | |
105 | break; | |
106 | case 32: | |
107 | *(uint32_t *)ptr = rbo ? GUINT32_SWAP_LE_BE((uint32_t) v) : | |
108 | (uint32_t) v; | |
109 | break; | |
110 | case 64: | |
111 | *(uint64_t *)ptr = rbo ? GUINT64_SWAP_LE_BE(v) : v; | |
112 | break; | |
113 | default: | |
114 | assert(0); | |
115 | } | |
116 | end: | |
117 | return len; | |
118 | } | |
119 | ||
120 | size_t int_write(uint8_t *ptr, size_t len, int byte_order, int64_t v) | |
121 | { | |
122 | int rbo = (byte_order != BYTE_ORDER); /* reverse byte order */ | |
123 | ||
124 | if (!ptr) | |
125 | goto end; | |
126 | ||
127 | switch (len) { | |
128 | case 8: *(int8_t *)ptr = (int8_t) v; | |
129 | break; | |
130 | case 16: | |
131 | *(int16_t *)ptr = rbo ? GUINT16_SWAP_LE_BE((int16_t) v) : | |
132 | (int16_t) v; | |
133 | break; | |
134 | case 32: | |
135 | *(int32_t *)ptr = rbo ? GUINT32_SWAP_LE_BE((int32_t) v) : | |
136 | (int32_t) v; | |
137 | break; | |
138 | case 64: | |
139 | *(int64_t *)ptr = rbo ? GUINT64_SWAP_LE_BE(v) : v; | |
140 | break; | |
141 | default: | |
142 | assert(0); | |
143 | } | |
144 | end: | |
145 | return len; | |
146 | } |