From 99f1280a37bce5361d68bcac9c6b174c9ac540cc Mon Sep 17 00:00:00 2001 From: marian Date: Mon, 7 Jul 2014 07:52:42 +0000 Subject: [PATCH] Added support for (un)hexify functions: btoh and htob. git-svn-id: http://yate.null.ro/svn/yate/trunk@5865 acf43c95-373e-0410-b603-e72c3f656dc1 --- modules/javascript.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/modules/javascript.cpp b/modules/javascript.cpp index 921e55f3..7868a945 100644 --- a/modules/javascript.cpp +++ b/modules/javascript.cpp @@ -246,6 +246,8 @@ public: params().addParam(new ExpFunction("btoa")); params().addParam(new ExpFunction("atoh")); params().addParam(new ExpFunction("htoa")); + params().addParam(new ExpFunction("btoh")); + params().addParam(new ExpFunction("htob")); } static void initialize(ScriptContext* context); inline void resetWorker() @@ -1091,6 +1093,39 @@ bool JsEngine::runNative(ObjList& stack, const ExpOperation& oper, GenObject* co else ExpEvaluator::pushOne(stack,new ExpOperation(false)); } + else if (oper.name() == YSTRING("btoh")) { + // hex_str = Engine.btoh(str[,sep[,upCase]]) + ObjList args; + ExpOperation* data = 0; + ExpOperation* sep = 0; + ExpOperation* upCase = 0; + if (!extractStackArgs(1,this,stack,oper,context,args,&data,&sep,&upCase)) + return false; + String tmp; + tmp.hexify((void*)data->c_str(),data->length(),(sep ? sep->at(0) : 0), + (upCase && upCase->toBoolean())); + ExpEvaluator::pushOne(stack,new ExpOperation(tmp,"hex")); + } + else if (oper.name() == YSTRING("htob")) { + // str = Engine.unHexify(hex_str[,sep]) + ObjList args; + ExpOperation* data = 0; + ExpOperation* sep = 0; + if (!extractStackArgs(1,this,stack,oper,context,args,&data,&sep)) + return false; + bool ok = true; + DataBlock buf; + if (!sep) + ok = buf.unHexify(data->c_str(),data->length()); + else + ok = buf.unHexify(data->c_str(),data->length(),sep->at(0)); + if (ok) { + String tmp((const char*)buf.data(),buf.length()); + ExpEvaluator::pushOne(stack,new ExpOperation(tmp,"bin")); + } + else + ExpEvaluator::pushOne(stack,new ExpOperation(false)); + } else return JsObject::runNative(stack,oper,context); return true;