added more TLV unit tests.
[senf.git] / Packets / 80221Bundle / TLVPacket.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief TLVPacket non-inline non-template implementation */
25
26 #include "TLVPacket.hh"
27 //#include "TLVPacket.ih"
28
29 // Custom includes
30 #include <iomanip>
31 #include <senf/Utils/hexdump.hh>
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 prefix_ senf::DynamicTLVLengthParser::value_type senf::DynamicTLVLengthParser::value() const 
37 {
38     switch (bytes() ) {
39     case 1:
40         return fixed_length_field().value();
41     case 2:
42         return parse<UInt8Parser>( 1 ).value();
43     case 3:
44         return parse<UInt16Parser>( 1 ).value();
45     case 4:
46         return parse<UInt24Parser>( 1 ).value();
47     case 5:
48         return parse<UInt32Parser>( 1 ).value();
49     default:
50         throw(TLVLengthException());
51     };
52 }
53
54
55 prefix_ void senf::DynamicTLVLengthParser::value(value_type const & v) 
56 {
57     switch (bytes() ) {
58     case 1:
59         if (v > 127) throw( TLVLengthException());
60         fixed_length_field() = v;
61         return;
62     case 2:
63         if (v > UInt8Parser::max_value) throw( TLVLengthException());
64         parse<UInt8Parser>(1) = v;
65         return;
66     case 3:
67         if (v > UInt16Parser::max_value) throw( TLVLengthException());
68         parse<UInt16Parser>(1) = v;
69         return;
70     case 4:
71         if (v > UInt24Parser::max_value) throw( TLVLengthException());
72         parse<UInt24Parser>(1) = v;
73         return;
74     case 5:
75         parse<UInt32Parser>(1) = v;
76         return;
77     default:
78         throw( TLVLengthException());
79     };
80 }
81
82
83 prefix_ senf::DynamicTLVLengthParser const & senf::DynamicTLVLengthParser::operator= (value_type other) 
84 {
85     value(other);
86     return *this; 
87 }
88
89
90 prefix_ senf::DynamicTLVLengthParser::size_type senf::DynamicTLVLengthParser::bytes() const 
91 {
92     if ( extended_length_flag() )
93         return 1 + fixed_length_field();
94     else
95         return 1;
96 }
97     
98
99 prefix_ void senf::DynamicTLVLengthParser::init() const 
100 {
101     defaultInit();
102     extended_length_flag() = false;
103 }
104
105
106 prefix_ void senf::DynamicTLVLengthParser::shrink()
107 {
108     value_type v = value();
109     size_type b = bytes();
110     if (v <= 127) {
111         if (b != 1) resize(1);
112         return;
113     }
114     if (v <= UInt8Parser::max_value) {
115         if (b != 2) resize(2);
116         return;
117     }
118     if (v <= UInt16Parser::max_value) {
119         if (b != 3) resize(3);
120         return;
121     }
122     if (v <= UInt24Parser::max_value) {
123         if (b != 4) resize(4);
124         return;
125     }
126     if (b != 5) resize(5);
127 }
128
129
130 prefix_ void senf::BaseTLVPacketParser:: maxLengthValue(DynamicTLVLengthParser::value_type v)
131     const
132 {
133     if (v <= 127)
134         return;
135     size_type b = senf::bytes( length_());
136     if (v <= UInt8Parser::max_value) {
137         if (b < 2) length_().resize(2);
138         return;
139     }
140     if (v <= UInt16Parser::max_value) {
141         if (b < 3) length_().resize(3);
142         return;
143     }
144     if (v <= UInt24Parser::max_value) {
145         if (b < 4) length_().resize(4);
146         return;
147     }
148     if (b < 5) length_().resize(5);
149 }
150
151
152 prefix_ senf::PacketInterpreterBase::range senf::GenericTLVPacketParser::value() 
153     const
154 {
155     senf::PacketData::iterator begin (boost::next(data().begin(), 1 + length_bytes() ));
156     return PacketInterpreterBase::range(
157             begin, boost::next( begin, length()) );
158 }
159
160
161 prefix_ void senf::DynamicTLVLengthParser::resize(size_type size)
162 {
163     value_type v = value();
164     size_type current_size (bytes());
165     SafePacketParserWrapper<DynamicTLVLengthParser> safeThis (*this);
166     
167     safe_data_iterator si (data(), i());
168     if (current_size > size)
169         data().erase( si, boost::next(si, current_size-size));
170     else
171         data().insert( si, size-current_size, 0);
172     
173     if (size > 1) {
174         safeThis->extended_length_flag() = true;
175         safeThis->fixed_length_field() = size - 1;
176     } else {
177         safeThis->extended_length_flag() = false;
178     }
179     value(v);
180 }
181
182
183 prefix_ void senf::GenericTLVPacketType::dump(packet p, std::ostream & os)
184 {
185     boost::io::ios_all_saver ias(os);
186     os << "GenericTLVPacket:\n"
187        << std::dec
188        << "  type:   " << unsigned( p->type()) << "\n"
189        << "  length: " << unsigned( p->length()) << "\n"
190        << "  value\n:";
191     senf::hexdump( p->value().begin(), p->value().end(), os);
192 }
193
194
195 prefix_ void senf::GenericTLVPacketType::finalize(packet p)
196 {
197     p->shrinkLength();
198 }
199
200
201 ///////////////////////////////cc.e////////////////////////////////////////
202 #undef prefix_
203
204 \f
205 // Local Variables:
206 // mode: c++
207 // fill-column: 100
208 // c-file-style: "senf"
209 // indent-tabs-mode: nil
210 // ispell-local-dictionary: "american"
211 // compile-command: "scons -u test"
212 // comment-column: 40
213 // End: