diff options
Diffstat (limited to '')
-rw-r--r-- | lauxlib.c | 25 | ||||
-rw-r--r-- | ltests.c | 7 |
2 files changed, 20 insertions, 12 deletions
@@ -1124,29 +1124,30 @@ static void warnfon (void *ud, const char *message, int tocont) { | |||
1124 | #include <time.h> | 1124 | #include <time.h> |
1125 | 1125 | ||
1126 | 1126 | ||
1127 | /* | 1127 | /* Size for the buffer, in bytes */ |
1128 | ** Size of 'e' measured in number of 'unsigned int's. (In the weird | 1128 | #define BUFSEEDB (sizeof(void*) + sizeof(time_t)) |
1129 | ** case that the division truncates, we just lose some part of the | 1129 | |
1130 | ** value, no big deal.) | 1130 | /* Size for the buffer in int's, rounded up */ |
1131 | */ | 1131 | #define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int)) |
1132 | #define sof(e) (sizeof(e) / sizeof(unsigned int)) | ||
1133 | 1132 | ||
1134 | 1133 | ||
1135 | #define addbuff(b,v) \ | 1134 | #define addbuff(b,v) (memcpy(b, &(v), sizeof(v)), b += sizeof(v)) |
1136 | (memcpy(b, &(v), sof(v) * sizeof(unsigned int)), b += sof(v)) | ||
1137 | 1135 | ||
1138 | 1136 | ||
1139 | static unsigned int luai_makeseed (void) { | 1137 | static unsigned int luai_makeseed (void) { |
1140 | unsigned int buff[sof(void*) + sof(time_t)]; | 1138 | unsigned int buff[BUFSEED]; |
1141 | unsigned int res; | 1139 | unsigned int res; |
1142 | unsigned int *b = buff; | 1140 | unsigned int i; |
1143 | time_t t = time(NULL); | 1141 | time_t t = time(NULL); |
1144 | void *h = buff; | 1142 | void *h = buff; |
1143 | char *b = (char*)buff; | ||
1145 | addbuff(b, h); /* local variable's address */ | 1144 | addbuff(b, h); /* local variable's address */ |
1146 | addbuff(b, t); /* time */ | 1145 | addbuff(b, t); /* time */ |
1146 | /* fill (rare but possible) remain of the buffer with zeros */ | ||
1147 | memset(b, 0, BUFSEED * sizeof(int) - BUFSEEDB); | ||
1147 | res = buff[0]; | 1148 | res = buff[0]; |
1148 | for (b = buff + 1; b < buff + sof(buff); b++) | 1149 | for (i = 0; i < BUFSEED; i++) |
1149 | res ^= (res >> 3) + (res << 7) + *b; | 1150 | res ^= (res >> 3) + (res << 7) + buff[i]; |
1150 | return res; | 1151 | return res; |
1151 | } | 1152 | } |
1152 | 1153 | ||
@@ -1160,6 +1160,12 @@ static int num2int (lua_State *L) { | |||
1160 | } | 1160 | } |
1161 | 1161 | ||
1162 | 1162 | ||
1163 | static int makeseed (lua_State *L) { | ||
1164 | lua_pushinteger(L, luaL_makeseed(L)); | ||
1165 | return 1; | ||
1166 | } | ||
1167 | |||
1168 | |||
1163 | static int newstate (lua_State *L) { | 1169 | static int newstate (lua_State *L) { |
1164 | void *ud; | 1170 | void *ud; |
1165 | lua_Alloc f = lua_getallocf(L, &ud); | 1171 | lua_Alloc f = lua_getallocf(L, &ud); |
@@ -1962,6 +1968,7 @@ static const struct luaL_Reg tests_funcs[] = { | |||
1962 | {"newstate", newstate}, | 1968 | {"newstate", newstate}, |
1963 | {"newuserdata", newuserdata}, | 1969 | {"newuserdata", newuserdata}, |
1964 | {"num2int", num2int}, | 1970 | {"num2int", num2int}, |
1971 | {"makeseed", makeseed}, | ||
1965 | {"pushuserdata", pushuserdata}, | 1972 | {"pushuserdata", pushuserdata}, |
1966 | {"querystr", string_query}, | 1973 | {"querystr", string_query}, |
1967 | {"querytab", table_query}, | 1974 | {"querytab", table_query}, |