Skip to content

Commit

Permalink
[feature][expression] base64 to and from
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCaha authored and nyalldawson committed May 20, 2020
1 parent c6773a4 commit 357bc1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -5624,6 +5624,25 @@ static QVariant fcnHashSha256( const QVariantList &values, const QgsExpressionCo
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha256 );
}

static QVariant fcnToBase64( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QString value = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );

const QByteArray input = value.toLocal8Bit();

return QVariant( QString( input.toBase64() ) );
}

static QVariant fcnFromBase64( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QString value = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );

const QByteArray base64 = value.toLocal8Bit();
const QByteArray decoded = QByteArray::fromBase64( base64 );

return QVariant( QString( decoded ) );
}

const QList<QgsExpressionFunction *> &QgsExpression::Functions()
{
// The construction of the list isn't thread-safe, and without the mutex,
Expand Down Expand Up @@ -5953,6 +5972,12 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "sha256" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
fcnHashSha256, QStringLiteral( "Conversions" ) )

//base64
<< new QgsStaticExpressionFunction( QStringLiteral( "to_base64" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
fcnToBase64, QStringLiteral( "Conversions" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "from_base64" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
fcnFromBase64, QStringLiteral( "Conversions" ) )

// deprecated stuff - hidden from users
<< new QgsStaticExpressionFunction( QStringLiteral( "$scale" ), QgsExpressionFunction::ParameterList(), fcnMapScale, QStringLiteral( "deprecated" ) );

Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -1641,6 +1641,10 @@ class TestQgsExpression: public QObject
QTest::newRow( "hash('QGIS', 'keccak_384')" ) << QStringLiteral( "hash('QGIS', 'keccak_384')" ) << false << QVariant( "c57a3aed9d856fa04e5eeee9b62b6e027cca81ba574116d3cc1f0d48a1ef9e5886ff463ea8d0fac772ee473bf92f810d" );
QTest::newRow( "hash('QGIS', 'keccak_512')" ) << QStringLiteral( "hash('QGIS', 'keccak_512')" ) << false << QVariant( "6f0f751776b505e317de222508fa5d3ed7099d8f07c74fed54ccee6e7cdc6b89b4a085e309f2ee5210c942bbeb142bdfe48f84f912e0f3f41bdbf47110c2d344" );
#endif
QTest::newRow( "to_base64 NULL" ) << QStringLiteral( "to_base64(NULL)" ) << false << QVariant();
QTest::newRow( "to_base64" ) << QStringLiteral( "to_base64('QGIS')" ) << false << QVariant( "UUdJUw==" );
QTest::newRow( "from_base64 NULL" ) << QStringLiteral( "from_base64(NULL)" ) << false << QVariant();
QTest::newRow( "from_base64" ) << QStringLiteral( "from_base64('UUdJUw==')" ) << false << QVariant( "QGIS" );
}


Expand Down

0 comments on commit 357bc1d

Please sign in to comment.