Packets: Fix VariantParser invalid parser access bug
[senf.git] / Packets / MPEGDVBBundle / TLVPacket.hh
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 public header */
25
26 #ifndef HH_SENF_Packets_MPEGDVBBundle_TLVPacket_
27 #define HH_SENF_Packets_MPEGDVBBundle_TLVPacket_ 1
28
29 // Custom includes
30 #include <algorithm>
31 #include "../../Packets/Packets.hh"
32
33 //#include "TLVPacket.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35
36 namespace senf {
37
38     /** \brief xxx
39         
40         \todo document me
41         \todo add usefull exceptions strings
42          
43     */
44     struct UnsuportedTLVPacketException : public senf::Exception
45     { UnsuportedTLVPacketException() 
46           : senf::Exception("length of length can be max. 4 bytes. Sorry."){} };
47
48     /** \brief xxx
49         \todo document me
50     */
51     class DynamicTLVLengthParser 
52         : public detail::packet::IntParserOps<DynamicTLVLengthParser, boost::uint32_t>,
53           public PacketParserBase
54     {
55     public:
56         DynamicTLVLengthParser(data_iterator i, state_type s) : PacketParserBase(i,s) {}
57
58         typedef boost::uint32_t value_type;
59         static const size_type init_bytes = 1;
60         static value_type const min_value = 0;
61         static value_type const max_value = 4294967295u;
62
63         value_type value() const;
64         void value(value_type const & v);
65         
66         DynamicTLVLengthParser const & operator= (value_type other);
67         size_type bytes() const;
68         void init() const;
69
70     private:
71         typedef FlagParser      <    0 > ExtendedLengthFlagParser;
72         typedef UIntFieldParser < 1, 8 > FixedLengthParser;
73
74         ExtendedLengthFlagParser extended_length_flag() const {
75             return parse<ExtendedLengthFlagParser>( 0 );
76         }
77
78         FixedLengthParser fixed_length_field() const {
79             return parse<FixedLengthParser>( 0 );
80         }
81         
82         void resize(size_type size);
83     };  
84         
85     /** \brief parse TLVPacket Packet
86     
87         \todo document me
88      
89         \see TLVPacketType
90      */
91     template <class TypeParser, class LengthParser>
92     struct TLVPacketParser : public PacketParserBase
93     {
94 #       include SENF_PARSER()
95         
96         SENF_PARSER_FIELD( type,   TypeParser );
97         SENF_PARSER_FIELD( length, LengthParser );
98         
99         SENF_PARSER_FINALIZE(TLVPacketParser);
100     };
101     
102     /** \brief generic TLV Packet type
103         
104         \todo document me
105         
106         \ingroup protocolbundle_mpegdvb
107      */
108     template <class TypeParser, class LengthParser>
109     struct TLVPacketType
110         : public PacketTypeBase
111     {
112         typedef ConcretePacket<TLVPacketType<TypeParser, LengthParser> > packet;
113         typedef TLVPacketParser<TypeParser, LengthParser> parser;
114
115         static optional_range nextPacketRange(packet p);
116         static size_type initSize();
117         
118         static void finalize(packet p);
119         
120         static void dump(packet p, std::ostream & os);
121     };
122     
123     typedef ConcretePacket<TLVPacketType<UInt32Parser, DynamicTLVLengthParser> > MIHInfoElement;
124 }
125
126
127 ///////////////////////////////hh.e////////////////////////////////////////
128 //#include "TLVPacket.cci"
129 #include "TLVPacket.ct"
130 //#include "TLVPacket.cti"
131 #endif
132
133 \f
134 // Local Variables:
135 // mode: c++
136 // fill-column: 100
137 // c-file-style: "senf"
138 // indent-tabs-mode: nil
139 // ispell-local-dictionary: "american"
140 // compile-command: "scons -u test"
141 // comment-column: 40
142 // End: