diff options
author | william <william@25tandclement.com> | 2014-03-27 22:29:31 -0700 |
---|---|---|
committer | william <william@25tandclement.com> | 2014-03-27 22:29:31 -0700 |
commit | f61a39615ebffb61e8cca28d5dfafb6363da54ec (patch) | |
tree | 5f095744050c72227a1a28e362056b1d2c85e36a | |
parent | b2ca9062bc95ff7bc822a330f1d9f597ec95b770 (diff) | |
download | luaossl-f61a39615ebffb61e8cca28d5dfafb6363da54ec.tar.gz luaossl-f61a39615ebffb61e8cca28d5dfafb6363da54ec.tar.bz2 luaossl-f61a39615ebffb61e8cca28d5dfafb6363da54ec.zip |
support Lua 5.3 64-bit integer type in rand.uniform
-rw-r--r-- | src/openssl.c | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/src/openssl.c b/src/openssl.c index acd2454..7a03c97 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
@@ -4017,15 +4017,18 @@ static unsigned long long rand_llu(lua_State *L) { | |||
4017 | * (https://groups.google.com/forum/message/raw?msg=sci.crypt/DMslf6tSrD8/rv9rk6oP3r4J) | 4017 | * (https://groups.google.com/forum/message/raw?msg=sci.crypt/DMslf6tSrD8/rv9rk6oP3r4J) |
4018 | */ | 4018 | */ |
4019 | static int rand_uniform(lua_State *L) { | 4019 | static int rand_uniform(lua_State *L) { |
4020 | if (lua_isnoneornil(L, 1)) { | 4020 | unsigned long long r; |
4021 | unsigned long long r = rand_llu(L); | ||
4022 | |||
4023 | lua_pushnumber(L, r); | ||
4024 | 4021 | ||
4025 | return 1; | 4022 | if (lua_isnoneornil(L, 1)) { |
4023 | r = rand_llu(L); | ||
4026 | } else { | 4024 | } else { |
4027 | unsigned long long N = luaL_checknumber(L, 1); | 4025 | unsigned long long N, m; |
4028 | unsigned long long r, m; | 4026 | |
4027 | if (sizeof (lua_Unsigned) >= sizeof r) { | ||
4028 | N = luaL_checkunsigned(L, 1); | ||
4029 | } else { | ||
4030 | N = luaL_checknumber(L, 1); | ||
4031 | } | ||
4029 | 4032 | ||
4030 | luaL_argcheck(L, N > 1, 1, lua_pushfstring(L, "[0, %d): interval is empty", (int)N)); | 4033 | luaL_argcheck(L, N > 1, 1, lua_pushfstring(L, "[0, %d): interval is empty", (int)N)); |
4031 | 4034 | ||
@@ -4035,10 +4038,16 @@ static int rand_uniform(lua_State *L) { | |||
4035 | r = rand_llu(L); | 4038 | r = rand_llu(L); |
4036 | } while (r < m); | 4039 | } while (r < m); |
4037 | 4040 | ||
4038 | lua_pushnumber(L, (r % N)); | 4041 | r = r % N; |
4042 | } | ||
4039 | 4043 | ||
4040 | return 1; | 4044 | if (sizeof (lua_Unsigned) >= sizeof r) { |
4045 | lua_pushunsigned(L, r); | ||
4046 | } else { | ||
4047 | lua_pushnumber(L, r); | ||
4041 | } | 4048 | } |
4049 | |||
4050 | return 1; | ||
4042 | } /* rand_uniform() */ | 4051 | } /* rand_uniform() */ |
4043 | 4052 | ||
4044 | 4053 | ||