AusweisApp2
LengthValue.h
gehe zur Dokumentation dieser Datei
1 
7 #pragma once
8 
9 #include <QByteArray>
10 #include <QtEndian>
11 
12 #include <algorithm>
13 #include <climits>
14 
15 
16 namespace governikus
17 {
18 
20 {
21  private:
22  LengthValue() = delete;
23  ~LengthValue() = delete;
24 
25  public:
26  template<typename T> static QByteArray readByteArray(const QByteArray& pInput, int& pOffset)
27  {
28  Q_ASSERT(sizeof(T) < INT_MAX);
29 
30  const auto typeLength = static_cast<int>(sizeof(T));
31  if (pInput.size() < pOffset + typeLength)
32  {
33  return QByteArray();
34  }
35 
36  const T length = qFromLittleEndian<T>(pInput.data() + pOffset);
37  pOffset += typeLength;
38  const QByteArray result = pInput.mid(pOffset, length);
39  pOffset += length;
40  return result;
41  }
42 
43 
44  template<typename T> static void writeByteArray(const QByteArray& pValue, QByteArray& pOutput)
45  {
46  Q_ASSERT(sizeof(T) < INT_MAX);
47 
48  const int maxSize = std::numeric_limits<T>::max();
49 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
50  const int size = std::min(maxSize, pValue.size());
51 #else
52  const int size = std::min(maxSize, static_cast<int>(pValue.size()));
53 #endif
54 
55  const auto it = pOutput.size();
56  pOutput.resize(it + static_cast<int>(sizeof(T)));
57  qToLittleEndian(static_cast<T>(size), pOutput.data() + it);
58  pOutput += pValue.mid(0, size);
59  }
60 
61 
62 };
63 
64 } // namespace governikus
Definition: LengthValue.h:20
static QByteArray readByteArray(const QByteArray &pInput, int &pOffset)
Definition: LengthValue.h:26
static void writeByteArray(const QByteArray &pValue, QByteArray &pOutput)
Definition: LengthValue.h:44
#define T(v)
Definition: http_parser.cpp:237
Implementation of ActivationContext for Intent based activation on Android systems.
Definition: ActivationContext.h:15