KItinerary

protobufstreamreader.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "protobufstreamreader.h"
7
8#include <QDebug>
9
10using namespace KItinerary;
11
12ProtobufStreamReader::ProtobufStreamReader() = default;
13
14ProtobufStreamReader::ProtobufStreamReader(std::string_view data)
15 : m_data(data)
16{
17}
18
19ProtobufStreamReader::ProtobufStreamReader(const QByteArray &data)
20 : m_ownedData(data)
21 , m_data(std::string_view(data.constData(), data.size()))
22{
23}
24
25ProtobufStreamReader::~ProtobufStreamReader() = default;
26
27uint64_t ProtobufStreamReader::readVarint()
28{
29 uint64_t result = 0;
30 int shift = 0;
31 while (m_cursor < m_data.size()) {
32 const uint64_t b = m_data[m_cursor++];
33 result |= (b & 0b0111'1111) << shift;
34 if ((b & 0b1000'0000) == 0) {
35 break;
36 }
37 shift += 7;
38 }
39
40 return result;
41}
42
43uint64_t ProtobufStreamReader::peekVarint()
44{
45 auto prevCursor = m_cursor;
46 const auto result = readVarint();
47 m_cursor = prevCursor;
48 return result;
49}
50
52{
53 return peekVarint() >> 3;
54}
55
56ProtobufStreamReader::WireType ProtobufStreamReader::wireType()
57{
58 return static_cast<WireType>(peekVarint() & 0b111);
59}
60
62{
63 readVarint(); // skip field number and wire type
64 return readVarint();
65}
66
68{
69 if (wireType() != LEN) {
70 return {};
71 }
72 readVarint(); // skip field number and wire type
73 const auto len = readVarint();
74 if (m_cursor + len <= m_data.size()) {
75 auto data = m_data.substr(m_cursor, len);
76 m_cursor += len;
77 return data;
78 }
79 return {};
80}
81
83{
84 const auto data = readLengthDelimitedRecord();
85 return QString::fromUtf8(data.data(), data.size());
86}
87
92
94{
95 return m_cursor >= m_data.size();
96}
97
99{
100 switch (wireType()) {
101 case VARINT:
103 break;
104 case LEN:
106 break;
107 case I64:
108 case I32:
109 case SGROUP:
110 case EGROUP:
111 qWarning() << "encountered deprecated or unsupported protobuf wire type!" << wireType();
112 m_cursor = m_data.size();
113 break;
114 }
115}
116
117#include "moc_protobufstreamreader.cpp"
Protocol Buffers stream reader.
Q_INVOKABLE void skip()
Skips over the next field in the stream.
Q_INVOKABLE QString readString()
Reads a string.
Q_INVOKABLE WireType wireType()
Returns the wire type of the current field.
Q_INVOKABLE bool atEnd() const
Returns true when having reached the end of the stream.
Q_INVOKABLE KItinerary::ProtobufStreamReader readSubMessage()
Reads a nested message.
Q_INVOKABLE quint64 fieldNumber()
Returns the number of the current field.
std::string_view readLengthDelimitedRecord()
Reads a field of type LEN.
Q_INVOKABLE quint64 readVarintField()
Read a field of type VARINT.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:45:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.