Optimize code, decrease execution time
Code is taking advantage of unpack function to process the hash with one instruction 16 bits at a time.
This commit is contained in:
parent
e72d502b45
commit
26cf0753ad
|
|
@ -1,24 +1,17 @@
|
|||
<?php
|
||||
|
||||
function aprspass ($callsign) {
|
||||
$stophere = strpos($callsign, '-');
|
||||
if ($stophere) $callsign = substr($callsign, 0, $stophere);
|
||||
$realcall = strtoupper(substr($callsign, 0, 10));
|
||||
|
||||
// initialize hash
|
||||
$hash = 0x73e2;
|
||||
$i = 0;
|
||||
$len = strlen($realcall);
|
||||
|
||||
// hash callsign two bytes at a time
|
||||
while ($i < $len) {
|
||||
$hash ^= ord(substr($realcall, $i, 1))<<8;
|
||||
$hash ^= ord(substr($realcall, $i + 1, 1));
|
||||
$i += 2;
|
||||
}
|
||||
// drop SSID and use only first 10 characters
|
||||
$call = strtoupper(substr(strtok($callsign,'-'), 0, 10));
|
||||
|
||||
// make sure station len is even, convert and explode the data
|
||||
$parts = unpack("n*",strlen($call)%2?"$call\x00":$call);
|
||||
|
||||
// initiate the hash and process the data
|
||||
$hash = 0x73e2; foreach($parts as $part) $hash ^= $part;
|
||||
|
||||
// mask off the high bit so number is always positive
|
||||
return $hash & 0x7fff;
|
||||
}
|
||||
|
||||
?>
|
||||
?>
|
||||
|
|
|
|||
Loading…
Reference in New Issue