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 */
27 #define IH_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 ///////////////////////////////////////////////////////////////////////////
159 // bitfield extraction
161 // Doxygen doesn't like this stuff ...
165 template <unsigned offset, unsigned endb, unsigned start, unsigned end>
166 struct parse_bitfield_i
168 static boost::uint32_t parse(iterator i) {
169 return ( ( ( parse_uint32(i+offset+1)>>(40-end) ) // Beware of sign extension !!
170 & boost::low_bits_mask_t<32-(40-end)>::sig_bits )
171 | (i[offset]<<(32-(40-end))) )
172 & boost::low_bits_mask_t<end-start>::sig_bits;
175 static void write(iterator i, boost::uint32_t v) {
176 write_uint32(i+offset+1,
177 (parse_uint32(i+offset+1) & ~(boost::low_bits_mask_t<end-8>::sig_bits<<(40-end)))
178 | ((v & boost::low_bits_mask_t<end-8>::sig_bits) << (40-end)));
179 i[offset] = (i[offset] & ~(boost::low_bits_mask_t<8-start>::sig_bits))
180 | ((v>>(end-8)) & boost::low_bits_mask_t<8-start>::sig_bits);
184 template <unsigned offset, unsigned start, unsigned end>
185 struct parse_bitfield_i<offset, 3, start, end>
187 static boost::uint32_t parse(iterator i) {
188 return ( parse_uint32(i+offset)>>(32-end) )
189 & boost::low_bits_mask_t<end-start>::sig_bits;
192 static void write(iterator i, boost::uint32_t v) {
193 write_uint32(i+offset,
194 (parse_uint32(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(32-end)))
195 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (32-end)));
199 template <unsigned offset, unsigned start, unsigned end>
200 struct parse_bitfield_i<offset, 2, start, end>
202 static boost::uint32_t parse(iterator i) {
203 return ( parse_uint24(i+offset)>>(24-end) )
204 & boost::low_bits_mask_t<end-start>::sig_bits;
207 static void write(iterator i, boost::uint32_t v) {
208 write_uint24(i+offset,
209 (parse_uint24(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(24-end)))
210 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (24-end)));
214 template <unsigned offset, unsigned start, unsigned end>
215 struct parse_bitfield_i<offset, 1, start, end>
217 static boost::uint32_t parse(iterator i) {
218 return ( parse_uint16(i+offset)>>(16-end) )
219 & boost::low_bits_mask_t<end-start>::sig_bits;
222 static void write(iterator i, boost::uint32_t v) {
223 write_uint16(i+offset,
224 (parse_uint16(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(16-end)))
225 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (16-end)));
229 template <unsigned offset, unsigned start, unsigned end>
230 struct parse_bitfield_i<offset, 0, start, end>
232 static boost::uint32_t parse(iterator i) {
233 return ( i[offset]>>(8-end) )
234 & boost::low_bits_mask_t<end-start>::sig_bits;
237 static void write(iterator i, boost::uint32_t v) {
238 i[offset] = (i[offset] & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(8-end)))
239 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (8-end));
245 /** \brief Internal: Bitfield read/write helper
249 Using template specializations, this class provides optimized bitfield parsers and
250 writers. For each number of bytes the bitfield covers (from 1 to 5 bytes), a template
251 specialization is provided in \c parse_bitfield_i.
253 This helper always processes unsigned values. For signed values sign extension still needs
256 template <unsigned start, unsigned end>
257 struct parse_bitfield
258 : public parse_bitfield_i<start/8,(end-1)/8-start/8,start%8,end-8*(start/8)>
263 ///////////////////////////////ih.e////////////////////////////////////////
270 // c-file-style: "senf"
271 // indent-tabs-mode: nil
272 // ispell-local-dictionary: "american"
273 // compile-command: "scons -u test"
274 // comment-column: 40