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::DynamicTLVLengthParser::value_type senf::DynamicTLVLengthParser::value() const
40 return fixed_length_field().value();
42 return parse<UInt8Parser>( 1 ).value();
44 return parse<UInt16Parser>( 1 ).value();
46 return parse<UInt24Parser>( 1 ).value();
48 return parse<UInt32Parser>( 1 ).value();
50 throw(TLVLengthException());
55 prefix_ void senf::DynamicTLVLengthParser::value(value_type const & v)
59 if (v > 127) throw( TLVLengthException());
60 fixed_length_field() = v;
63 if (v > UInt8Parser::max_value) throw( TLVLengthException());
64 parse<UInt8Parser>(1) = v;
67 if (v > UInt16Parser::max_value) throw( TLVLengthException());
68 parse<UInt16Parser>(1) = v;
71 if (v > UInt24Parser::max_value) throw( TLVLengthException());
72 parse<UInt24Parser>(1) = v;
75 parse<UInt32Parser>(1) = v;
78 throw( TLVLengthException());
83 prefix_ senf::DynamicTLVLengthParser const & senf::DynamicTLVLengthParser::operator= (value_type other)
90 prefix_ senf::DynamicTLVLengthParser::size_type senf::DynamicTLVLengthParser::bytes() const
92 if ( extended_length_flag() )
93 return 1 + fixed_length_field();
99 prefix_ void senf::DynamicTLVLengthParser::init() const
102 extended_length_flag() = false;
106 prefix_ void senf::DynamicTLVLengthParser::shrink()
108 value_type v = value();
109 size_type b = bytes();
111 if (b != 1) resize(1);
114 if (v <= UInt8Parser::max_value) {
115 if (b != 2) resize(2);
118 if (v <= UInt16Parser::max_value) {
119 if (b != 3) resize(3);
122 if (v <= UInt24Parser::max_value) {
123 if (b != 4) resize(4);
126 if (b != 5) resize(5);
130 prefix_ void senf::DynamicTLVLengthParser:: maxValue(DynamicTLVLengthParser::value_type v)
134 size_type b = bytes();
135 if (v <= UInt8Parser::max_value) {
136 if (b < 2) resize(2);
139 if (v <= UInt16Parser::max_value) {
140 if (b < 3) resize(3);
143 if (v <= UInt24Parser::max_value) {
144 if (b < 4) resize(4);
147 if (b < 5) resize(5);
151 prefix_ senf::PacketInterpreterBase::range senf::GenericTLVPacketParser::value()
154 senf::PacketData::iterator begin (boost::next(data().begin(), 1 + length_bytes() ));
155 return PacketInterpreterBase::range(
156 begin, boost::next( begin, length()) );
160 prefix_ void senf::DynamicTLVLengthParser::resize(size_type size)
162 value_type v = value();
163 size_type current_size (bytes());
164 SafePacketParserWrapper<DynamicTLVLengthParser> safeThis (*this);
166 if (current_size > size)
167 data().erase( i(), boost::next(i(), current_size-size));
169 data().insert( i(), size-current_size, 0);
172 safeThis->extended_length_flag() = true;
173 safeThis->fixed_length_field() = size - 1;
175 safeThis->extended_length_flag() = false;
181 prefix_ void senf::GenericTLVPacketType::dump(packet p, std::ostream & os)
183 boost::io::ios_all_saver ias(os);
184 os << "GenericTLVPacket:\n"
186 << " type: " << unsigned( p->type()) << "\n"
187 << " length: " << unsigned( p->length()) << "\n"
189 senf::hexdump( p->value().begin(), p->value().end(), os);
193 prefix_ void senf::GenericTLVPacketType::finalize(packet p)
199 ///////////////////////////////cc.e////////////////////////////////////////
206 // c-file-style: "senf"
207 // indent-tabs-mode: nil
208 // ispell-local-dictionary: "american"
209 // compile-command: "scons -u test"
210 // comment-column: 40