Add 'unflatten' to doxygen/dot processing
[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::resizeValue(
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 fixed_length_field().value();
56     case 2:
57         return parse<UInt8Parser>( 1 ).value();
58     case 3:
59         return parse<UInt16Parser>( 1 ).value();
60     case 4:
61         return parse<UInt24Parser>( 1 ).value();
62     case 5:
63         return parse<UInt32Parser>( 1 ).value();
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 > 127) throw( TLVLengthException());
75         fixed_length_field() = v;
76         return;
77     case 2:
78         if (v > UInt8Parser::max_value) throw( TLVLengthException());
79         parse<UInt8Parser>(1) = v;
80         return;
81     case 3:
82         if (v > UInt16Parser::max_value) throw( TLVLengthException());
83         parse<UInt16Parser>(1) = v;
84         return;
85     case 4:
86         if (v > UInt24Parser::max_value) throw( TLVLengthException());
87         parse<UInt24Parser>(1) = v;
88         return;
89     case 5:
90         parse<UInt32Parser>(1) = v;
91         return;
92     default:
93         throw( TLVLengthException());
94     };
95 }
96
97
98 prefix_ senf::DynamicTLVLengthParser const & senf::DynamicTLVLengthParser::operator= (value_type other) 
99 {
100     value(other);
101     return *this; 
102 }
103
104
105 prefix_ senf::DynamicTLVLengthParser::size_type senf::DynamicTLVLengthParser::bytes() const 
106 {
107     if ( extended_length_flag() )
108         return 1 + fixed_length_field();
109     else
110         return 1;
111 }
112     
113
114 prefix_ void senf::DynamicTLVLengthParser::init() const 
115 {
116     defaultInit();
117     extended_length_flag() = false;
118 }
119
120
121 prefix_ void senf::DynamicTLVLengthParser::shrink()
122 {
123     value_type v = value();
124     size_type b = bytes();
125     if (v <= 127) {
126         if (b != 1) resize(1);
127         return;
128     }
129     if (v <= UInt8Parser::max_value) {
130         if (b != 2) resize(2);
131         return;
132     }
133     if (v <= UInt16Parser::max_value) {
134         if (b != 3) resize(3);
135         return;
136     }
137     if (v <= UInt24Parser::max_value) {
138         if (b != 4) resize(4);
139         return;
140     }
141     if (b != 5) resize(5);
142 }
143
144
145 prefix_ void senf::DynamicTLVLengthParser:: maxValue(DynamicTLVLengthParser::value_type v)
146 {
147     if (v <= 127)
148         return;
149     size_type b = bytes();
150     if (v <= UInt8Parser::max_value) {
151         if (b < 2) resize(2);
152         return;
153     }
154     if (v <= UInt16Parser::max_value) {
155         if (b < 3) resize(3);
156         return;
157     }
158     if (v <= UInt24Parser::max_value) {
159         if (b < 4) resize(4);
160         return;
161     }
162     if (b < 5) resize(5);
163 }
164
165
166 prefix_ void senf::DynamicTLVLengthParser::resize(size_type size)
167 {
168     value_type v = value();
169     size_type current_size (bytes());
170     SafePacketParserWrapper<DynamicTLVLengthParser> safeThis (*this);
171     
172     if (current_size > size)
173         data().erase( i(), boost::next(i(), current_size-size));
174     else
175         data().insert( i(), size-current_size, 0);
176     
177     if (size > 1) {
178         safeThis->extended_length_flag() = true;
179         safeThis->fixed_length_field() = size - 1;
180     } else {
181         safeThis->extended_length_flag() = false;
182     }
183     safeThis->value(v);
184 }
185
186
187 prefix_ senf::PacketInterpreterBase::range senf::GenericTLVPacketParser::value() 
188     const
189 {
190     senf::PacketData::iterator begin (boost::next(data().begin(), 1 + length_bytes() ));
191     return PacketInterpreterBase::range(
192             begin, boost::next( begin, length()) );
193 }
194
195
196 prefix_ void senf::GenericTLVPacketType::dump(packet p, std::ostream & os)
197 {
198     boost::io::ios_all_saver ias(os);
199     os << "GenericTLVPacket:\n"
200        << std::dec
201        << "  type:   " << unsigned( p->type()) << "\n"
202        << "  length: " << unsigned( p->length()) << "\n"
203        << "  value\n:";
204     senf::hexdump( p->value().begin(), p->value().end(), os);
205 }
206
207
208 prefix_ void senf::GenericTLVPacketType::finalize(packet p)
209 {
210     p->shrinkLength();
211 }
212
213
214 ///////////////////////////////cc.e////////////////////////////////////////
215 #undef prefix_
216
217 \f
218 // Local Variables:
219 // mode: c++
220 // fill-column: 100
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
226 // End: