3
Copyright (c) 2003, Arvid Norberg
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
* Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the distribution.
15
* Neither the name of the author nor the names of its
16
contributors may be used to endorse or promote products derived
17
from this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
33
#ifndef TORRENT_IO_HPP_INCLUDED
34
#define TORRENT_IO_HPP_INCLUDED
36
#include <boost/cstdint.hpp>
43
template <class T> struct type {};
45
// reads an integer from a byte stream
46
// in big endian byte order and converts
47
// it to native endianess
48
template <class T, class InIt>
49
inline T read_impl(InIt& start, type<T>)
52
for (int i = 0; i < (int)sizeof(T); ++i)
55
ret |= static_cast<unsigned char>(*start);
61
template <class T, class OutIt>
62
inline void write_impl(T val, OutIt& start)
64
for (int i = (int)sizeof(T)-1; i >= 0; --i)
66
*start = static_cast<unsigned char>((val >> (i * 8)) & 0xff);
74
boost::int64_t read_int64(InIt& start)
75
{ return read_impl(start, type<boost::int64_t>()); }
78
boost::uint64_t read_uint64(InIt& start)
79
{ return read_impl(start, type<boost::uint64_t>()); }
82
boost::uint32_t read_uint32(InIt& start)
83
{ return read_impl(start, type<boost::uint32_t>()); }
86
boost::int32_t read_int32(InIt& start)
87
{ return read_impl(start, type<boost::int32_t>()); }
90
boost::int16_t read_int16(InIt& start)
91
{ return read_impl(start, type<boost::int16_t>()); }
94
boost::uint16_t read_uint16(InIt& start)
95
{ return read_impl(start, type<boost::uint16_t>()); }
98
boost::int8_t read_int8(InIt& start)
99
{ return read_impl(start, type<boost::int8_t>()); }
101
template <class InIt>
102
boost::uint8_t read_uint8(InIt& start)
103
{ return read_impl(start, type<boost::uint8_t>()); }
106
template <class OutIt>
107
void write_uint64(boost::uint64_t val, OutIt& start)
108
{ write_impl(val, start); }
110
template <class OutIt>
111
void write_int64(boost::int64_t val, OutIt& start)
112
{ write_impl(val, start); }
114
template <class OutIt>
115
void write_uint32(boost::uint32_t val, OutIt& start)
116
{ write_impl(val, start); }
118
template <class OutIt>
119
void write_int32(boost::int32_t val, OutIt& start)
120
{ write_impl(val, start); }
122
template <class OutIt>
123
void write_uint16(boost::uint16_t val, OutIt& start)
124
{ write_impl(val, start); }
126
template <class OutIt>
127
void write_int16(boost::int16_t val, OutIt& start)
128
{ write_impl(val, start); }
130
template <class OutIt>
131
void write_uint8(boost::uint8_t val, OutIt& start)
132
{ write_impl(val, start); }
134
template <class OutIt>
135
void write_int8(boost::int8_t val, OutIt& start)
136
{ write_impl(val, start); }
138
inline void write_string(std::string const& str, char*& start)
140
std::copy(str.begin(), str.end(), start);
144
template <class OutIt>
145
void write_string(std::string const& str, OutIt& start)
147
std::copy(str.begin(), str.end(), start);
153
#endif // TORRENT_IO_HPP_INCLUDED