]>
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 **************************************************************************/
19 #include <arpa/inet.h>
28 #define bswap_16(x) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
32 #define bswap_32(x) ((((x) & 0xff000000) >> 24) | \
33 (((x) & 0x00ff0000) >> 8) | \
34 (((x) & 0x0000ff00) << 8) | \
35 (((x) & 0x000000ff) << 24))
39 #define bswap_64(x) (((x) << 56) | \
40 (((x) << 40) & 0xff000000000000ULL) | \
41 (((x) << 24) & 0xff0000000000ULL) | \
42 (((x) << 8) & 0xff00000000ULL) | \
43 (((x) >> 8) & 0xff000000ULL) | \
44 (((x) >> 24) & 0xff0000ULL) | \
45 (((x) >> 40) & 0xff00ULL) | \
51 static uint16_t htons(uint16_t x
)
53 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
59 static uint16_t ntohs(uint16_t x
)
64 static uint32_t htonl(uint32_t x
)
66 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
72 static uint32_t ntohl(uint32_t x
)
79 static uint64_t htonll(uint64_t x
)
81 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
87 static uint64_t ntohll(uint64_t x
)
96 packet::packet(size_t size
) :
97 buffer_((char*)malloc(size
)),
100 packet::packet(const char* data
, size_t size
) :
101 buffer_((char*)malloc(size
)),
106 if (buffer_
) memcpy(buffer_
, data
, size
);
107 else throw std::length_error("out of memory");
111 packet::packet(const packet
& copy
) :
112 buffer_((char*)malloc(copy
.size_
)),
117 if (buffer_
) memcpy(buffer_
, copy
.buffer_
, size_
);
118 else throw std::length_error("out of memory");
121 packet
& packet::operator = (const packet
& copy
)
124 buffer_
= (char*)malloc(copy
.size_
);
126 if (buffer_
) memcpy(buffer_
, copy
.buffer_
, size_
);
127 else throw std::length_error("out of memory");
129 state_
= copy
.state_
;
130 saved_
= copy
.saved_
;
141 packet
& packet::operator << (bool value
)
143 int bit
= state_
.write_bool_num
% 8;
146 state_
.write_bool_mark
= state_
.write_mark
;
148 unsigned char byte
= 0;
149 if (write(&byte
, 1) == 0) throw std::length_error("out of memory");
152 if (value
) buffer_
[state_
.write_bool_mark
] |= (1 << bit
);
153 ++state_
.write_bool_num
;
159 packet
& packet::operator << (int8_t value
)
161 return *this << reinterpret_cast<uint8_t&>(value
);
164 packet
& packet::operator << (int16_t value
)
166 return *this << reinterpret_cast<uint16_t&>(value
);
169 packet
& packet::operator << (int32_t value
)
171 return *this << reinterpret_cast<uint32_t&>(value
);
174 packet
& packet::operator << (int64_t value
)
176 return *this << reinterpret_cast<uint64_t&>(value
);
180 packet
& packet::operator << (uint8_t value
)
182 if (write(&value
, sizeof(value
)) != sizeof(value
))
184 throw std::length_error("out of memory");
189 packet
& packet::operator << (uint16_t value
)
191 value
= htons(value
);
192 if (write(&value
, sizeof(value
)) != sizeof(value
))
194 throw std::length_error("out of memory");
199 packet
& packet::operator << (uint32_t value
)
201 value
= htonl(value
);
202 if (write(&value
, sizeof(value
)) != sizeof(value
))
204 throw std::length_error("out of memory");
209 packet
& packet::operator << (uint64_t value
)
211 value
= htonll(value
);
212 if (write(&value
, sizeof(value
)) != sizeof(value
))
214 throw std::length_error("out of memory");
219 packet
& packet::operator << (float value
)
221 // XXX: assumes the ieee-754
222 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
223 *integer
= htonl(*integer
);
224 if (write(integer
, sizeof(value
)) != sizeof(value
))
226 throw std::length_error("out of memory");
231 packet
& packet::operator << (double value
)
233 // XXX: assumes the ieee-754
234 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
235 *integer
= htonll(*integer
);
236 if (write(integer
, sizeof(value
)) != sizeof(value
))
238 throw std::length_error("out of memory");
243 size_t packet::write(const void* bytes
, size_t size
)
245 size_t num_bytes
= std::min(size
, size_
- state_
.write_mark
);
246 if (!buffer_
|| num_bytes
< size
)
248 int num_pages
= 1 + size
/ PAGE_SIZE
;
249 int new_size
= size_
+ num_pages
* PAGE_SIZE
;
250 char* new_buffer
= (char*)realloc(buffer_
, new_size
);
253 buffer_
= new_buffer
;
257 if (!buffer_
) return 0;
259 memcpy(buffer_
+ state_
.write_mark
, bytes
, num_bytes
);
260 state_
.write_mark
+= num_bytes
;
265 packet
& packet::operator >> (bool& value
)
267 int bit
= state_
.read_bool_num
% 8;
270 state_
.read_bool_mark
= state_
.read_mark
;
272 unsigned char byte
= 0;
273 if (read(&byte
, 1) == 0) throw std::out_of_range("end of packet");
276 value
= 1 & (buffer_
[state_
.read_bool_mark
] >> bit
);
277 ++state_
.read_bool_num
;
282 packet
& packet::operator >> (int8_t& value
)
284 return *this >> reinterpret_cast<uint8_t&>(value
);
287 packet
& packet::operator >> (int16_t& value
)
289 return *this >> reinterpret_cast<uint16_t&>(value
);
292 packet
& packet::operator >> (int32_t& value
)
294 return *this >> reinterpret_cast<uint32_t&>(value
);
297 packet
& packet::operator >> (int64_t& value
)
299 return *this >> reinterpret_cast<uint64_t&>(value
);
302 packet
& packet::operator >> (uint8_t& value
)
304 if (read(&value
, sizeof(value
)) != sizeof(value
))
306 throw std::out_of_range("end of packet");
311 packet
& packet::operator >> (uint16_t& value
)
313 if (read(&value
, sizeof(value
)) != sizeof(value
))
315 throw std::out_of_range("end of packet");
317 value
= ntohs(value
);
321 packet
& packet::operator >> (uint32_t& value
)
323 if (read(&value
, sizeof(value
)) != sizeof(value
))
325 throw std::out_of_range("end of packet");
327 value
= ntohl(value
);
331 packet
& packet::operator >> (uint64_t& value
)
333 if (read(&value
, sizeof(value
)) != sizeof(value
))
335 throw std::out_of_range("end of packet");
337 value
= ntohll(value
);
341 packet
& packet::operator >> (float& value
)
343 // XXX: assumes the ieee-754
344 uint32_t* integer
= reinterpret_cast<uint32_t*>(&value
);
345 if (read(integer
, sizeof(value
)) != sizeof(value
))
347 throw std::out_of_range("end of packet");
349 *integer
= htonl(*integer
);
353 packet
& packet::operator >> (double& value
)
355 // XXX: assumes the ieee-754
356 uint64_t* integer
= reinterpret_cast<uint64_t*>(&value
);
357 if (read(integer
, sizeof(value
)) != sizeof(value
))
359 throw std::out_of_range("end of packet");
361 *integer
= htonll(*integer
);
365 size_t packet::read(void* bytes
, size_t size
)
367 size_t num_bytes
= std::min(size
, state_
.write_mark
- state_
.read_mark
);
368 memcpy(bytes
, buffer_
+ state_
.read_mark
, num_bytes
);
369 state_
.read_mark
+= num_bytes
;
376 state_
.read_mark
= state_
.write_mark
;
377 state_
.read_bool_mark
= 0;
378 state_
.read_bool_num
= 0;
379 state_
.write_bool_mark
= 0;
380 state_
.write_bool_num
= 0;
389 void packet::revert()
This page took 0.05662 seconds and 5 git commands to generate.