Packets/80221Bundle: just another fix for DynamicTLVLengthParser
[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 "../../Utils/hexdump.hh"
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 prefix_ senf::safe_data_iterator senf::BaseTLVPacketParser::resizeValueField(
37         DynamicTLVLengthParser::value_type size) 
38 {
39     DynamicTLVLengthParser::value_type current_length ( length());
40     length( size);
41
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));
45     else
46         data().insert( si, size-current_length, 0);
47     return si;
48 }
49
50
51 prefix_ senf::DynamicTLVLengthParser::value_type senf::DynamicTLVLengthParser::value() const 
52 {
53     switch (bytes() ) {
54     case 1:
55         return length_field().value();
56     case 2:
57         return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
58     case 3:
59         return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
60     case 4:
61         return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
62     case 5:
63         return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
64     default:
65         throw(TLVLengthException());
66     };
67 }
68
69
70 prefix_ void senf::DynamicTLVLengthParser::value(value_type const & v) 
71 {
72     switch (bytes() ) {
73     case 1:
74         if (v > 128) throw( TLVLengthException());
75         length_field() = v;
76         return;
77     case 2:
78         if (v > UInt8Parser::max_value + 128) throw( TLVLengthException());
79         parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
80         break;
81     case 3:
82         if (v > UInt16Parser::max_value + 128) throw( TLVLengthException());
83         parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
84         break;;
85     case 4:
86         if (v > UInt24Parser::max_value + 128) throw( TLVLengthException());
87         parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
88         break;
89     case 5:
90         parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
91         break;
92     default:
93         throw( TLVLengthException());
94     };
95     underflow_flag() = v < 128;
96 }
97
98
99 prefix_ senf::DynamicTLVLengthParser const & senf::DynamicTLVLengthParser::operator= (value_type other) 
100 {
101     value(other);
102     return *this; 
103 }
104
105
106 prefix_ void senf::DynamicTLVLengthParser::init() const 
107 {
108     defaultInit();
109     extended_length_flag() = false;
110 }
111
112
113 prefix_ void senf::DynamicTLVLengthParser::shrink()
114 {
115     value_type v = value();
116     size_type b = bytes();
117     if (v <= 128) {
118         if (b != 1) resize(1);
119         return;
120     }
121     if (v <= UInt8Parser::max_value + 128) {
122         if (b != 2) resize(2);
123         return;
124     }
125     if (v <= UInt16Parser::max_value + 128) {
126         if (b != 3) resize(3);
127         return;
128     }
129     if (v <= UInt24Parser::max_value + 128 ){
130         if (b != 4) resize(4);
131         return;
132     }
133     if (b != 5) resize(5);
134 }
135
136
137 prefix_ void senf::DynamicTLVLengthParser:: maxValue(DynamicTLVLengthParser::value_type v)
138 {
139     if (v <= 128)
140         return;
141     size_type b = bytes();
142     if (v <= UInt8Parser::max_value + 128) {
143         if (b < 2) resize(2);
144         return;
145     }
146     if (v <= UInt16Parser::max_value + 128) {
147         if (b < 3) resize(3);
148         return;
149     }
150     if (v <= UInt24Parser::max_value + 128) {
151         if (b < 4) resize(4);
152         return;
153     }
154     if (b < 5) resize(5);
155 }
156
157
158 prefix_ void senf::DynamicTLVLengthParser::resize(size_type size)
159 {
160     value_type v = value();
161     size_type current_size (bytes());
162     SafePacketParserWrapper<DynamicTLVLengthParser> safeThis (*this);
163     
164     if (current_size > size)
165         data().erase( i(), boost::next(i(), current_size-size));
166     else
167         data().insert( i(), size-current_size, 0);
168     
169     if (size > 1) {
170         safeThis->extended_length_flag() = true;
171         safeThis->fixed_length_field() = size - 1;
172     } else {
173         safeThis->extended_length_flag() = false;
174     }
175     safeThis->value(v);
176 }
177
178
179 prefix_ senf::PacketInterpreterBase::range senf::GenericTLVPacketParser::value() 
180     const
181 {
182     senf::PacketData::iterator begin (boost::next(data().begin(), 1 + length_bytes() ));
183     return PacketInterpreterBase::range(
184             begin, boost::next( begin, length()) );
185 }
186
187
188 prefix_ void senf::GenericTLVPacketType::dump(packet p, std::ostream & os)
189 {
190     boost::io::ios_all_saver ias(os);
191     os << "GenericTLVPacket:\n"
192        << std::dec
193        << "  type:   " << unsigned( p->type()) << "\n"
194        << "  length: " << unsigned( p->length()) << "\n"
195        << "  value\n:";
196     senf::hexdump( p->value().begin(), p->value().end(), os);
197 }
198
199
200 prefix_ void senf::GenericTLVPacketType::finalize(packet p)
201 {
202     p->shrinkLength();
203 }
204
205
206 ///////////////////////////////cc.e////////////////////////////////////////
207 #undef prefix_
208
209 \f
210 // Local Variables:
211 // mode: c++
212 // fill-column: 100
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
218 // End: