Commit 090639e5 authored by Volker Krause's avatar Volker Krause
Browse files

Add method to find BER sub-element of a given type

parent eaa28a24
......@@ -134,6 +134,29 @@ private Q_SLOTS:
}
QCOMPARE(i, childCount);
}
void testBerFindChild_data()
{
QTest::addColumn<QByteArray>("input");
QTest::addColumn<uint32_t>("type");
QTest::addColumn<bool>("found");
QTest::newRow("no children") << QByteArray::fromHex("020142") << 0x02u << false;
QTest::newRow("children, not found") << QByteArray::fromHex("1E06020142020123") << 0x04u << false;
QTest::newRow("children, first hit") << QByteArray::fromHex("1E06040142020123") << 0x04u << true;
QTest::newRow("children, second hit") << QByteArray::fromHex("1E06020142040123") << 0x04u << true;
}
void testBerFindChild()
{
QFETCH(QByteArray, input);
QFETCH(uint32_t, type);
QFETCH(bool, found);
BER::Element e(input);
QVERIFY(e.isValid());
auto c = e.find(type);
QCOMPARE(c.isValid(), found);
}
};
QTEST_APPLESS_MAIN(BerDecoderTest)
......
......@@ -172,3 +172,15 @@ BER::Element BER::Element::next() const
}
return BER::Element(m_data, m_offset + s, m_dataSize - m_offset - s);
}
BER::Element BER::Element::find(uint32_t type) const
{
auto e = first();
while (e.isValid()) {
if (e.type() == type) {
return e;
}
e = e.next();
}
return {};
}
......@@ -69,6 +69,8 @@ public:
Element first() const;
/** Next child element, for nested types. */
Element next() const;
/** Returns the first child element of the given @p type. */
Element find(uint32_t type) const;
private:
int typeSize() const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment