@@ -31,7 +31,7 @@ void CLuaCryptDefs::LoadFunctions()
31
31
{" generateKeyPair" , ArgumentParser<GenerateKeyPair>},
32
32
{" passwordVerify" , PasswordVerify},
33
33
{" encodeString" , EncodeString},
34
- {" decodeString" , DecodeString},
34
+ {" decodeString" , DecodeString}
35
35
};
36
36
37
37
// Add functions
@@ -402,7 +402,11 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
402
402
CScriptArgReader argStream (luaVM);
403
403
argStream.ReadEnumString (algorithm);
404
404
argStream.ReadString (data);
405
- argStream.ReadStringMap (options);
405
+
406
+ if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsTable ())
407
+ {
408
+ argStream.ReadStringMap (options);
409
+ }
406
410
407
411
argStream.ReadFunction (luaFunctionRef, LUA_REFNIL);
408
412
argStream.ReadFunctionComplete ();
@@ -589,6 +593,140 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
589
593
}
590
594
return 1 ;
591
595
}
596
+ case StringEncodeFunction::BASE64:
597
+ {
598
+ const SString variant = options[" variant" ].ToUpper ();
599
+
600
+ if (!variant.empty () && variant != " URL" )
601
+ {
602
+ m_pScriptDebugging->LogCustom (luaVM, " Invalid value for field 'variant'" );
603
+ lua::Push (luaVM, false );
604
+ return 1 ;
605
+ }
606
+
607
+ // Async
608
+ if (VERIFY_FUNCTION (luaFunctionRef))
609
+ {
610
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaVM);
611
+ if (pLuaMain)
612
+ {
613
+ CLuaShared::GetAsyncTaskScheduler ()->PushTask (
614
+ [data, variant]
615
+ {
616
+ try
617
+ {
618
+ return std::make_pair (SharedUtil::Base64encode (data, variant), true );
619
+ }
620
+ catch (const CryptoPP::Exception& ex)
621
+ {
622
+ return std::make_pair (SString (ex.GetWhat ()), false );
623
+ }
624
+ },
625
+ [luaFunctionRef](const std::pair<SString, bool >& result)
626
+ {
627
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaFunctionRef.GetLuaVM ());
628
+ if (pLuaMain)
629
+ {
630
+ CLuaArguments arguments;
631
+ if (result.second )
632
+ {
633
+ arguments.PushString (result.first );
634
+ arguments.Call (pLuaMain, luaFunctionRef);
635
+ }
636
+ else
637
+ {
638
+ m_pScriptDebugging->LogWarning (luaFunctionRef.GetLuaVM (), result.first .c_str ());
639
+ arguments.PushBoolean (false );
640
+ arguments.Call (pLuaMain, luaFunctionRef);
641
+ }
642
+ }
643
+ });
644
+
645
+ lua::Push (luaVM, true );
646
+ }
647
+ }
648
+ else // Sync
649
+ {
650
+ try
651
+ {
652
+ lua::Push (luaVM, SharedUtil::Base64encode (data, variant));
653
+ }
654
+ catch (const CryptoPP::Exception& ex)
655
+ {
656
+ m_pScriptDebugging->LogWarning (luaVM, ex.what ());
657
+ lua::Push (luaVM, false );
658
+ }
659
+ return 1 ;
660
+ }
661
+ return 1 ;
662
+ }
663
+ case StringEncodeFunction::BASE32:
664
+ {
665
+ const SString variant = options[" variant" ].ToUpper ();
666
+
667
+ if (!variant.empty () && variant != " HEX" )
668
+ {
669
+ m_pScriptDebugging->LogCustom (luaVM, " Invalid value for field 'variant'" );
670
+ lua::Push (luaVM, false );
671
+ return 1 ;
672
+ }
673
+
674
+ // Async
675
+ if (VERIFY_FUNCTION (luaFunctionRef))
676
+ {
677
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaVM);
678
+ if (pLuaMain)
679
+ {
680
+ CLuaShared::GetAsyncTaskScheduler ()->PushTask (
681
+ [data, variant]
682
+ {
683
+ try
684
+ {
685
+ return std::make_pair (SharedUtil::Base32encode (data, variant), true );
686
+ }
687
+ catch (const CryptoPP::Exception& ex)
688
+ {
689
+ return std::make_pair (SString (ex.GetWhat ()), false );
690
+ }
691
+ },
692
+ [luaFunctionRef](const std::pair<SString, bool >& result)
693
+ {
694
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaFunctionRef.GetLuaVM ());
695
+ if (pLuaMain)
696
+ {
697
+ CLuaArguments arguments;
698
+ if (result.second )
699
+ {
700
+ arguments.PushString (result.first );
701
+ arguments.Call (pLuaMain, luaFunctionRef);
702
+ }
703
+ else
704
+ {
705
+ m_pScriptDebugging->LogWarning (luaFunctionRef.GetLuaVM (), result.first .c_str ());
706
+ arguments.PushBoolean (false );
707
+ arguments.Call (pLuaMain, luaFunctionRef);
708
+ }
709
+ }
710
+ });
711
+
712
+ lua::Push (luaVM, true );
713
+ }
714
+ }
715
+ else // Sync
716
+ {
717
+ try
718
+ {
719
+ lua::Push (luaVM, SharedUtil::Base32encode (data, variant));
720
+ }
721
+ catch (const CryptoPP::Exception& ex)
722
+ {
723
+ m_pScriptDebugging->LogWarning (luaVM, ex.what ());
724
+ lua::Push (luaVM, false );
725
+ }
726
+ return 1 ;
727
+ }
728
+ return 1 ;
729
+ }
592
730
default :
593
731
{
594
732
m_pScriptDebugging->LogCustom (luaVM, " Unknown encryption algorithm" );
@@ -614,7 +752,11 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
614
752
CScriptArgReader argStream (luaVM);
615
753
argStream.ReadEnumString (algorithm);
616
754
argStream.ReadString (data);
617
- argStream.ReadStringMap (options);
755
+
756
+ if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsTable ())
757
+ {
758
+ argStream.ReadStringMap (options);
759
+ }
618
760
619
761
argStream.ReadFunction (luaFunctionRef, LUA_REFNIL);
620
762
argStream.ReadFunctionComplete ();
@@ -808,6 +950,140 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
808
950
}
809
951
return 1 ;
810
952
}
953
+ case StringEncodeFunction::BASE64:
954
+ {
955
+ const SString variant = options[" variant" ].ToUpper ();
956
+
957
+ if (!variant.empty () && variant != " URL" )
958
+ {
959
+ m_pScriptDebugging->LogCustom (luaVM, " Invalid value for field 'variant'" );
960
+ lua::Push (luaVM, false );
961
+ return 1 ;
962
+ }
963
+
964
+ // Async
965
+ if (VERIFY_FUNCTION (luaFunctionRef))
966
+ {
967
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaVM);
968
+ if (pLuaMain)
969
+ {
970
+ CLuaShared::GetAsyncTaskScheduler ()->PushTask (
971
+ [data, variant]
972
+ {
973
+ try
974
+ {
975
+ return std::make_pair (SharedUtil::Base64decode (data, variant), true );
976
+ }
977
+ catch (const CryptoPP::Exception& ex)
978
+ {
979
+ return std::make_pair (SString (ex.GetWhat ()), false );
980
+ }
981
+ },
982
+ [luaFunctionRef](const std::pair<SString, bool >& result)
983
+ {
984
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaFunctionRef.GetLuaVM ());
985
+ if (pLuaMain)
986
+ {
987
+ CLuaArguments arguments;
988
+ if (result.second )
989
+ {
990
+ arguments.PushString (result.first );
991
+ arguments.Call (pLuaMain, luaFunctionRef);
992
+ }
993
+ else
994
+ {
995
+ m_pScriptDebugging->LogWarning (luaFunctionRef.GetLuaVM (), result.first .c_str ());
996
+ arguments.PushBoolean (false );
997
+ arguments.Call (pLuaMain, luaFunctionRef);
998
+ }
999
+ }
1000
+ });
1001
+
1002
+ lua::Push (luaVM, true );
1003
+ }
1004
+ }
1005
+ else // Sync
1006
+ {
1007
+ try
1008
+ {
1009
+ lua::Push (luaVM, SharedUtil::Base64decode (data, variant));
1010
+ }
1011
+ catch (const CryptoPP::Exception& ex)
1012
+ {
1013
+ m_pScriptDebugging->LogWarning (luaVM, ex.what ());
1014
+ lua::Push (luaVM, false );
1015
+ }
1016
+ return 1 ;
1017
+ }
1018
+ return 1 ;
1019
+ }
1020
+ case StringEncodeFunction::BASE32:
1021
+ {
1022
+ const SString variant = options[" variant" ].ToUpper ();
1023
+
1024
+ if (!variant.empty () && variant != " HEX" )
1025
+ {
1026
+ m_pScriptDebugging->LogCustom (luaVM, " Invalid value for field 'variant'" );
1027
+ lua::Push (luaVM, false );
1028
+ return 1 ;
1029
+ }
1030
+
1031
+ // Async
1032
+ if (VERIFY_FUNCTION (luaFunctionRef))
1033
+ {
1034
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaVM);
1035
+ if (pLuaMain)
1036
+ {
1037
+ CLuaShared::GetAsyncTaskScheduler ()->PushTask (
1038
+ [data, variant]
1039
+ {
1040
+ try
1041
+ {
1042
+ return std::make_pair (SharedUtil::Base32decode (data, variant), true );
1043
+ }
1044
+ catch (const CryptoPP::Exception& ex)
1045
+ {
1046
+ return std::make_pair (SString (ex.GetWhat ()), false );
1047
+ }
1048
+ },
1049
+ [luaFunctionRef](const std::pair<SString, bool >& result)
1050
+ {
1051
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaFunctionRef.GetLuaVM ());
1052
+ if (pLuaMain)
1053
+ {
1054
+ CLuaArguments arguments;
1055
+ if (result.second )
1056
+ {
1057
+ arguments.PushString (result.first );
1058
+ arguments.Call (pLuaMain, luaFunctionRef);
1059
+ }
1060
+ else
1061
+ {
1062
+ m_pScriptDebugging->LogWarning (luaFunctionRef.GetLuaVM (), result.first .c_str ());
1063
+ arguments.PushBoolean (false );
1064
+ arguments.Call (pLuaMain, luaFunctionRef);
1065
+ }
1066
+ }
1067
+ });
1068
+
1069
+ lua::Push (luaVM, true );
1070
+ }
1071
+ }
1072
+ else // Sync
1073
+ {
1074
+ try
1075
+ {
1076
+ lua::Push (luaVM, SharedUtil::Base32decode (data, variant));
1077
+ }
1078
+ catch (const CryptoPP::Exception& ex)
1079
+ {
1080
+ m_pScriptDebugging->LogWarning (luaVM, ex.what ());
1081
+ lua::Push (luaVM, false );
1082
+ }
1083
+ return 1 ;
1084
+ }
1085
+ return 1 ;
1086
+ }
811
1087
default :
812
1088
{
813
1089
m_pScriptDebugging->LogCustom (luaVM, " Unknown encryption algorithm" );
0 commit comments