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 "../../Utils/hexdump.hh"
34 ///////////////////////////////cc.p////////////////////////////////////////
36 prefix_ senf::safe_data_iterator senf::BaseTLVPacketParser::resizeValue(
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 fixed_length_field().value();
57 return parse<UInt8Parser>( 1 ).value();
59 return parse<UInt16Parser>( 1 ).value();
61 return parse<UInt24Parser>( 1 ).value();
63 return parse<UInt32Parser>( 1 ).value();
65 throw(TLVLengthException());
70 prefix_ void senf::DynamicTLVLengthParser::value(value_type const & v)
74 if (v > 127) throw( TLVLengthException());
75 fixed_length_field() = v;
78 if (v > UInt8Parser::max_value) throw( TLVLengthException());
79 parse<UInt8Parser>(1) = v;
82 if (v > UInt16Parser::max_value) throw( TLVLengthException());
83 parse<UInt16Parser>(1) = v;
86 if (v > UInt24Parser::max_value) throw( TLVLengthException());
87 parse<UInt24Parser>(1) = v;
90 parse<UInt32Parser>(1) = v;
93 throw( TLVLengthException());
98 prefix_ senf::DynamicTLVLengthParser const & senf::DynamicTLVLengthParser::operator= (value_type other)
105 prefix_ senf::DynamicTLVLengthParser::size_type senf::DynamicTLVLengthParser::bytes() const
107 if ( extended_length_flag() )
108 return 1 + fixed_length_field();
114 prefix_ void senf::DynamicTLVLengthParser::init() const
117 extended_length_flag() = false;
121 prefix_ void senf::DynamicTLVLengthParser::shrink()
123 value_type v = value();
124 size_type b = bytes();
126 if (b != 1) resize(1);
129 if (v <= UInt8Parser::max_value) {
130 if (b != 2) resize(2);
133 if (v <= UInt16Parser::max_value) {
134 if (b != 3) resize(3);
137 if (v <= UInt24Parser::max_value) {
138 if (b != 4) resize(4);
141 if (b != 5) resize(5);
145 prefix_ void senf::DynamicTLVLengthParser:: maxValue(DynamicTLVLengthParser::value_type v)
149 size_type b = bytes();
150 if (v <= UInt8Parser::max_value) {
151 if (b < 2) resize(2);
154 if (v <= UInt16Parser::max_value) {
155 if (b < 3) resize(3);
158 if (v <= UInt24Parser::max_value) {
159 if (b < 4) resize(4);
162 if (b < 5) resize(5);
166 prefix_ void senf::DynamicTLVLengthParser::resize(size_type size)
168 value_type v = value();
169 size_type current_size (bytes());
170 SafePacketParserWrapper<DynamicTLVLengthParser> safeThis (*this);
172 if (current_size > size)
173 data().erase( i(), boost::next(i(), current_size-size));
175 data().insert( i(), size-current_size, 0);
178 safeThis->extended_length_flag() = true;
179 safeThis->fixed_length_field() = size - 1;
181 safeThis->extended_length_flag() = false;
187 prefix_ senf::PacketInterpreterBase::range senf::GenericTLVPacketParser::value()
190 senf::PacketData::iterator begin (boost::next(data().begin(), 1 + length_bytes() ));
191 return PacketInterpreterBase::range(
192 begin, boost::next( begin, length()) );
196 prefix_ void senf::GenericTLVPacketType::dump(packet p, std::ostream & os)
198 boost::io::ios_all_saver ias(os);
199 os << "GenericTLVPacket:\n"
201 << " type: " << unsigned( p->type()) << "\n"
202 << " length: " << unsigned( p->length()) << "\n"
204 senf::hexdump( p->value().begin(), p->value().end(), os);
208 prefix_ void senf::GenericTLVPacketType::finalize(packet p)
214 ///////////////////////////////cc.e////////////////////////////////////////
221 // c-file-style: "senf"
222 // indent-tabs-mode: nil
223 // ispell-local-dictionary: "american"
224 // compile-command: "scons -u test"
225 // comment-column: 40