qprotobufserializer.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>, Viktor Kopp <vifactor@gmail.com>
  5. *
  6. * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "qprotobufserializer.h"
  26. #include "qprotobufserializer_p.h"
  27. #include "qprotobufmetaproperty.h"
  28. using namespace QtProtobuf;
  29. template<>
  30. QByteArray QProtobufSerializerPrivate::serializeListType<QByteArray>(const QByteArrayList &listValue, int &outFieldIndex)
  31. {
  32. qProtoDebug() << __func__ << "listValue.count" << listValue.count() << "outFiledIndex" << outFieldIndex;
  33. if (listValue.count() <= 0) {
  34. outFieldIndex = QtProtobufPrivate::NotUsedFieldIndex;
  35. return QByteArray();
  36. }
  37. QByteArray serializedList;
  38. for (auto &value : listValue) {
  39. serializedList.append(QProtobufSerializerPrivate::encodeHeader(outFieldIndex, LengthDelimited));
  40. serializedList.append(serializeLengthDelimited(value));
  41. }
  42. outFieldIndex = QtProtobufPrivate::NotUsedFieldIndex;
  43. return serializedList;
  44. }
  45. template<>
  46. void QProtobufSerializerPrivate::deserializeList<QByteArray>(QProtobufSelfcheckIterator &it, QVariant &previousValue)
  47. {
  48. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  49. QByteArrayList list = previousValue.value<QByteArrayList>();
  50. list.append(deserializeLengthDelimited(it));
  51. previousValue.setValue(list);
  52. }
  53. template<>
  54. void QProtobufSerializerPrivate::deserializeList<QString>(QProtobufSelfcheckIterator &it, QVariant &previousValue)
  55. {
  56. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  57. QStringList list = previousValue.value<QStringList>();
  58. QByteArray value = deserializeLengthDelimited(it);
  59. list.append(QString::fromUtf8(value));
  60. previousValue.setValue(list);
  61. }
  62. QProtobufSerializer::~QProtobufSerializer() = default;
  63. QProtobufSerializer::QProtobufSerializer() : d_ptr(new QProtobufSerializerPrivate(this))
  64. {
  65. QProtobufSerializerPrivate::wrapSerializer<float, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<float>, Fixed32>();
  66. QProtobufSerializerPrivate::wrapSerializer<double, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<double>, Fixed64>();
  67. QProtobufSerializerPrivate::wrapSerializer<int32, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<int32>, Varint>();
  68. QProtobufSerializerPrivate::wrapSerializer<int64, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<int64>, Varint>();
  69. QProtobufSerializerPrivate::wrapSerializer<uint32, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<uint32>, Varint>();
  70. QProtobufSerializerPrivate::wrapSerializer<uint64, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<uint64>, Varint>();
  71. QProtobufSerializerPrivate::wrapSerializer<sint32, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<sint32>, Varint>();
  72. QProtobufSerializerPrivate::wrapSerializer<sint64, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<sint64>, Varint>();
  73. QProtobufSerializerPrivate::wrapSerializer<fixed32, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<fixed32>, Fixed32>();
  74. QProtobufSerializerPrivate::wrapSerializer<fixed64, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<fixed64>, Fixed64>();
  75. QProtobufSerializerPrivate::wrapSerializer<sfixed32, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<sfixed32>, Fixed32>();
  76. QProtobufSerializerPrivate::wrapSerializer<sfixed64, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<sfixed64>, Fixed64>();
  77. QProtobufSerializerPrivate::wrapSerializer<bool, uint32, QProtobufSerializerPrivate::serializeBasic<uint32>, QProtobufSerializerPrivate::deserializeBasic<uint32>, Varint>();
  78. QProtobufSerializerPrivate::wrapSerializer<QString, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<QString>, LengthDelimited>();
  79. QProtobufSerializerPrivate::wrapSerializer<QByteArray, QProtobufSerializerPrivate::serializeBasic, QProtobufSerializerPrivate::deserializeBasic<QByteArray>, LengthDelimited>();
  80. QProtobufSerializerPrivate::wrapSerializer<FloatList, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<float>, LengthDelimited>();
  81. QProtobufSerializerPrivate::wrapSerializer<DoubleList, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<double>, LengthDelimited>();
  82. QProtobufSerializerPrivate::wrapSerializer<fixed32List, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<fixed32>, LengthDelimited>();
  83. QProtobufSerializerPrivate::wrapSerializer<fixed64List, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<fixed64>, LengthDelimited>();
  84. QProtobufSerializerPrivate::wrapSerializer<sfixed32List, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<sfixed32>, LengthDelimited>();
  85. QProtobufSerializerPrivate::wrapSerializer<sfixed64List, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<sfixed64>, LengthDelimited>();
  86. QProtobufSerializerPrivate::wrapSerializer<int32List, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<int32>, LengthDelimited>();
  87. QProtobufSerializerPrivate::wrapSerializer<int64List, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<int64>, LengthDelimited>();
  88. QProtobufSerializerPrivate::wrapSerializer<sint32List, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<sint32>, LengthDelimited>();
  89. QProtobufSerializerPrivate::wrapSerializer<sint64List, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<sint64>, LengthDelimited>();
  90. QProtobufSerializerPrivate::wrapSerializer<uint32List, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<uint32>, LengthDelimited>();
  91. QProtobufSerializerPrivate::wrapSerializer<uint64List, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<uint64>, LengthDelimited>();
  92. QProtobufSerializerPrivate::wrapSerializer<QStringList, QStringList, QProtobufSerializerPrivate::serializeListType<QString>, QProtobufSerializerPrivate::deserializeList<QString>, LengthDelimited>();
  93. QProtobufSerializerPrivate::wrapSerializer<QByteArrayList, QProtobufSerializerPrivate::serializeListType, QProtobufSerializerPrivate::deserializeList<QByteArray>, LengthDelimited>();
  94. }
  95. QByteArray QProtobufSerializer::serializeMessage(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const
  96. {
  97. QByteArray result;
  98. for (const auto &field : propertyOrdering) {
  99. int propertyIndex = field.second;
  100. int fieldIndex = field.first;
  101. Q_ASSERT_X(fieldIndex < 536870912 && fieldIndex > 0, "", "fieldIndex is out of range");
  102. QMetaProperty metaProperty = metaObject.property(propertyIndex);
  103. const char *propertyName = metaProperty.name();
  104. const QVariant &propertyValue = object->property(propertyName);
  105. result.append(d_ptr->serializeProperty(propertyValue, QProtobufMetaProperty(metaProperty, fieldIndex)));
  106. }
  107. return result;
  108. }
  109. void QProtobufSerializer::deserializeMessage(QObject *object, const QByteArray &data, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const
  110. {
  111. for (QProtobufSelfcheckIterator it(data); it != data.end();) {
  112. d_ptr->deserializeProperty(object, it, propertyOrdering, metaObject);
  113. }
  114. }
  115. QByteArray QProtobufSerializer::serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, const QProtobufMetaProperty &metaProperty) const
  116. {
  117. QByteArray result = QProtobufSerializerPrivate::encodeHeader(metaProperty.protoFieldIndex(), LengthDelimited);
  118. result.append(QProtobufSerializerPrivate::prependLengthDelimitedSize(serializeMessage(object, propertyOrdering, metaObject)));
  119. return result;
  120. }
  121. void QProtobufSerializer::deserializeObject(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const
  122. {
  123. QByteArray array = QProtobufSerializerPrivate::deserializeLengthDelimited(it);
  124. deserializeMessage(object, array, propertyOrdering, metaObject);
  125. }
  126. QByteArray QProtobufSerializer::serializeListObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, const QProtobufMetaProperty &metaProperty) const
  127. {
  128. return serializeObject(object, propertyOrdering, metaObject, metaProperty);
  129. }
  130. void QProtobufSerializer::deserializeListObject(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const
  131. {
  132. deserializeObject(object, it, propertyOrdering, metaObject);
  133. }
  134. QByteArray QProtobufSerializer::serializeMapPair(const QVariant &key, const QVariant &value, const QProtobufMetaProperty &metaProperty) const
  135. {
  136. QByteArray result = QProtobufSerializerPrivate::encodeHeader(metaProperty.protoFieldIndex(), LengthDelimited);
  137. result.append(QProtobufSerializerPrivate::prependLengthDelimitedSize(d_ptr->serializeProperty(key, QProtobufMetaProperty(metaProperty, 1)) + d_ptr->serializeProperty(value, QProtobufMetaProperty(metaProperty, 2))));
  138. return result;
  139. }
  140. void QProtobufSerializer::deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it) const
  141. {
  142. int mapIndex = 0;
  143. WireTypes type = WireTypes::UnknownWireType;
  144. unsigned int count = QProtobufSerializerPrivate::deserializeVarintCommon<uint32>(it);
  145. qProtoDebug() << __func__ << "count:" << count;
  146. QProtobufSelfcheckIterator last = it + count;
  147. while (it != last) {
  148. QProtobufSerializerPrivate::decodeHeader(it, mapIndex, type);
  149. if (mapIndex == 1) {
  150. //Only simple types are supported as keys
  151. int userType = key.userType();
  152. auto &handler = QProtobufSerializerPrivate::handlers.at(userType);//throws if not found
  153. handler.deserializer(it, key);
  154. } else {
  155. //TODO: replace with some common function
  156. int userType = value.userType();
  157. auto basicIt = QProtobufSerializerPrivate::handlers.find(userType);
  158. if (basicIt != QProtobufSerializerPrivate::handlers.end()) {
  159. basicIt->second.deserializer(it, value);
  160. } else {
  161. auto &handler = QtProtobufPrivate::findHandler(userType);
  162. handler.deserializer(this, it, value);//throws if not implemented
  163. }
  164. }
  165. }
  166. }
  167. QProtobufSerializerPrivate::QProtobufSerializerPrivate(QProtobufSerializer *q) : q_ptr(q)
  168. {
  169. }
  170. void QProtobufSerializerPrivate::skipVarint(QProtobufSelfcheckIterator &it)
  171. {
  172. while ((*it) & 0x80) {
  173. ++it;
  174. }
  175. ++it;
  176. }
  177. void QProtobufSerializerPrivate::skipLengthDelimited(QProtobufSelfcheckIterator &it)
  178. {
  179. //Get length of lenght-delimited field
  180. uint32 length = QProtobufSerializerPrivate::deserializeVarintCommon<uint32>(it);
  181. it += length;
  182. }
  183. int QProtobufSerializerPrivate::skipSerializedFieldBytes(QProtobufSelfcheckIterator &it, WireTypes type)
  184. {
  185. const auto initialIt = QByteArray::const_iterator(it);
  186. switch (type) {
  187. case WireTypes::Varint:
  188. skipVarint(it);
  189. break;
  190. case WireTypes::Fixed32:
  191. it += sizeof(decltype(fixed32::_t));
  192. break;
  193. case WireTypes::Fixed64:
  194. it += sizeof(decltype(fixed64::_t));
  195. break;
  196. case WireTypes::LengthDelimited:
  197. skipLengthDelimited(it);
  198. break;
  199. case WireTypes::UnknownWireType:
  200. default:
  201. throw std::invalid_argument("Cannot skip due to undefined length of the redundant field.");
  202. }
  203. return std::distance(initialIt, QByteArray::const_iterator(it));
  204. }
  205. QByteArray QProtobufSerializerPrivate::serializeProperty(const QVariant &propertyValue, const QProtobufMetaProperty &metaProperty)
  206. {
  207. qProtoDebug() << __func__ << "propertyValue" << propertyValue << "fieldIndex" << metaProperty.protoFieldIndex()
  208. << static_cast<QMetaType::Type>(propertyValue.type());
  209. QByteArray result;
  210. WireTypes type = UnknownWireType;
  211. int userType = propertyValue.userType();
  212. if (metaProperty.isEnumType()) {
  213. userType = qMetaTypeId<int64>();
  214. }
  215. //TODO: replace with some common function
  216. int fieldIndex = metaProperty.protoFieldIndex();
  217. auto basicIt = QProtobufSerializerPrivate::handlers.find(userType);
  218. if (basicIt != QProtobufSerializerPrivate::handlers.end()) {
  219. type = basicIt->second.type;
  220. result.append(basicIt->second.serializer(propertyValue, fieldIndex));
  221. if (fieldIndex != QtProtobufPrivate::NotUsedFieldIndex
  222. && type != UnknownWireType) {
  223. result.prepend(QProtobufSerializerPrivate::encodeHeader(metaProperty.protoFieldIndex(), type));
  224. }
  225. } else {
  226. auto &handler = QtProtobufPrivate::findHandler(userType);
  227. handler.serializer(q_ptr, propertyValue, QProtobufMetaProperty(metaProperty, metaProperty.protoFieldIndex()), result);
  228. }
  229. return result;
  230. }
  231. void QProtobufSerializerPrivate::deserializeProperty(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject)
  232. {
  233. //Each iteration we expect iterator is setup to beginning of next chunk
  234. int fieldNumber = QtProtobufPrivate::NotUsedFieldIndex;
  235. WireTypes wireType = UnknownWireType;
  236. if (!QProtobufSerializerPrivate::decodeHeader(it, fieldNumber, wireType)) {
  237. qProtoCritical() << "Message received doesn't contains valid header byte. "
  238. "Trying next, but seems stream is broken" << QString::number((*it), 16);
  239. throw std::invalid_argument("Message received doesn't contains valid header byte. "
  240. "Seems stream is broken");
  241. }
  242. auto propertyNumberIt = propertyOrdering.find(fieldNumber);
  243. if (propertyNumberIt == std::end(propertyOrdering)) {
  244. auto bytesCount = QProtobufSerializerPrivate::skipSerializedFieldBytes(it, wireType);
  245. qProtoWarning() << "Message received contains unexpected/optional field. WireType:" << wireType
  246. << ", field number: " << fieldNumber << "Skipped:" << (bytesCount + 1) << "bytes";
  247. return;
  248. }
  249. int propertyIndex = propertyNumberIt->second;
  250. QMetaProperty metaProperty = metaObject.property(propertyIndex);
  251. qProtoDebug() << __func__ << " wireType: " << wireType << " metaProperty: " << metaProperty.typeName()
  252. << "currentByte:" << QString::number((*it), 16);
  253. QVariant newPropertyValue;
  254. if (metaProperty.isEnumType()) {
  255. QProtobufSerializerPrivate::deserializeBasic<int64>(it, newPropertyValue);
  256. newPropertyValue.convert(qMetaTypeId<int32_t>());
  257. } else {
  258. newPropertyValue = metaProperty.read(object);
  259. int userType = metaProperty.userType();
  260. //TODO: replace with some common function
  261. auto basicIt = QProtobufSerializerPrivate::handlers.find(userType);
  262. if (basicIt != QProtobufSerializerPrivate::handlers.end()) {
  263. basicIt->second.deserializer(it, newPropertyValue);
  264. } else {
  265. auto &handler = QtProtobufPrivate::findHandler(userType);
  266. handler.deserializer(q_ptr, it, newPropertyValue);//throws if not implemented
  267. }
  268. }
  269. metaProperty.write(object, newPropertyValue);
  270. }
  271. QProtobufSerializerPrivate::SerializerRegistry QProtobufSerializerPrivate::handlers = {};