Packets/80221Bundle: more GenericTLVBase integration; removed GenericTLVPacket; some...
[senf.git] / senf / Packets / 80221Bundle / TLVParser.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 TLVParser non-inline non-template implementation */
25
26 #include "TLVParser.hh"
27 //#include "TLVParser.ih"
28
29 // Custom includes
30 #include <iomanip>
31 #include <senf/Utils/hexdump.hh>
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // MIHBaseTLVParser
38
39 prefix_ senf::safe_data_iterator senf::MIHBaseTLVParser::resizeValueField(
40         MIHTLVLengthParser::value_type size) 
41 {
42     MIHTLVLengthParser::value_type current_length ( length());
43     length_() << size;
44
45     safe_data_iterator si (data(), boost::next(i(), 1 + length_().bytes() ));
46     if (current_length > size)
47         data().erase( si, boost::next(si, current_length-size));
48     else
49         data().insert( si, size-current_length, 0);
50     return si;
51 }
52
53
54 ///////////////////////////////////////////////////////////////////////////
55 // MIHTLVLengthParser
56
57 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::value() const 
58 {
59     switch (bytes() ) {
60     case 1:
61         return length_field().value();
62     case 2:
63         return parse<UInt8Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
64     case 3:
65         return parse<UInt16Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
66     case 4:
67         return parse<UInt24Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
68     case 5:
69         return parse<UInt32Parser>( 1 ).value() + (underflow_flag() ? 0 : 128u);
70     default:
71         throw( MIHTLVLengthException());
72     };
73 }
74
75
76 prefix_ void senf::MIHTLVLengthParser::value(value_type const & v) 
77 {
78     switch (bytes() ) {
79     case 1:
80         if (v > 128) throw( MIHTLVLengthException());
81         length_field() = v;
82         return;
83     case 2:
84         if (v > UInt8Parser::max_value + 128) throw( MIHTLVLengthException());
85         parse<UInt8Parser>(1) = v - (v>128 ? 128 : 0);
86         break;
87     case 3:
88         if (v > UInt16Parser::max_value + 128) throw( MIHTLVLengthException());
89         parse<UInt16Parser>(1) = v - (v>128 ? 128 : 0);
90         break;;
91     case 4:
92         if (v > UInt24Parser::max_value + 128) throw( MIHTLVLengthException());
93         parse<UInt24Parser>(1) = v - (v>128 ? 128 : 0);
94         break;
95     case 5:
96         parse<UInt32Parser>(1) = v - (v>128 ? 128 : 0);
97         break;
98     default:
99         throw( MIHTLVLengthException());
100     };
101     underflow_flag() = (v <= 128);
102 }
103
104
105 prefix_ senf::MIHTLVLengthParser::value_type senf::MIHTLVLengthParser::maxValue()
106     const
107 {
108     switch (bytes() ) {
109     case 1:
110         return 128;
111     case 2:
112         return UInt8Parser::max_value + 128;
113     case 3:
114         return UInt16Parser::max_value + 128;
115     case 4:
116         return UInt24Parser::max_value + 128;
117     case 5:
118         return UInt32Parser::max_value; 
119     default:
120         throw( MIHTLVLengthException());
121     };
122 }
123
124
125 prefix_ senf::MIHTLVLengthParser const & senf::MIHTLVLengthParser::operator= (value_type other) 
126 {
127     value(other);
128     return *this; 
129 }
130
131
132 prefix_ void senf::MIHTLVLengthParser::init() const 
133 {
134     defaultInit();
135     extended_length_flag() = false;
136 }
137
138
139 prefix_ void senf::MIHTLVLengthParser::finalize()
140 {
141     value_type v = value();
142     size_type b = bytes();
143     if (v <= 128) {
144         if (b != 1) resize_(1);
145         return;
146     }
147     if (v <= UInt8Parser::max_value + 128) {
148         if (b != 2) resize_(2);
149         return;
150     }
151     if (v <= UInt16Parser::max_value + 128) {
152         if (b != 3) resize_(3);
153         return;
154     }
155     if (v <= UInt24Parser::max_value + 128 ) {
156         if (b != 4) resize_(4);
157         return;
158     }
159     if (b != 5) resize_(5);
160 }
161
162
163 prefix_ void senf::MIHTLVLengthParser:: maxValue(MIHTLVLengthParser::value_type v)
164 {
165     if (v <= 128)
166         return;
167     size_type b = bytes();
168     if (v <= UInt8Parser::max_value + 128) {
169         if (b < 2) resize_(2);
170         return;
171     }
172     if (v <= UInt16Parser::max_value + 128) {
173         if (b < 3) resize_(3);
174         return;
175     }
176     if (v <= UInt24Parser::max_value + 128) {
177         if (b < 4) resize_(4);
178         return;
179     }
180     if (b < 5) resize_(5);
181 }
182
183
184 prefix_ void senf::MIHTLVLengthParser::resize_(size_type size)
185 {
186     value_type v = value();
187     resize(bytes(), size);
188     if (size > 1) {
189         extended_length_flag() = true;
190         fixed_length_field() = size - 1;
191     } else {
192         extended_length_flag() = false;
193     }
194     value(v);
195 }
196
197
198 ///////////////////////////////cc.e////////////////////////////////////////
199 #undef prefix_
200
201 \f
202 // Local Variables:
203 // mode: c++
204 // fill-column: 100
205 // c-file-style: "senf"
206 // indent-tabs-mode: nil
207 // ispell-local-dictionary: "american"
208 // compile-command: "scons -u test"
209 // comment-column: 40
210 // End: