From 074dc8c05fd46fcafc960d6b3833ab0ca00ca5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Sat, 31 Dec 2011 02:09:12 +0000 Subject: [PATCH] [INTERPRETER] * BUG: The Internet condom had an hole. Use a better one. git-svn-id: svn://localhost/gambas/trunk@4382 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- main/gbx/gbx_math.c | 17 ++++++++++++++++- main/share/gb_hash_temp.h | 38 ++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/main/gbx/gbx_math.c b/main/gbx/gbx_math.c index dd985f3cc..c68f5c8dd 100644 --- a/main/gbx/gbx_math.c +++ b/main/gbx/gbx_math.c @@ -260,9 +260,24 @@ long double modfl(long double x, long double *iptr) } #endif +static int nbits(uint n) +{ + uint c; + for (c = 0; n; c++) + n &= n - 1; + return c; +} + void MATH_init(void) { + uint seed; + randomize(FALSE, 0); + // Internet condom - HASH_seed ^= GFSR_random(); + do + seed = GFSR_random(); + while (nbits(seed) < 16); + + HASH_seed = seed; } diff --git a/main/share/gb_hash_temp.h b/main/share/gb_hash_temp.h index ec6034cb3..b5dbeec1c 100644 --- a/main/share/gb_hash_temp.h +++ b/main/share/gb_hash_temp.h @@ -77,7 +77,8 @@ static int spaced_primes_closest(int num) static uint key_hash_binary(const char *key, int len) { static const void *jump[] = { &&__LEN_0, &&__LEN_1, &&__LEN_2, &&__LEN_3, &&__LEN_4, &&__LEN_5, &&__LEN_6, &&__LEN_7, &&__LEN_8 }; - uint hash = HASH_seed ^ len; + uint seed = HASH_seed; + uint hash = seed ^ len; if (len > 8) { @@ -88,21 +89,21 @@ static uint key_hash_binary(const char *key, int len) goto *jump[len]; __LEN_8: - hash = hash * 101 + key[7]; + hash = hash * seed + key[7]; __LEN_7: - hash = hash * 101 + key[6]; + hash = hash * seed + key[6]; __LEN_6: - hash = hash * 101 + key[5]; + hash = hash * seed + key[5]; __LEN_5: - hash = hash * 101 + key[4]; + hash = hash * seed + key[4]; __LEN_4: - hash = hash * 101 + key[3]; + hash = hash * seed + key[3]; __LEN_3: - hash = hash * 101 + key[2]; + hash = hash * seed + key[2]; __LEN_2: - hash = hash * 101 + key[1]; + hash = hash * seed + key[1]; __LEN_1: - hash = hash * 101 + key[0]; + hash = hash * seed + key[0]; __LEN_0: return hash; @@ -111,7 +112,8 @@ __LEN_0: static uint key_hash_text(const char *key, int len) { static const void *jump[] = { &&__LEN_0, &&__LEN_1, &&__LEN_2, &&__LEN_3, &&__LEN_4, &&__LEN_5, &&__LEN_6, &&__LEN_7, &&__LEN_8 }; - uint hash = HASH_seed ^ len; + uint seed = HASH_seed; + uint hash = seed ^ len; if (len > 8) { @@ -122,21 +124,21 @@ static uint key_hash_text(const char *key, int len) goto *jump[len]; __LEN_8: - hash = hash * 101 + (key[7] & ~0x20); + hash = hash * seed + ((uint)key[7] & ~0x20U); __LEN_7: - hash = hash * 101 + (key[6] & ~0x20); + hash = hash * seed + ((uint)key[6] & ~0x20U); __LEN_6: - hash = hash * 101 + (key[5] & ~0x20); + hash = hash * seed + ((uint)key[5] & ~0x20U); __LEN_5: - hash = hash * 101 + (key[4] & ~0x20); + hash = hash * seed + ((uint)key[4] & ~0x20U); __LEN_4: - hash = hash * 101 + (key[3] & ~0x20); + hash = hash * seed + ((uint)key[3] & ~0x20U); __LEN_3: - hash = hash * 101 + (key[2] & ~0x20); + hash = hash * seed + ((uint)key[2] & ~0x20U); __LEN_2: - hash = hash * 101 + (key[1] & ~0x20); + hash = hash * seed + ((uint)key[1] & ~0x20U); __LEN_1: - hash = hash * 101 + (key[0] & ~0x20); + hash = hash * seed + ((uint)key[0] & ~0x20U); __LEN_0: return hash;