-
Notifications
You must be signed in to change notification settings - Fork 588
/
Message.h
259 lines (217 loc) · 8.6 KB
/
Message.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef Message_H
#define Message_H
#include "exception/Er.h"
#include "memory/Allocator.h"
#include "util/Bits.h"
#include "util/Endian.h"
#include "util/Linker.h"
Linker_require("wire/Message.c")
#include <stdint.h>
typedef struct Message
{
/** The length of the message. */
int32_t _length;
/** The number of bytes of padding BEFORE where bytes begins. */
int32_t _padding;
/** The content. */
uint8_t* msgbytes;
/** Amount of bytes of storage space available in the message. */
int32_t _capacity;
// Amount of associated data
int32_t _adLen;
// Pointer to associated data
uint8_t* _ad;
/**
* When sending/receiving a Message on a unix socket, a file descriptor to attach.
* Caviat: In order to maintain backward compatibility with a Message which is
* allocated using calloc, file descriptor 0 is referred to by -1
*/
int _associatedFd;
#ifdef PARANOIA
/** This is used inside of Iface.h to support Iface_next() */
struct Iface* currentIface;
#endif
/** The allocator which allocated space for this message. */
struct Allocator* _alloc;
} Message_t;
static inline struct Allocator* Message_getAlloc(struct Message* msg)
{
return msg->_alloc;
}
static inline int32_t Message_getLength(struct Message* msg)
{
return msg->_length;
}
static inline Er_DEFUN(void Message_truncate(struct Message* msg, int32_t newLen))
{
if (newLen > msg->_length) {
Er_raise(msg->_alloc, "Message_truncate(%d) message length is %d", newLen, msg->_length);
}
msg->_length = newLen;
Er_ret();
}
static inline int32_t Message_getPadding(struct Message* msg)
{
return msg->_padding;
}
static inline int32_t Message_getCapacity(struct Message* msg)
{
return msg->_capacity;
}
static inline Message_t Message_foreign(uint32_t len, uint8_t* bytes)
{
return (Message_t){ ._length = len, .msgbytes = bytes, ._capacity = len };
}
// static inline Er_DEFUN(void Message_ecopy(struct Message* to, struct Message* from, uint32_t amt))
// {
// }
struct Message* Message_new(uint32_t messageLength,
uint32_t amountOfPadding,
struct Allocator* alloc);
void Message_setAssociatedFd(struct Message* msg, int fd);
int Message_getAssociatedFd(struct Message* msg);
struct Message* Message_clone(struct Message* toClone, struct Allocator* alloc);
static inline Er_DEFUN(uint8_t* Message_peakBytes(struct Message* msg, int32_t len))
{
if (len > msg->_length) {
Er_raise(msg->_alloc, "peakBytes(%d) too big, message length is %d", len, msg->_length);
}
Er_ret(msg->msgbytes);
}
/**
* Pretend to shift the content forward by amount.
* Really it shifts the bytes value backward.
*/
static inline Er_DEFUN(void Message_eshift(struct Message* toShift, int32_t amount))
{
if (amount > 0 && toShift->_padding < amount) {
Er_raise(toShift->_alloc, "buffer overflow adding %d to length %d",
amount, toShift->_length);
} else if (toShift->_length < (-amount)) {
Er_raise(toShift->_alloc, "buffer underflow");
}
toShift->_length += amount;
toShift->_capacity += amount;
toShift->msgbytes -= amount;
toShift->_padding -= amount;
Er_ret();
}
static inline Er_DEFUN(void Message_epushAd(struct Message* restrict msg,
const void* restrict object,
size_t size))
{
if (msg->_padding < (int)size) {
Er_raise(msg->_alloc, "not enough padding to push ad");
}
if (object) {
Bits_memcpy(msg->_ad, object, size);
} else {
Bits_memset(msg->_ad, 0x00, size);
}
msg->_adLen += size;
msg->_padding -= size;
msg->_ad = &msg->_ad[size];
Er_ret();
}
static inline Er_DEFUN(void Message_epopAd(struct Message* restrict msg,
void* restrict object,
size_t size))
{
if (msg->_adLen < (int)size) {
Er_raise(msg->_alloc, "underflow, cannot pop ad");
}
msg->_adLen -= size;
msg->_padding += size;
msg->_ad = &msg->_ad[-((int)size)];
if (object) {
Bits_memcpy(object, msg->_ad, size);
}
Er_ret();
}
static inline void Message_reset(struct Message* toShift)
{
Assert_true(toShift->_length <= toShift->_capacity);
Er_assert(Message_epopAd(toShift, NULL, toShift->_adLen));
toShift->_length = toShift->_capacity;
Er_assert(Message_eshift(toShift, -toShift->_length));
}
static inline Er_DEFUN(void Message_epush(struct Message* restrict msg,
const void* restrict object,
size_t size))
{
Er(Message_eshift(msg, (int)size));
if (object) {
Bits_memcpy(msg->msgbytes, object, size);
} else {
Bits_memset(msg->msgbytes, 0x00, size);
}
Er_ret();
}
static inline Er_DEFUN(void Message_epop(struct Message* restrict msg,
void* restrict object,
size_t size))
{
Er(Message_eshift(msg, -(int)size));
if (object) {
Bits_memcpy(object, &msg->msgbytes[-((int)size)], size);
}
Er_ret();
}
#define Message_pushH(size) \
static inline Er_DEFUN(void Message_epush ## size ## h \
(struct Message* msg, uint ## size ## _t dat)) \
{ \
Er(Message_epush(msg, &dat, (size)/8)); \
Er_ret(); \
}
#define Message_popH(size) \
static inline Er_DEFUN(uint ## size ## _t Message_epop ## size ## h(struct Message* msg)) \
{ \
uint ## size ## _t out; \
Er(Message_epop(msg, &out, (size)/8)); \
Er_ret(out); \
}
#define Message_popBE(size) \
static inline Er_DEFUN(uint ## size ## _t Message_epop ## size ## be(struct Message* msg)) \
{ \
uint ## size ## _t out = Er(Message_epop ## size ## h(msg)); \
uint ## size ## _t out1 = Endian_bigEndianToHost ## size (out); \
Er_ret(out1); \
}
#define Message_popLE(size) \
static inline Er_DEFUN(uint ## size ## _t Message_epop ## size ## le(struct Message* msg)) \
{ \
uint ## size ## _t out = Er(Message_epop ## size ## h(msg)); \
uint ## size ## _t out1 = Endian_littleEndianToHost ## size (out); \
Er_ret(out1); \
}
#define Message_pushPop(size) \
Message_pushH(size) Message_popH(size) Message_popBE(size) Message_popLE(size)
Message_pushH(8)
Message_popH(8)
#define Message_epush8 Message_epush8h
#define Message_epop8 Message_epop8h
Message_pushPop(16)
Message_pushPop(32)
Message_pushPop(64)
#define Message_epush16le(msg, x) Message_epush16h(msg, Endian_hostToLittleEndian16(x))
#define Message_epush32le(msg, x) Message_epush16h(msg, Endian_hostToLittleEndian32(x))
#define Message_epush64le(msg, x) Message_epush16h(msg, Endian_hostToLittleEndian64(x))
#define Message_epush16be(msg, x) Message_epush16h(msg, Endian_hostToBigEndian16(x))
#define Message_epush32be(msg, x) Message_epush32h(msg, Endian_hostToBigEndian32(x))
#define Message_epush64be(msg, x) Message_epush64h(msg, Endian_hostToBigEndian64(x))
#endif