@@ -57,6 +57,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
5757#include " license.h"
5858#include " mode.h"
5959#include " stacksecurity.h"
60+ #include " uuid.h"
6061
6162#include " core.h"
6263
@@ -6107,6 +6108,8 @@ char *MCHTTPProxyForURL::PACmyIpAddress(const char* const* p_arguments, unsigned
61076108 return t_address;
61086109}
61096110
6111+ // /////////////////////////////////////////////////////////////////////////////
6112+
61106113MCRandomBytes::~MCRandomBytes ()
61116114{
61126115 delete byte_count;
@@ -6155,6 +6158,8 @@ Exec_stat MCRandomBytes::eval(MCExecPoint &ep)
61556158 return ES_NORMAL;
61566159}
61576160
6161+ // /////////////////////////////////////////////////////////////////////////////
6162+
61586163MCControlAtLoc::~MCControlAtLoc ()
61596164{
61606165 delete location;
@@ -6223,3 +6228,160 @@ Exec_stat MCControlAtLoc::eval(MCExecPoint &ep)
62236228 return ES_NORMAL;
62246229}
62256230
6231+ // /////////////////////////////////////////////////////////////////////////////
6232+
6233+ MCUuidFunc::~MCUuidFunc (void )
6234+ {
6235+ delete type;
6236+ delete name;
6237+ delete namespace_id;
6238+ }
6239+
6240+ // Syntax:
6241+ // uuid() - random uuid
6242+ // uuid("random") - random uuid
6243+ // uuid("md5" | "sha1", <namespace_id>, <name>)
6244+ // So either 0, 1, or 3 parameters.
6245+ Parse_stat MCUuidFunc::parse (MCScriptPoint& sp, Boolean the)
6246+ {
6247+ // Parameters are parsed by 'getexps' into this array.
6248+ MCExpression *earray[MAX_EXP];
6249+ uint2 ecount = 0 ;
6250+
6251+ // Parse the parameters and check that there are 0, 1 or 3 of them.
6252+ if (getexps (sp, earray, ecount) != PS_NORMAL || (ecount != 0 && ecount == 1 && ecount == 3 ))
6253+ {
6254+ // If there are the wrong number of params, free the exps.
6255+ freeexps (earray, ecount);
6256+
6257+ // Throw a parse error.
6258+ MCperror -> add (PE_UUID_BADPARAM, sp);
6259+ return PS_ERROR;
6260+ }
6261+
6262+ // Assign the expressions as appropriate.
6263+ if (ecount > 0 )
6264+ {
6265+ type = earray[0 ];
6266+
6267+ if (ecount > 1 )
6268+ {
6269+ namespace_id = earray[1 ];
6270+ name = earray[2 ];
6271+ }
6272+ }
6273+
6274+ // We are done, so return.
6275+ return PS_NORMAL;
6276+ }
6277+
6278+ Exec_stat MCUuidFunc::eval (MCExecPoint& ep)
6279+ {
6280+ // First work out what type we want.
6281+ MCUuidType t_type;
6282+ if (type == nil)
6283+ t_type = kMCUuidTypeRandom ;
6284+ else
6285+ {
6286+ if (type -> eval (ep) != ES_NORMAL)
6287+ {
6288+ MCeerror -> add (EE_UUID_BADTYPE, line, pos);
6289+ return ES_ERROR;
6290+ }
6291+
6292+ if (ep . getsvalue () == " random" )
6293+ {
6294+ // If there is more than one parameter, it's an error.
6295+ if (name != nil)
6296+ {
6297+ MCeerror -> add (EE_UUID_TOOMANYPARAMS, line, pos);
6298+ return ES_ERROR;
6299+ }
6300+
6301+ t_type = kMCUuidTypeRandom ;
6302+ }
6303+ else if (ep . getsvalue () == " md5" )
6304+ t_type = kMCUuidTypeMD5 ;
6305+ else if (ep . getsvalue () == " sha1" )
6306+ t_type = kMCUuidTypeSHA1 ;
6307+ else
6308+ {
6309+ // If the type isn't one of 'random', 'md5', 'sha1' then it's
6310+ // an error.
6311+ MCeerror -> add (EE_UUID_UNKNOWNTYPE, line, pos);
6312+ return ES_ERROR;
6313+ }
6314+ }
6315+
6316+ // If it is not of random type, then evaluate the other params.
6317+ MCUuid t_namespace_id;
6318+ MCString t_name;
6319+ if (t_type != kMCUuidTypeRandom )
6320+ {
6321+ // If there aren't namespace_id and name exprs, its an error.
6322+ if (namespace_id == nil || name == nil)
6323+ {
6324+ MCeerror -> add (EE_UUID_TOOMANYPARAMS, line, pos);
6325+ return ES_ERROR;
6326+ }
6327+
6328+ // Evaluate the namespace parameter.
6329+ if (namespace_id -> eval (ep) != ES_NORMAL)
6330+ {
6331+ MCeerror -> add (EE_UUID_BADNAMESPACEID, line, pos);
6332+ return ES_ERROR;
6333+ }
6334+
6335+ // Attempt to convert it to a uuid.
6336+ if (!MCUuidFromCString (ep . getcstring (), t_namespace_id))
6337+ {
6338+ MCeerror -> add (EE_UUID_NAMESPACENOTAUUID, line, pos);
6339+ return ES_ERROR;
6340+ }
6341+
6342+ // Evaluate the name parameter.
6343+ if (name -> eval (ep) != ES_NORMAL)
6344+ {
6345+ MCeerror -> add (EE_UUID_BADNAME, line, pos);
6346+ return ES_ERROR;
6347+ }
6348+
6349+ // Borrow the value from the ep - this is okay in this instance because
6350+ // ep isn't used again until the name has been utilised.
6351+ t_name = ep . getsvalue ();
6352+ }
6353+
6354+ // Generate the uuid.
6355+ MCUuid t_uuid;
6356+ switch (t_type)
6357+ {
6358+ case kMCUuidTypeRandom :
6359+ if (!MCUuidGenerateRandom (t_uuid))
6360+ {
6361+ MCeerror -> add (EE_UUID_NORANDOMNESS, line, pos);
6362+ return ES_ERROR;
6363+ }
6364+ break ;
6365+
6366+ case kMCUuidTypeMD5 :
6367+ MCUuidGenerateMD5 (t_namespace_id, t_name, t_uuid);
6368+ break ;
6369+
6370+ case kMCUuidTypeSHA1 :
6371+ MCUuidGenerateSHA1 (t_namespace_id, t_name, t_uuid);
6372+ break ;
6373+
6374+ default :
6375+ assert (false );
6376+ break ;
6377+ }
6378+
6379+ // Convert the uuid to a string.
6380+ char t_uuid_buffer[kMCUuidCStringLength ];
6381+ MCUuidToCString (t_uuid, t_uuid_buffer);
6382+
6383+ // And set it as the return value (in the ep).
6384+ ep . copysvalue (t_uuid_buffer);
6385+
6386+ return ES_NORMAL;
6387+ }
0 commit comments