image added for tlv
[senf.git] / Packets / MPEGDVBBundle / TLVPacket.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.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::Parse_TLVPacketLength::value_type senf::Parse_TLVPacketLength::value() const 
37 {
38     switch (bytes() ) {
39     case 1:
40         return fixed_length_field().value();
41     case 2:
42         return parse<Parse_UInt8>( 1 ).value();
43     case 3:
44         return parse<Parse_UInt16>( 1 ).value();
45     case 4:
46         return parse<Parse_UInt24>( 1 ).value();
47     case 5:
48         return parse<Parse_UInt32>( 1 ).value();
49     default:
50         throw(UnsuportedTLVPacketException());
51     };
52 }
53
54 prefix_ void senf::Parse_TLVPacketLength::value(value_type const & v) 
55 {
56     if (v > 4294967295u)
57         throw(UnsuportedTLVPacketException());
58     
59     SafePacketParser<Parse_TLVPacketLength> safeThis (*this);
60     if (v < 128u) {
61         if (bytes() != 1) {
62             resize(1);
63             safeThis->extended_length_flag() = false;
64         }
65         safeThis->fixed_length_field() = v;
66         return;
67     }
68     if (v < 256u) {
69         if (bytes() != 2) {
70             resize(2);
71             safeThis->extended_length_flag() = true;
72             safeThis->fixed_length_field() = 1;
73         }
74         safeThis->parse<Parse_UInt8>(1) = v;
75         return;
76     }
77     if (v < 65536u) {
78         if (bytes() != 3) {
79             resize(3);
80             safeThis->extended_length_flag() = true;
81             safeThis->fixed_length_field() = 2;
82         }
83         safeThis->parse<Parse_UInt16>(1) = v;
84         return;
85     }
86     if (v < 16777216u) {
87         if (bytes() != 4) {
88             resize(4);
89             safeThis->extended_length_flag() = true;
90             safeThis->fixed_length_field() = 3;
91         }
92         safeThis->parse<Parse_UInt24>(1) = v;
93         return;
94     }
95     if (v <= 4294967295u) {
96         if (bytes() != 5) {
97             resize(5);
98             safeThis->extended_length_flag() = true;
99             safeThis->fixed_length_field() = 4;
100         }
101         safeThis->parse<Parse_UInt32>(1) = v;
102         return;
103     }
104 }
105
106 prefix_ senf::Parse_TLVPacketLength const & senf::Parse_TLVPacketLength::operator= (value_type other) 
107 {
108     value(other);
109     return *this; 
110 }
111
112 prefix_ senf::Parse_TLVPacketLength::size_type senf::Parse_TLVPacketLength::bytes() const 
113 {
114     if ( extended_length_flag() )
115         return 1 + fixed_length_field();
116     else
117         return 1;
118 }
119     
120 prefix_ void senf::Parse_TLVPacketLength::init() const 
121 {
122     defaultInit();
123     extended_length_flag() = 0;
124 }
125
126 prefix_ void senf::Parse_TLVPacketLength::resize(size_type size) 
127 {
128     size_type current_size (bytes());
129     safe_data_iterator si (data(), i());
130     
131     if (current_size > size)
132         data().erase( si, boost::next(si, current_size-size));
133     else
134         data().insert( si, size-current_size, 0);
135 }
136
137 prefix_ void senf::TLVPacketType::dump(packet p, std::ostream & os)
138 {
139     os << "TLVPacket:\n"
140        << std::dec
141        << "  type: " <<  unsigned(p->type()) << "\n"
142        << "  length: " << unsigned(p->length()) << "\n";
143 }
144
145 prefix_ senf::PacketParserBase::size_type senf::TLVPacketType::initSize()
146 {
147     return 5;  // 4 bytes type + 1 byte length
148 }
149
150 prefix_ void senf::TLVPacketType::init(packet p)
151 {
152     p->init();
153 }
154
155 prefix_ void senf::TLVPacketType::finalize(packet p)
156 {
157     p->length() = p.next().data().size();
158 }
159
160 prefix_ senf::PacketInterpreterBase::optional_range 
161 senf::TLVPacketType::nextPacketRange(packet p) 
162 {
163     if (p.data().size() < 5)
164         return no_range();
165     return range(
166             boost::next(p.data().begin(), 4 + senf::bytes(p->length()) ),
167             p.data().end() );
168 }
169
170
171 ///////////////////////////////cc.e////////////////////////////////////////
172 #undef prefix_
173
174 \f
175 // Local Variables:
176 // mode: c++
177 // fill-column: 100
178 // c-file-style: "senf"
179 // indent-tabs-mode: nil
180 // ispell-local-dictionary: "american"
181 // compile-command: "scons -u test"
182 // comment-column: 40
183 // End: