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:
ecrode 2019-07-30 13:55:29 -04:00 committed by GitHub
parent e72d502b45
commit 26cf0753ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 16 deletions

View File

@ -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;
}
?>
?>