doclib: changed copyright information in footer
[senf.git] / senf / 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::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::value_type senf::DynamicTLVLengthParser::maxValue()
100     const
101 {
102     switch (bytes() ) {
103     case 1:
104         return 128;
105     case 2:
106         return UInt8Parser::max_value + 128;
107     case 3:
108         return UInt16Parser::max_value + 128;
109     case 4:
110         return UInt24Parser::max_value + 128;
111     case 5:
112         return UInt32Parser::max_value; 
113     default:
114         throw( TLVLengthException());
115     };
116 }
117
118
119 prefix_ senf::DynamicTLVLengthParser const & senf::DynamicTLVLengthParser::operator= (value_type other) 
120 {
121     value(other);
122     return *this; 
123 }
124
125
126 prefix_ void senf::DynamicTLVLengthParser::init() const 
127 {
128     defaultInit();
129     extended_length_flag() = false;
130 }
131
132
133 prefix_ void senf::DynamicTLVLengthParser::finalize()
134 {
135     value_type v = value();
136     size_type b = bytes();
137     if (v <= 128) {
138         if (b != 1) resize(1);
139         return;
140     }
141     if (v <= UInt8Parser::max_value + 128) {
142         if (b != 2) resize(2);
143         return;
144     }
145     if (v <= UInt16Parser::max_value + 128) {
146         if (b != 3) resize(3);
147         return;
148     }
149     if (v <= UInt24Parser::max_value + 128 ) {
150         if (b != 4) resize(4);
151         return;
152     }
153     if (b != 5) resize(5);
154 }
155
156
157 prefix_ void senf::DynamicTLVLengthParser:: maxValue(DynamicTLVLengthParser::value_type v)
158 {
159     if (v <= 128)
160         return;
161     size_type b = bytes();
162     if (v <= UInt8Parser::max_value + 128) {
163         if (b < 2) resize(2);
164         return;
165     }
166     if (v <= UInt16Parser::max_value + 128) {
167         if (b < 3) resize(3);
168         return;
169     }
170     if (v <= UInt24Parser::max_value + 128) {
171         if (b < 4) resize(4);
172         return;
173     }
174     if (b < 5) resize(5);
175 }
176
177
178 prefix_ void senf::DynamicTLVLengthParser::resize(size_type size)
179 {
180     value_type v = value();
181     size_type current_size (bytes());
182     SafePacketParserWrapper<DynamicTLVLengthParser> safeThis (*this);
183     
184     if (current_size > size)
185         data().erase( i(), boost::next(i(), current_size-size));
186     else
187         data().insert( i(), size-current_size, 0);
188     
189     if (size > 1) {
190         safeThis->extended_length_flag() = true;
191         safeThis->fixed_length_field() = size - 1;
192     } else {
193         safeThis->extended_length_flag() = false;
194     }
195     safeThis->value(v);
196 }
197
198
199 prefix_ senf::PacketInterpreterBase::range senf::GenericTLVPacketParser::value() 
200     const
201 {
202     senf::PacketData::iterator begin (boost::next(data().begin(), 1 + length_().bytes() ));
203     return PacketInterpreterBase::range(
204             begin, boost::next( begin, length()) );
205 }
206
207
208 prefix_ void senf::GenericTLVPacketType::dump(packet p, std::ostream & os)
209 {
210     boost::io::ios_all_saver ias(os);
211     os << "GenericTLVPacket:\n"
212        << std::dec
213        << senf::fieldName("type")                      << unsigned( p->type()) << "\n"
214        << senf::fieldName("length")                    << unsigned( p->length()) << "\n"
215        << "  value:\n";
216     senf::hexdump( p->value().begin(), p->value().end(), os);
217 }
218
219
220 ///////////////////////////////cc.e////////////////////////////////////////
221 #undef prefix_
222
223 \f
224 // Local Variables:
225 // mode: c++
226 // fill-column: 100
227 // c-file-style: "senf"
228 // indent-tabs-mode: nil
229 // ispell-local-dictionary: "american"
230 // compile-command: "scons -u test"
231 // comment-column: 40
232 // End: