]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAVariantRecordDataType.cxx
hla: Fix buffer overrun in SGMath vector types.
[simgear.git] / simgear / hla / HLAVariantRecordDataType.cxx
1 // Copyright (C) 2009 - 2012  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #include "HLAVariantRecordDataType.hxx"
19
20 #include "HLADataTypeVisitor.hxx"
21 #include "HLAVariantRecordDataElement.hxx"
22
23 namespace simgear {
24
25 HLAVariantRecordDataType::HLAVariantRecordDataType(const std::string& name) :
26     HLADataType(name)
27 {
28 }
29
30 HLAVariantRecordDataType::~HLAVariantRecordDataType()
31 {
32 }
33
34 void
35 HLAVariantRecordDataType::accept(HLADataTypeVisitor& visitor) const
36 {
37     visitor.apply(*this);
38 }
39
40 const HLAVariantRecordDataType*
41 HLAVariantRecordDataType::toVariantRecordDataType() const
42 {
43     return this;
44 }
45
46 void
47 HLAVariantRecordDataType::releaseDataTypeReferences()
48 {
49     for (AlternativeList::iterator i = _alternativeList.begin(); i != _alternativeList.end(); ++i)
50         i->_dataType = 0;
51 }
52
53 bool
54 HLAVariantRecordDataType::decode(HLADecodeStream& stream, HLAAbstractVariantRecordDataElement& value) const
55 {
56     if (!stream.alignOffsetForSize(getAlignment()))
57         return false;
58     if (!_enumeratedDataType.valid())
59         return false;
60     unsigned index = ~0u;
61     if (!_enumeratedDataType->decode(stream, index))
62         return false;
63     if (!value.setAlternativeIndex(index))
64         return false;
65     if (!value.decodeAlternative(stream))
66         return false;
67     return true;
68 }
69
70 bool
71 HLAVariantRecordDataType::encode(HLAEncodeStream& stream, const HLAAbstractVariantRecordDataElement& value) const
72 {
73     if (!stream.alignOffsetForSize(getAlignment()))
74         return false;
75     if (!_enumeratedDataType.valid())
76         return false;
77     unsigned index = value.getAlternativeIndex();
78     if (!_enumeratedDataType->encode(stream, index))
79         return false;
80     if (!value.encodeAlternative(stream))
81         return false;
82     return true;
83 }
84
85 void
86 HLAVariantRecordDataType::setEnumeratedDataType(HLAEnumeratedDataType* dataType)
87 {
88     _enumeratedDataType = dataType;
89 }
90
91 bool
92 HLAVariantRecordDataType::addAlternative(const std::string& name, const std::string& enumerator,
93                                          const HLADataType* dataType, const std::string& semantics)
94 {
95     if (!_enumeratedDataType.valid())
96         return false;
97     unsigned index = _enumeratedDataType->getIndex(enumerator);
98     if (_enumeratedDataType->getNumEnumerators() <= index)
99         return false;
100     _alternativeList.resize(_enumeratedDataType->getNumEnumerators());
101     _alternativeList[index]._name = name;
102     _alternativeList[index]._dataType = dataType;
103     _alternativeList[index]._semantics = semantics;
104     return true;
105 }
106
107 void
108 HLAVariantRecordDataType::_recomputeAlignmentImplementation()
109 {
110     unsigned alignment = 1;
111     if (const HLADataType* dataType = getEnumeratedDataType())
112         alignment = std::max(alignment, dataType->getAlignment());
113     for (unsigned i = 0; i < getNumAlternatives(); ++i) {
114         if (const HLADataType* dataType = getAlternativeDataType(i))
115             alignment = std::max(alignment, dataType->getAlignment());
116     }
117     setAlignment(alignment);
118 }
119
120 } // namespace simgear