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::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());
75 fixed_length_field() = v;
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 const & senf::DynamicTLVLengthParser::operator= (value_type other)
106 prefix_ void senf::DynamicTLVLengthParser::init() const
109 extended_length_flag() = false;
113 prefix_ void senf::DynamicTLVLengthParser::shrink()
115 value_type v = value();
116 size_type b = bytes();
118 if (b != 1) resize(1);
121 if (v <= UInt8Parser::max_value + 128) {
122 if (b != 2) resize(2);
125 if (v <= UInt16Parser::max_value + 128) {
126 if (b != 3) resize(3);
129 if (v <= UInt24Parser::max_value + 128 ){
130 if (b != 4) resize(4);
133 if (b != 5) resize(5);
137 prefix_ void senf::DynamicTLVLengthParser:: maxValue(DynamicTLVLengthParser::value_type v)
141 size_type b = bytes();
142 if (v <= UInt8Parser::max_value + 128) {
143 if (b < 2) resize(2);
146 if (v <= UInt16Parser::max_value + 128) {
147 if (b < 3) resize(3);
150 if (v <= UInt24Parser::max_value + 128) {
151 if (b < 4) resize(4);
154 if (b < 5) resize(5);
158 prefix_ void senf::DynamicTLVLengthParser::resize(size_type size)
160 value_type v = value();
161 size_type current_size (bytes());
162 SafePacketParserWrapper<DynamicTLVLengthParser> safeThis (*this);
164 if (current_size > size)
165 data().erase( i(), boost::next(i(), current_size-size));
167 data().insert( i(), size-current_size, 0);
170 safeThis->extended_length_flag() = true;
171 safeThis->fixed_length_field() = size - 1;
173 safeThis->extended_length_flag() = false;
179 prefix_ senf::PacketInterpreterBase::range senf::GenericTLVPacketParser::value()
182 senf::PacketData::iterator begin (boost::next(data().begin(), 1 + length_bytes() ));
183 return PacketInterpreterBase::range(
184 begin, boost::next( begin, length()) );
188 prefix_ void senf::GenericTLVPacketType::dump(packet p, std::ostream & os)
190 boost::io::ios_all_saver ias(os);
191 os << "GenericTLVPacket:\n"
193 << " type: " << unsigned( p->type()) << "\n"
194 << " length: " << unsigned( p->length()) << "\n"
196 senf::hexdump( p->value().begin(), p->value().end(), os);
200 prefix_ void senf::GenericTLVPacketType::finalize(packet p)
206 ///////////////////////////////cc.e////////////////////////////////////////
213 // c-file-style: "senf"
214 // indent-tabs-mode: nil
215 // ispell-local-dictionary: "american"
216 // compile-command: "scons -u test"
217 // comment-column: 40