]>
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 buffer_((char*)malloc(size
)),
102 packet::packet(const char* data
, size_t size
) :
103 buffer_((char*)malloc(size
)),
108 if (buffer_
) memcpy(buffer_
, data
, size
);
109 else throw std::length_error("out of memory");
113 packet::packet(const packet
& copy
) :
114 buffer_((char*)malloc(copy
.size_
)),
119 if (buffer_
) memcpy(buffer_
, copy
.buffer_
, size_
);
120 else throw std::length_error("out of memory");
123 packet
& packet::operator = (const packet
& copy
)
126 buffer_
= (char*)malloc(copy
.size_
);
128 if (buffer_
) memcpy(buffer_
, copy
.buffer_
, size_
);
129 else throw std::length_error("out of memory");
131 state_
= copy
.state_
;
132 saved_
= copy
.saved_
;
143 packet
& packet::operator << (bool value
)
145 int bit
= state_
.write_bool_num
% 8;
148 state_
.write_bool_mark
= state_
.write_mark
;
150 unsigned char byte
= 0;
151 if (write(&byte
, 1) == 0) throw std::length_error("out of memory");
154 if (value
) buffer_
[state_
.write_bool_mark
] |= (1 << bit
);
155 ++state_
.write_bool_num
;
161 packet
& packet::operator << (int8_t value
)
163 return *this << reinterpret_cast<uint8_t&>(value
);
166 packet
& packet::operator << (int16_t value
)
168 return *this << reinterpret_cast<uint16_t&>(value
);
171 packet
& packet::operator << (int32_t value
)
173 return *this << reinterpret_cast<uint32_t&>(value
);
176 packet
& packet::operator << (int64_t value
)
178 return *this << reinterpret_cast<uint64_t&>(value
);
182 packet
& packet::operator << (uint8_t value
)
184 if (write(&value
, sizeof(value
)) != sizeof(value
))
186 throw std::length_error("out of memory");
191 packet
& packet::operator << (uint16_t value
)
193 value
= htons(value
);
194 if (write(&value
, sizeof(value
)) != sizeof(value
))
196 throw std::length_error("out of memory");
201 packet
& packet::operator << (uint32_t value
)
203 value
= htonl(value
);
204 if (write(&value
, sizeof(value
)) != sizeof(value
))
206 throw std::length_error("out of memory");
211 packet
& packet::operator << (uint64_t value
)
213 value
= htonll(value
);
214 if (write(&value
, sizeof(value
)) != sizeof(value
))
216 throw std::length_error("out of memory");
221 packet
& packet::operator << (float value
)
223 // XXX: assumes the ieee-754
224 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
225 *integer
= htonl(*integer
);
226 if (write(integer
, sizeof(value
)) != sizeof(value
))
228 throw std::length_error("out of memory");
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 if (write(integer
, sizeof(value
)) != sizeof(value
))
240 throw std::length_error("out of memory");
245 size_t packet::write(const void* bytes
, size_t size
)
247 size_t num_bytes
= std::min(size
, size_
- state_
.write_mark
);
248 if (!buffer_
|| num_bytes
< size
)
250 int num_pages
= 1 + size
/ PAGE_SIZE
;
251 int new_size
= size_
+ num_pages
* PAGE_SIZE
;
252 char* new_buffer
= (char*)realloc(buffer_
, new_size
);
255 buffer_
= new_buffer
;
259 if (!buffer_
) return 0;
261 memcpy(buffer_
+ state_
.write_mark
, bytes
, num_bytes
);
262 state_
.write_mark
+= num_bytes
;
267 packet
& packet::operator >> (bool& value
)
269 int bit
= state_
.read_bool_num
% 8;
272 state_
.read_bool_mark
= state_
.read_mark
;
274 unsigned char byte
= 0;
275 if (read(&byte
, 1) == 0) throw std::out_of_range("end of packet");
278 value
= 1 & (buffer_
[state_
.read_bool_mark
] >> bit
);
279 ++state_
.read_bool_num
;
284 packet
& packet::operator >> (int8_t& value
)
286 return *this >> reinterpret_cast<uint8_t&>(value
);
289 packet
& packet::operator >> (int16_t& value
)
291 return *this >> reinterpret_cast<uint16_t&>(value
);
294 packet
& packet::operator >> (int32_t& value
)
296 return *this >> reinterpret_cast<uint32_t&>(value
);
299 packet
& packet::operator >> (int64_t& value
)
301 return *this >> reinterpret_cast<uint64_t&>(value
);
304 packet
& packet::operator >> (uint8_t& value
)
306 if (read(&value
, sizeof(value
)) != sizeof(value
))
308 throw std::out_of_range("end of packet");
313 packet
& packet::operator >> (uint16_t& value
)
315 if (read(&value
, sizeof(value
)) != sizeof(value
))
317 throw std::out_of_range("end of packet");
319 value
= ntohs(value
);
323 packet
& packet::operator >> (uint32_t& value
)
325 if (read(&value
, sizeof(value
)) != sizeof(value
))
327 throw std::out_of_range("end of packet");
329 value
= ntohl(value
);
333 packet
& packet::operator >> (uint64_t& value
)
335 if (read(&value
, sizeof(value
)) != sizeof(value
))
337 throw std::out_of_range("end of packet");
339 value
= ntohll(value
);
343 packet
& packet::operator >> (float& value
)
345 // XXX: assumes the ieee-754
346 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
347 if (read(integer
, sizeof(value
)) != sizeof(value
))
349 throw std::out_of_range("end of packet");
351 *integer
= htonl(*integer
);
355 packet
& packet::operator >> (double& value
)
357 // XXX: assumes the ieee-754
358 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
359 if (read(integer
, sizeof(value
)) != sizeof(value
))
361 throw std::out_of_range("end of packet");
363 *integer
= htonll(*integer
);
367 size_t packet::read(void* bytes
, size_t size
)
369 size_t num_bytes
= std::min(size
, state_
.write_mark
- state_
.read_mark
);
370 memcpy(bytes
, buffer_
+ state_
.read_mark
, num_bytes
);
371 state_
.read_mark
+= num_bytes
;
378 state_
.read_mark
= state_
.write_mark
;
379 state_
.read_bool_mark
= 0;
380 state_
.read_bool_num
= 0;
381 state_
.write_bool_mark
= 0;
382 state_
.write_bool_num
= 0;
391 void packet::revert()
This page took 0.050816 seconds and 5 git commands to generate.