Kaynağa Gözat

Expose axtls random numero generator

mingodad 8 yıl önce
ebeveyn
işleme
c866998352
1 değiştirilmiş dosya ile 38 ekleme ve 0 silme
  1. 38 0
      SquiLu-ext/sq_axtls.c

+ 38 - 0
SquiLu-ext/sq_axtls.c

@@ -344,6 +344,40 @@ static SQRESULT sq_axtls_sha256(HSQUIRRELVM v)
     sq_pushstring(v, buf, -1);
     return 1;
 }
+static SQRESULT sq_axtls_rng_initialize(HSQUIRRELVM v)
+{
+    RNG_initialize();
+    return 0;
+}
+
+static SQRESULT sq_axtls_rng_terminate(HSQUIRRELVM v)
+{
+    RNG_terminate();
+    return 0;
+}
+typedef int (*get_random_fptr_t)(int, uint8_t*);
+
+static SQRESULT sq_axtls_get_random0(HSQUIRRELVM v, get_random_fptr_t grf)
+{
+	SQ_FUNC_VARS_NO_TOP(v);
+    SQ_GET_INTEGER(v, 2, length);
+    if(length < 1) return sq_throwerror(v, _SC("Minimun length error %d"), length);
+    if(!RNG_is_initialized()) return sq_throwerror(v, _SC("Need to call rng_initialize first"));
+    SQChar *buff = sq_getscratchpad(v, length);
+    (*grf)(length, buff);
+	sq_pushstring(v, buff, length);
+    return 1;
+}
+
+static SQRESULT sq_axtls_get_random(HSQUIRRELVM v)
+{
+    return sq_axtls_get_random0(v, get_random);
+}
+
+static SQRESULT sq_axtls_get_random_nz(HSQUIRRELVM v)
+{
+    return sq_axtls_get_random0(v, get_random_NZ);
+}
 #define _DECL_AXTLS_FUNC(name,nparams,pmask) {_SC(#name),sq_axtls_##name,nparams,pmask}
 static SQRegFunction axtls_obj_funcs[]={
 	_DECL_AXTLS_FUNC(get_config,2,_SC(".i")),
@@ -353,6 +387,10 @@ static SQRegFunction axtls_obj_funcs[]={
 	_DECL_AXTLS_FUNC(md5,-2,_SC(".s")),
 	_DECL_AXTLS_FUNC(sha1,-2,_SC(".s")),
 	_DECL_AXTLS_FUNC(sha256,-2,_SC(".s")),
+	_DECL_AXTLS_FUNC(rng_initialize,1,_SC(".")),
+	_DECL_AXTLS_FUNC(rng_terminate,1,_SC(".")),
+	_DECL_AXTLS_FUNC(get_random,2,_SC(".i")),
+	_DECL_AXTLS_FUNC(get_random_nz,2,_SC(".i")),
 	{0,0}
 };
 #undef _DECL_AXTLS_FUNC