2010-11-19 6 views
2
1. static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength) 
    2. { 
    3.  register ulong hash = 5381; 
    4. 
    5.  /* variant with the hash unrolled eight times */ 
    6.  for (; nKeyLength >= 8; nKeyLength -= 8) { 
    7.   hash = ((hash << 5) + hash) + *arKey++; 
    8.   hash = ((hash << 5) + hash) + *arKey++; 
    9.   hash = ((hash << 5) + hash) + *arKey++; 
    10.   hash = ((hash << 5) + hash) + *arKey++; 
    11.   hash = ((hash << 5) + hash) + *arKey++; 
    12.   hash = ((hash << 5) + hash) + *arKey++; 
    13.   hash = ((hash << 5) + hash) + *arKey++; 
    14.   hash = ((hash << 5) + hash) + *arKey++; 
    15.  } 
    16.  switch (nKeyLength) { 
    17.   case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 
    18.   case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 
    19.   case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 
    20.   case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 
    21.   case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 
    22.   case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 
    23.   case 1: hash = ((hash << 5) + hash) + *arKey++; break; 
    24.   case 0: break; 
    25. EMPTY_SWITCH_DEFAULT_CASE() 
    26.  } 
    27.  return hash; 
    28. } 

Antwort

4

Alle Hash-Tabellen verwenden diesen Hash-Algorithmus; Hash-Tabellen in PHP werden beispielsweise verwendet, um Arrays und Symboltabellen zu implementieren.

Der Algorithmus, wie in der header gezeigt, ist DJBX33A (Daniel J. Bernstein, Times 33 mit Zusatz).

+0

So wird es nur intern von PHP verwendet, ohne eine solche API für PHP-Benutzer? – ajx

+1

Ich würde auch damit antworten, war mir aber nicht sicher und habe mich verirrt, nach Quellen zu suchen. Sieht so aus, als hättest du es bestätigt, +1 – BoltClock

+0

@ajx Soweit ich weiß gibt es keine API für User-Land. – Artefacto