]>
Dogcows Code - chaz/yoink/blob - src/Moof/Packet.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #include "../config.h"
21 #include <arpa/inet.h>
30 #define bswap_16(x) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
34 #define bswap_32(x) ((((x) & 0xff000000) >> 24) | \
35 (((x) & 0x00ff0000) >> 8) | \
36 (((x) & 0x0000ff00) << 8) | \
37 (((x) & 0x000000ff) << 24))
41 #define bswap_64(x) (((x) << 56) | \
42 (((x) << 40) & 0xff000000000000ULL) | \
43 (((x) << 24) & 0xff0000000000ULL) | \
44 (((x) << 8) & 0xff00000000ULL) | \
45 (((x) >> 8) & 0xff000000ULL) | \
46 (((x) >> 24) & 0xff0000ULL) | \
47 (((x) >> 40) & 0xff00ULL) | \
53 static uint16_t htons(uint16_t x
)
55 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
61 static uint16_t ntohs(uint16_t x
)
66 static uint32_t htonl(uint32_t x
)
68 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
74 static uint32_t ntohl(uint32_t x
)
81 static uint64_t htonll(uint64_t x
)
83 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
89 static uint64_t ntohll(uint64_t x
)
98 Packet::Packet(size_t size
) :
99 mBuffer((char*)malloc(size
)),
108 Packet::Packet(const char* data
, size_t size
) :
109 mBuffer((char*)malloc(size
)),
118 if (mBuffer
) memcpy(mBuffer
, data
, size
);
122 Packet::Packet(const Packet
& copy
) :
123 mBuffer((char*)malloc(copy
.mSize
)),
129 mBoolNumR(copy
.mBoolNumR
),
130 mBoolNumW(copy
.mBoolNumW
)
132 if (mBuffer
) memcpy(mBuffer
, copy
.mBuffer
, mSize
);
135 Packet
& Packet::operator=(const Packet
& copy
)
139 mBuffer
= (char*)malloc(copy
.mSize
);
143 mBoolR
= copy
.mBoolR
;
144 mBoolW
= copy
.mBoolW
;
145 mBoolNumR
= copy
.mBoolNumR
;
146 mBoolNumW
= copy
.mBoolNumW
;
147 if (mBuffer
) memcpy(mBuffer
, copy
.mBuffer
, mSize
);
158 Packet
& Packet::operator<<(bool value
)
160 int bit
= mBoolNumW
% 8;
165 unsigned char byte
= 0;
166 if (write(&byte
, 1) == 0) return *this;
169 if (value
) mBuffer
[mBoolW
] |= (1 << bit
);
176 Packet
& Packet::operator<<(int8_t value
)
178 return *this << (uint8_t)value
;
181 Packet
& Packet::operator<<(int16_t value
)
183 return *this << (uint16_t)value
;
186 Packet
& Packet::operator<<(int32_t value
)
188 return *this << (uint32_t)value
;
191 Packet
& Packet::operator<<(int64_t value
)
193 return *this << (uint64_t)value
;
197 Packet
& Packet::operator<<(uint8_t value
)
199 write(&value
, sizeof(value
));
203 Packet
& Packet::operator<<(uint16_t value
)
205 value
= htons(value
);
206 write(&value
, sizeof(value
));
210 Packet
& Packet::operator<<(uint32_t value
)
212 value
= htonl(value
);
213 write(&value
, sizeof(value
));
217 Packet
& Packet::operator<<(uint64_t value
)
219 value
= htonll(value
);
220 write(&value
, sizeof(value
));
224 Packet
& Packet::operator<<(float value
)
226 // XXX: assumes the ieee-754
227 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
228 *integer
= htonl(*integer
);
229 write(integer
, sizeof(value
));
233 Packet
& Packet::operator<<(double value
)
235 // XXX: assumes the ieee-754
236 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
237 *integer
= htonll(*integer
);
238 write(integer
, sizeof(value
));
242 size_t Packet::write(const void* bytes
, size_t size
)
244 size_t nBytes
= std::min(size
, mSize
- mW
);
245 if (!mBuffer
|| nBytes
< size
)
247 int nPages
= 1 + size
/ PAGE_SIZE
;
248 int newSize
= mSize
+ nPages
* PAGE_SIZE
;
249 char* newBuffer
= (char*)realloc(mBuffer
, newSize
);
256 if (!mBuffer
) return 0;
258 memcpy(mBuffer
+ mW
, bytes
, nBytes
);
264 Packet
& Packet::operator>>(bool& value
)
266 int bit
= mBoolNumR
% 8;
271 unsigned char byte
= 0;
272 if (read(&byte
, 1) == 0) return *this;
275 value
= 1 & (mBuffer
[mBoolR
] >> bit
);
281 Packet
& Packet::operator>>(int8_t& value
)
283 return *this >> (uint8_t&)value
;
286 Packet
& Packet::operator>>(int16_t& value
)
288 return *this >> (uint16_t&)value
;
291 Packet
& Packet::operator>>(int32_t& value
)
293 return *this >> (uint32_t&)value
;
296 Packet
& Packet::operator>>(int64_t& value
)
298 return *this >> (uint64_t&)value
;
301 Packet
& Packet::operator>>(uint8_t& value
)
303 read(&value
, sizeof(value
));
307 Packet
& Packet::operator>>(uint16_t& value
)
309 read(&value
, sizeof(value
));
310 value
= ntohs(value
);
314 Packet
& Packet::operator>>(uint32_t& value
)
316 read(&value
, sizeof(value
));
317 value
= ntohl(value
);
321 Packet
& Packet::operator>>(uint64_t& value
)
323 read(&value
, sizeof(value
));
324 value
= ntohll(value
);
328 Packet
& Packet::operator>>(float& value
)
330 // XXX: assumes the ieee-754
331 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
332 read(integer
, sizeof(value
));
333 *integer
= htonl(*integer
);
337 Packet
& Packet::operator>>(double& value
)
339 // XXX: assumes the ieee-754
340 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
341 read(integer
, sizeof(value
));
342 *integer
= htonll(*integer
);
346 size_t Packet::read(void* bytes
, size_t size
)
348 size_t nBytes
= std::min(size
, mW
- mR
);
349 memcpy(bytes
, mBuffer
+ mR
, nBytes
);
This page took 0.05191 seconds and 4 git commands to generate.