bytearrayconverter.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "bytearrayconverter_p.h"
  2. #include "exception.h"
  3. #include "jsonserializer.h"
  4. #include <QtCore/QByteArray>
  5. #include <QtCore/QRegularExpression>
  6. using namespace QtJsonSerializer;
  7. using namespace QtJsonSerializer::TypeConverters;
  8. bool BytearrayConverter::canConvert(int metaTypeId) const
  9. {
  10. return metaTypeId == QMetaType::QByteArray;
  11. }
  12. QList<QCborTag> BytearrayConverter::allowedCborTags(int metaTypeId) const
  13. {
  14. Q_UNUSED(metaTypeId)
  15. return {
  16. NoTag,
  17. static_cast<QCborTag>(QCborKnownTags::ExpectedBase64),
  18. static_cast<QCborTag>(QCborKnownTags::ExpectedBase64url),
  19. static_cast<QCborTag>(QCborKnownTags::ExpectedBase16),
  20. };
  21. }
  22. QList<QCborValue::Type> BytearrayConverter::allowedCborTypes(int metaTypeId, QCborTag tag) const
  23. {
  24. Q_UNUSED(metaTypeId)
  25. Q_UNUSED(tag)
  26. return {QCborValue::ByteArray};
  27. }
  28. int BytearrayConverter::guessType(QCborTag tag, QCborValue::Type dataType) const
  29. {
  30. Q_UNUSED(dataType)
  31. switch (tag) {
  32. case static_cast<QCborTag>(QCborKnownTags::ExpectedBase64):
  33. case static_cast<QCborTag>(QCborKnownTags::ExpectedBase64url):
  34. case static_cast<QCborTag>(QCborKnownTags::ExpectedBase16):
  35. return QMetaType::QByteArray;
  36. default:
  37. return QMetaType::UnknownType;
  38. }
  39. }
  40. QCborValue BytearrayConverter::serialize(int propertyType, const QVariant &value) const
  41. {
  42. Q_UNUSED(propertyType)
  43. return value.toByteArray();
  44. }
  45. QVariant BytearrayConverter::deserializeCbor(int propertyType, const QCborValue &value, QObject *parent) const
  46. {
  47. Q_UNUSED(propertyType)
  48. Q_UNUSED(parent)
  49. return (value.isTag() ? value.taggedValue() : value).toByteArray();
  50. }
  51. QVariant BytearrayConverter::deserializeJson(int propertyType, const QCborValue &value, QObject *parent) const
  52. {
  53. Q_UNUSED(propertyType)
  54. Q_UNUSED(parent)
  55. const auto mode = helper()->getProperty("byteArrayFormat").value<JsonSerializer::ByteArrayFormat>();
  56. const auto strValue = value.toString();
  57. if (helper()->getProperty("validateBase64").toBool()) {
  58. switch (mode) {
  59. case JsonSerializer::ByteArrayFormat::Base64: {
  60. if ((strValue.size() % 4) != 0)
  61. throw DeserializationException("String has invalid length for base64 encoding");
  62. static const QRegularExpression regex(QStringLiteral(R"__(^[a-zA-Z0-9+\/]*(={0,2})$)__"));
  63. if (!regex.match(strValue).hasMatch())
  64. throw DeserializationException("String contains unallowed symbols for base64 encoding");
  65. break;
  66. }
  67. case JsonSerializer::ByteArrayFormat::Base64url: {
  68. static const QRegularExpression regex(QStringLiteral(R"__(^[a-zA-Z0-9\-_]*$)__"));
  69. if (!regex.match(strValue).hasMatch())
  70. throw DeserializationException("String contains unallowed symbols for base64url encoding");
  71. break;
  72. }
  73. case JsonSerializer::ByteArrayFormat::Base16: {
  74. if ((strValue.size() % 2) != 0)
  75. throw DeserializationException("String has invalid length for base16 encoding");
  76. static const QRegularExpression regex(QStringLiteral(R"__(^[a-fA-F0-9]*$)__"));
  77. if (!regex.match(strValue).hasMatch())
  78. throw DeserializationException("String contains unallowed symbols for base16 encoding");
  79. break;
  80. }
  81. default:
  82. Q_UNREACHABLE();
  83. }
  84. }
  85. switch (mode) {
  86. case JsonSerializer::ByteArrayFormat::Base64:
  87. return QByteArray::fromBase64(strValue.toUtf8(), QByteArray::Base64Encoding);
  88. case JsonSerializer::ByteArrayFormat::Base64url:
  89. return QByteArray::fromBase64(strValue.toUtf8(), QByteArray::Base64UrlEncoding);
  90. case JsonSerializer::ByteArrayFormat::Base16:
  91. return QByteArray::fromHex(strValue.toUtf8());
  92. default:
  93. Q_UNREACHABLE();
  94. }
  95. }