Game Development Community

T3D 1.2 - Strange Returns

by Robert Fritzen · in Torque 3D Professional · 12/01/2011 (4:54 pm) · 2 replies

Well, a little bit before I get here.

I had informed everyone of my xxz re-release, and in doing so, I was planning on patching up a few interesting bugs in the Torque side of things, but so far, no go on that, so I have not been able to hit up the release on the upgraded system.

I now turn to the community of C++ programmers here for some help.

So a little bit on my problem:

I have in hand my cryptography library filled with the necessary tools, however, when I try to invoke calls to certain functions, this issue becomes apparent.

For Example:
DefineEngineFunction(AESEncrypt, const char *, (const char *input, const char *key),, "(string) [input, key] AES Encrypt") {
   std::string out;
   cryptoPackage->AESEncrypt(key, input, out, 1024);

   return out.c_str();
}

Does not work (IE: puts nothing but ASCII in the data), but If I switch it to this:
DefineEngineFunction(AESEncrypt, void, (const char *input, const char *key),, "(string) [input, key] AES Encrypt") {
   std::string out;
   cryptoPackage->AESEncrypt(key, input, out, 1024);

   Con::setVariable("$AESENCRYPT", out.c_str());
}
It works fine.

While this is fine and all, it's not very optimal to be defining random variables only to clear them in script a few seconds later.

This is extremely prominent when I try to invoke decryption functions. Is this a common issue with everyone else here? if so, are there any known fixes for this? I would definitely love to get this one off the belt and into your hands.

#1
12/01/2011 (9:51 pm)
Your first function fails is because your std::string out variable is destroyed upon leaving the function scope therefore the char pointer you just returned is now invalid.

Try this one instead:
DefineEngineFunction(AESEncrypt, const char *, (const char *input, const char *key),, "(string) [input, key] AES Encrypt") {
   std::string out;
   cryptoPackage->AESEncrypt(key, input, out, 1024);

   char *str = Con::getReturnBuffer(out.size() +1);
   dStrcpy(str, out.c_str());

   return str;
}

Edit: Fixed missing closing brace within dStrcpy().
#2
12/02/2011 (9:37 am)
Looks like I need to brush up on my C++ when it comes to variable creation and destruction...

Thanks, works fine now.