2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
15 \brief IntParser internal header */
17 #ifndef IH_SENF_Packets_IntParser_
18 #define IH_SENF_Packets_IntParser_ 1
21 #include "PacketTypes.hh"
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 //-/////////////////////////////////////////////////////////////////////////////////////////////
32 /** \brief Internal: Integer operation mixin for integer parsers
36 This class contains all the integer operations supported by the integer parsers. It is
37 inherited by each integer parser.
39 template <class Derived, class Value>
43 typedef Value value_type;
45 operator Value () const { return derived().value(); }
48 Value operator op () const { return op derived().value(); }
49 # define mutator(op) \
50 template <class Other> Derived const & operator op ## = (Other other) \
51 { derived().value( derived().value() op other ); return derived(); }
71 Derived const & operator ++ ()
72 { derived().value( derived().value()+1 ); return derived(); }
73 Derived const & operator -- ()
74 { derived().value( derived().value()-1 ); return derived(); }
76 Value operator ++ (int)
77 { Value v (derived().value()); derived().value( v+1 ); return v; }
78 Value operator -- (int)
79 { Value v (derived().value()); derived().value( v-1 ); return v; }
82 Derived & derived() { return *static_cast<Derived *>(this); }
83 Derived const & derived() const { return *static_cast<Derived const *>(this); };
86 //-/////////////////////////////////////////////////////////////////////////////////////////////
87 // Network byte order integer extraction
89 /** \brief Internal: Extract 16bit network byte order value
93 inline boost::uint16_t parse_uint16(iterator i)
95 return i[1] | i[0]<<8;
98 /** \brief Internal: Write 16bit network byte order value
102 inline void write_uint16(iterator i, boost::uint16_t v)
104 i[0] = ( v >> 8 ) & 0xff;
108 /** \brief Internal: Extract 16bit least significant bit order value
112 inline boost::uint16_t parse_uint16LSB(iterator i)
114 return i[0] | i[1]<<8;
117 /** \brief Internal: Write 16bit least significant bit order value
121 inline void write_uint16LSB(iterator i, boost::uint16_t v)
124 i[1] = ( v >> 8 ) & 0xff;
127 /** \brief Internal: Extract 24bit network byte order value
131 inline boost::uint32_t parse_uint24(iterator i)
133 return i[2] | i[1]<<8 | i[0]<<16;
136 /** \brief Internal: Write 24bit network byte order value
140 inline void write_uint24(iterator i, boost::uint32_t v)
142 i[0] = ( v >> 16 ) & 0xff;
143 i[1] = ( v >> 8 ) & 0xff;
147 /** \brief Internal: Extract 32bit network byte order value
151 inline boost::uint32_t parse_uint32(iterator i)
153 return i[3] | i[2]<<8 | i[1]<<16 | i[0]<<24;
156 /** \brief Internal: Write 32bit network byte order value
160 inline void write_uint32(iterator i, boost::uint32_t v)
162 i[0] = ( v >> 24 ) & 0xff;
163 i[1] = ( v >> 16 ) & 0xff;
164 i[2] = ( v >> 8 ) & 0xff;
168 /** \brief Internal: Extract 32bit network byte order value
172 inline boost::uint32_t parse_uint32LSB(iterator i)
174 return i[0] | i[1]<<8 | i[2]<<16 | i[3]<<24;
177 /** \brief Internal: Write 32bit network byte order value
181 inline void write_uint32LSB(iterator i, boost::uint32_t v)
183 i[3] = ( v >> 24 ) & 0xff;
184 i[2] = ( v >> 16 ) & 0xff;
185 i[1] = ( v >> 8 ) & 0xff;
189 /** \brief Internal: Extract 64bit network byte order value
193 inline boost::uint64_t parse_uint64(iterator i)
195 return ((boost::uint64_t)i[7]) | ((boost::uint64_t)i[6])<<8
196 | ((boost::uint64_t)i[5])<<16 | ((boost::uint64_t)i[4])<<24
197 | ((boost::uint64_t)i[3])<<32 | ((boost::uint64_t)i[2])<<40
198 | ((boost::uint64_t)i[1])<<48 | ((boost::uint64_t)i[0])<<56;
201 /** \brief Internal: Write 64bit network byte order value
205 inline void write_uint64(iterator i, boost::uint64_t v)
207 i[0] = ( v >> 56 ) & 0xff;
208 i[1] = ( v >> 48 ) & 0xff;
209 i[2] = ( v >> 40 ) & 0xff;
210 i[3] = ( v >> 32 ) & 0xff;
211 i[4] = ( v >> 24 ) & 0xff;
212 i[5] = ( v >> 16 ) & 0xff;
213 i[6] = ( v >> 8 ) & 0xff;
217 /** \brief Internal: Extract 64bit least significant bit order value
221 inline boost::uint64_t parse_uint64LSB(iterator i)
223 return ((boost::uint64_t)i[0]) | ((boost::uint64_t)i[1])<<8
224 | ((boost::uint64_t)i[2])<<16 | ((boost::uint64_t)i[3])<<24
225 | ((boost::uint64_t)i[4])<<32 | ((boost::uint64_t)i[5])<<40
226 | ((boost::uint64_t)i[6])<<48 | ((boost::uint64_t)i[7])<<56;
229 /** \brief Internal: Write 64bit least significant bit order value
233 inline void write_uint64LSB(iterator i, boost::uint64_t v)
236 i[1] = ( v >> 8 ) & 0xff;
237 i[2] = ( v >> 16 ) & 0xff;
238 i[3] = ( v >> 24 ) & 0xff;
239 i[4] = ( v >> 32 ) & 0xff;
240 i[5] = ( v >> 40 ) & 0xff;
241 i[6] = ( v >> 48 ) & 0xff;
242 i[7] = ( v >> 56 ) & 0xff;
245 //-/////////////////////////////////////////////////////////////////////////////////////////////
246 // bitfield extraction
248 // Doxygen doesn't like this stuff ...
252 template <unsigned offset, unsigned endb, unsigned start, unsigned end>
253 struct parse_bitfield_i
255 static boost::uint32_t parse(iterator i) {
256 return ( ( ( parse_uint32(i+offset+1)>>(40-end) ) // Beware of sign extension !!
257 & boost::low_bits_mask_t<32-(40-end)>::sig_bits )
258 | (i[offset]<<(32-(40-end))) )
259 & boost::low_bits_mask_t<end-start>::sig_bits;
262 static void write(iterator i, boost::uint32_t v) {
263 write_uint32(i+offset+1,
264 (parse_uint32(i+offset+1) & ~(boost::low_bits_mask_t<end-8>::sig_bits<<(40-end)))
265 | ((v & boost::low_bits_mask_t<end-8>::sig_bits) << (40-end)));
266 i[offset] = (i[offset] & ~(boost::low_bits_mask_t<8-start>::sig_bits))
267 | ((v>>(end-8)) & boost::low_bits_mask_t<8-start>::sig_bits);
271 template <unsigned offset, unsigned start, unsigned end>
272 struct parse_bitfield_i<offset, 3, start, end>
274 static boost::uint32_t parse(iterator i) {
275 return ( parse_uint32(i+offset)>>(32-end) )
276 & boost::low_bits_mask_t<end-start>::sig_bits;
279 static void write(iterator i, boost::uint32_t v) {
280 write_uint32(i+offset,
281 (parse_uint32(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(32-end)))
282 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (32-end)));
286 template <unsigned offset, unsigned start, unsigned end>
287 struct parse_bitfield_i<offset, 2, start, end>
289 static boost::uint32_t parse(iterator i) {
290 return ( parse_uint24(i+offset)>>(24-end) )
291 & boost::low_bits_mask_t<end-start>::sig_bits;
294 static void write(iterator i, boost::uint32_t v) {
295 write_uint24(i+offset,
296 (parse_uint24(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(24-end)))
297 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (24-end)));
301 template <unsigned offset, unsigned start, unsigned end>
302 struct parse_bitfield_i<offset, 1, start, end>
304 static boost::uint32_t parse(iterator i) {
305 return ( parse_uint16(i+offset)>>(16-end) )
306 & boost::low_bits_mask_t<end-start>::sig_bits;
309 static void write(iterator i, boost::uint32_t v) {
310 write_uint16(i+offset,
311 (parse_uint16(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(16-end)))
312 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (16-end)));
316 template <unsigned offset, unsigned start, unsigned end>
317 struct parse_bitfield_i<offset, 0, start, end>
319 static boost::uint32_t parse(iterator i) {
320 return ( i[offset]>>(8-end) )
321 & boost::low_bits_mask_t<end-start>::sig_bits;
324 static void write(iterator i, boost::uint32_t v) {
325 i[offset] = (i[offset] & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(8-end)))
326 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (8-end));
332 /** \brief Internal: Bitfield read/write helper
336 Using template specializations, this class provides optimized bitfield parsers and
337 writers. For each number of bytes the bitfield covers (from 1 to 5 bytes), a template
338 specialization is provided in \c parse_bitfield_i.
340 This helper always processes unsigned values. For signed values sign extension still needs
343 template <unsigned start, unsigned end>
344 struct parse_bitfield
345 : public parse_bitfield_i<start/8,(end-1)/8-start/8,start%8,end-8*(start/8)>
350 //-/////////////////////////////////////////////////////////////////////////////////////////////////
357 // c-file-style: "senf"
358 // indent-tabs-mode: nil
359 // ispell-local-dictionary: "american"
360 // compile-command: "scons -u test"
361 // comment-column: 40