4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Thorsten Horstmann <tho@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 TLVPacket non-inline non-template implementation */
26 #include "TLVPacket.hh"
27 //#include "TLVPacket.ih"
31 #include <senf/Utils/hexdump.hh>
34 ///////////////////////////////cc.p////////////////////////////////////////
36 prefix_ senf::safe_data_iterator senf::BaseTLVPacketParser::resizeValueField(
37 DynamicTLVLengthParser::value_type size)
39 DynamicTLVLengthParser::value_type current_length ( length());
42 safe_data_iterator si (data(), boost::next(i(), 1 + length_bytes() ));
43 if (current_length > size)
44 data().erase( si, boost::next(si, current_length-size));
46 data().insert( si, size-current_length, 0);
51 prefix_ senf::DynamicTLVLengthParser::value_type senf::DynamicTLVLengthParser::value() const
55 return length_field().value();
57 return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
59 return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
61 return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
63 return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
65 throw(TLVLengthException());
70 prefix_ void senf::DynamicTLVLengthParser::value(value_type const & v)
74 if (v > 128) throw( TLVLengthException());
78 if (v > UInt8Parser::max_value + 128) throw( TLVLengthException());
79 parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
82 if (v > UInt16Parser::max_value + 128) throw( TLVLengthException());
83 parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
86 if (v > UInt24Parser::max_value + 128) throw( TLVLengthException());
87 parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
90 parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
93 throw( TLVLengthException());
95 underflow_flag() = (v <= 128);
99 prefix_ senf::DynamicTLVLengthParser::value_type senf::DynamicTLVLengthParser::maxValue()
106 return UInt8Parser::max_value + 128;
108 return UInt16Parser::max_value + 128;
110 return UInt24Parser::max_value + 128;
112 return UInt32Parser::max_value;
114 throw( TLVLengthException());
119 prefix_ senf::DynamicTLVLengthParser const & senf::DynamicTLVLengthParser::operator= (value_type other)
126 prefix_ void senf::DynamicTLVLengthParser::init() const
129 extended_length_flag() = false;
133 prefix_ void senf::DynamicTLVLengthParser::finalize()
135 value_type v = value();
136 size_type b = bytes();
138 if (b != 1) resize(1);
141 if (v <= UInt8Parser::max_value + 128) {
142 if (b != 2) resize(2);
145 if (v <= UInt16Parser::max_value + 128) {
146 if (b != 3) resize(3);
149 if (v <= UInt24Parser::max_value + 128 ){
150 if (b != 4) resize(4);
153 if (b != 5) resize(5);
157 prefix_ void senf::DynamicTLVLengthParser:: maxValue(DynamicTLVLengthParser::value_type v)
161 size_type b = bytes();
162 if (v <= UInt8Parser::max_value + 128) {
163 if (b < 2) resize(2);
166 if (v <= UInt16Parser::max_value + 128) {
167 if (b < 3) resize(3);
170 if (v <= UInt24Parser::max_value + 128) {
171 if (b < 4) resize(4);
174 if (b < 5) resize(5);
178 prefix_ void senf::DynamicTLVLengthParser::resize(size_type size)
180 value_type v = value();
181 size_type current_size (bytes());
182 SafePacketParserWrapper<DynamicTLVLengthParser> safeThis (*this);
184 if (current_size > size)
185 data().erase( i(), boost::next(i(), current_size-size));
187 data().insert( i(), size-current_size, 0);
190 safeThis->extended_length_flag() = true;
191 safeThis->fixed_length_field() = size - 1;
193 safeThis->extended_length_flag() = false;
199 prefix_ senf::PacketInterpreterBase::range senf::GenericTLVPacketParser::value()
202 senf::PacketData::iterator begin (boost::next(data().begin(), 1 + length_bytes() ));
203 return PacketInterpreterBase::range(
204 begin, boost::next( begin, length()) );
208 prefix_ void senf::GenericTLVPacketType::dump(packet p, std::ostream & os)
210 boost::io::ios_all_saver ias(os);
211 os << "GenericTLVPacket:\n"
213 << " type : " << unsigned( p->type()) << "\n"
214 << " length : " << unsigned( p->length()) << "\n"
216 senf::hexdump( p->value().begin(), p->value().end(), os);
220 prefix_ void senf::GenericTLVPacketType::finalize(packet p)
226 ///////////////////////////////cc.e////////////////////////////////////////
233 // c-file-style: "senf"
234 // indent-tabs-mode: nil
235 // ispell-local-dictionary: "american"
236 // compile-command: "scons -u test"
237 // comment-column: 40