4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief IntParser internal header */
26 #ifndef IH_SENF_Packets_IntParser_
27 #define IH_SENF_Packets_IntParser_ 1
30 #include "PacketTypes.hh"
32 ///////////////////////////////ih.p////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////
41 /** \brief Internal: Integer operation mixin for integer parsers
45 This class contains all the integer operations supported by the integer parsers. It is
46 inherited by each integer parser.
48 template <class Derived, class Value>
52 typedef Value value_type;
54 operator Value () const { return derived().value(); }
57 Value operator op () const { return op derived().value(); }
58 # define mutator(op) \
59 template <class Other> Derived const & operator op ## = (Other other) \
60 { derived().value( derived().value() op other ); return derived(); }
80 Derived const & operator ++ ()
81 { derived().value( derived().value()+1 ); return derived(); }
82 Derived const & operator -- ()
83 { derived().value( derived().value()-1 ); return derived(); }
85 Value operator ++ (int)
86 { Value v (derived().value()); derived().value( v+1 ); return v; }
87 Value operator -- (int)
88 { Value v (derived().value()); derived().value( v-1 ); return v; }
91 Derived & derived() { return *static_cast<Derived *>(this); }
92 Derived const & derived() const { return *static_cast<Derived const *>(this); };
95 ///////////////////////////////////////////////////////////////////////////
96 // Network byte order integer extraction
98 /** \brief Internal: Extract 16bit network byte order value
102 inline boost::uint16_t parse_uint16(iterator i)
104 return i[1] | i[0]<<8;
107 /** \brief Internal: Write 16bit network byte order value
111 inline void write_uint16(iterator i, boost::uint16_t v)
113 i[0] = ( v >> 8 ) & 0xff;
117 /** \brief Internal: Extract 24bit network byte order value
121 inline boost::uint32_t parse_uint24(iterator i)
123 return i[2] | i[1]<<8 | i[0]<<16;
126 /** \brief Internal: Write 24bit network byte order value
130 inline void write_uint24(iterator i, boost::uint32_t v)
132 i[0] = ( v >> 16 ) & 0xff;
133 i[1] = ( v >> 8 ) & 0xff;
137 /** \brief Internal: Extract 32bit network byte order value
141 inline boost::uint32_t parse_uint32(iterator i)
143 return i[3] | i[2]<<8 | i[1]<<16 | i[0]<<24;
146 /** \brief Internal: Write 32bit network byte order value
150 inline void write_uint32(iterator i, boost::uint32_t v)
152 i[0] = ( v >> 24 ) & 0xff;
153 i[1] = ( v >> 16 ) & 0xff;
154 i[2] = ( v >> 8 ) & 0xff;
158 /** \brief Internal: Extract 64bit network byte order value
162 inline boost::uint64_t parse_uint64(iterator i)
164 return ((boost::uint64_t)i[7]) | ((boost::uint64_t)i[6])<<8
165 | ((boost::uint64_t)i[5])<<16 | ((boost::uint64_t)i[4])<<24
166 | ((boost::uint64_t)i[3])<<32 | ((boost::uint64_t)i[2])<<40
167 | ((boost::uint64_t)i[1])<<48 | ((boost::uint64_t)i[0])<<56;
170 /** \brief Internal: Write 64bit network byte order value
174 inline void write_uint64(iterator i, boost::uint64_t v)
176 i[0] = ( v >> 56 ) & 0xff;
177 i[1] = ( v >> 48 ) & 0xff;
178 i[2] = ( v >> 40 ) & 0xff;
179 i[3] = ( v >> 32 ) & 0xff;
180 i[4] = ( v >> 24 ) & 0xff;
181 i[5] = ( v >> 16 ) & 0xff;
182 i[6] = ( v >> 8 ) & 0xff;
186 ///////////////////////////////////////////////////////////////////////////
187 // bitfield extraction
189 // Doxygen doesn't like this stuff ...
193 template <unsigned offset, unsigned endb, unsigned start, unsigned end>
194 struct parse_bitfield_i
196 static boost::uint32_t parse(iterator i) {
197 return ( ( ( parse_uint32(i+offset+1)>>(40-end) ) // Beware of sign extension !!
198 & boost::low_bits_mask_t<32-(40-end)>::sig_bits )
199 | (i[offset]<<(32-(40-end))) )
200 & boost::low_bits_mask_t<end-start>::sig_bits;
203 static void write(iterator i, boost::uint32_t v) {
204 write_uint32(i+offset+1,
205 (parse_uint32(i+offset+1) & ~(boost::low_bits_mask_t<end-8>::sig_bits<<(40-end)))
206 | ((v & boost::low_bits_mask_t<end-8>::sig_bits) << (40-end)));
207 i[offset] = (i[offset] & ~(boost::low_bits_mask_t<8-start>::sig_bits))
208 | ((v>>(end-8)) & boost::low_bits_mask_t<8-start>::sig_bits);
212 template <unsigned offset, unsigned start, unsigned end>
213 struct parse_bitfield_i<offset, 3, start, end>
215 static boost::uint32_t parse(iterator i) {
216 return ( parse_uint32(i+offset)>>(32-end) )
217 & boost::low_bits_mask_t<end-start>::sig_bits;
220 static void write(iterator i, boost::uint32_t v) {
221 write_uint32(i+offset,
222 (parse_uint32(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(32-end)))
223 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (32-end)));
227 template <unsigned offset, unsigned start, unsigned end>
228 struct parse_bitfield_i<offset, 2, start, end>
230 static boost::uint32_t parse(iterator i) {
231 return ( parse_uint24(i+offset)>>(24-end) )
232 & boost::low_bits_mask_t<end-start>::sig_bits;
235 static void write(iterator i, boost::uint32_t v) {
236 write_uint24(i+offset,
237 (parse_uint24(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(24-end)))
238 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (24-end)));
242 template <unsigned offset, unsigned start, unsigned end>
243 struct parse_bitfield_i<offset, 1, start, end>
245 static boost::uint32_t parse(iterator i) {
246 return ( parse_uint16(i+offset)>>(16-end) )
247 & boost::low_bits_mask_t<end-start>::sig_bits;
250 static void write(iterator i, boost::uint32_t v) {
251 write_uint16(i+offset,
252 (parse_uint16(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(16-end)))
253 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (16-end)));
257 template <unsigned offset, unsigned start, unsigned end>
258 struct parse_bitfield_i<offset, 0, start, end>
260 static boost::uint32_t parse(iterator i) {
261 return ( i[offset]>>(8-end) )
262 & boost::low_bits_mask_t<end-start>::sig_bits;
265 static void write(iterator i, boost::uint32_t v) {
266 i[offset] = (i[offset] & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(8-end)))
267 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (8-end));
273 /** \brief Internal: Bitfield read/write helper
277 Using template specializations, this class provides optimized bitfield parsers and
278 writers. For each number of bytes the bitfield covers (from 1 to 5 bytes), a template
279 specialization is provided in \c parse_bitfield_i.
281 This helper always processes unsigned values. For signed values sign extension still needs
284 template <unsigned start, unsigned end>
285 struct parse_bitfield
286 : public parse_bitfield_i<start/8,(end-1)/8-start/8,start%8,end-8*(start/8)>
291 ///////////////////////////////ih.e////////////////////////////////////////
298 // c-file-style: "senf"
299 // indent-tabs-mode: nil
300 // ispell-local-dictionary: "american"
301 // compile-command: "scons -u test"
302 // comment-column: 40