| 1 | // Copyright (C) 2022 The Qt Company Ltd. |
| 2 | // Copyright (C) 2019 Alexey Edelev <semlanik@gmail.com>, Viktor Kopp <vifactor@gmail.com> |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QPROTOBUFSERIALIZER_P_H |
| 6 | #define QPROTOBUFSERIALIZER_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists purely as an |
| 13 | // implementation detail. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include <QtProtobuf/qabstractprotobufserializer.h> |
| 20 | #include <QtProtobuf/qprotobufpropertyordering.h> |
| 21 | #include <QtProtobuf/qprotobufserializer.h> |
| 22 | #include <QtProtobuf/qprotobufrepeatediterator.h> |
| 23 | #include <QtProtobuf/qtprotobuftypes.h> |
| 24 | |
| 25 | #include <QtProtobuf/private/protobuffieldpresencechecker_p.h> |
| 26 | #include <QtProtobuf/private/qtprotobuflogging_p.h> |
| 27 | #include <QtProtobuf/private/qprotobufdeserializerbase_p.h> |
| 28 | #include <QtProtobuf/private/qprotobufselfcheckiterator_p.h> |
| 29 | #include <QtProtobuf/private/qprotobufserializerbase_p.h> |
| 30 | |
| 31 | #include <QtCore/qendian.h> |
| 32 | #include <QtCore/qstring.h> |
| 33 | #include <QtCore/qbytearray.h> |
| 34 | #include <QtCore/qhash.h> |
| 35 | #include <QtCore/qvariant.h> |
| 36 | |
| 37 | QT_BEGIN_NAMESPACE |
| 38 | |
| 39 | class QProtobufSerializerImpl final : public QProtobufSerializerBase |
| 40 | { |
| 41 | public: |
| 42 | explicit QProtobufSerializerImpl(QProtobufSerializerPrivate *parent); |
| 43 | ~QProtobufSerializerImpl(); |
| 44 | |
| 45 | const QByteArray &result() const { return m_result; } |
| 46 | |
| 47 | void reset(); |
| 48 | void serializeUnknownFields(const QProtobufMessage *message); |
| 49 | |
| 50 | QProtobufSerializerPrivate *m_parent = nullptr; |
| 51 | |
| 52 | private: |
| 53 | bool serializeEnum(QVariant &value, |
| 54 | const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo) override; |
| 55 | bool serializeScalarField(const QVariant &value, |
| 56 | const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo) override; |
| 57 | void serializeMessageFieldBegin() override; |
| 58 | void serializeMessageFieldEnd(const QProtobufMessage *message, |
| 59 | const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo) override; |
| 60 | |
| 61 | static QByteArray (int fieldNumber, QtProtobuf::WireTypes wireType); |
| 62 | |
| 63 | QByteArray m_result; |
| 64 | QList<QByteArray> m_state; |
| 65 | |
| 66 | Q_DISABLE_COPY_MOVE(QProtobufSerializerImpl) |
| 67 | }; |
| 68 | |
| 69 | class QProtobufDeserializerImpl final : public QProtobufDeserializerBase |
| 70 | { |
| 71 | public: |
| 72 | explicit QProtobufDeserializerImpl(QProtobufSerializerPrivate *parent); |
| 73 | ~QProtobufDeserializerImpl(); |
| 74 | |
| 75 | void reset(QByteArrayView data); |
| 76 | |
| 77 | QProtobufSelfcheckIterator m_it = {}; |
| 78 | |
| 79 | private: |
| 80 | void setError(QAbstractProtobufSerializer::Error error, QAnyStringView errorString) override; |
| 81 | bool deserializeEnum(QVariant &value, |
| 82 | const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo) override; |
| 83 | int nextFieldIndex(QProtobufMessage *message) override; |
| 84 | bool deserializeScalarField(QVariant &value, |
| 85 | const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo) override; |
| 86 | |
| 87 | qsizetype skipField(const QProtobufSelfcheckIterator &fieldBegin); |
| 88 | void skipVarint(); |
| 89 | void skipLengthDelimited(); |
| 90 | void setUnexpectedEndOfStreamError(); |
| 91 | |
| 92 | [[nodiscard]] |
| 93 | static bool (QProtobufSelfcheckIterator &it, int &fieldIndex, |
| 94 | QtProtobuf::WireTypes &wireType); |
| 95 | |
| 96 | QtProtobuf::WireTypes m_wireType = QtProtobuf::WireTypes::Unknown; |
| 97 | QList<QByteArrayView::iterator> m_state; |
| 98 | QProtobufSerializerPrivate *m_parent = nullptr; |
| 99 | |
| 100 | Q_DISABLE_COPY_MOVE(QProtobufDeserializerImpl) |
| 101 | }; |
| 102 | |
| 103 | class QProtobufSerializerPrivate |
| 104 | { |
| 105 | public: |
| 106 | // Serializer is interface function for serialize method |
| 107 | using Serializer = QByteArray (*)(const QVariant &, const QByteArray &); |
| 108 | // Deserializer is interface function for deserialize method |
| 109 | using Deserializer = bool (*)(QProtobufSelfcheckIterator &, QVariant &); |
| 110 | // Function checks if value in QVariant is considered to be non-ignorable. |
| 111 | |
| 112 | // SerializationHandlers contains set of objects that required for class |
| 113 | // serializaion/deserialization |
| 114 | struct ProtobufSerializationHandler |
| 115 | { |
| 116 | QMetaType metaType; |
| 117 | Serializer serializer; // serializer assigned to class |
| 118 | Deserializer deserializer; // deserializer assigned to class |
| 119 | ProtobufFieldPresenceChecker::Function isPresent; // checks if contains non-ignorable value |
| 120 | QtProtobuf::WireTypes wireType; // Serialization WireType |
| 121 | }; |
| 122 | |
| 123 | QProtobufSerializerPrivate(); |
| 124 | ~QProtobufSerializerPrivate() = default; |
| 125 | |
| 126 | template<typename T, QByteArray (*s)(const T &)> |
| 127 | static QByteArray serializeWrapper(const QVariant &variantValue, const QByteArray &) |
| 128 | { |
| 129 | return header + s(variantValue.value<T>()); |
| 130 | } |
| 131 | |
| 132 | template<typename T, QByteArray (*s)(const T &, const QByteArray &)> |
| 133 | static QByteArray serializeNonPackedWrapper(const QVariant &variantValue, |
| 134 | const QByteArray &) |
| 135 | { |
| 136 | return s(variantValue.value<T>(), header); |
| 137 | } |
| 138 | |
| 139 | void clearError(); |
| 140 | |
| 141 | QAbstractProtobufSerializer::Error lastError = |
| 142 | QAbstractProtobufSerializer::Error::UnknownType; |
| 143 | QString lastErrorString; |
| 144 | |
| 145 | bool preserveUnknownFields = true; |
| 146 | |
| 147 | QProtobufSerializerImpl serializer; |
| 148 | QProtobufDeserializerImpl deserializer; |
| 149 | |
| 150 | private: |
| 151 | Q_DISABLE_COPY_MOVE(QProtobufSerializerPrivate) |
| 152 | }; |
| 153 | |
| 154 | QT_END_NAMESPACE |
| 155 | |
| 156 | #endif // QPROTOBUFSERIALIZER_P_H |
| 157 | |