KItinerary

berelement.h
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "kitinerary_export.h"
10
11#include <QByteArray>
12
13#include <cstdint>
14
15class QIODevice;
16
17namespace KItinerary {
18
19/** BER/DER/X.690 encoding classes and functions. */
20namespace BER {
21
22/**
23 * An element in BER/DER/X.690 encoding.
24 * Implicitly this is also kinda implementing a QByteArrayRef, as this works without copying
25 * the underlying data.
26 */
27class KITINERARY_EXPORT Element
28{
29public:
30 Element();
31 explicit Element(const QByteArray &data, int offset = 0, int size = -1);
32 ~Element();
33
34 /** Returns @c true if this element has a valid structure and can be read from. */
35 bool isValid() const;
36
37 /** Type, "right-aligned" in the returned 32bit value. */
38 uint32_t type() const;
39
40 /** Size of the entire element (type, size and content). */
41 int size() const;
42 /** Raw data of this element.
43 * Typically only needed when copying/writing this element somewhere.
44 */
45 const char* rawData() const;
46
47 /** Size of the value part of this element.
48 * This is excluding a possible variable length end marker.
49 */
50 int contentSize() const;
51 /** Raw content data. */
52 const uint8_t* contentData() const;
53
54 /** Convenience method to access typed content. */
55 template <typename T>
56 inline const T* contentAt(int offset = 0) const
57 {
58 if (offset < 0 || (int)sizeof(T) > contentSize() - offset) {
59 return nullptr;
60 }
61 return reinterpret_cast<const T*>(contentData() + offset);
62 }
63
64 /** First child element, for nested types. */
65 Element first() const;
66 /** Next child element, for nested types. */
67 Element next() const;
68 /** Returns the first child element of the given @p type. */
69 Element find(uint32_t type) const;
70
71 /** Writes the given size in BER encoding to @p out. */
72 static void writeSize(QIODevice *out, int size);
73
74private:
75 int typeSize() const;
76 int lengthSize() const;
77 int contentOffset() const;
78
79 QByteArray m_data;
80 int m_offset = -1;
81 int m_dataSize = -1;
82};
83
84template <uint32_t TagValue>
85class TypedElement : public Element
86{
87public:
88 using Element::Element;
89 inline bool isValid() const
90 {
91 return Element::isValid() && type() == TagValue;
92 }
93};
94
95}
96}
97
An element in BER/DER/X.690 encoding.
Definition berelement.h:28
const T * contentAt(int offset=0) const
Convenience method to access typed content.
Definition berelement.h:56
uint32_t type() const
Type, "right-aligned" in the returned 32bit value.
bool isValid() const
Returns true if this element has a valid structure and can be read from.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.