@@ -403,7 +403,8 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
403
403
argStream.ReadEnumString (algorithm);
404
404
argStream.ReadString (data);
405
405
406
- if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsTable ())
406
+ if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32 && algorithm != StringEncodeFunction::ZLIB) ||
407
+ argStream.NextIsTable ())
407
408
{
408
409
argStream.ReadStringMap (options);
409
410
}
@@ -727,6 +728,88 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
727
728
}
728
729
return 1 ;
729
730
}
731
+ case StringEncodeFunction::ZLIB:
732
+ {
733
+ int compression = 9 ;
734
+ int format = (int )ZLibFormat::GZIP;
735
+ ZLibStrategy strategy = ZLibStrategy::DEFAULT;
736
+ if (!options[" format" ].empty () && !StringToEnum (options[" format" ], (ZLibFormat&)format) && !StringToZLibFormat (options[" format" ], format))
737
+ {
738
+ m_pScriptDebugging->LogCustom (luaVM, " Invalid value for field 'format'" );
739
+ lua::Push (luaVM, false );
740
+ return 1 ;
741
+ }
742
+ if (!options[" strategy" ].empty () && !StringToEnum (options[" strategy" ], strategy))
743
+ {
744
+ m_pScriptDebugging->LogCustom (luaVM, " Invalid value for field 'strategy'" );
745
+ lua::Push (luaVM, false );
746
+ return 1 ;
747
+ }
748
+ if (!options[" compression" ].empty ())
749
+ {
750
+ compression = atoi (options[" compression" ].c_str ());
751
+ if (compression < 0 || compression > 9 )
752
+ {
753
+ m_pScriptDebugging->LogCustom (luaVM, " Value for field 'compression' is out of range (0-9)" );
754
+ lua::Push (luaVM, false );
755
+ return 1 ;
756
+ }
757
+ }
758
+
759
+ // Async
760
+ if (VERIFY_FUNCTION (luaFunctionRef))
761
+ {
762
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaVM);
763
+ if (pLuaMain)
764
+ {
765
+ CLuaShared::GetAsyncTaskScheduler ()->PushTask (
766
+ [data, format, compression, strategy]
767
+ {
768
+ // Execute time-consuming task
769
+ SString output;
770
+ int result = SharedUtil::ZLibCompress (data, output, format, compression, strategy);
771
+ if (result == Z_STREAM_END)
772
+ return std::make_pair (output, true );
773
+ else
774
+ return std::make_pair (SString (" zlib error: %i" , result), false );
775
+ },
776
+ [luaFunctionRef](const std::pair<SString, bool >& result)
777
+ {
778
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaFunctionRef.GetLuaVM ());
779
+ if (pLuaMain)
780
+ {
781
+ CLuaArguments arguments;
782
+ if (result.second )
783
+ {
784
+ arguments.PushString (result.first );
785
+ arguments.Call (pLuaMain, luaFunctionRef);
786
+ }
787
+ else
788
+ {
789
+ m_pScriptDebugging->LogWarning (luaFunctionRef.GetLuaVM (), result.first .c_str ());
790
+ arguments.PushBoolean (false );
791
+ arguments.Call (pLuaMain, luaFunctionRef);
792
+ }
793
+ }
794
+ });
795
+
796
+ lua_pushboolean (luaVM, true );
797
+ }
798
+ }
799
+ else // Sync
800
+ {
801
+ SString output;
802
+ int result = SharedUtil::ZLibCompress (data, output, format, compression, strategy);
803
+ if (result == Z_STREAM_END)
804
+ lua::Push (luaVM, output);
805
+ else
806
+ {
807
+ m_pScriptDebugging->LogWarning (luaVM, " zlib error: %i" , result);
808
+ lua::Push (luaVM, false );
809
+ }
810
+ }
811
+ return 1 ;
812
+ }
730
813
default :
731
814
{
732
815
m_pScriptDebugging->LogCustom (luaVM, " Unknown encryption algorithm" );
@@ -753,7 +836,8 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
753
836
argStream.ReadEnumString (algorithm);
754
837
argStream.ReadString (data);
755
838
756
- if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsTable ())
839
+ if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32 && algorithm != StringEncodeFunction::ZLIB) ||
840
+ argStream.NextIsTable ())
757
841
{
758
842
argStream.ReadStringMap (options);
759
843
}
@@ -1084,6 +1168,70 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
1084
1168
}
1085
1169
return 1 ;
1086
1170
}
1171
+ case StringEncodeFunction::ZLIB:
1172
+ {
1173
+ int format = 0 ;
1174
+ if (!options[" format" ].empty () && !StringToEnum (options[" format" ], (ZLibFormat&)format) && !StringToZLibFormat (options[" format" ], format))
1175
+ {
1176
+ m_pScriptDebugging->LogCustom (luaVM, " Not supported value for field 'format'" );
1177
+ lua::Push (luaVM, false );
1178
+ return 1 ;
1179
+ }
1180
+
1181
+ // Async
1182
+ if (VERIFY_FUNCTION (luaFunctionRef))
1183
+ {
1184
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaVM);
1185
+ if (pLuaMain)
1186
+ {
1187
+ CLuaShared::GetAsyncTaskScheduler ()->PushTask (
1188
+ [data, format]
1189
+ {
1190
+ // Execute time-consuming task
1191
+ SString output;
1192
+ int result = SharedUtil::ZLibUncompress (data, output, format);
1193
+ if (result == Z_STREAM_END)
1194
+ return std::make_pair (output, true );
1195
+ else
1196
+ return std::make_pair (SString (" zlib error: %i" , result), false );
1197
+ },
1198
+ [luaFunctionRef](const std::pair<SString, bool >& result)
1199
+ {
1200
+ CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine (luaFunctionRef.GetLuaVM ());
1201
+ if (pLuaMain)
1202
+ {
1203
+ CLuaArguments arguments;
1204
+ if (result.second )
1205
+ {
1206
+ arguments.PushString (result.first );
1207
+ arguments.Call (pLuaMain, luaFunctionRef);
1208
+ }
1209
+ else
1210
+ {
1211
+ m_pScriptDebugging->LogWarning (luaFunctionRef.GetLuaVM (), result.first .c_str ());
1212
+ arguments.PushBoolean (false );
1213
+ arguments.Call (pLuaMain, luaFunctionRef);
1214
+ }
1215
+ }
1216
+ });
1217
+
1218
+ lua_pushboolean (luaVM, true );
1219
+ }
1220
+ }
1221
+ else // Sync
1222
+ {
1223
+ SString output;
1224
+ int result = SharedUtil::ZLibUncompress (data, output, format);
1225
+ if (result == Z_STREAM_END)
1226
+ lua::Push (luaVM, output);
1227
+ else
1228
+ {
1229
+ m_pScriptDebugging->LogWarning (luaVM, " zlib error: %i" , result);
1230
+ lua::Push (luaVM, false );
1231
+ }
1232
+ }
1233
+ return 1 ;
1234
+ }
1087
1235
default :
1088
1236
{
1089
1237
m_pScriptDebugging->LogCustom (luaVM, " Unknown encryption algorithm" );
0 commit comments