4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.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 #define IH_ParseInt_ 1
28 ///////////////////////////////ih.p////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////
37 template <class Derived, class Value>
41 typedef Value value_type;
43 operator Value () const { return derived().value(); }
46 Value operator op () const { return op derived().value(); }
47 # define mutator(op) \
48 template <class Other> Derived const & operator op ## = (Other other) \
49 { derived().value( derived().value() op other ); return derived(); }
69 Derived const & operator ++ ()
70 { derived().value( derived.value()+1 ); return derived(); }
71 Derived const & operator -- ()
72 { derived().value( derived.value()-1 ); return derived(); }
75 Derived & derived() { return *static_cast<Derived *>(this); }
76 Derived const & derived() const { return *static_cast<Derived const *>(this); };
79 ///////////////////////////////////////////////////////////////////////////
80 // Network byte order integer extraction
82 template <class Iterator>
83 boost::uint16_t parse_uint16(Iterator const & i)
85 return i[1] | i[0]<<8;
88 template <class Iterator>
89 void write_uint16(Iterator const & i, boost::uint16_t v)
91 i[0] = ( v >> 8 ) & 0xff;
95 template <class Iterator>
96 boost::uint32_t parse_uint24(Iterator const & i)
98 return i[2] | i[1]<<8 | i[0]<<16;
101 template <class Iterator>
102 void write_uint24(Iterator const & i, boost::uint32_t v)
104 i[0] = ( v >> 16 ) & 0xff;
105 i[1] = ( v >> 8 ) & 0xff;
109 template <class Iterator>
110 boost::uint32_t parse_uint32(Iterator const & i)
112 return i[3] | i[2]<<8 | i[1]<<16 | i[0]<<24;
115 template <class Iterator>
116 void write_uint32(Iterator const & i, boost::uint32_t v)
118 i[0] = ( v >> 24 ) & 0xff;
119 i[1] = ( v >> 16 ) & 0xff;
120 i[2] = ( v >> 8 ) & 0xff;
124 ///////////////////////////////////////////////////////////////////////////
125 // bitfield extraction
127 template <class Iterator, unsigned offset, unsigned endb, unsigned start, unsigned end>
128 struct parse_bitfield_i
130 static boost::uint32_t parse(Iterator const & i) {
131 return ( ( ( parse_uint32(i+offset+1)>>(40-end) ) // Beware of sign extension !!
132 & boost::low_bits_mask_t<32-(40-end)>::sig_bits )
133 | (i[offset]<<(32-(40-end))) )
134 & boost::low_bits_mask_t<end-start>::sig_bits;
137 static void write(Iterator const & i, boost::uint32_t v) {
138 write_uint32(i+offset+1,
139 (parse_uint32(i+offset+1) & ~(boost::low_bits_mask_t<end-8>::sig_bits<<(40-end)))
140 | ((v & boost::low_bits_mask_t<end-8>::sig_bits) << (40-end)));
141 i[offset] = (i[offset] & ~(boost::low_bits_mask_t<8-start>::sig_bits))
142 | ((v>>(end-8)) & boost::low_bits_mask_t<8-start>::sig_bits);
146 template <class Iterator, unsigned offset, unsigned start, unsigned end>
147 struct parse_bitfield_i<Iterator, offset, 3, start, end>
149 static boost::uint32_t parse(Iterator const & i) {
150 return ( parse_uint32(i+offset)>>(32-end) )
151 & boost::low_bits_mask_t<end-start>::sig_bits;
154 static void write(Iterator const & i, boost::uint32_t v) {
155 write_uint32(i+offset,
156 (parse_uint32(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(32-end)))
157 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (32-end)));
161 template <class Iterator, unsigned offset, unsigned start, unsigned end>
162 struct parse_bitfield_i<Iterator, offset, 2, start, end>
164 static boost::uint32_t parse(Iterator const & i) {
165 return ( parse_uint24(i+offset)>>(24-end) )
166 & boost::low_bits_mask_t<end-start>::sig_bits;
169 static void write(Iterator const & i, boost::uint32_t v) {
170 write_uint24(i+offset,
171 (parse_uint24(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(24-end)))
172 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (24-end)));
176 template <class Iterator, unsigned offset, unsigned start, unsigned end>
177 struct parse_bitfield_i<Iterator, offset, 1, start, end>
179 static boost::uint32_t parse(Iterator const & i) {
180 return ( parse_uint16(i+offset)>>(16-end) )
181 & boost::low_bits_mask_t<end-start>::sig_bits;
184 static void write(Iterator const & i, boost::uint32_t v) {
185 write_uint16(i+offset,
186 (parse_uint16(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(16-end)))
187 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (16-end)));
191 template <class Iterator, unsigned offset, unsigned start, unsigned end>
192 struct parse_bitfield_i<Iterator, offset, 0, start, end>
194 static boost::uint32_t parse(Iterator const & i) {
195 return ( i[offset]>>(8-end) )
196 & boost::low_bits_mask_t<end-start>::sig_bits;
199 static void write(Iterator const & i, boost::uint32_t v) {
200 i[offset] = (i[offset] & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(8-end)))
201 | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (8-end));
205 template <class Iterator, unsigned start, unsigned end>
206 struct parse_bitfield
207 : public parse_bitfield_i<Iterator,start/8,(end-1)/8-start/8,start%8,end-8*(start/8)>
212 ///////////////////////////////ih.e////////////////////////////////////////
218 // c-file-style: "satcom"